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 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "mojo/public/c/system/trap.h"
#include <stdint.h>
#include <array>
#include <map>
#include <memory>
#include <set>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/rand_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/threading/platform_thread.h"
#include "base/threading/simple_thread.h"
#include "base/time/time.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/test/mojo_test_base.h"
#include "mojo/public/c/system/data_pipe.h"
#include "mojo/public/c/system/types.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace core {
namespace {
using TrapTest = test::MojoTestBase;
class TriggerHelper {
public:
using ContextCallback = base::RepeatingCallback<void(const MojoTrapEvent&)>;
TriggerHelper() = default;
TriggerHelper(const TriggerHelper&) = delete;
TriggerHelper& operator=(const TriggerHelper&) = delete;
~TriggerHelper() = default;
MojoResult CreateTrap(MojoHandle* handle) {
return MojoCreateTrap(&Notify, nullptr, handle);
}
template <typename Handler>
uintptr_t CreateContext(Handler handler) {
return CreateContextWithCancel(handler, [] {});
}
template <typename Handler, typename CancelHandler>
uintptr_t CreateContextWithCancel(Handler handler,
CancelHandler cancel_handler) {
auto* context =
new NotificationContext(base::BindLambdaForTesting(handler));
context->SetCancelCallback(
base::BindLambdaForTesting([cancel_handler, context] {
cancel_handler();
delete context;
}));
return reinterpret_cast<uintptr_t>(context);
}
private:
class NotificationContext {
public:
explicit NotificationContext(const ContextCallback& callback)
: callback_(callback) {}
NotificationContext(const NotificationContext&) = delete;
NotificationContext& operator=(const NotificationContext&) = delete;
~NotificationContext() = default;
void SetCancelCallback(base::OnceClosure cancel_callback) {
cancel_callback_ = std::move(cancel_callback);
}
void Notify(const MojoTrapEvent& event) {
if (event.result == MOJO_RESULT_CANCELLED && cancel_callback_)
std::move(cancel_callback_).Run();
else
callback_.Run(event);
}
private:
const ContextCallback callback_;
base::OnceClosure cancel_callback_;
};
static void Notify(const MojoTrapEvent* event) {
reinterpret_cast<NotificationContext*>(event->trigger_context)
->Notify(*event);
}
};
class ThreadedRunner : public base::SimpleThread {
public:
explicit ThreadedRunner(base::OnceClosure callback)
: SimpleThread("ThreadedRunner"), callback_(std::move(callback)) {}
ThreadedRunner(const ThreadedRunner&) = delete;
ThreadedRunner& operator=(const ThreadedRunner&) = delete;
~ThreadedRunner() override = default;
void Run() override { std::move(callback_).Run(); }
private:
base::OnceClosure callback_;
};
void ExpectNoNotification(const MojoTrapEvent* event) {
NOTREACHED();
}
void ExpectOnlyCancel(const MojoTrapEvent* event) {
EXPECT_EQ(event->result, MOJO_RESULT_CANCELLED);
}
TEST_F(TrapTest, InvalidArguments) {
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoCreateTrap(&ExpectNoNotification, nullptr, nullptr));
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectNoNotification, nullptr, &t));
// Try to add triggers for handles which don't raise trappable signals.
EXPECT_EQ(
MOJO_RESULT_INVALID_ARGUMENT,
MojoAddTrigger(t, t, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
MojoHandle buffer_handle = CreateBuffer(42);
EXPECT_EQ(
MOJO_RESULT_INVALID_ARGUMENT,
MojoAddTrigger(t, buffer_handle, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
// Try to remove a trigger on a non-trap handle.
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoRemoveTrigger(buffer_handle, 0, nullptr));
// Try to arm an invalid or non-trap handle.
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoArmTrap(MOJO_HANDLE_INVALID, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoArmTrap(buffer_handle, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(buffer_handle));
// Try to arm with a non-null count but a null output buffer.
uint32_t num_blocking_events = 1;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoArmTrap(t, nullptr, &num_blocking_events, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, TrapMessagePipeReadable) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
int num_expected_notifications = 1;
const uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_GT(num_expected_notifications, 0);
num_expected_notifications -= 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
const char kMessage1[] = "hey hey hey hey";
const char kMessage2[] = "i said hey";
const char kMessage3[] = "what's goin' on?";
// Writing to |b| multiple times should notify exactly once.
WriteMessage(b, kMessage1);
WriteMessage(b, kMessage2);
wait.Wait();
// This also shouldn't fire a notification; the trap is still disarmed.
WriteMessage(b, kMessage3);
// Arming should fail with relevant information.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(readable_a_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
// Flush the three messages from above.
EXPECT_EQ(kMessage1, ReadMessage(a));
EXPECT_EQ(kMessage2, ReadMessage(a));
EXPECT_EQ(kMessage3, ReadMessage(a));
// Now we can rearm the trap.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
}
TEST_F(TrapTest, CloseWatchedMessagePipeHandle) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t readable_a_context = helper.CreateContextWithCancel(
[](const MojoTrapEvent&) {}, [&] { wait.Signal(); });
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
// Test that closing a watched handle fires an appropriate notification, even
// when the trap is unarmed.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, CloseWatchedMessagePipeHandlePeer) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
// Test that closing a watched handle's peer with an armed trap fires an
// appropriate notification.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
wait.Wait();
// And now arming should fail with correct information about |a|'s state.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(readable_a_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_PEER_CLOSED);
EXPECT_FALSE(blocking_events[0].signals_state.satisfiable_signals &
MOJO_HANDLE_SIGNAL_READABLE);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
}
TEST_F(TrapTest, TrapDataPipeConsumerReadable) {
constexpr size_t kTestPipeCapacity = 64;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
int num_expected_notifications = 1;
const uintptr_t readable_consumer_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_GT(num_expected_notifications, 0);
num_expected_notifications -= 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, consumer, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_consumer_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
const char kMessage1[] = "hey hey hey hey";
const char kMessage2[] = "i said hey";
const char kMessage3[] = "what's goin' on?";
// Writing to |producer| multiple times should notify exactly once.
WriteData(producer, kMessage1);
WriteData(producer, kMessage2);
wait.Wait();
// This also shouldn't fire a notification; the trap is still disarmed.
WriteData(producer, kMessage3);
// Arming should fail with relevant information.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(readable_consumer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
// Flush the three messages from above.
EXPECT_EQ(kMessage1, ReadData(consumer, sizeof(kMessage1) - 1));
EXPECT_EQ(kMessage2, ReadData(consumer, sizeof(kMessage2) - 1));
EXPECT_EQ(kMessage3, ReadData(consumer, sizeof(kMessage3) - 1));
// Now we can rearm the trap.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
}
TEST_F(TrapTest, TrapDataPipeConsumerNewDataReadable) {
constexpr size_t kTestPipeCapacity = 64;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
int num_new_data_notifications = 0;
const uintptr_t new_data_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
num_new_data_notifications += 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, consumer, MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
new_data_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
const char kMessage1[] = "hey hey hey hey";
const char kMessage2[] = "i said hey";
const char kMessage3[] = "what's goin' on?";
// Writing to |producer| multiple times should notify exactly once.
WriteData(producer, kMessage1);
WriteData(producer, kMessage2);
wait.Wait();
// This also shouldn't fire a notification; the trap is still disarmed.
WriteData(producer, kMessage3);
// Arming should fail with relevant information.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(new_data_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
// Attempt to read more data than is available. Should fail but clear the
// NEW_DATA_READABLE signal.
char large_buffer[512];
uint32_t large_read_size = 512;
MojoReadDataOptions options;
options.struct_size = sizeof(options);
options.flags = MOJO_READ_DATA_FLAG_ALL_OR_NONE;
EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE,
MojoReadData(consumer, &options, large_buffer, &large_read_size));
// Attempt to arm again. Should succeed.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Write more data. Should notify.
wait.Reset();
WriteData(producer, kMessage1);
wait.Wait();
// Reading some data should clear NEW_DATA_READABLE again so we can rearm.
EXPECT_EQ(kMessage1, ReadData(consumer, sizeof(kMessage1) - 1));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(2, num_new_data_notifications);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
}
TEST_F(TrapTest, TrapDataPipeProducerWritable) {
constexpr size_t kTestPipeCapacity = 8;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
// Half the capacity of the data pipe.
const char kTestData[] = "aaaa";
static_assert((sizeof(kTestData) - 1) * 2 == kTestPipeCapacity,
"Invalid test data for this test.");
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
int num_expected_notifications = 1;
const uintptr_t writable_producer_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_GT(num_expected_notifications, 0);
num_expected_notifications -= 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, producer, MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
writable_producer_context, nullptr));
// The producer is already writable, so arming should fail with relevant
// information.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(writable_producer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
// Write some data, but don't fill the pipe yet. Arming should fail again.
WriteData(producer, kTestData);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(writable_producer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
// Write more data, filling the pipe to capacity. Arming should succeed now.
WriteData(producer, kTestData);
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Now read from the pipe, making the producer writable again. Should notify.
EXPECT_EQ(kTestData, ReadData(consumer, sizeof(kTestData) - 1));
wait.Wait();
// Arming should fail again.
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(writable_producer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
// Fill the pipe once more and arm the trap. Should succeed.
WriteData(producer, kTestData);
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
}
TEST_F(TrapTest, CloseWatchedDataPipeConsumerHandle) {
constexpr size_t kTestPipeCapacity = 8;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t readable_consumer_context = helper.CreateContextWithCancel(
[](const MojoTrapEvent&) {}, [&] { wait.Signal(); });
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, consumer, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_consumer_context, nullptr));
// Closing the consumer should fire a cancellation notification.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, CloseWatchedDataPipeConsumerHandlePeer) {
constexpr size_t kTestPipeCapacity = 8;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t readable_consumer_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, consumer, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_consumer_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Closing the producer should fire a notification for an unsatisfiable
// condition.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
wait.Wait();
// Now attempt to rearm and expect appropriate error feedback.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(readable_consumer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, blocking_events[0].result);
EXPECT_FALSE(blocking_events[0].signals_state.satisfiable_signals &
MOJO_HANDLE_SIGNAL_READABLE);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
}
TEST_F(TrapTest, CloseWatchedDataPipeProducerHandle) {
constexpr size_t kTestPipeCapacity = 8;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t writable_producer_context = helper.CreateContextWithCancel(
[](const MojoTrapEvent&) {}, [&] { wait.Signal(); });
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, producer, MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
writable_producer_context, nullptr));
// Closing the consumer should fire a cancellation notification.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, CloseWatchedDataPipeProducerHandlePeer) {
constexpr size_t kTestPipeCapacity = 8;
MojoHandle producer, consumer;
CreateDataPipe(&producer, &consumer, kTestPipeCapacity);
const char kTestMessageFullCapacity[] = "xxxxxxxx";
static_assert(sizeof(kTestMessageFullCapacity) - 1 == kTestPipeCapacity,
"Invalid test message size for this test.");
// Make the pipe unwritable initially.
WriteData(producer, kTestMessageFullCapacity);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t writable_producer_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, producer, MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
writable_producer_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Closing the consumer should fire a notification for an unsatisfiable
// condition, as the full data pipe can never be read from again and is
// therefore permanently full and unwritable.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(consumer));
wait.Wait();
// Now attempt to rearm and expect appropriate error feedback.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(writable_producer_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, blocking_events[0].result);
EXPECT_FALSE(blocking_events[0].signals_state.satisfiable_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(producer));
}
TEST_F(TrapTest, ArmWithNoTriggers) {
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectNoNotification, nullptr, &t));
EXPECT_EQ(MOJO_RESULT_NOT_FOUND, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, DuplicateTriggerContext) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectOnlyCancel, nullptr, &t));
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
EXPECT_EQ(
MOJO_RESULT_ALREADY_EXISTS,
MojoAddTrigger(t, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, RemoveUnknownTrigger) {
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectNoNotification, nullptr, &t));
EXPECT_EQ(MOJO_RESULT_NOT_FOUND, MojoRemoveTrigger(t, 1234, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, ArmWithTriggerConditionAlreadySatisfied) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectOnlyCancel, nullptr, &t));
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
// |a| is always writable, so we can never arm this trap.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(0u, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
}
TEST_F(TrapTest, ArmWithTriggerConditionAlreadyUnsatisfiable) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectOnlyCancel, nullptr, &t));
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, 0, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
// |b| is closed and never wrote any messages, so |a| won't be readable again.
// MojoArmTrap() should fail, incidcating as much.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = kMaxBlockingEvents;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(0u, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_PEER_CLOSED);
EXPECT_FALSE(blocking_events[0].signals_state.satisfiable_signals &
MOJO_HANDLE_SIGNAL_READABLE);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
}
TEST_F(TrapTest, MultipleTriggers) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
base::WaitableEvent a_wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent b_wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
int num_a_notifications = 0;
int num_b_notifications = 0;
uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
num_a_notifications += 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
a_wait.Signal();
});
uintptr_t readable_b_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
num_b_notifications += 1;
EXPECT_EQ(MOJO_RESULT_OK, event.result);
b_wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
// Add two independent triggers to trap |a| or |b| readability.
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_b_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
const char kMessage1[] = "things are happening";
const char kMessage2[] = "ok. ok. ok. ok.";
const char kMessage3[] = "plz wake up";
// Writing to |b| should signal |a|'s watch.
WriteMessage(b, kMessage1);
a_wait.Wait();
a_wait.Reset();
// Subsequent messages on |b| should not trigger another notification.
WriteMessage(b, kMessage2);
WriteMessage(b, kMessage3);
// Messages on |a| also shouldn't trigger |b|'s notification, since the
// trap should be disarmed by now.
WriteMessage(a, kMessage1);
WriteMessage(a, kMessage2);
WriteMessage(a, kMessage3);
// Arming should fail. Since we only ask for at most one context's information
// that's all we should get back. Which one we get is unspecified.
constexpr size_t kMaxBlockingEvents = 3;
uint32_t num_blocking_events = 1;
MojoTrapEvent blocking_events[kMaxBlockingEvents] = {
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])},
{sizeof(blocking_events[0])}};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_TRUE(blocking_events[0].trigger_context == readable_a_context ||
blocking_events[0].trigger_context == readable_b_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
// Now try arming again, verifying that both contexts are returned.
num_blocking_events = kMaxBlockingEvents;
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(2u, num_blocking_events);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[1].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
EXPECT_TRUE(blocking_events[1].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
EXPECT_TRUE((blocking_events[0].trigger_context == readable_a_context &&
blocking_events[1].trigger_context == readable_b_context) ||
(blocking_events[0].trigger_context == readable_b_context &&
blocking_events[1].trigger_context == readable_a_context));
// Flush out the test messages so we should be able to successfully rearm.
EXPECT_EQ(kMessage1, ReadMessage(a));
EXPECT_EQ(kMessage2, ReadMessage(a));
EXPECT_EQ(kMessage3, ReadMessage(a));
EXPECT_EQ(kMessage1, ReadMessage(b));
EXPECT_EQ(kMessage2, ReadMessage(b));
EXPECT_EQ(kMessage3, ReadMessage(b));
// Add a trigger whose condition is always satisfied so we can't arm. Arming
// should fail with only this new watch's information.
uintptr_t writable_c_context =
helper.CreateContext([](const MojoTrapEvent&) { NOTREACHED(); });
MojoHandle c, d;
CreateMessagePipe(&c, &d);
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, c, MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
writable_c_context, nullptr));
num_blocking_events = kMaxBlockingEvents;
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_events[0]));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(writable_c_context, blocking_events[0].trigger_context);
EXPECT_EQ(MOJO_RESULT_OK, blocking_events[0].result);
EXPECT_TRUE(blocking_events[0].signals_state.satisfied_signals &
MOJO_HANDLE_SIGNAL_WRITABLE);
// Remove the new trigger and arming should succeed once again.
EXPECT_EQ(MOJO_RESULT_OK, MojoRemoveTrigger(t, writable_c_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
}
TEST_F(TrapTest, ActivateOtherTriggerFromEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hello a";
static const char kTestMessageToB[] = "hello b";
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ("hello a", ReadMessage(a));
// Re-arm the trap and signal |b|.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(a, kTestMessageToB);
});
uintptr_t readable_b_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToB, ReadMessage(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
wait.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_b_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Send a message to |a|. The relevant trigger should be notified and the
// event handler should send a message to |b|, in turn notifying the other
// trigger. The second event handler will signal |wait|.
WriteMessage(b, kTestMessageToA);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, ActivateSameTriggerFromEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hello a";
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
int expected_notifications = 10;
uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ("hello a", ReadMessage(a));
EXPECT_GT(expected_notifications, 0);
expected_notifications -= 1;
if (expected_notifications == 0) {
wait.Signal();
return;
} else {
// Re-arm the trap and signal |a| again.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(b, kTestMessageToA);
}
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Send a message to |a|. When the trigger above is activated, the event
// handler will rearm the trap and send another message to |a|. This will
// happen until |expected_notifications| reaches 0.
WriteMessage(b, kTestMessageToA);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, ImplicitRemoveOtherTriggerWithinEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle c, d;
CreateMessagePipe(&c, &d);
static const char kTestMessageToA[] = "hi a";
static const char kTestMessageToC[] = "hi c";
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
uintptr_t readable_a_context = helper.CreateContextWithCancel(
[](const MojoTrapEvent&) { NOTREACHED(); }, [&] { wait.Signal(); });
uintptr_t readable_c_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToC, ReadMessage(c));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Must result in exactly ONE notification from the above trigger, for
// CANCELLED only. Because we cannot dispatch notifications until the
// stack unwinds, and because we must never dispatch non-cancellation
// notifications for a handle once it's been closed, we must be certain
// that cancellation due to closure preemptively invalidates any
// pending non-cancellation notifications queued on the current
// RequestContext, such as the one resulting from the WriteMessage here.
WriteMessage(b, kTestMessageToA);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
// Rearming should be fine since |a|'s trigger should already be
// implicitly removed (even though the notification will not have
// been invoked yet.)
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Nothing interesting should happen as a result of this.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, c, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_c_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(d, kTestMessageToC);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
}
TEST_F(TrapTest, ExplicitRemoveOtherTriggerWithinEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle c, d;
CreateMessagePipe(&c, &d);
static const char kTestMessageToA[] = "hi a";
static const char kTestMessageToC[] = "hi c";
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
uintptr_t readable_a_context =
helper.CreateContext([](const MojoTrapEvent&) { NOTREACHED(); });
uintptr_t readable_c_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToC, ReadMessage(c));
// Now rearm the trap.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Should result in no notifications from the above trigger, because the
// trigger will have been removed by the time the event handler can
// execute.
WriteMessage(b, kTestMessageToA);
WriteMessage(b, kTestMessageToA);
EXPECT_EQ(MOJO_RESULT_OK,
MojoRemoveTrigger(t, readable_a_context, nullptr));
// Rearming should be fine now.
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Nothing interesting should happen as a result of these.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
wait.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, c, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_c_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(d, kTestMessageToC);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
}
TEST_F(TrapTest, NestedCancellation) {
if (IsMojoIpczEnabled()) {
GTEST_SKIP() << "This test expects trap handlers to be reentrant in some "
<< "edge cases, which is an unsafe artifact of pre-ipcz Mojo "
<< "that is unsupported by MojoIpcz. This case should not be "
<< "relevant to any current production usage.";
}
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle c, d;
CreateMessagePipe(&c, &d);
static const char kTestMessageToA[] = "hey a";
static const char kTestMessageToC[] = "hey c";
static const char kTestMessageToD[] = "hey d";
// This is a tricky test. It establishes a trigger on |b| using one trap and
// triggers on |c| and |d| using another trap.
//
// A message is written to |d| to activate |c|'s trigger, and the resuling
// event handler invocation does the folllowing:
// 1. Writes to |a| to eventually activate |b|'s trigger.
// 2. Rearms |c|'s trap.
// 3. Writes to |d| to eventually activate |c|'s trigger again.
//
// Meanwhile, |b|'s event handler removes |c|'s trigger altogether before
// writing to |c| to activate |d|'s trigger.
//
// The net result should be that |c|'s trigger only gets activated once (from
// the first write to |d| above) and everyone else gets notified as expected.
MojoHandle b_trap;
MojoHandle cd_trap;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&b_trap));
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&cd_trap));
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
uintptr_t readable_d_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToD, ReadMessage(d));
wait.Signal();
});
int num_expected_c_notifications = 1;
uintptr_t readable_c_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_GT(num_expected_c_notifications--, 0);
// Trigger an eventual |readable_b_context| notification.
WriteMessage(a, kTestMessageToA);
EXPECT_EQ(kTestMessageToC, ReadMessage(c));
EXPECT_EQ(MOJO_RESULT_OK,
MojoArmTrap(cd_trap, nullptr, nullptr, nullptr));
// Trigger another eventual |readable_c_context| notification.
WriteMessage(d, kTestMessageToC);
});
uintptr_t readable_b_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK,
MojoRemoveTrigger(cd_trap, readable_c_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoArmTrap(cd_trap, nullptr, nullptr, nullptr));
WriteMessage(c, kTestMessageToD);
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(b_trap, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_b_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(cd_trap, c, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_c_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(cd_trap, d, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_d_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(b_trap, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(cd_trap, nullptr, nullptr, nullptr));
WriteMessage(d, kTestMessageToC);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(cd_trap));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b_trap));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
}
TEST_F(TrapTest, RemoveSelfWithinEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hey a";
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
static uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
// Subtle: Captures may be invalidated by MojoRemoveTrigger() below,
// since it may destroy this lambda. Copy the values we'll need for the
// remainder of this function.
MojoHandle a_handle = a;
MojoHandle trap = t;
uintptr_t context = readable_a_context;
auto& wait_event = wait;
// There should be no problem removing this trigger from its own
// notification invocation.
EXPECT_EQ(MOJO_RESULT_OK, MojoRemoveTrigger(trap, context, nullptr));
EXPECT_EQ(kTestMessageToA, ReadMessage(a_handle));
// Arming should fail because there are no longer any registered
// triggers on the trap.
EXPECT_EQ(MOJO_RESULT_NOT_FOUND,
MojoArmTrap(trap, nullptr, nullptr, nullptr));
// And closing |a| should be fine (and should not invoke this
// notification with MOJO_RESULT_CANCELLED) for the same reason.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a_handle));
wait_event.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(b, kTestMessageToA);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, CloseTrapWithinEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA1[] = "hey a";
static const char kTestMessageToA2[] = "hey a again";
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToA1, ReadMessage(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// There should be no problem closing this trap from its own
// notification callback. Note that it may however delete this lambda
// and invalidate any captures, so we copy some captured values for use
// below.
MojoHandle a_handle = a;
MojoHandle b_handle = b;
base::WaitableEvent& wait_event = wait;
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
// And these should not trigger more notifications, because |t| has been
// closed already.
WriteMessage(b_handle, kTestMessageToA2);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b_handle));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a_handle));
wait_event.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(b, kTestMessageToA1);
wait.Wait();
}
TEST_F(TrapTest, CloseTrapAfterImplicitTriggerRemoval) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hey a";
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToA, ReadMessage(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// Closing `a` deletes its trigger which may in turn delete this lambda
// and invalidate any captures. Copy the values we need first.
base::WaitableEvent& wait_event = wait;
MojoHandle trap = t;
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(trap));
wait_event.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(b, kTestMessageToA);
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, OtherThreadRemovesTriggerDuringEventHandler) {
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hey a";
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
base::WaitableEvent wait_for_notification(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent wait_for_cancellation(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
static bool callback_done = false;
uintptr_t readable_a_context = helper.CreateContextWithCancel(
[&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToA, ReadMessage(a));
wait_for_notification.Signal();
// Give the other thread sufficient time to race with the completion
// of this callback. There should be no race, since the cancellation
// notification must be mutually exclusive to this notification.
base::PlatformThread::Sleep(base::Seconds(1));
callback_done = true;
},
[&] {
EXPECT_TRUE(callback_done);
wait_for_cancellation.Signal();
});
ThreadedRunner runner(base::BindLambdaForTesting([&] {
wait_for_notification.Wait();
// Cancel the watch while the notification is still running.
EXPECT_EQ(MOJO_RESULT_OK,
MojoRemoveTrigger(t, readable_a_context, nullptr));
wait_for_cancellation.Wait();
EXPECT_TRUE(callback_done);
}));
runner.Start();
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
WriteMessage(b, kTestMessageToA);
runner.Join();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, TriggersRemoveEachOtherWithinEventHandlers) {
if (IsMojoIpczEnabled()) {
GTEST_SKIP() << "This test deadlocks with MojoIpcz, because it expects "
<< "trap handlers to be re-entrant in some edge cases. Not "
<< "relevant to any current production usage.";
}
MojoHandle a, b;
CreateMessagePipe(&a, &b);
static const char kTestMessageToA[] = "hey a";
static const char kTestMessageToB[] = "hey b";
base::WaitableEvent wait_for_a_to_notify(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent wait_for_b_to_notify(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent wait_for_a_to_cancel(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
base::WaitableEvent wait_for_b_to_cancel(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
MojoHandle a_trap;
MojoHandle b_trap;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&a_trap));
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&b_trap));
// We set up two traps, one triggered on |a| readability and one triggered on
// |b| readability. Each removes the other's trigger from within its own event
// handler. This should be safe, i.e., it should not deadlock in spite of the
// fact that we also guarantee mutually exclusive event handler invocation
// (including cancellations) on any given trap.
bool a_cancelled = false;
bool b_cancelled = false;
static uintptr_t readable_b_context;
uintptr_t readable_a_context = helper.CreateContextWithCancel(
[&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToA, ReadMessage(a));
// Our removal from the other handler may delete this lambda and
// invalidate its captures. Copy the references we need first.
base::WaitableEvent& wait_for_b = wait_for_b_to_notify;
MojoHandle b_trap_handle = b_trap;
uintptr_t b_context = readable_b_context;
wait_for_a_to_notify.Signal();
wait_for_b.Wait();
EXPECT_EQ(MOJO_RESULT_OK,
MojoRemoveTrigger(b_trap_handle, b_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b_trap_handle));
},
[&] {
a_cancelled = true;
wait_for_a_to_cancel.Signal();
wait_for_b_to_cancel.Wait();
});
readable_b_context = helper.CreateContextWithCancel(
[&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
EXPECT_EQ(kTestMessageToB, ReadMessage(b));
// Our removal from the other handler may delete this lambda and
// invalidate its captures. Copy the references we need first.
base::WaitableEvent& wait_for_a = wait_for_a_to_notify;
MojoHandle a_trap_handle = a_trap;
uintptr_t a_context = readable_a_context;
wait_for_b_to_notify.Signal();
wait_for_a.Wait();
EXPECT_EQ(MOJO_RESULT_OK,
MojoRemoveTrigger(a_trap_handle, a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a_trap_handle));
},
[&] {
b_cancelled = true;
wait_for_b_to_cancel.Signal();
wait_for_a_to_cancel.Wait();
});
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(a_trap, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(a_trap, nullptr, nullptr, nullptr));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(b_trap, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_b_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(b_trap, nullptr, nullptr, nullptr));
ThreadedRunner runner(base::BindOnce(
[](MojoHandle b) { WriteMessage(b, kTestMessageToA); }, b));
runner.Start();
// To enforce that the two traps run concurrently, wait until the WriteMessage
// above has made a readable before firing the readable trap on b.
wait_for_a_to_notify.Wait();
WriteMessage(a, kTestMessageToB);
wait_for_a_to_cancel.Wait();
wait_for_b_to_cancel.Wait();
runner.Join();
EXPECT_TRUE(a_cancelled);
EXPECT_TRUE(b_cancelled);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, AlwaysCancel) {
// Basic sanity check to ensure that all possible ways to remove a trigger
// result in a final MOJO_RESULT_CANCELLED notification.
MojoHandle a, b;
CreateMessagePipe(&a, &b);
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
auto ignore_event = [](const MojoTrapEvent&) {};
auto signal_wait = [&] { wait.Signal(); };
// Cancel via |MojoRemoveTrigger()|.
uintptr_t context = helper.CreateContextWithCancel(ignore_event, signal_wait);
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, context,
nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoRemoveTrigger(t, context, nullptr));
wait.Wait();
wait.Reset();
// Cancel by closing the trigger's watched handle.
context = helper.CreateContextWithCancel(ignore_event, signal_wait);
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, context,
nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
wait.Wait();
wait.Reset();
// Cancel by closing the trap handle.
context = helper.CreateContextWithCancel(ignore_event, signal_wait);
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, b, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, context,
nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
}
TEST_F(TrapTest, ArmFailureCirculation) {
// Sanity check to ensure that all ready trigger events will eventually be
// returned over a finite number of calls to MojoArmTrap().
constexpr size_t kNumTestPipes = 100;
constexpr size_t kNumTestHandles = kNumTestPipes * 2;
std::array<MojoHandle, kNumTestHandles> handles;
// Create a bunch of pipes and make sure they're all readable.
for (size_t i = 0; i < kNumTestPipes; ++i) {
CreateMessagePipe(&handles[i], &handles[i + kNumTestPipes]);
WriteMessage(handles[i], "hey");
WriteMessage(handles[i + kNumTestPipes], "hay");
WaitForSignals(handles[i], MOJO_HANDLE_SIGNAL_READABLE);
WaitForSignals(handles[i + kNumTestPipes], MOJO_HANDLE_SIGNAL_READABLE);
}
// Create a trap and watch all of them for readability.
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateTrap(&ExpectOnlyCancel, nullptr, &t));
for (size_t i = 0; i < kNumTestHandles; ++i) {
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(t, handles[i], MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED, i, nullptr));
}
// Keep trying to arm |t| until every trigger gets an entry in
// |ready_contexts|. If MojoArmTrap() is well-behaved, this should terminate
// eventually.
std::set<uintptr_t> ready_contexts;
while (ready_contexts.size() < kNumTestHandles) {
uint32_t num_blocking_events = 1;
MojoTrapEvent blocking_event = {sizeof(blocking_event)};
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoArmTrap(t, nullptr, &num_blocking_events, &blocking_event));
EXPECT_EQ(1u, num_blocking_events);
EXPECT_EQ(MOJO_RESULT_OK, blocking_event.result);
ready_contexts.insert(blocking_event.trigger_context);
}
for (size_t i = 0; i < kNumTestHandles; ++i)
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handles[i]));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
}
TEST_F(TrapTest, TriggerOnUnsatisfiedSignals) {
if (IsMojoIpczEnabled()) {
GTEST_SKIP() << "Monitoring for unsatisfied signals is not supported by "
<< "MojoIpcz.";
}
MojoHandle a, b;
CreateMessagePipe(&a, &b);
base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
TriggerHelper helper;
const uintptr_t readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
MojoHandle t;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
const char kMessage[] = "this is not a message";
WriteMessage(b, kMessage);
wait.Wait();
// Now we know |a| is readable. Remove the trigger and add a new one to watch
// for a not-readable state.
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
const uintptr_t not_readable_a_context =
helper.CreateContext([&](const MojoTrapEvent& event) {
EXPECT_EQ(MOJO_RESULT_OK, event.result);
wait.Signal();
});
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED,
not_readable_a_context, nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
// This should not block, because the event should be signaled by
// |not_readable_a_context| when we read the only available message off of
// |a|.
wait.Reset();
EXPECT_EQ(kMessage, ReadMessage(a));
wait.Wait();
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(t));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
}
TEST_F(TrapTest, TriggerDuringDestruction) {
// Regression test for races between trap event firing and trap destruction.
// See https://crbug.com/1385643.
MojoHandle a, b;
CreateMessagePipe(&a, &b);
auto drain_a = [&](const MojoTrapEvent& event) {
MojoMessageHandle m;
while (MojoReadMessage(a, nullptr, &m) == MOJO_RESULT_OK) {
MojoDestroyMessage(m);
}
};
constexpr size_t kNumIterations = 1000;
for (size_t i = 0; i < kNumIterations; ++i) {
MojoHandle t;
TriggerHelper helper;
EXPECT_EQ(MOJO_RESULT_OK, helper.CreateTrap(&t));
EXPECT_EQ(MOJO_RESULT_OK,
MojoAddTrigger(t, a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
helper.CreateContext(drain_a), nullptr));
drain_a(MojoTrapEvent{});
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(t, nullptr, nullptr, nullptr));
ThreadedRunner writer(
base::BindLambdaForTesting([&] { WriteMessage(b, "ping!"); }));
ThreadedRunner closer(base::BindLambdaForTesting([&] { MojoClose(t); }));
closer.Start();
writer.Start();
writer.Join();
closer.Join();
}
MojoClose(a);
MojoClose(b);
}
TEST_F(TrapTest, RaceDispatchAndBlockedCancel) {
// Regression test for https://crbug.com/1508753. This bug was caused by
// reordering of a MOJO_RESULT_CANCELLED event to before some other event for
// the same trap context, violating an API constraint that must be upheld for
// memory safety in application code. The scenario which could elicit the bug
// was as follows:
//
// 1. A single trap is watching two pipes, P and Q.
// 2. Thread A closes pipe P, triggering a CANCELLED event.
// 3. Thread A re-arms the trap from within the CANCELLED event handler.
// 4. Thread B changes Q's state to elicit a event for Q (not CANCELLED).
// 5. Thread B dispatch is blocked because thread A is still dispatching.
// 6. Before thread B gets a chance to be scheduled, thread A closes Q.
// 7. Thread A dispatches a CANCELLED event for Q.
// 8. Thread B is scheduled and proceeds to dispatch its Q event. [BAD]
struct State;
struct Pipe {
explicit Pipe(State* state) : state(state) { CreateMessagePipe(&a, &b); }
uintptr_t context() const { return reinterpret_cast<uintptr_t>(this); }
MojoHandle a;
MojoHandle b;
bool trigger_cancelled = false;
// Back-reference to common state so it's reachable from the event handler.
const raw_ptr<State> state;
};
struct State {
Pipe pipe0{this};
Pipe pipe1{this};
MojoHandle trap;
base::WaitableEvent event;
};
State state;
// NOTE: + to turn the lambda into a function pointer.
const MojoTrapEventHandler event_handler = +[](const MojoTrapEvent* event) {
auto& pipe = *reinterpret_cast<Pipe*>(event->trigger_context);
auto& state = *pipe.state;
// If the bug is present, this expectation can fail flakily. No event should
// fire for a pipe after its watch has been cancelled.
EXPECT_FALSE(pipe.trigger_cancelled);
if (event->result == MOJO_RESULT_CANCELLED) {
pipe.trigger_cancelled = true;
if (&pipe == &state.pipe0) {
// When pipe0's watch is cancelled (on the main thread by closure down
// below) we re-arm the trap immediately. This must succeed because
// `pipe1.a` is now the only handle being watched, and it's still in an
// uninteresting state.
EXPECT_EQ(MOJO_RESULT_OK,
MojoArmTrap(state.trap, nullptr, nullptr, nullptr));
// Unblock the other thread so it can elicit a trap event on pipe1 now
// that the trap is re-armed. It will still block just before
// dispatching as long as we're still in this event handler on the main
// thread.
state.event.Signal();
// A nice long delay to make it very likely for the waiting
// ThreadedRunner to progress right up to its event dispatch.
base::PlatformThread::Sleep(base::Milliseconds(10));
// Trigger cancellation for pipe1 by closing its `a`. This will queue a
// CANCELLED event to fire on the same thread immediately after we
// return from this handler.
MojoClose(state.pipe1.a);
}
}
};
EXPECT_EQ(MOJO_RESULT_OK,
MojoCreateTrap(event_handler, nullptr, &state.trap));
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(state.trap, state.pipe0.a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
state.pipe0.context(), nullptr));
EXPECT_EQ(
MOJO_RESULT_OK,
MojoAddTrigger(state.trap, state.pipe1.a, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
state.pipe1.context(), nullptr));
EXPECT_EQ(MOJO_RESULT_OK, MojoArmTrap(state.trap, nullptr, nullptr, nullptr));
ThreadedRunner close_pipe1_b(base::BindLambdaForTesting([&] {
state.event.Wait();
MojoClose(state.pipe1.b);
}));
close_pipe1_b.Start();
// Trigger cancellation of the watch on `pipe0.a`. See event_handler above.
MojoClose(state.pipe0.a);
close_pipe1_b.Join();
MojoClose(state.pipe0.b);
MojoClose(state.trap);
}
base::RepeatingClosure g_do_random_thing_callback;
void ReadAllMessages(const MojoTrapEvent* event) {
if (event->result == MOJO_RESULT_OK) {
MojoHandle handle = static_cast<MojoHandle>(event->trigger_context);
MojoMessageHandle message;
while (MojoReadMessage(handle, nullptr, &message) == MOJO_RESULT_OK)
MojoDestroyMessage(message);
}
constexpr size_t kNumRandomThingsToDoOnNotify = 5;
for (size_t i = 0; i < kNumRandomThingsToDoOnNotify; ++i)
g_do_random_thing_callback.Run();
}
MojoHandle RandomHandle(MojoHandle* handles, size_t size) {
return handles[base::RandInt(0, static_cast<int>(size) - 1)];
}
void DoRandomThing(MojoHandle* traps,
size_t num_traps,
MojoHandle* watched_handles,
size_t num_watched_handles) {
switch (base::RandInt(0, 10)) {
case 0:
MojoClose(RandomHandle(traps, num_traps));
break;
case 1:
MojoClose(RandomHandle(watched_handles, num_watched_handles));
break;
case 2:
case 3:
case 4: {
MojoMessageHandle message;
ASSERT_EQ(MOJO_RESULT_OK, MojoCreateMessage(nullptr, &message));
ASSERT_EQ(MOJO_RESULT_OK,
MojoSetMessageContext(message, 1, nullptr, nullptr, nullptr));
MojoWriteMessage(RandomHandle(watched_handles, num_watched_handles),
message, nullptr);
break;
}
case 5:
case 6: {
MojoHandle t = RandomHandle(traps, num_traps);
MojoHandle h = RandomHandle(watched_handles, num_watched_handles);
MojoAddTrigger(t, h, MOJO_HANDLE_SIGNAL_READABLE,
MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
static_cast<uintptr_t>(h), nullptr);
break;
}
case 7:
case 8: {
uint32_t num_blocking_events = 1;
MojoTrapEvent blocking_event = {sizeof(blocking_event)};
if (MojoArmTrap(RandomHandle(traps, num_traps), nullptr,
&num_blocking_events,
&blocking_event) == MOJO_RESULT_FAILED_PRECONDITION &&
blocking_event.result == MOJO_RESULT_OK) {
ReadAllMessages(&blocking_event);
}
break;
}
case 9:
case 10: {
MojoHandle t = RandomHandle(traps, num_traps);
MojoHandle h = RandomHandle(watched_handles, num_watched_handles);
MojoRemoveTrigger(t, static_cast<uintptr_t>(h), nullptr);
break;
}
default:
NOTREACHED();
}
}
TEST_F(TrapTest, ConcurrencyStressTest) {
// Regression test for https://crbug.com/740044. Exercises racy usage of the
// trap API to weed out potential crashes.
if (IsMojoIpczEnabled()) {
GTEST_SKIP() << "This test relies on implementation assumptions which are "
<< "invalid when MojoIpcz is enabled; namely that it's safe "
<< "to attempt operations on invalid handles.";
}
constexpr size_t kNumTraps = 50;
constexpr size_t kNumWatchedHandles = 50;
static_assert(kNumWatchedHandles % 2 == 0, "Invalid number of test handles.");
constexpr size_t kNumThreads = 10;
static constexpr size_t kNumOperationsPerThread = 400;
MojoHandle traps[kNumTraps];
MojoHandle watched_handles[kNumWatchedHandles];
g_do_random_thing_callback = base::BindRepeating(
&DoRandomThing, traps, kNumTraps, watched_handles, kNumWatchedHandles);
for (size_t i = 0; i < kNumTraps; ++i)
MojoCreateTrap(&ReadAllMessages, nullptr, &traps[i]);
for (size_t i = 0; i < kNumWatchedHandles; i += 2)
CreateMessagePipe(&watched_handles[i], &watched_handles[i + 1]);
std::array<std::unique_ptr<ThreadedRunner>, kNumThreads> threads;
for (size_t i = 0; i < kNumThreads; ++i) {
threads[i] = std::make_unique<ThreadedRunner>(base::BindOnce([] {
for (size_t i = 0; i < kNumOperationsPerThread; ++i)
g_do_random_thing_callback.Run();
}));
threads[i]->Start();
}
for (size_t i = 0; i < kNumThreads; ++i)
threads[i]->Join();
for (size_t i = 0; i < kNumTraps; ++i)
MojoClose(traps[i]);
for (size_t i = 0; i < kNumWatchedHandles; ++i)
MojoClose(watched_handles[i]);
}
} // namespace
} // namespace core
} // namespace mojo
|