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
|
//===--- rtsan_test_interceptors.cpp - Realtime Sanitizer -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_platform.h"
#if SANITIZER_POSIX
#include "gtest/gtest.h"
#include "sanitizer_common/sanitizer_platform_interceptors.h"
#include "rtsan_test_utilities.h"
#if SANITIZER_APPLE
#include <libkern/OSAtomic.h>
#include <os/lock.h>
#include <unistd.h>
#endif
#if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC
#include <malloc.h>
#endif
#if SANITIZER_INTERCEPT_EPOLL
#include <sys/epoll.h>
#endif
#if SANITIZER_INTERCEPT_KQUEUE
#include <sys/event.h>
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#if SANITIZER_LINUX
#include <sys/eventfd.h>
#include <sys/inotify.h>
#include <sys/timerfd.h>
#endif
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
#if _FILE_OFFSET_BITS == 64 && SANITIZER_GLIBC
// Under these conditions, some system calls are `foo64` instead of `foo`
#define MAYBE_APPEND_64(func) func "64"
#else
#define MAYBE_APPEND_64(func) func
#endif
#if SANITIZER_INTERCEPT_FREE_SIZED
extern "C" void free_sized(void *ptr, size_t size);
#endif
#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
extern "C" void free_aligned_sized(void *ptr, size_t alignment, size_t size);
#endif
using namespace testing;
using namespace rtsan_testing;
using namespace std::chrono_literals;
// NOTE: In the socket tests we pass in bad info to the calls to ensure they
// fail which is why we EXPECT_NE 0 for their return codes.
// We just care that the call is intercepted
const int kNotASocketFd = 0;
void *FakeThreadEntryPoint(void *) { return nullptr; }
class RtsanFileTest : public ::testing::Test {
protected:
void SetUp() override {
const ::testing::TestInfo *const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
file_path_ = std::string("/tmp/rtsan_temporary_test_file_") +
test_info->name() + ".txt";
RemoveTemporaryFile();
}
// Gets a file path with the test's name in it
// This file will be removed if it exists at the end of the test
const char *GetTemporaryFilePath() const { return file_path_.c_str(); }
void TearDown() override { RemoveTemporaryFile(); }
private:
void RemoveTemporaryFile() const { std::remove(GetTemporaryFilePath()); }
std::string file_path_;
};
/*
Allocation and deallocation
*/
TEST(TestRtsanInterceptors, MallocDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(nullptr, malloc(1)); };
ExpectRealtimeDeath(Func, "malloc");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, CallocDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(nullptr, calloc(2, 4)); };
ExpectRealtimeDeath(Func, "calloc");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, ReallocDiesWhenRealtime) {
void *ptr_1 = malloc(1);
auto Func = [ptr_1]() { EXPECT_NE(nullptr, realloc(ptr_1, 8)); };
ExpectRealtimeDeath(Func, "realloc");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_APPLE
TEST(TestRtsanInterceptors, ReallocfDiesWhenRealtime) {
void *ptr_1 = malloc(1);
auto Func = [ptr_1]() { EXPECT_NE(nullptr, reallocf(ptr_1, 8)); };
ExpectRealtimeDeath(Func, "reallocf");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(nullptr, valloc(4)); };
ExpectRealtimeDeath(Func, "valloc");
ExpectNonRealtimeSurvival(Func);
}
#if __has_builtin(__builtin_available) && SANITIZER_APPLE
#define ALIGNED_ALLOC_AVAILABLE() (__builtin_available(macOS 10.15, *))
#else
// We are going to assume this is true until we hit systems where it isn't
#define ALIGNED_ALLOC_AVAILABLE() (true)
#endif
TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
if (ALIGNED_ALLOC_AVAILABLE()) {
auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
ExpectRealtimeDeath(Func, "aligned_alloc");
ExpectNonRealtimeSurvival(Func);
}
}
TEST(TestRtsanInterceptors, FreeDiesWhenRealtime) {
void *ptr_1 = malloc(1);
void *ptr_2 = malloc(1);
ExpectRealtimeDeath([ptr_1]() { free(ptr_1); }, "free");
ExpectNonRealtimeSurvival([ptr_2]() { free(ptr_2); });
// Prevent malloc/free pair being optimised out
ASSERT_NE(nullptr, ptr_1);
ASSERT_NE(nullptr, ptr_2);
}
#if SANITIZER_INTERCEPT_FREE_SIZED
TEST(TestRtsanInterceptors, FreeSizedDiesWhenRealtime) {
void *ptr_1 = malloc(1);
void *ptr_2 = malloc(1);
ExpectRealtimeDeath([ptr_1]() { free_sized(ptr_1, 1); }, "free_sized");
ExpectNonRealtimeSurvival([ptr_2]() { free_sized(ptr_2, 1); });
// Prevent malloc/free pair being optimised out
ASSERT_NE(nullptr, ptr_1);
ASSERT_NE(nullptr, ptr_2);
}
#endif
#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
TEST(TestRtsanInterceptors, FreeAlignedSizedDiesWhenRealtime) {
if (ALIGNED_ALLOC_AVAILABLE()) {
void *ptr_1 = aligned_alloc(16, 32);
void *ptr_2 = aligned_alloc(16, 32);
ExpectRealtimeDeath([ptr_1]() { free_aligned_sized(ptr_1, 16, 32); },
"free_aligned_sized");
ExpectNonRealtimeSurvival([ptr_2]() { free_aligned_sized(ptr_2, 16, 32); });
// Prevent malloc/free pair being optimised out
ASSERT_NE(nullptr, ptr_1);
ASSERT_NE(nullptr, ptr_2);
}
}
#endif
TEST(TestRtsanInterceptors, FreeSurvivesWhenRealtimeIfArgumentIsNull) {
RealtimeInvoke([]() { free(NULL); });
ExpectNonRealtimeSurvival([]() { free(NULL); });
}
#if SANITIZER_INTERCEPT_FREE_SIZED
TEST(TestRtsanInterceptors, FreeSizedSurvivesWhenRealtimeIfArgumentIsNull) {
RealtimeInvoke([]() { free_sized(NULL, 0); });
ExpectNonRealtimeSurvival([]() { free_sized(NULL, 0); });
}
#endif
#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
TEST(TestRtsanInterceptors,
FreeAlignedSizedSurvivesWhenRealtimeIfArgumentIsNull) {
RealtimeInvoke([]() { free_aligned_sized(NULL, 0, 0); });
ExpectNonRealtimeSurvival([]() { free_aligned_sized(NULL, 0, 0); });
}
#endif
TEST(TestRtsanInterceptors, PosixMemalignDiesWhenRealtime) {
auto Func = []() {
void *ptr;
posix_memalign(&ptr, 4, 4);
};
ExpectRealtimeDeath(Func, "posix_memalign");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_MEMALIGN
TEST(TestRtsanInterceptors, MemalignDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(memalign(2, 2048), nullptr); };
ExpectRealtimeDeath(Func, "memalign");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_PVALLOC
TEST(TestRtsanInterceptors, PvallocDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(pvalloc(2048), nullptr); };
ExpectRealtimeDeath(Func, "pvalloc");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, MmapDiesWhenRealtime) {
auto Func = []() {
void *_ = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("mmap"));
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_LINUX
TEST(TestRtsanInterceptors, MremapDiesWhenRealtime) {
void *addr = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
auto Func = [addr]() { void *_ = mremap(addr, 8, 16, 0); };
ExpectRealtimeDeath(Func, "mremap");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
void *ptr = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
EXPECT_NE(ptr, nullptr);
auto Func = [ptr]() { munmap(ptr, 8); };
printf("Right before death munmap\n");
ExpectRealtimeDeath(Func, "munmap");
ExpectNonRealtimeSurvival(Func);
}
class RtsanOpenedMmapTest : public RtsanFileTest {
protected:
void SetUp() override {
RtsanFileTest::SetUp();
file = fopen(GetTemporaryFilePath(), "w+");
ASSERT_THAT(file, Ne(nullptr));
fd = fileno(file);
ASSERT_THAT(fd, Ne(-1));
int ret = ftruncate(GetOpenFd(), size);
ASSERT_THAT(ret, Ne(-1));
addr =
mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, GetOpenFd(), 0);
ASSERT_THAT(addr, Ne(MAP_FAILED));
ASSERT_THAT(addr, Ne(nullptr));
}
void TearDown() override {
if (addr != nullptr && addr != MAP_FAILED)
munmap(addr, size);
RtsanFileTest::TearDown();
}
void *GetAddr() { return addr; }
static constexpr size_t GetSize() { return size; }
int GetOpenFd() { return fd; }
private:
void *addr = nullptr;
static constexpr size_t size = 4096;
FILE *file = nullptr;
int fd = -1;
};
#if !SANITIZER_APPLE
TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
ExpectRealtimeDeath(Func, "madvise");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedMmapTest, PosixMadviseDiesWhenRealtime) {
auto Func = [this]() {
posix_madvise(GetAddr(), GetSize(), POSIX_MADV_NORMAL);
};
ExpectRealtimeDeath(Func, "posix_madvise");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };
ExpectRealtimeDeath(Func, "mprotect");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
auto Func = [this]() { msync(GetAddr(), GetSize(), MS_INVALIDATE); };
ExpectRealtimeDeath(Func, "msync");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
#if SANITIZER_APPLE
std::vector<char> vec(GetSize() / 1024);
#else
std::vector<unsigned char> vec(GetSize() / 1024);
#endif
auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec.data()); };
ExpectRealtimeDeath(Func, "mincore");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
ExpectRealtimeDeath(Func, "shm_open");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, ShmUnlinkDiesWhenRealtime) {
auto Func = []() { shm_unlink("/rtsan_test_shm"); };
ExpectRealtimeDeath(Func, "shm_unlink");
ExpectNonRealtimeSurvival(Func);
}
#if !SANITIZER_APPLE
TEST(TestRtsanInterceptors, MemfdCreateDiesWhenRealtime) {
auto Func = []() { memfd_create("/rtsan_test_memfd_create", MFD_CLOEXEC); };
ExpectRealtimeDeath(Func, "memfd_create");
ExpectNonRealtimeSurvival(Func);
}
#endif
/*
Sleeping
*/
TEST(TestRtsanInterceptors, SleepDiesWhenRealtime) {
auto Func = []() { sleep(0u); };
ExpectRealtimeDeath(Func, "sleep");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, UsleepDiesWhenRealtime) {
auto Func = []() { usleep(1u); };
ExpectRealtimeDeath(Func, "usleep");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, NanosleepDiesWhenRealtime) {
auto Func = []() {
timespec T{};
nanosleep(&T, &T);
};
ExpectRealtimeDeath(Func, "nanosleep");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, SchedYieldDiesWhenRealtime) {
auto Func = []() { sched_yield(); };
ExpectRealtimeDeath(Func, "sched_yield");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_LINUX
TEST(TestRtsanInterceptors, SchedGetaffinityDiesWhenRealtime) {
cpu_set_t set{};
auto Func = [&set]() { sched_getaffinity(0, sizeof(set), &set); };
ExpectRealtimeDeath(Func, "sched_getaffinity");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, SchedSetaffinityDiesWhenRealtime) {
cpu_set_t set{};
auto Func = [&set]() { sched_setaffinity(0, sizeof(set), &set); };
ExpectRealtimeDeath(Func, "sched_setaffinity");
ExpectNonRealtimeSurvival(Func);
}
#endif
/*
Filesystem
*/
TEST_F(RtsanFileTest, OpenDiesWhenRealtime) {
auto Func = [this]() { open(GetTemporaryFilePath(), O_RDONLY); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("open"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, OpenatDiesWhenRealtime) {
auto Func = [this]() { openat(0, GetTemporaryFilePath(), O_RDONLY); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("openat"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, OpenCreatesFileWithProperMode) {
const mode_t existing_umask = umask(0);
umask(existing_umask);
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;
const int fd = open(GetTemporaryFilePath(), O_CREAT | O_WRONLY, mode);
ASSERT_THAT(fd, Ne(-1));
close(fd);
struct stat st;
ASSERT_THAT(stat(GetTemporaryFilePath(), &st), Eq(0));
// Mask st_mode to get permission bits only
const mode_t actual_mode = st.st_mode & 0777;
const mode_t expected_mode = mode & ~existing_umask;
ASSERT_THAT(actual_mode, Eq(expected_mode));
}
TEST_F(RtsanFileTest, CreatDiesWhenRealtime) {
auto Func = [this]() { creat(GetTemporaryFilePath(), S_IWOTH | S_IROTH); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("creat"));
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, FcntlDiesWhenRealtime) {
auto Func = []() { fcntl(0, F_GETFL); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
ASSERT_THAT(fd, Ne(-1));
auto Func = [fd]() {
struct flock lock{};
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = ::getpid();
ASSERT_THAT(fcntl(fd, F_GETLK, &lock), Eq(0));
ASSERT_THAT(lock.l_type, F_UNLCK);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
ExpectNonRealtimeSurvival(Func);
close(fd);
}
TEST_F(RtsanFileTest, FcntlSetFdDiesWhenRealtime) {
int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
ASSERT_THAT(fd, Ne(-1));
auto Func = [fd]() {
int old_flags = fcntl(fd, F_GETFD);
ASSERT_THAT(fcntl(fd, F_SETFD, FD_CLOEXEC), Eq(0));
int flags = fcntl(fd, F_GETFD);
ASSERT_THAT(flags, Ne(-1));
ASSERT_THAT(flags & FD_CLOEXEC, Eq(FD_CLOEXEC));
ASSERT_THAT(fcntl(fd, F_SETFD, old_flags), Eq(0));
ASSERT_THAT(fcntl(fd, F_GETFD), Eq(old_flags));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
ExpectNonRealtimeSurvival(Func);
close(fd);
}
TEST(TestRtsanInterceptors, ChdirDiesWhenRealtime) {
auto Func = []() { chdir("."); };
ExpectRealtimeDeath(Func, "chdir");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, FchdirDiesWhenRealtime) {
auto Func = []() { fchdir(0); };
ExpectRealtimeDeath(Func, "fchdir");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_READLINK
TEST(TestRtsanInterceptors, ReadlinkDiesWhenRealtime) {
char buf[1024];
auto Func = [&buf]() { readlink("/proc/self", buf, sizeof(buf)); };
ExpectRealtimeDeath(Func, "readlink");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_READLINKAT
TEST(TestRtsanInterceptors, ReadlinkatDiesWhenRealtime) {
char buf[1024];
auto Func = [&buf]() { readlinkat(0, "/proc/self", buf, sizeof(buf)); };
ExpectRealtimeDeath(Func, "readlinkat");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST_F(RtsanFileTest, FopenDiesWhenRealtime) {
auto Func = [this]() {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fopen"));
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_FOPENCOOKIE
TEST_F(RtsanFileTest, FopenCookieDieWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
struct fholder {
FILE *fp;
size_t read;
} fh = {f, 0};
auto CookieRead = [](void *cookie, char *buf, size_t size) {
fholder *p = reinterpret_cast<fholder *>(cookie);
p->read = fread(static_cast<void *>(buf), 1, size, p->fp);
EXPECT_NE(0u, p->read);
};
cookie_io_functions_t funcs = {(cookie_read_function_t *)&CookieRead, nullptr,
nullptr, nullptr};
auto Func = [&fh, &funcs]() {
FILE *f = fopencookie(&fh, "w", funcs);
EXPECT_THAT(f, Ne(nullptr));
};
ExpectRealtimeDeath(Func, "fopencookie");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
TEST_F(RtsanFileTest, OpenMemstreamDiesWhenRealtime) {
char *buffer;
size_t size;
auto Func = [&buffer, &size]() {
FILE *f = open_memstream(&buffer, &size);
EXPECT_THAT(f, Ne(nullptr));
};
ExpectRealtimeDeath(Func, "open_memstream");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, FmemOpenDiesWhenRealtime) {
char buffer[1024];
auto Func = [&buffer]() {
FILE *f = fmemopen(&buffer, sizeof(buffer), "w");
EXPECT_THAT(f, Ne(nullptr));
};
ExpectRealtimeDeath(Func, "fmemopen");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_SETVBUF
TEST_F(RtsanFileTest, SetbufDieWhenRealtime) {
char buffer[BUFSIZ];
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
auto Func = [f, &buffer]() { setbuf(f, buffer); };
ExpectRealtimeDeath(Func, "setbuf");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
char buffer[1024];
size_t size = sizeof(buffer);
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
auto Func = [f, &buffer, size]() {
int r = setvbuf(f, buffer, _IOFBF, size);
EXPECT_THAT(r, Eq(0));
};
ExpectRealtimeDeath(Func, "setvbuf");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, SetlinebufDieWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
auto Func = [f]() { setlinebuf(f); };
ExpectRealtimeDeath(Func, "setlinebuf");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
char buffer[1024];
size_t size = sizeof(buffer);
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
auto Func = [f, &buffer, size]() { setbuffer(f, buffer, size); };
ExpectRealtimeDeath(Func, "setbuffer");
ExpectNonRealtimeSurvival(Func);
}
#endif
class RtsanOpenedFileTest : public RtsanFileTest {
protected:
void SetUp() override {
RtsanFileTest::SetUp();
file = fopen(GetTemporaryFilePath(), "w");
ASSERT_THAT(file, Ne(nullptr));
fd = fileno(file);
ASSERT_THAT(fd, Ne(-1));
}
void TearDown() override {
const bool is_open = fcntl(fd, F_GETFD) != -1;
if (is_open && file != nullptr)
fclose(file);
RtsanFileTest::TearDown();
}
FILE *GetOpenFile() { return file; }
int GetOpenFd() { return fd; }
private:
FILE *file = nullptr;
int fd = -1;
};
TEST_F(RtsanOpenedFileTest, CloseDiesWhenRealtime) {
auto Func = [this]() { close(GetOpenFd()); };
ExpectRealtimeDeath(Func, "close");
}
TEST_F(RtsanOpenedFileTest, CloseSurvivesWhenNotRealtime) {
auto Func = [this]() { close(GetOpenFd()); };
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_FSEEK
TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
auto Func = [this]() {
fpos_t pos;
int ret = fgetpos(GetOpenFile(), &pos);
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fgetpos"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FsetposDieWhenRealtime) {
fpos_t pos;
int ret = fgetpos(GetOpenFile(), &pos);
ASSERT_THAT(ret, Eq(0));
auto Func = [this, pos]() {
int ret = fsetpos(GetOpenFile(), &pos);
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fsetpos"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FseekDieWhenRealtime) {
auto Func = [this]() {
int ret = fseek(GetOpenFile(), 0, SEEK_CUR);
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, "fseek");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FseekoDieWhenRealtime) {
auto Func = [this]() {
int ret = fseeko(GetOpenFile(), 0, SEEK_CUR);
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fseeko"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FtellDieWhenRealtime) {
auto Func = [this]() {
long ret = ftell(GetOpenFile());
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, "ftell");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FtelloDieWhenRealtime) {
auto Func = [this]() {
off_t ret = ftello(GetOpenFile());
ASSERT_THAT(ret, Eq(0));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftello"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, RewindDieWhenRealtime) {
int end = fseek(GetOpenFile(), 0, SEEK_END);
EXPECT_THAT(end, Eq(0));
auto Func = [this]() { rewind(GetOpenFile()); };
ExpectRealtimeDeath(Func, "rewind");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, IoctlDiesWhenRealtime) {
auto Func = []() { ioctl(0, FIONREAD); };
ExpectRealtimeDeath(Func, "ioctl");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, IoctlBehavesWithOutputArg) {
int arg{};
ioctl(GetOpenFd(), FIONREAD, &arg);
EXPECT_THAT(arg, Ge(0));
}
TEST_F(RtsanOpenedFileTest, FdopenDiesWhenRealtime) {
auto Func = [&]() {
FILE *f = fdopen(GetOpenFd(), "w");
EXPECT_THAT(f, Ne(nullptr));
};
ExpectRealtimeDeath(Func, "fdopen");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FreopenDiesWhenRealtime) {
auto Func = [&]() {
FILE *newfile = freopen(GetTemporaryFilePath(), "w", GetOpenFile());
EXPECT_THAT(newfile, Ne(nullptr));
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("freopen"));
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
// These initial checks just see if we CAN run these tests.
// If we can't (can't open a socket, or can't find an interface, just
// gracefully skip.
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
GTEST_SKIP();
}
struct ifaddrs *ifaddr = nullptr;
if (getifaddrs(&ifaddr) == -1 || ifaddr == nullptr) {
perror("getifaddrs");
close(sock);
GTEST_SKIP();
}
struct ifreq ifr{};
strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
int retval = ioctl(sock, SIOCGIFADDR, &ifr);
if (retval == -1) {
perror("ioctl");
close(sock);
freeifaddrs(ifaddr);
FAIL();
}
freeifaddrs(ifaddr);
close(sock);
ASSERT_THAT(ifr.ifr_addr.sa_data, NotNull());
ASSERT_THAT(ifr.ifr_addr.sa_family, Eq(AF_INET));
}
TEST_F(RtsanOpenedFileTest, LseekDiesWhenRealtime) {
auto Func = [this]() { lseek(GetOpenFd(), 0, SEEK_SET); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lseek"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, DupDiesWhenRealtime) {
auto Func = [this]() { dup(GetOpenFd()); };
ExpectRealtimeDeath(Func, "dup");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, Dup2DiesWhenRealtime) {
auto Func = [this]() { dup2(GetOpenFd(), 0); };
ExpectRealtimeDeath(Func, "dup2");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, ChmodDiesWhenRealtime) {
auto Func = [this]() { chmod(GetTemporaryFilePath(), 0777); };
ExpectRealtimeDeath(Func, "chmod");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FchmodDiesWhenRealtime) {
auto Func = [this]() { fchmod(GetOpenFd(), 0777); };
ExpectRealtimeDeath(Func, "fchmod");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, UmaskDiesWhenRealtime) {
auto Func = []() { umask(0); };
ExpectRealtimeDeath(Func, "umask");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_PROCESS_VM_READV
TEST(TestRtsanInterceptors, ProcessVmReadvDiesWhenRealtime) {
char stack[1024];
int p;
iovec lcl{&stack, sizeof(stack)};
iovec rmt{&p, sizeof(p)};
auto Func = [&lcl, &rmt]() { process_vm_readv(0, &lcl, 1, &rmt, 1, 0); };
ExpectRealtimeDeath(Func, "process_vm_readv");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, ProcessVmWritevDiesWhenRealtime) {
char stack[1024];
int p;
iovec lcl{&p, sizeof(p)};
iovec rmt{&stack, sizeof(stack)};
auto Func = [&lcl, &rmt]() { process_vm_writev(0, &lcl, 1, &rmt, 1, 0); };
ExpectRealtimeDeath(Func, "process_vm_writev");
ExpectNonRealtimeSurvival(Func);
}
#endif
class RtsanDirectoryTest : public ::testing::Test {
protected:
void SetUp() override {
const ::testing::TestInfo *const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
directory_path_ = std::string("/tmp/rtsan_temp_dir_") + test_info->name();
RemoveTemporaryDirectory();
}
const char *GetTemporaryDirectoryPath() const {
return directory_path_.c_str();
}
void TearDown() override { RemoveTemporaryDirectory(); }
private:
void RemoveTemporaryDirectory() const {
std::remove(GetTemporaryDirectoryPath());
}
std::string directory_path_;
};
TEST_F(RtsanDirectoryTest, MkdirDiesWhenRealtime) {
auto Func = [this]() { mkdir(GetTemporaryDirectoryPath(), 0777); };
ExpectRealtimeDeath(Func, "mkdir");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanDirectoryTest, RmdirDiesWhenRealtime) {
// We don't actually create this directory before we try to remove it
// Thats OK - we are just making sure the call gets intercepted
auto Func = [this]() { rmdir(GetTemporaryDirectoryPath()); };
ExpectRealtimeDeath(Func, "rmdir");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FreadDiesWhenRealtime) {
auto Func = [this]() {
char c{};
fread(&c, 1, 1, GetOpenFile());
};
ExpectRealtimeDeath(Func, "fread");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FwriteDiesWhenRealtime) {
const char *message = "Hello, world!";
auto Func = [&]() { fwrite(&message, 1, 4, GetOpenFile()); };
ExpectRealtimeDeath(Func, "fwrite");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, UnlinkDiesWhenRealtime) {
auto Func = [&]() { unlink(GetTemporaryFilePath()); };
ExpectRealtimeDeath(Func, "unlink");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, UnlinkatDiesWhenRealtime) {
auto Func = [&]() { unlinkat(0, GetTemporaryFilePath(), 0); };
ExpectRealtimeDeath(Func, "unlinkat");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, TruncateDiesWhenRealtime) {
auto Func = [&]() { truncate(GetTemporaryFilePath(), 16); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("truncate"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FtruncateDiesWhenRealtime) {
auto Func = [&]() { ftruncate(GetOpenFd(), 16); };
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftruncate"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, SymlinkDiesWhenRealtime) {
auto Func = [&]() {
symlink("/tmp/rtsan_symlink_test", GetTemporaryFilePath());
};
ExpectRealtimeDeath(Func, "symlink");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, SymlinkatDiesWhenRealtime) {
auto Func = [&]() {
symlinkat("/tmp/rtsan_symlinkat_test", AT_FDCWD, GetTemporaryFilePath());
};
ExpectRealtimeDeath(Func, "symlinkat");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
auto Func = [f]() { fclose(f); };
ExpectRealtimeDeath(Func, "fclose");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, PutsDiesWhenRealtime) {
auto Func = []() { puts("Hello, world!\n"); };
ExpectRealtimeDeath(Func);
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) {
auto Func = [this]() { fputs("Hello, world!\n", GetOpenFile()); };
ExpectRealtimeDeath(Func);
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanFileTest, FflushDiesWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
int written = fwrite("abc", 1, 3, f);
EXPECT_THAT(written, Eq(3));
auto Func = [&f]() {
int res = fflush(f);
EXPECT_THAT(res, Eq(0));
};
ExpectRealtimeDeath(Func, "fflush");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_APPLE
TEST_F(RtsanFileTest, FpurgeDiesWhenRealtime) {
FILE *f = fopen(GetTemporaryFilePath(), "w");
EXPECT_THAT(f, Ne(nullptr));
int written = fwrite("abc", 1, 3, f);
EXPECT_THAT(written, Eq(3));
auto Func = [&f]() {
int res = fpurge(f);
EXPECT_THAT(res, Eq(0));
};
ExpectRealtimeDeath(Func, "fpurge");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST_F(RtsanOpenedFileTest, ReadDiesWhenRealtime) {
auto Func = [this]() {
char c{};
read(GetOpenFd(), &c, 1);
};
ExpectRealtimeDeath(Func, "read");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, WriteDiesWhenRealtime) {
auto Func = [this]() {
char c = 'a';
write(GetOpenFd(), &c, 1);
};
ExpectRealtimeDeath(Func, "write");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, PreadDiesWhenRealtime) {
auto Func = [this]() {
char c{};
pread(GetOpenFd(), &c, 1, 0);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pread"));
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_PREADV
TEST_F(RtsanOpenedFileTest, PreadvDiesWhenRealtime) {
auto Func = [this]() {
char c{};
iovec iov{&c, sizeof(c)};
preadv(GetOpenFd(), &iov, 1, 0);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("preadv"));
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_PWRITEV
TEST_F(RtsanOpenedFileTest, PwritevDiesWhenRealtime) {
auto Func = [this]() {
char c{};
iovec iov{&c, sizeof(c)};
pwritev(GetOpenFd(), &iov, 1, 0);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pwritev"));
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST_F(RtsanOpenedFileTest, ReadvDiesWhenRealtime) {
auto Func = [this]() {
char c{};
iovec iov{&c, 1};
readv(GetOpenFd(), &iov, 1);
};
ExpectRealtimeDeath(Func, "readv");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, PwriteDiesWhenRealtime) {
auto Func = [this]() {
char c = 'a';
pwrite(GetOpenFd(), &c, 1, 0);
};
ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pwrite"));
ExpectNonRealtimeSurvival(Func);
}
TEST_F(RtsanOpenedFileTest, WritevDiesWhenRealtime) {
auto Func = [this]() {
char c = 'a';
iovec iov{&c, 1};
writev(GetOpenFd(), &iov, 1);
};
ExpectRealtimeDeath(Func, "writev");
ExpectNonRealtimeSurvival(Func);
}
/*
Concurrency
*/
TEST(TestRtsanInterceptors, PthreadCreateDiesWhenRealtime) {
auto Func = []() {
pthread_t thread{};
const pthread_attr_t attr{};
struct thread_info *thread_info{};
pthread_create(&thread, &attr, &FakeThreadEntryPoint, thread_info);
};
ExpectRealtimeDeath(Func, "pthread_create");
ExpectNonRealtimeSurvival(Func);
}
class PthreadMutexLockTest : public ::testing::Test {
protected:
void SetUp() override {
pthread_mutex_init(&mutex, nullptr);
is_locked = false;
}
void TearDown() override {
if (is_locked)
Unlock();
pthread_mutex_destroy(&mutex);
}
void Lock() {
ASSERT_TRUE(!is_locked);
pthread_mutex_lock(&mutex);
is_locked = true;
}
void Unlock() {
ASSERT_TRUE(is_locked);
pthread_mutex_unlock(&mutex);
is_locked = false;
}
private:
pthread_mutex_t mutex;
bool is_locked;
};
TEST_F(PthreadMutexLockTest, PthreadMutexLockDiesWhenRealtime) {
auto Func = [this]() { Lock(); };
ExpectRealtimeDeath(Func, "pthread_mutex_lock");
}
TEST_F(PthreadMutexLockTest, PthreadMutexLockSurvivesWhenNotRealtime) {
auto Func = [this]() { Lock(); };
ExpectNonRealtimeSurvival(Func);
}
TEST_F(PthreadMutexLockTest, PthreadMutexUnlockDiesWhenRealtime) {
Lock();
auto Func = [this]() { Unlock(); };
ExpectRealtimeDeath(Func, "pthread_mutex_unlock");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(PthreadMutexLockTest, PthreadMutexUnlockSurvivesWhenNotRealtime) {
Lock();
auto Func = [this]() { Unlock(); };
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, PthreadJoinDiesWhenRealtime) {
pthread_t thread{};
ASSERT_EQ(0,
pthread_create(&thread, nullptr, &FakeThreadEntryPoint, nullptr));
auto Func = [&thread]() { pthread_join(thread, nullptr); };
ExpectRealtimeDeath(Func, "pthread_join");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_APPLE
#pragma clang diagnostic push
// OSSpinLockLock is deprecated, but still in use in libc++
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#undef OSSpinLockLock
extern "C" {
typedef int32_t OSSpinLock;
void OSSpinLockLock(volatile OSSpinLock *__lock);
// _os_nospin_lock_lock may replace OSSpinLockLock due to deprecation macro.
typedef volatile OSSpinLock *_os_nospin_lock_t;
void _os_nospin_lock_lock(_os_nospin_lock_t lock);
}
TEST(TestRtsanInterceptors, OsSpinLockLockDiesWhenRealtime) {
auto Func = []() {
OSSpinLock spin_lock{};
OSSpinLockLock(&spin_lock);
};
ExpectRealtimeDeath(Func, "OSSpinLockLock");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, OsNoSpinLockLockDiesWhenRealtime) {
OSSpinLock lock{};
auto Func = [&]() { _os_nospin_lock_lock(&lock); };
ExpectRealtimeDeath(Func, "_os_nospin_lock_lock");
ExpectNonRealtimeSurvival(Func);
}
#pragma clang diagnostic pop //"-Wdeprecated-declarations"
TEST(TestRtsanInterceptors, OsUnfairLockLockDiesWhenRealtime) {
auto Func = []() {
os_unfair_lock_s unfair_lock{};
os_unfair_lock_lock(&unfair_lock);
};
ExpectRealtimeDeath(Func, "os_unfair_lock_lock");
ExpectNonRealtimeSurvival(Func);
}
#endif // SANITIZER_APPLE
#if SANITIZER_LINUX
TEST(TestRtsanInterceptors, SpinLockLockDiesWhenRealtime) {
pthread_spinlock_t spin_lock;
pthread_spin_init(&spin_lock, PTHREAD_PROCESS_SHARED);
auto Func = [&]() { pthread_spin_lock(&spin_lock); };
ExpectRealtimeDeath(Func, "pthread_spin_lock");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, PthreadCondSignalDiesWhenRealtime) {
pthread_cond_t cond{};
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
auto Func = [&cond]() { pthread_cond_signal(&cond); };
ExpectRealtimeDeath(Func, "pthread_cond_signal");
ExpectNonRealtimeSurvival(Func);
pthread_cond_destroy(&cond);
}
TEST(TestRtsanInterceptors, PthreadCondBroadcastDiesWhenRealtime) {
pthread_cond_t cond{};
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
auto Func = [&cond]() { pthread_cond_broadcast(&cond); };
ExpectRealtimeDeath(Func, "pthread_cond_broadcast");
ExpectNonRealtimeSurvival(Func);
pthread_cond_destroy(&cond);
}
TEST(TestRtsanInterceptors, PthreadCondWaitDiesWhenRealtime) {
pthread_cond_t cond;
pthread_mutex_t mutex;
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
auto Func = [&]() { pthread_cond_wait(&cond, &mutex); };
ExpectRealtimeDeath(Func, "pthread_cond_wait");
// It's very difficult to test the success case here without doing some
// sleeping, which is at the mercy of the scheduler. What's really important
// here is the interception - so we're only testing that for now.
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
class PthreadRwlockTest : public ::testing::Test {
protected:
void SetUp() override {
pthread_rwlock_init(&rw_lock, nullptr);
is_locked = false;
}
void TearDown() override {
if (is_locked)
Unlock();
pthread_rwlock_destroy(&rw_lock);
}
void RdLock() {
ASSERT_TRUE(!is_locked);
pthread_rwlock_rdlock(&rw_lock);
is_locked = true;
}
void WrLock() {
ASSERT_TRUE(!is_locked);
pthread_rwlock_wrlock(&rw_lock);
is_locked = true;
}
void Unlock() {
ASSERT_TRUE(is_locked);
pthread_rwlock_unlock(&rw_lock);
is_locked = false;
}
private:
pthread_rwlock_t rw_lock;
bool is_locked;
};
TEST_F(PthreadRwlockTest, PthreadRwlockRdlockDiesWhenRealtime) {
auto Func = [this]() { RdLock(); };
ExpectRealtimeDeath(Func, "pthread_rwlock_rdlock");
}
TEST_F(PthreadRwlockTest, PthreadRwlockRdlockSurvivesWhenNonRealtime) {
auto Func = [this]() { RdLock(); };
ExpectNonRealtimeSurvival(Func);
}
TEST_F(PthreadRwlockTest, PthreadRwlockUnlockDiesWhenRealtime) {
RdLock();
auto Func = [this]() { Unlock(); };
ExpectRealtimeDeath(Func, "pthread_rwlock_unlock");
}
TEST_F(PthreadRwlockTest, PthreadRwlockUnlockSurvivesWhenNonRealtime) {
RdLock();
auto Func = [this]() { Unlock(); };
ExpectNonRealtimeSurvival(Func);
}
TEST_F(PthreadRwlockTest, PthreadRwlockWrlockDiesWhenRealtime) {
auto Func = [this]() { WrLock(); };
ExpectRealtimeDeath(Func, "pthread_rwlock_wrlock");
}
TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
auto Func = [this]() { WrLock(); };
ExpectNonRealtimeSurvival(Func);
}
/*
Sockets
*/
TEST(TestRtsanInterceptors, GetAddrInfoDiesWhenRealtime) {
auto Func = []() {
addrinfo *info{};
getaddrinfo("localhost", "http", nullptr, &info);
};
ExpectRealtimeDeath(Func, "getaddrinfo");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, GetNameInfoDiesWhenRealtime) {
auto Func = []() {
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
getnameinfo(nullptr, 0, host, NI_MAXHOST, serv, NI_MAXSERV, 0);
};
ExpectRealtimeDeath(Func, "getnameinfo");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, BindingASocketDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(bind(kNotASocketFd, nullptr, 0), 0); };
ExpectRealtimeDeath(Func, "bind");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, ListeningOnASocketDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(listen(kNotASocketFd, 0), 0); };
ExpectRealtimeDeath(Func, "listen");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, AcceptingASocketDiesWhenRealtime) {
auto Func = []() { EXPECT_LT(accept(kNotASocketFd, nullptr, nullptr), 0); };
ExpectRealtimeDeath(Func, "accept");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_ACCEPT4
TEST(TestRtsanInterceptors, Accepting4ASocketDiesWhenRealtime) {
auto Func = []() {
EXPECT_LT(accept4(kNotASocketFd, nullptr, nullptr, 0), 0);
};
ExpectRealtimeDeath(Func, "accept4");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, ConnectingASocketDiesWhenRealtime) {
auto Func = []() { EXPECT_NE(connect(kNotASocketFd, nullptr, 0), 0); };
ExpectRealtimeDeath(Func, "connect");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, OpeningASocketDiesWhenRealtime) {
auto Func = []() { socket(PF_INET, SOCK_STREAM, 0); };
ExpectRealtimeDeath(Func, "socket");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, SendToASocketDiesWhenRealtime) {
auto Func = []() { send(0, nullptr, 0, 0); };
ExpectRealtimeDeath(Func, "send");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, SendmsgToASocketDiesWhenRealtime) {
msghdr msg{};
auto Func = [&]() { sendmsg(0, &msg, 0); };
ExpectRealtimeDeath(Func, "sendmsg");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_SENDMMSG
TEST(TestRtsanInterceptors, SendmmsgOnASocketDiesWhenRealtime) {
mmsghdr msg{};
auto Func = [&]() { sendmmsg(0, &msg, 0, 0); };
ExpectRealtimeDeath(Func, "sendmmsg");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, SendtoToASocketDiesWhenRealtime) {
sockaddr addr{};
socklen_t len{};
auto Func = [&]() { sendto(0, nullptr, 0, 0, &addr, len); };
ExpectRealtimeDeath(Func, "sendto");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, RecvFromASocketDiesWhenRealtime) {
auto Func = []() { recv(0, nullptr, 0, 0); };
ExpectRealtimeDeath(Func, "recv");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, RecvfromOnASocketDiesWhenRealtime) {
sockaddr addr{};
socklen_t len{};
auto Func = [&]() { recvfrom(0, nullptr, 0, 0, &addr, &len); };
ExpectRealtimeDeath(Func, "recvfrom");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, RecvmsgOnASocketDiesWhenRealtime) {
msghdr msg{};
auto Func = [&]() { recvmsg(0, &msg, 0); };
ExpectRealtimeDeath(Func, "recvmsg");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_RECVMMSG
TEST(TestRtsanInterceptors, RecvmmsgOnASocketDiesWhenRealtime) {
mmsghdr msg{};
auto Func = [&]() { recvmmsg(0, &msg, 0, 0, nullptr); };
ExpectRealtimeDeath(Func, "recvmmsg");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, ShutdownOnASocketDiesWhenRealtime) {
auto Func = [&]() { shutdown(0, 0); };
ExpectRealtimeDeath(Func, "shutdown");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_GETSOCKNAME
TEST(TestRtsanInterceptors, GetsocknameOnASocketDiesWhenRealtime) {
sockaddr addr{};
socklen_t len{};
auto Func = [&]() { getsockname(0, &addr, &len); };
ExpectRealtimeDeath(Func, "getsockname");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_GETPEERNAME
TEST(TestRtsanInterceptors, GetpeernameOnASocketDiesWhenRealtime) {
sockaddr addr{};
socklen_t len{};
auto Func = [&]() { getpeername(0, &addr, &len); };
ExpectRealtimeDeath(Func, "getpeername");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_GETSOCKOPT
TEST(TestRtsanInterceptors, GetsockoptOnASocketDiesWhenRealtime) {
int val = 0;
socklen_t len = static_cast<socklen_t>(sizeof(val));
auto Func = [&val, &len]() {
getsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, &len);
};
ExpectRealtimeDeath(Func, "getsockopt");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, SetsockoptOnASocketDiesWhenRealtime) {
int val = 0;
socklen_t len = static_cast<socklen_t>(sizeof(val));
auto Func = [&val, &len]() {
setsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, len);
};
ExpectRealtimeDeath(Func, "setsockopt");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, SocketpairDiesWhenRealtime) {
int pair[2]{};
auto Func = [&pair]() { socketpair(0, 0, 0, pair); };
ExpectRealtimeDeath(Func, "socketpair");
ExpectNonRealtimeSurvival(Func);
}
/*
I/O Multiplexing
*/
TEST(TestRtsanInterceptors, PollDiesWhenRealtime) {
struct pollfd fds[1];
fds[0].fd = 0;
fds[0].events = POLLIN;
auto Func = [&fds]() { poll(fds, 1, 0); };
ExpectRealtimeDeath(Func, "poll");
ExpectNonRealtimeSurvival(Func);
}
#if !SANITIZER_APPLE
// FIXME: This should work on Darwin as well
// see the comment near the interceptor
TEST(TestRtsanInterceptors, SelectDiesWhenRealtime) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
struct timeval timeout = {0, 0};
auto Func = [&readfds, &timeout]() {
select(1, &readfds, nullptr, nullptr, &timeout);
};
ExpectRealtimeDeath(Func, "select");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, PSelectDiesWhenRealtime) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
struct timespec timeout = {0, 0};
auto Func = [&]() {
pselect(1, &readfds, nullptr, nullptr, &timeout, nullptr);
};
ExpectRealtimeDeath(Func, "pselect");
ExpectNonRealtimeSurvival(Func);
}
#if SANITIZER_INTERCEPT_EPOLL
TEST(TestRtsanInterceptors, EpollCreateDiesWhenRealtime) {
auto Func = []() { epoll_create(1); };
ExpectRealtimeDeath(Func, "epoll_create");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, EpollCreate1DiesWhenRealtime) {
auto Func = []() { epoll_create1(EPOLL_CLOEXEC); };
ExpectRealtimeDeath(Func, "epoll_create1");
ExpectNonRealtimeSurvival(Func);
}
class EpollTest : public ::testing::Test {
protected:
void SetUp() override {
epfd = epoll_create1(EPOLL_CLOEXEC);
ASSERT_GE(epfd, 0);
}
void TearDown() override {
if (epfd >= 0)
close(epfd);
}
int GetEpollFd() { return epfd; }
private:
int epfd = -1;
};
TEST_F(EpollTest, EpollCtlDiesWhenRealtime) {
auto Func = [this]() {
struct epoll_event event = {.events = EPOLLIN, .data = {.fd = 0}};
epoll_ctl(GetEpollFd(), EPOLL_CTL_ADD, 0, &event);
};
ExpectRealtimeDeath(Func, "epoll_ctl");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(EpollTest, EpollWaitDiesWhenRealtime) {
auto Func = [this]() {
struct epoll_event events[1];
epoll_wait(GetEpollFd(), events, 1, 0);
};
ExpectRealtimeDeath(Func, "epoll_wait");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(EpollTest, EpollPWaitDiesWhenRealtime) {
auto Func = [this]() {
struct epoll_event events[1];
epoll_pwait(GetEpollFd(), events, 1, 0, nullptr);
};
ExpectRealtimeDeath(Func, "epoll_pwait");
ExpectNonRealtimeSurvival(Func);
}
#endif // SANITIZER_INTERCEPT_EPOLL
#if SANITIZER_INTERCEPT_PPOLL
TEST(TestRtsanInterceptors, PpollDiesWhenRealtime) {
struct pollfd fds[1];
fds[0].fd = 0;
fds[0].events = POLLIN;
timespec ts = {0, 0};
auto Func = [&fds, &ts]() { ppoll(fds, 1, &ts, nullptr); };
ExpectRealtimeDeath(Func, "ppoll");
ExpectNonRealtimeSurvival(Func);
}
#endif
#if SANITIZER_INTERCEPT_KQUEUE
TEST(TestRtsanInterceptors, KqueueDiesWhenRealtime) {
auto Func = []() { kqueue(); };
ExpectRealtimeDeath(Func, "kqueue");
ExpectNonRealtimeSurvival(Func);
}
class KqueueTest : public ::testing::Test {
protected:
void SetUp() override {
kq = kqueue();
ASSERT_GE(kq, 0);
}
void TearDown() override {
if (kq >= 0)
close(kq);
}
int GetKqueueFd() { return kq; }
private:
int kq = -1;
};
TEST_F(KqueueTest, KeventDiesWhenRealtime) {
struct kevent event;
EV_SET(&event, 0, EVFILT_READ, EV_ADD, 0, 0, nullptr);
struct timespec timeout = {0, 0};
auto Func = [this, event, timeout]() {
kevent(GetKqueueFd(), &event, 1, nullptr, 0, &timeout);
};
ExpectRealtimeDeath(Func, "kevent");
ExpectNonRealtimeSurvival(Func);
}
TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
struct kevent64_s event;
EV_SET64(&event, 0, EVFILT_READ, EV_ADD, 0, 0, 0, 0, 0);
struct timespec timeout = {0, 0};
auto Func = [this, event, timeout]() {
kevent64(GetKqueueFd(), &event, 1, nullptr, 0, 0, &timeout);
};
ExpectRealtimeDeath(Func, "kevent64");
ExpectNonRealtimeSurvival(Func);
}
#endif // SANITIZER_INTERCEPT_KQUEUE
#if SANITIZER_LINUX
TEST(TestRtsanInterceptors, InotifyInitDiesWhenRealtime) {
auto Func = []() { inotify_init(); };
ExpectRealtimeDeath(Func, "inotify_init");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, InotifyInit1DiesWhenRealtime) {
auto Func = []() { inotify_init1(0); };
ExpectRealtimeDeath(Func, "inotify_init1");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, InotifyAddWatchDiesWhenRealtime) {
int fd = inotify_init();
EXPECT_THAT(fd, Ne(-1));
auto Func = [fd]() {
inotify_add_watch(fd, "/tmp/rtsan_inotify", IN_CREATE);
};
ExpectRealtimeDeath(Func, "inotify_add_watch");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
int fd = inotify_init();
EXPECT_THAT(fd, Ne(-1));
auto Func = [fd]() { inotify_rm_watch(fd, -1); };
ExpectRealtimeDeath(Func, "inotify_rm_watch");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, TimerfdCreateDiesWhenRealtime) {
auto Func = []() { timerfd_create(CLOCK_MONOTONIC, 0); };
ExpectRealtimeDeath(Func, "timerfd_create");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, TimerfdSettimeDiesWhenRealtime) {
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
EXPECT_THAT(fd, Ne(-1));
auto ts = itimerspec{{0, 0}, {0, 0}};
auto Func = [fd, ts]() { timerfd_settime(fd, 0, &ts, NULL); };
ExpectRealtimeDeath(Func, "timerfd_settime");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, TimerfdGettimeDiesWhenRealtime) {
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
EXPECT_THAT(fd, Ne(-1));
itimerspec ts{};
auto Func = [fd, &ts]() { timerfd_gettime(fd, &ts); };
ExpectRealtimeDeath(Func, "timerfd_gettime");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, EventfdDiesWhenRealtime) {
auto Func = []() { eventfd(EFD_CLOEXEC, 0); };
ExpectRealtimeDeath(Func, "eventfd");
ExpectNonRealtimeSurvival(Func);
}
#endif
TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
ExpectRealtimeDeath(Func, "mkfifo");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, PipeDiesWhenRealtime) {
int fds[2];
auto Func = [&fds]() { pipe(fds); };
ExpectRealtimeDeath(Func, "pipe");
ExpectNonRealtimeSurvival(Func);
}
#if !SANITIZER_APPLE
TEST(TestRtsanInterceptors, Pipe2DiesWhenRealtime) {
int fds[2];
auto Func = [&fds]() { pipe2(fds, O_CLOEXEC); };
ExpectRealtimeDeath(Func, "pipe2");
ExpectNonRealtimeSurvival(Func);
}
#endif
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
TEST(TestRtsanInterceptors, SyscallDiesWhenRealtime) {
auto Func = []() { syscall(SYS_getpid); };
ExpectRealtimeDeath(Func, "syscall");
ExpectNonRealtimeSurvival(Func);
}
TEST(TestRtsanInterceptors, GetPidReturnsSame) {
int pid = syscall(SYS_getpid);
EXPECT_THAT(pid, Ne(-1));
EXPECT_THAT(getpid(), Eq(pid));
}
#pragma clang diagnostic pop
#endif // SANITIZER_POSIX
|