1 /* Optimize and expand sanitizer functions.
2 Copyright (C) 2014-2015 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 "fold-const.h"
31 #include "hard-reg-set.h"
33 #include "dominance.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
41 #include "gimple-iterator.h"
42 #include "plugin-api.h"
43 #include "tree-pass.h"
45 #include "gimple-pretty-print.h"
47 #include "langhooks.h"
50 #include "tree-ssa-operands.h"
53 /* This is used to carry information about basic blocks. It is
54 attached to the AUX field of the standard CFG block. */
58 /* True if this BB might call (directly or indirectly) free/munmap
59 or similar operation. */
60 bool has_freeing_call_p
;
62 /* True if HAS_FREEING_CALL_P flag has been computed. */
63 bool has_freeing_call_computed_p
;
65 /* True if there is a block with HAS_FREEING_CALL_P flag set
66 on any path between an immediate dominator of BB, denoted
68 bool imm_dom_path_with_freeing_call_p
;
70 /* True if IMM_DOM_PATH_WITH_FREEING_CALL_P has been computed. */
71 bool imm_dom_path_with_freeing_call_computed_p
;
73 /* Number of possibly freeing calls encountered in this bb
75 uint64_t freeing_call_events
;
77 /* True if BB is currently being visited during computation
78 of IMM_DOM_PATH_WITH_FREEING_CALL_P flag. */
81 /* True if this BB has been visited in the dominator walk. */
85 /* If T has a single definition of form T = T2, return T2. */
88 maybe_get_single_definition (tree t
)
90 if (TREE_CODE (t
) == SSA_NAME
)
92 gimple g
= SSA_NAME_DEF_STMT (t
);
93 if (gimple_assign_single_p (g
))
94 return gimple_assign_rhs1 (g
);
99 /* Traits class for tree hash maps below. */
101 struct sanopt_tree_map_traits
: default_hashmap_traits
103 static inline hashval_t
hash (const_tree ref
)
105 return iterative_hash_expr (ref
, 0);
108 static inline bool equal_keys (const_tree ref1
, const_tree ref2
)
110 return operand_equal_p (ref1
, ref2
, 0);
114 /* Tree triplet for vptr_check_map. */
115 struct sanopt_tree_triplet
120 /* Traits class for tree triplet hash maps below. */
122 struct sanopt_tree_triplet_map_traits
: default_hashmap_traits
124 static inline hashval_t
125 hash (const sanopt_tree_triplet
&ref
)
127 inchash::hash
hstate (0);
128 inchash::add_expr (ref
.t1
, hstate
);
129 inchash::add_expr (ref
.t2
, hstate
);
130 inchash::add_expr (ref
.t3
, hstate
);
131 return hstate
.end ();
135 equal_keys (const sanopt_tree_triplet
&ref1
, const sanopt_tree_triplet
&ref2
)
137 return operand_equal_p (ref1
.t1
, ref2
.t1
, 0)
138 && operand_equal_p (ref1
.t2
, ref2
.t2
, 0)
139 && operand_equal_p (ref1
.t3
, ref2
.t3
, 0);
146 e
.m_key
.t1
= reinterpret_cast<T
*> (1);
160 return e
.m_key
.t1
== (void *) 1;
167 return e
.m_key
.t1
== NULL
;
171 /* This is used to carry various hash maps and variables used
172 in sanopt_optimize_walker. */
176 /* This map maps a pointer (the first argument of UBSAN_NULL) to
177 a vector of UBSAN_NULL call statements that check this pointer. */
178 hash_map
<tree
, auto_vec
<gimple
> > null_check_map
;
180 /* This map maps a pointer (the second argument of ASAN_CHECK) to
181 a vector of ASAN_CHECK call statements that check the access. */
182 hash_map
<tree
, auto_vec
<gimple
>, sanopt_tree_map_traits
> asan_check_map
;
184 /* This map maps a tree triplet (the first, second and fourth argument
185 of UBSAN_VPTR) to a vector of UBSAN_VPTR call statements that check
186 that virtual table pointer. */
187 hash_map
<sanopt_tree_triplet
, auto_vec
<gimple
>,
188 sanopt_tree_triplet_map_traits
> vptr_check_map
;
190 /* Number of IFN_ASAN_CHECK statements. */
191 int asan_num_accesses
;
195 /* Return true if there might be any call to free/munmap operation
196 on any path in between DOM (which should be imm(BB)) and BB. */
199 imm_dom_path_with_freeing_call (basic_block bb
, basic_block dom
)
201 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
205 if (info
->imm_dom_path_with_freeing_call_computed_p
)
206 return info
->imm_dom_path_with_freeing_call_p
;
208 info
->being_visited_p
= true;
210 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
212 sanopt_info
*pred_info
= (sanopt_info
*) e
->src
->aux
;
217 if ((pred_info
->imm_dom_path_with_freeing_call_computed_p
218 && pred_info
->imm_dom_path_with_freeing_call_p
)
219 || (pred_info
->has_freeing_call_computed_p
220 && pred_info
->has_freeing_call_p
))
222 info
->imm_dom_path_with_freeing_call_computed_p
= true;
223 info
->imm_dom_path_with_freeing_call_p
= true;
224 info
->being_visited_p
= false;
229 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
231 sanopt_info
*pred_info
= (sanopt_info
*) e
->src
->aux
;
236 if (pred_info
->has_freeing_call_computed_p
)
239 gimple_stmt_iterator gsi
;
240 for (gsi
= gsi_start_bb (e
->src
); !gsi_end_p (gsi
); gsi_next (&gsi
))
242 gimple stmt
= gsi_stmt (gsi
);
244 if (is_gimple_call (stmt
) && !nonfreeing_call_p (stmt
))
246 pred_info
->has_freeing_call_p
= true;
251 pred_info
->has_freeing_call_computed_p
= true;
252 if (pred_info
->has_freeing_call_p
)
254 info
->imm_dom_path_with_freeing_call_computed_p
= true;
255 info
->imm_dom_path_with_freeing_call_p
= true;
256 info
->being_visited_p
= false;
261 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
267 for (src
= e
->src
; src
!= dom
; )
269 sanopt_info
*pred_info
= (sanopt_info
*) src
->aux
;
270 if (pred_info
->being_visited_p
)
272 basic_block imm
= get_immediate_dominator (CDI_DOMINATORS
, src
);
273 if (imm_dom_path_with_freeing_call (src
, imm
))
275 info
->imm_dom_path_with_freeing_call_computed_p
= true;
276 info
->imm_dom_path_with_freeing_call_p
= true;
277 info
->being_visited_p
= false;
284 info
->imm_dom_path_with_freeing_call_computed_p
= true;
285 info
->imm_dom_path_with_freeing_call_p
= false;
286 info
->being_visited_p
= false;
290 /* Get the first dominating check from the list of stored checks.
291 Non-dominating checks are silently dropped. */
294 maybe_get_dominating_check (auto_vec
<gimple
> &v
)
296 for (; !v
.is_empty (); v
.pop ())
298 gimple g
= v
.last ();
299 sanopt_info
*si
= (sanopt_info
*) gimple_bb (g
)->aux
;
301 /* At this point we shouldn't have any statements
302 that aren't dominating the current BB. */
308 /* Optimize away redundant UBSAN_NULL calls. */
311 maybe_optimize_ubsan_null_ifn (struct sanopt_ctx
*ctx
, gimple stmt
)
313 gcc_assert (gimple_call_num_args (stmt
) == 3);
314 tree ptr
= gimple_call_arg (stmt
, 0);
315 tree cur_align
= gimple_call_arg (stmt
, 2);
316 gcc_assert (TREE_CODE (cur_align
) == INTEGER_CST
);
319 auto_vec
<gimple
> &v
= ctx
->null_check_map
.get_or_insert (ptr
);
320 gimple g
= maybe_get_dominating_check (v
);
323 /* For this PTR we don't have any UBSAN_NULL stmts recorded, so there's
324 nothing to optimize yet. */
329 /* We already have recorded a UBSAN_NULL check for this pointer. Perhaps we
330 can drop this one. But only if this check doesn't specify stricter
333 tree align
= gimple_call_arg (g
, 2);
334 int kind
= tree_to_shwi (gimple_call_arg (g
, 1));
335 /* If this is a NULL pointer check where we had segv anyway, we can
337 if (integer_zerop (align
)
338 && (kind
== UBSAN_LOAD_OF
339 || kind
== UBSAN_STORE_OF
340 || kind
== UBSAN_MEMBER_ACCESS
))
342 /* Otherwise remove the check in non-recovering mode, or if the
343 stmts have same location. */
344 else if (integer_zerop (align
))
345 remove
= (flag_sanitize_recover
& SANITIZE_NULL
) == 0
346 || flag_sanitize_undefined_trap_on_error
347 || gimple_location (g
) == gimple_location (stmt
);
348 else if (tree_int_cst_le (cur_align
, align
))
349 remove
= (flag_sanitize_recover
& SANITIZE_ALIGNMENT
) == 0
350 || flag_sanitize_undefined_trap_on_error
351 || gimple_location (g
) == gimple_location (stmt
);
353 if (!remove
&& gimple_bb (g
) == gimple_bb (stmt
)
354 && tree_int_cst_compare (cur_align
, align
) == 0)
362 /* Optimize away redundant UBSAN_VPTR calls. The second argument
363 is the value loaded from the virtual table, so rely on FRE to find out
364 when we can actually optimize. */
367 maybe_optimize_ubsan_vptr_ifn (struct sanopt_ctx
*ctx
, gimple stmt
)
369 gcc_assert (gimple_call_num_args (stmt
) == 5);
370 sanopt_tree_triplet triplet
;
371 triplet
.t1
= gimple_call_arg (stmt
, 0);
372 triplet
.t2
= gimple_call_arg (stmt
, 1);
373 triplet
.t3
= gimple_call_arg (stmt
, 3);
375 auto_vec
<gimple
> &v
= ctx
->vptr_check_map
.get_or_insert (triplet
);
376 gimple g
= maybe_get_dominating_check (v
);
379 /* For this PTR we don't have any UBSAN_VPTR stmts recorded, so there's
380 nothing to optimize yet. */
388 /* Returns TRUE if ASan check of length LEN in block BB can be removed
389 if preceded by checks in V. */
392 can_remove_asan_check (auto_vec
<gimple
> &v
, tree len
, basic_block bb
)
396 gimple to_pop
= NULL
;
398 basic_block last_bb
= bb
;
399 bool cleanup
= false;
401 FOR_EACH_VEC_ELT_REVERSE (v
, i
, g
)
403 basic_block gbb
= gimple_bb (g
);
404 sanopt_info
*si
= (sanopt_info
*) gbb
->aux
;
405 if (gimple_uid (g
) < si
->freeing_call_events
)
407 /* If there is a potentially freeing call after g in gbb, we should
408 remove it from the vector, can't use in optimization. */
413 tree glen
= gimple_call_arg (g
, 2);
414 gcc_assert (TREE_CODE (glen
) == INTEGER_CST
);
416 /* If we've checked only smaller length than we want to check now,
417 we can't remove the current stmt. If g is in the same basic block,
418 we want to remove it though, as the current stmt is better. */
419 if (tree_int_cst_lt (glen
, len
))
429 while (last_bb
!= gbb
)
431 /* Paths from last_bb to bb have been checked before.
432 gbb is necessarily a dominator of last_bb, but not necessarily
433 immediate dominator. */
434 if (((sanopt_info
*) last_bb
->aux
)->freeing_call_events
)
437 basic_block imm
= get_immediate_dominator (CDI_DOMINATORS
, last_bb
);
439 if (imm_dom_path_with_freeing_call (last_bb
, imm
))
451 unsigned int j
= 0, l
= v
.length ();
452 for (i
= 0; i
< l
; i
++)
454 && (gimple_uid (v
[i
])
456 gimple_bb (v
[i
])->aux
)->freeing_call_events
))
468 /* Optimize away redundant ASAN_CHECK calls. */
471 maybe_optimize_asan_check_ifn (struct sanopt_ctx
*ctx
, gimple stmt
)
473 gcc_assert (gimple_call_num_args (stmt
) == 4);
474 tree ptr
= gimple_call_arg (stmt
, 1);
475 tree len
= gimple_call_arg (stmt
, 2);
476 basic_block bb
= gimple_bb (stmt
);
477 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
479 if (TREE_CODE (len
) != INTEGER_CST
)
481 if (integer_zerop (len
))
484 gimple_set_uid (stmt
, info
->freeing_call_events
);
486 auto_vec
<gimple
> *ptr_checks
= &ctx
->asan_check_map
.get_or_insert (ptr
);
488 tree base_addr
= maybe_get_single_definition (ptr
);
489 auto_vec
<gimple
> *base_checks
= NULL
;
492 base_checks
= &ctx
->asan_check_map
.get_or_insert (base_addr
);
493 /* Original pointer might have been invalidated. */
494 ptr_checks
= ctx
->asan_check_map
.get (ptr
);
497 gimple g
= maybe_get_dominating_check (*ptr_checks
);
501 /* Try with base address as well. */
502 g2
= maybe_get_dominating_check (*base_checks
);
504 if (g
== NULL
&& g2
== NULL
)
506 /* For this PTR we don't have any ASAN_CHECK stmts recorded, so there's
507 nothing to optimize yet. */
508 ptr_checks
->safe_push (stmt
);
510 base_checks
->safe_push (stmt
);
517 remove
= can_remove_asan_check (*ptr_checks
, len
, bb
);
519 if (!remove
&& base_checks
)
520 /* Try with base address as well. */
521 remove
= can_remove_asan_check (*base_checks
, len
, bb
);
525 ptr_checks
->safe_push (stmt
);
527 base_checks
->safe_push (stmt
);
533 /* Try to optimize away redundant UBSAN_NULL and ASAN_CHECK calls.
535 We walk blocks in the CFG via a depth first search of the dominator
536 tree; we push unique UBSAN_NULL or ASAN_CHECK statements into a vector
537 in the NULL_CHECK_MAP or ASAN_CHECK_MAP hash maps as we enter the
538 blocks. When leaving a block, we mark the block as visited; then
539 when checking the statements in the vector, we ignore statements that
540 are coming from already visited blocks, because these cannot dominate
541 anything anymore. CTX is a sanopt context. */
544 sanopt_optimize_walker (basic_block bb
, struct sanopt_ctx
*ctx
)
547 gimple_stmt_iterator gsi
;
548 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
549 bool asan_check_optimize
= (flag_sanitize
& SANITIZE_ADDRESS
) != 0;
551 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
553 gimple stmt
= gsi_stmt (gsi
);
556 if (!is_gimple_call (stmt
))
558 /* Handle asm volatile or asm with "memory" clobber
559 the same as potentionally freeing call. */
560 gasm
*asm_stmt
= dyn_cast
<gasm
*> (stmt
);
562 && asan_check_optimize
563 && (gimple_asm_clobbers_memory_p (asm_stmt
)
564 || gimple_asm_volatile_p (asm_stmt
)))
565 info
->freeing_call_events
++;
570 if (asan_check_optimize
&& !nonfreeing_call_p (stmt
))
571 info
->freeing_call_events
++;
573 if (gimple_call_internal_p (stmt
))
574 switch (gimple_call_internal_fn (stmt
))
577 remove
= maybe_optimize_ubsan_null_ifn (ctx
, stmt
);
580 remove
= maybe_optimize_ubsan_vptr_ifn (ctx
, stmt
);
583 if (asan_check_optimize
)
584 remove
= maybe_optimize_asan_check_ifn (ctx
, stmt
);
586 ctx
->asan_num_accesses
++;
594 /* Drop this check. */
595 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
597 fprintf (dump_file
, "Optimizing out\n ");
598 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
599 fprintf (dump_file
, "\n");
601 unlink_stmt_vdef (stmt
);
602 gsi_remove (&gsi
, true);
608 if (asan_check_optimize
)
610 info
->has_freeing_call_p
= info
->freeing_call_events
!= 0;
611 info
->has_freeing_call_computed_p
= true;
614 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
616 son
= next_dom_son (CDI_DOMINATORS
, son
))
617 sanopt_optimize_walker (son
, ctx
);
619 /* We're leaving this BB, so mark it to that effect. */
620 info
->visited_p
= true;
623 /* Try to remove redundant sanitizer checks in function FUN. */
626 sanopt_optimize (function
*fun
)
628 struct sanopt_ctx ctx
;
629 ctx
.asan_num_accesses
= 0;
631 /* Set up block info for each basic block. */
632 alloc_aux_for_blocks (sizeof (sanopt_info
));
634 /* We're going to do a dominator walk, so ensure that we have
635 dominance information. */
636 calculate_dominance_info (CDI_DOMINATORS
);
638 /* Recursively walk the dominator tree optimizing away
640 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun
), &ctx
);
642 free_aux_for_blocks ();
644 return ctx
.asan_num_accesses
;
647 /* Perform optimization of sanitize functions. */
651 const pass_data pass_data_sanopt
=
653 GIMPLE_PASS
, /* type */
655 OPTGROUP_NONE
, /* optinfo_flags */
657 ( PROP_ssa
| PROP_cfg
| PROP_gimple_leh
), /* properties_required */
658 0, /* properties_provided */
659 0, /* properties_destroyed */
660 0, /* todo_flags_start */
661 TODO_update_ssa
, /* todo_flags_finish */
664 class pass_sanopt
: public gimple_opt_pass
667 pass_sanopt (gcc::context
*ctxt
)
668 : gimple_opt_pass (pass_data_sanopt
, ctxt
)
671 /* opt_pass methods: */
672 virtual bool gate (function
*) { return flag_sanitize
; }
673 virtual unsigned int execute (function
*);
675 }; // class pass_sanopt
678 pass_sanopt::execute (function
*fun
)
681 int asan_num_accesses
= 0;
683 /* Try to remove redundant checks. */
686 & (SANITIZE_NULL
| SANITIZE_ALIGNMENT
687 | SANITIZE_ADDRESS
| SANITIZE_VPTR
)))
688 asan_num_accesses
= sanopt_optimize (fun
);
689 else if (flag_sanitize
& SANITIZE_ADDRESS
)
691 gimple_stmt_iterator gsi
;
692 FOR_EACH_BB_FN (bb
, fun
)
693 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
695 gimple stmt
= gsi_stmt (gsi
);
696 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
)
697 && gimple_call_internal_fn (stmt
) == IFN_ASAN_CHECK
)
702 bool use_calls
= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
< INT_MAX
703 && asan_num_accesses
>= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
;
705 FOR_EACH_BB_FN (bb
, fun
)
707 gimple_stmt_iterator gsi
;
708 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
710 gimple stmt
= gsi_stmt (gsi
);
711 bool no_next
= false;
713 if (!is_gimple_call (stmt
))
719 if (gimple_call_internal_p (stmt
))
721 enum internal_fn ifn
= gimple_call_internal_fn (stmt
);
725 no_next
= ubsan_expand_null_ifn (&gsi
);
727 case IFN_UBSAN_BOUNDS
:
728 no_next
= ubsan_expand_bounds_ifn (&gsi
);
730 case IFN_UBSAN_OBJECT_SIZE
:
731 no_next
= ubsan_expand_objsize_ifn (&gsi
);
734 no_next
= ubsan_expand_vptr_ifn (&gsi
);
737 no_next
= asan_expand_check_ifn (&gsi
, use_calls
);
743 else if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
745 tree callee
= gimple_call_fndecl (stmt
);
746 switch (DECL_FUNCTION_CODE (callee
))
748 case BUILT_IN_UNREACHABLE
:
749 if (flag_sanitize
& SANITIZE_UNREACHABLE
750 && !lookup_attribute ("no_sanitize_undefined",
751 DECL_ATTRIBUTES (fun
->decl
)))
752 no_next
= ubsan_instrument_unreachable (&gsi
);
759 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
761 fprintf (dump_file
, "Expanded\n ");
762 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
763 fprintf (dump_file
, "\n");
776 make_pass_sanopt (gcc::context
*ctxt
)
778 return new pass_sanopt (ctxt
);