1 // RUN: %clangxx_msan -O0 %s -o %t && %run %t 2>&1 2 // RUN: %clangxx_msan -O3 %s -o %t && %run %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 <linux/aio_abi.h> 11 #include <signal.h> 12 #include <sys/ptrace.h> 13 #include <sys/stat.h> 14 #include <sys/uio.h> 15 16 #include <sanitizer/linux_syscall_hooks.h> 17 #include <sanitizer/msan_interface.h> 18 19 /* Test the presence of __sanitizer_syscall_ in the tool runtime, and general 20 sanity of their behaviour. */ 21 22 int main(int argc, char *argv[]) { 23 char buf[1000] __attribute__((aligned(8))); 24 const int kTen = 10; 25 const int kFortyTwo = 42; 26 memset(buf, 0, sizeof(buf)); 27 __msan_unpoison(buf, sizeof(buf)); 28 __sanitizer_syscall_pre_recvmsg(0, buf, 0); 29 __sanitizer_syscall_pre_rt_sigpending(buf, kTen); 30 __sanitizer_syscall_pre_getdents(0, buf, kTen); 31 __sanitizer_syscall_pre_getdents64(0, buf, kTen); 32 33 __msan_unpoison(buf, sizeof(buf)); 34 __sanitizer_syscall_post_recvmsg(0, 0, buf, 0); 35 __sanitizer_syscall_post_rt_sigpending(-1, buf, kTen); 36 __sanitizer_syscall_post_getdents(0, 0, buf, kTen); 37 __sanitizer_syscall_post_getdents64(0, 0, buf, kTen); 38 assert(__msan_test_shadow(buf, sizeof(buf)) == -1); 39 40 __msan_unpoison(buf, sizeof(buf)); 41 __sanitizer_syscall_post_recvmsg(kTen, 0, buf, 0); 42 43 // Tell the kernel that the output struct size is 10 bytes, verify that those 44 // bytes are unpoisoned, and the next byte is not. 45 __msan_poison(buf, kTen + 1); 46 __sanitizer_syscall_post_rt_sigpending(0, buf, kTen); 47 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen); 48 49 __msan_poison(buf, kTen + 1); 50 __sanitizer_syscall_post_getdents(kTen, 0, buf, kTen); 51 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen); 52 53 __msan_poison(buf, kTen + 1); 54 __sanitizer_syscall_post_getdents64(kTen, 0, buf, kTen); 55 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen); 56 57 __msan_poison(buf, sizeof(buf)); 58 __sanitizer_syscall_post_clock_getres(0, 0, buf); 59 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(long) * 2); 60 61 __msan_poison(buf, sizeof(buf)); 62 __sanitizer_syscall_post_clock_gettime(0, 0, buf); 63 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(long) * 2); 64 65 // Failed syscall does not write to the buffer. 66 __msan_poison(buf, sizeof(buf)); 67 __sanitizer_syscall_post_clock_gettime(-1, 0, buf); 68 assert(__msan_test_shadow(buf, sizeof(buf)) == 0); 69 70 __msan_poison(buf, sizeof(buf)); 71 __sanitizer_syscall_post_read(5, 42, buf, 10); 72 assert(__msan_test_shadow(buf, sizeof(buf)) == 5); 73 74 __msan_poison(buf, sizeof(buf)); 75 __sanitizer_syscall_post_newfstatat(0, 5, "/path/to/file", buf, 0); 76 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(struct stat)); 77 78 __msan_poison(buf, sizeof(buf)); 79 int prio = 0; 80 __sanitizer_syscall_post_mq_timedreceive(kFortyTwo, 5, buf, sizeof(buf), &prio, 0); 81 assert(__msan_test_shadow(buf, sizeof(buf)) == kFortyTwo); 82 assert(__msan_test_shadow(&prio, sizeof(prio)) == -1); 83 84 __msan_poison(buf, sizeof(buf)); 85 __sanitizer_syscall_post_ptrace(0, PTRACE_PEEKUSER, kFortyTwo, 0xABCD, buf); 86 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(void *)); 87 88 __msan_poison(buf, sizeof(buf)); 89 struct iocb iocb[3]; 90 struct iocb *iocbp[3] = { &iocb[0], &iocb[1], &iocb[2] }; 91 memset(iocb, 0, sizeof(iocb)); 92 iocb[0].aio_lio_opcode = IOCB_CMD_PREAD; 93 iocb[0].aio_buf = (__u64)buf; 94 iocb[0].aio_nbytes = 10; 95 iocb[1].aio_lio_opcode = IOCB_CMD_PREAD; 96 iocb[1].aio_buf = (__u64)(&buf[20]); 97 iocb[1].aio_nbytes = 15; 98 struct iovec vec[2] = { {&buf[40], 3}, {&buf[50], 20} }; 99 iocb[2].aio_lio_opcode = IOCB_CMD_PREADV; 100 iocb[2].aio_buf = (__u64)(&vec); 101 iocb[2].aio_nbytes = 2; 102 __sanitizer_syscall_pre_io_submit(0, 3, &iocbp); 103 assert(__msan_test_shadow(buf, sizeof(buf)) == 10); 104 assert(__msan_test_shadow(buf + 20, sizeof(buf) - 20) == 15); 105 assert(__msan_test_shadow(buf + 40, sizeof(buf) - 40) == 3); 106 assert(__msan_test_shadow(buf + 50, sizeof(buf) - 50) == 20); 107 108 __msan_poison(buf, sizeof(buf)); 109 char *p = buf; 110 __msan_poison(&p, sizeof(p)); 111 __sanitizer_syscall_post_io_setup(0, 1, &p); 112 assert(__msan_test_shadow(&p, sizeof(p)) == -1); 113 assert(__msan_test_shadow(buf, sizeof(buf)) >= 32); 114 115 __msan_poison(buf, sizeof(buf)); 116 __sanitizer_syscall_post_pipe(0, (int *)buf); 117 assert(__msan_test_shadow(buf, sizeof(buf)) == 2 * sizeof(int)); 118 119 __msan_poison(buf, sizeof(buf)); 120 __sanitizer_syscall_post_pipe2(0, (int *)buf, 0); 121 assert(__msan_test_shadow(buf, sizeof(buf)) == 2 * sizeof(int)); 122 123 __msan_poison(buf, sizeof(buf)); 124 __sanitizer_syscall_post_socketpair(0, 0, 0, 0, (int *)buf); 125 assert(__msan_test_shadow(buf, sizeof(buf)) == 2 * sizeof(int)); 126 127 __msan_poison(buf, sizeof(buf)); 128 __sanitizer_syscall_post_sigaltstack(0, nullptr, (stack_t *)buf); 129 assert(__msan_test_shadow(buf, sizeof(buf)) == sizeof(stack_t)); 130 131 return 0; 132 } 133