1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Frank Ch. Eigler <fche@redhat.com>
5 and Graydon Hoare <graydon@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
28 #include "hard-reg-set.h"
32 #include "basic-block.h"
35 #include "tree-inline.h"
36 #include "tree-gimple.h"
37 #include "tree-flow.h"
38 #include "tree-mudflap.h"
39 #include "tree-dump.h"
40 #include "tree-pass.h"
42 #include "diagnostic.h"
44 #include "langhooks.h"
49 /* Internal function decls */
53 #define flag_mudflap_threads (flag_mudflap == 2)
56 static tree
mf_build_string (const char *string
);
57 static tree
mf_varname_tree (tree
);
58 static tree
mf_file_function_line_tree (location_t
);
60 /* Indirection-related instrumentation. */
61 static void mf_decl_cache_locals (void);
62 static void mf_decl_clear_locals (void);
63 static void mf_xform_derefs (void);
64 static unsigned int execute_mudflap_function_ops (void);
66 /* Addressable variables instrumentation. */
67 static void mf_xform_decls (tree
, tree
);
68 static tree
mx_xfn_xform_decls (tree
*, int *, void *);
69 static void mx_register_decls (tree
, tree
*);
70 static unsigned int execute_mudflap_function_decls (void);
73 /* ------------------------------------------------------------------------ */
74 /* Some generally helpful functions for mudflap instrumentation. */
76 /* Build a reference to a literal string. */
78 mf_build_string (const char *string
)
80 size_t len
= strlen (string
);
81 tree result
= mf_mark (build_string (len
+ 1, string
));
83 TREE_TYPE (result
) = build_array_type
84 (char_type_node
, build_index_type (build_int_cst (NULL_TREE
, len
)));
85 TREE_CONSTANT (result
) = 1;
86 TREE_INVARIANT (result
) = 1;
87 TREE_READONLY (result
) = 1;
88 TREE_STATIC (result
) = 1;
90 result
= build1 (ADDR_EXPR
, build_pointer_type (char_type_node
), result
);
92 return mf_mark (result
);
95 /* Create a properly typed STRING_CST node that describes the given
96 declaration. It will be used as an argument for __mf_register().
97 Try to construct a helpful string, including file/function/variable
101 mf_varname_tree (tree decl
)
103 static pretty_printer buf_rec
;
104 static int initialized
= 0;
105 pretty_printer
*buf
= & buf_rec
;
106 const char *buf_contents
;
113 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 #ifdef USE_MAPPED_LOCATION
125 sourcecolumn
= xloc
.column
;
127 sourcefile
= xloc
.file
;
128 if (sourcefile
== NULL
&& current_function_decl
!= NULL_TREE
)
129 sourcefile
= DECL_SOURCE_FILE (current_function_decl
);
130 if (sourcefile
== NULL
)
131 sourcefile
= "<unknown file>";
133 pp_string (buf
, sourcefile
);
137 pp_string (buf
, ":");
138 pp_decimal_int (buf
, sourceline
);
140 if (sourcecolumn
!= 0)
142 pp_string (buf
, ":");
143 pp_decimal_int (buf
, sourcecolumn
);
148 if (current_function_decl
!= NULL_TREE
)
151 pp_string (buf
, " (");
153 const char *funcname
= NULL
;
154 if (DECL_NAME (current_function_decl
))
155 funcname
= lang_hooks
.decl_printable_name (current_function_decl
, 1);
156 if (funcname
== NULL
)
157 funcname
= "anonymous fn";
159 pp_string (buf
, funcname
);
161 pp_string (buf
, ") ");
164 pp_string (buf
, " ");
166 /* Add <variable-declaration>, possibly demangled. */
168 const char *declname
= NULL
;
170 if (DECL_NAME (decl
) != NULL
)
172 if (strcmp ("GNU C++", lang_hooks
.name
) == 0)
174 /* The gcc/cp decl_printable_name hook doesn't do as good a job as
175 the libiberty demangler. */
176 declname
= cplus_demangle (IDENTIFIER_POINTER (DECL_NAME (decl
)),
177 DMGL_AUTO
| DMGL_VERBOSE
);
179 if (declname
== NULL
)
180 declname
= lang_hooks
.decl_printable_name (decl
, 3);
182 if (declname
== NULL
)
183 declname
= "<unnamed variable>";
185 pp_string (buf
, declname
);
188 /* Return the lot as a new STRING_CST. */
189 buf_contents
= pp_base_formatted_text (buf
);
190 result
= mf_build_string (buf_contents
);
191 pp_clear_output_area (buf
);
197 /* And another friend, for producing a simpler message. */
200 mf_file_function_line_tree (location_t location
)
202 expanded_location xloc
= expand_location (location
);
203 const char *file
= NULL
, *colon
, *line
, *op
, *name
, *cp
;
204 char linecolbuf
[30]; /* Enough for two decimal numbers plus a colon. */
208 /* Add FILENAME[:LINENUMBER[:COLUMNNUMBER]]. */
210 if (file
== NULL
&& current_function_decl
!= NULL_TREE
)
211 file
= DECL_SOURCE_FILE (current_function_decl
);
213 file
= "<unknown file>";
217 #ifdef USE_MAPPED_LOCATION
219 sprintf (linecolbuf
, "%d:%d", xloc
.line
, xloc
.column
);
222 sprintf (linecolbuf
, "%d", xloc
.line
);
229 /* Add (FUNCTION). */
230 name
= lang_hooks
.decl_printable_name (current_function_decl
, 1);
239 string
= concat (file
, colon
, line
, op
, name
, cp
, NULL
);
240 result
= mf_build_string (string
);
247 /* global tree nodes */
249 /* Global tree objects for global variables and functions exported by
250 mudflap runtime library. mf_init_extern_trees must be called
251 before using these. */
253 /* uintptr_t (usually "unsigned long") */
254 static GTY (()) tree mf_uintptr_type
;
256 /* struct __mf_cache { uintptr_t low; uintptr_t high; }; */
257 static GTY (()) tree mf_cache_struct_type
;
259 /* struct __mf_cache * const */
260 static GTY (()) tree mf_cache_structptr_type
;
262 /* extern struct __mf_cache __mf_lookup_cache []; */
263 static GTY (()) tree mf_cache_array_decl
;
265 /* extern unsigned char __mf_lc_shift; */
266 static GTY (()) tree mf_cache_shift_decl
;
268 /* extern uintptr_t __mf_lc_mask; */
269 static GTY (()) tree mf_cache_mask_decl
;
271 /* Their function-scope local shadows, used in single-threaded mode only. */
273 /* auto const unsigned char __mf_lc_shift_l; */
274 static GTY (()) tree mf_cache_shift_decl_l
;
276 /* auto const uintptr_t __mf_lc_mask_l; */
277 static GTY (()) tree mf_cache_mask_decl_l
;
279 /* extern void __mf_check (void *ptr, size_t sz, int type, const char *); */
280 static GTY (()) tree mf_check_fndecl
;
282 /* extern void __mf_register (void *ptr, size_t sz, int type, const char *); */
283 static GTY (()) tree mf_register_fndecl
;
285 /* extern void __mf_unregister (void *ptr, size_t sz, int type); */
286 static GTY (()) tree mf_unregister_fndecl
;
288 /* extern void __mf_init (); */
289 static GTY (()) tree mf_init_fndecl
;
291 /* extern int __mf_set_options (const char*); */
292 static GTY (()) tree mf_set_options_fndecl
;
295 /* Helper for mudflap_init: construct a decl with the given category,
296 name, and type, mark it an external reference, and pushdecl it. */
298 mf_make_builtin (enum tree_code category
, const char *name
, tree type
)
300 tree decl
= mf_mark (build_decl (category
, get_identifier (name
), type
));
301 TREE_PUBLIC (decl
) = 1;
302 DECL_EXTERNAL (decl
) = 1;
303 lang_hooks
.decls
.pushdecl (decl
);
304 /* The decl was declared by the compiler. */
305 DECL_ARTIFICIAL (decl
) = 1;
306 /* And we don't want debug info for it. */
307 DECL_IGNORED_P (decl
) = 1;
311 /* Helper for mudflap_init: construct a tree corresponding to the type
312 struct __mf_cache { uintptr_t low; uintptr_t high; };
313 where uintptr_t is the FIELD_TYPE argument. */
315 mf_make_mf_cache_struct_type (tree field_type
)
317 /* There is, abominably, no language-independent way to construct a
318 RECORD_TYPE. So we have to call the basic type construction
319 primitives by hand. */
320 tree fieldlo
= build_decl (FIELD_DECL
, get_identifier ("low"), field_type
);
321 tree fieldhi
= build_decl (FIELD_DECL
, get_identifier ("high"), field_type
);
323 tree struct_type
= make_node (RECORD_TYPE
);
324 DECL_CONTEXT (fieldlo
) = struct_type
;
325 DECL_CONTEXT (fieldhi
) = struct_type
;
326 TREE_CHAIN (fieldlo
) = fieldhi
;
327 TYPE_FIELDS (struct_type
) = fieldlo
;
328 TYPE_NAME (struct_type
) = get_identifier ("__mf_cache");
329 layout_type (struct_type
);
334 #define build_function_type_0(rtype) \
335 build_function_type (rtype, void_list_node)
336 #define build_function_type_1(rtype, arg1) \
337 build_function_type (rtype, tree_cons (0, arg1, void_list_node))
338 #define build_function_type_3(rtype, arg1, arg2, arg3) \
339 build_function_type (rtype, \
340 tree_cons (0, arg1, \
341 tree_cons (0, arg2, \
342 tree_cons (0, arg3, \
344 #define build_function_type_4(rtype, arg1, arg2, arg3, arg4) \
345 build_function_type (rtype, \
346 tree_cons (0, arg1, \
347 tree_cons (0, arg2, \
348 tree_cons (0, arg3, \
349 tree_cons (0, arg4, \
352 /* Initialize the global tree nodes that correspond to mf-runtime.h
357 static bool done
= false;
358 tree mf_const_string_type
;
359 tree mf_cache_array_type
;
360 tree mf_check_register_fntype
;
361 tree mf_unregister_fntype
;
363 tree mf_set_options_fntype
;
369 mf_uintptr_type
= lang_hooks
.types
.type_for_mode (ptr_mode
,
372 = build_pointer_type (build_qualified_type
373 (char_type_node
, TYPE_QUAL_CONST
));
375 mf_cache_struct_type
= mf_make_mf_cache_struct_type (mf_uintptr_type
);
376 mf_cache_structptr_type
= build_pointer_type (mf_cache_struct_type
);
377 mf_cache_array_type
= build_array_type (mf_cache_struct_type
, 0);
378 mf_check_register_fntype
=
379 build_function_type_4 (void_type_node
, ptr_type_node
, size_type_node
,
380 integer_type_node
, mf_const_string_type
);
381 mf_unregister_fntype
=
382 build_function_type_3 (void_type_node
, ptr_type_node
, size_type_node
,
385 build_function_type_0 (void_type_node
);
386 mf_set_options_fntype
=
387 build_function_type_1 (integer_type_node
, mf_const_string_type
);
389 mf_cache_array_decl
= mf_make_builtin (VAR_DECL
, "__mf_lookup_cache",
390 mf_cache_array_type
);
391 mf_cache_shift_decl
= mf_make_builtin (VAR_DECL
, "__mf_lc_shift",
392 unsigned_char_type_node
);
393 mf_cache_mask_decl
= mf_make_builtin (VAR_DECL
, "__mf_lc_mask",
395 /* Don't process these in mudflap_enqueue_decl, should they come by
396 there for some reason. */
397 mf_mark (mf_cache_array_decl
);
398 mf_mark (mf_cache_shift_decl
);
399 mf_mark (mf_cache_mask_decl
);
400 mf_check_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_check",
401 mf_check_register_fntype
);
402 mf_register_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_register",
403 mf_check_register_fntype
);
404 mf_unregister_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_unregister",
405 mf_unregister_fntype
);
406 mf_init_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_init",
408 mf_set_options_fndecl
= mf_make_builtin (FUNCTION_DECL
, "__mf_set_options",
409 mf_set_options_fntype
);
411 #undef build_function_type_4
412 #undef build_function_type_3
413 #undef build_function_type_1
414 #undef build_function_type_0
417 /* ------------------------------------------------------------------------ */
418 /* Memory reference transforms. Perform the mudflap indirection-related
419 tree transforms on the current function.
421 This is the second part of the mudflap instrumentation. It works on
422 low-level GIMPLE using the CFG, because we want to run this pass after
423 tree optimizations have been performed, but we have to preserve the CFG
424 for expansion from trees to RTL. */
427 execute_mudflap_function_ops (void)
429 /* Don't instrument functions such as the synthetic constructor
430 built during mudflap_finish_file. */
431 if (mf_marked_p (current_function_decl
) ||
432 DECL_ARTIFICIAL (current_function_decl
))
435 push_gimplify_context ();
437 /* In multithreaded mode, don't cache the lookup cache parameters. */
438 if (! flag_mudflap_threads
)
439 mf_decl_cache_locals ();
443 if (! flag_mudflap_threads
)
444 mf_decl_clear_locals ();
446 pop_gimplify_context (NULL
);
450 /* Create and initialize local shadow variables for the lookup cache
451 globals. Put their decls in the *_l globals for use by
452 mf_build_check_statement_for. */
455 mf_decl_cache_locals (void)
457 tree t
, shift_init_stmts
, mask_init_stmts
;
458 tree_stmt_iterator tsi
;
460 /* Build the cache vars. */
461 mf_cache_shift_decl_l
462 = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_shift_decl
),
463 "__mf_lookup_shift_l"));
466 = mf_mark (create_tmp_var (TREE_TYPE (mf_cache_mask_decl
),
467 "__mf_lookup_mask_l"));
469 /* Build initialization nodes for the cache vars. We just load the
470 globals into the cache variables. */
471 t
= build_gimple_modify_stmt (mf_cache_shift_decl_l
, mf_cache_shift_decl
);
472 SET_EXPR_LOCATION (t
, DECL_SOURCE_LOCATION (current_function_decl
));
473 gimplify_to_stmt_list (&t
);
474 shift_init_stmts
= t
;
476 t
= build_gimple_modify_stmt (mf_cache_mask_decl_l
, mf_cache_mask_decl
);
477 SET_EXPR_LOCATION (t
, DECL_SOURCE_LOCATION (current_function_decl
));
478 gimplify_to_stmt_list (&t
);
481 /* Anticipating multiple entry points, we insert the cache vars
482 initializers in each successor of the ENTRY_BLOCK_PTR. */
483 for (tsi
= tsi_start (shift_init_stmts
);
486 insert_edge_copies (tsi_stmt (tsi
), ENTRY_BLOCK_PTR
);
488 for (tsi
= tsi_start (mask_init_stmts
);
491 insert_edge_copies (tsi_stmt (tsi
), ENTRY_BLOCK_PTR
);
492 bsi_commit_edge_inserts ();
497 mf_decl_clear_locals (void)
499 /* Unset local shadows. */
500 mf_cache_shift_decl_l
= NULL_TREE
;
501 mf_cache_mask_decl_l
= NULL_TREE
;
505 mf_build_check_statement_for (tree base
, tree limit
,
506 block_stmt_iterator
*instr_bsi
,
507 location_t
*locus
, tree dirflag
)
509 tree_stmt_iterator head
, tsi
;
510 block_stmt_iterator bsi
;
511 basic_block cond_bb
, then_bb
, join_bb
;
518 /* We first need to split the current basic block, and start altering
519 the CFG. This allows us to insert the statements we're about to
520 construct into the right basic blocks. */
522 cond_bb
= bb_for_stmt (bsi_stmt (*instr_bsi
));
525 if (! bsi_end_p (bsi
))
526 e
= split_block (cond_bb
, bsi_stmt (bsi
));
528 e
= split_block_after_labels (cond_bb
);
532 /* A recap at this point: join_bb is the basic block at whose head
533 is the gimple statement for which this check expression is being
534 built. cond_bb is the (possibly new, synthetic) basic block the
535 end of which will contain the cache-lookup code, and a
536 conditional that jumps to the cache-miss code or, much more
537 likely, over to join_bb. */
539 /* Create the bb that contains the cache-miss fallback block (mf_check). */
540 then_bb
= create_empty_bb (cond_bb
);
541 make_edge (cond_bb
, then_bb
, EDGE_TRUE_VALUE
);
542 make_single_succ_edge (then_bb
, join_bb
, EDGE_FALLTHRU
);
544 /* Mark the pseudo-fallthrough edge from cond_bb to join_bb. */
545 e
= find_edge (cond_bb
, join_bb
);
546 e
->flags
= EDGE_FALSE_VALUE
;
547 e
->count
= cond_bb
->count
;
548 e
->probability
= REG_BR_PROB_BASE
;
550 /* Update dominance info. Note that bb_join's data was
551 updated by split_block. */
552 if (dom_info_available_p (CDI_DOMINATORS
))
554 set_immediate_dominator (CDI_DOMINATORS
, then_bb
, cond_bb
);
555 set_immediate_dominator (CDI_DOMINATORS
, join_bb
, cond_bb
);
558 /* Build our local variables. */
559 mf_elem
= create_tmp_var (mf_cache_structptr_type
, "__mf_elem");
560 mf_base
= create_tmp_var (mf_uintptr_type
, "__mf_base");
561 mf_limit
= create_tmp_var (mf_uintptr_type
, "__mf_limit");
563 /* Build: __mf_base = (uintptr_t) <base address expression>. */
564 t
= build_gimple_modify_stmt (mf_base
,
565 fold_convert (mf_uintptr_type
,
566 unshare_expr (base
)));
567 SET_EXPR_LOCUS (t
, locus
);
568 gimplify_to_stmt_list (&t
);
569 head
= tsi_start (t
);
572 /* Build: __mf_limit = (uintptr_t) <limit address expression>. */
573 t
= build_gimple_modify_stmt (mf_limit
,
574 fold_convert (mf_uintptr_type
,
575 unshare_expr (limit
)));
576 SET_EXPR_LOCUS (t
, locus
);
577 gimplify_to_stmt_list (&t
);
578 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
580 /* Build: __mf_elem = &__mf_lookup_cache [(__mf_base >> __mf_shift)
582 t
= build2 (RSHIFT_EXPR
, mf_uintptr_type
, mf_base
,
583 flag_mudflap_threads
? mf_cache_shift_decl
584 : mf_cache_shift_decl_l
);
585 t
= build2 (BIT_AND_EXPR
, mf_uintptr_type
, t
,
586 flag_mudflap_threads
? mf_cache_mask_decl
587 : mf_cache_mask_decl_l
);
588 t
= build4 (ARRAY_REF
,
589 TREE_TYPE (TREE_TYPE (mf_cache_array_decl
)),
590 mf_cache_array_decl
, t
, NULL_TREE
, NULL_TREE
);
591 t
= build1 (ADDR_EXPR
, mf_cache_structptr_type
, t
);
592 t
= build_gimple_modify_stmt (mf_elem
, t
);
593 SET_EXPR_LOCUS (t
, locus
);
594 gimplify_to_stmt_list (&t
);
595 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
597 /* Quick validity check.
599 if (__mf_elem->low > __mf_base
600 || (__mf_elem_high < __mf_limit))
603 ... and only if single-threaded:
604 __mf_lookup_shift_1 = f...;
605 __mf_lookup_mask_l = ...;
608 It is expected that this body of code is rarely executed so we mark
609 the edge to the THEN clause of the conditional jump as unlikely. */
611 /* Construct t <-- '__mf_elem->low > __mf_base'. */
612 t
= build3 (COMPONENT_REF
, mf_uintptr_type
,
613 build1 (INDIRECT_REF
, mf_cache_struct_type
, mf_elem
),
614 TYPE_FIELDS (mf_cache_struct_type
), NULL_TREE
);
615 t
= build2 (GT_EXPR
, boolean_type_node
, t
, mf_base
);
617 /* Construct '__mf_elem->high < __mf_limit'.
620 1) u <-- '__mf_elem->high'
621 2) v <-- '__mf_limit'.
623 Then build 'u <-- (u < v). */
625 u
= build3 (COMPONENT_REF
, mf_uintptr_type
,
626 build1 (INDIRECT_REF
, mf_cache_struct_type
, mf_elem
),
627 TREE_CHAIN (TYPE_FIELDS (mf_cache_struct_type
)), NULL_TREE
);
631 u
= build2 (LT_EXPR
, boolean_type_node
, u
, v
);
633 /* Build the composed conditional: t <-- 't || u'. Then store the
634 result of the evaluation of 't' in a temporary variable which we
635 can use as the condition for the conditional jump. */
636 t
= build2 (TRUTH_OR_EXPR
, boolean_type_node
, t
, u
);
637 cond
= create_tmp_var (boolean_type_node
, "__mf_unlikely_cond");
638 t
= build_gimple_modify_stmt (cond
, t
);
639 gimplify_to_stmt_list (&t
);
640 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
642 /* Build the conditional jump. 'cond' is just a temporary so we can
643 simply build a void COND_EXPR. We do need labels in both arms though. */
644 t
= build3 (COND_EXPR
, void_type_node
, cond
, NULL_TREE
, NULL_TREE
);
645 SET_EXPR_LOCUS (t
, locus
);
646 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
648 /* At this point, after so much hard work, we have only constructed
649 the conditional jump,
651 if (__mf_elem->low > __mf_base
652 || (__mf_elem_high < __mf_limit))
654 The lowered GIMPLE tree representing this code is in the statement
655 list starting at 'head'.
657 We can insert this now in the current basic block, i.e. the one that
658 the statement we're instrumenting was originally in. */
659 bsi
= bsi_last (cond_bb
);
660 for (tsi
= head
; ! tsi_end_p (tsi
); tsi_next (&tsi
))
661 bsi_insert_after (&bsi
, tsi_stmt (tsi
), BSI_CONTINUE_LINKING
);
663 /* Now build up the body of the cache-miss handling:
668 This is the body of the conditional. */
670 u
= mf_file_function_line_tree (locus
== NULL
? UNKNOWN_LOCATION
: *locus
);
671 /* NB: we pass the overall [base..limit] range to mf_check. */
672 v
= fold_build2 (PLUS_EXPR
, integer_type_node
,
673 fold_build2 (MINUS_EXPR
, mf_uintptr_type
, mf_limit
, mf_base
),
675 t
= build_call_expr (mf_check_fndecl
, 4, mf_base
, v
, dirflag
, u
);
676 gimplify_to_stmt_list (&t
);
677 head
= tsi_start (t
);
680 if (! flag_mudflap_threads
)
682 t
= build_gimple_modify_stmt (mf_cache_shift_decl_l
,
683 mf_cache_shift_decl
);
684 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
686 t
= build_gimple_modify_stmt (mf_cache_mask_decl_l
,
688 tsi_link_after (&tsi
, t
, TSI_CONTINUE_LINKING
);
691 /* Insert the check code in the THEN block. */
692 bsi
= bsi_start (then_bb
);
693 for (tsi
= head
; ! tsi_end_p (tsi
); tsi_next (&tsi
))
694 bsi_insert_after (&bsi
, tsi_stmt (tsi
), BSI_CONTINUE_LINKING
);
696 *instr_bsi
= bsi_start (join_bb
);
700 /* Check whether the given decl, generally a VAR_DECL or PARM_DECL, is
701 eligible for instrumentation. For the mudflap1 pass, this implies
702 that it should be registered with the libmudflap runtime. For the
703 mudflap2 pass this means instrumenting an indirection operation with
704 respect to the object.
707 mf_decl_eligible_p (tree decl
)
709 return ((TREE_CODE (decl
) == VAR_DECL
|| TREE_CODE (decl
) == PARM_DECL
)
710 /* The decl must have its address taken. In the case of
711 arrays, this flag is also set if the indexes are not
712 compile-time known valid constants. */
713 /* XXX: not sufficient: return-by-value structs! */
714 && TREE_ADDRESSABLE (decl
)
715 /* The type of the variable must be complete. */
716 && COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (decl
))
717 /* The decl hasn't been decomposed somehow. */
718 && !DECL_HAS_VALUE_EXPR_P (decl
));
723 mf_xform_derefs_1 (block_stmt_iterator
*iter
, tree
*tp
,
724 location_t
*locus
, tree dirflag
)
726 tree type
, base
, limit
, addr
, size
, t
;
728 /* Don't instrument read operations. */
729 if (dirflag
== integer_zero_node
&& flag_mudflap_ignore_reads
)
732 /* Don't instrument marked nodes. */
733 if (mf_marked_p (*tp
))
737 type
= TREE_TYPE (t
);
739 if (type
== error_mark_node
)
742 size
= TYPE_SIZE_UNIT (type
);
744 switch (TREE_CODE (t
))
749 /* This is trickier than it may first appear. The reason is
750 that we are looking at expressions from the "inside out" at
751 this point. We may have a complex nested aggregate/array
752 expression (e.g. "a.b[i].c"), maybe with an indirection as
753 the leftmost operator ("p->a.b.d"), where instrumentation
754 is necessary. Or we may have an innocent "a.b.c"
755 expression that must not be instrumented. We need to
756 recurse all the way down the nesting structure to figure it
757 out: looking just at the outer node is not enough. */
759 int component_ref_only
= (TREE_CODE (t
) == COMPONENT_REF
);
760 /* If we have a bitfield component reference, we must note the
761 innermost addressable object in ELT, from which we will
762 construct the byte-addressable bounds of the bitfield. */
763 tree elt
= NULL_TREE
;
764 int bitfield_ref_p
= (TREE_CODE (t
) == COMPONENT_REF
765 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1)));
767 /* Iterate to the top of the ARRAY_REF/COMPONENT_REF
768 containment hierarchy to find the outermost VAR_DECL. */
769 var
= TREE_OPERAND (t
, 0);
772 if (bitfield_ref_p
&& elt
== NULL_TREE
773 && (TREE_CODE (var
) == ARRAY_REF
774 || TREE_CODE (var
) == COMPONENT_REF
))
777 if (TREE_CODE (var
) == ARRAY_REF
)
779 component_ref_only
= 0;
780 var
= TREE_OPERAND (var
, 0);
782 else if (TREE_CODE (var
) == COMPONENT_REF
)
783 var
= TREE_OPERAND (var
, 0);
784 else if (INDIRECT_REF_P (var
))
786 base
= TREE_OPERAND (var
, 0);
791 gcc_assert (TREE_CODE (var
) == VAR_DECL
792 || TREE_CODE (var
) == PARM_DECL
793 || TREE_CODE (var
) == RESULT_DECL
794 || TREE_CODE (var
) == STRING_CST
);
795 /* Don't instrument this access if the underlying
796 variable is not "eligible". This test matches
797 those arrays that have only known-valid indexes,
798 and thus are not labeled TREE_ADDRESSABLE. */
799 if (! mf_decl_eligible_p (var
) || component_ref_only
)
803 base
= build1 (ADDR_EXPR
,
804 build_pointer_type (TREE_TYPE (var
)), var
);
810 /* Handle the case of ordinary non-indirection structure
811 accesses. These have only nested COMPONENT_REF nodes (no
812 INDIRECT_REF), but pass through the above filter loop.
813 Note that it's possible for such a struct variable to match
814 the eligible_p test because someone else might take its
817 /* We need special processing for bitfield components, because
818 their addresses cannot be taken. */
821 tree field
= TREE_OPERAND (t
, 1);
823 if (TREE_CODE (DECL_SIZE_UNIT (field
)) == INTEGER_CST
)
824 size
= DECL_SIZE_UNIT (field
);
827 elt
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (elt
)),
829 addr
= fold_convert (ptr_type_node
, elt
? elt
: base
);
830 addr
= fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
,
831 addr
, fold_convert (sizetype
,
832 byte_position (field
)));
835 addr
= build1 (ADDR_EXPR
, build_pointer_type (type
), t
);
837 limit
= fold_build2 (MINUS_EXPR
, mf_uintptr_type
,
838 fold_build2 (PLUS_EXPR
, mf_uintptr_type
,
839 convert (mf_uintptr_type
, addr
),
846 addr
= TREE_OPERAND (t
, 0);
848 limit
= fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
,
849 fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
, base
,
855 addr
= tree_mem_ref_addr (ptr_type_node
, t
);
857 limit
= fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
,
858 fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
, base
,
863 case ARRAY_RANGE_REF
:
864 warning (0, "mudflap checking not yet implemented for ARRAY_RANGE_REF");
868 /* ??? merge with COMPONENT_REF code above? */
872 /* If we're not dereferencing something, then the access
874 if (TREE_CODE (TREE_OPERAND (t
, 0)) != INDIRECT_REF
)
877 bpu
= bitsize_int (BITS_PER_UNIT
);
878 ofs
= convert (bitsizetype
, TREE_OPERAND (t
, 2));
879 rem
= size_binop (TRUNC_MOD_EXPR
, ofs
, bpu
);
880 ofs
= fold_convert (sizetype
, size_binop (TRUNC_DIV_EXPR
, ofs
, bpu
));
882 size
= convert (bitsizetype
, TREE_OPERAND (t
, 1));
883 size
= size_binop (PLUS_EXPR
, size
, rem
);
884 size
= size_binop (CEIL_DIV_EXPR
, size
, bpu
);
885 size
= convert (sizetype
, size
);
887 addr
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
888 addr
= convert (ptr_type_node
, addr
);
889 addr
= fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
, addr
, ofs
);
892 limit
= fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
,
893 fold_build2 (POINTER_PLUS_EXPR
, ptr_type_node
,
903 mf_build_check_statement_for (base
, limit
, iter
, locus
, dirflag
);
907 mf_xform_derefs (void)
909 basic_block bb
, next
;
910 block_stmt_iterator i
;
911 int saved_last_basic_block
= last_basic_block
;
913 bb
= ENTRY_BLOCK_PTR
->next_bb
;
917 for (i
= bsi_start (bb
); !bsi_end_p (i
); bsi_next (&i
))
919 tree s
= bsi_stmt (i
);
921 /* Only a few GIMPLE statements can reference memory. */
922 switch (TREE_CODE (s
))
924 case GIMPLE_MODIFY_STMT
:
925 mf_xform_derefs_1 (&i
, &GIMPLE_STMT_OPERAND (s
, 0),
926 EXPR_LOCUS (s
), integer_one_node
);
927 mf_xform_derefs_1 (&i
, &GIMPLE_STMT_OPERAND (s
, 1),
928 EXPR_LOCUS (s
), integer_zero_node
);
932 if (TREE_OPERAND (s
, 0) != NULL_TREE
)
934 if (TREE_CODE (TREE_OPERAND (s
, 0)) == GIMPLE_MODIFY_STMT
)
935 mf_xform_derefs_1 (&i
, &GIMPLE_STMT_OPERAND
936 (TREE_OPERAND (s
, 0), 1),
937 EXPR_LOCUS (s
), integer_zero_node
);
939 mf_xform_derefs_1 (&i
, &TREE_OPERAND (s
, 0), EXPR_LOCUS (s
),
950 while (bb
&& bb
->index
<= saved_last_basic_block
);
953 /* ------------------------------------------------------------------------ */
954 /* ADDR_EXPR transforms. Perform the declaration-related mudflap tree
955 transforms on the current function.
957 This is the first part of the mudflap instrumentation. It works on
958 high-level GIMPLE because after lowering, all variables are moved out
959 of their BIND_EXPR binding context, and we lose liveness information
960 for the declarations we wish to instrument. */
963 execute_mudflap_function_decls (void)
965 /* Don't instrument functions such as the synthetic constructor
966 built during mudflap_finish_file. */
967 if (mf_marked_p (current_function_decl
) ||
968 DECL_ARTIFICIAL (current_function_decl
))
971 push_gimplify_context ();
973 mf_xform_decls (DECL_SAVED_TREE (current_function_decl
),
974 DECL_ARGUMENTS (current_function_decl
));
976 pop_gimplify_context (NULL
);
980 /* This struct is passed between mf_xform_decls to store state needed
981 during the traversal searching for objects that have their
983 struct mf_xform_decls_data
989 /* Synthesize a CALL_EXPR and a TRY_FINALLY_EXPR, for this chain of
990 _DECLs if appropriate. Arrange to call the __mf_register function
991 now, and the __mf_unregister function later for each. */
993 mx_register_decls (tree decl
, tree
*stmt_list
)
995 tree finally_stmts
= NULL_TREE
;
996 tree_stmt_iterator initially_stmts
= tsi_start (*stmt_list
);
998 while (decl
!= NULL_TREE
)
1000 if (mf_decl_eligible_p (decl
)
1001 /* Not already processed. */
1002 && ! mf_marked_p (decl
)
1003 /* Automatic variable. */
1004 && ! DECL_EXTERNAL (decl
)
1005 && ! TREE_STATIC (decl
))
1007 tree size
= NULL_TREE
, variable_name
;
1008 tree unregister_fncall
, unregister_fncall_param
;
1009 tree register_fncall
, register_fncall_param
;
1011 size
= convert (size_type_node
, TYPE_SIZE_UNIT (TREE_TYPE (decl
)));
1014 unregister_fncall_param
=
1015 convert (ptr_type_node
,
1016 mf_mark (build1 (ADDR_EXPR
,
1017 build_pointer_type (TREE_TYPE (decl
)),
1019 /* __mf_unregister (&VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK) */
1020 unregister_fncall
= build_call_expr (mf_unregister_fndecl
, 3,
1021 unregister_fncall_param
,
1023 build_int_cst (NULL_TREE
, 3));
1026 variable_name
= mf_varname_tree (decl
);
1027 register_fncall_param
=
1028 convert (ptr_type_node
,
1029 mf_mark (build1 (ADDR_EXPR
,
1030 build_pointer_type (TREE_TYPE (decl
)),
1032 /* __mf_register (&VARIABLE, sizeof (VARIABLE), __MF_TYPE_STACK,
1034 register_fncall
= build_call_expr (mf_register_fndecl
, 4,
1035 register_fncall_param
,
1037 build_int_cst (NULL_TREE
, 3),
1041 /* Accumulate the two calls. */
1042 /* ??? Set EXPR_LOCATION. */
1043 gimplify_stmt (®ister_fncall
);
1044 gimplify_stmt (&unregister_fncall
);
1046 /* Add the __mf_register call at the current appending point. */
1047 if (tsi_end_p (initially_stmts
))
1049 if (!DECL_ARTIFICIAL (decl
))
1050 warning (0, "mudflap cannot track %qs in stub function",
1051 IDENTIFIER_POINTER (DECL_NAME (decl
)));
1055 tsi_link_before (&initially_stmts
, register_fncall
,
1058 /* Accumulate the FINALLY piece. */
1059 append_to_statement_list (unregister_fncall
, &finally_stmts
);
1064 decl
= TREE_CHAIN (decl
);
1067 /* Actually, (initially_stmts!=NULL) <=> (finally_stmts!=NULL) */
1068 if (finally_stmts
!= NULL_TREE
)
1070 tree t
= build2 (TRY_FINALLY_EXPR
, void_type_node
,
1071 *stmt_list
, finally_stmts
);
1073 append_to_statement_list (t
, stmt_list
);
1078 /* Process every variable mentioned in BIND_EXPRs. */
1080 mx_xfn_xform_decls (tree
*t
, int *continue_p
, void *data
)
1082 struct mf_xform_decls_data
* d
= (struct mf_xform_decls_data
*) data
;
1084 if (*t
== NULL_TREE
|| *t
== error_mark_node
)
1092 switch (TREE_CODE (*t
))
1096 /* Process function parameters now (but only once). */
1097 mx_register_decls (d
->param_decls
, &BIND_EXPR_BODY (*t
));
1098 d
->param_decls
= NULL_TREE
;
1100 mx_register_decls (BIND_EXPR_VARS (*t
), &BIND_EXPR_BODY (*t
));
1111 /* Perform the object lifetime tracking mudflap transform on the given function
1112 tree. The tree is mutated in place, with possibly copied subtree nodes.
1114 For every auto variable declared, if its address is ever taken
1115 within the function, then supply its lifetime to the mudflap
1116 runtime with the __mf_register and __mf_unregister calls.
1120 mf_xform_decls (tree fnbody
, tree fnparams
)
1122 struct mf_xform_decls_data d
;
1123 d
.param_decls
= fnparams
;
1124 walk_tree_without_duplicates (&fnbody
, mx_xfn_xform_decls
, &d
);
1128 /* ------------------------------------------------------------------------ */
1129 /* Externally visible mudflap functions. */
1132 /* Mark and return the given tree node to prevent further mudflap
1134 static GTY ((param_is (union tree_node
))) htab_t marked_trees
= NULL
;
1141 if (marked_trees
== NULL
)
1142 marked_trees
= htab_create_ggc (31, htab_hash_pointer
, htab_eq_pointer
,
1145 slot
= htab_find_slot (marked_trees
, t
, INSERT
);
1151 mf_marked_p (tree t
)
1155 if (marked_trees
== NULL
)
1158 entry
= htab_find (marked_trees
, t
);
1159 return (entry
!= NULL
);
1162 /* Remember given node as a static of some kind: global data,
1163 function-scope static, or an anonymous constant. Its assembler
1166 /* A list of globals whose incomplete declarations we encountered.
1167 Instead of emitting the __mf_register call for them here, it's
1168 delayed until program finish time. If they're still incomplete by
1169 then, warnings are emitted. */
1171 static GTY (()) VEC(tree
,gc
) *deferred_static_decls
;
1173 /* A list of statements for calling __mf_register() at startup time. */
1174 static GTY (()) tree enqueued_call_stmt_chain
;
1177 mudflap_register_call (tree obj
, tree object_size
, tree varname
)
1179 tree arg
, call_stmt
;
1181 arg
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (obj
)), obj
);
1182 arg
= convert (ptr_type_node
, arg
);
1184 call_stmt
= build_call_expr (mf_register_fndecl
, 4,
1186 convert (size_type_node
, object_size
),
1187 /* __MF_TYPE_STATIC */
1188 build_int_cst (NULL_TREE
, 4),
1191 append_to_statement_list (call_stmt
, &enqueued_call_stmt_chain
);
1195 mudflap_enqueue_decl (tree obj
)
1197 if (mf_marked_p (obj
))
1200 /* We don't need to process variable decls that are internally
1201 generated extern. If we did, we'd end up with warnings for them
1202 during mudflap_finish_file (). That would confuse the user,
1203 since the text would refer to variables that don't show up in the
1204 user's source code. */
1205 if (DECL_P (obj
) && DECL_EXTERNAL (obj
) && DECL_ARTIFICIAL (obj
))
1208 VEC_safe_push (tree
, gc
, deferred_static_decls
, obj
);
1213 mudflap_enqueue_constant (tree obj
)
1215 tree object_size
, varname
;
1217 if (mf_marked_p (obj
))
1220 if (TREE_CODE (obj
) == STRING_CST
)
1221 object_size
= build_int_cst (NULL_TREE
, TREE_STRING_LENGTH (obj
));
1223 object_size
= size_in_bytes (TREE_TYPE (obj
));
1225 if (TREE_CODE (obj
) == STRING_CST
)
1226 varname
= mf_build_string ("string literal");
1228 varname
= mf_build_string ("constant");
1230 mudflap_register_call (obj
, object_size
, varname
);
1234 /* Emit any file-wide instrumentation. */
1236 mudflap_finish_file (void)
1238 tree ctor_statements
= NULL_TREE
;
1240 /* No need to continue when there were errors. */
1241 if (errorcount
!= 0 || sorrycount
!= 0)
1244 /* Insert a call to __mf_init. */
1246 tree call2_stmt
= build_call_expr (mf_init_fndecl
, 0);
1247 append_to_statement_list (call2_stmt
, &ctor_statements
);
1250 /* If appropriate, call __mf_set_options to pass along read-ignore mode. */
1251 if (flag_mudflap_ignore_reads
)
1253 tree arg
= mf_build_string ("-ignore-reads");
1254 tree call_stmt
= build_call_expr (mf_set_options_fndecl
, 1, arg
);
1255 append_to_statement_list (call_stmt
, &ctor_statements
);
1258 /* Process all enqueued object decls. */
1259 if (deferred_static_decls
)
1263 for (i
= 0; VEC_iterate (tree
, deferred_static_decls
, i
, obj
); i
++)
1265 gcc_assert (DECL_P (obj
));
1267 if (mf_marked_p (obj
))
1270 /* Omit registration for static unaddressed objects. NB:
1271 Perform registration for non-static objects regardless of
1272 TREE_USED or TREE_ADDRESSABLE, because they may be used
1273 from other compilation units. */
1274 if (! TREE_PUBLIC (obj
) && ! TREE_ADDRESSABLE (obj
))
1277 if (! COMPLETE_TYPE_P (TREE_TYPE (obj
)))
1279 warning (0, "mudflap cannot track unknown size extern %qs",
1280 IDENTIFIER_POINTER (DECL_NAME (obj
)));
1284 mudflap_register_call (obj
,
1285 size_in_bytes (TREE_TYPE (obj
)),
1286 mf_varname_tree (obj
));
1289 VEC_truncate (tree
, deferred_static_decls
, 0);
1292 /* Append all the enqueued registration calls. */
1293 if (enqueued_call_stmt_chain
)
1295 append_to_statement_list (enqueued_call_stmt_chain
, &ctor_statements
);
1296 enqueued_call_stmt_chain
= NULL_TREE
;
1299 cgraph_build_static_cdtor ('I', ctor_statements
,
1300 MAX_RESERVED_INIT_PRIORITY
-1);
1307 return flag_mudflap
!= 0;
1310 struct tree_opt_pass pass_mudflap_1
=
1312 "mudflap1", /* name */
1313 gate_mudflap
, /* gate */
1314 execute_mudflap_function_decls
, /* execute */
1317 0, /* static_pass_number */
1319 PROP_gimple_any
, /* properties_required */
1320 0, /* properties_provided */
1321 0, /* properties_destroyed */
1322 0, /* todo_flags_start */
1323 TODO_dump_func
, /* todo_flags_finish */
1327 struct tree_opt_pass pass_mudflap_2
=
1329 "mudflap2", /* name */
1330 gate_mudflap
, /* gate */
1331 execute_mudflap_function_ops
, /* execute */
1334 0, /* static_pass_number */
1336 PROP_gimple_leh
, /* properties_required */
1337 0, /* properties_provided */
1338 0, /* properties_destroyed */
1339 0, /* todo_flags_start */
1340 TODO_verify_flow
| TODO_verify_stmts
1341 | TODO_dump_func
, /* todo_flags_finish */
1345 #include "gt-tree-mudflap.h"