brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · a23a934 Raw
79 lines · cpp
1// RUN: %clangxx -fsanitize=realtime %s -o %t2// RUN: %env_rtsan_opts="halt_on_error=false" %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-RTSAN,CHECK3 4// RUN: %clangxx %s -o %t5// RUN: %run %t 2>&1 | FileCheck %s6 7// UNSUPPORTED: ios8 9// Intent: Ensure the `syscall` call behaves in the same way with/without the10//         sanitizer disabled11 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 20const char *GetTemporaryFilePath() { return "/tmp/rtsan_syscall_test.txt"; }21 22void custom_assert(bool condition, const char *message) {23  if (!condition) {24    fprintf(stderr, "ASSERTION FAILED: %s\n", message);25    exit(1);26  }27}28 29class ScopedFileCleanup {30public: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 error39#pragma clang diagnostic push40#pragma clang diagnostic ignored "-Wdeprecated-declarations"41int 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 pop74 75// CHECK-NOT: ASSERTION FAILED76// CHECK-RTSAN-COUNT-6: Intercepted call to real-time unsafe function `syscall`77 78// CHECK: DONE79