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
120 while (!v
.is_empty ())
122 gimple g
= v
.last ();
123 /* Remove statements for BBs that have been
124 already processed. */
125 sanopt_info
*si
= (sanopt_info
*) gimple_bb (g
)->aux
;
130 /* At this point we shouldn't have any statements
131 that aren't dominating the current BB. */
132 tree align
= gimple_call_arg (g
, 2);
133 remove
= tree_int_cst_le (cur_align
, align
);
140 /* Drop this check. */
141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
143 fprintf (dump_file
, "Optimizing out\n ");
144 print_gimple_stmt (dump_file
, stmt
, 0,
146 fprintf (dump_file
, "\n");
148 gsi_remove (&gsi
, true);
150 else if (v
.length () < 30)
155 ctx
->asan_num_accesses
++;
161 /* If we were able to remove the current statement, gsi_remove
162 already pointed us to the next statement. */
167 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
169 son
= next_dom_son (CDI_DOMINATORS
, son
))
170 sanopt_optimize_walker (son
, ctx
);
172 /* We're leaving this BB, so mark it to that effect. */
173 sanopt_info
*info
= (sanopt_info
*) bb
->aux
;
174 info
->visited_p
= true;
177 /* Try to remove redundant sanitizer checks in function FUN. */
180 sanopt_optimize (function
*fun
)
182 struct sanopt_ctx ctx
;
183 ctx
.asan_num_accesses
= 0;
185 /* Set up block info for each basic block. */
186 alloc_aux_for_blocks (sizeof (sanopt_info
));
188 /* We're going to do a dominator walk, so ensure that we have
189 dominance information. */
190 calculate_dominance_info (CDI_DOMINATORS
);
192 /* Recursively walk the dominator tree optimizing away
194 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun
), &ctx
);
196 free_aux_for_blocks ();
198 return ctx
.asan_num_accesses
;
201 /* Perform optimization of sanitize functions. */
205 const pass_data pass_data_sanopt
=
207 GIMPLE_PASS
, /* type */
209 OPTGROUP_NONE
, /* optinfo_flags */
211 ( PROP_ssa
| PROP_cfg
| PROP_gimple_leh
), /* properties_required */
212 0, /* properties_provided */
213 0, /* properties_destroyed */
214 0, /* todo_flags_start */
215 TODO_update_ssa
, /* todo_flags_finish */
218 class pass_sanopt
: public gimple_opt_pass
221 pass_sanopt (gcc::context
*ctxt
)
222 : gimple_opt_pass (pass_data_sanopt
, ctxt
)
225 /* opt_pass methods: */
226 virtual bool gate (function
*) { return flag_sanitize
; }
227 virtual unsigned int execute (function
*);
229 }; // class pass_sanopt
232 pass_sanopt::execute (function
*fun
)
235 int asan_num_accesses
= 0;
237 /* Try to remove redundant checks. */
239 && (flag_sanitize
& (SANITIZE_NULL
| SANITIZE_ALIGNMENT
)))
240 asan_num_accesses
= sanopt_optimize (fun
);
241 else if (flag_sanitize
& SANITIZE_ADDRESS
)
243 gimple_stmt_iterator gsi
;
244 FOR_EACH_BB_FN (bb
, fun
)
245 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
247 gimple stmt
= gsi_stmt (gsi
);
248 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
)
249 && gimple_call_internal_fn (stmt
) == IFN_ASAN_CHECK
)
254 bool use_calls
= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
< INT_MAX
255 && asan_num_accesses
>= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD
;
257 FOR_EACH_BB_FN (bb
, fun
)
259 gimple_stmt_iterator gsi
;
260 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
262 gimple stmt
= gsi_stmt (gsi
);
263 bool no_next
= false;
265 if (!is_gimple_call (stmt
))
271 if (gimple_call_internal_p (stmt
))
273 enum internal_fn ifn
= gimple_call_internal_fn (stmt
);
277 no_next
= ubsan_expand_null_ifn (&gsi
);
279 case IFN_UBSAN_BOUNDS
:
280 no_next
= ubsan_expand_bounds_ifn (&gsi
);
282 case IFN_UBSAN_OBJECT_SIZE
:
283 no_next
= ubsan_expand_objsize_ifn (&gsi
);
286 no_next
= asan_expand_check_ifn (&gsi
, use_calls
);
293 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
295 fprintf (dump_file
, "Expanded\n ");
296 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
297 fprintf (dump_file
, "\n");
310 make_pass_sanopt (gcc::context
*ctxt
)
312 return new pass_sanopt (ctxt
);