xref: /llvm-project/compiler-rt/test/rtsan/syscall.cpp (revision eae30a240e34e1fd31b57096a1b8bdbd8b84352d)
1 // RUN: %clangxx -fsanitize=realtime %s -o %t
2 // RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-RTSAN,CHECK
3 
4 // RUN: %clangxx %s -o %t
5 // RUN: %run %t 2>&1 | FileCheck %s
6 
7 // UNSUPPORTED: ios
8 
9 // Intent: Ensure the `syscall` call behaves in the same way with/without the
10 //         sanitizer disabled
11 
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/syscall.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 
20 const char *GetTemporaryFilePath() { return "/tmp/rtsan_syscall_test.txt"; }
21 
22 void custom_assert(bool condition, const char *message) {
23   if (!condition) {
24     fprintf(stderr, "ASSERTION FAILED: %s\n", message);
25     exit(1);
26   }
27 }
28 
29 class ScopedFileCleanup {
30 public:
31   [[nodiscard]] ScopedFileCleanup() = default;
32   ~ScopedFileCleanup() {
33     if (access(GetTemporaryFilePath(), F_OK) != -1)
34       unlink(GetTemporaryFilePath());
35   }
36 };
37 
38 // Apple has deprecated `syscall`, ignore that error
39 #pragma clang diagnostic push
40 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
41 int main() [[clang::nonblocking]] {
42   ScopedFileCleanup cleanup;
43 
44   {
45     int fd = syscall(SYS_openat, AT_FDCWD, GetTemporaryFilePath(),
46                      O_CREAT | O_WRONLY, 0644);
47 
48     custom_assert(fd != -1, "Failed to open file - write");
49 
50     int written = syscall(SYS_write, fd, "Hello, world!", 13);
51     custom_assert(written == 13, "Failed to write to file");
52 
53     custom_assert(syscall(SYS_close, fd) == 0, "Failed to close file - write");
54   }
55 
56   {
57     int fd = syscall(SYS_openat, AT_FDCWD, GetTemporaryFilePath(), O_RDONLY);
58     custom_assert(fd != -1, "Failed to open file - read");
59 
60     char buffer[13];
61     int read = syscall(SYS_read, fd, buffer, 13);
62     custom_assert(read == 13, "Failed to read from file");
63 
64     custom_assert(memcmp(buffer, "Hello, world!", 13) == 0,
65                   "Read data does not match written data");
66 
67     custom_assert(syscall(SYS_close, fd) == 0, "Failed to close file - read");
68   }
69 
70   unlink(GetTemporaryFilePath());
71   printf("DONE\n");
72 }
73 #pragma clang diagnostic pop
74 
75 // CHECK-NOT: ASSERTION FAILED
76 // CHECK-RTSAN-COUNT-6: Intercepted call to real-time unsafe function `syscall`
77 
78 // CHECK: DONE
79