[compiler-rt] r226929 - [compiler-rt] Ensure AsanInitFromRtl is called from a static initializer on OS X by using ASAN_DYNAMIC=1

Kuba Brecka kuba.brecka at gmail.com
Fri Jan 23 11:29:19 PST 2015


Author: kuba.brecka
Date: Fri Jan 23 13:29:19 2015
New Revision: 226929

URL: http://llvm.org/viewvc/llvm-project?rev=226929&view=rev
Log:
[compiler-rt] Ensure AsanInitFromRtl is called from a static initializer on OS X by using ASAN_DYNAMIC=1

The idea is to ensure that the ASan runtime gets initialized early (i.e.
before other initializers/constructors) even when DYLD_INSERT_LIBRARIES
is not used. In that case, the interceptors are not installed (on OS X,
DYLD_INSERT_LIBRARIES is required for interceptors to work), and therefore
ASan gets currently initialized quite late -- from the main executable's
module initializer. The following issues are a consequence of this:

  https://code.google.com/p/address-sanitizer/issues/detail?id=363
  https://code.google.com/p/address-sanitizer/issues/detail?id=357

Both of them are fixed with this patch.

Reviewed at http://reviews.llvm.org/D7117


Added:
    compiler-rt/trunk/test/asan/TestCases/Darwin/linked-only.cc
    compiler-rt/trunk/test/asan/TestCases/Darwin/mixing-global-constructors.cc
Modified:
    compiler-rt/trunk/lib/asan/CMakeLists.txt
    compiler-rt/trunk/make/platform/clang_darwin.mk

Modified: compiler-rt/trunk/lib/asan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/CMakeLists.txt?rev=226929&r1=226928&r2=226929&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/asan/CMakeLists.txt Fri Jan 23 13:29:19 2015
@@ -65,8 +65,8 @@ if(APPLE)
     add_compiler_rt_darwin_object_library(RTAsan ${os}
       ARCH ${ASAN_SUPPORTED_ARCH}
       SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
-      CFLAGS ${ASAN_CFLAGS}
-      DEFS ${ASAN_COMMON_DEFINITIONS})
+      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
+      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
   endforeach()
 else()
   foreach(arch ${ASAN_SUPPORTED_ARCH})
@@ -96,8 +96,8 @@ if(APPLE)
               $<TARGET_OBJECTS:RTInterception.${os}>
               $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
               $<TARGET_OBJECTS:RTLSanCommon.${os}>
-      CFLAGS ${ASAN_CFLAGS}
-      DEFS ${ASAN_COMMON_DEFINITIONS})
+      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
+      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
     add_dependencies(asan clang_rt.asan_${os}_dynamic)
   endforeach()
 else()

Modified: compiler-rt/trunk/make/platform/clang_darwin.mk
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/make/platform/clang_darwin.mk?rev=226929&r1=226928&r2=226929&view=diff
==============================================================================
--- compiler-rt/trunk/make/platform/clang_darwin.mk (original)
+++ compiler-rt/trunk/make/platform/clang_darwin.mk Fri Jan 23 13:29:19 2015
@@ -173,14 +173,16 @@ CFLAGS.asan_osx_dynamic := \
 	-isysroot $(OSX_SDK) \
 	-fno-builtin \
 	-gline-tables-only \
-	-DMAC_INTERPOSE_FUNCTIONS=1
+	-DMAC_INTERPOSE_FUNCTIONS=1 \
+	-DASAN_DYNAMIC=1
 
 CFLAGS.asan_iossim_dynamic := \
 	$(CFLAGS) -mios-simulator-version-min=7.0 \
         -isysroot $(IOSSIM_SDK) \
         -fno-builtin \
 	-gline-tables-only \
-	-DMAC_INTERPOSE_FUNCTIONS=1
+	-DMAC_INTERPOSE_FUNCTIONS=1 \
+	-DASAN_DYNAMIC=1
 
 CFLAGS.ubsan_osx := $(CFLAGS) -mmacosx-version-min=10.6 \
 	-isysroot $(OSX_SDK) \

Added: compiler-rt/trunk/test/asan/TestCases/Darwin/linked-only.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Darwin/linked-only.cc?rev=226929&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Darwin/linked-only.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Darwin/linked-only.cc Fri Jan 23 13:29:19 2015
@@ -0,0 +1,33 @@
+// Main executable is uninstrumented, but linked to ASan runtime.
+// Regression test for https://code.google.com/p/address-sanitizer/issues/detail?id=357.
+
+// RUN: %clangxx -g -O0 %s -c -o %t.o
+// RUN: %clangxx_asan -g -O0 %t.o -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sanitizer/asan_interface.h"
+
+void test_shadow(char *p, size_t size) {
+  fprintf(stderr, "p = %p\n", p);
+  char *q = (char *)__asan_region_is_poisoned(p, size);
+  fprintf(stderr, "=%zd=\n", q ? q - p : -1);
+}
+
+int main(int argc, char *argv[]) {
+  char *p = (char *)malloc(10000);
+  test_shadow(p, 100);
+  free(p);
+  // CHECK: =-1=
+
+  test_shadow((char *)&main, 1);
+  // CHECK: =-1=
+
+  test_shadow((char *)&p, 1);
+  // CHECK: =-1=
+
+  return 0;
+}

Added: compiler-rt/trunk/test/asan/TestCases/Darwin/mixing-global-constructors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Darwin/mixing-global-constructors.cc?rev=226929&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Darwin/mixing-global-constructors.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Darwin/mixing-global-constructors.cc Fri Jan 23 13:29:19 2015
@@ -0,0 +1,42 @@
+// A global constructor from a non-instrumented part calls a function
+// in an instrumented part.
+// Regression test for https://code.google.com/p/address-sanitizer/issues/detail?id=363.
+
+// RUN: %clangxx      -DINSTRUMENTED_PART=0 -c %s -o %t-uninst.o
+// RUN: %clangxx_asan -DINSTRUMENTED_PART=1 -c %s -o %t-inst.o
+// RUN: %clangxx_asan %t-uninst.o %t-inst.o -o %t
+
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void func(char *ptr);
+
+#if INSTRUMENTED_PART == 1
+
+void func(char *ptr) {
+  *ptr = 'X';
+}
+
+#else // INSTRUMENTED_PART == 1
+
+struct C1 {
+  C1() {
+    printf("Hello ");
+    char buffer[10] = "world";
+    func(buffer);
+    printf("%s\n", buffer);
+  }
+};
+
+C1 *obj = new C1();
+
+int main(int argc, const char *argv[]) {
+  return 0;
+}
+
+#endif // INSTRUMENTED_PART == 1
+
+// CHECK: Hello Xorld





More information about the llvm-commits mailing list