Skip to content

Commit 4bdac08

Browse files
authored
[rtsan] Warn if instrumented rtsan library opened via dlopen and interceptors are not working (#119029)
1 parent 94c6dd6 commit 4bdac08

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
8383

8484
SanitizerToolName = "RealtimeSanitizer";
8585
InitializeFlags();
86+
87+
InitializePlatformEarly();
88+
8689
InitializeInterceptors();
8790

8891
InitializeSuppressions();

compiler-rt/lib/rtsan/tests/rtsan_test_main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ extern "C" const char *__rtsan_default_options() {
1919
// and make sure we do not overwhelm the syslog while testing. Also, let's
2020
// turn symbolization off to speed up testing, especially when not running
2121
// with llvm-symbolizer but with atos.
22-
return "symbolize=false:abort_on_error=0:log_to_syslog=0";
22+
return "symbolize=false:"
23+
"abort_on_error=0:"
24+
"log_to_syslog=0:"
25+
"verify_interceptors=0:"; // some of our tests don't need interceptors
2326
#else
2427
// Let's turn symbolization off to speed up testing (more than 3 times speedup
2528
// observed).

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,9 @@ static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
972972
LowLevelAllocator allocator_for_env;
973973

974974
static bool ShouldCheckInterceptors() {
975-
// Restrict "interceptors working?" check to ASan and TSan.
976-
const char *sanitizer_names[] = {"AddressSanitizer", "ThreadSanitizer"};
975+
// Restrict "interceptors working?" check
976+
const char *sanitizer_names[] = {"AddressSanitizer", "ThreadSanitizer",
977+
"RealtimeSanitizer"};
977978
size_t count = sizeof(sanitizer_names) / sizeof(sanitizer_names[0]);
978979
for (size_t i = 0; i < count; i++) {
979980
if (internal_strcmp(sanitizer_names[i], SanitizerToolName) == 0)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Checks that on OS X 10.11+ dlopen'ing a RTsanified library from a
2+
// non-instrumented program exits with a user-friendly message.
3+
4+
// REQUIRES: osx-autointerception
5+
6+
// XFAIL: ios
7+
8+
// RUN: %clangxx -fsanitize=realtime %s -o %t.so -shared -DSHARED_LIB
9+
// RUN: %clangxx %s -o %t
10+
11+
// RUN: RTSAN_DYLIB_PATH=`%clangxx -fsanitize=realtime %s -### 2>&1 \
12+
// RUN: | grep "libclang_rt.rtsan_osx_dynamic.dylib" \
13+
// RUN: | sed -e 's/.*"\(.*libclang_rt.rtsan_osx_dynamic.dylib\)".*/\1/'`
14+
15+
// Launching a non-instrumented binary that dlopen's an instrumented library should fail.
16+
// RUN: not %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL
17+
// Launching a non-instrumented binary with an explicit DYLD_INSERT_LIBRARIES should work.
18+
// RUN: DYLD_INSERT_LIBRARIES=$RTSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s
19+
20+
// Launching an instrumented binary with the DYLD_INSERT_LIBRARIES env variable has no error
21+
// RUN: %clangxx -fsanitize=realtime %s -o %t
22+
// RUN: DYLD_INSERT_LIBRARIES=$RTSAN_DYLIB_PATH %run %t %t.so 2>&1 | FileCheck %s --check-prefix=CHECK-INSTRUMENTED
23+
24+
#include <dlfcn.h>
25+
#include <stdio.h>
26+
27+
#if defined(SHARED_LIB)
28+
extern "C" void foo() { fprintf(stderr, "Hello world.\n"); }
29+
#else // defined(SHARED_LIB)
30+
int main(int argc, char *argv[]) {
31+
void *handle = dlopen(argv[1], RTLD_NOW);
32+
void (*foo)() = (void (*)())dlsym(handle, "foo");
33+
foo();
34+
}
35+
#endif // defined(SHARED_LIB)
36+
37+
// CHECK: Hello world.
38+
// CHECK-NOT: ERROR: Interceptors are not working.
39+
40+
// CHECK-FAIL-NOT: Hello world.
41+
// CHECK-FAIL: ERROR: Interceptors are not working.
42+
43+
// CHECK-INSTRUMENTED-NOT: ERROR: Interceptors are not working
44+
// CHECK-INSTRUMENTED: Hello world.

0 commit comments

Comments
 (0)