1 // RUN: %clangxx_msan -m64 -O0 %s -o %t && %t 2>&1
2 // RUN: %clangxx_msan -m64 -O3 %s -o %t && %t 2>&1
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <glob.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #include <sanitizer/linux_syscall_hooks.h>
11 #include <sanitizer/msan_interface.h>
12
13 /* Test the presence of __sanitizer_syscall_ in the tool runtime, and general
14 sanity of their behaviour. */
15
main(int argc,char * argv[])16 int main(int argc, char *argv[]) {
17 char buf[1000];
18 const int kTen = 10;
19 memset(buf, 0, sizeof(buf));
20 __msan_unpoison(buf, sizeof(buf));
21 __sanitizer_syscall_pre_recvmsg(0, buf, 0);
22 __sanitizer_syscall_pre_rt_sigpending(buf, kTen);
23 __sanitizer_syscall_pre_getdents(0, buf, kTen);
24 __sanitizer_syscall_pre_getdents64(0, buf, kTen);
25
26 __msan_unpoison(buf, sizeof(buf));
27 __sanitizer_syscall_post_recvmsg(0, 0, buf, 0);
28 __sanitizer_syscall_post_rt_sigpending(-1, buf, kTen);
29 __sanitizer_syscall_post_getdents(0, 0, buf, kTen);
30 __sanitizer_syscall_post_getdents64(0, 0, buf, kTen);
31 assert(__msan_test_shadow(buf, sizeof(buf)) == -1);
32
33 __msan_unpoison(buf, sizeof(buf));
34 __sanitizer_syscall_post_recvmsg(kTen, 0, buf, 0);
35
36 // Tell the kernel that the output struct size is 10 bytes, verify that those
37 // bytes are unpoisoned, and the next byte is not.
38 __msan_poison(buf, kTen + 1);
39 __sanitizer_syscall_post_rt_sigpending(0, buf, kTen);
40 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen);
41
42 __msan_poison(buf, kTen + 1);
43 __sanitizer_syscall_post_getdents(kTen, 0, buf, kTen);
44 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen);
45
46 __msan_poison(buf, kTen + 1);
47 __sanitizer_syscall_post_getdents64(kTen, 0, buf, kTen);
48 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen);
49
50 __msan_poison(buf, sizeof(buf));
51 __sanitizer_syscall_post_clock_getres(0, 0, buf);
52 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(long) * 2);
53
54 __msan_poison(buf, sizeof(buf));
55 __sanitizer_syscall_post_clock_gettime(0, 0, buf);
56 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(long) * 2);
57
58 // Failed syscall does not write to the buffer.
59 __msan_poison(buf, sizeof(buf));
60 __sanitizer_syscall_post_clock_gettime(-1, 0, buf);
61 assert(__msan_test_shadow(buf, sizeof(buf)) == 0);
62
63 __msan_poison(buf, sizeof(buf));
64 __sanitizer_syscall_post_read(5, 42, buf, 10);
65 assert(__msan_test_shadow(buf, sizeof(buf)) == 5);
66
67 return 0;
68 }
69