Skip to content

Commit

Permalink
[dfsan] Use namespace qualifier and internalize accidentally exported…
Browse files Browse the repository at this point in the history
… functions. NFC
  • Loading branch information
MaskRay committed Aug 10, 2024
1 parent c27415f commit 80eea01
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 84 deletions.
30 changes: 12 additions & 18 deletions compiler-rt/lib/dfsan/dfsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,45 +779,45 @@ 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 "
"-dfsan-track-origins=1 option?%s\n",
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 "
"be a DFSan bug.%s\n",
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 "
"be a DFSan bug.%s\n",
d.Warning(), origin, d.Default());
}

bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
static bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
Decorator d;
bool found = false;

Expand All @@ -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;
Expand All @@ -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()) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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"),
Expand Down
39 changes: 20 additions & 19 deletions compiler-rt/lib/dfsan/dfsan_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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<AllocatorCache *>(ms->allocator_cache);
Expand Down Expand Up @@ -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<Metadata *>(allocator.GetMetaData(p));
uptr size = meta->requested_size;
Expand All @@ -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<Metadata *>(allocator.GetMetaData(old_p));
uptr old_size = meta->requested_size;
uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
Expand All @@ -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;
Expand Down Expand Up @@ -209,15 +212,15 @@ static uptr AllocationSizeFast(const void *p) {
return reinterpret_cast<Metadata *>(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) {
Expand All @@ -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())
Expand All @@ -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;
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -331,3 +331,4 @@ uptr __sanitizer_get_allocated_size_fast(const void *p) {
DCHECK_EQ(ret, __sanitizer_get_allocated_size(p));
return ret;
}
}
14 changes: 8 additions & 6 deletions compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions compiler-rt/lib/dfsan/dfsan_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -80,6 +81,7 @@ class SignalSpinLocker {
SignalSpinLocker(const SignalSpinLocker &) = delete;
SignalSpinLocker &operator=(const SignalSpinLocker &) = delete;
};
} // namespace

StaticSpinMutex SignalSpinLocker::sigactions_mu;

Expand Down
Loading

0 comments on commit 80eea01

Please sign in to comment.