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 
13 typedef ssize_t (*process_vm_readwritev_fn)(pid_t, const iovec *, unsigned long,
14                                             const iovec *, unsigned long,
15                                             unsigned long);
16 
main(void)17 int main(void) {
18   // This requires glibc 2.15.
19   process_vm_readwritev_fn libc_process_vm_readv =
20       (process_vm_readwritev_fn)dlsym(RTLD_NEXT, "process_vm_readv");
21   if (!libc_process_vm_readv) {
22 // Exit with success, emulating the expected output.
23 #ifdef POSITIVE
24     printf("process_vm_readv not found!\n");
25     printf(
26         "WARNING: MemorySanitizer: use-of-uninitialized-value (not really)\n");
27     return 1;
28 #else
29     return 0;
30 #endif
31   }
32 
33   process_vm_readwritev_fn process_vm_readv =
34       (process_vm_readwritev_fn)dlsym(RTLD_DEFAULT, "process_vm_readv");
35   process_vm_readwritev_fn process_vm_writev =
36       (process_vm_readwritev_fn)dlsym(RTLD_DEFAULT, "process_vm_writev");
37 
38   char a[100];
39   memset(a, 0xab, 100);
40 
41   char b[100];
42   iovec iov_a[] = {{(void *)a, 20}, (void *)(a + 50), 10};
43   iovec iov_b[] = {{(void *)(b + 10), 10}, (void *)(b + 30), 20};
44 
45   __msan_poison(&b, sizeof(b));
46   ssize_t res = process_vm_readv(getpid(), iov_b, 2, iov_a, 2, 0);
47   assert(res == 30);
48   __msan_check_mem_is_initialized(b + 10, 10);
49   __msan_check_mem_is_initialized(b + 30, 20);
50   assert(__msan_test_shadow(b + 9, 1) == 0);
51   assert(__msan_test_shadow(b + 20, 1) == 0);
52   assert(__msan_test_shadow(b + 29, 1) == 0);
53   assert(__msan_test_shadow(b + 50, 1) == 0);
54 
55 #ifdef POSITIVE
56   __msan_unpoison(&b, sizeof(b));
57   __msan_poison(b + 32, 1);
58   res = process_vm_writev(getpid(), iov_b, 2, iov_a, 2, 0);
59 // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
60 #else
61   __msan_unpoison(&b, sizeof(b));
62   res = process_vm_writev(getpid(), iov_b, 2, iov_a, 2, 0);
63   assert(res == 30);
64 #endif
65 
66   return 0;
67 }
68