1 // Checks the ASan memory address type debugging API, makes sure it returns
2 // the correct memory type for heap, stack, global and shadow addresses and
3 // that it correctly finds out which region (and name and size) the address
5 // RUN: %clangxx_asan -O0 %s -o %t && %run %t 2>&1
8 #include <sanitizer/asan_interface.h>
17 char *heap_ptr
= (char *)malloc(10);
24 type
= __asan_locate_address(&global_var
, name
, 100,
25 ®ion_address
, ®ion_size
);
26 assert(nullptr != strstr(name
, "global_var"));
27 assert(0 == strcmp(type
, "global"));
28 assert(region_address
== &global_var
);
29 assert(region_size
== sizeof(global_var
));
31 type
= __asan_locate_address((char *)(&global_var
)+1, name
, 100,
32 ®ion_address
, ®ion_size
);
33 assert(nullptr != strstr(name
, "global_var"));
34 assert(0 == strcmp(type
, "global"));
35 assert(region_address
== &global_var
);
36 assert(region_size
== sizeof(global_var
));
38 type
= __asan_locate_address(&local_var
, name
, 100,
39 ®ion_address
, ®ion_size
);
40 assert(0 == strcmp(name
, "local_var"));
41 assert(0 == strcmp(type
, "stack"));
42 assert(region_address
== &local_var
);
43 assert(region_size
== sizeof(local_var
));
45 type
= __asan_locate_address((char *)(&local_var
)+1, name
, 100,
46 ®ion_address
, ®ion_size
);
47 assert(0 == strcmp(name
, "local_var"));
48 assert(0 == strcmp(type
, "stack"));
49 assert(region_address
== &local_var
);
50 assert(region_size
== sizeof(local_var
));
52 type
= __asan_locate_address(heap_ptr
, name
, 100,
53 ®ion_address
, ®ion_size
);
54 assert(0 == strcmp(type
, "heap"));
55 assert(region_address
== heap_ptr
);
56 assert(10 == region_size
);
58 type
= __asan_locate_address(heap_ptr
+1, name
, 100,
59 ®ion_address
, ®ion_size
);
60 assert(0 == strcmp(type
, "heap"));
61 assert(region_address
== heap_ptr
);
62 assert(10 == region_size
);
66 __asan_get_shadow_mapping(&shadow_scale
, &shadow_offset
);
68 uintptr_t shadow_ptr
= (((uintptr_t)heap_ptr
) >> shadow_scale
)
70 type
= __asan_locate_address((void *)shadow_ptr
, NULL
, 0, NULL
, NULL
);
71 assert((0 == strcmp(type
, "high shadow")) || 0 == strcmp(type
, "low shadow"));
73 uintptr_t shadow_gap
= (shadow_ptr
>> shadow_scale
) + shadow_offset
;
74 type
= __asan_locate_address((void *)shadow_gap
, NULL
, 0, NULL
, NULL
);
75 assert(0 == strcmp(type
, "shadow gap"));