Merge branch 'devel3'
[smatch.git] / smatch_type_val.c
blob6e5013807f3c90aa68cf7fbf473d09753978b84a
1 /*
2 * Copyright (C) 2013 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * The plan here is to save all the possible values store to a given struct
20 * member.
22 * We will load all the values in to the function_type_val table first then
23 * run a script on that and load all the resulting values into the type_val
24 * table.
26 * So in this file we want to take the union of everything assigned to the
27 * struct member and insert it into the function_type_val at the end.
29 * You would think that we could use smatch_modification_hooks.c or
30 * extra_modification_hook() here to get the information here but in the end we
31 * need to code everything again a third time.
35 #include "smatch.h"
36 #include "smatch_slist.h"
37 #include "smatch_extra.h"
39 static int my_id;
41 struct stree_stack *fn_type_val_stack;
42 struct stree *fn_type_val;
43 struct stree *global_type_val;
45 static char *db_vals;
46 static int get_vals(void *unused, int argc, char **argv, char **azColName)
48 db_vals = alloc_string(argv[0]);
49 return 0;
52 static void match_inline_start(struct expression *expr)
54 push_stree(&fn_type_val_stack, fn_type_val);
55 fn_type_val = NULL;
58 static void match_inline_end(struct expression *expr)
60 free_stree(&fn_type_val);
61 fn_type_val = pop_stree(&fn_type_val_stack);
64 int get_db_type_rl(struct expression *expr, struct range_list **rl)
66 char *member;
67 struct range_list *tmp;
69 member = get_member_name(expr);
70 if (!member)
71 return 0;
73 db_vals = NULL;
74 run_sql(get_vals, NULL,
75 "select value from type_value where type = '%s';", member);
76 free_string(member);
77 if (!db_vals)
78 return 0;
79 str_to_rl(&llong_ctype, db_vals, &tmp);
80 tmp = cast_rl(get_type(expr), tmp);
81 if (is_whole_rl(tmp))
82 return 0;
83 *rl = tmp;
84 free_string(db_vals);
86 return 1;
89 static void add_type_val(char *member, struct range_list *rl)
91 struct smatch_state *old, *add, *new;
93 member = alloc_string(member);
94 old = get_state_stree(fn_type_val, my_id, member, NULL);
95 add = alloc_estate_rl(rl);
96 if (old)
97 new = merge_estates(old, add);
98 else
99 new = add;
100 set_state_stree(&fn_type_val, my_id, member, NULL, new);
103 static void add_fake_type_val(char *member, struct range_list *rl, int ignore)
105 struct smatch_state *old, *add, *new;
107 member = alloc_string(member);
108 old = get_state_stree(fn_type_val, my_id, member, NULL);
109 if (old && strcmp(old->name, "min-max") == 0)
110 return;
111 if (ignore && old && strcmp(old->name, "ignore") == 0)
112 return;
113 add = alloc_estate_rl(rl);
114 if (old) {
115 new = merge_estates(old, add);
116 } else {
117 new = add;
118 if (ignore)
119 new->name = alloc_string("ignore");
120 else
121 new->name = alloc_string("min-max");
123 set_state_stree(&fn_type_val, my_id, member, NULL, new);
126 static void add_global_type_val(char *member, struct range_list *rl)
128 struct smatch_state *old, *add, *new;
130 member = alloc_string(member);
131 old = get_state_stree(global_type_val, my_id, member, NULL);
132 add = alloc_estate_rl(rl);
133 if (old)
134 new = merge_estates(old, add);
135 else
136 new = add;
137 new = clone_estate_perm(new);
138 set_state_stree_perm(&global_type_val, my_id, member, NULL, new);
141 static int has_link_cb(void *has_link, int argc, char **argv, char **azColName)
143 *(int *)has_link = 1;
144 return 0;
147 static int is_ignored_fake_assignment(void)
149 struct expression *expr;
150 struct symbol *type;
151 char *member_name;
152 int has_link = 0;
154 expr = get_faked_expression();
155 if (!expr || expr->type != EXPR_ASSIGNMENT)
156 return 0;
157 if (!is_void_pointer(expr->right))
158 return 0;
159 member_name = get_member_name(expr->right);
160 if (!member_name)
161 return 0;
163 type = get_type(expr->left);
164 if (!type || type->type != SYM_PTR)
165 return 0;
166 type = get_real_base_type(type);
167 if (!type || type->type != SYM_STRUCT)
168 return 0;
170 run_sql(has_link_cb, &has_link,
171 "select * from data_info where type = %d and data = '%s' and value = '%s';",
172 TYPE_LINK, member_name, type_to_str(type));
173 return has_link;
176 static int is_ignored_macro(void)
178 struct expression *expr;
179 char *name;
181 expr = get_faked_expression();
182 if (!expr || expr->type != EXPR_ASSIGNMENT)
183 return 0;
184 name = get_macro_name(expr->right->pos);
185 if (!name)
186 return 0;
187 if (strcmp(name, "container_of") == 0)
188 return 1;
189 if (strcmp(name, "list_entry") == 0)
190 return 1;
191 if (strstr(name, "for_each"))
192 return 1;
193 return 0;
196 static int is_ignored_function(void)
198 struct expression *expr;
200 expr = get_faked_expression();
201 if (!expr || expr->type != EXPR_ASSIGNMENT)
202 return 0;
203 expr = strip_expr(expr->right);
204 if (!expr || expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
205 return 0;
207 if (sym_name_is("kmalloc", expr->fn))
208 return 1;
210 return 0;
213 static int is_uncasted_function(void)
215 struct expression *expr;
217 expr = get_faked_expression();
218 if (!expr || expr->type != EXPR_ASSIGNMENT)
219 return 0;
220 if (expr->right->type == EXPR_CALL)
221 return 1;
222 return 0;
225 static void match_assign_value(struct expression *expr)
227 char *member, *right_member;
228 struct range_list *rl;
229 struct symbol *type;
231 type = get_type(expr->left);
232 if (type && type->type == SYM_STRUCT)
233 return;
235 member = get_member_name(expr->left);
236 if (!member)
237 return;
239 /* if we're saying foo->mtu = bar->mtu then that doesn't add information */
240 right_member = get_member_name(expr->right);
241 if (right_member && strcmp(right_member, member) == 0)
242 goto free;
244 if (is_fake_call(expr->right)) {
245 if (is_ignored_macro())
246 goto free;
247 if (is_ignored_function())
248 goto free;
249 if (is_uncasted_function())
250 goto free;
251 add_fake_type_val(member, alloc_whole_rl(get_type(expr->left)), is_ignored_fake_assignment());
252 goto free;
255 if (expr->op != '=') {
256 add_type_val(member, alloc_whole_rl(get_type(expr->left)));
257 goto free;
259 get_absolute_rl(expr->right, &rl);
260 rl = cast_rl(type, rl);
261 add_type_val(member, rl);
262 free:
263 free_string(right_member);
264 free_string(member);
268 * If we too: int *p = &my_struct->member then abandon all hope of tracking
269 * my_struct->member.
271 static void match_assign_pointer(struct expression *expr)
273 struct expression *right;
274 char *member;
275 struct range_list *rl;
276 struct symbol *type;
278 right = strip_expr(expr->right);
279 if (right->type != EXPR_PREOP || right->op != '&')
280 return;
281 right = strip_expr(right->unop);
283 member = get_member_name(right);
284 if (!member)
285 return;
286 type = get_type(right);
287 rl = alloc_whole_rl(type);
288 add_type_val(member, rl);
289 free_string(member);
292 static void match_global_assign(struct expression *expr)
294 char *member;
295 struct range_list *rl;
297 member = get_member_name(expr->left);
298 if (!member)
299 return;
300 get_absolute_rl(expr->right, &rl);
301 add_global_type_val(member, rl);
302 free_string(member);
305 static void unop_expr(struct expression *expr)
307 struct range_list *rl;
308 char *member;
310 if (expr->op != SPECIAL_DECREMENT && expr->op != SPECIAL_INCREMENT)
311 return;
313 expr = strip_expr(expr->unop);
314 member = get_member_name(expr);
315 if (!member)
316 return;
317 rl = alloc_whole_rl(get_type(expr));
318 add_type_val(member, rl);
319 free_string(member);
322 static void asm_expr(struct statement *stmt)
324 struct expression *expr;
325 struct range_list *rl;
326 char *member;
327 int state = 0;
329 FOR_EACH_PTR(stmt->asm_outputs, expr) {
330 switch (state) {
331 case 0: /* identifier */
332 case 1: /* constraint */
333 state++;
334 continue;
335 case 2: /* expression */
336 state = 0;
337 member = get_member_name(expr);
338 if (!member)
339 continue;
340 rl = alloc_whole_rl(get_type(expr));
341 add_type_val(member, rl);
342 free_string(member);
343 continue;
345 } END_FOR_EACH_PTR(expr);
348 static void db_param_add(struct expression *expr, int param, char *key, char *value)
350 struct expression *arg;
351 struct symbol *type;
352 struct range_list *rl;
353 char *member;
355 if (strcmp(key, "*$") != 0)
356 return;
358 while (expr->type == EXPR_ASSIGNMENT)
359 expr = strip_expr(expr->right);
360 if (expr->type != EXPR_CALL)
361 return;
363 arg = get_argument_from_call_expr(expr->args, param);
364 arg = strip_expr(arg);
365 if (!arg)
366 return;
367 type = get_member_type_from_key(arg, key);
368 if (arg->type != EXPR_PREOP || arg->op != '&')
369 return;
370 arg = strip_expr(arg->unop);
372 member = get_member_name(arg);
373 if (!member)
374 return;
375 call_results_to_rl(expr, type, value, &rl);
376 add_type_val(member, rl);
377 free_string(member);
380 static void match_end_func_info(struct symbol *sym)
382 struct sm_state *sm;
384 FOR_EACH_SM(fn_type_val, sm) {
385 sql_insert_function_type_value(sm->name, sm->state->name);
386 } END_FOR_EACH_SM(sm);
388 free_stree(&fn_type_val);
391 static void match_end_file(struct symbol_list *sym_list)
393 struct sm_state *sm;
395 FOR_EACH_SM(global_type_val, sm) {
396 sql_insert_function_type_value(sm->name, sm->state->name);
397 } END_FOR_EACH_SM(sm);
400 void register_type_val(int id)
402 if (!option_info)
403 return;
405 my_id = id;
407 add_hook(&match_assign_value, ASSIGNMENT_HOOK);
408 add_hook(&match_assign_pointer, ASSIGNMENT_HOOK);
409 add_hook(&unop_expr, OP_HOOK);
410 add_hook(&asm_expr, ASM_HOOK);
411 select_return_states_hook(ADDED_VALUE, &db_param_add);
412 select_return_states_hook(PARAM_SET, &db_param_add);
415 add_hook(&match_inline_start, INLINE_FN_START);
416 add_hook(&match_inline_end, INLINE_FN_END);
418 add_hook(&match_end_func_info, END_FUNC_HOOK);
420 add_hook(&match_global_assign, GLOBAL_ASSIGNMENT_HOOK);
421 add_hook(&match_end_file, END_FILE_HOOK);