[compiler-rt] r182996 - [msan] Handle mixed track-origins and keep-going settings (compiler-rt part).

Evgeniy Stepanov eugeni.stepanov at gmail.com
Fri May 31 05:04:08 PDT 2013


Author: eugenis
Date: Fri May 31 07:04:08 2013
New Revision: 182996

URL: http://llvm.org/viewvc/llvm-project?rev=182996&view=rev
Log:
[msan] Handle mixed track-origins and keep-going settings (compiler-rt part).

Before this change, each module defined a weak_odr global __msan_track_origins
with a value of 1 if origin tracking is enabled, 0 if disabled. If there are
modules with different values, any of them may win. If 0 wins, and there is at
least one module with 1, the program will most likely crash.

With this change, __msan_track_origins is only emitted if origin tracking is
on. Then runtime library detects if there is at least one module with origin
tracking, and enables runtime support for it.


Added:
    compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/
    compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc   (with props)
    compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h   (with props)
    compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/lit.local.cfg
    compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc   (with props)
Modified:
    compiler-rt/trunk/lib/msan/msan.cc
    compiler-rt/trunk/lib/msan/msan_interceptors.cc

Added: compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc?rev=182996&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc Fri May 31 07:04:08 2013
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+
+#include "dso-origin.h"
+
+void my_access(int *p) {
+  volatile int tmp;
+  // Force initialize-ness check.
+  if (*p)
+    tmp = 1;
+}
+
+void *my_alloc(unsigned sz) {
+  return malloc(sz);
+}

Propchange: compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin-so.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h?rev=182996&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h Fri May 31 07:04:08 2013
@@ -0,0 +1,4 @@
+extern "C" {
+void my_access(int *p);
+void *my_alloc(unsigned sz);
+}

Propchange: compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/dso-origin.h
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/lit.local.cfg?rev=182996&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/lit.local.cfg (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/SharedLibs/lit.local.cfg Fri May 31 07:04:08 2013
@@ -0,0 +1,4 @@
+# Sources in this directory are compiled as shared libraries and used by
+# tests in parent directory.
+
+config.suffixes = []

Added: compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc?rev=182996&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc Fri May 31 07:04:08 2013
@@ -0,0 +1,25 @@
+// Build a library with origin tracking and an executable w/o origin tracking.
+// Test that origin tracking is enabled at runtime.
+// RUN: %clangxx_msan -fsanitize-memory-track-origins -m64 -O0 %p/SharedLibs/dso-origin-so.cc \
+// RUN:     -fPIC -shared -o %t-so.so
+// RUN: %clangxx_msan -m64 -O0 %s %t-so.so -o %t && %t 2>&1 | FileCheck %s
+
+#include <stdlib.h>
+
+#include "SharedLibs/dso-origin.h"
+
+int main(int argc, char **argv) {
+  int *x = (int *)my_alloc(sizeof(int));
+  my_access(x);
+  delete x;
+
+  // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
+  // CHECK: {{#0 0x.* in my_access .*dso-origin-so.cc:}}
+  // CHECK: {{#1 0x.* in main .*dso-origin.cc:}}[[@LINE-5]]
+  // CHECK: Uninitialized value was created by a heap allocation
+  // CHECK: {{#0 0x.* in .*malloc}}
+  // CHECK: {{#1 0x.* in my_alloc .*dso-origin-so.cc:}}
+  // CHECK: {{#2 0x.* in main .*dso-origin.cc:}}[[@LINE-10]]
+  // CHECK: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*dso-origin-so.cc:.* my_access}}
+  return 0;
+}

Propchange: compiler-rt/trunk/lib/msan/lit_tests/dso-origin.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: compiler-rt/trunk/lib/msan/msan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan.cc?rev=182996&r1=182995&r2=182996&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan.cc (original)
+++ compiler-rt/trunk/lib/msan/msan.cc Fri May 31 07:04:08 2013
@@ -61,9 +61,11 @@ static THREADLOCAL struct {
 static THREADLOCAL bool is_in_symbolizer;
 static THREADLOCAL bool is_in_loader;
 
+SANITIZER_WEAK_ATTRIBUTE
 extern "C" const int __msan_track_origins;
+
 int __msan_get_track_origins() {
-  return __msan_track_origins;
+  return &__msan_track_origins ? __msan_track_origins : 0;
 }
 
 namespace __msan {
@@ -204,10 +206,10 @@ void PrintWarningWithOrigin(uptr pc, upt
                 common_flags()->fast_unwind_on_fatal);
 
   u32 report_origin =
-    (__msan_track_origins && OriginIsValid(origin)) ? origin : 0;
+    (__msan_get_track_origins() && OriginIsValid(origin)) ? origin : 0;
   ReportUMR(&stack, report_origin);
 
-  if (__msan_track_origins && !OriginIsValid(origin)) {
+  if (__msan_get_track_origins() && !OriginIsValid(origin)) {
     Printf("  ORIGIN: invalid (%x). Might be a bug in MemorySanitizer, "
            "please report to MemorySanitizer developers.\n",
            origin);
@@ -262,10 +264,10 @@ void __msan_init() {
 
   msan_running_under_dr = IsRunningUnderDr();
   __msan_clear_on_return();
-  if (__msan_track_origins && flags()->verbosity > 0)
+  if (__msan_get_track_origins() && flags()->verbosity > 0)
     Printf("msan_track_origins\n");
-  if (!InitShadow(/* prot1 */false, /* prot2 */true, /* map_shadow */true,
-                  __msan_track_origins)) {
+  if (!InitShadow(/* prot1 */ false, /* prot2 */ true, /* map_shadow */ true,
+                  __msan_get_track_origins())) {
     // FIXME: prot1 = false is only required when running under DR.
     Printf("FATAL: MemorySanitizer can not mmap the shadow memory.\n");
     Printf("FATAL: Make sure to compile with -fPIE and to link with -pie.\n");
@@ -316,7 +318,7 @@ void __msan_print_shadow(const void *x,
     Printf("%x%x ", s[i] >> 4, s[i] & 0xf);
   }
   Printf("\n");
-  if (__msan_track_origins) {
+  if (__msan_get_track_origins()) {
     for (uptr i = 0; i < size / 4; i++) {
       Printf(" o: %x ", o[i]);
     }
@@ -396,7 +398,7 @@ void __msan_set_origin(const void *a, up
   // Origin mapping is 4 bytes per 4 bytes of application memory.
   // Here we extend the range such that its left and right bounds are both
   // 4 byte aligned.
-  if (!__msan_track_origins) return;
+  if (!__msan_get_track_origins()) return;
   uptr x = MEM_TO_ORIGIN((uptr)a);
   uptr beg = x & ~3UL;  // align down.
   uptr end = (x + size + 3) & ~3UL;  // align up.
@@ -447,7 +449,7 @@ const char *__msan_get_origin_descr_if_s
 
 
 u32 __msan_get_origin(const void *a) {
-  if (!__msan_track_origins) return 0;
+  if (!__msan_get_track_origins()) return 0;
   uptr x = (uptr)a;
   uptr aligned = x & ~3ULL;
   uptr origin_ptr = MEM_TO_ORIGIN(aligned);

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=182996&r1=182995&r2=182996&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Fri May 31 07:04:08 2013
@@ -28,8 +28,13 @@
 // ACHTUNG! No other system header includes in this file.
 // Ideally, we should get rid of stdarg.h as well.
 
+SANITIZER_WEAK_ATTRIBUTE
 extern "C" const int __msan_keep_going;
 
+int __msan_get_keep_going() {
+  return &__msan_keep_going ? __msan_keep_going : 0;
+}
+
 using namespace __msan;
 
 // True if this is a nested interceptor.
@@ -63,7 +68,7 @@ bool IsInInterceptorScope() {
              offset, x, n);                                                   \
       __msan::PrintWarningWithOrigin(pc, bp,                                  \
                                      __msan_get_origin((char *) x + offset)); \
-      if (!__msan_keep_going) {                                               \
+      if (!__msan_get_keep_going()) {                                         \
         Printf("Exiting\n");                                                  \
         Die();                                                                \
       }                                                                       \





More information about the llvm-commits mailing list