annotate gcc/asan.c @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* AddressSanitizer, a fast memory error detector.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Kostya Serebryany <kcc@google.com>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 #include "config.h"
kono
parents:
diff changeset
23 #include "system.h"
kono
parents:
diff changeset
24 #include "coretypes.h"
kono
parents:
diff changeset
25 #include "backend.h"
kono
parents:
diff changeset
26 #include "target.h"
kono
parents:
diff changeset
27 #include "rtl.h"
kono
parents:
diff changeset
28 #include "tree.h"
kono
parents:
diff changeset
29 #include "gimple.h"
kono
parents:
diff changeset
30 #include "cfghooks.h"
kono
parents:
diff changeset
31 #include "alloc-pool.h"
kono
parents:
diff changeset
32 #include "tree-pass.h"
kono
parents:
diff changeset
33 #include "memmodel.h"
kono
parents:
diff changeset
34 #include "tm_p.h"
kono
parents:
diff changeset
35 #include "ssa.h"
kono
parents:
diff changeset
36 #include "stringpool.h"
kono
parents:
diff changeset
37 #include "tree-ssanames.h"
kono
parents:
diff changeset
38 #include "optabs.h"
kono
parents:
diff changeset
39 #include "emit-rtl.h"
kono
parents:
diff changeset
40 #include "cgraph.h"
kono
parents:
diff changeset
41 #include "gimple-pretty-print.h"
kono
parents:
diff changeset
42 #include "alias.h"
kono
parents:
diff changeset
43 #include "fold-const.h"
kono
parents:
diff changeset
44 #include "cfganal.h"
kono
parents:
diff changeset
45 #include "gimplify.h"
kono
parents:
diff changeset
46 #include "gimple-iterator.h"
kono
parents:
diff changeset
47 #include "varasm.h"
kono
parents:
diff changeset
48 #include "stor-layout.h"
kono
parents:
diff changeset
49 #include "tree-iterator.h"
kono
parents:
diff changeset
50 #include "stringpool.h"
kono
parents:
diff changeset
51 #include "attribs.h"
kono
parents:
diff changeset
52 #include "asan.h"
kono
parents:
diff changeset
53 #include "dojump.h"
kono
parents:
diff changeset
54 #include "explow.h"
kono
parents:
diff changeset
55 #include "expr.h"
kono
parents:
diff changeset
56 #include "output.h"
kono
parents:
diff changeset
57 #include "langhooks.h"
kono
parents:
diff changeset
58 #include "cfgloop.h"
kono
parents:
diff changeset
59 #include "gimple-builder.h"
kono
parents:
diff changeset
60 #include "gimple-fold.h"
kono
parents:
diff changeset
61 #include "ubsan.h"
kono
parents:
diff changeset
62 #include "params.h"
kono
parents:
diff changeset
63 #include "builtins.h"
kono
parents:
diff changeset
64 #include "fnmatch.h"
kono
parents:
diff changeset
65 #include "tree-inline.h"
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 /* AddressSanitizer finds out-of-bounds and use-after-free bugs
kono
parents:
diff changeset
68 with <2x slowdown on average.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 The tool consists of two parts:
kono
parents:
diff changeset
71 instrumentation module (this file) and a run-time library.
kono
parents:
diff changeset
72 The instrumentation module adds a run-time check before every memory insn.
kono
parents:
diff changeset
73 For a 8- or 16- byte load accessing address X:
kono
parents:
diff changeset
74 ShadowAddr = (X >> 3) + Offset
kono
parents:
diff changeset
75 ShadowValue = *(char*)ShadowAddr; // *(short*) for 16-byte access.
kono
parents:
diff changeset
76 if (ShadowValue)
kono
parents:
diff changeset
77 __asan_report_load8(X);
kono
parents:
diff changeset
78 For a load of N bytes (N=1, 2 or 4) from address X:
kono
parents:
diff changeset
79 ShadowAddr = (X >> 3) + Offset
kono
parents:
diff changeset
80 ShadowValue = *(char*)ShadowAddr;
kono
parents:
diff changeset
81 if (ShadowValue)
kono
parents:
diff changeset
82 if ((X & 7) + N - 1 > ShadowValue)
kono
parents:
diff changeset
83 __asan_report_loadN(X);
kono
parents:
diff changeset
84 Stores are instrumented similarly, but using __asan_report_storeN functions.
kono
parents:
diff changeset
85 A call too __asan_init_vN() is inserted to the list of module CTORs.
kono
parents:
diff changeset
86 N is the version number of the AddressSanitizer API. The changes between the
kono
parents:
diff changeset
87 API versions are listed in libsanitizer/asan/asan_interface_internal.h.
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 The run-time library redefines malloc (so that redzone are inserted around
kono
parents:
diff changeset
90 the allocated memory) and free (so that reuse of free-ed memory is delayed),
kono
parents:
diff changeset
91 provides __asan_report* and __asan_init_vN functions.
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 Read more:
kono
parents:
diff changeset
94 http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 The current implementation supports detection of out-of-bounds and
kono
parents:
diff changeset
97 use-after-free in the heap, on the stack and for global variables.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 [Protection of stack variables]
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 To understand how detection of out-of-bounds and use-after-free works
kono
parents:
diff changeset
102 for stack variables, lets look at this example on x86_64 where the
kono
parents:
diff changeset
103 stack grows downward:
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 int
kono
parents:
diff changeset
106 foo ()
kono
parents:
diff changeset
107 {
kono
parents:
diff changeset
108 char a[23] = {0};
kono
parents:
diff changeset
109 int b[2] = {0};
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 a[5] = 1;
kono
parents:
diff changeset
112 b[1] = 2;
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 return a[5] + b[1];
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 For this function, the stack protected by asan will be organized as
kono
parents:
diff changeset
118 follows, from the top of the stack to the bottom:
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 Slot 1/ [red zone of 32 bytes called 'RIGHT RedZone']
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 Slot 2/ [8 bytes of red zone, that adds up to the space of 'a' to make
kono
parents:
diff changeset
123 the next slot be 32 bytes aligned; this one is called Partial
kono
parents:
diff changeset
124 Redzone; this 32 bytes alignment is an asan constraint]
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 Slot 3/ [24 bytes for variable 'a']
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 Slot 4/ [red zone of 32 bytes called 'Middle RedZone']
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 Slot 5/ [24 bytes of Partial Red Zone (similar to slot 2]
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 Slot 6/ [8 bytes for variable 'b']
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 Slot 7/ [32 bytes of Red Zone at the bottom of the stack, called
kono
parents:
diff changeset
135 'LEFT RedZone']
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 The 32 bytes of LEFT red zone at the bottom of the stack can be
kono
parents:
diff changeset
138 decomposed as such:
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 1/ The first 8 bytes contain a magical asan number that is always
kono
parents:
diff changeset
141 0x41B58AB3.
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 2/ The following 8 bytes contains a pointer to a string (to be
kono
parents:
diff changeset
144 parsed at runtime by the runtime asan library), which format is
kono
parents:
diff changeset
145 the following:
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 "<function-name> <space> <num-of-variables-on-the-stack>
kono
parents:
diff changeset
148 (<32-bytes-aligned-offset-in-bytes-of-variable> <space>
kono
parents:
diff changeset
149 <length-of-var-in-bytes> ){n} "
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 where '(...){n}' means the content inside the parenthesis occurs 'n'
kono
parents:
diff changeset
152 times, with 'n' being the number of variables on the stack.
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 3/ The following 8 bytes contain the PC of the current function which
kono
parents:
diff changeset
155 will be used by the run-time library to print an error message.
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 4/ The following 8 bytes are reserved for internal use by the run-time.
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 The shadow memory for that stack layout is going to look like this:
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 - content of shadow memory 8 bytes for slot 7: 0xF1F1F1F1.
kono
parents:
diff changeset
162 The F1 byte pattern is a magic number called
kono
parents:
diff changeset
163 ASAN_STACK_MAGIC_LEFT and is a way for the runtime to know that
kono
parents:
diff changeset
164 the memory for that shadow byte is part of a the LEFT red zone
kono
parents:
diff changeset
165 intended to seat at the bottom of the variables on the stack.
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 - content of shadow memory 8 bytes for slots 6 and 5:
kono
parents:
diff changeset
168 0xF4F4F400. The F4 byte pattern is a magic number
kono
parents:
diff changeset
169 called ASAN_STACK_MAGIC_PARTIAL. It flags the fact that the
kono
parents:
diff changeset
170 memory region for this shadow byte is a PARTIAL red zone
kono
parents:
diff changeset
171 intended to pad a variable A, so that the slot following
kono
parents:
diff changeset
172 {A,padding} is 32 bytes aligned.
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 Note that the fact that the least significant byte of this
kono
parents:
diff changeset
175 shadow memory content is 00 means that 8 bytes of its
kono
parents:
diff changeset
176 corresponding memory (which corresponds to the memory of
kono
parents:
diff changeset
177 variable 'b') is addressable.
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 - content of shadow memory 8 bytes for slot 4: 0xF2F2F2F2.
kono
parents:
diff changeset
180 The F2 byte pattern is a magic number called
kono
parents:
diff changeset
181 ASAN_STACK_MAGIC_MIDDLE. It flags the fact that the memory
kono
parents:
diff changeset
182 region for this shadow byte is a MIDDLE red zone intended to
kono
parents:
diff changeset
183 seat between two 32 aligned slots of {variable,padding}.
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 - content of shadow memory 8 bytes for slot 3 and 2:
kono
parents:
diff changeset
186 0xF4000000. This represents is the concatenation of
kono
parents:
diff changeset
187 variable 'a' and the partial red zone following it, like what we
kono
parents:
diff changeset
188 had for variable 'b'. The least significant 3 bytes being 00
kono
parents:
diff changeset
189 means that the 3 bytes of variable 'a' are addressable.
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 - content of shadow memory 8 bytes for slot 1: 0xF3F3F3F3.
kono
parents:
diff changeset
192 The F3 byte pattern is a magic number called
kono
parents:
diff changeset
193 ASAN_STACK_MAGIC_RIGHT. It flags the fact that the memory
kono
parents:
diff changeset
194 region for this shadow byte is a RIGHT red zone intended to seat
kono
parents:
diff changeset
195 at the top of the variables of the stack.
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 Note that the real variable layout is done in expand_used_vars in
kono
parents:
diff changeset
198 cfgexpand.c. As far as Address Sanitizer is concerned, it lays out
kono
parents:
diff changeset
199 stack variables as well as the different red zones, emits some
kono
parents:
diff changeset
200 prologue code to populate the shadow memory as to poison (mark as
kono
parents:
diff changeset
201 non-accessible) the regions of the red zones and mark the regions of
kono
parents:
diff changeset
202 stack variables as accessible, and emit some epilogue code to
kono
parents:
diff changeset
203 un-poison (mark as accessible) the regions of red zones right before
kono
parents:
diff changeset
204 the function exits.
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 [Protection of global variables]
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 The basic idea is to insert a red zone between two global variables
kono
parents:
diff changeset
209 and install a constructor function that calls the asan runtime to do
kono
parents:
diff changeset
210 the populating of the relevant shadow memory regions at load time.
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 So the global variables are laid out as to insert a red zone between
kono
parents:
diff changeset
213 them. The size of the red zones is so that each variable starts on a
kono
parents:
diff changeset
214 32 bytes boundary.
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 Then a constructor function is installed so that, for each global
kono
parents:
diff changeset
217 variable, it calls the runtime asan library function
kono
parents:
diff changeset
218 __asan_register_globals_with an instance of this type:
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 struct __asan_global
kono
parents:
diff changeset
221 {
kono
parents:
diff changeset
222 // Address of the beginning of the global variable.
kono
parents:
diff changeset
223 const void *__beg;
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 // Initial size of the global variable.
kono
parents:
diff changeset
226 uptr __size;
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 // Size of the global variable + size of the red zone. This
kono
parents:
diff changeset
229 // size is 32 bytes aligned.
kono
parents:
diff changeset
230 uptr __size_with_redzone;
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 // Name of the global variable.
kono
parents:
diff changeset
233 const void *__name;
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 // Name of the module where the global variable is declared.
kono
parents:
diff changeset
236 const void *__module_name;
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 // 1 if it has dynamic initialization, 0 otherwise.
kono
parents:
diff changeset
239 uptr __has_dynamic_init;
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 // A pointer to struct that contains source location, could be NULL.
kono
parents:
diff changeset
242 __asan_global_source_location *__location;
kono
parents:
diff changeset
243 }
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 A destructor function that calls the runtime asan library function
kono
parents:
diff changeset
246 _asan_unregister_globals is also installed. */
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 static unsigned HOST_WIDE_INT asan_shadow_offset_value;
kono
parents:
diff changeset
249 static bool asan_shadow_offset_computed;
kono
parents:
diff changeset
250 static vec<char *> sanitized_sections;
kono
parents:
diff changeset
251 static tree last_alloca_addr;
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 /* Set of variable declarations that are going to be guarded by
kono
parents:
diff changeset
254 use-after-scope sanitizer. */
kono
parents:
diff changeset
255
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
256 hash_set<tree> *asan_handled_variables = NULL;
111
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 hash_set <tree> *asan_used_labels = NULL;
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 /* Sets shadow offset to value in string VAL. */
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 bool
kono
parents:
diff changeset
263 set_asan_shadow_offset (const char *val)
kono
parents:
diff changeset
264 {
kono
parents:
diff changeset
265 char *endp;
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 errno = 0;
kono
parents:
diff changeset
268 #ifdef HAVE_LONG_LONG
kono
parents:
diff changeset
269 asan_shadow_offset_value = strtoull (val, &endp, 0);
kono
parents:
diff changeset
270 #else
kono
parents:
diff changeset
271 asan_shadow_offset_value = strtoul (val, &endp, 0);
kono
parents:
diff changeset
272 #endif
kono
parents:
diff changeset
273 if (!(*val != '\0' && *endp == '\0' && errno == 0))
kono
parents:
diff changeset
274 return false;
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 asan_shadow_offset_computed = true;
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 return true;
kono
parents:
diff changeset
279 }
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 /* Set list of user-defined sections that need to be sanitized. */
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 void
kono
parents:
diff changeset
284 set_sanitized_sections (const char *sections)
kono
parents:
diff changeset
285 {
kono
parents:
diff changeset
286 char *pat;
kono
parents:
diff changeset
287 unsigned i;
kono
parents:
diff changeset
288 FOR_EACH_VEC_ELT (sanitized_sections, i, pat)
kono
parents:
diff changeset
289 free (pat);
kono
parents:
diff changeset
290 sanitized_sections.truncate (0);
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 for (const char *s = sections; *s; )
kono
parents:
diff changeset
293 {
kono
parents:
diff changeset
294 const char *end;
kono
parents:
diff changeset
295 for (end = s; *end && *end != ','; ++end);
kono
parents:
diff changeset
296 size_t len = end - s;
kono
parents:
diff changeset
297 sanitized_sections.safe_push (xstrndup (s, len));
kono
parents:
diff changeset
298 s = *end ? end + 1 : end;
kono
parents:
diff changeset
299 }
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 bool
kono
parents:
diff changeset
303 asan_mark_p (gimple *stmt, enum asan_mark_flags flag)
kono
parents:
diff changeset
304 {
kono
parents:
diff changeset
305 return (gimple_call_internal_p (stmt, IFN_ASAN_MARK)
kono
parents:
diff changeset
306 && tree_to_uhwi (gimple_call_arg (stmt, 0)) == flag);
kono
parents:
diff changeset
307 }
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 bool
kono
parents:
diff changeset
310 asan_sanitize_stack_p (void)
kono
parents:
diff changeset
311 {
kono
parents:
diff changeset
312 return (sanitize_flags_p (SANITIZE_ADDRESS) && ASAN_STACK);
kono
parents:
diff changeset
313 }
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 bool
kono
parents:
diff changeset
316 asan_sanitize_allocas_p (void)
kono
parents:
diff changeset
317 {
kono
parents:
diff changeset
318 return (asan_sanitize_stack_p () && ASAN_PROTECT_ALLOCAS);
kono
parents:
diff changeset
319 }
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 /* Checks whether section SEC should be sanitized. */
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 static bool
kono
parents:
diff changeset
324 section_sanitized_p (const char *sec)
kono
parents:
diff changeset
325 {
kono
parents:
diff changeset
326 char *pat;
kono
parents:
diff changeset
327 unsigned i;
kono
parents:
diff changeset
328 FOR_EACH_VEC_ELT (sanitized_sections, i, pat)
kono
parents:
diff changeset
329 if (fnmatch (pat, sec, FNM_PERIOD) == 0)
kono
parents:
diff changeset
330 return true;
kono
parents:
diff changeset
331 return false;
kono
parents:
diff changeset
332 }
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 /* Returns Asan shadow offset. */
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 static unsigned HOST_WIDE_INT
kono
parents:
diff changeset
337 asan_shadow_offset ()
kono
parents:
diff changeset
338 {
kono
parents:
diff changeset
339 if (!asan_shadow_offset_computed)
kono
parents:
diff changeset
340 {
kono
parents:
diff changeset
341 asan_shadow_offset_computed = true;
kono
parents:
diff changeset
342 asan_shadow_offset_value = targetm.asan_shadow_offset ();
kono
parents:
diff changeset
343 }
kono
parents:
diff changeset
344 return asan_shadow_offset_value;
kono
parents:
diff changeset
345 }
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 alias_set_type asan_shadow_set = -1;
kono
parents:
diff changeset
348
kono
parents:
diff changeset
349 /* Pointer types to 1, 2 or 4 byte integers in shadow memory. A separate
kono
parents:
diff changeset
350 alias set is used for all shadow memory accesses. */
kono
parents:
diff changeset
351 static GTY(()) tree shadow_ptr_types[3];
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 /* Decl for __asan_option_detect_stack_use_after_return. */
kono
parents:
diff changeset
354 static GTY(()) tree asan_detect_stack_use_after_return;
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 /* Hashtable support for memory references used by gimple
kono
parents:
diff changeset
357 statements. */
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 /* This type represents a reference to a memory region. */
kono
parents:
diff changeset
360 struct asan_mem_ref
kono
parents:
diff changeset
361 {
kono
parents:
diff changeset
362 /* The expression of the beginning of the memory region. */
kono
parents:
diff changeset
363 tree start;
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 /* The size of the access. */
kono
parents:
diff changeset
366 HOST_WIDE_INT access_size;
kono
parents:
diff changeset
367 };
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 object_allocator <asan_mem_ref> asan_mem_ref_pool ("asan_mem_ref");
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 /* Initializes an instance of asan_mem_ref. */
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 static void
kono
parents:
diff changeset
374 asan_mem_ref_init (asan_mem_ref *ref, tree start, HOST_WIDE_INT access_size)
kono
parents:
diff changeset
375 {
kono
parents:
diff changeset
376 ref->start = start;
kono
parents:
diff changeset
377 ref->access_size = access_size;
kono
parents:
diff changeset
378 }
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 /* Allocates memory for an instance of asan_mem_ref into the memory
kono
parents:
diff changeset
381 pool returned by asan_mem_ref_get_alloc_pool and initialize it.
kono
parents:
diff changeset
382 START is the address of (or the expression pointing to) the
kono
parents:
diff changeset
383 beginning of memory reference. ACCESS_SIZE is the size of the
kono
parents:
diff changeset
384 access to the referenced memory. */
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 static asan_mem_ref*
kono
parents:
diff changeset
387 asan_mem_ref_new (tree start, HOST_WIDE_INT access_size)
kono
parents:
diff changeset
388 {
kono
parents:
diff changeset
389 asan_mem_ref *ref = asan_mem_ref_pool.allocate ();
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 asan_mem_ref_init (ref, start, access_size);
kono
parents:
diff changeset
392 return ref;
kono
parents:
diff changeset
393 }
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 /* This builds and returns a pointer to the end of the memory region
kono
parents:
diff changeset
396 that starts at START and of length LEN. */
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 tree
kono
parents:
diff changeset
399 asan_mem_ref_get_end (tree start, tree len)
kono
parents:
diff changeset
400 {
kono
parents:
diff changeset
401 if (len == NULL_TREE || integer_zerop (len))
kono
parents:
diff changeset
402 return start;
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 if (!ptrofftype_p (len))
kono
parents:
diff changeset
405 len = convert_to_ptrofftype (len);
kono
parents:
diff changeset
406
kono
parents:
diff changeset
407 return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (start), start, len);
kono
parents:
diff changeset
408 }
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 /* Return a tree expression that represents the end of the referenced
kono
parents:
diff changeset
411 memory region. Beware that this function can actually build a new
kono
parents:
diff changeset
412 tree expression. */
kono
parents:
diff changeset
413
kono
parents:
diff changeset
414 tree
kono
parents:
diff changeset
415 asan_mem_ref_get_end (const asan_mem_ref *ref, tree len)
kono
parents:
diff changeset
416 {
kono
parents:
diff changeset
417 return asan_mem_ref_get_end (ref->start, len);
kono
parents:
diff changeset
418 }
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 struct asan_mem_ref_hasher : nofree_ptr_hash <asan_mem_ref>
kono
parents:
diff changeset
421 {
kono
parents:
diff changeset
422 static inline hashval_t hash (const asan_mem_ref *);
kono
parents:
diff changeset
423 static inline bool equal (const asan_mem_ref *, const asan_mem_ref *);
kono
parents:
diff changeset
424 };
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 /* Hash a memory reference. */
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 inline hashval_t
kono
parents:
diff changeset
429 asan_mem_ref_hasher::hash (const asan_mem_ref *mem_ref)
kono
parents:
diff changeset
430 {
kono
parents:
diff changeset
431 return iterative_hash_expr (mem_ref->start, 0);
kono
parents:
diff changeset
432 }
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 /* Compare two memory references. We accept the length of either
kono
parents:
diff changeset
435 memory references to be NULL_TREE. */
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 inline bool
kono
parents:
diff changeset
438 asan_mem_ref_hasher::equal (const asan_mem_ref *m1,
kono
parents:
diff changeset
439 const asan_mem_ref *m2)
kono
parents:
diff changeset
440 {
kono
parents:
diff changeset
441 return operand_equal_p (m1->start, m2->start, 0);
kono
parents:
diff changeset
442 }
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 static hash_table<asan_mem_ref_hasher> *asan_mem_ref_ht;
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 /* Returns a reference to the hash table containing memory references.
kono
parents:
diff changeset
447 This function ensures that the hash table is created. Note that
kono
parents:
diff changeset
448 this hash table is updated by the function
kono
parents:
diff changeset
449 update_mem_ref_hash_table. */
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 static hash_table<asan_mem_ref_hasher> *
kono
parents:
diff changeset
452 get_mem_ref_hash_table ()
kono
parents:
diff changeset
453 {
kono
parents:
diff changeset
454 if (!asan_mem_ref_ht)
kono
parents:
diff changeset
455 asan_mem_ref_ht = new hash_table<asan_mem_ref_hasher> (10);
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 return asan_mem_ref_ht;
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 /* Clear all entries from the memory references hash table. */
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 static void
kono
parents:
diff changeset
463 empty_mem_ref_hash_table ()
kono
parents:
diff changeset
464 {
kono
parents:
diff changeset
465 if (asan_mem_ref_ht)
kono
parents:
diff changeset
466 asan_mem_ref_ht->empty ();
kono
parents:
diff changeset
467 }
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 /* Free the memory references hash table. */
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 static void
kono
parents:
diff changeset
472 free_mem_ref_resources ()
kono
parents:
diff changeset
473 {
kono
parents:
diff changeset
474 delete asan_mem_ref_ht;
kono
parents:
diff changeset
475 asan_mem_ref_ht = NULL;
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 asan_mem_ref_pool.release ();
kono
parents:
diff changeset
478 }
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 /* Return true iff the memory reference REF has been instrumented. */
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 static bool
kono
parents:
diff changeset
483 has_mem_ref_been_instrumented (tree ref, HOST_WIDE_INT access_size)
kono
parents:
diff changeset
484 {
kono
parents:
diff changeset
485 asan_mem_ref r;
kono
parents:
diff changeset
486 asan_mem_ref_init (&r, ref, access_size);
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 asan_mem_ref *saved_ref = get_mem_ref_hash_table ()->find (&r);
kono
parents:
diff changeset
489 return saved_ref && saved_ref->access_size >= access_size;
kono
parents:
diff changeset
490 }
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 /* Return true iff the memory reference REF has been instrumented. */
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 static bool
kono
parents:
diff changeset
495 has_mem_ref_been_instrumented (const asan_mem_ref *ref)
kono
parents:
diff changeset
496 {
kono
parents:
diff changeset
497 return has_mem_ref_been_instrumented (ref->start, ref->access_size);
kono
parents:
diff changeset
498 }
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 /* Return true iff access to memory region starting at REF and of
kono
parents:
diff changeset
501 length LEN has been instrumented. */
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 static bool
kono
parents:
diff changeset
504 has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len)
kono
parents:
diff changeset
505 {
kono
parents:
diff changeset
506 HOST_WIDE_INT size_in_bytes
kono
parents:
diff changeset
507 = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
kono
parents:
diff changeset
508
kono
parents:
diff changeset
509 return size_in_bytes != -1
kono
parents:
diff changeset
510 && has_mem_ref_been_instrumented (ref->start, size_in_bytes);
kono
parents:
diff changeset
511 }
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 /* Set REF to the memory reference present in a gimple assignment
kono
parents:
diff changeset
514 ASSIGNMENT. Return true upon successful completion, false
kono
parents:
diff changeset
515 otherwise. */
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 static bool
kono
parents:
diff changeset
518 get_mem_ref_of_assignment (const gassign *assignment,
kono
parents:
diff changeset
519 asan_mem_ref *ref,
kono
parents:
diff changeset
520 bool *ref_is_store)
kono
parents:
diff changeset
521 {
kono
parents:
diff changeset
522 gcc_assert (gimple_assign_single_p (assignment));
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 if (gimple_store_p (assignment)
kono
parents:
diff changeset
525 && !gimple_clobber_p (assignment))
kono
parents:
diff changeset
526 {
kono
parents:
diff changeset
527 ref->start = gimple_assign_lhs (assignment);
kono
parents:
diff changeset
528 *ref_is_store = true;
kono
parents:
diff changeset
529 }
kono
parents:
diff changeset
530 else if (gimple_assign_load_p (assignment))
kono
parents:
diff changeset
531 {
kono
parents:
diff changeset
532 ref->start = gimple_assign_rhs1 (assignment);
kono
parents:
diff changeset
533 *ref_is_store = false;
kono
parents:
diff changeset
534 }
kono
parents:
diff changeset
535 else
kono
parents:
diff changeset
536 return false;
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 ref->access_size = int_size_in_bytes (TREE_TYPE (ref->start));
kono
parents:
diff changeset
539 return true;
kono
parents:
diff changeset
540 }
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 /* Return address of last allocated dynamic alloca. */
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 static tree
kono
parents:
diff changeset
545 get_last_alloca_addr ()
kono
parents:
diff changeset
546 {
kono
parents:
diff changeset
547 if (last_alloca_addr)
kono
parents:
diff changeset
548 return last_alloca_addr;
kono
parents:
diff changeset
549
kono
parents:
diff changeset
550 last_alloca_addr = create_tmp_reg (ptr_type_node, "last_alloca_addr");
kono
parents:
diff changeset
551 gassign *g = gimple_build_assign (last_alloca_addr, null_pointer_node);
kono
parents:
diff changeset
552 edge e = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
kono
parents:
diff changeset
553 gsi_insert_on_edge_immediate (e, g);
kono
parents:
diff changeset
554 return last_alloca_addr;
kono
parents:
diff changeset
555 }
kono
parents:
diff changeset
556
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
557 /* Insert __asan_allocas_unpoison (top, bottom) call before
111
kono
parents:
diff changeset
558 __builtin_stack_restore (new_sp) call.
kono
parents:
diff changeset
559 The pseudocode of this routine should look like this:
kono
parents:
diff changeset
560 top = last_alloca_addr;
kono
parents:
diff changeset
561 bot = new_sp;
kono
parents:
diff changeset
562 __asan_allocas_unpoison (top, bot);
kono
parents:
diff changeset
563 last_alloca_addr = new_sp;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
564 __builtin_stack_restore (new_sp);
111
kono
parents:
diff changeset
565 In general, we can't use new_sp as bot parameter because on some
kono
parents:
diff changeset
566 architectures SP has non zero offset from dynamic stack area. Moreover, on
kono
parents:
diff changeset
567 some architectures this offset (STACK_DYNAMIC_OFFSET) becomes known for each
kono
parents:
diff changeset
568 particular function only after all callees were expanded to rtl.
kono
parents:
diff changeset
569 The most noticeable example is PowerPC{,64}, see
kono
parents:
diff changeset
570 http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#DYNAM-STACK.
kono
parents:
diff changeset
571 To overcome the issue we use following trick: pass new_sp as a second
kono
parents:
diff changeset
572 parameter to __asan_allocas_unpoison and rewrite it during expansion with
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
573 new_sp + (virtual_dynamic_stack_rtx - sp) later in
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
574 expand_asan_emit_allocas_unpoison function. */
111
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 static void
kono
parents:
diff changeset
577 handle_builtin_stack_restore (gcall *call, gimple_stmt_iterator *iter)
kono
parents:
diff changeset
578 {
kono
parents:
diff changeset
579 if (!iter || !asan_sanitize_allocas_p ())
kono
parents:
diff changeset
580 return;
kono
parents:
diff changeset
581
kono
parents:
diff changeset
582 tree last_alloca = get_last_alloca_addr ();
kono
parents:
diff changeset
583 tree restored_stack = gimple_call_arg (call, 0);
kono
parents:
diff changeset
584 tree fn = builtin_decl_implicit (BUILT_IN_ASAN_ALLOCAS_UNPOISON);
kono
parents:
diff changeset
585 gimple *g = gimple_build_call (fn, 2, last_alloca, restored_stack);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
586 gsi_insert_before (iter, g, GSI_SAME_STMT);
111
kono
parents:
diff changeset
587 g = gimple_build_assign (last_alloca, restored_stack);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
588 gsi_insert_before (iter, g, GSI_SAME_STMT);
111
kono
parents:
diff changeset
589 }
kono
parents:
diff changeset
590
kono
parents:
diff changeset
591 /* Deploy and poison redzones around __builtin_alloca call. To do this, we
kono
parents:
diff changeset
592 should replace this call with another one with changed parameters and
kono
parents:
diff changeset
593 replace all its uses with new address, so
kono
parents:
diff changeset
594 addr = __builtin_alloca (old_size, align);
kono
parents:
diff changeset
595 is replaced by
kono
parents:
diff changeset
596 left_redzone_size = max (align, ASAN_RED_ZONE_SIZE);
kono
parents:
diff changeset
597 Following two statements are optimized out if we know that
kono
parents:
diff changeset
598 old_size & (ASAN_RED_ZONE_SIZE - 1) == 0, i.e. alloca doesn't need partial
kono
parents:
diff changeset
599 redzone.
kono
parents:
diff changeset
600 misalign = old_size & (ASAN_RED_ZONE_SIZE - 1);
kono
parents:
diff changeset
601 partial_redzone_size = ASAN_RED_ZONE_SIZE - misalign;
kono
parents:
diff changeset
602 right_redzone_size = ASAN_RED_ZONE_SIZE;
kono
parents:
diff changeset
603 additional_size = left_redzone_size + partial_redzone_size +
kono
parents:
diff changeset
604 right_redzone_size;
kono
parents:
diff changeset
605 new_size = old_size + additional_size;
kono
parents:
diff changeset
606 new_alloca = __builtin_alloca (new_size, max (align, 32))
kono
parents:
diff changeset
607 __asan_alloca_poison (new_alloca, old_size)
kono
parents:
diff changeset
608 addr = new_alloca + max (align, ASAN_RED_ZONE_SIZE);
kono
parents:
diff changeset
609 last_alloca_addr = new_alloca;
kono
parents:
diff changeset
610 ADDITIONAL_SIZE is added to make new memory allocation contain not only
kono
parents:
diff changeset
611 requested memory, but also left, partial and right redzones as well as some
kono
parents:
diff changeset
612 additional space, required by alignment. */
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 static void
kono
parents:
diff changeset
615 handle_builtin_alloca (gcall *call, gimple_stmt_iterator *iter)
kono
parents:
diff changeset
616 {
kono
parents:
diff changeset
617 if (!iter || !asan_sanitize_allocas_p ())
kono
parents:
diff changeset
618 return;
kono
parents:
diff changeset
619
kono
parents:
diff changeset
620 gassign *g;
kono
parents:
diff changeset
621 gcall *gg;
kono
parents:
diff changeset
622 const HOST_WIDE_INT redzone_mask = ASAN_RED_ZONE_SIZE - 1;
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 tree last_alloca = get_last_alloca_addr ();
kono
parents:
diff changeset
625 tree callee = gimple_call_fndecl (call);
kono
parents:
diff changeset
626 tree old_size = gimple_call_arg (call, 0);
kono
parents:
diff changeset
627 tree ptr_type = gimple_call_lhs (call) ? TREE_TYPE (gimple_call_lhs (call))
kono
parents:
diff changeset
628 : ptr_type_node;
kono
parents:
diff changeset
629 tree partial_size = NULL_TREE;
kono
parents:
diff changeset
630 unsigned int align
kono
parents:
diff changeset
631 = DECL_FUNCTION_CODE (callee) == BUILT_IN_ALLOCA
kono
parents:
diff changeset
632 ? 0 : tree_to_uhwi (gimple_call_arg (call, 1));
kono
parents:
diff changeset
633
kono
parents:
diff changeset
634 /* If ALIGN > ASAN_RED_ZONE_SIZE, we embed left redzone into first ALIGN
kono
parents:
diff changeset
635 bytes of allocated space. Otherwise, align alloca to ASAN_RED_ZONE_SIZE
kono
parents:
diff changeset
636 manually. */
kono
parents:
diff changeset
637 align = MAX (align, ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
kono
parents:
diff changeset
638
kono
parents:
diff changeset
639 tree alloca_rz_mask = build_int_cst (size_type_node, redzone_mask);
kono
parents:
diff changeset
640 tree redzone_size = build_int_cst (size_type_node, ASAN_RED_ZONE_SIZE);
kono
parents:
diff changeset
641
kono
parents:
diff changeset
642 /* Extract lower bits from old_size. */
kono
parents:
diff changeset
643 wide_int size_nonzero_bits = get_nonzero_bits (old_size);
kono
parents:
diff changeset
644 wide_int rz_mask
kono
parents:
diff changeset
645 = wi::uhwi (redzone_mask, wi::get_precision (size_nonzero_bits));
kono
parents:
diff changeset
646 wide_int old_size_lower_bits = wi::bit_and (size_nonzero_bits, rz_mask);
kono
parents:
diff changeset
647
kono
parents:
diff changeset
648 /* If alloca size is aligned to ASAN_RED_ZONE_SIZE, we don't need partial
kono
parents:
diff changeset
649 redzone. Otherwise, compute its size here. */
kono
parents:
diff changeset
650 if (wi::ne_p (old_size_lower_bits, 0))
kono
parents:
diff changeset
651 {
kono
parents:
diff changeset
652 /* misalign = size & (ASAN_RED_ZONE_SIZE - 1)
kono
parents:
diff changeset
653 partial_size = ASAN_RED_ZONE_SIZE - misalign. */
kono
parents:
diff changeset
654 g = gimple_build_assign (make_ssa_name (size_type_node, NULL),
kono
parents:
diff changeset
655 BIT_AND_EXPR, old_size, alloca_rz_mask);
kono
parents:
diff changeset
656 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
657 tree misalign = gimple_assign_lhs (g);
kono
parents:
diff changeset
658 g = gimple_build_assign (make_ssa_name (size_type_node, NULL), MINUS_EXPR,
kono
parents:
diff changeset
659 redzone_size, misalign);
kono
parents:
diff changeset
660 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
661 partial_size = gimple_assign_lhs (g);
kono
parents:
diff changeset
662 }
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 /* additional_size = align + ASAN_RED_ZONE_SIZE. */
kono
parents:
diff changeset
665 tree additional_size = build_int_cst (size_type_node, align / BITS_PER_UNIT
kono
parents:
diff changeset
666 + ASAN_RED_ZONE_SIZE);
kono
parents:
diff changeset
667 /* If alloca has partial redzone, include it to additional_size too. */
kono
parents:
diff changeset
668 if (partial_size)
kono
parents:
diff changeset
669 {
kono
parents:
diff changeset
670 /* additional_size += partial_size. */
kono
parents:
diff changeset
671 g = gimple_build_assign (make_ssa_name (size_type_node), PLUS_EXPR,
kono
parents:
diff changeset
672 partial_size, additional_size);
kono
parents:
diff changeset
673 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
674 additional_size = gimple_assign_lhs (g);
kono
parents:
diff changeset
675 }
kono
parents:
diff changeset
676
kono
parents:
diff changeset
677 /* new_size = old_size + additional_size. */
kono
parents:
diff changeset
678 g = gimple_build_assign (make_ssa_name (size_type_node), PLUS_EXPR, old_size,
kono
parents:
diff changeset
679 additional_size);
kono
parents:
diff changeset
680 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
681 tree new_size = gimple_assign_lhs (g);
kono
parents:
diff changeset
682
kono
parents:
diff changeset
683 /* Build new __builtin_alloca call:
kono
parents:
diff changeset
684 new_alloca_with_rz = __builtin_alloca (new_size, align). */
kono
parents:
diff changeset
685 tree fn = builtin_decl_implicit (BUILT_IN_ALLOCA_WITH_ALIGN);
kono
parents:
diff changeset
686 gg = gimple_build_call (fn, 2, new_size,
kono
parents:
diff changeset
687 build_int_cst (size_type_node, align));
kono
parents:
diff changeset
688 tree new_alloca_with_rz = make_ssa_name (ptr_type, gg);
kono
parents:
diff changeset
689 gimple_call_set_lhs (gg, new_alloca_with_rz);
kono
parents:
diff changeset
690 gsi_insert_before (iter, gg, GSI_SAME_STMT);
kono
parents:
diff changeset
691
kono
parents:
diff changeset
692 /* new_alloca = new_alloca_with_rz + align. */
kono
parents:
diff changeset
693 g = gimple_build_assign (make_ssa_name (ptr_type), POINTER_PLUS_EXPR,
kono
parents:
diff changeset
694 new_alloca_with_rz,
kono
parents:
diff changeset
695 build_int_cst (size_type_node,
kono
parents:
diff changeset
696 align / BITS_PER_UNIT));
kono
parents:
diff changeset
697 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
698 tree new_alloca = gimple_assign_lhs (g);
kono
parents:
diff changeset
699
kono
parents:
diff changeset
700 /* Poison newly created alloca redzones:
kono
parents:
diff changeset
701 __asan_alloca_poison (new_alloca, old_size). */
kono
parents:
diff changeset
702 fn = builtin_decl_implicit (BUILT_IN_ASAN_ALLOCA_POISON);
kono
parents:
diff changeset
703 gg = gimple_build_call (fn, 2, new_alloca, old_size);
kono
parents:
diff changeset
704 gsi_insert_before (iter, gg, GSI_SAME_STMT);
kono
parents:
diff changeset
705
kono
parents:
diff changeset
706 /* Save new_alloca_with_rz value into last_alloca to use it during
kono
parents:
diff changeset
707 allocas unpoisoning. */
kono
parents:
diff changeset
708 g = gimple_build_assign (last_alloca, new_alloca_with_rz);
kono
parents:
diff changeset
709 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
710
kono
parents:
diff changeset
711 /* Finally, replace old alloca ptr with NEW_ALLOCA. */
kono
parents:
diff changeset
712 replace_call_with_value (iter, new_alloca);
kono
parents:
diff changeset
713 }
kono
parents:
diff changeset
714
kono
parents:
diff changeset
715 /* Return the memory references contained in a gimple statement
kono
parents:
diff changeset
716 representing a builtin call that has to do with memory access. */
kono
parents:
diff changeset
717
kono
parents:
diff changeset
718 static bool
kono
parents:
diff changeset
719 get_mem_refs_of_builtin_call (gcall *call,
kono
parents:
diff changeset
720 asan_mem_ref *src0,
kono
parents:
diff changeset
721 tree *src0_len,
kono
parents:
diff changeset
722 bool *src0_is_store,
kono
parents:
diff changeset
723 asan_mem_ref *src1,
kono
parents:
diff changeset
724 tree *src1_len,
kono
parents:
diff changeset
725 bool *src1_is_store,
kono
parents:
diff changeset
726 asan_mem_ref *dst,
kono
parents:
diff changeset
727 tree *dst_len,
kono
parents:
diff changeset
728 bool *dst_is_store,
kono
parents:
diff changeset
729 bool *dest_is_deref,
kono
parents:
diff changeset
730 bool *intercepted_p,
kono
parents:
diff changeset
731 gimple_stmt_iterator *iter = NULL)
kono
parents:
diff changeset
732 {
kono
parents:
diff changeset
733 gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL));
kono
parents:
diff changeset
734
kono
parents:
diff changeset
735 tree callee = gimple_call_fndecl (call);
kono
parents:
diff changeset
736 tree source0 = NULL_TREE, source1 = NULL_TREE,
kono
parents:
diff changeset
737 dest = NULL_TREE, len = NULL_TREE;
kono
parents:
diff changeset
738 bool is_store = true, got_reference_p = false;
kono
parents:
diff changeset
739 HOST_WIDE_INT access_size = 1;
kono
parents:
diff changeset
740
kono
parents:
diff changeset
741 *intercepted_p = asan_intercepted_p ((DECL_FUNCTION_CODE (callee)));
kono
parents:
diff changeset
742
kono
parents:
diff changeset
743 switch (DECL_FUNCTION_CODE (callee))
kono
parents:
diff changeset
744 {
kono
parents:
diff changeset
745 /* (s, s, n) style memops. */
kono
parents:
diff changeset
746 case BUILT_IN_BCMP:
kono
parents:
diff changeset
747 case BUILT_IN_MEMCMP:
kono
parents:
diff changeset
748 source0 = gimple_call_arg (call, 0);
kono
parents:
diff changeset
749 source1 = gimple_call_arg (call, 1);
kono
parents:
diff changeset
750 len = gimple_call_arg (call, 2);
kono
parents:
diff changeset
751 break;
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 /* (src, dest, n) style memops. */
kono
parents:
diff changeset
754 case BUILT_IN_BCOPY:
kono
parents:
diff changeset
755 source0 = gimple_call_arg (call, 0);
kono
parents:
diff changeset
756 dest = gimple_call_arg (call, 1);
kono
parents:
diff changeset
757 len = gimple_call_arg (call, 2);
kono
parents:
diff changeset
758 break;
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 /* (dest, src, n) style memops. */
kono
parents:
diff changeset
761 case BUILT_IN_MEMCPY:
kono
parents:
diff changeset
762 case BUILT_IN_MEMCPY_CHK:
kono
parents:
diff changeset
763 case BUILT_IN_MEMMOVE:
kono
parents:
diff changeset
764 case BUILT_IN_MEMMOVE_CHK:
kono
parents:
diff changeset
765 case BUILT_IN_MEMPCPY:
kono
parents:
diff changeset
766 case BUILT_IN_MEMPCPY_CHK:
kono
parents:
diff changeset
767 dest = gimple_call_arg (call, 0);
kono
parents:
diff changeset
768 source0 = gimple_call_arg (call, 1);
kono
parents:
diff changeset
769 len = gimple_call_arg (call, 2);
kono
parents:
diff changeset
770 break;
kono
parents:
diff changeset
771
kono
parents:
diff changeset
772 /* (dest, n) style memops. */
kono
parents:
diff changeset
773 case BUILT_IN_BZERO:
kono
parents:
diff changeset
774 dest = gimple_call_arg (call, 0);
kono
parents:
diff changeset
775 len = gimple_call_arg (call, 1);
kono
parents:
diff changeset
776 break;
kono
parents:
diff changeset
777
kono
parents:
diff changeset
778 /* (dest, x, n) style memops*/
kono
parents:
diff changeset
779 case BUILT_IN_MEMSET:
kono
parents:
diff changeset
780 case BUILT_IN_MEMSET_CHK:
kono
parents:
diff changeset
781 dest = gimple_call_arg (call, 0);
kono
parents:
diff changeset
782 len = gimple_call_arg (call, 2);
kono
parents:
diff changeset
783 break;
kono
parents:
diff changeset
784
kono
parents:
diff changeset
785 case BUILT_IN_STRLEN:
kono
parents:
diff changeset
786 source0 = gimple_call_arg (call, 0);
kono
parents:
diff changeset
787 len = gimple_call_lhs (call);
kono
parents:
diff changeset
788 break;
kono
parents:
diff changeset
789
kono
parents:
diff changeset
790 case BUILT_IN_STACK_RESTORE:
kono
parents:
diff changeset
791 handle_builtin_stack_restore (call, iter);
kono
parents:
diff changeset
792 break;
kono
parents:
diff changeset
793
kono
parents:
diff changeset
794 CASE_BUILT_IN_ALLOCA:
kono
parents:
diff changeset
795 handle_builtin_alloca (call, iter);
kono
parents:
diff changeset
796 break;
kono
parents:
diff changeset
797 /* And now the __atomic* and __sync builtins.
kono
parents:
diff changeset
798 These are handled differently from the classical memory memory
kono
parents:
diff changeset
799 access builtins above. */
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 case BUILT_IN_ATOMIC_LOAD_1:
kono
parents:
diff changeset
802 is_store = false;
kono
parents:
diff changeset
803 /* FALLTHRU */
kono
parents:
diff changeset
804 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
kono
parents:
diff changeset
805 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
kono
parents:
diff changeset
806 case BUILT_IN_SYNC_FETCH_AND_OR_1:
kono
parents:
diff changeset
807 case BUILT_IN_SYNC_FETCH_AND_AND_1:
kono
parents:
diff changeset
808 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
kono
parents:
diff changeset
809 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
kono
parents:
diff changeset
810 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
kono
parents:
diff changeset
811 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
kono
parents:
diff changeset
812 case BUILT_IN_SYNC_OR_AND_FETCH_1:
kono
parents:
diff changeset
813 case BUILT_IN_SYNC_AND_AND_FETCH_1:
kono
parents:
diff changeset
814 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
kono
parents:
diff changeset
815 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
kono
parents:
diff changeset
816 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
kono
parents:
diff changeset
817 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
kono
parents:
diff changeset
818 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
kono
parents:
diff changeset
819 case BUILT_IN_SYNC_LOCK_RELEASE_1:
kono
parents:
diff changeset
820 case BUILT_IN_ATOMIC_EXCHANGE_1:
kono
parents:
diff changeset
821 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
kono
parents:
diff changeset
822 case BUILT_IN_ATOMIC_STORE_1:
kono
parents:
diff changeset
823 case BUILT_IN_ATOMIC_ADD_FETCH_1:
kono
parents:
diff changeset
824 case BUILT_IN_ATOMIC_SUB_FETCH_1:
kono
parents:
diff changeset
825 case BUILT_IN_ATOMIC_AND_FETCH_1:
kono
parents:
diff changeset
826 case BUILT_IN_ATOMIC_NAND_FETCH_1:
kono
parents:
diff changeset
827 case BUILT_IN_ATOMIC_XOR_FETCH_1:
kono
parents:
diff changeset
828 case BUILT_IN_ATOMIC_OR_FETCH_1:
kono
parents:
diff changeset
829 case BUILT_IN_ATOMIC_FETCH_ADD_1:
kono
parents:
diff changeset
830 case BUILT_IN_ATOMIC_FETCH_SUB_1:
kono
parents:
diff changeset
831 case BUILT_IN_ATOMIC_FETCH_AND_1:
kono
parents:
diff changeset
832 case BUILT_IN_ATOMIC_FETCH_NAND_1:
kono
parents:
diff changeset
833 case BUILT_IN_ATOMIC_FETCH_XOR_1:
kono
parents:
diff changeset
834 case BUILT_IN_ATOMIC_FETCH_OR_1:
kono
parents:
diff changeset
835 access_size = 1;
kono
parents:
diff changeset
836 goto do_atomic;
kono
parents:
diff changeset
837
kono
parents:
diff changeset
838 case BUILT_IN_ATOMIC_LOAD_2:
kono
parents:
diff changeset
839 is_store = false;
kono
parents:
diff changeset
840 /* FALLTHRU */
kono
parents:
diff changeset
841 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
kono
parents:
diff changeset
842 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
kono
parents:
diff changeset
843 case BUILT_IN_SYNC_FETCH_AND_OR_2:
kono
parents:
diff changeset
844 case BUILT_IN_SYNC_FETCH_AND_AND_2:
kono
parents:
diff changeset
845 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
kono
parents:
diff changeset
846 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
kono
parents:
diff changeset
847 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
kono
parents:
diff changeset
848 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
kono
parents:
diff changeset
849 case BUILT_IN_SYNC_OR_AND_FETCH_2:
kono
parents:
diff changeset
850 case BUILT_IN_SYNC_AND_AND_FETCH_2:
kono
parents:
diff changeset
851 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
kono
parents:
diff changeset
852 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
kono
parents:
diff changeset
853 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
kono
parents:
diff changeset
854 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
kono
parents:
diff changeset
855 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
kono
parents:
diff changeset
856 case BUILT_IN_SYNC_LOCK_RELEASE_2:
kono
parents:
diff changeset
857 case BUILT_IN_ATOMIC_EXCHANGE_2:
kono
parents:
diff changeset
858 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
kono
parents:
diff changeset
859 case BUILT_IN_ATOMIC_STORE_2:
kono
parents:
diff changeset
860 case BUILT_IN_ATOMIC_ADD_FETCH_2:
kono
parents:
diff changeset
861 case BUILT_IN_ATOMIC_SUB_FETCH_2:
kono
parents:
diff changeset
862 case BUILT_IN_ATOMIC_AND_FETCH_2:
kono
parents:
diff changeset
863 case BUILT_IN_ATOMIC_NAND_FETCH_2:
kono
parents:
diff changeset
864 case BUILT_IN_ATOMIC_XOR_FETCH_2:
kono
parents:
diff changeset
865 case BUILT_IN_ATOMIC_OR_FETCH_2:
kono
parents:
diff changeset
866 case BUILT_IN_ATOMIC_FETCH_ADD_2:
kono
parents:
diff changeset
867 case BUILT_IN_ATOMIC_FETCH_SUB_2:
kono
parents:
diff changeset
868 case BUILT_IN_ATOMIC_FETCH_AND_2:
kono
parents:
diff changeset
869 case BUILT_IN_ATOMIC_FETCH_NAND_2:
kono
parents:
diff changeset
870 case BUILT_IN_ATOMIC_FETCH_XOR_2:
kono
parents:
diff changeset
871 case BUILT_IN_ATOMIC_FETCH_OR_2:
kono
parents:
diff changeset
872 access_size = 2;
kono
parents:
diff changeset
873 goto do_atomic;
kono
parents:
diff changeset
874
kono
parents:
diff changeset
875 case BUILT_IN_ATOMIC_LOAD_4:
kono
parents:
diff changeset
876 is_store = false;
kono
parents:
diff changeset
877 /* FALLTHRU */
kono
parents:
diff changeset
878 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
kono
parents:
diff changeset
879 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
kono
parents:
diff changeset
880 case BUILT_IN_SYNC_FETCH_AND_OR_4:
kono
parents:
diff changeset
881 case BUILT_IN_SYNC_FETCH_AND_AND_4:
kono
parents:
diff changeset
882 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
kono
parents:
diff changeset
883 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
kono
parents:
diff changeset
884 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
kono
parents:
diff changeset
885 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
kono
parents:
diff changeset
886 case BUILT_IN_SYNC_OR_AND_FETCH_4:
kono
parents:
diff changeset
887 case BUILT_IN_SYNC_AND_AND_FETCH_4:
kono
parents:
diff changeset
888 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
kono
parents:
diff changeset
889 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
kono
parents:
diff changeset
890 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
kono
parents:
diff changeset
891 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
kono
parents:
diff changeset
892 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
kono
parents:
diff changeset
893 case BUILT_IN_SYNC_LOCK_RELEASE_4:
kono
parents:
diff changeset
894 case BUILT_IN_ATOMIC_EXCHANGE_4:
kono
parents:
diff changeset
895 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
kono
parents:
diff changeset
896 case BUILT_IN_ATOMIC_STORE_4:
kono
parents:
diff changeset
897 case BUILT_IN_ATOMIC_ADD_FETCH_4:
kono
parents:
diff changeset
898 case BUILT_IN_ATOMIC_SUB_FETCH_4:
kono
parents:
diff changeset
899 case BUILT_IN_ATOMIC_AND_FETCH_4:
kono
parents:
diff changeset
900 case BUILT_IN_ATOMIC_NAND_FETCH_4:
kono
parents:
diff changeset
901 case BUILT_IN_ATOMIC_XOR_FETCH_4:
kono
parents:
diff changeset
902 case BUILT_IN_ATOMIC_OR_FETCH_4:
kono
parents:
diff changeset
903 case BUILT_IN_ATOMIC_FETCH_ADD_4:
kono
parents:
diff changeset
904 case BUILT_IN_ATOMIC_FETCH_SUB_4:
kono
parents:
diff changeset
905 case BUILT_IN_ATOMIC_FETCH_AND_4:
kono
parents:
diff changeset
906 case BUILT_IN_ATOMIC_FETCH_NAND_4:
kono
parents:
diff changeset
907 case BUILT_IN_ATOMIC_FETCH_XOR_4:
kono
parents:
diff changeset
908 case BUILT_IN_ATOMIC_FETCH_OR_4:
kono
parents:
diff changeset
909 access_size = 4;
kono
parents:
diff changeset
910 goto do_atomic;
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 case BUILT_IN_ATOMIC_LOAD_8:
kono
parents:
diff changeset
913 is_store = false;
kono
parents:
diff changeset
914 /* FALLTHRU */
kono
parents:
diff changeset
915 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
kono
parents:
diff changeset
916 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
kono
parents:
diff changeset
917 case BUILT_IN_SYNC_FETCH_AND_OR_8:
kono
parents:
diff changeset
918 case BUILT_IN_SYNC_FETCH_AND_AND_8:
kono
parents:
diff changeset
919 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
kono
parents:
diff changeset
920 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
kono
parents:
diff changeset
921 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
kono
parents:
diff changeset
922 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
kono
parents:
diff changeset
923 case BUILT_IN_SYNC_OR_AND_FETCH_8:
kono
parents:
diff changeset
924 case BUILT_IN_SYNC_AND_AND_FETCH_8:
kono
parents:
diff changeset
925 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
kono
parents:
diff changeset
926 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
kono
parents:
diff changeset
927 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
kono
parents:
diff changeset
928 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
kono
parents:
diff changeset
929 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
kono
parents:
diff changeset
930 case BUILT_IN_SYNC_LOCK_RELEASE_8:
kono
parents:
diff changeset
931 case BUILT_IN_ATOMIC_EXCHANGE_8:
kono
parents:
diff changeset
932 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
kono
parents:
diff changeset
933 case BUILT_IN_ATOMIC_STORE_8:
kono
parents:
diff changeset
934 case BUILT_IN_ATOMIC_ADD_FETCH_8:
kono
parents:
diff changeset
935 case BUILT_IN_ATOMIC_SUB_FETCH_8:
kono
parents:
diff changeset
936 case BUILT_IN_ATOMIC_AND_FETCH_8:
kono
parents:
diff changeset
937 case BUILT_IN_ATOMIC_NAND_FETCH_8:
kono
parents:
diff changeset
938 case BUILT_IN_ATOMIC_XOR_FETCH_8:
kono
parents:
diff changeset
939 case BUILT_IN_ATOMIC_OR_FETCH_8:
kono
parents:
diff changeset
940 case BUILT_IN_ATOMIC_FETCH_ADD_8:
kono
parents:
diff changeset
941 case BUILT_IN_ATOMIC_FETCH_SUB_8:
kono
parents:
diff changeset
942 case BUILT_IN_ATOMIC_FETCH_AND_8:
kono
parents:
diff changeset
943 case BUILT_IN_ATOMIC_FETCH_NAND_8:
kono
parents:
diff changeset
944 case BUILT_IN_ATOMIC_FETCH_XOR_8:
kono
parents:
diff changeset
945 case BUILT_IN_ATOMIC_FETCH_OR_8:
kono
parents:
diff changeset
946 access_size = 8;
kono
parents:
diff changeset
947 goto do_atomic;
kono
parents:
diff changeset
948
kono
parents:
diff changeset
949 case BUILT_IN_ATOMIC_LOAD_16:
kono
parents:
diff changeset
950 is_store = false;
kono
parents:
diff changeset
951 /* FALLTHRU */
kono
parents:
diff changeset
952 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
kono
parents:
diff changeset
953 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
kono
parents:
diff changeset
954 case BUILT_IN_SYNC_FETCH_AND_OR_16:
kono
parents:
diff changeset
955 case BUILT_IN_SYNC_FETCH_AND_AND_16:
kono
parents:
diff changeset
956 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
kono
parents:
diff changeset
957 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
kono
parents:
diff changeset
958 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
kono
parents:
diff changeset
959 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
kono
parents:
diff changeset
960 case BUILT_IN_SYNC_OR_AND_FETCH_16:
kono
parents:
diff changeset
961 case BUILT_IN_SYNC_AND_AND_FETCH_16:
kono
parents:
diff changeset
962 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
kono
parents:
diff changeset
963 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
kono
parents:
diff changeset
964 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
kono
parents:
diff changeset
965 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
kono
parents:
diff changeset
966 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
kono
parents:
diff changeset
967 case BUILT_IN_SYNC_LOCK_RELEASE_16:
kono
parents:
diff changeset
968 case BUILT_IN_ATOMIC_EXCHANGE_16:
kono
parents:
diff changeset
969 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
kono
parents:
diff changeset
970 case BUILT_IN_ATOMIC_STORE_16:
kono
parents:
diff changeset
971 case BUILT_IN_ATOMIC_ADD_FETCH_16:
kono
parents:
diff changeset
972 case BUILT_IN_ATOMIC_SUB_FETCH_16:
kono
parents:
diff changeset
973 case BUILT_IN_ATOMIC_AND_FETCH_16:
kono
parents:
diff changeset
974 case BUILT_IN_ATOMIC_NAND_FETCH_16:
kono
parents:
diff changeset
975 case BUILT_IN_ATOMIC_XOR_FETCH_16:
kono
parents:
diff changeset
976 case BUILT_IN_ATOMIC_OR_FETCH_16:
kono
parents:
diff changeset
977 case BUILT_IN_ATOMIC_FETCH_ADD_16:
kono
parents:
diff changeset
978 case BUILT_IN_ATOMIC_FETCH_SUB_16:
kono
parents:
diff changeset
979 case BUILT_IN_ATOMIC_FETCH_AND_16:
kono
parents:
diff changeset
980 case BUILT_IN_ATOMIC_FETCH_NAND_16:
kono
parents:
diff changeset
981 case BUILT_IN_ATOMIC_FETCH_XOR_16:
kono
parents:
diff changeset
982 case BUILT_IN_ATOMIC_FETCH_OR_16:
kono
parents:
diff changeset
983 access_size = 16;
kono
parents:
diff changeset
984 /* FALLTHRU */
kono
parents:
diff changeset
985 do_atomic:
kono
parents:
diff changeset
986 {
kono
parents:
diff changeset
987 dest = gimple_call_arg (call, 0);
kono
parents:
diff changeset
988 /* DEST represents the address of a memory location.
kono
parents:
diff changeset
989 instrument_derefs wants the memory location, so lets
kono
parents:
diff changeset
990 dereference the address DEST before handing it to
kono
parents:
diff changeset
991 instrument_derefs. */
kono
parents:
diff changeset
992 tree type = build_nonstandard_integer_type (access_size
kono
parents:
diff changeset
993 * BITS_PER_UNIT, 1);
kono
parents:
diff changeset
994 dest = build2 (MEM_REF, type, dest,
kono
parents:
diff changeset
995 build_int_cst (build_pointer_type (char_type_node), 0));
kono
parents:
diff changeset
996 break;
kono
parents:
diff changeset
997 }
kono
parents:
diff changeset
998
kono
parents:
diff changeset
999 default:
kono
parents:
diff changeset
1000 /* The other builtins memory access are not instrumented in this
kono
parents:
diff changeset
1001 function because they either don't have any length parameter,
kono
parents:
diff changeset
1002 or their length parameter is just a limit. */
kono
parents:
diff changeset
1003 break;
kono
parents:
diff changeset
1004 }
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 if (len != NULL_TREE)
kono
parents:
diff changeset
1007 {
kono
parents:
diff changeset
1008 if (source0 != NULL_TREE)
kono
parents:
diff changeset
1009 {
kono
parents:
diff changeset
1010 src0->start = source0;
kono
parents:
diff changeset
1011 src0->access_size = access_size;
kono
parents:
diff changeset
1012 *src0_len = len;
kono
parents:
diff changeset
1013 *src0_is_store = false;
kono
parents:
diff changeset
1014 }
kono
parents:
diff changeset
1015
kono
parents:
diff changeset
1016 if (source1 != NULL_TREE)
kono
parents:
diff changeset
1017 {
kono
parents:
diff changeset
1018 src1->start = source1;
kono
parents:
diff changeset
1019 src1->access_size = access_size;
kono
parents:
diff changeset
1020 *src1_len = len;
kono
parents:
diff changeset
1021 *src1_is_store = false;
kono
parents:
diff changeset
1022 }
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 if (dest != NULL_TREE)
kono
parents:
diff changeset
1025 {
kono
parents:
diff changeset
1026 dst->start = dest;
kono
parents:
diff changeset
1027 dst->access_size = access_size;
kono
parents:
diff changeset
1028 *dst_len = len;
kono
parents:
diff changeset
1029 *dst_is_store = true;
kono
parents:
diff changeset
1030 }
kono
parents:
diff changeset
1031
kono
parents:
diff changeset
1032 got_reference_p = true;
kono
parents:
diff changeset
1033 }
kono
parents:
diff changeset
1034 else if (dest)
kono
parents:
diff changeset
1035 {
kono
parents:
diff changeset
1036 dst->start = dest;
kono
parents:
diff changeset
1037 dst->access_size = access_size;
kono
parents:
diff changeset
1038 *dst_len = NULL_TREE;
kono
parents:
diff changeset
1039 *dst_is_store = is_store;
kono
parents:
diff changeset
1040 *dest_is_deref = true;
kono
parents:
diff changeset
1041 got_reference_p = true;
kono
parents:
diff changeset
1042 }
kono
parents:
diff changeset
1043
kono
parents:
diff changeset
1044 return got_reference_p;
kono
parents:
diff changeset
1045 }
kono
parents:
diff changeset
1046
kono
parents:
diff changeset
1047 /* Return true iff a given gimple statement has been instrumented.
kono
parents:
diff changeset
1048 Note that the statement is "defined" by the memory references it
kono
parents:
diff changeset
1049 contains. */
kono
parents:
diff changeset
1050
kono
parents:
diff changeset
1051 static bool
kono
parents:
diff changeset
1052 has_stmt_been_instrumented_p (gimple *stmt)
kono
parents:
diff changeset
1053 {
kono
parents:
diff changeset
1054 if (gimple_assign_single_p (stmt))
kono
parents:
diff changeset
1055 {
kono
parents:
diff changeset
1056 bool r_is_store;
kono
parents:
diff changeset
1057 asan_mem_ref r;
kono
parents:
diff changeset
1058 asan_mem_ref_init (&r, NULL, 1);
kono
parents:
diff changeset
1059
kono
parents:
diff changeset
1060 if (get_mem_ref_of_assignment (as_a <gassign *> (stmt), &r,
kono
parents:
diff changeset
1061 &r_is_store))
kono
parents:
diff changeset
1062 return has_mem_ref_been_instrumented (&r);
kono
parents:
diff changeset
1063 }
kono
parents:
diff changeset
1064 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
kono
parents:
diff changeset
1065 {
kono
parents:
diff changeset
1066 asan_mem_ref src0, src1, dest;
kono
parents:
diff changeset
1067 asan_mem_ref_init (&src0, NULL, 1);
kono
parents:
diff changeset
1068 asan_mem_ref_init (&src1, NULL, 1);
kono
parents:
diff changeset
1069 asan_mem_ref_init (&dest, NULL, 1);
kono
parents:
diff changeset
1070
kono
parents:
diff changeset
1071 tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE;
kono
parents:
diff changeset
1072 bool src0_is_store = false, src1_is_store = false,
kono
parents:
diff changeset
1073 dest_is_store = false, dest_is_deref = false, intercepted_p = true;
kono
parents:
diff changeset
1074 if (get_mem_refs_of_builtin_call (as_a <gcall *> (stmt),
kono
parents:
diff changeset
1075 &src0, &src0_len, &src0_is_store,
kono
parents:
diff changeset
1076 &src1, &src1_len, &src1_is_store,
kono
parents:
diff changeset
1077 &dest, &dest_len, &dest_is_store,
kono
parents:
diff changeset
1078 &dest_is_deref, &intercepted_p))
kono
parents:
diff changeset
1079 {
kono
parents:
diff changeset
1080 if (src0.start != NULL_TREE
kono
parents:
diff changeset
1081 && !has_mem_ref_been_instrumented (&src0, src0_len))
kono
parents:
diff changeset
1082 return false;
kono
parents:
diff changeset
1083
kono
parents:
diff changeset
1084 if (src1.start != NULL_TREE
kono
parents:
diff changeset
1085 && !has_mem_ref_been_instrumented (&src1, src1_len))
kono
parents:
diff changeset
1086 return false;
kono
parents:
diff changeset
1087
kono
parents:
diff changeset
1088 if (dest.start != NULL_TREE
kono
parents:
diff changeset
1089 && !has_mem_ref_been_instrumented (&dest, dest_len))
kono
parents:
diff changeset
1090 return false;
kono
parents:
diff changeset
1091
kono
parents:
diff changeset
1092 return true;
kono
parents:
diff changeset
1093 }
kono
parents:
diff changeset
1094 }
kono
parents:
diff changeset
1095 else if (is_gimple_call (stmt) && gimple_store_p (stmt))
kono
parents:
diff changeset
1096 {
kono
parents:
diff changeset
1097 asan_mem_ref r;
kono
parents:
diff changeset
1098 asan_mem_ref_init (&r, NULL, 1);
kono
parents:
diff changeset
1099
kono
parents:
diff changeset
1100 r.start = gimple_call_lhs (stmt);
kono
parents:
diff changeset
1101 r.access_size = int_size_in_bytes (TREE_TYPE (r.start));
kono
parents:
diff changeset
1102 return has_mem_ref_been_instrumented (&r);
kono
parents:
diff changeset
1103 }
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 return false;
kono
parents:
diff changeset
1106 }
kono
parents:
diff changeset
1107
kono
parents:
diff changeset
1108 /* Insert a memory reference into the hash table. */
kono
parents:
diff changeset
1109
kono
parents:
diff changeset
1110 static void
kono
parents:
diff changeset
1111 update_mem_ref_hash_table (tree ref, HOST_WIDE_INT access_size)
kono
parents:
diff changeset
1112 {
kono
parents:
diff changeset
1113 hash_table<asan_mem_ref_hasher> *ht = get_mem_ref_hash_table ();
kono
parents:
diff changeset
1114
kono
parents:
diff changeset
1115 asan_mem_ref r;
kono
parents:
diff changeset
1116 asan_mem_ref_init (&r, ref, access_size);
kono
parents:
diff changeset
1117
kono
parents:
diff changeset
1118 asan_mem_ref **slot = ht->find_slot (&r, INSERT);
kono
parents:
diff changeset
1119 if (*slot == NULL || (*slot)->access_size < access_size)
kono
parents:
diff changeset
1120 *slot = asan_mem_ref_new (ref, access_size);
kono
parents:
diff changeset
1121 }
kono
parents:
diff changeset
1122
kono
parents:
diff changeset
1123 /* Initialize shadow_ptr_types array. */
kono
parents:
diff changeset
1124
kono
parents:
diff changeset
1125 static void
kono
parents:
diff changeset
1126 asan_init_shadow_ptr_types (void)
kono
parents:
diff changeset
1127 {
kono
parents:
diff changeset
1128 asan_shadow_set = new_alias_set ();
kono
parents:
diff changeset
1129 tree types[3] = { signed_char_type_node, short_integer_type_node,
kono
parents:
diff changeset
1130 integer_type_node };
kono
parents:
diff changeset
1131
kono
parents:
diff changeset
1132 for (unsigned i = 0; i < 3; i++)
kono
parents:
diff changeset
1133 {
kono
parents:
diff changeset
1134 shadow_ptr_types[i] = build_distinct_type_copy (types[i]);
kono
parents:
diff changeset
1135 TYPE_ALIAS_SET (shadow_ptr_types[i]) = asan_shadow_set;
kono
parents:
diff changeset
1136 shadow_ptr_types[i] = build_pointer_type (shadow_ptr_types[i]);
kono
parents:
diff changeset
1137 }
kono
parents:
diff changeset
1138
kono
parents:
diff changeset
1139 initialize_sanitizer_builtins ();
kono
parents:
diff changeset
1140 }
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 /* Create ADDR_EXPR of STRING_CST with the PP pretty printer text. */
kono
parents:
diff changeset
1143
kono
parents:
diff changeset
1144 static tree
kono
parents:
diff changeset
1145 asan_pp_string (pretty_printer *pp)
kono
parents:
diff changeset
1146 {
kono
parents:
diff changeset
1147 const char *buf = pp_formatted_text (pp);
kono
parents:
diff changeset
1148 size_t len = strlen (buf);
kono
parents:
diff changeset
1149 tree ret = build_string (len + 1, buf);
kono
parents:
diff changeset
1150 TREE_TYPE (ret)
kono
parents:
diff changeset
1151 = build_array_type (TREE_TYPE (shadow_ptr_types[0]),
kono
parents:
diff changeset
1152 build_index_type (size_int (len)));
kono
parents:
diff changeset
1153 TREE_READONLY (ret) = 1;
kono
parents:
diff changeset
1154 TREE_STATIC (ret) = 1;
kono
parents:
diff changeset
1155 return build1 (ADDR_EXPR, shadow_ptr_types[0], ret);
kono
parents:
diff changeset
1156 }
kono
parents:
diff changeset
1157
kono
parents:
diff changeset
1158 /* Return a CONST_INT representing 4 subsequent shadow memory bytes. */
kono
parents:
diff changeset
1159
kono
parents:
diff changeset
1160 static rtx
kono
parents:
diff changeset
1161 asan_shadow_cst (unsigned char shadow_bytes[4])
kono
parents:
diff changeset
1162 {
kono
parents:
diff changeset
1163 int i;
kono
parents:
diff changeset
1164 unsigned HOST_WIDE_INT val = 0;
kono
parents:
diff changeset
1165 gcc_assert (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN);
kono
parents:
diff changeset
1166 for (i = 0; i < 4; i++)
kono
parents:
diff changeset
1167 val |= (unsigned HOST_WIDE_INT) shadow_bytes[BYTES_BIG_ENDIAN ? 3 - i : i]
kono
parents:
diff changeset
1168 << (BITS_PER_UNIT * i);
kono
parents:
diff changeset
1169 return gen_int_mode (val, SImode);
kono
parents:
diff changeset
1170 }
kono
parents:
diff changeset
1171
kono
parents:
diff changeset
1172 /* Clear shadow memory at SHADOW_MEM, LEN bytes. Can't call a library call here
kono
parents:
diff changeset
1173 though. */
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 static void
kono
parents:
diff changeset
1176 asan_clear_shadow (rtx shadow_mem, HOST_WIDE_INT len)
kono
parents:
diff changeset
1177 {
kono
parents:
diff changeset
1178 rtx_insn *insn, *insns, *jump;
kono
parents:
diff changeset
1179 rtx_code_label *top_label;
kono
parents:
diff changeset
1180 rtx end, addr, tmp;
kono
parents:
diff changeset
1181
kono
parents:
diff changeset
1182 start_sequence ();
kono
parents:
diff changeset
1183 clear_storage (shadow_mem, GEN_INT (len), BLOCK_OP_NORMAL);
kono
parents:
diff changeset
1184 insns = get_insns ();
kono
parents:
diff changeset
1185 end_sequence ();
kono
parents:
diff changeset
1186 for (insn = insns; insn; insn = NEXT_INSN (insn))
kono
parents:
diff changeset
1187 if (CALL_P (insn))
kono
parents:
diff changeset
1188 break;
kono
parents:
diff changeset
1189 if (insn == NULL_RTX)
kono
parents:
diff changeset
1190 {
kono
parents:
diff changeset
1191 emit_insn (insns);
kono
parents:
diff changeset
1192 return;
kono
parents:
diff changeset
1193 }
kono
parents:
diff changeset
1194
kono
parents:
diff changeset
1195 gcc_assert ((len & 3) == 0);
kono
parents:
diff changeset
1196 top_label = gen_label_rtx ();
kono
parents:
diff changeset
1197 addr = copy_to_mode_reg (Pmode, XEXP (shadow_mem, 0));
kono
parents:
diff changeset
1198 shadow_mem = adjust_automodify_address (shadow_mem, SImode, addr, 0);
kono
parents:
diff changeset
1199 end = force_reg (Pmode, plus_constant (Pmode, addr, len));
kono
parents:
diff changeset
1200 emit_label (top_label);
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 emit_move_insn (shadow_mem, const0_rtx);
kono
parents:
diff changeset
1203 tmp = expand_simple_binop (Pmode, PLUS, addr, gen_int_mode (4, Pmode), addr,
kono
parents:
diff changeset
1204 true, OPTAB_LIB_WIDEN);
kono
parents:
diff changeset
1205 if (tmp != addr)
kono
parents:
diff changeset
1206 emit_move_insn (addr, tmp);
kono
parents:
diff changeset
1207 emit_cmp_and_jump_insns (addr, end, LT, NULL_RTX, Pmode, true, top_label);
kono
parents:
diff changeset
1208 jump = get_last_insn ();
kono
parents:
diff changeset
1209 gcc_assert (JUMP_P (jump));
kono
parents:
diff changeset
1210 add_reg_br_prob_note (jump,
kono
parents:
diff changeset
1211 profile_probability::guessed_always ()
kono
parents:
diff changeset
1212 .apply_scale (80, 100));
kono
parents:
diff changeset
1213 }
kono
parents:
diff changeset
1214
kono
parents:
diff changeset
1215 void
kono
parents:
diff changeset
1216 asan_function_start (void)
kono
parents:
diff changeset
1217 {
kono
parents:
diff changeset
1218 section *fnsec = function_section (current_function_decl);
kono
parents:
diff changeset
1219 switch_to_section (fnsec);
kono
parents:
diff changeset
1220 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LASANPC",
kono
parents:
diff changeset
1221 current_function_funcdef_no);
kono
parents:
diff changeset
1222 }
kono
parents:
diff changeset
1223
kono
parents:
diff changeset
1224 /* Return number of shadow bytes that are occupied by a local variable
kono
parents:
diff changeset
1225 of SIZE bytes. */
kono
parents:
diff changeset
1226
kono
parents:
diff changeset
1227 static unsigned HOST_WIDE_INT
kono
parents:
diff changeset
1228 shadow_mem_size (unsigned HOST_WIDE_INT size)
kono
parents:
diff changeset
1229 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1230 /* It must be possible to align stack variables to granularity
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1231 of shadow memory. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1232 gcc_assert (BITS_PER_UNIT
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1233 * ASAN_SHADOW_GRANULARITY <= MAX_SUPPORTED_STACK_ALIGNMENT);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1234
111
kono
parents:
diff changeset
1235 return ROUND_UP (size, ASAN_SHADOW_GRANULARITY) / ASAN_SHADOW_GRANULARITY;
kono
parents:
diff changeset
1236 }
kono
parents:
diff changeset
1237
kono
parents:
diff changeset
1238 /* Insert code to protect stack vars. The prologue sequence should be emitted
kono
parents:
diff changeset
1239 directly, epilogue sequence returned. BASE is the register holding the
kono
parents:
diff changeset
1240 stack base, against which OFFSETS array offsets are relative to, OFFSETS
kono
parents:
diff changeset
1241 array contains pairs of offsets in reverse order, always the end offset
kono
parents:
diff changeset
1242 of some gap that needs protection followed by starting offset,
kono
parents:
diff changeset
1243 and DECLS is an array of representative decls for each var partition.
kono
parents:
diff changeset
1244 LENGTH is the length of the OFFSETS array, DECLS array is LENGTH / 2 - 1
kono
parents:
diff changeset
1245 elements long (OFFSETS include gap before the first variable as well
kono
parents:
diff changeset
1246 as gaps after each stack variable). PBASE is, if non-NULL, some pseudo
kono
parents:
diff changeset
1247 register which stack vars DECL_RTLs are based on. Either BASE should be
kono
parents:
diff changeset
1248 assigned to PBASE, when not doing use after return protection, or
kono
parents:
diff changeset
1249 corresponding address based on __asan_stack_malloc* return value. */
kono
parents:
diff changeset
1250
kono
parents:
diff changeset
1251 rtx_insn *
kono
parents:
diff changeset
1252 asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb,
kono
parents:
diff changeset
1253 HOST_WIDE_INT *offsets, tree *decls, int length)
kono
parents:
diff changeset
1254 {
kono
parents:
diff changeset
1255 rtx shadow_base, shadow_mem, ret, mem, orig_base;
kono
parents:
diff changeset
1256 rtx_code_label *lab;
kono
parents:
diff changeset
1257 rtx_insn *insns;
kono
parents:
diff changeset
1258 char buf[32];
kono
parents:
diff changeset
1259 unsigned char shadow_bytes[4];
kono
parents:
diff changeset
1260 HOST_WIDE_INT base_offset = offsets[length - 1];
kono
parents:
diff changeset
1261 HOST_WIDE_INT base_align_bias = 0, offset, prev_offset;
kono
parents:
diff changeset
1262 HOST_WIDE_INT asan_frame_size = offsets[0] - base_offset;
kono
parents:
diff changeset
1263 HOST_WIDE_INT last_offset, last_size;
kono
parents:
diff changeset
1264 int l;
kono
parents:
diff changeset
1265 unsigned char cur_shadow_byte = ASAN_STACK_MAGIC_LEFT;
kono
parents:
diff changeset
1266 tree str_cst, decl, id;
kono
parents:
diff changeset
1267 int use_after_return_class = -1;
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 if (shadow_ptr_types[0] == NULL_TREE)
kono
parents:
diff changeset
1270 asan_init_shadow_ptr_types ();
kono
parents:
diff changeset
1271
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1272 expanded_location cfun_xloc
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1273 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1274
111
kono
parents:
diff changeset
1275 /* First of all, prepare the description string. */
kono
parents:
diff changeset
1276 pretty_printer asan_pp;
kono
parents:
diff changeset
1277
kono
parents:
diff changeset
1278 pp_decimal_int (&asan_pp, length / 2 - 1);
kono
parents:
diff changeset
1279 pp_space (&asan_pp);
kono
parents:
diff changeset
1280 for (l = length - 2; l; l -= 2)
kono
parents:
diff changeset
1281 {
kono
parents:
diff changeset
1282 tree decl = decls[l / 2 - 1];
kono
parents:
diff changeset
1283 pp_wide_integer (&asan_pp, offsets[l] - base_offset);
kono
parents:
diff changeset
1284 pp_space (&asan_pp);
kono
parents:
diff changeset
1285 pp_wide_integer (&asan_pp, offsets[l - 1] - offsets[l]);
kono
parents:
diff changeset
1286 pp_space (&asan_pp);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1287
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1288 expanded_location xloc
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1289 = expand_location (DECL_SOURCE_LOCATION (decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1290 char location[32];
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1291
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1292 if (xloc.file == cfun_xloc.file)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1293 sprintf (location, ":%d", xloc.line);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1294 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1295 location[0] = '\0';
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1296
111
kono
parents:
diff changeset
1297 if (DECL_P (decl) && DECL_NAME (decl))
kono
parents:
diff changeset
1298 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1299 unsigned idlen
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1300 = IDENTIFIER_LENGTH (DECL_NAME (decl)) + strlen (location);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1301 pp_decimal_int (&asan_pp, idlen);
111
kono
parents:
diff changeset
1302 pp_space (&asan_pp);
kono
parents:
diff changeset
1303 pp_tree_identifier (&asan_pp, DECL_NAME (decl));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1304 pp_string (&asan_pp, location);
111
kono
parents:
diff changeset
1305 }
kono
parents:
diff changeset
1306 else
kono
parents:
diff changeset
1307 pp_string (&asan_pp, "9 <unknown>");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1308
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1309 if (l > 2)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1310 pp_space (&asan_pp);
111
kono
parents:
diff changeset
1311 }
kono
parents:
diff changeset
1312 str_cst = asan_pp_string (&asan_pp);
kono
parents:
diff changeset
1313
kono
parents:
diff changeset
1314 /* Emit the prologue sequence. */
kono
parents:
diff changeset
1315 if (asan_frame_size > 32 && asan_frame_size <= 65536 && pbase
kono
parents:
diff changeset
1316 && ASAN_USE_AFTER_RETURN)
kono
parents:
diff changeset
1317 {
kono
parents:
diff changeset
1318 use_after_return_class = floor_log2 (asan_frame_size - 1) - 5;
kono
parents:
diff changeset
1319 /* __asan_stack_malloc_N guarantees alignment
kono
parents:
diff changeset
1320 N < 6 ? (64 << N) : 4096 bytes. */
kono
parents:
diff changeset
1321 if (alignb > (use_after_return_class < 6
kono
parents:
diff changeset
1322 ? (64U << use_after_return_class) : 4096U))
kono
parents:
diff changeset
1323 use_after_return_class = -1;
kono
parents:
diff changeset
1324 else if (alignb > ASAN_RED_ZONE_SIZE && (asan_frame_size & (alignb - 1)))
kono
parents:
diff changeset
1325 base_align_bias = ((asan_frame_size + alignb - 1)
kono
parents:
diff changeset
1326 & ~(alignb - HOST_WIDE_INT_1)) - asan_frame_size;
kono
parents:
diff changeset
1327 }
kono
parents:
diff changeset
1328 /* Align base if target is STRICT_ALIGNMENT. */
kono
parents:
diff changeset
1329 if (STRICT_ALIGNMENT)
kono
parents:
diff changeset
1330 base = expand_binop (Pmode, and_optab, base,
kono
parents:
diff changeset
1331 gen_int_mode (-((GET_MODE_ALIGNMENT (SImode)
kono
parents:
diff changeset
1332 << ASAN_SHADOW_SHIFT)
kono
parents:
diff changeset
1333 / BITS_PER_UNIT), Pmode), NULL_RTX,
kono
parents:
diff changeset
1334 1, OPTAB_DIRECT);
kono
parents:
diff changeset
1335
kono
parents:
diff changeset
1336 if (use_after_return_class == -1 && pbase)
kono
parents:
diff changeset
1337 emit_move_insn (pbase, base);
kono
parents:
diff changeset
1338
kono
parents:
diff changeset
1339 base = expand_binop (Pmode, add_optab, base,
kono
parents:
diff changeset
1340 gen_int_mode (base_offset - base_align_bias, Pmode),
kono
parents:
diff changeset
1341 NULL_RTX, 1, OPTAB_DIRECT);
kono
parents:
diff changeset
1342 orig_base = NULL_RTX;
kono
parents:
diff changeset
1343 if (use_after_return_class != -1)
kono
parents:
diff changeset
1344 {
kono
parents:
diff changeset
1345 if (asan_detect_stack_use_after_return == NULL_TREE)
kono
parents:
diff changeset
1346 {
kono
parents:
diff changeset
1347 id = get_identifier ("__asan_option_detect_stack_use_after_return");
kono
parents:
diff changeset
1348 decl = build_decl (BUILTINS_LOCATION, VAR_DECL, id,
kono
parents:
diff changeset
1349 integer_type_node);
kono
parents:
diff changeset
1350 SET_DECL_ASSEMBLER_NAME (decl, id);
kono
parents:
diff changeset
1351 TREE_ADDRESSABLE (decl) = 1;
kono
parents:
diff changeset
1352 DECL_ARTIFICIAL (decl) = 1;
kono
parents:
diff changeset
1353 DECL_IGNORED_P (decl) = 1;
kono
parents:
diff changeset
1354 DECL_EXTERNAL (decl) = 1;
kono
parents:
diff changeset
1355 TREE_STATIC (decl) = 1;
kono
parents:
diff changeset
1356 TREE_PUBLIC (decl) = 1;
kono
parents:
diff changeset
1357 TREE_USED (decl) = 1;
kono
parents:
diff changeset
1358 asan_detect_stack_use_after_return = decl;
kono
parents:
diff changeset
1359 }
kono
parents:
diff changeset
1360 orig_base = gen_reg_rtx (Pmode);
kono
parents:
diff changeset
1361 emit_move_insn (orig_base, base);
kono
parents:
diff changeset
1362 ret = expand_normal (asan_detect_stack_use_after_return);
kono
parents:
diff changeset
1363 lab = gen_label_rtx ();
kono
parents:
diff changeset
1364 emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX,
kono
parents:
diff changeset
1365 VOIDmode, 0, lab,
kono
parents:
diff changeset
1366 profile_probability::very_likely ());
kono
parents:
diff changeset
1367 snprintf (buf, sizeof buf, "__asan_stack_malloc_%d",
kono
parents:
diff changeset
1368 use_after_return_class);
kono
parents:
diff changeset
1369 ret = init_one_libfunc (buf);
kono
parents:
diff changeset
1370 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
kono
parents:
diff changeset
1371 GEN_INT (asan_frame_size
kono
parents:
diff changeset
1372 + base_align_bias),
kono
parents:
diff changeset
1373 TYPE_MODE (pointer_sized_int_node));
kono
parents:
diff changeset
1374 /* __asan_stack_malloc_[n] returns a pointer to fake stack if succeeded
kono
parents:
diff changeset
1375 and NULL otherwise. Check RET value is NULL here and jump over the
kono
parents:
diff changeset
1376 BASE reassignment in this case. Otherwise, reassign BASE to RET. */
kono
parents:
diff changeset
1377 emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX,
kono
parents:
diff changeset
1378 VOIDmode, 0, lab,
kono
parents:
diff changeset
1379 profile_probability:: very_unlikely ());
kono
parents:
diff changeset
1380 ret = convert_memory_address (Pmode, ret);
kono
parents:
diff changeset
1381 emit_move_insn (base, ret);
kono
parents:
diff changeset
1382 emit_label (lab);
kono
parents:
diff changeset
1383 emit_move_insn (pbase, expand_binop (Pmode, add_optab, base,
kono
parents:
diff changeset
1384 gen_int_mode (base_align_bias
kono
parents:
diff changeset
1385 - base_offset, Pmode),
kono
parents:
diff changeset
1386 NULL_RTX, 1, OPTAB_DIRECT));
kono
parents:
diff changeset
1387 }
kono
parents:
diff changeset
1388 mem = gen_rtx_MEM (ptr_mode, base);
kono
parents:
diff changeset
1389 mem = adjust_address (mem, VOIDmode, base_align_bias);
kono
parents:
diff changeset
1390 emit_move_insn (mem, gen_int_mode (ASAN_STACK_FRAME_MAGIC, ptr_mode));
kono
parents:
diff changeset
1391 mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode));
kono
parents:
diff changeset
1392 emit_move_insn (mem, expand_normal (str_cst));
kono
parents:
diff changeset
1393 mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode));
kono
parents:
diff changeset
1394 ASM_GENERATE_INTERNAL_LABEL (buf, "LASANPC", current_function_funcdef_no);
kono
parents:
diff changeset
1395 id = get_identifier (buf);
kono
parents:
diff changeset
1396 decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
kono
parents:
diff changeset
1397 VAR_DECL, id, char_type_node);
kono
parents:
diff changeset
1398 SET_DECL_ASSEMBLER_NAME (decl, id);
kono
parents:
diff changeset
1399 TREE_ADDRESSABLE (decl) = 1;
kono
parents:
diff changeset
1400 TREE_READONLY (decl) = 1;
kono
parents:
diff changeset
1401 DECL_ARTIFICIAL (decl) = 1;
kono
parents:
diff changeset
1402 DECL_IGNORED_P (decl) = 1;
kono
parents:
diff changeset
1403 TREE_STATIC (decl) = 1;
kono
parents:
diff changeset
1404 TREE_PUBLIC (decl) = 0;
kono
parents:
diff changeset
1405 TREE_USED (decl) = 1;
kono
parents:
diff changeset
1406 DECL_INITIAL (decl) = decl;
kono
parents:
diff changeset
1407 TREE_ASM_WRITTEN (decl) = 1;
kono
parents:
diff changeset
1408 TREE_ASM_WRITTEN (id) = 1;
kono
parents:
diff changeset
1409 emit_move_insn (mem, expand_normal (build_fold_addr_expr (decl)));
kono
parents:
diff changeset
1410 shadow_base = expand_binop (Pmode, lshr_optab, base,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1411 gen_int_shift_amount (Pmode, ASAN_SHADOW_SHIFT),
111
kono
parents:
diff changeset
1412 NULL_RTX, 1, OPTAB_DIRECT);
kono
parents:
diff changeset
1413 shadow_base
kono
parents:
diff changeset
1414 = plus_constant (Pmode, shadow_base,
kono
parents:
diff changeset
1415 asan_shadow_offset ()
kono
parents:
diff changeset
1416 + (base_align_bias >> ASAN_SHADOW_SHIFT));
kono
parents:
diff changeset
1417 gcc_assert (asan_shadow_set != -1
kono
parents:
diff changeset
1418 && (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4);
kono
parents:
diff changeset
1419 shadow_mem = gen_rtx_MEM (SImode, shadow_base);
kono
parents:
diff changeset
1420 set_mem_alias_set (shadow_mem, asan_shadow_set);
kono
parents:
diff changeset
1421 if (STRICT_ALIGNMENT)
kono
parents:
diff changeset
1422 set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode)));
kono
parents:
diff changeset
1423 prev_offset = base_offset;
kono
parents:
diff changeset
1424 for (l = length; l; l -= 2)
kono
parents:
diff changeset
1425 {
kono
parents:
diff changeset
1426 if (l == 2)
kono
parents:
diff changeset
1427 cur_shadow_byte = ASAN_STACK_MAGIC_RIGHT;
kono
parents:
diff changeset
1428 offset = offsets[l - 1];
kono
parents:
diff changeset
1429 if ((offset - base_offset) & (ASAN_RED_ZONE_SIZE - 1))
kono
parents:
diff changeset
1430 {
kono
parents:
diff changeset
1431 int i;
kono
parents:
diff changeset
1432 HOST_WIDE_INT aoff
kono
parents:
diff changeset
1433 = base_offset + ((offset - base_offset)
kono
parents:
diff changeset
1434 & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1));
kono
parents:
diff changeset
1435 shadow_mem = adjust_address (shadow_mem, VOIDmode,
kono
parents:
diff changeset
1436 (aoff - prev_offset)
kono
parents:
diff changeset
1437 >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1438 prev_offset = aoff;
kono
parents:
diff changeset
1439 for (i = 0; i < 4; i++, aoff += ASAN_SHADOW_GRANULARITY)
kono
parents:
diff changeset
1440 if (aoff < offset)
kono
parents:
diff changeset
1441 {
kono
parents:
diff changeset
1442 if (aoff < offset - (HOST_WIDE_INT)ASAN_SHADOW_GRANULARITY + 1)
kono
parents:
diff changeset
1443 shadow_bytes[i] = 0;
kono
parents:
diff changeset
1444 else
kono
parents:
diff changeset
1445 shadow_bytes[i] = offset - aoff;
kono
parents:
diff changeset
1446 }
kono
parents:
diff changeset
1447 else
kono
parents:
diff changeset
1448 shadow_bytes[i] = ASAN_STACK_MAGIC_MIDDLE;
kono
parents:
diff changeset
1449 emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes));
kono
parents:
diff changeset
1450 offset = aoff;
kono
parents:
diff changeset
1451 }
kono
parents:
diff changeset
1452 while (offset <= offsets[l - 2] - ASAN_RED_ZONE_SIZE)
kono
parents:
diff changeset
1453 {
kono
parents:
diff changeset
1454 shadow_mem = adjust_address (shadow_mem, VOIDmode,
kono
parents:
diff changeset
1455 (offset - prev_offset)
kono
parents:
diff changeset
1456 >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1457 prev_offset = offset;
kono
parents:
diff changeset
1458 memset (shadow_bytes, cur_shadow_byte, 4);
kono
parents:
diff changeset
1459 emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes));
kono
parents:
diff changeset
1460 offset += ASAN_RED_ZONE_SIZE;
kono
parents:
diff changeset
1461 }
kono
parents:
diff changeset
1462 cur_shadow_byte = ASAN_STACK_MAGIC_MIDDLE;
kono
parents:
diff changeset
1463 }
kono
parents:
diff changeset
1464 do_pending_stack_adjust ();
kono
parents:
diff changeset
1465
kono
parents:
diff changeset
1466 /* Construct epilogue sequence. */
kono
parents:
diff changeset
1467 start_sequence ();
kono
parents:
diff changeset
1468
kono
parents:
diff changeset
1469 lab = NULL;
kono
parents:
diff changeset
1470 if (use_after_return_class != -1)
kono
parents:
diff changeset
1471 {
kono
parents:
diff changeset
1472 rtx_code_label *lab2 = gen_label_rtx ();
kono
parents:
diff changeset
1473 char c = (char) ASAN_STACK_MAGIC_USE_AFTER_RET;
kono
parents:
diff changeset
1474 emit_cmp_and_jump_insns (orig_base, base, EQ, NULL_RTX,
kono
parents:
diff changeset
1475 VOIDmode, 0, lab2,
kono
parents:
diff changeset
1476 profile_probability::very_likely ());
kono
parents:
diff changeset
1477 shadow_mem = gen_rtx_MEM (BLKmode, shadow_base);
kono
parents:
diff changeset
1478 set_mem_alias_set (shadow_mem, asan_shadow_set);
kono
parents:
diff changeset
1479 mem = gen_rtx_MEM (ptr_mode, base);
kono
parents:
diff changeset
1480 mem = adjust_address (mem, VOIDmode, base_align_bias);
kono
parents:
diff changeset
1481 emit_move_insn (mem, gen_int_mode (ASAN_STACK_RETIRED_MAGIC, ptr_mode));
kono
parents:
diff changeset
1482 unsigned HOST_WIDE_INT sz = asan_frame_size >> ASAN_SHADOW_SHIFT;
kono
parents:
diff changeset
1483 if (use_after_return_class < 5
kono
parents:
diff changeset
1484 && can_store_by_pieces (sz, builtin_memset_read_str, &c,
kono
parents:
diff changeset
1485 BITS_PER_UNIT, true))
kono
parents:
diff changeset
1486 store_by_pieces (shadow_mem, sz, builtin_memset_read_str, &c,
kono
parents:
diff changeset
1487 BITS_PER_UNIT, true, 0);
kono
parents:
diff changeset
1488 else if (use_after_return_class >= 5
kono
parents:
diff changeset
1489 || !set_storage_via_setmem (shadow_mem,
kono
parents:
diff changeset
1490 GEN_INT (sz),
kono
parents:
diff changeset
1491 gen_int_mode (c, QImode),
kono
parents:
diff changeset
1492 BITS_PER_UNIT, BITS_PER_UNIT,
kono
parents:
diff changeset
1493 -1, sz, sz, sz))
kono
parents:
diff changeset
1494 {
kono
parents:
diff changeset
1495 snprintf (buf, sizeof buf, "__asan_stack_free_%d",
kono
parents:
diff changeset
1496 use_after_return_class);
kono
parents:
diff changeset
1497 ret = init_one_libfunc (buf);
kono
parents:
diff changeset
1498 rtx addr = convert_memory_address (ptr_mode, base);
kono
parents:
diff changeset
1499 rtx orig_addr = convert_memory_address (ptr_mode, orig_base);
kono
parents:
diff changeset
1500 emit_library_call (ret, LCT_NORMAL, ptr_mode, addr, ptr_mode,
kono
parents:
diff changeset
1501 GEN_INT (asan_frame_size + base_align_bias),
kono
parents:
diff changeset
1502 TYPE_MODE (pointer_sized_int_node),
kono
parents:
diff changeset
1503 orig_addr, ptr_mode);
kono
parents:
diff changeset
1504 }
kono
parents:
diff changeset
1505 lab = gen_label_rtx ();
kono
parents:
diff changeset
1506 emit_jump (lab);
kono
parents:
diff changeset
1507 emit_label (lab2);
kono
parents:
diff changeset
1508 }
kono
parents:
diff changeset
1509
kono
parents:
diff changeset
1510 shadow_mem = gen_rtx_MEM (BLKmode, shadow_base);
kono
parents:
diff changeset
1511 set_mem_alias_set (shadow_mem, asan_shadow_set);
kono
parents:
diff changeset
1512
kono
parents:
diff changeset
1513 if (STRICT_ALIGNMENT)
kono
parents:
diff changeset
1514 set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode)));
kono
parents:
diff changeset
1515
kono
parents:
diff changeset
1516 prev_offset = base_offset;
kono
parents:
diff changeset
1517 last_offset = base_offset;
kono
parents:
diff changeset
1518 last_size = 0;
kono
parents:
diff changeset
1519 for (l = length; l; l -= 2)
kono
parents:
diff changeset
1520 {
kono
parents:
diff changeset
1521 offset = base_offset + ((offsets[l - 1] - base_offset)
kono
parents:
diff changeset
1522 & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1));
kono
parents:
diff changeset
1523 if (last_offset + last_size != offset)
kono
parents:
diff changeset
1524 {
kono
parents:
diff changeset
1525 shadow_mem = adjust_address (shadow_mem, VOIDmode,
kono
parents:
diff changeset
1526 (last_offset - prev_offset)
kono
parents:
diff changeset
1527 >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1528 prev_offset = last_offset;
kono
parents:
diff changeset
1529 asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1530 last_offset = offset;
kono
parents:
diff changeset
1531 last_size = 0;
kono
parents:
diff changeset
1532 }
kono
parents:
diff changeset
1533 last_size += base_offset + ((offsets[l - 2] - base_offset)
kono
parents:
diff changeset
1534 & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1))
kono
parents:
diff changeset
1535 - offset;
kono
parents:
diff changeset
1536
kono
parents:
diff changeset
1537 /* Unpoison shadow memory that corresponds to a variable that is
kono
parents:
diff changeset
1538 is subject of use-after-return sanitization. */
kono
parents:
diff changeset
1539 if (l > 2)
kono
parents:
diff changeset
1540 {
kono
parents:
diff changeset
1541 decl = decls[l / 2 - 2];
kono
parents:
diff changeset
1542 if (asan_handled_variables != NULL
kono
parents:
diff changeset
1543 && asan_handled_variables->contains (decl))
kono
parents:
diff changeset
1544 {
kono
parents:
diff changeset
1545 HOST_WIDE_INT size = offsets[l - 3] - offsets[l - 2];
kono
parents:
diff changeset
1546 if (dump_file && (dump_flags & TDF_DETAILS))
kono
parents:
diff changeset
1547 {
kono
parents:
diff changeset
1548 const char *n = (DECL_NAME (decl)
kono
parents:
diff changeset
1549 ? IDENTIFIER_POINTER (DECL_NAME (decl))
kono
parents:
diff changeset
1550 : "<unknown>");
kono
parents:
diff changeset
1551 fprintf (dump_file, "Unpoisoning shadow stack for variable: "
kono
parents:
diff changeset
1552 "%s (%" PRId64 " B)\n", n, size);
kono
parents:
diff changeset
1553 }
kono
parents:
diff changeset
1554
kono
parents:
diff changeset
1555 last_size += size & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1);
kono
parents:
diff changeset
1556 }
kono
parents:
diff changeset
1557 }
kono
parents:
diff changeset
1558 }
kono
parents:
diff changeset
1559 if (last_size)
kono
parents:
diff changeset
1560 {
kono
parents:
diff changeset
1561 shadow_mem = adjust_address (shadow_mem, VOIDmode,
kono
parents:
diff changeset
1562 (last_offset - prev_offset)
kono
parents:
diff changeset
1563 >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1564 asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1565 }
kono
parents:
diff changeset
1566
kono
parents:
diff changeset
1567 /* Clean-up set with instrumented stack variables. */
kono
parents:
diff changeset
1568 delete asan_handled_variables;
kono
parents:
diff changeset
1569 asan_handled_variables = NULL;
kono
parents:
diff changeset
1570 delete asan_used_labels;
kono
parents:
diff changeset
1571 asan_used_labels = NULL;
kono
parents:
diff changeset
1572
kono
parents:
diff changeset
1573 do_pending_stack_adjust ();
kono
parents:
diff changeset
1574 if (lab)
kono
parents:
diff changeset
1575 emit_label (lab);
kono
parents:
diff changeset
1576
kono
parents:
diff changeset
1577 insns = get_insns ();
kono
parents:
diff changeset
1578 end_sequence ();
kono
parents:
diff changeset
1579 return insns;
kono
parents:
diff changeset
1580 }
kono
parents:
diff changeset
1581
kono
parents:
diff changeset
1582 /* Emit __asan_allocas_unpoison (top, bot) call. The BASE parameter corresponds
kono
parents:
diff changeset
1583 to BOT argument, for TOP virtual_stack_dynamic_rtx is used. NEW_SEQUENCE
kono
parents:
diff changeset
1584 indicates whether we're emitting new instructions sequence or not. */
kono
parents:
diff changeset
1585
kono
parents:
diff changeset
1586 rtx_insn *
kono
parents:
diff changeset
1587 asan_emit_allocas_unpoison (rtx top, rtx bot, rtx_insn *before)
kono
parents:
diff changeset
1588 {
kono
parents:
diff changeset
1589 if (before)
kono
parents:
diff changeset
1590 push_to_sequence (before);
kono
parents:
diff changeset
1591 else
kono
parents:
diff changeset
1592 start_sequence ();
kono
parents:
diff changeset
1593 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
kono
parents:
diff changeset
1594 top = convert_memory_address (ptr_mode, top);
kono
parents:
diff changeset
1595 bot = convert_memory_address (ptr_mode, bot);
kono
parents:
diff changeset
1596 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
kono
parents:
diff changeset
1597 top, ptr_mode, bot, ptr_mode);
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 do_pending_stack_adjust ();
kono
parents:
diff changeset
1600 rtx_insn *insns = get_insns ();
kono
parents:
diff changeset
1601 end_sequence ();
kono
parents:
diff changeset
1602 return insns;
kono
parents:
diff changeset
1603 }
kono
parents:
diff changeset
1604
kono
parents:
diff changeset
1605 /* Return true if DECL, a global var, might be overridden and needs
kono
parents:
diff changeset
1606 therefore a local alias. */
kono
parents:
diff changeset
1607
kono
parents:
diff changeset
1608 static bool
kono
parents:
diff changeset
1609 asan_needs_local_alias (tree decl)
kono
parents:
diff changeset
1610 {
kono
parents:
diff changeset
1611 return DECL_WEAK (decl) || !targetm.binds_local_p (decl);
kono
parents:
diff changeset
1612 }
kono
parents:
diff changeset
1613
kono
parents:
diff changeset
1614 /* Return true if DECL, a global var, is an artificial ODR indicator symbol
kono
parents:
diff changeset
1615 therefore doesn't need protection. */
kono
parents:
diff changeset
1616
kono
parents:
diff changeset
1617 static bool
kono
parents:
diff changeset
1618 is_odr_indicator (tree decl)
kono
parents:
diff changeset
1619 {
kono
parents:
diff changeset
1620 return (DECL_ARTIFICIAL (decl)
kono
parents:
diff changeset
1621 && lookup_attribute ("asan odr indicator", DECL_ATTRIBUTES (decl)));
kono
parents:
diff changeset
1622 }
kono
parents:
diff changeset
1623
kono
parents:
diff changeset
1624 /* Return true if DECL is a VAR_DECL that should be protected
kono
parents:
diff changeset
1625 by Address Sanitizer, by appending a red zone with protected
kono
parents:
diff changeset
1626 shadow memory after it and aligning it to at least
kono
parents:
diff changeset
1627 ASAN_RED_ZONE_SIZE bytes. */
kono
parents:
diff changeset
1628
kono
parents:
diff changeset
1629 bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1630 asan_protect_global (tree decl, bool ignore_decl_rtl_set_p)
111
kono
parents:
diff changeset
1631 {
kono
parents:
diff changeset
1632 if (!ASAN_GLOBALS)
kono
parents:
diff changeset
1633 return false;
kono
parents:
diff changeset
1634
kono
parents:
diff changeset
1635 rtx rtl, symbol;
kono
parents:
diff changeset
1636
kono
parents:
diff changeset
1637 if (TREE_CODE (decl) == STRING_CST)
kono
parents:
diff changeset
1638 {
kono
parents:
diff changeset
1639 /* Instrument all STRING_CSTs except those created
kono
parents:
diff changeset
1640 by asan_pp_string here. */
kono
parents:
diff changeset
1641 if (shadow_ptr_types[0] != NULL_TREE
kono
parents:
diff changeset
1642 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
kono
parents:
diff changeset
1643 && TREE_TYPE (TREE_TYPE (decl)) == TREE_TYPE (shadow_ptr_types[0]))
kono
parents:
diff changeset
1644 return false;
kono
parents:
diff changeset
1645 return true;
kono
parents:
diff changeset
1646 }
kono
parents:
diff changeset
1647 if (!VAR_P (decl)
kono
parents:
diff changeset
1648 /* TLS vars aren't statically protectable. */
kono
parents:
diff changeset
1649 || DECL_THREAD_LOCAL_P (decl)
kono
parents:
diff changeset
1650 /* Externs will be protected elsewhere. */
kono
parents:
diff changeset
1651 || DECL_EXTERNAL (decl)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1652 /* PR sanitizer/81697: For architectures that use section anchors first
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1653 call to asan_protect_global may occur before DECL_RTL (decl) is set.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1654 We should ignore DECL_RTL_SET_P then, because otherwise the first call
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1655 to asan_protect_global will return FALSE and the following calls on the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1656 same decl after setting DECL_RTL (decl) will return TRUE and we'll end
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1657 up with inconsistency at runtime. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1658 || (!DECL_RTL_SET_P (decl) && !ignore_decl_rtl_set_p)
111
kono
parents:
diff changeset
1659 /* Comdat vars pose an ABI problem, we can't know if
kono
parents:
diff changeset
1660 the var that is selected by the linker will have
kono
parents:
diff changeset
1661 padding or not. */
kono
parents:
diff changeset
1662 || DECL_ONE_ONLY (decl)
kono
parents:
diff changeset
1663 /* Similarly for common vars. People can use -fno-common.
kono
parents:
diff changeset
1664 Note: Linux kernel is built with -fno-common, so we do instrument
kono
parents:
diff changeset
1665 globals there even if it is C. */
kono
parents:
diff changeset
1666 || (DECL_COMMON (decl) && TREE_PUBLIC (decl))
kono
parents:
diff changeset
1667 /* Don't protect if using user section, often vars placed
kono
parents:
diff changeset
1668 into user section from multiple TUs are then assumed
kono
parents:
diff changeset
1669 to be an array of such vars, putting padding in there
kono
parents:
diff changeset
1670 breaks this assumption. */
kono
parents:
diff changeset
1671 || (DECL_SECTION_NAME (decl) != NULL
kono
parents:
diff changeset
1672 && !symtab_node::get (decl)->implicit_section
kono
parents:
diff changeset
1673 && !section_sanitized_p (DECL_SECTION_NAME (decl)))
kono
parents:
diff changeset
1674 || DECL_SIZE (decl) == 0
kono
parents:
diff changeset
1675 || ASAN_RED_ZONE_SIZE * BITS_PER_UNIT > MAX_OFILE_ALIGNMENT
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1676 || TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST
111
kono
parents:
diff changeset
1677 || !valid_constant_size_p (DECL_SIZE_UNIT (decl))
kono
parents:
diff changeset
1678 || DECL_ALIGN_UNIT (decl) > 2 * ASAN_RED_ZONE_SIZE
kono
parents:
diff changeset
1679 || TREE_TYPE (decl) == ubsan_get_source_location_type ()
kono
parents:
diff changeset
1680 || is_odr_indicator (decl))
kono
parents:
diff changeset
1681 return false;
kono
parents:
diff changeset
1682
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1683 if (!ignore_decl_rtl_set_p || DECL_RTL_SET_P (decl))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1684 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1685
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1686 rtl = DECL_RTL (decl);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1687 if (!MEM_P (rtl) || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1688 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1689 symbol = XEXP (rtl, 0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1690
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1691 if (CONSTANT_POOL_ADDRESS_P (symbol)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1692 || TREE_CONSTANT_POOL_ADDRESS_P (symbol))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1693 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1694 }
111
kono
parents:
diff changeset
1695
kono
parents:
diff changeset
1696 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
kono
parents:
diff changeset
1697 return false;
kono
parents:
diff changeset
1698
kono
parents:
diff changeset
1699 if (!TARGET_SUPPORTS_ALIASES && asan_needs_local_alias (decl))
kono
parents:
diff changeset
1700 return false;
kono
parents:
diff changeset
1701
kono
parents:
diff changeset
1702 return true;
kono
parents:
diff changeset
1703 }
kono
parents:
diff changeset
1704
kono
parents:
diff changeset
1705 /* Construct a function tree for __asan_report_{load,store}{1,2,4,8,16,_n}.
kono
parents:
diff changeset
1706 IS_STORE is either 1 (for a store) or 0 (for a load). */
kono
parents:
diff changeset
1707
kono
parents:
diff changeset
1708 static tree
kono
parents:
diff changeset
1709 report_error_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes,
kono
parents:
diff changeset
1710 int *nargs)
kono
parents:
diff changeset
1711 {
kono
parents:
diff changeset
1712 static enum built_in_function report[2][2][6]
kono
parents:
diff changeset
1713 = { { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2,
kono
parents:
diff changeset
1714 BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8,
kono
parents:
diff changeset
1715 BUILT_IN_ASAN_REPORT_LOAD16, BUILT_IN_ASAN_REPORT_LOAD_N },
kono
parents:
diff changeset
1716 { BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2,
kono
parents:
diff changeset
1717 BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8,
kono
parents:
diff changeset
1718 BUILT_IN_ASAN_REPORT_STORE16, BUILT_IN_ASAN_REPORT_STORE_N } },
kono
parents:
diff changeset
1719 { { BUILT_IN_ASAN_REPORT_LOAD1_NOABORT,
kono
parents:
diff changeset
1720 BUILT_IN_ASAN_REPORT_LOAD2_NOABORT,
kono
parents:
diff changeset
1721 BUILT_IN_ASAN_REPORT_LOAD4_NOABORT,
kono
parents:
diff changeset
1722 BUILT_IN_ASAN_REPORT_LOAD8_NOABORT,
kono
parents:
diff changeset
1723 BUILT_IN_ASAN_REPORT_LOAD16_NOABORT,
kono
parents:
diff changeset
1724 BUILT_IN_ASAN_REPORT_LOAD_N_NOABORT },
kono
parents:
diff changeset
1725 { BUILT_IN_ASAN_REPORT_STORE1_NOABORT,
kono
parents:
diff changeset
1726 BUILT_IN_ASAN_REPORT_STORE2_NOABORT,
kono
parents:
diff changeset
1727 BUILT_IN_ASAN_REPORT_STORE4_NOABORT,
kono
parents:
diff changeset
1728 BUILT_IN_ASAN_REPORT_STORE8_NOABORT,
kono
parents:
diff changeset
1729 BUILT_IN_ASAN_REPORT_STORE16_NOABORT,
kono
parents:
diff changeset
1730 BUILT_IN_ASAN_REPORT_STORE_N_NOABORT } } };
kono
parents:
diff changeset
1731 if (size_in_bytes == -1)
kono
parents:
diff changeset
1732 {
kono
parents:
diff changeset
1733 *nargs = 2;
kono
parents:
diff changeset
1734 return builtin_decl_implicit (report[recover_p][is_store][5]);
kono
parents:
diff changeset
1735 }
kono
parents:
diff changeset
1736 *nargs = 1;
kono
parents:
diff changeset
1737 int size_log2 = exact_log2 (size_in_bytes);
kono
parents:
diff changeset
1738 return builtin_decl_implicit (report[recover_p][is_store][size_log2]);
kono
parents:
diff changeset
1739 }
kono
parents:
diff changeset
1740
kono
parents:
diff changeset
1741 /* Construct a function tree for __asan_{load,store}{1,2,4,8,16,_n}.
kono
parents:
diff changeset
1742 IS_STORE is either 1 (for a store) or 0 (for a load). */
kono
parents:
diff changeset
1743
kono
parents:
diff changeset
1744 static tree
kono
parents:
diff changeset
1745 check_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes,
kono
parents:
diff changeset
1746 int *nargs)
kono
parents:
diff changeset
1747 {
kono
parents:
diff changeset
1748 static enum built_in_function check[2][2][6]
kono
parents:
diff changeset
1749 = { { { BUILT_IN_ASAN_LOAD1, BUILT_IN_ASAN_LOAD2,
kono
parents:
diff changeset
1750 BUILT_IN_ASAN_LOAD4, BUILT_IN_ASAN_LOAD8,
kono
parents:
diff changeset
1751 BUILT_IN_ASAN_LOAD16, BUILT_IN_ASAN_LOADN },
kono
parents:
diff changeset
1752 { BUILT_IN_ASAN_STORE1, BUILT_IN_ASAN_STORE2,
kono
parents:
diff changeset
1753 BUILT_IN_ASAN_STORE4, BUILT_IN_ASAN_STORE8,
kono
parents:
diff changeset
1754 BUILT_IN_ASAN_STORE16, BUILT_IN_ASAN_STOREN } },
kono
parents:
diff changeset
1755 { { BUILT_IN_ASAN_LOAD1_NOABORT,
kono
parents:
diff changeset
1756 BUILT_IN_ASAN_LOAD2_NOABORT,
kono
parents:
diff changeset
1757 BUILT_IN_ASAN_LOAD4_NOABORT,
kono
parents:
diff changeset
1758 BUILT_IN_ASAN_LOAD8_NOABORT,
kono
parents:
diff changeset
1759 BUILT_IN_ASAN_LOAD16_NOABORT,
kono
parents:
diff changeset
1760 BUILT_IN_ASAN_LOADN_NOABORT },
kono
parents:
diff changeset
1761 { BUILT_IN_ASAN_STORE1_NOABORT,
kono
parents:
diff changeset
1762 BUILT_IN_ASAN_STORE2_NOABORT,
kono
parents:
diff changeset
1763 BUILT_IN_ASAN_STORE4_NOABORT,
kono
parents:
diff changeset
1764 BUILT_IN_ASAN_STORE8_NOABORT,
kono
parents:
diff changeset
1765 BUILT_IN_ASAN_STORE16_NOABORT,
kono
parents:
diff changeset
1766 BUILT_IN_ASAN_STOREN_NOABORT } } };
kono
parents:
diff changeset
1767 if (size_in_bytes == -1)
kono
parents:
diff changeset
1768 {
kono
parents:
diff changeset
1769 *nargs = 2;
kono
parents:
diff changeset
1770 return builtin_decl_implicit (check[recover_p][is_store][5]);
kono
parents:
diff changeset
1771 }
kono
parents:
diff changeset
1772 *nargs = 1;
kono
parents:
diff changeset
1773 int size_log2 = exact_log2 (size_in_bytes);
kono
parents:
diff changeset
1774 return builtin_decl_implicit (check[recover_p][is_store][size_log2]);
kono
parents:
diff changeset
1775 }
kono
parents:
diff changeset
1776
kono
parents:
diff changeset
1777 /* Split the current basic block and create a condition statement
kono
parents:
diff changeset
1778 insertion point right before or after the statement pointed to by
kono
parents:
diff changeset
1779 ITER. Return an iterator to the point at which the caller might
kono
parents:
diff changeset
1780 safely insert the condition statement.
kono
parents:
diff changeset
1781
kono
parents:
diff changeset
1782 THEN_BLOCK must be set to the address of an uninitialized instance
kono
parents:
diff changeset
1783 of basic_block. The function will then set *THEN_BLOCK to the
kono
parents:
diff changeset
1784 'then block' of the condition statement to be inserted by the
kono
parents:
diff changeset
1785 caller.
kono
parents:
diff changeset
1786
kono
parents:
diff changeset
1787 If CREATE_THEN_FALLTHRU_EDGE is false, no edge will be created from
kono
parents:
diff changeset
1788 *THEN_BLOCK to *FALLTHROUGH_BLOCK.
kono
parents:
diff changeset
1789
kono
parents:
diff changeset
1790 Similarly, the function will set *FALLTRHOUGH_BLOCK to the 'else
kono
parents:
diff changeset
1791 block' of the condition statement to be inserted by the caller.
kono
parents:
diff changeset
1792
kono
parents:
diff changeset
1793 Note that *FALLTHROUGH_BLOCK is a new block that contains the
kono
parents:
diff changeset
1794 statements starting from *ITER, and *THEN_BLOCK is a new empty
kono
parents:
diff changeset
1795 block.
kono
parents:
diff changeset
1796
kono
parents:
diff changeset
1797 *ITER is adjusted to point to always point to the first statement
kono
parents:
diff changeset
1798 of the basic block * FALLTHROUGH_BLOCK. That statement is the
kono
parents:
diff changeset
1799 same as what ITER was pointing to prior to calling this function,
kono
parents:
diff changeset
1800 if BEFORE_P is true; otherwise, it is its following statement. */
kono
parents:
diff changeset
1801
kono
parents:
diff changeset
1802 gimple_stmt_iterator
kono
parents:
diff changeset
1803 create_cond_insert_point (gimple_stmt_iterator *iter,
kono
parents:
diff changeset
1804 bool before_p,
kono
parents:
diff changeset
1805 bool then_more_likely_p,
kono
parents:
diff changeset
1806 bool create_then_fallthru_edge,
kono
parents:
diff changeset
1807 basic_block *then_block,
kono
parents:
diff changeset
1808 basic_block *fallthrough_block)
kono
parents:
diff changeset
1809 {
kono
parents:
diff changeset
1810 gimple_stmt_iterator gsi = *iter;
kono
parents:
diff changeset
1811
kono
parents:
diff changeset
1812 if (!gsi_end_p (gsi) && before_p)
kono
parents:
diff changeset
1813 gsi_prev (&gsi);
kono
parents:
diff changeset
1814
kono
parents:
diff changeset
1815 basic_block cur_bb = gsi_bb (*iter);
kono
parents:
diff changeset
1816
kono
parents:
diff changeset
1817 edge e = split_block (cur_bb, gsi_stmt (gsi));
kono
parents:
diff changeset
1818
kono
parents:
diff changeset
1819 /* Get a hold on the 'condition block', the 'then block' and the
kono
parents:
diff changeset
1820 'else block'. */
kono
parents:
diff changeset
1821 basic_block cond_bb = e->src;
kono
parents:
diff changeset
1822 basic_block fallthru_bb = e->dest;
kono
parents:
diff changeset
1823 basic_block then_bb = create_empty_bb (cond_bb);
kono
parents:
diff changeset
1824 if (current_loops)
kono
parents:
diff changeset
1825 {
kono
parents:
diff changeset
1826 add_bb_to_loop (then_bb, cond_bb->loop_father);
kono
parents:
diff changeset
1827 loops_state_set (LOOPS_NEED_FIXUP);
kono
parents:
diff changeset
1828 }
kono
parents:
diff changeset
1829
kono
parents:
diff changeset
1830 /* Set up the newly created 'then block'. */
kono
parents:
diff changeset
1831 e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
kono
parents:
diff changeset
1832 profile_probability fallthrough_probability
kono
parents:
diff changeset
1833 = then_more_likely_p
kono
parents:
diff changeset
1834 ? profile_probability::very_unlikely ()
kono
parents:
diff changeset
1835 : profile_probability::very_likely ();
kono
parents:
diff changeset
1836 e->probability = fallthrough_probability.invert ();
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1837 then_bb->count = e->count ();
111
kono
parents:
diff changeset
1838 if (create_then_fallthru_edge)
kono
parents:
diff changeset
1839 make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU);
kono
parents:
diff changeset
1840
kono
parents:
diff changeset
1841 /* Set up the fallthrough basic block. */
kono
parents:
diff changeset
1842 e = find_edge (cond_bb, fallthru_bb);
kono
parents:
diff changeset
1843 e->flags = EDGE_FALSE_VALUE;
kono
parents:
diff changeset
1844 e->probability = fallthrough_probability;
kono
parents:
diff changeset
1845
kono
parents:
diff changeset
1846 /* Update dominance info for the newly created then_bb; note that
kono
parents:
diff changeset
1847 fallthru_bb's dominance info has already been updated by
kono
parents:
diff changeset
1848 split_bock. */
kono
parents:
diff changeset
1849 if (dom_info_available_p (CDI_DOMINATORS))
kono
parents:
diff changeset
1850 set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
kono
parents:
diff changeset
1851
kono
parents:
diff changeset
1852 *then_block = then_bb;
kono
parents:
diff changeset
1853 *fallthrough_block = fallthru_bb;
kono
parents:
diff changeset
1854 *iter = gsi_start_bb (fallthru_bb);
kono
parents:
diff changeset
1855
kono
parents:
diff changeset
1856 return gsi_last_bb (cond_bb);
kono
parents:
diff changeset
1857 }
kono
parents:
diff changeset
1858
kono
parents:
diff changeset
1859 /* Insert an if condition followed by a 'then block' right before the
kono
parents:
diff changeset
1860 statement pointed to by ITER. The fallthrough block -- which is the
kono
parents:
diff changeset
1861 else block of the condition as well as the destination of the
kono
parents:
diff changeset
1862 outcoming edge of the 'then block' -- starts with the statement
kono
parents:
diff changeset
1863 pointed to by ITER.
kono
parents:
diff changeset
1864
kono
parents:
diff changeset
1865 COND is the condition of the if.
kono
parents:
diff changeset
1866
kono
parents:
diff changeset
1867 If THEN_MORE_LIKELY_P is true, the probability of the edge to the
kono
parents:
diff changeset
1868 'then block' is higher than the probability of the edge to the
kono
parents:
diff changeset
1869 fallthrough block.
kono
parents:
diff changeset
1870
kono
parents:
diff changeset
1871 Upon completion of the function, *THEN_BB is set to the newly
kono
parents:
diff changeset
1872 inserted 'then block' and similarly, *FALLTHROUGH_BB is set to the
kono
parents:
diff changeset
1873 fallthrough block.
kono
parents:
diff changeset
1874
kono
parents:
diff changeset
1875 *ITER is adjusted to still point to the same statement it was
kono
parents:
diff changeset
1876 pointing to initially. */
kono
parents:
diff changeset
1877
kono
parents:
diff changeset
1878 static void
kono
parents:
diff changeset
1879 insert_if_then_before_iter (gcond *cond,
kono
parents:
diff changeset
1880 gimple_stmt_iterator *iter,
kono
parents:
diff changeset
1881 bool then_more_likely_p,
kono
parents:
diff changeset
1882 basic_block *then_bb,
kono
parents:
diff changeset
1883 basic_block *fallthrough_bb)
kono
parents:
diff changeset
1884 {
kono
parents:
diff changeset
1885 gimple_stmt_iterator cond_insert_point =
kono
parents:
diff changeset
1886 create_cond_insert_point (iter,
kono
parents:
diff changeset
1887 /*before_p=*/true,
kono
parents:
diff changeset
1888 then_more_likely_p,
kono
parents:
diff changeset
1889 /*create_then_fallthru_edge=*/true,
kono
parents:
diff changeset
1890 then_bb,
kono
parents:
diff changeset
1891 fallthrough_bb);
kono
parents:
diff changeset
1892 gsi_insert_after (&cond_insert_point, cond, GSI_NEW_STMT);
kono
parents:
diff changeset
1893 }
kono
parents:
diff changeset
1894
kono
parents:
diff changeset
1895 /* Build (base_addr >> ASAN_SHADOW_SHIFT) + asan_shadow_offset ().
kono
parents:
diff changeset
1896 If RETURN_ADDRESS is set to true, return memory location instread
kono
parents:
diff changeset
1897 of a value in the shadow memory. */
kono
parents:
diff changeset
1898
kono
parents:
diff changeset
1899 static tree
kono
parents:
diff changeset
1900 build_shadow_mem_access (gimple_stmt_iterator *gsi, location_t location,
kono
parents:
diff changeset
1901 tree base_addr, tree shadow_ptr_type,
kono
parents:
diff changeset
1902 bool return_address = false)
kono
parents:
diff changeset
1903 {
kono
parents:
diff changeset
1904 tree t, uintptr_type = TREE_TYPE (base_addr);
kono
parents:
diff changeset
1905 tree shadow_type = TREE_TYPE (shadow_ptr_type);
kono
parents:
diff changeset
1906 gimple *g;
kono
parents:
diff changeset
1907
kono
parents:
diff changeset
1908 t = build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT);
kono
parents:
diff changeset
1909 g = gimple_build_assign (make_ssa_name (uintptr_type), RSHIFT_EXPR,
kono
parents:
diff changeset
1910 base_addr, t);
kono
parents:
diff changeset
1911 gimple_set_location (g, location);
kono
parents:
diff changeset
1912 gsi_insert_after (gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1913
kono
parents:
diff changeset
1914 t = build_int_cst (uintptr_type, asan_shadow_offset ());
kono
parents:
diff changeset
1915 g = gimple_build_assign (make_ssa_name (uintptr_type), PLUS_EXPR,
kono
parents:
diff changeset
1916 gimple_assign_lhs (g), t);
kono
parents:
diff changeset
1917 gimple_set_location (g, location);
kono
parents:
diff changeset
1918 gsi_insert_after (gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1919
kono
parents:
diff changeset
1920 g = gimple_build_assign (make_ssa_name (shadow_ptr_type), NOP_EXPR,
kono
parents:
diff changeset
1921 gimple_assign_lhs (g));
kono
parents:
diff changeset
1922 gimple_set_location (g, location);
kono
parents:
diff changeset
1923 gsi_insert_after (gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1924
kono
parents:
diff changeset
1925 if (!return_address)
kono
parents:
diff changeset
1926 {
kono
parents:
diff changeset
1927 t = build2 (MEM_REF, shadow_type, gimple_assign_lhs (g),
kono
parents:
diff changeset
1928 build_int_cst (shadow_ptr_type, 0));
kono
parents:
diff changeset
1929 g = gimple_build_assign (make_ssa_name (shadow_type), MEM_REF, t);
kono
parents:
diff changeset
1930 gimple_set_location (g, location);
kono
parents:
diff changeset
1931 gsi_insert_after (gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1932 }
kono
parents:
diff changeset
1933
kono
parents:
diff changeset
1934 return gimple_assign_lhs (g);
kono
parents:
diff changeset
1935 }
kono
parents:
diff changeset
1936
kono
parents:
diff changeset
1937 /* BASE can already be an SSA_NAME; in that case, do not create a
kono
parents:
diff changeset
1938 new SSA_NAME for it. */
kono
parents:
diff changeset
1939
kono
parents:
diff changeset
1940 static tree
kono
parents:
diff changeset
1941 maybe_create_ssa_name (location_t loc, tree base, gimple_stmt_iterator *iter,
kono
parents:
diff changeset
1942 bool before_p)
kono
parents:
diff changeset
1943 {
kono
parents:
diff changeset
1944 if (TREE_CODE (base) == SSA_NAME)
kono
parents:
diff changeset
1945 return base;
kono
parents:
diff changeset
1946 gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (base)),
kono
parents:
diff changeset
1947 TREE_CODE (base), base);
kono
parents:
diff changeset
1948 gimple_set_location (g, loc);
kono
parents:
diff changeset
1949 if (before_p)
kono
parents:
diff changeset
1950 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
1951 else
kono
parents:
diff changeset
1952 gsi_insert_after (iter, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1953 return gimple_assign_lhs (g);
kono
parents:
diff changeset
1954 }
kono
parents:
diff changeset
1955
kono
parents:
diff changeset
1956 /* LEN can already have necessary size and precision;
kono
parents:
diff changeset
1957 in that case, do not create a new variable. */
kono
parents:
diff changeset
1958
kono
parents:
diff changeset
1959 tree
kono
parents:
diff changeset
1960 maybe_cast_to_ptrmode (location_t loc, tree len, gimple_stmt_iterator *iter,
kono
parents:
diff changeset
1961 bool before_p)
kono
parents:
diff changeset
1962 {
kono
parents:
diff changeset
1963 if (ptrofftype_p (len))
kono
parents:
diff changeset
1964 return len;
kono
parents:
diff changeset
1965 gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
1966 NOP_EXPR, len);
kono
parents:
diff changeset
1967 gimple_set_location (g, loc);
kono
parents:
diff changeset
1968 if (before_p)
kono
parents:
diff changeset
1969 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
1970 else
kono
parents:
diff changeset
1971 gsi_insert_after (iter, g, GSI_NEW_STMT);
kono
parents:
diff changeset
1972 return gimple_assign_lhs (g);
kono
parents:
diff changeset
1973 }
kono
parents:
diff changeset
1974
kono
parents:
diff changeset
1975 /* Instrument the memory access instruction BASE. Insert new
kono
parents:
diff changeset
1976 statements before or after ITER.
kono
parents:
diff changeset
1977
kono
parents:
diff changeset
1978 Note that the memory access represented by BASE can be either an
kono
parents:
diff changeset
1979 SSA_NAME, or a non-SSA expression. LOCATION is the source code
kono
parents:
diff changeset
1980 location. IS_STORE is TRUE for a store, FALSE for a load.
kono
parents:
diff changeset
1981 BEFORE_P is TRUE for inserting the instrumentation code before
kono
parents:
diff changeset
1982 ITER, FALSE for inserting it after ITER. IS_SCALAR_ACCESS is TRUE
kono
parents:
diff changeset
1983 for a scalar memory access and FALSE for memory region access.
kono
parents:
diff changeset
1984 NON_ZERO_P is TRUE if memory region is guaranteed to have non-zero
kono
parents:
diff changeset
1985 length. ALIGN tells alignment of accessed memory object.
kono
parents:
diff changeset
1986
kono
parents:
diff changeset
1987 START_INSTRUMENTED and END_INSTRUMENTED are TRUE if start/end of
kono
parents:
diff changeset
1988 memory region have already been instrumented.
kono
parents:
diff changeset
1989
kono
parents:
diff changeset
1990 If BEFORE_P is TRUE, *ITER is arranged to still point to the
kono
parents:
diff changeset
1991 statement it was pointing to prior to calling this function,
kono
parents:
diff changeset
1992 otherwise, it points to the statement logically following it. */
kono
parents:
diff changeset
1993
kono
parents:
diff changeset
1994 static void
kono
parents:
diff changeset
1995 build_check_stmt (location_t loc, tree base, tree len,
kono
parents:
diff changeset
1996 HOST_WIDE_INT size_in_bytes, gimple_stmt_iterator *iter,
kono
parents:
diff changeset
1997 bool is_non_zero_len, bool before_p, bool is_store,
kono
parents:
diff changeset
1998 bool is_scalar_access, unsigned int align = 0)
kono
parents:
diff changeset
1999 {
kono
parents:
diff changeset
2000 gimple_stmt_iterator gsi = *iter;
kono
parents:
diff changeset
2001 gimple *g;
kono
parents:
diff changeset
2002
kono
parents:
diff changeset
2003 gcc_assert (!(size_in_bytes > 0 && !is_non_zero_len));
kono
parents:
diff changeset
2004
kono
parents:
diff changeset
2005 gsi = *iter;
kono
parents:
diff changeset
2006
kono
parents:
diff changeset
2007 base = unshare_expr (base);
kono
parents:
diff changeset
2008 base = maybe_create_ssa_name (loc, base, &gsi, before_p);
kono
parents:
diff changeset
2009
kono
parents:
diff changeset
2010 if (len)
kono
parents:
diff changeset
2011 {
kono
parents:
diff changeset
2012 len = unshare_expr (len);
kono
parents:
diff changeset
2013 len = maybe_cast_to_ptrmode (loc, len, iter, before_p);
kono
parents:
diff changeset
2014 }
kono
parents:
diff changeset
2015 else
kono
parents:
diff changeset
2016 {
kono
parents:
diff changeset
2017 gcc_assert (size_in_bytes != -1);
kono
parents:
diff changeset
2018 len = build_int_cst (pointer_sized_int_node, size_in_bytes);
kono
parents:
diff changeset
2019 }
kono
parents:
diff changeset
2020
kono
parents:
diff changeset
2021 if (size_in_bytes > 1)
kono
parents:
diff changeset
2022 {
kono
parents:
diff changeset
2023 if ((size_in_bytes & (size_in_bytes - 1)) != 0
kono
parents:
diff changeset
2024 || size_in_bytes > 16)
kono
parents:
diff changeset
2025 is_scalar_access = false;
kono
parents:
diff changeset
2026 else if (align && align < size_in_bytes * BITS_PER_UNIT)
kono
parents:
diff changeset
2027 {
kono
parents:
diff changeset
2028 /* On non-strict alignment targets, if
kono
parents:
diff changeset
2029 16-byte access is just 8-byte aligned,
kono
parents:
diff changeset
2030 this will result in misaligned shadow
kono
parents:
diff changeset
2031 memory 2 byte load, but otherwise can
kono
parents:
diff changeset
2032 be handled using one read. */
kono
parents:
diff changeset
2033 if (size_in_bytes != 16
kono
parents:
diff changeset
2034 || STRICT_ALIGNMENT
kono
parents:
diff changeset
2035 || align < 8 * BITS_PER_UNIT)
kono
parents:
diff changeset
2036 is_scalar_access = false;
kono
parents:
diff changeset
2037 }
kono
parents:
diff changeset
2038 }
kono
parents:
diff changeset
2039
kono
parents:
diff changeset
2040 HOST_WIDE_INT flags = 0;
kono
parents:
diff changeset
2041 if (is_store)
kono
parents:
diff changeset
2042 flags |= ASAN_CHECK_STORE;
kono
parents:
diff changeset
2043 if (is_non_zero_len)
kono
parents:
diff changeset
2044 flags |= ASAN_CHECK_NON_ZERO_LEN;
kono
parents:
diff changeset
2045 if (is_scalar_access)
kono
parents:
diff changeset
2046 flags |= ASAN_CHECK_SCALAR_ACCESS;
kono
parents:
diff changeset
2047
kono
parents:
diff changeset
2048 g = gimple_build_call_internal (IFN_ASAN_CHECK, 4,
kono
parents:
diff changeset
2049 build_int_cst (integer_type_node, flags),
kono
parents:
diff changeset
2050 base, len,
kono
parents:
diff changeset
2051 build_int_cst (integer_type_node,
kono
parents:
diff changeset
2052 align / BITS_PER_UNIT));
kono
parents:
diff changeset
2053 gimple_set_location (g, loc);
kono
parents:
diff changeset
2054 if (before_p)
kono
parents:
diff changeset
2055 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
kono
parents:
diff changeset
2056 else
kono
parents:
diff changeset
2057 {
kono
parents:
diff changeset
2058 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
2059 gsi_next (&gsi);
kono
parents:
diff changeset
2060 *iter = gsi;
kono
parents:
diff changeset
2061 }
kono
parents:
diff changeset
2062 }
kono
parents:
diff changeset
2063
kono
parents:
diff changeset
2064 /* If T represents a memory access, add instrumentation code before ITER.
kono
parents:
diff changeset
2065 LOCATION is source code location.
kono
parents:
diff changeset
2066 IS_STORE is either TRUE (for a store) or FALSE (for a load). */
kono
parents:
diff changeset
2067
kono
parents:
diff changeset
2068 static void
kono
parents:
diff changeset
2069 instrument_derefs (gimple_stmt_iterator *iter, tree t,
kono
parents:
diff changeset
2070 location_t location, bool is_store)
kono
parents:
diff changeset
2071 {
kono
parents:
diff changeset
2072 if (is_store && !ASAN_INSTRUMENT_WRITES)
kono
parents:
diff changeset
2073 return;
kono
parents:
diff changeset
2074 if (!is_store && !ASAN_INSTRUMENT_READS)
kono
parents:
diff changeset
2075 return;
kono
parents:
diff changeset
2076
kono
parents:
diff changeset
2077 tree type, base;
kono
parents:
diff changeset
2078 HOST_WIDE_INT size_in_bytes;
kono
parents:
diff changeset
2079 if (location == UNKNOWN_LOCATION)
kono
parents:
diff changeset
2080 location = EXPR_LOCATION (t);
kono
parents:
diff changeset
2081
kono
parents:
diff changeset
2082 type = TREE_TYPE (t);
kono
parents:
diff changeset
2083 switch (TREE_CODE (t))
kono
parents:
diff changeset
2084 {
kono
parents:
diff changeset
2085 case ARRAY_REF:
kono
parents:
diff changeset
2086 case COMPONENT_REF:
kono
parents:
diff changeset
2087 case INDIRECT_REF:
kono
parents:
diff changeset
2088 case MEM_REF:
kono
parents:
diff changeset
2089 case VAR_DECL:
kono
parents:
diff changeset
2090 case BIT_FIELD_REF:
kono
parents:
diff changeset
2091 break;
kono
parents:
diff changeset
2092 /* FALLTHRU */
kono
parents:
diff changeset
2093 default:
kono
parents:
diff changeset
2094 return;
kono
parents:
diff changeset
2095 }
kono
parents:
diff changeset
2096
kono
parents:
diff changeset
2097 size_in_bytes = int_size_in_bytes (type);
kono
parents:
diff changeset
2098 if (size_in_bytes <= 0)
kono
parents:
diff changeset
2099 return;
kono
parents:
diff changeset
2100
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2101 poly_int64 bitsize, bitpos;
111
kono
parents:
diff changeset
2102 tree offset;
kono
parents:
diff changeset
2103 machine_mode mode;
kono
parents:
diff changeset
2104 int unsignedp, reversep, volatilep = 0;
kono
parents:
diff changeset
2105 tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
kono
parents:
diff changeset
2106 &unsignedp, &reversep, &volatilep);
kono
parents:
diff changeset
2107
kono
parents:
diff changeset
2108 if (TREE_CODE (t) == COMPONENT_REF
kono
parents:
diff changeset
2109 && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE)
kono
parents:
diff changeset
2110 {
kono
parents:
diff changeset
2111 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1));
kono
parents:
diff changeset
2112 instrument_derefs (iter, build3 (COMPONENT_REF, TREE_TYPE (repr),
kono
parents:
diff changeset
2113 TREE_OPERAND (t, 0), repr,
kono
parents:
diff changeset
2114 TREE_OPERAND (t, 2)),
kono
parents:
diff changeset
2115 location, is_store);
kono
parents:
diff changeset
2116 return;
kono
parents:
diff changeset
2117 }
kono
parents:
diff changeset
2118
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2119 if (!multiple_p (bitpos, BITS_PER_UNIT)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2120 || maybe_ne (bitsize, size_in_bytes * BITS_PER_UNIT))
111
kono
parents:
diff changeset
2121 return;
kono
parents:
diff changeset
2122
kono
parents:
diff changeset
2123 if (VAR_P (inner) && DECL_HARD_REGISTER (inner))
kono
parents:
diff changeset
2124 return;
kono
parents:
diff changeset
2125
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2126 poly_int64 decl_size;
111
kono
parents:
diff changeset
2127 if (VAR_P (inner)
kono
parents:
diff changeset
2128 && offset == NULL_TREE
kono
parents:
diff changeset
2129 && DECL_SIZE (inner)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2130 && poly_int_tree_p (DECL_SIZE (inner), &decl_size)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2131 && known_subrange_p (bitpos, bitsize, 0, decl_size))
111
kono
parents:
diff changeset
2132 {
kono
parents:
diff changeset
2133 if (DECL_THREAD_LOCAL_P (inner))
kono
parents:
diff changeset
2134 return;
kono
parents:
diff changeset
2135 if (!ASAN_GLOBALS && is_global_var (inner))
kono
parents:
diff changeset
2136 return;
kono
parents:
diff changeset
2137 if (!TREE_STATIC (inner))
kono
parents:
diff changeset
2138 {
kono
parents:
diff changeset
2139 /* Automatic vars in the current function will be always
kono
parents:
diff changeset
2140 accessible. */
kono
parents:
diff changeset
2141 if (decl_function_context (inner) == current_function_decl
kono
parents:
diff changeset
2142 && (!asan_sanitize_use_after_scope ()
kono
parents:
diff changeset
2143 || !TREE_ADDRESSABLE (inner)))
kono
parents:
diff changeset
2144 return;
kono
parents:
diff changeset
2145 }
kono
parents:
diff changeset
2146 /* Always instrument external vars, they might be dynamically
kono
parents:
diff changeset
2147 initialized. */
kono
parents:
diff changeset
2148 else if (!DECL_EXTERNAL (inner))
kono
parents:
diff changeset
2149 {
kono
parents:
diff changeset
2150 /* For static vars if they are known not to be dynamically
kono
parents:
diff changeset
2151 initialized, they will be always accessible. */
kono
parents:
diff changeset
2152 varpool_node *vnode = varpool_node::get (inner);
kono
parents:
diff changeset
2153 if (vnode && !vnode->dynamically_initialized)
kono
parents:
diff changeset
2154 return;
kono
parents:
diff changeset
2155 }
kono
parents:
diff changeset
2156 }
kono
parents:
diff changeset
2157
kono
parents:
diff changeset
2158 base = build_fold_addr_expr (t);
kono
parents:
diff changeset
2159 if (!has_mem_ref_been_instrumented (base, size_in_bytes))
kono
parents:
diff changeset
2160 {
kono
parents:
diff changeset
2161 unsigned int align = get_object_alignment (t);
kono
parents:
diff changeset
2162 build_check_stmt (location, base, NULL_TREE, size_in_bytes, iter,
kono
parents:
diff changeset
2163 /*is_non_zero_len*/size_in_bytes > 0, /*before_p=*/true,
kono
parents:
diff changeset
2164 is_store, /*is_scalar_access*/true, align);
kono
parents:
diff changeset
2165 update_mem_ref_hash_table (base, size_in_bytes);
kono
parents:
diff changeset
2166 update_mem_ref_hash_table (t, size_in_bytes);
kono
parents:
diff changeset
2167 }
kono
parents:
diff changeset
2168
kono
parents:
diff changeset
2169 }
kono
parents:
diff changeset
2170
kono
parents:
diff changeset
2171 /* Insert a memory reference into the hash table if access length
kono
parents:
diff changeset
2172 can be determined in compile time. */
kono
parents:
diff changeset
2173
kono
parents:
diff changeset
2174 static void
kono
parents:
diff changeset
2175 maybe_update_mem_ref_hash_table (tree base, tree len)
kono
parents:
diff changeset
2176 {
kono
parents:
diff changeset
2177 if (!POINTER_TYPE_P (TREE_TYPE (base))
kono
parents:
diff changeset
2178 || !INTEGRAL_TYPE_P (TREE_TYPE (len)))
kono
parents:
diff changeset
2179 return;
kono
parents:
diff changeset
2180
kono
parents:
diff changeset
2181 HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
kono
parents:
diff changeset
2182
kono
parents:
diff changeset
2183 if (size_in_bytes != -1)
kono
parents:
diff changeset
2184 update_mem_ref_hash_table (base, size_in_bytes);
kono
parents:
diff changeset
2185 }
kono
parents:
diff changeset
2186
kono
parents:
diff changeset
2187 /* Instrument an access to a contiguous memory region that starts at
kono
parents:
diff changeset
2188 the address pointed to by BASE, over a length of LEN (expressed in
kono
parents:
diff changeset
2189 the sizeof (*BASE) bytes). ITER points to the instruction before
kono
parents:
diff changeset
2190 which the instrumentation instructions must be inserted. LOCATION
kono
parents:
diff changeset
2191 is the source location that the instrumentation instructions must
kono
parents:
diff changeset
2192 have. If IS_STORE is true, then the memory access is a store;
kono
parents:
diff changeset
2193 otherwise, it's a load. */
kono
parents:
diff changeset
2194
kono
parents:
diff changeset
2195 static void
kono
parents:
diff changeset
2196 instrument_mem_region_access (tree base, tree len,
kono
parents:
diff changeset
2197 gimple_stmt_iterator *iter,
kono
parents:
diff changeset
2198 location_t location, bool is_store)
kono
parents:
diff changeset
2199 {
kono
parents:
diff changeset
2200 if (!POINTER_TYPE_P (TREE_TYPE (base))
kono
parents:
diff changeset
2201 || !INTEGRAL_TYPE_P (TREE_TYPE (len))
kono
parents:
diff changeset
2202 || integer_zerop (len))
kono
parents:
diff changeset
2203 return;
kono
parents:
diff changeset
2204
kono
parents:
diff changeset
2205 HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
kono
parents:
diff changeset
2206
kono
parents:
diff changeset
2207 if ((size_in_bytes == -1)
kono
parents:
diff changeset
2208 || !has_mem_ref_been_instrumented (base, size_in_bytes))
kono
parents:
diff changeset
2209 {
kono
parents:
diff changeset
2210 build_check_stmt (location, base, len, size_in_bytes, iter,
kono
parents:
diff changeset
2211 /*is_non_zero_len*/size_in_bytes > 0, /*before_p*/true,
kono
parents:
diff changeset
2212 is_store, /*is_scalar_access*/false, /*align*/0);
kono
parents:
diff changeset
2213 }
kono
parents:
diff changeset
2214
kono
parents:
diff changeset
2215 maybe_update_mem_ref_hash_table (base, len);
kono
parents:
diff changeset
2216 *iter = gsi_for_stmt (gsi_stmt (*iter));
kono
parents:
diff changeset
2217 }
kono
parents:
diff changeset
2218
kono
parents:
diff changeset
2219 /* Instrument the call to a built-in memory access function that is
kono
parents:
diff changeset
2220 pointed to by the iterator ITER.
kono
parents:
diff changeset
2221
kono
parents:
diff changeset
2222 Upon completion, return TRUE iff *ITER has been advanced to the
kono
parents:
diff changeset
2223 statement following the one it was originally pointing to. */
kono
parents:
diff changeset
2224
kono
parents:
diff changeset
2225 static bool
kono
parents:
diff changeset
2226 instrument_builtin_call (gimple_stmt_iterator *iter)
kono
parents:
diff changeset
2227 {
kono
parents:
diff changeset
2228 if (!ASAN_MEMINTRIN)
kono
parents:
diff changeset
2229 return false;
kono
parents:
diff changeset
2230
kono
parents:
diff changeset
2231 bool iter_advanced_p = false;
kono
parents:
diff changeset
2232 gcall *call = as_a <gcall *> (gsi_stmt (*iter));
kono
parents:
diff changeset
2233
kono
parents:
diff changeset
2234 gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL));
kono
parents:
diff changeset
2235
kono
parents:
diff changeset
2236 location_t loc = gimple_location (call);
kono
parents:
diff changeset
2237
kono
parents:
diff changeset
2238 asan_mem_ref src0, src1, dest;
kono
parents:
diff changeset
2239 asan_mem_ref_init (&src0, NULL, 1);
kono
parents:
diff changeset
2240 asan_mem_ref_init (&src1, NULL, 1);
kono
parents:
diff changeset
2241 asan_mem_ref_init (&dest, NULL, 1);
kono
parents:
diff changeset
2242
kono
parents:
diff changeset
2243 tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE;
kono
parents:
diff changeset
2244 bool src0_is_store = false, src1_is_store = false, dest_is_store = false,
kono
parents:
diff changeset
2245 dest_is_deref = false, intercepted_p = true;
kono
parents:
diff changeset
2246
kono
parents:
diff changeset
2247 if (get_mem_refs_of_builtin_call (call,
kono
parents:
diff changeset
2248 &src0, &src0_len, &src0_is_store,
kono
parents:
diff changeset
2249 &src1, &src1_len, &src1_is_store,
kono
parents:
diff changeset
2250 &dest, &dest_len, &dest_is_store,
kono
parents:
diff changeset
2251 &dest_is_deref, &intercepted_p, iter))
kono
parents:
diff changeset
2252 {
kono
parents:
diff changeset
2253 if (dest_is_deref)
kono
parents:
diff changeset
2254 {
kono
parents:
diff changeset
2255 instrument_derefs (iter, dest.start, loc, dest_is_store);
kono
parents:
diff changeset
2256 gsi_next (iter);
kono
parents:
diff changeset
2257 iter_advanced_p = true;
kono
parents:
diff changeset
2258 }
kono
parents:
diff changeset
2259 else if (!intercepted_p
kono
parents:
diff changeset
2260 && (src0_len || src1_len || dest_len))
kono
parents:
diff changeset
2261 {
kono
parents:
diff changeset
2262 if (src0.start != NULL_TREE)
kono
parents:
diff changeset
2263 instrument_mem_region_access (src0.start, src0_len,
kono
parents:
diff changeset
2264 iter, loc, /*is_store=*/false);
kono
parents:
diff changeset
2265 if (src1.start != NULL_TREE)
kono
parents:
diff changeset
2266 instrument_mem_region_access (src1.start, src1_len,
kono
parents:
diff changeset
2267 iter, loc, /*is_store=*/false);
kono
parents:
diff changeset
2268 if (dest.start != NULL_TREE)
kono
parents:
diff changeset
2269 instrument_mem_region_access (dest.start, dest_len,
kono
parents:
diff changeset
2270 iter, loc, /*is_store=*/true);
kono
parents:
diff changeset
2271
kono
parents:
diff changeset
2272 *iter = gsi_for_stmt (call);
kono
parents:
diff changeset
2273 gsi_next (iter);
kono
parents:
diff changeset
2274 iter_advanced_p = true;
kono
parents:
diff changeset
2275 }
kono
parents:
diff changeset
2276 else
kono
parents:
diff changeset
2277 {
kono
parents:
diff changeset
2278 if (src0.start != NULL_TREE)
kono
parents:
diff changeset
2279 maybe_update_mem_ref_hash_table (src0.start, src0_len);
kono
parents:
diff changeset
2280 if (src1.start != NULL_TREE)
kono
parents:
diff changeset
2281 maybe_update_mem_ref_hash_table (src1.start, src1_len);
kono
parents:
diff changeset
2282 if (dest.start != NULL_TREE)
kono
parents:
diff changeset
2283 maybe_update_mem_ref_hash_table (dest.start, dest_len);
kono
parents:
diff changeset
2284 }
kono
parents:
diff changeset
2285 }
kono
parents:
diff changeset
2286 return iter_advanced_p;
kono
parents:
diff changeset
2287 }
kono
parents:
diff changeset
2288
kono
parents:
diff changeset
2289 /* Instrument the assignment statement ITER if it is subject to
kono
parents:
diff changeset
2290 instrumentation. Return TRUE iff instrumentation actually
kono
parents:
diff changeset
2291 happened. In that case, the iterator ITER is advanced to the next
kono
parents:
diff changeset
2292 logical expression following the one initially pointed to by ITER,
kono
parents:
diff changeset
2293 and the relevant memory reference that which access has been
kono
parents:
diff changeset
2294 instrumented is added to the memory references hash table. */
kono
parents:
diff changeset
2295
kono
parents:
diff changeset
2296 static bool
kono
parents:
diff changeset
2297 maybe_instrument_assignment (gimple_stmt_iterator *iter)
kono
parents:
diff changeset
2298 {
kono
parents:
diff changeset
2299 gimple *s = gsi_stmt (*iter);
kono
parents:
diff changeset
2300
kono
parents:
diff changeset
2301 gcc_assert (gimple_assign_single_p (s));
kono
parents:
diff changeset
2302
kono
parents:
diff changeset
2303 tree ref_expr = NULL_TREE;
kono
parents:
diff changeset
2304 bool is_store, is_instrumented = false;
kono
parents:
diff changeset
2305
kono
parents:
diff changeset
2306 if (gimple_store_p (s))
kono
parents:
diff changeset
2307 {
kono
parents:
diff changeset
2308 ref_expr = gimple_assign_lhs (s);
kono
parents:
diff changeset
2309 is_store = true;
kono
parents:
diff changeset
2310 instrument_derefs (iter, ref_expr,
kono
parents:
diff changeset
2311 gimple_location (s),
kono
parents:
diff changeset
2312 is_store);
kono
parents:
diff changeset
2313 is_instrumented = true;
kono
parents:
diff changeset
2314 }
kono
parents:
diff changeset
2315
kono
parents:
diff changeset
2316 if (gimple_assign_load_p (s))
kono
parents:
diff changeset
2317 {
kono
parents:
diff changeset
2318 ref_expr = gimple_assign_rhs1 (s);
kono
parents:
diff changeset
2319 is_store = false;
kono
parents:
diff changeset
2320 instrument_derefs (iter, ref_expr,
kono
parents:
diff changeset
2321 gimple_location (s),
kono
parents:
diff changeset
2322 is_store);
kono
parents:
diff changeset
2323 is_instrumented = true;
kono
parents:
diff changeset
2324 }
kono
parents:
diff changeset
2325
kono
parents:
diff changeset
2326 if (is_instrumented)
kono
parents:
diff changeset
2327 gsi_next (iter);
kono
parents:
diff changeset
2328
kono
parents:
diff changeset
2329 return is_instrumented;
kono
parents:
diff changeset
2330 }
kono
parents:
diff changeset
2331
kono
parents:
diff changeset
2332 /* Instrument the function call pointed to by the iterator ITER, if it
kono
parents:
diff changeset
2333 is subject to instrumentation. At the moment, the only function
kono
parents:
diff changeset
2334 calls that are instrumented are some built-in functions that access
kono
parents:
diff changeset
2335 memory. Look at instrument_builtin_call to learn more.
kono
parents:
diff changeset
2336
kono
parents:
diff changeset
2337 Upon completion return TRUE iff *ITER was advanced to the statement
kono
parents:
diff changeset
2338 following the one it was originally pointing to. */
kono
parents:
diff changeset
2339
kono
parents:
diff changeset
2340 static bool
kono
parents:
diff changeset
2341 maybe_instrument_call (gimple_stmt_iterator *iter)
kono
parents:
diff changeset
2342 {
kono
parents:
diff changeset
2343 gimple *stmt = gsi_stmt (*iter);
kono
parents:
diff changeset
2344 bool is_builtin = gimple_call_builtin_p (stmt, BUILT_IN_NORMAL);
kono
parents:
diff changeset
2345
kono
parents:
diff changeset
2346 if (is_builtin && instrument_builtin_call (iter))
kono
parents:
diff changeset
2347 return true;
kono
parents:
diff changeset
2348
kono
parents:
diff changeset
2349 if (gimple_call_noreturn_p (stmt))
kono
parents:
diff changeset
2350 {
kono
parents:
diff changeset
2351 if (is_builtin)
kono
parents:
diff changeset
2352 {
kono
parents:
diff changeset
2353 tree callee = gimple_call_fndecl (stmt);
kono
parents:
diff changeset
2354 switch (DECL_FUNCTION_CODE (callee))
kono
parents:
diff changeset
2355 {
kono
parents:
diff changeset
2356 case BUILT_IN_UNREACHABLE:
kono
parents:
diff changeset
2357 case BUILT_IN_TRAP:
kono
parents:
diff changeset
2358 /* Don't instrument these. */
kono
parents:
diff changeset
2359 return false;
kono
parents:
diff changeset
2360 default:
kono
parents:
diff changeset
2361 break;
kono
parents:
diff changeset
2362 }
kono
parents:
diff changeset
2363 }
kono
parents:
diff changeset
2364 tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN);
kono
parents:
diff changeset
2365 gimple *g = gimple_build_call (decl, 0);
kono
parents:
diff changeset
2366 gimple_set_location (g, gimple_location (stmt));
kono
parents:
diff changeset
2367 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
2368 }
kono
parents:
diff changeset
2369
kono
parents:
diff changeset
2370 bool instrumented = false;
kono
parents:
diff changeset
2371 if (gimple_store_p (stmt))
kono
parents:
diff changeset
2372 {
kono
parents:
diff changeset
2373 tree ref_expr = gimple_call_lhs (stmt);
kono
parents:
diff changeset
2374 instrument_derefs (iter, ref_expr,
kono
parents:
diff changeset
2375 gimple_location (stmt),
kono
parents:
diff changeset
2376 /*is_store=*/true);
kono
parents:
diff changeset
2377
kono
parents:
diff changeset
2378 instrumented = true;
kono
parents:
diff changeset
2379 }
kono
parents:
diff changeset
2380
kono
parents:
diff changeset
2381 /* Walk through gimple_call arguments and check them id needed. */
kono
parents:
diff changeset
2382 unsigned args_num = gimple_call_num_args (stmt);
kono
parents:
diff changeset
2383 for (unsigned i = 0; i < args_num; ++i)
kono
parents:
diff changeset
2384 {
kono
parents:
diff changeset
2385 tree arg = gimple_call_arg (stmt, i);
kono
parents:
diff changeset
2386 /* If ARG is not a non-aggregate register variable, compiler in general
kono
parents:
diff changeset
2387 creates temporary for it and pass it as argument to gimple call.
kono
parents:
diff changeset
2388 But in some cases, e.g. when we pass by value a small structure that
kono
parents:
diff changeset
2389 fits to register, compiler can avoid extra overhead by pulling out
kono
parents:
diff changeset
2390 these temporaries. In this case, we should check the argument. */
kono
parents:
diff changeset
2391 if (!is_gimple_reg (arg) && !is_gimple_min_invariant (arg))
kono
parents:
diff changeset
2392 {
kono
parents:
diff changeset
2393 instrument_derefs (iter, arg,
kono
parents:
diff changeset
2394 gimple_location (stmt),
kono
parents:
diff changeset
2395 /*is_store=*/false);
kono
parents:
diff changeset
2396 instrumented = true;
kono
parents:
diff changeset
2397 }
kono
parents:
diff changeset
2398 }
kono
parents:
diff changeset
2399 if (instrumented)
kono
parents:
diff changeset
2400 gsi_next (iter);
kono
parents:
diff changeset
2401 return instrumented;
kono
parents:
diff changeset
2402 }
kono
parents:
diff changeset
2403
kono
parents:
diff changeset
2404 /* Walk each instruction of all basic block and instrument those that
kono
parents:
diff changeset
2405 represent memory references: loads, stores, or function calls.
kono
parents:
diff changeset
2406 In a given basic block, this function avoids instrumenting memory
kono
parents:
diff changeset
2407 references that have already been instrumented. */
kono
parents:
diff changeset
2408
kono
parents:
diff changeset
2409 static void
kono
parents:
diff changeset
2410 transform_statements (void)
kono
parents:
diff changeset
2411 {
kono
parents:
diff changeset
2412 basic_block bb, last_bb = NULL;
kono
parents:
diff changeset
2413 gimple_stmt_iterator i;
kono
parents:
diff changeset
2414 int saved_last_basic_block = last_basic_block_for_fn (cfun);
kono
parents:
diff changeset
2415
kono
parents:
diff changeset
2416 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
2417 {
kono
parents:
diff changeset
2418 basic_block prev_bb = bb;
kono
parents:
diff changeset
2419
kono
parents:
diff changeset
2420 if (bb->index >= saved_last_basic_block) continue;
kono
parents:
diff changeset
2421
kono
parents:
diff changeset
2422 /* Flush the mem ref hash table, if current bb doesn't have
kono
parents:
diff changeset
2423 exactly one predecessor, or if that predecessor (skipping
kono
parents:
diff changeset
2424 over asan created basic blocks) isn't the last processed
kono
parents:
diff changeset
2425 basic block. Thus we effectively flush on extended basic
kono
parents:
diff changeset
2426 block boundaries. */
kono
parents:
diff changeset
2427 while (single_pred_p (prev_bb))
kono
parents:
diff changeset
2428 {
kono
parents:
diff changeset
2429 prev_bb = single_pred (prev_bb);
kono
parents:
diff changeset
2430 if (prev_bb->index < saved_last_basic_block)
kono
parents:
diff changeset
2431 break;
kono
parents:
diff changeset
2432 }
kono
parents:
diff changeset
2433 if (prev_bb != last_bb)
kono
parents:
diff changeset
2434 empty_mem_ref_hash_table ();
kono
parents:
diff changeset
2435 last_bb = bb;
kono
parents:
diff changeset
2436
kono
parents:
diff changeset
2437 for (i = gsi_start_bb (bb); !gsi_end_p (i);)
kono
parents:
diff changeset
2438 {
kono
parents:
diff changeset
2439 gimple *s = gsi_stmt (i);
kono
parents:
diff changeset
2440
kono
parents:
diff changeset
2441 if (has_stmt_been_instrumented_p (s))
kono
parents:
diff changeset
2442 gsi_next (&i);
kono
parents:
diff changeset
2443 else if (gimple_assign_single_p (s)
kono
parents:
diff changeset
2444 && !gimple_clobber_p (s)
kono
parents:
diff changeset
2445 && maybe_instrument_assignment (&i))
kono
parents:
diff changeset
2446 /* Nothing to do as maybe_instrument_assignment advanced
kono
parents:
diff changeset
2447 the iterator I. */;
kono
parents:
diff changeset
2448 else if (is_gimple_call (s) && maybe_instrument_call (&i))
kono
parents:
diff changeset
2449 /* Nothing to do as maybe_instrument_call
kono
parents:
diff changeset
2450 advanced the iterator I. */;
kono
parents:
diff changeset
2451 else
kono
parents:
diff changeset
2452 {
kono
parents:
diff changeset
2453 /* No instrumentation happened.
kono
parents:
diff changeset
2454
kono
parents:
diff changeset
2455 If the current instruction is a function call that
kono
parents:
diff changeset
2456 might free something, let's forget about the memory
kono
parents:
diff changeset
2457 references that got instrumented. Otherwise we might
kono
parents:
diff changeset
2458 miss some instrumentation opportunities. Do the same
kono
parents:
diff changeset
2459 for a ASAN_MARK poisoning internal function. */
kono
parents:
diff changeset
2460 if (is_gimple_call (s)
kono
parents:
diff changeset
2461 && (!nonfreeing_call_p (s)
kono
parents:
diff changeset
2462 || asan_mark_p (s, ASAN_MARK_POISON)))
kono
parents:
diff changeset
2463 empty_mem_ref_hash_table ();
kono
parents:
diff changeset
2464
kono
parents:
diff changeset
2465 gsi_next (&i);
kono
parents:
diff changeset
2466 }
kono
parents:
diff changeset
2467 }
kono
parents:
diff changeset
2468 }
kono
parents:
diff changeset
2469 free_mem_ref_resources ();
kono
parents:
diff changeset
2470 }
kono
parents:
diff changeset
2471
kono
parents:
diff changeset
2472 /* Build
kono
parents:
diff changeset
2473 __asan_before_dynamic_init (module_name)
kono
parents:
diff changeset
2474 or
kono
parents:
diff changeset
2475 __asan_after_dynamic_init ()
kono
parents:
diff changeset
2476 call. */
kono
parents:
diff changeset
2477
kono
parents:
diff changeset
2478 tree
kono
parents:
diff changeset
2479 asan_dynamic_init_call (bool after_p)
kono
parents:
diff changeset
2480 {
kono
parents:
diff changeset
2481 if (shadow_ptr_types[0] == NULL_TREE)
kono
parents:
diff changeset
2482 asan_init_shadow_ptr_types ();
kono
parents:
diff changeset
2483
kono
parents:
diff changeset
2484 tree fn = builtin_decl_implicit (after_p
kono
parents:
diff changeset
2485 ? BUILT_IN_ASAN_AFTER_DYNAMIC_INIT
kono
parents:
diff changeset
2486 : BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT);
kono
parents:
diff changeset
2487 tree module_name_cst = NULL_TREE;
kono
parents:
diff changeset
2488 if (!after_p)
kono
parents:
diff changeset
2489 {
kono
parents:
diff changeset
2490 pretty_printer module_name_pp;
kono
parents:
diff changeset
2491 pp_string (&module_name_pp, main_input_filename);
kono
parents:
diff changeset
2492
kono
parents:
diff changeset
2493 module_name_cst = asan_pp_string (&module_name_pp);
kono
parents:
diff changeset
2494 module_name_cst = fold_convert (const_ptr_type_node,
kono
parents:
diff changeset
2495 module_name_cst);
kono
parents:
diff changeset
2496 }
kono
parents:
diff changeset
2497
kono
parents:
diff changeset
2498 return build_call_expr (fn, after_p ? 0 : 1, module_name_cst);
kono
parents:
diff changeset
2499 }
kono
parents:
diff changeset
2500
kono
parents:
diff changeset
2501 /* Build
kono
parents:
diff changeset
2502 struct __asan_global
kono
parents:
diff changeset
2503 {
kono
parents:
diff changeset
2504 const void *__beg;
kono
parents:
diff changeset
2505 uptr __size;
kono
parents:
diff changeset
2506 uptr __size_with_redzone;
kono
parents:
diff changeset
2507 const void *__name;
kono
parents:
diff changeset
2508 const void *__module_name;
kono
parents:
diff changeset
2509 uptr __has_dynamic_init;
kono
parents:
diff changeset
2510 __asan_global_source_location *__location;
kono
parents:
diff changeset
2511 char *__odr_indicator;
kono
parents:
diff changeset
2512 } type. */
kono
parents:
diff changeset
2513
kono
parents:
diff changeset
2514 static tree
kono
parents:
diff changeset
2515 asan_global_struct (void)
kono
parents:
diff changeset
2516 {
kono
parents:
diff changeset
2517 static const char *field_names[]
kono
parents:
diff changeset
2518 = { "__beg", "__size", "__size_with_redzone",
kono
parents:
diff changeset
2519 "__name", "__module_name", "__has_dynamic_init", "__location",
kono
parents:
diff changeset
2520 "__odr_indicator" };
kono
parents:
diff changeset
2521 tree fields[ARRAY_SIZE (field_names)], ret;
kono
parents:
diff changeset
2522 unsigned i;
kono
parents:
diff changeset
2523
kono
parents:
diff changeset
2524 ret = make_node (RECORD_TYPE);
kono
parents:
diff changeset
2525 for (i = 0; i < ARRAY_SIZE (field_names); i++)
kono
parents:
diff changeset
2526 {
kono
parents:
diff changeset
2527 fields[i]
kono
parents:
diff changeset
2528 = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
kono
parents:
diff changeset
2529 get_identifier (field_names[i]),
kono
parents:
diff changeset
2530 (i == 0 || i == 3) ? const_ptr_type_node
kono
parents:
diff changeset
2531 : pointer_sized_int_node);
kono
parents:
diff changeset
2532 DECL_CONTEXT (fields[i]) = ret;
kono
parents:
diff changeset
2533 if (i)
kono
parents:
diff changeset
2534 DECL_CHAIN (fields[i - 1]) = fields[i];
kono
parents:
diff changeset
2535 }
kono
parents:
diff changeset
2536 tree type_decl = build_decl (input_location, TYPE_DECL,
kono
parents:
diff changeset
2537 get_identifier ("__asan_global"), ret);
kono
parents:
diff changeset
2538 DECL_IGNORED_P (type_decl) = 1;
kono
parents:
diff changeset
2539 DECL_ARTIFICIAL (type_decl) = 1;
kono
parents:
diff changeset
2540 TYPE_FIELDS (ret) = fields[0];
kono
parents:
diff changeset
2541 TYPE_NAME (ret) = type_decl;
kono
parents:
diff changeset
2542 TYPE_STUB_DECL (ret) = type_decl;
kono
parents:
diff changeset
2543 layout_type (ret);
kono
parents:
diff changeset
2544 return ret;
kono
parents:
diff changeset
2545 }
kono
parents:
diff changeset
2546
kono
parents:
diff changeset
2547 /* Create and return odr indicator symbol for DECL.
kono
parents:
diff changeset
2548 TYPE is __asan_global struct type as returned by asan_global_struct. */
kono
parents:
diff changeset
2549
kono
parents:
diff changeset
2550 static tree
kono
parents:
diff changeset
2551 create_odr_indicator (tree decl, tree type)
kono
parents:
diff changeset
2552 {
kono
parents:
diff changeset
2553 char *name;
kono
parents:
diff changeset
2554 tree uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
kono
parents:
diff changeset
2555 tree decl_name
kono
parents:
diff changeset
2556 = (HAS_DECL_ASSEMBLER_NAME_P (decl) ? DECL_ASSEMBLER_NAME (decl)
kono
parents:
diff changeset
2557 : DECL_NAME (decl));
kono
parents:
diff changeset
2558 /* DECL_NAME theoretically might be NULL. Bail out with 0 in this case. */
kono
parents:
diff changeset
2559 if (decl_name == NULL_TREE)
kono
parents:
diff changeset
2560 return build_int_cst (uptr, 0);
kono
parents:
diff changeset
2561 const char *dname = IDENTIFIER_POINTER (decl_name);
kono
parents:
diff changeset
2562 if (HAS_DECL_ASSEMBLER_NAME_P (decl))
kono
parents:
diff changeset
2563 dname = targetm.strip_name_encoding (dname);
kono
parents:
diff changeset
2564 size_t len = strlen (dname) + sizeof ("__odr_asan_");
kono
parents:
diff changeset
2565 name = XALLOCAVEC (char, len);
kono
parents:
diff changeset
2566 snprintf (name, len, "__odr_asan_%s", dname);
kono
parents:
diff changeset
2567 #ifndef NO_DOT_IN_LABEL
kono
parents:
diff changeset
2568 name[sizeof ("__odr_asan") - 1] = '.';
kono
parents:
diff changeset
2569 #elif !defined(NO_DOLLAR_IN_LABEL)
kono
parents:
diff changeset
2570 name[sizeof ("__odr_asan") - 1] = '$';
kono
parents:
diff changeset
2571 #endif
kono
parents:
diff changeset
2572 tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (name),
kono
parents:
diff changeset
2573 char_type_node);
kono
parents:
diff changeset
2574 TREE_ADDRESSABLE (var) = 1;
kono
parents:
diff changeset
2575 TREE_READONLY (var) = 0;
kono
parents:
diff changeset
2576 TREE_THIS_VOLATILE (var) = 1;
kono
parents:
diff changeset
2577 DECL_GIMPLE_REG_P (var) = 0;
kono
parents:
diff changeset
2578 DECL_ARTIFICIAL (var) = 1;
kono
parents:
diff changeset
2579 DECL_IGNORED_P (var) = 1;
kono
parents:
diff changeset
2580 TREE_STATIC (var) = 1;
kono
parents:
diff changeset
2581 TREE_PUBLIC (var) = 1;
kono
parents:
diff changeset
2582 DECL_VISIBILITY (var) = DECL_VISIBILITY (decl);
kono
parents:
diff changeset
2583 DECL_VISIBILITY_SPECIFIED (var) = DECL_VISIBILITY_SPECIFIED (decl);
kono
parents:
diff changeset
2584
kono
parents:
diff changeset
2585 TREE_USED (var) = 1;
kono
parents:
diff changeset
2586 tree ctor = build_constructor_va (TREE_TYPE (var), 1, NULL_TREE,
kono
parents:
diff changeset
2587 build_int_cst (unsigned_type_node, 0));
kono
parents:
diff changeset
2588 TREE_CONSTANT (ctor) = 1;
kono
parents:
diff changeset
2589 TREE_STATIC (ctor) = 1;
kono
parents:
diff changeset
2590 DECL_INITIAL (var) = ctor;
kono
parents:
diff changeset
2591 DECL_ATTRIBUTES (var) = tree_cons (get_identifier ("asan odr indicator"),
kono
parents:
diff changeset
2592 NULL, DECL_ATTRIBUTES (var));
kono
parents:
diff changeset
2593 make_decl_rtl (var);
kono
parents:
diff changeset
2594 varpool_node::finalize_decl (var);
kono
parents:
diff changeset
2595 return fold_convert (uptr, build_fold_addr_expr (var));
kono
parents:
diff changeset
2596 }
kono
parents:
diff changeset
2597
kono
parents:
diff changeset
2598 /* Return true if DECL, a global var, might be overridden and needs
kono
parents:
diff changeset
2599 an additional odr indicator symbol. */
kono
parents:
diff changeset
2600
kono
parents:
diff changeset
2601 static bool
kono
parents:
diff changeset
2602 asan_needs_odr_indicator_p (tree decl)
kono
parents:
diff changeset
2603 {
kono
parents:
diff changeset
2604 /* Don't emit ODR indicators for kernel because:
kono
parents:
diff changeset
2605 a) Kernel is written in C thus doesn't need ODR indicators.
kono
parents:
diff changeset
2606 b) Some kernel code may have assumptions about symbols containing specific
kono
parents:
diff changeset
2607 patterns in their names. Since ODR indicators contain original names
kono
parents:
diff changeset
2608 of symbols they are emitted for, these assumptions would be broken for
kono
parents:
diff changeset
2609 ODR indicator symbols. */
kono
parents:
diff changeset
2610 return (!(flag_sanitize & SANITIZE_KERNEL_ADDRESS)
kono
parents:
diff changeset
2611 && !DECL_ARTIFICIAL (decl)
kono
parents:
diff changeset
2612 && !DECL_WEAK (decl)
kono
parents:
diff changeset
2613 && TREE_PUBLIC (decl));
kono
parents:
diff changeset
2614 }
kono
parents:
diff changeset
2615
kono
parents:
diff changeset
2616 /* Append description of a single global DECL into vector V.
kono
parents:
diff changeset
2617 TYPE is __asan_global struct type as returned by asan_global_struct. */
kono
parents:
diff changeset
2618
kono
parents:
diff changeset
2619 static void
kono
parents:
diff changeset
2620 asan_add_global (tree decl, tree type, vec<constructor_elt, va_gc> *v)
kono
parents:
diff changeset
2621 {
kono
parents:
diff changeset
2622 tree init, uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type)));
kono
parents:
diff changeset
2623 unsigned HOST_WIDE_INT size;
kono
parents:
diff changeset
2624 tree str_cst, module_name_cst, refdecl = decl;
kono
parents:
diff changeset
2625 vec<constructor_elt, va_gc> *vinner = NULL;
kono
parents:
diff changeset
2626
kono
parents:
diff changeset
2627 pretty_printer asan_pp, module_name_pp;
kono
parents:
diff changeset
2628
kono
parents:
diff changeset
2629 if (DECL_NAME (decl))
kono
parents:
diff changeset
2630 pp_tree_identifier (&asan_pp, DECL_NAME (decl));
kono
parents:
diff changeset
2631 else
kono
parents:
diff changeset
2632 pp_string (&asan_pp, "<unknown>");
kono
parents:
diff changeset
2633 str_cst = asan_pp_string (&asan_pp);
kono
parents:
diff changeset
2634
kono
parents:
diff changeset
2635 pp_string (&module_name_pp, main_input_filename);
kono
parents:
diff changeset
2636 module_name_cst = asan_pp_string (&module_name_pp);
kono
parents:
diff changeset
2637
kono
parents:
diff changeset
2638 if (asan_needs_local_alias (decl))
kono
parents:
diff changeset
2639 {
kono
parents:
diff changeset
2640 char buf[20];
kono
parents:
diff changeset
2641 ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", vec_safe_length (v) + 1);
kono
parents:
diff changeset
2642 refdecl = build_decl (DECL_SOURCE_LOCATION (decl),
kono
parents:
diff changeset
2643 VAR_DECL, get_identifier (buf), TREE_TYPE (decl));
kono
parents:
diff changeset
2644 TREE_ADDRESSABLE (refdecl) = TREE_ADDRESSABLE (decl);
kono
parents:
diff changeset
2645 TREE_READONLY (refdecl) = TREE_READONLY (decl);
kono
parents:
diff changeset
2646 TREE_THIS_VOLATILE (refdecl) = TREE_THIS_VOLATILE (decl);
kono
parents:
diff changeset
2647 DECL_GIMPLE_REG_P (refdecl) = DECL_GIMPLE_REG_P (decl);
kono
parents:
diff changeset
2648 DECL_ARTIFICIAL (refdecl) = DECL_ARTIFICIAL (decl);
kono
parents:
diff changeset
2649 DECL_IGNORED_P (refdecl) = DECL_IGNORED_P (decl);
kono
parents:
diff changeset
2650 TREE_STATIC (refdecl) = 1;
kono
parents:
diff changeset
2651 TREE_PUBLIC (refdecl) = 0;
kono
parents:
diff changeset
2652 TREE_USED (refdecl) = 1;
kono
parents:
diff changeset
2653 assemble_alias (refdecl, DECL_ASSEMBLER_NAME (decl));
kono
parents:
diff changeset
2654 }
kono
parents:
diff changeset
2655
kono
parents:
diff changeset
2656 tree odr_indicator_ptr
kono
parents:
diff changeset
2657 = (asan_needs_odr_indicator_p (decl) ? create_odr_indicator (decl, type)
kono
parents:
diff changeset
2658 : build_int_cst (uptr, 0));
kono
parents:
diff changeset
2659 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE,
kono
parents:
diff changeset
2660 fold_convert (const_ptr_type_node,
kono
parents:
diff changeset
2661 build_fold_addr_expr (refdecl)));
kono
parents:
diff changeset
2662 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
kono
parents:
diff changeset
2663 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size));
kono
parents:
diff changeset
2664 size += asan_red_zone_size (size);
kono
parents:
diff changeset
2665 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size));
kono
parents:
diff changeset
2666 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE,
kono
parents:
diff changeset
2667 fold_convert (const_ptr_type_node, str_cst));
kono
parents:
diff changeset
2668 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE,
kono
parents:
diff changeset
2669 fold_convert (const_ptr_type_node, module_name_cst));
kono
parents:
diff changeset
2670 varpool_node *vnode = varpool_node::get (decl);
kono
parents:
diff changeset
2671 int has_dynamic_init = 0;
kono
parents:
diff changeset
2672 /* FIXME: Enable initialization order fiasco detection in LTO mode once
kono
parents:
diff changeset
2673 proper fix for PR 79061 will be applied. */
kono
parents:
diff changeset
2674 if (!in_lto_p)
kono
parents:
diff changeset
2675 has_dynamic_init = vnode ? vnode->dynamically_initialized : 0;
kono
parents:
diff changeset
2676 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE,
kono
parents:
diff changeset
2677 build_int_cst (uptr, has_dynamic_init));
kono
parents:
diff changeset
2678 tree locptr = NULL_TREE;
kono
parents:
diff changeset
2679 location_t loc = DECL_SOURCE_LOCATION (decl);
kono
parents:
diff changeset
2680 expanded_location xloc = expand_location (loc);
kono
parents:
diff changeset
2681 if (xloc.file != NULL)
kono
parents:
diff changeset
2682 {
kono
parents:
diff changeset
2683 static int lasanloccnt = 0;
kono
parents:
diff changeset
2684 char buf[25];
kono
parents:
diff changeset
2685 ASM_GENERATE_INTERNAL_LABEL (buf, "LASANLOC", ++lasanloccnt);
kono
parents:
diff changeset
2686 tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf),
kono
parents:
diff changeset
2687 ubsan_get_source_location_type ());
kono
parents:
diff changeset
2688 TREE_STATIC (var) = 1;
kono
parents:
diff changeset
2689 TREE_PUBLIC (var) = 0;
kono
parents:
diff changeset
2690 DECL_ARTIFICIAL (var) = 1;
kono
parents:
diff changeset
2691 DECL_IGNORED_P (var) = 1;
kono
parents:
diff changeset
2692 pretty_printer filename_pp;
kono
parents:
diff changeset
2693 pp_string (&filename_pp, xloc.file);
kono
parents:
diff changeset
2694 tree str = asan_pp_string (&filename_pp);
kono
parents:
diff changeset
2695 tree ctor = build_constructor_va (TREE_TYPE (var), 3,
kono
parents:
diff changeset
2696 NULL_TREE, str, NULL_TREE,
kono
parents:
diff changeset
2697 build_int_cst (unsigned_type_node,
kono
parents:
diff changeset
2698 xloc.line), NULL_TREE,
kono
parents:
diff changeset
2699 build_int_cst (unsigned_type_node,
kono
parents:
diff changeset
2700 xloc.column));
kono
parents:
diff changeset
2701 TREE_CONSTANT (ctor) = 1;
kono
parents:
diff changeset
2702 TREE_STATIC (ctor) = 1;
kono
parents:
diff changeset
2703 DECL_INITIAL (var) = ctor;
kono
parents:
diff changeset
2704 varpool_node::finalize_decl (var);
kono
parents:
diff changeset
2705 locptr = fold_convert (uptr, build_fold_addr_expr (var));
kono
parents:
diff changeset
2706 }
kono
parents:
diff changeset
2707 else
kono
parents:
diff changeset
2708 locptr = build_int_cst (uptr, 0);
kono
parents:
diff changeset
2709 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, locptr);
kono
parents:
diff changeset
2710 CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, odr_indicator_ptr);
kono
parents:
diff changeset
2711 init = build_constructor (type, vinner);
kono
parents:
diff changeset
2712 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
kono
parents:
diff changeset
2713 }
kono
parents:
diff changeset
2714
kono
parents:
diff changeset
2715 /* Initialize sanitizer.def builtins if the FE hasn't initialized them. */
kono
parents:
diff changeset
2716 void
kono
parents:
diff changeset
2717 initialize_sanitizer_builtins (void)
kono
parents:
diff changeset
2718 {
kono
parents:
diff changeset
2719 tree decl;
kono
parents:
diff changeset
2720
kono
parents:
diff changeset
2721 if (builtin_decl_implicit_p (BUILT_IN_ASAN_INIT))
kono
parents:
diff changeset
2722 return;
kono
parents:
diff changeset
2723
kono
parents:
diff changeset
2724 tree BT_FN_VOID = build_function_type_list (void_type_node, NULL_TREE);
kono
parents:
diff changeset
2725 tree BT_FN_VOID_PTR
kono
parents:
diff changeset
2726 = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
kono
parents:
diff changeset
2727 tree BT_FN_VOID_CONST_PTR
kono
parents:
diff changeset
2728 = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
kono
parents:
diff changeset
2729 tree BT_FN_VOID_PTR_PTR
kono
parents:
diff changeset
2730 = build_function_type_list (void_type_node, ptr_type_node,
kono
parents:
diff changeset
2731 ptr_type_node, NULL_TREE);
kono
parents:
diff changeset
2732 tree BT_FN_VOID_PTR_PTR_PTR
kono
parents:
diff changeset
2733 = build_function_type_list (void_type_node, ptr_type_node,
kono
parents:
diff changeset
2734 ptr_type_node, ptr_type_node, NULL_TREE);
kono
parents:
diff changeset
2735 tree BT_FN_VOID_PTR_PTRMODE
kono
parents:
diff changeset
2736 = build_function_type_list (void_type_node, ptr_type_node,
kono
parents:
diff changeset
2737 pointer_sized_int_node, NULL_TREE);
kono
parents:
diff changeset
2738 tree BT_FN_VOID_INT
kono
parents:
diff changeset
2739 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
kono
parents:
diff changeset
2740 tree BT_FN_SIZE_CONST_PTR_INT
kono
parents:
diff changeset
2741 = build_function_type_list (size_type_node, const_ptr_type_node,
kono
parents:
diff changeset
2742 integer_type_node, NULL_TREE);
kono
parents:
diff changeset
2743
kono
parents:
diff changeset
2744 tree BT_FN_VOID_UINT8_UINT8
kono
parents:
diff changeset
2745 = build_function_type_list (void_type_node, unsigned_char_type_node,
kono
parents:
diff changeset
2746 unsigned_char_type_node, NULL_TREE);
kono
parents:
diff changeset
2747 tree BT_FN_VOID_UINT16_UINT16
kono
parents:
diff changeset
2748 = build_function_type_list (void_type_node, uint16_type_node,
kono
parents:
diff changeset
2749 uint16_type_node, NULL_TREE);
kono
parents:
diff changeset
2750 tree BT_FN_VOID_UINT32_UINT32
kono
parents:
diff changeset
2751 = build_function_type_list (void_type_node, uint32_type_node,
kono
parents:
diff changeset
2752 uint32_type_node, NULL_TREE);
kono
parents:
diff changeset
2753 tree BT_FN_VOID_UINT64_UINT64
kono
parents:
diff changeset
2754 = build_function_type_list (void_type_node, uint64_type_node,
kono
parents:
diff changeset
2755 uint64_type_node, NULL_TREE);
kono
parents:
diff changeset
2756 tree BT_FN_VOID_FLOAT_FLOAT
kono
parents:
diff changeset
2757 = build_function_type_list (void_type_node, float_type_node,
kono
parents:
diff changeset
2758 float_type_node, NULL_TREE);
kono
parents:
diff changeset
2759 tree BT_FN_VOID_DOUBLE_DOUBLE
kono
parents:
diff changeset
2760 = build_function_type_list (void_type_node, double_type_node,
kono
parents:
diff changeset
2761 double_type_node, NULL_TREE);
kono
parents:
diff changeset
2762 tree BT_FN_VOID_UINT64_PTR
kono
parents:
diff changeset
2763 = build_function_type_list (void_type_node, uint64_type_node,
kono
parents:
diff changeset
2764 ptr_type_node, NULL_TREE);
kono
parents:
diff changeset
2765
kono
parents:
diff changeset
2766 tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5];
kono
parents:
diff changeset
2767 tree BT_FN_IX_CONST_VPTR_INT[5];
kono
parents:
diff changeset
2768 tree BT_FN_IX_VPTR_IX_INT[5];
kono
parents:
diff changeset
2769 tree BT_FN_VOID_VPTR_IX_INT[5];
kono
parents:
diff changeset
2770 tree vptr
kono
parents:
diff changeset
2771 = build_pointer_type (build_qualified_type (void_type_node,
kono
parents:
diff changeset
2772 TYPE_QUAL_VOLATILE));
kono
parents:
diff changeset
2773 tree cvptr
kono
parents:
diff changeset
2774 = build_pointer_type (build_qualified_type (void_type_node,
kono
parents:
diff changeset
2775 TYPE_QUAL_VOLATILE
kono
parents:
diff changeset
2776 |TYPE_QUAL_CONST));
kono
parents:
diff changeset
2777 tree boolt
kono
parents:
diff changeset
2778 = lang_hooks.types.type_for_size (BOOL_TYPE_SIZE, 1);
kono
parents:
diff changeset
2779 int i;
kono
parents:
diff changeset
2780 for (i = 0; i < 5; i++)
kono
parents:
diff changeset
2781 {
kono
parents:
diff changeset
2782 tree ix = build_nonstandard_integer_type (BITS_PER_UNIT * (1 << i), 1);
kono
parents:
diff changeset
2783 BT_FN_BOOL_VPTR_PTR_IX_INT_INT[i]
kono
parents:
diff changeset
2784 = build_function_type_list (boolt, vptr, ptr_type_node, ix,
kono
parents:
diff changeset
2785 integer_type_node, integer_type_node,
kono
parents:
diff changeset
2786 NULL_TREE);
kono
parents:
diff changeset
2787 BT_FN_IX_CONST_VPTR_INT[i]
kono
parents:
diff changeset
2788 = build_function_type_list (ix, cvptr, integer_type_node, NULL_TREE);
kono
parents:
diff changeset
2789 BT_FN_IX_VPTR_IX_INT[i]
kono
parents:
diff changeset
2790 = build_function_type_list (ix, vptr, ix, integer_type_node,
kono
parents:
diff changeset
2791 NULL_TREE);
kono
parents:
diff changeset
2792 BT_FN_VOID_VPTR_IX_INT[i]
kono
parents:
diff changeset
2793 = build_function_type_list (void_type_node, vptr, ix,
kono
parents:
diff changeset
2794 integer_type_node, NULL_TREE);
kono
parents:
diff changeset
2795 }
kono
parents:
diff changeset
2796 #define BT_FN_BOOL_VPTR_PTR_I1_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[0]
kono
parents:
diff changeset
2797 #define BT_FN_I1_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[0]
kono
parents:
diff changeset
2798 #define BT_FN_I1_VPTR_I1_INT BT_FN_IX_VPTR_IX_INT[0]
kono
parents:
diff changeset
2799 #define BT_FN_VOID_VPTR_I1_INT BT_FN_VOID_VPTR_IX_INT[0]
kono
parents:
diff changeset
2800 #define BT_FN_BOOL_VPTR_PTR_I2_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[1]
kono
parents:
diff changeset
2801 #define BT_FN_I2_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[1]
kono
parents:
diff changeset
2802 #define BT_FN_I2_VPTR_I2_INT BT_FN_IX_VPTR_IX_INT[1]
kono
parents:
diff changeset
2803 #define BT_FN_VOID_VPTR_I2_INT BT_FN_VOID_VPTR_IX_INT[1]
kono
parents:
diff changeset
2804 #define BT_FN_BOOL_VPTR_PTR_I4_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[2]
kono
parents:
diff changeset
2805 #define BT_FN_I4_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[2]
kono
parents:
diff changeset
2806 #define BT_FN_I4_VPTR_I4_INT BT_FN_IX_VPTR_IX_INT[2]
kono
parents:
diff changeset
2807 #define BT_FN_VOID_VPTR_I4_INT BT_FN_VOID_VPTR_IX_INT[2]
kono
parents:
diff changeset
2808 #define BT_FN_BOOL_VPTR_PTR_I8_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[3]
kono
parents:
diff changeset
2809 #define BT_FN_I8_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[3]
kono
parents:
diff changeset
2810 #define BT_FN_I8_VPTR_I8_INT BT_FN_IX_VPTR_IX_INT[3]
kono
parents:
diff changeset
2811 #define BT_FN_VOID_VPTR_I8_INT BT_FN_VOID_VPTR_IX_INT[3]
kono
parents:
diff changeset
2812 #define BT_FN_BOOL_VPTR_PTR_I16_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[4]
kono
parents:
diff changeset
2813 #define BT_FN_I16_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[4]
kono
parents:
diff changeset
2814 #define BT_FN_I16_VPTR_I16_INT BT_FN_IX_VPTR_IX_INT[4]
kono
parents:
diff changeset
2815 #define BT_FN_VOID_VPTR_I16_INT BT_FN_VOID_VPTR_IX_INT[4]
kono
parents:
diff changeset
2816 #undef ATTR_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2817 #define ATTR_NOTHROW_LEAF_LIST ECF_NOTHROW | ECF_LEAF
kono
parents:
diff changeset
2818 #undef ATTR_TMPURE_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2819 #define ATTR_TMPURE_NOTHROW_LEAF_LIST ECF_TM_PURE | ATTR_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2820 #undef ATTR_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2821 #define ATTR_NORETURN_NOTHROW_LEAF_LIST ECF_NORETURN | ATTR_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2822 #undef ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2823 #define ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST \
kono
parents:
diff changeset
2824 ECF_CONST | ATTR_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2825 #undef ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2826 #define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST \
kono
parents:
diff changeset
2827 ECF_TM_PURE | ATTR_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2828 #undef ATTR_COLD_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2829 #define ATTR_COLD_NOTHROW_LEAF_LIST \
kono
parents:
diff changeset
2830 /* ECF_COLD missing */ ATTR_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2831 #undef ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2832 #define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \
kono
parents:
diff changeset
2833 /* ECF_COLD missing */ ATTR_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2834 #undef ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2835 #define ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST \
kono
parents:
diff changeset
2836 /* ECF_COLD missing */ ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2837 #undef ATTR_PURE_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2838 #define ATTR_PURE_NOTHROW_LEAF_LIST ECF_PURE | ATTR_NOTHROW_LEAF_LIST
kono
parents:
diff changeset
2839 #undef DEF_BUILTIN_STUB
kono
parents:
diff changeset
2840 #define DEF_BUILTIN_STUB(ENUM, NAME)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2841 #undef DEF_SANITIZER_BUILTIN_1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2842 #define DEF_SANITIZER_BUILTIN_1(ENUM, NAME, TYPE, ATTRS) \
111
kono
parents:
diff changeset
2843 do { \
kono
parents:
diff changeset
2844 decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM, \
kono
parents:
diff changeset
2845 BUILT_IN_NORMAL, NAME, NULL_TREE); \
kono
parents:
diff changeset
2846 set_call_expr_flags (decl, ATTRS); \
kono
parents:
diff changeset
2847 set_builtin_decl (ENUM, decl, true); \
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2848 } while (0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2849 #undef DEF_SANITIZER_BUILTIN
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2850 #define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2851 DEF_SANITIZER_BUILTIN_1 (ENUM, NAME, TYPE, ATTRS);
111
kono
parents:
diff changeset
2852
kono
parents:
diff changeset
2853 #include "sanitizer.def"
kono
parents:
diff changeset
2854
kono
parents:
diff changeset
2855 /* -fsanitize=object-size uses __builtin_object_size, but that might
kono
parents:
diff changeset
2856 not be available for e.g. Fortran at this point. We use
kono
parents:
diff changeset
2857 DEF_SANITIZER_BUILTIN here only as a convenience macro. */
kono
parents:
diff changeset
2858 if ((flag_sanitize & SANITIZE_OBJECT_SIZE)
kono
parents:
diff changeset
2859 && !builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE))
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2860 DEF_SANITIZER_BUILTIN_1 (BUILT_IN_OBJECT_SIZE, "object_size",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2861 BT_FN_SIZE_CONST_PTR_INT,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2862 ATTR_PURE_NOTHROW_LEAF_LIST);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2863
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2864 #undef DEF_SANITIZER_BUILTIN_1
111
kono
parents:
diff changeset
2865 #undef DEF_SANITIZER_BUILTIN
kono
parents:
diff changeset
2866 #undef DEF_BUILTIN_STUB
kono
parents:
diff changeset
2867 }
kono
parents:
diff changeset
2868
kono
parents:
diff changeset
2869 /* Called via htab_traverse. Count number of emitted
kono
parents:
diff changeset
2870 STRING_CSTs in the constant hash table. */
kono
parents:
diff changeset
2871
kono
parents:
diff changeset
2872 int
kono
parents:
diff changeset
2873 count_string_csts (constant_descriptor_tree **slot,
kono
parents:
diff changeset
2874 unsigned HOST_WIDE_INT *data)
kono
parents:
diff changeset
2875 {
kono
parents:
diff changeset
2876 struct constant_descriptor_tree *desc = *slot;
kono
parents:
diff changeset
2877 if (TREE_CODE (desc->value) == STRING_CST
kono
parents:
diff changeset
2878 && TREE_ASM_WRITTEN (desc->value)
kono
parents:
diff changeset
2879 && asan_protect_global (desc->value))
kono
parents:
diff changeset
2880 ++*data;
kono
parents:
diff changeset
2881 return 1;
kono
parents:
diff changeset
2882 }
kono
parents:
diff changeset
2883
kono
parents:
diff changeset
2884 /* Helper structure to pass two parameters to
kono
parents:
diff changeset
2885 add_string_csts. */
kono
parents:
diff changeset
2886
kono
parents:
diff changeset
2887 struct asan_add_string_csts_data
kono
parents:
diff changeset
2888 {
kono
parents:
diff changeset
2889 tree type;
kono
parents:
diff changeset
2890 vec<constructor_elt, va_gc> *v;
kono
parents:
diff changeset
2891 };
kono
parents:
diff changeset
2892
kono
parents:
diff changeset
2893 /* Called via hash_table::traverse. Call asan_add_global
kono
parents:
diff changeset
2894 on emitted STRING_CSTs from the constant hash table. */
kono
parents:
diff changeset
2895
kono
parents:
diff changeset
2896 int
kono
parents:
diff changeset
2897 add_string_csts (constant_descriptor_tree **slot,
kono
parents:
diff changeset
2898 asan_add_string_csts_data *aascd)
kono
parents:
diff changeset
2899 {
kono
parents:
diff changeset
2900 struct constant_descriptor_tree *desc = *slot;
kono
parents:
diff changeset
2901 if (TREE_CODE (desc->value) == STRING_CST
kono
parents:
diff changeset
2902 && TREE_ASM_WRITTEN (desc->value)
kono
parents:
diff changeset
2903 && asan_protect_global (desc->value))
kono
parents:
diff changeset
2904 {
kono
parents:
diff changeset
2905 asan_add_global (SYMBOL_REF_DECL (XEXP (desc->rtl, 0)),
kono
parents:
diff changeset
2906 aascd->type, aascd->v);
kono
parents:
diff changeset
2907 }
kono
parents:
diff changeset
2908 return 1;
kono
parents:
diff changeset
2909 }
kono
parents:
diff changeset
2910
kono
parents:
diff changeset
2911 /* Needs to be GTY(()), because cgraph_build_static_cdtor may
kono
parents:
diff changeset
2912 invoke ggc_collect. */
kono
parents:
diff changeset
2913 static GTY(()) tree asan_ctor_statements;
kono
parents:
diff changeset
2914
kono
parents:
diff changeset
2915 /* Module-level instrumentation.
kono
parents:
diff changeset
2916 - Insert __asan_init_vN() into the list of CTORs.
kono
parents:
diff changeset
2917 - TODO: insert redzones around globals.
kono
parents:
diff changeset
2918 */
kono
parents:
diff changeset
2919
kono
parents:
diff changeset
2920 void
kono
parents:
diff changeset
2921 asan_finish_file (void)
kono
parents:
diff changeset
2922 {
kono
parents:
diff changeset
2923 varpool_node *vnode;
kono
parents:
diff changeset
2924 unsigned HOST_WIDE_INT gcount = 0;
kono
parents:
diff changeset
2925
kono
parents:
diff changeset
2926 if (shadow_ptr_types[0] == NULL_TREE)
kono
parents:
diff changeset
2927 asan_init_shadow_ptr_types ();
kono
parents:
diff changeset
2928 /* Avoid instrumenting code in the asan ctors/dtors.
kono
parents:
diff changeset
2929 We don't need to insert padding after the description strings,
kono
parents:
diff changeset
2930 nor after .LASAN* array. */
kono
parents:
diff changeset
2931 flag_sanitize &= ~SANITIZE_ADDRESS;
kono
parents:
diff changeset
2932
kono
parents:
diff changeset
2933 /* For user-space we want asan constructors to run first.
kono
parents:
diff changeset
2934 Linux kernel does not support priorities other than default, and the only
kono
parents:
diff changeset
2935 other user of constructors is coverage. So we run with the default
kono
parents:
diff changeset
2936 priority. */
kono
parents:
diff changeset
2937 int priority = flag_sanitize & SANITIZE_USER_ADDRESS
kono
parents:
diff changeset
2938 ? MAX_RESERVED_INIT_PRIORITY - 1 : DEFAULT_INIT_PRIORITY;
kono
parents:
diff changeset
2939
kono
parents:
diff changeset
2940 if (flag_sanitize & SANITIZE_USER_ADDRESS)
kono
parents:
diff changeset
2941 {
kono
parents:
diff changeset
2942 tree fn = builtin_decl_implicit (BUILT_IN_ASAN_INIT);
kono
parents:
diff changeset
2943 append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements);
kono
parents:
diff changeset
2944 fn = builtin_decl_implicit (BUILT_IN_ASAN_VERSION_MISMATCH_CHECK);
kono
parents:
diff changeset
2945 append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements);
kono
parents:
diff changeset
2946 }
kono
parents:
diff changeset
2947 FOR_EACH_DEFINED_VARIABLE (vnode)
kono
parents:
diff changeset
2948 if (TREE_ASM_WRITTEN (vnode->decl)
kono
parents:
diff changeset
2949 && asan_protect_global (vnode->decl))
kono
parents:
diff changeset
2950 ++gcount;
kono
parents:
diff changeset
2951 hash_table<tree_descriptor_hasher> *const_desc_htab = constant_pool_htab ();
kono
parents:
diff changeset
2952 const_desc_htab->traverse<unsigned HOST_WIDE_INT *, count_string_csts>
kono
parents:
diff changeset
2953 (&gcount);
kono
parents:
diff changeset
2954 if (gcount)
kono
parents:
diff changeset
2955 {
kono
parents:
diff changeset
2956 tree type = asan_global_struct (), var, ctor;
kono
parents:
diff changeset
2957 tree dtor_statements = NULL_TREE;
kono
parents:
diff changeset
2958 vec<constructor_elt, va_gc> *v;
kono
parents:
diff changeset
2959 char buf[20];
kono
parents:
diff changeset
2960
kono
parents:
diff changeset
2961 type = build_array_type_nelts (type, gcount);
kono
parents:
diff changeset
2962 ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", 0);
kono
parents:
diff changeset
2963 var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf),
kono
parents:
diff changeset
2964 type);
kono
parents:
diff changeset
2965 TREE_STATIC (var) = 1;
kono
parents:
diff changeset
2966 TREE_PUBLIC (var) = 0;
kono
parents:
diff changeset
2967 DECL_ARTIFICIAL (var) = 1;
kono
parents:
diff changeset
2968 DECL_IGNORED_P (var) = 1;
kono
parents:
diff changeset
2969 vec_alloc (v, gcount);
kono
parents:
diff changeset
2970 FOR_EACH_DEFINED_VARIABLE (vnode)
kono
parents:
diff changeset
2971 if (TREE_ASM_WRITTEN (vnode->decl)
kono
parents:
diff changeset
2972 && asan_protect_global (vnode->decl))
kono
parents:
diff changeset
2973 asan_add_global (vnode->decl, TREE_TYPE (type), v);
kono
parents:
diff changeset
2974 struct asan_add_string_csts_data aascd;
kono
parents:
diff changeset
2975 aascd.type = TREE_TYPE (type);
kono
parents:
diff changeset
2976 aascd.v = v;
kono
parents:
diff changeset
2977 const_desc_htab->traverse<asan_add_string_csts_data *, add_string_csts>
kono
parents:
diff changeset
2978 (&aascd);
kono
parents:
diff changeset
2979 ctor = build_constructor (type, v);
kono
parents:
diff changeset
2980 TREE_CONSTANT (ctor) = 1;
kono
parents:
diff changeset
2981 TREE_STATIC (ctor) = 1;
kono
parents:
diff changeset
2982 DECL_INITIAL (var) = ctor;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2983 SET_DECL_ALIGN (var, MAX (DECL_ALIGN (var),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2984 ASAN_SHADOW_GRANULARITY * BITS_PER_UNIT));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2985
111
kono
parents:
diff changeset
2986 varpool_node::finalize_decl (var);
kono
parents:
diff changeset
2987
kono
parents:
diff changeset
2988 tree fn = builtin_decl_implicit (BUILT_IN_ASAN_REGISTER_GLOBALS);
kono
parents:
diff changeset
2989 tree gcount_tree = build_int_cst (pointer_sized_int_node, gcount);
kono
parents:
diff changeset
2990 append_to_statement_list (build_call_expr (fn, 2,
kono
parents:
diff changeset
2991 build_fold_addr_expr (var),
kono
parents:
diff changeset
2992 gcount_tree),
kono
parents:
diff changeset
2993 &asan_ctor_statements);
kono
parents:
diff changeset
2994
kono
parents:
diff changeset
2995 fn = builtin_decl_implicit (BUILT_IN_ASAN_UNREGISTER_GLOBALS);
kono
parents:
diff changeset
2996 append_to_statement_list (build_call_expr (fn, 2,
kono
parents:
diff changeset
2997 build_fold_addr_expr (var),
kono
parents:
diff changeset
2998 gcount_tree),
kono
parents:
diff changeset
2999 &dtor_statements);
kono
parents:
diff changeset
3000 cgraph_build_static_cdtor ('D', dtor_statements, priority);
kono
parents:
diff changeset
3001 }
kono
parents:
diff changeset
3002 if (asan_ctor_statements)
kono
parents:
diff changeset
3003 cgraph_build_static_cdtor ('I', asan_ctor_statements, priority);
kono
parents:
diff changeset
3004 flag_sanitize |= SANITIZE_ADDRESS;
kono
parents:
diff changeset
3005 }
kono
parents:
diff changeset
3006
kono
parents:
diff changeset
3007 /* Poison or unpoison (depending on IS_CLOBBER variable) shadow memory based
kono
parents:
diff changeset
3008 on SHADOW address. Newly added statements will be added to ITER with
kono
parents:
diff changeset
3009 given location LOC. We mark SIZE bytes in shadow memory, where
kono
parents:
diff changeset
3010 LAST_CHUNK_SIZE is greater than zero in situation where we are at the
kono
parents:
diff changeset
3011 end of a variable. */
kono
parents:
diff changeset
3012
kono
parents:
diff changeset
3013 static void
kono
parents:
diff changeset
3014 asan_store_shadow_bytes (gimple_stmt_iterator *iter, location_t loc,
kono
parents:
diff changeset
3015 tree shadow,
kono
parents:
diff changeset
3016 unsigned HOST_WIDE_INT base_addr_offset,
kono
parents:
diff changeset
3017 bool is_clobber, unsigned size,
kono
parents:
diff changeset
3018 unsigned last_chunk_size)
kono
parents:
diff changeset
3019 {
kono
parents:
diff changeset
3020 tree shadow_ptr_type;
kono
parents:
diff changeset
3021
kono
parents:
diff changeset
3022 switch (size)
kono
parents:
diff changeset
3023 {
kono
parents:
diff changeset
3024 case 1:
kono
parents:
diff changeset
3025 shadow_ptr_type = shadow_ptr_types[0];
kono
parents:
diff changeset
3026 break;
kono
parents:
diff changeset
3027 case 2:
kono
parents:
diff changeset
3028 shadow_ptr_type = shadow_ptr_types[1];
kono
parents:
diff changeset
3029 break;
kono
parents:
diff changeset
3030 case 4:
kono
parents:
diff changeset
3031 shadow_ptr_type = shadow_ptr_types[2];
kono
parents:
diff changeset
3032 break;
kono
parents:
diff changeset
3033 default:
kono
parents:
diff changeset
3034 gcc_unreachable ();
kono
parents:
diff changeset
3035 }
kono
parents:
diff changeset
3036
kono
parents:
diff changeset
3037 unsigned char c = (char) is_clobber ? ASAN_STACK_MAGIC_USE_AFTER_SCOPE : 0;
kono
parents:
diff changeset
3038 unsigned HOST_WIDE_INT val = 0;
kono
parents:
diff changeset
3039 unsigned last_pos = size;
kono
parents:
diff changeset
3040 if (last_chunk_size && !is_clobber)
kono
parents:
diff changeset
3041 last_pos = BYTES_BIG_ENDIAN ? 0 : size - 1;
kono
parents:
diff changeset
3042 for (unsigned i = 0; i < size; ++i)
kono
parents:
diff changeset
3043 {
kono
parents:
diff changeset
3044 unsigned char shadow_c = c;
kono
parents:
diff changeset
3045 if (i == last_pos)
kono
parents:
diff changeset
3046 shadow_c = last_chunk_size;
kono
parents:
diff changeset
3047 val |= (unsigned HOST_WIDE_INT) shadow_c << (BITS_PER_UNIT * i);
kono
parents:
diff changeset
3048 }
kono
parents:
diff changeset
3049
kono
parents:
diff changeset
3050 /* Handle last chunk in unpoisoning. */
kono
parents:
diff changeset
3051 tree magic = build_int_cst (TREE_TYPE (shadow_ptr_type), val);
kono
parents:
diff changeset
3052
kono
parents:
diff changeset
3053 tree dest = build2 (MEM_REF, TREE_TYPE (shadow_ptr_type), shadow,
kono
parents:
diff changeset
3054 build_int_cst (shadow_ptr_type, base_addr_offset));
kono
parents:
diff changeset
3055
kono
parents:
diff changeset
3056 gimple *g = gimple_build_assign (dest, magic);
kono
parents:
diff changeset
3057 gimple_set_location (g, loc);
kono
parents:
diff changeset
3058 gsi_insert_after (iter, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3059 }
kono
parents:
diff changeset
3060
kono
parents:
diff changeset
3061 /* Expand the ASAN_MARK builtins. */
kono
parents:
diff changeset
3062
kono
parents:
diff changeset
3063 bool
kono
parents:
diff changeset
3064 asan_expand_mark_ifn (gimple_stmt_iterator *iter)
kono
parents:
diff changeset
3065 {
kono
parents:
diff changeset
3066 gimple *g = gsi_stmt (*iter);
kono
parents:
diff changeset
3067 location_t loc = gimple_location (g);
kono
parents:
diff changeset
3068 HOST_WIDE_INT flag = tree_to_shwi (gimple_call_arg (g, 0));
kono
parents:
diff changeset
3069 bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
kono
parents:
diff changeset
3070
kono
parents:
diff changeset
3071 tree base = gimple_call_arg (g, 1);
kono
parents:
diff changeset
3072 gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
kono
parents:
diff changeset
3073 tree decl = TREE_OPERAND (base, 0);
kono
parents:
diff changeset
3074
kono
parents:
diff changeset
3075 /* For a nested function, we can have: ASAN_MARK (2, &FRAME.2.fp_input, 4) */
kono
parents:
diff changeset
3076 if (TREE_CODE (decl) == COMPONENT_REF
kono
parents:
diff changeset
3077 && DECL_NONLOCAL_FRAME (TREE_OPERAND (decl, 0)))
kono
parents:
diff changeset
3078 decl = TREE_OPERAND (decl, 0);
kono
parents:
diff changeset
3079
kono
parents:
diff changeset
3080 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
kono
parents:
diff changeset
3081
kono
parents:
diff changeset
3082 if (is_poison)
kono
parents:
diff changeset
3083 {
kono
parents:
diff changeset
3084 if (asan_handled_variables == NULL)
kono
parents:
diff changeset
3085 asan_handled_variables = new hash_set<tree> (16);
kono
parents:
diff changeset
3086 asan_handled_variables->add (decl);
kono
parents:
diff changeset
3087 }
kono
parents:
diff changeset
3088 tree len = gimple_call_arg (g, 2);
kono
parents:
diff changeset
3089
kono
parents:
diff changeset
3090 gcc_assert (tree_fits_shwi_p (len));
kono
parents:
diff changeset
3091 unsigned HOST_WIDE_INT size_in_bytes = tree_to_shwi (len);
kono
parents:
diff changeset
3092 gcc_assert (size_in_bytes);
kono
parents:
diff changeset
3093
kono
parents:
diff changeset
3094 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3095 NOP_EXPR, base);
kono
parents:
diff changeset
3096 gimple_set_location (g, loc);
kono
parents:
diff changeset
3097 gsi_replace (iter, g, false);
kono
parents:
diff changeset
3098 tree base_addr = gimple_assign_lhs (g);
kono
parents:
diff changeset
3099
kono
parents:
diff changeset
3100 /* Generate direct emission if size_in_bytes is small. */
kono
parents:
diff changeset
3101 if (size_in_bytes <= ASAN_PARAM_USE_AFTER_SCOPE_DIRECT_EMISSION_THRESHOLD)
kono
parents:
diff changeset
3102 {
kono
parents:
diff changeset
3103 unsigned HOST_WIDE_INT shadow_size = shadow_mem_size (size_in_bytes);
kono
parents:
diff changeset
3104
kono
parents:
diff changeset
3105 tree shadow = build_shadow_mem_access (iter, loc, base_addr,
kono
parents:
diff changeset
3106 shadow_ptr_types[0], true);
kono
parents:
diff changeset
3107
kono
parents:
diff changeset
3108 for (unsigned HOST_WIDE_INT offset = 0; offset < shadow_size;)
kono
parents:
diff changeset
3109 {
kono
parents:
diff changeset
3110 unsigned size = 1;
kono
parents:
diff changeset
3111 if (shadow_size - offset >= 4)
kono
parents:
diff changeset
3112 size = 4;
kono
parents:
diff changeset
3113 else if (shadow_size - offset >= 2)
kono
parents:
diff changeset
3114 size = 2;
kono
parents:
diff changeset
3115
kono
parents:
diff changeset
3116 unsigned HOST_WIDE_INT last_chunk_size = 0;
kono
parents:
diff changeset
3117 unsigned HOST_WIDE_INT s = (offset + size) * ASAN_SHADOW_GRANULARITY;
kono
parents:
diff changeset
3118 if (s > size_in_bytes)
kono
parents:
diff changeset
3119 last_chunk_size = ASAN_SHADOW_GRANULARITY - (s - size_in_bytes);
kono
parents:
diff changeset
3120
kono
parents:
diff changeset
3121 asan_store_shadow_bytes (iter, loc, shadow, offset, is_poison,
kono
parents:
diff changeset
3122 size, last_chunk_size);
kono
parents:
diff changeset
3123 offset += size;
kono
parents:
diff changeset
3124 }
kono
parents:
diff changeset
3125 }
kono
parents:
diff changeset
3126 else
kono
parents:
diff changeset
3127 {
kono
parents:
diff changeset
3128 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3129 NOP_EXPR, len);
kono
parents:
diff changeset
3130 gimple_set_location (g, loc);
kono
parents:
diff changeset
3131 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
3132 tree sz_arg = gimple_assign_lhs (g);
kono
parents:
diff changeset
3133
kono
parents:
diff changeset
3134 tree fun
kono
parents:
diff changeset
3135 = builtin_decl_implicit (is_poison ? BUILT_IN_ASAN_POISON_STACK_MEMORY
kono
parents:
diff changeset
3136 : BUILT_IN_ASAN_UNPOISON_STACK_MEMORY);
kono
parents:
diff changeset
3137 g = gimple_build_call (fun, 2, base_addr, sz_arg);
kono
parents:
diff changeset
3138 gimple_set_location (g, loc);
kono
parents:
diff changeset
3139 gsi_insert_after (iter, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3140 }
kono
parents:
diff changeset
3141
kono
parents:
diff changeset
3142 return false;
kono
parents:
diff changeset
3143 }
kono
parents:
diff changeset
3144
kono
parents:
diff changeset
3145 /* Expand the ASAN_{LOAD,STORE} builtins. */
kono
parents:
diff changeset
3146
kono
parents:
diff changeset
3147 bool
kono
parents:
diff changeset
3148 asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls)
kono
parents:
diff changeset
3149 {
kono
parents:
diff changeset
3150 gimple *g = gsi_stmt (*iter);
kono
parents:
diff changeset
3151 location_t loc = gimple_location (g);
kono
parents:
diff changeset
3152 bool recover_p;
kono
parents:
diff changeset
3153 if (flag_sanitize & SANITIZE_USER_ADDRESS)
kono
parents:
diff changeset
3154 recover_p = (flag_sanitize_recover & SANITIZE_USER_ADDRESS) != 0;
kono
parents:
diff changeset
3155 else
kono
parents:
diff changeset
3156 recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0;
kono
parents:
diff changeset
3157
kono
parents:
diff changeset
3158 HOST_WIDE_INT flags = tree_to_shwi (gimple_call_arg (g, 0));
kono
parents:
diff changeset
3159 gcc_assert (flags < ASAN_CHECK_LAST);
kono
parents:
diff changeset
3160 bool is_scalar_access = (flags & ASAN_CHECK_SCALAR_ACCESS) != 0;
kono
parents:
diff changeset
3161 bool is_store = (flags & ASAN_CHECK_STORE) != 0;
kono
parents:
diff changeset
3162 bool is_non_zero_len = (flags & ASAN_CHECK_NON_ZERO_LEN) != 0;
kono
parents:
diff changeset
3163
kono
parents:
diff changeset
3164 tree base = gimple_call_arg (g, 1);
kono
parents:
diff changeset
3165 tree len = gimple_call_arg (g, 2);
kono
parents:
diff changeset
3166 HOST_WIDE_INT align = tree_to_shwi (gimple_call_arg (g, 3));
kono
parents:
diff changeset
3167
kono
parents:
diff changeset
3168 HOST_WIDE_INT size_in_bytes
kono
parents:
diff changeset
3169 = is_scalar_access && tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1;
kono
parents:
diff changeset
3170
kono
parents:
diff changeset
3171 if (use_calls)
kono
parents:
diff changeset
3172 {
kono
parents:
diff changeset
3173 /* Instrument using callbacks. */
kono
parents:
diff changeset
3174 gimple *g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3175 NOP_EXPR, base);
kono
parents:
diff changeset
3176 gimple_set_location (g, loc);
kono
parents:
diff changeset
3177 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
3178 tree base_addr = gimple_assign_lhs (g);
kono
parents:
diff changeset
3179
kono
parents:
diff changeset
3180 int nargs;
kono
parents:
diff changeset
3181 tree fun = check_func (is_store, recover_p, size_in_bytes, &nargs);
kono
parents:
diff changeset
3182 if (nargs == 1)
kono
parents:
diff changeset
3183 g = gimple_build_call (fun, 1, base_addr);
kono
parents:
diff changeset
3184 else
kono
parents:
diff changeset
3185 {
kono
parents:
diff changeset
3186 gcc_assert (nargs == 2);
kono
parents:
diff changeset
3187 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3188 NOP_EXPR, len);
kono
parents:
diff changeset
3189 gimple_set_location (g, loc);
kono
parents:
diff changeset
3190 gsi_insert_before (iter, g, GSI_SAME_STMT);
kono
parents:
diff changeset
3191 tree sz_arg = gimple_assign_lhs (g);
kono
parents:
diff changeset
3192 g = gimple_build_call (fun, nargs, base_addr, sz_arg);
kono
parents:
diff changeset
3193 }
kono
parents:
diff changeset
3194 gimple_set_location (g, loc);
kono
parents:
diff changeset
3195 gsi_replace (iter, g, false);
kono
parents:
diff changeset
3196 return false;
kono
parents:
diff changeset
3197 }
kono
parents:
diff changeset
3198
kono
parents:
diff changeset
3199 HOST_WIDE_INT real_size_in_bytes = size_in_bytes == -1 ? 1 : size_in_bytes;
kono
parents:
diff changeset
3200
kono
parents:
diff changeset
3201 tree shadow_ptr_type = shadow_ptr_types[real_size_in_bytes == 16 ? 1 : 0];
kono
parents:
diff changeset
3202 tree shadow_type = TREE_TYPE (shadow_ptr_type);
kono
parents:
diff changeset
3203
kono
parents:
diff changeset
3204 gimple_stmt_iterator gsi = *iter;
kono
parents:
diff changeset
3205
kono
parents:
diff changeset
3206 if (!is_non_zero_len)
kono
parents:
diff changeset
3207 {
kono
parents:
diff changeset
3208 /* So, the length of the memory area to asan-protect is
kono
parents:
diff changeset
3209 non-constant. Let's guard the generated instrumentation code
kono
parents:
diff changeset
3210 like:
kono
parents:
diff changeset
3211
kono
parents:
diff changeset
3212 if (len != 0)
kono
parents:
diff changeset
3213 {
kono
parents:
diff changeset
3214 //asan instrumentation code goes here.
kono
parents:
diff changeset
3215 }
kono
parents:
diff changeset
3216 // falltrough instructions, starting with *ITER. */
kono
parents:
diff changeset
3217
kono
parents:
diff changeset
3218 g = gimple_build_cond (NE_EXPR,
kono
parents:
diff changeset
3219 len,
kono
parents:
diff changeset
3220 build_int_cst (TREE_TYPE (len), 0),
kono
parents:
diff changeset
3221 NULL_TREE, NULL_TREE);
kono
parents:
diff changeset
3222 gimple_set_location (g, loc);
kono
parents:
diff changeset
3223
kono
parents:
diff changeset
3224 basic_block then_bb, fallthrough_bb;
kono
parents:
diff changeset
3225 insert_if_then_before_iter (as_a <gcond *> (g), iter,
kono
parents:
diff changeset
3226 /*then_more_likely_p=*/true,
kono
parents:
diff changeset
3227 &then_bb, &fallthrough_bb);
kono
parents:
diff changeset
3228 /* Note that fallthrough_bb starts with the statement that was
kono
parents:
diff changeset
3229 pointed to by ITER. */
kono
parents:
diff changeset
3230
kono
parents:
diff changeset
3231 /* The 'then block' of the 'if (len != 0) condition is where
kono
parents:
diff changeset
3232 we'll generate the asan instrumentation code now. */
kono
parents:
diff changeset
3233 gsi = gsi_last_bb (then_bb);
kono
parents:
diff changeset
3234 }
kono
parents:
diff changeset
3235
kono
parents:
diff changeset
3236 /* Get an iterator on the point where we can add the condition
kono
parents:
diff changeset
3237 statement for the instrumentation. */
kono
parents:
diff changeset
3238 basic_block then_bb, else_bb;
kono
parents:
diff changeset
3239 gsi = create_cond_insert_point (&gsi, /*before_p*/false,
kono
parents:
diff changeset
3240 /*then_more_likely_p=*/false,
kono
parents:
diff changeset
3241 /*create_then_fallthru_edge*/recover_p,
kono
parents:
diff changeset
3242 &then_bb,
kono
parents:
diff changeset
3243 &else_bb);
kono
parents:
diff changeset
3244
kono
parents:
diff changeset
3245 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3246 NOP_EXPR, base);
kono
parents:
diff changeset
3247 gimple_set_location (g, loc);
kono
parents:
diff changeset
3248 gsi_insert_before (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3249 tree base_addr = gimple_assign_lhs (g);
kono
parents:
diff changeset
3250
kono
parents:
diff changeset
3251 tree t = NULL_TREE;
kono
parents:
diff changeset
3252 if (real_size_in_bytes >= 8)
kono
parents:
diff changeset
3253 {
kono
parents:
diff changeset
3254 tree shadow = build_shadow_mem_access (&gsi, loc, base_addr,
kono
parents:
diff changeset
3255 shadow_ptr_type);
kono
parents:
diff changeset
3256 t = shadow;
kono
parents:
diff changeset
3257 }
kono
parents:
diff changeset
3258 else
kono
parents:
diff changeset
3259 {
kono
parents:
diff changeset
3260 /* Slow path for 1, 2 and 4 byte accesses. */
kono
parents:
diff changeset
3261 /* Test (shadow != 0)
kono
parents:
diff changeset
3262 & ((base_addr & 7) + (real_size_in_bytes - 1)) >= shadow). */
kono
parents:
diff changeset
3263 tree shadow = build_shadow_mem_access (&gsi, loc, base_addr,
kono
parents:
diff changeset
3264 shadow_ptr_type);
kono
parents:
diff changeset
3265 gimple *shadow_test = build_assign (NE_EXPR, shadow, 0);
kono
parents:
diff changeset
3266 gimple_seq seq = NULL;
kono
parents:
diff changeset
3267 gimple_seq_add_stmt (&seq, shadow_test);
kono
parents:
diff changeset
3268 /* Aligned (>= 8 bytes) can test just
kono
parents:
diff changeset
3269 (real_size_in_bytes - 1 >= shadow), as base_addr & 7 is known
kono
parents:
diff changeset
3270 to be 0. */
kono
parents:
diff changeset
3271 if (align < 8)
kono
parents:
diff changeset
3272 {
kono
parents:
diff changeset
3273 gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR,
kono
parents:
diff changeset
3274 base_addr, 7));
kono
parents:
diff changeset
3275 gimple_seq_add_stmt (&seq,
kono
parents:
diff changeset
3276 build_type_cast (shadow_type,
kono
parents:
diff changeset
3277 gimple_seq_last (seq)));
kono
parents:
diff changeset
3278 if (real_size_in_bytes > 1)
kono
parents:
diff changeset
3279 gimple_seq_add_stmt (&seq,
kono
parents:
diff changeset
3280 build_assign (PLUS_EXPR,
kono
parents:
diff changeset
3281 gimple_seq_last (seq),
kono
parents:
diff changeset
3282 real_size_in_bytes - 1));
kono
parents:
diff changeset
3283 t = gimple_assign_lhs (gimple_seq_last_stmt (seq));
kono
parents:
diff changeset
3284 }
kono
parents:
diff changeset
3285 else
kono
parents:
diff changeset
3286 t = build_int_cst (shadow_type, real_size_in_bytes - 1);
kono
parents:
diff changeset
3287 gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, t, shadow));
kono
parents:
diff changeset
3288 gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test,
kono
parents:
diff changeset
3289 gimple_seq_last (seq)));
kono
parents:
diff changeset
3290 t = gimple_assign_lhs (gimple_seq_last (seq));
kono
parents:
diff changeset
3291 gimple_seq_set_location (seq, loc);
kono
parents:
diff changeset
3292 gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
kono
parents:
diff changeset
3293
kono
parents:
diff changeset
3294 /* For non-constant, misaligned or otherwise weird access sizes,
kono
parents:
diff changeset
3295 check first and last byte. */
kono
parents:
diff changeset
3296 if (size_in_bytes == -1)
kono
parents:
diff changeset
3297 {
kono
parents:
diff changeset
3298 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3299 MINUS_EXPR, len,
kono
parents:
diff changeset
3300 build_int_cst (pointer_sized_int_node, 1));
kono
parents:
diff changeset
3301 gimple_set_location (g, loc);
kono
parents:
diff changeset
3302 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3303 tree last = gimple_assign_lhs (g);
kono
parents:
diff changeset
3304 g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
kono
parents:
diff changeset
3305 PLUS_EXPR, base_addr, last);
kono
parents:
diff changeset
3306 gimple_set_location (g, loc);
kono
parents:
diff changeset
3307 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3308 tree base_end_addr = gimple_assign_lhs (g);
kono
parents:
diff changeset
3309
kono
parents:
diff changeset
3310 tree shadow = build_shadow_mem_access (&gsi, loc, base_end_addr,
kono
parents:
diff changeset
3311 shadow_ptr_type);
kono
parents:
diff changeset
3312 gimple *shadow_test = build_assign (NE_EXPR, shadow, 0);
kono
parents:
diff changeset
3313 gimple_seq seq = NULL;
kono
parents:
diff changeset
3314 gimple_seq_add_stmt (&seq, shadow_test);
kono
parents:
diff changeset
3315 gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR,
kono
parents:
diff changeset
3316 base_end_addr, 7));
kono
parents:
diff changeset
3317 gimple_seq_add_stmt (&seq, build_type_cast (shadow_type,
kono
parents:
diff changeset
3318 gimple_seq_last (seq)));
kono
parents:
diff changeset
3319 gimple_seq_add_stmt (&seq, build_assign (GE_EXPR,
kono
parents:
diff changeset
3320 gimple_seq_last (seq),
kono
parents:
diff changeset
3321 shadow));
kono
parents:
diff changeset
3322 gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test,
kono
parents:
diff changeset
3323 gimple_seq_last (seq)));
kono
parents:
diff changeset
3324 gimple_seq_add_stmt (&seq, build_assign (BIT_IOR_EXPR, t,
kono
parents:
diff changeset
3325 gimple_seq_last (seq)));
kono
parents:
diff changeset
3326 t = gimple_assign_lhs (gimple_seq_last (seq));
kono
parents:
diff changeset
3327 gimple_seq_set_location (seq, loc);
kono
parents:
diff changeset
3328 gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
kono
parents:
diff changeset
3329 }
kono
parents:
diff changeset
3330 }
kono
parents:
diff changeset
3331
kono
parents:
diff changeset
3332 g = gimple_build_cond (NE_EXPR, t, build_int_cst (TREE_TYPE (t), 0),
kono
parents:
diff changeset
3333 NULL_TREE, NULL_TREE);
kono
parents:
diff changeset
3334 gimple_set_location (g, loc);
kono
parents:
diff changeset
3335 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3336
kono
parents:
diff changeset
3337 /* Generate call to the run-time library (e.g. __asan_report_load8). */
kono
parents:
diff changeset
3338 gsi = gsi_start_bb (then_bb);
kono
parents:
diff changeset
3339 int nargs;
kono
parents:
diff changeset
3340 tree fun = report_error_func (is_store, recover_p, size_in_bytes, &nargs);
kono
parents:
diff changeset
3341 g = gimple_build_call (fun, nargs, base_addr, len);
kono
parents:
diff changeset
3342 gimple_set_location (g, loc);
kono
parents:
diff changeset
3343 gsi_insert_after (&gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3344
kono
parents:
diff changeset
3345 gsi_remove (iter, true);
kono
parents:
diff changeset
3346 *iter = gsi_start_bb (else_bb);
kono
parents:
diff changeset
3347
kono
parents:
diff changeset
3348 return true;
kono
parents:
diff changeset
3349 }
kono
parents:
diff changeset
3350
kono
parents:
diff changeset
3351 /* Create ASAN shadow variable for a VAR_DECL which has been rewritten
kono
parents:
diff changeset
3352 into SSA. Already seen VAR_DECLs are stored in SHADOW_VARS_MAPPING. */
kono
parents:
diff changeset
3353
kono
parents:
diff changeset
3354 static tree
kono
parents:
diff changeset
3355 create_asan_shadow_var (tree var_decl,
kono
parents:
diff changeset
3356 hash_map<tree, tree> &shadow_vars_mapping)
kono
parents:
diff changeset
3357 {
kono
parents:
diff changeset
3358 tree *slot = shadow_vars_mapping.get (var_decl);
kono
parents:
diff changeset
3359 if (slot == NULL)
kono
parents:
diff changeset
3360 {
kono
parents:
diff changeset
3361 tree shadow_var = copy_node (var_decl);
kono
parents:
diff changeset
3362
kono
parents:
diff changeset
3363 copy_body_data id;
kono
parents:
diff changeset
3364 memset (&id, 0, sizeof (copy_body_data));
kono
parents:
diff changeset
3365 id.src_fn = id.dst_fn = current_function_decl;
kono
parents:
diff changeset
3366 copy_decl_for_dup_finish (&id, var_decl, shadow_var);
kono
parents:
diff changeset
3367
kono
parents:
diff changeset
3368 DECL_ARTIFICIAL (shadow_var) = 1;
kono
parents:
diff changeset
3369 DECL_IGNORED_P (shadow_var) = 1;
kono
parents:
diff changeset
3370 DECL_SEEN_IN_BIND_EXPR_P (shadow_var) = 0;
kono
parents:
diff changeset
3371 gimple_add_tmp_var (shadow_var);
kono
parents:
diff changeset
3372
kono
parents:
diff changeset
3373 shadow_vars_mapping.put (var_decl, shadow_var);
kono
parents:
diff changeset
3374 return shadow_var;
kono
parents:
diff changeset
3375 }
kono
parents:
diff changeset
3376 else
kono
parents:
diff changeset
3377 return *slot;
kono
parents:
diff changeset
3378 }
kono
parents:
diff changeset
3379
kono
parents:
diff changeset
3380 /* Expand ASAN_POISON ifn. */
kono
parents:
diff changeset
3381
kono
parents:
diff changeset
3382 bool
kono
parents:
diff changeset
3383 asan_expand_poison_ifn (gimple_stmt_iterator *iter,
kono
parents:
diff changeset
3384 bool *need_commit_edge_insert,
kono
parents:
diff changeset
3385 hash_map<tree, tree> &shadow_vars_mapping)
kono
parents:
diff changeset
3386 {
kono
parents:
diff changeset
3387 gimple *g = gsi_stmt (*iter);
kono
parents:
diff changeset
3388 tree poisoned_var = gimple_call_lhs (g);
kono
parents:
diff changeset
3389 if (!poisoned_var || has_zero_uses (poisoned_var))
kono
parents:
diff changeset
3390 {
kono
parents:
diff changeset
3391 gsi_remove (iter, true);
kono
parents:
diff changeset
3392 return true;
kono
parents:
diff changeset
3393 }
kono
parents:
diff changeset
3394
kono
parents:
diff changeset
3395 if (SSA_NAME_VAR (poisoned_var) == NULL_TREE)
kono
parents:
diff changeset
3396 SET_SSA_NAME_VAR_OR_IDENTIFIER (poisoned_var,
kono
parents:
diff changeset
3397 create_tmp_var (TREE_TYPE (poisoned_var)));
kono
parents:
diff changeset
3398
kono
parents:
diff changeset
3399 tree shadow_var = create_asan_shadow_var (SSA_NAME_VAR (poisoned_var),
kono
parents:
diff changeset
3400 shadow_vars_mapping);
kono
parents:
diff changeset
3401
kono
parents:
diff changeset
3402 bool recover_p;
kono
parents:
diff changeset
3403 if (flag_sanitize & SANITIZE_USER_ADDRESS)
kono
parents:
diff changeset
3404 recover_p = (flag_sanitize_recover & SANITIZE_USER_ADDRESS) != 0;
kono
parents:
diff changeset
3405 else
kono
parents:
diff changeset
3406 recover_p = (flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0;
kono
parents:
diff changeset
3407 tree size = DECL_SIZE_UNIT (shadow_var);
kono
parents:
diff changeset
3408 gimple *poison_call
kono
parents:
diff changeset
3409 = gimple_build_call_internal (IFN_ASAN_MARK, 3,
kono
parents:
diff changeset
3410 build_int_cst (integer_type_node,
kono
parents:
diff changeset
3411 ASAN_MARK_POISON),
kono
parents:
diff changeset
3412 build_fold_addr_expr (shadow_var), size);
kono
parents:
diff changeset
3413
kono
parents:
diff changeset
3414 gimple *use;
kono
parents:
diff changeset
3415 imm_use_iterator imm_iter;
kono
parents:
diff changeset
3416 FOR_EACH_IMM_USE_STMT (use, imm_iter, poisoned_var)
kono
parents:
diff changeset
3417 {
kono
parents:
diff changeset
3418 if (is_gimple_debug (use))
kono
parents:
diff changeset
3419 continue;
kono
parents:
diff changeset
3420
kono
parents:
diff changeset
3421 int nargs;
kono
parents:
diff changeset
3422 bool store_p = gimple_call_internal_p (use, IFN_ASAN_POISON_USE);
kono
parents:
diff changeset
3423 tree fun = report_error_func (store_p, recover_p, tree_to_uhwi (size),
kono
parents:
diff changeset
3424 &nargs);
kono
parents:
diff changeset
3425
kono
parents:
diff changeset
3426 gcall *call = gimple_build_call (fun, 1,
kono
parents:
diff changeset
3427 build_fold_addr_expr (shadow_var));
kono
parents:
diff changeset
3428 gimple_set_location (call, gimple_location (use));
kono
parents:
diff changeset
3429 gimple *call_to_insert = call;
kono
parents:
diff changeset
3430
kono
parents:
diff changeset
3431 /* The USE can be a gimple PHI node. If so, insert the call on
kono
parents:
diff changeset
3432 all edges leading to the PHI node. */
kono
parents:
diff changeset
3433 if (is_a <gphi *> (use))
kono
parents:
diff changeset
3434 {
kono
parents:
diff changeset
3435 gphi *phi = dyn_cast<gphi *> (use);
kono
parents:
diff changeset
3436 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
kono
parents:
diff changeset
3437 if (gimple_phi_arg_def (phi, i) == poisoned_var)
kono
parents:
diff changeset
3438 {
kono
parents:
diff changeset
3439 edge e = gimple_phi_arg_edge (phi, i);
kono
parents:
diff changeset
3440
kono
parents:
diff changeset
3441 /* Do not insert on an edge we can't split. */
kono
parents:
diff changeset
3442 if (e->flags & EDGE_ABNORMAL)
kono
parents:
diff changeset
3443 continue;
kono
parents:
diff changeset
3444
kono
parents:
diff changeset
3445 if (call_to_insert == NULL)
kono
parents:
diff changeset
3446 call_to_insert = gimple_copy (call);
kono
parents:
diff changeset
3447
kono
parents:
diff changeset
3448 gsi_insert_seq_on_edge (e, call_to_insert);
kono
parents:
diff changeset
3449 *need_commit_edge_insert = true;
kono
parents:
diff changeset
3450 call_to_insert = NULL;
kono
parents:
diff changeset
3451 }
kono
parents:
diff changeset
3452 }
kono
parents:
diff changeset
3453 else
kono
parents:
diff changeset
3454 {
kono
parents:
diff changeset
3455 gimple_stmt_iterator gsi = gsi_for_stmt (use);
kono
parents:
diff changeset
3456 if (store_p)
kono
parents:
diff changeset
3457 gsi_replace (&gsi, call, true);
kono
parents:
diff changeset
3458 else
kono
parents:
diff changeset
3459 gsi_insert_before (&gsi, call, GSI_NEW_STMT);
kono
parents:
diff changeset
3460 }
kono
parents:
diff changeset
3461 }
kono
parents:
diff changeset
3462
kono
parents:
diff changeset
3463 SSA_NAME_IS_DEFAULT_DEF (poisoned_var) = true;
kono
parents:
diff changeset
3464 SSA_NAME_DEF_STMT (poisoned_var) = gimple_build_nop ();
kono
parents:
diff changeset
3465 gsi_replace (iter, poison_call, false);
kono
parents:
diff changeset
3466
kono
parents:
diff changeset
3467 return true;
kono
parents:
diff changeset
3468 }
kono
parents:
diff changeset
3469
kono
parents:
diff changeset
3470 /* Instrument the current function. */
kono
parents:
diff changeset
3471
kono
parents:
diff changeset
3472 static unsigned int
kono
parents:
diff changeset
3473 asan_instrument (void)
kono
parents:
diff changeset
3474 {
kono
parents:
diff changeset
3475 if (shadow_ptr_types[0] == NULL_TREE)
kono
parents:
diff changeset
3476 asan_init_shadow_ptr_types ();
kono
parents:
diff changeset
3477 transform_statements ();
kono
parents:
diff changeset
3478 last_alloca_addr = NULL_TREE;
kono
parents:
diff changeset
3479 return 0;
kono
parents:
diff changeset
3480 }
kono
parents:
diff changeset
3481
kono
parents:
diff changeset
3482 static bool
kono
parents:
diff changeset
3483 gate_asan (void)
kono
parents:
diff changeset
3484 {
kono
parents:
diff changeset
3485 return sanitize_flags_p (SANITIZE_ADDRESS);
kono
parents:
diff changeset
3486 }
kono
parents:
diff changeset
3487
kono
parents:
diff changeset
3488 namespace {
kono
parents:
diff changeset
3489
kono
parents:
diff changeset
3490 const pass_data pass_data_asan =
kono
parents:
diff changeset
3491 {
kono
parents:
diff changeset
3492 GIMPLE_PASS, /* type */
kono
parents:
diff changeset
3493 "asan", /* name */
kono
parents:
diff changeset
3494 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
3495 TV_NONE, /* tv_id */
kono
parents:
diff changeset
3496 ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
kono
parents:
diff changeset
3497 0, /* properties_provided */
kono
parents:
diff changeset
3498 0, /* properties_destroyed */
kono
parents:
diff changeset
3499 0, /* todo_flags_start */
kono
parents:
diff changeset
3500 TODO_update_ssa, /* todo_flags_finish */
kono
parents:
diff changeset
3501 };
kono
parents:
diff changeset
3502
kono
parents:
diff changeset
3503 class pass_asan : public gimple_opt_pass
kono
parents:
diff changeset
3504 {
kono
parents:
diff changeset
3505 public:
kono
parents:
diff changeset
3506 pass_asan (gcc::context *ctxt)
kono
parents:
diff changeset
3507 : gimple_opt_pass (pass_data_asan, ctxt)
kono
parents:
diff changeset
3508 {}
kono
parents:
diff changeset
3509
kono
parents:
diff changeset
3510 /* opt_pass methods: */
kono
parents:
diff changeset
3511 opt_pass * clone () { return new pass_asan (m_ctxt); }
kono
parents:
diff changeset
3512 virtual bool gate (function *) { return gate_asan (); }
kono
parents:
diff changeset
3513 virtual unsigned int execute (function *) { return asan_instrument (); }
kono
parents:
diff changeset
3514
kono
parents:
diff changeset
3515 }; // class pass_asan
kono
parents:
diff changeset
3516
kono
parents:
diff changeset
3517 } // anon namespace
kono
parents:
diff changeset
3518
kono
parents:
diff changeset
3519 gimple_opt_pass *
kono
parents:
diff changeset
3520 make_pass_asan (gcc::context *ctxt)
kono
parents:
diff changeset
3521 {
kono
parents:
diff changeset
3522 return new pass_asan (ctxt);
kono
parents:
diff changeset
3523 }
kono
parents:
diff changeset
3524
kono
parents:
diff changeset
3525 namespace {
kono
parents:
diff changeset
3526
kono
parents:
diff changeset
3527 const pass_data pass_data_asan_O0 =
kono
parents:
diff changeset
3528 {
kono
parents:
diff changeset
3529 GIMPLE_PASS, /* type */
kono
parents:
diff changeset
3530 "asan0", /* name */
kono
parents:
diff changeset
3531 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
3532 TV_NONE, /* tv_id */
kono
parents:
diff changeset
3533 ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
kono
parents:
diff changeset
3534 0, /* properties_provided */
kono
parents:
diff changeset
3535 0, /* properties_destroyed */
kono
parents:
diff changeset
3536 0, /* todo_flags_start */
kono
parents:
diff changeset
3537 TODO_update_ssa, /* todo_flags_finish */
kono
parents:
diff changeset
3538 };
kono
parents:
diff changeset
3539
kono
parents:
diff changeset
3540 class pass_asan_O0 : public gimple_opt_pass
kono
parents:
diff changeset
3541 {
kono
parents:
diff changeset
3542 public:
kono
parents:
diff changeset
3543 pass_asan_O0 (gcc::context *ctxt)
kono
parents:
diff changeset
3544 : gimple_opt_pass (pass_data_asan_O0, ctxt)
kono
parents:
diff changeset
3545 {}
kono
parents:
diff changeset
3546
kono
parents:
diff changeset
3547 /* opt_pass methods: */
kono
parents:
diff changeset
3548 virtual bool gate (function *) { return !optimize && gate_asan (); }
kono
parents:
diff changeset
3549 virtual unsigned int execute (function *) { return asan_instrument (); }
kono
parents:
diff changeset
3550
kono
parents:
diff changeset
3551 }; // class pass_asan_O0
kono
parents:
diff changeset
3552
kono
parents:
diff changeset
3553 } // anon namespace
kono
parents:
diff changeset
3554
kono
parents:
diff changeset
3555 gimple_opt_pass *
kono
parents:
diff changeset
3556 make_pass_asan_O0 (gcc::context *ctxt)
kono
parents:
diff changeset
3557 {
kono
parents:
diff changeset
3558 return new pass_asan_O0 (ctxt);
kono
parents:
diff changeset
3559 }
kono
parents:
diff changeset
3560
kono
parents:
diff changeset
3561 #include "gt-asan.h"