[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

llvm-svn: 226929
This commit is contained in:
Kuba Brecka 2015-01-23 19:29:19 +00:00
parent ba2bcb0da3
commit 9a36b3e147
4 changed files with 83 additions and 6 deletions

View File

@ -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()

View File

@ -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) \

View File

@ -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;
}

View File

@ -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