• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t
2 // RUN: %clangxx_msan -std=c++11 -O0 %s -o %t -DPOSITIVE && not %run %t |& FileCheck %s
3 
4 #include <assert.h>
5 #include <dlfcn.h>
6 #include <sanitizer/msan_interface.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <unistd.h>
12 #include <errno.h>
13 
14 typedef ssize_t (*process_vm_readwritev_fn)(pid_t, const iovec *, unsigned long,
15                                             const iovec *, unsigned long,
16                                             unsigned long);
17 
18 // Exit with success, emulating the expected output.
exit_dummy()19 int exit_dummy()
20 {
21 #ifdef POSITIVE
22     printf("process_vm_readv not found or not implemented!\n");
23     printf(
24         "WARNING: MemorySanitizer: use-of-uninitialized-value (not really)\n");
25     return 1;
26 #else
27     return 0;
28 #endif
29 }
30 
main(void)31 int main(void) {
32   // This requires glibc 2.15.
33   process_vm_readwritev_fn libc_process_vm_readv =
34       (process_vm_readwritev_fn)dlsym(RTLD_NEXT, "process_vm_readv");
35   if (!libc_process_vm_readv)
36     return exit_dummy();
37 
38   process_vm_readwritev_fn process_vm_readv =
39       (process_vm_readwritev_fn)dlsym(RTLD_DEFAULT, "process_vm_readv");
40   process_vm_readwritev_fn process_vm_writev =
41       (process_vm_readwritev_fn)dlsym(RTLD_DEFAULT, "process_vm_writev");
42 
43   char a[100];
44   memset(a, 0xab, 100);
45 
46   char b[100];
47   iovec iov_a[] = {{(void *)a, 20}, (void *)(a + 50), 10};
48   iovec iov_b[] = {{(void *)(b + 10), 10}, (void *)(b + 30), 20};
49 
50   __msan_poison(&b, sizeof(b));
51   ssize_t res = process_vm_readv(getpid(), iov_b, 2, iov_a, 2, 0);
52   if (errno == ENOSYS) // Function not implemented
53     return exit_dummy();
54 
55   assert(res == 30);
56   __msan_check_mem_is_initialized(b + 10, 10);
57   __msan_check_mem_is_initialized(b + 30, 20);
58   assert(__msan_test_shadow(b + 9, 1) == 0);
59   assert(__msan_test_shadow(b + 20, 1) == 0);
60   assert(__msan_test_shadow(b + 29, 1) == 0);
61   assert(__msan_test_shadow(b + 50, 1) == 0);
62 
63 #ifdef POSITIVE
64   __msan_unpoison(&b, sizeof(b));
65   __msan_poison(b + 32, 1);
66   res = process_vm_writev(getpid(), iov_b, 2, iov_a, 2, 0);
67 // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
68 #else
69   __msan_unpoison(&b, sizeof(b));
70   res = process_vm_writev(getpid(), iov_b, 2, iov_a, 2, 0);
71   assert(res == 30);
72 #endif
73 
74   return 0;
75 }
76