1 // RUN: echo "int extra_global;" > %t.extra-source.cpp
2 // RUN: echo "global:*ignorelisted_global*" > %t.ignorelist
3 // RUN: %clang_cc1 -include %t.extra-source.cpp -fsanitize=address -fsanitize-ignorelist=%t.ignorelist -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,GLOBS,ASAN
4 // RUN: %clang_cc1 -include %t.extra-source.cpp -fsanitize=kernel-address -fsanitize-ignorelist=%t.ignorelist -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,GLOBS,KASAN
5 // The ignorelist file uses regexps, so Windows path backslashes.
6 // RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t.ignorelist-src
7 // RUN: %clang_cc1 -include %t.extra-source.cpp -fsanitize=address -fsanitize-ignorelist=%t.ignorelist-src -emit-llvm -o - %s | FileCheck %s --check-prefix=IGNORELIST-SRC
8 // RUN: %clang_cc1 -include %t.extra-source.cpp -fsanitize=kernel-address -fsanitize-ignorelist=%t.ignorelist-src -emit-llvm -o - %s | FileCheck %s --check-prefix=IGNORELIST-SRC
9 
10 int global;
11 int dyn_init_global = global;
12 int __attribute__((no_sanitize("address"))) attributed_global;
13 int __attribute__((disable_sanitizer_instrumentation)) disable_instrumentation_global;
14 int ignorelisted_global;
15 
16 int __attribute__((section("__DATA, __common"))) sectioned_global; // KASAN - ignore globals in a section
17 extern "C" {
18 int __special_global; // KASAN - ignore globals with __-prefix
19 }
20 
21 void func() {
22   static int static_var = 0;
23   const char *literal = "Hello, world!";
24 }
25 
26 // GLOBS:     @{{.*}}extra_global{{.*}} ={{.*}} global
27 // GLOBS-NOT: no_sanitize_address
28 // GLOBS:     @{{.*}}global{{.*}} ={{.*}} global
29 // GLOBS-NOT: no_sanitize_address
30 // GLOBS:     @{{.*}}dyn_init_global{{.*}} ={{.*}} global {{.*}}, sanitize_address_dyninit
31 // GLOBS-NOT: no_sanitize_address
32 
33 // GLOBS:     @{{.*}}attributed_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
34 // GLOBS:     @{{.*}}disable_instrumentation_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
35 // GLOBS:     @{{.*}}ignorelisted_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
36 
37 // ASAN:     @{{.*}}sectioned_global{{.*}} ={{.*}} global { i32, [28 x i8] }{{.*}}, align 32
38 // ASAN-NOT: no_sanitize_address
39 // ASAN:     @{{.*}}__special_global{{.*}} ={{.*}} global { i32, [28 x i8] }{{.*}}, align 32
40 // ASAN-NOT: no_sanitize_address
41 
42 /// Note: No attribute is added by the IR pass, but the type didn't change, so
43 /// that checks our assertions that the globals didn't get instrumented.
44 // KASAN:    @{{.*}}sectioned_global{{.*}} ={{.*}} global i32 {{.*}}
45 // KASAN:    @{{.*}}__special_global{{.*}} ={{.*}} global i32 {{.*}}
46 
47 // GLOBS:     @{{[^ ]*}}static_var{{[^ ]*}} ={{.*}} global {{.*}}
48 // GLOBS-NOT: no_sanitize_address
49 // GLOBS:     @{{.*}} = {{.*}}c"Hello, world!\00"
50 // GLOBS-NOT: no_sanitize_address
51 
52 /// Without -fasynchronous-unwind-tables, ctor and dtor get the uwtable attribute.
53 // CHECK-LABEL: define internal void @asan.module_ctor() #[[#ATTR:]] {
54 // ASAN-NEXT: call void @__asan_init
55 // ASAN-NEXT: call void @__asan_version_mismatch_check
56 // KASAN-NOT: call void @__asan_init
57 // KASAN-NOT: call void @__asan_version_mismatch_check
58 // ASAN-NEXT: call void @__asan_register_globals({{.*}}, i{{32|64}} 7)
59 // KASAN-NEXT: call void @__asan_register_globals({{.*}}, i{{32|64}} 5)
60 // CHECK-NEXT: ret void
61 
62 // CHECK:      define internal void @asan.module_dtor() #[[#ATTR]] {
63 // CHECK-NEXT: call void @__asan_unregister_globals
64 // CHECK-NEXT: ret void
65 
66 // CHECK: attributes #[[#ATTR]] = { nounwind }
67 
68 /// If -fasynchronous-unwind-tables, set the module flag "uwtable". ctor/dtor
69 /// will thus get the uwtable attribute.
70 // RUN: %clang_cc1 -emit-llvm -fsanitize=address -funwind-tables=2 -o - %s | FileCheck %s --check-prefixes=UWTABLE
71 // UWTABLE: define internal void @asan.module_dtor() #[[#ATTR:]] {
72 // UWTABLE: attributes #[[#ATTR]] = { nounwind uwtable }
73 // UWTABLE: ![[#]] = !{i32 7, !"uwtable", i32 2}
74 
75 // IGNORELIST-SRC:     @{{.*}}extra_global{{.*}} ={{.*}} global
76 // IGNORELIST-SRC-NOT: no_sanitize_address
77 // IGNORELIST-SRC:     @{{.*}}global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
78 // IGNORELIST-SRC:     @{{.*}}dyn_init_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
79 // IGNORELIST-SRC:     @{{.*}}attributed_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
80 // IGNORELIST-SRC:     @{{.*}}disable_instrumentation_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
81 // IGNORELIST-SRC:     @{{.*}}ignorelisted_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
82 // IGNORELIST-SRC:     @{{.*}}sectioned_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
83 // IGNORELIST-SRC:     @{{.*}}__special_global{{.*}} ={{.*}} global {{.*}} no_sanitize_address
84 // IGNORELIST-SRC:     @{{.*}}static_var{{.*}} ={{.*}} global {{.*}} no_sanitize_address
85 // IGNORELIST-SRC:     @{{.*}} ={{.*}} c"Hello, world!\00"{{.*}} no_sanitize_address
86