1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Frank Ch. Eigler <fche@redhat.com>
4 and Graydon Hoare <graydon@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
29 #include "basic-block.h"
32 #include "tree-inline.h"
34 #include "tree-iterator.h"
35 #include "tree-flow.h"
36 #include "tree-mudflap.h"
37 #include "tree-pass.h"
39 #include "diagnostic.h"
41 #include "langhooks.h"
46 extern void add_bb_to_loop (basic_block
, struct loop
*);
48 /* Internal function decls */
52 #define flag_mudflap_threads (flag_mudflap == 2)
55 static tree
mf_build_string (const char *string
);
56 static tree
mf_varname_tree (tree
);
57 static tree
mf_file_function_line_tree (location_t
);
59 /* Indirection-related instrumentation. */
60 static void mf_decl_cache_locals (void);
61 static void mf_decl_clear_locals (void);
62 static void mf_xform_statements (void);
63 static unsigned int execute_mudflap_function_ops (void);
65 /* Addressable variables instrumentation. */
66 static void mf_xform_decls (gimple_seq
, tree
);
67 static tree
mx_xfn_xform_decls (gimple_stmt_iterator
*, bool *,
68 struct walk_stmt_info
*);
69 static gimple_seq
mx_register_decls (tree
, gimple_seq
, location_t
);
70 static unsigned int execute_mudflap_function_decls (void);
72 /* Return true if DECL is artificial stub that shouldn't be instrumented by
73 mf. We should instrument clones of non-artificial functions. */
75 mf_artificial (const_tree decl
)
77 return DECL_ARTIFICIAL (DECL_ORIGIN (decl
));
80 /* ------------------------------------------------------------------------ */
81 /* Some generally helpful functions for mudflap instrumentation. */
83 /* Build a reference to a literal string. */
85 mf_build_string (const char *string
)
87 size_t len
= strlen (string
);
88 tree result
= mf_mark (build_string (len
+ 1, string
));
90 TREE_TYPE (result
) = build_array_type
91 (char_type_node
, build_index_type (size_int (len
)));
92 TREE_CONSTANT (result
) = 1;
93 TREE_READONLY (result
) = 1;
94 TREE_STATIC (result
) = 1;
96 result
= build1 (ADDR_EXPR
, build_pointer_type (char_type_node
), result
);
98 return mf_mark (result
);
101 /* Create a properly typed STRING_CST node that describes the given
102 declaration. It will be used as an argument for __mf_register().
103 Try to construct a helpful string, including file/function/variable
107 mf_varname_tree (tree decl
)
109 const char *buf_contents
;
115 pp_construct (&buf
, /* prefix */ NULL
, /* line-width */ 0);
116 pp_clear_output_area (&buf
);
118 /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]]. */
120 expanded_location xloc
= expand_location (DECL_SOURCE_LOCATION (decl
));
121 const char *sourcefile
;
122 unsigned sourceline
= xloc
.line
;
123 unsigned sourcecolumn
= 0;
124 sourcecolumn
= xloc
.column
;
125 sourcefile
= xloc
.file
;
126 if (sourcefile
== NULL
&& current_function_decl
!= NULL_TREE
)
127 sourcefile
= DECL_SOURCE_FILE (current_function_decl
);
128 if (sourcefile
== NULL
)
129 sourcefile
= "<unknown file>";
131 pp_string (&buf
, sourcefile
);
136 pp_decimal_int (&buf
, sourceline
);
138 if (sourcecolumn
!= 0)
141 pp_decimal_int (&buf
, sourcecolumn
);
146 if (current_function_decl
!= NULL_TREE
)
149 pp_string (&buf
, " (");
151 const char *funcname
= NULL
;
152 if (DECL_NAME (current_function_decl
))
153 funcname
= lang_hooks
.decl_printable_name (current_function_decl
, 1);
154 if (funcname
== NULL
)
155 funcname
= "anonymous fn";
157 pp_string (&buf
, funcname
);
159 pp_string (&buf
, ") ");
164 /* Add <variable-declaration>, possibly demangled. */
166 const char *declname
= NULL
;
168 if (DECL_NAME (decl
) != NULL
)
170 if (strcmp ("GNU C++", lang_hooks
.name
) == 0)
172 /* The gcc/cp decl_printable_name hook doesn't do as good a job as
173 the libiberty demangler. */
174 declname
= cplus_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)),
175 DMGL_AUTO
| DMGL_VERBOSE
);
177 if (declname
== NULL
)
178 declname
= lang_hooks
.decl_printable_name (decl
, 3);
180 if (declname
== NULL
)
181 declname
= "<unnamed variable>";
183 pp_string (&buf
, declname
);
186 /* Return the lot as a new STRING_CST. */
187 buf_contents
= ggc_strdup (pp_formatted_text (&buf
));
188 result
= mf_build_string (buf_contents
);
189 pp_clear_output_area (&buf
);
195 /* And another friend, for producing a simpler message. */
198 mf_file_function_line_tree (location_t location
)
200 expanded_location xloc
= expand_location (location
);
201 const char *file
= NULL
, *colon
, *line
, *op
, *name
, *cp
;
202 char linecolbuf
[30]; /* Enough for two decimal numbers plus a colon. */
206 /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]]. */
208 if (file
== NULL
&& current_function_decl
!= NULL_TREE
)
209 file
= DECL_SOURCE_FILE (current_function_decl
);
211 file
= "<unknown file>";
216 sprintf (linecolbuf
, "%d:%d", xloc
.line
, xloc
.column
);
218 sprintf (linecolbuf
, "%d", xloc
.line
);
225 /* Add (FUNCTION). */
226 name
= lang_hooks
.decl_printable_name (current_function_decl
, 1);
235 string
= concat (file
, colon
, line
, op
, name
, cp
, NULL
);
236 result
= mf_build_string (string
);
243 /* global tree nodes */
245 /* Global tree objects for global variables and functions exported by
246 mudflap runtime library. mf_init_extern_trees must be called
247 before using these. */
249 /* uintptr_t (usually "unsigned long") */
250 static GTY (()) tree mf_uintptr_type
;
252 /* struct __mf_cache { uintptr_t low; uintptr_t high; }; */
253 static GTY (()) tree mf_cache_struct_type
;
255 /* struct __mf_cache * const */
256 static GTY (()) tree mf_cache_structptr_type
;
258 /* extern struct __mf_cache __mf_lookup_cache []; */
259 static GTY (()) tree mf_cache_array_decl
;
261 /* extern unsigned char __mf_lc_shift; */
262 static GTY (()) tree mf_cache_shift_decl
;
264 /* extern uintptr_t __mf_lc_mask; */
265 static GTY (()) tree mf_cache_mask_decl
;
267 /* Their function-scope local shadows, used in single-threaded mode only. */
269 /* auto const unsigned char __mf_lc_shift_l; */
270 static GTY (()) tree mf_cache_shift_decl_l
;
272 /* auto const uintptr_t __mf_lc_mask_l; */
273 static GTY (()) tree mf_cache_mask_decl_l
;
275 /* extern void __mf_check (void *ptr, size_t sz, int type, const char *); */
276 static GTY (()) tree mf_check_fndecl
;
278 /* extern void __mf_register (void *ptr, size_t sz, int type, const char *); */
279 static GTY (()) tree mf_register_fndecl
;
281 /* extern void __mf_unregister (void *ptr, size_t sz, int type); */
282 static GTY (()) tree mf_unregister_fndecl
;
284 /* extern void __mf_init (); */
285 static GTY (()) tree mf_init_fndecl
;
287 /* extern int __mf_set_options (const char*); */
288 static GTY (()) tree mf_set_options_fndecl
;
291 /* Helper for mudflap_init: construct a decl with the given category,
292 name, and type, mark it an external reference, and pushdecl it. */
294 mf_make_builtin (enum tree_code category
, const char *name
, tree type
)
296 tree decl
= mf_mark (build_decl (UNKNOWN_LOCATION
,
297 category
, get_identifier (name
), type
));
298 TREE_PUBLIC (decl
) = 1;
299 DECL_EXTERNAL (decl
) = 1;
300 lang_hooks
.decls
.pushdecl (decl
);
301 /* The decl was declared by the compiler. */
302 DECL_ARTIFICIAL (decl
) = 1;
303 /* And we don't want debug info for it. */
304 DECL_IGNORED_P (decl
) = 1;
308 /* Helper for mudflap_init: construct a tree corresponding to the type
309 struct __mf_cache { uintptr_t low; uintptr_t high; };
310 where uintptr_t is the FIELD_TYPE argument. */
312 mf_make_mf_cache_struct_type (tree field_type
)
314 /* There is, abominably, no language-independent way to construct a
315 RECORD_TYPE. So we have to call the basic type construction
316 primitives by hand. */
317 tree fieldlo
= build_decl (UNKNOWN_LOCATION
,
318 FIELD_DECL
, get_identifier ("low"), field_type
);
319 tree fieldhi
= build_decl (UNKNOWN_LOCATION
,
320 FIELD_DECL
, get_identifier ("high"), field_type
);
322 tree struct_type
= make_node (RECORD_TYPE
);
323 DECL_CONTEXT (fieldlo
) = struct_type
;
324 DECL_CONTEXT (fieldhi
) = struct_type
;
325 DECL_CHAIN (fieldlo
) = fieldhi
;
326 TYPE_FIELDS (struct_type
) = fieldlo
;
327 TYPE_NAME (struct_type
) = get_identifier ("__mf_cache");
328 layout_type (struct_type
);
333 /* Initialize the global tree nodes that correspond to mf-runtime.h
338 static bool done
= false;
339 tree mf_const_string_type
;
340 tree mf_cache_array_type
;
341 tree mf_check_register_fntype
;
342 tree mf_unregister_fntype
;
344 tree mf_set_options_fntype
;
350 mf_uintptr_type
= lang_hooks
.types
.type_for_mode (ptr_mode
,
353 = build_pointer_type (build_qualified_type
354 (char_type_node
, TYPE_QUAL_CONST
));
356 mf_cache_struct_type
= mf_make_mf_cache_struct_type (mf_uintptr_type
);
357 mf_cache_structptr_type
= build_pointer_type (mf_cache_struct_type
);
358 mf_cache_array_type
= build_array_type (mf_cache_struct_type
, 0);
359 mf_check_register_fntype
=
360 build_function_type_list (void_type_node
, ptr_type_node
, size_type_node
,
361 integer_type_node
, mf_const_string_type
, NULL_TREE
);
362 mf_unregister_fntype
=
363 build_function_type_list (void_type_node
, ptr_type_node
, size_type_node
,
364 integer_type_node
, NULL_TREE
);
366 build_function_type_list (void_type_node
, NULL_TREE
);
367 mf_set_options_fntype
=
368 build_function_type_list (integer_type_node
, mf_const_string_type
, NULL_TREE
);
370 mf_cache_array_decl
= mf_make_builtin (VAR_DECL
, "__mf_lookup_cache",
371 mf_cache_array_type
);
372 mf_cache_shift_decl
= mf_make_builtin (VAR_DECL
, "__mf_lc_shift",
373 unsigned_char_type_node
);
374 mf_cache_mask_decl
= mf_make_builtin (VAR_DECL
, "__mf_lc_mask",
376 /* Don't process these in mudflap_enqueue_decl, should they come by
377 there for some reason. */
378 mf_mark (mf_cache_array_decl
);
379 mf_mark (mf_cache_shift_decl
);
380 mf_mark (mf_cache_mask_decl
);
381 mf_check_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_check",
382 mf_check_register_fntype
);
383 mf_register_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_register",
384 mf_check_register_fntype
);
385 mf_unregister_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_unregister",
386 mf_unregister_fntype
);
387 mf_init_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_init",
389 mf_set_options_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_set_options",
390 mf_set_options_fntype
);
394 /* ------------------------------------------------------------------------ */
395 /* This is the second part of the mudflap instrumentation. It works on
396 low-level GIMPLE using the CFG, because we want to run this pass after
397 tree optimizations have been performed, but we have to preserve the CFG
398 for expansion from trees to RTL.
399 Below is the list of transformations performed on statements in the
402 1) Memory reference transforms: Perform the mudflap indirection-related
403 tree transforms on memory references.
405 2) Mark BUILTIN_ALLOCA calls not inlineable.
410 execute_mudflap_function_ops (void)
412 struct gimplify_ctx gctx
;
414 /* Don't instrument functions such as the synthetic constructor
415 built during mudflap_finish_file. */
416 if (mf_marked_p (current_function_decl
)
417 || mf_artificial (current_function_decl
))
420 push_gimplify_context (&gctx
);
422 /* In multithreaded mode, don't cache the lookup cache parameters. */
423 if (! flag_mudflap_threads
)
424 mf_decl_cache_locals ();
426 mf_xform_statements ();
428 if (! flag_mudflap_threads
)
429 mf_decl_clear_locals ();
431 pop_gimplify_context (NULL
);
435 /* Insert a gimple_seq SEQ on all the outgoing edges out of BB. Note that
436 if BB has more than one edge, STMT will be replicated for each edge.
437 Also, abnormal edges will be ignored. */
440 insert_edge_copies_seq (gimple_seq seq
, basic_block bb
)
444 unsigned n_copies
= -1;
446 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
447 if (!(e
->flags
& EDGE_ABNORMAL
))
450 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
451 if (!(e
->flags
& EDGE_ABNORMAL
))
452 gsi_insert_seq_on_edge (e
, n_copies
-- > 0 ? gimple_seq_copy (seq
) : seq
);
455 /* Create and initialize local shadow variables for the lookup cache
456 globals. Put their decls in the *_l globals for use by
457 mf_build_check_statement_for. */
460 mf_decl_cache_locals (void)
463 gimple_seq seq
= NULL
;
465 /* Build the cache vars. */
466 mf_cache_shift_decl_l
467 = mf_mark (create_tmp_reg (TREE_TYPE (mf_cache_shift_decl
),
468 "__mf_lookup_shift_l"));
471 = mf_mark (create_tmp_reg (TREE_TYPE (mf_cache_mask_decl
),
472 "__mf_lookup_mask_l"));
474 /* Build initialization nodes for the cache vars. We just load the
475 globals into the cache variables. */
476 g
= gimple_build_assign (mf_cache_shift_decl_l
, mf_cache_shift_decl
);
477 gimple_set_location (g
, DECL_SOURCE_LOCATION (current_function_decl
));
478 gimple_seq_add_stmt (&seq
, g
);
480 g
= gimple_build_assign (mf_cache_mask_decl_l
, mf_cache_mask_decl
);
481 gimple_set_location (g
, DECL_SOURCE_LOCATION (current_function_decl
));
482 gimple_seq_add_stmt (&seq
, g
);
484 insert_edge_copies_seq (seq
, ENTRY_BLOCK_PTR
);
486 gsi_commit_edge_inserts ();
491 mf_decl_clear_locals (void)
493 /* Unset local shadows. */
494 mf_cache_shift_decl_l
= NULL_TREE
;
495 mf_cache_mask_decl_l
= NULL_TREE
;
499 mf_build_check_statement_for (tree base
, tree limit
,
500 gimple_stmt_iterator
*instr_gsi
,
501 location_t location
, tree dirflag
)
503 gimple_stmt_iterator gsi
;
504 basic_block cond_bb
, then_bb
, join_bb
;
511 gimple_seq seq
, stmts
;
513 /* We first need to split the current basic block, and start altering
514 the CFG. This allows us to insert the statements we're about to
515 construct into the right basic blocks. */
517 cond_bb
= gimple_bb (gsi_stmt (*instr_gsi
));
520 if (! gsi_end_p (gsi
))
521 e
= split_block (cond_bb
, gsi_stmt (gsi
));
523 e
= split_block_after_labels (cond_bb
);
527 /* A recap at this point: join_bb is the basic block at whose head
528 is the gimple statement for which this check expression is being
529 built. cond_bb is the (possibly new, synthetic) basic block the
530 end of which will contain the cache-lookup code, and a
531 conditional that jumps to the cache-miss code or, much more
532 likely, over to join_bb. */
534 /* Create the bb that contains the cache-miss fallback block (mf_check). */
535 then_bb
= create_empty_bb (cond_bb
);
536 make_edge (cond_bb
, then_bb
, EDGE_TRUE_VALUE
);
537 make_single_succ_edge (then_bb
, join_bb
, EDGE_FALLTHRU
);
539 /* Mark the pseudo-fallthrough edge from cond_bb to join_bb. */
540 e
= find_edge (cond_bb
, join_bb
);
541 e
->flags
= EDGE_FALSE_VALUE
;
542 e
->count
= cond_bb
->count
;
543 e
->probability
= REG_BR_PROB_BASE
;
545 /* Update dominance info. Note that bb_join's data was
546 updated by split_block. */
547 if (dom_info_available_p (CDI_DOMINATORS
))
549 set_immediate_dominator (CDI_DOMINATORS
, then_bb
, cond_bb
);
550 set_immediate_dominator (CDI_DOMINATORS
, join_bb
, cond_bb
);
553 /* Update loop info. */
555 add_bb_to_loop (then_bb
, cond_bb
->loop_father
);
557 /* Build our local variables. */
558 mf_elem
= create_tmp_reg (mf_cache_structptr_type
, "__mf_elem");
559 mf_base
= create_tmp_reg (mf_uintptr_type
, "__mf_base");
560 mf_limit
= create_tmp_reg (mf_uintptr_type
, "__mf_limit");
562 /* Build: __mf_base = (uintptr_t) <base address expression>. */
564 t
= fold_convert_loc (location
, mf_uintptr_type
,
565 unshare_expr (base
));
566 t
= force_gimple_operand (t
, &stmts
, false, NULL_TREE
);
567 gimple_seq_add_seq (&seq
, stmts
);
568 g
= gimple_build_assign (mf_base
, t
);
569 gimple_set_location (g
, location
);
570 gimple_seq_add_stmt (&seq
, g
);
572 /* Build: __mf_limit = (uintptr_t) <limit address expression>. */
573 t
= fold_convert_loc (location
, mf_uintptr_type
,
574 unshare_expr (limit
));
575 t
= force_gimple_operand (t
, &stmts
, false, NULL_TREE
);
576 gimple_seq_add_seq (&seq
, stmts
);
577 g
= gimple_build_assign (mf_limit
, t
);
578 gimple_set_location (g
, location
);
579 gimple_seq_add_stmt (&seq
, g
);
581 /* Build: __mf_elem = &__mf_lookup_cache [(__mf_base >> __mf_shift)
583 t
= build2 (RSHIFT_EXPR
, mf_uintptr_type
, mf_base
,
584 flag_mudflap_threads
? mf_cache_shift_decl
585 : mf_cache_shift_decl_l
);
586 t
= build2 (BIT_AND_EXPR
, mf_uintptr_type
, t
,
587 flag_mudflap_threads
? mf_cache_mask_decl
588 : mf_cache_mask_decl_l
);
589 t
= build4 (ARRAY_REF
,
590 TREE_TYPE (TREE_TYPE (mf_cache_array_decl
)),
591 mf_cache_array_decl
, t
, NULL_TREE
, NULL_TREE
);
592 t
= build1 (ADDR_EXPR
, mf_cache_structptr_type
, t
);
593 t
= force_gimple_operand (t
, &stmts
, false, NULL_TREE
);
594 gimple_seq_add_seq (&seq
, stmts
);
595 g
= gimple_build_assign (mf_elem
, t
);
596 gimple_set_location (g
, location
);
597 gimple_seq_add_stmt (&seq
, g
);
599 /* Quick validity check.
601 if (__mf_elem->low > __mf_base
602 || (__mf_elem_high < __mf_limit))
605 ... and only if single-threaded:
606 __mf_lookup_shift_1 = f...;
607 __mf_lookup_mask_l = ...;
610 It is expected that this body of code is rarely executed so we mark
611 the edge to the THEN clause of the conditional jump as unlikely. */
613 /* Construct t <-- '__mf_elem->low > __mf_base'. */
614 t
= build3 (COMPONENT_REF
, mf_uintptr_type
,
615 build1 (INDIRECT_REF
, mf_cache_struct_type
, mf_elem
),
616 TYPE_FIELDS (mf_cache_struct_type
), NULL_TREE
);
617 t
= build2 (GT_EXPR
, boolean_type_node
, t
, mf_base
);
619 /* Construct '__mf_elem->high < __mf_limit'.
622 1) u <-- '__mf_elem->high'
623 2) v <-- '__mf_limit'.
625 Then build 'u <-- (u < v). */
627 u
= build3 (COMPONENT_REF
, mf_uintptr_type
,
628 build1 (INDIRECT_REF
, mf_cache_struct_type
, mf_elem
),
629 DECL_CHAIN (TYPE_FIELDS (mf_cache_struct_type
)), NULL_TREE
);
633 u
= build2 (LT_EXPR
, boolean_type_node
, u
, v
);
635 /* Build the composed conditional: t <-- 't || u'. Then store the
636 result of the evaluation of 't' in a temporary variable which we
637 can use as the condition for the conditional jump. */
638 t
= build2 (TRUTH_OR_EXPR
, boolean_type_node
, t
, u
);
639 t
= force_gimple_operand (t
, &stmts
, false, NULL_TREE
);
640 gimple_seq_add_seq (&seq
, stmts
);
641 cond
= create_tmp_reg (boolean_type_node
, "__mf_unlikely_cond");
642 g
= gimple_build_assign (cond
, t
);
643 gimple_set_location (g
, location
);
644 gimple_seq_add_stmt (&seq
, g
);
646 /* Build the conditional jump. 'cond' is just a temporary so we can
647 simply build a void COND_EXPR. We do need labels in both arms though. */
648 g
= gimple_build_cond (NE_EXPR
, cond
, boolean_false_node
, NULL_TREE
,
650 gimple_set_location (g
, location
);
651 gimple_seq_add_stmt (&seq
, g
);
653 /* At this point, after so much hard work, we have only constructed
654 the conditional jump,
656 if (__mf_elem->low > __mf_base
657 || (__mf_elem_high < __mf_limit))
659 The lowered GIMPLE tree representing this code is in the statement
660 list starting at 'head'.
662 We can insert this now in the current basic block, i.e. the one that
663 the statement we're instrumenting was originally in. */
664 gsi
= gsi_last_bb (cond_bb
);
665 gsi_insert_seq_after (&gsi
, seq
, GSI_CONTINUE_LINKING
);
667 /* Now build up the body of the cache-miss handling:
672 This is the body of the conditional. */
675 /* u is a string, so it is already a gimple value. */
676 u
= mf_file_function_line_tree (location
);
677 /* NB: we pass the overall [base..limit] range to mf_check. */
678 v
= fold_build2_loc (location
, PLUS_EXPR
, mf_uintptr_type
,
679 fold_build2_loc (location
,
680 MINUS_EXPR
, mf_uintptr_type
, mf_limit
, mf_base
),
681 build_int_cst (mf_uintptr_type
, 1));
682 v
= force_gimple_operand (v
, &stmts
, true, NULL_TREE
);
683 gimple_seq_add_seq (&seq
, stmts
);
684 g
= gimple_build_call (mf_check_fndecl
, 4, mf_base
, v
, dirflag
, u
);
685 gimple_seq_add_stmt (&seq
, g
);
687 if (! flag_mudflap_threads
)
689 if (stmt_ends_bb_p (g
))
691 gsi
= gsi_start_bb (then_bb
);
692 gsi_insert_seq_after (&gsi
, seq
, GSI_CONTINUE_LINKING
);
693 e
= split_block (then_bb
, g
);
698 g
= gimple_build_assign (mf_cache_shift_decl_l
, mf_cache_shift_decl
);
699 gimple_seq_add_stmt (&seq
, g
);
701 g
= gimple_build_assign (mf_cache_mask_decl_l
, mf_cache_mask_decl
);
702 gimple_seq_add_stmt (&seq
, g
);
705 /* Insert the check code in the THEN block. */
706 gsi
= gsi_start_bb (then_bb
);
707 gsi_insert_seq_after (&gsi
, seq
, GSI_CONTINUE_LINKING
);
709 *instr_gsi
= gsi_start_bb (join_bb
);
713 /* Check whether the given decl, generally a VAR_DECL or PARM_DECL, is
714 eligible for instrumentation. For the mudflap1 pass, this implies
715 that it should be registered with the libmudflap runtime. For the
716 mudflap2 pass this means instrumenting an indirection operation with
717 respect to the object.
720 mf_decl_eligible_p (tree decl
)
722 return ((TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == PARM_DECL
)
723 /* The decl must have its address taken. In the case of
724 arrays, this flag is also set if the indexes are not
725 compile-time known valid constants. */
726 /* XXX: not sufficient: return-by-value structs! */
727 && TREE_ADDRESSABLE (decl
)
728 /* The type of the variable must be complete. */
729 && COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (decl
))
730 /* The decl hasn't been decomposed somehow. */
731 && !DECL_HAS_VALUE_EXPR_P (decl
));
736 mf_xform_derefs_1 (gimple_stmt_iterator
*iter
, tree
*tp
,
737 location_t location
, tree dirflag
)
739 tree type
, base
, limit
, addr
, size
, t
;
741 /* Don't instrument read operations. */
742 if (dirflag
== integer_zero_node
&& flag_mudflap_ignore_reads
)
745 /* Don't instrument marked nodes. */
746 if (mf_marked_p (*tp
))
750 type
= TREE_TYPE (t
);
752 if (type
== error_mark_node
)
755 size
= TYPE_SIZE_UNIT (type
);
757 switch (TREE_CODE (t
))
762 /* This is trickier than it may first appear. The reason is
763 that we are looking at expressions from the "inside out" at
764 this point. We may have a complex nested aggregate/array
765 expression (e.g. "a.b[i].c"), maybe with an indirection as
766 the leftmost operator ("p->a.b.d"), where instrumentation
767 is necessary. Or we may have an innocent "a.b.c"
768 expression that must not be instrumented. We need to
769 recurse all the way down the nesting structure to figure it
770 out: looking just at the outer node is not enough. */
772 int component_ref_only
= (TREE_CODE (t
) == COMPONENT_REF
);
773 /* If we have a bitfield component reference, we must note the
774 innermost addressable object in ELT, from which we will
775 construct the byte-addressable bounds of the bitfield. */
776 tree elt
= NULL_TREE
;
777 int bitfield_ref_p
= (TREE_CODE (t
) == COMPONENT_REF
778 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1)));
780 /* Iterate to the top of the ARRAY_REF/COMPONENT_REF
781 containment hierarchy to find the outermost VAR_DECL. */
782 var
= TREE_OPERAND (t
, 0);
785 if (bitfield_ref_p
&& elt
== NULL_TREE
786 && (TREE_CODE (var
) == ARRAY_REF
787 || TREE_CODE (var
) == COMPONENT_REF
))
790 if (TREE_CODE (var
) == ARRAY_REF
)
792 component_ref_only
= 0;
793 var
= TREE_OPERAND (var
, 0);
795 else if (TREE_CODE (var
) == COMPONENT_REF
)
796 var
= TREE_OPERAND (var
, 0);
797 else if (INDIRECT_REF_P (var
)
798 || TREE_CODE (var
) == MEM_REF
)
800 base
= TREE_OPERAND (var
, 0);
803 else if (TREE_CODE (var
) == VIEW_CONVERT_EXPR
)
805 var
= TREE_OPERAND (var
, 0);
806 if (CONSTANT_CLASS_P (var
)
807 && TREE_CODE (var
) != STRING_CST
)
812 gcc_assert (TREE_CODE (var
) == VAR_DECL
813 || TREE_CODE (var
) == PARM_DECL
814 || TREE_CODE (var
) == RESULT_DECL
815 || TREE_CODE (var
) == STRING_CST
);
816 /* Don't instrument this access if the underlying
817 variable is not "eligible". This test matches
818 those arrays that have only known-valid indexes,
819 and thus are not labeled TREE_ADDRESSABLE. */
820 if (! mf_decl_eligible_p (var
) || component_ref_only
)
824 base
= build1 (ADDR_EXPR
,
825 build_pointer_type (TREE_TYPE (var
)), var
);
831 /* Handle the case of ordinary non-indirection structure
832 accesses. These have only nested COMPONENT_REF nodes (no
833 INDIRECT_REF), but pass through the above filter loop.
834 Note that it's possible for such a struct variable to match
835 the eligible_p test because someone else might take its
838 /* We need special processing for bitfield components, because
839 their addresses cannot be taken. */
842 tree field
= TREE_OPERAND (t
, 1);
844 if (TREE_CODE (DECL_SIZE_UNIT (field
)) == INTEGER_CST
)
845 size
= DECL_SIZE_UNIT (field
);
848 elt
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (elt
)),
850 addr
= fold_convert_loc (location
, ptr_type_node
, elt
? elt
: base
);
851 addr
= fold_build_pointer_plus_loc (location
,
852 addr
, byte_position (field
));
855 addr
= build1 (ADDR_EXPR
, build_pointer_type (type
), t
);
857 limit
= fold_build2_loc (location
, MINUS_EXPR
, mf_uintptr_type
,
858 fold_build2_loc (location
, PLUS_EXPR
, mf_uintptr_type
,
859 fold_convert (mf_uintptr_type
, addr
),
866 addr
= TREE_OPERAND (t
, 0);
868 limit
= fold_build_pointer_plus_hwi_loc
869 (location
, fold_build_pointer_plus_loc (location
, base
, size
), -1);
873 if (addr_expr_of_non_mem_decl_p (TREE_OPERAND (t
, 0)))
876 addr
= fold_build_pointer_plus_loc (location
, TREE_OPERAND (t
, 0),
877 TREE_OPERAND (t
, 1));
879 limit
= fold_build_pointer_plus_hwi_loc (location
,
880 fold_build_pointer_plus_loc (location
,
885 if (addr_expr_of_non_mem_decl_p (TMR_BASE (t
)))
888 addr
= tree_mem_ref_addr (ptr_type_node
, t
);
890 limit
= fold_build_pointer_plus_hwi_loc (location
,
891 fold_build_pointer_plus_loc (location
,
895 case ARRAY_RANGE_REF
:
896 warning (OPT_Wmudflap
,
897 "mudflap checking not yet implemented for ARRAY_RANGE_REF");
901 /* ??? merge with COMPONENT_REF code above? */
905 /* If we're not dereferencing something, then the access
907 if (TREE_CODE (TREE_OPERAND (t
, 0)) != INDIRECT_REF
)
910 bpu
= bitsize_int (BITS_PER_UNIT
);
911 ofs
= fold_convert (bitsizetype
, TREE_OPERAND (t
, 2));
912 rem
= size_binop_loc (location
, TRUNC_MOD_EXPR
, ofs
, bpu
);
913 ofs
= size_binop_loc (location
, TRUNC_DIV_EXPR
, ofs
, bpu
);
915 size
= fold_convert (bitsizetype
, TREE_OPERAND (t
, 1));
916 size
= size_binop_loc (location
, PLUS_EXPR
, size
, rem
);
917 size
= size_binop_loc (location
, CEIL_DIV_EXPR
, size
, bpu
);
918 size
= fold_convert (sizetype
, size
);
920 addr
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
921 addr
= fold_convert (ptr_type_node
, addr
);
922 addr
= fold_build_pointer_plus_loc (location
, addr
, ofs
);
925 limit
= fold_build_pointer_plus_hwi_loc (location
,
926 fold_build_pointer_plus_loc (location
,
935 mf_build_check_statement_for (base
, limit
, iter
, location
, dirflag
);
938 1) Memory references.
941 mf_xform_statements (void)
943 basic_block bb
, next
;
944 gimple_stmt_iterator i
;
945 int saved_last_basic_block
= last_basic_block
;
946 enum gimple_rhs_class grhs_class
;
948 bb
= ENTRY_BLOCK_PTR
->next_bb
;
952 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
954 gimple s
= gsi_stmt (i
);
956 /* Only a few GIMPLE statements can reference memory. */
957 switch (gimple_code (s
))
960 mf_xform_derefs_1 (&i
, gimple_assign_lhs_ptr (s
),
961 gimple_location (s
), integer_one_node
);
962 mf_xform_derefs_1 (&i
, gimple_assign_rhs1_ptr (s
),
963 gimple_location (s
), integer_zero_node
);
964 grhs_class
= get_gimple_rhs_class (gimple_assign_rhs_code (s
));
965 if (grhs_class
== GIMPLE_BINARY_RHS
)
966 mf_xform_derefs_1 (&i
, gimple_assign_rhs2_ptr (s
),
967 gimple_location (s
), integer_zero_node
);
971 if (gimple_return_retval (s
) != NULL_TREE
)
973 mf_xform_derefs_1 (&i
, gimple_return_retval_ptr (s
),
985 while (bb
&& bb
->index
<= saved_last_basic_block
);
988 /* ------------------------------------------------------------------------ */
989 /* ADDR_EXPR transforms. Perform the declaration-related mudflap tree
990 transforms on the current function.
992 This is the first part of the mudflap instrumentation. It works on
993 high-level GIMPLE because after lowering, all variables are moved out
994 of their BIND_EXPR binding context, and we lose liveness information
995 for the declarations we wish to instrument. */
998 execute_mudflap_function_decls (void)
1000 struct gimplify_ctx gctx
;
1002 /* Don't instrument functions such as the synthetic constructor
1003 built during mudflap_finish_file. */
1004 if (mf_marked_p (current_function_decl
)
1005 || mf_artificial (current_function_decl
))
1008 push_gimplify_context (&gctx
);
1010 mf_xform_decls (gimple_body (current_function_decl
),
1011 DECL_ARGUMENTS (current_function_decl
));
1013 pop_gimplify_context (NULL
);
1017 /* This struct is passed between mf_xform_decls to store state needed
1018 during the traversal searching for objects that have their
1020 struct mf_xform_decls_data
1026 /* Synthesize a CALL_EXPR and a TRY_FINALLY_EXPR, for this chain of
1027 _DECLs if appropriate. Arrange to call the __mf_register function
1028 now, and the __mf_unregister function later for each. Return the
1029 gimple sequence after synthesis. */
1031 mx_register_decls (tree decl
, gimple_seq seq
, location_t location
)
1033 gimple_seq finally_stmts
= NULL
;
1034 gimple_stmt_iterator initially_stmts
= gsi_start (seq
);
1036 while (decl
!= NULL_TREE
)
1038 if (mf_decl_eligible_p (decl
)
1039 /* Not already processed. */
1040 && ! mf_marked_p (decl
)
1041 /* Automatic variable. */
1042 && ! DECL_EXTERNAL (decl
)
1043 && ! TREE_STATIC (decl
))
1045 tree size
= NULL_TREE
, variable_name
;
1046 gimple unregister_fncall
, register_fncall
;
1047 tree unregister_fncall_param
, register_fncall_param
;
1049 /* Variable-sized objects should have sizes already been
1050 gimplified when we got here. */
1051 size
= fold_convert (size_type_node
,
1052 TYPE_SIZE_UNIT (TREE_TYPE (decl
)));
1053 gcc_assert (is_gimple_val (size
));
1056 unregister_fncall_param
=
1057 mf_mark (build1 (ADDR_EXPR
,
1058 build_pointer_type (TREE_TYPE (decl
)),
1060 /* __mf_unregister (&VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK) */
1061 unregister_fncall
= gimple_build_call (mf_unregister_fndecl
, 3,
1062 unregister_fncall_param
,
1064 integer_three_node
);
1067 variable_name
= mf_varname_tree (decl
);
1068 register_fncall_param
=
1069 mf_mark (build1 (ADDR_EXPR
,
1070 build_pointer_type (TREE_TYPE (decl
)),
1072 /* __mf_register (&VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK,
1074 register_fncall
= gimple_build_call (mf_register_fndecl
, 4,
1075 register_fncall_param
,
1081 /* Accumulate the two calls. */
1082 gimple_set_location (register_fncall
, location
);
1083 gimple_set_location (unregister_fncall
, location
);
1085 /* Add the __mf_register call at the current appending point. */
1086 if (gsi_end_p (initially_stmts
))
1088 if (!mf_artificial (decl
))
1089 warning (OPT_Wmudflap
,
1090 "mudflap cannot track %qE in stub function",
1095 gsi_insert_before (&initially_stmts
, register_fncall
,
1098 /* Accumulate the FINALLY piece. */
1099 gimple_seq_add_stmt (&finally_stmts
, unregister_fncall
);
1104 decl
= DECL_CHAIN (decl
);
1107 /* Actually, (initially_stmts!=NULL) <=> (finally_stmts!=NULL) */
1108 if (finally_stmts
!= NULL
)
1110 gimple stmt
= gimple_build_try (seq
, finally_stmts
, GIMPLE_TRY_FINALLY
);
1111 gimple_seq new_seq
= NULL
;
1113 gimple_seq_add_stmt (&new_seq
, stmt
);
1121 /* Process every variable mentioned in BIND_EXPRs. */
1123 mx_xfn_xform_decls (gimple_stmt_iterator
*gsi
,
1124 bool *handled_operands_p ATTRIBUTE_UNUSED
,
1125 struct walk_stmt_info
*wi
)
1127 struct mf_xform_decls_data
*d
= (struct mf_xform_decls_data
*) wi
->info
;
1128 gimple stmt
= gsi_stmt (*gsi
);
1130 switch (gimple_code (stmt
))
1134 /* Process function parameters now (but only once). */
1137 gimple_bind_set_body (stmt
,
1138 mx_register_decls (d
->param_decls
,
1139 gimple_bind_body (stmt
),
1140 gimple_location (stmt
)));
1141 d
->param_decls
= NULL_TREE
;
1144 gimple_bind_set_body (stmt
,
1145 mx_register_decls (gimple_bind_vars (stmt
),
1146 gimple_bind_body (stmt
),
1147 gimple_location (stmt
)));
1158 /* Perform the object lifetime tracking mudflap transform on the given function
1159 tree. The tree is mutated in place, with possibly copied subtree nodes.
1161 For every auto variable declared, if its address is ever taken
1162 within the function, then supply its lifetime to the mudflap
1163 runtime with the __mf_register and __mf_unregister calls.
1167 mf_xform_decls (gimple_seq fnbody
, tree fnparams
)
1169 struct mf_xform_decls_data d
;
1170 struct walk_stmt_info wi
;
1171 struct pointer_set_t
*pset
= pointer_set_create ();
1173 d
.param_decls
= fnparams
;
1174 memset (&wi
, 0, sizeof (wi
));
1175 wi
.info
= (void*) &d
;
1177 walk_gimple_seq (fnbody
, mx_xfn_xform_decls
, NULL
, &wi
);
1178 pointer_set_destroy (pset
);
1182 /* ------------------------------------------------------------------------ */
1183 /* Externally visible mudflap functions. */
1186 /* Mark and return the given tree node to prevent further mudflap
1188 static GTY ((param_is (union tree_node
))) htab_t marked_trees
= NULL
;
1195 if (marked_trees
== NULL
)
1196 marked_trees
= htab_create_ggc (31, htab_hash_pointer
, htab_eq_pointer
,
1199 slot
= htab_find_slot (marked_trees
, t
, INSERT
);
1205 mf_marked_p (tree t
)
1209 if (marked_trees
== NULL
)
1212 entry
= htab_find (marked_trees
, t
);
1213 return (entry
!= NULL
);
1216 /* Remember given node as a static of some kind: global data,
1217 function-scope static, or an anonymous constant. Its assembler
1220 /* A list of globals whose incomplete declarations we encountered.
1221 Instead of emitting the __mf_register call for them here, it's
1222 delayed until program finish time. If they're still incomplete by
1223 then, warnings are emitted. */
1225 static GTY (()) vec
<tree
, va_gc
> *deferred_static_decls
;
1227 /* A list of statements for calling __mf_register() at startup time. */
1228 static GTY (()) tree enqueued_call_stmt_chain
;
1231 mudflap_register_call (tree obj
, tree object_size
, tree varname
)
1233 tree arg
, call_stmt
;
1235 arg
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (obj
)), obj
);
1236 arg
= fold_convert (ptr_type_node
, arg
);
1238 call_stmt
= build_call_expr (mf_register_fndecl
, 4,
1240 fold_convert (size_type_node
, object_size
),
1241 /* __MF_TYPE_STATIC */
1242 build_int_cst (integer_type_node
, 4),
1245 append_to_statement_list (call_stmt
, &enqueued_call_stmt_chain
);
1249 mudflap_enqueue_decl (tree obj
)
1251 if (mf_marked_p (obj
))
1254 /* We don't need to process variable decls that are internally
1255 generated extern. If we did, we'd end up with warnings for them
1256 during mudflap_finish_file (). That would confuse the user,
1257 since the text would refer to variables that don't show up in the
1258 user's source code. */
1259 if (DECL_P (obj
) && DECL_EXTERNAL (obj
) && mf_artificial (obj
))
1262 vec_safe_push (deferred_static_decls
, obj
);
1267 mudflap_enqueue_constant (tree obj
)
1269 tree object_size
, varname
;
1271 if (mf_marked_p (obj
))
1274 if (TREE_CODE (obj
) == STRING_CST
)
1275 object_size
= size_int (TREE_STRING_LENGTH (obj
));
1277 object_size
= size_in_bytes (TREE_TYPE (obj
));
1279 if (TREE_CODE (obj
) == STRING_CST
)
1280 varname
= mf_build_string ("string literal");
1282 varname
= mf_build_string ("constant");
1284 mudflap_register_call (obj
, object_size
, varname
);
1288 /* Emit any file-wide instrumentation. */
1290 mudflap_finish_file (void)
1292 tree ctor_statements
= NULL_TREE
;
1294 /* No need to continue when there were errors. */
1298 /* Insert a call to __mf_init. */
1300 tree call2_stmt
= build_call_expr (mf_init_fndecl
, 0);
1301 append_to_statement_list (call2_stmt
, &ctor_statements
);
1304 /* If appropriate, call __mf_set_options to pass along read-ignore mode. */
1305 if (flag_mudflap_ignore_reads
)
1307 tree arg
= mf_build_string ("-ignore-reads");
1308 tree call_stmt
= build_call_expr (mf_set_options_fndecl
, 1, arg
);
1309 append_to_statement_list (call_stmt
, &ctor_statements
);
1312 /* Process all enqueued object decls. */
1313 if (deferred_static_decls
)
1317 FOR_EACH_VEC_ELT (*deferred_static_decls
, i
, obj
)
1319 gcc_assert (DECL_P (obj
));
1321 if (mf_marked_p (obj
))
1324 /* Omit registration for static unaddressed objects. NB:
1325 Perform registration for non-static objects regardless of
1326 TREE_USED or TREE_ADDRESSABLE, because they may be used
1327 from other compilation units. */
1328 if (! TREE_PUBLIC (obj
) && ! TREE_ADDRESSABLE (obj
))
1331 /* If we're neither emitting nor referencing the symbol,
1332 don't register it. We have to register external symbols
1333 if they happen to be in other files not compiled with
1334 mudflap (say system libraries), and we must not register
1335 internal symbols that we don't emit or they'll become
1336 dangling references or force symbols to be emitted that
1338 if (!symtab_get_node (obj
))
1341 if (! COMPLETE_TYPE_P (TREE_TYPE (obj
)))
1343 warning (OPT_Wmudflap
,
1344 "mudflap cannot track unknown size extern %qE",
1349 mudflap_register_call (obj
,
1350 size_in_bytes (TREE_TYPE (obj
)),
1351 mf_varname_tree (obj
));
1354 deferred_static_decls
->truncate (0);
1357 /* Append all the enqueued registration calls. */
1358 if (enqueued_call_stmt_chain
)
1360 append_to_statement_list (enqueued_call_stmt_chain
, &ctor_statements
);
1361 enqueued_call_stmt_chain
= NULL_TREE
;
1364 cgraph_build_static_cdtor ('I', ctor_statements
,
1365 MAX_RESERVED_INIT_PRIORITY
-1);
1372 return flag_mudflap
!= 0;
1377 const pass_data pass_data_mudflap_1
=
1379 GIMPLE_PASS
, /* type */
1380 "mudflap1", /* name */
1381 OPTGROUP_NONE
, /* optinfo_flags */
1382 true, /* has_gate */
1383 true, /* has_execute */
1384 TV_NONE
, /* tv_id */
1385 PROP_gimple_any
, /* properties_required */
1386 0, /* properties_provided */
1387 0, /* properties_destroyed */
1388 0, /* todo_flags_start */
1389 0, /* todo_flags_finish */
1392 class pass_mudflap_1
: public gimple_opt_pass
1395 pass_mudflap_1(gcc::context
*ctxt
)
1396 : gimple_opt_pass(pass_data_mudflap_1
, ctxt
)
1399 /* opt_pass methods: */
1400 bool gate () { return gate_mudflap (); }
1401 unsigned int execute () { return execute_mudflap_function_decls (); }
1403 }; // class pass_mudflap_1
1408 make_pass_mudflap_1 (gcc::context
*ctxt
)
1410 return new pass_mudflap_1 (ctxt
);
1415 const pass_data pass_data_mudflap_2
=
1417 GIMPLE_PASS
, /* type */
1418 "mudflap2", /* name */
1419 OPTGROUP_NONE
, /* optinfo_flags */
1420 true, /* has_gate */
1421 true, /* has_execute */
1422 TV_NONE
, /* tv_id */
1423 ( PROP_ssa
| PROP_cfg
| PROP_gimple_leh
), /* properties_required */
1424 0, /* properties_provided */
1425 0, /* properties_destroyed */
1426 0, /* todo_flags_start */
1427 ( TODO_verify_flow
| TODO_verify_stmts
1428 | TODO_update_ssa
), /* todo_flags_finish */
1431 class pass_mudflap_2
: public gimple_opt_pass
1434 pass_mudflap_2(gcc::context
*ctxt
)
1435 : gimple_opt_pass(pass_data_mudflap_2
, ctxt
)
1438 /* opt_pass methods: */
1439 bool gate () { return gate_mudflap (); }
1440 unsigned int execute () { return execute_mudflap_function_ops (); }
1442 }; // class pass_mudflap_2
1447 make_pass_mudflap_2 (gcc::context
*ctxt
)
1449 return new pass_mudflap_2 (ctxt
);
1452 #include "gt-tree-mudflap.h"