Skip to content

Commit ceea546

Browse files
committedNov 29, 2017
[sanitizer] Add 'strip_env' flag to enable/disable removing sanitizer dylib from DYLD_INSERT_LIBRARIES
On macOS, we usually don't require launching the target with DYLD_INSERT_LIBRARIES anymore. However, it is still necessary when running a target that is not instrumented (and e.g. dlopen's an instrument library later). In any case, ASan and TSan currently remove themselves from the DYLD_INSERT_LIBRARIES environment variable to avoid passing it onto children. This works well e.g. when instrumenting a shell. A problem arises when the target is a non-instrumented shim (e.g. "xcrun") that either re-execs or launches a child that is supposed to get DYLD_INSERT_LIBRARIES propagated. To support this mode, this patch introduces 'strip_env' flag that can be used to keep DYLD_INSERT_LIBRARIES untouched. Differential Revision: https://reviews.llvm.org/D39991 llvm-svn: 319365
1 parent 7383b8e commit ceea546

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed
 

‎compiler-rt/lib/sanitizer_common/sanitizer_flags.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ COMMON_FLAG(
6262
COMMON_FLAG(
6363
int, verbosity, 0,
6464
"Verbosity level (0 - silent, 1 - a bit of output, 2+ - more output).")
65+
COMMON_FLAG(bool, strip_env, 1,
66+
"Whether to remove the sanitizer from DYLD_INSERT_LIBRARIES to "
67+
"avoid passing it to children. Default is true.")
6568
COMMON_FLAG(bool, detect_leaks, !SANITIZER_MAC, "Enable memory leak detection.")
6669
COMMON_FLAG(
6770
bool, leak_check_at_exit, true,

‎compiler-rt/lib/sanitizer_common/sanitizer_mac.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ void MaybeReexec() {
741741
if (!lib_is_in_env)
742742
return;
743743

744+
if (!common_flags()->strip_env)
745+
return;
746+
744747
// DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
745748
// the dylib from the environment variable, because interceptors are installed
746749
// and we don't want our children to inherit the variable.

‎compiler-rt/test/asan/TestCases/Darwin/dyld_insert_libraries_remove.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
// RUN: DYLD_INSERT_LIBRARIES=libclang_rt.asan_osx_dynamic.dylib:dummy-so.dylib \
2121
// RUN: %run ./a.out 2>&1 ) | FileCheck %s || exit 1
2222

23+
// RUN: ( cd %t && \
24+
// RUN: %env_asan_opts=strip_env=0 \
25+
// RUN: DYLD_INSERT_LIBRARIES=libclang_rt.asan_osx_dynamic.dylib:dummy-so.dylib \
26+
// RUN: %run ./a.out 2>&1 ) | FileCheck %s --check-prefix=CHECK-KEEP || exit 1
27+
2328
// RUN: ( cd %t && \
2429
// RUN: DYLD_INSERT_LIBRARIES=%t/libclang_rt.asan_osx_dynamic.dylib:dummy-so.dylib \
2530
// RUN: %run ./a.out 2>&1 ) | FileCheck %s || exit 1
@@ -32,6 +37,7 @@ int main() {
3237
const char kEnvName[] = "DYLD_INSERT_LIBRARIES";
3338
printf("%s=%s\n", kEnvName, getenv(kEnvName));
3439
// CHECK: {{DYLD_INSERT_LIBRARIES=dummy-so.dylib}}
40+
// CHECK-KEEP: {{DYLD_INSERT_LIBRARIES=libclang_rt.asan_osx_dynamic.dylib:dummy-so.dylib}}
3541
return 0;
3642
}
3743
#else // SHARED_LIB

0 commit comments

Comments
 (0)
Please sign in to comment.