Skip to content

[tsan] Use DlSymAllocator #108920

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 6 commits into from
Sep 17, 2024
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
24 changes: 21 additions & 3 deletions compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// sanitizer_common/sanitizer_common_interceptors.inc
//===----------------------------------------------------------------------===//

#include "sanitizer_common/sanitizer_allocator_dlsym.h"
#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_errno.h"
#include "sanitizer_common/sanitizer_glibc_version.h"
#include "sanitizer_common/sanitizer_internal_defs.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_linux.h"
#include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_posix.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_tls_get_addr.h"
Expand Down Expand Up @@ -252,6 +253,13 @@ SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionBegin() {}
SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionEnd() {}
#endif

// FIXME: Use for `in_symbolizer()` as well. As-is we can't use
// `DlSymAllocator`, because it uses the primary allocator only. Symbolizer
// requires support of the secondary allocator for larger blocks.
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
static bool UseImpl() { return (ctx && !ctx->initialized); }
};

} // namespace __tsan

static ThreadSignalContext *SigCtx(ThreadState *thr) {
Expand Down Expand Up @@ -661,6 +669,8 @@ TSAN_INTERCEPTOR(void, _longjmp, uptr *env, int val) {
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
if (in_symbolizer())
return InternalAlloc(size);
if (DlsymAlloc::Use())
return DlsymAlloc::Allocate(size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(malloc, size);
Expand All @@ -681,6 +691,8 @@ TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
if (in_symbolizer())
return InternalCalloc(n, size);
if (DlsymAlloc::Use())
return DlsymAlloc::Callocate(n, size);
void *p = 0;
{
SCOPED_INTERCEPTOR_RAW(calloc, n, size);
Expand All @@ -693,6 +705,8 @@ TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
if (in_symbolizer())
return InternalRealloc(p, size);
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
return DlsymAlloc::Realloc(p, size);
if (p)
invoke_free_hook(p);
{
Expand All @@ -717,20 +731,24 @@ TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
}

TSAN_INTERCEPTOR(void, free, void *p) {
if (p == 0)
if (UNLIKELY(!p))
return;
if (in_symbolizer())
return InternalFree(p);
if (DlsymAlloc::PointerIsMine(p))
return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(free, p);
user_free(thr, pc, p);
}

TSAN_INTERCEPTOR(void, cfree, void *p) {
if (p == 0)
if (UNLIKELY(!p))
return;
if (in_symbolizer())
return InternalFree(p);
if (DlsymAlloc::PointerIsMine(p))
return DlsymAlloc::Free(p);
invoke_free_hook(p);
SCOPED_INTERCEPTOR_RAW(cfree, p);
user_free(thr, pc, p);
Expand Down
2 changes: 0 additions & 2 deletions compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// RUN: %clang -O0 %s -o %t && %run %t

// FIXME: TSAN does not use DlsymAlloc.
// UNSUPPORTED: tsan
// FIXME: investigate why this fails on macos
// UNSUPPORTED: darwin

Expand Down
Loading