1// Test that __msan_copy_shadow copies shadow, updates origin and does not touch
2// the application memory.
3// RUN: %clangxx_msan -fsanitize-memory-track-origins=0 -O0 %s -o %t && not %run %t 2>&1
4// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O0 %s -o %t && not %run %t 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-%short-stack %s
5
6#include <assert.h>
7#include <string.h>
8#include <sanitizer/msan_interface.h>
9
10int main() {
11 char *a = new char[4];
12 char *b = new char[4];
13 a[1] = 1;
14 a[3] = 2;
15 memset(s: b, c: 42, n: 4);
16
17 // Test that __msan_copy_shadow does not touch the contents of b[].
18 __msan_copy_shadow(dst: b, src: a, size: 4);
19 __msan_unpoison(a: b, size: 4);
20 assert(b[0] == 42 && b[1] == 42 && b[2] == 42 && b[3] == 42);
21
22 // Test that __msan_copy_shadow correctly updates shadow and origin of b[].
23 __msan_copy_shadow(dst: b, src: a, size: 4);
24 assert(__msan_test_shadow(b, 4) == 0);
25 assert(__msan_test_shadow(b + 1, 3) == 1);
26 assert(__msan_test_shadow(b + 3, 1) == -1);
27 __msan_check_mem_is_initialized(x: b, size: 4);
28 // CHECK: use-of-uninitialized-value
29 // CHECK: {{in main.*msan_copy_shadow.cpp:}}[[@LINE-2]]
30 // CHECK: Uninitialized value was stored to memory at
31 // CHECK-FULL-STACK: {{in main.*msan_copy_shadow.cpp:}}[[@LINE-8]]
32 // CHECK-SHORT-STACK: {{in __msan_copy_shadow .*msan_interceptors.cpp:}}
33 // CHECK: Uninitialized value was created by a heap allocation
34 // CHECK: {{in main.*msan_copy_shadow.cpp:}}[[@LINE-23]]
35}
36

source code of compiler-rt/test/msan/msan_copy_shadow.cpp