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, format: "ASSERTION FAILED: %s\n" , message); |
25 | exit(status: 1); |
26 | } |
27 | } |
28 | |
29 | class ScopedFileCleanup { |
30 | public: |
31 | [[nodiscard]] ScopedFileCleanup() = default; |
32 | ~ScopedFileCleanup() { |
33 | if (access(name: GetTemporaryFilePath(), F_OK) != -1) |
34 | unlink(name: 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(condition: fd != -1, message: "Failed to open file - write" ); |
49 | |
50 | int written = syscall(SYS_write, fd, "Hello, world!" , 13); |
51 | custom_assert(condition: written == 13, message: "Failed to write to file" ); |
52 | |
53 | custom_assert(condition: syscall(SYS_close, fd) == 0, message: "Failed to close file - write" ); |
54 | } |
55 | |
56 | { |
57 | int fd = syscall(SYS_openat, AT_FDCWD, GetTemporaryFilePath(), O_RDONLY); |
58 | custom_assert(condition: fd != -1, message: "Failed to open file - read" ); |
59 | |
60 | char buffer[13]; |
61 | int read = syscall(SYS_read, fd, buffer, 13); |
62 | custom_assert(condition: read == 13, message: "Failed to read from file" ); |
63 | |
64 | custom_assert(condition: memcmp(s1: buffer, s2: "Hello, world!" , n: 13) == 0, |
65 | message: "Read data does not match written data" ); |
66 | |
67 | custom_assert(condition: syscall(SYS_close, fd) == 0, message: "Failed to close file - read" ); |
68 | } |
69 | |
70 | unlink(name: GetTemporaryFilePath()); |
71 | printf(format: "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 | |