Skip to content

Commit 23182af

Browse files
vitalybukayuxuanchen1997
authored andcommitted
[sanitizer_common] Use %p to print addresses (#98578)
Summary: Pointers print more leading zeroes for better alignment. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251661
1 parent 94ef887 commit 23182af

14 files changed

+50
-41
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary64.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ class SizeClassAllocator64 {
316316
Printf(
317317
"%s %02zd (%6zd): mapped: %6zdK allocs: %7zd frees: %7zd inuse: %6zd "
318318
"num_freed_chunks %7zd avail: %6zd rss: %6zdK releases: %6zd "
319-
"last released: %6lldK region: 0x%zx\n",
319+
"last released: %6lldK region: %p\n",
320320
region->exhausted ? "F" : " ", class_id, ClassIdToSize(class_id),
321321
region->mapped_user >> 10, region->stats.n_allocated,
322322
region->stats.n_freed, in_use, region->num_freed_chunks, avail_chunks,
323323
rss >> 10, region->rtoi.num_releases,
324324
region->rtoi.last_released_bytes >> 10,
325-
SpaceBeg() + kRegionSize * class_id);
325+
(void *)(SpaceBeg() + kRegionSize * class_id));
326326
}
327327

328328
void PrintStats() {

compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) {
7575
if (!pc) continue;
7676

7777
if (!GetModuleAndOffsetForPc(pc, nullptr, 0, &pcs[i])) {
78-
Printf("ERROR: unknown pc 0x%zx (may happen if dlclose is used)\n", pc);
78+
Printf("ERROR: unknown pc %p (may happen if dlclose is used)\n",
79+
(void*)pc);
7980
continue;
8081
}
8182
uptr module_base = pc - pcs[i];

compiler-rt/lib/sanitizer_common/sanitizer_libignore.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ void LibIgnore::OnLibraryLoaded(const char *name) {
105105
continue;
106106
if (IsPcInstrumented(range.beg) && IsPcInstrumented(range.end - 1))
107107
continue;
108-
VReport(1, "Adding instrumented range 0x%zx-0x%zx from library '%s'\n",
109-
range.beg, range.end, mod.full_name());
108+
VReport(1, "Adding instrumented range %p-%p from library '%s'\n",
109+
(void *)range.beg, (void *)range.end, mod.full_name());
110110
const uptr idx =
111111
atomic_load(&instrumented_ranges_count_, memory_order_relaxed);
112112
CHECK_LT(idx, ARRAY_SIZE(instrumented_code_ranges_));

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,8 @@ void DumpProcessMap() {
13721372
for (uptr i = 0; i < modules.size(); ++i) {
13731373
char uuid_str[128];
13741374
FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());
1375-
Printf("0x%zx-0x%zx %s (%s) %s\n", modules[i].base_address(),
1376-
modules[i].max_address(), modules[i].full_name(),
1375+
Printf("%p-%p %s (%s) %s\n", (void *)modules[i].base_address(),
1376+
(void *)modules[i].max_address(), modules[i].full_name(),
13771377
ModuleArchToString(modules[i].arch()), uuid_str);
13781378
}
13791379
Printf("End of module map.\n");

compiler-rt/lib/sanitizer_common/sanitizer_posix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ static void *MmapFixedImpl(uptr fixed_addr, uptr size, bool tolerate_enomem,
130130
if (tolerate_enomem && reserrno == ENOMEM)
131131
return nullptr;
132132
char mem_type[40];
133-
internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
134-
fixed_addr);
133+
internal_snprintf(mem_type, sizeof(mem_type), "memory at address %p",
134+
(void *)fixed_addr);
135135
ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
136136
}
137137
IncreaseTotalMmap(size);

compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ static bool MmapFixed(uptr fixed_addr, uptr size, int additional_flags,
327327
MAP_PRIVATE | MAP_FIXED | additional_flags | MAP_ANON, name);
328328
int reserrno;
329329
if (internal_iserror(p, &reserrno)) {
330-
Report("ERROR: %s failed to "
331-
"allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
332-
SanitizerToolName, size, size, fixed_addr, reserrno);
330+
Report(
331+
"ERROR: %s failed to "
332+
"allocate 0x%zx (%zd) bytes at address %p (errno: %d)\n",
333+
SanitizerToolName, size, size, (void *)fixed_addr, reserrno);
333334
return false;
334335
}
335336
IncreaseTotalMmap(size);

compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_printer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void FormattedStackTracePrinter::RenderFrame(InternalScopedString *buffer,
192192
buffer->AppendF("%u", frame_no);
193193
break;
194194
case 'p':
195-
buffer->AppendF("0x%zx", address);
195+
buffer->AppendF("%p", (void *)address);
196196
break;
197197
case 'm':
198198
buffer->AppendF("%s", StripPathPrefix(info->module, strip_path_prefix));

compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ static void TracerThreadDieCallback() {
257257
static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo,
258258
void *uctx) {
259259
SignalContext ctx(siginfo, uctx);
260-
Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum,
261-
ctx.addr, ctx.pc, ctx.sp);
260+
Printf("Tracer caught signal %d: addr=%p pc=%p sp=%p\n", signum,
261+
(void *)ctx.addr, (void *)ctx.pc, (void *)ctx.sp);
262262
ThreadSuspender *inst = thread_suspender_instance;
263263
if (inst) {
264264
if (signum == SIGABRT)

compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ static void TracerThreadDieCallback() {
158158
static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo,
159159
void *uctx) {
160160
SignalContext ctx(siginfo, uctx);
161-
Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum,
162-
ctx.addr, ctx.pc, ctx.sp);
161+
Printf("Tracer caught signal %d: addr=%p pc=%p sp=%p\n", signum,
162+
(void *)ctx.addr, (void *)ctx.pc, (void *)ctx.sp);
163163
ThreadSuspender *inst = thread_suspender_instance;
164164
if (inst) {
165165
if (signum == SIGABRT)

compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,25 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
121121
uptr tls_size = 0;
122122
uptr tls_beg = reinterpret_cast<uptr>(res) - arg->offset - kDtvOffset;
123123
VReport(2,
124-
"__tls_get_addr: %p {0x%zx,0x%zx} => %p; tls_beg: 0x%zx; sp: %p "
124+
"__tls_get_addr: %p {0x%zx,0x%zx} => %p; tls_beg: %p; sp: %p "
125125
"num_live_dtls %zd\n",
126-
(void *)arg, arg->dso_id, arg->offset, res, tls_beg, (void *)&tls_beg,
126+
(void *)arg, arg->dso_id, arg->offset, res, (void *)tls_beg, &tls_beg,
127127
atomic_load(&number_of_live_dtls, memory_order_relaxed));
128128
if (dtls.last_memalign_ptr == tls_beg) {
129129
tls_size = dtls.last_memalign_size;
130-
VReport(2, "__tls_get_addr: glibc <=2.24 suspected; tls={0x%zx,0x%zx}\n",
131-
tls_beg, tls_size);
130+
VReport(2, "__tls_get_addr: glibc <=2.24 suspected; tls={%p,0x%zx}\n",
131+
(void *)tls_beg, tls_size);
132132
} else if (tls_beg >= static_tls_begin && tls_beg < static_tls_end) {
133133
// This is the static TLS block which was initialized / unpoisoned at thread
134134
// creation.
135-
VReport(2, "__tls_get_addr: static tls: 0x%zx\n", tls_beg);
135+
VReport(2, "__tls_get_addr: static tls: %p\n", (void *)tls_beg);
136136
tls_size = 0;
137137
} else if (const void *start =
138138
__sanitizer_get_allocated_begin((void *)tls_beg)) {
139139
tls_beg = (uptr)start;
140140
tls_size = __sanitizer_get_allocated_size(start);
141-
VReport(2, "__tls_get_addr: glibc >=2.25 suspected; tls={0x%zx,0x%zx}\n",
142-
tls_beg, tls_size);
141+
VReport(2, "__tls_get_addr: glibc >=2.25 suspected; tls={%p,0x%zx}\n",
142+
(void *)tls_beg, tls_size);
143143
} else {
144144
VReport(2, "__tls_get_addr: Can't guess glibc version\n");
145145
// This may happen inside the DTOR of main thread, so just ignore it.

compiler-rt/lib/sanitizer_common/sanitizer_win.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name) {
276276
MEM_COMMIT, PAGE_READWRITE);
277277
if (p == 0) {
278278
char mem_type[30];
279-
internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
280-
fixed_addr);
279+
internal_snprintf(mem_type, sizeof(mem_type), "memory at address %p",
280+
(void *)fixed_addr);
281281
ReportMmapFailureAndDie(size, mem_type, "allocate", GetLastError());
282282
}
283283
return p;
@@ -308,8 +308,8 @@ void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size, const char *name) {
308308
MEM_COMMIT, PAGE_READWRITE);
309309
if (p == 0) {
310310
char mem_type[30];
311-
internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
312-
fixed_addr);
311+
internal_snprintf(mem_type, sizeof(mem_type), "memory at address %p",
312+
(void *)fixed_addr);
313313
return ReturnNullptrOnOOMOrDie(size, mem_type, "allocate");
314314
}
315315
return p;

compiler-rt/lib/sanitizer_common/tests/sanitizer_stackdepot_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ TEST_F(StackDepotTest, Print) {
9898
return s;
9999
return std::regex_replace(s, std::regex("\\.\\*"), ".*\\n.*");
100100
};
101-
EXPECT_EXIT(
102-
(StackDepotPrintAll(), exit(0)), ::testing::ExitedWithCode(0),
103-
fix_regex("Stack for id .*#0 0x1.*#1 0x2.*#2 0x3.*#3 0x4.*#4 0x7.*"));
104101
EXPECT_EXIT(
105102
(StackDepotPrintAll(), exit(0)), ::testing::ExitedWithCode(0),
106103
fix_regex(
107-
"Stack for id .*#0 0x1.*#1 0x2.*#2 0x3.*#3 0x4.*#4 0x8.*#5 0x9.*"));
104+
"Stack for id .*#0 0x0*1.*#1 0x0*2.*#2 0x0*3.*#3 0x0*4.*#4 0x0*7.*"));
105+
EXPECT_EXIT((StackDepotPrintAll(), exit(0)), ::testing::ExitedWithCode(0),
106+
fix_regex("Stack for id .*#0 0x0*1.*#1 0x0*2.*#2 0x0*3.*#3 "
107+
"0x0*4.*#4 0x0*8.*#5 0x0*9.*"));
108108
}
109109

110110
TEST_F(StackDepotTest, PrintNoLock) {

compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_printer_test.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
//===----------------------------------------------------------------------===//
1212
#include "sanitizer_common/sanitizer_stacktrace_printer.h"
1313

14+
#include "gmock/gmock.h"
1415
#include "gtest/gtest.h"
1516
#include "interception/interception.h"
1617

18+
using testing::MatchesRegex;
19+
1720
namespace __sanitizer {
1821

1922
class TestFormattedStackTracePrinter final : public FormattedStackTracePrinter {
@@ -96,10 +99,12 @@ TEST(FormattedStackTracePrinter, RenderFrame) {
9699
"Function:%f FunctionOffset:%q Source:%s Line:%l "
97100
"Column:%c",
98101
frame_no, info.address, &info, false, "/path/to/");
99-
EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 "
100-
"Function:foo FunctionOffset:0x100 Source:my/source Line:10 "
101-
"Column:5",
102-
str.data());
102+
EXPECT_THAT(
103+
str.data(),
104+
MatchesRegex(
105+
"% Frame:42 PC:0x0*400000 Module:my/module ModuleOffset:0x200 "
106+
"Function:foo FunctionOffset:0x100 Source:my/source Line:10 "
107+
"Column:5"));
103108

104109
str.clear();
105110
// Check that RenderFrame() strips interceptor prefixes.
@@ -109,10 +114,12 @@ TEST(FormattedStackTracePrinter, RenderFrame) {
109114
"Function:%f FunctionOffset:%q Source:%s Line:%l "
110115
"Column:%c",
111116
frame_no, info.address, &info, false, "/path/to/");
112-
EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 "
113-
"Function:bar FunctionOffset:0x100 Source:my/source Line:10 "
114-
"Column:5",
115-
str.data());
117+
EXPECT_THAT(
118+
str.data(),
119+
MatchesRegex(
120+
"% Frame:42 PC:0x0*400000 Module:my/module ModuleOffset:0x200 "
121+
"Function:bar FunctionOffset:0x100 Source:my/source Line:10 "
122+
"Column:5"));
116123
info.Clear();
117124
str.clear();
118125

compiler-rt/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ TEST(GetCurrentPc, Basic) {
284284
StackTrace::GetCurrentPc(),
285285
};
286286
for (uptr i = 0; i < ARRAY_SIZE(pcs); i++)
287-
Printf("pc%zu: 0x%zx\n", i, pcs[i]);
287+
Printf("pc%zu: %p\n", i, (void *)(pcs[i]));
288288
for (uptr i = 1; i < ARRAY_SIZE(pcs); i++) {
289289
EXPECT_GT(pcs[i], pcs[0]);
290290
EXPECT_LT(pcs[i], pcs[0] + 1000);

0 commit comments

Comments
 (0)