Makefile.in (OBJS): Add sanopt.o.
[gcc.git] / gcc / sanopt.c
1 /* Optimize and expand sanitizer functions.
2 Copyright (C) 2014 Free Software Foundation, Inc.
3 Contributed by Marek Polacek <polacek@redhat.com>
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 for more details.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "hash-table.h"
26 #include "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "tm.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "gimplify.h"
42 #include "gimple-iterator.h"
43 #include "hash-map.h"
44 #include "plugin-api.h"
45 #include "tree-pass.h"
46 #include "asan.h"
47 #include "gimple-pretty-print.h"
48 #include "tm_p.h"
49 #include "langhooks.h"
50 #include "ubsan.h"
51 #include "params.h"
52
53
54 /* This is used to carry information about basic blocks. It is
55 attached to the AUX field of the standard CFG block. */
56
57 struct sanopt_info
58 {
59 /* True if this BB has been visited. */
60 bool visited_p;
61 };
62
63 /* This is used to carry various hash maps and variables used
64 in sanopt_optimize_walker. */
65
66 struct sanopt_ctx
67 {
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;
71
72 /* Number of IFN_ASAN_CHECK statements. */
73 int asan_num_accesses;
74 };
75
76
77 /* Try to optimize away redundant UBSAN_NULL checks.
78
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. */
86
87 static void
88 sanopt_optimize_walker (basic_block bb, struct sanopt_ctx *ctx)
89 {
90 basic_block son;
91 gimple_stmt_iterator gsi;
92
93 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
94 {
95 gimple stmt = gsi_stmt (gsi);
96 bool remove = false;
97
98 if (is_gimple_call (stmt)
99 && gimple_call_internal_p (stmt))
100 switch (gimple_call_internal_fn (stmt))
101 {
102 case IFN_UBSAN_NULL:
103 {
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);
108
109 auto_vec<gimple> &v = ctx->null_check_map.get_or_insert (ptr);
110 if (v.is_empty ())
111 /* For this PTR we don't have any UBSAN_NULL stmts
112 recorded, so there's nothing to optimize yet. */
113 v.safe_push (stmt);
114 else
115 {
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
119 alignment. */
120 int i;
121 gimple g;
122
123 while (!v.is_empty ())
124 {
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;
129 if (si->visited_p)
130 v.pop ();
131 else
132 {
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);
137 break;
138 }
139 }
140
141 if (remove)
142 {
143 /* Drop this check. */
144 if (dump_file && (dump_flags & TDF_DETAILS))
145 {
146 fprintf (dump_file, "Optimizing out\n ");
147 print_gimple_stmt (dump_file, stmt, 0,
148 dump_flags);
149 fprintf (dump_file, "\n");
150 }
151 gsi_remove (&gsi, true);
152 }
153 else if (v.length () < 30)
154 v.safe_push (stmt);
155 }
156 }
157 case IFN_ASAN_CHECK:
158 ctx->asan_num_accesses++;
159 break;
160 default:
161 break;
162 }
163
164 /* If we were able to remove the current statement, gsi_remove
165 already pointed us to the next statement. */
166 if (!remove)
167 gsi_next (&gsi);
168 }
169
170 for (son = first_dom_son (CDI_DOMINATORS, bb);
171 son;
172 son = next_dom_son (CDI_DOMINATORS, son))
173 sanopt_optimize_walker (son, ctx);
174
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;
178 }
179
180 /* Try to remove redundant sanitizer checks in function FUN. */
181
182 static int
183 sanopt_optimize (function *fun)
184 {
185 struct sanopt_ctx ctx;
186 ctx.asan_num_accesses = 0;
187
188 /* Set up block info for each basic block. */
189 alloc_aux_for_blocks (sizeof (sanopt_info));
190
191 /* We're going to do a dominator walk, so ensure that we have
192 dominance information. */
193 calculate_dominance_info (CDI_DOMINATORS);
194
195 /* Recursively walk the dominator tree optimizing away
196 redundant checks. */
197 sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun), &ctx);
198
199 free_aux_for_blocks ();
200
201 return ctx.asan_num_accesses;
202 }
203
204 /* Perform optimization of sanitize functions. */
205
206 namespace {
207
208 const pass_data pass_data_sanopt =
209 {
210 GIMPLE_PASS, /* type */
211 "sanopt", /* name */
212 OPTGROUP_NONE, /* optinfo_flags */
213 TV_NONE, /* tv_id */
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 */
219 };
220
221 class pass_sanopt : public gimple_opt_pass
222 {
223 public:
224 pass_sanopt (gcc::context *ctxt)
225 : gimple_opt_pass (pass_data_sanopt, ctxt)
226 {}
227
228 /* opt_pass methods: */
229 virtual bool gate (function *) { return flag_sanitize; }
230 virtual unsigned int execute (function *);
231
232 }; // class pass_sanopt
233
234 unsigned int
235 pass_sanopt::execute (function *fun)
236 {
237 basic_block bb;
238 int asan_num_accesses = 0;
239
240 /* Try to remove redundant checks. */
241 if (optimize
242 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT)))
243 asan_num_accesses = sanopt_optimize (fun);
244 else if (flag_sanitize & SANITIZE_ADDRESS)
245 {
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))
249 {
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)
253 ++asan_num_accesses;
254 }
255 }
256
257 bool use_calls = ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD < INT_MAX
258 && asan_num_accesses >= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD;
259
260 FOR_EACH_BB_FN (bb, fun)
261 {
262 gimple_stmt_iterator gsi;
263 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
264 {
265 gimple stmt = gsi_stmt (gsi);
266 bool no_next = false;
267
268 if (!is_gimple_call (stmt))
269 {
270 gsi_next (&gsi);
271 continue;
272 }
273
274 if (gimple_call_internal_p (stmt))
275 {
276 enum internal_fn ifn = gimple_call_internal_fn (stmt);
277 switch (ifn)
278 {
279 case IFN_UBSAN_NULL:
280 no_next = ubsan_expand_null_ifn (&gsi);
281 break;
282 case IFN_UBSAN_BOUNDS:
283 no_next = ubsan_expand_bounds_ifn (&gsi);
284 break;
285 case IFN_UBSAN_OBJECT_SIZE:
286 no_next = ubsan_expand_objsize_ifn (&gsi);
287 break;
288 case IFN_ASAN_CHECK:
289 no_next = asan_expand_check_ifn (&gsi, use_calls);
290 break;
291 default:
292 break;
293 }
294 }
295
296 if (dump_file && (dump_flags & TDF_DETAILS))
297 {
298 fprintf (dump_file, "Expanded\n ");
299 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
300 fprintf (dump_file, "\n");
301 }
302
303 if (!no_next)
304 gsi_next (&gsi);
305 }
306 }
307 return 0;
308 }
309
310 } // anon namespace
311
312 gimple_opt_pass *
313 make_pass_sanopt (gcc::context *ctxt)
314 {
315 return new pass_sanopt (ctxt);
316 }