17c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar// RUN: %clang_dfsan %s -o %t && %run %t | FileCheck %s 27c9150579ed0278492f51cc8434b1d63a44b9bd1Pirama Arumuga Nainar// RUN: %clang_dfsan -mllvm -dfsan-args-abi %s -o %t && %run %t | FileCheck %s 32d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 42d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// Tests that the custom implementation of write() does writes with or without 52d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// a callback set using dfsan_set_write_callback(). 62d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 72d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <sanitizer/dfsan_interface.h> 82d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 92d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <assert.h> 102d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <fcntl.h> 112d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <stdio.h> 122d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <string.h> 132d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include <unistd.h> 142d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 152d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// Check write callback arguments by having the callback store them in 162d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// the following variables: 172d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic int last_callback_arg_fd; 182d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic const void *last_callback_arg_buf; 192d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic size_t last_callback_arg_count; 202d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 212d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// Allow tests to check the number of callbacks made by incrementing 222d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// this count. When callbacks are verified, the count is reset. 232d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic int count_unverified_callbacks = 0; 242d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 252d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// This callbact will be installed using dfsan_set_write_callback() 262d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// in tests below. 272d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic void write_callback(int fd, const void *buf, size_t count) { 282d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Do not do anything in this function that might call write(). 292d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines count_unverified_callbacks++; 302d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 312d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines last_callback_arg_fd = fd; 322d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines last_callback_arg_buf = buf; 332d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines last_callback_arg_count = count; 342d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 352d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 362d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic void write_string_to_stdout(char *string) { 372d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines char *cur = string; 382d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines int bytes_left = strlen(string); 392d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines while (bytes_left > 0) { 402d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines int res = write(fileno(stdout), cur, bytes_left); 412d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert (res >= 0); 422d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines cur += res; 432d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines bytes_left -= res; 442d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines } 452d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 462d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 472d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic void test_can_write_without_callback() { 482d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines dfsan_set_write_callback(NULL); 492d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines count_unverified_callbacks = 0; 502d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 512d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines char aString[] = "Test that writes work without callback.\n"; 522d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // CHECK: Test that writes work without callback. 532d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines write_string_to_stdout(aString); 542d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 552d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(count_unverified_callbacks == 0); 562d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 572d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 582d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic void test_can_write_with_callback() { 592d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines dfsan_set_write_callback(write_callback); 602d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 612d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines count_unverified_callbacks = 0; 622d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 632d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines char stringWithCallback[] = "Test that writes work with callback.\n"; 642d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // CHECK: Test that writes work with callback. 652d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines write_string_to_stdout(stringWithCallback); 662d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 672d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Data was written, so at least one call to write() was made. 682d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Because a write may not process all the bytes it is passed, there 692d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // may have been several calls to write(). 702d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(count_unverified_callbacks > 0); 712d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines count_unverified_callbacks = 0; 722d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 732d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines dfsan_set_write_callback(NULL); 742d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 752d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines char stringWithoutCallback[] = "Writes work after the callback is removed.\n"; 762d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // CHECK: Writes work after the callback is removed. 772d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines write_string_to_stdout(stringWithoutCallback); 782d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(count_unverified_callbacks == 0); 792d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 802d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 812d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesstatic void test_failing_write_runs_callback() { 822d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Open /dev/null in read-only mode. Calling write() on fd will fail. 832d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines int fd = open("/dev/null", O_RDONLY); 842d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(fd != -1); 852d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 862d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Install a callback. 872d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines dfsan_set_write_callback(write_callback); 882d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 892d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // Write to the read-only file handle. The write will fail, but the callback 902d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines // should still be invoked. 912d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines char aString[] = "This text will fail to be written.\n"; 922d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines int len = strlen(aString); 932d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines int write_result = write(fd, aString, len); 942d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(write_result == -1); 952d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 962d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(count_unverified_callbacks == 1); 972d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines count_unverified_callbacks = 0; 982d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 992d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(fd == last_callback_arg_fd); 1002d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(aString == last_callback_arg_buf); 1012d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines assert(len == last_callback_arg_count); 1022d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 1032d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines close(fd); 1042d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 1052d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines 1062d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hinesint main(int argc, char* argv[]) { 1072d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines test_can_write_without_callback(); 1082d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines test_can_write_with_callback(); 1092d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines test_failing_write_runs_callback(); 1102d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines} 111