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 2>&1 | 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
14typedef 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.
19int 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
31int 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, name: "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, name: "process_vm_readv");
40 process_vm_readwritev_fn process_vm_writev =
41 (process_vm_readwritev_fn)dlsym(RTLD_DEFAULT, name: "process_vm_writev");
42
43 char a[100];
44 memset(s: a, c: 0xab, n: 100);
45
46 char b[100];
47 iovec iov_a[] = {{.iov_base: (void *)a, .iov_len: 20}, (void *)(a + 50), 10};
48 iovec iov_b[] = {{.iov_base: (void *)(b + 10), .iov_len: 10}, (void *)(b + 30), 20};
49
50 __msan_poison(a: &b, size: 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(x: b + 10, size: 10);
57 __msan_check_mem_is_initialized(x: b + 30, size: 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(a: &b, size: 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

source code of compiler-rt/test/msan/Linux/process_vm_readv.cpp