Skip to content

Commit f65e1ae

Browse files
committed
[compiler-rt][asan] Make wild-pointer crash error more useful
Right now, when you have an invalid memory address, asan would just crash and does not offer much useful info. This patch attempted to give a bit more detail on the access. Differential Revision: https://reviews.llvm.org/D98280
1 parent 11b70b9 commit f65e1ae

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

compiler-rt/lib/asan/asan_descriptions.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ static bool GetShadowKind(uptr addr, ShadowKind *shadow_kind) {
7777
} else if (AddrIsInLowShadow(addr)) {
7878
*shadow_kind = kShadowKindLow;
7979
} else {
80-
CHECK(0 && "Address is not in memory and not in shadow?");
8180
return false;
8281
}
8382
return true;
@@ -464,7 +463,13 @@ AddressDescription::AddressDescription(uptr addr, uptr access_size,
464463
return;
465464
}
466465
data.kind = kAddressKindWild;
467-
addr = 0;
466+
data.wild.addr = addr;
467+
data.wild.access_size = access_size;
468+
}
469+
470+
void WildAddressDescription::Print() const {
471+
Printf("Address %p is a wild pointer inside of access range of size %p.\n",
472+
addr, access_size);
468473
}
469474

470475
void PrintAddressDescription(uptr addr, uptr access_size,

compiler-rt/lib/asan/asan_descriptions.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ struct StackAddressDescription {
146146
bool GetStackAddressInformation(uptr addr, uptr access_size,
147147
StackAddressDescription *descr);
148148

149+
struct WildAddressDescription {
150+
uptr addr;
151+
uptr access_size;
152+
153+
void Print() const;
154+
};
155+
149156
struct GlobalAddressDescription {
150157
uptr addr;
151158
// Assume address is close to at most four globals.
@@ -193,7 +200,7 @@ class AddressDescription {
193200
HeapAddressDescription heap;
194201
StackAddressDescription stack;
195202
GlobalAddressDescription global;
196-
uptr addr;
203+
WildAddressDescription wild;
197204
};
198205
};
199206

@@ -211,7 +218,7 @@ class AddressDescription {
211218
uptr Address() const {
212219
switch (data.kind) {
213220
case kAddressKindWild:
214-
return data.addr;
221+
return data.wild.addr;
215222
case kAddressKindShadow:
216223
return data.shadow.addr;
217224
case kAddressKindHeap:
@@ -226,7 +233,7 @@ class AddressDescription {
226233
void Print(const char *bug_descr = nullptr) const {
227234
switch (data.kind) {
228235
case kAddressKindWild:
229-
Printf("Address %p is a wild pointer.\n", data.addr);
236+
data.wild.Print();
230237
return;
231238
case kAddressKindShadow:
232239
return data.shadow.Print();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clangxx_asan %s -o %t
2+
// RUN: not %run %t 2>&1 | FileCheck %s
3+
// REQUIRES: asan-64-bits
4+
5+
#include <stdarg.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
9+
int main() {
10+
char *p = new char;
11+
char *dest = new char;
12+
const size_t offset = 0x4567890123456789;
13+
// Flush it so the output came out before the asan report.
14+
fprintf(stderr, "Expected bad addr: %p\n", p + offset);
15+
fflush(stderr);
16+
memmove(dest, p, offset);
17+
return 0;
18+
}
19+
20+
// CHECK: Expected bad addr: [[ADDR:0x[0-9,a-f]+]]
21+
// CHECK: AddressSanitizer: unknown-crash on address [[ADDR]]
22+
// CHECK: Address [[ADDR]] is a wild pointer inside of access range of size 0x4567890123456789

0 commit comments

Comments
 (0)