]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sanopt.c
/cp
[thirdparty/gcc.git] / gcc / sanopt.c
CommitLineData
6b5813f5 1/* Optimize and expand sanitizer functions.
fbd26352 2 Copyright (C) 2014-2019 Free Software Foundation, Inc.
6b5813f5 3 Contributed by Marek Polacek <polacek@redhat.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#include "config.h"
22#include "system.h"
23#include "coretypes.h"
9ef16211 24#include "backend.h"
6b5813f5 25#include "tree.h"
9ef16211 26#include "gimple.h"
05f7c0e8 27#include "ssa.h"
7c29e30e 28#include "tree-pass.h"
7c29e30e 29#include "tree-ssa-operands.h"
30#include "gimple-pretty-print.h"
9ef16211 31#include "fold-const.h"
6b5813f5 32#include "gimple-iterator.h"
30a86690 33#include "stringpool.h"
34#include "attribs.h"
6b5813f5 35#include "asan.h"
6b5813f5 36#include "ubsan.h"
37#include "params.h"
f4ff098a 38#include "tree-hash-traits.h"
a940fdc7 39#include "gimple-ssa.h"
40#include "tree-phinodes.h"
41#include "ssa-iterators.h"
f91fab92 42#include "gimplify.h"
43#include "gimple-iterator.h"
44#include "gimple-walk.h"
45#include "cfghooks.h"
46#include "tree-dfa.h"
47#include "tree-ssa.h"
df7bbf08 48#include "varasm.h"
6b5813f5 49
6b5813f5 50/* This is used to carry information about basic blocks. It is
51 attached to the AUX field of the standard CFG block. */
52
53struct sanopt_info
54{
f407b74a 55 /* True if this BB might call (directly or indirectly) free/munmap
56 or similar operation. */
57 bool has_freeing_call_p;
58
59 /* True if HAS_FREEING_CALL_P flag has been computed. */
60 bool has_freeing_call_computed_p;
61
62 /* True if there is a block with HAS_FREEING_CALL_P flag set
63 on any path between an immediate dominator of BB, denoted
64 imm(BB), and BB. */
65 bool imm_dom_path_with_freeing_call_p;
66
67 /* True if IMM_DOM_PATH_WITH_FREEING_CALL_P has been computed. */
68 bool imm_dom_path_with_freeing_call_computed_p;
69
70 /* Number of possibly freeing calls encountered in this bb
71 (so far). */
72 uint64_t freeing_call_events;
73
74 /* True if BB is currently being visited during computation
75 of IMM_DOM_PATH_WITH_FREEING_CALL_P flag. */
76 bool being_visited_p;
77
78 /* True if this BB has been visited in the dominator walk. */
6b5813f5 79 bool visited_p;
80};
81
977a4cdf 82/* If T has a single definition of form T = T2, return T2. */
83
84static tree
85maybe_get_single_definition (tree t)
86{
87 if (TREE_CODE (t) == SSA_NAME)
88 {
42acab1c 89 gimple *g = SSA_NAME_DEF_STMT (t);
977a4cdf 90 if (gimple_assign_single_p (g))
91 return gimple_assign_rhs1 (g);
92 }
93 return NULL_TREE;
94}
95
32cf7025 96/* Tree triplet for vptr_check_map. */
97struct sanopt_tree_triplet
98{
99 tree t1, t2, t3;
100};
101
102/* Traits class for tree triplet hash maps below. */
103
091deb37 104struct sanopt_tree_triplet_hash : typed_noop_remove <sanopt_tree_triplet>
32cf7025 105{
091deb37 106 typedef sanopt_tree_triplet value_type;
107 typedef sanopt_tree_triplet compare_type;
108
df7bbf08 109 static hashval_t
32cf7025 110 hash (const sanopt_tree_triplet &ref)
111 {
112 inchash::hash hstate (0);
113 inchash::add_expr (ref.t1, hstate);
114 inchash::add_expr (ref.t2, hstate);
115 inchash::add_expr (ref.t3, hstate);
116 return hstate.end ();
117 }
118
df7bbf08 119 static bool
091deb37 120 equal (const sanopt_tree_triplet &ref1, const sanopt_tree_triplet &ref2)
32cf7025 121 {
122 return operand_equal_p (ref1.t1, ref2.t1, 0)
123 && operand_equal_p (ref1.t2, ref2.t2, 0)
124 && operand_equal_p (ref1.t3, ref2.t3, 0);
125 }
126
df7bbf08 127 static void
091deb37 128 mark_deleted (sanopt_tree_triplet &ref)
32cf7025 129 {
091deb37 130 ref.t1 = reinterpret_cast<tree> (1);
32cf7025 131 }
132
df7bbf08 133 static void
091deb37 134 mark_empty (sanopt_tree_triplet &ref)
32cf7025 135 {
091deb37 136 ref.t1 = NULL;
32cf7025 137 }
138
df7bbf08 139 static bool
091deb37 140 is_deleted (const sanopt_tree_triplet &ref)
32cf7025 141 {
df7bbf08 142 return ref.t1 == reinterpret_cast<tree> (1);
32cf7025 143 }
144
df7bbf08 145 static bool
091deb37 146 is_empty (const sanopt_tree_triplet &ref)
32cf7025 147 {
091deb37 148 return ref.t1 == NULL;
32cf7025 149 }
150};
151
df7bbf08 152/* Tree couple for ptr_check_map. */
153struct sanopt_tree_couple
154{
155 tree ptr;
156 bool pos_p;
157};
158
159/* Traits class for tree triplet hash maps below. */
160
161struct sanopt_tree_couple_hash : typed_noop_remove <sanopt_tree_couple>
162{
163 typedef sanopt_tree_couple value_type;
164 typedef sanopt_tree_couple compare_type;
165
166 static hashval_t
167 hash (const sanopt_tree_couple &ref)
168 {
169 inchash::hash hstate (0);
170 inchash::add_expr (ref.ptr, hstate);
171 hstate.add_int (ref.pos_p);
172 return hstate.end ();
173 }
174
175 static bool
176 equal (const sanopt_tree_couple &ref1, const sanopt_tree_couple &ref2)
177 {
178 return operand_equal_p (ref1.ptr, ref2.ptr, 0)
179 && ref1.pos_p == ref2.pos_p;
180 }
181
182 static void
183 mark_deleted (sanopt_tree_couple &ref)
184 {
185 ref.ptr = reinterpret_cast<tree> (1);
186 }
187
188 static void
189 mark_empty (sanopt_tree_couple &ref)
190 {
191 ref.ptr = NULL;
192 }
193
194 static bool
195 is_deleted (const sanopt_tree_couple &ref)
196 {
197 return ref.ptr == reinterpret_cast<tree> (1);
198 }
199
200 static bool
201 is_empty (const sanopt_tree_couple &ref)
202 {
203 return ref.ptr == NULL;
204 }
205};
206
6b5813f5 207/* This is used to carry various hash maps and variables used
208 in sanopt_optimize_walker. */
209
251317e4 210class sanopt_ctx
6b5813f5 211{
251317e4 212public:
6b5813f5 213 /* This map maps a pointer (the first argument of UBSAN_NULL) to
214 a vector of UBSAN_NULL call statements that check this pointer. */
42acab1c 215 hash_map<tree, auto_vec<gimple *> > null_check_map;
6b5813f5 216
f407b74a 217 /* This map maps a pointer (the second argument of ASAN_CHECK) to
218 a vector of ASAN_CHECK call statements that check the access. */
42acab1c 219 hash_map<tree_operand_hash, auto_vec<gimple *> > asan_check_map;
32cf7025 220
221 /* This map maps a tree triplet (the first, second and fourth argument
222 of UBSAN_VPTR) to a vector of UBSAN_VPTR call statements that check
223 that virtual table pointer. */
42acab1c 224 hash_map<sanopt_tree_triplet_hash, auto_vec<gimple *> > vptr_check_map;
f407b74a 225
df7bbf08 226 /* This map maps a couple (tree and boolean) to a vector of UBSAN_PTR
227 call statements that check that pointer overflow. */
228 hash_map<sanopt_tree_couple_hash, auto_vec<gimple *> > ptr_check_map;
229
6b5813f5 230 /* Number of IFN_ASAN_CHECK statements. */
231 int asan_num_accesses;
6b5813f5 232
05f7c0e8 233 /* True when the current functions constains an ASAN_MARK. */
234 bool contains_asan_mark;
235};
6b5813f5 236
f407b74a 237/* Return true if there might be any call to free/munmap operation
238 on any path in between DOM (which should be imm(BB)) and BB. */
239
240static bool
241imm_dom_path_with_freeing_call (basic_block bb, basic_block dom)
242{
243 sanopt_info *info = (sanopt_info *) bb->aux;
244 edge e;
245 edge_iterator ei;
246
247 if (info->imm_dom_path_with_freeing_call_computed_p)
248 return info->imm_dom_path_with_freeing_call_p;
249
250 info->being_visited_p = true;
251
252 FOR_EACH_EDGE (e, ei, bb->preds)
253 {
254 sanopt_info *pred_info = (sanopt_info *) e->src->aux;
255
256 if (e->src == dom)
257 continue;
258
259 if ((pred_info->imm_dom_path_with_freeing_call_computed_p
260 && pred_info->imm_dom_path_with_freeing_call_p)
261 || (pred_info->has_freeing_call_computed_p
262 && pred_info->has_freeing_call_p))
263 {
264 info->imm_dom_path_with_freeing_call_computed_p = true;
265 info->imm_dom_path_with_freeing_call_p = true;
266 info->being_visited_p = false;
267 return true;
268 }
269 }
270
271 FOR_EACH_EDGE (e, ei, bb->preds)
272 {
273 sanopt_info *pred_info = (sanopt_info *) e->src->aux;
274
275 if (e->src == dom)
276 continue;
277
278 if (pred_info->has_freeing_call_computed_p)
279 continue;
280
281 gimple_stmt_iterator gsi;
282 for (gsi = gsi_start_bb (e->src); !gsi_end_p (gsi); gsi_next (&gsi))
283 {
42acab1c 284 gimple *stmt = gsi_stmt (gsi);
a07d5339 285 gasm *asm_stmt;
f407b74a 286
a07d5339 287 if ((is_gimple_call (stmt) && !nonfreeing_call_p (stmt))
288 || ((asm_stmt = dyn_cast <gasm *> (stmt))
289 && (gimple_asm_clobbers_memory_p (asm_stmt)
290 || gimple_asm_volatile_p (asm_stmt))))
f407b74a 291 {
292 pred_info->has_freeing_call_p = true;
293 break;
294 }
295 }
296
297 pred_info->has_freeing_call_computed_p = true;
298 if (pred_info->has_freeing_call_p)
299 {
300 info->imm_dom_path_with_freeing_call_computed_p = true;
301 info->imm_dom_path_with_freeing_call_p = true;
302 info->being_visited_p = false;
303 return true;
304 }
305 }
306
307 FOR_EACH_EDGE (e, ei, bb->preds)
308 {
309 if (e->src == dom)
310 continue;
311
312 basic_block src;
313 for (src = e->src; src != dom; )
314 {
315 sanopt_info *pred_info = (sanopt_info *) src->aux;
316 if (pred_info->being_visited_p)
317 break;
318 basic_block imm = get_immediate_dominator (CDI_DOMINATORS, src);
319 if (imm_dom_path_with_freeing_call (src, imm))
320 {
321 info->imm_dom_path_with_freeing_call_computed_p = true;
322 info->imm_dom_path_with_freeing_call_p = true;
323 info->being_visited_p = false;
324 return true;
325 }
326 src = imm;
327 }
328 }
329
330 info->imm_dom_path_with_freeing_call_computed_p = true;
331 info->imm_dom_path_with_freeing_call_p = false;
332 info->being_visited_p = false;
333 return false;
334}
335
977a4cdf 336/* Get the first dominating check from the list of stored checks.
337 Non-dominating checks are silently dropped. */
338
42acab1c 339static gimple *
340maybe_get_dominating_check (auto_vec<gimple *> &v)
977a4cdf 341{
342 for (; !v.is_empty (); v.pop ())
343 {
42acab1c 344 gimple *g = v.last ();
977a4cdf 345 sanopt_info *si = (sanopt_info *) gimple_bb (g)->aux;
346 if (!si->visited_p)
347 /* At this point we shouldn't have any statements
348 that aren't dominating the current BB. */
349 return g;
350 }
351 return NULL;
352}
353
f407b74a 354/* Optimize away redundant UBSAN_NULL calls. */
355
356static bool
2e966e2a 357maybe_optimize_ubsan_null_ifn (class sanopt_ctx *ctx, gimple *stmt)
f407b74a 358{
359 gcc_assert (gimple_call_num_args (stmt) == 3);
360 tree ptr = gimple_call_arg (stmt, 0);
361 tree cur_align = gimple_call_arg (stmt, 2);
362 gcc_assert (TREE_CODE (cur_align) == INTEGER_CST);
363 bool remove = false;
364
42acab1c 365 auto_vec<gimple *> &v = ctx->null_check_map.get_or_insert (ptr);
366 gimple *g = maybe_get_dominating_check (v);
977a4cdf 367 if (!g)
f407b74a 368 {
369 /* For this PTR we don't have any UBSAN_NULL stmts recorded, so there's
370 nothing to optimize yet. */
371 v.safe_push (stmt);
372 return false;
373 }
374
375 /* We already have recorded a UBSAN_NULL check for this pointer. Perhaps we
376 can drop this one. But only if this check doesn't specify stricter
377 alignment. */
f407b74a 378
977a4cdf 379 tree align = gimple_call_arg (g, 2);
380 int kind = tree_to_shwi (gimple_call_arg (g, 1));
381 /* If this is a NULL pointer check where we had segv anyway, we can
382 remove it. */
383 if (integer_zerop (align)
384 && (kind == UBSAN_LOAD_OF
385 || kind == UBSAN_STORE_OF
386 || kind == UBSAN_MEMBER_ACCESS))
387 remove = true;
388 /* Otherwise remove the check in non-recovering mode, or if the
389 stmts have same location. */
390 else if (integer_zerop (align))
391 remove = (flag_sanitize_recover & SANITIZE_NULL) == 0
392 || flag_sanitize_undefined_trap_on_error
393 || gimple_location (g) == gimple_location (stmt);
394 else if (tree_int_cst_le (cur_align, align))
395 remove = (flag_sanitize_recover & SANITIZE_ALIGNMENT) == 0
396 || flag_sanitize_undefined_trap_on_error
397 || gimple_location (g) == gimple_location (stmt);
398
399 if (!remove && gimple_bb (g) == gimple_bb (stmt)
400 && tree_int_cst_compare (cur_align, align) == 0)
401 v.pop ();
f407b74a 402
403 if (!remove)
404 v.safe_push (stmt);
405 return remove;
406}
407
df7bbf08 408/* Return true when pointer PTR for a given CUR_OFFSET is already sanitized
409 in a given sanitization context CTX. */
410
411static bool
412has_dominating_ubsan_ptr_check (sanopt_ctx *ctx, tree ptr,
413 offset_int &cur_offset)
414{
415 bool pos_p = !wi::neg_p (cur_offset);
416 sanopt_tree_couple couple;
417 couple.ptr = ptr;
418 couple.pos_p = pos_p;
419
420 auto_vec<gimple *> &v = ctx->ptr_check_map.get_or_insert (couple);
421 gimple *g = maybe_get_dominating_check (v);
422 if (!g)
423 return false;
424
425 /* We already have recorded a UBSAN_PTR check for this pointer. Perhaps we
426 can drop this one. But only if this check doesn't specify larger offset.
427 */
428 tree offset = gimple_call_arg (g, 1);
429 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
430 offset_int ooffset = wi::sext (wi::to_offset (offset), POINTER_SIZE);
431
432 if (pos_p)
433 {
434 if (wi::les_p (cur_offset, ooffset))
435 return true;
436 }
437 else if (!pos_p && wi::les_p (ooffset, cur_offset))
438 return true;
439
440 return false;
441}
442
443/* Record UBSAN_PTR check of given context CTX. Register pointer PTR on
444 a given OFFSET that it's handled by GIMPLE STMT. */
445
446static void
447record_ubsan_ptr_check_stmt (sanopt_ctx *ctx, gimple *stmt, tree ptr,
448 const offset_int &offset)
449{
450 sanopt_tree_couple couple;
451 couple.ptr = ptr;
452 couple.pos_p = !wi::neg_p (offset);
453
454 auto_vec<gimple *> &v = ctx->ptr_check_map.get_or_insert (couple);
455 v.safe_push (stmt);
456}
457
458/* Optimize away redundant UBSAN_PTR calls. */
459
460static bool
461maybe_optimize_ubsan_ptr_ifn (sanopt_ctx *ctx, gimple *stmt)
462{
81bc0f0f 463 poly_int64 bitsize, pbitpos;
df7bbf08 464 machine_mode mode;
465 int volatilep = 0, reversep, unsignedp = 0;
466 tree offset;
467
468 gcc_assert (gimple_call_num_args (stmt) == 2);
469 tree ptr = gimple_call_arg (stmt, 0);
470 tree off = gimple_call_arg (stmt, 1);
471
472 if (TREE_CODE (off) != INTEGER_CST)
473 return false;
474
475 if (integer_zerop (off))
476 return true;
477
478 offset_int cur_offset = wi::sext (wi::to_offset (off), POINTER_SIZE);
479 if (has_dominating_ubsan_ptr_check (ctx, ptr, cur_offset))
480 return true;
481
482 tree base = ptr;
483 if (TREE_CODE (base) == ADDR_EXPR)
484 {
485 base = TREE_OPERAND (base, 0);
486
81bc0f0f 487 HOST_WIDE_INT bitpos;
488 base = get_inner_reference (base, &bitsize, &pbitpos, &offset, &mode,
df7bbf08 489 &unsignedp, &reversep, &volatilep);
a72aeafe 490 if ((offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
81bc0f0f 491 && DECL_P (base)
5d8527be 492 && !DECL_REGISTER (base)
81bc0f0f 493 && pbitpos.is_constant (&bitpos))
df7bbf08 494 {
a72aeafe 495 offset_int expr_offset;
496 if (offset)
497 expr_offset = wi::to_offset (offset) + bitpos / BITS_PER_UNIT;
498 else
499 expr_offset = bitpos / BITS_PER_UNIT;
500 expr_offset = wi::sext (expr_offset, POINTER_SIZE);
df7bbf08 501 offset_int total_offset = expr_offset + cur_offset;
502 if (total_offset != wi::sext (total_offset, POINTER_SIZE))
503 {
504 record_ubsan_ptr_check_stmt (ctx, stmt, ptr, cur_offset);
505 return false;
506 }
507
508 /* If BASE is a fixed size automatic variable or
509 global variable defined in the current TU, we don't have
510 to instrument anything if offset is within address
511 of the variable. */
512 if ((VAR_P (base)
513 || TREE_CODE (base) == PARM_DECL
514 || TREE_CODE (base) == RESULT_DECL)
515 && DECL_SIZE_UNIT (base)
516 && TREE_CODE (DECL_SIZE_UNIT (base)) == INTEGER_CST
517 && (!is_global_var (base) || decl_binds_to_current_def_p (base)))
518 {
519 offset_int base_size = wi::to_offset (DECL_SIZE_UNIT (base));
a72aeafe 520 if (!wi::neg_p (expr_offset)
df7bbf08 521 && wi::les_p (total_offset, base_size))
522 {
523 if (!wi::neg_p (total_offset)
524 && wi::les_p (total_offset, base_size))
525 return true;
526 }
527 }
528
529 /* Following expression: UBSAN_PTR (&MEM_REF[ptr + x], y) can be
530 handled as follows:
531
532 1) sign (x) == sign (y), then check for dominating check of (x + y)
533 2) sign (x) != sign (y), then first check if we have a dominating
534 check for ptr + x. If so, then we have 2 situations:
535 a) sign (x) == sign (x + y), here we are done, example:
536 UBSAN_PTR (&MEM_REF[ptr + 100], -50)
537 b) check for dominating check of ptr + x + y.
538 */
539
540 bool sign_cur_offset = !wi::neg_p (cur_offset);
a72aeafe 541 bool sign_expr_offset = !wi::neg_p (expr_offset);
df7bbf08 542
543 tree base_addr
544 = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (base)), base);
545
546 bool add = false;
547 if (sign_cur_offset == sign_expr_offset)
548 {
549 if (has_dominating_ubsan_ptr_check (ctx, base_addr, total_offset))
550 return true;
551 else
552 add = true;
553 }
554 else
555 {
556 if (!has_dominating_ubsan_ptr_check (ctx, base_addr, expr_offset))
557 ; /* Don't record base_addr + expr_offset, it's not a guarding
558 check. */
559 else
560 {
561 bool sign_total_offset = !wi::neg_p (total_offset);
562 if (sign_expr_offset == sign_total_offset)
563 return true;
564 else
565 {
566 if (has_dominating_ubsan_ptr_check (ctx, base_addr,
567 total_offset))
568 return true;
569 else
570 add = true;
571 }
572 }
573 }
574
575 /* Record a new dominating check for base_addr + total_offset. */
576 if (add && !operand_equal_p (base, base_addr, 0))
577 record_ubsan_ptr_check_stmt (ctx, stmt, base_addr,
578 total_offset);
579 }
580 }
581
582 /* For this PTR we don't have any UBSAN_PTR stmts recorded, so there's
583 nothing to optimize yet. */
584 record_ubsan_ptr_check_stmt (ctx, stmt, ptr, cur_offset);
585
586 return false;
587}
588
32cf7025 589/* Optimize away redundant UBSAN_VPTR calls. The second argument
590 is the value loaded from the virtual table, so rely on FRE to find out
591 when we can actually optimize. */
592
593static bool
2e966e2a 594maybe_optimize_ubsan_vptr_ifn (class sanopt_ctx *ctx, gimple *stmt)
32cf7025 595{
596 gcc_assert (gimple_call_num_args (stmt) == 5);
597 sanopt_tree_triplet triplet;
598 triplet.t1 = gimple_call_arg (stmt, 0);
599 triplet.t2 = gimple_call_arg (stmt, 1);
600 triplet.t3 = gimple_call_arg (stmt, 3);
601
42acab1c 602 auto_vec<gimple *> &v = ctx->vptr_check_map.get_or_insert (triplet);
603 gimple *g = maybe_get_dominating_check (v);
32cf7025 604 if (!g)
605 {
606 /* For this PTR we don't have any UBSAN_VPTR stmts recorded, so there's
607 nothing to optimize yet. */
608 v.safe_push (stmt);
609 return false;
610 }
611
612 return true;
613}
614
977a4cdf 615/* Returns TRUE if ASan check of length LEN in block BB can be removed
616 if preceded by checks in V. */
f407b74a 617
618static bool
42acab1c 619can_remove_asan_check (auto_vec<gimple *> &v, tree len, basic_block bb)
f407b74a 620{
f407b74a 621 unsigned int i;
42acab1c 622 gimple *g;
623 gimple *to_pop = NULL;
f407b74a 624 bool remove = false;
625 basic_block last_bb = bb;
626 bool cleanup = false;
627
628 FOR_EACH_VEC_ELT_REVERSE (v, i, g)
629 {
630 basic_block gbb = gimple_bb (g);
631 sanopt_info *si = (sanopt_info *) gbb->aux;
632 if (gimple_uid (g) < si->freeing_call_events)
633 {
634 /* If there is a potentially freeing call after g in gbb, we should
635 remove it from the vector, can't use in optimization. */
636 cleanup = true;
637 continue;
638 }
639
f407b74a 640 tree glen = gimple_call_arg (g, 2);
977a4cdf 641 gcc_assert (TREE_CODE (glen) == INTEGER_CST);
642
f407b74a 643 /* If we've checked only smaller length than we want to check now,
644 we can't remove the current stmt. If g is in the same basic block,
645 we want to remove it though, as the current stmt is better. */
646 if (tree_int_cst_lt (glen, len))
647 {
648 if (gbb == bb)
649 {
650 to_pop = g;
651 cleanup = true;
652 }
653 continue;
654 }
655
656 while (last_bb != gbb)
657 {
658 /* Paths from last_bb to bb have been checked before.
659 gbb is necessarily a dominator of last_bb, but not necessarily
660 immediate dominator. */
661 if (((sanopt_info *) last_bb->aux)->freeing_call_events)
662 break;
663
664 basic_block imm = get_immediate_dominator (CDI_DOMINATORS, last_bb);
665 gcc_assert (imm);
666 if (imm_dom_path_with_freeing_call (last_bb, imm))
667 break;
668
669 last_bb = imm;
670 }
671 if (last_bb == gbb)
672 remove = true;
673 break;
674 }
675
676 if (cleanup)
677 {
678 unsigned int j = 0, l = v.length ();
679 for (i = 0; i < l; i++)
680 if (v[i] != to_pop
681 && (gimple_uid (v[i])
682 == ((sanopt_info *)
683 gimple_bb (v[i])->aux)->freeing_call_events))
684 {
685 if (i != j)
686 v[j] = v[i];
687 j++;
688 }
689 v.truncate (j);
690 }
691
977a4cdf 692 return remove;
693}
694
695/* Optimize away redundant ASAN_CHECK calls. */
696
697static bool
2e966e2a 698maybe_optimize_asan_check_ifn (class sanopt_ctx *ctx, gimple *stmt)
977a4cdf 699{
700 gcc_assert (gimple_call_num_args (stmt) == 4);
701 tree ptr = gimple_call_arg (stmt, 1);
702 tree len = gimple_call_arg (stmt, 2);
703 basic_block bb = gimple_bb (stmt);
704 sanopt_info *info = (sanopt_info *) bb->aux;
705
706 if (TREE_CODE (len) != INTEGER_CST)
707 return false;
708 if (integer_zerop (len))
709 return false;
710
711 gimple_set_uid (stmt, info->freeing_call_events);
712
42acab1c 713 auto_vec<gimple *> *ptr_checks = &ctx->asan_check_map.get_or_insert (ptr);
977a4cdf 714
715 tree base_addr = maybe_get_single_definition (ptr);
42acab1c 716 auto_vec<gimple *> *base_checks = NULL;
977a4cdf 717 if (base_addr)
718 {
719 base_checks = &ctx->asan_check_map.get_or_insert (base_addr);
720 /* Original pointer might have been invalidated. */
721 ptr_checks = ctx->asan_check_map.get (ptr);
722 }
723
42acab1c 724 gimple *g = maybe_get_dominating_check (*ptr_checks);
725 gimple *g2 = NULL;
977a4cdf 726
661191fd 727 if (base_checks)
977a4cdf 728 /* Try with base address as well. */
661191fd 729 g2 = maybe_get_dominating_check (*base_checks);
977a4cdf 730
661191fd 731 if (g == NULL && g2 == NULL)
977a4cdf 732 {
733 /* For this PTR we don't have any ASAN_CHECK stmts recorded, so there's
734 nothing to optimize yet. */
735 ptr_checks->safe_push (stmt);
736 if (base_checks)
737 base_checks->safe_push (stmt);
738 return false;
739 }
740
741 bool remove = false;
742
743 if (ptr_checks)
744 remove = can_remove_asan_check (*ptr_checks, len, bb);
745
746 if (!remove && base_checks)
747 /* Try with base address as well. */
748 remove = can_remove_asan_check (*base_checks, len, bb);
749
f407b74a 750 if (!remove)
977a4cdf 751 {
752 ptr_checks->safe_push (stmt);
753 if (base_checks)
754 base_checks->safe_push (stmt);
755 }
756
f407b74a 757 return remove;
758}
759
760/* Try to optimize away redundant UBSAN_NULL and ASAN_CHECK calls.
761
6b5813f5 762 We walk blocks in the CFG via a depth first search of the dominator
f407b74a 763 tree; we push unique UBSAN_NULL or ASAN_CHECK statements into a vector
764 in the NULL_CHECK_MAP or ASAN_CHECK_MAP hash maps as we enter the
765 blocks. When leaving a block, we mark the block as visited; then
766 when checking the statements in the vector, we ignore statements that
767 are coming from already visited blocks, because these cannot dominate
768 anything anymore. CTX is a sanopt context. */
6b5813f5 769
770static void
2e966e2a 771sanopt_optimize_walker (basic_block bb, class sanopt_ctx *ctx)
6b5813f5 772{
773 basic_block son;
774 gimple_stmt_iterator gsi;
f407b74a 775 sanopt_info *info = (sanopt_info *) bb->aux;
977a4cdf 776 bool asan_check_optimize = (flag_sanitize & SANITIZE_ADDRESS) != 0;
6b5813f5 777
778 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
779 {
42acab1c 780 gimple *stmt = gsi_stmt (gsi);
6b5813f5 781 bool remove = false;
782
f407b74a 783 if (!is_gimple_call (stmt))
784 {
785 /* Handle asm volatile or asm with "memory" clobber
786 the same as potentionally freeing call. */
1a91d914 787 gasm *asm_stmt = dyn_cast <gasm *> (stmt);
788 if (asm_stmt
f407b74a 789 && asan_check_optimize
1a91d914 790 && (gimple_asm_clobbers_memory_p (asm_stmt)
791 || gimple_asm_volatile_p (asm_stmt)))
f407b74a 792 info->freeing_call_events++;
793 gsi_next (&gsi);
794 continue;
795 }
796
797 if (asan_check_optimize && !nonfreeing_call_p (stmt))
798 info->freeing_call_events++;
799
a940fdc7 800 /* If __asan_before_dynamic_init ("module"); is followed by
801 __asan_after_dynamic_init (); without intervening memory loads/stores,
802 there is nothing to guard, so optimize both away. */
803 if (asan_check_optimize
804 && gimple_call_builtin_p (stmt, BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT))
805 {
806 use_operand_p use;
807 gimple *use_stmt;
808 if (single_imm_use (gimple_vdef (stmt), &use, &use_stmt))
809 {
810 if (is_gimple_call (use_stmt)
811 && gimple_call_builtin_p (use_stmt,
812 BUILT_IN_ASAN_AFTER_DYNAMIC_INIT))
813 {
814 unlink_stmt_vdef (use_stmt);
815 gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
816 gsi_remove (&gsi2, true);
817 remove = true;
818 }
819 }
820 }
821
f407b74a 822 if (gimple_call_internal_p (stmt))
6b5813f5 823 switch (gimple_call_internal_fn (stmt))
824 {
825 case IFN_UBSAN_NULL:
f407b74a 826 remove = maybe_optimize_ubsan_null_ifn (ctx, stmt);
827 break;
32cf7025 828 case IFN_UBSAN_VPTR:
829 remove = maybe_optimize_ubsan_vptr_ifn (ctx, stmt);
830 break;
df7bbf08 831 case IFN_UBSAN_PTR:
832 remove = maybe_optimize_ubsan_ptr_ifn (ctx, stmt);
833 break;
6b5813f5 834 case IFN_ASAN_CHECK:
f407b74a 835 if (asan_check_optimize)
836 remove = maybe_optimize_asan_check_ifn (ctx, stmt);
837 if (!remove)
838 ctx->asan_num_accesses++;
6b5813f5 839 break;
05f7c0e8 840 case IFN_ASAN_MARK:
841 ctx->contains_asan_mark = true;
842 break;
6b5813f5 843 default:
844 break;
845 }
846
f407b74a 847 if (remove)
848 {
849 /* Drop this check. */
850 if (dump_file && (dump_flags & TDF_DETAILS))
851 {
df7bbf08 852 fprintf (dump_file, "Optimizing out: ");
f407b74a 853 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
f407b74a 854 }
855 unlink_stmt_vdef (stmt);
856 gsi_remove (&gsi, true);
857 }
858 else
df7bbf08 859 {
860 if (dump_file && (dump_flags & TDF_DETAILS))
861 {
862 fprintf (dump_file, "Leaving: ");
863 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
864 }
865
866 gsi_next (&gsi);
867 }
6b5813f5 868 }
869
f407b74a 870 if (asan_check_optimize)
871 {
872 info->has_freeing_call_p = info->freeing_call_events != 0;
873 info->has_freeing_call_computed_p = true;
874 }
875
6b5813f5 876 for (son = first_dom_son (CDI_DOMINATORS, bb);
877 son;
878 son = next_dom_son (CDI_DOMINATORS, son))
879 sanopt_optimize_walker (son, ctx);
880
881 /* We're leaving this BB, so mark it to that effect. */
6b5813f5 882 info->visited_p = true;
883}
884
885/* Try to remove redundant sanitizer checks in function FUN. */
886
887static int
05f7c0e8 888sanopt_optimize (function *fun, bool *contains_asan_mark)
6b5813f5 889{
2e966e2a 890 class sanopt_ctx ctx;
6b5813f5 891 ctx.asan_num_accesses = 0;
05f7c0e8 892 ctx.contains_asan_mark = false;
6b5813f5 893
894 /* Set up block info for each basic block. */
895 alloc_aux_for_blocks (sizeof (sanopt_info));
896
897 /* We're going to do a dominator walk, so ensure that we have
898 dominance information. */
899 calculate_dominance_info (CDI_DOMINATORS);
900
901 /* Recursively walk the dominator tree optimizing away
902 redundant checks. */
903 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun), &ctx);
904
905 free_aux_for_blocks ();
906
05f7c0e8 907 *contains_asan_mark = ctx.contains_asan_mark;
6b5813f5 908 return ctx.asan_num_accesses;
909}
910
911/* Perform optimization of sanitize functions. */
912
7620bc82 913namespace {
914
915const pass_data pass_data_sanopt =
6b5813f5 916{
917 GIMPLE_PASS, /* type */
918 "sanopt", /* name */
919 OPTGROUP_NONE, /* optinfo_flags */
920 TV_NONE, /* tv_id */
921 ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
922 0, /* properties_provided */
923 0, /* properties_destroyed */
924 0, /* todo_flags_start */
925 TODO_update_ssa, /* todo_flags_finish */
926};
927
7620bc82 928class pass_sanopt : public gimple_opt_pass
6b5813f5 929{
930public:
931 pass_sanopt (gcc::context *ctxt)
932 : gimple_opt_pass (pass_data_sanopt, ctxt)
933 {}
934
935 /* opt_pass methods: */
936 virtual bool gate (function *) { return flag_sanitize; }
937 virtual unsigned int execute (function *);
938
939}; // class pass_sanopt
940
05f7c0e8 941/* Sanitize all ASAN_MARK unpoison calls that are not reachable by a BB
942 that contains an ASAN_MARK poison. All these ASAN_MARK unpoison call
943 can be removed as all variables are unpoisoned in a function prologue. */
944
945static void
946sanitize_asan_mark_unpoison (void)
947{
948 /* 1) Find all BBs that contain an ASAN_MARK poison call. */
949 auto_sbitmap with_poison (last_basic_block_for_fn (cfun) + 1);
950 bitmap_clear (with_poison);
951 basic_block bb;
952
953 FOR_EACH_BB_FN (bb, cfun)
954 {
955 if (bitmap_bit_p (with_poison, bb->index))
956 continue;
957
958 gimple_stmt_iterator gsi;
959 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
960 {
961 gimple *stmt = gsi_stmt (gsi);
962 if (asan_mark_p (stmt, ASAN_MARK_POISON))
963 {
964 bitmap_set_bit (with_poison, bb->index);
965 break;
966 }
967 }
968 }
969
970 auto_sbitmap poisoned (last_basic_block_for_fn (cfun) + 1);
971 bitmap_clear (poisoned);
972 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
973 bitmap_copy (worklist, with_poison);
974
975 /* 2) Propagate the information to all reachable blocks. */
976 while (!bitmap_empty_p (worklist))
977 {
978 unsigned i = bitmap_first_set_bit (worklist);
979 bitmap_clear_bit (worklist, i);
980 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
981 gcc_assert (bb);
982
983 edge e;
984 edge_iterator ei;
985 FOR_EACH_EDGE (e, ei, bb->succs)
986 if (!bitmap_bit_p (poisoned, e->dest->index))
987 {
988 bitmap_set_bit (poisoned, e->dest->index);
989 bitmap_set_bit (worklist, e->dest->index);
990 }
991 }
992
993 /* 3) Iterate all BBs not included in POISONED BBs and remove unpoison
994 ASAN_MARK preceding an ASAN_MARK poison (which can still happen). */
995 FOR_EACH_BB_FN (bb, cfun)
996 {
997 if (bitmap_bit_p (poisoned, bb->index))
998 continue;
999
1000 gimple_stmt_iterator gsi;
1001 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1002 {
05f7c0e8 1003 gimple *stmt = gsi_stmt (gsi);
1004 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1005 {
1006 if (asan_mark_p (stmt, ASAN_MARK_POISON))
1007 break;
1008 else
1009 {
1010 if (dump_file)
1011 fprintf (dump_file, "Removing ASAN_MARK unpoison\n");
1012 unlink_stmt_vdef (stmt);
1013 release_defs (stmt);
1014 gsi_remove (&gsi, true);
22135584 1015 continue;
05f7c0e8 1016 }
1017 }
1018
22135584 1019 gsi_next (&gsi);
05f7c0e8 1020 }
1021 }
1022}
1023
1024/* Return true when STMT is either ASAN_CHECK call or a call of a function
1025 that can contain an ASAN_CHECK. */
1026
1027static bool
1028maybe_contains_asan_check (gimple *stmt)
1029{
1030 if (is_gimple_call (stmt))
1031 {
1032 if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1033 return false;
1034 else
1035 return !(gimple_call_flags (stmt) & ECF_CONST);
1036 }
1037 else if (is_a<gasm *> (stmt))
1038 return true;
1039
1040 return false;
1041}
1042
1043/* Sanitize all ASAN_MARK poison calls that are not followed by an ASAN_CHECK
1044 call. These calls can be removed. */
1045
1046static void
1047sanitize_asan_mark_poison (void)
1048{
1049 /* 1) Find all BBs that possibly contain an ASAN_CHECK. */
1050 auto_sbitmap with_check (last_basic_block_for_fn (cfun) + 1);
1051 bitmap_clear (with_check);
1052 basic_block bb;
1053
1054 FOR_EACH_BB_FN (bb, cfun)
1055 {
1056 gimple_stmt_iterator gsi;
1057 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
1058 {
1059 gimple *stmt = gsi_stmt (gsi);
1060 if (maybe_contains_asan_check (stmt))
1061 {
1062 bitmap_set_bit (with_check, bb->index);
1063 break;
1064 }
1065 }
1066 }
1067
1068 auto_sbitmap can_reach_check (last_basic_block_for_fn (cfun) + 1);
1069 bitmap_clear (can_reach_check);
1070 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
1071 bitmap_copy (worklist, with_check);
1072
1073 /* 2) Propagate the information to all definitions blocks. */
1074 while (!bitmap_empty_p (worklist))
1075 {
1076 unsigned i = bitmap_first_set_bit (worklist);
1077 bitmap_clear_bit (worklist, i);
1078 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1079 gcc_assert (bb);
1080
1081 edge e;
1082 edge_iterator ei;
1083 FOR_EACH_EDGE (e, ei, bb->preds)
1084 if (!bitmap_bit_p (can_reach_check, e->src->index))
1085 {
1086 bitmap_set_bit (can_reach_check, e->src->index);
1087 bitmap_set_bit (worklist, e->src->index);
1088 }
1089 }
1090
1091 /* 3) Iterate all BBs not included in CAN_REACH_CHECK BBs and remove poison
1092 ASAN_MARK not followed by a call to function having an ASAN_CHECK. */
1093 FOR_EACH_BB_FN (bb, cfun)
1094 {
1095 if (bitmap_bit_p (can_reach_check, bb->index))
1096 continue;
1097
1098 gimple_stmt_iterator gsi;
1099 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
1100 {
05f7c0e8 1101 gimple *stmt = gsi_stmt (gsi);
1102 if (maybe_contains_asan_check (stmt))
1103 break;
1104 else if (asan_mark_p (stmt, ASAN_MARK_POISON))
1105 {
1106 if (dump_file)
1107 fprintf (dump_file, "Removing ASAN_MARK poison\n");
1108 unlink_stmt_vdef (stmt);
1109 release_defs (stmt);
22135584 1110 gimple_stmt_iterator gsi2 = gsi;
1111 gsi_prev (&gsi);
1112 gsi_remove (&gsi2, true);
1113 continue;
05f7c0e8 1114 }
1115
22135584 1116 gsi_prev (&gsi);
05f7c0e8 1117 }
1118 }
1119}
1120
f91fab92 1121/* Rewrite all usages of tree OP which is a PARM_DECL with a VAR_DECL
1122 that is it's DECL_VALUE_EXPR. */
1123
1124static tree
1125rewrite_usage_of_param (tree *op, int *walk_subtrees, void *)
1126{
1127 if (TREE_CODE (*op) == PARM_DECL && DECL_HAS_VALUE_EXPR_P (*op))
1128 {
1129 *op = DECL_VALUE_EXPR (*op);
1130 *walk_subtrees = 0;
1131 }
1132
1133 return NULL;
1134}
1135
1136/* For a given function FUN, rewrite all addressable parameters so that
1137 a new automatic variable is introduced. Right after function entry
1138 a parameter is assigned to the variable. */
1139
1140static void
1141sanitize_rewrite_addressable_params (function *fun)
1142{
1143 gimple *g;
1144 gimple_seq stmts = NULL;
1145 bool has_any_addressable_param = false;
1146 auto_vec<tree> clear_value_expr_list;
1147
1148 for (tree arg = DECL_ARGUMENTS (current_function_decl);
1149 arg; arg = DECL_CHAIN (arg))
1150 {
bba0033c 1151 tree type = TREE_TYPE (arg);
93c45686 1152 if (TREE_ADDRESSABLE (arg)
1153 && !TREE_ADDRESSABLE (type)
1154 && !TREE_THIS_VOLATILE (arg)
bba0033c 1155 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
f91fab92 1156 {
1157 TREE_ADDRESSABLE (arg) = 0;
1158 /* The parameter is no longer addressable. */
f91fab92 1159 has_any_addressable_param = true;
1160
1161 /* Create a new automatic variable. */
1162 tree var = build_decl (DECL_SOURCE_LOCATION (arg),
1163 VAR_DECL, DECL_NAME (arg), type);
1164 TREE_ADDRESSABLE (var) = 1;
ccaa30f1 1165 DECL_IGNORED_P (var) = 1;
f91fab92 1166
1167 gimple_add_tmp_var (var);
1168
64f41321 1169 /* We skip parameters that have a DECL_VALUE_EXPR. */
1170 if (DECL_HAS_VALUE_EXPR_P (arg))
1171 continue;
1172
f91fab92 1173 if (dump_file)
1174 fprintf (dump_file,
1175 "Rewriting parameter whose address is taken: %s\n",
1176 IDENTIFIER_POINTER (DECL_NAME (arg)));
1177
f91fab92 1178 SET_DECL_PT_UID (var, DECL_PT_UID (arg));
1179
1180 /* Assign value of parameter to newly created variable. */
1181 if ((TREE_CODE (type) == COMPLEX_TYPE
1182 || TREE_CODE (type) == VECTOR_TYPE))
1183 {
1184 /* We need to create a SSA name that will be used for the
1185 assignment. */
1186 DECL_GIMPLE_REG_P (arg) = 1;
1187 tree tmp = get_or_create_ssa_default_def (cfun, arg);
1188 g = gimple_build_assign (var, tmp);
1189 gimple_set_location (g, DECL_SOURCE_LOCATION (arg));
1190 gimple_seq_add_stmt (&stmts, g);
1191 }
1192 else
1193 {
1194 g = gimple_build_assign (var, arg);
1195 gimple_set_location (g, DECL_SOURCE_LOCATION (arg));
1196 gimple_seq_add_stmt (&stmts, g);
1197 }
1198
1199 if (target_for_debug_bind (arg))
1200 {
1201 g = gimple_build_debug_bind (arg, var, NULL);
1202 gimple_seq_add_stmt (&stmts, g);
1203 clear_value_expr_list.safe_push (arg);
1204 }
f7519c12 1205
1206 DECL_HAS_VALUE_EXPR_P (arg) = 1;
1207 SET_DECL_VALUE_EXPR (arg, var);
f91fab92 1208 }
1209 }
1210
1211 if (!has_any_addressable_param)
1212 return;
1213
1214 /* Replace all usages of PARM_DECLs with the newly
1215 created variable VAR. */
1216 basic_block bb;
1217 FOR_EACH_BB_FN (bb, fun)
1218 {
1219 gimple_stmt_iterator gsi;
1220 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1221 {
1222 gimple *stmt = gsi_stmt (gsi);
1223 gimple_stmt_iterator it = gsi_for_stmt (stmt);
1224 walk_gimple_stmt (&it, NULL, rewrite_usage_of_param, NULL);
1225 }
1226 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1227 {
1228 gphi *phi = dyn_cast<gphi *> (gsi_stmt (gsi));
1229 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
1230 {
1231 hash_set<tree> visited_nodes;
1232 walk_tree (gimple_phi_arg_def_ptr (phi, i),
1233 rewrite_usage_of_param, NULL, &visited_nodes);
1234 }
1235 }
1236 }
1237
1238 /* Unset value expr for parameters for which we created debug bind
1239 expressions. */
1240 unsigned i;
1241 tree arg;
1242 FOR_EACH_VEC_ELT (clear_value_expr_list, i, arg)
1243 {
1244 DECL_HAS_VALUE_EXPR_P (arg) = 0;
1245 SET_DECL_VALUE_EXPR (arg, NULL_TREE);
1246 }
1247
1248 /* Insert default assignments at the beginning of a function. */
1249 basic_block entry_bb = ENTRY_BLOCK_PTR_FOR_FN (fun);
1250 entry_bb = split_edge (single_succ_edge (entry_bb));
1251
1252 gimple_stmt_iterator gsi = gsi_start_bb (entry_bb);
1253 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
1254}
1255
6b5813f5 1256unsigned int
1257pass_sanopt::execute (function *fun)
1258{
1259 basic_block bb;
1260 int asan_num_accesses = 0;
05f7c0e8 1261 bool contains_asan_mark = false;
6b5813f5 1262
1263 /* Try to remove redundant checks. */
1264 if (optimize
f407b74a 1265 && (flag_sanitize
32cf7025 1266 & (SANITIZE_NULL | SANITIZE_ALIGNMENT
df7bbf08 1267 | SANITIZE_ADDRESS | SANITIZE_VPTR | SANITIZE_POINTER_OVERFLOW)))
05f7c0e8 1268 asan_num_accesses = sanopt_optimize (fun, &contains_asan_mark);
6b5813f5 1269 else if (flag_sanitize & SANITIZE_ADDRESS)
1270 {
1271 gimple_stmt_iterator gsi;
1272 FOR_EACH_BB_FN (bb, fun)
1273 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1274 {
42acab1c 1275 gimple *stmt = gsi_stmt (gsi);
7408cd7d 1276 if (gimple_call_internal_p (stmt, IFN_ASAN_CHECK))
6b5813f5 1277 ++asan_num_accesses;
05f7c0e8 1278 else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1279 contains_asan_mark = true;
6b5813f5 1280 }
1281 }
1282
05f7c0e8 1283 if (contains_asan_mark)
1284 {
1285 sanitize_asan_mark_unpoison ();
1286 sanitize_asan_mark_poison ();
1287 }
1288
f91fab92 1289 if (asan_sanitize_stack_p ())
1290 sanitize_rewrite_addressable_params (fun);
1291
6b5813f5 1292 bool use_calls = ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD < INT_MAX
1293 && asan_num_accesses >= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD;
1294
c51887c5 1295 hash_map<tree, tree> shadow_vars_mapping;
1296 bool need_commit_edge_insert = false;
6b5813f5 1297 FOR_EACH_BB_FN (bb, fun)
1298 {
1299 gimple_stmt_iterator gsi;
1300 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1301 {
42acab1c 1302 gimple *stmt = gsi_stmt (gsi);
6b5813f5 1303 bool no_next = false;
1304
1305 if (!is_gimple_call (stmt))
1306 {
1307 gsi_next (&gsi);
1308 continue;
1309 }
1310
1311 if (gimple_call_internal_p (stmt))
1312 {
1313 enum internal_fn ifn = gimple_call_internal_fn (stmt);
1314 switch (ifn)
1315 {
1316 case IFN_UBSAN_NULL:
1317 no_next = ubsan_expand_null_ifn (&gsi);
1318 break;
1319 case IFN_UBSAN_BOUNDS:
1320 no_next = ubsan_expand_bounds_ifn (&gsi);
1321 break;
1322 case IFN_UBSAN_OBJECT_SIZE:
1323 no_next = ubsan_expand_objsize_ifn (&gsi);
1324 break;
b4fce8f9 1325 case IFN_UBSAN_PTR:
1326 no_next = ubsan_expand_ptr_ifn (&gsi);
1327 break;
32cf7025 1328 case IFN_UBSAN_VPTR:
1329 no_next = ubsan_expand_vptr_ifn (&gsi);
1330 break;
6b5813f5 1331 case IFN_ASAN_CHECK:
1332 no_next = asan_expand_check_ifn (&gsi, use_calls);
1333 break;
629b6abc 1334 case IFN_ASAN_MARK:
1335 no_next = asan_expand_mark_ifn (&gsi);
1336 break;
c51887c5 1337 case IFN_ASAN_POISON:
1338 no_next = asan_expand_poison_ifn (&gsi,
1339 &need_commit_edge_insert,
1340 shadow_vars_mapping);
1341 break;
6b5813f5 1342 default:
1343 break;
1344 }
1345 }
f6b540af 1346 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1347 {
1348 tree callee = gimple_call_fndecl (stmt);
1349 switch (DECL_FUNCTION_CODE (callee))
1350 {
1351 case BUILT_IN_UNREACHABLE:
9917317a 1352 if (sanitize_flags_p (SANITIZE_UNREACHABLE))
f6b540af 1353 no_next = ubsan_instrument_unreachable (&gsi);
1354 break;
1355 default:
1356 break;
1357 }
1358 }
6b5813f5 1359
1360 if (dump_file && (dump_flags & TDF_DETAILS))
1361 {
df7bbf08 1362 fprintf (dump_file, "Expanded: ");
6b5813f5 1363 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
6b5813f5 1364 }
1365
1366 if (!no_next)
1367 gsi_next (&gsi);
1368 }
1369 }
c51887c5 1370
1371 if (need_commit_edge_insert)
1372 gsi_commit_edge_inserts ();
1373
6b5813f5 1374 return 0;
1375}
1376
7620bc82 1377} // anon namespace
1378
6b5813f5 1379gimple_opt_pass *
1380make_pass_sanopt (gcc::context *ctxt)
1381{
1382 return new pass_sanopt (ctxt);
1383}