1 // RUN: %clangxx_msan -O0 %s -o %t && %run %t 2>&1
2 // RUN: %clangxx_msan -O3 %s -o %t && %run %t 2>&1
10 #include <linux/aio_abi.h>
12 #include <sys/epoll.h>
13 #include <sys/ptrace.h>
17 #include <sanitizer/linux_syscall_hooks.h>
18 #include <sanitizer/msan_interface.h>
20 /* Test the presence of __sanitizer_syscall_ in the tool runtime, and general
21 sanity of their behaviour. */
23 int main(int argc
, char *argv
[]) {
24 char buf
[1000] __attribute__((aligned(8)));
26 const int kFortyTwo
= 42;
27 memset(buf
, 0, sizeof(buf
));
28 __msan_unpoison(buf
, sizeof(buf
));
29 __sanitizer_syscall_pre_recvmsg(0, buf
, 0);
30 __sanitizer_syscall_pre_rt_sigpending(buf
, kTen
);
31 __sanitizer_syscall_pre_getdents(0, buf
, kTen
);
32 __sanitizer_syscall_pre_getdents64(0, buf
, kTen
);
34 __msan_unpoison(buf
, sizeof(buf
));
35 __sanitizer_syscall_post_recvmsg(0, 0, buf
, 0);
36 __sanitizer_syscall_post_rt_sigpending(-1, buf
, kTen
);
37 __sanitizer_syscall_post_getdents(0, 0, buf
, kTen
);
38 __sanitizer_syscall_post_getdents64(0, 0, buf
, kTen
);
39 assert(__msan_test_shadow(buf
, sizeof(buf
)) == -1);
41 __msan_unpoison(buf
, sizeof(buf
));
42 __sanitizer_syscall_post_recvmsg(kTen
, 0, buf
, 0);
44 // Tell the kernel that the output struct size is 10 bytes, verify that those
45 // bytes are unpoisoned, and the next byte is not.
46 __msan_poison(buf
, kTen
+ 1);
47 __sanitizer_syscall_post_rt_sigpending(0, buf
, kTen
);
48 assert(__msan_test_shadow(buf
, sizeof(buf
)) == kTen
);
50 __msan_poison(buf
, kTen
+ 1);
51 __sanitizer_syscall_post_getdents(kTen
, 0, buf
, kTen
);
52 assert(__msan_test_shadow(buf
, sizeof(buf
)) == kTen
);
54 __msan_poison(buf
, kTen
+ 1);
55 __sanitizer_syscall_post_getdents64(kTen
, 0, buf
, kTen
);
56 assert(__msan_test_shadow(buf
, sizeof(buf
)) == kTen
);
58 __msan_poison(buf
, sizeof(buf
));
59 __sanitizer_syscall_post_clock_getres(0, 0, buf
);
60 assert(__msan_test_shadow(buf
, sizeof(buf
)) == sizeof(long) * 2);
62 __msan_poison(buf
, sizeof(buf
));
63 __sanitizer_syscall_post_clock_gettime(0, 0, buf
);
64 assert(__msan_test_shadow(buf
, sizeof(buf
)) == sizeof(long) * 2);
66 // Failed syscall does not write to the buffer.
67 __msan_poison(buf
, sizeof(buf
));
68 __sanitizer_syscall_post_clock_gettime(-1, 0, buf
);
69 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 0);
71 __msan_poison(buf
, sizeof(buf
));
72 __sanitizer_syscall_post_read(5, 42, buf
, 10);
73 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 5);
75 __msan_poison(buf
, sizeof(buf
));
76 __sanitizer_syscall_post_newfstatat(0, 5, "/path/to/file", buf
, 0);
77 assert(__msan_test_shadow(buf
, sizeof(buf
)) == sizeof(struct stat
));
79 __msan_poison(buf
, sizeof(buf
));
81 __sanitizer_syscall_post_mq_timedreceive(kFortyTwo
, 5, buf
, sizeof(buf
), &prio
, 0);
82 assert(__msan_test_shadow(buf
, sizeof(buf
)) == kFortyTwo
);
83 assert(__msan_test_shadow(&prio
, sizeof(prio
)) == -1);
85 __msan_poison(buf
, sizeof(buf
));
86 __sanitizer_syscall_post_ptrace(0, PTRACE_PEEKUSER
, kFortyTwo
, 0xABCD, buf
);
87 assert(__msan_test_shadow(buf
, sizeof(buf
)) == sizeof(void *));
89 __msan_poison(buf
, sizeof(buf
));
91 struct iocb
*iocbp
[3] = { &iocb
[0], &iocb
[1], &iocb
[2] };
92 memset(iocb
, 0, sizeof(iocb
));
93 iocb
[0].aio_lio_opcode
= IOCB_CMD_PREAD
;
94 iocb
[0].aio_buf
= (__u64
)buf
;
95 iocb
[0].aio_nbytes
= 10;
96 iocb
[1].aio_lio_opcode
= IOCB_CMD_PREAD
;
97 iocb
[1].aio_buf
= (__u64
)(&buf
[20]);
98 iocb
[1].aio_nbytes
= 15;
99 struct iovec vec
[2] = { {&buf
[40], 3}, {&buf
[50], 20} };
100 iocb
[2].aio_lio_opcode
= IOCB_CMD_PREADV
;
101 iocb
[2].aio_buf
= (__u64
)(&vec
);
102 iocb
[2].aio_nbytes
= 2;
103 __sanitizer_syscall_pre_io_submit(0, 3, &iocbp
);
104 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 10);
105 assert(__msan_test_shadow(buf
+ 20, sizeof(buf
) - 20) == 15);
106 assert(__msan_test_shadow(buf
+ 40, sizeof(buf
) - 40) == 3);
107 assert(__msan_test_shadow(buf
+ 50, sizeof(buf
) - 50) == 20);
109 __msan_poison(buf
, sizeof(buf
));
111 __msan_poison(&p
, sizeof(p
));
112 __sanitizer_syscall_post_io_setup(0, 1, &p
);
113 assert(__msan_test_shadow(&p
, sizeof(p
)) == -1);
114 assert(__msan_test_shadow(buf
, sizeof(buf
)) >= 32);
116 __msan_poison(buf
, sizeof(buf
));
117 __sanitizer_syscall_post_pipe(0, (int *)buf
);
118 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 2 * sizeof(int));
120 __msan_poison(buf
, sizeof(buf
));
121 __sanitizer_syscall_post_pipe2(0, (int *)buf
, 0);
122 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 2 * sizeof(int));
124 __msan_poison(buf
, sizeof(buf
));
125 __sanitizer_syscall_post_socketpair(0, 0, 0, 0, (int *)buf
);
126 assert(__msan_test_shadow(buf
, sizeof(buf
)) == 2 * sizeof(int));
128 __msan_poison(buf
, sizeof(buf
));
129 __sanitizer_syscall_post_sigaltstack(0, nullptr, (stack_t
*)buf
);
130 assert(__msan_test_shadow(buf
, sizeof(buf
)) == sizeof(stack_t
));
132 __msan_poison(buf
, sizeof(buf
));
133 long max_events
= sizeof(buf
) / sizeof(epoll_event
);
134 __sanitizer_syscall_pre_epoll_wait(0, buf
, max_events
, 0);
135 __sanitizer_syscall_post_epoll_wait(max_events
, 0, buf
, max_events
, 0);
136 assert(__msan_test_shadow(buf
, sizeof(buf
)) == max_events
* sizeof(epoll_event
));
138 __msan_poison(buf
, sizeof(buf
));
139 sigset_t sigset
= {};
140 __sanitizer_syscall_pre_epoll_pwait(0, buf
, max_events
, 0, &sigset
, sizeof(sigset
));
141 __sanitizer_syscall_post_epoll_pwait(max_events
, 0, buf
, max_events
, 0, &sigset
, sizeof(sigset
));
142 assert(__msan_test_shadow(buf
, sizeof(buf
)) == max_events
* sizeof(epoll_event
));
144 __msan_poison(buf
, sizeof(buf
));
146 timespec timespec
= {};
147 __sanitizer_syscall_pre_epoll_pwait2(0, buf
, max_events
, ×pec
,
148 &sigset
, sizeof(sigset
));
149 __sanitizer_syscall_post_epoll_pwait2(max_events
, 0, buf
, max_events
,
150 ×pec
, &sigset
, sizeof(sigset
));
151 assert(__msan_test_shadow(buf
, sizeof(buf
)) == max_events
* sizeof(epoll_event
));