Skip to content

Commit c557621

Browse files
committed
[NFC][hwasan] Make ShowHeapOrGlobalCandidate a method (#66682)
1 parent 36b37c7 commit c557621

File tree

1 file changed

+75
-73
lines changed

1 file changed

+75
-73
lines changed

compiler-rt/lib/hwasan/hwasan_report.cpp

+75-73
Original file line numberDiff line numberDiff line change
@@ -319,77 +319,6 @@ static uptr GetGlobalSizeFromDescriptor(uptr ptr) {
319319
return 0;
320320
}
321321

322-
static void ShowHeapOrGlobalCandidate(uptr untagged_addr, tag_t *candidate,
323-
tag_t *left, tag_t *right) {
324-
Decorator d;
325-
uptr mem = ShadowToMem(reinterpret_cast<uptr>(candidate));
326-
HwasanChunkView chunk = FindHeapChunkByAddress(mem);
327-
if (chunk.IsAllocated()) {
328-
uptr offset;
329-
const char *whence;
330-
if (untagged_addr < chunk.End() && untagged_addr >= chunk.Beg()) {
331-
offset = untagged_addr - chunk.Beg();
332-
whence = "inside";
333-
} else if (candidate == left) {
334-
offset = untagged_addr - chunk.End();
335-
whence = "after";
336-
} else {
337-
offset = chunk.Beg() - untagged_addr;
338-
whence = "before";
339-
}
340-
Printf("%s", d.Error());
341-
Printf("\nCause: heap-buffer-overflow\n");
342-
Printf("%s", d.Default());
343-
Printf("%s", d.Location());
344-
Printf("%p is located %zd bytes %s a %zd-byte region [%p,%p)\n",
345-
untagged_addr, offset, whence, chunk.UsedSize(), chunk.Beg(),
346-
chunk.End());
347-
Printf("%s", d.Allocation());
348-
Printf("allocated by thread T%u here:\n", chunk.GetAllocThreadId());
349-
Printf("%s", d.Default());
350-
GetStackTraceFromId(chunk.GetAllocStackId()).Print();
351-
return;
352-
}
353-
// Check whether the address points into a loaded library. If so, this is
354-
// most likely a global variable.
355-
const char *module_name;
356-
uptr module_address;
357-
Symbolizer *sym = Symbolizer::GetOrInit();
358-
if (sym->GetModuleNameAndOffsetForPC(mem, &module_name, &module_address)) {
359-
Printf("%s", d.Error());
360-
Printf("\nCause: global-overflow\n");
361-
Printf("%s", d.Default());
362-
DataInfo info;
363-
Printf("%s", d.Location());
364-
if (sym->SymbolizeData(mem, &info) && info.start) {
365-
Printf(
366-
"%p is located %zd bytes %s a %zd-byte global variable "
367-
"%s [%p,%p) in %s\n",
368-
untagged_addr,
369-
candidate == left ? untagged_addr - (info.start + info.size)
370-
: info.start - untagged_addr,
371-
candidate == left ? "after" : "before", info.size, info.name,
372-
info.start, info.start + info.size, module_name);
373-
} else {
374-
uptr size = GetGlobalSizeFromDescriptor(mem);
375-
if (size == 0)
376-
// We couldn't find the size of the global from the descriptors.
377-
Printf(
378-
"%p is located %s a global variable in "
379-
"\n #0 0x%x (%s+0x%x)\n",
380-
untagged_addr, candidate == left ? "after" : "before", mem,
381-
module_name, module_address);
382-
else
383-
Printf(
384-
"%p is located %s a %zd-byte global variable in "
385-
"\n #0 0x%x (%s+0x%x)\n",
386-
untagged_addr, candidate == left ? "after" : "before", size, mem,
387-
module_name, module_address);
388-
}
389-
Printf("%s", d.Default());
390-
}
391-
}
392-
393322
void ReportStats() {}
394323

395324
static void PrintTagInfoAroundAddr(tag_t *tag_ptr, uptr num_rows,
@@ -479,6 +408,8 @@ class BaseReport {
479408

480409
protected:
481410
void PrintAddressDescription() const;
411+
void PrintHeapOrGlobalCandidate(tag_t *candidate, tag_t *left,
412+
tag_t *right) const;
482413

483414
ScopedReport scoped_report;
484415
StackTrace *stack = nullptr;
@@ -498,6 +429,77 @@ class BaseReport {
498429
} heap;
499430
};
500431

432+
void BaseReport::PrintHeapOrGlobalCandidate(tag_t *candidate, tag_t *left,
433+
tag_t *right) const {
434+
Decorator d;
435+
uptr mem = ShadowToMem(reinterpret_cast<uptr>(candidate));
436+
HwasanChunkView chunk = FindHeapChunkByAddress(mem);
437+
if (chunk.IsAllocated()) {
438+
uptr offset;
439+
const char *whence;
440+
if (untagged_addr < chunk.End() && untagged_addr >= chunk.Beg()) {
441+
offset = untagged_addr - chunk.Beg();
442+
whence = "inside";
443+
} else if (candidate == left) {
444+
offset = untagged_addr - chunk.End();
445+
whence = "after";
446+
} else {
447+
offset = chunk.Beg() - untagged_addr;
448+
whence = "before";
449+
}
450+
Printf("%s", d.Error());
451+
Printf("\nCause: heap-buffer-overflow\n");
452+
Printf("%s", d.Default());
453+
Printf("%s", d.Location());
454+
Printf("%p is located %zd bytes %s a %zd-byte region [%p,%p)\n",
455+
untagged_addr, offset, whence, chunk.UsedSize(), chunk.Beg(),
456+
chunk.End());
457+
Printf("%s", d.Allocation());
458+
Printf("allocated by thread T%u here:\n", chunk.GetAllocThreadId());
459+
Printf("%s", d.Default());
460+
GetStackTraceFromId(chunk.GetAllocStackId()).Print();
461+
return;
462+
}
463+
// Check whether the address points into a loaded library. If so, this is
464+
// most likely a global variable.
465+
const char *module_name;
466+
uptr module_address;
467+
Symbolizer *sym = Symbolizer::GetOrInit();
468+
if (sym->GetModuleNameAndOffsetForPC(mem, &module_name, &module_address)) {
469+
Printf("%s", d.Error());
470+
Printf("\nCause: global-overflow\n");
471+
Printf("%s", d.Default());
472+
DataInfo info;
473+
Printf("%s", d.Location());
474+
if (sym->SymbolizeData(mem, &info) && info.start) {
475+
Printf(
476+
"%p is located %zd bytes %s a %zd-byte global variable "
477+
"%s [%p,%p) in %s\n",
478+
untagged_addr,
479+
candidate == left ? untagged_addr - (info.start + info.size)
480+
: info.start - untagged_addr,
481+
candidate == left ? "after" : "before", info.size, info.name,
482+
info.start, info.start + info.size, module_name);
483+
} else {
484+
uptr size = GetGlobalSizeFromDescriptor(mem);
485+
if (size == 0)
486+
// We couldn't find the size of the global from the descriptors.
487+
Printf(
488+
"%p is located %s a global variable in "
489+
"\n #0 0x%x (%s+0x%x)\n",
490+
untagged_addr, candidate == left ? "after" : "before", mem,
491+
module_name, module_address);
492+
else
493+
Printf(
494+
"%p is located %s a %zd-byte global variable in "
495+
"\n #0 0x%x (%s+0x%x)\n",
496+
untagged_addr, candidate == left ? "after" : "before", size, mem,
497+
module_name, module_address);
498+
}
499+
Printf("%s", d.Default());
500+
}
501+
}
502+
501503
void BaseReport::PrintAddressDescription() const {
502504
Decorator d;
503505
int num_descriptions_printed = 0;
@@ -565,7 +567,7 @@ void BaseReport::PrintAddressDescription() const {
565567

566568
if (!stack_allocations_count && candidate &&
567569
candidate_distance <= kCloseCandidateDistance) {
568-
ShowHeapOrGlobalCandidate(untagged_addr, candidate, left, right);
570+
PrintHeapOrGlobalCandidate(candidate, left, right);
569571
num_descriptions_printed++;
570572
}
571573

@@ -607,7 +609,7 @@ void BaseReport::PrintAddressDescription() const {
607609
});
608610

609611
if (candidate && num_descriptions_printed == 0) {
610-
ShowHeapOrGlobalCandidate(untagged_addr, candidate, left, right);
612+
PrintHeapOrGlobalCandidate(candidate, left, right);
611613
num_descriptions_printed++;
612614
}
613615

0 commit comments

Comments
 (0)