From 80eea015ba4c1cf1182067579cd385dfdcb2e118 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 10 Aug 2024 11:35:14 -0700 Subject: [PATCH] [dfsan] Use namespace qualifier and internalize accidentally exported functions. NFC --- compiler-rt/lib/dfsan/dfsan.cpp | 30 ++++----- compiler-rt/lib/dfsan/dfsan_allocator.cpp | 39 +++++------ .../lib/dfsan/dfsan_chained_origin_depot.cpp | 14 ++-- compiler-rt/lib/dfsan/dfsan_custom.cpp | 2 + compiler-rt/lib/dfsan/dfsan_interceptors.cpp | 65 +++++++++---------- compiler-rt/lib/dfsan/dfsan_thread.cpp | 12 ++-- 6 files changed, 78 insertions(+), 84 deletions(-) diff --git a/compiler-rt/lib/dfsan/dfsan.cpp b/compiler-rt/lib/dfsan/dfsan.cpp index 360288cb88f344..3bf86c7ec8dd1b 100644 --- a/compiler-rt/lib/dfsan/dfsan.cpp +++ b/compiler-rt/lib/dfsan/dfsan.cpp @@ -779,15 +779,15 @@ dfsan_get_labels_in_signal_reaches_function() { return __dfsan::labels_in_signal_reaches_function; } +namespace { class Decorator : public __sanitizer::SanitizerCommonDecorator { public: Decorator() : SanitizerCommonDecorator() {} const char *Origin() const { return Magenta(); } }; +} // namespace -namespace { - -void PrintNoOriginTrackingWarning() { +static void PrintNoOriginTrackingWarning() { Decorator d; Printf( " %sDFSan: origin tracking is not enabled. Did you specify the " @@ -795,13 +795,13 @@ void PrintNoOriginTrackingWarning() { d.Warning(), d.Default()); } -void PrintNoTaintWarning(const void *address) { +static void PrintNoTaintWarning(const void *address) { Decorator d; Printf(" %sDFSan: no tainted value at %x%s\n", d.Warning(), address, d.Default()); } -void PrintInvalidOriginWarning(dfsan_label label, const void *address) { +static void PrintInvalidOriginWarning(dfsan_label label, const void *address) { Decorator d; Printf( " %sTaint value 0x%x (at %p) has invalid origin tracking. This can " @@ -809,7 +809,7 @@ void PrintInvalidOriginWarning(dfsan_label label, const void *address) { d.Warning(), label, address, d.Default()); } -void PrintInvalidOriginIdWarning(dfsan_origin origin) { +static void PrintInvalidOriginIdWarning(dfsan_origin origin) { Decorator d; Printf( " %sOrigin Id %d has invalid origin tracking. This can " @@ -817,7 +817,7 @@ void PrintInvalidOriginIdWarning(dfsan_origin origin) { d.Warning(), origin, d.Default()); } -bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) { +static bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) { Decorator d; bool found = false; @@ -841,8 +841,8 @@ bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) { return found; } -bool PrintOriginTraceToStr(const void *addr, const char *description, - InternalScopedString *out) { +static bool PrintOriginTraceToStr(const void *addr, const char *description, + InternalScopedString *out) { CHECK(out); CHECK(dfsan_get_track_origins()); Decorator d; @@ -860,8 +860,6 @@ bool PrintOriginTraceToStr(const void *addr, const char *description, return PrintOriginTraceFramesToStr(o, out); } -} // namespace - extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_print_origin_trace( const void *addr, const char *description) { if (!dfsan_get_track_origins()) { @@ -1167,7 +1165,7 @@ static bool ProtectMemoryRange(uptr beg, uptr size, const char *name) { // TODO: InitShadow is based on msan. // Consider refactoring these into a shared implementation. -bool InitShadow(bool init_origins, bool dry_run) { +static bool InitShadow(bool init_origins, bool dry_run) { // Let user know mapping parameters first. VPrintf(1, "dfsan_init %p\n", (void *)&__dfsan::dfsan_init); for (unsigned i = 0; i < kMemoryLayoutSize; ++i) @@ -1227,7 +1225,7 @@ bool InitShadow(bool init_origins, bool dry_run) { return true; } -bool InitShadowWithReExec(bool init_origins) { +static bool InitShadowWithReExec(bool init_origins) { // Start with dry run: check layout is ok, but don't print warnings because // warning messages will cause tests to fail (even if we successfully re-exec // after the warning). @@ -1290,11 +1288,7 @@ static void DFsanInit(int argc, char **argv, char **envp) { dfsan_inited = true; } -namespace __dfsan { - -void dfsan_init() { DFsanInit(0, nullptr, nullptr); } - -} // namespace __dfsan +void __dfsan::dfsan_init() { DFsanInit(0, nullptr, nullptr); } #if SANITIZER_CAN_USE_PREINIT_ARRAY __attribute__((section(".preinit_array"), diff --git a/compiler-rt/lib/dfsan/dfsan_allocator.cpp b/compiler-rt/lib/dfsan/dfsan_allocator.cpp index 81ea91580a989d..160b1a64d8f6f1 100644 --- a/compiler-rt/lib/dfsan/dfsan_allocator.cpp +++ b/compiler-rt/lib/dfsan/dfsan_allocator.cpp @@ -22,7 +22,9 @@ #include "sanitizer_common/sanitizer_allocator_report.h" #include "sanitizer_common/sanitizer_errno.h" -namespace __dfsan { +using namespace __dfsan; + +namespace { struct Metadata { uptr requested_size; @@ -67,8 +69,9 @@ static AllocatorCache fallback_allocator_cache; static StaticSpinMutex fallback_mutex; static uptr max_malloc_size; +} // namespace -void dfsan_allocator_init() { +void __dfsan::dfsan_allocator_init() { SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null); allocator.Init(common_flags()->allocator_release_to_os_interval_ms); if (common_flags()->max_allocation_size_mb) @@ -78,7 +81,7 @@ void dfsan_allocator_init() { max_malloc_size = kMaxAllowedMallocSize; } -AllocatorCache *GetAllocatorCache(DFsanThreadLocalMallocStorage *ms) { +static AllocatorCache *GetAllocatorCache(DFsanThreadLocalMallocStorage *ms) { CHECK(ms); CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache)); return reinterpret_cast(ms->allocator_cache); @@ -133,7 +136,7 @@ static void *DFsanAllocate(uptr size, uptr alignment, bool zeroise) { return allocated; } -void dfsan_deallocate(void *p) { +void __dfsan::dfsan_deallocate(void *p) { CHECK(p); Metadata *meta = reinterpret_cast(allocator.GetMetaData(p)); uptr size = meta->requested_size; @@ -151,7 +154,7 @@ void dfsan_deallocate(void *p) { } } -void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) { +static void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) { Metadata *meta = reinterpret_cast(allocator.GetMetaData(old_p)); uptr old_size = meta->requested_size; uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p); @@ -171,7 +174,7 @@ void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) { return new_p; } -void *DFsanCalloc(uptr nmemb, uptr size) { +static void *DFsanCalloc(uptr nmemb, uptr size) { if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) { if (AllocatorMayReturnNull()) return nullptr; @@ -209,15 +212,15 @@ static uptr AllocationSizeFast(const void *p) { return reinterpret_cast(allocator.GetMetaData(p))->requested_size; } -void *dfsan_malloc(uptr size) { +void *__dfsan::dfsan_malloc(uptr size) { return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/)); } -void *dfsan_calloc(uptr nmemb, uptr size) { +void *__dfsan::dfsan_calloc(uptr nmemb, uptr size) { return SetErrnoOnNull(DFsanCalloc(nmemb, size)); } -void *dfsan_realloc(void *ptr, uptr size) { +void *__dfsan::dfsan_realloc(void *ptr, uptr size) { if (!ptr) return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/)); if (size == 0) { @@ -227,7 +230,7 @@ void *dfsan_realloc(void *ptr, uptr size) { return SetErrnoOnNull(DFsanReallocate(ptr, size, sizeof(u64))); } -void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) { +void *__dfsan::dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) { if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) { errno = errno_ENOMEM; if (AllocatorMayReturnNull()) @@ -238,12 +241,12 @@ void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) { return dfsan_realloc(ptr, nmemb * size); } -void *dfsan_valloc(uptr size) { +void *__dfsan::dfsan_valloc(uptr size) { return SetErrnoOnNull( DFsanAllocate(size, GetPageSizeCached(), false /*zeroise*/)); } -void *dfsan_pvalloc(uptr size) { +void *__dfsan::dfsan_pvalloc(uptr size) { uptr PageSize = GetPageSizeCached(); if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) { errno = errno_ENOMEM; @@ -257,7 +260,7 @@ void *dfsan_pvalloc(uptr size) { return SetErrnoOnNull(DFsanAllocate(size, PageSize, false /*zeroise*/)); } -void *dfsan_aligned_alloc(uptr alignment, uptr size) { +void *__dfsan::dfsan_aligned_alloc(uptr alignment, uptr size) { if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) { errno = errno_EINVAL; if (AllocatorMayReturnNull()) @@ -268,7 +271,7 @@ void *dfsan_aligned_alloc(uptr alignment, uptr size) { return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/)); } -void *dfsan_memalign(uptr alignment, uptr size) { +void *__dfsan::dfsan_memalign(uptr alignment, uptr size) { if (UNLIKELY(!IsPowerOfTwo(alignment))) { errno = errno_EINVAL; if (AllocatorMayReturnNull()) @@ -279,7 +282,7 @@ void *dfsan_memalign(uptr alignment, uptr size) { return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/)); } -int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) { +int __dfsan::dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) { if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) { if (AllocatorMayReturnNull()) return errno_EINVAL; @@ -295,10 +298,7 @@ int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) { return 0; } -} // namespace __dfsan - -using namespace __dfsan; - +extern "C" { uptr __sanitizer_get_current_allocated_bytes() { uptr stats[AllocatorStatCount]; allocator.GetStats(stats); @@ -331,3 +331,4 @@ uptr __sanitizer_get_allocated_size_fast(const void *p) { DCHECK_EQ(ret, __sanitizer_get_allocated_size(p)); return ret; } +} diff --git a/compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp b/compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp index f95194d19f03a4..2ca619e92adb5a 100644 --- a/compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp +++ b/compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp @@ -13,16 +13,18 @@ #include "dfsan_chained_origin_depot.h" -namespace __dfsan { +using namespace __dfsan; static ChainedOriginDepot chainedOriginDepot; -ChainedOriginDepot* GetChainedOriginDepot() { return &chainedOriginDepot; } +ChainedOriginDepot* __dfsan::GetChainedOriginDepot() { + return &chainedOriginDepot; +} -void ChainedOriginDepotLockBeforeFork() { chainedOriginDepot.LockBeforeFork(); } +void __dfsan::ChainedOriginDepotLockBeforeFork() { + chainedOriginDepot.LockBeforeFork(); +} -void ChainedOriginDepotUnlockAfterFork(bool fork_child) { +void __dfsan::ChainedOriginDepotUnlockAfterFork(bool fork_child) { chainedOriginDepot.UnlockAfterFork(fork_child); } - -} // namespace __dfsan diff --git a/compiler-rt/lib/dfsan/dfsan_custom.cpp b/compiler-rt/lib/dfsan/dfsan_custom.cpp index 050f5232c04087..8fe34b2cf2b46b 100644 --- a/compiler-rt/lib/dfsan/dfsan_custom.cpp +++ b/compiler-rt/lib/dfsan/dfsan_custom.cpp @@ -60,6 +60,7 @@ SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__); SANITIZER_INTERFACE_ATTRIBUTE void __dfso_##fun() ALIAS(__dfso_##real); // Async-safe, non-reentrant spin lock. +namespace { class SignalSpinLocker { public: SignalSpinLocker() { @@ -80,6 +81,7 @@ class SignalSpinLocker { SignalSpinLocker(const SignalSpinLocker &) = delete; SignalSpinLocker &operator=(const SignalSpinLocker &) = delete; }; +} // namespace StaticSpinMutex SignalSpinLocker::sigactions_mu; diff --git a/compiler-rt/lib/dfsan/dfsan_interceptors.cpp b/compiler-rt/lib/dfsan/dfsan_interceptors.cpp index d8fb9ea8661893..20e95c23c4bc92 100644 --- a/compiler-rt/lib/dfsan/dfsan_interceptors.cpp +++ b/compiler-rt/lib/dfsan/dfsan_interceptors.cpp @@ -25,45 +25,46 @@ #include "sanitizer_common/sanitizer_posix.h" #include "sanitizer_common/sanitizer_tls_get_addr.h" +using namespace __dfsan; using namespace __sanitizer; static bool interceptors_initialized; struct DlsymAlloc : public DlSymAllocator { - static bool UseImpl() { return !__dfsan::dfsan_inited; } + static bool UseImpl() { return !dfsan_inited; } }; INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) { - return __dfsan::dfsan_reallocarray(ptr, nmemb, size); + return dfsan_reallocarray(ptr, nmemb, size); } INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) { - void *ptr = __dfsan::dfsan_memalign(alignment, size); + void *ptr = dfsan_memalign(alignment, size); if (ptr) DTLS_on_libc_memalign(ptr, size); return ptr; } INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) { - return __dfsan::dfsan_aligned_alloc(alignment, size); + return dfsan_aligned_alloc(alignment, size); } INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) { if (DlsymAlloc::Use()) return DlsymAlloc::Callocate(nmemb, size); - return __dfsan::dfsan_calloc(nmemb, size); + return dfsan_calloc(nmemb, size); } INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) { if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr)) return DlsymAlloc::Realloc(ptr, size); - return __dfsan::dfsan_realloc(ptr, size); + return dfsan_realloc(ptr, size); } INTERCEPTOR(void *, malloc, SIZE_T size) { if (DlsymAlloc::Use()) return DlsymAlloc::Allocate(size); - return __dfsan::dfsan_malloc(size); + return dfsan_malloc(size); } INTERCEPTOR(void, free, void *ptr) { @@ -71,7 +72,7 @@ INTERCEPTOR(void, free, void *ptr) { return; if (DlsymAlloc::PointerIsMine(ptr)) return DlsymAlloc::Free(ptr); - return __dfsan::dfsan_deallocate(ptr); + return dfsan_deallocate(ptr); } INTERCEPTOR(void, cfree, void *ptr) { @@ -79,26 +80,24 @@ INTERCEPTOR(void, cfree, void *ptr) { return; if (DlsymAlloc::PointerIsMine(ptr)) return DlsymAlloc::Free(ptr); - return __dfsan::dfsan_deallocate(ptr); + return dfsan_deallocate(ptr); } INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { CHECK_NE(memptr, 0); - int res = __dfsan::dfsan_posix_memalign(memptr, alignment, size); + int res = dfsan_posix_memalign(memptr, alignment, size); if (!res) dfsan_set_label(0, memptr, sizeof(*memptr)); return res; } INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) { - return __dfsan::dfsan_memalign(alignment, size); + return dfsan_memalign(alignment, size); } -INTERCEPTOR(void *, valloc, SIZE_T size) { return __dfsan::dfsan_valloc(size); } +INTERCEPTOR(void *, valloc, SIZE_T size) { return dfsan_valloc(size); } -INTERCEPTOR(void *, pvalloc, SIZE_T size) { - return __dfsan::dfsan_pvalloc(size); -} +INTERCEPTOR(void *, pvalloc, SIZE_T size) { return dfsan_pvalloc(size); } INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) { internal_memset(sret, 0, sizeof(*sret)); @@ -115,16 +114,16 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { return __sanitizer_get_allocated_size(ptr); } -#define ENSURE_DFSAN_INITED() \ - do { \ - CHECK(!__dfsan::dfsan_init_is_running); \ - if (!__dfsan::dfsan_inited) { \ - __dfsan::dfsan_init(); \ - } \ +#define ENSURE_DFSAN_INITED() \ + do { \ + CHECK(!dfsan_init_is_running); \ + if (!dfsan_inited) { \ + dfsan_init(); \ + } \ } while (0) #define COMMON_INTERCEPTOR_ENTER(func, ...) \ - if (__dfsan::dfsan_init_is_running) \ + if (dfsan_init_is_running) \ return REAL(func)(__VA_ARGS__); \ ENSURE_DFSAN_INITED(); \ dfsan_set_label(0, __errno_location(), sizeof(int)); @@ -133,7 +132,7 @@ INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, int fd, OFF_T offset) { if (common_flags()->detect_write_exec) ReportMmapWriteExec(prot, flags); - if (!__dfsan::dfsan_inited) + if (!dfsan_inited) return (void *)internal_mmap(addr, length, prot, flags, fd, offset); COMMON_INTERCEPTOR_ENTER(mmap, addr, length, prot, flags, fd, offset); void *res = REAL(mmap)(addr, length, prot, flags, fd, offset); @@ -147,7 +146,7 @@ INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, int fd, OFF64_T offset) { if (common_flags()->detect_write_exec) ReportMmapWriteExec(prot, flags); - if (!__dfsan::dfsan_inited) + if (!dfsan_inited) return (void *)internal_mmap(addr, length, prot, flags, fd, offset); COMMON_INTERCEPTOR_ENTER(mmap64, addr, length, prot, flags, fd, offset); void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset); @@ -158,7 +157,7 @@ INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, } INTERCEPTOR(int, munmap, void *addr, SIZE_T length) { - if (!__dfsan::dfsan_inited) + if (!dfsan_inited) return internal_munmap(addr, length); COMMON_INTERCEPTOR_ENTER(munmap, addr, length); int res = REAL(munmap)(addr, length); @@ -167,12 +166,12 @@ INTERCEPTOR(int, munmap, void *addr, SIZE_T length) { return res; } -#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ - if (__dfsan::DFsanThread *t = __dfsan::GetCurrentThread()) { \ - *begin = t->tls_begin(); \ - *end = t->tls_end(); \ - } else { \ - *begin = *end = 0; \ +#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ + if (DFsanThread *t = GetCurrentThread()) { \ + *begin = t->tls_begin(); \ + *end = t->tls_end(); \ + } else { \ + *begin = *end = 0; \ } #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \ dfsan_set_label(0, ptr, size) @@ -190,8 +189,7 @@ INTERCEPTOR(void *, __tls_get_addr, void *arg) { return res; } -namespace __dfsan { -void initialize_interceptors() { +void __dfsan::initialize_interceptors() { CHECK(!interceptors_initialized); INTERCEPT_FUNCTION(aligned_alloc); @@ -217,4 +215,3 @@ void initialize_interceptors() { interceptors_initialized = true; } -} // namespace __dfsan diff --git a/compiler-rt/lib/dfsan/dfsan_thread.cpp b/compiler-rt/lib/dfsan/dfsan_thread.cpp index e64f0f818fb804..c1d47514f4bd99 100644 --- a/compiler-rt/lib/dfsan/dfsan_thread.cpp +++ b/compiler-rt/lib/dfsan/dfsan_thread.cpp @@ -5,7 +5,7 @@ #include "dfsan.h" #include "sanitizer_common/sanitizer_tls_get_addr.h" -namespace __dfsan { +using namespace __dfsan; DFsanThread *DFsanThread::Create(thread_callback_t start_routine, void *arg, bool track_origins) { @@ -99,7 +99,7 @@ bool DFsanThread::AddrIsInStack(uptr addr) { static pthread_key_t tsd_key; static bool tsd_key_inited = false; -void DFsanTSDInit(void (*destructor)(void *tsd)) { +void __dfsan::DFsanTSDInit(void (*destructor)(void *tsd)) { CHECK(!tsd_key_inited); tsd_key_inited = true; CHECK_EQ(0, pthread_key_create(&tsd_key, destructor)); @@ -107,9 +107,9 @@ void DFsanTSDInit(void (*destructor)(void *tsd)) { static THREADLOCAL DFsanThread *dfsan_current_thread; -DFsanThread *GetCurrentThread() { return dfsan_current_thread; } +DFsanThread *__dfsan::GetCurrentThread() { return dfsan_current_thread; } -void SetCurrentThread(DFsanThread *t) { +void __dfsan::SetCurrentThread(DFsanThread *t) { // Make sure we do not reset the current DFsanThread. CHECK_EQ(0, dfsan_current_thread); dfsan_current_thread = t; @@ -118,7 +118,7 @@ void SetCurrentThread(DFsanThread *t) { pthread_setspecific(tsd_key, t); } -void DFsanTSDDtor(void *tsd) { +void __dfsan::DFsanTSDDtor(void *tsd) { DFsanThread *t = (DFsanThread *)tsd; if (t->destructor_iterations_ > 1) { t->destructor_iterations_--; @@ -130,5 +130,3 @@ void DFsanTSDDtor(void *tsd) { atomic_signal_fence(memory_order_seq_cst); DFsanThread::TSDDtor(tsd); } - -} // namespace __dfsan