[compiler-rt] r175059 - [ASan] When re-executing the process on OS X, make sure we update the existing DYLD_INSERT_LIBRARIES correctly.

Alexander Potapenko glider at google.com
Wed Feb 13 09:52:55 PST 2013


Author: glider
Date: Wed Feb 13 11:52:55 2013
New Revision: 175059

URL: http://llvm.org/viewvc/llvm-project?rev=175059&view=rev
Log:
[ASan] When re-executing the process on OS X, make sure we update the existing DYLD_INSERT_LIBRARIES correctly.
Previously ASan used to hang in an exec loop, because it failed to overwrite the env var value
(see https://code.google.com/p/address-sanitizer/issues/detail?id=159).


Added:
    compiler-rt/trunk/lib/asan/lit_tests/Darwin/reexec-insert-libraries-env.cc
    compiler-rt/trunk/lib/asan/lit_tests/SharedLibs/darwin-dummy-shared-lib-so.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_mac.cc

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=175059&r1=175058&r2=175059&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Wed Feb 13 11:52:55 2013
@@ -106,7 +106,23 @@ void MaybeReexec() {
     _NSGetExecutablePath(program_name, &buf_size);
     // Ok to use setenv() since the wrappers don't depend on the value of
     // asan_inited.
-    setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
+    if (dyld_insert_libraries) {
+      // Append the runtime dylib name to the existing value of
+      // DYLD_INSERT_LIBRARIES.
+      uptr old_env_len = internal_strlen(dyld_insert_libraries);
+      uptr fname_len = internal_strlen(info.dli_fname);
+      LowLevelAllocator allocator_for_env;
+      char *new_env =
+          (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
+      internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
+      new_env[old_env_len] = ':';
+      // Copy fname_len and add a trailing zero.
+      internal_strncpy(new_env + old_env_len + 1, info.dli_fname, fname_len + 1);
+      setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
+    } else {
+      // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
+      setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
+    }
     if (flags()->verbosity >= 1) {
       Report("exec()-ing the program with\n");
       Report("%s=%s\n", kDyldInsertLibraries, info.dli_fname);

Added: compiler-rt/trunk/lib/asan/lit_tests/Darwin/reexec-insert-libraries-env.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/Darwin/reexec-insert-libraries-env.cc?rev=175059&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/Darwin/reexec-insert-libraries-env.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/Darwin/reexec-insert-libraries-env.cc Wed Feb 13 11:52:55 2013
@@ -0,0 +1,20 @@
+#include <stdio.h>
+// Make sure ASan doesn't hang in an exec loop if DYLD_INSERT_LIBRARIES is set.
+// This is a regression test for
+// https://code.google.com/p/address-sanitizer/issues/detail?id=159
+
+// RUN: %clangxx_asan -m64 %s -o %t
+// RUN: %clangxx -m64 %p/../SharedLibs/darwin-dummy-shared-lib-so.cc \
+// RUN:     -dynamiclib -o darwin-dummy-shared-lib-so.dylib
+
+// If the test hangs, kill it after 15 seconds.
+// RUN: DYLD_INSERT_LIBRARIES=darwin-dummy-shared-lib-so.dylib \
+// RUN:     alarm 15 %t 2>&1 | FileCheck %s || exit 1
+#include <stdlib.h>
+
+int main() {
+  const char kEnvName[] = "DYLD_INSERT_LIBRARIES";
+  printf("%s=%s\n", kEnvName, getenv(kEnvName));
+  // CHECK: {{DYLD_INSERT_LIBRARIES=.*darwin-dummy-shared-lib-so.dylib.*}}
+  return 0;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/SharedLibs/darwin-dummy-shared-lib-so.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/SharedLibs/darwin-dummy-shared-lib-so.cc?rev=175059&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/SharedLibs/darwin-dummy-shared-lib-so.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/SharedLibs/darwin-dummy-shared-lib-so.cc Wed Feb 13 11:52:55 2013
@@ -0,0 +1,13 @@
+//===----------- darwin-dummy-shared-lib-so.cc ------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of AddressSanitizer, an address sanity checker.
+//
+//===----------------------------------------------------------------------===//
+void foo() {}





More information about the llvm-commits mailing list