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