Skip to content

Commit 80eea01

Browse files
committed
[dfsan] Use namespace qualifier and internalize accidentally exported functions. NFC
1 parent c27415f commit 80eea01

File tree

6 files changed

+78
-84
lines changed

6 files changed

+78
-84
lines changed

compiler-rt/lib/dfsan/dfsan.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -779,45 +779,45 @@ dfsan_get_labels_in_signal_reaches_function() {
779779
return __dfsan::labels_in_signal_reaches_function;
780780
}
781781

782+
namespace {
782783
class Decorator : public __sanitizer::SanitizerCommonDecorator {
783784
public:
784785
Decorator() : SanitizerCommonDecorator() {}
785786
const char *Origin() const { return Magenta(); }
786787
};
788+
} // namespace
787789

788-
namespace {
789-
790-
void PrintNoOriginTrackingWarning() {
790+
static void PrintNoOriginTrackingWarning() {
791791
Decorator d;
792792
Printf(
793793
" %sDFSan: origin tracking is not enabled. Did you specify the "
794794
"-dfsan-track-origins=1 option?%s\n",
795795
d.Warning(), d.Default());
796796
}
797797

798-
void PrintNoTaintWarning(const void *address) {
798+
static void PrintNoTaintWarning(const void *address) {
799799
Decorator d;
800800
Printf(" %sDFSan: no tainted value at %x%s\n", d.Warning(), address,
801801
d.Default());
802802
}
803803

804-
void PrintInvalidOriginWarning(dfsan_label label, const void *address) {
804+
static void PrintInvalidOriginWarning(dfsan_label label, const void *address) {
805805
Decorator d;
806806
Printf(
807807
" %sTaint value 0x%x (at %p) has invalid origin tracking. This can "
808808
"be a DFSan bug.%s\n",
809809
d.Warning(), label, address, d.Default());
810810
}
811811

812-
void PrintInvalidOriginIdWarning(dfsan_origin origin) {
812+
static void PrintInvalidOriginIdWarning(dfsan_origin origin) {
813813
Decorator d;
814814
Printf(
815815
" %sOrigin Id %d has invalid origin tracking. This can "
816816
"be a DFSan bug.%s\n",
817817
d.Warning(), origin, d.Default());
818818
}
819819

820-
bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
820+
static bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
821821
Decorator d;
822822
bool found = false;
823823

@@ -841,8 +841,8 @@ bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
841841
return found;
842842
}
843843

844-
bool PrintOriginTraceToStr(const void *addr, const char *description,
845-
InternalScopedString *out) {
844+
static bool PrintOriginTraceToStr(const void *addr, const char *description,
845+
InternalScopedString *out) {
846846
CHECK(out);
847847
CHECK(dfsan_get_track_origins());
848848
Decorator d;
@@ -860,8 +860,6 @@ bool PrintOriginTraceToStr(const void *addr, const char *description,
860860
return PrintOriginTraceFramesToStr(o, out);
861861
}
862862

863-
} // namespace
864-
865863
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_print_origin_trace(
866864
const void *addr, const char *description) {
867865
if (!dfsan_get_track_origins()) {
@@ -1167,7 +1165,7 @@ static bool ProtectMemoryRange(uptr beg, uptr size, const char *name) {
11671165

11681166
// TODO: InitShadow is based on msan.
11691167
// Consider refactoring these into a shared implementation.
1170-
bool InitShadow(bool init_origins, bool dry_run) {
1168+
static bool InitShadow(bool init_origins, bool dry_run) {
11711169
// Let user know mapping parameters first.
11721170
VPrintf(1, "dfsan_init %p\n", (void *)&__dfsan::dfsan_init);
11731171
for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
@@ -1227,7 +1225,7 @@ bool InitShadow(bool init_origins, bool dry_run) {
12271225
return true;
12281226
}
12291227

1230-
bool InitShadowWithReExec(bool init_origins) {
1228+
static bool InitShadowWithReExec(bool init_origins) {
12311229
// Start with dry run: check layout is ok, but don't print warnings because
12321230
// warning messages will cause tests to fail (even if we successfully re-exec
12331231
// after the warning).
@@ -1290,11 +1288,7 @@ static void DFsanInit(int argc, char **argv, char **envp) {
12901288
dfsan_inited = true;
12911289
}
12921290

1293-
namespace __dfsan {
1294-
1295-
void dfsan_init() { DFsanInit(0, nullptr, nullptr); }
1296-
1297-
} // namespace __dfsan
1291+
void __dfsan::dfsan_init() { DFsanInit(0, nullptr, nullptr); }
12981292

12991293
#if SANITIZER_CAN_USE_PREINIT_ARRAY
13001294
__attribute__((section(".preinit_array"),

compiler-rt/lib/dfsan/dfsan_allocator.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "sanitizer_common/sanitizer_allocator_report.h"
2323
#include "sanitizer_common/sanitizer_errno.h"
2424

25-
namespace __dfsan {
25+
using namespace __dfsan;
26+
27+
namespace {
2628

2729
struct Metadata {
2830
uptr requested_size;
@@ -67,8 +69,9 @@ static AllocatorCache fallback_allocator_cache;
6769
static StaticSpinMutex fallback_mutex;
6870

6971
static uptr max_malloc_size;
72+
} // namespace
7073

71-
void dfsan_allocator_init() {
74+
void __dfsan::dfsan_allocator_init() {
7275
SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
7376
allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
7477
if (common_flags()->max_allocation_size_mb)
@@ -78,7 +81,7 @@ void dfsan_allocator_init() {
7881
max_malloc_size = kMaxAllowedMallocSize;
7982
}
8083

81-
AllocatorCache *GetAllocatorCache(DFsanThreadLocalMallocStorage *ms) {
84+
static AllocatorCache *GetAllocatorCache(DFsanThreadLocalMallocStorage *ms) {
8285
CHECK(ms);
8386
CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
8487
return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
@@ -133,7 +136,7 @@ static void *DFsanAllocate(uptr size, uptr alignment, bool zeroise) {
133136
return allocated;
134137
}
135138

136-
void dfsan_deallocate(void *p) {
139+
void __dfsan::dfsan_deallocate(void *p) {
137140
CHECK(p);
138141
Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
139142
uptr size = meta->requested_size;
@@ -151,7 +154,7 @@ void dfsan_deallocate(void *p) {
151154
}
152155
}
153156

154-
void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) {
157+
static void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) {
155158
Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(old_p));
156159
uptr old_size = meta->requested_size;
157160
uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
@@ -171,7 +174,7 @@ void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) {
171174
return new_p;
172175
}
173176

174-
void *DFsanCalloc(uptr nmemb, uptr size) {
177+
static void *DFsanCalloc(uptr nmemb, uptr size) {
175178
if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
176179
if (AllocatorMayReturnNull())
177180
return nullptr;
@@ -209,15 +212,15 @@ static uptr AllocationSizeFast(const void *p) {
209212
return reinterpret_cast<Metadata *>(allocator.GetMetaData(p))->requested_size;
210213
}
211214

212-
void *dfsan_malloc(uptr size) {
215+
void *__dfsan::dfsan_malloc(uptr size) {
213216
return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/));
214217
}
215218

216-
void *dfsan_calloc(uptr nmemb, uptr size) {
219+
void *__dfsan::dfsan_calloc(uptr nmemb, uptr size) {
217220
return SetErrnoOnNull(DFsanCalloc(nmemb, size));
218221
}
219222

220-
void *dfsan_realloc(void *ptr, uptr size) {
223+
void *__dfsan::dfsan_realloc(void *ptr, uptr size) {
221224
if (!ptr)
222225
return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/));
223226
if (size == 0) {
@@ -227,7 +230,7 @@ void *dfsan_realloc(void *ptr, uptr size) {
227230
return SetErrnoOnNull(DFsanReallocate(ptr, size, sizeof(u64)));
228231
}
229232

230-
void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) {
233+
void *__dfsan::dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) {
231234
if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
232235
errno = errno_ENOMEM;
233236
if (AllocatorMayReturnNull())
@@ -238,12 +241,12 @@ void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) {
238241
return dfsan_realloc(ptr, nmemb * size);
239242
}
240243

241-
void *dfsan_valloc(uptr size) {
244+
void *__dfsan::dfsan_valloc(uptr size) {
242245
return SetErrnoOnNull(
243246
DFsanAllocate(size, GetPageSizeCached(), false /*zeroise*/));
244247
}
245248

246-
void *dfsan_pvalloc(uptr size) {
249+
void *__dfsan::dfsan_pvalloc(uptr size) {
247250
uptr PageSize = GetPageSizeCached();
248251
if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
249252
errno = errno_ENOMEM;
@@ -257,7 +260,7 @@ void *dfsan_pvalloc(uptr size) {
257260
return SetErrnoOnNull(DFsanAllocate(size, PageSize, false /*zeroise*/));
258261
}
259262

260-
void *dfsan_aligned_alloc(uptr alignment, uptr size) {
263+
void *__dfsan::dfsan_aligned_alloc(uptr alignment, uptr size) {
261264
if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
262265
errno = errno_EINVAL;
263266
if (AllocatorMayReturnNull())
@@ -268,7 +271,7 @@ void *dfsan_aligned_alloc(uptr alignment, uptr size) {
268271
return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/));
269272
}
270273

271-
void *dfsan_memalign(uptr alignment, uptr size) {
274+
void *__dfsan::dfsan_memalign(uptr alignment, uptr size) {
272275
if (UNLIKELY(!IsPowerOfTwo(alignment))) {
273276
errno = errno_EINVAL;
274277
if (AllocatorMayReturnNull())
@@ -279,7 +282,7 @@ void *dfsan_memalign(uptr alignment, uptr size) {
279282
return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/));
280283
}
281284

282-
int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) {
285+
int __dfsan::dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) {
283286
if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
284287
if (AllocatorMayReturnNull())
285288
return errno_EINVAL;
@@ -295,10 +298,7 @@ int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) {
295298
return 0;
296299
}
297300

298-
} // namespace __dfsan
299-
300-
using namespace __dfsan;
301-
301+
extern "C" {
302302
uptr __sanitizer_get_current_allocated_bytes() {
303303
uptr stats[AllocatorStatCount];
304304
allocator.GetStats(stats);
@@ -331,3 +331,4 @@ uptr __sanitizer_get_allocated_size_fast(const void *p) {
331331
DCHECK_EQ(ret, __sanitizer_get_allocated_size(p));
332332
return ret;
333333
}
334+
}

compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313

1414
#include "dfsan_chained_origin_depot.h"
1515

16-
namespace __dfsan {
16+
using namespace __dfsan;
1717

1818
static ChainedOriginDepot chainedOriginDepot;
1919

20-
ChainedOriginDepot* GetChainedOriginDepot() { return &chainedOriginDepot; }
20+
ChainedOriginDepot* __dfsan::GetChainedOriginDepot() {
21+
return &chainedOriginDepot;
22+
}
2123

22-
void ChainedOriginDepotLockBeforeFork() { chainedOriginDepot.LockBeforeFork(); }
24+
void __dfsan::ChainedOriginDepotLockBeforeFork() {
25+
chainedOriginDepot.LockBeforeFork();
26+
}
2327

24-
void ChainedOriginDepotUnlockAfterFork(bool fork_child) {
28+
void __dfsan::ChainedOriginDepotUnlockAfterFork(bool fork_child) {
2529
chainedOriginDepot.UnlockAfterFork(fork_child);
2630
}
27-
28-
} // namespace __dfsan

compiler-rt/lib/dfsan/dfsan_custom.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__);
6060
SANITIZER_INTERFACE_ATTRIBUTE void __dfso_##fun() ALIAS(__dfso_##real);
6161

6262
// Async-safe, non-reentrant spin lock.
63+
namespace {
6364
class SignalSpinLocker {
6465
public:
6566
SignalSpinLocker() {
@@ -80,6 +81,7 @@ class SignalSpinLocker {
8081
SignalSpinLocker(const SignalSpinLocker &) = delete;
8182
SignalSpinLocker &operator=(const SignalSpinLocker &) = delete;
8283
};
84+
} // namespace
8385

8486
StaticSpinMutex SignalSpinLocker::sigactions_mu;
8587

0 commit comments

Comments
 (0)