• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 -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
8 
9 // RUN: echo "fun:*BlacklistedFunction*" > %t.func.blacklist
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-blacklist=%t.func.blacklist | FileCheck -check-prefix=BLFUNC %s
11 
12 // The blacklist file uses regexps, so escape backslashes, which are common in
13 // Windows paths.
14 // RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t.file.blacklist
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-blacklist=%t.file.blacklist | FileCheck -check-prefix=BLFILE %s
16 
17 // The sanitize_address attribute should be attached to functions
18 // when AddressSanitizer is enabled, unless no_sanitize_address attribute
19 // is present.
20 
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]+]]
26 
27 // Check that functions generated for global in different source file are
28 // not blacklisted.
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]]
37 
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))
NoAddressSafety1(int * a)43 int NoAddressSafety1(int *a) { return *a; }
44 
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);
NoAddressSafety2(int * a)51 int NoAddressSafety2(int *a) { return *a; }
52 
53 // WITHOUT:  NoAddressSafety3{{.*}}) [[NOATTR]]
54 // BLFILE:  NoAddressSafety3{{.*}}) [[NOATTR]]
55 // BLFUNC:  NoAddressSafety3{{.*}}) [[NOATTR]]
56 // ASAN:  NoAddressSafety3{{.*}}) [[NOATTR]]
57 [[gnu::no_sanitize_address]]
NoAddressSafety3(int * a)58 int NoAddressSafety3(int *a) { return *a; }
59 
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);
NoAddressSafety4(int * a)66 int NoAddressSafety4(int *a) { return *a; }
67 
68 // WITHOUT:  NoAddressSafety5{{.*}}) [[NOATTR]]
69 // BLFILE:  NoAddressSafety5{{.*}}) [[NOATTR]]
70 // BLFUNC:  NoAddressSafety5{{.*}}) [[NOATTR]]
71 // ASAN:  NoAddressSafety5{{.*}}) [[NOATTR]]
72 __attribute__((no_sanitize("address")))
NoAddressSafety5(int * a)73 int NoAddressSafety5(int *a) { return *a; }
74 
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);
NoAddressSafety6(int * a)81 int NoAddressSafety6(int *a) { return *a; }
82 
83 // WITHOUT:  AddressSafetyOk{{.*}}) [[NOATTR]]
84 // BLFILE:  AddressSafetyOk{{.*}}) [[NOATTR]]
85 // BLFUNC: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
86 // ASAN: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
AddressSafetyOk(int * a)87 int AddressSafetyOk(int *a) { return *a; }
88 
89 // WITHOUT:  BlacklistedFunction{{.*}}) [[NOATTR]]
90 // BLFILE:  BlacklistedFunction{{.*}}) [[NOATTR]]
91 // BLFUNC:  BlacklistedFunction{{.*}}) [[NOATTR]]
92 // ASAN:  BlacklistedFunction{{.*}}) [[WITH]]
BlacklistedFunction(int * a)93 int BlacklistedFunction(int *a) { return *a; }
94 
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]]
GENERATE_FUNC(GeneratedFunction)101 GENERATE_FUNC(GeneratedFunction)
102 
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; }
109 
110 // WITHOUT:  TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
111 // BLFILE:  TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
112 // BLFUNC:  TemplateAddressSafetyOk{{.*}}) [[WITH]]
113 // ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]]
114 template<int i>
TemplateAddressSafetyOk()115 int TemplateAddressSafetyOk() { return i; }
116 
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))
TemplateNoAddressSafety1()123 int TemplateNoAddressSafety1() { return i; }
124 
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")))
TemplateNoAddressSafety2()131 int TemplateNoAddressSafety2() { return i; }
132 
133 int force_instance = TemplateAddressSafetyOk<42>()
134                    + TemplateNoAddressSafety1<42>()
135                    + TemplateNoAddressSafety2<42>();
136 
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]+]]
144 
145 // WITHOUT: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
146 
147 // BLFILE: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
148 // BLFILE: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
149 
150 // BLFUNC: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
151 // BLFUNC: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
152 
153 // ASAN: attributes [[WITH]] = { noinline nounwind sanitize_address{{.*}} }
154 // ASAN: attributes [[NOATTR]] = { noinline nounwind{{.*}} }
155