]>
Commit | Line | Data |
---|---|---|
37d6f666 | 1 | /* AddressSanitizer, a fast memory error detector. |
d1e082c2 | 2 | Copyright (C) 2012-2013 Free Software Foundation, Inc. |
37d6f666 WM |
3 | Contributed by Kostya Serebryany <kcc@google.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
4d648807 | 25 | #include "tree.h" |
37d6f666 WM |
26 | #include "gimple.h" |
27 | #include "tree-iterator.h" | |
442b4905 AM |
28 | #include "cgraph.h" |
29 | #include "tree-ssanames.h" | |
37d6f666 | 30 | #include "tree-pass.h" |
37d6f666 WM |
31 | #include "asan.h" |
32 | #include "gimple-pretty-print.h" | |
dfe06d3e | 33 | #include "target.h" |
f3ddd692 JJ |
34 | #include "expr.h" |
35 | #include "optabs.h" | |
8240018b | 36 | #include "output.h" |
7f71fad9 | 37 | #include "tm_p.h" |
0e668eaf | 38 | #include "langhooks.h" |
bdcbe80c DS |
39 | #include "hash-table.h" |
40 | #include "alloc-pool.h" | |
a9e0d843 | 41 | #include "cfgloop.h" |
ff2a63a7 | 42 | #include "gimple-builder.h" |
37d6f666 | 43 | |
497a1c66 JJ |
44 | /* AddressSanitizer finds out-of-bounds and use-after-free bugs |
45 | with <2x slowdown on average. | |
46 | ||
47 | The tool consists of two parts: | |
48 | instrumentation module (this file) and a run-time library. | |
49 | The instrumentation module adds a run-time check before every memory insn. | |
50 | For a 8- or 16- byte load accessing address X: | |
51 | ShadowAddr = (X >> 3) + Offset | |
52 | ShadowValue = *(char*)ShadowAddr; // *(short*) for 16-byte access. | |
53 | if (ShadowValue) | |
54 | __asan_report_load8(X); | |
55 | For a load of N bytes (N=1, 2 or 4) from address X: | |
56 | ShadowAddr = (X >> 3) + Offset | |
57 | ShadowValue = *(char*)ShadowAddr; | |
58 | if (ShadowValue) | |
59 | if ((X & 7) + N - 1 > ShadowValue) | |
60 | __asan_report_loadN(X); | |
61 | Stores are instrumented similarly, but using __asan_report_storeN functions. | |
62 | A call too __asan_init() is inserted to the list of module CTORs. | |
63 | ||
64 | The run-time library redefines malloc (so that redzone are inserted around | |
65 | the allocated memory) and free (so that reuse of free-ed memory is delayed), | |
66 | provides __asan_report* and __asan_init functions. | |
67 | ||
68 | Read more: | |
69 | http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm | |
70 | ||
71 | The current implementation supports detection of out-of-bounds and | |
72 | use-after-free in the heap, on the stack and for global variables. | |
73 | ||
74 | [Protection of stack variables] | |
75 | ||
76 | To understand how detection of out-of-bounds and use-after-free works | |
77 | for stack variables, lets look at this example on x86_64 where the | |
78 | stack grows downward: | |
f3ddd692 JJ |
79 | |
80 | int | |
81 | foo () | |
82 | { | |
83 | char a[23] = {0}; | |
84 | int b[2] = {0}; | |
85 | ||
86 | a[5] = 1; | |
87 | b[1] = 2; | |
88 | ||
89 | return a[5] + b[1]; | |
90 | } | |
91 | ||
497a1c66 JJ |
92 | For this function, the stack protected by asan will be organized as |
93 | follows, from the top of the stack to the bottom: | |
f3ddd692 | 94 | |
497a1c66 | 95 | Slot 1/ [red zone of 32 bytes called 'RIGHT RedZone'] |
f3ddd692 | 96 | |
497a1c66 JJ |
97 | Slot 2/ [8 bytes of red zone, that adds up to the space of 'a' to make |
98 | the next slot be 32 bytes aligned; this one is called Partial | |
99 | Redzone; this 32 bytes alignment is an asan constraint] | |
f3ddd692 | 100 | |
497a1c66 | 101 | Slot 3/ [24 bytes for variable 'a'] |
f3ddd692 | 102 | |
497a1c66 | 103 | Slot 4/ [red zone of 32 bytes called 'Middle RedZone'] |
f3ddd692 | 104 | |
497a1c66 | 105 | Slot 5/ [24 bytes of Partial Red Zone (similar to slot 2] |
f3ddd692 | 106 | |
497a1c66 | 107 | Slot 6/ [8 bytes for variable 'b'] |
f3ddd692 | 108 | |
497a1c66 JJ |
109 | Slot 7/ [32 bytes of Red Zone at the bottom of the stack, called |
110 | 'LEFT RedZone'] | |
f3ddd692 | 111 | |
497a1c66 JJ |
112 | The 32 bytes of LEFT red zone at the bottom of the stack can be |
113 | decomposed as such: | |
f3ddd692 JJ |
114 | |
115 | 1/ The first 8 bytes contain a magical asan number that is always | |
116 | 0x41B58AB3. | |
117 | ||
118 | 2/ The following 8 bytes contains a pointer to a string (to be | |
119 | parsed at runtime by the runtime asan library), which format is | |
120 | the following: | |
121 | ||
122 | "<function-name> <space> <num-of-variables-on-the-stack> | |
123 | (<32-bytes-aligned-offset-in-bytes-of-variable> <space> | |
124 | <length-of-var-in-bytes> ){n} " | |
125 | ||
126 | where '(...){n}' means the content inside the parenthesis occurs 'n' | |
127 | times, with 'n' being the number of variables on the stack. | |
128 | ||
129 | 3/ The following 16 bytes of the red zone have no particular | |
130 | format. | |
131 | ||
497a1c66 | 132 | The shadow memory for that stack layout is going to look like this: |
f3ddd692 JJ |
133 | |
134 | - content of shadow memory 8 bytes for slot 7: 0xF1F1F1F1. | |
135 | The F1 byte pattern is a magic number called | |
136 | ASAN_STACK_MAGIC_LEFT and is a way for the runtime to know that | |
137 | the memory for that shadow byte is part of a the LEFT red zone | |
138 | intended to seat at the bottom of the variables on the stack. | |
139 | ||
140 | - content of shadow memory 8 bytes for slots 6 and 5: | |
141 | 0xF4F4F400. The F4 byte pattern is a magic number | |
142 | called ASAN_STACK_MAGIC_PARTIAL. It flags the fact that the | |
143 | memory region for this shadow byte is a PARTIAL red zone | |
144 | intended to pad a variable A, so that the slot following | |
145 | {A,padding} is 32 bytes aligned. | |
146 | ||
147 | Note that the fact that the least significant byte of this | |
148 | shadow memory content is 00 means that 8 bytes of its | |
149 | corresponding memory (which corresponds to the memory of | |
150 | variable 'b') is addressable. | |
151 | ||
152 | - content of shadow memory 8 bytes for slot 4: 0xF2F2F2F2. | |
153 | The F2 byte pattern is a magic number called | |
154 | ASAN_STACK_MAGIC_MIDDLE. It flags the fact that the memory | |
155 | region for this shadow byte is a MIDDLE red zone intended to | |
156 | seat between two 32 aligned slots of {variable,padding}. | |
157 | ||
158 | - content of shadow memory 8 bytes for slot 3 and 2: | |
497a1c66 | 159 | 0xF4000000. This represents is the concatenation of |
f3ddd692 JJ |
160 | variable 'a' and the partial red zone following it, like what we |
161 | had for variable 'b'. The least significant 3 bytes being 00 | |
162 | means that the 3 bytes of variable 'a' are addressable. | |
163 | ||
497a1c66 | 164 | - content of shadow memory 8 bytes for slot 1: 0xF3F3F3F3. |
f3ddd692 JJ |
165 | The F3 byte pattern is a magic number called |
166 | ASAN_STACK_MAGIC_RIGHT. It flags the fact that the memory | |
167 | region for this shadow byte is a RIGHT red zone intended to seat | |
168 | at the top of the variables of the stack. | |
169 | ||
497a1c66 JJ |
170 | Note that the real variable layout is done in expand_used_vars in |
171 | cfgexpand.c. As far as Address Sanitizer is concerned, it lays out | |
172 | stack variables as well as the different red zones, emits some | |
173 | prologue code to populate the shadow memory as to poison (mark as | |
174 | non-accessible) the regions of the red zones and mark the regions of | |
175 | stack variables as accessible, and emit some epilogue code to | |
176 | un-poison (mark as accessible) the regions of red zones right before | |
177 | the function exits. | |
8240018b | 178 | |
497a1c66 | 179 | [Protection of global variables] |
8240018b | 180 | |
497a1c66 JJ |
181 | The basic idea is to insert a red zone between two global variables |
182 | and install a constructor function that calls the asan runtime to do | |
183 | the populating of the relevant shadow memory regions at load time. | |
8240018b | 184 | |
497a1c66 JJ |
185 | So the global variables are laid out as to insert a red zone between |
186 | them. The size of the red zones is so that each variable starts on a | |
187 | 32 bytes boundary. | |
8240018b | 188 | |
497a1c66 JJ |
189 | Then a constructor function is installed so that, for each global |
190 | variable, it calls the runtime asan library function | |
191 | __asan_register_globals_with an instance of this type: | |
8240018b JJ |
192 | |
193 | struct __asan_global | |
194 | { | |
195 | // Address of the beginning of the global variable. | |
196 | const void *__beg; | |
197 | ||
198 | // Initial size of the global variable. | |
199 | uptr __size; | |
200 | ||
201 | // Size of the global variable + size of the red zone. This | |
202 | // size is 32 bytes aligned. | |
203 | uptr __size_with_redzone; | |
204 | ||
205 | // Name of the global variable. | |
206 | const void *__name; | |
207 | ||
208 | // This is always set to NULL for now. | |
209 | uptr __has_dynamic_init; | |
210 | } | |
211 | ||
497a1c66 JJ |
212 | A destructor function that calls the runtime asan library function |
213 | _asan_unregister_globals is also installed. */ | |
f3ddd692 JJ |
214 | |
215 | alias_set_type asan_shadow_set = -1; | |
37d6f666 | 216 | |
f6d98484 JJ |
217 | /* Pointer types to 1 resp. 2 byte integers in shadow memory. A separate |
218 | alias set is used for all shadow memory accesses. */ | |
219 | static GTY(()) tree shadow_ptr_types[2]; | |
220 | ||
bdcbe80c DS |
221 | /* Hashtable support for memory references used by gimple |
222 | statements. */ | |
223 | ||
224 | /* This type represents a reference to a memory region. */ | |
225 | struct asan_mem_ref | |
226 | { | |
688010ba | 227 | /* The expression of the beginning of the memory region. */ |
bdcbe80c DS |
228 | tree start; |
229 | ||
230 | /* The size of the access (can be 1, 2, 4, 8, 16 for now). */ | |
231 | char access_size; | |
232 | }; | |
233 | ||
234 | static alloc_pool asan_mem_ref_alloc_pool; | |
235 | ||
236 | /* This creates the alloc pool used to store the instances of | |
237 | asan_mem_ref that are stored in the hash table asan_mem_ref_ht. */ | |
238 | ||
239 | static alloc_pool | |
240 | asan_mem_ref_get_alloc_pool () | |
241 | { | |
242 | if (asan_mem_ref_alloc_pool == NULL) | |
243 | asan_mem_ref_alloc_pool = create_alloc_pool ("asan_mem_ref", | |
244 | sizeof (asan_mem_ref), | |
245 | 10); | |
246 | return asan_mem_ref_alloc_pool; | |
247 | ||
248 | } | |
249 | ||
250 | /* Initializes an instance of asan_mem_ref. */ | |
251 | ||
252 | static void | |
253 | asan_mem_ref_init (asan_mem_ref *ref, tree start, char access_size) | |
254 | { | |
255 | ref->start = start; | |
256 | ref->access_size = access_size; | |
257 | } | |
258 | ||
259 | /* Allocates memory for an instance of asan_mem_ref into the memory | |
260 | pool returned by asan_mem_ref_get_alloc_pool and initialize it. | |
261 | START is the address of (or the expression pointing to) the | |
262 | beginning of memory reference. ACCESS_SIZE is the size of the | |
263 | access to the referenced memory. */ | |
264 | ||
265 | static asan_mem_ref* | |
266 | asan_mem_ref_new (tree start, char access_size) | |
267 | { | |
268 | asan_mem_ref *ref = | |
269 | (asan_mem_ref *) pool_alloc (asan_mem_ref_get_alloc_pool ()); | |
270 | ||
271 | asan_mem_ref_init (ref, start, access_size); | |
272 | return ref; | |
273 | } | |
274 | ||
275 | /* This builds and returns a pointer to the end of the memory region | |
276 | that starts at START and of length LEN. */ | |
277 | ||
278 | tree | |
279 | asan_mem_ref_get_end (tree start, tree len) | |
280 | { | |
281 | if (len == NULL_TREE || integer_zerop (len)) | |
282 | return start; | |
283 | ||
284 | return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (start), start, len); | |
285 | } | |
286 | ||
287 | /* Return a tree expression that represents the end of the referenced | |
288 | memory region. Beware that this function can actually build a new | |
289 | tree expression. */ | |
290 | ||
291 | tree | |
292 | asan_mem_ref_get_end (const asan_mem_ref *ref, tree len) | |
293 | { | |
294 | return asan_mem_ref_get_end (ref->start, len); | |
295 | } | |
296 | ||
297 | struct asan_mem_ref_hasher | |
298 | : typed_noop_remove <asan_mem_ref> | |
299 | { | |
300 | typedef asan_mem_ref value_type; | |
301 | typedef asan_mem_ref compare_type; | |
302 | ||
303 | static inline hashval_t hash (const value_type *); | |
304 | static inline bool equal (const value_type *, const compare_type *); | |
305 | }; | |
306 | ||
307 | /* Hash a memory reference. */ | |
308 | ||
309 | inline hashval_t | |
310 | asan_mem_ref_hasher::hash (const asan_mem_ref *mem_ref) | |
311 | { | |
312 | hashval_t h = iterative_hash_expr (mem_ref->start, 0); | |
313 | h = iterative_hash_hashval_t (h, mem_ref->access_size); | |
314 | return h; | |
315 | } | |
316 | ||
317 | /* Compare two memory references. We accept the length of either | |
318 | memory references to be NULL_TREE. */ | |
319 | ||
320 | inline bool | |
321 | asan_mem_ref_hasher::equal (const asan_mem_ref *m1, | |
322 | const asan_mem_ref *m2) | |
323 | { | |
324 | return (m1->access_size == m2->access_size | |
325 | && operand_equal_p (m1->start, m2->start, 0)); | |
326 | } | |
327 | ||
328 | static hash_table <asan_mem_ref_hasher> asan_mem_ref_ht; | |
329 | ||
330 | /* Returns a reference to the hash table containing memory references. | |
331 | This function ensures that the hash table is created. Note that | |
332 | this hash table is updated by the function | |
333 | update_mem_ref_hash_table. */ | |
334 | ||
335 | static hash_table <asan_mem_ref_hasher> & | |
336 | get_mem_ref_hash_table () | |
337 | { | |
338 | if (!asan_mem_ref_ht.is_created ()) | |
339 | asan_mem_ref_ht.create (10); | |
340 | ||
341 | return asan_mem_ref_ht; | |
342 | } | |
343 | ||
344 | /* Clear all entries from the memory references hash table. */ | |
345 | ||
346 | static void | |
347 | empty_mem_ref_hash_table () | |
348 | { | |
349 | if (asan_mem_ref_ht.is_created ()) | |
350 | asan_mem_ref_ht.empty (); | |
351 | } | |
352 | ||
353 | /* Free the memory references hash table. */ | |
354 | ||
355 | static void | |
356 | free_mem_ref_resources () | |
357 | { | |
358 | if (asan_mem_ref_ht.is_created ()) | |
359 | asan_mem_ref_ht.dispose (); | |
360 | ||
361 | if (asan_mem_ref_alloc_pool) | |
362 | { | |
363 | free_alloc_pool (asan_mem_ref_alloc_pool); | |
364 | asan_mem_ref_alloc_pool = NULL; | |
365 | } | |
366 | } | |
367 | ||
368 | /* Return true iff the memory reference REF has been instrumented. */ | |
369 | ||
370 | static bool | |
371 | has_mem_ref_been_instrumented (tree ref, char access_size) | |
372 | { | |
373 | asan_mem_ref r; | |
374 | asan_mem_ref_init (&r, ref, access_size); | |
375 | ||
376 | return (get_mem_ref_hash_table ().find (&r) != NULL); | |
377 | } | |
378 | ||
379 | /* Return true iff the memory reference REF has been instrumented. */ | |
380 | ||
381 | static bool | |
382 | has_mem_ref_been_instrumented (const asan_mem_ref *ref) | |
383 | { | |
384 | return has_mem_ref_been_instrumented (ref->start, ref->access_size); | |
385 | } | |
386 | ||
387 | /* Return true iff access to memory region starting at REF and of | |
388 | length LEN has been instrumented. */ | |
389 | ||
390 | static bool | |
391 | has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len) | |
392 | { | |
393 | /* First let's see if the address of the beginning of REF has been | |
394 | instrumented. */ | |
395 | if (!has_mem_ref_been_instrumented (ref)) | |
396 | return false; | |
397 | ||
398 | if (len != 0) | |
399 | { | |
400 | /* Let's see if the end of the region has been instrumented. */ | |
401 | if (!has_mem_ref_been_instrumented (asan_mem_ref_get_end (ref, len), | |
402 | ref->access_size)) | |
403 | return false; | |
404 | } | |
405 | return true; | |
406 | } | |
407 | ||
408 | /* Set REF to the memory reference present in a gimple assignment | |
409 | ASSIGNMENT. Return true upon successful completion, false | |
410 | otherwise. */ | |
411 | ||
412 | static bool | |
413 | get_mem_ref_of_assignment (const gimple assignment, | |
414 | asan_mem_ref *ref, | |
415 | bool *ref_is_store) | |
416 | { | |
417 | gcc_assert (gimple_assign_single_p (assignment)); | |
418 | ||
5d751b0c JJ |
419 | if (gimple_store_p (assignment) |
420 | && !gimple_clobber_p (assignment)) | |
bdcbe80c DS |
421 | { |
422 | ref->start = gimple_assign_lhs (assignment); | |
423 | *ref_is_store = true; | |
424 | } | |
425 | else if (gimple_assign_load_p (assignment)) | |
426 | { | |
427 | ref->start = gimple_assign_rhs1 (assignment); | |
428 | *ref_is_store = false; | |
429 | } | |
430 | else | |
431 | return false; | |
432 | ||
433 | ref->access_size = int_size_in_bytes (TREE_TYPE (ref->start)); | |
434 | return true; | |
435 | } | |
436 | ||
437 | /* Return the memory references contained in a gimple statement | |
438 | representing a builtin call that has to do with memory access. */ | |
439 | ||
440 | static bool | |
441 | get_mem_refs_of_builtin_call (const gimple call, | |
442 | asan_mem_ref *src0, | |
443 | tree *src0_len, | |
444 | bool *src0_is_store, | |
445 | asan_mem_ref *src1, | |
446 | tree *src1_len, | |
447 | bool *src1_is_store, | |
448 | asan_mem_ref *dst, | |
449 | tree *dst_len, | |
450 | bool *dst_is_store, | |
451 | bool *dest_is_deref) | |
452 | { | |
453 | gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); | |
454 | ||
455 | tree callee = gimple_call_fndecl (call); | |
456 | tree source0 = NULL_TREE, source1 = NULL_TREE, | |
457 | dest = NULL_TREE, len = NULL_TREE; | |
458 | bool is_store = true, got_reference_p = false; | |
459 | char access_size = 1; | |
460 | ||
461 | switch (DECL_FUNCTION_CODE (callee)) | |
462 | { | |
463 | /* (s, s, n) style memops. */ | |
464 | case BUILT_IN_BCMP: | |
465 | case BUILT_IN_MEMCMP: | |
466 | source0 = gimple_call_arg (call, 0); | |
467 | source1 = gimple_call_arg (call, 1); | |
468 | len = gimple_call_arg (call, 2); | |
469 | break; | |
470 | ||
471 | /* (src, dest, n) style memops. */ | |
472 | case BUILT_IN_BCOPY: | |
473 | source0 = gimple_call_arg (call, 0); | |
474 | dest = gimple_call_arg (call, 1); | |
475 | len = gimple_call_arg (call, 2); | |
476 | break; | |
477 | ||
478 | /* (dest, src, n) style memops. */ | |
479 | case BUILT_IN_MEMCPY: | |
480 | case BUILT_IN_MEMCPY_CHK: | |
481 | case BUILT_IN_MEMMOVE: | |
482 | case BUILT_IN_MEMMOVE_CHK: | |
483 | case BUILT_IN_MEMPCPY: | |
484 | case BUILT_IN_MEMPCPY_CHK: | |
485 | dest = gimple_call_arg (call, 0); | |
486 | source0 = gimple_call_arg (call, 1); | |
487 | len = gimple_call_arg (call, 2); | |
488 | break; | |
489 | ||
490 | /* (dest, n) style memops. */ | |
491 | case BUILT_IN_BZERO: | |
492 | dest = gimple_call_arg (call, 0); | |
493 | len = gimple_call_arg (call, 1); | |
494 | break; | |
495 | ||
496 | /* (dest, x, n) style memops*/ | |
497 | case BUILT_IN_MEMSET: | |
498 | case BUILT_IN_MEMSET_CHK: | |
499 | dest = gimple_call_arg (call, 0); | |
500 | len = gimple_call_arg (call, 2); | |
501 | break; | |
502 | ||
503 | case BUILT_IN_STRLEN: | |
504 | source0 = gimple_call_arg (call, 0); | |
505 | len = gimple_call_lhs (call); | |
506 | break ; | |
507 | ||
508 | /* And now the __atomic* and __sync builtins. | |
509 | These are handled differently from the classical memory memory | |
510 | access builtins above. */ | |
511 | ||
512 | case BUILT_IN_ATOMIC_LOAD_1: | |
513 | case BUILT_IN_ATOMIC_LOAD_2: | |
514 | case BUILT_IN_ATOMIC_LOAD_4: | |
515 | case BUILT_IN_ATOMIC_LOAD_8: | |
516 | case BUILT_IN_ATOMIC_LOAD_16: | |
517 | is_store = false; | |
518 | /* fall through. */ | |
519 | ||
520 | case BUILT_IN_SYNC_FETCH_AND_ADD_1: | |
521 | case BUILT_IN_SYNC_FETCH_AND_ADD_2: | |
522 | case BUILT_IN_SYNC_FETCH_AND_ADD_4: | |
523 | case BUILT_IN_SYNC_FETCH_AND_ADD_8: | |
524 | case BUILT_IN_SYNC_FETCH_AND_ADD_16: | |
525 | ||
526 | case BUILT_IN_SYNC_FETCH_AND_SUB_1: | |
527 | case BUILT_IN_SYNC_FETCH_AND_SUB_2: | |
528 | case BUILT_IN_SYNC_FETCH_AND_SUB_4: | |
529 | case BUILT_IN_SYNC_FETCH_AND_SUB_8: | |
530 | case BUILT_IN_SYNC_FETCH_AND_SUB_16: | |
531 | ||
532 | case BUILT_IN_SYNC_FETCH_AND_OR_1: | |
533 | case BUILT_IN_SYNC_FETCH_AND_OR_2: | |
534 | case BUILT_IN_SYNC_FETCH_AND_OR_4: | |
535 | case BUILT_IN_SYNC_FETCH_AND_OR_8: | |
536 | case BUILT_IN_SYNC_FETCH_AND_OR_16: | |
537 | ||
538 | case BUILT_IN_SYNC_FETCH_AND_AND_1: | |
539 | case BUILT_IN_SYNC_FETCH_AND_AND_2: | |
540 | case BUILT_IN_SYNC_FETCH_AND_AND_4: | |
541 | case BUILT_IN_SYNC_FETCH_AND_AND_8: | |
542 | case BUILT_IN_SYNC_FETCH_AND_AND_16: | |
543 | ||
544 | case BUILT_IN_SYNC_FETCH_AND_XOR_1: | |
545 | case BUILT_IN_SYNC_FETCH_AND_XOR_2: | |
546 | case BUILT_IN_SYNC_FETCH_AND_XOR_4: | |
547 | case BUILT_IN_SYNC_FETCH_AND_XOR_8: | |
548 | case BUILT_IN_SYNC_FETCH_AND_XOR_16: | |
549 | ||
550 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: | |
551 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
552 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
553 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
554 | ||
555 | case BUILT_IN_SYNC_ADD_AND_FETCH_1: | |
556 | case BUILT_IN_SYNC_ADD_AND_FETCH_2: | |
557 | case BUILT_IN_SYNC_ADD_AND_FETCH_4: | |
558 | case BUILT_IN_SYNC_ADD_AND_FETCH_8: | |
559 | case BUILT_IN_SYNC_ADD_AND_FETCH_16: | |
560 | ||
561 | case BUILT_IN_SYNC_SUB_AND_FETCH_1: | |
562 | case BUILT_IN_SYNC_SUB_AND_FETCH_2: | |
563 | case BUILT_IN_SYNC_SUB_AND_FETCH_4: | |
564 | case BUILT_IN_SYNC_SUB_AND_FETCH_8: | |
565 | case BUILT_IN_SYNC_SUB_AND_FETCH_16: | |
566 | ||
567 | case BUILT_IN_SYNC_OR_AND_FETCH_1: | |
568 | case BUILT_IN_SYNC_OR_AND_FETCH_2: | |
569 | case BUILT_IN_SYNC_OR_AND_FETCH_4: | |
570 | case BUILT_IN_SYNC_OR_AND_FETCH_8: | |
571 | case BUILT_IN_SYNC_OR_AND_FETCH_16: | |
572 | ||
573 | case BUILT_IN_SYNC_AND_AND_FETCH_1: | |
574 | case BUILT_IN_SYNC_AND_AND_FETCH_2: | |
575 | case BUILT_IN_SYNC_AND_AND_FETCH_4: | |
576 | case BUILT_IN_SYNC_AND_AND_FETCH_8: | |
577 | case BUILT_IN_SYNC_AND_AND_FETCH_16: | |
578 | ||
579 | case BUILT_IN_SYNC_XOR_AND_FETCH_1: | |
580 | case BUILT_IN_SYNC_XOR_AND_FETCH_2: | |
581 | case BUILT_IN_SYNC_XOR_AND_FETCH_4: | |
582 | case BUILT_IN_SYNC_XOR_AND_FETCH_8: | |
583 | case BUILT_IN_SYNC_XOR_AND_FETCH_16: | |
584 | ||
585 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: | |
586 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
587 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
588 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
589 | ||
590 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: | |
591 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
592 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
593 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
594 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
595 | ||
596 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: | |
597 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: | |
598 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: | |
599 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: | |
600 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: | |
601 | ||
602 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: | |
603 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: | |
604 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: | |
605 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: | |
606 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: | |
607 | ||
608 | case BUILT_IN_SYNC_LOCK_RELEASE_1: | |
609 | case BUILT_IN_SYNC_LOCK_RELEASE_2: | |
610 | case BUILT_IN_SYNC_LOCK_RELEASE_4: | |
611 | case BUILT_IN_SYNC_LOCK_RELEASE_8: | |
612 | case BUILT_IN_SYNC_LOCK_RELEASE_16: | |
613 | ||
614 | case BUILT_IN_ATOMIC_EXCHANGE_1: | |
615 | case BUILT_IN_ATOMIC_EXCHANGE_2: | |
616 | case BUILT_IN_ATOMIC_EXCHANGE_4: | |
617 | case BUILT_IN_ATOMIC_EXCHANGE_8: | |
618 | case BUILT_IN_ATOMIC_EXCHANGE_16: | |
619 | ||
620 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
621 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
622 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
623 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
624 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
625 | ||
626 | case BUILT_IN_ATOMIC_STORE_1: | |
627 | case BUILT_IN_ATOMIC_STORE_2: | |
628 | case BUILT_IN_ATOMIC_STORE_4: | |
629 | case BUILT_IN_ATOMIC_STORE_8: | |
630 | case BUILT_IN_ATOMIC_STORE_16: | |
631 | ||
632 | case BUILT_IN_ATOMIC_ADD_FETCH_1: | |
633 | case BUILT_IN_ATOMIC_ADD_FETCH_2: | |
634 | case BUILT_IN_ATOMIC_ADD_FETCH_4: | |
635 | case BUILT_IN_ATOMIC_ADD_FETCH_8: | |
636 | case BUILT_IN_ATOMIC_ADD_FETCH_16: | |
637 | ||
638 | case BUILT_IN_ATOMIC_SUB_FETCH_1: | |
639 | case BUILT_IN_ATOMIC_SUB_FETCH_2: | |
640 | case BUILT_IN_ATOMIC_SUB_FETCH_4: | |
641 | case BUILT_IN_ATOMIC_SUB_FETCH_8: | |
642 | case BUILT_IN_ATOMIC_SUB_FETCH_16: | |
643 | ||
644 | case BUILT_IN_ATOMIC_AND_FETCH_1: | |
645 | case BUILT_IN_ATOMIC_AND_FETCH_2: | |
646 | case BUILT_IN_ATOMIC_AND_FETCH_4: | |
647 | case BUILT_IN_ATOMIC_AND_FETCH_8: | |
648 | case BUILT_IN_ATOMIC_AND_FETCH_16: | |
649 | ||
650 | case BUILT_IN_ATOMIC_NAND_FETCH_1: | |
651 | case BUILT_IN_ATOMIC_NAND_FETCH_2: | |
652 | case BUILT_IN_ATOMIC_NAND_FETCH_4: | |
653 | case BUILT_IN_ATOMIC_NAND_FETCH_8: | |
654 | case BUILT_IN_ATOMIC_NAND_FETCH_16: | |
655 | ||
656 | case BUILT_IN_ATOMIC_XOR_FETCH_1: | |
657 | case BUILT_IN_ATOMIC_XOR_FETCH_2: | |
658 | case BUILT_IN_ATOMIC_XOR_FETCH_4: | |
659 | case BUILT_IN_ATOMIC_XOR_FETCH_8: | |
660 | case BUILT_IN_ATOMIC_XOR_FETCH_16: | |
661 | ||
662 | case BUILT_IN_ATOMIC_OR_FETCH_1: | |
663 | case BUILT_IN_ATOMIC_OR_FETCH_2: | |
664 | case BUILT_IN_ATOMIC_OR_FETCH_4: | |
665 | case BUILT_IN_ATOMIC_OR_FETCH_8: | |
666 | case BUILT_IN_ATOMIC_OR_FETCH_16: | |
667 | ||
668 | case BUILT_IN_ATOMIC_FETCH_ADD_1: | |
669 | case BUILT_IN_ATOMIC_FETCH_ADD_2: | |
670 | case BUILT_IN_ATOMIC_FETCH_ADD_4: | |
671 | case BUILT_IN_ATOMIC_FETCH_ADD_8: | |
672 | case BUILT_IN_ATOMIC_FETCH_ADD_16: | |
673 | ||
674 | case BUILT_IN_ATOMIC_FETCH_SUB_1: | |
675 | case BUILT_IN_ATOMIC_FETCH_SUB_2: | |
676 | case BUILT_IN_ATOMIC_FETCH_SUB_4: | |
677 | case BUILT_IN_ATOMIC_FETCH_SUB_8: | |
678 | case BUILT_IN_ATOMIC_FETCH_SUB_16: | |
679 | ||
680 | case BUILT_IN_ATOMIC_FETCH_AND_1: | |
681 | case BUILT_IN_ATOMIC_FETCH_AND_2: | |
682 | case BUILT_IN_ATOMIC_FETCH_AND_4: | |
683 | case BUILT_IN_ATOMIC_FETCH_AND_8: | |
684 | case BUILT_IN_ATOMIC_FETCH_AND_16: | |
685 | ||
686 | case BUILT_IN_ATOMIC_FETCH_NAND_1: | |
687 | case BUILT_IN_ATOMIC_FETCH_NAND_2: | |
688 | case BUILT_IN_ATOMIC_FETCH_NAND_4: | |
689 | case BUILT_IN_ATOMIC_FETCH_NAND_8: | |
690 | case BUILT_IN_ATOMIC_FETCH_NAND_16: | |
691 | ||
692 | case BUILT_IN_ATOMIC_FETCH_XOR_1: | |
693 | case BUILT_IN_ATOMIC_FETCH_XOR_2: | |
694 | case BUILT_IN_ATOMIC_FETCH_XOR_4: | |
695 | case BUILT_IN_ATOMIC_FETCH_XOR_8: | |
696 | case BUILT_IN_ATOMIC_FETCH_XOR_16: | |
697 | ||
698 | case BUILT_IN_ATOMIC_FETCH_OR_1: | |
699 | case BUILT_IN_ATOMIC_FETCH_OR_2: | |
700 | case BUILT_IN_ATOMIC_FETCH_OR_4: | |
701 | case BUILT_IN_ATOMIC_FETCH_OR_8: | |
702 | case BUILT_IN_ATOMIC_FETCH_OR_16: | |
703 | { | |
704 | dest = gimple_call_arg (call, 0); | |
705 | /* DEST represents the address of a memory location. | |
706 | instrument_derefs wants the memory location, so lets | |
707 | dereference the address DEST before handing it to | |
708 | instrument_derefs. */ | |
709 | if (TREE_CODE (dest) == ADDR_EXPR) | |
710 | dest = TREE_OPERAND (dest, 0); | |
711 | else if (TREE_CODE (dest) == SSA_NAME) | |
712 | dest = build2 (MEM_REF, TREE_TYPE (TREE_TYPE (dest)), | |
713 | dest, build_int_cst (TREE_TYPE (dest), 0)); | |
714 | else | |
715 | gcc_unreachable (); | |
716 | ||
717 | access_size = int_size_in_bytes (TREE_TYPE (dest)); | |
718 | } | |
719 | ||
720 | default: | |
721 | /* The other builtins memory access are not instrumented in this | |
722 | function because they either don't have any length parameter, | |
723 | or their length parameter is just a limit. */ | |
724 | break; | |
725 | } | |
726 | ||
727 | if (len != NULL_TREE) | |
728 | { | |
729 | if (source0 != NULL_TREE) | |
730 | { | |
731 | src0->start = source0; | |
732 | src0->access_size = access_size; | |
733 | *src0_len = len; | |
734 | *src0_is_store = false; | |
735 | } | |
736 | ||
737 | if (source1 != NULL_TREE) | |
738 | { | |
739 | src1->start = source1; | |
740 | src1->access_size = access_size; | |
741 | *src1_len = len; | |
742 | *src1_is_store = false; | |
743 | } | |
744 | ||
745 | if (dest != NULL_TREE) | |
746 | { | |
747 | dst->start = dest; | |
748 | dst->access_size = access_size; | |
749 | *dst_len = len; | |
750 | *dst_is_store = true; | |
751 | } | |
752 | ||
753 | got_reference_p = true; | |
754 | } | |
b41288b3 JJ |
755 | else if (dest) |
756 | { | |
757 | dst->start = dest; | |
758 | dst->access_size = access_size; | |
759 | *dst_len = NULL_TREE; | |
760 | *dst_is_store = is_store; | |
761 | *dest_is_deref = true; | |
762 | got_reference_p = true; | |
763 | } | |
bdcbe80c | 764 | |
b41288b3 | 765 | return got_reference_p; |
bdcbe80c DS |
766 | } |
767 | ||
768 | /* Return true iff a given gimple statement has been instrumented. | |
769 | Note that the statement is "defined" by the memory references it | |
770 | contains. */ | |
771 | ||
772 | static bool | |
773 | has_stmt_been_instrumented_p (gimple stmt) | |
774 | { | |
775 | if (gimple_assign_single_p (stmt)) | |
776 | { | |
777 | bool r_is_store; | |
778 | asan_mem_ref r; | |
779 | asan_mem_ref_init (&r, NULL, 1); | |
780 | ||
781 | if (get_mem_ref_of_assignment (stmt, &r, &r_is_store)) | |
782 | return has_mem_ref_been_instrumented (&r); | |
783 | } | |
784 | else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) | |
785 | { | |
786 | asan_mem_ref src0, src1, dest; | |
787 | asan_mem_ref_init (&src0, NULL, 1); | |
788 | asan_mem_ref_init (&src1, NULL, 1); | |
789 | asan_mem_ref_init (&dest, NULL, 1); | |
790 | ||
791 | tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; | |
792 | bool src0_is_store = false, src1_is_store = false, | |
793 | dest_is_store = false, dest_is_deref = false; | |
794 | if (get_mem_refs_of_builtin_call (stmt, | |
795 | &src0, &src0_len, &src0_is_store, | |
796 | &src1, &src1_len, &src1_is_store, | |
797 | &dest, &dest_len, &dest_is_store, | |
798 | &dest_is_deref)) | |
799 | { | |
800 | if (src0.start != NULL_TREE | |
801 | && !has_mem_ref_been_instrumented (&src0, src0_len)) | |
802 | return false; | |
803 | ||
804 | if (src1.start != NULL_TREE | |
805 | && !has_mem_ref_been_instrumented (&src1, src1_len)) | |
806 | return false; | |
807 | ||
808 | if (dest.start != NULL_TREE | |
809 | && !has_mem_ref_been_instrumented (&dest, dest_len)) | |
810 | return false; | |
811 | ||
812 | return true; | |
813 | } | |
814 | } | |
815 | return false; | |
816 | } | |
817 | ||
818 | /* Insert a memory reference into the hash table. */ | |
819 | ||
820 | static void | |
821 | update_mem_ref_hash_table (tree ref, char access_size) | |
822 | { | |
823 | hash_table <asan_mem_ref_hasher> ht = get_mem_ref_hash_table (); | |
824 | ||
825 | asan_mem_ref r; | |
826 | asan_mem_ref_init (&r, ref, access_size); | |
827 | ||
828 | asan_mem_ref **slot = ht.find_slot (&r, INSERT); | |
829 | if (*slot == NULL) | |
830 | *slot = asan_mem_ref_new (ref, access_size); | |
831 | } | |
832 | ||
94fce891 JJ |
833 | /* Initialize shadow_ptr_types array. */ |
834 | ||
835 | static void | |
836 | asan_init_shadow_ptr_types (void) | |
837 | { | |
838 | asan_shadow_set = new_alias_set (); | |
839 | shadow_ptr_types[0] = build_distinct_type_copy (signed_char_type_node); | |
840 | TYPE_ALIAS_SET (shadow_ptr_types[0]) = asan_shadow_set; | |
841 | shadow_ptr_types[0] = build_pointer_type (shadow_ptr_types[0]); | |
842 | shadow_ptr_types[1] = build_distinct_type_copy (short_integer_type_node); | |
843 | TYPE_ALIAS_SET (shadow_ptr_types[1]) = asan_shadow_set; | |
844 | shadow_ptr_types[1] = build_pointer_type (shadow_ptr_types[1]); | |
845 | initialize_sanitizer_builtins (); | |
846 | } | |
847 | ||
11a877b3 | 848 | /* Create ADDR_EXPR of STRING_CST with the PP pretty printer text. */ |
8240018b JJ |
849 | |
850 | static tree | |
11a877b3 | 851 | asan_pp_string (pretty_printer *pp) |
8240018b | 852 | { |
11a877b3 | 853 | const char *buf = pp_formatted_text (pp); |
8240018b JJ |
854 | size_t len = strlen (buf); |
855 | tree ret = build_string (len + 1, buf); | |
856 | TREE_TYPE (ret) | |
94fce891 JJ |
857 | = build_array_type (TREE_TYPE (shadow_ptr_types[0]), |
858 | build_index_type (size_int (len))); | |
8240018b JJ |
859 | TREE_READONLY (ret) = 1; |
860 | TREE_STATIC (ret) = 1; | |
94fce891 | 861 | return build1 (ADDR_EXPR, shadow_ptr_types[0], ret); |
8240018b JJ |
862 | } |
863 | ||
f3ddd692 JJ |
864 | /* Return a CONST_INT representing 4 subsequent shadow memory bytes. */ |
865 | ||
866 | static rtx | |
867 | asan_shadow_cst (unsigned char shadow_bytes[4]) | |
868 | { | |
869 | int i; | |
870 | unsigned HOST_WIDE_INT val = 0; | |
871 | gcc_assert (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN); | |
872 | for (i = 0; i < 4; i++) | |
873 | val |= (unsigned HOST_WIDE_INT) shadow_bytes[BYTES_BIG_ENDIAN ? 3 - i : i] | |
874 | << (BITS_PER_UNIT * i); | |
dcad1dd3 | 875 | return gen_int_mode (val, SImode); |
f3ddd692 JJ |
876 | } |
877 | ||
aeb7e7c1 JJ |
878 | /* Clear shadow memory at SHADOW_MEM, LEN bytes. Can't call a library call here |
879 | though. */ | |
880 | ||
881 | static void | |
882 | asan_clear_shadow (rtx shadow_mem, HOST_WIDE_INT len) | |
883 | { | |
884 | rtx insn, insns, top_label, end, addr, tmp, jump; | |
885 | ||
886 | start_sequence (); | |
887 | clear_storage (shadow_mem, GEN_INT (len), BLOCK_OP_NORMAL); | |
888 | insns = get_insns (); | |
889 | end_sequence (); | |
890 | for (insn = insns; insn; insn = NEXT_INSN (insn)) | |
891 | if (CALL_P (insn)) | |
892 | break; | |
893 | if (insn == NULL_RTX) | |
894 | { | |
895 | emit_insn (insns); | |
896 | return; | |
897 | } | |
898 | ||
899 | gcc_assert ((len & 3) == 0); | |
900 | top_label = gen_label_rtx (); | |
57d4d653 | 901 | addr = copy_to_mode_reg (Pmode, XEXP (shadow_mem, 0)); |
aeb7e7c1 JJ |
902 | shadow_mem = adjust_automodify_address (shadow_mem, SImode, addr, 0); |
903 | end = force_reg (Pmode, plus_constant (Pmode, addr, len)); | |
904 | emit_label (top_label); | |
905 | ||
906 | emit_move_insn (shadow_mem, const0_rtx); | |
2f1cd2eb | 907 | tmp = expand_simple_binop (Pmode, PLUS, addr, gen_int_mode (4, Pmode), addr, |
aeb7e7c1 JJ |
908 | true, OPTAB_LIB_WIDEN); |
909 | if (tmp != addr) | |
910 | emit_move_insn (addr, tmp); | |
911 | emit_cmp_and_jump_insns (addr, end, LT, NULL_RTX, Pmode, true, top_label); | |
912 | jump = get_last_insn (); | |
913 | gcc_assert (JUMP_P (jump)); | |
e5af9ddd | 914 | add_int_reg_note (jump, REG_BR_PROB, REG_BR_PROB_BASE * 80 / 100); |
aeb7e7c1 JJ |
915 | } |
916 | ||
f3ddd692 JJ |
917 | /* Insert code to protect stack vars. The prologue sequence should be emitted |
918 | directly, epilogue sequence returned. BASE is the register holding the | |
919 | stack base, against which OFFSETS array offsets are relative to, OFFSETS | |
920 | array contains pairs of offsets in reverse order, always the end offset | |
921 | of some gap that needs protection followed by starting offset, | |
922 | and DECLS is an array of representative decls for each var partition. | |
923 | LENGTH is the length of the OFFSETS array, DECLS array is LENGTH / 2 - 1 | |
924 | elements long (OFFSETS include gap before the first variable as well | |
925 | as gaps after each stack variable). */ | |
926 | ||
927 | rtx | |
928 | asan_emit_stack_protection (rtx base, HOST_WIDE_INT *offsets, tree *decls, | |
929 | int length) | |
930 | { | |
931 | rtx shadow_base, shadow_mem, ret, mem; | |
932 | unsigned char shadow_bytes[4]; | |
933 | HOST_WIDE_INT base_offset = offsets[length - 1], offset, prev_offset; | |
934 | HOST_WIDE_INT last_offset, last_size; | |
935 | int l; | |
936 | unsigned char cur_shadow_byte = ASAN_STACK_MAGIC_LEFT; | |
f3ddd692 JJ |
937 | tree str_cst; |
938 | ||
94fce891 JJ |
939 | if (shadow_ptr_types[0] == NULL_TREE) |
940 | asan_init_shadow_ptr_types (); | |
941 | ||
f3ddd692 | 942 | /* First of all, prepare the description string. */ |
11a877b3 | 943 | pretty_printer asan_pp; |
da6ca2b5 | 944 | |
f3ddd692 | 945 | if (DECL_NAME (current_function_decl)) |
b066401f | 946 | pp_tree_identifier (&asan_pp, DECL_NAME (current_function_decl)); |
f3ddd692 | 947 | else |
8240018b JJ |
948 | pp_string (&asan_pp, "<unknown>"); |
949 | pp_space (&asan_pp); | |
950 | pp_decimal_int (&asan_pp, length / 2 - 1); | |
951 | pp_space (&asan_pp); | |
f3ddd692 JJ |
952 | for (l = length - 2; l; l -= 2) |
953 | { | |
954 | tree decl = decls[l / 2 - 1]; | |
8240018b JJ |
955 | pp_wide_integer (&asan_pp, offsets[l] - base_offset); |
956 | pp_space (&asan_pp); | |
957 | pp_wide_integer (&asan_pp, offsets[l - 1] - offsets[l]); | |
958 | pp_space (&asan_pp); | |
f3ddd692 JJ |
959 | if (DECL_P (decl) && DECL_NAME (decl)) |
960 | { | |
8240018b JJ |
961 | pp_decimal_int (&asan_pp, IDENTIFIER_LENGTH (DECL_NAME (decl))); |
962 | pp_space (&asan_pp); | |
b066401f | 963 | pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
f3ddd692 JJ |
964 | } |
965 | else | |
8240018b JJ |
966 | pp_string (&asan_pp, "9 <unknown>"); |
967 | pp_space (&asan_pp); | |
f3ddd692 | 968 | } |
11a877b3 | 969 | str_cst = asan_pp_string (&asan_pp); |
f3ddd692 JJ |
970 | |
971 | /* Emit the prologue sequence. */ | |
2f1cd2eb RS |
972 | base = expand_binop (Pmode, add_optab, base, |
973 | gen_int_mode (base_offset, Pmode), | |
f3ddd692 JJ |
974 | NULL_RTX, 1, OPTAB_DIRECT); |
975 | mem = gen_rtx_MEM (ptr_mode, base); | |
69db2d57 | 976 | emit_move_insn (mem, gen_int_mode (ASAN_STACK_FRAME_MAGIC, ptr_mode)); |
f3ddd692 JJ |
977 | mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode)); |
978 | emit_move_insn (mem, expand_normal (str_cst)); | |
979 | shadow_base = expand_binop (Pmode, lshr_optab, base, | |
980 | GEN_INT (ASAN_SHADOW_SHIFT), | |
981 | NULL_RTX, 1, OPTAB_DIRECT); | |
982 | shadow_base = expand_binop (Pmode, add_optab, shadow_base, | |
2f1cd2eb RS |
983 | gen_int_mode (targetm.asan_shadow_offset (), |
984 | Pmode), | |
f3ddd692 JJ |
985 | NULL_RTX, 1, OPTAB_DIRECT); |
986 | gcc_assert (asan_shadow_set != -1 | |
987 | && (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4); | |
988 | shadow_mem = gen_rtx_MEM (SImode, shadow_base); | |
989 | set_mem_alias_set (shadow_mem, asan_shadow_set); | |
990 | prev_offset = base_offset; | |
991 | for (l = length; l; l -= 2) | |
992 | { | |
993 | if (l == 2) | |
994 | cur_shadow_byte = ASAN_STACK_MAGIC_RIGHT; | |
995 | offset = offsets[l - 1]; | |
996 | if ((offset - base_offset) & (ASAN_RED_ZONE_SIZE - 1)) | |
997 | { | |
998 | int i; | |
999 | HOST_WIDE_INT aoff | |
1000 | = base_offset + ((offset - base_offset) | |
1001 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); | |
1002 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1003 | (aoff - prev_offset) | |
1004 | >> ASAN_SHADOW_SHIFT); | |
1005 | prev_offset = aoff; | |
1006 | for (i = 0; i < 4; i++, aoff += (1 << ASAN_SHADOW_SHIFT)) | |
1007 | if (aoff < offset) | |
1008 | { | |
1009 | if (aoff < offset - (1 << ASAN_SHADOW_SHIFT) + 1) | |
1010 | shadow_bytes[i] = 0; | |
1011 | else | |
1012 | shadow_bytes[i] = offset - aoff; | |
1013 | } | |
1014 | else | |
1015 | shadow_bytes[i] = ASAN_STACK_MAGIC_PARTIAL; | |
1016 | emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes)); | |
1017 | offset = aoff; | |
1018 | } | |
1019 | while (offset <= offsets[l - 2] - ASAN_RED_ZONE_SIZE) | |
1020 | { | |
1021 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1022 | (offset - prev_offset) | |
1023 | >> ASAN_SHADOW_SHIFT); | |
1024 | prev_offset = offset; | |
1025 | memset (shadow_bytes, cur_shadow_byte, 4); | |
1026 | emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes)); | |
1027 | offset += ASAN_RED_ZONE_SIZE; | |
1028 | } | |
1029 | cur_shadow_byte = ASAN_STACK_MAGIC_MIDDLE; | |
1030 | } | |
1031 | do_pending_stack_adjust (); | |
1032 | ||
1033 | /* Construct epilogue sequence. */ | |
1034 | start_sequence (); | |
1035 | ||
1036 | shadow_mem = gen_rtx_MEM (BLKmode, shadow_base); | |
1037 | set_mem_alias_set (shadow_mem, asan_shadow_set); | |
1038 | prev_offset = base_offset; | |
1039 | last_offset = base_offset; | |
1040 | last_size = 0; | |
1041 | for (l = length; l; l -= 2) | |
1042 | { | |
1043 | offset = base_offset + ((offsets[l - 1] - base_offset) | |
1044 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); | |
1045 | if (last_offset + last_size != offset) | |
1046 | { | |
1047 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1048 | (last_offset - prev_offset) | |
1049 | >> ASAN_SHADOW_SHIFT); | |
1050 | prev_offset = last_offset; | |
aeb7e7c1 | 1051 | asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT); |
f3ddd692 JJ |
1052 | last_offset = offset; |
1053 | last_size = 0; | |
1054 | } | |
1055 | last_size += base_offset + ((offsets[l - 2] - base_offset) | |
1056 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)) | |
1057 | - offset; | |
1058 | } | |
1059 | if (last_size) | |
1060 | { | |
1061 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1062 | (last_offset - prev_offset) | |
1063 | >> ASAN_SHADOW_SHIFT); | |
aeb7e7c1 | 1064 | asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT); |
f3ddd692 JJ |
1065 | } |
1066 | ||
1067 | do_pending_stack_adjust (); | |
1068 | ||
1069 | ret = get_insns (); | |
1070 | end_sequence (); | |
1071 | return ret; | |
1072 | } | |
1073 | ||
8240018b JJ |
1074 | /* Return true if DECL, a global var, might be overridden and needs |
1075 | therefore a local alias. */ | |
1076 | ||
1077 | static bool | |
1078 | asan_needs_local_alias (tree decl) | |
1079 | { | |
1080 | return DECL_WEAK (decl) || !targetm.binds_local_p (decl); | |
1081 | } | |
1082 | ||
1083 | /* Return true if DECL is a VAR_DECL that should be protected | |
1084 | by Address Sanitizer, by appending a red zone with protected | |
1085 | shadow memory after it and aligning it to at least | |
1086 | ASAN_RED_ZONE_SIZE bytes. */ | |
1087 | ||
1088 | bool | |
1089 | asan_protect_global (tree decl) | |
1090 | { | |
1091 | rtx rtl, symbol; | |
8240018b | 1092 | |
94fce891 JJ |
1093 | if (TREE_CODE (decl) == STRING_CST) |
1094 | { | |
1095 | /* Instrument all STRING_CSTs except those created | |
1096 | by asan_pp_string here. */ | |
1097 | if (shadow_ptr_types[0] != NULL_TREE | |
1098 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
1099 | && TREE_TYPE (TREE_TYPE (decl)) == TREE_TYPE (shadow_ptr_types[0])) | |
1100 | return false; | |
1101 | return true; | |
1102 | } | |
8240018b JJ |
1103 | if (TREE_CODE (decl) != VAR_DECL |
1104 | /* TLS vars aren't statically protectable. */ | |
1105 | || DECL_THREAD_LOCAL_P (decl) | |
1106 | /* Externs will be protected elsewhere. */ | |
1107 | || DECL_EXTERNAL (decl) | |
8240018b JJ |
1108 | || !DECL_RTL_SET_P (decl) |
1109 | /* Comdat vars pose an ABI problem, we can't know if | |
1110 | the var that is selected by the linker will have | |
1111 | padding or not. */ | |
1112 | || DECL_ONE_ONLY (decl) | |
1113 | /* Similarly for common vars. People can use -fno-common. */ | |
a8a6fd74 | 1114 | || (DECL_COMMON (decl) && TREE_PUBLIC (decl)) |
8240018b JJ |
1115 | /* Don't protect if using user section, often vars placed |
1116 | into user section from multiple TUs are then assumed | |
1117 | to be an array of such vars, putting padding in there | |
1118 | breaks this assumption. */ | |
1119 | || (DECL_SECTION_NAME (decl) != NULL_TREE | |
1120 | && !DECL_HAS_IMPLICIT_SECTION_NAME_P (decl)) | |
1121 | || DECL_SIZE (decl) == 0 | |
1122 | || ASAN_RED_ZONE_SIZE * BITS_PER_UNIT > MAX_OFILE_ALIGNMENT | |
1123 | || !valid_constant_size_p (DECL_SIZE_UNIT (decl)) | |
1124 | || DECL_ALIGN_UNIT (decl) > 2 * ASAN_RED_ZONE_SIZE) | |
1125 | return false; | |
1126 | ||
1127 | rtl = DECL_RTL (decl); | |
1128 | if (!MEM_P (rtl) || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF) | |
1129 | return false; | |
1130 | symbol = XEXP (rtl, 0); | |
1131 | ||
1132 | if (CONSTANT_POOL_ADDRESS_P (symbol) | |
1133 | || TREE_CONSTANT_POOL_ADDRESS_P (symbol)) | |
1134 | return false; | |
1135 | ||
8240018b JJ |
1136 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
1137 | return false; | |
1138 | ||
1139 | #ifndef ASM_OUTPUT_DEF | |
1140 | if (asan_needs_local_alias (decl)) | |
1141 | return false; | |
1142 | #endif | |
1143 | ||
497a1c66 | 1144 | return true; |
8240018b JJ |
1145 | } |
1146 | ||
37d6f666 WM |
1147 | /* Construct a function tree for __asan_report_{load,store}{1,2,4,8,16}. |
1148 | IS_STORE is either 1 (for a store) or 0 (for a load). | |
1149 | SIZE_IN_BYTES is one of 1, 2, 4, 8, 16. */ | |
1150 | ||
1151 | static tree | |
f6d98484 | 1152 | report_error_func (bool is_store, int size_in_bytes) |
37d6f666 | 1153 | { |
0e668eaf JJ |
1154 | static enum built_in_function report[2][5] |
1155 | = { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2, | |
1156 | BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8, | |
1157 | BUILT_IN_ASAN_REPORT_LOAD16 }, | |
1158 | { BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2, | |
1159 | BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8, | |
1160 | BUILT_IN_ASAN_REPORT_STORE16 } }; | |
1161 | return builtin_decl_implicit (report[is_store][exact_log2 (size_in_bytes)]); | |
37d6f666 WM |
1162 | } |
1163 | ||
f6d98484 JJ |
1164 | #define PROB_VERY_UNLIKELY (REG_BR_PROB_BASE / 2000 - 1) |
1165 | #define PROB_ALWAYS (REG_BR_PROB_BASE) | |
1166 | ||
01452015 | 1167 | /* Split the current basic block and create a condition statement |
25ae5027 DS |
1168 | insertion point right before or after the statement pointed to by |
1169 | ITER. Return an iterator to the point at which the caller might | |
1170 | safely insert the condition statement. | |
01452015 DS |
1171 | |
1172 | THEN_BLOCK must be set to the address of an uninitialized instance | |
1173 | of basic_block. The function will then set *THEN_BLOCK to the | |
1174 | 'then block' of the condition statement to be inserted by the | |
1175 | caller. | |
1176 | ||
c4bfe8bf JJ |
1177 | If CREATE_THEN_FALLTHRU_EDGE is false, no edge will be created from |
1178 | *THEN_BLOCK to *FALLTHROUGH_BLOCK. | |
1179 | ||
01452015 DS |
1180 | Similarly, the function will set *FALLTRHOUGH_BLOCK to the 'else |
1181 | block' of the condition statement to be inserted by the caller. | |
1182 | ||
1183 | Note that *FALLTHROUGH_BLOCK is a new block that contains the | |
1184 | statements starting from *ITER, and *THEN_BLOCK is a new empty | |
1185 | block. | |
1186 | ||
25ae5027 DS |
1187 | *ITER is adjusted to point to always point to the first statement |
1188 | of the basic block * FALLTHROUGH_BLOCK. That statement is the | |
1189 | same as what ITER was pointing to prior to calling this function, | |
1190 | if BEFORE_P is true; otherwise, it is its following statement. */ | |
01452015 DS |
1191 | |
1192 | static gimple_stmt_iterator | |
25ae5027 DS |
1193 | create_cond_insert_point (gimple_stmt_iterator *iter, |
1194 | bool before_p, | |
1195 | bool then_more_likely_p, | |
c4bfe8bf | 1196 | bool create_then_fallthru_edge, |
25ae5027 DS |
1197 | basic_block *then_block, |
1198 | basic_block *fallthrough_block) | |
01452015 DS |
1199 | { |
1200 | gimple_stmt_iterator gsi = *iter; | |
1201 | ||
25ae5027 | 1202 | if (!gsi_end_p (gsi) && before_p) |
01452015 DS |
1203 | gsi_prev (&gsi); |
1204 | ||
1205 | basic_block cur_bb = gsi_bb (*iter); | |
1206 | ||
1207 | edge e = split_block (cur_bb, gsi_stmt (gsi)); | |
1208 | ||
1209 | /* Get a hold on the 'condition block', the 'then block' and the | |
1210 | 'else block'. */ | |
1211 | basic_block cond_bb = e->src; | |
1212 | basic_block fallthru_bb = e->dest; | |
1213 | basic_block then_bb = create_empty_bb (cond_bb); | |
a9e0d843 RB |
1214 | if (current_loops) |
1215 | { | |
1216 | add_bb_to_loop (then_bb, cond_bb->loop_father); | |
1217 | loops_state_set (LOOPS_NEED_FIXUP); | |
1218 | } | |
01452015 DS |
1219 | |
1220 | /* Set up the newly created 'then block'. */ | |
1221 | e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE); | |
1222 | int fallthrough_probability | |
1223 | = then_more_likely_p | |
1224 | ? PROB_VERY_UNLIKELY | |
1225 | : PROB_ALWAYS - PROB_VERY_UNLIKELY; | |
1226 | e->probability = PROB_ALWAYS - fallthrough_probability; | |
c4bfe8bf JJ |
1227 | if (create_then_fallthru_edge) |
1228 | make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); | |
01452015 DS |
1229 | |
1230 | /* Set up the fallthrough basic block. */ | |
1231 | e = find_edge (cond_bb, fallthru_bb); | |
1232 | e->flags = EDGE_FALSE_VALUE; | |
1233 | e->count = cond_bb->count; | |
1234 | e->probability = fallthrough_probability; | |
1235 | ||
1236 | /* Update dominance info for the newly created then_bb; note that | |
1237 | fallthru_bb's dominance info has already been updated by | |
1238 | split_bock. */ | |
1239 | if (dom_info_available_p (CDI_DOMINATORS)) | |
1240 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); | |
1241 | ||
1242 | *then_block = then_bb; | |
1243 | *fallthrough_block = fallthru_bb; | |
1244 | *iter = gsi_start_bb (fallthru_bb); | |
1245 | ||
1246 | return gsi_last_bb (cond_bb); | |
1247 | } | |
1248 | ||
25ae5027 DS |
1249 | /* Insert an if condition followed by a 'then block' right before the |
1250 | statement pointed to by ITER. The fallthrough block -- which is the | |
1251 | else block of the condition as well as the destination of the | |
1252 | outcoming edge of the 'then block' -- starts with the statement | |
1253 | pointed to by ITER. | |
1254 | ||
497a1c66 | 1255 | COND is the condition of the if. |
25ae5027 DS |
1256 | |
1257 | If THEN_MORE_LIKELY_P is true, the probability of the edge to the | |
1258 | 'then block' is higher than the probability of the edge to the | |
1259 | fallthrough block. | |
1260 | ||
1261 | Upon completion of the function, *THEN_BB is set to the newly | |
1262 | inserted 'then block' and similarly, *FALLTHROUGH_BB is set to the | |
1263 | fallthrough block. | |
1264 | ||
1265 | *ITER is adjusted to still point to the same statement it was | |
1266 | pointing to initially. */ | |
1267 | ||
1268 | static void | |
1269 | insert_if_then_before_iter (gimple cond, | |
1270 | gimple_stmt_iterator *iter, | |
1271 | bool then_more_likely_p, | |
1272 | basic_block *then_bb, | |
1273 | basic_block *fallthrough_bb) | |
1274 | { | |
1275 | gimple_stmt_iterator cond_insert_point = | |
1276 | create_cond_insert_point (iter, | |
1277 | /*before_p=*/true, | |
1278 | then_more_likely_p, | |
c4bfe8bf | 1279 | /*create_then_fallthru_edge=*/true, |
25ae5027 DS |
1280 | then_bb, |
1281 | fallthrough_bb); | |
1282 | gsi_insert_after (&cond_insert_point, cond, GSI_NEW_STMT); | |
1283 | } | |
1284 | ||
dc29bf1e | 1285 | /* Instrument the memory access instruction BASE. Insert new |
25ae5027 | 1286 | statements before or after ITER. |
dc29bf1e DS |
1287 | |
1288 | Note that the memory access represented by BASE can be either an | |
1289 | SSA_NAME, or a non-SSA expression. LOCATION is the source code | |
1290 | location. IS_STORE is TRUE for a store, FALSE for a load. | |
25ae5027 DS |
1291 | BEFORE_P is TRUE for inserting the instrumentation code before |
1292 | ITER, FALSE for inserting it after ITER. SIZE_IN_BYTES is one of | |
1293 | 1, 2, 4, 8, 16. | |
1294 | ||
1295 | If BEFORE_P is TRUE, *ITER is arranged to still point to the | |
1296 | statement it was pointing to prior to calling this function, | |
1297 | otherwise, it points to the statement logically following it. */ | |
37d6f666 WM |
1298 | |
1299 | static void | |
25ae5027 DS |
1300 | build_check_stmt (location_t location, tree base, gimple_stmt_iterator *iter, |
1301 | bool before_p, bool is_store, int size_in_bytes) | |
37d6f666 WM |
1302 | { |
1303 | gimple_stmt_iterator gsi; | |
01452015 | 1304 | basic_block then_bb, else_bb; |
f6d98484 | 1305 | tree t, base_addr, shadow; |
37d6f666 | 1306 | gimple g; |
f6d98484 JJ |
1307 | tree shadow_ptr_type = shadow_ptr_types[size_in_bytes == 16 ? 1 : 0]; |
1308 | tree shadow_type = TREE_TYPE (shadow_ptr_type); | |
1309 | tree uintptr_type | |
1310 | = build_nonstandard_integer_type (TYPE_PRECISION (TREE_TYPE (base)), 1); | |
dc29bf1e | 1311 | tree base_ssa = base; |
37d6f666 | 1312 | |
01452015 DS |
1313 | /* Get an iterator on the point where we can add the condition |
1314 | statement for the instrumentation. */ | |
25ae5027 DS |
1315 | gsi = create_cond_insert_point (iter, before_p, |
1316 | /*then_more_likely_p=*/false, | |
c4bfe8bf | 1317 | /*create_then_fallthru_edge=*/false, |
25ae5027 DS |
1318 | &then_bb, |
1319 | &else_bb); | |
37d6f666 | 1320 | |
f6d98484 | 1321 | base = unshare_expr (base); |
37d6f666 | 1322 | |
dc29bf1e DS |
1323 | /* BASE can already be an SSA_NAME; in that case, do not create a |
1324 | new SSA_NAME for it. */ | |
1325 | if (TREE_CODE (base) != SSA_NAME) | |
1326 | { | |
1327 | g = gimple_build_assign_with_ops (TREE_CODE (base), | |
1328 | make_ssa_name (TREE_TYPE (base), NULL), | |
1329 | base, NULL_TREE); | |
1330 | gimple_set_location (g, location); | |
1331 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
1332 | base_ssa = gimple_assign_lhs (g); | |
1333 | } | |
37d6f666 | 1334 | |
f6d98484 JJ |
1335 | g = gimple_build_assign_with_ops (NOP_EXPR, |
1336 | make_ssa_name (uintptr_type, NULL), | |
dc29bf1e | 1337 | base_ssa, NULL_TREE); |
37d6f666 | 1338 | gimple_set_location (g, location); |
f6d98484 JJ |
1339 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
1340 | base_addr = gimple_assign_lhs (g); | |
37d6f666 | 1341 | |
f6d98484 JJ |
1342 | /* Build |
1343 | (base_addr >> ASAN_SHADOW_SHIFT) + targetm.asan_shadow_offset (). */ | |
37d6f666 | 1344 | |
f6d98484 JJ |
1345 | t = build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT); |
1346 | g = gimple_build_assign_with_ops (RSHIFT_EXPR, | |
1347 | make_ssa_name (uintptr_type, NULL), | |
1348 | base_addr, t); | |
37d6f666 | 1349 | gimple_set_location (g, location); |
f6d98484 JJ |
1350 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
1351 | ||
1352 | t = build_int_cst (uintptr_type, targetm.asan_shadow_offset ()); | |
1353 | g = gimple_build_assign_with_ops (PLUS_EXPR, | |
1354 | make_ssa_name (uintptr_type, NULL), | |
1355 | gimple_assign_lhs (g), t); | |
37d6f666 | 1356 | gimple_set_location (g, location); |
f6d98484 | 1357 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
37d6f666 | 1358 | |
f6d98484 JJ |
1359 | g = gimple_build_assign_with_ops (NOP_EXPR, |
1360 | make_ssa_name (shadow_ptr_type, NULL), | |
1361 | gimple_assign_lhs (g), NULL_TREE); | |
1362 | gimple_set_location (g, location); | |
1363 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
37d6f666 | 1364 | |
f6d98484 JJ |
1365 | t = build2 (MEM_REF, shadow_type, gimple_assign_lhs (g), |
1366 | build_int_cst (shadow_ptr_type, 0)); | |
1367 | g = gimple_build_assign_with_ops (MEM_REF, | |
1368 | make_ssa_name (shadow_type, NULL), | |
1369 | t, NULL_TREE); | |
1370 | gimple_set_location (g, location); | |
1371 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
1372 | shadow = gimple_assign_lhs (g); | |
1373 | ||
1374 | if (size_in_bytes < 8) | |
1375 | { | |
1376 | /* Slow path for 1, 2 and 4 byte accesses. | |
1377 | Test (shadow != 0) | |
1378 | & ((base_addr & 7) + (size_in_bytes - 1)) >= shadow). */ | |
475b8f37 DN |
1379 | gimple_seq seq = NULL; |
1380 | gimple shadow_test = build_assign (NE_EXPR, shadow, 0); | |
1381 | gimple_seq_add_stmt (&seq, shadow_test); | |
1382 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, base_addr, 7)); | |
1383 | gimple_seq_add_stmt (&seq, build_type_cast (shadow_type, | |
1384 | gimple_seq_last (seq))); | |
f6d98484 | 1385 | if (size_in_bytes > 1) |
475b8f37 DN |
1386 | gimple_seq_add_stmt (&seq, |
1387 | build_assign (PLUS_EXPR, gimple_seq_last (seq), | |
1388 | size_in_bytes - 1)); | |
1389 | gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, gimple_seq_last (seq), | |
1390 | shadow)); | |
1391 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, | |
1392 | gimple_seq_last (seq))); | |
1393 | t = gimple_assign_lhs (gimple_seq_last (seq)); | |
1394 | gimple_seq_set_location (seq, location); | |
1395 | gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); | |
f6d98484 JJ |
1396 | } |
1397 | else | |
1398 | t = shadow; | |
37d6f666 | 1399 | |
f6d98484 JJ |
1400 | g = gimple_build_cond (NE_EXPR, t, build_int_cst (TREE_TYPE (t), 0), |
1401 | NULL_TREE, NULL_TREE); | |
1402 | gimple_set_location (g, location); | |
1403 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
37d6f666 | 1404 | |
f6d98484 | 1405 | /* Generate call to the run-time library (e.g. __asan_report_load8). */ |
37d6f666 | 1406 | gsi = gsi_start_bb (then_bb); |
f6d98484 JJ |
1407 | g = gimple_build_call (report_error_func (is_store, size_in_bytes), |
1408 | 1, base_addr); | |
1409 | gimple_set_location (g, location); | |
1410 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
37d6f666 | 1411 | |
dfb9e332 | 1412 | *iter = gsi_start_bb (else_bb); |
37d6f666 WM |
1413 | } |
1414 | ||
1415 | /* If T represents a memory access, add instrumentation code before ITER. | |
1416 | LOCATION is source code location. | |
25ae5027 | 1417 | IS_STORE is either TRUE (for a store) or FALSE (for a load). */ |
37d6f666 WM |
1418 | |
1419 | static void | |
1420 | instrument_derefs (gimple_stmt_iterator *iter, tree t, | |
bdcbe80c | 1421 | location_t location, bool is_store) |
37d6f666 WM |
1422 | { |
1423 | tree type, base; | |
f6d98484 | 1424 | HOST_WIDE_INT size_in_bytes; |
37d6f666 WM |
1425 | |
1426 | type = TREE_TYPE (t); | |
37d6f666 WM |
1427 | switch (TREE_CODE (t)) |
1428 | { | |
1429 | case ARRAY_REF: | |
1430 | case COMPONENT_REF: | |
1431 | case INDIRECT_REF: | |
1432 | case MEM_REF: | |
1433 | break; | |
1434 | default: | |
1435 | return; | |
1436 | } | |
f6d98484 JJ |
1437 | |
1438 | size_in_bytes = int_size_in_bytes (type); | |
1439 | if ((size_in_bytes & (size_in_bytes - 1)) != 0 | |
1440 | || (unsigned HOST_WIDE_INT) size_in_bytes - 1 >= 16) | |
1441 | return; | |
1442 | ||
f6d98484 JJ |
1443 | HOST_WIDE_INT bitsize, bitpos; |
1444 | tree offset; | |
1445 | enum machine_mode mode; | |
1446 | int volatilep = 0, unsignedp = 0; | |
1447 | get_inner_reference (t, &bitsize, &bitpos, &offset, | |
1448 | &mode, &unsignedp, &volatilep, false); | |
25ae5027 DS |
1449 | if (bitpos % (size_in_bytes * BITS_PER_UNIT) |
1450 | || bitsize != size_in_bytes * BITS_PER_UNIT) | |
1fe04fdc JJ |
1451 | { |
1452 | if (TREE_CODE (t) == COMPONENT_REF | |
1453 | && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE) | |
1454 | { | |
1455 | tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)); | |
1456 | instrument_derefs (iter, build3 (COMPONENT_REF, TREE_TYPE (repr), | |
1457 | TREE_OPERAND (t, 0), repr, | |
1458 | NULL_TREE), location, is_store); | |
1459 | } | |
1460 | return; | |
1461 | } | |
f6d98484 JJ |
1462 | |
1463 | base = build_fold_addr_expr (t); | |
bdcbe80c DS |
1464 | if (!has_mem_ref_been_instrumented (base, size_in_bytes)) |
1465 | { | |
1466 | build_check_stmt (location, base, iter, /*before_p=*/true, | |
1467 | is_store, size_in_bytes); | |
1468 | update_mem_ref_hash_table (base, size_in_bytes); | |
1469 | update_mem_ref_hash_table (t, size_in_bytes); | |
1470 | } | |
1471 | ||
25ae5027 DS |
1472 | } |
1473 | ||
1474 | /* Instrument an access to a contiguous memory region that starts at | |
1475 | the address pointed to by BASE, over a length of LEN (expressed in | |
1476 | the sizeof (*BASE) bytes). ITER points to the instruction before | |
1477 | which the instrumentation instructions must be inserted. LOCATION | |
1478 | is the source location that the instrumentation instructions must | |
1479 | have. If IS_STORE is true, then the memory access is a store; | |
1480 | otherwise, it's a load. */ | |
1481 | ||
1482 | static void | |
1483 | instrument_mem_region_access (tree base, tree len, | |
1484 | gimple_stmt_iterator *iter, | |
1485 | location_t location, bool is_store) | |
1486 | { | |
c63d3b96 JJ |
1487 | if (!POINTER_TYPE_P (TREE_TYPE (base)) |
1488 | || !INTEGRAL_TYPE_P (TREE_TYPE (len)) | |
1489 | || integer_zerop (len)) | |
25ae5027 DS |
1490 | return; |
1491 | ||
1492 | gimple_stmt_iterator gsi = *iter; | |
1493 | ||
1494 | basic_block fallthrough_bb = NULL, then_bb = NULL; | |
bdcbe80c DS |
1495 | |
1496 | /* If the beginning of the memory region has already been | |
1497 | instrumented, do not instrument it. */ | |
b41288b3 JJ |
1498 | bool start_instrumented = has_mem_ref_been_instrumented (base, 1); |
1499 | ||
1500 | /* If the end of the memory region has already been instrumented, do | |
1501 | not instrument it. */ | |
1502 | tree end = asan_mem_ref_get_end (base, len); | |
1503 | bool end_instrumented = has_mem_ref_been_instrumented (end, 1); | |
1504 | ||
1505 | if (start_instrumented && end_instrumented) | |
1506 | return; | |
bdcbe80c | 1507 | |
25ae5027 DS |
1508 | if (!is_gimple_constant (len)) |
1509 | { | |
1510 | /* So, the length of the memory area to asan-protect is | |
1511 | non-constant. Let's guard the generated instrumentation code | |
1512 | like: | |
1513 | ||
1514 | if (len != 0) | |
1515 | { | |
1516 | //asan instrumentation code goes here. | |
497a1c66 | 1517 | } |
25ae5027 DS |
1518 | // falltrough instructions, starting with *ITER. */ |
1519 | ||
1520 | gimple g = gimple_build_cond (NE_EXPR, | |
1521 | len, | |
1522 | build_int_cst (TREE_TYPE (len), 0), | |
1523 | NULL_TREE, NULL_TREE); | |
1524 | gimple_set_location (g, location); | |
1525 | insert_if_then_before_iter (g, iter, /*then_more_likely_p=*/true, | |
1526 | &then_bb, &fallthrough_bb); | |
1527 | /* Note that fallthrough_bb starts with the statement that was | |
1528 | pointed to by ITER. */ | |
1529 | ||
1530 | /* The 'then block' of the 'if (len != 0) condition is where | |
1531 | we'll generate the asan instrumentation code now. */ | |
b41288b3 | 1532 | gsi = gsi_last_bb (then_bb); |
25ae5027 DS |
1533 | } |
1534 | ||
b41288b3 JJ |
1535 | if (!start_instrumented) |
1536 | { | |
1537 | /* Instrument the beginning of the memory region to be accessed, | |
1538 | and arrange for the rest of the intrumentation code to be | |
1539 | inserted in the then block *after* the current gsi. */ | |
1540 | build_check_stmt (location, base, &gsi, /*before_p=*/true, is_store, 1); | |
1541 | ||
1542 | if (then_bb) | |
1543 | /* We are in the case where the length of the region is not | |
1544 | constant; so instrumentation code is being generated in the | |
1545 | 'then block' of the 'if (len != 0) condition. Let's arrange | |
1546 | for the subsequent instrumentation statements to go in the | |
1547 | 'then block'. */ | |
1548 | gsi = gsi_last_bb (then_bb); | |
1549 | else | |
1550 | { | |
1551 | *iter = gsi; | |
1552 | /* Don't remember this access as instrumented, if length | |
1553 | is unknown. It might be zero and not being actually | |
1554 | instrumented, so we can't rely on it being instrumented. */ | |
1555 | update_mem_ref_hash_table (base, 1); | |
1556 | } | |
1557 | } | |
bdcbe80c | 1558 | |
b41288b3 JJ |
1559 | if (end_instrumented) |
1560 | return; | |
bdcbe80c | 1561 | |
25ae5027 DS |
1562 | /* We want to instrument the access at the end of the memory region, |
1563 | which is at (base + len - 1). */ | |
1564 | ||
1565 | /* offset = len - 1; */ | |
1566 | len = unshare_expr (len); | |
c63d3b96 JJ |
1567 | tree offset; |
1568 | gimple_seq seq = NULL; | |
1569 | if (TREE_CODE (len) == INTEGER_CST) | |
1570 | offset = fold_build2 (MINUS_EXPR, size_type_node, | |
1571 | fold_convert (size_type_node, len), | |
1572 | build_int_cst (size_type_node, 1)); | |
1573 | else | |
1574 | { | |
1575 | gimple g; | |
1576 | tree t; | |
1577 | ||
1578 | if (TREE_CODE (len) != SSA_NAME) | |
1579 | { | |
1580 | t = make_ssa_name (TREE_TYPE (len), NULL); | |
1581 | g = gimple_build_assign_with_ops (TREE_CODE (len), t, len, NULL); | |
1582 | gimple_set_location (g, location); | |
1583 | gimple_seq_add_stmt_without_update (&seq, g); | |
1584 | len = t; | |
1585 | } | |
1586 | if (!useless_type_conversion_p (size_type_node, TREE_TYPE (len))) | |
1587 | { | |
1588 | t = make_ssa_name (size_type_node, NULL); | |
1589 | g = gimple_build_assign_with_ops (NOP_EXPR, t, len, NULL); | |
1590 | gimple_set_location (g, location); | |
1591 | gimple_seq_add_stmt_without_update (&seq, g); | |
1592 | len = t; | |
1593 | } | |
1594 | ||
1595 | t = make_ssa_name (size_type_node, NULL); | |
1596 | g = gimple_build_assign_with_ops (MINUS_EXPR, t, len, | |
1597 | build_int_cst (size_type_node, 1)); | |
1598 | gimple_set_location (g, location); | |
bdcbe80c DS |
1599 | gimple_seq_add_stmt_without_update (&seq, g); |
1600 | offset = gimple_assign_lhs (g); | |
1601 | } | |
25ae5027 | 1602 | |
bdcbe80c DS |
1603 | /* _1 = base; */ |
1604 | base = unshare_expr (base); | |
1605 | gimple region_end = | |
1606 | gimple_build_assign_with_ops (TREE_CODE (base), | |
1607 | make_ssa_name (TREE_TYPE (base), NULL), | |
1608 | base, NULL); | |
1609 | gimple_set_location (region_end, location); | |
1610 | gimple_seq_add_stmt_without_update (&seq, region_end); | |
25ae5027 | 1611 | |
bdcbe80c DS |
1612 | /* _2 = _1 + offset; */ |
1613 | region_end = | |
1614 | gimple_build_assign_with_ops (POINTER_PLUS_EXPR, | |
1615 | make_ssa_name (TREE_TYPE (base), NULL), | |
1616 | gimple_assign_lhs (region_end), | |
1617 | offset); | |
1618 | gimple_set_location (region_end, location); | |
b41288b3 JJ |
1619 | gimple_seq_add_stmt_without_update (&seq, region_end); |
1620 | gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); | |
25ae5027 | 1621 | |
bdcbe80c | 1622 | /* instrument access at _2; */ |
b41288b3 | 1623 | gsi = gsi_for_stmt (region_end); |
bdcbe80c DS |
1624 | build_check_stmt (location, gimple_assign_lhs (region_end), |
1625 | &gsi, /*before_p=*/false, is_store, 1); | |
25ae5027 | 1626 | |
b41288b3 JJ |
1627 | if (then_bb == NULL) |
1628 | update_mem_ref_hash_table (end, 1); | |
1629 | ||
1630 | *iter = gsi_for_stmt (gsi_stmt (*iter)); | |
bdcbe80c | 1631 | } |
25ae5027 | 1632 | |
bdcbe80c DS |
1633 | /* Instrument the call (to the builtin strlen function) pointed to by |
1634 | ITER. | |
25ae5027 | 1635 | |
bdcbe80c DS |
1636 | This function instruments the access to the first byte of the |
1637 | argument, right before the call. After the call it instruments the | |
1638 | access to the last byte of the argument; it uses the result of the | |
1639 | call to deduce the offset of that last byte. | |
25ae5027 | 1640 | |
99c2bd54 | 1641 | Upon completion, iff the call has actually been instrumented, this |
bdcbe80c DS |
1642 | function returns TRUE and *ITER points to the statement logically |
1643 | following the built-in strlen function call *ITER was initially | |
1644 | pointing to. Otherwise, the function returns FALSE and *ITER | |
1645 | remains unchanged. */ | |
25ae5027 | 1646 | |
bdcbe80c DS |
1647 | static bool |
1648 | instrument_strlen_call (gimple_stmt_iterator *iter) | |
1649 | { | |
1650 | gimple call = gsi_stmt (*iter); | |
1651 | gcc_assert (is_gimple_call (call)); | |
25ae5027 | 1652 | |
bdcbe80c DS |
1653 | tree callee = gimple_call_fndecl (call); |
1654 | gcc_assert (is_builtin_fn (callee) | |
1655 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL | |
1656 | && DECL_FUNCTION_CODE (callee) == BUILT_IN_STRLEN); | |
25ae5027 | 1657 | |
bdcbe80c DS |
1658 | tree len = gimple_call_lhs (call); |
1659 | if (len == NULL) | |
1660 | /* Some passes might clear the return value of the strlen call; | |
1661 | bail out in that case. Return FALSE as we are not advancing | |
1662 | *ITER. */ | |
1663 | return false; | |
1664 | gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (len))); | |
25ae5027 | 1665 | |
bdcbe80c DS |
1666 | location_t loc = gimple_location (call); |
1667 | tree str_arg = gimple_call_arg (call, 0); | |
25ae5027 | 1668 | |
bdcbe80c | 1669 | /* Instrument the access to the first byte of str_arg. i.e: |
25ae5027 | 1670 | |
bdcbe80c | 1671 | _1 = str_arg; instrument (_1); */ |
99c2bd54 | 1672 | tree cptr_type = build_pointer_type (char_type_node); |
bdcbe80c DS |
1673 | gimple str_arg_ssa = |
1674 | gimple_build_assign_with_ops (NOP_EXPR, | |
99c2bd54 | 1675 | make_ssa_name (cptr_type, NULL), |
bdcbe80c DS |
1676 | str_arg, NULL); |
1677 | gimple_set_location (str_arg_ssa, loc); | |
1678 | gimple_stmt_iterator gsi = *iter; | |
1679 | gsi_insert_before (&gsi, str_arg_ssa, GSI_NEW_STMT); | |
1680 | build_check_stmt (loc, gimple_assign_lhs (str_arg_ssa), &gsi, | |
1681 | /*before_p=*/false, /*is_store=*/false, 1); | |
25ae5027 | 1682 | |
bdcbe80c | 1683 | /* If we initially had an instruction like: |
25ae5027 | 1684 | |
bdcbe80c | 1685 | int n = strlen (str) |
25ae5027 | 1686 | |
bdcbe80c DS |
1687 | we now want to instrument the access to str[n], after the |
1688 | instruction above.*/ | |
25ae5027 | 1689 | |
bdcbe80c DS |
1690 | /* So let's build the access to str[n] that is, access through the |
1691 | pointer_plus expr: (_1 + len). */ | |
1692 | gimple stmt = | |
1693 | gimple_build_assign_with_ops (POINTER_PLUS_EXPR, | |
99c2bd54 | 1694 | make_ssa_name (cptr_type, NULL), |
bdcbe80c DS |
1695 | gimple_assign_lhs (str_arg_ssa), |
1696 | len); | |
1697 | gimple_set_location (stmt, loc); | |
1698 | gsi_insert_after (&gsi, stmt, GSI_NEW_STMT); | |
25ae5027 | 1699 | |
bdcbe80c DS |
1700 | build_check_stmt (loc, gimple_assign_lhs (stmt), &gsi, |
1701 | /*before_p=*/false, /*is_store=*/false, 1); | |
25ae5027 | 1702 | |
bdcbe80c DS |
1703 | /* Ensure that iter points to the statement logically following the |
1704 | one it was initially pointing to. */ | |
1705 | *iter = gsi; | |
1706 | /* As *ITER has been advanced to point to the next statement, let's | |
1707 | return true to inform transform_statements that it shouldn't | |
1708 | advance *ITER anymore; otherwises it will skip that next | |
1709 | statement, which wouldn't be instrumented. */ | |
1710 | return true; | |
1711 | } | |
25ae5027 | 1712 | |
bdcbe80c DS |
1713 | /* Instrument the call to a built-in memory access function that is |
1714 | pointed to by the iterator ITER. | |
25ae5027 | 1715 | |
bdcbe80c DS |
1716 | Upon completion, return TRUE iff *ITER has been advanced to the |
1717 | statement following the one it was originally pointing to. */ | |
25ae5027 | 1718 | |
bdcbe80c DS |
1719 | static bool |
1720 | instrument_builtin_call (gimple_stmt_iterator *iter) | |
1721 | { | |
1722 | bool iter_advanced_p = false; | |
1723 | gimple call = gsi_stmt (*iter); | |
25ae5027 | 1724 | |
bdcbe80c | 1725 | gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); |
25ae5027 | 1726 | |
bdcbe80c DS |
1727 | tree callee = gimple_call_fndecl (call); |
1728 | location_t loc = gimple_location (call); | |
25ae5027 | 1729 | |
bdcbe80c DS |
1730 | if (DECL_FUNCTION_CODE (callee) == BUILT_IN_STRLEN) |
1731 | iter_advanced_p = instrument_strlen_call (iter); | |
1732 | else | |
25ae5027 | 1733 | { |
bdcbe80c DS |
1734 | asan_mem_ref src0, src1, dest; |
1735 | asan_mem_ref_init (&src0, NULL, 1); | |
1736 | asan_mem_ref_init (&src1, NULL, 1); | |
1737 | asan_mem_ref_init (&dest, NULL, 1); | |
1738 | ||
1739 | tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; | |
1740 | bool src0_is_store = false, src1_is_store = false, | |
1741 | dest_is_store = false, dest_is_deref = false; | |
1742 | ||
1743 | if (get_mem_refs_of_builtin_call (call, | |
1744 | &src0, &src0_len, &src0_is_store, | |
8fb06726 | 1745 | &src1, &src1_len, &src1_is_store, |
bdcbe80c DS |
1746 | &dest, &dest_len, &dest_is_store, |
1747 | &dest_is_deref)) | |
1748 | { | |
1749 | if (dest_is_deref) | |
1750 | { | |
1751 | instrument_derefs (iter, dest.start, loc, dest_is_store); | |
1752 | gsi_next (iter); | |
1753 | iter_advanced_p = true; | |
1754 | } | |
1755 | else if (src0_len || src1_len || dest_len) | |
1756 | { | |
b41288b3 | 1757 | if (src0.start != NULL_TREE) |
bdcbe80c DS |
1758 | instrument_mem_region_access (src0.start, src0_len, |
1759 | iter, loc, /*is_store=*/false); | |
1760 | if (src1.start != NULL_TREE) | |
1761 | instrument_mem_region_access (src1.start, src1_len, | |
1762 | iter, loc, /*is_store=*/false); | |
1763 | if (dest.start != NULL_TREE) | |
1764 | instrument_mem_region_access (dest.start, dest_len, | |
1765 | iter, loc, /*is_store=*/true); | |
1766 | *iter = gsi_for_stmt (call); | |
1767 | gsi_next (iter); | |
1768 | iter_advanced_p = true; | |
1769 | } | |
1770 | } | |
25ae5027 | 1771 | } |
bdcbe80c | 1772 | return iter_advanced_p; |
25ae5027 DS |
1773 | } |
1774 | ||
1775 | /* Instrument the assignment statement ITER if it is subject to | |
bdcbe80c DS |
1776 | instrumentation. Return TRUE iff instrumentation actually |
1777 | happened. In that case, the iterator ITER is advanced to the next | |
1778 | logical expression following the one initially pointed to by ITER, | |
1779 | and the relevant memory reference that which access has been | |
1780 | instrumented is added to the memory references hash table. */ | |
25ae5027 | 1781 | |
bdcbe80c DS |
1782 | static bool |
1783 | maybe_instrument_assignment (gimple_stmt_iterator *iter) | |
25ae5027 DS |
1784 | { |
1785 | gimple s = gsi_stmt (*iter); | |
1786 | ||
1787 | gcc_assert (gimple_assign_single_p (s)); | |
1788 | ||
bdcbe80c DS |
1789 | tree ref_expr = NULL_TREE; |
1790 | bool is_store, is_instrumented = false; | |
1791 | ||
52f2e7e1 | 1792 | if (gimple_store_p (s)) |
bdcbe80c DS |
1793 | { |
1794 | ref_expr = gimple_assign_lhs (s); | |
1795 | is_store = true; | |
1796 | instrument_derefs (iter, ref_expr, | |
1797 | gimple_location (s), | |
1798 | is_store); | |
1799 | is_instrumented = true; | |
1800 | } | |
1801 | ||
52f2e7e1 | 1802 | if (gimple_assign_load_p (s)) |
bdcbe80c DS |
1803 | { |
1804 | ref_expr = gimple_assign_rhs1 (s); | |
1805 | is_store = false; | |
1806 | instrument_derefs (iter, ref_expr, | |
1807 | gimple_location (s), | |
1808 | is_store); | |
1809 | is_instrumented = true; | |
1810 | } | |
1811 | ||
1812 | if (is_instrumented) | |
1813 | gsi_next (iter); | |
1814 | ||
1815 | return is_instrumented; | |
25ae5027 DS |
1816 | } |
1817 | ||
1818 | /* Instrument the function call pointed to by the iterator ITER, if it | |
1819 | is subject to instrumentation. At the moment, the only function | |
1820 | calls that are instrumented are some built-in functions that access | |
1821 | memory. Look at instrument_builtin_call to learn more. | |
1822 | ||
1823 | Upon completion return TRUE iff *ITER was advanced to the statement | |
1824 | following the one it was originally pointing to. */ | |
1825 | ||
1826 | static bool | |
1827 | maybe_instrument_call (gimple_stmt_iterator *iter) | |
1828 | { | |
2b2571c9 | 1829 | gimple stmt = gsi_stmt (*iter); |
bdcbe80c DS |
1830 | bool is_builtin = gimple_call_builtin_p (stmt, BUILT_IN_NORMAL); |
1831 | ||
1832 | if (is_builtin && instrument_builtin_call (iter)) | |
2b2571c9 | 1833 | return true; |
bdcbe80c | 1834 | |
2b2571c9 JJ |
1835 | if (gimple_call_noreturn_p (stmt)) |
1836 | { | |
1837 | if (is_builtin) | |
1838 | { | |
1839 | tree callee = gimple_call_fndecl (stmt); | |
1840 | switch (DECL_FUNCTION_CODE (callee)) | |
1841 | { | |
1842 | case BUILT_IN_UNREACHABLE: | |
1843 | case BUILT_IN_TRAP: | |
1844 | /* Don't instrument these. */ | |
1845 | return false; | |
1846 | } | |
1847 | } | |
1848 | tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN); | |
1849 | gimple g = gimple_build_call (decl, 0); | |
1850 | gimple_set_location (g, gimple_location (stmt)); | |
1851 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
1852 | } | |
25ae5027 | 1853 | return false; |
37d6f666 WM |
1854 | } |
1855 | ||
bdcbe80c DS |
1856 | /* Walk each instruction of all basic block and instrument those that |
1857 | represent memory references: loads, stores, or function calls. | |
1858 | In a given basic block, this function avoids instrumenting memory | |
1859 | references that have already been instrumented. */ | |
37d6f666 WM |
1860 | |
1861 | static void | |
1862 | transform_statements (void) | |
1863 | { | |
c4bfe8bf | 1864 | basic_block bb, last_bb = NULL; |
37d6f666 WM |
1865 | gimple_stmt_iterator i; |
1866 | int saved_last_basic_block = last_basic_block; | |
37d6f666 WM |
1867 | |
1868 | FOR_EACH_BB (bb) | |
1869 | { | |
c4bfe8bf | 1870 | basic_block prev_bb = bb; |
bdcbe80c | 1871 | |
37d6f666 | 1872 | if (bb->index >= saved_last_basic_block) continue; |
c4bfe8bf JJ |
1873 | |
1874 | /* Flush the mem ref hash table, if current bb doesn't have | |
1875 | exactly one predecessor, or if that predecessor (skipping | |
1876 | over asan created basic blocks) isn't the last processed | |
1877 | basic block. Thus we effectively flush on extended basic | |
1878 | block boundaries. */ | |
1879 | while (single_pred_p (prev_bb)) | |
1880 | { | |
1881 | prev_bb = single_pred (prev_bb); | |
1882 | if (prev_bb->index < saved_last_basic_block) | |
1883 | break; | |
1884 | } | |
1885 | if (prev_bb != last_bb) | |
1886 | empty_mem_ref_hash_table (); | |
1887 | last_bb = bb; | |
1888 | ||
25ae5027 | 1889 | for (i = gsi_start_bb (bb); !gsi_end_p (i);) |
497a1c66 | 1890 | { |
25ae5027 DS |
1891 | gimple s = gsi_stmt (i); |
1892 | ||
bdcbe80c DS |
1893 | if (has_stmt_been_instrumented_p (s)) |
1894 | gsi_next (&i); | |
1895 | else if (gimple_assign_single_p (s) | |
1896 | && maybe_instrument_assignment (&i)) | |
1897 | /* Nothing to do as maybe_instrument_assignment advanced | |
1898 | the iterator I. */; | |
1899 | else if (is_gimple_call (s) && maybe_instrument_call (&i)) | |
1900 | /* Nothing to do as maybe_instrument_call | |
1901 | advanced the iterator I. */; | |
1902 | else | |
25ae5027 | 1903 | { |
bdcbe80c DS |
1904 | /* No instrumentation happened. |
1905 | ||
c4bfe8bf JJ |
1906 | If the current instruction is a function call that |
1907 | might free something, let's forget about the memory | |
1908 | references that got instrumented. Otherwise we might | |
1909 | miss some instrumentation opportunities. */ | |
1910 | if (is_gimple_call (s) && !nonfreeing_call_p (s)) | |
bdcbe80c DS |
1911 | empty_mem_ref_hash_table (); |
1912 | ||
1913 | gsi_next (&i); | |
25ae5027 | 1914 | } |
497a1c66 | 1915 | } |
37d6f666 | 1916 | } |
bdcbe80c | 1917 | free_mem_ref_resources (); |
37d6f666 WM |
1918 | } |
1919 | ||
8240018b JJ |
1920 | /* Build |
1921 | struct __asan_global | |
1922 | { | |
1923 | const void *__beg; | |
1924 | uptr __size; | |
1925 | uptr __size_with_redzone; | |
1926 | const void *__name; | |
1927 | uptr __has_dynamic_init; | |
1928 | } type. */ | |
1929 | ||
1930 | static tree | |
1931 | asan_global_struct (void) | |
1932 | { | |
1933 | static const char *field_names[5] | |
1934 | = { "__beg", "__size", "__size_with_redzone", | |
1935 | "__name", "__has_dynamic_init" }; | |
1936 | tree fields[5], ret; | |
1937 | int i; | |
1938 | ||
1939 | ret = make_node (RECORD_TYPE); | |
1940 | for (i = 0; i < 5; i++) | |
1941 | { | |
1942 | fields[i] | |
1943 | = build_decl (UNKNOWN_LOCATION, FIELD_DECL, | |
1944 | get_identifier (field_names[i]), | |
1945 | (i == 0 || i == 3) ? const_ptr_type_node | |
de5a5fa1 | 1946 | : pointer_sized_int_node); |
8240018b JJ |
1947 | DECL_CONTEXT (fields[i]) = ret; |
1948 | if (i) | |
1949 | DECL_CHAIN (fields[i - 1]) = fields[i]; | |
1950 | } | |
1951 | TYPE_FIELDS (ret) = fields[0]; | |
1952 | TYPE_NAME (ret) = get_identifier ("__asan_global"); | |
1953 | layout_type (ret); | |
1954 | return ret; | |
1955 | } | |
1956 | ||
1957 | /* Append description of a single global DECL into vector V. | |
1958 | TYPE is __asan_global struct type as returned by asan_global_struct. */ | |
1959 | ||
1960 | static void | |
9771b263 | 1961 | asan_add_global (tree decl, tree type, vec<constructor_elt, va_gc> *v) |
8240018b JJ |
1962 | { |
1963 | tree init, uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))); | |
1964 | unsigned HOST_WIDE_INT size; | |
1965 | tree str_cst, refdecl = decl; | |
9771b263 | 1966 | vec<constructor_elt, va_gc> *vinner = NULL; |
8240018b | 1967 | |
11a877b3 | 1968 | pretty_printer asan_pp; |
8240018b | 1969 | |
8240018b | 1970 | if (DECL_NAME (decl)) |
b066401f | 1971 | pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
8240018b JJ |
1972 | else |
1973 | pp_string (&asan_pp, "<unknown>"); | |
1974 | pp_space (&asan_pp); | |
1975 | pp_left_paren (&asan_pp); | |
1976 | pp_string (&asan_pp, main_input_filename); | |
1977 | pp_right_paren (&asan_pp); | |
11a877b3 | 1978 | str_cst = asan_pp_string (&asan_pp); |
8240018b JJ |
1979 | |
1980 | if (asan_needs_local_alias (decl)) | |
1981 | { | |
1982 | char buf[20]; | |
9771b263 | 1983 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", vec_safe_length (v) + 1); |
8240018b JJ |
1984 | refdecl = build_decl (DECL_SOURCE_LOCATION (decl), |
1985 | VAR_DECL, get_identifier (buf), TREE_TYPE (decl)); | |
1986 | TREE_ADDRESSABLE (refdecl) = TREE_ADDRESSABLE (decl); | |
1987 | TREE_READONLY (refdecl) = TREE_READONLY (decl); | |
1988 | TREE_THIS_VOLATILE (refdecl) = TREE_THIS_VOLATILE (decl); | |
1989 | DECL_GIMPLE_REG_P (refdecl) = DECL_GIMPLE_REG_P (decl); | |
1990 | DECL_ARTIFICIAL (refdecl) = DECL_ARTIFICIAL (decl); | |
1991 | DECL_IGNORED_P (refdecl) = DECL_IGNORED_P (decl); | |
1992 | TREE_STATIC (refdecl) = 1; | |
1993 | TREE_PUBLIC (refdecl) = 0; | |
1994 | TREE_USED (refdecl) = 1; | |
1995 | assemble_alias (refdecl, DECL_ASSEMBLER_NAME (decl)); | |
1996 | } | |
1997 | ||
1998 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, | |
1999 | fold_convert (const_ptr_type_node, | |
2000 | build_fold_addr_expr (refdecl))); | |
2001 | size = tree_low_cst (DECL_SIZE_UNIT (decl), 1); | |
2002 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); | |
2003 | size += asan_red_zone_size (size); | |
2004 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); | |
2005 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, | |
2006 | fold_convert (const_ptr_type_node, str_cst)); | |
2007 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, 0)); | |
2008 | init = build_constructor (type, vinner); | |
2009 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); | |
2010 | } | |
2011 | ||
0e668eaf JJ |
2012 | /* Initialize sanitizer.def builtins if the FE hasn't initialized them. */ |
2013 | void | |
2014 | initialize_sanitizer_builtins (void) | |
2015 | { | |
2016 | tree decl; | |
2017 | ||
2018 | if (builtin_decl_implicit_p (BUILT_IN_ASAN_INIT)) | |
2019 | return; | |
2020 | ||
2021 | tree BT_FN_VOID = build_function_type_list (void_type_node, NULL_TREE); | |
2022 | tree BT_FN_VOID_PTR | |
2023 | = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); | |
b906f4ca MP |
2024 | tree BT_FN_VOID_PTR_PTR |
2025 | = build_function_type_list (void_type_node, ptr_type_node, | |
2026 | ptr_type_node, NULL_TREE); | |
de5a5fa1 MP |
2027 | tree BT_FN_VOID_PTR_PTR_PTR |
2028 | = build_function_type_list (void_type_node, ptr_type_node, | |
2029 | ptr_type_node, ptr_type_node, NULL_TREE); | |
0e668eaf JJ |
2030 | tree BT_FN_VOID_PTR_PTRMODE |
2031 | = build_function_type_list (void_type_node, ptr_type_node, | |
de5a5fa1 | 2032 | pointer_sized_int_node, NULL_TREE); |
c954bddd JJ |
2033 | tree BT_FN_VOID_INT |
2034 | = build_function_type_list (void_type_node, integer_type_node, NULL_TREE); | |
2035 | tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5]; | |
2036 | tree BT_FN_IX_CONST_VPTR_INT[5]; | |
2037 | tree BT_FN_IX_VPTR_IX_INT[5]; | |
2038 | tree BT_FN_VOID_VPTR_IX_INT[5]; | |
2039 | tree vptr | |
2040 | = build_pointer_type (build_qualified_type (void_type_node, | |
2041 | TYPE_QUAL_VOLATILE)); | |
2042 | tree cvptr | |
2043 | = build_pointer_type (build_qualified_type (void_type_node, | |
2044 | TYPE_QUAL_VOLATILE | |
2045 | |TYPE_QUAL_CONST)); | |
2046 | tree boolt | |
2047 | = lang_hooks.types.type_for_size (BOOL_TYPE_SIZE, 1); | |
2048 | int i; | |
2049 | for (i = 0; i < 5; i++) | |
2050 | { | |
2051 | tree ix = build_nonstandard_integer_type (BITS_PER_UNIT * (1 << i), 1); | |
2052 | BT_FN_BOOL_VPTR_PTR_IX_INT_INT[i] | |
2053 | = build_function_type_list (boolt, vptr, ptr_type_node, ix, | |
2054 | integer_type_node, integer_type_node, | |
2055 | NULL_TREE); | |
2056 | BT_FN_IX_CONST_VPTR_INT[i] | |
2057 | = build_function_type_list (ix, cvptr, integer_type_node, NULL_TREE); | |
2058 | BT_FN_IX_VPTR_IX_INT[i] | |
2059 | = build_function_type_list (ix, vptr, ix, integer_type_node, | |
2060 | NULL_TREE); | |
2061 | BT_FN_VOID_VPTR_IX_INT[i] | |
2062 | = build_function_type_list (void_type_node, vptr, ix, | |
2063 | integer_type_node, NULL_TREE); | |
2064 | } | |
2065 | #define BT_FN_BOOL_VPTR_PTR_I1_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[0] | |
2066 | #define BT_FN_I1_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[0] | |
2067 | #define BT_FN_I1_VPTR_I1_INT BT_FN_IX_VPTR_IX_INT[0] | |
2068 | #define BT_FN_VOID_VPTR_I1_INT BT_FN_VOID_VPTR_IX_INT[0] | |
2069 | #define BT_FN_BOOL_VPTR_PTR_I2_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[1] | |
2070 | #define BT_FN_I2_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[1] | |
2071 | #define BT_FN_I2_VPTR_I2_INT BT_FN_IX_VPTR_IX_INT[1] | |
2072 | #define BT_FN_VOID_VPTR_I2_INT BT_FN_VOID_VPTR_IX_INT[1] | |
2073 | #define BT_FN_BOOL_VPTR_PTR_I4_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[2] | |
2074 | #define BT_FN_I4_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[2] | |
2075 | #define BT_FN_I4_VPTR_I4_INT BT_FN_IX_VPTR_IX_INT[2] | |
2076 | #define BT_FN_VOID_VPTR_I4_INT BT_FN_VOID_VPTR_IX_INT[2] | |
2077 | #define BT_FN_BOOL_VPTR_PTR_I8_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[3] | |
2078 | #define BT_FN_I8_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[3] | |
2079 | #define BT_FN_I8_VPTR_I8_INT BT_FN_IX_VPTR_IX_INT[3] | |
2080 | #define BT_FN_VOID_VPTR_I8_INT BT_FN_VOID_VPTR_IX_INT[3] | |
2081 | #define BT_FN_BOOL_VPTR_PTR_I16_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[4] | |
2082 | #define BT_FN_I16_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[4] | |
2083 | #define BT_FN_I16_VPTR_I16_INT BT_FN_IX_VPTR_IX_INT[4] | |
2084 | #define BT_FN_VOID_VPTR_I16_INT BT_FN_VOID_VPTR_IX_INT[4] | |
0e668eaf JJ |
2085 | #undef ATTR_NOTHROW_LEAF_LIST |
2086 | #define ATTR_NOTHROW_LEAF_LIST ECF_NOTHROW | ECF_LEAF | |
bc77608b JJ |
2087 | #undef ATTR_TMPURE_NOTHROW_LEAF_LIST |
2088 | #define ATTR_TMPURE_NOTHROW_LEAF_LIST ECF_TM_PURE | ATTR_NOTHROW_LEAF_LIST | |
0e668eaf JJ |
2089 | #undef ATTR_NORETURN_NOTHROW_LEAF_LIST |
2090 | #define ATTR_NORETURN_NOTHROW_LEAF_LIST ECF_NORETURN | ATTR_NOTHROW_LEAF_LIST | |
bc77608b JJ |
2091 | #undef ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST |
2092 | #define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST \ | |
2093 | ECF_TM_PURE | ATTR_NORETURN_NOTHROW_LEAF_LIST | |
de5a5fa1 MP |
2094 | #undef ATTR_COLD_NOTHROW_LEAF_LIST |
2095 | #define ATTR_COLD_NOTHROW_LEAF_LIST \ | |
2096 | /* ECF_COLD missing */ ATTR_NOTHROW_LEAF_LIST | |
2097 | #undef ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST | |
2098 | #define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \ | |
2099 | /* ECF_COLD missing */ ATTR_NORETURN_NOTHROW_LEAF_LIST | |
0e668eaf JJ |
2100 | #undef DEF_SANITIZER_BUILTIN |
2101 | #define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \ | |
2102 | decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM, \ | |
2103 | BUILT_IN_NORMAL, NAME, NULL_TREE); \ | |
2104 | set_call_expr_flags (decl, ATTRS); \ | |
2105 | set_builtin_decl (ENUM, decl, true); | |
2106 | ||
2107 | #include "sanitizer.def" | |
2108 | ||
2109 | #undef DEF_SANITIZER_BUILTIN | |
2110 | } | |
2111 | ||
94fce891 JJ |
2112 | /* Called via htab_traverse. Count number of emitted |
2113 | STRING_CSTs in the constant hash table. */ | |
2114 | ||
2115 | static int | |
2116 | count_string_csts (void **slot, void *data) | |
2117 | { | |
2118 | struct constant_descriptor_tree *desc | |
2119 | = (struct constant_descriptor_tree *) *slot; | |
2120 | if (TREE_CODE (desc->value) == STRING_CST | |
2121 | && TREE_ASM_WRITTEN (desc->value) | |
2122 | && asan_protect_global (desc->value)) | |
2123 | ++*((unsigned HOST_WIDE_INT *) data); | |
2124 | return 1; | |
2125 | } | |
2126 | ||
2127 | /* Helper structure to pass two parameters to | |
2128 | add_string_csts. */ | |
2129 | ||
2130 | struct asan_add_string_csts_data | |
2131 | { | |
2132 | tree type; | |
2133 | vec<constructor_elt, va_gc> *v; | |
2134 | }; | |
2135 | ||
2136 | /* Called via htab_traverse. Call asan_add_global | |
2137 | on emitted STRING_CSTs from the constant hash table. */ | |
2138 | ||
2139 | static int | |
2140 | add_string_csts (void **slot, void *data) | |
2141 | { | |
2142 | struct constant_descriptor_tree *desc | |
2143 | = (struct constant_descriptor_tree *) *slot; | |
2144 | if (TREE_CODE (desc->value) == STRING_CST | |
2145 | && TREE_ASM_WRITTEN (desc->value) | |
2146 | && asan_protect_global (desc->value)) | |
2147 | { | |
2148 | struct asan_add_string_csts_data *aascd | |
2149 | = (struct asan_add_string_csts_data *) data; | |
2150 | asan_add_global (SYMBOL_REF_DECL (XEXP (desc->rtl, 0)), | |
2151 | aascd->type, aascd->v); | |
2152 | } | |
2153 | return 1; | |
2154 | } | |
2155 | ||
8240018b JJ |
2156 | /* Needs to be GTY(()), because cgraph_build_static_cdtor may |
2157 | invoke ggc_collect. */ | |
2158 | static GTY(()) tree asan_ctor_statements; | |
2159 | ||
37d6f666 WM |
2160 | /* Module-level instrumentation. |
2161 | - Insert __asan_init() into the list of CTORs. | |
2162 | - TODO: insert redzones around globals. | |
2163 | */ | |
2164 | ||
2165 | void | |
2166 | asan_finish_file (void) | |
2167 | { | |
8240018b JJ |
2168 | struct varpool_node *vnode; |
2169 | unsigned HOST_WIDE_INT gcount = 0; | |
2170 | ||
94fce891 JJ |
2171 | if (shadow_ptr_types[0] == NULL_TREE) |
2172 | asan_init_shadow_ptr_types (); | |
2173 | /* Avoid instrumenting code in the asan ctors/dtors. | |
2174 | We don't need to insert padding after the description strings, | |
2175 | nor after .LASAN* array. */ | |
de5a5fa1 | 2176 | flag_sanitize &= ~SANITIZE_ADDRESS; |
0e668eaf JJ |
2177 | |
2178 | tree fn = builtin_decl_implicit (BUILT_IN_ASAN_INIT); | |
2179 | append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements); | |
8240018b | 2180 | FOR_EACH_DEFINED_VARIABLE (vnode) |
67348ccc DM |
2181 | if (TREE_ASM_WRITTEN (vnode->decl) |
2182 | && asan_protect_global (vnode->decl)) | |
8240018b | 2183 | ++gcount; |
94fce891 JJ |
2184 | htab_t const_desc_htab = constant_pool_htab (); |
2185 | htab_traverse (const_desc_htab, count_string_csts, &gcount); | |
8240018b JJ |
2186 | if (gcount) |
2187 | { | |
0e668eaf | 2188 | tree type = asan_global_struct (), var, ctor; |
8240018b | 2189 | tree dtor_statements = NULL_TREE; |
9771b263 | 2190 | vec<constructor_elt, va_gc> *v; |
8240018b JJ |
2191 | char buf[20]; |
2192 | ||
2193 | type = build_array_type_nelts (type, gcount); | |
2194 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", 0); | |
2195 | var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf), | |
2196 | type); | |
2197 | TREE_STATIC (var) = 1; | |
2198 | TREE_PUBLIC (var) = 0; | |
2199 | DECL_ARTIFICIAL (var) = 1; | |
2200 | DECL_IGNORED_P (var) = 1; | |
9771b263 | 2201 | vec_alloc (v, gcount); |
8240018b | 2202 | FOR_EACH_DEFINED_VARIABLE (vnode) |
67348ccc DM |
2203 | if (TREE_ASM_WRITTEN (vnode->decl) |
2204 | && asan_protect_global (vnode->decl)) | |
2205 | asan_add_global (vnode->decl, TREE_TYPE (type), v); | |
94fce891 JJ |
2206 | struct asan_add_string_csts_data aascd; |
2207 | aascd.type = TREE_TYPE (type); | |
2208 | aascd.v = v; | |
2209 | htab_traverse (const_desc_htab, add_string_csts, &aascd); | |
8240018b JJ |
2210 | ctor = build_constructor (type, v); |
2211 | TREE_CONSTANT (ctor) = 1; | |
2212 | TREE_STATIC (ctor) = 1; | |
2213 | DECL_INITIAL (var) = ctor; | |
2214 | varpool_assemble_decl (varpool_node_for_decl (var)); | |
2215 | ||
0e668eaf | 2216 | fn = builtin_decl_implicit (BUILT_IN_ASAN_REGISTER_GLOBALS); |
de5a5fa1 | 2217 | tree gcount_tree = build_int_cst (pointer_sized_int_node, gcount); |
0e668eaf | 2218 | append_to_statement_list (build_call_expr (fn, 2, |
8240018b | 2219 | build_fold_addr_expr (var), |
de5a5fa1 | 2220 | gcount_tree), |
8240018b JJ |
2221 | &asan_ctor_statements); |
2222 | ||
0e668eaf JJ |
2223 | fn = builtin_decl_implicit (BUILT_IN_ASAN_UNREGISTER_GLOBALS); |
2224 | append_to_statement_list (build_call_expr (fn, 2, | |
8240018b | 2225 | build_fold_addr_expr (var), |
de5a5fa1 | 2226 | gcount_tree), |
8240018b JJ |
2227 | &dtor_statements); |
2228 | cgraph_build_static_cdtor ('D', dtor_statements, | |
2229 | MAX_RESERVED_INIT_PRIORITY - 1); | |
2230 | } | |
2231 | cgraph_build_static_cdtor ('I', asan_ctor_statements, | |
2232 | MAX_RESERVED_INIT_PRIORITY - 1); | |
de5a5fa1 | 2233 | flag_sanitize |= SANITIZE_ADDRESS; |
f6d98484 JJ |
2234 | } |
2235 | ||
37d6f666 WM |
2236 | /* Instrument the current function. */ |
2237 | ||
2238 | static unsigned int | |
2239 | asan_instrument (void) | |
2240 | { | |
f6d98484 | 2241 | if (shadow_ptr_types[0] == NULL_TREE) |
94fce891 | 2242 | asan_init_shadow_ptr_types (); |
37d6f666 | 2243 | transform_statements (); |
37d6f666 WM |
2244 | return 0; |
2245 | } | |
2246 | ||
2247 | static bool | |
2248 | gate_asan (void) | |
2249 | { | |
de5a5fa1 | 2250 | return (flag_sanitize & SANITIZE_ADDRESS) != 0 |
e664c61c | 2251 | && !lookup_attribute ("no_sanitize_address", |
77bc5132 | 2252 | DECL_ATTRIBUTES (current_function_decl)); |
37d6f666 WM |
2253 | } |
2254 | ||
27a4cd48 DM |
2255 | namespace { |
2256 | ||
2257 | const pass_data pass_data_asan = | |
37d6f666 | 2258 | { |
27a4cd48 DM |
2259 | GIMPLE_PASS, /* type */ |
2260 | "asan", /* name */ | |
2261 | OPTGROUP_NONE, /* optinfo_flags */ | |
2262 | true, /* has_gate */ | |
2263 | true, /* has_execute */ | |
2264 | TV_NONE, /* tv_id */ | |
2265 | ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ | |
2266 | 0, /* properties_provided */ | |
2267 | 0, /* properties_destroyed */ | |
2268 | 0, /* todo_flags_start */ | |
2269 | ( TODO_verify_flow | TODO_verify_stmts | |
2270 | | TODO_update_ssa ), /* todo_flags_finish */ | |
37d6f666 | 2271 | }; |
f6d98484 | 2272 | |
27a4cd48 DM |
2273 | class pass_asan : public gimple_opt_pass |
2274 | { | |
2275 | public: | |
c3284718 RS |
2276 | pass_asan (gcc::context *ctxt) |
2277 | : gimple_opt_pass (pass_data_asan, ctxt) | |
27a4cd48 DM |
2278 | {} |
2279 | ||
2280 | /* opt_pass methods: */ | |
65d3284b | 2281 | opt_pass * clone () { return new pass_asan (m_ctxt); } |
27a4cd48 DM |
2282 | bool gate () { return gate_asan (); } |
2283 | unsigned int execute () { return asan_instrument (); } | |
2284 | ||
2285 | }; // class pass_asan | |
2286 | ||
2287 | } // anon namespace | |
2288 | ||
2289 | gimple_opt_pass * | |
2290 | make_pass_asan (gcc::context *ctxt) | |
2291 | { | |
2292 | return new pass_asan (ctxt); | |
2293 | } | |
2294 | ||
dfb9e332 JJ |
2295 | static bool |
2296 | gate_asan_O0 (void) | |
2297 | { | |
77bc5132 | 2298 | return !optimize && gate_asan (); |
dfb9e332 JJ |
2299 | } |
2300 | ||
27a4cd48 DM |
2301 | namespace { |
2302 | ||
2303 | const pass_data pass_data_asan_O0 = | |
dfb9e332 | 2304 | { |
27a4cd48 DM |
2305 | GIMPLE_PASS, /* type */ |
2306 | "asan0", /* name */ | |
2307 | OPTGROUP_NONE, /* optinfo_flags */ | |
2308 | true, /* has_gate */ | |
2309 | true, /* has_execute */ | |
2310 | TV_NONE, /* tv_id */ | |
2311 | ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ | |
2312 | 0, /* properties_provided */ | |
2313 | 0, /* properties_destroyed */ | |
2314 | 0, /* todo_flags_start */ | |
2315 | ( TODO_verify_flow | TODO_verify_stmts | |
2316 | | TODO_update_ssa ), /* todo_flags_finish */ | |
dfb9e332 JJ |
2317 | }; |
2318 | ||
27a4cd48 DM |
2319 | class pass_asan_O0 : public gimple_opt_pass |
2320 | { | |
2321 | public: | |
c3284718 RS |
2322 | pass_asan_O0 (gcc::context *ctxt) |
2323 | : gimple_opt_pass (pass_data_asan_O0, ctxt) | |
27a4cd48 DM |
2324 | {} |
2325 | ||
2326 | /* opt_pass methods: */ | |
2327 | bool gate () { return gate_asan_O0 (); } | |
2328 | unsigned int execute () { return asan_instrument (); } | |
2329 | ||
2330 | }; // class pass_asan_O0 | |
2331 | ||
2332 | } // anon namespace | |
2333 | ||
2334 | gimple_opt_pass * | |
2335 | make_pass_asan_O0 (gcc::context *ctxt) | |
2336 | { | |
2337 | return new pass_asan_O0 (ctxt); | |
2338 | } | |
2339 | ||
f6d98484 | 2340 | #include "gt-asan.h" |