[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGen / address-safety-attr.cpp
blobbd899ed2f9a7491733f61c34f11d7067e9af92ff
1 int DefinedInDifferentFile(int *a);
2 // RUN: echo "int DefinedInDifferentFile(int *a) { return *a; }" > %t.extra-source.cpp
3 // RUN: echo "struct S { S(){} ~S(){} };" >> %t.extra-source.cpp
4 // RUN: echo "S glob_array[5];" >> %t.extra-source.cpp
6 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -disable-O0-optnone -emit-llvm -o - %s -include %t.extra-source.cpp | FileCheck -check-prefix=WITHOUT %s
7 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -disable-O0-optnone -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address | FileCheck -check-prefix=ASAN %s
9 // RUN: echo "fun:*IgnorelistedFunction*" > %t.func.ignorelist
10 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -disable-O0-optnone -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address -fsanitize-ignorelist=%t.func.ignorelist | FileCheck -check-prefix=BLFUNC %s
12 // The ignorelist file uses regexps, so escape backslashes, which are common in
13 // Windows paths.
14 // RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t.file.ignorelist
15 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -disable-O0-optnone -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address -fsanitize-ignorelist=%t.file.ignorelist | FileCheck -check-prefix=BLFILE %s
17 // The sanitize_address attribute should be attached to functions
18 // when AddressSanitizer is enabled, unless no_sanitize_address attribute
19 // is present.
21 // Attributes for function defined in different source file:
22 // WITHOUT: DefinedInDifferentFile{{.*}} [[NOATTR:#[0-9]+]]
23 // BLFILE: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
24 // BLFUNC: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
25 // ASAN: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
27 // Check that functions generated for global in different source file are
28 // not ignorelisted.
29 // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
30 // WITHOUT: @__cxx_global_array_dtor{{.*}}[[NOATTR]]
31 // BLFILE: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
32 // BLFILE: @__cxx_global_array_dtor{{.*}}[[WITH]]
33 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
34 // BLFUNC: @__cxx_global_array_dtor{{.*}}[[WITH]]
35 // ASAN: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
36 // ASAN: @__cxx_global_array_dtor{{.*}}[[WITH]]
38 // WITHOUT: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
39 // BLFILE: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
40 // BLFUNC: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
41 // ASAN: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
42 __attribute__((no_sanitize_address))
43 int NoAddressSafety1(int *a) { return *a; }
45 // WITHOUT: NoAddressSafety2{{.*}}) [[NOATTR]]
46 // BLFILE: NoAddressSafety2{{.*}}) [[NOATTR]]
47 // BLFUNC: NoAddressSafety2{{.*}}) [[NOATTR]]
48 // ASAN: NoAddressSafety2{{.*}}) [[NOATTR]]
49 __attribute__((no_sanitize_address))
50 int NoAddressSafety2(int *a);
51 int NoAddressSafety2(int *a) { return *a; }
53 // WITHOUT: NoAddressSafety3{{.*}}) [[NOATTR]]
54 // BLFILE: NoAddressSafety3{{.*}}) [[NOATTR]]
55 // BLFUNC: NoAddressSafety3{{.*}}) [[NOATTR]]
56 // ASAN: NoAddressSafety3{{.*}}) [[NOATTR]]
57 [[gnu::no_sanitize_address]]
58 int NoAddressSafety3(int *a) { return *a; }
60 // WITHOUT: NoAddressSafety4{{.*}}) [[NOATTR]]
61 // BLFILE: NoAddressSafety4{{.*}}) [[NOATTR]]
62 // BLFUNC: NoAddressSafety4{{.*}}) [[NOATTR]]
63 // ASAN: NoAddressSafety4{{.*}}) [[NOATTR]]
64 [[gnu::no_sanitize_address]]
65 int NoAddressSafety4(int *a);
66 int NoAddressSafety4(int *a) { return *a; }
68 // WITHOUT: NoAddressSafety5{{.*}}) [[NOATTR]]
69 // BLFILE: NoAddressSafety5{{.*}}) [[NOATTR]]
70 // BLFUNC: NoAddressSafety5{{.*}}) [[NOATTR]]
71 // ASAN: NoAddressSafety5{{.*}}) [[NOATTR]]
72 __attribute__((no_sanitize("address")))
73 int NoAddressSafety5(int *a) { return *a; }
75 // WITHOUT: NoAddressSafety6{{.*}}) [[NOATTR]]
76 // BLFILE: NoAddressSafety6{{.*}}) [[NOATTR]]
77 // BLFUNC: NoAddressSafety6{{.*}}) [[NOATTR]]
78 // ASAN: NoAddressSafety6{{.*}}) [[NOATTR]]
79 __attribute__((no_sanitize("address")))
80 int NoAddressSafety6(int *a);
81 int NoAddressSafety6(int *a) { return *a; }
83 // WITHOUT: AddressSafetyOk{{.*}}) [[NOATTR]]
84 // BLFILE: AddressSafetyOk{{.*}}) [[NOATTR]]
85 // BLFUNC: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
86 // ASAN: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
87 int AddressSafetyOk(int *a) { return *a; }
89 // WITHOUT: IgnorelistedFunction{{.*}}) [[NOATTR]]
90 // BLFILE: IgnorelistedFunction{{.*}}) [[NOATTR]]
91 // BLFUNC: IgnorelistedFunction{{.*}}) [[NOATTR]]
92 // ASAN: IgnorelistedFunction{{.*}}) [[WITH]]
93 int IgnorelistedFunction(int *a) { return *a; }
95 #define GENERATE_FUNC(name) \
96 int name(int *a) { return *a; }
97 // WITHOUT: GeneratedFunction{{.*}}) [[NOATTR]]
98 // BLFILE: GeneratedFunction{{.*}}) [[NOATTR]]
99 // BLFUNC: GeneratedFunction{{.*}}) [[WITH]]
100 // ASAN: GeneratedFunction{{.*}}) [[WITH]]
101 GENERATE_FUNC(GeneratedFunction)
103 #define GENERATE_NAME(name) name##_generated
104 // WITHOUT: Function_generated{{.*}}) [[NOATTR]]
105 // BLFILE: Function_generated{{.*}}) [[NOATTR]]
106 // BLFUNC: Function_generated{{.*}}) [[WITH]]
107 // ASAN: Function_generated{{.*}}) [[WITH]]
108 int GENERATE_NAME(Function)(int *a) { return *a; }
110 // WITHOUT: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
111 // BLFILE: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
112 // BLFUNC: TemplateAddressSafetyOk{{.*}}) [[WITH]]
113 // ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]]
114 template<int i>
115 int TemplateAddressSafetyOk() { return i; }
117 // WITHOUT: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
118 // BLFILE: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
119 // BLFUNC: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
120 // ASAN: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
121 template<int i>
122 __attribute__((no_sanitize_address))
123 int TemplateNoAddressSafety1() { return i; }
125 // WITHOUT: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
126 // BLFILE: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
127 // BLFUNC: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
128 // ASAN: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
129 template<int i>
130 __attribute__((no_sanitize("address")))
131 int TemplateNoAddressSafety2() { return i; }
133 int force_instance = TemplateAddressSafetyOk<42>()
134 + TemplateNoAddressSafety1<42>()
135 + TemplateNoAddressSafety2<42>();
137 // Check that __cxx_global_var_init* get the sanitize_address attribute.
138 int global1 = 0;
139 int global2 = *(int*)((char*)&global1+1);
140 // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
141 // BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
142 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
143 // ASAN: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
145 // WITHOUT: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
147 // BLFILE: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
148 // BLFILE: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
150 // BLFUNC: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
151 // BLFUNC: attributes [[NOATTR]] = { mustprogress noinline nounwind{{.*}} }
153 // ASAN: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
154 // ASAN: attributes [[NOATTR]] = { mustprogress noinline nounwind{{.*}} }