Skip to content

[TSan] Support initialize/finalize hooks in dynamic libraries #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler-rt/include/sanitizer/tsan_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void __tsan_external_write(void *addr, void *caller_pc, void *tag);
// and freed by __tsan_destroy_fiber.
// - TSAN context of current fiber or thread can be obtained
// by calling __tsan_get_current_fiber.
// - __tsan_switch_to_fiber should be called immediatly before switch
// - __tsan_switch_to_fiber should be called immediately before switch
// to fiber, such as call of swapcontext.
// - Fiber name can be set by __tsan_set_fiber_name.
void *__tsan_get_current_fiber(void);
Expand All @@ -154,6 +154,15 @@ void __tsan_set_fiber_name(void *fiber, const char *name);
// Do not establish a happens-before relation between fibers
static const unsigned __tsan_switch_to_fiber_no_sync = 1 << 0;

// User-provided callback invoked on TSan initialization.
void __tsan_on_initialize();

// User-provided callback invoked on TSan shutdown.
// `failed` - Nonzero if TSan did detect issues, zero otherwise.
// Return `0` if TSan should exit as if no issues were detected. Return nonzero
// if TSan should exit as if issues were detected.
int __tsan_on_finalize(int failed);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
7 changes: 7 additions & 0 deletions compiler-rt/lib/tsan/rtl/tsan_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ void __tsan_go_atomic32_compare_exchange(ThreadState *thr, uptr cpc, uptr pc,
SANITIZER_INTERFACE_ATTRIBUTE
void __tsan_go_atomic64_compare_exchange(ThreadState *thr, uptr cpc, uptr pc,
u8 *a);

SANITIZER_INTERFACE_ATTRIBUTE
void __tsan_on_initialize();

SANITIZER_INTERFACE_ATTRIBUTE
int __tsan_on_finalize(int failed);

} // extern "C"

} // namespace __tsan
Expand Down
14 changes: 13 additions & 1 deletion compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_symbolizer.h"
#include "tsan_defs.h"
#include "tsan_interface.h"
#include "tsan_platform.h"
#include "tsan_rtl.h"
#include "tsan_mman.h"
Expand Down Expand Up @@ -56,12 +57,23 @@ Context *ctx;
bool OnFinalize(bool failed);
void OnInitialize();
#else
#include <dlfcn.h>
SANITIZER_WEAK_CXX_DEFAULT_IMPL
bool OnFinalize(bool failed) {
#if !SANITIZER_GO
if (auto *ptr = dlsym(RTLD_DEFAULT, "__tsan_on_finalize"))
return reinterpret_cast<decltype(&__tsan_on_finalize)>(ptr)(failed);
#endif
return failed;
}
SANITIZER_WEAK_CXX_DEFAULT_IMPL
void OnInitialize() {}
void OnInitialize() {
#if !SANITIZER_GO
if (auto *ptr = dlsym(RTLD_DEFAULT, "__tsan_on_initialize")) {
return reinterpret_cast<decltype(&__tsan_on_initialize)>(ptr)();
}
#endif
}
#endif

static char thread_registry_placeholder[sizeof(ThreadRegistry)];
Expand Down
33 changes: 33 additions & 0 deletions compiler-rt/test/tsan/on_initialize_finalize_hooks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %clang_tsan -O1 %s -o %t.lib -fno-sanitize=thread -shared -fPIC -DBUILD_LIB=1
// RUN: %clang_tsan -O1 %s %t.lib -o %t
// RUN: %run %t | FileCheck %s

// Test that initialization/finalization hooks are called, even when they are
// not defined in the main executable, but by another another library that
// doesn't directly link against the TSan runtime.

#include <stdio.h>

#if BUILD_LIB

extern "C" void __tsan_on_initialize() {
printf("__tsan_on_initialize()\n");
}

extern "C" int __tsan_on_finalize(int failed) {
printf("__tsan_on_finalize()\n");
return failed;
}

#else // BUILD_LIB

int main() {
printf("main()\n");
return 0;
}

#endif // BUILD_LIB

// CHECK: __tsan_on_initialize()
// CHECK: main()
// CHECK: __tsan_on_finalize()