]>
Commit | Line | Data |
---|---|---|
06cefae9 | 1 | /* Optimize and expand sanitizer functions. |
cbe34bb5 | 2 | Copyright (C) 2014-2017 Free Software Foundation, Inc. |
06cefae9 MP |
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" | |
c7131fb2 | 24 | #include "backend.h" |
06cefae9 | 25 | #include "tree.h" |
c7131fb2 | 26 | #include "gimple.h" |
2f75d6eb | 27 | #include "ssa.h" |
957060b5 | 28 | #include "tree-pass.h" |
957060b5 AM |
29 | #include "tree-ssa-operands.h" |
30 | #include "gimple-pretty-print.h" | |
c7131fb2 | 31 | #include "fold-const.h" |
06cefae9 | 32 | #include "gimple-iterator.h" |
06cefae9 | 33 | #include "asan.h" |
06cefae9 MP |
34 | #include "ubsan.h" |
35 | #include "params.h" | |
d6a818c5 | 36 | #include "tree-hash-traits.h" |
94087e88 JJ |
37 | #include "gimple-ssa.h" |
38 | #include "tree-phinodes.h" | |
39 | #include "ssa-iterators.h" | |
da402967 ML |
40 | #include "gimplify.h" |
41 | #include "gimple-iterator.h" | |
42 | #include "gimple-walk.h" | |
43 | #include "cfghooks.h" | |
44 | #include "tree-dfa.h" | |
45 | #include "tree-ssa.h" | |
06cefae9 | 46 | |
06cefae9 MP |
47 | /* This is used to carry information about basic blocks. It is |
48 | attached to the AUX field of the standard CFG block. */ | |
49 | ||
50 | struct sanopt_info | |
51 | { | |
ab9a4330 JJ |
52 | /* True if this BB might call (directly or indirectly) free/munmap |
53 | or similar operation. */ | |
54 | bool has_freeing_call_p; | |
55 | ||
56 | /* True if HAS_FREEING_CALL_P flag has been computed. */ | |
57 | bool has_freeing_call_computed_p; | |
58 | ||
59 | /* True if there is a block with HAS_FREEING_CALL_P flag set | |
60 | on any path between an immediate dominator of BB, denoted | |
61 | imm(BB), and BB. */ | |
62 | bool imm_dom_path_with_freeing_call_p; | |
63 | ||
64 | /* True if IMM_DOM_PATH_WITH_FREEING_CALL_P has been computed. */ | |
65 | bool imm_dom_path_with_freeing_call_computed_p; | |
66 | ||
67 | /* Number of possibly freeing calls encountered in this bb | |
68 | (so far). */ | |
69 | uint64_t freeing_call_events; | |
70 | ||
71 | /* True if BB is currently being visited during computation | |
72 | of IMM_DOM_PATH_WITH_FREEING_CALL_P flag. */ | |
73 | bool being_visited_p; | |
74 | ||
75 | /* True if this BB has been visited in the dominator walk. */ | |
06cefae9 MP |
76 | bool visited_p; |
77 | }; | |
78 | ||
e28f2090 YG |
79 | /* If T has a single definition of form T = T2, return T2. */ |
80 | ||
81 | static tree | |
82 | maybe_get_single_definition (tree t) | |
83 | { | |
84 | if (TREE_CODE (t) == SSA_NAME) | |
85 | { | |
355fe088 | 86 | gimple *g = SSA_NAME_DEF_STMT (t); |
e28f2090 YG |
87 | if (gimple_assign_single_p (g)) |
88 | return gimple_assign_rhs1 (g); | |
89 | } | |
90 | return NULL_TREE; | |
91 | } | |
92 | ||
35228ac7 JJ |
93 | /* Tree triplet for vptr_check_map. */ |
94 | struct sanopt_tree_triplet | |
95 | { | |
96 | tree t1, t2, t3; | |
97 | }; | |
98 | ||
99 | /* Traits class for tree triplet hash maps below. */ | |
100 | ||
9654754b | 101 | struct sanopt_tree_triplet_hash : typed_noop_remove <sanopt_tree_triplet> |
35228ac7 | 102 | { |
9654754b RS |
103 | typedef sanopt_tree_triplet value_type; |
104 | typedef sanopt_tree_triplet compare_type; | |
105 | ||
35228ac7 JJ |
106 | static inline hashval_t |
107 | hash (const sanopt_tree_triplet &ref) | |
108 | { | |
109 | inchash::hash hstate (0); | |
110 | inchash::add_expr (ref.t1, hstate); | |
111 | inchash::add_expr (ref.t2, hstate); | |
112 | inchash::add_expr (ref.t3, hstate); | |
113 | return hstate.end (); | |
114 | } | |
115 | ||
116 | static inline bool | |
9654754b | 117 | equal (const sanopt_tree_triplet &ref1, const sanopt_tree_triplet &ref2) |
35228ac7 JJ |
118 | { |
119 | return operand_equal_p (ref1.t1, ref2.t1, 0) | |
120 | && operand_equal_p (ref1.t2, ref2.t2, 0) | |
121 | && operand_equal_p (ref1.t3, ref2.t3, 0); | |
122 | } | |
123 | ||
35228ac7 | 124 | static inline void |
9654754b | 125 | mark_deleted (sanopt_tree_triplet &ref) |
35228ac7 | 126 | { |
9654754b | 127 | ref.t1 = reinterpret_cast<tree> (1); |
35228ac7 JJ |
128 | } |
129 | ||
35228ac7 | 130 | static inline void |
9654754b | 131 | mark_empty (sanopt_tree_triplet &ref) |
35228ac7 | 132 | { |
9654754b | 133 | ref.t1 = NULL; |
35228ac7 JJ |
134 | } |
135 | ||
35228ac7 | 136 | static inline bool |
9654754b | 137 | is_deleted (const sanopt_tree_triplet &ref) |
35228ac7 | 138 | { |
9654754b | 139 | return ref.t1 == (void *) 1; |
35228ac7 JJ |
140 | } |
141 | ||
35228ac7 | 142 | static inline bool |
9654754b | 143 | is_empty (const sanopt_tree_triplet &ref) |
35228ac7 | 144 | { |
9654754b | 145 | return ref.t1 == NULL; |
35228ac7 JJ |
146 | } |
147 | }; | |
148 | ||
06cefae9 MP |
149 | /* This is used to carry various hash maps and variables used |
150 | in sanopt_optimize_walker. */ | |
151 | ||
152 | struct sanopt_ctx | |
153 | { | |
154 | /* This map maps a pointer (the first argument of UBSAN_NULL) to | |
155 | a vector of UBSAN_NULL call statements that check this pointer. */ | |
355fe088 | 156 | hash_map<tree, auto_vec<gimple *> > null_check_map; |
06cefae9 | 157 | |
ab9a4330 JJ |
158 | /* This map maps a pointer (the second argument of ASAN_CHECK) to |
159 | a vector of ASAN_CHECK call statements that check the access. */ | |
355fe088 | 160 | hash_map<tree_operand_hash, auto_vec<gimple *> > asan_check_map; |
35228ac7 JJ |
161 | |
162 | /* This map maps a tree triplet (the first, second and fourth argument | |
163 | of UBSAN_VPTR) to a vector of UBSAN_VPTR call statements that check | |
164 | that virtual table pointer. */ | |
355fe088 | 165 | hash_map<sanopt_tree_triplet_hash, auto_vec<gimple *> > vptr_check_map; |
ab9a4330 | 166 | |
06cefae9 MP |
167 | /* Number of IFN_ASAN_CHECK statements. */ |
168 | int asan_num_accesses; | |
06cefae9 | 169 | |
2f75d6eb ML |
170 | /* True when the current functions constains an ASAN_MARK. */ |
171 | bool contains_asan_mark; | |
172 | }; | |
06cefae9 | 173 | |
ab9a4330 JJ |
174 | /* Return true if there might be any call to free/munmap operation |
175 | on any path in between DOM (which should be imm(BB)) and BB. */ | |
176 | ||
177 | static bool | |
178 | imm_dom_path_with_freeing_call (basic_block bb, basic_block dom) | |
179 | { | |
180 | sanopt_info *info = (sanopt_info *) bb->aux; | |
181 | edge e; | |
182 | edge_iterator ei; | |
183 | ||
184 | if (info->imm_dom_path_with_freeing_call_computed_p) | |
185 | return info->imm_dom_path_with_freeing_call_p; | |
186 | ||
187 | info->being_visited_p = true; | |
188 | ||
189 | FOR_EACH_EDGE (e, ei, bb->preds) | |
190 | { | |
191 | sanopt_info *pred_info = (sanopt_info *) e->src->aux; | |
192 | ||
193 | if (e->src == dom) | |
194 | continue; | |
195 | ||
196 | if ((pred_info->imm_dom_path_with_freeing_call_computed_p | |
197 | && pred_info->imm_dom_path_with_freeing_call_p) | |
198 | || (pred_info->has_freeing_call_computed_p | |
199 | && pred_info->has_freeing_call_p)) | |
200 | { | |
201 | info->imm_dom_path_with_freeing_call_computed_p = true; | |
202 | info->imm_dom_path_with_freeing_call_p = true; | |
203 | info->being_visited_p = false; | |
204 | return true; | |
205 | } | |
206 | } | |
207 | ||
208 | FOR_EACH_EDGE (e, ei, bb->preds) | |
209 | { | |
210 | sanopt_info *pred_info = (sanopt_info *) e->src->aux; | |
211 | ||
212 | if (e->src == dom) | |
213 | continue; | |
214 | ||
215 | if (pred_info->has_freeing_call_computed_p) | |
216 | continue; | |
217 | ||
218 | gimple_stmt_iterator gsi; | |
219 | for (gsi = gsi_start_bb (e->src); !gsi_end_p (gsi); gsi_next (&gsi)) | |
220 | { | |
355fe088 | 221 | gimple *stmt = gsi_stmt (gsi); |
8e104951 | 222 | gasm *asm_stmt; |
ab9a4330 | 223 | |
8e104951 ML |
224 | if ((is_gimple_call (stmt) && !nonfreeing_call_p (stmt)) |
225 | || ((asm_stmt = dyn_cast <gasm *> (stmt)) | |
226 | && (gimple_asm_clobbers_memory_p (asm_stmt) | |
227 | || gimple_asm_volatile_p (asm_stmt)))) | |
ab9a4330 JJ |
228 | { |
229 | pred_info->has_freeing_call_p = true; | |
230 | break; | |
231 | } | |
232 | } | |
233 | ||
234 | pred_info->has_freeing_call_computed_p = true; | |
235 | if (pred_info->has_freeing_call_p) | |
236 | { | |
237 | info->imm_dom_path_with_freeing_call_computed_p = true; | |
238 | info->imm_dom_path_with_freeing_call_p = true; | |
239 | info->being_visited_p = false; | |
240 | return true; | |
241 | } | |
242 | } | |
243 | ||
244 | FOR_EACH_EDGE (e, ei, bb->preds) | |
245 | { | |
246 | if (e->src == dom) | |
247 | continue; | |
248 | ||
249 | basic_block src; | |
250 | for (src = e->src; src != dom; ) | |
251 | { | |
252 | sanopt_info *pred_info = (sanopt_info *) src->aux; | |
253 | if (pred_info->being_visited_p) | |
254 | break; | |
255 | basic_block imm = get_immediate_dominator (CDI_DOMINATORS, src); | |
256 | if (imm_dom_path_with_freeing_call (src, imm)) | |
257 | { | |
258 | info->imm_dom_path_with_freeing_call_computed_p = true; | |
259 | info->imm_dom_path_with_freeing_call_p = true; | |
260 | info->being_visited_p = false; | |
261 | return true; | |
262 | } | |
263 | src = imm; | |
264 | } | |
265 | } | |
266 | ||
267 | info->imm_dom_path_with_freeing_call_computed_p = true; | |
268 | info->imm_dom_path_with_freeing_call_p = false; | |
269 | info->being_visited_p = false; | |
270 | return false; | |
271 | } | |
272 | ||
e28f2090 YG |
273 | /* Get the first dominating check from the list of stored checks. |
274 | Non-dominating checks are silently dropped. */ | |
275 | ||
355fe088 TS |
276 | static gimple * |
277 | maybe_get_dominating_check (auto_vec<gimple *> &v) | |
e28f2090 YG |
278 | { |
279 | for (; !v.is_empty (); v.pop ()) | |
280 | { | |
355fe088 | 281 | gimple *g = v.last (); |
e28f2090 YG |
282 | sanopt_info *si = (sanopt_info *) gimple_bb (g)->aux; |
283 | if (!si->visited_p) | |
284 | /* At this point we shouldn't have any statements | |
285 | that aren't dominating the current BB. */ | |
286 | return g; | |
287 | } | |
288 | return NULL; | |
289 | } | |
290 | ||
ab9a4330 JJ |
291 | /* Optimize away redundant UBSAN_NULL calls. */ |
292 | ||
293 | static bool | |
355fe088 | 294 | maybe_optimize_ubsan_null_ifn (struct sanopt_ctx *ctx, gimple *stmt) |
ab9a4330 JJ |
295 | { |
296 | gcc_assert (gimple_call_num_args (stmt) == 3); | |
297 | tree ptr = gimple_call_arg (stmt, 0); | |
298 | tree cur_align = gimple_call_arg (stmt, 2); | |
299 | gcc_assert (TREE_CODE (cur_align) == INTEGER_CST); | |
300 | bool remove = false; | |
301 | ||
355fe088 TS |
302 | auto_vec<gimple *> &v = ctx->null_check_map.get_or_insert (ptr); |
303 | gimple *g = maybe_get_dominating_check (v); | |
e28f2090 | 304 | if (!g) |
ab9a4330 JJ |
305 | { |
306 | /* For this PTR we don't have any UBSAN_NULL stmts recorded, so there's | |
307 | nothing to optimize yet. */ | |
308 | v.safe_push (stmt); | |
309 | return false; | |
310 | } | |
311 | ||
312 | /* We already have recorded a UBSAN_NULL check for this pointer. Perhaps we | |
313 | can drop this one. But only if this check doesn't specify stricter | |
314 | alignment. */ | |
ab9a4330 | 315 | |
e28f2090 YG |
316 | tree align = gimple_call_arg (g, 2); |
317 | int kind = tree_to_shwi (gimple_call_arg (g, 1)); | |
318 | /* If this is a NULL pointer check where we had segv anyway, we can | |
319 | remove it. */ | |
320 | if (integer_zerop (align) | |
321 | && (kind == UBSAN_LOAD_OF | |
322 | || kind == UBSAN_STORE_OF | |
323 | || kind == UBSAN_MEMBER_ACCESS)) | |
324 | remove = true; | |
325 | /* Otherwise remove the check in non-recovering mode, or if the | |
326 | stmts have same location. */ | |
327 | else if (integer_zerop (align)) | |
328 | remove = (flag_sanitize_recover & SANITIZE_NULL) == 0 | |
329 | || flag_sanitize_undefined_trap_on_error | |
330 | || gimple_location (g) == gimple_location (stmt); | |
331 | else if (tree_int_cst_le (cur_align, align)) | |
332 | remove = (flag_sanitize_recover & SANITIZE_ALIGNMENT) == 0 | |
333 | || flag_sanitize_undefined_trap_on_error | |
334 | || gimple_location (g) == gimple_location (stmt); | |
335 | ||
336 | if (!remove && gimple_bb (g) == gimple_bb (stmt) | |
337 | && tree_int_cst_compare (cur_align, align) == 0) | |
338 | v.pop (); | |
ab9a4330 JJ |
339 | |
340 | if (!remove) | |
341 | v.safe_push (stmt); | |
342 | return remove; | |
343 | } | |
344 | ||
35228ac7 JJ |
345 | /* Optimize away redundant UBSAN_VPTR calls. The second argument |
346 | is the value loaded from the virtual table, so rely on FRE to find out | |
347 | when we can actually optimize. */ | |
348 | ||
349 | static bool | |
355fe088 | 350 | maybe_optimize_ubsan_vptr_ifn (struct sanopt_ctx *ctx, gimple *stmt) |
35228ac7 JJ |
351 | { |
352 | gcc_assert (gimple_call_num_args (stmt) == 5); | |
353 | sanopt_tree_triplet triplet; | |
354 | triplet.t1 = gimple_call_arg (stmt, 0); | |
355 | triplet.t2 = gimple_call_arg (stmt, 1); | |
356 | triplet.t3 = gimple_call_arg (stmt, 3); | |
357 | ||
355fe088 TS |
358 | auto_vec<gimple *> &v = ctx->vptr_check_map.get_or_insert (triplet); |
359 | gimple *g = maybe_get_dominating_check (v); | |
35228ac7 JJ |
360 | if (!g) |
361 | { | |
362 | /* For this PTR we don't have any UBSAN_VPTR stmts recorded, so there's | |
363 | nothing to optimize yet. */ | |
364 | v.safe_push (stmt); | |
365 | return false; | |
366 | } | |
367 | ||
368 | return true; | |
369 | } | |
370 | ||
e28f2090 YG |
371 | /* Returns TRUE if ASan check of length LEN in block BB can be removed |
372 | if preceded by checks in V. */ | |
ab9a4330 JJ |
373 | |
374 | static bool | |
355fe088 | 375 | can_remove_asan_check (auto_vec<gimple *> &v, tree len, basic_block bb) |
ab9a4330 | 376 | { |
ab9a4330 | 377 | unsigned int i; |
355fe088 TS |
378 | gimple *g; |
379 | gimple *to_pop = NULL; | |
ab9a4330 JJ |
380 | bool remove = false; |
381 | basic_block last_bb = bb; | |
382 | bool cleanup = false; | |
383 | ||
384 | FOR_EACH_VEC_ELT_REVERSE (v, i, g) | |
385 | { | |
386 | basic_block gbb = gimple_bb (g); | |
387 | sanopt_info *si = (sanopt_info *) gbb->aux; | |
388 | if (gimple_uid (g) < si->freeing_call_events) | |
389 | { | |
390 | /* If there is a potentially freeing call after g in gbb, we should | |
391 | remove it from the vector, can't use in optimization. */ | |
392 | cleanup = true; | |
393 | continue; | |
394 | } | |
395 | ||
ab9a4330 | 396 | tree glen = gimple_call_arg (g, 2); |
e28f2090 YG |
397 | gcc_assert (TREE_CODE (glen) == INTEGER_CST); |
398 | ||
ab9a4330 JJ |
399 | /* If we've checked only smaller length than we want to check now, |
400 | we can't remove the current stmt. If g is in the same basic block, | |
401 | we want to remove it though, as the current stmt is better. */ | |
402 | if (tree_int_cst_lt (glen, len)) | |
403 | { | |
404 | if (gbb == bb) | |
405 | { | |
406 | to_pop = g; | |
407 | cleanup = true; | |
408 | } | |
409 | continue; | |
410 | } | |
411 | ||
412 | while (last_bb != gbb) | |
413 | { | |
414 | /* Paths from last_bb to bb have been checked before. | |
415 | gbb is necessarily a dominator of last_bb, but not necessarily | |
416 | immediate dominator. */ | |
417 | if (((sanopt_info *) last_bb->aux)->freeing_call_events) | |
418 | break; | |
419 | ||
420 | basic_block imm = get_immediate_dominator (CDI_DOMINATORS, last_bb); | |
421 | gcc_assert (imm); | |
422 | if (imm_dom_path_with_freeing_call (last_bb, imm)) | |
423 | break; | |
424 | ||
425 | last_bb = imm; | |
426 | } | |
427 | if (last_bb == gbb) | |
428 | remove = true; | |
429 | break; | |
430 | } | |
431 | ||
432 | if (cleanup) | |
433 | { | |
434 | unsigned int j = 0, l = v.length (); | |
435 | for (i = 0; i < l; i++) | |
436 | if (v[i] != to_pop | |
437 | && (gimple_uid (v[i]) | |
438 | == ((sanopt_info *) | |
439 | gimple_bb (v[i])->aux)->freeing_call_events)) | |
440 | { | |
441 | if (i != j) | |
442 | v[j] = v[i]; | |
443 | j++; | |
444 | } | |
445 | v.truncate (j); | |
446 | } | |
447 | ||
e28f2090 YG |
448 | return remove; |
449 | } | |
450 | ||
451 | /* Optimize away redundant ASAN_CHECK calls. */ | |
452 | ||
453 | static bool | |
355fe088 | 454 | maybe_optimize_asan_check_ifn (struct sanopt_ctx *ctx, gimple *stmt) |
e28f2090 YG |
455 | { |
456 | gcc_assert (gimple_call_num_args (stmt) == 4); | |
457 | tree ptr = gimple_call_arg (stmt, 1); | |
458 | tree len = gimple_call_arg (stmt, 2); | |
459 | basic_block bb = gimple_bb (stmt); | |
460 | sanopt_info *info = (sanopt_info *) bb->aux; | |
461 | ||
462 | if (TREE_CODE (len) != INTEGER_CST) | |
463 | return false; | |
464 | if (integer_zerop (len)) | |
465 | return false; | |
466 | ||
467 | gimple_set_uid (stmt, info->freeing_call_events); | |
468 | ||
355fe088 | 469 | auto_vec<gimple *> *ptr_checks = &ctx->asan_check_map.get_or_insert (ptr); |
e28f2090 YG |
470 | |
471 | tree base_addr = maybe_get_single_definition (ptr); | |
355fe088 | 472 | auto_vec<gimple *> *base_checks = NULL; |
e28f2090 YG |
473 | if (base_addr) |
474 | { | |
475 | base_checks = &ctx->asan_check_map.get_or_insert (base_addr); | |
476 | /* Original pointer might have been invalidated. */ | |
477 | ptr_checks = ctx->asan_check_map.get (ptr); | |
478 | } | |
479 | ||
355fe088 TS |
480 | gimple *g = maybe_get_dominating_check (*ptr_checks); |
481 | gimple *g2 = NULL; | |
e28f2090 | 482 | |
06dd2ace | 483 | if (base_checks) |
e28f2090 | 484 | /* Try with base address as well. */ |
06dd2ace | 485 | g2 = maybe_get_dominating_check (*base_checks); |
e28f2090 | 486 | |
06dd2ace | 487 | if (g == NULL && g2 == NULL) |
e28f2090 YG |
488 | { |
489 | /* For this PTR we don't have any ASAN_CHECK stmts recorded, so there's | |
490 | nothing to optimize yet. */ | |
491 | ptr_checks->safe_push (stmt); | |
492 | if (base_checks) | |
493 | base_checks->safe_push (stmt); | |
494 | return false; | |
495 | } | |
496 | ||
497 | bool remove = false; | |
498 | ||
499 | if (ptr_checks) | |
500 | remove = can_remove_asan_check (*ptr_checks, len, bb); | |
501 | ||
502 | if (!remove && base_checks) | |
503 | /* Try with base address as well. */ | |
504 | remove = can_remove_asan_check (*base_checks, len, bb); | |
505 | ||
ab9a4330 | 506 | if (!remove) |
e28f2090 YG |
507 | { |
508 | ptr_checks->safe_push (stmt); | |
509 | if (base_checks) | |
510 | base_checks->safe_push (stmt); | |
511 | } | |
512 | ||
ab9a4330 JJ |
513 | return remove; |
514 | } | |
515 | ||
516 | /* Try to optimize away redundant UBSAN_NULL and ASAN_CHECK calls. | |
517 | ||
06cefae9 | 518 | We walk blocks in the CFG via a depth first search of the dominator |
ab9a4330 JJ |
519 | tree; we push unique UBSAN_NULL or ASAN_CHECK statements into a vector |
520 | in the NULL_CHECK_MAP or ASAN_CHECK_MAP hash maps as we enter the | |
521 | blocks. When leaving a block, we mark the block as visited; then | |
522 | when checking the statements in the vector, we ignore statements that | |
523 | are coming from already visited blocks, because these cannot dominate | |
524 | anything anymore. CTX is a sanopt context. */ | |
06cefae9 MP |
525 | |
526 | static void | |
527 | sanopt_optimize_walker (basic_block bb, struct sanopt_ctx *ctx) | |
528 | { | |
529 | basic_block son; | |
530 | gimple_stmt_iterator gsi; | |
ab9a4330 | 531 | sanopt_info *info = (sanopt_info *) bb->aux; |
e28f2090 | 532 | bool asan_check_optimize = (flag_sanitize & SANITIZE_ADDRESS) != 0; |
06cefae9 MP |
533 | |
534 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) | |
535 | { | |
355fe088 | 536 | gimple *stmt = gsi_stmt (gsi); |
06cefae9 MP |
537 | bool remove = false; |
538 | ||
ab9a4330 JJ |
539 | if (!is_gimple_call (stmt)) |
540 | { | |
541 | /* Handle asm volatile or asm with "memory" clobber | |
542 | the same as potentionally freeing call. */ | |
538dd0b7 DM |
543 | gasm *asm_stmt = dyn_cast <gasm *> (stmt); |
544 | if (asm_stmt | |
ab9a4330 | 545 | && asan_check_optimize |
538dd0b7 DM |
546 | && (gimple_asm_clobbers_memory_p (asm_stmt) |
547 | || gimple_asm_volatile_p (asm_stmt))) | |
ab9a4330 JJ |
548 | info->freeing_call_events++; |
549 | gsi_next (&gsi); | |
550 | continue; | |
551 | } | |
552 | ||
553 | if (asan_check_optimize && !nonfreeing_call_p (stmt)) | |
554 | info->freeing_call_events++; | |
555 | ||
94087e88 JJ |
556 | /* If __asan_before_dynamic_init ("module"); is followed by |
557 | __asan_after_dynamic_init (); without intervening memory loads/stores, | |
558 | there is nothing to guard, so optimize both away. */ | |
559 | if (asan_check_optimize | |
560 | && gimple_call_builtin_p (stmt, BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT)) | |
561 | { | |
562 | use_operand_p use; | |
563 | gimple *use_stmt; | |
564 | if (single_imm_use (gimple_vdef (stmt), &use, &use_stmt)) | |
565 | { | |
566 | if (is_gimple_call (use_stmt) | |
567 | && gimple_call_builtin_p (use_stmt, | |
568 | BUILT_IN_ASAN_AFTER_DYNAMIC_INIT)) | |
569 | { | |
570 | unlink_stmt_vdef (use_stmt); | |
571 | gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt); | |
572 | gsi_remove (&gsi2, true); | |
573 | remove = true; | |
574 | } | |
575 | } | |
576 | } | |
577 | ||
ab9a4330 | 578 | if (gimple_call_internal_p (stmt)) |
06cefae9 MP |
579 | switch (gimple_call_internal_fn (stmt)) |
580 | { | |
581 | case IFN_UBSAN_NULL: | |
ab9a4330 JJ |
582 | remove = maybe_optimize_ubsan_null_ifn (ctx, stmt); |
583 | break; | |
35228ac7 JJ |
584 | case IFN_UBSAN_VPTR: |
585 | remove = maybe_optimize_ubsan_vptr_ifn (ctx, stmt); | |
586 | break; | |
06cefae9 | 587 | case IFN_ASAN_CHECK: |
ab9a4330 JJ |
588 | if (asan_check_optimize) |
589 | remove = maybe_optimize_asan_check_ifn (ctx, stmt); | |
590 | if (!remove) | |
591 | ctx->asan_num_accesses++; | |
06cefae9 | 592 | break; |
2f75d6eb ML |
593 | case IFN_ASAN_MARK: |
594 | ctx->contains_asan_mark = true; | |
595 | break; | |
06cefae9 MP |
596 | default: |
597 | break; | |
598 | } | |
599 | ||
ab9a4330 JJ |
600 | if (remove) |
601 | { | |
602 | /* Drop this check. */ | |
603 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
604 | { | |
605 | fprintf (dump_file, "Optimizing out\n "); | |
606 | print_gimple_stmt (dump_file, stmt, 0, dump_flags); | |
607 | fprintf (dump_file, "\n"); | |
608 | } | |
609 | unlink_stmt_vdef (stmt); | |
610 | gsi_remove (&gsi, true); | |
611 | } | |
612 | else | |
06cefae9 MP |
613 | gsi_next (&gsi); |
614 | } | |
615 | ||
ab9a4330 JJ |
616 | if (asan_check_optimize) |
617 | { | |
618 | info->has_freeing_call_p = info->freeing_call_events != 0; | |
619 | info->has_freeing_call_computed_p = true; | |
620 | } | |
621 | ||
06cefae9 MP |
622 | for (son = first_dom_son (CDI_DOMINATORS, bb); |
623 | son; | |
624 | son = next_dom_son (CDI_DOMINATORS, son)) | |
625 | sanopt_optimize_walker (son, ctx); | |
626 | ||
627 | /* We're leaving this BB, so mark it to that effect. */ | |
06cefae9 MP |
628 | info->visited_p = true; |
629 | } | |
630 | ||
631 | /* Try to remove redundant sanitizer checks in function FUN. */ | |
632 | ||
633 | static int | |
2f75d6eb | 634 | sanopt_optimize (function *fun, bool *contains_asan_mark) |
06cefae9 MP |
635 | { |
636 | struct sanopt_ctx ctx; | |
637 | ctx.asan_num_accesses = 0; | |
2f75d6eb | 638 | ctx.contains_asan_mark = false; |
06cefae9 MP |
639 | |
640 | /* Set up block info for each basic block. */ | |
641 | alloc_aux_for_blocks (sizeof (sanopt_info)); | |
642 | ||
643 | /* We're going to do a dominator walk, so ensure that we have | |
644 | dominance information. */ | |
645 | calculate_dominance_info (CDI_DOMINATORS); | |
646 | ||
647 | /* Recursively walk the dominator tree optimizing away | |
648 | redundant checks. */ | |
649 | sanopt_optimize_walker (ENTRY_BLOCK_PTR_FOR_FN (fun), &ctx); | |
650 | ||
651 | free_aux_for_blocks (); | |
652 | ||
2f75d6eb | 653 | *contains_asan_mark = ctx.contains_asan_mark; |
06cefae9 MP |
654 | return ctx.asan_num_accesses; |
655 | } | |
656 | ||
657 | /* Perform optimization of sanitize functions. */ | |
658 | ||
17795822 TS |
659 | namespace { |
660 | ||
661 | const pass_data pass_data_sanopt = | |
06cefae9 MP |
662 | { |
663 | GIMPLE_PASS, /* type */ | |
664 | "sanopt", /* name */ | |
665 | OPTGROUP_NONE, /* optinfo_flags */ | |
666 | TV_NONE, /* tv_id */ | |
667 | ( PROP_ssa | PROP_cfg | PROP_gimple_leh ), /* properties_required */ | |
668 | 0, /* properties_provided */ | |
669 | 0, /* properties_destroyed */ | |
670 | 0, /* todo_flags_start */ | |
671 | TODO_update_ssa, /* todo_flags_finish */ | |
672 | }; | |
673 | ||
17795822 | 674 | class pass_sanopt : public gimple_opt_pass |
06cefae9 MP |
675 | { |
676 | public: | |
677 | pass_sanopt (gcc::context *ctxt) | |
678 | : gimple_opt_pass (pass_data_sanopt, ctxt) | |
679 | {} | |
680 | ||
681 | /* opt_pass methods: */ | |
682 | virtual bool gate (function *) { return flag_sanitize; } | |
683 | virtual unsigned int execute (function *); | |
684 | ||
685 | }; // class pass_sanopt | |
686 | ||
2f75d6eb ML |
687 | /* Sanitize all ASAN_MARK unpoison calls that are not reachable by a BB |
688 | that contains an ASAN_MARK poison. All these ASAN_MARK unpoison call | |
689 | can be removed as all variables are unpoisoned in a function prologue. */ | |
690 | ||
691 | static void | |
692 | sanitize_asan_mark_unpoison (void) | |
693 | { | |
694 | /* 1) Find all BBs that contain an ASAN_MARK poison call. */ | |
695 | auto_sbitmap with_poison (last_basic_block_for_fn (cfun) + 1); | |
696 | bitmap_clear (with_poison); | |
697 | basic_block bb; | |
698 | ||
699 | FOR_EACH_BB_FN (bb, cfun) | |
700 | { | |
701 | if (bitmap_bit_p (with_poison, bb->index)) | |
702 | continue; | |
703 | ||
704 | gimple_stmt_iterator gsi; | |
705 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi)) | |
706 | { | |
707 | gimple *stmt = gsi_stmt (gsi); | |
708 | if (asan_mark_p (stmt, ASAN_MARK_POISON)) | |
709 | { | |
710 | bitmap_set_bit (with_poison, bb->index); | |
711 | break; | |
712 | } | |
713 | } | |
714 | } | |
715 | ||
716 | auto_sbitmap poisoned (last_basic_block_for_fn (cfun) + 1); | |
717 | bitmap_clear (poisoned); | |
718 | auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1); | |
719 | bitmap_copy (worklist, with_poison); | |
720 | ||
721 | /* 2) Propagate the information to all reachable blocks. */ | |
722 | while (!bitmap_empty_p (worklist)) | |
723 | { | |
724 | unsigned i = bitmap_first_set_bit (worklist); | |
725 | bitmap_clear_bit (worklist, i); | |
726 | basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i); | |
727 | gcc_assert (bb); | |
728 | ||
729 | edge e; | |
730 | edge_iterator ei; | |
731 | FOR_EACH_EDGE (e, ei, bb->succs) | |
732 | if (!bitmap_bit_p (poisoned, e->dest->index)) | |
733 | { | |
734 | bitmap_set_bit (poisoned, e->dest->index); | |
735 | bitmap_set_bit (worklist, e->dest->index); | |
736 | } | |
737 | } | |
738 | ||
739 | /* 3) Iterate all BBs not included in POISONED BBs and remove unpoison | |
740 | ASAN_MARK preceding an ASAN_MARK poison (which can still happen). */ | |
741 | FOR_EACH_BB_FN (bb, cfun) | |
742 | { | |
743 | if (bitmap_bit_p (poisoned, bb->index)) | |
744 | continue; | |
745 | ||
746 | gimple_stmt_iterator gsi; | |
747 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) | |
748 | { | |
2f75d6eb ML |
749 | gimple *stmt = gsi_stmt (gsi); |
750 | if (gimple_call_internal_p (stmt, IFN_ASAN_MARK)) | |
751 | { | |
752 | if (asan_mark_p (stmt, ASAN_MARK_POISON)) | |
753 | break; | |
754 | else | |
755 | { | |
756 | if (dump_file) | |
757 | fprintf (dump_file, "Removing ASAN_MARK unpoison\n"); | |
758 | unlink_stmt_vdef (stmt); | |
759 | release_defs (stmt); | |
760 | gsi_remove (&gsi, true); | |
8ccaace8 | 761 | continue; |
2f75d6eb ML |
762 | } |
763 | } | |
764 | ||
8ccaace8 | 765 | gsi_next (&gsi); |
2f75d6eb ML |
766 | } |
767 | } | |
768 | } | |
769 | ||
770 | /* Return true when STMT is either ASAN_CHECK call or a call of a function | |
771 | that can contain an ASAN_CHECK. */ | |
772 | ||
773 | static bool | |
774 | maybe_contains_asan_check (gimple *stmt) | |
775 | { | |
776 | if (is_gimple_call (stmt)) | |
777 | { | |
778 | if (gimple_call_internal_p (stmt, IFN_ASAN_MARK)) | |
779 | return false; | |
780 | else | |
781 | return !(gimple_call_flags (stmt) & ECF_CONST); | |
782 | } | |
783 | else if (is_a<gasm *> (stmt)) | |
784 | return true; | |
785 | ||
786 | return false; | |
787 | } | |
788 | ||
789 | /* Sanitize all ASAN_MARK poison calls that are not followed by an ASAN_CHECK | |
790 | call. These calls can be removed. */ | |
791 | ||
792 | static void | |
793 | sanitize_asan_mark_poison (void) | |
794 | { | |
795 | /* 1) Find all BBs that possibly contain an ASAN_CHECK. */ | |
796 | auto_sbitmap with_check (last_basic_block_for_fn (cfun) + 1); | |
797 | bitmap_clear (with_check); | |
798 | basic_block bb; | |
799 | ||
800 | FOR_EACH_BB_FN (bb, cfun) | |
801 | { | |
802 | gimple_stmt_iterator gsi; | |
803 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi)) | |
804 | { | |
805 | gimple *stmt = gsi_stmt (gsi); | |
806 | if (maybe_contains_asan_check (stmt)) | |
807 | { | |
808 | bitmap_set_bit (with_check, bb->index); | |
809 | break; | |
810 | } | |
811 | } | |
812 | } | |
813 | ||
814 | auto_sbitmap can_reach_check (last_basic_block_for_fn (cfun) + 1); | |
815 | bitmap_clear (can_reach_check); | |
816 | auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1); | |
817 | bitmap_copy (worklist, with_check); | |
818 | ||
819 | /* 2) Propagate the information to all definitions blocks. */ | |
820 | while (!bitmap_empty_p (worklist)) | |
821 | { | |
822 | unsigned i = bitmap_first_set_bit (worklist); | |
823 | bitmap_clear_bit (worklist, i); | |
824 | basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i); | |
825 | gcc_assert (bb); | |
826 | ||
827 | edge e; | |
828 | edge_iterator ei; | |
829 | FOR_EACH_EDGE (e, ei, bb->preds) | |
830 | if (!bitmap_bit_p (can_reach_check, e->src->index)) | |
831 | { | |
832 | bitmap_set_bit (can_reach_check, e->src->index); | |
833 | bitmap_set_bit (worklist, e->src->index); | |
834 | } | |
835 | } | |
836 | ||
837 | /* 3) Iterate all BBs not included in CAN_REACH_CHECK BBs and remove poison | |
838 | ASAN_MARK not followed by a call to function having an ASAN_CHECK. */ | |
839 | FOR_EACH_BB_FN (bb, cfun) | |
840 | { | |
841 | if (bitmap_bit_p (can_reach_check, bb->index)) | |
842 | continue; | |
843 | ||
844 | gimple_stmt_iterator gsi; | |
845 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) | |
846 | { | |
2f75d6eb ML |
847 | gimple *stmt = gsi_stmt (gsi); |
848 | if (maybe_contains_asan_check (stmt)) | |
849 | break; | |
850 | else if (asan_mark_p (stmt, ASAN_MARK_POISON)) | |
851 | { | |
852 | if (dump_file) | |
853 | fprintf (dump_file, "Removing ASAN_MARK poison\n"); | |
854 | unlink_stmt_vdef (stmt); | |
855 | release_defs (stmt); | |
8ccaace8 JJ |
856 | gimple_stmt_iterator gsi2 = gsi; |
857 | gsi_prev (&gsi); | |
858 | gsi_remove (&gsi2, true); | |
859 | continue; | |
2f75d6eb ML |
860 | } |
861 | ||
8ccaace8 | 862 | gsi_prev (&gsi); |
2f75d6eb ML |
863 | } |
864 | } | |
865 | } | |
866 | ||
da402967 ML |
867 | /* Rewrite all usages of tree OP which is a PARM_DECL with a VAR_DECL |
868 | that is it's DECL_VALUE_EXPR. */ | |
869 | ||
870 | static tree | |
871 | rewrite_usage_of_param (tree *op, int *walk_subtrees, void *) | |
872 | { | |
873 | if (TREE_CODE (*op) == PARM_DECL && DECL_HAS_VALUE_EXPR_P (*op)) | |
874 | { | |
875 | *op = DECL_VALUE_EXPR (*op); | |
876 | *walk_subtrees = 0; | |
877 | } | |
878 | ||
879 | return NULL; | |
880 | } | |
881 | ||
882 | /* For a given function FUN, rewrite all addressable parameters so that | |
883 | a new automatic variable is introduced. Right after function entry | |
884 | a parameter is assigned to the variable. */ | |
885 | ||
886 | static void | |
887 | sanitize_rewrite_addressable_params (function *fun) | |
888 | { | |
889 | gimple *g; | |
890 | gimple_seq stmts = NULL; | |
891 | bool has_any_addressable_param = false; | |
892 | auto_vec<tree> clear_value_expr_list; | |
893 | ||
894 | for (tree arg = DECL_ARGUMENTS (current_function_decl); | |
895 | arg; arg = DECL_CHAIN (arg)) | |
896 | { | |
70affe6a ML |
897 | tree type = TREE_TYPE (arg); |
898 | if (TREE_ADDRESSABLE (arg) && !TREE_ADDRESSABLE (type) | |
899 | && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) | |
da402967 ML |
900 | { |
901 | TREE_ADDRESSABLE (arg) = 0; | |
902 | /* The parameter is no longer addressable. */ | |
da402967 ML |
903 | has_any_addressable_param = true; |
904 | ||
905 | /* Create a new automatic variable. */ | |
906 | tree var = build_decl (DECL_SOURCE_LOCATION (arg), | |
907 | VAR_DECL, DECL_NAME (arg), type); | |
908 | TREE_ADDRESSABLE (var) = 1; | |
7c819e8a | 909 | DECL_IGNORED_P (var) = 1; |
da402967 ML |
910 | |
911 | gimple_add_tmp_var (var); | |
912 | ||
913 | if (dump_file) | |
914 | fprintf (dump_file, | |
915 | "Rewriting parameter whose address is taken: %s\n", | |
916 | IDENTIFIER_POINTER (DECL_NAME (arg))); | |
917 | ||
918 | gcc_assert (!DECL_HAS_VALUE_EXPR_P (arg)); | |
da402967 ML |
919 | |
920 | SET_DECL_PT_UID (var, DECL_PT_UID (arg)); | |
921 | ||
922 | /* Assign value of parameter to newly created variable. */ | |
923 | if ((TREE_CODE (type) == COMPLEX_TYPE | |
924 | || TREE_CODE (type) == VECTOR_TYPE)) | |
925 | { | |
926 | /* We need to create a SSA name that will be used for the | |
927 | assignment. */ | |
928 | DECL_GIMPLE_REG_P (arg) = 1; | |
929 | tree tmp = get_or_create_ssa_default_def (cfun, arg); | |
930 | g = gimple_build_assign (var, tmp); | |
931 | gimple_set_location (g, DECL_SOURCE_LOCATION (arg)); | |
932 | gimple_seq_add_stmt (&stmts, g); | |
933 | } | |
934 | else | |
935 | { | |
936 | g = gimple_build_assign (var, arg); | |
937 | gimple_set_location (g, DECL_SOURCE_LOCATION (arg)); | |
938 | gimple_seq_add_stmt (&stmts, g); | |
939 | } | |
940 | ||
941 | if (target_for_debug_bind (arg)) | |
942 | { | |
943 | g = gimple_build_debug_bind (arg, var, NULL); | |
944 | gimple_seq_add_stmt (&stmts, g); | |
945 | clear_value_expr_list.safe_push (arg); | |
946 | } | |
6d7649f8 ML |
947 | |
948 | DECL_HAS_VALUE_EXPR_P (arg) = 1; | |
949 | SET_DECL_VALUE_EXPR (arg, var); | |
da402967 ML |
950 | } |
951 | } | |
952 | ||
953 | if (!has_any_addressable_param) | |
954 | return; | |
955 | ||
956 | /* Replace all usages of PARM_DECLs with the newly | |
957 | created variable VAR. */ | |
958 | basic_block bb; | |
959 | FOR_EACH_BB_FN (bb, fun) | |
960 | { | |
961 | gimple_stmt_iterator gsi; | |
962 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
963 | { | |
964 | gimple *stmt = gsi_stmt (gsi); | |
965 | gimple_stmt_iterator it = gsi_for_stmt (stmt); | |
966 | walk_gimple_stmt (&it, NULL, rewrite_usage_of_param, NULL); | |
967 | } | |
968 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
969 | { | |
970 | gphi *phi = dyn_cast<gphi *> (gsi_stmt (gsi)); | |
971 | for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) | |
972 | { | |
973 | hash_set<tree> visited_nodes; | |
974 | walk_tree (gimple_phi_arg_def_ptr (phi, i), | |
975 | rewrite_usage_of_param, NULL, &visited_nodes); | |
976 | } | |
977 | } | |
978 | } | |
979 | ||
980 | /* Unset value expr for parameters for which we created debug bind | |
981 | expressions. */ | |
982 | unsigned i; | |
983 | tree arg; | |
984 | FOR_EACH_VEC_ELT (clear_value_expr_list, i, arg) | |
985 | { | |
986 | DECL_HAS_VALUE_EXPR_P (arg) = 0; | |
987 | SET_DECL_VALUE_EXPR (arg, NULL_TREE); | |
988 | } | |
989 | ||
990 | /* Insert default assignments at the beginning of a function. */ | |
991 | basic_block entry_bb = ENTRY_BLOCK_PTR_FOR_FN (fun); | |
992 | entry_bb = split_edge (single_succ_edge (entry_bb)); | |
993 | ||
994 | gimple_stmt_iterator gsi = gsi_start_bb (entry_bb); | |
995 | gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT); | |
996 | } | |
997 | ||
06cefae9 MP |
998 | unsigned int |
999 | pass_sanopt::execute (function *fun) | |
1000 | { | |
1001 | basic_block bb; | |
1002 | int asan_num_accesses = 0; | |
2f75d6eb | 1003 | bool contains_asan_mark = false; |
06cefae9 MP |
1004 | |
1005 | /* Try to remove redundant checks. */ | |
1006 | if (optimize | |
ab9a4330 | 1007 | && (flag_sanitize |
35228ac7 JJ |
1008 | & (SANITIZE_NULL | SANITIZE_ALIGNMENT |
1009 | | SANITIZE_ADDRESS | SANITIZE_VPTR))) | |
2f75d6eb | 1010 | asan_num_accesses = sanopt_optimize (fun, &contains_asan_mark); |
06cefae9 MP |
1011 | else if (flag_sanitize & SANITIZE_ADDRESS) |
1012 | { | |
1013 | gimple_stmt_iterator gsi; | |
1014 | FOR_EACH_BB_FN (bb, fun) | |
1015 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1016 | { | |
355fe088 | 1017 | gimple *stmt = gsi_stmt (gsi); |
8e4284d0 | 1018 | if (gimple_call_internal_p (stmt, IFN_ASAN_CHECK)) |
06cefae9 | 1019 | ++asan_num_accesses; |
2f75d6eb ML |
1020 | else if (gimple_call_internal_p (stmt, IFN_ASAN_MARK)) |
1021 | contains_asan_mark = true; | |
06cefae9 MP |
1022 | } |
1023 | } | |
1024 | ||
2f75d6eb ML |
1025 | if (contains_asan_mark) |
1026 | { | |
1027 | sanitize_asan_mark_unpoison (); | |
1028 | sanitize_asan_mark_poison (); | |
1029 | } | |
1030 | ||
da402967 ML |
1031 | if (asan_sanitize_stack_p ()) |
1032 | sanitize_rewrite_addressable_params (fun); | |
1033 | ||
06cefae9 MP |
1034 | bool use_calls = ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD < INT_MAX |
1035 | && asan_num_accesses >= ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD; | |
1036 | ||
c7775327 ML |
1037 | hash_map<tree, tree> shadow_vars_mapping; |
1038 | bool need_commit_edge_insert = false; | |
06cefae9 MP |
1039 | FOR_EACH_BB_FN (bb, fun) |
1040 | { | |
1041 | gimple_stmt_iterator gsi; | |
1042 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) | |
1043 | { | |
355fe088 | 1044 | gimple *stmt = gsi_stmt (gsi); |
06cefae9 MP |
1045 | bool no_next = false; |
1046 | ||
1047 | if (!is_gimple_call (stmt)) | |
1048 | { | |
1049 | gsi_next (&gsi); | |
1050 | continue; | |
1051 | } | |
1052 | ||
1053 | if (gimple_call_internal_p (stmt)) | |
1054 | { | |
1055 | enum internal_fn ifn = gimple_call_internal_fn (stmt); | |
1056 | switch (ifn) | |
1057 | { | |
1058 | case IFN_UBSAN_NULL: | |
1059 | no_next = ubsan_expand_null_ifn (&gsi); | |
1060 | break; | |
1061 | case IFN_UBSAN_BOUNDS: | |
1062 | no_next = ubsan_expand_bounds_ifn (&gsi); | |
1063 | break; | |
1064 | case IFN_UBSAN_OBJECT_SIZE: | |
1065 | no_next = ubsan_expand_objsize_ifn (&gsi); | |
1066 | break; | |
c9b39a49 JJ |
1067 | case IFN_UBSAN_PTR: |
1068 | no_next = ubsan_expand_ptr_ifn (&gsi); | |
1069 | break; | |
35228ac7 JJ |
1070 | case IFN_UBSAN_VPTR: |
1071 | no_next = ubsan_expand_vptr_ifn (&gsi); | |
1072 | break; | |
06cefae9 MP |
1073 | case IFN_ASAN_CHECK: |
1074 | no_next = asan_expand_check_ifn (&gsi, use_calls); | |
1075 | break; | |
6dc4a604 ML |
1076 | case IFN_ASAN_MARK: |
1077 | no_next = asan_expand_mark_ifn (&gsi); | |
1078 | break; | |
c7775327 ML |
1079 | case IFN_ASAN_POISON: |
1080 | no_next = asan_expand_poison_ifn (&gsi, | |
1081 | &need_commit_edge_insert, | |
1082 | shadow_vars_mapping); | |
1083 | break; | |
06cefae9 MP |
1084 | default: |
1085 | break; | |
1086 | } | |
1087 | } | |
4088b790 MP |
1088 | else if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) |
1089 | { | |
1090 | tree callee = gimple_call_fndecl (stmt); | |
1091 | switch (DECL_FUNCTION_CODE (callee)) | |
1092 | { | |
1093 | case BUILT_IN_UNREACHABLE: | |
45b2222a | 1094 | if (sanitize_flags_p (SANITIZE_UNREACHABLE)) |
4088b790 MP |
1095 | no_next = ubsan_instrument_unreachable (&gsi); |
1096 | break; | |
1097 | default: | |
1098 | break; | |
1099 | } | |
1100 | } | |
06cefae9 MP |
1101 | |
1102 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1103 | { | |
1104 | fprintf (dump_file, "Expanded\n "); | |
1105 | print_gimple_stmt (dump_file, stmt, 0, dump_flags); | |
1106 | fprintf (dump_file, "\n"); | |
1107 | } | |
1108 | ||
1109 | if (!no_next) | |
1110 | gsi_next (&gsi); | |
1111 | } | |
1112 | } | |
c7775327 ML |
1113 | |
1114 | if (need_commit_edge_insert) | |
1115 | gsi_commit_edge_inserts (); | |
1116 | ||
06cefae9 MP |
1117 | return 0; |
1118 | } | |
1119 | ||
17795822 TS |
1120 | } // anon namespace |
1121 | ||
06cefae9 MP |
1122 | gimple_opt_pass * |
1123 | make_pass_sanopt (gcc::context *ctxt) | |
1124 | { | |
1125 | return new pass_sanopt (ctxt); | |
1126 | } |