1 /* Optimize and expand sanitizer functions.
2 Copyright (C) 2014 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"
25 #include "hash-table.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"
42 #include "gimple-iterator.h"
44 #include "plugin-api.h"
45 #include "tree-pass.h"
47 #include "gimple-pretty-print.h"
49 #include "langhooks.h"
54 /* This is used to carry information about basic blocks. It is
55 attached to the AUX field of the standard CFG block. */
59 /* True if this BB has been visited. */
63 /* This is used to carry various hash maps and variables used
64 in sanopt_optimize_walker. */
68 /* This map maps a pointer (the first argument of UBSAN_NULL) to
69 a vector of UBSAN_NULL call statements that check this pointer. */
70 hash_map
<tree
, auto_vec
<gimple
> > null_check_map
;
72 /* Number of IFN_ASAN_CHECK statements. */
73 int asan_num_accesses
;
77 /* Try to optimize away redundant UBSAN_NULL checks.
79 We walk blocks in the CFG via a depth first search of the dominator
80 tree; we push unique UBSAN_NULL statements into a vector in the
81 NULL_CHECK_MAP as we enter the blocks. When leaving a block, we
82 mark the block as visited; then when checking the statements in the
83 vector, we ignore statements that are coming from already visited
84 blocks, because these cannot dominate anything anymore.
85 CTX is a sanopt context. */
88 sanopt_optimize_walker (basic_block bb
, struct sanopt_ctx
*ctx
)
91 gimple_stmt_iterator gsi
;
93 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
95 gimple stmt
= gsi_stmt (gsi
);
98 if (is_gimple_call (stmt
)
99 && gimple_call_internal_p (stmt
))
100 switch (gimple_call_internal_fn (stmt
))
104 gcc_assert (gimple_call_num_args (stmt
) == 3);
105 tree ptr
= gimple_call_arg (stmt
, 0);
106 tree cur_align
= gimple_call_arg (stmt
, 2);
107 gcc_assert (TREE_CODE (cur_align
) == INTEGER_CST
);
109 auto_vec
<gimple
> &v
= ctx
->null_check_map
.get_or_insert (ptr
);
111 /* For this PTR we don't have any UBSAN_NULL stmts
112 recorded, so there's nothing to optimize yet. */
116 /* We already have recorded a UBSAN_NULL check
117 for this pointer. Perhaps we can drop this one.
118 But only if this check doesn't specify stricter
123 while (!v
.is_empty ())
125 gimple g
= v
.last ();
126 /* Remove statements for BBs that have been
127 already processed. */
128 sanopt_info
*si
= (sanopt_info
*) gimple_bb (g
)->aux
;
133 /* At this point we shouldn't have any statements
134 that aren't dominating the current BB. */
135 tree align
= gimple_call_arg (g
, 2);
136 remove
= tree_int_cst_le (cur_align
, align
);
143 /* Drop this check. */
144 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
146 fprintf (dump_file
, "Optimizing out\n ");
147 print_gimple_stmt (dump_file
, stmt
, 0,
149 fprintf (dump_file
, "\n");
151 gsi_remove (&gsi
, true);
153 else if (v
.length () < 30)
158 ctx
->asan_num_accesses
++;
164 /* If we were able to remove the current statement, gsi_remove
165 already pointed us to the next statement. */
170 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
172 son
= next_dom_son (CDI_DOMINATORS
, son
))
173 sanopt_optimize_walker (son
, ctx
);
175 /* We're leaving this BB, so mark it to that effect. */
176 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
177 info
->visited_p
= true;
180 /* Try to remove redundant sanitizer checks in function FUN. */
183 sanopt_optimize (function
*fun
)
185 struct sanopt_ctx ctx
;
186 ctx
.asan_num_accesses
= 0;
188 /* Set up block info for each basic block. */
189 alloc_aux_for_blocks (sizeof (sanopt_info
));
191 /* We're going to do a dominator walk, so ensure that we have
192 dominance information. */
193 calculate_dominance_info (CDI_DOMINATORS
);
195 /* Recursively walk the dominator tree optimizing away
197 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun
), &ctx
);
199 free_aux_for_blocks ();
201 return ctx
.asan_num_accesses
;
204 /* Perform optimization of sanitize functions. */
208 const pass_data pass_data_sanopt
=
210 GIMPLE_PASS
, /* type */
212 OPTGROUP_NONE
, /* optinfo_flags */
214 ( PROP_ssa
| PROP_cfg
| PROP_gimple_leh
), /* properties_required */
215 0, /* properties_provided */
216 0, /* properties_destroyed */
217 0, /* todo_flags_start */
218 TODO_update_ssa
, /* todo_flags_finish */
221 class pass_sanopt
: public gimple_opt_pass
224 pass_sanopt (gcc::context
*ctxt
)
225 : gimple_opt_pass (pass_data_sanopt
, ctxt
)
228 /* opt_pass methods: */
229 virtual bool gate (function
*) { return flag_sanitize
; }
230 virtual unsigned int execute (function
*);
232 }; // class pass_sanopt
235 pass_sanopt::execute (function
*fun
)
238 int asan_num_accesses
= 0;
240 /* Try to remove redundant checks. */
242 && (flag_sanitize
& (SANITIZE_NULL
| SANITIZE_ALIGNMENT
)))
243 asan_num_accesses
= sanopt_optimize (fun
);
244 else if (flag_sanitize
& SANITIZE_ADDRESS
)
246 gimple_stmt_iterator gsi
;
247 FOR_EACH_BB_FN (bb
, fun
)
248 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
250 gimple stmt
= gsi_stmt (gsi
);
251 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
)
252 && gimple_call_internal_fn (stmt
) == IFN_ASAN_CHECK
)
257 bool use_calls
= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
< INT_MAX
258 && asan_num_accesses
>= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
;
260 FOR_EACH_BB_FN (bb
, fun
)
262 gimple_stmt_iterator gsi
;
263 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
265 gimple stmt
= gsi_stmt (gsi
);
266 bool no_next
= false;
268 if (!is_gimple_call (stmt
))
274 if (gimple_call_internal_p (stmt
))
276 enum internal_fn ifn
= gimple_call_internal_fn (stmt
);
280 no_next
= ubsan_expand_null_ifn (&gsi
);
282 case IFN_UBSAN_BOUNDS
:
283 no_next
= ubsan_expand_bounds_ifn (&gsi
);
285 case IFN_UBSAN_OBJECT_SIZE
:
286 no_next
= ubsan_expand_objsize_ifn (&gsi
);
289 no_next
= asan_expand_check_ifn (&gsi
, use_calls
);
296 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
298 fprintf (dump_file
, "Expanded\n ");
299 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
300 fprintf (dump_file
, "\n");
313 make_pass_sanopt (gcc::context
*ctxt
)
315 return new pass_sanopt (ctxt
);