Skip to content

Commit 71a91c1

Browse files
authored
[tsan] Use DlSymAllocator (#108920)
`DlSymAllocator` allows early allocations, when tsan is not yet initialized, e.g. from `dlsym`. All other sanitizers with interceptors already use `DlSymAllocator`. Existing `in_symbolizer()` tsan logic is very similar. However, we need to keep both as `DlSymAllocator` does not support large allocations, needed for Symolizer.
1 parent 0bbebf6 commit 71a91c1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// sanitizer_common/sanitizer_common_interceptors.inc
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "sanitizer_common/sanitizer_allocator_dlsym.h"
1516
#include "sanitizer_common/sanitizer_atomic.h"
1617
#include "sanitizer_common/sanitizer_errno.h"
1718
#include "sanitizer_common/sanitizer_glibc_version.h"
19+
#include "sanitizer_common/sanitizer_internal_defs.h"
1820
#include "sanitizer_common/sanitizer_libc.h"
1921
#include "sanitizer_common/sanitizer_linux.h"
2022
#include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
2123
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
22-
#include "sanitizer_common/sanitizer_placement_new.h"
2324
#include "sanitizer_common/sanitizer_posix.h"
2425
#include "sanitizer_common/sanitizer_stacktrace.h"
2526
#include "sanitizer_common/sanitizer_tls_get_addr.h"
@@ -252,6 +253,13 @@ SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionBegin() {}
252253
SANITIZER_WEAK_CXX_DEFAULT_IMPL void OnPotentiallyBlockingRegionEnd() {}
253254
#endif
254255

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

257265
static ThreadSignalContext *SigCtx(ThreadState *thr) {
@@ -661,6 +669,8 @@ TSAN_INTERCEPTOR(void, _longjmp, uptr *env, int val) {
661669
TSAN_INTERCEPTOR(void*, malloc, uptr size) {
662670
if (in_symbolizer())
663671
return InternalAlloc(size);
672+
if (DlsymAlloc::Use())
673+
return DlsymAlloc::Allocate(size);
664674
void *p = 0;
665675
{
666676
SCOPED_INTERCEPTOR_RAW(malloc, size);
@@ -681,6 +691,8 @@ TSAN_INTERCEPTOR(void*, __libc_memalign, uptr align, uptr sz) {
681691
TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
682692
if (in_symbolizer())
683693
return InternalCalloc(n, size);
694+
if (DlsymAlloc::Use())
695+
return DlsymAlloc::Callocate(n, size);
684696
void *p = 0;
685697
{
686698
SCOPED_INTERCEPTOR_RAW(calloc, n, size);
@@ -693,6 +705,8 @@ TSAN_INTERCEPTOR(void *, calloc, uptr n, uptr size) {
693705
TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
694706
if (in_symbolizer())
695707
return InternalRealloc(p, size);
708+
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(p))
709+
return DlsymAlloc::Realloc(p, size);
696710
if (p)
697711
invoke_free_hook(p);
698712
{
@@ -717,20 +731,24 @@ TSAN_INTERCEPTOR(void *, reallocarray, void *p, uptr n, uptr size) {
717731
}
718732

719733
TSAN_INTERCEPTOR(void, free, void *p) {
720-
if (p == 0)
734+
if (UNLIKELY(!p))
721735
return;
722736
if (in_symbolizer())
723737
return InternalFree(p);
738+
if (DlsymAlloc::PointerIsMine(p))
739+
return DlsymAlloc::Free(p);
724740
invoke_free_hook(p);
725741
SCOPED_INTERCEPTOR_RAW(free, p);
726742
user_free(thr, pc, p);
727743
}
728744

729745
TSAN_INTERCEPTOR(void, cfree, void *p) {
730-
if (p == 0)
746+
if (UNLIKELY(!p))
731747
return;
732748
if (in_symbolizer())
733749
return InternalFree(p);
750+
if (DlsymAlloc::PointerIsMine(p))
751+
return DlsymAlloc::Free(p);
734752
invoke_free_hook(p);
735753
SCOPED_INTERCEPTOR_RAW(cfree, p);
736754
user_free(thr, pc, p);

compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %clang -O0 %s -o %t && %run %t
22

3-
// FIXME: TSAN does not use DlsymAlloc.
4-
// UNSUPPORTED: tsan
53
// FIXME: investigate why this fails on macos
64
// UNSUPPORTED: darwin
75

0 commit comments

Comments
 (0)