1 /* Optimize and expand sanitizer functions.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
3 Contributed by Marek Polacek <polacek@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "tree-pass.h"
29 #include "tree-ssa-operands.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "gimple-iterator.h"
33 #include "stringpool.h"
37 #include "tree-hash-traits.h"
38 #include "gimple-ssa.h"
39 #include "tree-phinodes.h"
40 #include "ssa-iterators.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
49 /* This is used to carry information about basic blocks. It is
50 attached to the AUX field of the standard CFG block. */
54 /* True if this BB might call (directly or indirectly) free/munmap
55 or similar operation. */
56 bool has_freeing_call_p
;
58 /* True if HAS_FREEING_CALL_P flag has been computed. */
59 bool has_freeing_call_computed_p
;
61 /* True if there is a block with HAS_FREEING_CALL_P flag set
62 on any path between an immediate dominator of BB, denoted
64 bool imm_dom_path_with_freeing_call_p
;
66 /* True if IMM_DOM_PATH_WITH_FREEING_CALL_P has been computed. */
67 bool imm_dom_path_with_freeing_call_computed_p
;
69 /* Number of possibly freeing calls encountered in this bb
71 uint64_t freeing_call_events
;
73 /* True if BB is currently being visited during computation
74 of IMM_DOM_PATH_WITH_FREEING_CALL_P flag. */
77 /* True if this BB has been visited in the dominator walk. */
81 /* If T has a single definition of form T = T2, return T2. */
84 maybe_get_single_definition (tree t
)
86 if (TREE_CODE (t
) == SSA_NAME
)
88 gimple
*g
= SSA_NAME_DEF_STMT (t
);
89 if (gimple_assign_single_p (g
))
95 /* Tree triplet for vptr_check_map. */
96 struct sanopt_tree_triplet
101 /* Traits class for tree triplet hash maps below. */
103 struct sanopt_tree_triplet_hash
: typed_noop_remove
<sanopt_tree_triplet
>
105 typedef sanopt_tree_triplet value_type
;
106 typedef sanopt_tree_triplet compare_type
;
109 hash (const sanopt_tree_triplet
&ref
)
111 inchash::hash
hstate (0);
112 inchash::add_expr (ref
.t1
, hstate
);
113 inchash::add_expr (ref
.t2
, hstate
);
114 inchash::add_expr (ref
.t3
, hstate
);
115 return hstate
.end ();
119 equal (const sanopt_tree_triplet
&ref1
, const sanopt_tree_triplet
&ref2
)
121 return operand_equal_p (ref1
.t1
, ref2
.t1
, 0)
122 && operand_equal_p (ref1
.t2
, ref2
.t2
, 0)
123 && operand_equal_p (ref1
.t3
, ref2
.t3
, 0);
127 mark_deleted (sanopt_tree_triplet
&ref
)
129 ref
.t1
= reinterpret_cast<tree
> (1);
132 static const bool empty_zero_p
= true;
135 mark_empty (sanopt_tree_triplet
&ref
)
141 is_deleted (const sanopt_tree_triplet
&ref
)
143 return ref
.t1
== reinterpret_cast<tree
> (1);
147 is_empty (const sanopt_tree_triplet
&ref
)
149 return ref
.t1
== NULL
;
153 /* Tree couple for ptr_check_map. */
154 struct sanopt_tree_couple
160 /* Traits class for tree triplet hash maps below. */
162 struct sanopt_tree_couple_hash
: typed_noop_remove
<sanopt_tree_couple
>
164 typedef sanopt_tree_couple value_type
;
165 typedef sanopt_tree_couple compare_type
;
168 hash (const sanopt_tree_couple
&ref
)
170 inchash::hash
hstate (0);
171 inchash::add_expr (ref
.ptr
, hstate
);
172 hstate
.add_int (ref
.pos_p
);
173 return hstate
.end ();
177 equal (const sanopt_tree_couple
&ref1
, const sanopt_tree_couple
&ref2
)
179 return operand_equal_p (ref1
.ptr
, ref2
.ptr
, 0)
180 && ref1
.pos_p
== ref2
.pos_p
;
184 mark_deleted (sanopt_tree_couple
&ref
)
186 ref
.ptr
= reinterpret_cast<tree
> (1);
189 static const bool empty_zero_p
= true;
192 mark_empty (sanopt_tree_couple
&ref
)
198 is_deleted (const sanopt_tree_couple
&ref
)
200 return ref
.ptr
== reinterpret_cast<tree
> (1);
204 is_empty (const sanopt_tree_couple
&ref
)
206 return ref
.ptr
== NULL
;
210 /* This is used to carry various hash maps and variables used
211 in sanopt_optimize_walker. */
216 /* This map maps a pointer (the first argument of UBSAN_NULL) to
217 a vector of UBSAN_NULL call statements that check this pointer. */
218 hash_map
<tree
, auto_vec
<gimple
*> > null_check_map
;
220 /* This map maps a pointer (the second argument of ASAN_CHECK) to
221 a vector of ASAN_CHECK call statements that check the access. */
222 hash_map
<tree_operand_hash
, auto_vec
<gimple
*> > asan_check_map
;
224 /* This map maps a tree triplet (the first, second and fourth argument
225 of UBSAN_VPTR) to a vector of UBSAN_VPTR call statements that check
226 that virtual table pointer. */
227 hash_map
<sanopt_tree_triplet_hash
, auto_vec
<gimple
*> > vptr_check_map
;
229 /* This map maps a couple (tree and boolean) to a vector of UBSAN_PTR
230 call statements that check that pointer overflow. */
231 hash_map
<sanopt_tree_couple_hash
, auto_vec
<gimple
*> > ptr_check_map
;
233 /* Number of IFN_ASAN_CHECK statements. */
234 int asan_num_accesses
;
236 /* True when the current functions constains an ASAN_MARK. */
237 bool contains_asan_mark
;
240 /* Return true if there might be any call to free/munmap operation
241 on any path in between DOM (which should be imm(BB)) and BB. */
244 imm_dom_path_with_freeing_call (basic_block bb
, basic_block dom
)
246 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
250 if (info
->imm_dom_path_with_freeing_call_computed_p
)
251 return info
->imm_dom_path_with_freeing_call_p
;
253 info
->being_visited_p
= true;
255 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
257 sanopt_info
*pred_info
= (sanopt_info
*) e
->src
->aux
;
262 if ((pred_info
->imm_dom_path_with_freeing_call_computed_p
263 && pred_info
->imm_dom_path_with_freeing_call_p
)
264 || (pred_info
->has_freeing_call_computed_p
265 && pred_info
->has_freeing_call_p
))
267 info
->imm_dom_path_with_freeing_call_computed_p
= true;
268 info
->imm_dom_path_with_freeing_call_p
= true;
269 info
->being_visited_p
= false;
274 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
276 sanopt_info
*pred_info
= (sanopt_info
*) e
->src
->aux
;
281 if (pred_info
->has_freeing_call_computed_p
)
284 gimple_stmt_iterator gsi
;
285 for (gsi
= gsi_start_bb (e
->src
); !gsi_end_p (gsi
); gsi_next (&gsi
))
287 gimple
*stmt
= gsi_stmt (gsi
);
290 if ((is_gimple_call (stmt
) && !nonfreeing_call_p (stmt
))
291 || ((asm_stmt
= dyn_cast
<gasm
*> (stmt
))
292 && (gimple_asm_clobbers_memory_p (asm_stmt
)
293 || gimple_asm_volatile_p (asm_stmt
))))
295 pred_info
->has_freeing_call_p
= true;
300 pred_info
->has_freeing_call_computed_p
= true;
301 if (pred_info
->has_freeing_call_p
)
303 info
->imm_dom_path_with_freeing_call_computed_p
= true;
304 info
->imm_dom_path_with_freeing_call_p
= true;
305 info
->being_visited_p
= false;
310 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
316 for (src
= e
->src
; src
!= dom
; )
318 sanopt_info
*pred_info
= (sanopt_info
*) src
->aux
;
319 if (pred_info
->being_visited_p
)
321 basic_block imm
= get_immediate_dominator (CDI_DOMINATORS
, src
);
322 if (imm_dom_path_with_freeing_call (src
, imm
))
324 info
->imm_dom_path_with_freeing_call_computed_p
= true;
325 info
->imm_dom_path_with_freeing_call_p
= true;
326 info
->being_visited_p
= false;
333 info
->imm_dom_path_with_freeing_call_computed_p
= true;
334 info
->imm_dom_path_with_freeing_call_p
= false;
335 info
->being_visited_p
= false;
339 /* Get the first dominating check from the list of stored checks.
340 Non-dominating checks are silently dropped. */
343 maybe_get_dominating_check (auto_vec
<gimple
*> &v
)
345 for (; !v
.is_empty (); v
.pop ())
347 gimple
*g
= v
.last ();
348 sanopt_info
*si
= (sanopt_info
*) gimple_bb (g
)->aux
;
350 /* At this point we shouldn't have any statements
351 that aren't dominating the current BB. */
357 /* Optimize away redundant UBSAN_NULL calls. */
360 maybe_optimize_ubsan_null_ifn (class sanopt_ctx
*ctx
, gimple
*stmt
)
362 gcc_assert (gimple_call_num_args (stmt
) == 3);
363 tree ptr
= gimple_call_arg (stmt
, 0);
364 tree cur_align
= gimple_call_arg (stmt
, 2);
365 gcc_assert (TREE_CODE (cur_align
) == INTEGER_CST
);
368 auto_vec
<gimple
*> &v
= ctx
->null_check_map
.get_or_insert (ptr
);
369 gimple
*g
= maybe_get_dominating_check (v
);
372 /* For this PTR we don't have any UBSAN_NULL stmts recorded, so there's
373 nothing to optimize yet. */
378 /* We already have recorded a UBSAN_NULL check for this pointer. Perhaps we
379 can drop this one. But only if this check doesn't specify stricter
382 tree align
= gimple_call_arg (g
, 2);
383 int kind
= tree_to_shwi (gimple_call_arg (g
, 1));
384 /* If this is a NULL pointer check where we had segv anyway, we can
386 if (integer_zerop (align
)
387 && (kind
== UBSAN_LOAD_OF
388 || kind
== UBSAN_STORE_OF
389 || kind
== UBSAN_MEMBER_ACCESS
))
391 /* Otherwise remove the check in non-recovering mode, or if the
392 stmts have same location. */
393 else if (integer_zerop (align
))
394 remove
= (flag_sanitize_recover
& SANITIZE_NULL
) == 0
395 || (flag_sanitize_trap
& SANITIZE_NULL
) != 0
396 || gimple_location (g
) == gimple_location (stmt
);
397 else if (tree_int_cst_le (cur_align
, align
))
398 remove
= (flag_sanitize_recover
& SANITIZE_ALIGNMENT
) == 0
399 || (flag_sanitize_trap
& SANITIZE_ALIGNMENT
) != 0
400 || gimple_location (g
) == gimple_location (stmt
);
402 if (!remove
&& gimple_bb (g
) == gimple_bb (stmt
)
403 && tree_int_cst_compare (cur_align
, align
) == 0)
411 /* Return true when pointer PTR for a given CUR_OFFSET is already sanitized
412 in a given sanitization context CTX. */
415 has_dominating_ubsan_ptr_check (sanopt_ctx
*ctx
, tree ptr
,
416 offset_int
&cur_offset
)
418 bool pos_p
= !wi::neg_p (cur_offset
);
419 sanopt_tree_couple couple
;
421 couple
.pos_p
= pos_p
;
423 auto_vec
<gimple
*> &v
= ctx
->ptr_check_map
.get_or_insert (couple
);
424 gimple
*g
= maybe_get_dominating_check (v
);
428 /* We already have recorded a UBSAN_PTR check for this pointer. Perhaps we
429 can drop this one. But only if this check doesn't specify larger offset.
431 tree offset
= gimple_call_arg (g
, 1);
432 gcc_assert (TREE_CODE (offset
) == INTEGER_CST
);
433 offset_int ooffset
= wi::sext (wi::to_offset (offset
), POINTER_SIZE
);
437 if (wi::les_p (cur_offset
, ooffset
))
440 else if (!pos_p
&& wi::les_p (ooffset
, cur_offset
))
446 /* Record UBSAN_PTR check of given context CTX. Register pointer PTR on
447 a given OFFSET that it's handled by GIMPLE STMT. */
450 record_ubsan_ptr_check_stmt (sanopt_ctx
*ctx
, gimple
*stmt
, tree ptr
,
451 const offset_int
&offset
)
453 sanopt_tree_couple couple
;
455 couple
.pos_p
= !wi::neg_p (offset
);
457 auto_vec
<gimple
*> &v
= ctx
->ptr_check_map
.get_or_insert (couple
);
461 /* Optimize away redundant UBSAN_PTR calls. */
464 maybe_optimize_ubsan_ptr_ifn (sanopt_ctx
*ctx
, gimple
*stmt
)
466 poly_int64 bitsize
, pbitpos
;
468 int volatilep
= 0, reversep
, unsignedp
= 0;
471 gcc_assert (gimple_call_num_args (stmt
) == 2);
472 tree ptr
= gimple_call_arg (stmt
, 0);
473 tree off
= gimple_call_arg (stmt
, 1);
475 if (TREE_CODE (off
) != INTEGER_CST
)
478 if (integer_zerop (off
))
481 offset_int cur_offset
= wi::sext (wi::to_offset (off
), POINTER_SIZE
);
482 if (has_dominating_ubsan_ptr_check (ctx
, ptr
, cur_offset
))
486 if (TREE_CODE (base
) == ADDR_EXPR
)
488 base
= TREE_OPERAND (base
, 0);
490 HOST_WIDE_INT bitpos
;
491 base
= get_inner_reference (base
, &bitsize
, &pbitpos
, &offset
, &mode
,
492 &unsignedp
, &reversep
, &volatilep
);
493 if ((offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
496 && TREE_CODE (base
) != PARM_DECL
497 && TREE_CODE (base
) != RESULT_DECL
)
498 || !DECL_REGISTER (base
))
499 && pbitpos
.is_constant (&bitpos
))
501 offset_int expr_offset
;
503 expr_offset
= wi::to_offset (offset
) + bitpos
/ BITS_PER_UNIT
;
505 expr_offset
= bitpos
/ BITS_PER_UNIT
;
506 expr_offset
= wi::sext (expr_offset
, POINTER_SIZE
);
507 offset_int total_offset
= expr_offset
+ cur_offset
;
508 if (total_offset
!= wi::sext (total_offset
, POINTER_SIZE
))
510 record_ubsan_ptr_check_stmt (ctx
, stmt
, ptr
, cur_offset
);
514 /* If BASE is a fixed size automatic variable or
515 global variable defined in the current TU, we don't have
516 to instrument anything if offset is within address
519 || TREE_CODE (base
) == PARM_DECL
520 || TREE_CODE (base
) == RESULT_DECL
)
521 && DECL_SIZE_UNIT (base
)
522 && TREE_CODE (DECL_SIZE_UNIT (base
)) == INTEGER_CST
523 && (!is_global_var (base
) || decl_binds_to_current_def_p (base
)))
525 offset_int base_size
= wi::to_offset (DECL_SIZE_UNIT (base
));
526 if (!wi::neg_p (expr_offset
)
527 && wi::les_p (total_offset
, base_size
))
529 if (!wi::neg_p (total_offset
)
530 && wi::les_p (total_offset
, base_size
))
535 /* Following expression: UBSAN_PTR (&MEM_REF[ptr + x], y) can be
538 1) sign (x) == sign (y), then check for dominating check of (x + y)
539 2) sign (x) != sign (y), then first check if we have a dominating
540 check for ptr + x. If so, then we have 2 situations:
541 a) sign (x) == sign (x + y), here we are done, example:
542 UBSAN_PTR (&MEM_REF[ptr + 100], -50)
543 b) check for dominating check of ptr + x + y.
546 bool sign_cur_offset
= !wi::neg_p (cur_offset
);
547 bool sign_expr_offset
= !wi::neg_p (expr_offset
);
550 = build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (base
)), base
);
553 if (sign_cur_offset
== sign_expr_offset
)
555 if (has_dominating_ubsan_ptr_check (ctx
, base_addr
, total_offset
))
562 if (!has_dominating_ubsan_ptr_check (ctx
, base_addr
, expr_offset
))
563 ; /* Don't record base_addr + expr_offset, it's not a guarding
567 bool sign_total_offset
= !wi::neg_p (total_offset
);
568 if (sign_expr_offset
== sign_total_offset
)
572 if (has_dominating_ubsan_ptr_check (ctx
, base_addr
,
581 /* Record a new dominating check for base_addr + total_offset. */
582 if (add
&& !operand_equal_p (base
, base_addr
, 0))
583 record_ubsan_ptr_check_stmt (ctx
, stmt
, base_addr
,
588 /* For this PTR we don't have any UBSAN_PTR stmts recorded, so there's
589 nothing to optimize yet. */
590 record_ubsan_ptr_check_stmt (ctx
, stmt
, ptr
, cur_offset
);
595 /* Optimize away redundant UBSAN_VPTR calls. The second argument
596 is the value loaded from the virtual table, so rely on FRE to find out
597 when we can actually optimize. */
600 maybe_optimize_ubsan_vptr_ifn (class sanopt_ctx
*ctx
, gimple
*stmt
)
602 gcc_assert (gimple_call_num_args (stmt
) == 5);
603 sanopt_tree_triplet triplet
;
604 triplet
.t1
= gimple_call_arg (stmt
, 0);
605 triplet
.t2
= gimple_call_arg (stmt
, 1);
606 triplet
.t3
= gimple_call_arg (stmt
, 3);
608 auto_vec
<gimple
*> &v
= ctx
->vptr_check_map
.get_or_insert (triplet
);
609 gimple
*g
= maybe_get_dominating_check (v
);
612 /* For this PTR we don't have any UBSAN_VPTR stmts recorded, so there's
613 nothing to optimize yet. */
621 /* Checks whether value of T in CHECK and USE is the same. */
624 same_value_p (gimple
*check
, gimple
*use
, tree t
)
626 tree check_vuse
= gimple_vuse (check
);
627 tree use_vuse
= gimple_vuse (use
);
629 if (TREE_CODE (t
) == SSA_NAME
630 || is_gimple_min_invariant (t
)
634 if (check_vuse
== use_vuse
)
640 /* Returns TRUE if ASan check of length LEN in block BB can be removed
641 if preceded by checks in V. */
644 can_remove_asan_check (auto_vec
<gimple
*> &v
, tree len
, basic_block bb
,
645 gimple
*base_stmt
, tree base_addr
)
649 gimple
*to_pop
= NULL
;
651 basic_block last_bb
= bb
;
652 bool cleanup
= false;
654 FOR_EACH_VEC_ELT_REVERSE (v
, i
, g
)
656 basic_block gbb
= gimple_bb (g
);
657 sanopt_info
*si
= (sanopt_info
*) gbb
->aux
;
658 if (gimple_uid (g
) < si
->freeing_call_events
)
660 /* If there is a potentially freeing call after g in gbb, we should
661 remove it from the vector, can't use in optimization. */
666 tree glen
= gimple_call_arg (g
, 2);
667 gcc_assert (TREE_CODE (glen
) == INTEGER_CST
);
669 /* If we've checked only smaller length than we want to check now,
670 we can't remove the current stmt. If g is in the same basic block,
671 we want to remove it though, as the current stmt is better. */
672 if (tree_int_cst_lt (glen
, len
))
682 while (last_bb
!= gbb
)
684 /* Paths from last_bb to bb have been checked before.
685 gbb is necessarily a dominator of last_bb, but not necessarily
686 immediate dominator. */
687 if (((sanopt_info
*) last_bb
->aux
)->freeing_call_events
)
690 basic_block imm
= get_immediate_dominator (CDI_DOMINATORS
, last_bb
);
692 if (imm_dom_path_with_freeing_call (last_bb
, imm
))
699 // In case of base_addr residing in memory we also need to check aliasing
700 remove
= ! base_addr
|| same_value_p (g
, base_stmt
, base_addr
);
706 unsigned int j
= 0, l
= v
.length ();
707 for (i
= 0; i
< l
; i
++)
709 && (gimple_uid (v
[i
])
711 gimple_bb (v
[i
])->aux
)->freeing_call_events
))
723 /* Optimize away redundant ASAN_CHECK calls. */
726 maybe_optimize_asan_check_ifn (class sanopt_ctx
*ctx
, gimple
*stmt
)
728 gcc_assert (gimple_call_num_args (stmt
) == 4);
729 tree ptr
= gimple_call_arg (stmt
, 1);
730 tree len
= gimple_call_arg (stmt
, 2);
731 basic_block bb
= gimple_bb (stmt
);
732 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
734 if (TREE_CODE (len
) != INTEGER_CST
)
736 if (integer_zerop (len
))
739 gimple_set_uid (stmt
, info
->freeing_call_events
);
741 auto_vec
<gimple
*> *ptr_checks
= &ctx
->asan_check_map
.get_or_insert (ptr
);
743 gimple
*base_stmt
= maybe_get_single_definition (ptr
);
744 tree base_addr
= base_stmt
? gimple_assign_rhs1 (base_stmt
) : NULL_TREE
;
745 auto_vec
<gimple
*> *base_checks
= NULL
;
748 base_checks
= &ctx
->asan_check_map
.get_or_insert (base_addr
);
749 /* Original pointer might have been invalidated. */
750 ptr_checks
= ctx
->asan_check_map
.get (ptr
);
753 gimple
*g
= maybe_get_dominating_check (*ptr_checks
);
757 /* Try with base address as well. */
758 g2
= maybe_get_dominating_check (*base_checks
);
760 if (g
== NULL
&& g2
== NULL
)
762 /* For this PTR we don't have any ASAN_CHECK stmts recorded, so there's
763 nothing to optimize yet. */
764 ptr_checks
->safe_push (stmt
);
766 base_checks
->safe_push (stmt
);
773 remove
= can_remove_asan_check (*ptr_checks
, len
, bb
, NULL
, NULL
);
775 if (!remove
&& base_checks
)
776 /* Try with base address as well. */
777 remove
= can_remove_asan_check (*base_checks
, len
, bb
, base_stmt
,
782 ptr_checks
->safe_push (stmt
);
784 base_checks
->safe_push (stmt
);
790 /* Try to optimize away redundant UBSAN_NULL and ASAN_CHECK calls.
792 We walk blocks in the CFG via a depth first search of the dominator
793 tree; we push unique UBSAN_NULL or ASAN_CHECK statements into a vector
794 in the NULL_CHECK_MAP or ASAN_CHECK_MAP hash maps as we enter the
795 blocks. When leaving a block, we mark the block as visited; then
796 when checking the statements in the vector, we ignore statements that
797 are coming from already visited blocks, because these cannot dominate
798 anything anymore. CTX is a sanopt context. */
801 sanopt_optimize_walker (basic_block bb
, class sanopt_ctx
*ctx
)
804 gimple_stmt_iterator gsi
;
805 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
806 bool asan_check_optimize
807 = ((flag_sanitize
& (SANITIZE_ADDRESS
| SANITIZE_HWADDRESS
)) != 0);
809 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
811 gimple
*stmt
= gsi_stmt (gsi
);
814 if (!is_gimple_call (stmt
))
816 /* Handle asm volatile or asm with "memory" clobber
817 the same as potentionally freeing call. */
818 gasm
*asm_stmt
= dyn_cast
<gasm
*> (stmt
);
820 && asan_check_optimize
821 && (gimple_asm_clobbers_memory_p (asm_stmt
)
822 || gimple_asm_volatile_p (asm_stmt
)))
823 info
->freeing_call_events
++;
828 if (asan_check_optimize
&& !nonfreeing_call_p (stmt
))
829 info
->freeing_call_events
++;
831 /* If __asan_before_dynamic_init ("module"); is followed by
832 __asan_after_dynamic_init (); without intervening memory loads/stores,
833 there is nothing to guard, so optimize both away. */
834 if (asan_check_optimize
835 && gimple_call_builtin_p (stmt
, BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT
))
837 gcc_assert (!hwasan_sanitize_p ());
840 if (single_imm_use (gimple_vdef (stmt
), &use
, &use_stmt
))
842 if (is_gimple_call (use_stmt
)
843 && gimple_call_builtin_p (use_stmt
,
844 BUILT_IN_ASAN_AFTER_DYNAMIC_INIT
))
846 unlink_stmt_vdef (use_stmt
);
847 gimple_stmt_iterator gsi2
= gsi_for_stmt (use_stmt
);
848 gsi_remove (&gsi2
, true);
854 if (gimple_call_internal_p (stmt
))
855 switch (gimple_call_internal_fn (stmt
))
858 remove
= maybe_optimize_ubsan_null_ifn (ctx
, stmt
);
861 remove
= maybe_optimize_ubsan_vptr_ifn (ctx
, stmt
);
864 remove
= maybe_optimize_ubsan_ptr_ifn (ctx
, stmt
);
866 case IFN_HWASAN_CHECK
:
868 if (asan_check_optimize
)
869 remove
= maybe_optimize_asan_check_ifn (ctx
, stmt
);
871 ctx
->asan_num_accesses
++;
874 ctx
->contains_asan_mark
= true;
882 /* Drop this check. */
883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
885 fprintf (dump_file
, "Optimizing out: ");
886 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
888 unlink_stmt_vdef (stmt
);
889 gsi_remove (&gsi
, true);
893 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
895 fprintf (dump_file
, "Leaving: ");
896 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
903 if (asan_check_optimize
)
905 info
->has_freeing_call_p
= info
->freeing_call_events
!= 0;
906 info
->has_freeing_call_computed_p
= true;
909 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
911 son
= next_dom_son (CDI_DOMINATORS
, son
))
912 sanopt_optimize_walker (son
, ctx
);
914 /* We're leaving this BB, so mark it to that effect. */
915 info
->visited_p
= true;
918 /* Try to remove redundant sanitizer checks in function FUN. */
921 sanopt_optimize (function
*fun
, bool *contains_asan_mark
)
923 class sanopt_ctx ctx
;
924 ctx
.asan_num_accesses
= 0;
925 ctx
.contains_asan_mark
= false;
927 /* Set up block info for each basic block. */
928 alloc_aux_for_blocks (sizeof (sanopt_info
));
930 /* We're going to do a dominator walk, so ensure that we have
931 dominance information. */
932 calculate_dominance_info (CDI_DOMINATORS
);
934 /* Recursively walk the dominator tree optimizing away
936 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun
), &ctx
);
938 free_aux_for_blocks ();
940 *contains_asan_mark
= ctx
.contains_asan_mark
;
941 return ctx
.asan_num_accesses
;
944 /* Perform optimization of sanitize functions. */
948 const pass_data pass_data_sanopt
=
950 GIMPLE_PASS
, /* type */
952 OPTGROUP_NONE
, /* optinfo_flags */
954 ( PROP_ssa
| PROP_cfg
| PROP_gimple_leh
), /* properties_required */
955 0, /* properties_provided */
956 0, /* properties_destroyed */
957 0, /* todo_flags_start */
958 TODO_update_ssa
, /* todo_flags_finish */
961 class pass_sanopt
: public gimple_opt_pass
964 pass_sanopt (gcc::context
*ctxt
)
965 : gimple_opt_pass (pass_data_sanopt
, ctxt
)
968 /* opt_pass methods: */
969 bool gate (function
*) final override
971 /* SANITIZE_RETURN is handled in the front-end. When trapping,
972 SANITIZE_UNREACHABLE is handled by builtin_decl_unreachable. */
973 unsigned int mask
= SANITIZE_RETURN
;
974 if (flag_sanitize_trap
& SANITIZE_UNREACHABLE
)
975 mask
|= SANITIZE_UNREACHABLE
;
976 return flag_sanitize
& ~mask
;
978 unsigned int execute (function
*) final override
;
980 }; // class pass_sanopt
982 /* Sanitize all ASAN_MARK unpoison calls that are not reachable by a BB
983 that contains an ASAN_MARK poison. All these ASAN_MARK unpoison call
984 can be removed as all variables are unpoisoned in a function prologue. */
987 sanitize_asan_mark_unpoison (void)
989 /* 1) Find all BBs that contain an ASAN_MARK poison call. */
990 auto_sbitmap
with_poison (last_basic_block_for_fn (cfun
) + 1);
991 bitmap_clear (with_poison
);
994 FOR_EACH_BB_FN (bb
, cfun
)
996 if (bitmap_bit_p (with_poison
, bb
->index
))
999 gimple_stmt_iterator gsi
;
1000 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1002 gimple
*stmt
= gsi_stmt (gsi
);
1003 if (asan_mark_p (stmt
, ASAN_MARK_POISON
))
1005 bitmap_set_bit (with_poison
, bb
->index
);
1011 auto_sbitmap
poisoned (last_basic_block_for_fn (cfun
) + 1);
1012 bitmap_clear (poisoned
);
1013 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
1014 bitmap_copy (worklist
, with_poison
);
1016 /* 2) Propagate the information to all reachable blocks. */
1017 while (!bitmap_empty_p (worklist
))
1019 unsigned i
= bitmap_first_set_bit (worklist
);
1020 bitmap_clear_bit (worklist
, i
);
1021 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
1026 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1027 if (!bitmap_bit_p (poisoned
, e
->dest
->index
))
1029 bitmap_set_bit (poisoned
, e
->dest
->index
);
1030 bitmap_set_bit (worklist
, e
->dest
->index
);
1034 /* 3) Iterate all BBs not included in POISONED BBs and remove unpoison
1035 ASAN_MARK preceding an ASAN_MARK poison (which can still happen). */
1036 FOR_EACH_BB_FN (bb
, cfun
)
1038 if (bitmap_bit_p (poisoned
, bb
->index
))
1041 gimple_stmt_iterator gsi
;
1042 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
1044 gimple
*stmt
= gsi_stmt (gsi
);
1045 if (gimple_call_internal_p (stmt
, IFN_ASAN_MARK
))
1047 if (asan_mark_p (stmt
, ASAN_MARK_POISON
))
1052 fprintf (dump_file
, "Removing ASAN_MARK unpoison\n");
1053 unlink_stmt_vdef (stmt
);
1054 release_defs (stmt
);
1055 gsi_remove (&gsi
, true);
1065 /* Return true when STMT is either ASAN_CHECK call or a call of a function
1066 that can contain an ASAN_CHECK. */
1069 maybe_contains_asan_check (gimple
*stmt
)
1071 if (is_gimple_call (stmt
))
1073 if (gimple_call_internal_p (stmt
, IFN_ASAN_MARK
))
1076 return !(gimple_call_flags (stmt
) & ECF_CONST
);
1078 else if (is_a
<gasm
*> (stmt
))
1084 /* Sanitize all ASAN_MARK poison calls that are not followed by an ASAN_CHECK
1085 call. These calls can be removed. */
1088 sanitize_asan_mark_poison (void)
1090 /* 1) Find all BBs that possibly contain an ASAN_CHECK. */
1091 auto_sbitmap
with_check (last_basic_block_for_fn (cfun
) + 1);
1092 bitmap_clear (with_check
);
1095 FOR_EACH_BB_FN (bb
, cfun
)
1097 gimple_stmt_iterator gsi
;
1098 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1100 gimple
*stmt
= gsi_stmt (gsi
);
1101 if (maybe_contains_asan_check (stmt
))
1103 bitmap_set_bit (with_check
, bb
->index
);
1109 auto_sbitmap
can_reach_check (last_basic_block_for_fn (cfun
) + 1);
1110 bitmap_clear (can_reach_check
);
1111 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
1112 bitmap_copy (worklist
, with_check
);
1114 /* 2) Propagate the information to all definitions blocks. */
1115 while (!bitmap_empty_p (worklist
))
1117 unsigned i
= bitmap_first_set_bit (worklist
);
1118 bitmap_clear_bit (worklist
, i
);
1119 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
1124 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1125 if (!bitmap_bit_p (can_reach_check
, e
->src
->index
))
1127 bitmap_set_bit (can_reach_check
, e
->src
->index
);
1128 bitmap_set_bit (worklist
, e
->src
->index
);
1132 /* 3) Iterate all BBs not included in CAN_REACH_CHECK BBs and remove poison
1133 ASAN_MARK not followed by a call to function having an ASAN_CHECK. */
1134 FOR_EACH_BB_FN (bb
, cfun
)
1136 if (bitmap_bit_p (can_reach_check
, bb
->index
))
1139 gimple_stmt_iterator gsi
;
1140 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
1142 gimple
*stmt
= gsi_stmt (gsi
);
1143 if (maybe_contains_asan_check (stmt
))
1145 else if (asan_mark_p (stmt
, ASAN_MARK_POISON
))
1148 fprintf (dump_file
, "Removing ASAN_MARK poison\n");
1149 unlink_stmt_vdef (stmt
);
1150 release_defs (stmt
);
1151 gimple_stmt_iterator gsi2
= gsi
;
1153 gsi_remove (&gsi2
, true);
1162 /* Rewrite all usages of tree OP which is a PARM_DECL with a VAR_DECL
1163 that is it's DECL_VALUE_EXPR. */
1166 rewrite_usage_of_param (tree
*op
, int *walk_subtrees
, void *)
1168 if (TREE_CODE (*op
) == PARM_DECL
&& DECL_HAS_VALUE_EXPR_P (*op
))
1170 *op
= DECL_VALUE_EXPR (*op
);
1177 /* For a given function FUN, rewrite all addressable parameters so that
1178 a new automatic variable is introduced. Right after function entry
1179 a parameter is assigned to the variable. */
1182 sanitize_rewrite_addressable_params (function
*fun
)
1185 gimple_seq stmts
= NULL
;
1186 bool has_any_addressable_param
= false;
1187 auto_vec
<tree
> clear_value_expr_list
;
1189 for (tree arg
= DECL_ARGUMENTS (current_function_decl
);
1190 arg
; arg
= DECL_CHAIN (arg
))
1192 tree type
= TREE_TYPE (arg
);
1193 if (TREE_ADDRESSABLE (arg
)
1194 && !TREE_ADDRESSABLE (type
)
1195 && !TREE_THIS_VOLATILE (arg
)
1196 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
1198 TREE_ADDRESSABLE (arg
) = 0;
1199 DECL_NOT_GIMPLE_REG_P (arg
) = 0;
1200 /* The parameter is no longer addressable. */
1201 has_any_addressable_param
= true;
1203 /* Create a new automatic variable. */
1204 tree var
= build_decl (DECL_SOURCE_LOCATION (arg
),
1205 VAR_DECL
, DECL_NAME (arg
), type
);
1206 TREE_ADDRESSABLE (var
) = 1;
1207 DECL_IGNORED_P (var
) = 1;
1209 gimple_add_tmp_var (var
);
1211 /* We skip parameters that have a DECL_VALUE_EXPR. */
1212 if (DECL_HAS_VALUE_EXPR_P (arg
))
1218 "Rewriting parameter whose address is taken: ");
1219 print_generic_expr (dump_file
, arg
, dump_flags
);
1220 fputc ('\n', dump_file
);
1223 SET_DECL_PT_UID (var
, DECL_PT_UID (arg
));
1225 /* Assign value of parameter to newly created variable. */
1226 if ((TREE_CODE (type
) == COMPLEX_TYPE
1227 || TREE_CODE (type
) == VECTOR_TYPE
))
1229 /* We need to create a SSA name that will be used for the
1231 tree tmp
= get_or_create_ssa_default_def (cfun
, arg
);
1232 g
= gimple_build_assign (var
, tmp
);
1233 gimple_set_location (g
, DECL_SOURCE_LOCATION (arg
));
1234 gimple_seq_add_stmt (&stmts
, g
);
1238 g
= gimple_build_assign (var
, arg
);
1239 gimple_set_location (g
, DECL_SOURCE_LOCATION (arg
));
1240 gimple_seq_add_stmt (&stmts
, g
);
1243 if (target_for_debug_bind (arg
))
1245 g
= gimple_build_debug_bind (arg
, var
, NULL
);
1246 gimple_seq_add_stmt (&stmts
, g
);
1247 clear_value_expr_list
.safe_push (arg
);
1250 DECL_HAS_VALUE_EXPR_P (arg
) = 1;
1251 SET_DECL_VALUE_EXPR (arg
, var
);
1255 if (!has_any_addressable_param
)
1258 /* Replace all usages of PARM_DECLs with the newly
1259 created variable VAR. */
1261 FOR_EACH_BB_FN (bb
, fun
)
1263 gimple_stmt_iterator gsi
;
1264 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1266 gimple
*stmt
= gsi_stmt (gsi
);
1267 gimple_stmt_iterator it
= gsi_for_stmt (stmt
);
1268 walk_gimple_stmt (&it
, NULL
, rewrite_usage_of_param
, NULL
);
1270 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1272 gphi
*phi
= dyn_cast
<gphi
*> (gsi_stmt (gsi
));
1273 for (unsigned i
= 0; i
< gimple_phi_num_args (phi
); ++i
)
1275 hash_set
<tree
> visited_nodes
;
1276 walk_tree (gimple_phi_arg_def_ptr (phi
, i
),
1277 rewrite_usage_of_param
, NULL
, &visited_nodes
);
1282 /* Unset value expr for parameters for which we created debug bind
1284 for (tree arg
: clear_value_expr_list
)
1286 DECL_HAS_VALUE_EXPR_P (arg
) = 0;
1287 SET_DECL_VALUE_EXPR (arg
, NULL_TREE
);
1290 /* Insert default assignments at the beginning of a function. */
1291 basic_block entry_bb
= ENTRY_BLOCK_PTR_FOR_FN (fun
);
1292 entry_bb
= split_edge (single_succ_edge (entry_bb
));
1294 gimple_stmt_iterator gsi
= gsi_start_bb (entry_bb
);
1295 gsi_insert_seq_before (&gsi
, stmts
, GSI_NEW_STMT
);
1299 pass_sanopt::execute (function
*fun
)
1301 /* n.b. ASAN_MARK is used for both HWASAN and ASAN.
1302 asan_num_accesses is hence used to count either HWASAN_CHECK or ASAN_CHECK
1303 stuff. This is fine because you can only have one of these active at a
1306 int asan_num_accesses
= 0;
1307 bool contains_asan_mark
= false;
1309 /* Try to remove redundant checks. */
1312 & (SANITIZE_NULL
| SANITIZE_ALIGNMENT
| SANITIZE_HWADDRESS
1313 | SANITIZE_ADDRESS
| SANITIZE_VPTR
| SANITIZE_POINTER_OVERFLOW
)))
1314 asan_num_accesses
= sanopt_optimize (fun
, &contains_asan_mark
);
1315 else if (flag_sanitize
& (SANITIZE_ADDRESS
| SANITIZE_HWADDRESS
))
1317 gimple_stmt_iterator gsi
;
1318 FOR_EACH_BB_FN (bb
, fun
)
1319 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1321 gimple
*stmt
= gsi_stmt (gsi
);
1322 if (gimple_call_internal_p (stmt
, IFN_ASAN_CHECK
))
1323 ++asan_num_accesses
;
1324 else if (gimple_call_internal_p (stmt
, IFN_ASAN_MARK
))
1325 contains_asan_mark
= true;
1329 if (contains_asan_mark
)
1331 sanitize_asan_mark_unpoison ();
1332 sanitize_asan_mark_poison ();
1335 if (asan_sanitize_stack_p () || hwasan_sanitize_stack_p ())
1336 sanitize_rewrite_addressable_params (fun
);
1338 bool use_calls
= param_asan_instrumentation_with_call_threshold
< INT_MAX
1339 && asan_num_accesses
>= param_asan_instrumentation_with_call_threshold
;
1341 hash_map
<tree
, tree
> shadow_vars_mapping
;
1342 bool need_commit_edge_insert
= false;
1343 FOR_EACH_BB_FN (bb
, fun
)
1345 gimple_stmt_iterator gsi
;
1346 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
1348 gimple
*stmt
= gsi_stmt (gsi
);
1349 bool no_next
= false;
1351 if (!is_gimple_call (stmt
))
1357 if (gimple_call_internal_p (stmt
))
1359 enum internal_fn ifn
= gimple_call_internal_fn (stmt
);
1362 case IFN_UBSAN_NULL
:
1363 no_next
= ubsan_expand_null_ifn (&gsi
);
1365 case IFN_UBSAN_BOUNDS
:
1366 no_next
= ubsan_expand_bounds_ifn (&gsi
);
1368 case IFN_UBSAN_OBJECT_SIZE
:
1369 no_next
= ubsan_expand_objsize_ifn (&gsi
);
1372 no_next
= ubsan_expand_ptr_ifn (&gsi
);
1374 case IFN_UBSAN_VPTR
:
1375 no_next
= ubsan_expand_vptr_ifn (&gsi
);
1377 case IFN_HWASAN_CHECK
:
1378 no_next
= hwasan_expand_check_ifn (&gsi
, use_calls
);
1380 case IFN_ASAN_CHECK
:
1381 no_next
= asan_expand_check_ifn (&gsi
, use_calls
);
1384 no_next
= asan_expand_mark_ifn (&gsi
);
1386 case IFN_ASAN_POISON
:
1387 no_next
= asan_expand_poison_ifn (&gsi
,
1388 &need_commit_edge_insert
,
1389 shadow_vars_mapping
);
1391 case IFN_HWASAN_MARK
:
1392 no_next
= hwasan_expand_mark_ifn (&gsi
);
1398 else if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
1400 tree callee
= gimple_call_fndecl (stmt
);
1401 switch (DECL_FUNCTION_CODE (callee
))
1403 case BUILT_IN_UNREACHABLE
:
1404 if (sanitize_flags_p (SANITIZE_UNREACHABLE
))
1405 no_next
= ubsan_instrument_unreachable (&gsi
);
1412 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1414 fprintf (dump_file
, "Expanded: ");
1415 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
1423 if (need_commit_edge_insert
)
1424 gsi_commit_edge_inserts ();
1432 make_pass_sanopt (gcc::context
*ctxt
)
1434 return new pass_sanopt (ctxt
);