]>
Commit | Line | Data |
---|---|---|
37d6f666 | 1 | /* AddressSanitizer, a fast memory error detector. |
5624e564 | 2 | Copyright (C) 2012-2015 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" | |
40e23961 MC |
25 | #include "hash-set.h" |
26 | #include "machmode.h" | |
27 | #include "vec.h" | |
28 | #include "double-int.h" | |
29 | #include "input.h" | |
30 | #include "alias.h" | |
31 | #include "symtab.h" | |
32 | #include "options.h" | |
33 | #include "wide-int.h" | |
34 | #include "inchash.h" | |
4d648807 | 35 | #include "tree.h" |
40e23961 | 36 | #include "fold-const.h" |
2fb9a547 | 37 | #include "hash-table.h" |
60393bbc | 38 | #include "predict.h" |
60393bbc AM |
39 | #include "tm.h" |
40 | #include "hard-reg-set.h" | |
60393bbc AM |
41 | #include "function.h" |
42 | #include "dominance.h" | |
43 | #include "cfg.h" | |
44 | #include "cfganal.h" | |
2fb9a547 AM |
45 | #include "basic-block.h" |
46 | #include "tree-ssa-alias.h" | |
47 | #include "internal-fn.h" | |
48 | #include "gimple-expr.h" | |
49 | #include "is-a.h" | |
18f429e2 | 50 | #include "gimple.h" |
45b0be94 | 51 | #include "gimplify.h" |
5be5c238 | 52 | #include "gimple-iterator.h" |
d8a2d370 DN |
53 | #include "calls.h" |
54 | #include "varasm.h" | |
55 | #include "stor-layout.h" | |
37d6f666 | 56 | #include "tree-iterator.h" |
c582198b AM |
57 | #include "hash-map.h" |
58 | #include "plugin-api.h" | |
59 | #include "ipa-ref.h" | |
442b4905 | 60 | #include "cgraph.h" |
d8a2d370 | 61 | #include "stringpool.h" |
442b4905 | 62 | #include "tree-ssanames.h" |
37d6f666 | 63 | #include "tree-pass.h" |
37d6f666 WM |
64 | #include "asan.h" |
65 | #include "gimple-pretty-print.h" | |
dfe06d3e | 66 | #include "target.h" |
36566b39 PK |
67 | #include "hashtab.h" |
68 | #include "rtl.h" | |
69 | #include "flags.h" | |
70 | #include "statistics.h" | |
71 | #include "real.h" | |
72 | #include "fixed-value.h" | |
73 | #include "insn-config.h" | |
74 | #include "expmed.h" | |
75 | #include "dojump.h" | |
76 | #include "explow.h" | |
77 | #include "emit-rtl.h" | |
78 | #include "stmt.h" | |
f3ddd692 | 79 | #include "expr.h" |
b0710fe1 | 80 | #include "insn-codes.h" |
f3ddd692 | 81 | #include "optabs.h" |
8240018b | 82 | #include "output.h" |
7f71fad9 | 83 | #include "tm_p.h" |
0e668eaf | 84 | #include "langhooks.h" |
bdcbe80c | 85 | #include "alloc-pool.h" |
a9e0d843 | 86 | #include "cfgloop.h" |
ff2a63a7 | 87 | #include "gimple-builder.h" |
b9a55b13 | 88 | #include "ubsan.h" |
b5ebc991 | 89 | #include "params.h" |
9b2b7279 | 90 | #include "builtins.h" |
37d6f666 | 91 | |
497a1c66 JJ |
92 | /* AddressSanitizer finds out-of-bounds and use-after-free bugs |
93 | with <2x slowdown on average. | |
94 | ||
95 | The tool consists of two parts: | |
96 | instrumentation module (this file) and a run-time library. | |
97 | The instrumentation module adds a run-time check before every memory insn. | |
98 | For a 8- or 16- byte load accessing address X: | |
99 | ShadowAddr = (X >> 3) + Offset | |
100 | ShadowValue = *(char*)ShadowAddr; // *(short*) for 16-byte access. | |
101 | if (ShadowValue) | |
102 | __asan_report_load8(X); | |
103 | For a load of N bytes (N=1, 2 or 4) from address X: | |
104 | ShadowAddr = (X >> 3) + Offset | |
105 | ShadowValue = *(char*)ShadowAddr; | |
106 | if (ShadowValue) | |
107 | if ((X & 7) + N - 1 > ShadowValue) | |
108 | __asan_report_loadN(X); | |
109 | Stores are instrumented similarly, but using __asan_report_storeN functions. | |
ef1b3fda KS |
110 | A call too __asan_init_vN() is inserted to the list of module CTORs. |
111 | N is the version number of the AddressSanitizer API. The changes between the | |
112 | API versions are listed in libsanitizer/asan/asan_interface_internal.h. | |
497a1c66 JJ |
113 | |
114 | The run-time library redefines malloc (so that redzone are inserted around | |
115 | the allocated memory) and free (so that reuse of free-ed memory is delayed), | |
ef1b3fda | 116 | provides __asan_report* and __asan_init_vN functions. |
497a1c66 JJ |
117 | |
118 | Read more: | |
119 | http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm | |
120 | ||
121 | The current implementation supports detection of out-of-bounds and | |
122 | use-after-free in the heap, on the stack and for global variables. | |
123 | ||
124 | [Protection of stack variables] | |
125 | ||
126 | To understand how detection of out-of-bounds and use-after-free works | |
127 | for stack variables, lets look at this example on x86_64 where the | |
128 | stack grows downward: | |
f3ddd692 JJ |
129 | |
130 | int | |
131 | foo () | |
132 | { | |
133 | char a[23] = {0}; | |
134 | int b[2] = {0}; | |
135 | ||
136 | a[5] = 1; | |
137 | b[1] = 2; | |
138 | ||
139 | return a[5] + b[1]; | |
140 | } | |
141 | ||
497a1c66 JJ |
142 | For this function, the stack protected by asan will be organized as |
143 | follows, from the top of the stack to the bottom: | |
f3ddd692 | 144 | |
497a1c66 | 145 | Slot 1/ [red zone of 32 bytes called 'RIGHT RedZone'] |
f3ddd692 | 146 | |
497a1c66 JJ |
147 | Slot 2/ [8 bytes of red zone, that adds up to the space of 'a' to make |
148 | the next slot be 32 bytes aligned; this one is called Partial | |
149 | Redzone; this 32 bytes alignment is an asan constraint] | |
f3ddd692 | 150 | |
497a1c66 | 151 | Slot 3/ [24 bytes for variable 'a'] |
f3ddd692 | 152 | |
497a1c66 | 153 | Slot 4/ [red zone of 32 bytes called 'Middle RedZone'] |
f3ddd692 | 154 | |
497a1c66 | 155 | Slot 5/ [24 bytes of Partial Red Zone (similar to slot 2] |
f3ddd692 | 156 | |
497a1c66 | 157 | Slot 6/ [8 bytes for variable 'b'] |
f3ddd692 | 158 | |
497a1c66 JJ |
159 | Slot 7/ [32 bytes of Red Zone at the bottom of the stack, called |
160 | 'LEFT RedZone'] | |
f3ddd692 | 161 | |
497a1c66 JJ |
162 | The 32 bytes of LEFT red zone at the bottom of the stack can be |
163 | decomposed as such: | |
f3ddd692 JJ |
164 | |
165 | 1/ The first 8 bytes contain a magical asan number that is always | |
166 | 0x41B58AB3. | |
167 | ||
168 | 2/ The following 8 bytes contains a pointer to a string (to be | |
169 | parsed at runtime by the runtime asan library), which format is | |
170 | the following: | |
171 | ||
172 | "<function-name> <space> <num-of-variables-on-the-stack> | |
173 | (<32-bytes-aligned-offset-in-bytes-of-variable> <space> | |
174 | <length-of-var-in-bytes> ){n} " | |
175 | ||
176 | where '(...){n}' means the content inside the parenthesis occurs 'n' | |
177 | times, with 'n' being the number of variables on the stack. | |
ef1b3fda KS |
178 | |
179 | 3/ The following 8 bytes contain the PC of the current function which | |
180 | will be used by the run-time library to print an error message. | |
f3ddd692 | 181 | |
ef1b3fda | 182 | 4/ The following 8 bytes are reserved for internal use by the run-time. |
f3ddd692 | 183 | |
497a1c66 | 184 | The shadow memory for that stack layout is going to look like this: |
f3ddd692 JJ |
185 | |
186 | - content of shadow memory 8 bytes for slot 7: 0xF1F1F1F1. | |
187 | The F1 byte pattern is a magic number called | |
188 | ASAN_STACK_MAGIC_LEFT and is a way for the runtime to know that | |
189 | the memory for that shadow byte is part of a the LEFT red zone | |
190 | intended to seat at the bottom of the variables on the stack. | |
191 | ||
192 | - content of shadow memory 8 bytes for slots 6 and 5: | |
193 | 0xF4F4F400. The F4 byte pattern is a magic number | |
194 | called ASAN_STACK_MAGIC_PARTIAL. It flags the fact that the | |
195 | memory region for this shadow byte is a PARTIAL red zone | |
196 | intended to pad a variable A, so that the slot following | |
197 | {A,padding} is 32 bytes aligned. | |
198 | ||
199 | Note that the fact that the least significant byte of this | |
200 | shadow memory content is 00 means that 8 bytes of its | |
201 | corresponding memory (which corresponds to the memory of | |
202 | variable 'b') is addressable. | |
203 | ||
204 | - content of shadow memory 8 bytes for slot 4: 0xF2F2F2F2. | |
205 | The F2 byte pattern is a magic number called | |
206 | ASAN_STACK_MAGIC_MIDDLE. It flags the fact that the memory | |
207 | region for this shadow byte is a MIDDLE red zone intended to | |
208 | seat between two 32 aligned slots of {variable,padding}. | |
209 | ||
210 | - content of shadow memory 8 bytes for slot 3 and 2: | |
497a1c66 | 211 | 0xF4000000. This represents is the concatenation of |
f3ddd692 JJ |
212 | variable 'a' and the partial red zone following it, like what we |
213 | had for variable 'b'. The least significant 3 bytes being 00 | |
214 | means that the 3 bytes of variable 'a' are addressable. | |
215 | ||
497a1c66 | 216 | - content of shadow memory 8 bytes for slot 1: 0xF3F3F3F3. |
f3ddd692 JJ |
217 | The F3 byte pattern is a magic number called |
218 | ASAN_STACK_MAGIC_RIGHT. It flags the fact that the memory | |
219 | region for this shadow byte is a RIGHT red zone intended to seat | |
220 | at the top of the variables of the stack. | |
221 | ||
497a1c66 JJ |
222 | Note that the real variable layout is done in expand_used_vars in |
223 | cfgexpand.c. As far as Address Sanitizer is concerned, it lays out | |
224 | stack variables as well as the different red zones, emits some | |
225 | prologue code to populate the shadow memory as to poison (mark as | |
226 | non-accessible) the regions of the red zones and mark the regions of | |
227 | stack variables as accessible, and emit some epilogue code to | |
228 | un-poison (mark as accessible) the regions of red zones right before | |
229 | the function exits. | |
8240018b | 230 | |
497a1c66 | 231 | [Protection of global variables] |
8240018b | 232 | |
497a1c66 JJ |
233 | The basic idea is to insert a red zone between two global variables |
234 | and install a constructor function that calls the asan runtime to do | |
235 | the populating of the relevant shadow memory regions at load time. | |
8240018b | 236 | |
497a1c66 JJ |
237 | So the global variables are laid out as to insert a red zone between |
238 | them. The size of the red zones is so that each variable starts on a | |
239 | 32 bytes boundary. | |
8240018b | 240 | |
497a1c66 JJ |
241 | Then a constructor function is installed so that, for each global |
242 | variable, it calls the runtime asan library function | |
243 | __asan_register_globals_with an instance of this type: | |
8240018b JJ |
244 | |
245 | struct __asan_global | |
246 | { | |
247 | // Address of the beginning of the global variable. | |
248 | const void *__beg; | |
249 | ||
250 | // Initial size of the global variable. | |
251 | uptr __size; | |
252 | ||
253 | // Size of the global variable + size of the red zone. This | |
254 | // size is 32 bytes aligned. | |
255 | uptr __size_with_redzone; | |
256 | ||
257 | // Name of the global variable. | |
258 | const void *__name; | |
259 | ||
ef1b3fda KS |
260 | // Name of the module where the global variable is declared. |
261 | const void *__module_name; | |
262 | ||
59b36ecf | 263 | // 1 if it has dynamic initialization, 0 otherwise. |
8240018b | 264 | uptr __has_dynamic_init; |
866e32ad KS |
265 | |
266 | // A pointer to struct that contains source location, could be NULL. | |
267 | __asan_global_source_location *__location; | |
8240018b JJ |
268 | } |
269 | ||
497a1c66 JJ |
270 | A destructor function that calls the runtime asan library function |
271 | _asan_unregister_globals is also installed. */ | |
f3ddd692 | 272 | |
fd960af2 YG |
273 | static unsigned HOST_WIDE_INT asan_shadow_offset_value; |
274 | static bool asan_shadow_offset_computed; | |
275 | ||
276 | /* Sets shadow offset to value in string VAL. */ | |
277 | ||
278 | bool | |
279 | set_asan_shadow_offset (const char *val) | |
280 | { | |
281 | char *endp; | |
282 | ||
283 | errno = 0; | |
284 | #ifdef HAVE_LONG_LONG | |
285 | asan_shadow_offset_value = strtoull (val, &endp, 0); | |
286 | #else | |
287 | asan_shadow_offset_value = strtoul (val, &endp, 0); | |
288 | #endif | |
289 | if (!(*val != '\0' && *endp == '\0' && errno == 0)) | |
290 | return false; | |
291 | ||
292 | asan_shadow_offset_computed = true; | |
293 | ||
294 | return true; | |
295 | } | |
296 | ||
297 | /* Returns Asan shadow offset. */ | |
298 | ||
299 | static unsigned HOST_WIDE_INT | |
300 | asan_shadow_offset () | |
301 | { | |
302 | if (!asan_shadow_offset_computed) | |
303 | { | |
304 | asan_shadow_offset_computed = true; | |
305 | asan_shadow_offset_value = targetm.asan_shadow_offset (); | |
306 | } | |
307 | return asan_shadow_offset_value; | |
308 | } | |
309 | ||
f3ddd692 | 310 | alias_set_type asan_shadow_set = -1; |
37d6f666 | 311 | |
f6d98484 JJ |
312 | /* Pointer types to 1 resp. 2 byte integers in shadow memory. A separate |
313 | alias set is used for all shadow memory accesses. */ | |
314 | static GTY(()) tree shadow_ptr_types[2]; | |
315 | ||
e361382f JJ |
316 | /* Decl for __asan_option_detect_stack_use_after_return. */ |
317 | static GTY(()) tree asan_detect_stack_use_after_return; | |
318 | ||
c62ccb9a YG |
319 | /* Various flags for Asan builtins. */ |
320 | enum asan_check_flags | |
8946c29e | 321 | { |
c62ccb9a YG |
322 | ASAN_CHECK_STORE = 1 << 0, |
323 | ASAN_CHECK_SCALAR_ACCESS = 1 << 1, | |
324 | ASAN_CHECK_NON_ZERO_LEN = 1 << 2, | |
bdea98ca | 325 | ASAN_CHECK_LAST = 1 << 3 |
c62ccb9a | 326 | }; |
8946c29e | 327 | |
bdcbe80c DS |
328 | /* Hashtable support for memory references used by gimple |
329 | statements. */ | |
330 | ||
331 | /* This type represents a reference to a memory region. */ | |
332 | struct asan_mem_ref | |
333 | { | |
688010ba | 334 | /* The expression of the beginning of the memory region. */ |
bdcbe80c DS |
335 | tree start; |
336 | ||
40f9f6bb JJ |
337 | /* The size of the access. */ |
338 | HOST_WIDE_INT access_size; | |
bdcbe80c DS |
339 | }; |
340 | ||
341 | static alloc_pool asan_mem_ref_alloc_pool; | |
342 | ||
343 | /* This creates the alloc pool used to store the instances of | |
344 | asan_mem_ref that are stored in the hash table asan_mem_ref_ht. */ | |
345 | ||
346 | static alloc_pool | |
347 | asan_mem_ref_get_alloc_pool () | |
348 | { | |
349 | if (asan_mem_ref_alloc_pool == NULL) | |
350 | asan_mem_ref_alloc_pool = create_alloc_pool ("asan_mem_ref", | |
351 | sizeof (asan_mem_ref), | |
352 | 10); | |
353 | return asan_mem_ref_alloc_pool; | |
354 | ||
355 | } | |
356 | ||
357 | /* Initializes an instance of asan_mem_ref. */ | |
358 | ||
359 | static void | |
40f9f6bb | 360 | asan_mem_ref_init (asan_mem_ref *ref, tree start, HOST_WIDE_INT access_size) |
bdcbe80c DS |
361 | { |
362 | ref->start = start; | |
363 | ref->access_size = access_size; | |
364 | } | |
365 | ||
366 | /* Allocates memory for an instance of asan_mem_ref into the memory | |
367 | pool returned by asan_mem_ref_get_alloc_pool and initialize it. | |
368 | START is the address of (or the expression pointing to) the | |
369 | beginning of memory reference. ACCESS_SIZE is the size of the | |
370 | access to the referenced memory. */ | |
371 | ||
372 | static asan_mem_ref* | |
40f9f6bb | 373 | asan_mem_ref_new (tree start, HOST_WIDE_INT access_size) |
bdcbe80c DS |
374 | { |
375 | asan_mem_ref *ref = | |
376 | (asan_mem_ref *) pool_alloc (asan_mem_ref_get_alloc_pool ()); | |
377 | ||
378 | asan_mem_ref_init (ref, start, access_size); | |
379 | return ref; | |
380 | } | |
381 | ||
382 | /* This builds and returns a pointer to the end of the memory region | |
383 | that starts at START and of length LEN. */ | |
384 | ||
385 | tree | |
386 | asan_mem_ref_get_end (tree start, tree len) | |
387 | { | |
388 | if (len == NULL_TREE || integer_zerop (len)) | |
389 | return start; | |
390 | ||
a2f581e1 YG |
391 | if (!ptrofftype_p (len)) |
392 | len = convert_to_ptrofftype (len); | |
393 | ||
bdcbe80c DS |
394 | return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (start), start, len); |
395 | } | |
396 | ||
397 | /* Return a tree expression that represents the end of the referenced | |
398 | memory region. Beware that this function can actually build a new | |
399 | tree expression. */ | |
400 | ||
401 | tree | |
402 | asan_mem_ref_get_end (const asan_mem_ref *ref, tree len) | |
403 | { | |
404 | return asan_mem_ref_get_end (ref->start, len); | |
405 | } | |
406 | ||
407 | struct asan_mem_ref_hasher | |
408 | : typed_noop_remove <asan_mem_ref> | |
409 | { | |
410 | typedef asan_mem_ref value_type; | |
411 | typedef asan_mem_ref compare_type; | |
412 | ||
413 | static inline hashval_t hash (const value_type *); | |
414 | static inline bool equal (const value_type *, const compare_type *); | |
415 | }; | |
416 | ||
417 | /* Hash a memory reference. */ | |
418 | ||
419 | inline hashval_t | |
420 | asan_mem_ref_hasher::hash (const asan_mem_ref *mem_ref) | |
421 | { | |
bdea98ca | 422 | return iterative_hash_expr (mem_ref->start, 0); |
bdcbe80c DS |
423 | } |
424 | ||
425 | /* Compare two memory references. We accept the length of either | |
426 | memory references to be NULL_TREE. */ | |
427 | ||
428 | inline bool | |
429 | asan_mem_ref_hasher::equal (const asan_mem_ref *m1, | |
430 | const asan_mem_ref *m2) | |
431 | { | |
bdea98ca | 432 | return operand_equal_p (m1->start, m2->start, 0); |
bdcbe80c DS |
433 | } |
434 | ||
c203e8a7 | 435 | static hash_table<asan_mem_ref_hasher> *asan_mem_ref_ht; |
bdcbe80c DS |
436 | |
437 | /* Returns a reference to the hash table containing memory references. | |
438 | This function ensures that the hash table is created. Note that | |
439 | this hash table is updated by the function | |
440 | update_mem_ref_hash_table. */ | |
441 | ||
c203e8a7 | 442 | static hash_table<asan_mem_ref_hasher> * |
bdcbe80c DS |
443 | get_mem_ref_hash_table () |
444 | { | |
c203e8a7 TS |
445 | if (!asan_mem_ref_ht) |
446 | asan_mem_ref_ht = new hash_table<asan_mem_ref_hasher> (10); | |
bdcbe80c DS |
447 | |
448 | return asan_mem_ref_ht; | |
449 | } | |
450 | ||
451 | /* Clear all entries from the memory references hash table. */ | |
452 | ||
453 | static void | |
454 | empty_mem_ref_hash_table () | |
455 | { | |
c203e8a7 TS |
456 | if (asan_mem_ref_ht) |
457 | asan_mem_ref_ht->empty (); | |
bdcbe80c DS |
458 | } |
459 | ||
460 | /* Free the memory references hash table. */ | |
461 | ||
462 | static void | |
463 | free_mem_ref_resources () | |
464 | { | |
c203e8a7 TS |
465 | delete asan_mem_ref_ht; |
466 | asan_mem_ref_ht = NULL; | |
bdcbe80c DS |
467 | |
468 | if (asan_mem_ref_alloc_pool) | |
469 | { | |
470 | free_alloc_pool (asan_mem_ref_alloc_pool); | |
471 | asan_mem_ref_alloc_pool = NULL; | |
472 | } | |
473 | } | |
474 | ||
475 | /* Return true iff the memory reference REF has been instrumented. */ | |
476 | ||
477 | static bool | |
40f9f6bb | 478 | has_mem_ref_been_instrumented (tree ref, HOST_WIDE_INT access_size) |
bdcbe80c DS |
479 | { |
480 | asan_mem_ref r; | |
481 | asan_mem_ref_init (&r, ref, access_size); | |
482 | ||
bdea98ca MO |
483 | asan_mem_ref *saved_ref = get_mem_ref_hash_table ()->find (&r); |
484 | return saved_ref && saved_ref->access_size >= access_size; | |
bdcbe80c DS |
485 | } |
486 | ||
487 | /* Return true iff the memory reference REF has been instrumented. */ | |
488 | ||
489 | static bool | |
490 | has_mem_ref_been_instrumented (const asan_mem_ref *ref) | |
491 | { | |
492 | return has_mem_ref_been_instrumented (ref->start, ref->access_size); | |
493 | } | |
494 | ||
495 | /* Return true iff access to memory region starting at REF and of | |
496 | length LEN has been instrumented. */ | |
497 | ||
498 | static bool | |
499 | has_mem_ref_been_instrumented (const asan_mem_ref *ref, tree len) | |
500 | { | |
bdea98ca MO |
501 | HOST_WIDE_INT size_in_bytes |
502 | = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; | |
bdcbe80c | 503 | |
bdea98ca MO |
504 | return size_in_bytes != -1 |
505 | && has_mem_ref_been_instrumented (ref->start, size_in_bytes); | |
bdcbe80c DS |
506 | } |
507 | ||
508 | /* Set REF to the memory reference present in a gimple assignment | |
509 | ASSIGNMENT. Return true upon successful completion, false | |
510 | otherwise. */ | |
511 | ||
512 | static bool | |
538dd0b7 | 513 | get_mem_ref_of_assignment (const gassign *assignment, |
bdcbe80c DS |
514 | asan_mem_ref *ref, |
515 | bool *ref_is_store) | |
516 | { | |
517 | gcc_assert (gimple_assign_single_p (assignment)); | |
518 | ||
5d751b0c JJ |
519 | if (gimple_store_p (assignment) |
520 | && !gimple_clobber_p (assignment)) | |
bdcbe80c DS |
521 | { |
522 | ref->start = gimple_assign_lhs (assignment); | |
523 | *ref_is_store = true; | |
524 | } | |
525 | else if (gimple_assign_load_p (assignment)) | |
526 | { | |
527 | ref->start = gimple_assign_rhs1 (assignment); | |
528 | *ref_is_store = false; | |
529 | } | |
530 | else | |
531 | return false; | |
532 | ||
533 | ref->access_size = int_size_in_bytes (TREE_TYPE (ref->start)); | |
534 | return true; | |
535 | } | |
536 | ||
537 | /* Return the memory references contained in a gimple statement | |
538 | representing a builtin call that has to do with memory access. */ | |
539 | ||
540 | static bool | |
538dd0b7 | 541 | get_mem_refs_of_builtin_call (const gcall *call, |
bdcbe80c DS |
542 | asan_mem_ref *src0, |
543 | tree *src0_len, | |
544 | bool *src0_is_store, | |
545 | asan_mem_ref *src1, | |
546 | tree *src1_len, | |
547 | bool *src1_is_store, | |
548 | asan_mem_ref *dst, | |
549 | tree *dst_len, | |
550 | bool *dst_is_store, | |
bdea98ca MO |
551 | bool *dest_is_deref, |
552 | bool *intercepted_p) | |
bdcbe80c DS |
553 | { |
554 | gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); | |
555 | ||
556 | tree callee = gimple_call_fndecl (call); | |
557 | tree source0 = NULL_TREE, source1 = NULL_TREE, | |
558 | dest = NULL_TREE, len = NULL_TREE; | |
559 | bool is_store = true, got_reference_p = false; | |
40f9f6bb | 560 | HOST_WIDE_INT access_size = 1; |
bdcbe80c | 561 | |
bdea98ca MO |
562 | *intercepted_p = asan_intercepted_p ((DECL_FUNCTION_CODE (callee))); |
563 | ||
bdcbe80c DS |
564 | switch (DECL_FUNCTION_CODE (callee)) |
565 | { | |
566 | /* (s, s, n) style memops. */ | |
567 | case BUILT_IN_BCMP: | |
568 | case BUILT_IN_MEMCMP: | |
569 | source0 = gimple_call_arg (call, 0); | |
570 | source1 = gimple_call_arg (call, 1); | |
571 | len = gimple_call_arg (call, 2); | |
572 | break; | |
573 | ||
574 | /* (src, dest, n) style memops. */ | |
575 | case BUILT_IN_BCOPY: | |
576 | source0 = gimple_call_arg (call, 0); | |
577 | dest = gimple_call_arg (call, 1); | |
578 | len = gimple_call_arg (call, 2); | |
579 | break; | |
580 | ||
581 | /* (dest, src, n) style memops. */ | |
582 | case BUILT_IN_MEMCPY: | |
583 | case BUILT_IN_MEMCPY_CHK: | |
584 | case BUILT_IN_MEMMOVE: | |
585 | case BUILT_IN_MEMMOVE_CHK: | |
586 | case BUILT_IN_MEMPCPY: | |
587 | case BUILT_IN_MEMPCPY_CHK: | |
588 | dest = gimple_call_arg (call, 0); | |
589 | source0 = gimple_call_arg (call, 1); | |
590 | len = gimple_call_arg (call, 2); | |
591 | break; | |
592 | ||
593 | /* (dest, n) style memops. */ | |
594 | case BUILT_IN_BZERO: | |
595 | dest = gimple_call_arg (call, 0); | |
596 | len = gimple_call_arg (call, 1); | |
597 | break; | |
598 | ||
599 | /* (dest, x, n) style memops*/ | |
600 | case BUILT_IN_MEMSET: | |
601 | case BUILT_IN_MEMSET_CHK: | |
602 | dest = gimple_call_arg (call, 0); | |
603 | len = gimple_call_arg (call, 2); | |
604 | break; | |
605 | ||
606 | case BUILT_IN_STRLEN: | |
607 | source0 = gimple_call_arg (call, 0); | |
608 | len = gimple_call_lhs (call); | |
609 | break ; | |
610 | ||
611 | /* And now the __atomic* and __sync builtins. | |
612 | These are handled differently from the classical memory memory | |
613 | access builtins above. */ | |
614 | ||
615 | case BUILT_IN_ATOMIC_LOAD_1: | |
616 | case BUILT_IN_ATOMIC_LOAD_2: | |
617 | case BUILT_IN_ATOMIC_LOAD_4: | |
618 | case BUILT_IN_ATOMIC_LOAD_8: | |
619 | case BUILT_IN_ATOMIC_LOAD_16: | |
620 | is_store = false; | |
621 | /* fall through. */ | |
622 | ||
623 | case BUILT_IN_SYNC_FETCH_AND_ADD_1: | |
624 | case BUILT_IN_SYNC_FETCH_AND_ADD_2: | |
625 | case BUILT_IN_SYNC_FETCH_AND_ADD_4: | |
626 | case BUILT_IN_SYNC_FETCH_AND_ADD_8: | |
627 | case BUILT_IN_SYNC_FETCH_AND_ADD_16: | |
628 | ||
629 | case BUILT_IN_SYNC_FETCH_AND_SUB_1: | |
630 | case BUILT_IN_SYNC_FETCH_AND_SUB_2: | |
631 | case BUILT_IN_SYNC_FETCH_AND_SUB_4: | |
632 | case BUILT_IN_SYNC_FETCH_AND_SUB_8: | |
633 | case BUILT_IN_SYNC_FETCH_AND_SUB_16: | |
634 | ||
635 | case BUILT_IN_SYNC_FETCH_AND_OR_1: | |
636 | case BUILT_IN_SYNC_FETCH_AND_OR_2: | |
637 | case BUILT_IN_SYNC_FETCH_AND_OR_4: | |
638 | case BUILT_IN_SYNC_FETCH_AND_OR_8: | |
639 | case BUILT_IN_SYNC_FETCH_AND_OR_16: | |
640 | ||
641 | case BUILT_IN_SYNC_FETCH_AND_AND_1: | |
642 | case BUILT_IN_SYNC_FETCH_AND_AND_2: | |
643 | case BUILT_IN_SYNC_FETCH_AND_AND_4: | |
644 | case BUILT_IN_SYNC_FETCH_AND_AND_8: | |
645 | case BUILT_IN_SYNC_FETCH_AND_AND_16: | |
646 | ||
647 | case BUILT_IN_SYNC_FETCH_AND_XOR_1: | |
648 | case BUILT_IN_SYNC_FETCH_AND_XOR_2: | |
649 | case BUILT_IN_SYNC_FETCH_AND_XOR_4: | |
650 | case BUILT_IN_SYNC_FETCH_AND_XOR_8: | |
651 | case BUILT_IN_SYNC_FETCH_AND_XOR_16: | |
652 | ||
653 | case BUILT_IN_SYNC_FETCH_AND_NAND_1: | |
654 | case BUILT_IN_SYNC_FETCH_AND_NAND_2: | |
655 | case BUILT_IN_SYNC_FETCH_AND_NAND_4: | |
656 | case BUILT_IN_SYNC_FETCH_AND_NAND_8: | |
657 | ||
658 | case BUILT_IN_SYNC_ADD_AND_FETCH_1: | |
659 | case BUILT_IN_SYNC_ADD_AND_FETCH_2: | |
660 | case BUILT_IN_SYNC_ADD_AND_FETCH_4: | |
661 | case BUILT_IN_SYNC_ADD_AND_FETCH_8: | |
662 | case BUILT_IN_SYNC_ADD_AND_FETCH_16: | |
663 | ||
664 | case BUILT_IN_SYNC_SUB_AND_FETCH_1: | |
665 | case BUILT_IN_SYNC_SUB_AND_FETCH_2: | |
666 | case BUILT_IN_SYNC_SUB_AND_FETCH_4: | |
667 | case BUILT_IN_SYNC_SUB_AND_FETCH_8: | |
668 | case BUILT_IN_SYNC_SUB_AND_FETCH_16: | |
669 | ||
670 | case BUILT_IN_SYNC_OR_AND_FETCH_1: | |
671 | case BUILT_IN_SYNC_OR_AND_FETCH_2: | |
672 | case BUILT_IN_SYNC_OR_AND_FETCH_4: | |
673 | case BUILT_IN_SYNC_OR_AND_FETCH_8: | |
674 | case BUILT_IN_SYNC_OR_AND_FETCH_16: | |
675 | ||
676 | case BUILT_IN_SYNC_AND_AND_FETCH_1: | |
677 | case BUILT_IN_SYNC_AND_AND_FETCH_2: | |
678 | case BUILT_IN_SYNC_AND_AND_FETCH_4: | |
679 | case BUILT_IN_SYNC_AND_AND_FETCH_8: | |
680 | case BUILT_IN_SYNC_AND_AND_FETCH_16: | |
681 | ||
682 | case BUILT_IN_SYNC_XOR_AND_FETCH_1: | |
683 | case BUILT_IN_SYNC_XOR_AND_FETCH_2: | |
684 | case BUILT_IN_SYNC_XOR_AND_FETCH_4: | |
685 | case BUILT_IN_SYNC_XOR_AND_FETCH_8: | |
686 | case BUILT_IN_SYNC_XOR_AND_FETCH_16: | |
687 | ||
688 | case BUILT_IN_SYNC_NAND_AND_FETCH_1: | |
689 | case BUILT_IN_SYNC_NAND_AND_FETCH_2: | |
690 | case BUILT_IN_SYNC_NAND_AND_FETCH_4: | |
691 | case BUILT_IN_SYNC_NAND_AND_FETCH_8: | |
692 | ||
693 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1: | |
694 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2: | |
695 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4: | |
696 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8: | |
697 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16: | |
698 | ||
699 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1: | |
700 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2: | |
701 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4: | |
702 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8: | |
703 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16: | |
704 | ||
705 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1: | |
706 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2: | |
707 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4: | |
708 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8: | |
709 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16: | |
710 | ||
711 | case BUILT_IN_SYNC_LOCK_RELEASE_1: | |
712 | case BUILT_IN_SYNC_LOCK_RELEASE_2: | |
713 | case BUILT_IN_SYNC_LOCK_RELEASE_4: | |
714 | case BUILT_IN_SYNC_LOCK_RELEASE_8: | |
715 | case BUILT_IN_SYNC_LOCK_RELEASE_16: | |
716 | ||
717 | case BUILT_IN_ATOMIC_EXCHANGE_1: | |
718 | case BUILT_IN_ATOMIC_EXCHANGE_2: | |
719 | case BUILT_IN_ATOMIC_EXCHANGE_4: | |
720 | case BUILT_IN_ATOMIC_EXCHANGE_8: | |
721 | case BUILT_IN_ATOMIC_EXCHANGE_16: | |
722 | ||
723 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
724 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
725 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
726 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
727 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
728 | ||
729 | case BUILT_IN_ATOMIC_STORE_1: | |
730 | case BUILT_IN_ATOMIC_STORE_2: | |
731 | case BUILT_IN_ATOMIC_STORE_4: | |
732 | case BUILT_IN_ATOMIC_STORE_8: | |
733 | case BUILT_IN_ATOMIC_STORE_16: | |
734 | ||
735 | case BUILT_IN_ATOMIC_ADD_FETCH_1: | |
736 | case BUILT_IN_ATOMIC_ADD_FETCH_2: | |
737 | case BUILT_IN_ATOMIC_ADD_FETCH_4: | |
738 | case BUILT_IN_ATOMIC_ADD_FETCH_8: | |
739 | case BUILT_IN_ATOMIC_ADD_FETCH_16: | |
740 | ||
741 | case BUILT_IN_ATOMIC_SUB_FETCH_1: | |
742 | case BUILT_IN_ATOMIC_SUB_FETCH_2: | |
743 | case BUILT_IN_ATOMIC_SUB_FETCH_4: | |
744 | case BUILT_IN_ATOMIC_SUB_FETCH_8: | |
745 | case BUILT_IN_ATOMIC_SUB_FETCH_16: | |
746 | ||
747 | case BUILT_IN_ATOMIC_AND_FETCH_1: | |
748 | case BUILT_IN_ATOMIC_AND_FETCH_2: | |
749 | case BUILT_IN_ATOMIC_AND_FETCH_4: | |
750 | case BUILT_IN_ATOMIC_AND_FETCH_8: | |
751 | case BUILT_IN_ATOMIC_AND_FETCH_16: | |
752 | ||
753 | case BUILT_IN_ATOMIC_NAND_FETCH_1: | |
754 | case BUILT_IN_ATOMIC_NAND_FETCH_2: | |
755 | case BUILT_IN_ATOMIC_NAND_FETCH_4: | |
756 | case BUILT_IN_ATOMIC_NAND_FETCH_8: | |
757 | case BUILT_IN_ATOMIC_NAND_FETCH_16: | |
758 | ||
759 | case BUILT_IN_ATOMIC_XOR_FETCH_1: | |
760 | case BUILT_IN_ATOMIC_XOR_FETCH_2: | |
761 | case BUILT_IN_ATOMIC_XOR_FETCH_4: | |
762 | case BUILT_IN_ATOMIC_XOR_FETCH_8: | |
763 | case BUILT_IN_ATOMIC_XOR_FETCH_16: | |
764 | ||
765 | case BUILT_IN_ATOMIC_OR_FETCH_1: | |
766 | case BUILT_IN_ATOMIC_OR_FETCH_2: | |
767 | case BUILT_IN_ATOMIC_OR_FETCH_4: | |
768 | case BUILT_IN_ATOMIC_OR_FETCH_8: | |
769 | case BUILT_IN_ATOMIC_OR_FETCH_16: | |
770 | ||
771 | case BUILT_IN_ATOMIC_FETCH_ADD_1: | |
772 | case BUILT_IN_ATOMIC_FETCH_ADD_2: | |
773 | case BUILT_IN_ATOMIC_FETCH_ADD_4: | |
774 | case BUILT_IN_ATOMIC_FETCH_ADD_8: | |
775 | case BUILT_IN_ATOMIC_FETCH_ADD_16: | |
776 | ||
777 | case BUILT_IN_ATOMIC_FETCH_SUB_1: | |
778 | case BUILT_IN_ATOMIC_FETCH_SUB_2: | |
779 | case BUILT_IN_ATOMIC_FETCH_SUB_4: | |
780 | case BUILT_IN_ATOMIC_FETCH_SUB_8: | |
781 | case BUILT_IN_ATOMIC_FETCH_SUB_16: | |
782 | ||
783 | case BUILT_IN_ATOMIC_FETCH_AND_1: | |
784 | case BUILT_IN_ATOMIC_FETCH_AND_2: | |
785 | case BUILT_IN_ATOMIC_FETCH_AND_4: | |
786 | case BUILT_IN_ATOMIC_FETCH_AND_8: | |
787 | case BUILT_IN_ATOMIC_FETCH_AND_16: | |
788 | ||
789 | case BUILT_IN_ATOMIC_FETCH_NAND_1: | |
790 | case BUILT_IN_ATOMIC_FETCH_NAND_2: | |
791 | case BUILT_IN_ATOMIC_FETCH_NAND_4: | |
792 | case BUILT_IN_ATOMIC_FETCH_NAND_8: | |
793 | case BUILT_IN_ATOMIC_FETCH_NAND_16: | |
794 | ||
795 | case BUILT_IN_ATOMIC_FETCH_XOR_1: | |
796 | case BUILT_IN_ATOMIC_FETCH_XOR_2: | |
797 | case BUILT_IN_ATOMIC_FETCH_XOR_4: | |
798 | case BUILT_IN_ATOMIC_FETCH_XOR_8: | |
799 | case BUILT_IN_ATOMIC_FETCH_XOR_16: | |
800 | ||
801 | case BUILT_IN_ATOMIC_FETCH_OR_1: | |
802 | case BUILT_IN_ATOMIC_FETCH_OR_2: | |
803 | case BUILT_IN_ATOMIC_FETCH_OR_4: | |
804 | case BUILT_IN_ATOMIC_FETCH_OR_8: | |
805 | case BUILT_IN_ATOMIC_FETCH_OR_16: | |
806 | { | |
807 | dest = gimple_call_arg (call, 0); | |
808 | /* DEST represents the address of a memory location. | |
809 | instrument_derefs wants the memory location, so lets | |
810 | dereference the address DEST before handing it to | |
811 | instrument_derefs. */ | |
812 | if (TREE_CODE (dest) == ADDR_EXPR) | |
813 | dest = TREE_OPERAND (dest, 0); | |
77e83307 | 814 | else if (TREE_CODE (dest) == SSA_NAME || TREE_CODE (dest) == INTEGER_CST) |
bdcbe80c DS |
815 | dest = build2 (MEM_REF, TREE_TYPE (TREE_TYPE (dest)), |
816 | dest, build_int_cst (TREE_TYPE (dest), 0)); | |
817 | else | |
818 | gcc_unreachable (); | |
819 | ||
820 | access_size = int_size_in_bytes (TREE_TYPE (dest)); | |
821 | } | |
822 | ||
823 | default: | |
824 | /* The other builtins memory access are not instrumented in this | |
825 | function because they either don't have any length parameter, | |
826 | or their length parameter is just a limit. */ | |
827 | break; | |
828 | } | |
829 | ||
830 | if (len != NULL_TREE) | |
831 | { | |
832 | if (source0 != NULL_TREE) | |
833 | { | |
834 | src0->start = source0; | |
835 | src0->access_size = access_size; | |
836 | *src0_len = len; | |
837 | *src0_is_store = false; | |
838 | } | |
839 | ||
840 | if (source1 != NULL_TREE) | |
841 | { | |
842 | src1->start = source1; | |
843 | src1->access_size = access_size; | |
844 | *src1_len = len; | |
845 | *src1_is_store = false; | |
846 | } | |
847 | ||
848 | if (dest != NULL_TREE) | |
849 | { | |
850 | dst->start = dest; | |
851 | dst->access_size = access_size; | |
852 | *dst_len = len; | |
853 | *dst_is_store = true; | |
854 | } | |
855 | ||
856 | got_reference_p = true; | |
857 | } | |
b41288b3 JJ |
858 | else if (dest) |
859 | { | |
860 | dst->start = dest; | |
861 | dst->access_size = access_size; | |
862 | *dst_len = NULL_TREE; | |
863 | *dst_is_store = is_store; | |
864 | *dest_is_deref = true; | |
865 | got_reference_p = true; | |
866 | } | |
bdcbe80c | 867 | |
b41288b3 | 868 | return got_reference_p; |
bdcbe80c DS |
869 | } |
870 | ||
871 | /* Return true iff a given gimple statement has been instrumented. | |
872 | Note that the statement is "defined" by the memory references it | |
873 | contains. */ | |
874 | ||
875 | static bool | |
876 | has_stmt_been_instrumented_p (gimple stmt) | |
877 | { | |
878 | if (gimple_assign_single_p (stmt)) | |
879 | { | |
880 | bool r_is_store; | |
881 | asan_mem_ref r; | |
882 | asan_mem_ref_init (&r, NULL, 1); | |
883 | ||
538dd0b7 DM |
884 | if (get_mem_ref_of_assignment (as_a <gassign *> (stmt), &r, |
885 | &r_is_store)) | |
bdcbe80c DS |
886 | return has_mem_ref_been_instrumented (&r); |
887 | } | |
888 | else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) | |
889 | { | |
890 | asan_mem_ref src0, src1, dest; | |
891 | asan_mem_ref_init (&src0, NULL, 1); | |
892 | asan_mem_ref_init (&src1, NULL, 1); | |
893 | asan_mem_ref_init (&dest, NULL, 1); | |
894 | ||
895 | tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; | |
896 | bool src0_is_store = false, src1_is_store = false, | |
bdea98ca | 897 | dest_is_store = false, dest_is_deref = false, intercepted_p = true; |
538dd0b7 | 898 | if (get_mem_refs_of_builtin_call (as_a <gcall *> (stmt), |
bdcbe80c DS |
899 | &src0, &src0_len, &src0_is_store, |
900 | &src1, &src1_len, &src1_is_store, | |
901 | &dest, &dest_len, &dest_is_store, | |
bdea98ca | 902 | &dest_is_deref, &intercepted_p)) |
bdcbe80c DS |
903 | { |
904 | if (src0.start != NULL_TREE | |
905 | && !has_mem_ref_been_instrumented (&src0, src0_len)) | |
906 | return false; | |
907 | ||
908 | if (src1.start != NULL_TREE | |
909 | && !has_mem_ref_been_instrumented (&src1, src1_len)) | |
910 | return false; | |
911 | ||
912 | if (dest.start != NULL_TREE | |
913 | && !has_mem_ref_been_instrumented (&dest, dest_len)) | |
914 | return false; | |
915 | ||
916 | return true; | |
917 | } | |
918 | } | |
919 | return false; | |
920 | } | |
921 | ||
922 | /* Insert a memory reference into the hash table. */ | |
923 | ||
924 | static void | |
40f9f6bb | 925 | update_mem_ref_hash_table (tree ref, HOST_WIDE_INT access_size) |
bdcbe80c | 926 | { |
c203e8a7 | 927 | hash_table<asan_mem_ref_hasher> *ht = get_mem_ref_hash_table (); |
bdcbe80c DS |
928 | |
929 | asan_mem_ref r; | |
930 | asan_mem_ref_init (&r, ref, access_size); | |
931 | ||
c203e8a7 | 932 | asan_mem_ref **slot = ht->find_slot (&r, INSERT); |
bdea98ca | 933 | if (*slot == NULL || (*slot)->access_size < access_size) |
bdcbe80c DS |
934 | *slot = asan_mem_ref_new (ref, access_size); |
935 | } | |
936 | ||
94fce891 JJ |
937 | /* Initialize shadow_ptr_types array. */ |
938 | ||
939 | static void | |
940 | asan_init_shadow_ptr_types (void) | |
941 | { | |
942 | asan_shadow_set = new_alias_set (); | |
943 | shadow_ptr_types[0] = build_distinct_type_copy (signed_char_type_node); | |
944 | TYPE_ALIAS_SET (shadow_ptr_types[0]) = asan_shadow_set; | |
945 | shadow_ptr_types[0] = build_pointer_type (shadow_ptr_types[0]); | |
946 | shadow_ptr_types[1] = build_distinct_type_copy (short_integer_type_node); | |
947 | TYPE_ALIAS_SET (shadow_ptr_types[1]) = asan_shadow_set; | |
948 | shadow_ptr_types[1] = build_pointer_type (shadow_ptr_types[1]); | |
949 | initialize_sanitizer_builtins (); | |
950 | } | |
951 | ||
11a877b3 | 952 | /* Create ADDR_EXPR of STRING_CST with the PP pretty printer text. */ |
8240018b JJ |
953 | |
954 | static tree | |
11a877b3 | 955 | asan_pp_string (pretty_printer *pp) |
8240018b | 956 | { |
11a877b3 | 957 | const char *buf = pp_formatted_text (pp); |
8240018b JJ |
958 | size_t len = strlen (buf); |
959 | tree ret = build_string (len + 1, buf); | |
960 | TREE_TYPE (ret) | |
94fce891 JJ |
961 | = build_array_type (TREE_TYPE (shadow_ptr_types[0]), |
962 | build_index_type (size_int (len))); | |
8240018b JJ |
963 | TREE_READONLY (ret) = 1; |
964 | TREE_STATIC (ret) = 1; | |
94fce891 | 965 | return build1 (ADDR_EXPR, shadow_ptr_types[0], ret); |
8240018b JJ |
966 | } |
967 | ||
f3ddd692 JJ |
968 | /* Return a CONST_INT representing 4 subsequent shadow memory bytes. */ |
969 | ||
970 | static rtx | |
971 | asan_shadow_cst (unsigned char shadow_bytes[4]) | |
972 | { | |
973 | int i; | |
974 | unsigned HOST_WIDE_INT val = 0; | |
975 | gcc_assert (WORDS_BIG_ENDIAN == BYTES_BIG_ENDIAN); | |
976 | for (i = 0; i < 4; i++) | |
977 | val |= (unsigned HOST_WIDE_INT) shadow_bytes[BYTES_BIG_ENDIAN ? 3 - i : i] | |
978 | << (BITS_PER_UNIT * i); | |
dcad1dd3 | 979 | return gen_int_mode (val, SImode); |
f3ddd692 JJ |
980 | } |
981 | ||
aeb7e7c1 JJ |
982 | /* Clear shadow memory at SHADOW_MEM, LEN bytes. Can't call a library call here |
983 | though. */ | |
984 | ||
985 | static void | |
986 | asan_clear_shadow (rtx shadow_mem, HOST_WIDE_INT len) | |
987 | { | |
3a965f61 DM |
988 | rtx_insn *insn, *insns, *jump; |
989 | rtx_code_label *top_label; | |
990 | rtx end, addr, tmp; | |
aeb7e7c1 JJ |
991 | |
992 | start_sequence (); | |
993 | clear_storage (shadow_mem, GEN_INT (len), BLOCK_OP_NORMAL); | |
994 | insns = get_insns (); | |
995 | end_sequence (); | |
996 | for (insn = insns; insn; insn = NEXT_INSN (insn)) | |
997 | if (CALL_P (insn)) | |
998 | break; | |
999 | if (insn == NULL_RTX) | |
1000 | { | |
1001 | emit_insn (insns); | |
1002 | return; | |
1003 | } | |
1004 | ||
1005 | gcc_assert ((len & 3) == 0); | |
1006 | top_label = gen_label_rtx (); | |
57d4d653 | 1007 | addr = copy_to_mode_reg (Pmode, XEXP (shadow_mem, 0)); |
aeb7e7c1 JJ |
1008 | shadow_mem = adjust_automodify_address (shadow_mem, SImode, addr, 0); |
1009 | end = force_reg (Pmode, plus_constant (Pmode, addr, len)); | |
1010 | emit_label (top_label); | |
1011 | ||
1012 | emit_move_insn (shadow_mem, const0_rtx); | |
2f1cd2eb | 1013 | tmp = expand_simple_binop (Pmode, PLUS, addr, gen_int_mode (4, Pmode), addr, |
c62ccb9a | 1014 | true, OPTAB_LIB_WIDEN); |
aeb7e7c1 JJ |
1015 | if (tmp != addr) |
1016 | emit_move_insn (addr, tmp); | |
1017 | emit_cmp_and_jump_insns (addr, end, LT, NULL_RTX, Pmode, true, top_label); | |
1018 | jump = get_last_insn (); | |
1019 | gcc_assert (JUMP_P (jump)); | |
e5af9ddd | 1020 | add_int_reg_note (jump, REG_BR_PROB, REG_BR_PROB_BASE * 80 / 100); |
aeb7e7c1 JJ |
1021 | } |
1022 | ||
ef1b3fda KS |
1023 | void |
1024 | asan_function_start (void) | |
1025 | { | |
1026 | section *fnsec = function_section (current_function_decl); | |
1027 | switch_to_section (fnsec); | |
1028 | ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LASANPC", | |
c62ccb9a | 1029 | current_function_funcdef_no); |
ef1b3fda KS |
1030 | } |
1031 | ||
f3ddd692 JJ |
1032 | /* Insert code to protect stack vars. The prologue sequence should be emitted |
1033 | directly, epilogue sequence returned. BASE is the register holding the | |
1034 | stack base, against which OFFSETS array offsets are relative to, OFFSETS | |
1035 | array contains pairs of offsets in reverse order, always the end offset | |
1036 | of some gap that needs protection followed by starting offset, | |
1037 | and DECLS is an array of representative decls for each var partition. | |
1038 | LENGTH is the length of the OFFSETS array, DECLS array is LENGTH / 2 - 1 | |
1039 | elements long (OFFSETS include gap before the first variable as well | |
e361382f JJ |
1040 | as gaps after each stack variable). PBASE is, if non-NULL, some pseudo |
1041 | register which stack vars DECL_RTLs are based on. Either BASE should be | |
1042 | assigned to PBASE, when not doing use after return protection, or | |
1043 | corresponding address based on __asan_stack_malloc* return value. */ | |
f3ddd692 | 1044 | |
3a4abd2f | 1045 | rtx_insn * |
e361382f JJ |
1046 | asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb, |
1047 | HOST_WIDE_INT *offsets, tree *decls, int length) | |
f3ddd692 | 1048 | { |
19f8b229 TS |
1049 | rtx shadow_base, shadow_mem, ret, mem, orig_base; |
1050 | rtx_code_label *lab; | |
3a4abd2f | 1051 | rtx_insn *insns; |
ef1b3fda | 1052 | char buf[30]; |
f3ddd692 | 1053 | unsigned char shadow_bytes[4]; |
e361382f JJ |
1054 | HOST_WIDE_INT base_offset = offsets[length - 1]; |
1055 | HOST_WIDE_INT base_align_bias = 0, offset, prev_offset; | |
1056 | HOST_WIDE_INT asan_frame_size = offsets[0] - base_offset; | |
f3ddd692 JJ |
1057 | HOST_WIDE_INT last_offset, last_size; |
1058 | int l; | |
1059 | unsigned char cur_shadow_byte = ASAN_STACK_MAGIC_LEFT; | |
ef1b3fda | 1060 | tree str_cst, decl, id; |
e361382f | 1061 | int use_after_return_class = -1; |
f3ddd692 | 1062 | |
94fce891 JJ |
1063 | if (shadow_ptr_types[0] == NULL_TREE) |
1064 | asan_init_shadow_ptr_types (); | |
1065 | ||
f3ddd692 | 1066 | /* First of all, prepare the description string. */ |
11a877b3 | 1067 | pretty_printer asan_pp; |
da6ca2b5 | 1068 | |
8240018b JJ |
1069 | pp_decimal_int (&asan_pp, length / 2 - 1); |
1070 | pp_space (&asan_pp); | |
f3ddd692 JJ |
1071 | for (l = length - 2; l; l -= 2) |
1072 | { | |
1073 | tree decl = decls[l / 2 - 1]; | |
8240018b JJ |
1074 | pp_wide_integer (&asan_pp, offsets[l] - base_offset); |
1075 | pp_space (&asan_pp); | |
1076 | pp_wide_integer (&asan_pp, offsets[l - 1] - offsets[l]); | |
1077 | pp_space (&asan_pp); | |
f3ddd692 JJ |
1078 | if (DECL_P (decl) && DECL_NAME (decl)) |
1079 | { | |
8240018b JJ |
1080 | pp_decimal_int (&asan_pp, IDENTIFIER_LENGTH (DECL_NAME (decl))); |
1081 | pp_space (&asan_pp); | |
b066401f | 1082 | pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
f3ddd692 JJ |
1083 | } |
1084 | else | |
8240018b JJ |
1085 | pp_string (&asan_pp, "9 <unknown>"); |
1086 | pp_space (&asan_pp); | |
f3ddd692 | 1087 | } |
11a877b3 | 1088 | str_cst = asan_pp_string (&asan_pp); |
f3ddd692 JJ |
1089 | |
1090 | /* Emit the prologue sequence. */ | |
b5ebc991 MO |
1091 | if (asan_frame_size > 32 && asan_frame_size <= 65536 && pbase |
1092 | && ASAN_USE_AFTER_RETURN) | |
e361382f JJ |
1093 | { |
1094 | use_after_return_class = floor_log2 (asan_frame_size - 1) - 5; | |
1095 | /* __asan_stack_malloc_N guarantees alignment | |
c62ccb9a | 1096 | N < 6 ? (64 << N) : 4096 bytes. */ |
e361382f JJ |
1097 | if (alignb > (use_after_return_class < 6 |
1098 | ? (64U << use_after_return_class) : 4096U)) | |
1099 | use_after_return_class = -1; | |
1100 | else if (alignb > ASAN_RED_ZONE_SIZE && (asan_frame_size & (alignb - 1))) | |
1101 | base_align_bias = ((asan_frame_size + alignb - 1) | |
1102 | & ~(alignb - HOST_WIDE_INT_1)) - asan_frame_size; | |
1103 | } | |
e5dcd695 LZ |
1104 | /* Align base if target is STRICT_ALIGNMENT. */ |
1105 | if (STRICT_ALIGNMENT) | |
1106 | base = expand_binop (Pmode, and_optab, base, | |
1107 | gen_int_mode (-((GET_MODE_ALIGNMENT (SImode) | |
1108 | << ASAN_SHADOW_SHIFT) | |
1109 | / BITS_PER_UNIT), Pmode), NULL_RTX, | |
1110 | 1, OPTAB_DIRECT); | |
1111 | ||
e361382f JJ |
1112 | if (use_after_return_class == -1 && pbase) |
1113 | emit_move_insn (pbase, base); | |
e5dcd695 | 1114 | |
2f1cd2eb | 1115 | base = expand_binop (Pmode, add_optab, base, |
e361382f | 1116 | gen_int_mode (base_offset - base_align_bias, Pmode), |
f3ddd692 | 1117 | NULL_RTX, 1, OPTAB_DIRECT); |
e361382f JJ |
1118 | orig_base = NULL_RTX; |
1119 | if (use_after_return_class != -1) | |
1120 | { | |
1121 | if (asan_detect_stack_use_after_return == NULL_TREE) | |
1122 | { | |
1123 | id = get_identifier ("__asan_option_detect_stack_use_after_return"); | |
1124 | decl = build_decl (BUILTINS_LOCATION, VAR_DECL, id, | |
1125 | integer_type_node); | |
1126 | SET_DECL_ASSEMBLER_NAME (decl, id); | |
1127 | TREE_ADDRESSABLE (decl) = 1; | |
1128 | DECL_ARTIFICIAL (decl) = 1; | |
1129 | DECL_IGNORED_P (decl) = 1; | |
1130 | DECL_EXTERNAL (decl) = 1; | |
1131 | TREE_STATIC (decl) = 1; | |
1132 | TREE_PUBLIC (decl) = 1; | |
1133 | TREE_USED (decl) = 1; | |
1134 | asan_detect_stack_use_after_return = decl; | |
1135 | } | |
1136 | orig_base = gen_reg_rtx (Pmode); | |
1137 | emit_move_insn (orig_base, base); | |
1138 | ret = expand_normal (asan_detect_stack_use_after_return); | |
1139 | lab = gen_label_rtx (); | |
1140 | int very_likely = REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1); | |
1141 | emit_cmp_and_jump_insns (ret, const0_rtx, EQ, NULL_RTX, | |
1142 | VOIDmode, 0, lab, very_likely); | |
1143 | snprintf (buf, sizeof buf, "__asan_stack_malloc_%d", | |
1144 | use_after_return_class); | |
1145 | ret = init_one_libfunc (buf); | |
1146 | rtx addr = convert_memory_address (ptr_mode, base); | |
1147 | ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode, 2, | |
1148 | GEN_INT (asan_frame_size | |
1149 | + base_align_bias), | |
1150 | TYPE_MODE (pointer_sized_int_node), | |
1151 | addr, ptr_mode); | |
1152 | ret = convert_memory_address (Pmode, ret); | |
1153 | emit_move_insn (base, ret); | |
1154 | emit_label (lab); | |
1155 | emit_move_insn (pbase, expand_binop (Pmode, add_optab, base, | |
1156 | gen_int_mode (base_align_bias | |
1157 | - base_offset, Pmode), | |
1158 | NULL_RTX, 1, OPTAB_DIRECT)); | |
1159 | } | |
f3ddd692 | 1160 | mem = gen_rtx_MEM (ptr_mode, base); |
e361382f | 1161 | mem = adjust_address (mem, VOIDmode, base_align_bias); |
69db2d57 | 1162 | emit_move_insn (mem, gen_int_mode (ASAN_STACK_FRAME_MAGIC, ptr_mode)); |
f3ddd692 JJ |
1163 | mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode)); |
1164 | emit_move_insn (mem, expand_normal (str_cst)); | |
ef1b3fda KS |
1165 | mem = adjust_address (mem, VOIDmode, GET_MODE_SIZE (ptr_mode)); |
1166 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASANPC", current_function_funcdef_no); | |
1167 | id = get_identifier (buf); | |
1168 | decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl), | |
c62ccb9a | 1169 | VAR_DECL, id, char_type_node); |
ef1b3fda KS |
1170 | SET_DECL_ASSEMBLER_NAME (decl, id); |
1171 | TREE_ADDRESSABLE (decl) = 1; | |
1172 | TREE_READONLY (decl) = 1; | |
1173 | DECL_ARTIFICIAL (decl) = 1; | |
1174 | DECL_IGNORED_P (decl) = 1; | |
1175 | TREE_STATIC (decl) = 1; | |
1176 | TREE_PUBLIC (decl) = 0; | |
1177 | TREE_USED (decl) = 1; | |
8c8b21e4 JJ |
1178 | DECL_INITIAL (decl) = decl; |
1179 | TREE_ASM_WRITTEN (decl) = 1; | |
1180 | TREE_ASM_WRITTEN (id) = 1; | |
ef1b3fda | 1181 | emit_move_insn (mem, expand_normal (build_fold_addr_expr (decl))); |
f3ddd692 JJ |
1182 | shadow_base = expand_binop (Pmode, lshr_optab, base, |
1183 | GEN_INT (ASAN_SHADOW_SHIFT), | |
1184 | NULL_RTX, 1, OPTAB_DIRECT); | |
e361382f JJ |
1185 | shadow_base |
1186 | = plus_constant (Pmode, shadow_base, | |
fd960af2 | 1187 | asan_shadow_offset () |
e361382f | 1188 | + (base_align_bias >> ASAN_SHADOW_SHIFT)); |
f3ddd692 JJ |
1189 | gcc_assert (asan_shadow_set != -1 |
1190 | && (ASAN_RED_ZONE_SIZE >> ASAN_SHADOW_SHIFT) == 4); | |
1191 | shadow_mem = gen_rtx_MEM (SImode, shadow_base); | |
1192 | set_mem_alias_set (shadow_mem, asan_shadow_set); | |
e5dcd695 LZ |
1193 | if (STRICT_ALIGNMENT) |
1194 | set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode))); | |
f3ddd692 JJ |
1195 | prev_offset = base_offset; |
1196 | for (l = length; l; l -= 2) | |
1197 | { | |
1198 | if (l == 2) | |
1199 | cur_shadow_byte = ASAN_STACK_MAGIC_RIGHT; | |
1200 | offset = offsets[l - 1]; | |
1201 | if ((offset - base_offset) & (ASAN_RED_ZONE_SIZE - 1)) | |
1202 | { | |
1203 | int i; | |
1204 | HOST_WIDE_INT aoff | |
1205 | = base_offset + ((offset - base_offset) | |
1206 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); | |
1207 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1208 | (aoff - prev_offset) | |
1209 | >> ASAN_SHADOW_SHIFT); | |
1210 | prev_offset = aoff; | |
1211 | for (i = 0; i < 4; i++, aoff += (1 << ASAN_SHADOW_SHIFT)) | |
1212 | if (aoff < offset) | |
1213 | { | |
1214 | if (aoff < offset - (1 << ASAN_SHADOW_SHIFT) + 1) | |
1215 | shadow_bytes[i] = 0; | |
1216 | else | |
1217 | shadow_bytes[i] = offset - aoff; | |
1218 | } | |
1219 | else | |
1220 | shadow_bytes[i] = ASAN_STACK_MAGIC_PARTIAL; | |
1221 | emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes)); | |
1222 | offset = aoff; | |
1223 | } | |
1224 | while (offset <= offsets[l - 2] - ASAN_RED_ZONE_SIZE) | |
1225 | { | |
1226 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1227 | (offset - prev_offset) | |
1228 | >> ASAN_SHADOW_SHIFT); | |
1229 | prev_offset = offset; | |
1230 | memset (shadow_bytes, cur_shadow_byte, 4); | |
1231 | emit_move_insn (shadow_mem, asan_shadow_cst (shadow_bytes)); | |
1232 | offset += ASAN_RED_ZONE_SIZE; | |
1233 | } | |
1234 | cur_shadow_byte = ASAN_STACK_MAGIC_MIDDLE; | |
1235 | } | |
1236 | do_pending_stack_adjust (); | |
1237 | ||
1238 | /* Construct epilogue sequence. */ | |
1239 | start_sequence (); | |
1240 | ||
19f8b229 | 1241 | lab = NULL; |
e361382f JJ |
1242 | if (use_after_return_class != -1) |
1243 | { | |
19f8b229 | 1244 | rtx_code_label *lab2 = gen_label_rtx (); |
e361382f JJ |
1245 | char c = (char) ASAN_STACK_MAGIC_USE_AFTER_RET; |
1246 | int very_likely = REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1); | |
1247 | emit_cmp_and_jump_insns (orig_base, base, EQ, NULL_RTX, | |
1248 | VOIDmode, 0, lab2, very_likely); | |
1249 | shadow_mem = gen_rtx_MEM (BLKmode, shadow_base); | |
1250 | set_mem_alias_set (shadow_mem, asan_shadow_set); | |
1251 | mem = gen_rtx_MEM (ptr_mode, base); | |
1252 | mem = adjust_address (mem, VOIDmode, base_align_bias); | |
1253 | emit_move_insn (mem, gen_int_mode (ASAN_STACK_RETIRED_MAGIC, ptr_mode)); | |
1254 | unsigned HOST_WIDE_INT sz = asan_frame_size >> ASAN_SHADOW_SHIFT; | |
1255 | if (use_after_return_class < 5 | |
1256 | && can_store_by_pieces (sz, builtin_memset_read_str, &c, | |
1257 | BITS_PER_UNIT, true)) | |
1258 | store_by_pieces (shadow_mem, sz, builtin_memset_read_str, &c, | |
1259 | BITS_PER_UNIT, true, 0); | |
1260 | else if (use_after_return_class >= 5 | |
1261 | || !set_storage_via_setmem (shadow_mem, | |
1262 | GEN_INT (sz), | |
1263 | gen_int_mode (c, QImode), | |
1264 | BITS_PER_UNIT, BITS_PER_UNIT, | |
1265 | -1, sz, sz, sz)) | |
1266 | { | |
1267 | snprintf (buf, sizeof buf, "__asan_stack_free_%d", | |
1268 | use_after_return_class); | |
1269 | ret = init_one_libfunc (buf); | |
1270 | rtx addr = convert_memory_address (ptr_mode, base); | |
1271 | rtx orig_addr = convert_memory_address (ptr_mode, orig_base); | |
1272 | emit_library_call (ret, LCT_NORMAL, ptr_mode, 3, addr, ptr_mode, | |
1273 | GEN_INT (asan_frame_size + base_align_bias), | |
1274 | TYPE_MODE (pointer_sized_int_node), | |
1275 | orig_addr, ptr_mode); | |
1276 | } | |
1277 | lab = gen_label_rtx (); | |
1278 | emit_jump (lab); | |
1279 | emit_label (lab2); | |
1280 | } | |
1281 | ||
f3ddd692 JJ |
1282 | shadow_mem = gen_rtx_MEM (BLKmode, shadow_base); |
1283 | set_mem_alias_set (shadow_mem, asan_shadow_set); | |
e5dcd695 LZ |
1284 | |
1285 | if (STRICT_ALIGNMENT) | |
1286 | set_mem_align (shadow_mem, (GET_MODE_ALIGNMENT (SImode))); | |
1287 | ||
f3ddd692 JJ |
1288 | prev_offset = base_offset; |
1289 | last_offset = base_offset; | |
1290 | last_size = 0; | |
1291 | for (l = length; l; l -= 2) | |
1292 | { | |
1293 | offset = base_offset + ((offsets[l - 1] - base_offset) | |
1294 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)); | |
1295 | if (last_offset + last_size != offset) | |
1296 | { | |
1297 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1298 | (last_offset - prev_offset) | |
1299 | >> ASAN_SHADOW_SHIFT); | |
1300 | prev_offset = last_offset; | |
aeb7e7c1 | 1301 | asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT); |
f3ddd692 JJ |
1302 | last_offset = offset; |
1303 | last_size = 0; | |
1304 | } | |
1305 | last_size += base_offset + ((offsets[l - 2] - base_offset) | |
1306 | & ~(ASAN_RED_ZONE_SIZE - HOST_WIDE_INT_1)) | |
1307 | - offset; | |
1308 | } | |
1309 | if (last_size) | |
1310 | { | |
1311 | shadow_mem = adjust_address (shadow_mem, VOIDmode, | |
1312 | (last_offset - prev_offset) | |
1313 | >> ASAN_SHADOW_SHIFT); | |
aeb7e7c1 | 1314 | asan_clear_shadow (shadow_mem, last_size >> ASAN_SHADOW_SHIFT); |
f3ddd692 JJ |
1315 | } |
1316 | ||
1317 | do_pending_stack_adjust (); | |
e361382f JJ |
1318 | if (lab) |
1319 | emit_label (lab); | |
f3ddd692 | 1320 | |
3a4abd2f | 1321 | insns = get_insns (); |
f3ddd692 | 1322 | end_sequence (); |
3a4abd2f | 1323 | return insns; |
f3ddd692 JJ |
1324 | } |
1325 | ||
8240018b JJ |
1326 | /* Return true if DECL, a global var, might be overridden and needs |
1327 | therefore a local alias. */ | |
1328 | ||
1329 | static bool | |
1330 | asan_needs_local_alias (tree decl) | |
1331 | { | |
1332 | return DECL_WEAK (decl) || !targetm.binds_local_p (decl); | |
1333 | } | |
1334 | ||
1335 | /* Return true if DECL is a VAR_DECL that should be protected | |
1336 | by Address Sanitizer, by appending a red zone with protected | |
1337 | shadow memory after it and aligning it to at least | |
1338 | ASAN_RED_ZONE_SIZE bytes. */ | |
1339 | ||
1340 | bool | |
1341 | asan_protect_global (tree decl) | |
1342 | { | |
b5ebc991 MO |
1343 | if (!ASAN_GLOBALS) |
1344 | return false; | |
1345 | ||
8240018b | 1346 | rtx rtl, symbol; |
8240018b | 1347 | |
94fce891 JJ |
1348 | if (TREE_CODE (decl) == STRING_CST) |
1349 | { | |
1350 | /* Instrument all STRING_CSTs except those created | |
1351 | by asan_pp_string here. */ | |
1352 | if (shadow_ptr_types[0] != NULL_TREE | |
1353 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
1354 | && TREE_TYPE (TREE_TYPE (decl)) == TREE_TYPE (shadow_ptr_types[0])) | |
1355 | return false; | |
1356 | return true; | |
1357 | } | |
8240018b JJ |
1358 | if (TREE_CODE (decl) != VAR_DECL |
1359 | /* TLS vars aren't statically protectable. */ | |
1360 | || DECL_THREAD_LOCAL_P (decl) | |
1361 | /* Externs will be protected elsewhere. */ | |
1362 | || DECL_EXTERNAL (decl) | |
8240018b JJ |
1363 | || !DECL_RTL_SET_P (decl) |
1364 | /* Comdat vars pose an ABI problem, we can't know if | |
1365 | the var that is selected by the linker will have | |
1366 | padding or not. */ | |
1367 | || DECL_ONE_ONLY (decl) | |
f1d15bb9 DV |
1368 | /* Similarly for common vars. People can use -fno-common. |
1369 | Note: Linux kernel is built with -fno-common, so we do instrument | |
1370 | globals there even if it is C. */ | |
a8a6fd74 | 1371 | || (DECL_COMMON (decl) && TREE_PUBLIC (decl)) |
8240018b JJ |
1372 | /* Don't protect if using user section, often vars placed |
1373 | into user section from multiple TUs are then assumed | |
1374 | to be an array of such vars, putting padding in there | |
1375 | breaks this assumption. */ | |
f961457f | 1376 | || (DECL_SECTION_NAME (decl) != NULL |
d52f5295 | 1377 | && !symtab_node::get (decl)->implicit_section) |
8240018b JJ |
1378 | || DECL_SIZE (decl) == 0 |
1379 | || ASAN_RED_ZONE_SIZE * BITS_PER_UNIT > MAX_OFILE_ALIGNMENT | |
1380 | || !valid_constant_size_p (DECL_SIZE_UNIT (decl)) | |
21a82048 JJ |
1381 | || DECL_ALIGN_UNIT (decl) > 2 * ASAN_RED_ZONE_SIZE |
1382 | || TREE_TYPE (decl) == ubsan_get_source_location_type ()) | |
8240018b JJ |
1383 | return false; |
1384 | ||
1385 | rtl = DECL_RTL (decl); | |
1386 | if (!MEM_P (rtl) || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF) | |
1387 | return false; | |
1388 | symbol = XEXP (rtl, 0); | |
1389 | ||
1390 | if (CONSTANT_POOL_ADDRESS_P (symbol) | |
1391 | || TREE_CONSTANT_POOL_ADDRESS_P (symbol)) | |
1392 | return false; | |
1393 | ||
8240018b JJ |
1394 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
1395 | return false; | |
1396 | ||
1397 | #ifndef ASM_OUTPUT_DEF | |
1398 | if (asan_needs_local_alias (decl)) | |
1399 | return false; | |
1400 | #endif | |
1401 | ||
497a1c66 | 1402 | return true; |
8240018b JJ |
1403 | } |
1404 | ||
40f9f6bb JJ |
1405 | /* Construct a function tree for __asan_report_{load,store}{1,2,4,8,16,_n}. |
1406 | IS_STORE is either 1 (for a store) or 0 (for a load). */ | |
37d6f666 WM |
1407 | |
1408 | static tree | |
fed4de37 YG |
1409 | report_error_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes, |
1410 | int *nargs) | |
37d6f666 | 1411 | { |
fed4de37 YG |
1412 | static enum built_in_function report[2][2][6] |
1413 | = { { { BUILT_IN_ASAN_REPORT_LOAD1, BUILT_IN_ASAN_REPORT_LOAD2, | |
1414 | BUILT_IN_ASAN_REPORT_LOAD4, BUILT_IN_ASAN_REPORT_LOAD8, | |
1415 | BUILT_IN_ASAN_REPORT_LOAD16, BUILT_IN_ASAN_REPORT_LOAD_N }, | |
1416 | { BUILT_IN_ASAN_REPORT_STORE1, BUILT_IN_ASAN_REPORT_STORE2, | |
1417 | BUILT_IN_ASAN_REPORT_STORE4, BUILT_IN_ASAN_REPORT_STORE8, | |
1418 | BUILT_IN_ASAN_REPORT_STORE16, BUILT_IN_ASAN_REPORT_STORE_N } }, | |
1419 | { { BUILT_IN_ASAN_REPORT_LOAD1_NOABORT, | |
1420 | BUILT_IN_ASAN_REPORT_LOAD2_NOABORT, | |
1421 | BUILT_IN_ASAN_REPORT_LOAD4_NOABORT, | |
1422 | BUILT_IN_ASAN_REPORT_LOAD8_NOABORT, | |
1423 | BUILT_IN_ASAN_REPORT_LOAD16_NOABORT, | |
1424 | BUILT_IN_ASAN_REPORT_LOAD_N_NOABORT }, | |
1425 | { BUILT_IN_ASAN_REPORT_STORE1_NOABORT, | |
1426 | BUILT_IN_ASAN_REPORT_STORE2_NOABORT, | |
1427 | BUILT_IN_ASAN_REPORT_STORE4_NOABORT, | |
1428 | BUILT_IN_ASAN_REPORT_STORE8_NOABORT, | |
1429 | BUILT_IN_ASAN_REPORT_STORE16_NOABORT, | |
1430 | BUILT_IN_ASAN_REPORT_STORE_N_NOABORT } } }; | |
8946c29e YG |
1431 | if (size_in_bytes == -1) |
1432 | { | |
1433 | *nargs = 2; | |
fed4de37 | 1434 | return builtin_decl_implicit (report[recover_p][is_store][5]); |
8946c29e YG |
1435 | } |
1436 | *nargs = 1; | |
fed4de37 YG |
1437 | int size_log2 = exact_log2 (size_in_bytes); |
1438 | return builtin_decl_implicit (report[recover_p][is_store][size_log2]); | |
37d6f666 WM |
1439 | } |
1440 | ||
8946c29e YG |
1441 | /* Construct a function tree for __asan_{load,store}{1,2,4,8,16,_n}. |
1442 | IS_STORE is either 1 (for a store) or 0 (for a load). */ | |
1443 | ||
1444 | static tree | |
fed4de37 YG |
1445 | check_func (bool is_store, bool recover_p, HOST_WIDE_INT size_in_bytes, |
1446 | int *nargs) | |
8946c29e | 1447 | { |
fed4de37 YG |
1448 | static enum built_in_function check[2][2][6] |
1449 | = { { { BUILT_IN_ASAN_LOAD1, BUILT_IN_ASAN_LOAD2, | |
1450 | BUILT_IN_ASAN_LOAD4, BUILT_IN_ASAN_LOAD8, | |
1451 | BUILT_IN_ASAN_LOAD16, BUILT_IN_ASAN_LOADN }, | |
1452 | { BUILT_IN_ASAN_STORE1, BUILT_IN_ASAN_STORE2, | |
1453 | BUILT_IN_ASAN_STORE4, BUILT_IN_ASAN_STORE8, | |
1454 | BUILT_IN_ASAN_STORE16, BUILT_IN_ASAN_STOREN } }, | |
1455 | { { BUILT_IN_ASAN_LOAD1_NOABORT, | |
1456 | BUILT_IN_ASAN_LOAD2_NOABORT, | |
1457 | BUILT_IN_ASAN_LOAD4_NOABORT, | |
1458 | BUILT_IN_ASAN_LOAD8_NOABORT, | |
1459 | BUILT_IN_ASAN_LOAD16_NOABORT, | |
1460 | BUILT_IN_ASAN_LOADN_NOABORT }, | |
1461 | { BUILT_IN_ASAN_STORE1_NOABORT, | |
1462 | BUILT_IN_ASAN_STORE2_NOABORT, | |
1463 | BUILT_IN_ASAN_STORE4_NOABORT, | |
1464 | BUILT_IN_ASAN_STORE8_NOABORT, | |
1465 | BUILT_IN_ASAN_STORE16_NOABORT, | |
1466 | BUILT_IN_ASAN_STOREN_NOABORT } } }; | |
8946c29e YG |
1467 | if (size_in_bytes == -1) |
1468 | { | |
1469 | *nargs = 2; | |
fed4de37 | 1470 | return builtin_decl_implicit (check[recover_p][is_store][5]); |
8946c29e YG |
1471 | } |
1472 | *nargs = 1; | |
fed4de37 YG |
1473 | int size_log2 = exact_log2 (size_in_bytes); |
1474 | return builtin_decl_implicit (check[recover_p][is_store][size_log2]); | |
8946c29e YG |
1475 | } |
1476 | ||
01452015 | 1477 | /* Split the current basic block and create a condition statement |
25ae5027 DS |
1478 | insertion point right before or after the statement pointed to by |
1479 | ITER. Return an iterator to the point at which the caller might | |
1480 | safely insert the condition statement. | |
01452015 DS |
1481 | |
1482 | THEN_BLOCK must be set to the address of an uninitialized instance | |
1483 | of basic_block. The function will then set *THEN_BLOCK to the | |
1484 | 'then block' of the condition statement to be inserted by the | |
1485 | caller. | |
1486 | ||
c4bfe8bf JJ |
1487 | If CREATE_THEN_FALLTHRU_EDGE is false, no edge will be created from |
1488 | *THEN_BLOCK to *FALLTHROUGH_BLOCK. | |
1489 | ||
01452015 DS |
1490 | Similarly, the function will set *FALLTRHOUGH_BLOCK to the 'else |
1491 | block' of the condition statement to be inserted by the caller. | |
1492 | ||
1493 | Note that *FALLTHROUGH_BLOCK is a new block that contains the | |
1494 | statements starting from *ITER, and *THEN_BLOCK is a new empty | |
1495 | block. | |
1496 | ||
25ae5027 DS |
1497 | *ITER is adjusted to point to always point to the first statement |
1498 | of the basic block * FALLTHROUGH_BLOCK. That statement is the | |
1499 | same as what ITER was pointing to prior to calling this function, | |
1500 | if BEFORE_P is true; otherwise, it is its following statement. */ | |
01452015 | 1501 | |
ac0ff9f2 | 1502 | gimple_stmt_iterator |
25ae5027 DS |
1503 | create_cond_insert_point (gimple_stmt_iterator *iter, |
1504 | bool before_p, | |
1505 | bool then_more_likely_p, | |
c4bfe8bf | 1506 | bool create_then_fallthru_edge, |
25ae5027 DS |
1507 | basic_block *then_block, |
1508 | basic_block *fallthrough_block) | |
01452015 DS |
1509 | { |
1510 | gimple_stmt_iterator gsi = *iter; | |
1511 | ||
25ae5027 | 1512 | if (!gsi_end_p (gsi) && before_p) |
01452015 DS |
1513 | gsi_prev (&gsi); |
1514 | ||
1515 | basic_block cur_bb = gsi_bb (*iter); | |
1516 | ||
1517 | edge e = split_block (cur_bb, gsi_stmt (gsi)); | |
1518 | ||
1519 | /* Get a hold on the 'condition block', the 'then block' and the | |
1520 | 'else block'. */ | |
1521 | basic_block cond_bb = e->src; | |
1522 | basic_block fallthru_bb = e->dest; | |
1523 | basic_block then_bb = create_empty_bb (cond_bb); | |
a9e0d843 RB |
1524 | if (current_loops) |
1525 | { | |
1526 | add_bb_to_loop (then_bb, cond_bb->loop_father); | |
1527 | loops_state_set (LOOPS_NEED_FIXUP); | |
1528 | } | |
01452015 DS |
1529 | |
1530 | /* Set up the newly created 'then block'. */ | |
1531 | e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE); | |
1532 | int fallthrough_probability | |
1533 | = then_more_likely_p | |
1534 | ? PROB_VERY_UNLIKELY | |
1535 | : PROB_ALWAYS - PROB_VERY_UNLIKELY; | |
1536 | e->probability = PROB_ALWAYS - fallthrough_probability; | |
c4bfe8bf JJ |
1537 | if (create_then_fallthru_edge) |
1538 | make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU); | |
01452015 DS |
1539 | |
1540 | /* Set up the fallthrough basic block. */ | |
1541 | e = find_edge (cond_bb, fallthru_bb); | |
1542 | e->flags = EDGE_FALSE_VALUE; | |
1543 | e->count = cond_bb->count; | |
1544 | e->probability = fallthrough_probability; | |
1545 | ||
1546 | /* Update dominance info for the newly created then_bb; note that | |
1547 | fallthru_bb's dominance info has already been updated by | |
1548 | split_bock. */ | |
1549 | if (dom_info_available_p (CDI_DOMINATORS)) | |
1550 | set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb); | |
1551 | ||
1552 | *then_block = then_bb; | |
1553 | *fallthrough_block = fallthru_bb; | |
1554 | *iter = gsi_start_bb (fallthru_bb); | |
1555 | ||
1556 | return gsi_last_bb (cond_bb); | |
1557 | } | |
1558 | ||
25ae5027 DS |
1559 | /* Insert an if condition followed by a 'then block' right before the |
1560 | statement pointed to by ITER. The fallthrough block -- which is the | |
1561 | else block of the condition as well as the destination of the | |
1562 | outcoming edge of the 'then block' -- starts with the statement | |
1563 | pointed to by ITER. | |
1564 | ||
497a1c66 | 1565 | COND is the condition of the if. |
25ae5027 DS |
1566 | |
1567 | If THEN_MORE_LIKELY_P is true, the probability of the edge to the | |
1568 | 'then block' is higher than the probability of the edge to the | |
1569 | fallthrough block. | |
1570 | ||
1571 | Upon completion of the function, *THEN_BB is set to the newly | |
1572 | inserted 'then block' and similarly, *FALLTHROUGH_BB is set to the | |
1573 | fallthrough block. | |
1574 | ||
1575 | *ITER is adjusted to still point to the same statement it was | |
1576 | pointing to initially. */ | |
1577 | ||
1578 | static void | |
538dd0b7 | 1579 | insert_if_then_before_iter (gcond *cond, |
25ae5027 DS |
1580 | gimple_stmt_iterator *iter, |
1581 | bool then_more_likely_p, | |
1582 | basic_block *then_bb, | |
1583 | basic_block *fallthrough_bb) | |
1584 | { | |
1585 | gimple_stmt_iterator cond_insert_point = | |
1586 | create_cond_insert_point (iter, | |
1587 | /*before_p=*/true, | |
1588 | then_more_likely_p, | |
c4bfe8bf | 1589 | /*create_then_fallthru_edge=*/true, |
25ae5027 DS |
1590 | then_bb, |
1591 | fallthrough_bb); | |
1592 | gsi_insert_after (&cond_insert_point, cond, GSI_NEW_STMT); | |
1593 | } | |
1594 | ||
40f9f6bb | 1595 | /* Build |
fd960af2 | 1596 | (base_addr >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). */ |
40f9f6bb JJ |
1597 | |
1598 | static tree | |
1599 | build_shadow_mem_access (gimple_stmt_iterator *gsi, location_t location, | |
1600 | tree base_addr, tree shadow_ptr_type) | |
1601 | { | |
1602 | tree t, uintptr_type = TREE_TYPE (base_addr); | |
1603 | tree shadow_type = TREE_TYPE (shadow_ptr_type); | |
1604 | gimple g; | |
1605 | ||
1606 | t = build_int_cst (uintptr_type, ASAN_SHADOW_SHIFT); | |
0d0e4a03 JJ |
1607 | g = gimple_build_assign (make_ssa_name (uintptr_type), RSHIFT_EXPR, |
1608 | base_addr, t); | |
40f9f6bb JJ |
1609 | gimple_set_location (g, location); |
1610 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
1611 | ||
fd960af2 | 1612 | t = build_int_cst (uintptr_type, asan_shadow_offset ()); |
0d0e4a03 JJ |
1613 | g = gimple_build_assign (make_ssa_name (uintptr_type), PLUS_EXPR, |
1614 | gimple_assign_lhs (g), t); | |
40f9f6bb JJ |
1615 | gimple_set_location (g, location); |
1616 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
1617 | ||
0d0e4a03 JJ |
1618 | g = gimple_build_assign (make_ssa_name (shadow_ptr_type), NOP_EXPR, |
1619 | gimple_assign_lhs (g)); | |
40f9f6bb JJ |
1620 | gimple_set_location (g, location); |
1621 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
1622 | ||
1623 | t = build2 (MEM_REF, shadow_type, gimple_assign_lhs (g), | |
1624 | build_int_cst (shadow_ptr_type, 0)); | |
0d0e4a03 | 1625 | g = gimple_build_assign (make_ssa_name (shadow_type), MEM_REF, t); |
40f9f6bb JJ |
1626 | gimple_set_location (g, location); |
1627 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
1628 | return gimple_assign_lhs (g); | |
1629 | } | |
1630 | ||
8946c29e YG |
1631 | /* BASE can already be an SSA_NAME; in that case, do not create a |
1632 | new SSA_NAME for it. */ | |
1633 | ||
1634 | static tree | |
1635 | maybe_create_ssa_name (location_t loc, tree base, gimple_stmt_iterator *iter, | |
1636 | bool before_p) | |
1637 | { | |
1638 | if (TREE_CODE (base) == SSA_NAME) | |
1639 | return base; | |
0d0e4a03 JJ |
1640 | gimple g = gimple_build_assign (make_ssa_name (TREE_TYPE (base)), |
1641 | TREE_CODE (base), base); | |
8946c29e YG |
1642 | gimple_set_location (g, loc); |
1643 | if (before_p) | |
1644 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
1645 | else | |
1646 | gsi_insert_after (iter, g, GSI_NEW_STMT); | |
1647 | return gimple_assign_lhs (g); | |
1648 | } | |
1649 | ||
a2f581e1 YG |
1650 | /* LEN can already have necessary size and precision; |
1651 | in that case, do not create a new variable. */ | |
1652 | ||
1653 | tree | |
1654 | maybe_cast_to_ptrmode (location_t loc, tree len, gimple_stmt_iterator *iter, | |
1655 | bool before_p) | |
1656 | { | |
1657 | if (ptrofftype_p (len)) | |
1658 | return len; | |
0d0e4a03 JJ |
1659 | gimple g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
1660 | NOP_EXPR, len); | |
a2f581e1 YG |
1661 | gimple_set_location (g, loc); |
1662 | if (before_p) | |
1663 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
1664 | else | |
1665 | gsi_insert_after (iter, g, GSI_NEW_STMT); | |
1666 | return gimple_assign_lhs (g); | |
1667 | } | |
1668 | ||
dc29bf1e | 1669 | /* Instrument the memory access instruction BASE. Insert new |
25ae5027 | 1670 | statements before or after ITER. |
dc29bf1e DS |
1671 | |
1672 | Note that the memory access represented by BASE can be either an | |
1673 | SSA_NAME, or a non-SSA expression. LOCATION is the source code | |
1674 | location. IS_STORE is TRUE for a store, FALSE for a load. | |
25ae5027 | 1675 | BEFORE_P is TRUE for inserting the instrumentation code before |
8946c29e YG |
1676 | ITER, FALSE for inserting it after ITER. IS_SCALAR_ACCESS is TRUE |
1677 | for a scalar memory access and FALSE for memory region access. | |
1678 | NON_ZERO_P is TRUE if memory region is guaranteed to have non-zero | |
1679 | length. ALIGN tells alignment of accessed memory object. | |
1680 | ||
1681 | START_INSTRUMENTED and END_INSTRUMENTED are TRUE if start/end of | |
1682 | memory region have already been instrumented. | |
25ae5027 DS |
1683 | |
1684 | If BEFORE_P is TRUE, *ITER is arranged to still point to the | |
1685 | statement it was pointing to prior to calling this function, | |
1686 | otherwise, it points to the statement logically following it. */ | |
37d6f666 WM |
1687 | |
1688 | static void | |
c62ccb9a | 1689 | build_check_stmt (location_t loc, tree base, tree len, |
8946c29e | 1690 | HOST_WIDE_INT size_in_bytes, gimple_stmt_iterator *iter, |
c62ccb9a | 1691 | bool is_non_zero_len, bool before_p, bool is_store, |
bdea98ca | 1692 | bool is_scalar_access, unsigned int align = 0) |
37d6f666 | 1693 | { |
8946c29e | 1694 | gimple_stmt_iterator gsi = *iter; |
37d6f666 | 1695 | gimple g; |
8946c29e | 1696 | |
c62ccb9a | 1697 | gcc_assert (!(size_in_bytes > 0 && !is_non_zero_len)); |
8946c29e | 1698 | |
c62ccb9a YG |
1699 | gsi = *iter; |
1700 | ||
1701 | base = unshare_expr (base); | |
1702 | base = maybe_create_ssa_name (loc, base, &gsi, before_p); | |
1703 | ||
8946c29e | 1704 | if (len) |
a2f581e1 YG |
1705 | { |
1706 | len = unshare_expr (len); | |
1707 | len = maybe_cast_to_ptrmode (loc, len, iter, before_p); | |
1708 | } | |
8946c29e YG |
1709 | else |
1710 | { | |
1711 | gcc_assert (size_in_bytes != -1); | |
1712 | len = build_int_cst (pointer_sized_int_node, size_in_bytes); | |
1713 | } | |
1714 | ||
1715 | if (size_in_bytes > 1) | |
b3f1051b | 1716 | { |
8946c29e YG |
1717 | if ((size_in_bytes & (size_in_bytes - 1)) != 0 |
1718 | || size_in_bytes > 16) | |
c62ccb9a | 1719 | is_scalar_access = false; |
8946c29e YG |
1720 | else if (align && align < size_in_bytes * BITS_PER_UNIT) |
1721 | { | |
1722 | /* On non-strict alignment targets, if | |
1723 | 16-byte access is just 8-byte aligned, | |
1724 | this will result in misaligned shadow | |
1725 | memory 2 byte load, but otherwise can | |
1726 | be handled using one read. */ | |
1727 | if (size_in_bytes != 16 | |
1728 | || STRICT_ALIGNMENT | |
1729 | || align < 8 * BITS_PER_UNIT) | |
c62ccb9a | 1730 | is_scalar_access = false; |
40f9f6bb | 1731 | } |
f6d98484 | 1732 | } |
37d6f666 | 1733 | |
c62ccb9a YG |
1734 | HOST_WIDE_INT flags = 0; |
1735 | if (is_store) | |
1736 | flags |= ASAN_CHECK_STORE; | |
1737 | if (is_non_zero_len) | |
1738 | flags |= ASAN_CHECK_NON_ZERO_LEN; | |
1739 | if (is_scalar_access) | |
1740 | flags |= ASAN_CHECK_SCALAR_ACCESS; | |
c62ccb9a | 1741 | |
f434eb69 | 1742 | g = gimple_build_call_internal (IFN_ASAN_CHECK, 4, |
c62ccb9a | 1743 | build_int_cst (integer_type_node, flags), |
f434eb69 MZ |
1744 | base, len, |
1745 | build_int_cst (integer_type_node, | |
1746 | align / BITS_PER_UNIT)); | |
c62ccb9a YG |
1747 | gimple_set_location (g, loc); |
1748 | if (before_p) | |
1749 | gsi_insert_before (&gsi, g, GSI_SAME_STMT); | |
8946c29e YG |
1750 | else |
1751 | { | |
8946c29e | 1752 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); |
c62ccb9a YG |
1753 | gsi_next (&gsi); |
1754 | *iter = gsi; | |
8946c29e | 1755 | } |
37d6f666 WM |
1756 | } |
1757 | ||
1758 | /* If T represents a memory access, add instrumentation code before ITER. | |
1759 | LOCATION is source code location. | |
25ae5027 | 1760 | IS_STORE is either TRUE (for a store) or FALSE (for a load). */ |
37d6f666 WM |
1761 | |
1762 | static void | |
1763 | instrument_derefs (gimple_stmt_iterator *iter, tree t, | |
bdcbe80c | 1764 | location_t location, bool is_store) |
37d6f666 | 1765 | { |
b5ebc991 MO |
1766 | if (is_store && !ASAN_INSTRUMENT_WRITES) |
1767 | return; | |
1768 | if (!is_store && !ASAN_INSTRUMENT_READS) | |
1769 | return; | |
1770 | ||
37d6f666 | 1771 | tree type, base; |
f6d98484 | 1772 | HOST_WIDE_INT size_in_bytes; |
37d6f666 WM |
1773 | |
1774 | type = TREE_TYPE (t); | |
37d6f666 WM |
1775 | switch (TREE_CODE (t)) |
1776 | { | |
1777 | case ARRAY_REF: | |
1778 | case COMPONENT_REF: | |
1779 | case INDIRECT_REF: | |
1780 | case MEM_REF: | |
59b36ecf | 1781 | case VAR_DECL: |
913f32a1 | 1782 | case BIT_FIELD_REF: |
37d6f666 | 1783 | break; |
59b36ecf | 1784 | /* FALLTHRU */ |
37d6f666 WM |
1785 | default: |
1786 | return; | |
1787 | } | |
f6d98484 JJ |
1788 | |
1789 | size_in_bytes = int_size_in_bytes (type); | |
40f9f6bb | 1790 | if (size_in_bytes <= 0) |
f6d98484 JJ |
1791 | return; |
1792 | ||
f6d98484 JJ |
1793 | HOST_WIDE_INT bitsize, bitpos; |
1794 | tree offset; | |
ef4bddc2 | 1795 | machine_mode mode; |
f6d98484 | 1796 | int volatilep = 0, unsignedp = 0; |
59b36ecf | 1797 | tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, |
b3ecff82 | 1798 | &mode, &unsignedp, &volatilep, false); |
87d1d65a YG |
1799 | |
1800 | if (TREE_CODE (t) == COMPONENT_REF | |
1801 | && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE) | |
1fe04fdc | 1802 | { |
87d1d65a YG |
1803 | tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)); |
1804 | instrument_derefs (iter, build3 (COMPONENT_REF, TREE_TYPE (repr), | |
1805 | TREE_OPERAND (t, 0), repr, | |
1806 | NULL_TREE), location, is_store); | |
1fe04fdc JJ |
1807 | return; |
1808 | } | |
87d1d65a YG |
1809 | |
1810 | if (bitpos % BITS_PER_UNIT | |
1811 | || bitsize != size_in_bytes * BITS_PER_UNIT) | |
40f9f6bb | 1812 | return; |
f6d98484 | 1813 | |
59b36ecf JJ |
1814 | if (TREE_CODE (inner) == VAR_DECL |
1815 | && offset == NULL_TREE | |
1816 | && bitpos >= 0 | |
1817 | && DECL_SIZE (inner) | |
1818 | && tree_fits_shwi_p (DECL_SIZE (inner)) | |
1819 | && bitpos + bitsize <= tree_to_shwi (DECL_SIZE (inner))) | |
1820 | { | |
1821 | if (DECL_THREAD_LOCAL_P (inner)) | |
1822 | return; | |
1823 | if (!TREE_STATIC (inner)) | |
1824 | { | |
1825 | /* Automatic vars in the current function will be always | |
1826 | accessible. */ | |
1827 | if (decl_function_context (inner) == current_function_decl) | |
1828 | return; | |
1829 | } | |
1830 | /* Always instrument external vars, they might be dynamically | |
1831 | initialized. */ | |
1832 | else if (!DECL_EXTERNAL (inner)) | |
1833 | { | |
1834 | /* For static vars if they are known not to be dynamically | |
1835 | initialized, they will be always accessible. */ | |
9041d2e6 | 1836 | varpool_node *vnode = varpool_node::get (inner); |
59b36ecf JJ |
1837 | if (vnode && !vnode->dynamically_initialized) |
1838 | return; | |
1839 | } | |
1840 | } | |
1841 | ||
f6d98484 | 1842 | base = build_fold_addr_expr (t); |
bdcbe80c DS |
1843 | if (!has_mem_ref_been_instrumented (base, size_in_bytes)) |
1844 | { | |
8946c29e YG |
1845 | unsigned int align = get_object_alignment (t); |
1846 | build_check_stmt (location, base, NULL_TREE, size_in_bytes, iter, | |
c62ccb9a | 1847 | /*is_non_zero_len*/size_in_bytes > 0, /*before_p=*/true, |
8946c29e | 1848 | is_store, /*is_scalar_access*/true, align); |
bdcbe80c DS |
1849 | update_mem_ref_hash_table (base, size_in_bytes); |
1850 | update_mem_ref_hash_table (t, size_in_bytes); | |
1851 | } | |
1852 | ||
25ae5027 DS |
1853 | } |
1854 | ||
bdea98ca MO |
1855 | /* Insert a memory reference into the hash table if access length |
1856 | can be determined in compile time. */ | |
1857 | ||
1858 | static void | |
1859 | maybe_update_mem_ref_hash_table (tree base, tree len) | |
1860 | { | |
1861 | if (!POINTER_TYPE_P (TREE_TYPE (base)) | |
1862 | || !INTEGRAL_TYPE_P (TREE_TYPE (len))) | |
1863 | return; | |
1864 | ||
1865 | HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; | |
1866 | ||
1867 | if (size_in_bytes != -1) | |
1868 | update_mem_ref_hash_table (base, size_in_bytes); | |
1869 | } | |
1870 | ||
25ae5027 DS |
1871 | /* Instrument an access to a contiguous memory region that starts at |
1872 | the address pointed to by BASE, over a length of LEN (expressed in | |
1873 | the sizeof (*BASE) bytes). ITER points to the instruction before | |
1874 | which the instrumentation instructions must be inserted. LOCATION | |
1875 | is the source location that the instrumentation instructions must | |
1876 | have. If IS_STORE is true, then the memory access is a store; | |
1877 | otherwise, it's a load. */ | |
1878 | ||
1879 | static void | |
1880 | instrument_mem_region_access (tree base, tree len, | |
1881 | gimple_stmt_iterator *iter, | |
1882 | location_t location, bool is_store) | |
1883 | { | |
c63d3b96 JJ |
1884 | if (!POINTER_TYPE_P (TREE_TYPE (base)) |
1885 | || !INTEGRAL_TYPE_P (TREE_TYPE (len)) | |
1886 | || integer_zerop (len)) | |
25ae5027 DS |
1887 | return; |
1888 | ||
8946c29e | 1889 | HOST_WIDE_INT size_in_bytes = tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; |
bdcbe80c | 1890 | |
bdea98ca MO |
1891 | if ((size_in_bytes == -1) |
1892 | || !has_mem_ref_been_instrumented (base, size_in_bytes)) | |
1893 | { | |
1894 | build_check_stmt (location, base, len, size_in_bytes, iter, | |
1895 | /*is_non_zero_len*/size_in_bytes > 0, /*before_p*/true, | |
1896 | is_store, /*is_scalar_access*/false, /*align*/0); | |
1897 | } | |
b41288b3 | 1898 | |
bdea98ca | 1899 | maybe_update_mem_ref_hash_table (base, len); |
b41288b3 | 1900 | *iter = gsi_for_stmt (gsi_stmt (*iter)); |
bdcbe80c | 1901 | } |
25ae5027 | 1902 | |
bdcbe80c DS |
1903 | /* Instrument the call to a built-in memory access function that is |
1904 | pointed to by the iterator ITER. | |
25ae5027 | 1905 | |
bdcbe80c DS |
1906 | Upon completion, return TRUE iff *ITER has been advanced to the |
1907 | statement following the one it was originally pointing to. */ | |
25ae5027 | 1908 | |
bdcbe80c DS |
1909 | static bool |
1910 | instrument_builtin_call (gimple_stmt_iterator *iter) | |
1911 | { | |
b5ebc991 MO |
1912 | if (!ASAN_MEMINTRIN) |
1913 | return false; | |
1914 | ||
bdcbe80c | 1915 | bool iter_advanced_p = false; |
538dd0b7 | 1916 | gcall *call = as_a <gcall *> (gsi_stmt (*iter)); |
25ae5027 | 1917 | |
bdcbe80c | 1918 | gcc_checking_assert (gimple_call_builtin_p (call, BUILT_IN_NORMAL)); |
25ae5027 | 1919 | |
bdcbe80c | 1920 | location_t loc = gimple_location (call); |
25ae5027 | 1921 | |
bdea98ca MO |
1922 | asan_mem_ref src0, src1, dest; |
1923 | asan_mem_ref_init (&src0, NULL, 1); | |
1924 | asan_mem_ref_init (&src1, NULL, 1); | |
1925 | asan_mem_ref_init (&dest, NULL, 1); | |
bdcbe80c | 1926 | |
bdea98ca MO |
1927 | tree src0_len = NULL_TREE, src1_len = NULL_TREE, dest_len = NULL_TREE; |
1928 | bool src0_is_store = false, src1_is_store = false, dest_is_store = false, | |
1929 | dest_is_deref = false, intercepted_p = true; | |
bdcbe80c | 1930 | |
bdea98ca MO |
1931 | if (get_mem_refs_of_builtin_call (call, |
1932 | &src0, &src0_len, &src0_is_store, | |
1933 | &src1, &src1_len, &src1_is_store, | |
1934 | &dest, &dest_len, &dest_is_store, | |
1935 | &dest_is_deref, &intercepted_p)) | |
1936 | { | |
1937 | if (dest_is_deref) | |
bdcbe80c | 1938 | { |
bdea98ca MO |
1939 | instrument_derefs (iter, dest.start, loc, dest_is_store); |
1940 | gsi_next (iter); | |
1941 | iter_advanced_p = true; | |
1942 | } | |
1943 | else if (!intercepted_p | |
1944 | && (src0_len || src1_len || dest_len)) | |
1945 | { | |
1946 | if (src0.start != NULL_TREE) | |
1947 | instrument_mem_region_access (src0.start, src0_len, | |
1948 | iter, loc, /*is_store=*/false); | |
1949 | if (src1.start != NULL_TREE) | |
1950 | instrument_mem_region_access (src1.start, src1_len, | |
1951 | iter, loc, /*is_store=*/false); | |
1952 | if (dest.start != NULL_TREE) | |
1953 | instrument_mem_region_access (dest.start, dest_len, | |
1954 | iter, loc, /*is_store=*/true); | |
1955 | ||
1956 | *iter = gsi_for_stmt (call); | |
1957 | gsi_next (iter); | |
1958 | iter_advanced_p = true; | |
1959 | } | |
1960 | else | |
1961 | { | |
1962 | if (src0.start != NULL_TREE) | |
1963 | maybe_update_mem_ref_hash_table (src0.start, src0_len); | |
1964 | if (src1.start != NULL_TREE) | |
1965 | maybe_update_mem_ref_hash_table (src1.start, src1_len); | |
1966 | if (dest.start != NULL_TREE) | |
1967 | maybe_update_mem_ref_hash_table (dest.start, dest_len); | |
bdcbe80c | 1968 | } |
25ae5027 | 1969 | } |
bdcbe80c | 1970 | return iter_advanced_p; |
25ae5027 DS |
1971 | } |
1972 | ||
1973 | /* Instrument the assignment statement ITER if it is subject to | |
bdcbe80c DS |
1974 | instrumentation. Return TRUE iff instrumentation actually |
1975 | happened. In that case, the iterator ITER is advanced to the next | |
1976 | logical expression following the one initially pointed to by ITER, | |
1977 | and the relevant memory reference that which access has been | |
1978 | instrumented is added to the memory references hash table. */ | |
25ae5027 | 1979 | |
bdcbe80c DS |
1980 | static bool |
1981 | maybe_instrument_assignment (gimple_stmt_iterator *iter) | |
25ae5027 DS |
1982 | { |
1983 | gimple s = gsi_stmt (*iter); | |
1984 | ||
1985 | gcc_assert (gimple_assign_single_p (s)); | |
1986 | ||
bdcbe80c DS |
1987 | tree ref_expr = NULL_TREE; |
1988 | bool is_store, is_instrumented = false; | |
1989 | ||
52f2e7e1 | 1990 | if (gimple_store_p (s)) |
bdcbe80c DS |
1991 | { |
1992 | ref_expr = gimple_assign_lhs (s); | |
1993 | is_store = true; | |
1994 | instrument_derefs (iter, ref_expr, | |
1995 | gimple_location (s), | |
1996 | is_store); | |
1997 | is_instrumented = true; | |
1998 | } | |
1999 | ||
52f2e7e1 | 2000 | if (gimple_assign_load_p (s)) |
bdcbe80c DS |
2001 | { |
2002 | ref_expr = gimple_assign_rhs1 (s); | |
2003 | is_store = false; | |
2004 | instrument_derefs (iter, ref_expr, | |
2005 | gimple_location (s), | |
2006 | is_store); | |
2007 | is_instrumented = true; | |
2008 | } | |
2009 | ||
2010 | if (is_instrumented) | |
2011 | gsi_next (iter); | |
2012 | ||
2013 | return is_instrumented; | |
25ae5027 DS |
2014 | } |
2015 | ||
2016 | /* Instrument the function call pointed to by the iterator ITER, if it | |
2017 | is subject to instrumentation. At the moment, the only function | |
2018 | calls that are instrumented are some built-in functions that access | |
2019 | memory. Look at instrument_builtin_call to learn more. | |
2020 | ||
2021 | Upon completion return TRUE iff *ITER was advanced to the statement | |
2022 | following the one it was originally pointing to. */ | |
2023 | ||
2024 | static bool | |
2025 | maybe_instrument_call (gimple_stmt_iterator *iter) | |
2026 | { | |
2b2571c9 | 2027 | gimple stmt = gsi_stmt (*iter); |
bdcbe80c DS |
2028 | bool is_builtin = gimple_call_builtin_p (stmt, BUILT_IN_NORMAL); |
2029 | ||
2030 | if (is_builtin && instrument_builtin_call (iter)) | |
2b2571c9 | 2031 | return true; |
bdcbe80c | 2032 | |
2b2571c9 JJ |
2033 | if (gimple_call_noreturn_p (stmt)) |
2034 | { | |
2035 | if (is_builtin) | |
2036 | { | |
2037 | tree callee = gimple_call_fndecl (stmt); | |
2038 | switch (DECL_FUNCTION_CODE (callee)) | |
2039 | { | |
2040 | case BUILT_IN_UNREACHABLE: | |
2041 | case BUILT_IN_TRAP: | |
2042 | /* Don't instrument these. */ | |
2043 | return false; | |
083e891e MP |
2044 | default: |
2045 | break; | |
2b2571c9 JJ |
2046 | } |
2047 | } | |
2048 | tree decl = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN); | |
2049 | gimple g = gimple_build_call (decl, 0); | |
2050 | gimple_set_location (g, gimple_location (stmt)); | |
2051 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
2052 | } | |
25ae5027 | 2053 | return false; |
37d6f666 WM |
2054 | } |
2055 | ||
bdcbe80c DS |
2056 | /* Walk each instruction of all basic block and instrument those that |
2057 | represent memory references: loads, stores, or function calls. | |
2058 | In a given basic block, this function avoids instrumenting memory | |
2059 | references that have already been instrumented. */ | |
37d6f666 WM |
2060 | |
2061 | static void | |
2062 | transform_statements (void) | |
2063 | { | |
c4bfe8bf | 2064 | basic_block bb, last_bb = NULL; |
37d6f666 | 2065 | gimple_stmt_iterator i; |
8b1c6fd7 | 2066 | int saved_last_basic_block = last_basic_block_for_fn (cfun); |
37d6f666 | 2067 | |
11cd3bed | 2068 | FOR_EACH_BB_FN (bb, cfun) |
37d6f666 | 2069 | { |
c4bfe8bf | 2070 | basic_block prev_bb = bb; |
bdcbe80c | 2071 | |
37d6f666 | 2072 | if (bb->index >= saved_last_basic_block) continue; |
c4bfe8bf JJ |
2073 | |
2074 | /* Flush the mem ref hash table, if current bb doesn't have | |
2075 | exactly one predecessor, or if that predecessor (skipping | |
2076 | over asan created basic blocks) isn't the last processed | |
2077 | basic block. Thus we effectively flush on extended basic | |
2078 | block boundaries. */ | |
2079 | while (single_pred_p (prev_bb)) | |
2080 | { | |
2081 | prev_bb = single_pred (prev_bb); | |
2082 | if (prev_bb->index < saved_last_basic_block) | |
2083 | break; | |
2084 | } | |
2085 | if (prev_bb != last_bb) | |
2086 | empty_mem_ref_hash_table (); | |
2087 | last_bb = bb; | |
2088 | ||
25ae5027 | 2089 | for (i = gsi_start_bb (bb); !gsi_end_p (i);) |
497a1c66 | 2090 | { |
25ae5027 DS |
2091 | gimple s = gsi_stmt (i); |
2092 | ||
bdcbe80c DS |
2093 | if (has_stmt_been_instrumented_p (s)) |
2094 | gsi_next (&i); | |
2095 | else if (gimple_assign_single_p (s) | |
e1e160c1 | 2096 | && !gimple_clobber_p (s) |
bdcbe80c DS |
2097 | && maybe_instrument_assignment (&i)) |
2098 | /* Nothing to do as maybe_instrument_assignment advanced | |
2099 | the iterator I. */; | |
2100 | else if (is_gimple_call (s) && maybe_instrument_call (&i)) | |
2101 | /* Nothing to do as maybe_instrument_call | |
2102 | advanced the iterator I. */; | |
2103 | else | |
25ae5027 | 2104 | { |
bdcbe80c DS |
2105 | /* No instrumentation happened. |
2106 | ||
c4bfe8bf JJ |
2107 | If the current instruction is a function call that |
2108 | might free something, let's forget about the memory | |
2109 | references that got instrumented. Otherwise we might | |
2110 | miss some instrumentation opportunities. */ | |
2111 | if (is_gimple_call (s) && !nonfreeing_call_p (s)) | |
bdcbe80c DS |
2112 | empty_mem_ref_hash_table (); |
2113 | ||
2114 | gsi_next (&i); | |
25ae5027 | 2115 | } |
497a1c66 | 2116 | } |
37d6f666 | 2117 | } |
bdcbe80c | 2118 | free_mem_ref_resources (); |
37d6f666 WM |
2119 | } |
2120 | ||
59b36ecf JJ |
2121 | /* Build |
2122 | __asan_before_dynamic_init (module_name) | |
2123 | or | |
2124 | __asan_after_dynamic_init () | |
2125 | call. */ | |
2126 | ||
2127 | tree | |
2128 | asan_dynamic_init_call (bool after_p) | |
2129 | { | |
2130 | tree fn = builtin_decl_implicit (after_p | |
2131 | ? BUILT_IN_ASAN_AFTER_DYNAMIC_INIT | |
2132 | : BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT); | |
2133 | tree module_name_cst = NULL_TREE; | |
2134 | if (!after_p) | |
2135 | { | |
2136 | pretty_printer module_name_pp; | |
2137 | pp_string (&module_name_pp, main_input_filename); | |
2138 | ||
2139 | if (shadow_ptr_types[0] == NULL_TREE) | |
2140 | asan_init_shadow_ptr_types (); | |
2141 | module_name_cst = asan_pp_string (&module_name_pp); | |
2142 | module_name_cst = fold_convert (const_ptr_type_node, | |
2143 | module_name_cst); | |
2144 | } | |
2145 | ||
2146 | return build_call_expr (fn, after_p ? 0 : 1, module_name_cst); | |
2147 | } | |
2148 | ||
8240018b JJ |
2149 | /* Build |
2150 | struct __asan_global | |
2151 | { | |
2152 | const void *__beg; | |
2153 | uptr __size; | |
2154 | uptr __size_with_redzone; | |
2155 | const void *__name; | |
ef1b3fda | 2156 | const void *__module_name; |
8240018b | 2157 | uptr __has_dynamic_init; |
866e32ad | 2158 | __asan_global_source_location *__location; |
8240018b JJ |
2159 | } type. */ |
2160 | ||
2161 | static tree | |
2162 | asan_global_struct (void) | |
2163 | { | |
866e32ad | 2164 | static const char *field_names[7] |
8240018b | 2165 | = { "__beg", "__size", "__size_with_redzone", |
866e32ad KS |
2166 | "__name", "__module_name", "__has_dynamic_init", "__location"}; |
2167 | tree fields[7], ret; | |
8240018b JJ |
2168 | int i; |
2169 | ||
2170 | ret = make_node (RECORD_TYPE); | |
866e32ad | 2171 | for (i = 0; i < 7; i++) |
8240018b JJ |
2172 | { |
2173 | fields[i] | |
2174 | = build_decl (UNKNOWN_LOCATION, FIELD_DECL, | |
2175 | get_identifier (field_names[i]), | |
2176 | (i == 0 || i == 3) ? const_ptr_type_node | |
de5a5fa1 | 2177 | : pointer_sized_int_node); |
8240018b JJ |
2178 | DECL_CONTEXT (fields[i]) = ret; |
2179 | if (i) | |
2180 | DECL_CHAIN (fields[i - 1]) = fields[i]; | |
2181 | } | |
bebcdc67 MP |
2182 | tree type_decl = build_decl (input_location, TYPE_DECL, |
2183 | get_identifier ("__asan_global"), ret); | |
2184 | DECL_IGNORED_P (type_decl) = 1; | |
2185 | DECL_ARTIFICIAL (type_decl) = 1; | |
8240018b | 2186 | TYPE_FIELDS (ret) = fields[0]; |
bebcdc67 MP |
2187 | TYPE_NAME (ret) = type_decl; |
2188 | TYPE_STUB_DECL (ret) = type_decl; | |
8240018b JJ |
2189 | layout_type (ret); |
2190 | return ret; | |
2191 | } | |
2192 | ||
2193 | /* Append description of a single global DECL into vector V. | |
2194 | TYPE is __asan_global struct type as returned by asan_global_struct. */ | |
2195 | ||
2196 | static void | |
9771b263 | 2197 | asan_add_global (tree decl, tree type, vec<constructor_elt, va_gc> *v) |
8240018b JJ |
2198 | { |
2199 | tree init, uptr = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (type))); | |
2200 | unsigned HOST_WIDE_INT size; | |
ef1b3fda | 2201 | tree str_cst, module_name_cst, refdecl = decl; |
9771b263 | 2202 | vec<constructor_elt, va_gc> *vinner = NULL; |
8240018b | 2203 | |
ef1b3fda | 2204 | pretty_printer asan_pp, module_name_pp; |
8240018b | 2205 | |
8240018b | 2206 | if (DECL_NAME (decl)) |
b066401f | 2207 | pp_tree_identifier (&asan_pp, DECL_NAME (decl)); |
8240018b JJ |
2208 | else |
2209 | pp_string (&asan_pp, "<unknown>"); | |
11a877b3 | 2210 | str_cst = asan_pp_string (&asan_pp); |
8240018b | 2211 | |
ef1b3fda KS |
2212 | pp_string (&module_name_pp, main_input_filename); |
2213 | module_name_cst = asan_pp_string (&module_name_pp); | |
2214 | ||
8240018b JJ |
2215 | if (asan_needs_local_alias (decl)) |
2216 | { | |
2217 | char buf[20]; | |
9771b263 | 2218 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", vec_safe_length (v) + 1); |
8240018b JJ |
2219 | refdecl = build_decl (DECL_SOURCE_LOCATION (decl), |
2220 | VAR_DECL, get_identifier (buf), TREE_TYPE (decl)); | |
2221 | TREE_ADDRESSABLE (refdecl) = TREE_ADDRESSABLE (decl); | |
2222 | TREE_READONLY (refdecl) = TREE_READONLY (decl); | |
2223 | TREE_THIS_VOLATILE (refdecl) = TREE_THIS_VOLATILE (decl); | |
2224 | DECL_GIMPLE_REG_P (refdecl) = DECL_GIMPLE_REG_P (decl); | |
2225 | DECL_ARTIFICIAL (refdecl) = DECL_ARTIFICIAL (decl); | |
2226 | DECL_IGNORED_P (refdecl) = DECL_IGNORED_P (decl); | |
2227 | TREE_STATIC (refdecl) = 1; | |
2228 | TREE_PUBLIC (refdecl) = 0; | |
2229 | TREE_USED (refdecl) = 1; | |
2230 | assemble_alias (refdecl, DECL_ASSEMBLER_NAME (decl)); | |
2231 | } | |
2232 | ||
2233 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, | |
2234 | fold_convert (const_ptr_type_node, | |
2235 | build_fold_addr_expr (refdecl))); | |
ae7e9ddd | 2236 | size = tree_to_uhwi (DECL_SIZE_UNIT (decl)); |
8240018b JJ |
2237 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); |
2238 | size += asan_red_zone_size (size); | |
2239 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, build_int_cst (uptr, size)); | |
2240 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, | |
2241 | fold_convert (const_ptr_type_node, str_cst)); | |
ef1b3fda KS |
2242 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, |
2243 | fold_convert (const_ptr_type_node, module_name_cst)); | |
9041d2e6 | 2244 | varpool_node *vnode = varpool_node::get (decl); |
59b36ecf JJ |
2245 | int has_dynamic_init = vnode ? vnode->dynamically_initialized : 0; |
2246 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, | |
2247 | build_int_cst (uptr, has_dynamic_init)); | |
21a82048 JJ |
2248 | tree locptr = NULL_TREE; |
2249 | location_t loc = DECL_SOURCE_LOCATION (decl); | |
2250 | expanded_location xloc = expand_location (loc); | |
2251 | if (xloc.file != NULL) | |
2252 | { | |
2253 | static int lasanloccnt = 0; | |
2254 | char buf[25]; | |
2255 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASANLOC", ++lasanloccnt); | |
2256 | tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf), | |
2257 | ubsan_get_source_location_type ()); | |
2258 | TREE_STATIC (var) = 1; | |
2259 | TREE_PUBLIC (var) = 0; | |
2260 | DECL_ARTIFICIAL (var) = 1; | |
2261 | DECL_IGNORED_P (var) = 1; | |
2262 | pretty_printer filename_pp; | |
2263 | pp_string (&filename_pp, xloc.file); | |
2264 | tree str = asan_pp_string (&filename_pp); | |
2265 | tree ctor = build_constructor_va (TREE_TYPE (var), 3, | |
2266 | NULL_TREE, str, NULL_TREE, | |
2267 | build_int_cst (unsigned_type_node, | |
2268 | xloc.line), NULL_TREE, | |
2269 | build_int_cst (unsigned_type_node, | |
2270 | xloc.column)); | |
2271 | TREE_CONSTANT (ctor) = 1; | |
2272 | TREE_STATIC (ctor) = 1; | |
2273 | DECL_INITIAL (var) = ctor; | |
2274 | varpool_node::finalize_decl (var); | |
2275 | locptr = fold_convert (uptr, build_fold_addr_expr (var)); | |
2276 | } | |
2277 | else | |
2278 | locptr = build_int_cst (uptr, 0); | |
2279 | CONSTRUCTOR_APPEND_ELT (vinner, NULL_TREE, locptr); | |
8240018b JJ |
2280 | init = build_constructor (type, vinner); |
2281 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); | |
2282 | } | |
2283 | ||
0e668eaf JJ |
2284 | /* Initialize sanitizer.def builtins if the FE hasn't initialized them. */ |
2285 | void | |
2286 | initialize_sanitizer_builtins (void) | |
2287 | { | |
2288 | tree decl; | |
2289 | ||
2290 | if (builtin_decl_implicit_p (BUILT_IN_ASAN_INIT)) | |
2291 | return; | |
2292 | ||
2293 | tree BT_FN_VOID = build_function_type_list (void_type_node, NULL_TREE); | |
2294 | tree BT_FN_VOID_PTR | |
2295 | = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); | |
59b36ecf JJ |
2296 | tree BT_FN_VOID_CONST_PTR |
2297 | = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE); | |
b906f4ca MP |
2298 | tree BT_FN_VOID_PTR_PTR |
2299 | = build_function_type_list (void_type_node, ptr_type_node, | |
2300 | ptr_type_node, NULL_TREE); | |
de5a5fa1 MP |
2301 | tree BT_FN_VOID_PTR_PTR_PTR |
2302 | = build_function_type_list (void_type_node, ptr_type_node, | |
2303 | ptr_type_node, ptr_type_node, NULL_TREE); | |
0e668eaf JJ |
2304 | tree BT_FN_VOID_PTR_PTRMODE |
2305 | = build_function_type_list (void_type_node, ptr_type_node, | |
de5a5fa1 | 2306 | pointer_sized_int_node, NULL_TREE); |
c954bddd JJ |
2307 | tree BT_FN_VOID_INT |
2308 | = build_function_type_list (void_type_node, integer_type_node, NULL_TREE); | |
0bae64d5 MP |
2309 | tree BT_FN_SIZE_CONST_PTR_INT |
2310 | = build_function_type_list (size_type_node, const_ptr_type_node, | |
2311 | integer_type_node, NULL_TREE); | |
c954bddd JJ |
2312 | tree BT_FN_BOOL_VPTR_PTR_IX_INT_INT[5]; |
2313 | tree BT_FN_IX_CONST_VPTR_INT[5]; | |
2314 | tree BT_FN_IX_VPTR_IX_INT[5]; | |
2315 | tree BT_FN_VOID_VPTR_IX_INT[5]; | |
2316 | tree vptr | |
2317 | = build_pointer_type (build_qualified_type (void_type_node, | |
2318 | TYPE_QUAL_VOLATILE)); | |
2319 | tree cvptr | |
2320 | = build_pointer_type (build_qualified_type (void_type_node, | |
2321 | TYPE_QUAL_VOLATILE | |
2322 | |TYPE_QUAL_CONST)); | |
2323 | tree boolt | |
2324 | = lang_hooks.types.type_for_size (BOOL_TYPE_SIZE, 1); | |
2325 | int i; | |
2326 | for (i = 0; i < 5; i++) | |
2327 | { | |
2328 | tree ix = build_nonstandard_integer_type (BITS_PER_UNIT * (1 << i), 1); | |
2329 | BT_FN_BOOL_VPTR_PTR_IX_INT_INT[i] | |
2330 | = build_function_type_list (boolt, vptr, ptr_type_node, ix, | |
2331 | integer_type_node, integer_type_node, | |
2332 | NULL_TREE); | |
2333 | BT_FN_IX_CONST_VPTR_INT[i] | |
2334 | = build_function_type_list (ix, cvptr, integer_type_node, NULL_TREE); | |
2335 | BT_FN_IX_VPTR_IX_INT[i] | |
2336 | = build_function_type_list (ix, vptr, ix, integer_type_node, | |
2337 | NULL_TREE); | |
2338 | BT_FN_VOID_VPTR_IX_INT[i] | |
2339 | = build_function_type_list (void_type_node, vptr, ix, | |
2340 | integer_type_node, NULL_TREE); | |
2341 | } | |
2342 | #define BT_FN_BOOL_VPTR_PTR_I1_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[0] | |
2343 | #define BT_FN_I1_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[0] | |
2344 | #define BT_FN_I1_VPTR_I1_INT BT_FN_IX_VPTR_IX_INT[0] | |
2345 | #define BT_FN_VOID_VPTR_I1_INT BT_FN_VOID_VPTR_IX_INT[0] | |
2346 | #define BT_FN_BOOL_VPTR_PTR_I2_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[1] | |
2347 | #define BT_FN_I2_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[1] | |
2348 | #define BT_FN_I2_VPTR_I2_INT BT_FN_IX_VPTR_IX_INT[1] | |
2349 | #define BT_FN_VOID_VPTR_I2_INT BT_FN_VOID_VPTR_IX_INT[1] | |
2350 | #define BT_FN_BOOL_VPTR_PTR_I4_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[2] | |
2351 | #define BT_FN_I4_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[2] | |
2352 | #define BT_FN_I4_VPTR_I4_INT BT_FN_IX_VPTR_IX_INT[2] | |
2353 | #define BT_FN_VOID_VPTR_I4_INT BT_FN_VOID_VPTR_IX_INT[2] | |
2354 | #define BT_FN_BOOL_VPTR_PTR_I8_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[3] | |
2355 | #define BT_FN_I8_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[3] | |
2356 | #define BT_FN_I8_VPTR_I8_INT BT_FN_IX_VPTR_IX_INT[3] | |
2357 | #define BT_FN_VOID_VPTR_I8_INT BT_FN_VOID_VPTR_IX_INT[3] | |
2358 | #define BT_FN_BOOL_VPTR_PTR_I16_INT_INT BT_FN_BOOL_VPTR_PTR_IX_INT_INT[4] | |
2359 | #define BT_FN_I16_CONST_VPTR_INT BT_FN_IX_CONST_VPTR_INT[4] | |
2360 | #define BT_FN_I16_VPTR_I16_INT BT_FN_IX_VPTR_IX_INT[4] | |
2361 | #define BT_FN_VOID_VPTR_I16_INT BT_FN_VOID_VPTR_IX_INT[4] | |
0e668eaf JJ |
2362 | #undef ATTR_NOTHROW_LEAF_LIST |
2363 | #define ATTR_NOTHROW_LEAF_LIST ECF_NOTHROW | ECF_LEAF | |
bc77608b JJ |
2364 | #undef ATTR_TMPURE_NOTHROW_LEAF_LIST |
2365 | #define ATTR_TMPURE_NOTHROW_LEAF_LIST ECF_TM_PURE | ATTR_NOTHROW_LEAF_LIST | |
0e668eaf JJ |
2366 | #undef ATTR_NORETURN_NOTHROW_LEAF_LIST |
2367 | #define ATTR_NORETURN_NOTHROW_LEAF_LIST ECF_NORETURN | ATTR_NOTHROW_LEAF_LIST | |
4088b790 MP |
2368 | #undef ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST |
2369 | #define ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST \ | |
2370 | ECF_CONST | ATTR_NORETURN_NOTHROW_LEAF_LIST | |
bc77608b JJ |
2371 | #undef ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST |
2372 | #define ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST \ | |
2373 | ECF_TM_PURE | ATTR_NORETURN_NOTHROW_LEAF_LIST | |
de5a5fa1 MP |
2374 | #undef ATTR_COLD_NOTHROW_LEAF_LIST |
2375 | #define ATTR_COLD_NOTHROW_LEAF_LIST \ | |
2376 | /* ECF_COLD missing */ ATTR_NOTHROW_LEAF_LIST | |
2377 | #undef ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST | |
2378 | #define ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST \ | |
2379 | /* ECF_COLD missing */ ATTR_NORETURN_NOTHROW_LEAF_LIST | |
4088b790 MP |
2380 | #undef ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST |
2381 | #define ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST \ | |
2382 | /* ECF_COLD missing */ ATTR_CONST_NORETURN_NOTHROW_LEAF_LIST | |
0bae64d5 MP |
2383 | #undef ATTR_PURE_NOTHROW_LEAF_LIST |
2384 | #define ATTR_PURE_NOTHROW_LEAF_LIST ECF_PURE | ATTR_NOTHROW_LEAF_LIST | |
0e668eaf JJ |
2385 | #undef DEF_SANITIZER_BUILTIN |
2386 | #define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \ | |
2387 | decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM, \ | |
2388 | BUILT_IN_NORMAL, NAME, NULL_TREE); \ | |
2389 | set_call_expr_flags (decl, ATTRS); \ | |
2390 | set_builtin_decl (ENUM, decl, true); | |
2391 | ||
2392 | #include "sanitizer.def" | |
2393 | ||
0bae64d5 MP |
2394 | /* -fsanitize=object-size uses __builtin_object_size, but that might |
2395 | not be available for e.g. Fortran at this point. We use | |
2396 | DEF_SANITIZER_BUILTIN here only as a convenience macro. */ | |
2397 | if ((flag_sanitize & SANITIZE_OBJECT_SIZE) | |
2398 | && !builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE)) | |
2399 | DEF_SANITIZER_BUILTIN (BUILT_IN_OBJECT_SIZE, "object_size", | |
2400 | BT_FN_SIZE_CONST_PTR_INT, | |
2401 | ATTR_PURE_NOTHROW_LEAF_LIST) | |
2402 | ||
0e668eaf JJ |
2403 | #undef DEF_SANITIZER_BUILTIN |
2404 | } | |
2405 | ||
94fce891 JJ |
2406 | /* Called via htab_traverse. Count number of emitted |
2407 | STRING_CSTs in the constant hash table. */ | |
2408 | ||
2a22f99c TS |
2409 | int |
2410 | count_string_csts (constant_descriptor_tree **slot, | |
2411 | unsigned HOST_WIDE_INT *data) | |
94fce891 | 2412 | { |
2a22f99c | 2413 | struct constant_descriptor_tree *desc = *slot; |
94fce891 JJ |
2414 | if (TREE_CODE (desc->value) == STRING_CST |
2415 | && TREE_ASM_WRITTEN (desc->value) | |
2416 | && asan_protect_global (desc->value)) | |
2a22f99c | 2417 | ++*data; |
94fce891 JJ |
2418 | return 1; |
2419 | } | |
2420 | ||
2421 | /* Helper structure to pass two parameters to | |
2422 | add_string_csts. */ | |
2423 | ||
2424 | struct asan_add_string_csts_data | |
2425 | { | |
2426 | tree type; | |
2427 | vec<constructor_elt, va_gc> *v; | |
2428 | }; | |
2429 | ||
2a22f99c | 2430 | /* Called via hash_table::traverse. Call asan_add_global |
94fce891 JJ |
2431 | on emitted STRING_CSTs from the constant hash table. */ |
2432 | ||
2a22f99c TS |
2433 | int |
2434 | add_string_csts (constant_descriptor_tree **slot, | |
2435 | asan_add_string_csts_data *aascd) | |
94fce891 | 2436 | { |
2a22f99c | 2437 | struct constant_descriptor_tree *desc = *slot; |
94fce891 JJ |
2438 | if (TREE_CODE (desc->value) == STRING_CST |
2439 | && TREE_ASM_WRITTEN (desc->value) | |
2440 | && asan_protect_global (desc->value)) | |
2441 | { | |
94fce891 JJ |
2442 | asan_add_global (SYMBOL_REF_DECL (XEXP (desc->rtl, 0)), |
2443 | aascd->type, aascd->v); | |
2444 | } | |
2445 | return 1; | |
2446 | } | |
2447 | ||
8240018b JJ |
2448 | /* Needs to be GTY(()), because cgraph_build_static_cdtor may |
2449 | invoke ggc_collect. */ | |
2450 | static GTY(()) tree asan_ctor_statements; | |
2451 | ||
37d6f666 | 2452 | /* Module-level instrumentation. |
ef1b3fda | 2453 | - Insert __asan_init_vN() into the list of CTORs. |
37d6f666 WM |
2454 | - TODO: insert redzones around globals. |
2455 | */ | |
2456 | ||
2457 | void | |
2458 | asan_finish_file (void) | |
2459 | { | |
2c8326a5 | 2460 | varpool_node *vnode; |
8240018b JJ |
2461 | unsigned HOST_WIDE_INT gcount = 0; |
2462 | ||
94fce891 JJ |
2463 | if (shadow_ptr_types[0] == NULL_TREE) |
2464 | asan_init_shadow_ptr_types (); | |
2465 | /* Avoid instrumenting code in the asan ctors/dtors. | |
2466 | We don't need to insert padding after the description strings, | |
2467 | nor after .LASAN* array. */ | |
de5a5fa1 | 2468 | flag_sanitize &= ~SANITIZE_ADDRESS; |
0e668eaf | 2469 | |
f1d15bb9 DV |
2470 | /* For user-space we want asan constructors to run first. |
2471 | Linux kernel does not support priorities other than default, and the only | |
2472 | other user of constructors is coverage. So we run with the default | |
2473 | priority. */ | |
2474 | int priority = flag_sanitize & SANITIZE_USER_ADDRESS | |
2475 | ? MAX_RESERVED_INIT_PRIORITY - 1 : DEFAULT_INIT_PRIORITY; | |
2476 | ||
c6d129b0 YG |
2477 | if (flag_sanitize & SANITIZE_USER_ADDRESS) |
2478 | { | |
2479 | tree fn = builtin_decl_implicit (BUILT_IN_ASAN_INIT); | |
2480 | append_to_statement_list (build_call_expr (fn, 0), &asan_ctor_statements); | |
2481 | } | |
8240018b | 2482 | FOR_EACH_DEFINED_VARIABLE (vnode) |
67348ccc DM |
2483 | if (TREE_ASM_WRITTEN (vnode->decl) |
2484 | && asan_protect_global (vnode->decl)) | |
8240018b | 2485 | ++gcount; |
2a22f99c TS |
2486 | hash_table<tree_descriptor_hasher> *const_desc_htab = constant_pool_htab (); |
2487 | const_desc_htab->traverse<unsigned HOST_WIDE_INT *, count_string_csts> | |
2488 | (&gcount); | |
8240018b JJ |
2489 | if (gcount) |
2490 | { | |
0e668eaf | 2491 | tree type = asan_global_struct (), var, ctor; |
8240018b | 2492 | tree dtor_statements = NULL_TREE; |
9771b263 | 2493 | vec<constructor_elt, va_gc> *v; |
8240018b JJ |
2494 | char buf[20]; |
2495 | ||
2496 | type = build_array_type_nelts (type, gcount); | |
2497 | ASM_GENERATE_INTERNAL_LABEL (buf, "LASAN", 0); | |
2498 | var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (buf), | |
2499 | type); | |
2500 | TREE_STATIC (var) = 1; | |
2501 | TREE_PUBLIC (var) = 0; | |
2502 | DECL_ARTIFICIAL (var) = 1; | |
2503 | DECL_IGNORED_P (var) = 1; | |
9771b263 | 2504 | vec_alloc (v, gcount); |
8240018b | 2505 | FOR_EACH_DEFINED_VARIABLE (vnode) |
67348ccc DM |
2506 | if (TREE_ASM_WRITTEN (vnode->decl) |
2507 | && asan_protect_global (vnode->decl)) | |
2508 | asan_add_global (vnode->decl, TREE_TYPE (type), v); | |
94fce891 JJ |
2509 | struct asan_add_string_csts_data aascd; |
2510 | aascd.type = TREE_TYPE (type); | |
2511 | aascd.v = v; | |
2a22f99c TS |
2512 | const_desc_htab->traverse<asan_add_string_csts_data *, add_string_csts> |
2513 | (&aascd); | |
8240018b JJ |
2514 | ctor = build_constructor (type, v); |
2515 | TREE_CONSTANT (ctor) = 1; | |
2516 | TREE_STATIC (ctor) = 1; | |
2517 | DECL_INITIAL (var) = ctor; | |
9041d2e6 | 2518 | varpool_node::finalize_decl (var); |
8240018b | 2519 | |
c6d129b0 | 2520 | tree fn = builtin_decl_implicit (BUILT_IN_ASAN_REGISTER_GLOBALS); |
de5a5fa1 | 2521 | tree gcount_tree = build_int_cst (pointer_sized_int_node, gcount); |
0e668eaf | 2522 | append_to_statement_list (build_call_expr (fn, 2, |
8240018b | 2523 | build_fold_addr_expr (var), |
de5a5fa1 | 2524 | gcount_tree), |
8240018b JJ |
2525 | &asan_ctor_statements); |
2526 | ||
0e668eaf JJ |
2527 | fn = builtin_decl_implicit (BUILT_IN_ASAN_UNREGISTER_GLOBALS); |
2528 | append_to_statement_list (build_call_expr (fn, 2, | |
8240018b | 2529 | build_fold_addr_expr (var), |
de5a5fa1 | 2530 | gcount_tree), |
8240018b | 2531 | &dtor_statements); |
f1d15bb9 | 2532 | cgraph_build_static_cdtor ('D', dtor_statements, priority); |
8240018b | 2533 | } |
c6d129b0 | 2534 | if (asan_ctor_statements) |
f1d15bb9 | 2535 | cgraph_build_static_cdtor ('I', asan_ctor_statements, priority); |
de5a5fa1 | 2536 | flag_sanitize |= SANITIZE_ADDRESS; |
f6d98484 JJ |
2537 | } |
2538 | ||
c62ccb9a YG |
2539 | /* Expand the ASAN_{LOAD,STORE} builtins. */ |
2540 | ||
06cefae9 | 2541 | bool |
c62ccb9a YG |
2542 | asan_expand_check_ifn (gimple_stmt_iterator *iter, bool use_calls) |
2543 | { | |
2544 | gimple g = gsi_stmt (*iter); | |
2545 | location_t loc = gimple_location (g); | |
2546 | ||
fed4de37 YG |
2547 | bool recover_p |
2548 | = (flag_sanitize & flag_sanitize_recover & SANITIZE_KERNEL_ADDRESS) != 0; | |
2549 | ||
c62ccb9a YG |
2550 | HOST_WIDE_INT flags = tree_to_shwi (gimple_call_arg (g, 0)); |
2551 | gcc_assert (flags < ASAN_CHECK_LAST); | |
2552 | bool is_scalar_access = (flags & ASAN_CHECK_SCALAR_ACCESS) != 0; | |
2553 | bool is_store = (flags & ASAN_CHECK_STORE) != 0; | |
2554 | bool is_non_zero_len = (flags & ASAN_CHECK_NON_ZERO_LEN) != 0; | |
c62ccb9a YG |
2555 | |
2556 | tree base = gimple_call_arg (g, 1); | |
2557 | tree len = gimple_call_arg (g, 2); | |
f434eb69 | 2558 | HOST_WIDE_INT align = tree_to_shwi (gimple_call_arg (g, 3)); |
c62ccb9a YG |
2559 | |
2560 | HOST_WIDE_INT size_in_bytes | |
2561 | = is_scalar_access && tree_fits_shwi_p (len) ? tree_to_shwi (len) : -1; | |
2562 | ||
2563 | if (use_calls) | |
2564 | { | |
2565 | /* Instrument using callbacks. */ | |
0d0e4a03 JJ |
2566 | gimple g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
2567 | NOP_EXPR, base); | |
c62ccb9a YG |
2568 | gimple_set_location (g, loc); |
2569 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
2570 | tree base_addr = gimple_assign_lhs (g); | |
2571 | ||
2572 | int nargs; | |
fed4de37 | 2573 | tree fun = check_func (is_store, recover_p, size_in_bytes, &nargs); |
c62ccb9a YG |
2574 | if (nargs == 1) |
2575 | g = gimple_build_call (fun, 1, base_addr); | |
2576 | else | |
2577 | { | |
2578 | gcc_assert (nargs == 2); | |
0d0e4a03 JJ |
2579 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
2580 | NOP_EXPR, len); | |
c62ccb9a YG |
2581 | gimple_set_location (g, loc); |
2582 | gsi_insert_before (iter, g, GSI_SAME_STMT); | |
2583 | tree sz_arg = gimple_assign_lhs (g); | |
2584 | g = gimple_build_call (fun, nargs, base_addr, sz_arg); | |
2585 | } | |
2586 | gimple_set_location (g, loc); | |
2587 | gsi_replace (iter, g, false); | |
2588 | return false; | |
2589 | } | |
2590 | ||
2591 | HOST_WIDE_INT real_size_in_bytes = size_in_bytes == -1 ? 1 : size_in_bytes; | |
2592 | ||
c62ccb9a YG |
2593 | tree shadow_ptr_type = shadow_ptr_types[real_size_in_bytes == 16 ? 1 : 0]; |
2594 | tree shadow_type = TREE_TYPE (shadow_ptr_type); | |
2595 | ||
2596 | gimple_stmt_iterator gsi = *iter; | |
2597 | ||
2598 | if (!is_non_zero_len) | |
2599 | { | |
2600 | /* So, the length of the memory area to asan-protect is | |
2601 | non-constant. Let's guard the generated instrumentation code | |
2602 | like: | |
2603 | ||
2604 | if (len != 0) | |
2605 | { | |
2606 | //asan instrumentation code goes here. | |
2607 | } | |
2608 | // falltrough instructions, starting with *ITER. */ | |
2609 | ||
2610 | g = gimple_build_cond (NE_EXPR, | |
2611 | len, | |
2612 | build_int_cst (TREE_TYPE (len), 0), | |
2613 | NULL_TREE, NULL_TREE); | |
2614 | gimple_set_location (g, loc); | |
2615 | ||
2616 | basic_block then_bb, fallthrough_bb; | |
538dd0b7 DM |
2617 | insert_if_then_before_iter (as_a <gcond *> (g), iter, |
2618 | /*then_more_likely_p=*/true, | |
2619 | &then_bb, &fallthrough_bb); | |
c62ccb9a YG |
2620 | /* Note that fallthrough_bb starts with the statement that was |
2621 | pointed to by ITER. */ | |
2622 | ||
2623 | /* The 'then block' of the 'if (len != 0) condition is where | |
2624 | we'll generate the asan instrumentation code now. */ | |
2625 | gsi = gsi_last_bb (then_bb); | |
2626 | } | |
2627 | ||
2628 | /* Get an iterator on the point where we can add the condition | |
2629 | statement for the instrumentation. */ | |
2630 | basic_block then_bb, else_bb; | |
2631 | gsi = create_cond_insert_point (&gsi, /*before_p*/false, | |
2632 | /*then_more_likely_p=*/false, | |
fed4de37 | 2633 | /*create_then_fallthru_edge*/recover_p, |
c62ccb9a YG |
2634 | &then_bb, |
2635 | &else_bb); | |
2636 | ||
0d0e4a03 JJ |
2637 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
2638 | NOP_EXPR, base); | |
c62ccb9a YG |
2639 | gimple_set_location (g, loc); |
2640 | gsi_insert_before (&gsi, g, GSI_NEW_STMT); | |
2641 | tree base_addr = gimple_assign_lhs (g); | |
2642 | ||
2643 | tree t = NULL_TREE; | |
2644 | if (real_size_in_bytes >= 8) | |
2645 | { | |
2646 | tree shadow = build_shadow_mem_access (&gsi, loc, base_addr, | |
2647 | shadow_ptr_type); | |
2648 | t = shadow; | |
2649 | } | |
2650 | else | |
2651 | { | |
2652 | /* Slow path for 1, 2 and 4 byte accesses. */ | |
bdea98ca MO |
2653 | /* Test (shadow != 0) |
2654 | & ((base_addr & 7) + (real_size_in_bytes - 1)) >= shadow). */ | |
2655 | tree shadow = build_shadow_mem_access (&gsi, loc, base_addr, | |
2656 | shadow_ptr_type); | |
2657 | gimple shadow_test = build_assign (NE_EXPR, shadow, 0); | |
2658 | gimple_seq seq = NULL; | |
2659 | gimple_seq_add_stmt (&seq, shadow_test); | |
2660 | /* Aligned (>= 8 bytes) can test just | |
2661 | (real_size_in_bytes - 1 >= shadow), as base_addr & 7 is known | |
2662 | to be 0. */ | |
2663 | if (align < 8) | |
c62ccb9a | 2664 | { |
bdea98ca MO |
2665 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, |
2666 | base_addr, 7)); | |
2667 | gimple_seq_add_stmt (&seq, | |
2668 | build_type_cast (shadow_type, | |
2669 | gimple_seq_last (seq))); | |
2670 | if (real_size_in_bytes > 1) | |
2671 | gimple_seq_add_stmt (&seq, | |
2672 | build_assign (PLUS_EXPR, | |
2673 | gimple_seq_last (seq), | |
2674 | real_size_in_bytes - 1)); | |
2675 | t = gimple_assign_lhs (gimple_seq_last_stmt (seq)); | |
c62ccb9a | 2676 | } |
bdea98ca MO |
2677 | else |
2678 | t = build_int_cst (shadow_type, real_size_in_bytes - 1); | |
2679 | gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, t, shadow)); | |
2680 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, | |
2681 | gimple_seq_last (seq))); | |
2682 | t = gimple_assign_lhs (gimple_seq_last (seq)); | |
2683 | gimple_seq_set_location (seq, loc); | |
2684 | gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); | |
c62ccb9a YG |
2685 | |
2686 | /* For non-constant, misaligned or otherwise weird access sizes, | |
bdea98ca MO |
2687 | check first and last byte. */ |
2688 | if (size_in_bytes == -1) | |
c62ccb9a | 2689 | { |
0d0e4a03 JJ |
2690 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
2691 | MINUS_EXPR, len, | |
2692 | build_int_cst (pointer_sized_int_node, 1)); | |
c62ccb9a YG |
2693 | gimple_set_location (g, loc); |
2694 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
2695 | tree last = gimple_assign_lhs (g); | |
0d0e4a03 JJ |
2696 | g = gimple_build_assign (make_ssa_name (pointer_sized_int_node), |
2697 | PLUS_EXPR, base_addr, last); | |
c62ccb9a YG |
2698 | gimple_set_location (g, loc); |
2699 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
2700 | tree base_end_addr = gimple_assign_lhs (g); | |
2701 | ||
2702 | tree shadow = build_shadow_mem_access (&gsi, loc, base_end_addr, | |
2703 | shadow_ptr_type); | |
2704 | gimple shadow_test = build_assign (NE_EXPR, shadow, 0); | |
2705 | gimple_seq seq = NULL; | |
2706 | gimple_seq_add_stmt (&seq, shadow_test); | |
2707 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, | |
2708 | base_end_addr, 7)); | |
2709 | gimple_seq_add_stmt (&seq, build_type_cast (shadow_type, | |
2710 | gimple_seq_last (seq))); | |
2711 | gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, | |
2712 | gimple_seq_last (seq), | |
2713 | shadow)); | |
2714 | gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, | |
2715 | gimple_seq_last (seq))); | |
bdea98ca MO |
2716 | gimple_seq_add_stmt (&seq, build_assign (BIT_IOR_EXPR, t, |
2717 | gimple_seq_last (seq))); | |
c62ccb9a YG |
2718 | t = gimple_assign_lhs (gimple_seq_last (seq)); |
2719 | gimple_seq_set_location (seq, loc); | |
2720 | gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); | |
2721 | } | |
2722 | } | |
2723 | ||
2724 | g = gimple_build_cond (NE_EXPR, t, build_int_cst (TREE_TYPE (t), 0), | |
2725 | NULL_TREE, NULL_TREE); | |
2726 | gimple_set_location (g, loc); | |
2727 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
2728 | ||
2729 | /* Generate call to the run-time library (e.g. __asan_report_load8). */ | |
2730 | gsi = gsi_start_bb (then_bb); | |
2731 | int nargs; | |
fed4de37 | 2732 | tree fun = report_error_func (is_store, recover_p, size_in_bytes, &nargs); |
c62ccb9a YG |
2733 | g = gimple_build_call (fun, nargs, base_addr, len); |
2734 | gimple_set_location (g, loc); | |
2735 | gsi_insert_after (&gsi, g, GSI_NEW_STMT); | |
2736 | ||
2737 | gsi_remove (iter, true); | |
2738 | *iter = gsi_start_bb (else_bb); | |
2739 | ||
2740 | return true; | |
2741 | } | |
2742 | ||
37d6f666 WM |
2743 | /* Instrument the current function. */ |
2744 | ||
2745 | static unsigned int | |
2746 | asan_instrument (void) | |
2747 | { | |
f6d98484 | 2748 | if (shadow_ptr_types[0] == NULL_TREE) |
94fce891 | 2749 | asan_init_shadow_ptr_types (); |
37d6f666 | 2750 | transform_statements (); |
37d6f666 WM |
2751 | return 0; |
2752 | } | |
2753 | ||
2754 | static bool | |
2755 | gate_asan (void) | |
2756 | { | |
de5a5fa1 | 2757 | return (flag_sanitize & SANITIZE_ADDRESS) != 0 |
e664c61c | 2758 | && !lookup_attribute ("no_sanitize_address", |
77bc5132 | 2759 | DECL_ATTRIBUTES (current_function_decl)); |
37d6f666 WM |
2760 | } |
2761 | ||
27a4cd48 DM |
2762 | namespace { |
2763 | ||
2764 | const pass_data pass_data_asan = | |
37d6f666 | 2765 | { |
27a4cd48 DM |
2766 | GIMPLE_PASS, /* type */ |
2767 | "asan", /* name */ | |
2768 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
2769 | TV_NONE, /* tv_id */ |
2770 | ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ | |
2771 | 0, /* properties_provided */ | |
2772 | 0, /* properties_destroyed */ | |
2773 | 0, /* todo_flags_start */ | |
3bea341f | 2774 | TODO_update_ssa, /* todo_flags_finish */ |
37d6f666 | 2775 | }; |
f6d98484 | 2776 | |
27a4cd48 DM |
2777 | class pass_asan : public gimple_opt_pass |
2778 | { | |
2779 | public: | |
c3284718 RS |
2780 | pass_asan (gcc::context *ctxt) |
2781 | : gimple_opt_pass (pass_data_asan, ctxt) | |
27a4cd48 DM |
2782 | {} |
2783 | ||
2784 | /* opt_pass methods: */ | |
65d3284b | 2785 | opt_pass * clone () { return new pass_asan (m_ctxt); } |
1a3d085c | 2786 | virtual bool gate (function *) { return gate_asan (); } |
be55bfe6 | 2787 | virtual unsigned int execute (function *) { return asan_instrument (); } |
27a4cd48 DM |
2788 | |
2789 | }; // class pass_asan | |
2790 | ||
2791 | } // anon namespace | |
2792 | ||
2793 | gimple_opt_pass * | |
2794 | make_pass_asan (gcc::context *ctxt) | |
2795 | { | |
2796 | return new pass_asan (ctxt); | |
2797 | } | |
2798 | ||
27a4cd48 DM |
2799 | namespace { |
2800 | ||
2801 | const pass_data pass_data_asan_O0 = | |
dfb9e332 | 2802 | { |
27a4cd48 DM |
2803 | GIMPLE_PASS, /* type */ |
2804 | "asan0", /* name */ | |
2805 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
2806 | TV_NONE, /* tv_id */ |
2807 | ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ | |
2808 | 0, /* properties_provided */ | |
2809 | 0, /* properties_destroyed */ | |
2810 | 0, /* todo_flags_start */ | |
3bea341f | 2811 | TODO_update_ssa, /* todo_flags_finish */ |
dfb9e332 JJ |
2812 | }; |
2813 | ||
27a4cd48 DM |
2814 | class pass_asan_O0 : public gimple_opt_pass |
2815 | { | |
2816 | public: | |
c3284718 RS |
2817 | pass_asan_O0 (gcc::context *ctxt) |
2818 | : gimple_opt_pass (pass_data_asan_O0, ctxt) | |
27a4cd48 DM |
2819 | {} |
2820 | ||
2821 | /* opt_pass methods: */ | |
1a3d085c | 2822 | virtual bool gate (function *) { return !optimize && gate_asan (); } |
be55bfe6 | 2823 | virtual unsigned int execute (function *) { return asan_instrument (); } |
27a4cd48 DM |
2824 | |
2825 | }; // class pass_asan_O0 | |
2826 | ||
2827 | } // anon namespace | |
2828 | ||
2829 | gimple_opt_pass * | |
2830 | make_pass_asan_O0 (gcc::context *ctxt) | |
2831 | { | |
2832 | return new pass_asan_O0 (ctxt); | |
2833 | } | |
2834 | ||
f6d98484 | 2835 | #include "gt-asan.h" |