1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include "gcc-common.h"
33
34
35#define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
36
37__visible int plugin_is_GPL_compatible;
38
39static struct plugin_info structleak_plugin_info = {
40 .version = "201607271510vanilla",
41 .help = "disable\tdo not activate plugin\n"
42 "verbose\tprint all initialized variables\n",
43};
44
45static bool verbose;
46static bool byref_all;
47
48static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
49{
50 *no_add_attrs = true;
51
52
53 if (TREE_CODE(*node) != FIELD_DECL)
54 return NULL_TREE;
55
56 *no_add_attrs = false;
57 return NULL_TREE;
58}
59
60static struct attribute_spec user_attr = {
61 .name = "user",
62 .min_length = 0,
63 .max_length = 0,
64 .decl_required = false,
65 .type_required = false,
66 .function_type_required = false,
67 .handler = handle_user_attribute,
68#if BUILDING_GCC_VERSION >= 4007
69 .affects_type_identity = true
70#endif
71};
72
73static void register_attributes(void *event_data, void *data)
74{
75 register_attribute(&user_attr);
76}
77
78static tree get_field_type(tree field)
79{
80 return strip_array_types(TREE_TYPE(field));
81}
82
83static bool is_userspace_type(tree type)
84{
85 tree field;
86
87 for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {
88 tree fieldtype = get_field_type(field);
89 enum tree_code code = TREE_CODE(fieldtype);
90
91 if (code == RECORD_TYPE || code == UNION_TYPE)
92 if (is_userspace_type(fieldtype))
93 return true;
94
95 if (lookup_attribute("user", DECL_ATTRIBUTES(field)))
96 return true;
97 }
98 return false;
99}
100
101static void finish_type(void *event_data, void *data)
102{
103 tree type = (tree)event_data;
104
105 if (type == NULL_TREE || type == error_mark_node)
106 return;
107
108#if BUILDING_GCC_VERSION >= 5000
109 if (TREE_CODE(type) == ENUMERAL_TYPE)
110 return;
111#endif
112
113 if (TYPE_USERSPACE(type))
114 return;
115
116 if (is_userspace_type(type))
117 TYPE_USERSPACE(type) = 1;
118}
119
120static void initialize(tree var)
121{
122 basic_block bb;
123 gimple_stmt_iterator gsi;
124 tree initializer;
125 gimple init_stmt;
126
127
128 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
129
130
131 for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
132 gimple stmt = gsi_stmt(gsi);
133 tree rhs1;
134
135
136 if (!gimple_assign_single_p(stmt))
137 continue;
138 rhs1 = gimple_assign_rhs1(stmt);
139#if BUILDING_GCC_VERSION >= 4007
140
141 if (TREE_CLOBBER_P(rhs1))
142 continue;
143#endif
144
145 if (gimple_get_lhs(stmt) != var)
146 continue;
147
148 if (TREE_CODE(rhs1) == CONSTRUCTOR)
149 return;
150 }
151
152
153 if (verbose)
154 inform(DECL_SOURCE_LOCATION(var),
155 "%s variable will be forcibly initialized",
156 (byref_all && TREE_ADDRESSABLE(var)) ? "byref"
157 : "userspace");
158
159
160 initializer = build_constructor(TREE_TYPE(var), NULL);
161
162
163 init_stmt = gimple_build_assign(var, initializer);
164 gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
165 gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);
166 update_stmt(init_stmt);
167}
168
169static unsigned int structleak_execute(void)
170{
171 basic_block bb;
172 unsigned int ret = 0;
173 tree var;
174 unsigned int i;
175
176
177 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
178 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
179 if (!single_pred_p(bb)) {
180 split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
181 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
182 }
183
184
185 FOR_EACH_LOCAL_DECL(cfun, i, var) {
186 tree type = TREE_TYPE(var);
187
188 gcc_assert(DECL_P(var));
189 if (!auto_var_in_fn_p(var, current_function_decl))
190 continue;
191
192
193 if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
194 continue;
195
196
197 if (TYPE_USERSPACE(type) ||
198 (byref_all && TREE_ADDRESSABLE(var)))
199 initialize(var);
200 }
201
202 return ret;
203}
204
205#define PASS_NAME structleak
206#define NO_GATE
207#define PROPERTIES_REQUIRED PROP_cfg
208#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
209#include "gcc-generate-gimple-pass.h"
210
211__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
212{
213 int i;
214 const char * const plugin_name = plugin_info->base_name;
215 const int argc = plugin_info->argc;
216 const struct plugin_argument * const argv = plugin_info->argv;
217 bool enable = true;
218
219 PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);
220
221 if (!plugin_default_version_check(version, &gcc_version)) {
222 error(G_("incompatible gcc/plugin versions"));
223 return 1;
224 }
225
226 if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
227 inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
228 enable = false;
229 }
230
231 for (i = 0; i < argc; ++i) {
232 if (!strcmp(argv[i].key, "disable")) {
233 enable = false;
234 continue;
235 }
236 if (!strcmp(argv[i].key, "verbose")) {
237 verbose = true;
238 continue;
239 }
240 if (!strcmp(argv[i].key, "byref-all")) {
241 byref_all = true;
242 continue;
243 }
244 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
245 }
246
247 register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);
248 if (enable) {
249 register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);
250 register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
251 }
252 register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
253
254 return 0;
255}
256