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