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