1 // RUN: %clang_asan -O0 %s -o %t
2 // RUN: %run %t 0x00 2>&1 | FileCheck %s -check-prefix=X00
3 // RUN: not %run %t 0xf1 2>&1 | FileCheck %s -check-prefix=XF1
4 // RUN: not %run %t 0xf2 2>&1 | FileCheck %s -check-prefix=XF2
5 // RUN: not %run %t 0xf3 2>&1 | FileCheck %s -check-prefix=XF3
6 // RUN: not %run %t 0xf5 2>&1 | FileCheck %s -check-prefix=XF5
7 // RUN: not %run %t 0xf8 2>&1 | FileCheck %s -check-prefix=XF8
8
9 #include <assert.h>
10 #include <sanitizer/asan_interface.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 void __asan_set_shadow_00(size_t addr, size_t size);
16 void __asan_set_shadow_f1(size_t addr, size_t size);
17 void __asan_set_shadow_f2(size_t addr, size_t size);
18 void __asan_set_shadow_f3(size_t addr, size_t size);
19 void __asan_set_shadow_f5(size_t addr, size_t size);
20 void __asan_set_shadow_f8(size_t addr, size_t size);
21
22 char* a;
23
f(long arg)24 void f(long arg) {
25 size_t shadow_offset;
26 size_t shadow_scale;
27 __asan_get_shadow_mapping(&shadow_scale, &shadow_offset);
28 size_t addr = (((size_t)a) >> shadow_scale) + shadow_offset;
29
30 switch (arg) {
31 // X00-NOT: AddressSanitizer
32 // X00: PASS
33 case 0x00:
34 return __asan_set_shadow_00(addr, 1);
35 // XF1: AddressSanitizer: stack-buffer-underflow
36 // XF1: [f1]
37 case 0xf1:
38 return __asan_set_shadow_f1(addr, 1);
39 // XF2: AddressSanitizer: stack-buffer-overflow
40 // XF2: [f2]
41 case 0xf2:
42 return __asan_set_shadow_f2(addr, 1);
43 // XF3: AddressSanitizer: stack-buffer-overflow
44 // XF3: [f3]
45 case 0xf3:
46 return __asan_set_shadow_f3(addr, 1);
47 // XF5: AddressSanitizer: stack-use-after-return
48 // XF5: [f5]
49 case 0xf5:
50 return __asan_set_shadow_f5(addr, 1);
51 // XF8: AddressSanitizer: stack-use-after-scope
52 // XF8: [f8]
53 case 0xf8:
54 return __asan_set_shadow_f8(addr, 1);
55 }
56 assert(0);
57 }
58
main(int argc,char ** argv)59 int main(int argc, char **argv) {
60 assert(argc > 1);
61
62 a = malloc(8);
63 long arg = strtol(argv[1], 0, 16);
64 f(arg);
65 *a = 1;
66 printf("PASS\n");
67 return 0;
68 }
69