Index: lib/sanitizer_common/sanitizer_flags.inc =================================================================== --- lib/sanitizer_common/sanitizer_flags.inc +++ lib/sanitizer_common/sanitizer_flags.inc @@ -207,3 +207,5 @@ "(asan only).") COMMON_FLAG(bool, html_cov_report, false, "Generate html coverage report.") COMMON_FLAG(const char *, sancov_path, "sancov", "Sancov tool location.") +COMMON_FLAG(bool, disable_reexec, false, "Don't re-execute the program even " + " when interceptors aren't installed.") Index: lib/sanitizer_common/sanitizer_mac.cc =================================================================== --- lib/sanitizer_common/sanitizer_mac.cc +++ lib/sanitizer_common/sanitizer_mac.cc @@ -573,7 +573,7 @@ } void MaybeReexec() { - if (reexec_disabled) return; + if (reexec_disabled || common_flags()->disable_reexec) return; // Make sure the dynamic runtime library is preloaded so that the // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec @@ -627,6 +627,24 @@ CHECK("execv failed" && 0); } + // Verify that interceptors really work. We'll use dlsym to locate + // "pthread_create", if interceptors are working, it should really point to + // "wrap_pthread_create" within our own dylib. + Dl_info info_pthread_create; + void *dlopen_addr = dlsym(RTLD_DEFAULT, "pthread_create"); + CHECK(dladdr(dlopen_addr, &info_pthread_create)); + Report("info.dli_fname = %p = %s, info_pthread_create.dli_fname = %p = %s\n", + info.dli_fname, info.dli_fname, info_pthread_create.dli_fname, + info_pthread_create.dli_fname); + if (internal_strcmp(info.dli_fname, info_pthread_create.dli_fname) != 0) { + Report( + "ERROR: Interceptors are not working. This may be because %s is " + "loaded too late (e.g. via dlopen). Please launch the executable " + "with:\n%s=%s\n", + SanitizerToolName, kDyldInsertLibraries, info.dli_fname); + CHECK("interceptors not installed" && 0); + } + if (!lib_is_in_env) return; Index: lib/tsan/tests/rtl/tsan_test.cc =================================================================== --- lib/tsan/tests/rtl/tsan_test.cc +++ lib/tsan/tests/rtl/tsan_test.cc @@ -49,8 +49,10 @@ #ifdef __APPLE__ // On Darwin, turns off symbolication and crash logs to make tests faster. +// We also need "disable_reexec=1" to make sure unit tests can run even +// when interceptors are not properly installed. extern "C" const char* __tsan_default_options() { - return "symbolize=false:abort_on_error=0"; + return "symbolize=false:abort_on_error=0:disable_reexec=1"; } #endif Index: lib/tsan/tests/unit/tsan_flags_test.cc =================================================================== --- lib/tsan/tests/unit/tsan_flags_test.cc +++ lib/tsan/tests/unit/tsan_flags_test.cc @@ -132,10 +132,8 @@ EXPECT_EQ(f->die_after_fork, false); } -static const char *test_default_options; -extern "C" const char *__tsan_default_options() { - return test_default_options; -} +// Defined in tsan_unit_test_main.cc. +extern const char *test_default_options; TEST(Flags, ParseDefaultOptions) { Flags f; Index: lib/tsan/tests/unit/tsan_unit_test_main.cc =================================================================== --- lib/tsan/tests/unit/tsan_unit_test_main.cc +++ lib/tsan/tests/unit/tsan_unit_test_main.cc @@ -12,6 +12,24 @@ //===----------------------------------------------------------------------===// #include "gtest/gtest.h" +namespace __tsan { + +// Used for testing in tsan_flags_test.cc. +const char *test_default_options = nullptr; + +extern "C" const char* __tsan_default_options() { + if (test_default_options) return test_default_options; +#ifdef __APPLE__ + // On Darwin, we need "disable_reexec=1" to make sure unit tests can run even + // when interceptors are not properly installed. + return "disable_reexec=1"; +#else + return ""; +#endif +} + +} // namespace __tsan + int main(int argc, char **argv) { testing::GTEST_FLAG(death_test_style) = "threadsafe"; testing::InitGoogleTest(&argc, argv); Index: test/lit.common.cfg =================================================================== --- test/lit.common.cfg +++ test/lit.common.cfg @@ -118,6 +118,14 @@ lit.util.usePlatformSdkOnDarwin(config, lit_config) +if config.host_os == 'Darwin': + ld_cmd = subprocess.Popen(["bash", "-c", "sw_vers -productVersion | awk -F '.' '{print $1 \".\" $2}'"], stdout = subprocess.PIPE) + ld_out = ld_cmd.stdout.read().decode() + ld_cmd.wait() + osx_version = float(ld_out) + if osx_version >= 10.11: + config.available_features.add('osx-autointerception') + sancovcc_path = os.path.join(llvm_tools_dir, "sancov") if os.path.exists(sancovcc_path): config.available_features.add("has_sancovcc") Index: test/tsan/Darwin/dlopen.cc =================================================================== --- test/tsan/Darwin/dlopen.cc +++ test/tsan/Darwin/dlopen.cc @@ -0,0 +1,41 @@ +// Checks that on OS X 10.11+ (where we do not re-exec anymore, because +// interceptors work automatically), dlopen'ing a TSanified library from a +// non-instrumented program exits with a user-friendly message. + +// REQUIRES: osx-autointerception + +// RUN: %clangxx_tsan %s -o %t.so -shared -DSHARED_LIB +// RUN: %clangxx_tsan -fno-sanitize=thread %s -o %t + +// RUN: TSAN_DYLIB_PATH=`%clangxx_tsan %s -### 2>&1 \ +// RUN: | grep "libclang_rt.tsan_osx_dynamic.dylib" \ +// RUN: | sed -e 's/.*"\(.*libclang_rt.tsan_osx_dynamic.dylib\)".*/\1/'` + +// Launching a non-instrumented binary that dlopen's an instrumented library should fail. +// RUN: not %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL +// Launching a non-instrumented binary with an explicit DYLD_INSERT_LIBRARIES should work. +// RUN: DYLD_INSERT_LIBRARIES=$TSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s + +#include +#include +#include + +#if defined(SHARED_LIB) +extern "C" void foo() { + fprintf(stderr, "Hello world.\n"); +} +#else // defined(SHARED_LIB) +int main(int argc, char *argv[]) { + void *handle = dlopen(argv[1], RTLD_NOW); + fprintf(stderr, "handle = %p\n", handle); + void (*foo)() = (void (*)())dlsym(handle, "foo"); + fprintf(stderr, "foo = %p\n", foo); + foo(); +} +#endif // defined(SHARED_LIB) + +// CHECK: Hello world. +// CHECK-NOT: ERROR: Interceptors are not working. + +// CHECK-FAIL-NOT: Hello world. +// CHECK-FAIL: ERROR: Interceptors are not working.