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