Skip to content

Commit ee9bb87

Browse files
committed
[hwasan] On every use-after-free print a developer note: the index of this heap object in the thread's deallocation ring buffer. Mostly useful to hwasan developers, will hopefully let us know the good size of the deallocation ring buffer
llvm-svn: 342014
1 parent ca007b7 commit ee9bb87

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

compiler-rt/lib/hwasan/hwasan_report.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,21 @@ class Decorator: public __sanitizer::SanitizerCommonDecorator {
4646
const char *Thread() { return Green(); }
4747
};
4848

49-
bool FindHeapAllocation(HeapAllocationsRingBuffer *rb,
49+
// Returns the index of the rb element that matches tagged_addr (plus one),
50+
// or zero if found nothing.
51+
uptr FindHeapAllocation(HeapAllocationsRingBuffer *rb,
5052
uptr tagged_addr,
5153
HeapAllocationRecord *har) {
52-
if (!rb) return false;
54+
if (!rb) return 0;
5355
for (uptr i = 0, size = rb->size(); i < size; i++) {
5456
auto h = (*rb)[i];
5557
if (h.tagged_addr <= tagged_addr &&
5658
h.tagged_addr + h.requested_size > tagged_addr) {
5759
*har = h;
58-
return true;
60+
return i + 1;
5961
}
6062
}
61-
return false;
63+
return 0;
6264
}
6365

6466
void PrintAddressDescription(uptr tagged_addr, uptr access_size) {
@@ -110,7 +112,7 @@ void PrintAddressDescription(uptr tagged_addr, uptr access_size) {
110112
Thread::VisitAllLiveThreads([&](Thread *t) {
111113
// Scan all threads' ring buffers to find if it's a heap-use-after-free.
112114
HeapAllocationRecord har;
113-
if (FindHeapAllocation(t->heap_allocations(), tagged_addr, &har)) {
115+
if (uptr D = FindHeapAllocation(t->heap_allocations(), tagged_addr, &har)) {
114116
Printf("%s", d.Location());
115117
Printf("%p is located %zd bytes inside of %zd-byte region [%p,%p)\n",
116118
untagged_addr, untagged_addr - UntagAddr(har.tagged_addr),
@@ -127,6 +129,11 @@ void PrintAddressDescription(uptr tagged_addr, uptr access_size) {
127129
GetStackTraceFromId(har.alloc_context_id).Print();
128130
t->Announce();
129131

132+
// Print a developer note: the index of this heap object
133+
// in the thread's deallocation ring buffer.
134+
Printf("hwasan_dev_note_heap_rb_distance: %zd %zd\n", D,
135+
flags()->heap_history_size);
136+
130137
num_descriptions_printed++;
131138
}
132139

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Checks how we print the developer note "hwasan_dev_note_heap_rb_distance".
2+
// RUN: %clang_hwasan %s -o %t
3+
// RUN: not %run %t 10 2>&1 | FileCheck %s --check-prefix=D10
4+
// RUN: not %run %t 42 2>&1 | FileCheck %s --check-prefix=D42
5+
6+
// REQUIRES: stable-runtime
7+
8+
#include <stdlib.h>
9+
#include <stdio.h>
10+
#include <sanitizer/hwasan_interface.h>
11+
12+
13+
void *p[100];
14+
15+
int main(int argc, char **argv) {
16+
__hwasan_enable_allocator_tagging();
17+
int distance = argc >= 2 ? atoi(argv[1]) : 1;
18+
for (int i = 0; i < 100; i++)
19+
p[i] = malloc(i);
20+
for (int i = 0; i < 100; i++)
21+
free(p[i]);
22+
23+
*(int*)p[distance] = 0;
24+
}
25+
26+
// D10: hwasan_dev_note_heap_rb_distance: 90 1023
27+
// D42: hwasan_dev_note_heap_rb_distance: 58 1023

0 commit comments

Comments
 (0)