1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
|
/*
* Copyright (C) 2010-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "Connection.h"
#include "Encoder.h"
#include "Logging.h"
#include "MessageFlags.h"
#include "MessageReceiveQueues.h"
#include "WorkQueueMessageReceiver.h"
#include <memory>
#include <wtf/ArgumentCoder.h>
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/ObjectIdentifier.h>
#include <wtf/RunLoop.h>
#include <wtf/Scope.h>
#include <wtf/WTFProcess.h>
#include <wtf/text/WTFString.h>
#include <wtf/threads/BinarySemaphore.h>
#if PLATFORM(COCOA)
#include "ArgumentCodersDarwin.h"
#include "MachMessage.h"
#endif
#if USE(UNIX_DOMAIN_SOCKETS)
#include "ArgumentCodersUnix.h"
#include "UnixMessage.h"
#endif
#if OS(WINDOWS)
#include "ArgumentCodersWin.h"
#endif
namespace IPC {
#if PLATFORM(COCOA)
// The IPC connection gets killed if the incoming message queue reaches 50000 messages before the main thread has a chance to dispatch them.
constexpr size_t maxPendingIncomingMessagesKillingThreshold { 50000 };
#endif
constexpr size_t largeOutgoingMessageQueueCountThreshold { 1024 };
constexpr Seconds largeOutgoingMessageQueueTimeThreshold { 30_s };
std::atomic<unsigned> UnboundedSynchronousIPCScope::unboundedSynchronousIPCCount = 0;
Lock Connection::s_connectionMapLock;
struct Connection::WaitForMessageState {
WaitForMessageState(MessageName messageName, uint64_t destinationID, OptionSet<WaitForOption> waitForOptions)
: messageName(messageName)
, destinationID(destinationID)
, waitForOptions(waitForOptions)
{
}
MessageName messageName;
uint64_t destinationID;
OptionSet<WaitForOption> waitForOptions;
bool messageWaitingInterrupted = false;
std::unique_ptr<Decoder> decoder;
};
class Connection::SyncMessageState {
public:
static std::unique_ptr<SyncMessageState, SyncMessageStateRelease> get(SerialFunctionDispatcher&);
SerialFunctionDispatcher& dispatcher() { return m_dispatcher; }
void wakeUpClientRunLoop()
{
m_waitForSyncReplySemaphore.signal();
}
bool wait(Timeout timeout)
{
return m_waitForSyncReplySemaphore.waitUntil(timeout.deadline());
}
// Returns true if this message will be handled on a client thread that is currently
// waiting for a reply to a synchronous message.
bool processIncomingMessage(Connection& connection, std::unique_ptr<Decoder>&) WTF_REQUIRES_LOCK(connection.m_incomingMessagesLock);
// Dispatch pending sync messages.
void dispatchMessages(Function<void(MessageName, uint64_t)>&& willDispatchMessage = { });
// Add matching pending messages to the provided MessageReceiveQueue.
void enqueueMatchingMessages(Connection&, MessageReceiveQueue&, const ReceiverMatcher&);
// Dispatch pending sync messages for given connection.
void dispatchMessagesAndResetDidScheduleDispatchMessagesForConnection(Connection&);
private:
explicit SyncMessageState(SerialFunctionDispatcher& dispatcher)
: m_dispatcher(dispatcher)
{
}
static Lock syncMessageStateMapLock;
static HashMap<SerialFunctionDispatcher*, SyncMessageState*>& syncMessageStateMap() WTF_REQUIRES_LOCK(syncMessageStateMapLock)
{
static NeverDestroyed<HashMap<SerialFunctionDispatcher*, SyncMessageState*>> map;
return map;
}
BinarySemaphore m_waitForSyncReplySemaphore;
// Protects m_didScheduleDispatchMessagesWorkSet and m_messagesToDispatchWhileWaitingForSyncReply.
Lock m_lock;
// The set of connections for which we've scheduled a call to dispatchMessageAndResetDidScheduleDispatchMessagesForConnection.
HashSet<RefPtr<Connection>> m_didScheduleDispatchMessagesWorkSet WTF_GUARDED_BY_LOCK(m_lock);
struct ConnectionAndIncomingMessage {
Ref<Connection> connection;
std::unique_ptr<Decoder> message;
void dispatch()
{
connection->dispatchMessage(WTFMove(message));
}
};
Deque<ConnectionAndIncomingMessage> m_messagesBeingDispatched; // Only used on the main thread.
Deque<ConnectionAndIncomingMessage> m_messagesToDispatchWhileWaitingForSyncReply WTF_GUARDED_BY_LOCK(m_lock);
SerialFunctionDispatcher& m_dispatcher;
unsigned m_clients WTF_GUARDED_BY_LOCK(syncMessageStateMapLock) { 0 };
friend struct Connection::SyncMessageStateRelease;
};
Lock Connection::SyncMessageState::syncMessageStateMapLock;
std::unique_ptr<Connection::SyncMessageState, Connection::SyncMessageStateRelease> Connection::SyncMessageState::get(SerialFunctionDispatcher& dispatcher)
{
Locker locker { syncMessageStateMapLock };
auto result = syncMessageStateMap().ensure(&dispatcher, [&dispatcher] { return new SyncMessageState { dispatcher }; }); // NOLINT.
auto* state = result.iterator->value;
state->m_clients++;
return { state, Connection::SyncMessageStateRelease { } };
}
void Connection::SyncMessageStateRelease::operator()(SyncMessageState* instance) const
{
if (!instance)
return;
{
Locker locker { Connection::SyncMessageState::syncMessageStateMapLock };
--instance->m_clients;
if (instance->m_clients)
return;
Connection::SyncMessageState::syncMessageStateMap().remove(&instance->m_dispatcher);
}
delete instance;
}
void Connection::SyncMessageState::enqueueMatchingMessages(Connection& connection, MessageReceiveQueue& receiveQueue, const ReceiverMatcher& receiverMatcher)
{
assertIsCurrent(m_dispatcher);
auto enqueueMatchingMessagesInContainer = [&](Deque<ConnectionAndIncomingMessage>& connectionAndMessages) {
Deque<ConnectionAndIncomingMessage> rest;
for (auto& connectionAndMessage : connectionAndMessages) {
if (connectionAndMessage.connection.ptr() == &connection && connectionAndMessage.message->matches(receiverMatcher))
receiveQueue.enqueueMessage(connection, WTFMove(connectionAndMessage.message));
else
rest.append(WTFMove(connectionAndMessage));
}
connectionAndMessages = WTFMove(rest);
};
Locker locker { m_lock };
enqueueMatchingMessagesInContainer(m_messagesBeingDispatched);
enqueueMatchingMessagesInContainer(m_messagesToDispatchWhileWaitingForSyncReply);
}
bool Connection::SyncMessageState::processIncomingMessage(Connection& connection, std::unique_ptr<Decoder>& message)
{
switch (message->shouldDispatchMessageWhenWaitingForSyncReply()) {
case ShouldDispatchWhenWaitingForSyncReply::No:
return false;
case ShouldDispatchWhenWaitingForSyncReply::YesDuringUnboundedIPC:
if (!UnboundedSynchronousIPCScope::hasOngoingUnboundedSyncIPC())
return false;
break;
case ShouldDispatchWhenWaitingForSyncReply::Yes:
break;
}
bool shouldDispatch;
{
Locker locker { m_lock };
shouldDispatch = m_didScheduleDispatchMessagesWorkSet.add(&connection).isNewEntry;
ASSERT(connection.m_incomingMessagesLock.isHeld());
if (message->shouldMaintainOrderingWithAsyncMessages()) {
// This sync message should maintain ordering with async messages so we need to process the pending async messages first.
while (!connection.m_incomingMessages.isEmpty())
m_messagesToDispatchWhileWaitingForSyncReply.append(ConnectionAndIncomingMessage { connection, connection.m_incomingMessages.takeFirst() });
}
m_messagesToDispatchWhileWaitingForSyncReply.append(ConnectionAndIncomingMessage { connection, WTFMove(message) });
}
if (shouldDispatch) {
m_dispatcher.dispatch([protectedConnection = Ref { connection }]() mutable {
protectedConnection->dispatchSyncStateMessages();
});
}
wakeUpClientRunLoop();
return true;
}
void Connection::SyncMessageState::dispatchMessages(Function<void(MessageName, uint64_t)>&& willDispatchMessage)
{
assertIsCurrent(m_dispatcher);
{
Locker locker { m_lock };
if (m_messagesBeingDispatched.isEmpty())
m_messagesBeingDispatched = std::exchange(m_messagesToDispatchWhileWaitingForSyncReply, { });
else {
while (!m_messagesToDispatchWhileWaitingForSyncReply.isEmpty())
m_messagesBeingDispatched.append(m_messagesToDispatchWhileWaitingForSyncReply.takeLast());
}
}
while (!m_messagesBeingDispatched.isEmpty()) {
auto messageToDispatch = m_messagesBeingDispatched.takeFirst();
if (willDispatchMessage)
willDispatchMessage(messageToDispatch.message->messageName(), messageToDispatch.message->destinationID());
messageToDispatch.dispatch();
}
}
void Connection::SyncMessageState::dispatchMessagesAndResetDidScheduleDispatchMessagesForConnection(Connection& connection)
{
assertIsCurrent(m_dispatcher);
{
Locker locker { m_lock };
ASSERT(m_didScheduleDispatchMessagesWorkSet.contains(&connection));
m_didScheduleDispatchMessagesWorkSet.remove(&connection);
Deque<ConnectionAndIncomingMessage> messagesToPutBack;
for (auto& connectionAndIncomingMessage : m_messagesToDispatchWhileWaitingForSyncReply) {
if (&connection == connectionAndIncomingMessage.connection.ptr())
m_messagesBeingDispatched.append(WTFMove(connectionAndIncomingMessage));
else
messagesToPutBack.append(WTFMove(connectionAndIncomingMessage));
}
m_messagesToDispatchWhileWaitingForSyncReply = WTFMove(messagesToPutBack);
}
while (!m_messagesBeingDispatched.isEmpty())
m_messagesBeingDispatched.takeFirst().dispatch(); // This may cause the function to re-enter when there is a nested run loop.
}
// Represents a sync request for which we're waiting on a reply.
struct Connection::PendingSyncReply {
// The request ID.
Connection::SyncRequestID syncRequestID;
// The reply decoder, will be null if there was an error processing the sync
// message on the other side.
std::unique_ptr<Decoder> replyDecoder;
// Will be set to true once a reply has been received.
bool didReceiveReply { false };
PendingSyncReply() = default;
explicit PendingSyncReply(Connection::SyncRequestID syncRequestID)
: syncRequestID(syncRequestID)
{
}
};
Ref<Connection> Connection::createServerConnection(Identifier identifier)
{
return adoptRef(*new Connection(identifier, true));
}
Ref<Connection> Connection::createClientConnection(Identifier identifier)
{
return adoptRef(*new Connection(identifier, false));
}
HashMap<IPC::Connection::UniqueID, Connection*>& Connection::connectionMap()
{
static NeverDestroyed<HashMap<IPC::Connection::UniqueID, Connection*>> map;
return map;
}
Connection::Connection(Identifier identifier, bool isServer)
: m_uniqueID(UniqueID::generate())
, m_isServer(isServer)
, m_connectionQueue(WorkQueue::create("com.apple.IPC.ReceiveQueue"))
{
{
Locker locker { s_connectionMapLock };
connectionMap().add(m_uniqueID, this);
}
platformInitialize(identifier);
}
Connection::~Connection()
{
ASSERT(!isValid());
{
Locker locker { s_connectionMapLock };
connectionMap().remove(m_uniqueID);
}
cancelAsyncReplyHandlers();
}
RefPtr<Connection> Connection::connection(UniqueID uniqueID)
{
// FIXME(https://bugs.webkit.org/show_bug.cgi?id=238493): Removing with lock in destructor is not thread-safe.
Locker locker { s_connectionMapLock };
return connectionMap().get(uniqueID);
}
void Connection::setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(bool flag)
{
ASSERT(!m_isConnected);
m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage = flag;
}
void Connection::setShouldExitOnSyncMessageSendFailure(bool shouldExitOnSyncMessageSendFailure)
{
ASSERT(!m_isConnected);
m_shouldExitOnSyncMessageSendFailure = shouldExitOnSyncMessageSendFailure;
}
// Enqueue any pending message to the MessageReceiveQueue that is meant to go on that queue. This is important to maintain the ordering of
// IPC messages as some messages may get received on the IPC thread before the message receiver registered itself on the main thread.
void Connection::enqueueMatchingMessagesToMessageReceiveQueue(MessageReceiveQueue& receiveQueue, const ReceiverMatcher& receiverMatcher)
{
if (!isValid())
return;
// FIXME: m_isValid starts as true. It will be switched to start as false and toggled as true on
// open. For the time being, check for m_syncState.
if (m_syncState)
m_syncState->enqueueMatchingMessages(*this, receiveQueue, receiverMatcher);
Deque<std::unique_ptr<Decoder>> remainingIncomingMessages;
for (auto& message : m_incomingMessages) {
if (message->matches(receiverMatcher))
receiveQueue.enqueueMessage(*this, WTFMove(message));
else
remainingIncomingMessages.append(WTFMove(message));
}
m_incomingMessages = WTFMove(remainingIncomingMessages);
}
void Connection::addMessageReceiveQueue(MessageReceiveQueue& receiveQueue, const ReceiverMatcher& receiverMatcher)
{
Locker incomingMessagesLocker { m_incomingMessagesLock };
enqueueMatchingMessagesToMessageReceiveQueue(receiveQueue, receiverMatcher);
m_receiveQueues.add(receiveQueue, receiverMatcher);
}
void Connection::removeMessageReceiveQueue(const ReceiverMatcher& receiverMatcher)
{
Locker locker { m_incomingMessagesLock };
m_receiveQueues.remove(receiverMatcher);
}
void Connection::addWorkQueueMessageReceiver(ReceiverName receiverName, WorkQueue& workQueue, WorkQueueMessageReceiver& receiver, uint64_t destinationID)
{
auto receiverMatcher = ReceiverMatcher::createWithZeroAsAnyDestination(receiverName, destinationID);
auto receiveQueue = makeUnique<WorkQueueMessageReceiverQueue>(workQueue, receiver);
Locker incomingMessagesLocker { m_incomingMessagesLock };
enqueueMatchingMessagesToMessageReceiveQueue(*receiveQueue, receiverMatcher);
m_receiveQueues.add(WTFMove(receiveQueue), receiverMatcher);
}
void Connection::removeWorkQueueMessageReceiver(ReceiverName receiverName, uint64_t destinationID)
{
removeMessageReceiveQueue(ReceiverMatcher::createWithZeroAsAnyDestination(receiverName, destinationID));
}
void Connection::addMessageReceiver(FunctionDispatcher& dispatcher, MessageReceiver& receiver, ReceiverName receiverName, uint64_t destinationID)
{
auto receiverMatcher = ReceiverMatcher::createWithZeroAsAnyDestination(receiverName, destinationID);
auto receiveQueue = makeUnique<FunctionDispatcherQueue>(dispatcher, receiver);
Locker incomingMessagesLocker { m_incomingMessagesLock };
enqueueMatchingMessagesToMessageReceiveQueue(*receiveQueue, receiverMatcher);
m_receiveQueues.add(WTFMove(receiveQueue), receiverMatcher);
}
void Connection::removeMessageReceiver(ReceiverName receiverName, uint64_t destinationID)
{
removeMessageReceiveQueue(ReceiverMatcher::createWithZeroAsAnyDestination(receiverName, destinationID));
}
void Connection::dispatchMessageReceiverMessage(MessageReceiver& messageReceiver, std::unique_ptr<Decoder>&& decoder)
{
if (!decoder->isSyncMessage()) {
messageReceiver.didReceiveMessage(*this, *decoder);
return;
}
SyncRequestID syncRequestID;
if (UNLIKELY(!decoder->decode(syncRequestID))) {
// We received an invalid sync message.
// FIXME: Handle this.
return;
}
auto replyEncoder = makeUniqueRef<Encoder>(MessageName::SyncMessageReply, syncRequestID.toUInt64());
// Hand off both the decoder and encoder to the work queue message receiver.
bool wasHandled = messageReceiver.didReceiveSyncMessage(*this, *decoder, replyEncoder);
// FIXME: If the message was invalid, we should send back a SyncMessageError.
ASSERT(decoder->isValid());
if (!wasHandled)
sendSyncReply(WTFMove(replyEncoder));
}
void Connection::setDidCloseOnConnectionWorkQueueCallback(DidCloseOnConnectionWorkQueueCallback callback)
{
ASSERT(!m_isConnected);
m_didCloseOnConnectionWorkQueueCallback = callback;
}
void Connection::setOutgoingMessageQueueIsGrowingLargeCallback(OutgoingMessageQueueIsGrowingLargeCallback&& callback)
{
m_outgoingMessageQueueIsGrowingLargeCallback = WTFMove(callback);
}
bool Connection::open(Client& client, SerialFunctionDispatcher& dispatcher)
{
ASSERT(!m_client);
if (!platformPrepareForOpen())
return false;
m_client = &client;
m_syncState = SyncMessageState::get(dispatcher);
platformOpen();
return true;
}
#if !USE(UNIX_DOMAIN_SOCKETS)
bool Connection::platformPrepareForOpen()
{
return true;
}
#endif
void Connection::invalidate()
{
m_isValid = false;
if (!m_client)
return;
assertIsCurrent(dispatcher());
m_client = nullptr;
m_outgoingMessageQueueIsGrowingLargeCallback = nullptr;
[this] {
Locker locker { m_incomingMessagesLock };
return WTFMove(m_syncState);
}();
cancelAsyncReplyHandlers();
m_connectionQueue->dispatch([protectedThis = Ref { *this }]() mutable {
protectedThis->platformInvalidate();
});
}
void Connection::markCurrentlyDispatchedMessageAsInvalid()
{
// This should only be called while processing a message.
ASSERT(m_inDispatchMessageCount > 0);
m_didReceiveInvalidMessage = true;
}
UniqueRef<Encoder> Connection::createSyncMessageEncoder(MessageName messageName, uint64_t destinationID, SyncRequestID& syncRequestID)
{
auto encoder = makeUniqueRef<Encoder>(messageName, destinationID);
// Encode the sync request ID.
syncRequestID = makeSyncRequestID();
encoder.get() << syncRequestID;
return encoder;
}
Error Connection::sendMessage(UniqueRef<Encoder>&& encoder, OptionSet<SendOption> sendOptions, std::optional<Thread::QOS> qos)
{
if (!isValid())
return Error::InvalidConnection;
#if ENABLE(IPC_TESTING_API)
if (isMainRunLoop()) {
bool hasDeadObservers = false;
for (auto& observerWeakPtr : m_messageObservers) {
if (auto* observer = observerWeakPtr.get())
observer->willSendMessage(encoder.get(), sendOptions);
else
hasDeadObservers = true;
}
if (hasDeadObservers)
m_messageObservers.removeAllMatching([](auto& observer) { return !observer; });
}
#endif
if (isMainRunLoop() && m_inDispatchMessageMarkedToUseFullySynchronousModeForTesting && !encoder->isSyncMessage() && !(encoder->messageReceiverName() == ReceiverName::IPC) && !sendOptions.contains(SendOption::IgnoreFullySynchronousMode)) {
SyncRequestID syncRequestID;
auto wrappedMessage = createSyncMessageEncoder(MessageName::WrappedAsyncMessageForTesting, encoder->destinationID(), syncRequestID);
wrappedMessage->setFullySynchronousModeForTesting();
wrappedMessage->wrapForTesting(WTFMove(encoder));
return sendSyncMessage(syncRequestID, WTFMove(wrappedMessage), Timeout::infinity(), { }).error;
}
#if ENABLE(IPC_TESTING_API)
if (!sendOptions.contains(SendOption::IPCTestingMessage)) {
#endif
if (sendOptions.contains(SendOption::DispatchMessageEvenWhenWaitingForSyncReply))
ASSERT(encoder->isAllowedWhenWaitingForSyncReply());
else if (sendOptions.contains(SendOption::DispatchMessageEvenWhenWaitingForUnboundedSyncReply))
ASSERT(encoder->isAllowedWhenWaitingForUnboundedSyncReply());
else
ASSERT(!encoder->isAllowedWhenWaitingForSyncReply() && !encoder->isAllowedWhenWaitingForUnboundedSyncReply());
#if ENABLE(IPC_TESTING_API)
}
#endif
if (sendOptions.contains(SendOption::DispatchMessageEvenWhenWaitingForSyncReply)
&& (!m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage
|| m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount))
encoder->setShouldDispatchMessageWhenWaitingForSyncReply(ShouldDispatchWhenWaitingForSyncReply::Yes);
else if (sendOptions.contains(SendOption::DispatchMessageEvenWhenWaitingForUnboundedSyncReply))
encoder->setShouldDispatchMessageWhenWaitingForSyncReply(ShouldDispatchWhenWaitingForSyncReply::YesDuringUnboundedIPC);
size_t outgoingMessagesCount;
bool shouldNotifyOfQueueGrowingLarge;
bool shouldDispatchMessageSend;
{
Locker locker { m_outgoingMessagesLock };
shouldDispatchMessageSend = m_outgoingMessages.isEmpty();
m_outgoingMessages.append(WTFMove(encoder));
outgoingMessagesCount = m_outgoingMessages.size();
shouldNotifyOfQueueGrowingLarge = m_outgoingMessageQueueIsGrowingLargeCallback && outgoingMessagesCount > largeOutgoingMessageQueueCountThreshold && (MonotonicTime::now() - m_lastOutgoingMessageQueueIsGrowingLargeCallbackCallTime) >= largeOutgoingMessageQueueTimeThreshold;
if (shouldNotifyOfQueueGrowingLarge)
m_lastOutgoingMessageQueueIsGrowingLargeCallbackCallTime = MonotonicTime::now();
}
if (shouldNotifyOfQueueGrowingLarge) {
#if OS(DARWIN)
RELEASE_LOG_ERROR(IPC, "Connection::sendMessage(): Too many messages (%zu) in the queue to remote PID: %d, notifying client", outgoingMessagesCount, remoteProcessID());
#else
RELEASE_LOG_ERROR(IPC, "Connection::sendMessage(): Too many messages (%zu) in the queue, notifying client", outgoingMessagesCount);
#endif
m_outgoingMessageQueueIsGrowingLargeCallback();
}
// It's not clear if calling dispatchWithQOS() will do anything if Connection::sendOutgoingMessages() is already running.
if (shouldDispatchMessageSend || qos) {
auto sendOutgoingMessages = [protectedThis = Ref { *this }]() mutable {
protectedThis->sendOutgoingMessages();
};
if (qos)
m_connectionQueue->dispatchWithQOS(WTFMove(sendOutgoingMessages), *qos);
else
m_connectionQueue->dispatch(WTFMove(sendOutgoingMessages));
}
return Error::NoError;
}
Error Connection::sendMessageWithAsyncReply(UniqueRef<Encoder>&& encoder, AsyncReplyHandler replyHandler, OptionSet<SendOption> sendOptions, std::optional<Thread::QOS> qos)
{
ASSERT(replyHandler.replyID);
ASSERT(replyHandler.completionHandler);
auto replyID = replyHandler.replyID;
encoder.get() << replyID;
addAsyncReplyHandler(WTFMove(replyHandler));
auto error = sendMessage(WTFMove(encoder), sendOptions, qos);
if (error == Error::NoError)
return Error::NoError;
// replyHandlerToCancel might be already cancelled if invalidate() happened in-between.
if (auto replyHandlerToCancel = takeAsyncReplyHandler(replyID)) {
// FIXME: Current contract is that completionHandler is called on the connection run loop.
// This does not make sense. However, this needs a change that is done later.
RunLoop::main().dispatch([completionHandler = WTFMove(replyHandlerToCancel)]() mutable {
completionHandler(nullptr);
});
}
return error;
}
Error Connection::sendSyncReply(UniqueRef<Encoder>&& encoder)
{
return sendMessage(WTFMove(encoder), { });
}
Timeout Connection::timeoutRespectingIgnoreTimeoutsForTesting(Timeout timeout) const
{
return m_ignoreTimeoutsForTesting ? Timeout::infinity() : timeout;
}
auto Connection::waitForMessage(MessageName messageName, uint64_t destinationID, Timeout timeout, OptionSet<WaitForOption> waitForOptions) -> DecoderOrError
{
if (!isValid())
return Error::InvalidConnection;
assertIsCurrent(dispatcher());
Ref protectedThis { *this };
timeout = timeoutRespectingIgnoreTimeoutsForTesting(timeout);
WaitForMessageState waitingForMessage(messageName, destinationID, waitForOptions);
{
Locker locker { m_waitForMessageLock };
// We don't support having multiple clients waiting for messages.
ASSERT(!m_waitingForMessage);
if (m_waitingForMessage)
return Error::MultipleWaitingClients;
// If the connection is already invalidated, don't even start waiting.
// Once m_waitingForMessage is set, messageWaitingInterrupted will cover this instead.
if (!m_shouldWaitForMessages)
return Error::AttemptingToWaitOnClosedConnection;
bool hasIncomingSynchronousMessage = false;
// First, check if this message is already in the incoming messages queue.
{
Locker locker { m_incomingMessagesLock };
for (auto it = m_incomingMessages.begin(), end = m_incomingMessages.end(); it != end; ++it) {
std::unique_ptr<Decoder>& message = *it;
if (message->messageName() == messageName && message->destinationID() == destinationID) {
std::unique_ptr<Decoder> returnedMessage = WTFMove(message);
m_incomingMessages.remove(it);
return { WTFMove(returnedMessage) };
}
if (message->isSyncMessage())
hasIncomingSynchronousMessage = true;
}
}
// Don't even start waiting if we have InterruptWaitingIfSyncMessageArrives and there's a sync message already in the queue.
if (hasIncomingSynchronousMessage && waitForOptions.contains(WaitForOption::InterruptWaitingIfSyncMessageArrives))
return { Error::SyncMessageInterruptedWait };
m_waitingForMessage = &waitingForMessage;
}
// Now wait for it to be set.
while (true) {
// Handle any messages that are blocked on a response from us.
bool wasMessageToWaitForAlreadyDispatched = false;
m_syncState->dispatchMessages([&](auto nameOfMessageToDispatch, uint64_t destinationOfMessageToDispatch) {
wasMessageToWaitForAlreadyDispatched |= messageName == nameOfMessageToDispatch && destinationID == destinationOfMessageToDispatch;
});
Locker locker { m_waitForMessageLock };
if (wasMessageToWaitForAlreadyDispatched) {
m_waitingForMessage = nullptr;
return { Error::WaitingOnAlreadyDispatchedMessage };
}
if (UNLIKELY(m_inDispatchSyncMessageCount && !timeout.isInfinity())) {
RELEASE_LOG_ERROR(IPC, "Connection::waitForMessage(%" PUBLIC_LOG_STRING "): Exiting immediately, since we're handling a sync message already", description(messageName));
m_waitingForMessage = nullptr;
return { Error::AttemptingToWaitInsideSyncMessageHandling };
}
if (m_waitingForMessage->decoder) {
auto decoder = WTFMove(m_waitingForMessage->decoder);
m_waitingForMessage = nullptr;
return { WTFMove(decoder) };
}
if (!isValid()) {
m_waitingForMessage = nullptr;
return Error::InvalidConnection;
}
bool didTimeout = !m_waitForMessageCondition.waitUntil(m_waitForMessageLock, timeout.deadline());
if (didTimeout) {
m_waitingForMessage = nullptr;
return Error::Timeout;
}
if (m_waitingForMessage->messageWaitingInterrupted) {
m_waitingForMessage = nullptr;
return Error::SyncMessageInterruptedWait;
}
}
return Error::Unspecified;
}
bool Connection::pushPendingSyncRequestID(SyncRequestID syncRequestID)
{
{
Locker locker { m_syncReplyStateLock };
if (!m_shouldWaitForSyncReplies)
return false;
m_pendingSyncReplies.append(PendingSyncReply(syncRequestID));
}
++m_inSendSyncCount;
return true;
}
void Connection::popPendingSyncRequestID(SyncRequestID syncRequestID)
{
--m_inSendSyncCount;
Locker locker { m_syncReplyStateLock };
ASSERT_UNUSED(syncRequestID, m_pendingSyncReplies.last().syncRequestID == syncRequestID);
m_pendingSyncReplies.removeLast();
}
auto Connection::sendSyncMessage(SyncRequestID syncRequestID, UniqueRef<Encoder>&& encoder, Timeout timeout, OptionSet<SendSyncOption> sendSyncOptions) -> DecoderOrError
{
ASSERT(syncRequestID);
if (!isValid()) {
didFailToSendSyncMessage(Error::InvalidConnection);
return Error::InvalidConnection;
}
assertIsCurrent(dispatcher());
if (!pushPendingSyncRequestID(syncRequestID)) {
didFailToSendSyncMessage(Error::CantWaitForSyncReplies);
return { Error::CantWaitForSyncReplies };
}
// First send the message.
OptionSet<SendOption> sendOptions = IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply;
if (sendSyncOptions.contains(SendSyncOption::ForceDispatchWhenDestinationIsWaitingForUnboundedSyncReply))
sendOptions = sendOptions | IPC::SendOption::DispatchMessageEvenWhenWaitingForUnboundedSyncReply;
if (sendSyncOptions.contains(IPC::SendSyncOption::MaintainOrderingWithAsyncMessages))
encoder->setShouldMaintainOrderingWithAsyncMessages();
auto messageName = encoder->messageName();
// Since sync IPC is blocking the current thread, make sure we use the same priority for the IPC sending thread
// as the current thread.
sendMessage(WTFMove(encoder), sendOptions, Thread::currentThreadQOS());
// Then wait for a reply. Waiting for a reply could involve dispatching incoming sync messages, so
// keep an extra reference to the connection here in case it's invalidated.
Ref<Connection> protect(*this);
auto replyOrError = waitForSyncReply(syncRequestID, messageName, timeout, sendSyncOptions);
popPendingSyncRequestID(syncRequestID);
if (!replyOrError.decoder) {
if (replyOrError.error == Error::NoError)
replyOrError.error = Error::Unspecified;
didFailToSendSyncMessage(replyOrError.error);
}
return replyOrError;
}
auto Connection::waitForSyncReply(SyncRequestID syncRequestID, MessageName messageName, Timeout timeout, OptionSet<SendSyncOption> sendSyncOptions) -> DecoderOrError
{
timeout = timeoutRespectingIgnoreTimeoutsForTesting(timeout);
willSendSyncMessage(sendSyncOptions);
bool timedOut = false;
while (!timedOut) {
// First, check if we have any messages that we need to process.
m_syncState->dispatchMessages();
{
Locker locker { m_syncReplyStateLock };
// Second, check if there is a sync reply at the top of the stack.
ASSERT(!m_pendingSyncReplies.isEmpty());
auto& pendingSyncReply = m_pendingSyncReplies.last();
ASSERT_UNUSED(syncRequestID, pendingSyncReply.syncRequestID == syncRequestID);
// We found the sync reply.
if (pendingSyncReply.didReceiveReply) {
didReceiveSyncReply(sendSyncOptions);
return { WTFMove(pendingSyncReply.replyDecoder) };
}
// The connection was closed.
if (!m_shouldWaitForSyncReplies) {
didReceiveSyncReply(sendSyncOptions);
return Error::InvalidConnection;
}
}
// Processing a sync message could cause the connection to be invalidated.
// (If the handler ends up calling Connection::invalidate).
// If that happens, we need to stop waiting, or we'll hang since we won't get
// any more incoming messages.
if (!isValid()) {
RELEASE_LOG_ERROR(IPC, "Connection::waitForSyncReply: Connection no longer valid, id=%" PRIu64, syncRequestID.toUInt64());
didReceiveSyncReply(sendSyncOptions);
return Error::InvalidConnection;
}
// We didn't find a sync reply yet, keep waiting.
// This allows the WebProcess to still serve clients while waiting for the message to return.
// Notably, it can continue to process accessibility requests, which are on the main thread.
timedOut = !m_syncState->wait(timeout);
}
#if OS(DARWIN)
RELEASE_LOG_ERROR(IPC, "Connection::waitForSyncReply: Timed-out while waiting for reply for %" PUBLIC_LOG_STRING " from process %d, id=%" PRIu64, description(messageName), remoteProcessID(), syncRequestID.toUInt64());
#else
RELEASE_LOG_ERROR(IPC, "Connection::waitForSyncReply: Timed-out while waiting for reply for %s, id=%" PRIu64, description(messageName), syncRequestID.toUInt64());
#endif
didReceiveSyncReply(sendSyncOptions);
return Error::Timeout;
}
void Connection::processIncomingSyncReply(std::unique_ptr<Decoder> decoder)
{
{
Locker locker { m_syncReplyStateLock };
// Go through the stack of sync requests that have pending replies and see which one
// this reply is for.
for (size_t i = m_pendingSyncReplies.size(); i > 0; --i) {
PendingSyncReply& pendingSyncReply = m_pendingSyncReplies[i - 1];
if (pendingSyncReply.syncRequestID.toUInt64() != decoder->destinationID())
continue;
ASSERT(!pendingSyncReply.replyDecoder);
pendingSyncReply.replyDecoder = WTFMove(decoder);
pendingSyncReply.didReceiveReply = true;
// We got a reply to the last send message, wake up the client run loop so it can be processed.
if (i == m_pendingSyncReplies.size()) {
Locker locker { m_incomingMessagesLock };
if (m_syncState)
m_syncState->wakeUpClientRunLoop();
}
return;
}
}
// If we get here, it means we got a reply for a message that wasn't in the sync request stack or map.
// This can happen if the send timed out, so it's fine to ignore.
}
void Connection::processIncomingMessage(std::unique_ptr<Decoder> message)
{
ASSERT(message->messageReceiverName() != ReceiverName::Invalid);
if (message->messageName() == MessageName::SyncMessageReply) {
processIncomingSyncReply(WTFMove(message));
return;
}
if (!MessageReceiveQueueMap::isValidMessage(*message)) {
dispatchDidReceiveInvalidMessage(message->messageName());
return;
}
// FIXME: These are practically the same mutex, so maybe they could be merged.
Locker waitForMessagesLocker { m_waitForMessageLock };
Locker incomingMessagesLocker { m_incomingMessagesLock };
if (!m_syncState)
return;
if (auto* receiveQueue = m_receiveQueues.get(*message)) {
receiveQueue->enqueueMessage(*this, WTFMove(message));
return;
}
if (message->isSyncMessage()) {
Locker locker { m_incomingSyncMessageCallbackLock };
for (auto& callback : m_incomingSyncMessageCallbacks.values())
m_incomingSyncMessageCallbackQueue->dispatch(WTFMove(callback));
m_incomingSyncMessageCallbacks.clear();
}
// Check if we're waiting for this message, or if we need to interrupt waiting due to an incoming sync message.
if (m_waitingForMessage && !m_waitingForMessage->decoder) {
if (m_waitingForMessage->messageName == message->messageName() && m_waitingForMessage->destinationID == message->destinationID()) {
m_waitingForMessage->decoder = WTFMove(message);
ASSERT(m_waitingForMessage->decoder);
m_waitForMessageCondition.notifyOne();
return;
}
if (m_waitingForMessage->waitForOptions.contains(WaitForOption::DispatchIncomingSyncMessagesWhileWaiting) && message->isSyncMessage() && m_syncState->processIncomingMessage(*this, message)) {
m_waitForMessageCondition.notifyOne();
return;
}
if (m_waitingForMessage->waitForOptions.contains(WaitForOption::InterruptWaitingIfSyncMessageArrives) && message->isSyncMessage()) {
m_waitingForMessage->messageWaitingInterrupted = true;
m_waitForMessageCondition.notifyOne();
enqueueIncomingMessage(WTFMove(message));
return;
}
}
if ((message->shouldDispatchMessageWhenWaitingForSyncReply() == ShouldDispatchWhenWaitingForSyncReply::YesDuringUnboundedIPC && !message->isAllowedWhenWaitingForUnboundedSyncReply()) || (message->shouldDispatchMessageWhenWaitingForSyncReply() == ShouldDispatchWhenWaitingForSyncReply::Yes && !message->isAllowedWhenWaitingForSyncReply())) {
dispatchDidReceiveInvalidMessage(message->messageName());
return;
}
// Check if this is a sync message or if it's a message that should be dispatched even when waiting for
// a sync reply. If it is, and we're waiting for a sync reply this message needs to be dispatched.
// If we don't we'll end up with a deadlock where both sync message senders are stuck waiting for a reply.
if (m_syncState->processIncomingMessage(*this, message))
return;
enqueueIncomingMessage(WTFMove(message));
}
uint64_t Connection::installIncomingSyncMessageCallback(WTF::Function<void ()>&& callback)
{
Locker locker { m_incomingSyncMessageCallbackLock };
m_nextIncomingSyncMessageCallbackID++;
if (!m_incomingSyncMessageCallbackQueue)
m_incomingSyncMessageCallbackQueue = WorkQueue::create("com.apple.WebKit.IPC.IncomingSyncMessageCallbackQueue");
m_incomingSyncMessageCallbacks.add(m_nextIncomingSyncMessageCallbackID, WTFMove(callback));
return m_nextIncomingSyncMessageCallbackID;
}
void Connection::uninstallIncomingSyncMessageCallback(uint64_t callbackID)
{
Locker locker { m_incomingSyncMessageCallbackLock };
m_incomingSyncMessageCallbacks.remove(callbackID);
}
bool Connection::hasIncomingSyncMessage()
{
Locker locker { m_incomingMessagesLock };
for (auto& message : m_incomingMessages) {
if (message->isSyncMessage())
return true;
}
return false;
}
void Connection::enableIncomingMessagesThrottling()
{
if (isIncomingMessagesThrottlingEnabled())
return;
m_incomingMessagesThrottlingLevel = 0;
}
#if ENABLE(IPC_TESTING_API)
void Connection::addMessageObserver(const MessageObserver& observer)
{
m_messageObservers.append(observer);
}
void Connection::dispatchIncomingMessageForTesting(std::unique_ptr<Decoder>&& decoder)
{
m_connectionQueue->dispatch([protectedThis = Ref { *this }, decoder = WTFMove(decoder)]() mutable {
protectedThis->processIncomingMessage(WTFMove(decoder));
});
}
#endif
void Connection::connectionDidClose()
{
// The connection is now invalid.
m_isValid = false;
platformInvalidate();
bool hasPendingWaiters = false;
{
Locker locker { m_syncReplyStateLock };
ASSERT(m_shouldWaitForSyncReplies);
m_shouldWaitForSyncReplies = false;
hasPendingWaiters = !m_pendingSyncReplies.isEmpty();
}
if (hasPendingWaiters) {
Locker locker { m_incomingMessagesLock };
if (m_syncState)
m_syncState->wakeUpClientRunLoop();
}
{
Locker locker { m_waitForMessageLock };
ASSERT(m_shouldWaitForMessages);
m_shouldWaitForMessages = false;
if (m_waitingForMessage)
m_waitingForMessage->messageWaitingInterrupted = true;
}
m_waitForMessageCondition.notifyAll();
if (m_didCloseOnConnectionWorkQueueCallback)
m_didCloseOnConnectionWorkQueueCallback(this);
dispatchDidCloseAndInvalidate();
}
bool Connection::canSendOutgoingMessages() const
{
return m_isConnected && platformCanSendOutgoingMessages();
}
void Connection::sendOutgoingMessages()
{
if (!canSendOutgoingMessages())
return;
while (true) {
std::unique_ptr<Encoder> message;
{
Locker locker { m_outgoingMessagesLock };
if (m_outgoingMessages.isEmpty())
break;
message = m_outgoingMessages.takeFirst().moveToUniquePtr();
}
ASSERT(message);
if (!sendOutgoingMessage(makeUniqueRefFromNonNullUniquePtr(WTFMove(message))))
break;
}
}
void Connection::dispatchSyncMessage(Decoder& decoder)
{
// FIXME: If the message is invalid, we should send back a SyncMessageError.
// Currently we just wait for a timeout to happen, which will block the WebContent process.
assertIsCurrent(dispatcher());
ASSERT(decoder.isSyncMessage());
SyncRequestID syncRequestID;
if (UNLIKELY(!decoder.decode(syncRequestID))) {
// We received an invalid sync message.
return;
}
++m_inDispatchSyncMessageCount;
auto decrementSyncMessageCount = makeScopeExit([&] {
ASSERT(m_inDispatchSyncMessageCount);
--m_inDispatchSyncMessageCount;
});
auto replyEncoder = makeUniqueRef<Encoder>(MessageName::SyncMessageReply, syncRequestID.toUInt64());
bool wasHandled = false;
if (decoder.messageName() == MessageName::WrappedAsyncMessageForTesting) {
if (!m_fullySynchronousModeIsAllowedForTesting) {
decoder.markInvalid();
return;
}
std::unique_ptr<Decoder> unwrappedDecoder = Decoder::unwrapForTesting(decoder);
RELEASE_ASSERT(unwrappedDecoder);
processIncomingMessage(WTFMove(unwrappedDecoder));
m_syncState->dispatchMessages();
} else {
// Hand off both the decoder and encoder to the client.
wasHandled = m_client->didReceiveSyncMessage(*this, decoder, replyEncoder);
}
#if ENABLE(IPC_TESTING_API)
ASSERT(decoder.isValid() || m_ignoreInvalidMessageForTesting);
#else
ASSERT(decoder.isValid());
#endif
if (!wasHandled)
sendSyncReply(WTFMove(replyEncoder));
}
void Connection::dispatchDidReceiveInvalidMessage(MessageName messageName)
{
dispatchToClient([protectedThis = Ref { *this }, messageName] {
if (!protectedThis->isValid())
return;
protectedThis->m_client->didReceiveInvalidMessage(protectedThis, messageName);
});
}
void Connection::dispatchDidCloseAndInvalidate()
{
dispatchToClient([protectedThis = Ref { *this }] {
// If the connection has been explicitly invalidated before dispatchConnectionDidClose was called,
// then the connection client will be nullptr here.
if (!protectedThis->m_client)
return;
protectedThis->m_client->didClose(protectedThis);
protectedThis->invalidate();
});
}
size_t Connection::pendingMessageCountForTesting() const
{
// Note: current testing does not need to inspect the sync message state.
Locker lock { m_incomingMessagesLock };
return m_incomingMessages.size();
}
void Connection::dispatchOnReceiveQueueForTesting(Function<void()>&& completionHandler)
{
m_connectionQueue->dispatch(WTFMove(completionHandler));
}
void Connection::didFailToSendSyncMessage(Error)
{
if (!m_shouldExitOnSyncMessageSendFailure)
return;
exitProcess(0);
}
void Connection::enqueueIncomingMessage(std::unique_ptr<Decoder> incomingMessage)
{
ASSERT(m_incomingMessagesLock.isHeld());
{
#if PLATFORM(COCOA)
if (m_wasKilled)
return;
if (isIncomingMessagesThrottlingEnabled() && m_incomingMessages.size() >= maxPendingIncomingMessagesKillingThreshold) {
if (kill()) {
RELEASE_LOG_FAULT(IPC, "%p - Connection::enqueueIncomingMessage: Over %zu incoming messages have been queued without the main thread processing them, killing the connection as the remote process seems to be misbehaving", this, maxPendingIncomingMessagesKillingThreshold);
m_incomingMessages.clear();
}
return;
}
#endif
m_incomingMessages.append(WTFMove(incomingMessage));
if (isIncomingMessagesThrottlingEnabled() && m_incomingMessages.size() != 1)
return;
}
if (!m_syncState)
return;
if (isIncomingMessagesThrottlingEnabled()) {
dispatcher().dispatch([protectedThis = Ref { *this }] {
protectedThis->dispatchIncomingMessages();
});
} else {
dispatcher().dispatch([protectedThis = Ref { *this }] {
protectedThis->dispatchOneIncomingMessage();
});
}
}
void Connection::dispatchMessage(Decoder& decoder)
{
assertIsCurrent(dispatcher());
RELEASE_ASSERT(m_client);
if (decoder.messageReceiverName() == ReceiverName::AsyncReply) {
auto handler = takeAsyncReplyHandler(AtomicObjectIdentifier<AsyncReplyIDType>(decoder.destinationID()));
if (!handler) {
markCurrentlyDispatchedMessageAsInvalid();
#if ENABLE(IPC_TESTING_API)
if (m_ignoreInvalidMessageForTesting)
return;
#endif
ASSERT_NOT_REACHED();
return;
}
handler(&decoder);
return;
}
#if ENABLE(IPC_TESTING_API)
if (isMainRunLoop()) {
bool hasDeadObservers = false;
for (auto& observerWeakPtr : m_messageObservers) {
if (auto* observer = observerWeakPtr.get())
observer->didReceiveMessage(decoder);
else
hasDeadObservers = true;
}
if (hasDeadObservers)
m_messageObservers.removeAllMatching([](auto& observer) { return !observer; });
}
#endif
m_client->didReceiveMessage(*this, decoder);
}
void Connection::dispatchMessage(std::unique_ptr<Decoder> message)
{
if (!isValid())
return;
assertIsCurrent(dispatcher());
{
// FIXME: The matches here come from
// m_messagesToDispatchWhileWaitingForSyncReply. This causes message
// reordering, because some of the messages go to
// SyncState::m_messagesToDispatchWhileWaitingForSyncReply while others
// go to Connection::m_incomingMessages. Should be fixed by adding all
// messages to one list.
Locker incomingMessagesLocker { m_incomingMessagesLock };
if (auto* receiveQueue = m_receiveQueues.get(*message)) {
receiveQueue->enqueueMessage(*this, WTFMove(message));
return;
}
}
if (message->shouldUseFullySynchronousModeForTesting()) {
if (!m_fullySynchronousModeIsAllowedForTesting) {
#if ENABLE(IPC_TESTING_API)
if (m_ignoreInvalidMessageForTesting)
return;
#endif
m_client->didReceiveInvalidMessage(*this, message->messageName());
return;
}
m_inDispatchMessageMarkedToUseFullySynchronousModeForTesting++;
}
m_inDispatchMessageCount++;
bool isDispatchingMessageWhileWaitingForSyncReply = (message->shouldDispatchMessageWhenWaitingForSyncReply() == ShouldDispatchWhenWaitingForSyncReply::Yes)
|| (message->shouldDispatchMessageWhenWaitingForSyncReply() == ShouldDispatchWhenWaitingForSyncReply::YesDuringUnboundedIPC && UnboundedSynchronousIPCScope::hasOngoingUnboundedSyncIPC());
if (isDispatchingMessageWhileWaitingForSyncReply)
m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount++;
bool oldDidReceiveInvalidMessage = m_didReceiveInvalidMessage;
m_didReceiveInvalidMessage = false;
if (message->isSyncMessage())
dispatchSyncMessage(*message);
else
dispatchMessage(*message);
m_didReceiveInvalidMessage |= !message->isValid();
m_inDispatchMessageCount--;
// FIXME: For synchronous messages, we should not decrement the counter until we send a response.
// Otherwise, we would deadlock if processing the message results in a sync message back after we exit this function.
if (isDispatchingMessageWhileWaitingForSyncReply)
m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount--;
if (message->shouldUseFullySynchronousModeForTesting())
m_inDispatchMessageMarkedToUseFullySynchronousModeForTesting--;
if (m_didReceiveInvalidMessage
#if ENABLE(IPC_TESTING_API)
&& !m_ignoreInvalidMessageForTesting
#endif
&& isValid())
m_client->didReceiveInvalidMessage(*this, message->messageName());
m_didReceiveInvalidMessage = oldDidReceiveInvalidMessage;
}
size_t Connection::numberOfMessagesToProcess(size_t totalMessages)
{
// Never dispatch more than 600 messages without returning to the run loop, we can go as low as 60 with maximum throttling level.
static const size_t maxIncomingMessagesDispatchingBatchSize { 600 };
static const uint8_t maxThrottlingLevel = 9;
size_t batchSize = maxIncomingMessagesDispatchingBatchSize / (*m_incomingMessagesThrottlingLevel + 1);
if (totalMessages > maxIncomingMessagesDispatchingBatchSize)
m_incomingMessagesThrottlingLevel = std::min<uint8_t>(*m_incomingMessagesThrottlingLevel + 1u, maxThrottlingLevel);
else if (*m_incomingMessagesThrottlingLevel)
--*m_incomingMessagesThrottlingLevel;
return std::min(totalMessages, batchSize);
}
SerialFunctionDispatcher& Connection::dispatcher()
{
// dispatcher can only be accessed while the connection is valid,
// and must have the incoming message lock held if not being
// called from the SerialFunctionDispatcher.
RELEASE_ASSERT(m_syncState);
if (!m_incomingMessagesLock.isLocked())
assertIsCurrent(m_syncState->dispatcher());
// Our syncState is specific to the SerialFunctionDispatcher we have been
// bound to during open(), so we can retrieve the SerialFunctionDispatcher
// from it (rather than storing another pointer on this class).
return m_syncState->dispatcher();
}
void Connection::dispatchOneIncomingMessage()
{
std::unique_ptr<Decoder> message;
{
Locker locker { m_incomingMessagesLock };
if (m_incomingMessages.isEmpty())
return;
message = m_incomingMessages.takeFirst();
}
dispatchMessage(WTFMove(message));
}
void Connection::dispatchSyncStateMessages()
{
if (m_syncState) {
assertIsCurrent(dispatcher());
m_syncState->dispatchMessagesAndResetDidScheduleDispatchMessagesForConnection(*this);
}
}
void Connection::dispatchIncomingMessages()
{
if (!isValid())
return;
std::unique_ptr<Decoder> message;
size_t messagesToProcess = 0;
{
Locker locker { m_incomingMessagesLock };
if (m_incomingMessages.isEmpty())
return;
message = m_incomingMessages.takeFirst();
// Incoming messages may get adding to the queue by the IPC thread while we're dispatching the messages below.
// To make sure dispatchIncomingMessages() yields, we only ever process messages that were in the queue when
// dispatchIncomingMessages() was called. Additionally, the message throttling may further cap the number of
// messages to process to make sure we give the main run loop a chance to process other events.
messagesToProcess = numberOfMessagesToProcess(m_incomingMessages.size());
if (messagesToProcess < m_incomingMessages.size()) {
RELEASE_LOG_ERROR(IPC, "%p - Connection::dispatchIncomingMessages: IPC throttling was triggered (has %zu pending incoming messages, will only process %zu before yielding)", this, m_incomingMessages.size(), messagesToProcess);
RELEASE_LOG_ERROR(IPC, "%p - Connection::dispatchIncomingMessages: first IPC message in queue is %" PUBLIC_LOG_STRING, this, description(message->messageName()));
}
// Re-schedule ourselves *before* we dispatch the messages because we want to process follow-up messages if the client
// spins a nested run loop while we're dispatching a message. Note that this means we can re-enter this method.
if (!m_incomingMessages.isEmpty()) {
dispatcher().dispatch([protectedThis = Ref { *this }] {
protectedThis->dispatchIncomingMessages();
});
}
}
dispatchMessage(WTFMove(message));
for (size_t i = 1; i < messagesToProcess; ++i) {
{
Locker locker { m_incomingMessagesLock };
if (m_incomingMessages.isEmpty())
return;
message = m_incomingMessages.takeFirst();
}
dispatchMessage(WTFMove(message));
}
}
void Connection::addAsyncReplyHandler(AsyncReplyHandler&& handler)
{
Locker locker { m_incomingMessagesLock };
auto result = m_asyncReplyHandlers.add(handler.replyID, WTFMove(handler.completionHandler));
ASSERT_UNUSED(result, result.isNewEntry);
}
void Connection::cancelAsyncReplyHandlers()
{
AsyncReplyHandlerMap map;
{
Locker locker { m_incomingMessagesLock };
map.swap(m_asyncReplyHandlers);
}
for (auto& handler : map.values()) {
if (handler)
handler(nullptr);
}
}
CompletionHandler<void(Decoder*)> Connection::takeAsyncReplyHandler(AsyncReplyID replyID)
{
Locker locker { m_incomingMessagesLock };
if (!m_asyncReplyHandlers.isValidKey(replyID))
return nullptr;
return m_asyncReplyHandlers.take(replyID);
}
void Connection::wakeUpRunLoop()
{
if (!isValid())
return;
if (&dispatcher() == &RunLoop::main())
RunLoop::main().wakeUp();
}
template<typename F>
void Connection::dispatchToClient(F&& clientRunLoopTask)
{
Locker lock { m_incomingMessagesLock };
if (!m_syncState)
return;
dispatcher().dispatch(WTFMove(clientRunLoopTask));
}
#if !USE(UNIX_DOMAIN_SOCKETS) && !OS(DARWIN) && !OS(WINDOWS)
std::optional<Connection::ConnectionIdentifierPair> Connection::createConnectionIdentifierPair()
{
notImplemented();
return std::nullopt;
}
#endif
void Connection::Handle::encode(Encoder& encoder)
{
encoder << WTFMove(handle);
}
std::optional<Connection::Handle> Connection::Handle::decode(Decoder& decoder)
{
#if USE(UNIX_DOMAIN_SOCKETS)
auto handle = decoder.decode<UnixFileDescriptor>();
#elif OS(WINDOWS)
auto handle = decoder.decode<Win32Handle>();
#elif OS(DARWIN)
auto handle = decoder.decode<MachSendRight>();
#endif
return handle;
}
const char* errorAsString(Error error)
{
switch (error) {
case Error::NoError: return "NoError";
case Error::InvalidConnection: return "InvalidConnection";
case Error::NoConnectionForIdentifier: return "NoConnectionForIdentifier";
case Error::NoMessageSenderConnection: return "NoMessageSenderConnection";
case Error::Timeout: return "Timeout";
case Error::Unspecified: return "Unspecified";
case Error::MultipleWaitingClients: return "MultipleWaitingClients";
case Error::AttemptingToWaitOnClosedConnection: return "AttemptingToWaitOnClosedConnection";
case Error::WaitingOnAlreadyDispatchedMessage: return "WaitingOnAlreadyDispatchedMessage";
case Error::AttemptingToWaitInsideSyncMessageHandling: return "AttemptingToWaitInsideSyncMessageHandling";
case Error::SyncMessageInterruptedWait: return "SyncMessageInterruptedWait";
case Error::CantWaitForSyncReplies: return "CantWaitForSyncReplies";
case Error::FailedToEncodeMessageArguments: return "FailedToEncodeMessageArguments";
case Error::FailedToDecodeReplyArguments: return "FailedToDecodeReplyArguments";
case Error::FailedToFindReplyHandler: return "FailedToFindReplyHandler";
case Error::FailedToAcquireBufferSpan: return "FailedToAcquireBufferSpan";
case Error::FailedToAcquireReplyBufferSpan: return "FailedToAcquireReplyBufferSpan";
case Error::StreamConnectionEncodingError: return "StreamConnectionEncodingError";
}
return "";
}
Connection::DecoderOrError::DecoderOrError(DecoderOrError&&) = default;
Connection::DecoderOrError::~DecoderOrError() = default;
} // namespace IPC
|