1 /* Optimize and expand sanitizer functions.
2 Copyright (C) 2014-2016 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"
27 #include "tree-pass.h"
28 #include "tree-ssa-operands.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "gimple-iterator.h"
36 #include "tree-hash-traits.h"
39 /* This is used to carry information about basic blocks. It is
40 attached to the AUX field of the standard CFG block. */
44 /* True if this BB might call (directly or indirectly) free/munmap
45 or similar operation. */
46 bool has_freeing_call_p;
48 /* True if HAS_FREEING_CALL_P flag has been computed. */
49 bool has_freeing_call_computed_p;
51 /* True if there is a block with HAS_FREEING_CALL_P flag set
52 on any path between an immediate dominator of BB, denoted
54 bool imm_dom_path_with_freeing_call_p;
56 /* True if IMM_DOM_PATH_WITH_FREEING_CALL_P has been computed. */
57 bool imm_dom_path_with_freeing_call_computed_p;
59 /* Number of possibly freeing calls encountered in this bb
61 uint64_t freeing_call_events;
63 /* True if BB is currently being visited during computation
64 of IMM_DOM_PATH_WITH_FREEING_CALL_P flag. */
67 /* True if this BB has been visited in the dominator walk. */
71 /* If T has a single definition of form T = T2, return T2. */
74 maybe_get_single_definition (tree t)
76 if (TREE_CODE (t) == SSA_NAME)
78 gimple *g = SSA_NAME_DEF_STMT (t);
79 if (gimple_assign_single_p (g))
80 return gimple_assign_rhs1 (g);
85 /* Tree triplet for vptr_check_map. */
86 struct sanopt_tree_triplet
91 /* Traits class for tree triplet hash maps below. */
93 struct sanopt_tree_triplet_hash : typed_noop_remove <sanopt_tree_triplet>
95 typedef sanopt_tree_triplet value_type;
96 typedef sanopt_tree_triplet compare_type;
98 static inline hashval_t
99 hash (const sanopt_tree_triplet &ref)
101 inchash::hash hstate (0);
102 inchash::add_expr (ref.t1, hstate);
103 inchash::add_expr (ref.t2, hstate);
104 inchash::add_expr (ref.t3, hstate);
105 return hstate.end ();
109 equal (const sanopt_tree_triplet &ref1, const sanopt_tree_triplet &ref2)
111 return operand_equal_p (ref1.t1, ref2.t1, 0)
112 && operand_equal_p (ref1.t2, ref2.t2, 0)
113 && operand_equal_p (ref1.t3, ref2.t3, 0);
117 mark_deleted (sanopt_tree_triplet &ref)
119 ref.t1 = reinterpret_cast<tree> (1);
123 mark_empty (sanopt_tree_triplet &ref)
129 is_deleted (const sanopt_tree_triplet &ref)
131 return ref.t1 == (void *) 1;
135 is_empty (const sanopt_tree_triplet &ref)
137 return ref.t1 == NULL;
141 /* This is used to carry various hash maps and variables used
142 in sanopt_optimize_walker. */
146 /* This map maps a pointer (the first argument of UBSAN_NULL) to
147 a vector of UBSAN_NULL call statements that check this pointer. */
148 hash_map<tree, auto_vec<gimple *> > null_check_map;
150 /* This map maps a pointer (the second argument of ASAN_CHECK) to
151 a vector of ASAN_CHECK call statements that check the access. */
152 hash_map<tree_operand_hash, auto_vec<gimple *> > asan_check_map;
154 /* This map maps a tree triplet (the first, second and fourth argument
155 of UBSAN_VPTR) to a vector of UBSAN_VPTR call statements that check
156 that virtual table pointer. */
157 hash_map<sanopt_tree_triplet_hash, auto_vec<gimple *> > vptr_check_map;
159 /* Number of IFN_ASAN_CHECK statements. */
160 int asan_num_accesses;
164 /* Return true if there might be any call to free/munmap operation
165 on any path in between DOM (which should be imm(BB)) and BB. */
168 imm_dom_path_with_freeing_call (basic_block bb, basic_block dom)
170 sanopt_info *info = (sanopt_info *) bb->aux;
174 if (info->imm_dom_path_with_freeing_call_computed_p)
175 return info->imm_dom_path_with_freeing_call_p;
177 info->being_visited_p = true;
179 FOR_EACH_EDGE (e, ei, bb->preds)
181 sanopt_info *pred_info = (sanopt_info *) e->src->aux;
186 if ((pred_info->imm_dom_path_with_freeing_call_computed_p
187 && pred_info->imm_dom_path_with_freeing_call_p)
188 || (pred_info->has_freeing_call_computed_p
189 && pred_info->has_freeing_call_p))
191 info->imm_dom_path_with_freeing_call_computed_p = true;
192 info->imm_dom_path_with_freeing_call_p = true;
193 info->being_visited_p = false;
198 FOR_EACH_EDGE (e, ei, bb->preds)
200 sanopt_info *pred_info = (sanopt_info *) e->src->aux;
205 if (pred_info->has_freeing_call_computed_p)
208 gimple_stmt_iterator gsi;
209 for (gsi = gsi_start_bb (e->src); !gsi_end_p (gsi); gsi_next (&gsi))
211 gimple *stmt = gsi_stmt (gsi);
213 if (is_gimple_call (stmt) && !nonfreeing_call_p (stmt))
215 pred_info->has_freeing_call_p = true;
220 pred_info->has_freeing_call_computed_p = true;
221 if (pred_info->has_freeing_call_p)
223 info->imm_dom_path_with_freeing_call_computed_p = true;
224 info->imm_dom_path_with_freeing_call_p = true;
225 info->being_visited_p = false;
230 FOR_EACH_EDGE (e, ei, bb->preds)
236 for (src = e->src; src != dom; )
238 sanopt_info *pred_info = (sanopt_info *) src->aux;
239 if (pred_info->being_visited_p)
241 basic_block imm = get_immediate_dominator (CDI_DOMINATORS, src);
242 if (imm_dom_path_with_freeing_call (src, imm))
244 info->imm_dom_path_with_freeing_call_computed_p = true;
245 info->imm_dom_path_with_freeing_call_p = true;
246 info->being_visited_p = false;
253 info->imm_dom_path_with_freeing_call_computed_p = true;
254 info->imm_dom_path_with_freeing_call_p = false;
255 info->being_visited_p = false;
259 /* Get the first dominating check from the list of stored checks.
260 Non-dominating checks are silently dropped. */
263 maybe_get_dominating_check (auto_vec<gimple *> &v)
265 for (; !v.is_empty (); v.pop ())
267 gimple *g = v.last ();
268 sanopt_info *si = (sanopt_info *) gimple_bb (g)->aux;
270 /* At this point we shouldn't have any statements
271 that aren't dominating the current BB. */
277 /* Optimize away redundant UBSAN_NULL calls. */
280 maybe_optimize_ubsan_null_ifn (struct sanopt_ctx *ctx, gimple *stmt)
282 gcc_assert (gimple_call_num_args (stmt) == 3);
283 tree ptr = gimple_call_arg (stmt, 0);
284 tree cur_align = gimple_call_arg (stmt, 2);
285 gcc_assert (TREE_CODE (cur_align) == INTEGER_CST);
288 auto_vec<gimple *> &v = ctx->null_check_map.get_or_insert (ptr);
289 gimple *g = maybe_get_dominating_check (v);
292 /* For this PTR we don't have any UBSAN_NULL stmts recorded, so there's
293 nothing to optimize yet. */
298 /* We already have recorded a UBSAN_NULL check for this pointer. Perhaps we
299 can drop this one. But only if this check doesn't specify stricter
302 tree align = gimple_call_arg (g, 2);
303 int kind = tree_to_shwi (gimple_call_arg (g, 1));
304 /* If this is a NULL pointer check where we had segv anyway, we can
306 if (integer_zerop (align)
307 && (kind == UBSAN_LOAD_OF
308 || kind == UBSAN_STORE_OF
309 || kind == UBSAN_MEMBER_ACCESS))
311 /* Otherwise remove the check in non-recovering mode, or if the
312 stmts have same location. */
313 else if (integer_zerop (align))
314 remove = (flag_sanitize_recover & SANITIZE_NULL) == 0
315 || flag_sanitize_undefined_trap_on_error
316 || gimple_location (g) == gimple_location (stmt);
317 else if (tree_int_cst_le (cur_align, align))
318 remove = (flag_sanitize_recover & SANITIZE_ALIGNMENT) == 0
319 || flag_sanitize_undefined_trap_on_error
320 || gimple_location (g) == gimple_location (stmt);
322 if (!remove && gimple_bb (g) == gimple_bb (stmt)
323 && tree_int_cst_compare (cur_align, align) == 0)
331 /* Optimize away redundant UBSAN_VPTR calls. The second argument
332 is the value loaded from the virtual table, so rely on FRE to find out
333 when we can actually optimize. */
336 maybe_optimize_ubsan_vptr_ifn (struct sanopt_ctx *ctx, gimple *stmt)
338 gcc_assert (gimple_call_num_args (stmt) == 5);
339 sanopt_tree_triplet triplet;
340 triplet.t1 = gimple_call_arg (stmt, 0);
341 triplet.t2 = gimple_call_arg (stmt, 1);
342 triplet.t3 = gimple_call_arg (stmt, 3);
344 auto_vec<gimple *> &v = ctx->vptr_check_map.get_or_insert (triplet);
345 gimple *g = maybe_get_dominating_check (v);
348 /* For this PTR we don't have any UBSAN_VPTR stmts recorded, so there's
349 nothing to optimize yet. */
357 /* Returns TRUE if ASan check of length LEN in block BB can be removed
358 if preceded by checks in V. */
361 can_remove_asan_check (auto_vec<gimple *> &v, tree len, basic_block bb)
365 gimple *to_pop = NULL;
367 basic_block last_bb = bb;
368 bool cleanup = false;
370 FOR_EACH_VEC_ELT_REVERSE (v, i, g)
372 basic_block gbb = gimple_bb (g);
373 sanopt_info *si = (sanopt_info *) gbb->aux;
374 if (gimple_uid (g) < si->freeing_call_events)
376 /* If there is a potentially freeing call after g in gbb, we should
377 remove it from the vector, can't use in optimization. */
382 tree glen = gimple_call_arg (g, 2);
383 gcc_assert (TREE_CODE (glen) == INTEGER_CST);
385 /* If we've checked only smaller length than we want to check now,
386 we can't remove the current stmt. If g is in the same basic block,
387 we want to remove it though, as the current stmt is better. */
388 if (tree_int_cst_lt (glen, len))
398 while (last_bb != gbb)
400 /* Paths from last_bb to bb have been checked before.
401 gbb is necessarily a dominator of last_bb, but not necessarily
402 immediate dominator. */
403 if (((sanopt_info *) last_bb->aux)->freeing_call_events)
406 basic_block imm = get_immediate_dominator (CDI_DOMINATORS, last_bb);
408 if (imm_dom_path_with_freeing_call (last_bb, imm))
420 unsigned int j = 0, l = v.length ();
421 for (i = 0; i < l; i++)
423 && (gimple_uid (v[i])
425 gimple_bb (v[i])->aux)->freeing_call_events))
437 /* Optimize away redundant ASAN_CHECK calls. */
440 maybe_optimize_asan_check_ifn (struct sanopt_ctx *ctx, gimple *stmt)
442 gcc_assert (gimple_call_num_args (stmt) == 4);
443 tree ptr = gimple_call_arg (stmt, 1);
444 tree len = gimple_call_arg (stmt, 2);
445 basic_block bb = gimple_bb (stmt);
446 sanopt_info *info = (sanopt_info *) bb->aux;
448 if (TREE_CODE (len) != INTEGER_CST)
450 if (integer_zerop (len))
453 gimple_set_uid (stmt, info->freeing_call_events);
455 auto_vec<gimple *> *ptr_checks = &ctx->asan_check_map.get_or_insert (ptr);
457 tree base_addr = maybe_get_single_definition (ptr);
458 auto_vec<gimple *> *base_checks = NULL;
461 base_checks = &ctx->asan_check_map.get_or_insert (base_addr);
462 /* Original pointer might have been invalidated. */
463 ptr_checks = ctx->asan_check_map.get (ptr);
466 gimple *g = maybe_get_dominating_check (*ptr_checks);
470 /* Try with base address as well. */
471 g2 = maybe_get_dominating_check (*base_checks);
473 if (g == NULL && g2 == NULL)
475 /* For this PTR we don't have any ASAN_CHECK stmts recorded, so there's
476 nothing to optimize yet. */
477 ptr_checks->safe_push (stmt);
479 base_checks->safe_push (stmt);
486 remove = can_remove_asan_check (*ptr_checks, len, bb);
488 if (!remove && base_checks)
489 /* Try with base address as well. */
490 remove = can_remove_asan_check (*base_checks, len, bb);
494 ptr_checks->safe_push (stmt);
496 base_checks->safe_push (stmt);
502 /* Try to optimize away redundant UBSAN_NULL and ASAN_CHECK calls.
504 We walk blocks in the CFG via a depth first search of the dominator
505 tree; we push unique UBSAN_NULL or ASAN_CHECK statements into a vector
506 in the NULL_CHECK_MAP or ASAN_CHECK_MAP hash maps as we enter the
507 blocks. When leaving a block, we mark the block as visited; then
508 when checking the statements in the vector, we ignore statements that
509 are coming from already visited blocks, because these cannot dominate
510 anything anymore. CTX is a sanopt context. */
513 sanopt_optimize_walker (basic_block bb, struct sanopt_ctx *ctx)
516 gimple_stmt_iterator gsi;
517 sanopt_info *info = (sanopt_info *) bb->aux;
518 bool asan_check_optimize = (flag_sanitize & SANITIZE_ADDRESS) != 0;
520 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
522 gimple *stmt = gsi_stmt (gsi);
525 if (!is_gimple_call (stmt))
527 /* Handle asm volatile or asm with "memory" clobber
528 the same as potentionally freeing call. */
529 gasm *asm_stmt = dyn_cast <gasm *> (stmt);
531 && asan_check_optimize
532 && (gimple_asm_clobbers_memory_p (asm_stmt)
533 || gimple_asm_volatile_p (asm_stmt)))
534 info->freeing_call_events++;
539 if (asan_check_optimize && !nonfreeing_call_p (stmt))
540 info->freeing_call_events++;
542 if (gimple_call_internal_p (stmt))
543 switch (gimple_call_internal_fn (stmt))
546 remove = maybe_optimize_ubsan_null_ifn (ctx, stmt);
549 remove = maybe_optimize_ubsan_vptr_ifn (ctx, stmt);
552 if (asan_check_optimize)
553 remove = maybe_optimize_asan_check_ifn (ctx, stmt);
555 ctx->asan_num_accesses++;
563 /* Drop this check. */
564 if (dump_file && (dump_flags & TDF_DETAILS))
566 fprintf (dump_file, "Optimizing out\n ");
567 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
568 fprintf (dump_file, "\n");
570 unlink_stmt_vdef (stmt);
571 gsi_remove (&gsi, true);
577 if (asan_check_optimize)
579 info->has_freeing_call_p = info->freeing_call_events != 0;
580 info->has_freeing_call_computed_p = true;
583 for (son = first_dom_son (CDI_DOMINATORS, bb);
585 son = next_dom_son (CDI_DOMINATORS, son))
586 sanopt_optimize_walker (son, ctx);
588 /* We're leaving this BB, so mark it to that effect. */
589 info->visited_p = true;
592 /* Try to remove redundant sanitizer checks in function FUN. */
595 sanopt_optimize (function *fun)
597 struct sanopt_ctx ctx;
598 ctx.asan_num_accesses = 0;
600 /* Set up block info for each basic block. */
601 alloc_aux_for_blocks (sizeof (sanopt_info));
603 /* We're going to do a dominator walk, so ensure that we have
604 dominance information. */
605 calculate_dominance_info (CDI_DOMINATORS);
607 /* Recursively walk the dominator tree optimizing away
609 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun), &ctx);
611 free_aux_for_blocks ();
613 return ctx.asan_num_accesses;
616 /* Perform optimization of sanitize functions. */
620 const pass_data pass_data_sanopt =
622 GIMPLE_PASS, /* type */
624 OPTGROUP_NONE, /* optinfo_flags */
626 ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */
627 0, /* properties_provided */
628 0, /* properties_destroyed */
629 0, /* todo_flags_start */
630 TODO_update_ssa, /* todo_flags_finish */
633 class pass_sanopt : public gimple_opt_pass
636 pass_sanopt (gcc::context *ctxt)
637 : gimple_opt_pass (pass_data_sanopt, ctxt)
640 /* opt_pass methods: */
641 virtual bool gate (function *) { return flag_sanitize; }
642 virtual unsigned int execute (function *);
644 }; // class pass_sanopt
647 pass_sanopt::execute (function *fun)
650 int asan_num_accesses = 0;
652 /* Try to remove redundant checks. */
655 & (SANITIZE_NULL | SANITIZE_ALIGNMENT
656 | SANITIZE_ADDRESS | SANITIZE_VPTR)))
657 asan_num_accesses = sanopt_optimize (fun);
658 else if (flag_sanitize & SANITIZE_ADDRESS)
660 gimple_stmt_iterator gsi;
661 FOR_EACH_BB_FN (bb, fun)
662 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
664 gimple *stmt = gsi_stmt (gsi);
665 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)
666 && gimple_call_internal_fn (stmt) == IFN_ASAN_CHECK)
671 bool use_calls = ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD < INT_MAX
672 && asan_num_accesses >= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD;
674 FOR_EACH_BB_FN (bb, fun)
676 gimple_stmt_iterator gsi;
677 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
679 gimple *stmt = gsi_stmt (gsi);
680 bool no_next = false;
682 if (!is_gimple_call (stmt))
688 if (gimple_call_internal_p (stmt))
690 enum internal_fn ifn = gimple_call_internal_fn (stmt);
694 no_next = ubsan_expand_null_ifn (&gsi);
696 case IFN_UBSAN_BOUNDS:
697 no_next = ubsan_expand_bounds_ifn (&gsi);
699 case IFN_UBSAN_OBJECT_SIZE:
700 no_next = ubsan_expand_objsize_ifn (&gsi);
703 no_next = ubsan_expand_vptr_ifn (&gsi);
706 no_next = asan_expand_check_ifn (&gsi, use_calls);
709 no_next = asan_expand_mark_ifn (&gsi);
711 case IFN_ESAN_RECORD_ACCESS:
712 no_next = esan_expand_record_access_ifn (&gsi);
717 else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
719 tree callee = gimple_call_fndecl (stmt);
720 switch (DECL_FUNCTION_CODE (callee))
722 case BUILT_IN_UNREACHABLE:
723 if (flag_sanitize & SANITIZE_UNREACHABLE
724 && !lookup_attribute ("no_sanitize_undefined",
725 DECL_ATTRIBUTES (fun->decl)))
726 no_next = ubsan_instrument_unreachable (&gsi);
733 if (dump_file && (dump_flags & TDF_DETAILS))
735 fprintf (dump_file, "Expanded\n ");
736 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
737 fprintf (dump_file, "\n");
750 make_pass_sanopt (gcc::context *ctxt)
752 return new pass_sanopt (ctxt);