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 -triple x86_64-apple-darwin -emit-llvm -o - %s -include %t.extra-source.cpp | FileCheck -check-prefix=WITHOUT %s
7 // RUN: %clang_cc1 -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 -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 -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_NO_TF:#[0-9]+]]
31 // WITHOUT: @__cxx_global_array_dtor{{.*}}[[NOATTR_NO_TF]]
32 // BLFILE: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]]
33 // BLFILE: @__cxx_global_array_dtor{{.*}}[[WITH_NO_TF]]
34 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]]
35 // BLFUNC: @__cxx_global_array_dtor{{.*}}[[WITH_NO_TF]]
36 // ASAN: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]]
37 // ASAN: @__cxx_global_array_dtor{{.*}}[[WITH_NO_TF]]
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: AddressSafetyOk{{.*}}) [[NOATTR]]
56 // BLFILE: AddressSafetyOk{{.*}}) [[NOATTR]]
57 // BLFUNC: AddressSafetyOk{{.*}}) [[WITH]]
58 // ASAN: AddressSafetyOk{{.*}}) [[WITH]]
AddressSafetyOk(int * a)59 int AddressSafetyOk(int *a) { return *a; }
60
61 // WITHOUT: BlacklistedFunction{{.*}}) [[NOATTR]]
62 // BLFILE: BlacklistedFunction{{.*}}) [[NOATTR]]
63 // BLFUNC: BlacklistedFunction{{.*}}) [[NOATTR]]
64 // ASAN: BlacklistedFunction{{.*}}) [[WITH]]
BlacklistedFunction(int * a)65 int BlacklistedFunction(int *a) { return *a; }
66
67 #define GENERATE_FUNC(name) \
68 int name(int *a) { return *a; }
69 // WITHOUT: GeneratedFunction{{.*}}) [[NOATTR]]
70 // BLFILE: GeneratedFunction{{.*}}) [[NOATTR]]
71 // BLFUNC: GeneratedFunction{{.*}}) [[WITH]]
72 // ASAN: GeneratedFunction{{.*}}) [[WITH]]
GENERATE_FUNC(GeneratedFunction)73 GENERATE_FUNC(GeneratedFunction)
74
75 #define GENERATE_NAME(name) name##_generated
76 // WITHOUT: Function_generated{{.*}}) [[NOATTR]]
77 // BLFILE: Function_generated{{.*}}) [[NOATTR]]
78 // BLFUNC: Function_generated{{.*}}) [[WITH]]
79 // ASAN: Function_generated{{.*}}) [[WITH]]
80 int GENERATE_NAME(Function)(int *a) { return *a; }
81
82 // WITHOUT: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
83 // BLFILE: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
84 // BLFUNC: TemplateAddressSafetyOk{{.*}}) [[WITH]]
85 // ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]]
86 template<int i>
TemplateAddressSafetyOk()87 int TemplateAddressSafetyOk() { return i; }
88
89 // WITHOUT: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
90 // BLFILE: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
91 // BLFUNC: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
92 // ASAN: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
93 template<int i>
94 __attribute__((no_sanitize_address))
TemplateNoAddressSafety()95 int TemplateNoAddressSafety() { return i; }
96
97 int force_instance = TemplateAddressSafetyOk<42>()
98 + TemplateNoAddressSafety<42>();
99
100 // Check that __cxx_global_var_init* get the sanitize_address attribute.
101 int global1 = 0;
102 int global2 = *(int*)((char*)&global1+1);
103 // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF]]
104 // BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF:#[0-9]+]]
105 // BLFUNC: @__cxx_global_var_init{{.*}}[[WITH_NO_TF]]
106 // ASAN: @__cxx_global_var_init{{.*}}[[WITH_NO_TF]]
107
108 // WITHOUT: attributes [[NOATTR]] = { nounwind{{.*}} }
109 // WITHOUT: attributes [[NOATTR_NO_TF]] = { nounwind }
110
111 // BLFILE: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
112 // BLFILE: attributes [[WITH_NO_TF]] = { nounwind sanitize_address }
113 // BLFILE: attributes [[NOATTR_NO_TF]] = { nounwind }
114 // BLFILE: attributes [[NOATTR]] = { nounwind{{.*}} }
115
116 // BLFUNC: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
117 // BLFUNC: attributes [[WITH_NO_TF]] = { nounwind sanitize_address }
118 // BLFUNC: attributes [[NOATTR]] = { nounwind{{.*}} }
119
120 // ASAN: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
121 // ASAN: attributes [[WITH_NO_TF]] = { nounwind sanitize_address }
122 // ASAN: attributes [[NOATTR]] = { nounwind{{.*}} }
123