1 // RUN: %clang_dfsan %s -o %t && %run %t | FileCheck %s 2 // 3 // REQUIRES: x86_64-target-arch 4 5 // Tests that the custom implementation of write() does writes with or without 6 // a callback set using dfsan_set_write_callback(). 7 // REQUIRES: stable-runtime 8 9 #include <sanitizer/dfsan_interface.h> 10 11 #include <assert.h> 12 #include <fcntl.h> 13 #include <stdio.h> 14 #include <string.h> 15 #include <unistd.h> 16 17 // Check write callback arguments by having the callback store them in 18 // the following variables: 19 static int last_callback_arg_fd; 20 static const void *last_callback_arg_buf; 21 static size_t last_callback_arg_count; 22 23 // Allow tests to check the number of callbacks made by incrementing 24 // this count. When callbacks are verified, the count is reset. 25 static int count_unverified_callbacks = 0; 26 27 // This callbact will be installed using dfsan_set_write_callback() 28 // in tests below. 29 static void write_callback(int fd, const void *buf, size_t count) { 30 // Do not do anything in this function that might call write(). 31 count_unverified_callbacks++; 32 33 last_callback_arg_fd = fd; 34 last_callback_arg_buf = buf; 35 last_callback_arg_count = count; 36 } 37 38 static void write_string_to_stdout(char *string) { 39 char *cur = string; 40 int bytes_left = strlen(string); 41 while (bytes_left > 0) { 42 int res = write(fileno(stdout), cur, bytes_left); 43 assert (res >= 0); 44 cur += res; 45 bytes_left -= res; 46 } 47 } 48 49 static void test_can_write_without_callback() { 50 dfsan_set_write_callback(NULL); 51 count_unverified_callbacks = 0; 52 53 char aString[] = "Test that writes work without callback.\n"; 54 // CHECK: Test that writes work without callback. 55 write_string_to_stdout(aString); 56 57 assert(count_unverified_callbacks == 0); 58 } 59 60 static void test_can_write_with_callback() { 61 dfsan_set_write_callback(write_callback); 62 63 count_unverified_callbacks = 0; 64 65 char stringWithCallback[] = "Test that writes work with callback.\n"; 66 // CHECK: Test that writes work with callback. 67 write_string_to_stdout(stringWithCallback); 68 69 // Data was written, so at least one call to write() was made. 70 // Because a write may not process all the bytes it is passed, there 71 // may have been several calls to write(). 72 assert(count_unverified_callbacks > 0); 73 count_unverified_callbacks = 0; 74 75 dfsan_set_write_callback(NULL); 76 77 char stringWithoutCallback[] = "Writes work after the callback is removed.\n"; 78 // CHECK: Writes work after the callback is removed. 79 write_string_to_stdout(stringWithoutCallback); 80 assert(count_unverified_callbacks == 0); 81 } 82 83 static void test_failing_write_runs_callback() { 84 // Open /dev/null in read-only mode. Calling write() on fd will fail. 85 int fd = open("/dev/null", O_RDONLY); 86 assert(fd != -1); 87 88 // Install a callback. 89 dfsan_set_write_callback(write_callback); 90 91 // Write to the read-only file handle. The write will fail, but the callback 92 // should still be invoked. 93 char aString[] = "This text will fail to be written.\n"; 94 int len = strlen(aString); 95 int write_result = write(fd, aString, len); 96 assert(write_result == -1); 97 98 assert(count_unverified_callbacks == 1); 99 count_unverified_callbacks = 0; 100 101 assert(fd == last_callback_arg_fd); 102 assert(aString == last_callback_arg_buf); 103 assert(len == last_callback_arg_count); 104 105 close(fd); 106 } 107 108 int main(int argc, char* argv[]) { 109 test_can_write_without_callback(); 110 test_can_write_with_callback(); 111 test_failing_write_runs_callback(); 112 } 113