aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/gcc-plugins
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/gcc-plugins')
-rw-r--r--scripts/gcc-plugins/cyc_complexity_plugin.c6
-rw-r--r--scripts/gcc-plugins/gcc-common.h140
-rw-r--r--scripts/gcc-plugins/latent_entropy_plugin.c12
-rw-r--r--scripts/gcc-plugins/sancov_plugin.c8
-rw-r--r--scripts/gcc-plugins/structleak_plugin.c246
5 files changed, 382 insertions, 30 deletions
diff --git a/scripts/gcc-plugins/cyc_complexity_plugin.c b/scripts/gcc-plugins/cyc_complexity_plugin.c
index 8af7db06122d..1909ec617431 100644
--- a/scripts/gcc-plugins/cyc_complexity_plugin.c
+++ b/scripts/gcc-plugins/cyc_complexity_plugin.c
@@ -52,12 +52,8 @@ static unsigned int cyc_complexity_execute(void)
52__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) 52__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
53{ 53{
54 const char * const plugin_name = plugin_info->base_name; 54 const char * const plugin_name = plugin_info->base_name;
55 struct register_pass_info cyc_complexity_pass_info;
56 55
57 cyc_complexity_pass_info.pass = make_cyc_complexity_pass(); 56 PASS_INFO(cyc_complexity, "ssa", 1, PASS_POS_INSERT_AFTER);
58 cyc_complexity_pass_info.reference_pass_name = "ssa";
59 cyc_complexity_pass_info.ref_pass_instance_number = 1;
60 cyc_complexity_pass_info.pos_op = PASS_POS_INSERT_AFTER;
61 57
62 if (!plugin_default_version_check(version, &gcc_version)) { 58 if (!plugin_default_version_check(version, &gcc_version)) {
63 error(G_("incompatible gcc/plugin versions")); 59 error(G_("incompatible gcc/plugin versions"));
diff --git a/scripts/gcc-plugins/gcc-common.h b/scripts/gcc-plugins/gcc-common.h
index 950fd2e64bb7..b232ab15624c 100644
--- a/scripts/gcc-plugins/gcc-common.h
+++ b/scripts/gcc-plugins/gcc-common.h
@@ -26,6 +26,9 @@
26#include "except.h" 26#include "except.h"
27#include "function.h" 27#include "function.h"
28#include "toplev.h" 28#include "toplev.h"
29#if BUILDING_GCC_VERSION >= 5000
30#include "expr.h"
31#endif
29#include "basic-block.h" 32#include "basic-block.h"
30#include "intl.h" 33#include "intl.h"
31#include "ggc.h" 34#include "ggc.h"
@@ -39,6 +42,9 @@
39#include "hash-map.h" 42#include "hash-map.h"
40#endif 43#endif
41 44
45#if BUILDING_GCC_VERSION >= 7000
46#include "memmodel.h"
47#endif
42#include "emit-rtl.h" 48#include "emit-rtl.h"
43#include "debug.h" 49#include "debug.h"
44#include "target.h" 50#include "target.h"
@@ -77,6 +83,9 @@
77#include "diagnostic.h" 83#include "diagnostic.h"
78#include "tree-dump.h" 84#include "tree-dump.h"
79#include "tree-pass.h" 85#include "tree-pass.h"
86#if BUILDING_GCC_VERSION >= 4009
87#include "pass_manager.h"
88#endif
80#include "predict.h" 89#include "predict.h"
81#include "ipa-utils.h" 90#include "ipa-utils.h"
82 91
@@ -91,6 +100,9 @@
91#include "tree-ssa-alias.h" 100#include "tree-ssa-alias.h"
92#include "tree-ssa.h" 101#include "tree-ssa.h"
93#include "stringpool.h" 102#include "stringpool.h"
103#if BUILDING_GCC_VERSION >= 7000
104#include "tree-vrp.h"
105#endif
94#include "tree-ssanames.h" 106#include "tree-ssanames.h"
95#include "print-tree.h" 107#include "print-tree.h"
96#include "tree-eh.h" 108#include "tree-eh.h"
@@ -113,20 +125,17 @@
113#include "builtins.h" 125#include "builtins.h"
114#endif 126#endif
115 127
116/* #include "expr.h" where are you... */
117extern rtx emit_move_insn(rtx x, rtx y);
118
119/* missing from basic_block.h... */ 128/* missing from basic_block.h... */
120extern void debug_dominance_info(enum cdi_direction dir); 129void debug_dominance_info(enum cdi_direction dir);
121extern void debug_dominance_tree(enum cdi_direction dir, basic_block root); 130void debug_dominance_tree(enum cdi_direction dir, basic_block root);
122 131
123#if BUILDING_GCC_VERSION == 4006 132#if BUILDING_GCC_VERSION == 4006
124extern void debug_gimple_stmt(gimple); 133void debug_gimple_stmt(gimple);
125extern void debug_gimple_seq(gimple_seq); 134void debug_gimple_seq(gimple_seq);
126extern void print_gimple_seq(FILE *, gimple_seq, int, int); 135void print_gimple_seq(FILE *, gimple_seq, int, int);
127extern void print_gimple_stmt(FILE *, gimple, int, int); 136void print_gimple_stmt(FILE *, gimple, int, int);
128extern void print_gimple_expr(FILE *, gimple, int, int); 137void print_gimple_expr(FILE *, gimple, int, int);
129extern void dump_gimple_stmt(pretty_printer *, gimple, int, int); 138void dump_gimple_stmt(pretty_printer *, gimple, int, int);
130#endif 139#endif
131 140
132#define __unused __attribute__((__unused__)) 141#define __unused __attribute__((__unused__))
@@ -140,6 +149,29 @@ extern void dump_gimple_stmt(pretty_printer *, gimple, int, int);
140/* should come from c-tree.h if only it were installed for gcc 4.5... */ 149/* should come from c-tree.h if only it were installed for gcc 4.5... */
141#define C_TYPE_FIELDS_READONLY(TYPE) TREE_LANG_FLAG_1(TYPE) 150#define C_TYPE_FIELDS_READONLY(TYPE) TREE_LANG_FLAG_1(TYPE)
142 151
152static inline tree build_const_char_string(int len, const char *str)
153{
154 tree cstr, elem, index, type;
155
156 cstr = build_string(len, str);
157 elem = build_type_variant(char_type_node, 1, 0);
158 index = build_index_type(size_int(len - 1));
159 type = build_array_type(elem, index);
160 TREE_TYPE(cstr) = type;
161 TREE_CONSTANT(cstr) = 1;
162 TREE_READONLY(cstr) = 1;
163 TREE_STATIC(cstr) = 1;
164 return cstr;
165}
166
167#define PASS_INFO(NAME, REF, ID, POS) \
168struct register_pass_info NAME##_pass_info = { \
169 .pass = make_##NAME##_pass(), \
170 .reference_pass_name = REF, \
171 .ref_pass_instance_number = ID, \
172 .pos_op = POS, \
173}
174
143#if BUILDING_GCC_VERSION == 4005 175#if BUILDING_GCC_VERSION == 4005
144#define FOR_EACH_LOCAL_DECL(FUN, I, D) \ 176#define FOR_EACH_LOCAL_DECL(FUN, I, D) \
145 for (tree vars = (FUN)->local_decls, (I) = 0; \ 177 for (tree vars = (FUN)->local_decls, (I) = 0; \
@@ -287,6 +319,22 @@ static inline struct cgraph_node *cgraph_next_function_with_gimple_body(struct c
287 return NULL; 319 return NULL;
288} 320}
289 321
322static inline bool cgraph_for_node_and_aliases(cgraph_node_ptr node, bool (*callback)(cgraph_node_ptr, void *), void *data, bool include_overwritable)
323{
324 cgraph_node_ptr alias;
325
326 if (callback(node, data))
327 return true;
328
329 for (alias = node->same_body; alias; alias = alias->next) {
330 if (include_overwritable || cgraph_function_body_availability(alias) > AVAIL_OVERWRITABLE)
331 if (cgraph_for_node_and_aliases(alias, callback, data, include_overwritable))
332 return true;
333 }
334
335 return false;
336}
337
290#define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(node) \ 338#define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(node) \
291 for ((node) = cgraph_first_function_with_gimple_body(); (node); \ 339 for ((node) = cgraph_first_function_with_gimple_body(); (node); \
292 (node) = cgraph_next_function_with_gimple_body(node)) 340 (node) = cgraph_next_function_with_gimple_body(node))
@@ -399,6 +447,7 @@ typedef union gimple_statement_d gassign;
399typedef union gimple_statement_d gcall; 447typedef union gimple_statement_d gcall;
400typedef union gimple_statement_d gcond; 448typedef union gimple_statement_d gcond;
401typedef union gimple_statement_d gdebug; 449typedef union gimple_statement_d gdebug;
450typedef union gimple_statement_d ggoto;
402typedef union gimple_statement_d gphi; 451typedef union gimple_statement_d gphi;
403typedef union gimple_statement_d greturn; 452typedef union gimple_statement_d greturn;
404 453
@@ -452,6 +501,16 @@ static inline const gdebug *as_a_const_gdebug(const_gimple stmt)
452 return stmt; 501 return stmt;
453} 502}
454 503
504static inline ggoto *as_a_ggoto(gimple stmt)
505{
506 return stmt;
507}
508
509static inline const ggoto *as_a_const_ggoto(const_gimple stmt)
510{
511 return stmt;
512}
513
455static inline gphi *as_a_gphi(gimple stmt) 514static inline gphi *as_a_gphi(gimple stmt)
456{ 515{
457 return stmt; 516 return stmt;
@@ -494,8 +553,18 @@ static inline const greturn *as_a_const_greturn(const_gimple stmt)
494#define section_name_prefix LTO_SECTION_NAME_PREFIX 553#define section_name_prefix LTO_SECTION_NAME_PREFIX
495#define fatal_error(loc, gmsgid, ...) fatal_error((gmsgid), __VA_ARGS__) 554#define fatal_error(loc, gmsgid, ...) fatal_error((gmsgid), __VA_ARGS__)
496 555
556rtx emit_move_insn(rtx x, rtx y);
557
497typedef struct rtx_def rtx_insn; 558typedef struct rtx_def rtx_insn;
498 559
560static inline const char *get_decl_section_name(const_tree decl)
561{
562 if (DECL_SECTION_NAME(decl) == NULL_TREE)
563 return NULL;
564
565 return TREE_STRING_POINTER(DECL_SECTION_NAME(decl));
566}
567
499static inline void set_decl_section_name(tree node, const char *value) 568static inline void set_decl_section_name(tree node, const char *value)
500{ 569{
501 if (value) 570 if (value)
@@ -511,6 +580,7 @@ typedef struct gimple_statement_base gassign;
511typedef struct gimple_statement_call gcall; 580typedef struct gimple_statement_call gcall;
512typedef struct gimple_statement_base gcond; 581typedef struct gimple_statement_base gcond;
513typedef struct gimple_statement_base gdebug; 582typedef struct gimple_statement_base gdebug;
583typedef struct gimple_statement_base ggoto;
514typedef struct gimple_statement_phi gphi; 584typedef struct gimple_statement_phi gphi;
515typedef struct gimple_statement_base greturn; 585typedef struct gimple_statement_base greturn;
516 586
@@ -564,6 +634,16 @@ static inline const gdebug *as_a_const_gdebug(const_gimple stmt)
564 return stmt; 634 return stmt;
565} 635}
566 636
637static inline ggoto *as_a_ggoto(gimple stmt)
638{
639 return stmt;
640}
641
642static inline const ggoto *as_a_const_ggoto(const_gimple stmt)
643{
644 return stmt;
645}
646
567static inline gphi *as_a_gphi(gimple stmt) 647static inline gphi *as_a_gphi(gimple stmt)
568{ 648{
569 return as_a<gphi>(stmt); 649 return as_a<gphi>(stmt);
@@ -591,6 +671,11 @@ static inline const greturn *as_a_const_greturn(const_gimple stmt)
591#define NODE_DECL(node) (node)->decl 671#define NODE_DECL(node) (node)->decl
592#define cgraph_node_name(node) (node)->name() 672#define cgraph_node_name(node) (node)->name()
593#define NODE_IMPLICIT_ALIAS(node) (node)->cpp_implicit_alias 673#define NODE_IMPLICIT_ALIAS(node) (node)->cpp_implicit_alias
674
675static inline opt_pass *get_pass_for_id(int id)
676{
677 return g->get_passes()->get_pass_for_id(id);
678}
594#endif 679#endif
595 680
596#if BUILDING_GCC_VERSION >= 5000 && BUILDING_GCC_VERSION < 6000 681#if BUILDING_GCC_VERSION >= 5000 && BUILDING_GCC_VERSION < 6000
@@ -611,6 +696,11 @@ inline bool is_a_helper<const gassign *>::test(const_gimple gs)
611 696
612#define INSN_DELETED_P(insn) (insn)->deleted() 697#define INSN_DELETED_P(insn) (insn)->deleted()
613 698
699static inline const char *get_decl_section_name(const_tree decl)
700{
701 return DECL_SECTION_NAME(decl);
702}
703
614/* symtab/cgraph related */ 704/* symtab/cgraph related */
615#define debug_cgraph_node(node) (node)->debug() 705#define debug_cgraph_node(node) (node)->debug()
616#define cgraph_get_node(decl) cgraph_node::get(decl) 706#define cgraph_get_node(decl) cgraph_node::get(decl)
@@ -619,6 +709,7 @@ inline bool is_a_helper<const gassign *>::test(const_gimple gs)
619#define cgraph_n_nodes symtab->cgraph_count 709#define cgraph_n_nodes symtab->cgraph_count
620#define cgraph_max_uid symtab->cgraph_max_uid 710#define cgraph_max_uid symtab->cgraph_max_uid
621#define varpool_get_node(decl) varpool_node::get(decl) 711#define varpool_get_node(decl) varpool_node::get(decl)
712#define dump_varpool_node(file, node) (node)->dump(file)
622 713
623#define cgraph_create_edge(caller, callee, call_stmt, count, freq, nest) \ 714#define cgraph_create_edge(caller, callee, call_stmt, count, freq, nest) \
624 (caller)->create_edge((callee), (call_stmt), (count), (freq)) 715 (caller)->create_edge((callee), (call_stmt), (count), (freq))
@@ -674,6 +765,11 @@ static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node)
674 return node->get_alias_target(); 765 return node->get_alias_target();
675} 766}
676 767
768static inline bool cgraph_for_node_and_aliases(cgraph_node_ptr node, bool (*callback)(cgraph_node_ptr, void *), void *data, bool include_overwritable)
769{
770 return node->call_for_symbol_thunks_and_aliases(callback, data, include_overwritable);
771}
772
677static inline struct cgraph_node_hook_list *cgraph_add_function_insertion_hook(cgraph_node_hook hook, void *data) 773static inline struct cgraph_node_hook_list *cgraph_add_function_insertion_hook(cgraph_node_hook hook, void *data)
678{ 774{
679 return symtab->add_cgraph_insertion_hook(hook, data); 775 return symtab->add_cgraph_insertion_hook(hook, data);
@@ -731,6 +827,13 @@ static inline gimple gimple_build_assign_with_ops(enum tree_code subcode, tree l
731 827
732template <> 828template <>
733template <> 829template <>
830inline bool is_a_helper<const ggoto *>::test(const_gimple gs)
831{
832 return gs->code == GIMPLE_GOTO;
833}
834
835template <>
836template <>
734inline bool is_a_helper<const greturn *>::test(const_gimple gs) 837inline bool is_a_helper<const greturn *>::test(const_gimple gs)
735{ 838{
736 return gs->code == GIMPLE_RETURN; 839 return gs->code == GIMPLE_RETURN;
@@ -766,6 +869,16 @@ static inline const gcall *as_a_const_gcall(const_gimple stmt)
766 return as_a<const gcall *>(stmt); 869 return as_a<const gcall *>(stmt);
767} 870}
768 871
872static inline ggoto *as_a_ggoto(gimple stmt)
873{
874 return as_a<ggoto *>(stmt);
875}
876
877static inline const ggoto *as_a_const_ggoto(const_gimple stmt)
878{
879 return as_a<const ggoto *>(stmt);
880}
881
769static inline gphi *as_a_gphi(gimple stmt) 882static inline gphi *as_a_gphi(gimple stmt)
770{ 883{
771 return as_a<gphi *>(stmt); 884 return as_a<gphi *>(stmt);
@@ -828,4 +941,9 @@ static inline void debug_gimple_stmt(const_gimple s)
828#define debug_gimple_stmt(s) debug_gimple_stmt(CONST_CAST_GIMPLE(s)) 941#define debug_gimple_stmt(s) debug_gimple_stmt(CONST_CAST_GIMPLE(s))
829#endif 942#endif
830 943
944#if BUILDING_GCC_VERSION >= 7000
945#define get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, preversep, pvolatilep, keep_aligning) \
946 get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, preversep, pvolatilep)
947#endif
948
831#endif 949#endif
diff --git a/scripts/gcc-plugins/latent_entropy_plugin.c b/scripts/gcc-plugins/latent_entropy_plugin.c
index 12541126575b..65264960910d 100644
--- a/scripts/gcc-plugins/latent_entropy_plugin.c
+++ b/scripts/gcc-plugins/latent_entropy_plugin.c
@@ -328,9 +328,9 @@ static enum tree_code get_op(tree *rhs)
328 op = LROTATE_EXPR; 328 op = LROTATE_EXPR;
329 /* 329 /*
330 * This code limits the value of random_const to 330 * This code limits the value of random_const to
331 * the size of a wide int for the rotation 331 * the size of a long for the rotation
332 */ 332 */
333 random_const &= HOST_BITS_PER_WIDE_INT - 1; 333 random_const %= TYPE_PRECISION(long_unsigned_type_node);
334 break; 334 break;
335 } 335 }
336 336
@@ -592,12 +592,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
592 const struct plugin_argument * const argv = plugin_info->argv; 592 const struct plugin_argument * const argv = plugin_info->argv;
593 int i; 593 int i;
594 594
595 struct register_pass_info latent_entropy_pass_info;
596
597 latent_entropy_pass_info.pass = make_latent_entropy_pass();
598 latent_entropy_pass_info.reference_pass_name = "optimized";
599 latent_entropy_pass_info.ref_pass_instance_number = 1;
600 latent_entropy_pass_info.pos_op = PASS_POS_INSERT_BEFORE;
601 static const struct ggc_root_tab gt_ggc_r_gt_latent_entropy[] = { 595 static const struct ggc_root_tab gt_ggc_r_gt_latent_entropy[] = {
602 { 596 {
603 .base = &latent_entropy_decl, 597 .base = &latent_entropy_decl,
@@ -609,6 +603,8 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
609 LAST_GGC_ROOT_TAB 603 LAST_GGC_ROOT_TAB
610 }; 604 };
611 605
606 PASS_INFO(latent_entropy, "optimized", 1, PASS_POS_INSERT_BEFORE);
607
612 if (!plugin_default_version_check(version, &gcc_version)) { 608 if (!plugin_default_version_check(version, &gcc_version)) {
613 error(G_("incompatible gcc/plugin versions")); 609 error(G_("incompatible gcc/plugin versions"));
614 return 1; 610 return 1;
diff --git a/scripts/gcc-plugins/sancov_plugin.c b/scripts/gcc-plugins/sancov_plugin.c
index 70f5fe0d590a..9b0b5cbc5b89 100644
--- a/scripts/gcc-plugins/sancov_plugin.c
+++ b/scripts/gcc-plugins/sancov_plugin.c
@@ -89,7 +89,6 @@ static void sancov_start_unit(void __unused *gcc_data, void __unused *user_data)
89__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version) 89__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
90{ 90{
91 int i; 91 int i;
92 struct register_pass_info sancov_plugin_pass_info;
93 const char * const plugin_name = plugin_info->base_name; 92 const char * const plugin_name = plugin_info->base_name;
94 const int argc = plugin_info->argc; 93 const int argc = plugin_info->argc;
95 const struct plugin_argument * const argv = plugin_info->argv; 94 const struct plugin_argument * const argv = plugin_info->argv;
@@ -107,14 +106,11 @@ __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gc
107 }; 106 };
108 107
109 /* BBs can be split afterwards?? */ 108 /* BBs can be split afterwards?? */
110 sancov_plugin_pass_info.pass = make_sancov_pass();
111#if BUILDING_GCC_VERSION >= 4009 109#if BUILDING_GCC_VERSION >= 4009
112 sancov_plugin_pass_info.reference_pass_name = "asan"; 110 PASS_INFO(sancov, "asan", 0, PASS_POS_INSERT_BEFORE);
113#else 111#else
114 sancov_plugin_pass_info.reference_pass_name = "nrv"; 112 PASS_INFO(sancov, "nrv", 1, PASS_POS_INSERT_BEFORE);
115#endif 113#endif
116 sancov_plugin_pass_info.ref_pass_instance_number = 0;
117 sancov_plugin_pass_info.pos_op = PASS_POS_INSERT_BEFORE;
118 114
119 if (!plugin_default_version_check(version, &gcc_version)) { 115 if (!plugin_default_version_check(version, &gcc_version)) {
120 error(G_("incompatible gcc/plugin versions")); 116 error(G_("incompatible gcc/plugin versions"));
diff --git a/scripts/gcc-plugins/structleak_plugin.c b/scripts/gcc-plugins/structleak_plugin.c
new file mode 100644
index 000000000000..fa3d7a4b26f2
--- /dev/null
+++ b/scripts/gcc-plugins/structleak_plugin.c
@@ -0,0 +1,246 @@
1/*
2 * Copyright 2013-2017 by PaX Team <pageexec@freemail.hu>
3 * Licensed under the GPL v2
4 *
5 * Note: the choice of the license means that the compilation process is
6 * NOT 'eligible' as defined by gcc's library exception to the GPL v3,
7 * but for the kernel it doesn't matter since it doesn't link against
8 * any of the gcc libraries
9 *
10 * gcc plugin to forcibly initialize certain local variables that could
11 * otherwise leak kernel stack to userland if they aren't properly initialized
12 * by later code
13 *
14 * Homepage: http://pax.grsecurity.net/
15 *
16 * Options:
17 * -fplugin-arg-structleak_plugin-disable
18 * -fplugin-arg-structleak_plugin-verbose
19 *
20 * Usage:
21 * $ # for 4.5/4.6/C based 4.7
22 * $ gcc -I`gcc -print-file-name=plugin`/include -I`gcc -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
23 * $ # for C++ based 4.7/4.8+
24 * $ g++ -I`g++ -print-file-name=plugin`/include -I`g++ -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
25 * $ gcc -fplugin=./structleak_plugin.so test.c -O2
26 *
27 * TODO: eliminate redundant initializers
28 * increase type coverage
29 */
30
31#include "gcc-common.h"
32
33/* unused C type flag in all versions 4.5-6 */
34#define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
35
36__visible int plugin_is_GPL_compatible;
37
38static struct plugin_info structleak_plugin_info = {
39 .version = "201607271510vanilla",
40 .help = "disable\tdo not activate plugin\n"
41 "verbose\tprint all initialized variables\n",
42};
43
44static bool verbose;
45
46static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
47{
48 *no_add_attrs = true;
49
50 /* check for types? for now accept everything linux has to offer */
51 if (TREE_CODE(*node) != FIELD_DECL)
52 return NULL_TREE;
53
54 *no_add_attrs = false;
55 return NULL_TREE;
56}
57
58static struct attribute_spec user_attr = {
59 .name = "user",
60 .min_length = 0,
61 .max_length = 0,
62 .decl_required = false,
63 .type_required = false,
64 .function_type_required = false,
65 .handler = handle_user_attribute,
66#if BUILDING_GCC_VERSION >= 4007
67 .affects_type_identity = true
68#endif
69};
70
71static void register_attributes(void *event_data, void *data)
72{
73 register_attribute(&user_attr);
74}
75
76static tree get_field_type(tree field)
77{
78 return strip_array_types(TREE_TYPE(field));
79}
80
81static bool is_userspace_type(tree type)
82{
83 tree field;
84
85 for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {
86 tree fieldtype = get_field_type(field);
87 enum tree_code code = TREE_CODE(fieldtype);
88
89 if (code == RECORD_TYPE || code == UNION_TYPE)
90 if (is_userspace_type(fieldtype))
91 return true;
92
93 if (lookup_attribute("user", DECL_ATTRIBUTES(field)))
94 return true;
95 }
96 return false;
97}
98
99static void finish_type(void *event_data, void *data)
100{
101 tree type = (tree)event_data;
102
103 if (type == NULL_TREE || type == error_mark_node)
104 return;
105
106#if BUILDING_GCC_VERSION >= 5000
107 if (TREE_CODE(type) == ENUMERAL_TYPE)
108 return;
109#endif
110
111 if (TYPE_USERSPACE(type))
112 return;
113
114 if (is_userspace_type(type))
115 TYPE_USERSPACE(type) = 1;
116}
117
118static void initialize(tree var)
119{
120 basic_block bb;
121 gimple_stmt_iterator gsi;
122 tree initializer;
123 gimple init_stmt;
124
125 /* this is the original entry bb before the forced split */
126 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
127
128 /* first check if variable is already initialized, warn otherwise */
129 for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
130 gimple stmt = gsi_stmt(gsi);
131 tree rhs1;
132
133 /* we're looking for an assignment of a single rhs... */
134 if (!gimple_assign_single_p(stmt))
135 continue;
136 rhs1 = gimple_assign_rhs1(stmt);
137#if BUILDING_GCC_VERSION >= 4007
138 /* ... of a non-clobbering expression... */
139 if (TREE_CLOBBER_P(rhs1))
140 continue;
141#endif
142 /* ... to our variable... */
143 if (gimple_get_lhs(stmt) != var)
144 continue;
145 /* if it's an initializer then we're good */
146 if (TREE_CODE(rhs1) == CONSTRUCTOR)
147 return;
148 }
149
150 /* these aren't the 0days you're looking for */
151 if (verbose)
152 inform(DECL_SOURCE_LOCATION(var),
153 "userspace variable will be forcibly initialized");
154
155 /* build the initializer expression */
156 initializer = build_constructor(TREE_TYPE(var), NULL);
157
158 /* build the initializer stmt */
159 init_stmt = gimple_build_assign(var, initializer);
160 gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
161 gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);
162 update_stmt(init_stmt);
163}
164
165static unsigned int structleak_execute(void)
166{
167 basic_block bb;
168 unsigned int ret = 0;
169 tree var;
170 unsigned int i;
171
172 /* split the first bb where we can put the forced initializers */
173 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
174 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
175 if (!single_pred_p(bb)) {
176 split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
177 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
178 }
179
180 /* enumerate all local variables and forcibly initialize our targets */
181 FOR_EACH_LOCAL_DECL(cfun, i, var) {
182 tree type = TREE_TYPE(var);
183
184 gcc_assert(DECL_P(var));
185 if (!auto_var_in_fn_p(var, current_function_decl))
186 continue;
187
188 /* only care about structure types */
189 if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
190 continue;
191
192 /* if the type is of interest, examine the variable */
193 if (TYPE_USERSPACE(type))
194 initialize(var);
195 }
196
197 return ret;
198}
199
200#define PASS_NAME structleak
201#define NO_GATE
202#define PROPERTIES_REQUIRED PROP_cfg
203#define TODO_FLAGS_FINISH TODO_verify_il | TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func | TODO_remove_unused_locals | TODO_update_ssa | TODO_ggc_collect | TODO_verify_flow
204#include "gcc-generate-gimple-pass.h"
205
206__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
207{
208 int i;
209 const char * const plugin_name = plugin_info->base_name;
210 const int argc = plugin_info->argc;
211 const struct plugin_argument * const argv = plugin_info->argv;
212 bool enable = true;
213
214 PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);
215
216 if (!plugin_default_version_check(version, &gcc_version)) {
217 error(G_("incompatible gcc/plugin versions"));
218 return 1;
219 }
220
221 if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
222 inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
223 enable = false;
224 }
225
226 for (i = 0; i < argc; ++i) {
227 if (!strcmp(argv[i].key, "disable")) {
228 enable = false;
229 continue;
230 }
231 if (!strcmp(argv[i].key, "verbose")) {
232 verbose = true;
233 continue;
234 }
235 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
236 }
237
238 register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);
239 if (enable) {
240 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);
241 register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
242 }
243 register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
244
245 return 0;
246}