debug_locate.cpp
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Checks the ASan memory address type debugging API, makes sure it returns
// the correct memory type for heap, stack, global and shadow addresses and
// that it correctly finds out which region (and name and size) the address
// belongs to.
// RUN: %clangxx_asan -O0 %s -o %t && %run %t 2>&1
#include <assert.h>
#include <sanitizer/asan_interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int global_var;
int main() {
int local_var;
char *heap_ptr = (char *)malloc(10);
char name[100];
void *region_address;
size_t region_size;
const char *type;
type = __asan_locate_address(&global_var, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(name, "global_var"));
assert(0 == strcmp(type, "global"));
assert(region_address == &global_var);
assert(region_size == sizeof(global_var));
type = __asan_locate_address((char *)(&global_var)+1, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(name, "global_var"));
assert(0 == strcmp(type, "global"));
assert(region_address == &global_var);
assert(region_size == sizeof(global_var));
type = __asan_locate_address(&local_var, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(name, "local_var"));
assert(0 == strcmp(type, "stack"));
assert(region_address == &local_var);
assert(region_size == sizeof(local_var));
type = __asan_locate_address((char *)(&local_var)+1, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(name, "local_var"));
assert(0 == strcmp(type, "stack"));
assert(region_address == &local_var);
assert(region_size == sizeof(local_var));
type = __asan_locate_address(heap_ptr, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(type, "heap"));
assert(region_address == heap_ptr);
assert(10 == region_size);
type = __asan_locate_address(heap_ptr+1, name, 100,
®ion_address, ®ion_size);
assert(0 == strcmp(type, "heap"));
assert(region_address == heap_ptr);
assert(10 == region_size);
size_t shadow_scale;
size_t shadow_offset;
__asan_get_shadow_mapping(&shadow_scale, &shadow_offset);
uintptr_t shadow_ptr = (((uintptr_t)heap_ptr) >> shadow_scale)
+ shadow_offset;
type = __asan_locate_address((void *)shadow_ptr, NULL, 0, NULL, NULL);
assert((0 == strcmp(type, "high shadow")) || 0 == strcmp(type, "low shadow"));
uintptr_t shadow_gap = (shadow_ptr >> shadow_scale) + shadow_offset;
type = __asan_locate_address((void *)shadow_gap, NULL, 0, NULL, NULL);
assert(0 == strcmp(type, "shadow gap"));
free(heap_ptr);
return 0;
}