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