155 lines · cpp
1// RUN: %clangxx_msan -O0 %s -o %t && %run %t 2>&12// RUN: %clangxx_msan -O3 %s -o %t && %run %t 2>&13 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/epoll.h>13#include <sys/ptrace.h>14#include <sys/stat.h>15#include <sys/uio.h>16 17#include <sanitizer/linux_syscall_hooks.h>18#include <sanitizer/msan_interface.h>19 20/* Test the presence of __sanitizer_syscall_ in the tool runtime, and general21 sanity of their behaviour. */22 23int main(int argc, char *argv[]) {24 char buf[1000] __attribute__((aligned(8)));25 const int kTen = 10;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);33 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);40 41 __msan_unpoison(buf, sizeof(buf));42 __sanitizer_syscall_post_recvmsg(kTen, 0, buf, 0);43 44 // Tell the kernel that the output struct size is 10 bytes, verify that those45 // 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);49 50 __msan_poison(buf, kTen + 1);51 __sanitizer_syscall_post_getdents(kTen, 0, buf, kTen);52 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen);53 54 __msan_poison(buf, kTen + 1);55 __sanitizer_syscall_post_getdents64(kTen, 0, buf, kTen);56 assert(__msan_test_shadow(buf, sizeof(buf)) == kTen);57 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);61 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);65 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);70 71 __msan_poison(buf, sizeof(buf));72 __sanitizer_syscall_post_read(5, 42, buf, 10);73 assert(__msan_test_shadow(buf, sizeof(buf)) == 5);74 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));78 79 __msan_poison(buf, sizeof(buf));80 int prio = 0;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);84 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 *));88 89 __msan_poison(buf, sizeof(buf));90 struct iocb iocb[3];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);108 109 __msan_poison(buf, sizeof(buf));110 char *p = 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);115 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));119 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));123 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));127 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));131 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));137 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));143 144 __msan_poison(buf, sizeof(buf));145 sigset = {};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));152 153 return 0;154}155