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 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ipc/ipc_sync_channel.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/message_loop/message_pump_type.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sender.h"
#include "ipc/ipc_sync_message_filter.h"
#include "ipc/ipc_sync_message_unittest.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::WaitableEvent;
namespace IPC {
namespace {
// Base class for a "process" with listener and IPC threads.
class Worker : public Listener, public Sender {
public:
// Will create a channel without a name.
Worker(Channel::Mode mode,
const std::string& thread_name,
mojo::ScopedMessagePipeHandle channel_handle)
: done_(
new WaitableEvent(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
channel_created_(
new WaitableEvent(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
channel_handle_(std::move(channel_handle)),
mode_(mode),
ipc_thread_(
std::make_unique<base::Thread>((thread_name + "_ipc").c_str())),
listener_thread_((thread_name + "_listener").c_str()),
overrided_thread_(nullptr),
shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED),
is_shutdown_(false) {}
// Will create a named channel and use this name for the threads' name.
Worker(mojo::ScopedMessagePipeHandle channel_handle, Channel::Mode mode)
: done_(
new WaitableEvent(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
channel_created_(
new WaitableEvent(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
channel_handle_(std::move(channel_handle)),
mode_(mode),
ipc_thread_(std::make_unique<base::Thread>("ipc thread")),
listener_thread_("listener thread"),
overrided_thread_(nullptr),
shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED),
is_shutdown_(false) {}
Worker(const Worker&) = delete;
Worker& operator=(const Worker&) = delete;
~Worker() override {
// Shutdown() must be called before destruction.
CHECK(is_shutdown_);
}
bool Send(Message* msg) override { return channel_->Send(msg); }
void WaitForChannelCreation() { channel_created_->Wait(); }
void CloseChannel() {
DCHECK(ListenerThread()->task_runner()->BelongsToCurrentThread());
channel_->Close();
}
void Start() {
StartThread(&listener_thread_, base::MessagePumpType::DEFAULT);
ListenerThread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&Worker::OnStart, base::Unretained(this)));
}
void Shutdown() {
// The IPC thread needs to outlive SyncChannel. We can't do this in
// ~Worker(), since that'll reset the vtable pointer (to Worker's), which
// may result in a race conditions. See http://crbug.com/25841.
WaitableEvent listener_done(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
ListenerThread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&Worker::OnListenerThreadShutdown1,
base::Unretained(this), &listener_done));
listener_done.Wait();
listener_thread_.Stop();
is_shutdown_ = true;
}
void OverrideThread(base::Thread* overrided_thread) {
DCHECK(!overrided_thread_);
overrided_thread_ = overrided_thread;
}
bool SendAnswerToLife(bool succeed) {
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
bool result = Send(msg);
DCHECK_EQ(result, succeed);
DCHECK_EQ(answer, (succeed ? 42 : 0));
return result;
}
bool SendDouble(bool succeed) {
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_Double(5, &answer);
bool result = Send(msg);
DCHECK_EQ(result, succeed);
DCHECK_EQ(answer, (succeed ? 10 : 0));
return result;
}
mojo::MessagePipeHandle TakeChannelHandle() {
DCHECK(channel_handle_.is_valid());
return channel_handle_.release();
}
Channel::Mode mode() { return mode_; }
WaitableEvent* done_event() { return done_.get(); }
WaitableEvent* shutdown_event() { return &shutdown_event_; }
void ResetChannel() { channel_.reset(); }
// Derived classes need to call this when they've completed their part of
// the test.
void Done() { done_->Signal(); }
protected:
SyncChannel* channel() { return channel_.get(); }
// Functions for derived classes to implement if they wish.
virtual void Run() { }
virtual void OnAnswer(int* answer) { NOTREACHED(); }
virtual void OnAnswerDelay(Message* reply_msg) {
// The message handler map below can only take one entry for
// SyncChannelTestMsg_AnswerToLife, so since some classes want
// the normal version while other want the delayed reply, we
// call the normal version if the derived class didn't override
// this function.
int answer;
OnAnswer(&answer);
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, answer);
Send(reply_msg);
}
virtual void OnDouble(int in, int* out) { NOTREACHED(); }
virtual void OnDoubleDelay(int in, Message* reply_msg) {
int result;
OnDouble(in, &result);
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, result);
Send(reply_msg);
}
virtual void OnNestedTestMsg(Message* reply_msg) { NOTREACHED(); }
virtual SyncChannel* CreateChannel() {
std::unique_ptr<SyncChannel> channel = SyncChannel::Create(
TakeChannelHandle(), mode_, this, ipc_thread_->task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(), true,
&shutdown_event_);
return channel.release();
}
base::Thread* ListenerThread() {
return overrided_thread_ ? overrided_thread_.get() : &listener_thread_;
}
const base::Thread& ipc_thread() const { return *ipc_thread_.get(); }
private:
// Called on the listener thread to create the sync channel.
void OnStart() {
// Link ipc_thread_, listener_thread_ and channel_ altogether.
StartThread(ipc_thread_.get(), base::MessagePumpType::IO);
channel_.reset(CreateChannel());
channel_created_->Signal();
Run();
}
void OnListenerThreadShutdown1(WaitableEvent* listener_event) {
WaitableEvent ipc_event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
// SyncChannel needs to be destructed on the thread that it was created on.
channel_.reset();
base::RunLoop().RunUntilIdle();
ipc_thread_->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&Worker::OnIPCThreadShutdown, base::Unretained(this),
listener_event, &ipc_event));
ipc_event.Wait();
// This destructs `ipc_thread_` on the listener thread.
ipc_thread_.reset();
listener_thread_.task_runner()->PostTask(
FROM_HERE, base::BindOnce(&Worker::OnListenerThreadShutdown2,
base::Unretained(this), listener_event));
}
void OnIPCThreadShutdown(WaitableEvent* listener_event,
WaitableEvent* ipc_event) {
base::RunLoop().RunUntilIdle();
ipc_event->Signal();
}
void OnListenerThreadShutdown2(WaitableEvent* listener_event) {
base::RunLoop().RunUntilIdle();
listener_event->Signal();
}
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(Worker, message)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_Double, OnDoubleDelay)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_AnswerToLife,
OnAnswerDelay)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelNestedTestMsg_String,
OnNestedTestMsg)
IPC_END_MESSAGE_MAP()
return true;
}
void StartThread(base::Thread* thread, base::MessagePumpType type) {
base::Thread::Options options;
options.message_pump_type = type;
thread->StartWithOptions(std::move(options));
}
std::unique_ptr<WaitableEvent> done_;
std::unique_ptr<WaitableEvent> channel_created_;
mojo::ScopedMessagePipeHandle channel_handle_;
Channel::Mode mode_;
std::unique_ptr<SyncChannel> channel_;
// This thread is constructed on the main thread, Start() on
// `listener_thread_`, and therefore destructed/Stop()'d on the
// `listener_thread_` too.
std::unique_ptr<base::Thread> ipc_thread_;
base::Thread listener_thread_;
raw_ptr<base::Thread> overrided_thread_;
base::WaitableEvent shutdown_event_;
bool is_shutdown_;
};
// Starts the test with the given workers. This function deletes the workers
// when it's done.
void RunTest(std::vector<Worker*> workers) {
// First we create the workers that are channel servers, or else the other
// workers' channel initialization might fail because the pipe isn't created..
for (size_t i = 0; i < workers.size(); ++i) {
if (workers[i]->mode() & Channel::MODE_SERVER_FLAG) {
workers[i]->Start();
workers[i]->WaitForChannelCreation();
}
}
// now create the clients
for (size_t i = 0; i < workers.size(); ++i) {
if (workers[i]->mode() & Channel::MODE_CLIENT_FLAG)
workers[i]->Start();
}
// wait for all the workers to finish
for (size_t i = 0; i < workers.size(); ++i)
workers[i]->done_event()->Wait();
for (size_t i = 0; i < workers.size(); ++i) {
workers[i]->Shutdown();
delete workers[i];
}
}
class IPCSyncChannelTest : public testing::Test {
private:
base::test::SingleThreadTaskEnvironment task_environment_;
};
//------------------------------------------------------------------------------
class SimpleServer : public Worker {
public:
explicit SimpleServer(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"simpler_server",
std::move(channel_handle)) {}
void Run() override {
SendAnswerToLife(true);
Done();
}
};
class SimpleClient : public Worker {
public:
explicit SimpleClient(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"simple_client",
std::move(channel_handle)) {}
void OnAnswer(int* answer) override {
*answer = 42;
Done();
}
};
void Simple() {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(new SimpleServer(std::move(pipe.handle0)));
workers.push_back(new SimpleClient(std::move(pipe.handle1)));
RunTest(workers);
}
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_Simple DISABLED_Simple
#else
#define MAYBE_Simple Simple
#endif
// Tests basic synchronous call
TEST_F(IPCSyncChannelTest, MAYBE_Simple) {
Simple();
}
//------------------------------------------------------------------------------
// Worker classes which override how the sync channel is created to use the
// two-step initialization (calling the lightweight constructor and then
// ChannelProxy::Init separately) process.
class TwoStepServer : public Worker {
public:
TwoStepServer(bool create_pipe_now,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"simpler_server",
std::move(channel_handle)),
create_pipe_now_(create_pipe_now) {}
void Run() override {
SendAnswerToLife(true);
Done();
}
SyncChannel* CreateChannel() override {
SyncChannel* channel =
SyncChannel::Create(TakeChannelHandle(), mode(), this,
ipc_thread().task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
create_pipe_now_, shutdown_event())
.release();
return channel;
}
bool create_pipe_now_;
};
class TwoStepClient : public Worker {
public:
TwoStepClient(bool create_pipe_now,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"simple_client",
std::move(channel_handle)),
create_pipe_now_(create_pipe_now) {}
void OnAnswer(int* answer) override {
*answer = 42;
Done();
}
SyncChannel* CreateChannel() override {
SyncChannel* channel =
SyncChannel::Create(TakeChannelHandle(), mode(), this,
ipc_thread().task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
create_pipe_now_, shutdown_event())
.release();
return channel;
}
bool create_pipe_now_;
};
void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(
new TwoStepServer(create_server_pipe_now, std::move(pipe.handle0)));
workers.push_back(
new TwoStepClient(create_client_pipe_now, std::move(pipe.handle1)));
RunTest(workers);
}
// Tests basic two-step initialization, where you call the lightweight
// constructor then Init.
TEST_F(IPCSyncChannelTest, TwoStepInitialization) {
TwoStep(false, false);
TwoStep(false, true);
TwoStep(true, false);
TwoStep(true, true);
}
//------------------------------------------------------------------------------
class DelayClient : public Worker {
public:
explicit DelayClient(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"delay_client",
std::move(channel_handle)) {}
void OnAnswerDelay(Message* reply_msg) override {
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
Done();
}
};
void DelayReply() {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(new SimpleServer(std::move(pipe.handle0)));
workers.push_back(new DelayClient(std::move(pipe.handle1)));
RunTest(workers);
}
// Tests that asynchronous replies work
TEST_F(IPCSyncChannelTest, DelayReply) {
DelayReply();
}
//------------------------------------------------------------------------------
class NoHangServer : public Worker {
public:
NoHangServer(WaitableEvent* got_first_reply,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"no_hang_server",
std::move(channel_handle)),
got_first_reply_(got_first_reply) {}
void Run() override {
SendAnswerToLife(true);
got_first_reply_->Signal();
SendAnswerToLife(false);
Done();
}
raw_ptr<WaitableEvent> got_first_reply_;
};
class NoHangClient : public Worker {
public:
NoHangClient(WaitableEvent* got_first_reply,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"no_hang_client",
std::move(channel_handle)),
got_first_reply_(got_first_reply) {}
void OnAnswerDelay(Message* reply_msg) override {
// Use the DELAY_REPLY macro so that we can force the reply to be sent
// before this function returns (when the channel will be reset).
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
got_first_reply_->Wait();
CloseChannel();
Done();
}
raw_ptr<WaitableEvent> got_first_reply_;
};
void NoHang() {
WaitableEvent got_first_reply(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(
new NoHangServer(&got_first_reply, std::move(pipe.handle0)));
workers.push_back(
new NoHangClient(&got_first_reply, std::move(pipe.handle1)));
RunTest(workers);
}
// Tests that caller doesn't hang if receiver dies
TEST_F(IPCSyncChannelTest, NoHang) {
NoHang();
}
//------------------------------------------------------------------------------
class UnblockServer : public Worker {
public:
UnblockServer(bool delete_during_send,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"unblock_server",
std::move(channel_handle)),
delete_during_send_(delete_during_send) {}
void Run() override {
if (delete_during_send_) {
// Use custom code since race conditions mean the answer may or may not be
// available.
int answer = 0;
SyncMessage* msg = new SyncChannelTestMsg_AnswerToLife(&answer);
Send(msg);
} else {
SendAnswerToLife(true);
}
Done();
}
void OnDoubleDelay(int in, Message* reply_msg) override {
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
Send(reply_msg);
if (delete_during_send_)
ResetChannel();
}
bool delete_during_send_;
};
class UnblockClient : public Worker {
public:
explicit UnblockClient(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"unblock_client",
std::move(channel_handle)) {}
void OnAnswer(int* answer) override {
SendDouble(true);
*answer = 42;
Done();
}
};
void Unblock(bool delete_during_send) {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(
new UnblockServer(delete_during_send, std::move(pipe.handle0)));
workers.push_back(new UnblockClient(std::move(pipe.handle1)));
RunTest(workers);
}
// Tests that the caller unblocks to answer a sync message from the receiver.
TEST_F(IPCSyncChannelTest, Unblock) {
Unblock(false);
}
//------------------------------------------------------------------------------
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_ChannelDeleteDuringSend DISABLED_ChannelDeleteDuringSend
#else
#define MAYBE_ChannelDeleteDuringSend ChannelDeleteDuringSend
#endif
// Tests that the the SyncChannel object can be deleted during a Send.
TEST_F(IPCSyncChannelTest, MAYBE_ChannelDeleteDuringSend) {
Unblock(true);
}
//------------------------------------------------------------------------------
class RecursiveServer : public Worker {
public:
RecursiveServer(bool expected_send_result,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"recursive_server",
std::move(channel_handle)),
expected_send_result_(expected_send_result) {}
void Run() override {
SendDouble(expected_send_result_);
Done();
}
void OnDouble(int in, int* out) override {
*out = in * 2;
SendAnswerToLife(expected_send_result_);
}
bool expected_send_result_;
};
class RecursiveClient : public Worker {
public:
RecursiveClient(bool close_channel,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_CLIENT,
"recursive_client",
std::move(channel_handle)),
close_channel_(close_channel) {}
void OnDoubleDelay(int in, Message* reply_msg) override {
SendDouble(!close_channel_);
if (close_channel_) {
delete reply_msg;
} else {
SyncChannelTestMsg_Double::WriteReplyParams(reply_msg, in * 2);
Send(reply_msg);
}
Done();
}
void OnAnswerDelay(Message* reply_msg) override {
if (close_channel_) {
delete reply_msg;
CloseChannel();
} else {
SyncChannelTestMsg_AnswerToLife::WriteReplyParams(reply_msg, 42);
Send(reply_msg);
}
}
bool close_channel_;
};
void Recursive() {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(new RecursiveServer(true, std::move(pipe.handle0)));
workers.push_back(new RecursiveClient(false, std::move(pipe.handle1)));
RunTest(workers);
}
// Tests a server calling Send while another Send is pending.
TEST_F(IPCSyncChannelTest, Recursive) {
Recursive();
}
//------------------------------------------------------------------------------
void RecursiveNoHang() {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(new RecursiveServer(false, std::move(pipe.handle0)));
workers.push_back(new RecursiveClient(true, std::move(pipe.handle1)));
RunTest(workers);
}
// Tests that if a caller makes a sync call during an existing sync call and
// the receiver dies, neither of the Send() calls hang.
TEST_F(IPCSyncChannelTest, RecursiveNoHang) {
RecursiveNoHang();
}
//------------------------------------------------------------------------------
class MultipleServer1 : public Worker {
public:
explicit MultipleServer1(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER) {}
void Run() override {
SendDouble(true);
Done();
}
};
class MultipleClient1 : public Worker {
public:
MultipleClient1(WaitableEvent* client1_msg_received,
WaitableEvent* client1_can_reply,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
client1_msg_received_(client1_msg_received),
client1_can_reply_(client1_can_reply) {}
void OnDouble(int in, int* out) override {
client1_msg_received_->Signal();
*out = in * 2;
client1_can_reply_->Wait();
Done();
}
private:
raw_ptr<WaitableEvent> client1_msg_received_;
raw_ptr<WaitableEvent> client1_can_reply_;
};
class MultipleServer2 : public Worker {
public:
explicit MultipleServer2(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER) {}
void OnAnswer(int* result) override {
*result = 42;
Done();
}
};
class MultipleClient2 : public Worker {
public:
MultipleClient2(WaitableEvent* client1_msg_received,
WaitableEvent* client1_can_reply,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
client1_msg_received_(client1_msg_received),
client1_can_reply_(client1_can_reply) {}
void Run() override {
client1_msg_received_->Wait();
SendAnswerToLife(true);
client1_can_reply_->Signal();
Done();
}
private:
raw_ptr<WaitableEvent> client1_msg_received_;
raw_ptr<WaitableEvent> client1_can_reply_;
};
void Multiple() {
std::vector<Worker*> workers;
// A shared worker thread so that server1 and server2 run on one thread.
base::Thread worker_thread("Multiple");
ASSERT_TRUE(worker_thread.Start());
// Server1 sends a sync msg to client1, which blocks the reply until
// server2 (which runs on the same worker thread as server1) responds
// to a sync msg from client2.
WaitableEvent client1_msg_received(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent client1_can_reply(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
Worker* worker;
mojo::MessagePipe pipe1, pipe2;
worker = new MultipleServer2(std::move(pipe2.handle0));
worker->OverrideThread(&worker_thread);
workers.push_back(worker);
worker = new MultipleClient2(&client1_msg_received, &client1_can_reply,
std::move(pipe2.handle1));
workers.push_back(worker);
worker = new MultipleServer1(std::move(pipe1.handle0));
worker->OverrideThread(&worker_thread);
workers.push_back(worker);
worker = new MultipleClient1(&client1_msg_received, &client1_can_reply,
std::move(pipe1.handle1));
workers.push_back(worker);
RunTest(workers);
}
// Tests that multiple SyncObjects on the same listener thread can unblock each
// other.
TEST_F(IPCSyncChannelTest, Multiple) {
Multiple();
}
//------------------------------------------------------------------------------
// This class provides server side functionality to test the case where
// multiple sync channels are in use on the same thread on the client.
class QueuedReplyServer : public Worker {
public:
QueuedReplyServer(base::Thread* listener_thread,
mojo::ScopedMessagePipeHandle channel_handle,
const std::string& reply_text)
: Worker(std::move(channel_handle), Channel::MODE_SERVER),
reply_text_(reply_text) {
Worker::OverrideThread(listener_thread);
}
void OnNestedTestMsg(Message* reply_msg) override {
VLOG(1) << __FUNCTION__ << " Sending reply: " << reply_text_;
SyncChannelNestedTestMsg_String::WriteReplyParams(reply_msg, reply_text_);
Send(reply_msg);
Done();
}
private:
std::string reply_text_;
};
// The QueuedReplyClient class provides functionality to test the case where
// multiple sync channels are in use on the same thread.
class QueuedReplyClient : public Worker {
public:
QueuedReplyClient(base::Thread* listener_thread,
mojo::ScopedMessagePipeHandle channel_handle,
const std::string& expected_text)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
expected_text_(expected_text) {
Worker::OverrideThread(listener_thread);
}
void Run() override {
std::string response;
SyncMessage* msg = new SyncChannelNestedTestMsg_String(&response);
bool result = Send(msg);
DCHECK(result);
DCHECK_EQ(response, expected_text_);
VLOG(1) << __FUNCTION__ << " Received reply: " << response;
Done();
}
private:
std::string expected_text_;
};
void QueuedReply() {
std::vector<Worker*> workers;
// A shared worker thread for servers
base::Thread server_worker_thread("QueuedReply_ServerListener");
ASSERT_TRUE(server_worker_thread.Start());
base::Thread client_worker_thread("QueuedReply_ClientListener");
ASSERT_TRUE(client_worker_thread.Start());
Worker* worker;
mojo::MessagePipe pipe1, pipe2;
worker = new QueuedReplyServer(&server_worker_thread,
std::move(pipe1.handle0), "Got first message");
workers.push_back(worker);
worker = new QueuedReplyServer(
&server_worker_thread, std::move(pipe2.handle0), "Got second message");
workers.push_back(worker);
worker = new QueuedReplyClient(&client_worker_thread,
std::move(pipe1.handle1), "Got first message");
workers.push_back(worker);
worker = new QueuedReplyClient(
&client_worker_thread, std::move(pipe2.handle1), "Got second message");
workers.push_back(worker);
RunTest(workers);
}
// While a blocking send is in progress, the listener thread might answer other
// synchronous messages. This tests that if during the response to another
// message the reply to the original messages comes, it is queued up correctly
// and the original Send is unblocked later.
TEST_F(IPCSyncChannelTest, QueuedReply) {
QueuedReply();
}
//------------------------------------------------------------------------------
class TestSyncMessageFilter : public SyncMessageFilter {
public:
TestSyncMessageFilter(
base::WaitableEvent* shutdown_event,
Worker* worker,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: SyncMessageFilter(shutdown_event),
worker_(worker),
task_runner_(task_runner) {}
void OnFilterAdded(Channel* channel) override {
SyncMessageFilter::OnFilterAdded(channel);
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&TestSyncMessageFilter::SendMessageOnHelperThread,
this));
}
void SendMessageOnHelperThread() {
int answer = 0;
bool result = Send(new SyncChannelTestMsg_AnswerToLife(&answer));
DCHECK(result);
DCHECK_EQ(answer, 42);
worker_->Done();
}
private:
~TestSyncMessageFilter() override = default;
raw_ptr<Worker> worker_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
};
class SyncMessageFilterServer : public Worker {
public:
explicit SyncMessageFilterServer(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"sync_message_filter_server",
std::move(channel_handle)),
thread_("helper_thread") {
base::Thread::Options options;
options.message_pump_type = base::MessagePumpType::DEFAULT;
thread_.StartWithOptions(std::move(options));
filter_ = new TestSyncMessageFilter(shutdown_event(), this,
thread_.task_runner());
}
void Run() override {
channel()->AddFilter(filter_.get());
}
base::Thread thread_;
scoped_refptr<TestSyncMessageFilter> filter_;
};
// This class provides functionality to test the case that a Send on the sync
// channel does not crash after the channel has been closed.
class ServerSendAfterClose : public Worker {
public:
explicit ServerSendAfterClose(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(Channel::MODE_SERVER,
"simpler_server",
std::move(channel_handle)),
send_result_(true) {}
bool SendDummy() {
ListenerThread()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(base::IgnoreResult(&ServerSendAfterClose::Send),
base::Unretained(this), new SyncChannelTestMsg_NoArgs));
return true;
}
bool send_result() const {
return send_result_;
}
private:
void Run() override {
CloseChannel();
Done();
}
bool Send(Message* msg) override {
send_result_ = Worker::Send(msg);
Done();
return send_result_;
}
bool send_result_;
};
// Tests basic synchronous call
TEST_F(IPCSyncChannelTest, SyncMessageFilter) {
std::vector<Worker*> workers;
mojo::MessagePipe pipe;
workers.push_back(new SyncMessageFilterServer(std::move(pipe.handle0)));
workers.push_back(new SimpleClient(std::move(pipe.handle1)));
RunTest(workers);
}
// Test the case when the channel is closed and a Send is attempted after that.
TEST_F(IPCSyncChannelTest, SendAfterClose) {
mojo::MessagePipe pipe;
ServerSendAfterClose server(std::move(pipe.handle0));
server.Start();
server.done_event()->Wait();
server.done_event()->Reset();
server.SendDummy();
server.done_event()->Wait();
EXPECT_FALSE(server.send_result());
server.Shutdown();
}
//------------------------------------------------------------------------------
class RestrictedDispatchServer : public Worker {
public:
RestrictedDispatchServer(WaitableEvent* sent_ping_event,
WaitableEvent* wait_event,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER),
sent_ping_event_(sent_ping_event),
wait_event_(wait_event) {}
void OnDoPing(int ping) {
// Send an asynchronous message that unblocks the caller.
Message* msg = new SyncChannelTestMsg_Ping(ping);
msg->set_unblock(true);
Send(msg);
// Signal the event after the message has been sent on the channel, on the
// IPC thread.
ipc_thread().task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RestrictedDispatchServer::OnPingSent,
base::Unretained(this)));
}
void OnPingTTL(int ping, int* out) {
*out = ping;
wait_event_->Wait();
}
base::Thread* ListenerThread() { return Worker::ListenerThread(); }
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchServer, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_PingTTL, OnPingTTL)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
IPC_END_MESSAGE_MAP()
return true;
}
void OnPingSent() {
sent_ping_event_->Signal();
}
void OnNoArgs() { }
raw_ptr<WaitableEvent> sent_ping_event_;
raw_ptr<WaitableEvent> wait_event_;
};
class NonRestrictedDispatchServer : public Worker {
public:
NonRestrictedDispatchServer(WaitableEvent* signal_event,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER),
signal_event_(signal_event) {}
base::Thread* ListenerThread() { return Worker::ListenerThread(); }
void OnDoPingTTL(int ping) {
int value = 0;
Send(new SyncChannelTestMsg_PingTTL(ping, &value));
signal_event_->Signal();
}
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(NonRestrictedDispatchServer, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
IPC_END_MESSAGE_MAP()
return true;
}
void OnNoArgs() { }
raw_ptr<WaitableEvent> signal_event_;
};
class RestrictedDispatchClient : public Worker {
public:
RestrictedDispatchClient(
WaitableEvent* sent_ping_event,
RestrictedDispatchServer* server,
NonRestrictedDispatchServer* server2,
int* success,
mojo::ScopedMessagePipeHandle restricted_channel_handle,
mojo::ScopedMessagePipeHandle non_restricted_channel_handle)
: Worker(std::move(restricted_channel_handle), Channel::MODE_CLIENT),
ping_(0),
server_(server),
server2_(server2),
success_(success),
sent_ping_event_(sent_ping_event),
non_restricted_channel_handle_(
std::move(non_restricted_channel_handle)) {}
void Run() override {
// Incoming messages from our channel should only be dispatched when we
// send a message on that same channel.
channel()->SetRestrictDispatchChannelGroup(1);
server_->ListenerThread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RestrictedDispatchServer::OnDoPing,
base::Unretained(server_), 1));
sent_ping_event_->Wait();
Send(new SyncChannelTestMsg_NoArgs);
if (ping_ == 1)
++*success_;
else
LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
non_restricted_channel_ = SyncChannel::Create(
non_restricted_channel_handle_.release(), IPC::Channel::MODE_CLIENT,
this, ipc_thread().task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(), true,
shutdown_event());
server_->ListenerThread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RestrictedDispatchServer::OnDoPing,
base::Unretained(server_), 2));
sent_ping_event_->Wait();
// Check that the incoming message is *not* dispatched when sending on the
// non restricted channel.
// TODO(piman): there is a possibility of a false positive race condition
// here, if the message that was posted on the server-side end of the pipe
// is not visible yet on the client side, but I don't know how to solve this
// without hooking into the internals of SyncChannel. I haven't seen it in
// practice (i.e. not setting SetRestrictDispatchToSameChannel does cause
// the following to fail).
non_restricted_channel_->Send(new SyncChannelTestMsg_NoArgs);
if (ping_ == 1)
++*success_;
else
LOG(ERROR) << "Send dispatched message from restricted channel";
Send(new SyncChannelTestMsg_NoArgs);
if (ping_ == 2)
++*success_;
else
LOG(ERROR) << "Send failed to dispatch incoming message on same channel";
// Check that the incoming message on the non-restricted channel is
// dispatched when sending on the restricted channel.
server2_->ListenerThread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&NonRestrictedDispatchServer::OnDoPingTTL,
base::Unretained(server2_), 3));
int value = 0;
Send(new SyncChannelTestMsg_PingTTL(4, &value));
if (ping_ == 3 && value == 4)
++*success_;
else
LOG(ERROR) << "Send failed to dispatch message from unrestricted channel";
non_restricted_channel_->Send(new SyncChannelTestMsg_Done);
non_restricted_channel_.reset();
Send(new SyncChannelTestMsg_Done);
Done();
}
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchClient, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Ping, OnPing)
IPC_MESSAGE_HANDLER_DELAY_REPLY(SyncChannelTestMsg_PingTTL, OnPingTTL)
IPC_END_MESSAGE_MAP()
return true;
}
void OnPing(int ping) {
ping_ = ping;
}
void OnPingTTL(int ping, IPC::Message* reply) {
ping_ = ping;
// This message comes from the NonRestrictedDispatchServer, we have to send
// the reply back manually.
SyncChannelTestMsg_PingTTL::WriteReplyParams(reply, ping);
non_restricted_channel_->Send(reply);
}
int ping_;
raw_ptr<RestrictedDispatchServer, DanglingUntriaged> server_;
raw_ptr<NonRestrictedDispatchServer, DanglingUntriaged> server2_;
raw_ptr<int> success_;
raw_ptr<WaitableEvent> sent_ping_event_;
std::unique_ptr<SyncChannel> non_restricted_channel_;
mojo::ScopedMessagePipeHandle non_restricted_channel_handle_;
};
TEST_F(IPCSyncChannelTest, RestrictedDispatch) {
WaitableEvent sent_ping_event(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent wait_event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
mojo::MessagePipe restricted_pipe, non_restricted_pipe;
RestrictedDispatchServer* server = new RestrictedDispatchServer(
&sent_ping_event, &wait_event, std::move(restricted_pipe.handle0));
NonRestrictedDispatchServer* server2 = new NonRestrictedDispatchServer(
&wait_event, std::move(non_restricted_pipe.handle0));
int success = 0;
std::vector<Worker*> workers;
workers.push_back(server);
workers.push_back(server2);
workers.push_back(
new RestrictedDispatchClient(&sent_ping_event, server, server2, &success,
std::move(restricted_pipe.handle1),
std::move(non_restricted_pipe.handle1)));
RunTest(workers);
EXPECT_EQ(4, success);
}
//------------------------------------------------------------------------------
// This test case inspired by crbug.com/108491
// We create two servers that use the same ListenerThread but have
// SetRestrictDispatchToSameChannel set to true.
// We create clients, then use some specific WaitableEvent wait/signalling to
// ensure that messages get dispatched in a way that causes a deadlock due to
// a nested dispatch and an eligible message in a higher-level dispatch's
// delayed_queue. Specifically, we start with client1 about so send an
// unblocking message to server1, while the shared listener thread for the
// servers server1 and server2 is about to send a non-unblocking message to
// client1. At the same time, client2 will be about to send an unblocking
// message to server2. Server1 will handle the client1->server1 message by
// telling server2 to send a non-unblocking message to client2.
// What should happen is that the send to server2 should find the pending,
// same-context client2->server2 message to dispatch, causing client2 to
// unblock then handle the server2->client2 message, so that the shared
// servers' listener thread can then respond to the client1->server1 message.
// Then client1 can handle the non-unblocking server1->client1 message.
// The old code would end up in a state where the server2->client2 message is
// sent, but the client2->server2 message (which is eligible for dispatch, and
// which is what client2 is waiting for) is stashed in a local delayed_queue
// that has server1's channel context, causing a deadlock.
// WaitableEvents in the events array are used to:
// event 0: indicate to client1 that server listener is in OnDoServerTask
// event 1: indicate to client1 that client2 listener is in OnDoClient2Task
// event 2: indicate to server1 that client2 listener is in OnDoClient2Task
// event 3: indicate to client2 that server listener is in OnDoServerTask
class RestrictedDispatchDeadlockServer : public Worker {
public:
RestrictedDispatchDeadlockServer(int server_num,
WaitableEvent* server_ready_event,
WaitableEvent** events,
RestrictedDispatchDeadlockServer* peer,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER),
server_num_(server_num),
server_ready_event_(server_ready_event),
events_(events),
peer_(peer) {}
void OnDoServerTask() {
events_[3]->Signal();
events_[2]->Wait();
events_[0]->Signal();
SendMessageToClient();
}
void Run() override {
channel()->SetRestrictDispatchChannelGroup(1);
server_ready_event_->Signal();
}
base::Thread* ListenerThread() { return Worker::ListenerThread(); }
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockServer, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, Done)
IPC_END_MESSAGE_MAP()
return true;
}
void OnNoArgs() {
if (server_num_ == 1) {
DCHECK(peer_);
peer_->SendMessageToClient();
}
}
void SendMessageToClient() {
Message* msg = new SyncChannelTestMsg_NoArgs;
msg->set_unblock(false);
DCHECK(!msg->should_unblock());
Send(msg);
}
int server_num_;
raw_ptr<WaitableEvent> server_ready_event_;
raw_ptr<WaitableEvent*, AllowPtrArithmetic> events_;
raw_ptr<RestrictedDispatchDeadlockServer, DanglingUntriaged> peer_;
};
class RestrictedDispatchDeadlockClient2 : public Worker {
public:
RestrictedDispatchDeadlockClient2(
RestrictedDispatchDeadlockServer* server,
WaitableEvent* server_ready_event,
WaitableEvent** events,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
server_ready_event_(server_ready_event),
events_(events),
received_msg_(false),
received_noarg_reply_(false),
done_issued_(false) {}
void Run() override {
server_ready_event_->Wait();
}
void OnDoClient2Task() {
events_[3]->Wait();
events_[1]->Signal();
events_[2]->Signal();
DCHECK(received_msg_ == false);
Message* message = new SyncChannelTestMsg_NoArgs;
message->set_unblock(true);
Send(message);
received_noarg_reply_ = true;
}
base::Thread* ListenerThread() { return Worker::ListenerThread(); }
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient2, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
IPC_END_MESSAGE_MAP()
return true;
}
void OnNoArgs() {
received_msg_ = true;
PossiblyDone();
}
void PossiblyDone() {
if (received_noarg_reply_ && received_msg_) {
DCHECK(done_issued_ == false);
done_issued_ = true;
Send(new SyncChannelTestMsg_Done);
Done();
}
}
raw_ptr<WaitableEvent> server_ready_event_;
raw_ptr<WaitableEvent*, AllowPtrArithmetic> events_;
bool received_msg_;
bool received_noarg_reply_;
bool done_issued_;
};
class RestrictedDispatchDeadlockClient1 : public Worker {
public:
RestrictedDispatchDeadlockClient1(
RestrictedDispatchDeadlockServer* server,
RestrictedDispatchDeadlockClient2* peer,
WaitableEvent* server_ready_event,
WaitableEvent** events,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
server_(server),
peer_(peer),
server_ready_event_(server_ready_event),
events_(events),
received_msg_(false),
received_noarg_reply_(false),
done_issued_(false) {}
void Run() override {
server_ready_event_->Wait();
server_->ListenerThread()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&RestrictedDispatchDeadlockServer::OnDoServerTask,
base::Unretained(server_)));
peer_->ListenerThread()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&RestrictedDispatchDeadlockClient2::OnDoClient2Task,
base::Unretained(peer_)));
events_[0]->Wait();
events_[1]->Wait();
DCHECK(received_msg_ == false);
Message* message = new SyncChannelTestMsg_NoArgs;
message->set_unblock(true);
Send(message);
received_noarg_reply_ = true;
PossiblyDone();
}
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchDeadlockClient1, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_NoArgs, OnNoArgs)
IPC_END_MESSAGE_MAP()
return true;
}
void OnNoArgs() {
received_msg_ = true;
PossiblyDone();
}
void PossiblyDone() {
if (received_noarg_reply_ && received_msg_) {
DCHECK(done_issued_ == false);
done_issued_ = true;
Send(new SyncChannelTestMsg_Done);
Done();
}
}
raw_ptr<RestrictedDispatchDeadlockServer, DanglingUntriaged> server_;
raw_ptr<RestrictedDispatchDeadlockClient2, DanglingUntriaged> peer_;
raw_ptr<WaitableEvent> server_ready_event_;
raw_ptr<WaitableEvent*, AllowPtrArithmetic> events_;
bool received_msg_;
bool received_noarg_reply_;
bool done_issued_;
};
TEST_F(IPCSyncChannelTest, RestrictedDispatchDeadlock) {
std::vector<Worker*> workers;
// A shared worker thread so that server1 and server2 run on one thread.
base::Thread worker_thread("RestrictedDispatchDeadlock");
ASSERT_TRUE(worker_thread.Start());
WaitableEvent server1_ready(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent server2_ready(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event0(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event1(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event2(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event3(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent* events[4] = {&event0, &event1, &event2, &event3};
RestrictedDispatchDeadlockServer* server1;
RestrictedDispatchDeadlockServer* server2;
RestrictedDispatchDeadlockClient1* client1;
RestrictedDispatchDeadlockClient2* client2;
mojo::MessagePipe pipe1, pipe2;
server2 = new RestrictedDispatchDeadlockServer(
2, &server2_ready, events, nullptr, std::move(pipe2.handle0));
server2->OverrideThread(&worker_thread);
workers.push_back(server2);
client2 = new RestrictedDispatchDeadlockClient2(
server2, &server2_ready, events, std::move(pipe2.handle1));
workers.push_back(client2);
server1 = new RestrictedDispatchDeadlockServer(
1, &server1_ready, events, server2, std::move(pipe1.handle0));
server1->OverrideThread(&worker_thread);
workers.push_back(server1);
client1 = new RestrictedDispatchDeadlockClient1(
server1, client2, &server1_ready, events, std::move(pipe1.handle1));
workers.push_back(client1);
RunTest(workers);
}
//------------------------------------------------------------------------------
// This test case inspired by crbug.com/120530
// We create 4 workers that pipe to each other W1->W2->W3->W4->W1 then we send a
// message that recurses through 3, 4 or 5 steps to make sure, say, W1 can
// re-enter when called from W4 while it's sending a message to W2.
// The first worker drives the whole test so it must be treated specially.
class RestrictedDispatchPipeWorker : public Worker {
public:
RestrictedDispatchPipeWorker(mojo::ScopedMessagePipeHandle channel_handle1,
WaitableEvent* event1,
mojo::ScopedMessagePipeHandle channel_handle2,
WaitableEvent* event2,
int group,
int* success)
: Worker(std::move(channel_handle1), Channel::MODE_SERVER),
event1_(event1),
event2_(event2),
other_channel_handle_(std::move(channel_handle2)),
group_(group),
success_(success) {}
void OnPingTTL(int ping, int* ret) {
*ret = 0;
if (!ping)
return;
other_channel_->Send(new SyncChannelTestMsg_PingTTL(ping - 1, ret));
++*ret;
}
void OnDone() {
if (is_first())
return;
other_channel_->Send(new SyncChannelTestMsg_Done);
other_channel_.reset();
Done();
}
void Run() override {
channel()->SetRestrictDispatchChannelGroup(group_);
if (is_first())
event1_->Signal();
event2_->Wait();
other_channel_ = SyncChannel::Create(
other_channel_handle_.release(), IPC::Channel::MODE_CLIENT, this,
ipc_thread().task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(), true,
shutdown_event());
other_channel_->SetRestrictDispatchChannelGroup(group_);
if (!is_first()) {
event1_->Signal();
return;
}
*success_ = 0;
int value = 0;
OnPingTTL(3, &value);
*success_ += (value == 3);
OnPingTTL(4, &value);
*success_ += (value == 4);
OnPingTTL(5, &value);
*success_ += (value == 5);
other_channel_->Send(new SyncChannelTestMsg_Done);
other_channel_.reset();
Done();
}
bool is_first() { return !!success_; }
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(RestrictedDispatchPipeWorker, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_PingTTL, OnPingTTL)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Done, OnDone)
IPC_END_MESSAGE_MAP()
return true;
}
std::unique_ptr<SyncChannel> other_channel_;
raw_ptr<WaitableEvent> event1_;
raw_ptr<WaitableEvent> event2_;
mojo::ScopedMessagePipeHandle other_channel_handle_;
int group_;
raw_ptr<int> success_;
};
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_RestrictedDispatch4WayDeadlock \
DISABLED_RestrictedDispatch4WayDeadlock
#else
#define MAYBE_RestrictedDispatch4WayDeadlock RestrictedDispatch4WayDeadlock
#endif
TEST_F(IPCSyncChannelTest, MAYBE_RestrictedDispatch4WayDeadlock) {
int success = 0;
std::vector<Worker*> workers;
WaitableEvent event0(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event1(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event2(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
WaitableEvent event3(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
mojo::MessagePipe pipe0, pipe1, pipe2, pipe3;
workers.push_back(new RestrictedDispatchPipeWorker(
std::move(pipe0.handle0), &event0, std::move(pipe1.handle1), &event1, 1,
&success));
workers.push_back(new RestrictedDispatchPipeWorker(
std::move(pipe1.handle0), &event1, std::move(pipe2.handle1), &event2, 2,
nullptr));
workers.push_back(new RestrictedDispatchPipeWorker(
std::move(pipe2.handle0), &event2, std::move(pipe3.handle1), &event3, 3,
nullptr));
workers.push_back(new RestrictedDispatchPipeWorker(
std::move(pipe3.handle0), &event3, std::move(pipe0.handle1), &event0, 4,
nullptr));
RunTest(workers);
EXPECT_EQ(3, success);
}
//------------------------------------------------------------------------------
// This test case inspired by crbug.com/122443
// We want to make sure a reply message with the unblock flag set correctly
// behaves as a reply, not a regular message.
// We have 3 workers. Server1 will send a message to Server2 (which will block),
// during which it will dispatch a message comming from Client, at which point
// it will send another message to Server2. While sending that second message it
// will receive a reply from Server1 with the unblock flag.
class ReentrantReplyServer1 : public Worker {
public:
ReentrantReplyServer1(WaitableEvent* server_ready,
mojo::ScopedMessagePipeHandle channel_handle1,
mojo::ScopedMessagePipeHandle channel_handle2)
: Worker(std::move(channel_handle1), Channel::MODE_SERVER),
server_ready_(server_ready),
other_channel_handle_(std::move(channel_handle2)) {}
void Run() override {
server2_channel_ = SyncChannel::Create(
other_channel_handle_.release(), IPC::Channel::MODE_CLIENT, this,
ipc_thread().task_runner(),
base::SingleThreadTaskRunner::GetCurrentDefault(), true,
shutdown_event());
server_ready_->Signal();
Message* msg = new SyncChannelTestMsg_Reentrant1();
server2_channel_->Send(msg);
server2_channel_.reset();
Done();
}
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer1, message)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Reentrant2, OnReentrant2)
IPC_REPLY_HANDLER(OnReply)
IPC_END_MESSAGE_MAP()
return true;
}
void OnReentrant2() {
Message* msg = new SyncChannelTestMsg_Reentrant3();
server2_channel_->Send(msg);
}
void OnReply(const Message& message) {
// If we get here, the Send() will never receive the reply (thus would
// hang), so abort instead.
LOG(FATAL) << "Reply message was dispatched";
}
raw_ptr<WaitableEvent> server_ready_;
std::unique_ptr<SyncChannel> server2_channel_;
mojo::ScopedMessagePipeHandle other_channel_handle_;
};
class ReentrantReplyServer2 : public Worker {
public:
ReentrantReplyServer2(mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_SERVER),
reply_(nullptr) {}
private:
bool OnMessageReceived(const Message& message) override {
IPC_BEGIN_MESSAGE_MAP(ReentrantReplyServer2, message)
IPC_MESSAGE_HANDLER_DELAY_REPLY(
SyncChannelTestMsg_Reentrant1, OnReentrant1)
IPC_MESSAGE_HANDLER(SyncChannelTestMsg_Reentrant3, OnReentrant3)
IPC_END_MESSAGE_MAP()
return true;
}
void OnReentrant1(Message* reply) {
DCHECK(!reply_);
reply_ = reply;
}
void OnReentrant3() {
DCHECK(reply_);
Message* reply = reply_;
reply_ = nullptr;
reply->set_unblock(true);
Send(reply);
Done();
}
raw_ptr<Message> reply_;
};
class ReentrantReplyClient : public Worker {
public:
ReentrantReplyClient(WaitableEvent* server_ready,
mojo::ScopedMessagePipeHandle channel_handle)
: Worker(std::move(channel_handle), Channel::MODE_CLIENT),
server_ready_(server_ready) {}
void Run() override {
server_ready_->Wait();
Send(new SyncChannelTestMsg_Reentrant2());
Done();
}
private:
raw_ptr<WaitableEvent> server_ready_;
};
TEST_F(IPCSyncChannelTest, ReentrantReply) {
std::vector<Worker*> workers;
WaitableEvent server_ready(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
mojo::MessagePipe pipe1, pipe2;
workers.push_back(new ReentrantReplyServer2(std::move(pipe2.handle0)));
workers.push_back(new ReentrantReplyServer1(
&server_ready, std::move(pipe1.handle0), std::move(pipe2.handle1)));
workers.push_back(
new ReentrantReplyClient(&server_ready, std::move(pipe1.handle1)));
RunTest(workers);
}
} // namespace
} // namespace IPC
|