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