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
5
6 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -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 -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address | FileCheck -check-prefix=ASAN %s
8
9 // RUN: echo "fun:*BlacklistedFunction*" > %t.func.blacklist
10 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address -fsanitize-blacklist=%t.func.blacklist | FileCheck -check-prefix=BLFUNC %s
11
12 // RUN: echo "src:%s" > %t.file.blacklist
13 // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin -emit-llvm -o - %s -include %t.extra-source.cpp -fsanitize=address -fsanitize-blacklist=%t.file.blacklist | FileCheck -check-prefix=BLFILE %s
14
15 // FIXME: %t.file.blacklist is like "src:x:\path\to\clang\test\CodeGen\address-safety-attr.cpp"
16 // REQUIRES: shell
17
18 // The sanitize_address attribute should be attached to functions
19 // when AddressSanitizer is enabled, unless no_sanitize_address attribute
20 // is present.
21
22 // Attributes for function defined in different source file:
23 // WITHOUT: DefinedInDifferentFile{{.*}} [[NOATTR:#[0-9]+]]
24 // BLFILE: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
25 // BLFUNC: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
26 // ASAN: DefinedInDifferentFile{{.*}} [[WITH:#[0-9]+]]
27
28 // Check that functions generated for global in different source file are
29 // not blacklisted.
30 // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
31 // WITHOUT: @__cxx_global_array_dtor{{.*}}[[NOATTR]]
32 // BLFILE: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
33 // BLFILE: @__cxx_global_array_dtor{{.*}}[[WITH]]
34 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
35 // BLFUNC: @__cxx_global_array_dtor{{.*}}[[WITH]]
36 // ASAN: @__cxx_global_var_init{{.*}}[[WITH:#[0-9]+]]
37 // ASAN: @__cxx_global_array_dtor{{.*}}[[WITH]]
38
39
40 // WITHOUT: NoAddressSafety1{{.*}}) [[NOATTR]]
41 // BLFILE: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
42 // BLFUNC: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
43 // ASAN: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
44 __attribute__((no_sanitize_address))
NoAddressSafety1(int * a)45 int NoAddressSafety1(int *a) { return *a; }
46
47 // WITHOUT: NoAddressSafety2{{.*}}) [[NOATTR]]
48 // BLFILE: NoAddressSafety2{{.*}}) [[NOATTR]]
49 // BLFUNC: NoAddressSafety2{{.*}}) [[NOATTR]]
50 // ASAN: NoAddressSafety2{{.*}}) [[NOATTR]]
51 __attribute__((no_sanitize_address))
52 int NoAddressSafety2(int *a);
NoAddressSafety2(int * a)53 int NoAddressSafety2(int *a) { return *a; }
54
55 // WITHOUT: NoAddressSafety3{{.*}}) [[NOATTR]]
56 // BLFILE: NoAddressSafety3{{.*}}) [[NOATTR]]
57 // BLFUNC: NoAddressSafety3{{.*}}) [[NOATTR]]
58 // ASAN: NoAddressSafety3{{.*}}) [[NOATTR]]
59 [[gnu::no_sanitize_address]]
NoAddressSafety3(int * a)60 int NoAddressSafety3(int *a) { return *a; }
61
62 // WITHOUT: NoAddressSafety4{{.*}}) [[NOATTR]]
63 // BLFILE: NoAddressSafety4{{.*}}) [[NOATTR]]
64 // BLFUNC: NoAddressSafety4{{.*}}) [[NOATTR]]
65 // ASAN: NoAddressSafety4{{.*}}) [[NOATTR]]
66 [[gnu::no_sanitize_address]]
67 int NoAddressSafety4(int *a);
NoAddressSafety4(int * a)68 int NoAddressSafety4(int *a) { return *a; }
69
70 // WITHOUT: NoAddressSafety5{{.*}}) [[NOATTR]]
71 // BLFILE: NoAddressSafety5{{.*}}) [[NOATTR]]
72 // BLFUNC: NoAddressSafety5{{.*}}) [[NOATTR]]
73 // ASAN: NoAddressSafety5{{.*}}) [[NOATTR]]
74 __attribute__((no_sanitize("address")))
NoAddressSafety5(int * a)75 int NoAddressSafety5(int *a) { return *a; }
76
77 // WITHOUT: NoAddressSafety6{{.*}}) [[NOATTR]]
78 // BLFILE: NoAddressSafety6{{.*}}) [[NOATTR]]
79 // BLFUNC: NoAddressSafety6{{.*}}) [[NOATTR]]
80 // ASAN: NoAddressSafety6{{.*}}) [[NOATTR]]
81 __attribute__((no_sanitize("address")))
82 int NoAddressSafety6(int *a);
NoAddressSafety6(int * a)83 int NoAddressSafety6(int *a) { return *a; }
84
85 // WITHOUT: AddressSafetyOk{{.*}}) [[NOATTR]]
86 // BLFILE: AddressSafetyOk{{.*}}) [[NOATTR]]
87 // BLFUNC: AddressSafetyOk{{.*}}) [[WITH]]
88 // ASAN: AddressSafetyOk{{.*}}) [[WITH]]
AddressSafetyOk(int * a)89 int AddressSafetyOk(int *a) { return *a; }
90
91 // WITHOUT: BlacklistedFunction{{.*}}) [[NOATTR]]
92 // BLFILE: BlacklistedFunction{{.*}}) [[NOATTR]]
93 // BLFUNC: BlacklistedFunction{{.*}}) [[NOATTR]]
94 // ASAN: BlacklistedFunction{{.*}}) [[WITH]]
BlacklistedFunction(int * a)95 int BlacklistedFunction(int *a) { return *a; }
96
97 #define GENERATE_FUNC(name) \
98 int name(int *a) { return *a; }
99 // WITHOUT: GeneratedFunction{{.*}}) [[NOATTR]]
100 // BLFILE: GeneratedFunction{{.*}}) [[NOATTR]]
101 // BLFUNC: GeneratedFunction{{.*}}) [[WITH]]
102 // ASAN: GeneratedFunction{{.*}}) [[WITH]]
GENERATE_FUNC(GeneratedFunction)103 GENERATE_FUNC(GeneratedFunction)
104
105 #define GENERATE_NAME(name) name##_generated
106 // WITHOUT: Function_generated{{.*}}) [[NOATTR]]
107 // BLFILE: Function_generated{{.*}}) [[NOATTR]]
108 // BLFUNC: Function_generated{{.*}}) [[WITH]]
109 // ASAN: Function_generated{{.*}}) [[WITH]]
110 int GENERATE_NAME(Function)(int *a) { return *a; }
111
112 // WITHOUT: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
113 // BLFILE: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
114 // BLFUNC: TemplateAddressSafetyOk{{.*}}) [[WITH]]
115 // ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]]
116 template<int i>
TemplateAddressSafetyOk()117 int TemplateAddressSafetyOk() { return i; }
118
119 // WITHOUT: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
120 // BLFILE: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
121 // BLFUNC: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
122 // ASAN: TemplateNoAddressSafety1{{.*}}) [[NOATTR]]
123 template<int i>
124 __attribute__((no_sanitize_address))
TemplateNoAddressSafety1()125 int TemplateNoAddressSafety1() { return i; }
126
127 // WITHOUT: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
128 // BLFILE: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
129 // BLFUNC: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
130 // ASAN: TemplateNoAddressSafety2{{.*}}) [[NOATTR]]
131 template<int i>
132 __attribute__((no_sanitize("address")))
TemplateNoAddressSafety2()133 int TemplateNoAddressSafety2() { return i; }
134
135 int force_instance = TemplateAddressSafetyOk<42>()
136 + TemplateNoAddressSafety1<42>()
137 + TemplateNoAddressSafety2<42>();
138
139 // Check that __cxx_global_var_init* get the sanitize_address attribute.
140 int global1 = 0;
141 int global2 = *(int*)((char*)&global1+1);
142 // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR]]
143 // BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR:#[0-9]+]]
144 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH]]
145 // ASAN: @__cxx_global_var_init{{.*}}[[WITH]]
146
147 // WITHOUT: attributes [[NOATTR]] = { nounwind{{.*}} }
148
149 // BLFILE: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
150 // BLFILE: attributes [[NOATTR]] = { nounwind{{.*}} }
151
152 // BLFUNC: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
153 // BLFUNC: attributes [[NOATTR]] = { nounwind{{.*}} }
154
155 // ASAN: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
156 // ASAN: attributes [[NOATTR]] = { nounwind{{.*}} }
157