xref: /llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (revision 23763a1200edfe209d1e334d1d1ff71b2a992b3a)
1 //===--- rtsan_test_interceptors.cpp - Realtime Sanitizer -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "sanitizer_common/sanitizer_platform.h"
12 #if SANITIZER_POSIX
13 
14 #include "gtest/gtest.h"
15 
16 #include "sanitizer_common/sanitizer_platform_interceptors.h"
17 
18 #include "rtsan_test_utilities.h"
19 
20 #if SANITIZER_APPLE
21 #include <libkern/OSAtomic.h>
22 #include <os/lock.h>
23 #include <unistd.h>
24 #endif
25 
26 #if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC
27 #include <malloc.h>
28 #endif
29 
30 #if SANITIZER_INTERCEPT_EPOLL
31 #include <sys/epoll.h>
32 #endif
33 
34 #if SANITIZER_INTERCEPT_KQUEUE
35 #include <sys/event.h>
36 #include <sys/time.h>
37 #endif
38 
39 #include <fcntl.h>
40 #include <ifaddrs.h>
41 #include <net/if.h>
42 #include <netdb.h>
43 #include <poll.h>
44 #include <pthread.h>
45 #include <stdio.h>
46 #if SANITIZER_LINUX
47 #include <sys/inotify.h>
48 #endif
49 #include <sys/ioctl.h>
50 #include <sys/mman.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <sys/syscall.h>
54 #include <sys/types.h>
55 #include <sys/uio.h>
56 
57 #if _FILE_OFFSET_BITS == 64 && SANITIZER_GLIBC
58 // Under these conditions, some system calls are `foo64` instead of `foo`
59 #define MAYBE_APPEND_64(func) func "64"
60 #else
61 #define MAYBE_APPEND_64(func) func
62 #endif
63 
64 using namespace testing;
65 using namespace rtsan_testing;
66 using namespace std::chrono_literals;
67 
68 // NOTE: In the socket tests we pass in bad info to the calls to ensure they
69 //       fail which is why we EXPECT_NE 0 for their return codes.
70 //       We just care that the call is intercepted
71 const int kNotASocketFd = 0;
72 
73 void *FakeThreadEntryPoint(void *) { return nullptr; }
74 
75 class RtsanFileTest : public ::testing::Test {
76 protected:
77   void SetUp() override {
78     const ::testing::TestInfo *const test_info =
79         ::testing::UnitTest::GetInstance()->current_test_info();
80     file_path_ = std::string("/tmp/rtsan_temporary_test_file_") +
81                  test_info->name() + ".txt";
82     RemoveTemporaryFile();
83   }
84 
85   // Gets a file path with the test's name in it
86   // This file will be removed if it exists at the end of the test
87   const char *GetTemporaryFilePath() const { return file_path_.c_str(); }
88 
89   void TearDown() override { RemoveTemporaryFile(); }
90 
91 private:
92   void RemoveTemporaryFile() const { std::remove(GetTemporaryFilePath()); }
93   std::string file_path_;
94 };
95 
96 /*
97     Allocation and deallocation
98 */
99 
100 TEST(TestRtsanInterceptors, MallocDiesWhenRealtime) {
101   auto Func = []() { EXPECT_NE(nullptr, malloc(1)); };
102   ExpectRealtimeDeath(Func, "malloc");
103   ExpectNonRealtimeSurvival(Func);
104 }
105 
106 TEST(TestRtsanInterceptors, CallocDiesWhenRealtime) {
107   auto Func = []() { EXPECT_NE(nullptr, calloc(2, 4)); };
108   ExpectRealtimeDeath(Func, "calloc");
109   ExpectNonRealtimeSurvival(Func);
110 }
111 
112 TEST(TestRtsanInterceptors, ReallocDiesWhenRealtime) {
113   void *ptr_1 = malloc(1);
114   auto Func = [ptr_1]() { EXPECT_NE(nullptr, realloc(ptr_1, 8)); };
115   ExpectRealtimeDeath(Func, "realloc");
116   ExpectNonRealtimeSurvival(Func);
117 }
118 
119 #if SANITIZER_APPLE
120 TEST(TestRtsanInterceptors, ReallocfDiesWhenRealtime) {
121   void *ptr_1 = malloc(1);
122   auto Func = [ptr_1]() { EXPECT_NE(nullptr, reallocf(ptr_1, 8)); };
123   ExpectRealtimeDeath(Func, "reallocf");
124   ExpectNonRealtimeSurvival(Func);
125 }
126 #endif
127 
128 TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
129   auto Func = []() { EXPECT_NE(nullptr, valloc(4)); };
130   ExpectRealtimeDeath(Func, "valloc");
131   ExpectNonRealtimeSurvival(Func);
132 }
133 
134 #if __has_builtin(__builtin_available) && SANITIZER_APPLE
135 #define ALIGNED_ALLOC_AVAILABLE() (__builtin_available(macOS 10.15, *))
136 #else
137 // We are going to assume this is true until we hit systems where it isn't
138 #define ALIGNED_ALLOC_AVAILABLE() (true)
139 #endif
140 
141 TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
142   if (ALIGNED_ALLOC_AVAILABLE()) {
143     auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
144     ExpectRealtimeDeath(Func, "aligned_alloc");
145     ExpectNonRealtimeSurvival(Func);
146   }
147 }
148 
149 // free_sized and free_aligned_sized (both C23) are not yet supported
150 TEST(TestRtsanInterceptors, FreeDiesWhenRealtime) {
151   void *ptr_1 = malloc(1);
152   void *ptr_2 = malloc(1);
153   ExpectRealtimeDeath([ptr_1]() { free(ptr_1); }, "free");
154   ExpectNonRealtimeSurvival([ptr_2]() { free(ptr_2); });
155 
156   // Prevent malloc/free pair being optimised out
157   ASSERT_NE(nullptr, ptr_1);
158   ASSERT_NE(nullptr, ptr_2);
159 }
160 
161 TEST(TestRtsanInterceptors, FreeSurvivesWhenRealtimeIfArgumentIsNull) {
162   RealtimeInvoke([]() { free(NULL); });
163   ExpectNonRealtimeSurvival([]() { free(NULL); });
164 }
165 
166 TEST(TestRtsanInterceptors, PosixMemalignDiesWhenRealtime) {
167   auto Func = []() {
168     void *ptr;
169     posix_memalign(&ptr, 4, 4);
170   };
171   ExpectRealtimeDeath(Func, "posix_memalign");
172   ExpectNonRealtimeSurvival(Func);
173 }
174 
175 #if SANITIZER_INTERCEPT_MEMALIGN
176 TEST(TestRtsanInterceptors, MemalignDiesWhenRealtime) {
177   auto Func = []() { EXPECT_NE(memalign(2, 2048), nullptr); };
178   ExpectRealtimeDeath(Func, "memalign");
179   ExpectNonRealtimeSurvival(Func);
180 }
181 #endif
182 
183 #if SANITIZER_INTERCEPT_PVALLOC
184 TEST(TestRtsanInterceptors, PvallocDiesWhenRealtime) {
185   auto Func = []() { EXPECT_NE(pvalloc(2048), nullptr); };
186   ExpectRealtimeDeath(Func, "pvalloc");
187   ExpectNonRealtimeSurvival(Func);
188 }
189 #endif
190 
191 TEST(TestRtsanInterceptors, MmapDiesWhenRealtime) {
192   auto Func = []() {
193     void *_ = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
194                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
195   };
196   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("mmap"));
197   ExpectNonRealtimeSurvival(Func);
198 }
199 
200 #if SANITIZER_LINUX
201 TEST(TestRtsanInterceptors, MremapDiesWhenRealtime) {
202   void *addr = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
203                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
204   auto Func = [addr]() { void *_ = mremap(addr, 8, 16, 0); };
205   ExpectRealtimeDeath(Func, "mremap");
206   ExpectNonRealtimeSurvival(Func);
207 }
208 #endif
209 
210 TEST(TestRtsanInterceptors, MunmapDiesWhenRealtime) {
211   void *ptr = mmap(nullptr, 8, PROT_READ | PROT_WRITE,
212                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
213   EXPECT_NE(ptr, nullptr);
214   auto Func = [ptr]() { munmap(ptr, 8); };
215   printf("Right before death munmap\n");
216   ExpectRealtimeDeath(Func, "munmap");
217   ExpectNonRealtimeSurvival(Func);
218 }
219 
220 class RtsanOpenedMmapTest : public RtsanFileTest {
221 protected:
222   void SetUp() override {
223     RtsanFileTest::SetUp();
224     file = fopen(GetTemporaryFilePath(), "w+");
225     ASSERT_THAT(file, Ne(nullptr));
226     fd = fileno(file);
227     ASSERT_THAT(fd, Ne(-1));
228     int ret = ftruncate(GetOpenFd(), size);
229     ASSERT_THAT(ret, Ne(-1));
230     addr =
231         mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, GetOpenFd(), 0);
232     ASSERT_THAT(addr, Ne(MAP_FAILED));
233     ASSERT_THAT(addr, Ne(nullptr));
234   }
235 
236   void TearDown() override {
237     if (addr != nullptr && addr != MAP_FAILED)
238       munmap(addr, size);
239     RtsanFileTest::TearDown();
240   }
241 
242   void *GetAddr() { return addr; }
243   static constexpr size_t GetSize() { return size; }
244 
245   int GetOpenFd() { return fd; }
246 
247 private:
248   void *addr = nullptr;
249   static constexpr size_t size = 4096;
250   FILE *file = nullptr;
251   int fd = -1;
252 };
253 
254 #if !SANITIZER_APPLE
255 TEST_F(RtsanOpenedMmapTest, MadviseDiesWhenRealtime) {
256   auto Func = [this]() { madvise(GetAddr(), GetSize(), MADV_NORMAL); };
257   ExpectRealtimeDeath(Func, "madvise");
258   ExpectNonRealtimeSurvival(Func);
259 }
260 
261 TEST_F(RtsanOpenedMmapTest, PosixMadviseDiesWhenRealtime) {
262   auto Func = [this]() {
263     posix_madvise(GetAddr(), GetSize(), POSIX_MADV_NORMAL);
264   };
265   ExpectRealtimeDeath(Func, "posix_madvise");
266   ExpectNonRealtimeSurvival(Func);
267 }
268 #endif
269 
270 TEST_F(RtsanOpenedMmapTest, MprotectDiesWhenRealtime) {
271   auto Func = [this]() { mprotect(GetAddr(), GetSize(), PROT_READ); };
272   ExpectRealtimeDeath(Func, "mprotect");
273   ExpectNonRealtimeSurvival(Func);
274 }
275 
276 TEST_F(RtsanOpenedMmapTest, MsyncDiesWhenRealtime) {
277   auto Func = [this]() { msync(GetAddr(), GetSize(), MS_INVALIDATE); };
278   ExpectRealtimeDeath(Func, "msync");
279   ExpectNonRealtimeSurvival(Func);
280 }
281 
282 TEST_F(RtsanOpenedMmapTest, MincoreDiesWhenRealtime) {
283 #if SANITIZER_APPLE
284   std::vector<char> vec(GetSize() / 1024);
285 #else
286   std::vector<unsigned char> vec(GetSize() / 1024);
287 #endif
288   auto Func = [this, &vec]() { mincore(GetAddr(), GetSize(), vec.data()); };
289   ExpectRealtimeDeath(Func, "mincore");
290   ExpectNonRealtimeSurvival(Func);
291 }
292 
293 TEST(TestRtsanInterceptors, ShmOpenDiesWhenRealtime) {
294   auto Func = []() { shm_open("/rtsan_test_shm", O_CREAT | O_RDWR, 0); };
295   ExpectRealtimeDeath(Func, "shm_open");
296   ExpectNonRealtimeSurvival(Func);
297 }
298 
299 TEST(TestRtsanInterceptors, ShmUnlinkDiesWhenRealtime) {
300   auto Func = []() { shm_unlink("/rtsan_test_shm"); };
301   ExpectRealtimeDeath(Func, "shm_unlink");
302   ExpectNonRealtimeSurvival(Func);
303 }
304 
305 /*
306     Sleeping
307 */
308 
309 TEST(TestRtsanInterceptors, SleepDiesWhenRealtime) {
310   auto Func = []() { sleep(0u); };
311   ExpectRealtimeDeath(Func, "sleep");
312   ExpectNonRealtimeSurvival(Func);
313 }
314 
315 TEST(TestRtsanInterceptors, UsleepDiesWhenRealtime) {
316   auto Func = []() { usleep(1u); };
317   ExpectRealtimeDeath(Func, "usleep");
318   ExpectNonRealtimeSurvival(Func);
319 }
320 
321 TEST(TestRtsanInterceptors, NanosleepDiesWhenRealtime) {
322   auto Func = []() {
323     timespec T{};
324     nanosleep(&T, &T);
325   };
326   ExpectRealtimeDeath(Func, "nanosleep");
327   ExpectNonRealtimeSurvival(Func);
328 }
329 
330 TEST(TestRtsanInterceptors, SchedYieldDiesWhenRealtime) {
331   auto Func = []() { sched_yield(); };
332   ExpectRealtimeDeath(Func, "sched_yield");
333   ExpectNonRealtimeSurvival(Func);
334 }
335 
336 #if SANITIZER_LINUX
337 TEST(TestRtsanInterceptors, SchedGetaffinityDiesWhenRealtime) {
338   cpu_set_t set{};
339   auto Func = [&set]() { sched_getaffinity(0, sizeof(set), &set); };
340   ExpectRealtimeDeath(Func, "sched_getaffinity");
341   ExpectNonRealtimeSurvival(Func);
342 }
343 
344 TEST(TestRtsanInterceptors, SchedSetaffinityDiesWhenRealtime) {
345   cpu_set_t set{};
346   auto Func = [&set]() { sched_setaffinity(0, sizeof(set), &set); };
347   ExpectRealtimeDeath(Func, "sched_setaffinity");
348   ExpectNonRealtimeSurvival(Func);
349 }
350 #endif
351 
352 /*
353     Filesystem
354 */
355 
356 TEST_F(RtsanFileTest, OpenDiesWhenRealtime) {
357   auto Func = [this]() { open(GetTemporaryFilePath(), O_RDONLY); };
358   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("open"));
359   ExpectNonRealtimeSurvival(Func);
360 }
361 
362 TEST_F(RtsanFileTest, OpenatDiesWhenRealtime) {
363   auto Func = [this]() { openat(0, GetTemporaryFilePath(), O_RDONLY); };
364   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("openat"));
365   ExpectNonRealtimeSurvival(Func);
366 }
367 
368 TEST_F(RtsanFileTest, OpenCreatesFileWithProperMode) {
369   const mode_t existing_umask = umask(0);
370   umask(existing_umask);
371 
372   const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;
373 
374   const int fd = open(GetTemporaryFilePath(), O_CREAT | O_WRONLY, mode);
375   ASSERT_THAT(fd, Ne(-1));
376   close(fd);
377 
378   struct stat st;
379   ASSERT_THAT(stat(GetTemporaryFilePath(), &st), Eq(0));
380 
381   // Mask st_mode to get permission bits only
382   const mode_t actual_mode = st.st_mode & 0777;
383   const mode_t expected_mode = mode & ~existing_umask;
384   ASSERT_THAT(actual_mode, Eq(expected_mode));
385 }
386 
387 TEST_F(RtsanFileTest, CreatDiesWhenRealtime) {
388   auto Func = [this]() { creat(GetTemporaryFilePath(), S_IWOTH | S_IROTH); };
389   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("creat"));
390   ExpectNonRealtimeSurvival(Func);
391 }
392 
393 TEST(TestRtsanInterceptors, FcntlDiesWhenRealtime) {
394   auto Func = []() { fcntl(0, F_GETFL); };
395   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
396   ExpectNonRealtimeSurvival(Func);
397 }
398 
399 TEST_F(RtsanFileTest, FcntlFlockDiesWhenRealtime) {
400   int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
401   ASSERT_THAT(fd, Ne(-1));
402 
403   auto Func = [fd]() {
404     struct flock lock {};
405     lock.l_type = F_RDLCK;
406     lock.l_whence = SEEK_SET;
407     lock.l_start = 0;
408     lock.l_len = 0;
409     lock.l_pid = ::getpid();
410 
411     ASSERT_THAT(fcntl(fd, F_GETLK, &lock), Eq(0));
412     ASSERT_THAT(lock.l_type, F_UNLCK);
413   };
414   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
415   ExpectNonRealtimeSurvival(Func);
416 
417   close(fd);
418 }
419 
420 TEST_F(RtsanFileTest, FcntlSetFdDiesWhenRealtime) {
421   int fd = creat(GetTemporaryFilePath(), S_IRUSR | S_IWUSR);
422   ASSERT_THAT(fd, Ne(-1));
423 
424   auto Func = [fd]() {
425     int old_flags = fcntl(fd, F_GETFD);
426     ASSERT_THAT(fcntl(fd, F_SETFD, FD_CLOEXEC), Eq(0));
427 
428     int flags = fcntl(fd, F_GETFD);
429     ASSERT_THAT(flags, Ne(-1));
430     ASSERT_THAT(flags & FD_CLOEXEC, Eq(FD_CLOEXEC));
431 
432     ASSERT_THAT(fcntl(fd, F_SETFD, old_flags), Eq(0));
433     ASSERT_THAT(fcntl(fd, F_GETFD), Eq(old_flags));
434   };
435 
436   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fcntl"));
437   ExpectNonRealtimeSurvival(Func);
438 
439   close(fd);
440 }
441 
442 TEST(TestRtsanInterceptors, CloseDiesWhenRealtime) {
443   auto Func = []() { close(0); };
444   ExpectRealtimeDeath(Func, "close");
445   ExpectNonRealtimeSurvival(Func);
446 }
447 
448 TEST_F(RtsanFileTest, FopenDiesWhenRealtime) {
449   auto Func = [this]() {
450     FILE *f = fopen(GetTemporaryFilePath(), "w");
451     EXPECT_THAT(f, Ne(nullptr));
452   };
453 
454   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fopen"));
455   ExpectNonRealtimeSurvival(Func);
456 }
457 
458 #if SANITIZER_INTERCEPT_FOPENCOOKIE
459 TEST_F(RtsanFileTest, FopenCookieDieWhenRealtime) {
460   FILE *f = fopen(GetTemporaryFilePath(), "w");
461   EXPECT_THAT(f, Ne(nullptr));
462   struct fholder {
463     FILE *fp;
464     size_t read;
465   } fh = {f, 0};
466   auto CookieRead = [](void *cookie, char *buf, size_t size) {
467     fholder *p = reinterpret_cast<fholder *>(cookie);
468     p->read = fread(static_cast<void *>(buf), 1, size, p->fp);
469     EXPECT_NE(0u, p->read);
470   };
471   cookie_io_functions_t funcs = {(cookie_read_function_t *)&CookieRead, nullptr,
472                                  nullptr, nullptr};
473   auto Func = [&fh, &funcs]() {
474     FILE *f = fopencookie(&fh, "w", funcs);
475     EXPECT_THAT(f, Ne(nullptr));
476   };
477 
478   ExpectRealtimeDeath(Func, "fopencookie");
479   ExpectNonRealtimeSurvival(Func);
480 }
481 #endif
482 
483 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
484 TEST_F(RtsanFileTest, OpenMemstreamDiesWhenRealtime) {
485   char *buffer;
486   size_t size;
487   auto Func = [&buffer, &size]() {
488     FILE *f = open_memstream(&buffer, &size);
489     EXPECT_THAT(f, Ne(nullptr));
490   };
491 
492   ExpectRealtimeDeath(Func, "open_memstream");
493   ExpectNonRealtimeSurvival(Func);
494 }
495 
496 TEST_F(RtsanFileTest, FmemOpenDiesWhenRealtime) {
497   char buffer[1024];
498   auto Func = [&buffer]() {
499     FILE *f = fmemopen(&buffer, sizeof(buffer), "w");
500     EXPECT_THAT(f, Ne(nullptr));
501   };
502 
503   ExpectRealtimeDeath(Func, "fmemopen");
504   ExpectNonRealtimeSurvival(Func);
505 }
506 #endif
507 
508 #if SANITIZER_INTERCEPT_SETVBUF
509 TEST_F(RtsanFileTest, SetbufDieWhenRealtime) {
510   char buffer[BUFSIZ];
511   FILE *f = fopen(GetTemporaryFilePath(), "w");
512   EXPECT_THAT(f, Ne(nullptr));
513 
514   auto Func = [f, &buffer]() { setbuf(f, buffer); };
515 
516   ExpectRealtimeDeath(Func, "setbuf");
517   ExpectNonRealtimeSurvival(Func);
518 }
519 
520 TEST_F(RtsanFileTest, SetvbufDieWhenRealtime) {
521   char buffer[1024];
522   size_t size = sizeof(buffer);
523   FILE *f = fopen(GetTemporaryFilePath(), "w");
524   EXPECT_THAT(f, Ne(nullptr));
525 
526   auto Func = [f, &buffer, size]() {
527     int r = setvbuf(f, buffer, _IOFBF, size);
528     EXPECT_THAT(r, Eq(0));
529   };
530 
531   ExpectRealtimeDeath(Func, "setvbuf");
532   ExpectNonRealtimeSurvival(Func);
533 }
534 
535 TEST_F(RtsanFileTest, SetlinebufDieWhenRealtime) {
536   FILE *f = fopen(GetTemporaryFilePath(), "w");
537   EXPECT_THAT(f, Ne(nullptr));
538 
539   auto Func = [f]() { setlinebuf(f); };
540 
541   ExpectRealtimeDeath(Func, "setlinebuf");
542   ExpectNonRealtimeSurvival(Func);
543 }
544 
545 TEST_F(RtsanFileTest, SetbufferDieWhenRealtime) {
546   char buffer[1024];
547   size_t size = sizeof(buffer);
548   FILE *f = fopen(GetTemporaryFilePath(), "w");
549   EXPECT_THAT(f, Ne(nullptr));
550 
551   auto Func = [f, &buffer, size]() { setbuffer(f, buffer, size); };
552 
553   ExpectRealtimeDeath(Func, "setbuffer");
554   ExpectNonRealtimeSurvival(Func);
555 }
556 #endif
557 
558 class RtsanOpenedFileTest : public RtsanFileTest {
559 protected:
560   void SetUp() override {
561     RtsanFileTest::SetUp();
562     file = fopen(GetTemporaryFilePath(), "w");
563     ASSERT_THAT(file, Ne(nullptr));
564     fd = fileno(file);
565     ASSERT_THAT(fd, Ne(-1));
566   }
567 
568   void TearDown() override {
569     if (file != nullptr)
570       fclose(file);
571     RtsanFileTest::TearDown();
572   }
573 
574   FILE *GetOpenFile() { return file; }
575 
576   int GetOpenFd() { return fd; }
577 
578 private:
579   FILE *file = nullptr;
580   int fd = -1;
581 };
582 
583 #if SANITIZER_INTERCEPT_FSEEK
584 TEST_F(RtsanOpenedFileTest, FgetposDieWhenRealtime) {
585   auto Func = [this]() {
586     fpos_t pos;
587     int ret = fgetpos(GetOpenFile(), &pos);
588     ASSERT_THAT(ret, Eq(0));
589   };
590 
591   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fgetpos"));
592   ExpectNonRealtimeSurvival(Func);
593 }
594 
595 TEST_F(RtsanOpenedFileTest, FsetposDieWhenRealtime) {
596   fpos_t pos;
597   int ret = fgetpos(GetOpenFile(), &pos);
598   ASSERT_THAT(ret, Eq(0));
599   auto Func = [this, pos]() {
600     int ret = fsetpos(GetOpenFile(), &pos);
601     ASSERT_THAT(ret, Eq(0));
602   };
603 
604   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fsetpos"));
605   ExpectNonRealtimeSurvival(Func);
606 }
607 
608 TEST_F(RtsanOpenedFileTest, FseekDieWhenRealtime) {
609   auto Func = [this]() {
610     int ret = fseek(GetOpenFile(), 0, SEEK_CUR);
611     ASSERT_THAT(ret, Eq(0));
612   };
613 
614   ExpectRealtimeDeath(Func, "fseek");
615   ExpectNonRealtimeSurvival(Func);
616 }
617 
618 TEST_F(RtsanOpenedFileTest, FseekoDieWhenRealtime) {
619   auto Func = [this]() {
620     int ret = fseeko(GetOpenFile(), 0, SEEK_CUR);
621     ASSERT_THAT(ret, Eq(0));
622   };
623 
624   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("fseeko"));
625   ExpectNonRealtimeSurvival(Func);
626 }
627 
628 TEST_F(RtsanOpenedFileTest, FtellDieWhenRealtime) {
629   auto Func = [this]() {
630     long ret = ftell(GetOpenFile());
631     ASSERT_THAT(ret, Eq(0));
632   };
633 
634   ExpectRealtimeDeath(Func, "ftell");
635   ExpectNonRealtimeSurvival(Func);
636 }
637 
638 TEST_F(RtsanOpenedFileTest, FtelloDieWhenRealtime) {
639   auto Func = [this]() {
640     off_t ret = ftello(GetOpenFile());
641     ASSERT_THAT(ret, Eq(0));
642   };
643 
644   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("ftello"));
645   ExpectNonRealtimeSurvival(Func);
646 }
647 
648 TEST_F(RtsanOpenedFileTest, RewindDieWhenRealtime) {
649   int end = fseek(GetOpenFile(), 0, SEEK_END);
650   EXPECT_THAT(end, Eq(0));
651   auto Func = [this]() { rewind(GetOpenFile()); };
652 
653   ExpectRealtimeDeath(Func, "rewind");
654   ExpectNonRealtimeSurvival(Func);
655 }
656 #endif
657 
658 TEST(TestRtsanInterceptors, IoctlDiesWhenRealtime) {
659   auto Func = []() { ioctl(0, FIONREAD); };
660   ExpectRealtimeDeath(Func, "ioctl");
661   ExpectNonRealtimeSurvival(Func);
662 }
663 
664 TEST_F(RtsanOpenedFileTest, IoctlBehavesWithOutputArg) {
665   int arg{};
666   ioctl(GetOpenFd(), FIONREAD, &arg);
667 
668   EXPECT_THAT(arg, Ge(0));
669 }
670 
671 TEST_F(RtsanOpenedFileTest, FdopenDiesWhenRealtime) {
672   auto Func = [&]() {
673     FILE *f = fdopen(GetOpenFd(), "w");
674     EXPECT_THAT(f, Ne(nullptr));
675   };
676 
677   ExpectRealtimeDeath(Func, "fdopen");
678   ExpectNonRealtimeSurvival(Func);
679 }
680 
681 TEST_F(RtsanOpenedFileTest, FreopenDiesWhenRealtime) {
682   auto Func = [&]() {
683     FILE *newfile = freopen(GetTemporaryFilePath(), "w", GetOpenFile());
684     EXPECT_THAT(newfile, Ne(nullptr));
685   };
686 
687   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("freopen"));
688   ExpectNonRealtimeSurvival(Func);
689 }
690 
691 TEST(TestRtsanInterceptors, IoctlBehavesWithOutputPointer) {
692   // These initial checks just see if we CAN run these tests.
693   // If we can't (can't open a socket, or can't find an interface, just
694   // gracefully skip.
695   int sock = socket(AF_INET, SOCK_STREAM, 0);
696   if (sock == -1) {
697     perror("socket");
698     GTEST_SKIP();
699   }
700 
701   struct ifaddrs *ifaddr = nullptr;
702   if (getifaddrs(&ifaddr) == -1 || ifaddr == nullptr) {
703     perror("getifaddrs");
704     close(sock);
705     GTEST_SKIP();
706   }
707 
708   struct ifreq ifr {};
709   strncpy(ifr.ifr_name, ifaddr->ifa_name, IFNAMSIZ - 1);
710 
711   int retval = ioctl(sock, SIOCGIFADDR, &ifr);
712   if (retval == -1) {
713     perror("ioctl");
714     close(sock);
715     freeifaddrs(ifaddr);
716     FAIL();
717   }
718 
719   freeifaddrs(ifaddr);
720   close(sock);
721 
722   ASSERT_THAT(ifr.ifr_addr.sa_data, NotNull());
723   ASSERT_THAT(ifr.ifr_addr.sa_family, Eq(AF_INET));
724 }
725 
726 TEST_F(RtsanOpenedFileTest, LseekDiesWhenRealtime) {
727   auto Func = [this]() { lseek(GetOpenFd(), 0, SEEK_SET); };
728   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("lseek"));
729   ExpectNonRealtimeSurvival(Func);
730 }
731 
732 TEST_F(RtsanOpenedFileTest, DupDiesWhenRealtime) {
733   auto Func = [this]() { dup(GetOpenFd()); };
734   ExpectRealtimeDeath(Func, "dup");
735   ExpectNonRealtimeSurvival(Func);
736 }
737 
738 TEST_F(RtsanOpenedFileTest, Dup2DiesWhenRealtime) {
739   auto Func = [this]() { dup2(GetOpenFd(), 0); };
740   ExpectRealtimeDeath(Func, "dup2");
741   ExpectNonRealtimeSurvival(Func);
742 }
743 
744 TEST_F(RtsanFileTest, ChmodDiesWhenRealtime) {
745   auto Func = [this]() { chmod(GetTemporaryFilePath(), 0777); };
746   ExpectRealtimeDeath(Func, "chmod");
747   ExpectNonRealtimeSurvival(Func);
748 }
749 
750 TEST_F(RtsanOpenedFileTest, FchmodDiesWhenRealtime) {
751   auto Func = [this]() { fchmod(GetOpenFd(), 0777); };
752   ExpectRealtimeDeath(Func, "fchmod");
753   ExpectNonRealtimeSurvival(Func);
754 }
755 
756 TEST(TestRtsanInterceptors, UmaskDiesWhenRealtime) {
757   auto Func = []() { umask(0); };
758   ExpectRealtimeDeath(Func, "umask");
759   ExpectNonRealtimeSurvival(Func);
760 }
761 
762 #if SANITIZER_INTERCEPT_PROCESS_VM_READV
763 TEST(TestRtsanInterceptors, ProcessVmReadvDiesWhenRealtime) {
764   char stack[1024];
765   int p;
766   iovec lcl{&stack, sizeof(stack)};
767   iovec rmt{&p, sizeof(p)};
768   auto Func = [&lcl, &rmt]() { process_vm_readv(0, &lcl, 1, &rmt, 1, 0); };
769   ExpectRealtimeDeath(Func, "process_vm_readv");
770   ExpectNonRealtimeSurvival(Func);
771 }
772 
773 TEST(TestRtsanInterceptors, ProcessVmWritevDiesWhenRealtime) {
774   char stack[1024];
775   int p;
776   iovec lcl{&p, sizeof(p)};
777   iovec rmt{&stack, sizeof(stack)};
778   auto Func = [&lcl, &rmt]() { process_vm_writev(0, &lcl, 1, &rmt, 1, 0); };
779   ExpectRealtimeDeath(Func, "process_vm_writev");
780   ExpectNonRealtimeSurvival(Func);
781 }
782 #endif
783 
784 class RtsanDirectoryTest : public ::testing::Test {
785 protected:
786   void SetUp() override {
787     const ::testing::TestInfo *const test_info =
788         ::testing::UnitTest::GetInstance()->current_test_info();
789     directory_path_ = std::string("/tmp/rtsan_temp_dir_") + test_info->name();
790     RemoveTemporaryDirectory();
791   }
792 
793   const char *GetTemporaryDirectoryPath() const {
794     return directory_path_.c_str();
795   }
796 
797   void TearDown() override { RemoveTemporaryDirectory(); }
798 
799 private:
800   void RemoveTemporaryDirectory() const {
801     std::remove(GetTemporaryDirectoryPath());
802   }
803   std::string directory_path_;
804 };
805 
806 TEST_F(RtsanDirectoryTest, MkdirDiesWhenRealtime) {
807   auto Func = [this]() { mkdir(GetTemporaryDirectoryPath(), 0777); };
808   ExpectRealtimeDeath(Func, "mkdir");
809   ExpectNonRealtimeSurvival(Func);
810 }
811 
812 TEST_F(RtsanDirectoryTest, RmdirDiesWhenRealtime) {
813   // We don't actually create this directory before we try to remove it
814   // Thats OK - we are just making sure the call gets intercepted
815   auto Func = [this]() { rmdir(GetTemporaryDirectoryPath()); };
816   ExpectRealtimeDeath(Func, "rmdir");
817   ExpectNonRealtimeSurvival(Func);
818 }
819 
820 TEST_F(RtsanOpenedFileTest, FreadDiesWhenRealtime) {
821   auto Func = [this]() {
822     char c{};
823     fread(&c, 1, 1, GetOpenFile());
824   };
825   ExpectRealtimeDeath(Func, "fread");
826   ExpectNonRealtimeSurvival(Func);
827 }
828 
829 TEST_F(RtsanOpenedFileTest, FwriteDiesWhenRealtime) {
830   const char *message = "Hello, world!";
831   auto Func = [&]() { fwrite(&message, 1, 4, GetOpenFile()); };
832   ExpectRealtimeDeath(Func, "fwrite");
833   ExpectNonRealtimeSurvival(Func);
834 }
835 
836 TEST_F(RtsanFileTest, FcloseDiesWhenRealtime) {
837   FILE *f = fopen(GetTemporaryFilePath(), "w");
838   EXPECT_THAT(f, Ne(nullptr));
839   auto Func = [f]() { fclose(f); };
840   ExpectRealtimeDeath(Func, "fclose");
841   ExpectNonRealtimeSurvival(Func);
842 }
843 
844 TEST(TestRtsanInterceptors, PutsDiesWhenRealtime) {
845   auto Func = []() { puts("Hello, world!\n"); };
846   ExpectRealtimeDeath(Func);
847   ExpectNonRealtimeSurvival(Func);
848 }
849 
850 TEST_F(RtsanOpenedFileTest, FputsDiesWhenRealtime) {
851   auto Func = [this]() { fputs("Hello, world!\n", GetOpenFile()); };
852   ExpectRealtimeDeath(Func);
853   ExpectNonRealtimeSurvival(Func);
854 }
855 
856 TEST_F(RtsanFileTest, FflushDiesWhenRealtime) {
857   FILE *f = fopen(GetTemporaryFilePath(), "w");
858   EXPECT_THAT(f, Ne(nullptr));
859   int written = fwrite("abc", 1, 3, f);
860   EXPECT_THAT(written, Eq(3));
861   auto Func = [&f]() {
862     int res = fflush(f);
863     EXPECT_THAT(res, Eq(0));
864   };
865   ExpectRealtimeDeath(Func, "fflush");
866   ExpectNonRealtimeSurvival(Func);
867 }
868 
869 #if SANITIZER_APPLE
870 TEST_F(RtsanFileTest, FpurgeDiesWhenRealtime) {
871   FILE *f = fopen(GetTemporaryFilePath(), "w");
872   EXPECT_THAT(f, Ne(nullptr));
873   int written = fwrite("abc", 1, 3, f);
874   EXPECT_THAT(written, Eq(3));
875   auto Func = [&f]() {
876     int res = fpurge(f);
877     EXPECT_THAT(res, Eq(0));
878   };
879   ExpectRealtimeDeath(Func, "fpurge");
880   ExpectNonRealtimeSurvival(Func);
881 }
882 #endif
883 
884 TEST_F(RtsanOpenedFileTest, ReadDiesWhenRealtime) {
885   auto Func = [this]() {
886     char c{};
887     read(GetOpenFd(), &c, 1);
888   };
889   ExpectRealtimeDeath(Func, "read");
890   ExpectNonRealtimeSurvival(Func);
891 }
892 
893 TEST_F(RtsanOpenedFileTest, WriteDiesWhenRealtime) {
894   auto Func = [this]() {
895     char c = 'a';
896     write(GetOpenFd(), &c, 1);
897   };
898   ExpectRealtimeDeath(Func, "write");
899   ExpectNonRealtimeSurvival(Func);
900 }
901 
902 TEST_F(RtsanOpenedFileTest, PreadDiesWhenRealtime) {
903   auto Func = [this]() {
904     char c{};
905     pread(GetOpenFd(), &c, 1, 0);
906   };
907   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pread"));
908   ExpectNonRealtimeSurvival(Func);
909 }
910 
911 #if SANITIZER_INTERCEPT_PREADV
912 TEST_F(RtsanOpenedFileTest, PreadvDiesWhenRealtime) {
913   auto Func = [this]() {
914     char c{};
915     iovec iov{&c, sizeof(c)};
916     preadv(GetOpenFd(), &iov, 1, 0);
917   };
918   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("preadv"));
919   ExpectNonRealtimeSurvival(Func);
920 }
921 #endif
922 
923 #if SANITIZER_INTERCEPT_PWRITEV
924 TEST_F(RtsanOpenedFileTest, PwritevDiesWhenRealtime) {
925   auto Func = [this]() {
926     char c{};
927     iovec iov{&c, sizeof(c)};
928     pwritev(GetOpenFd(), &iov, 1, 0);
929   };
930   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pwritev"));
931   ExpectNonRealtimeSurvival(Func);
932 }
933 #endif
934 
935 TEST_F(RtsanOpenedFileTest, ReadvDiesWhenRealtime) {
936   auto Func = [this]() {
937     char c{};
938     iovec iov{&c, 1};
939     readv(GetOpenFd(), &iov, 1);
940   };
941   ExpectRealtimeDeath(Func, "readv");
942   ExpectNonRealtimeSurvival(Func);
943 }
944 
945 TEST_F(RtsanOpenedFileTest, PwriteDiesWhenRealtime) {
946   auto Func = [this]() {
947     char c = 'a';
948     pwrite(GetOpenFd(), &c, 1, 0);
949   };
950   ExpectRealtimeDeath(Func, MAYBE_APPEND_64("pwrite"));
951   ExpectNonRealtimeSurvival(Func);
952 }
953 
954 TEST_F(RtsanOpenedFileTest, WritevDiesWhenRealtime) {
955   auto Func = [this]() {
956     char c = 'a';
957     iovec iov{&c, 1};
958     writev(GetOpenFd(), &iov, 1);
959   };
960   ExpectRealtimeDeath(Func, "writev");
961   ExpectNonRealtimeSurvival(Func);
962 }
963 
964 /*
965     Concurrency
966 */
967 
968 TEST(TestRtsanInterceptors, PthreadCreateDiesWhenRealtime) {
969   auto Func = []() {
970     pthread_t thread{};
971     const pthread_attr_t attr{};
972     struct thread_info *thread_info{};
973     pthread_create(&thread, &attr, &FakeThreadEntryPoint, thread_info);
974   };
975   ExpectRealtimeDeath(Func, "pthread_create");
976   ExpectNonRealtimeSurvival(Func);
977 }
978 
979 class PthreadMutexLockTest : public ::testing::Test {
980 protected:
981   void SetUp() override {
982     pthread_mutex_init(&mutex, nullptr);
983     is_locked = false;
984   }
985 
986   void TearDown() override {
987     if (is_locked)
988       Unlock();
989 
990     pthread_mutex_destroy(&mutex);
991   }
992 
993   void Lock() {
994     ASSERT_TRUE(!is_locked);
995     pthread_mutex_lock(&mutex);
996     is_locked = true;
997   }
998 
999   void Unlock() {
1000     ASSERT_TRUE(is_locked);
1001     pthread_mutex_unlock(&mutex);
1002     is_locked = false;
1003   }
1004 
1005 private:
1006   pthread_mutex_t mutex;
1007   bool is_locked;
1008 };
1009 
1010 TEST_F(PthreadMutexLockTest, PthreadMutexLockDiesWhenRealtime) {
1011   auto Func = [this]() { Lock(); };
1012 
1013   ExpectRealtimeDeath(Func, "pthread_mutex_lock");
1014 }
1015 
1016 TEST_F(PthreadMutexLockTest, PthreadMutexLockSurvivesWhenNotRealtime) {
1017   auto Func = [this]() { Lock(); };
1018 
1019   ExpectNonRealtimeSurvival(Func);
1020 }
1021 
1022 TEST_F(PthreadMutexLockTest, PthreadMutexUnlockDiesWhenRealtime) {
1023   Lock();
1024   auto Func = [this]() { Unlock(); };
1025 
1026   ExpectRealtimeDeath(Func, "pthread_mutex_unlock");
1027   ExpectNonRealtimeSurvival(Func);
1028 }
1029 
1030 TEST_F(PthreadMutexLockTest, PthreadMutexUnlockSurvivesWhenNotRealtime) {
1031   Lock();
1032   auto Func = [this]() { Unlock(); };
1033 
1034   ExpectNonRealtimeSurvival(Func);
1035 }
1036 
1037 TEST(TestRtsanInterceptors, PthreadJoinDiesWhenRealtime) {
1038   pthread_t thread{};
1039   ASSERT_EQ(0,
1040             pthread_create(&thread, nullptr, &FakeThreadEntryPoint, nullptr));
1041 
1042   auto Func = [&thread]() { pthread_join(thread, nullptr); };
1043 
1044   ExpectRealtimeDeath(Func, "pthread_join");
1045   ExpectNonRealtimeSurvival(Func);
1046 }
1047 
1048 #if SANITIZER_APPLE
1049 
1050 #pragma clang diagnostic push
1051 // OSSpinLockLock is deprecated, but still in use in libc++
1052 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1053 TEST(TestRtsanInterceptors, OsSpinLockLockDiesWhenRealtime) {
1054   auto Func = []() {
1055     OSSpinLock spin_lock{};
1056     OSSpinLockLock(&spin_lock);
1057   };
1058   ExpectRealtimeDeath(Func, "OSSpinLockLock");
1059   ExpectNonRealtimeSurvival(Func);
1060 }
1061 #pragma clang diagnostic pop
1062 
1063 TEST(TestRtsanInterceptors, OsUnfairLockLockDiesWhenRealtime) {
1064   auto Func = []() {
1065     os_unfair_lock_s unfair_lock{};
1066     os_unfair_lock_lock(&unfair_lock);
1067   };
1068   ExpectRealtimeDeath(Func, "os_unfair_lock_lock");
1069   ExpectNonRealtimeSurvival(Func);
1070 }
1071 #endif
1072 
1073 #if SANITIZER_LINUX
1074 TEST(TestRtsanInterceptors, SpinLockLockDiesWhenRealtime) {
1075   pthread_spinlock_t spin_lock;
1076   pthread_spin_init(&spin_lock, PTHREAD_PROCESS_SHARED);
1077   auto Func = [&]() { pthread_spin_lock(&spin_lock); };
1078   ExpectRealtimeDeath(Func, "pthread_spin_lock");
1079   ExpectNonRealtimeSurvival(Func);
1080 }
1081 #endif
1082 
1083 TEST(TestRtsanInterceptors, PthreadCondSignalDiesWhenRealtime) {
1084   pthread_cond_t cond{};
1085   ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1086 
1087   auto Func = [&cond]() { pthread_cond_signal(&cond); };
1088   ExpectRealtimeDeath(Func, "pthread_cond_signal");
1089   ExpectNonRealtimeSurvival(Func);
1090 
1091   pthread_cond_destroy(&cond);
1092 }
1093 
1094 TEST(TestRtsanInterceptors, PthreadCondBroadcastDiesWhenRealtime) {
1095   pthread_cond_t cond{};
1096   ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1097 
1098   auto Func = [&cond]() { pthread_cond_broadcast(&cond); };
1099   ExpectRealtimeDeath(Func, "pthread_cond_broadcast");
1100   ExpectNonRealtimeSurvival(Func);
1101 
1102   pthread_cond_destroy(&cond);
1103 }
1104 
1105 TEST(TestRtsanInterceptors, PthreadCondWaitDiesWhenRealtime) {
1106   pthread_cond_t cond;
1107   pthread_mutex_t mutex;
1108   ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1109   ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1110 
1111   auto Func = [&]() { pthread_cond_wait(&cond, &mutex); };
1112   ExpectRealtimeDeath(Func, "pthread_cond_wait");
1113   // It's very difficult to test the success case here without doing some
1114   // sleeping, which is at the mercy of the scheduler. What's really important
1115   // here is the interception - so we're only testing that for now.
1116 
1117   pthread_cond_destroy(&cond);
1118   pthread_mutex_destroy(&mutex);
1119 }
1120 
1121 class PthreadRwlockTest : public ::testing::Test {
1122 protected:
1123   void SetUp() override {
1124     pthread_rwlock_init(&rw_lock, nullptr);
1125     is_locked = false;
1126   }
1127 
1128   void TearDown() override {
1129     if (is_locked)
1130       Unlock();
1131 
1132     pthread_rwlock_destroy(&rw_lock);
1133   }
1134 
1135   void RdLock() {
1136     ASSERT_TRUE(!is_locked);
1137     pthread_rwlock_rdlock(&rw_lock);
1138     is_locked = true;
1139   }
1140 
1141   void WrLock() {
1142     ASSERT_TRUE(!is_locked);
1143     pthread_rwlock_wrlock(&rw_lock);
1144     is_locked = true;
1145   }
1146 
1147   void Unlock() {
1148     ASSERT_TRUE(is_locked);
1149     pthread_rwlock_unlock(&rw_lock);
1150     is_locked = false;
1151   }
1152 
1153 private:
1154   pthread_rwlock_t rw_lock;
1155   bool is_locked;
1156 };
1157 
1158 TEST_F(PthreadRwlockTest, PthreadRwlockRdlockDiesWhenRealtime) {
1159   auto Func = [this]() { RdLock(); };
1160   ExpectRealtimeDeath(Func, "pthread_rwlock_rdlock");
1161 }
1162 
1163 TEST_F(PthreadRwlockTest, PthreadRwlockRdlockSurvivesWhenNonRealtime) {
1164   auto Func = [this]() { RdLock(); };
1165   ExpectNonRealtimeSurvival(Func);
1166 }
1167 
1168 TEST_F(PthreadRwlockTest, PthreadRwlockUnlockDiesWhenRealtime) {
1169   RdLock();
1170 
1171   auto Func = [this]() { Unlock(); };
1172   ExpectRealtimeDeath(Func, "pthread_rwlock_unlock");
1173 }
1174 
1175 TEST_F(PthreadRwlockTest, PthreadRwlockUnlockSurvivesWhenNonRealtime) {
1176   RdLock();
1177 
1178   auto Func = [this]() { Unlock(); };
1179   ExpectNonRealtimeSurvival(Func);
1180 }
1181 
1182 TEST_F(PthreadRwlockTest, PthreadRwlockWrlockDiesWhenRealtime) {
1183   auto Func = [this]() { WrLock(); };
1184 
1185   ExpectRealtimeDeath(Func, "pthread_rwlock_wrlock");
1186 }
1187 
1188 TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
1189   auto Func = [this]() { WrLock(); };
1190 
1191   ExpectNonRealtimeSurvival(Func);
1192 }
1193 
1194 /*
1195     Sockets
1196 */
1197 TEST(TestRtsanInterceptors, GetAddrInfoDiesWhenRealtime) {
1198   auto Func = []() {
1199     addrinfo *info{};
1200     getaddrinfo("localhost", "http", nullptr, &info);
1201   };
1202   ExpectRealtimeDeath(Func, "getaddrinfo");
1203   ExpectNonRealtimeSurvival(Func);
1204 }
1205 
1206 TEST(TestRtsanInterceptors, GetNameInfoDiesWhenRealtime) {
1207   auto Func = []() {
1208     char host[NI_MAXHOST];
1209     char serv[NI_MAXSERV];
1210     getnameinfo(nullptr, 0, host, NI_MAXHOST, serv, NI_MAXSERV, 0);
1211   };
1212   ExpectRealtimeDeath(Func, "getnameinfo");
1213   ExpectNonRealtimeSurvival(Func);
1214 }
1215 
1216 TEST(TestRtsanInterceptors, BindingASocketDiesWhenRealtime) {
1217   auto Func = []() { EXPECT_NE(bind(kNotASocketFd, nullptr, 0), 0); };
1218   ExpectRealtimeDeath(Func, "bind");
1219   ExpectNonRealtimeSurvival(Func);
1220 }
1221 
1222 TEST(TestRtsanInterceptors, ListeningOnASocketDiesWhenRealtime) {
1223   auto Func = []() { EXPECT_NE(listen(kNotASocketFd, 0), 0); };
1224   ExpectRealtimeDeath(Func, "listen");
1225   ExpectNonRealtimeSurvival(Func);
1226 }
1227 
1228 TEST(TestRtsanInterceptors, AcceptingASocketDiesWhenRealtime) {
1229   auto Func = []() { EXPECT_LT(accept(kNotASocketFd, nullptr, nullptr), 0); };
1230   ExpectRealtimeDeath(Func, "accept");
1231   ExpectNonRealtimeSurvival(Func);
1232 }
1233 
1234 #if SANITIZER_INTERCEPT_ACCEPT4
1235 TEST(TestRtsanInterceptors, Accepting4ASocketDiesWhenRealtime) {
1236   auto Func = []() {
1237     EXPECT_LT(accept4(kNotASocketFd, nullptr, nullptr, 0), 0);
1238   };
1239   ExpectRealtimeDeath(Func, "accept4");
1240   ExpectNonRealtimeSurvival(Func);
1241 }
1242 #endif
1243 
1244 TEST(TestRtsanInterceptors, ConnectingASocketDiesWhenRealtime) {
1245   auto Func = []() { EXPECT_NE(connect(kNotASocketFd, nullptr, 0), 0); };
1246   ExpectRealtimeDeath(Func, "connect");
1247   ExpectNonRealtimeSurvival(Func);
1248 }
1249 
1250 TEST(TestRtsanInterceptors, OpeningASocketDiesWhenRealtime) {
1251   auto Func = []() { socket(PF_INET, SOCK_STREAM, 0); };
1252   ExpectRealtimeDeath(Func, "socket");
1253   ExpectNonRealtimeSurvival(Func);
1254 }
1255 
1256 TEST(TestRtsanInterceptors, SendToASocketDiesWhenRealtime) {
1257   auto Func = []() { send(0, nullptr, 0, 0); };
1258   ExpectRealtimeDeath(Func, "send");
1259   ExpectNonRealtimeSurvival(Func);
1260 }
1261 
1262 TEST(TestRtsanInterceptors, SendmsgToASocketDiesWhenRealtime) {
1263   msghdr msg{};
1264   auto Func = [&]() { sendmsg(0, &msg, 0); };
1265   ExpectRealtimeDeath(Func, "sendmsg");
1266   ExpectNonRealtimeSurvival(Func);
1267 }
1268 
1269 #if SANITIZER_INTERCEPT_SENDMMSG
1270 TEST(TestRtsanInterceptors, SendmmsgOnASocketDiesWhenRealtime) {
1271   mmsghdr msg{};
1272   auto Func = [&]() { sendmmsg(0, &msg, 0, 0); };
1273   ExpectRealtimeDeath(Func, "sendmmsg");
1274   ExpectNonRealtimeSurvival(Func);
1275 }
1276 #endif
1277 
1278 TEST(TestRtsanInterceptors, SendtoToASocketDiesWhenRealtime) {
1279   sockaddr addr{};
1280   socklen_t len{};
1281   auto Func = [&]() { sendto(0, nullptr, 0, 0, &addr, len); };
1282   ExpectRealtimeDeath(Func, "sendto");
1283   ExpectNonRealtimeSurvival(Func);
1284 }
1285 
1286 TEST(TestRtsanInterceptors, RecvFromASocketDiesWhenRealtime) {
1287   auto Func = []() { recv(0, nullptr, 0, 0); };
1288   ExpectRealtimeDeath(Func, "recv");
1289   ExpectNonRealtimeSurvival(Func);
1290 }
1291 
1292 TEST(TestRtsanInterceptors, RecvfromOnASocketDiesWhenRealtime) {
1293   sockaddr addr{};
1294   socklen_t len{};
1295   auto Func = [&]() { recvfrom(0, nullptr, 0, 0, &addr, &len); };
1296   ExpectRealtimeDeath(Func, "recvfrom");
1297   ExpectNonRealtimeSurvival(Func);
1298 }
1299 
1300 TEST(TestRtsanInterceptors, RecvmsgOnASocketDiesWhenRealtime) {
1301   msghdr msg{};
1302   auto Func = [&]() { recvmsg(0, &msg, 0); };
1303   ExpectRealtimeDeath(Func, "recvmsg");
1304   ExpectNonRealtimeSurvival(Func);
1305 }
1306 
1307 #if SANITIZER_INTERCEPT_RECVMMSG
1308 TEST(TestRtsanInterceptors, RecvmmsgOnASocketDiesWhenRealtime) {
1309   mmsghdr msg{};
1310   auto Func = [&]() { recvmmsg(0, &msg, 0, 0, nullptr); };
1311   ExpectRealtimeDeath(Func, "recvmmsg");
1312   ExpectNonRealtimeSurvival(Func);
1313 }
1314 #endif
1315 
1316 TEST(TestRtsanInterceptors, ShutdownOnASocketDiesWhenRealtime) {
1317   auto Func = [&]() { shutdown(0, 0); };
1318   ExpectRealtimeDeath(Func, "shutdown");
1319   ExpectNonRealtimeSurvival(Func);
1320 }
1321 
1322 #if SANITIZER_INTERCEPT_GETSOCKNAME
1323 TEST(TestRtsanInterceptors, GetsocknameOnASocketDiesWhenRealtime) {
1324   sockaddr addr{};
1325   socklen_t len{};
1326   auto Func = [&]() { getsockname(0, &addr, &len); };
1327   ExpectRealtimeDeath(Func, "getsockname");
1328   ExpectNonRealtimeSurvival(Func);
1329 }
1330 #endif
1331 
1332 #if SANITIZER_INTERCEPT_GETPEERNAME
1333 TEST(TestRtsanInterceptors, GetpeernameOnASocketDiesWhenRealtime) {
1334   sockaddr addr{};
1335   socklen_t len{};
1336   auto Func = [&]() { getpeername(0, &addr, &len); };
1337   ExpectRealtimeDeath(Func, "getpeername");
1338   ExpectNonRealtimeSurvival(Func);
1339 }
1340 #endif
1341 
1342 #if SANITIZER_INTERCEPT_GETSOCKOPT
1343 TEST(TestRtsanInterceptors, GetsockoptOnASocketDiesWhenRealtime) {
1344   int val = 0;
1345   socklen_t len = static_cast<socklen_t>(sizeof(val));
1346   auto Func = [&val, &len]() {
1347     getsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, &len);
1348   };
1349   ExpectRealtimeDeath(Func, "getsockopt");
1350   ExpectNonRealtimeSurvival(Func);
1351 }
1352 
1353 TEST(TestRtsanInterceptors, SetsockoptOnASocketDiesWhenRealtime) {
1354   int val = 0;
1355   socklen_t len = static_cast<socklen_t>(sizeof(val));
1356   auto Func = [&val, &len]() {
1357     setsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, len);
1358   };
1359   ExpectRealtimeDeath(Func, "setsockopt");
1360   ExpectNonRealtimeSurvival(Func);
1361 }
1362 #endif
1363 
1364 TEST(TestRtsanInterceptors, SocketpairDiesWhenRealtime) {
1365   int pair[2]{};
1366   auto Func = [&pair]() { socketpair(0, 0, 0, pair); };
1367   ExpectRealtimeDeath(Func, "socketpair");
1368   ExpectNonRealtimeSurvival(Func);
1369 }
1370 
1371 /*
1372     I/O Multiplexing
1373 */
1374 
1375 TEST(TestRtsanInterceptors, PollDiesWhenRealtime) {
1376   struct pollfd fds[1];
1377   fds[0].fd = 0;
1378   fds[0].events = POLLIN;
1379 
1380   auto Func = [&fds]() { poll(fds, 1, 0); };
1381 
1382   ExpectRealtimeDeath(Func, "poll");
1383   ExpectNonRealtimeSurvival(Func);
1384 }
1385 
1386 #if !SANITIZER_APPLE
1387 // FIXME: This should work on Darwin as well
1388 // see the comment near the interceptor
1389 TEST(TestRtsanInterceptors, SelectDiesWhenRealtime) {
1390   fd_set readfds;
1391   FD_ZERO(&readfds);
1392   FD_SET(0, &readfds);
1393   struct timeval timeout = {0, 0};
1394 
1395   auto Func = [&readfds, &timeout]() {
1396     select(1, &readfds, nullptr, nullptr, &timeout);
1397   };
1398   ExpectRealtimeDeath(Func, "select");
1399   ExpectNonRealtimeSurvival(Func);
1400 }
1401 #endif
1402 
1403 TEST(TestRtsanInterceptors, PSelectDiesWhenRealtime) {
1404   fd_set readfds;
1405   FD_ZERO(&readfds);
1406   FD_SET(0, &readfds);
1407   struct timespec timeout = {0, 0};
1408 
1409   auto Func = [&]() {
1410     pselect(1, &readfds, nullptr, nullptr, &timeout, nullptr);
1411   };
1412   ExpectRealtimeDeath(Func, "pselect");
1413   ExpectNonRealtimeSurvival(Func);
1414 }
1415 
1416 #if SANITIZER_INTERCEPT_EPOLL
1417 TEST(TestRtsanInterceptors, EpollCreateDiesWhenRealtime) {
1418   auto Func = []() { epoll_create(1); };
1419   ExpectRealtimeDeath(Func, "epoll_create");
1420   ExpectNonRealtimeSurvival(Func);
1421 }
1422 
1423 TEST(TestRtsanInterceptors, EpollCreate1DiesWhenRealtime) {
1424   auto Func = []() { epoll_create1(EPOLL_CLOEXEC); };
1425   ExpectRealtimeDeath(Func, "epoll_create1");
1426   ExpectNonRealtimeSurvival(Func);
1427 }
1428 
1429 class EpollTest : public ::testing::Test {
1430 protected:
1431   void SetUp() override {
1432     epfd = epoll_create1(EPOLL_CLOEXEC);
1433     ASSERT_GE(epfd, 0);
1434   }
1435 
1436   void TearDown() override {
1437     if (epfd >= 0)
1438       close(epfd);
1439   }
1440 
1441   int GetEpollFd() { return epfd; }
1442 
1443 private:
1444   int epfd = -1;
1445 };
1446 
1447 TEST_F(EpollTest, EpollCtlDiesWhenRealtime) {
1448   auto Func = [this]() {
1449     struct epoll_event event = {.events = EPOLLIN, .data = {.fd = 0}};
1450     epoll_ctl(GetEpollFd(), EPOLL_CTL_ADD, 0, &event);
1451   };
1452   ExpectRealtimeDeath(Func, "epoll_ctl");
1453   ExpectNonRealtimeSurvival(Func);
1454 }
1455 
1456 TEST_F(EpollTest, EpollWaitDiesWhenRealtime) {
1457   auto Func = [this]() {
1458     struct epoll_event events[1];
1459     epoll_wait(GetEpollFd(), events, 1, 0);
1460   };
1461 
1462   ExpectRealtimeDeath(Func, "epoll_wait");
1463   ExpectNonRealtimeSurvival(Func);
1464 }
1465 
1466 TEST_F(EpollTest, EpollPWaitDiesWhenRealtime) {
1467   auto Func = [this]() {
1468     struct epoll_event events[1];
1469     epoll_pwait(GetEpollFd(), events, 1, 0, nullptr);
1470   };
1471 
1472   ExpectRealtimeDeath(Func, "epoll_pwait");
1473   ExpectNonRealtimeSurvival(Func);
1474 }
1475 #endif // SANITIZER_INTERCEPT_EPOLL
1476 
1477 #if SANITIZER_INTERCEPT_PPOLL
1478 TEST(TestRtsanInterceptors, PpollDiesWhenRealtime) {
1479   struct pollfd fds[1];
1480   fds[0].fd = 0;
1481   fds[0].events = POLLIN;
1482 
1483   timespec ts = {0, 0};
1484 
1485   auto Func = [&fds, &ts]() { ppoll(fds, 1, &ts, nullptr); };
1486 
1487   ExpectRealtimeDeath(Func, "ppoll");
1488   ExpectNonRealtimeSurvival(Func);
1489 }
1490 #endif
1491 
1492 #if SANITIZER_INTERCEPT_KQUEUE
1493 TEST(TestRtsanInterceptors, KqueueDiesWhenRealtime) {
1494   auto Func = []() { kqueue(); };
1495   ExpectRealtimeDeath(Func, "kqueue");
1496   ExpectNonRealtimeSurvival(Func);
1497 }
1498 
1499 class KqueueTest : public ::testing::Test {
1500 protected:
1501   void SetUp() override {
1502     kq = kqueue();
1503     ASSERT_GE(kq, 0);
1504   }
1505 
1506   void TearDown() override {
1507     if (kq >= 0)
1508       close(kq);
1509   }
1510 
1511   int GetKqueueFd() { return kq; }
1512 
1513 private:
1514   int kq = -1;
1515 };
1516 
1517 TEST_F(KqueueTest, KeventDiesWhenRealtime) {
1518   struct kevent event;
1519   EV_SET(&event, 0, EVFILT_READ, EV_ADD, 0, 0, nullptr);
1520   struct timespec timeout = {0, 0};
1521 
1522   auto Func = [this, event, timeout]() {
1523     kevent(GetKqueueFd(), &event, 1, nullptr, 0, &timeout);
1524   };
1525 
1526   ExpectRealtimeDeath(Func, "kevent");
1527   ExpectNonRealtimeSurvival(Func);
1528 }
1529 
1530 TEST_F(KqueueTest, Kevent64DiesWhenRealtime) {
1531   struct kevent64_s event;
1532   EV_SET64(&event, 0, EVFILT_READ, EV_ADD, 0, 0, 0, 0, 0);
1533   struct timespec timeout = {0, 0};
1534 
1535   auto Func = [this, event, timeout]() {
1536     kevent64(GetKqueueFd(), &event, 1, nullptr, 0, 0, &timeout);
1537   };
1538 
1539   ExpectRealtimeDeath(Func, "kevent64");
1540   ExpectNonRealtimeSurvival(Func);
1541 }
1542 #endif // SANITIZER_INTERCEPT_KQUEUE
1543 
1544 #if SANITIZER_LINUX
1545 TEST(TestRtsanInterceptors, InotifyInitDiesWhenRealtime) {
1546   auto Func = []() { inotify_init(); };
1547   ExpectRealtimeDeath(Func, "inotify_init");
1548   ExpectNonRealtimeSurvival(Func);
1549 }
1550 
1551 TEST(TestRtsanInterceptors, InotifyInit1DiesWhenRealtime) {
1552   auto Func = []() { inotify_init1(0); };
1553   ExpectRealtimeDeath(Func, "inotify_init1");
1554   ExpectNonRealtimeSurvival(Func);
1555 }
1556 
1557 TEST(TestRtsanInterceptors, InotifyAddWatchDiesWhenRealtime) {
1558   int fd = inotify_init();
1559   EXPECT_THAT(fd, Ne(-1));
1560   auto Func = [fd]() {
1561     inotify_add_watch(fd, "/tmp/rtsan_inotify", IN_CREATE);
1562   };
1563   ExpectRealtimeDeath(Func, "inotify_add_watch");
1564   ExpectNonRealtimeSurvival(Func);
1565 }
1566 
1567 TEST(TestRtsanInterceptors, InotifyRmWatchDiesWhenRealtime) {
1568   int fd = inotify_init();
1569   EXPECT_THAT(fd, Ne(-1));
1570   auto Func = [fd]() { inotify_rm_watch(fd, -1); };
1571   ExpectRealtimeDeath(Func, "inotify_rm_watch");
1572   ExpectNonRealtimeSurvival(Func);
1573 }
1574 #endif
1575 
1576 TEST(TestRtsanInterceptors, MkfifoDiesWhenRealtime) {
1577   auto Func = []() { mkfifo("/tmp/rtsan_test_fifo", 0); };
1578   ExpectRealtimeDeath(Func, "mkfifo");
1579   ExpectNonRealtimeSurvival(Func);
1580 }
1581 
1582 TEST(TestRtsanInterceptors, PipeDiesWhenRealtime) {
1583   int fds[2];
1584   auto Func = [&fds]() { pipe(fds); };
1585   ExpectRealtimeDeath(Func, "pipe");
1586   ExpectNonRealtimeSurvival(Func);
1587 }
1588 
1589 #if !SANITIZER_APPLE
1590 TEST(TestRtsanInterceptors, Pipe2DiesWhenRealtime) {
1591   int fds[2];
1592   auto Func = [&fds]() { pipe2(fds, O_CLOEXEC); };
1593   ExpectRealtimeDeath(Func, "pipe2");
1594   ExpectNonRealtimeSurvival(Func);
1595 }
1596 #endif
1597 
1598 #pragma clang diagnostic push
1599 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1600 TEST(TestRtsanInterceptors, SyscallDiesWhenRealtime) {
1601   auto Func = []() { syscall(SYS_getpid); };
1602   ExpectRealtimeDeath(Func, "syscall");
1603   ExpectNonRealtimeSurvival(Func);
1604 }
1605 
1606 TEST(TestRtsanInterceptors, GetPidReturnsSame) {
1607   int pid = syscall(SYS_getpid);
1608   EXPECT_THAT(pid, Ne(-1));
1609 
1610   EXPECT_THAT(getpid(), Eq(pid));
1611 }
1612 #pragma clang diagnostic pop
1613 
1614 #endif // SANITIZER_POSIX
1615