[compiler-rt] r317930 - sanitizer_common: Try looking up symbols with RTLD_DEFAULT if RTLD_NEXT does not work.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 10 14:09:38 PST 2017


Author: pcc
Date: Fri Nov 10 14:09:37 2017
New Revision: 317930

URL: http://llvm.org/viewvc/llvm-project?rev=317930&view=rev
Log:
sanitizer_common: Try looking up symbols with RTLD_DEFAULT if RTLD_NEXT does not work.

If the lookup using RTLD_NEXT failed, the sanitizer runtime library
is later in the library search order than the DSO that we are trying
to intercept, which means that we cannot intercept this function. We
still want the address of the real definition, though, so look it up
using RTLD_DEFAULT.

Differential Revision: https://reviews.llvm.org/D39779

Added:
    compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/
    compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/no-interception-dso.c
    compiler-rt/trunk/test/ubsan/TestCases/Misc/no-interception.cpp
Modified:
    compiler-rt/trunk/lib/interception/interception_linux.cc
    compiler-rt/trunk/test/ubsan/lit.common.cfg

Modified: compiler-rt/trunk/lib/interception/interception_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/interception/interception_linux.cc?rev=317930&r1=317929&r2=317930&view=diff
==============================================================================
--- compiler-rt/trunk/lib/interception/interception_linux.cc (original)
+++ compiler-rt/trunk/lib/interception/interception_linux.cc Fri Nov 10 14:09:37 2017
@@ -29,6 +29,14 @@ bool GetRealFunctionAddress(const char *
   if (internal_strcmp(func_name, "sigaction") == 0) func_name = "__sigaction14";
 #endif
   *func_addr = (uptr)dlsym(RTLD_NEXT, func_name);
+  if (!*func_addr) {
+    // If the lookup using RTLD_NEXT failed, the sanitizer runtime library is
+    // later in the library search order than the DSO that we are trying to
+    // intercept, which means that we cannot intercept this function. We still
+    // want the address of the real definition, though, so look it up using
+    // RTLD_DEFAULT.
+    *func_addr = (uptr)dlsym(RTLD_DEFAULT, func_name);
+  }
   return real == wrapper;
 }
 

Added: compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/no-interception-dso.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/no-interception-dso.c?rev=317930&view=auto
==============================================================================
--- compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/no-interception-dso.c (added)
+++ compiler-rt/trunk/test/ubsan/TestCases/Misc/Inputs/no-interception-dso.c Fri Nov 10 14:09:37 2017
@@ -0,0 +1,3 @@
+int dso_function(int i) {
+  return i + 1;
+}

Added: compiler-rt/trunk/test/ubsan/TestCases/Misc/no-interception.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/Misc/no-interception.cpp?rev=317930&view=auto
==============================================================================
--- compiler-rt/trunk/test/ubsan/TestCases/Misc/no-interception.cpp (added)
+++ compiler-rt/trunk/test/ubsan/TestCases/Misc/no-interception.cpp Fri Nov 10 14:09:37 2017
@@ -0,0 +1,20 @@
+// REQUIRES: android
+
+// Tests that ubsan can detect errors on Android if libc appears before the
+// runtime in the library search order, which means that we cannot intercept
+// symbols.
+
+// RUN: %clangxx %p/Inputs/no-interception-dso.c -fsanitize=undefined -fPIC -shared -o %dynamiclib %ld_flags_rpath_so
+
+// Make sure that libc is first in DT_NEEDED.
+// RUN: %clangxx %s -lc -o %t %ld_flags_rpath_exe
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <limits.h>
+
+int dso_function(int);
+
+int main(int argc, char **argv) {
+  // CHECK: signed integer overflow
+  dso_function(INT_MAX);
+}

Modified: compiler-rt/trunk/test/ubsan/lit.common.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/lit.common.cfg?rev=317930&r1=317929&r2=317930&view=diff
==============================================================================
--- compiler-rt/trunk/test/ubsan/lit.common.cfg (original)
+++ compiler-rt/trunk/test/ubsan/lit.common.cfg Fri Nov 10 14:09:37 2017
@@ -74,3 +74,5 @@ if config.host_os not in ['Linux', 'Darw
   config.unsupported = True
 
 config.available_features.add('arch=' + config.target_arch)
+
+config.excludes = ['Inputs']




More information about the llvm-commits mailing list