[compiler-rt] r250124 - [msan] Add __msan_copy_shadow interface function.

Evgeniy Stepanov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 12 16:20:25 PDT 2015


Author: eugenis
Date: Mon Oct 12 18:20:24 2015
New Revision: 250124

URL: http://llvm.org/viewvc/llvm-project?rev=250124&view=rev
Log:
[msan] Add __msan_copy_shadow interface function.

This can be used to annotate copies of memory that are not observed by MSan.

Added:
    compiler-rt/trunk/test/msan/msan_copy_shadow.cc
Modified:
    compiler-rt/trunk/include/sanitizer/msan_interface.h
    compiler-rt/trunk/lib/msan/msan_interceptors.cc
    compiler-rt/trunk/lib/msan/msan_interface_internal.h

Modified: compiler-rt/trunk/include/sanitizer/msan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/sanitizer/msan_interface.h?rev=250124&r1=250123&r2=250124&view=diff
==============================================================================
--- compiler-rt/trunk/include/sanitizer/msan_interface.h (original)
+++ compiler-rt/trunk/include/sanitizer/msan_interface.h Mon Oct 12 18:20:24 2015
@@ -98,6 +98,12 @@ extern "C" {
   /* Deprecated. Call __sanitizer_set_death_callback instead. */
   void __msan_set_death_callback(void (*callback)(void));
 
+  /* Update shadow for the application copy of size bytes from src to dst.
+     Src and dst are application addresses. This function does not copy the
+     actual application memory, it only updates shadow and origin for such
+     copy. Source and destination regions can overlap. */
+  void __msan_copy_shadow(const volatile void *dst, const volatile void *src,
+                          size_t size);
 #ifdef __cplusplus
 }  // extern "C"
 #endif

Modified: compiler-rt/trunk/lib/msan/msan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_interceptors.cc?rev=250124&r1=250123&r2=250124&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Mon Oct 12 18:20:24 2015
@@ -1011,6 +1011,11 @@ void __msan_allocated_memory(const void
   }
 }
 
+void __msan_copy_shadow(void *dest, const void *src, uptr n) {
+  GET_STORE_STACK_TRACE;
+  MoveShadowAndOrigin(dest, src, n, &stack);
+}
+
 void __sanitizer_dtor_callback(const void *data, uptr size) {
   GET_MALLOC_STACK_TRACE;
   if (flags()->poison_in_dtor) {

Modified: compiler-rt/trunk/lib/msan/msan_interface_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_interface_internal.h?rev=250124&r1=250123&r2=250124&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interface_internal.h (original)
+++ compiler-rt/trunk/lib/msan/msan_interface_internal.h Mon Oct 12 18:20:24 2015
@@ -161,6 +161,9 @@ void __sanitizer_unaligned_store64(uu64
 
 SANITIZER_INTERFACE_ATTRIBUTE
 void __msan_set_death_callback(void (*callback)(void));
+
+SANITIZER_INTERFACE_ATTRIBUTE
+void __msan_copy_shadow(void *dst, const void *src, uptr size);
 }  // extern "C"
 
 #endif  // MSAN_INTERFACE_INTERNAL_H

Added: compiler-rt/trunk/test/msan/msan_copy_shadow.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/msan/msan_copy_shadow.cc?rev=250124&view=auto
==============================================================================
--- compiler-rt/trunk/test/msan/msan_copy_shadow.cc (added)
+++ compiler-rt/trunk/test/msan/msan_copy_shadow.cc Mon Oct 12 18:20:24 2015
@@ -0,0 +1,34 @@
+// Test that __msan_copy_shadow copies shadow, updates origin and does not touch
+// the application memory.
+// RUN: %clangxx_msan -fsanitize-memory-track-origins=0 -O0 %s -o %t && not %run %t 2>&1
+// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <string.h>
+#include <sanitizer/msan_interface.h>
+
+int main() {
+  char *a = new char[4];
+  char *b = new char[4];
+  a[1] = 1;
+  a[3] = 2;
+  memset(b, 42, 4);
+
+  // Test that __msan_copy_shadow does not touch the contents of b[].
+  __msan_copy_shadow(b, a, 4);
+  __msan_unpoison(b, 4);
+  assert(b[0] == 42 && b[1] == 42 && b[2] == 42 && b[3] == 42);
+
+  // Test that __msan_copy_shadow correctly updates shadow and origin of b[].
+  __msan_copy_shadow(b, a, 4);
+  assert(__msan_test_shadow(b, 4) == 0);
+  assert(__msan_test_shadow(b + 1, 3) == 1);
+  assert(__msan_test_shadow(b + 3, 1) == -1);
+  __msan_check_mem_is_initialized(b, 4);
+  // CHECK: use-of-uninitialized-value
+  // CHECK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-2]]
+  // CHECK: Uninitialized value was stored to memory at
+  // CHECK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-8]]
+  // CHECK: Uninitialized value was created by a heap allocation
+  // CHECK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-22]]
+}




More information about the llvm-commits mailing list