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