math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_untracked_param.c
blobcd65e46ef122a38931c743731d8aaef33ddfc18b
1 /*
2 * Copyright (C) 2014 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 * Sometimes we aren't able to track a variable through a function call. This
20 * usually happens because a function changes too many variables so we give up.
21 * Another reason this happens is because we call a function pointer and there
22 * are too many functions which implement that function pointer so we give up.
23 * Also maybe we don't have the database enabled.
25 * The goal here is to make a call back so what if we call:
27 * frob(&foo);
29 * but we're not able to say what happens to "foo", then let's assume that we
30 * don't know anything about "foo" if it's an untracked call.
34 #include "smatch.h"
35 #include "smatch_slist.h"
36 #include "smatch_extra.h"
38 static int my_id;
39 static int tracked;
41 STATE(untracked);
42 STATE(lost);
44 typedef void (untracked_hook)(struct expression *call, int param);
45 DECLARE_PTR_LIST(untracked_hook_list, untracked_hook *);
46 static struct untracked_hook_list *untracked_hooks;
47 static struct untracked_hook_list *lost_hooks;
49 struct int_stack *tracked_stack;
51 void add_untracked_param_hook(void (func)(struct expression *call, int param))
53 untracked_hook **p = malloc(sizeof(untracked_hook *));
54 *p = func;
55 add_ptr_list(&untracked_hooks, p);
58 static void call_untracked_callbacks(struct expression *expr, int param)
60 untracked_hook **fn;
62 FOR_EACH_PTR(untracked_hooks, fn) {
63 (*fn)(expr, param);
64 } END_FOR_EACH_PTR(fn);
67 void add_lost_param_hook(void (func)(struct expression *call, int param))
69 untracked_hook **p = malloc(sizeof(untracked_hook *));
70 *p = func;
71 add_ptr_list(&lost_hooks, p);
74 static void call_lost_callbacks(struct expression *expr, int param)
76 untracked_hook **fn;
78 FOR_EACH_PTR(lost_hooks, fn) {
79 (*fn)(expr, param);
80 } END_FOR_EACH_PTR(fn);
83 static void assume_tracked(struct expression *call_expr, int param, char *key, char *value)
85 tracked = 1;
88 static void mark_untracked_lost(struct expression *expr, int param, const char *key, int type)
90 char *name;
91 struct symbol *sym;
93 while (expr->type == EXPR_ASSIGNMENT)
94 expr = strip_expr(expr->right);
95 if (expr->type != EXPR_CALL)
96 return;
98 name = return_state_to_var_sym(expr, param, key, &sym);
99 if (!name || !sym)
100 goto free;
102 if (type == LOST_PARAM)
103 call_lost_callbacks(expr, param);
104 call_untracked_callbacks(expr, param);
105 set_state(my_id, name, sym, &untracked);
106 free:
107 free_string(name);
111 void mark_untracked(struct expression *expr, int param, const char *key, const char *value)
113 mark_untracked_lost(expr, param, key, UNTRACKED_PARAM);
116 void mark_lost(struct expression *expr, int param, const char *key, const char *value)
118 mark_untracked_lost(expr, param, key, LOST_PARAM);
121 static int lost_in_va_args(struct expression *expr)
123 struct symbol *fn;
124 char *name;
125 int is_lost;
127 fn = get_type(expr->fn);
128 if (!fn || !fn->variadic)
129 return 0;
131 is_lost = 1;
132 name = expr_to_var(expr->fn);
133 if (name && strstr(name, "print"))
134 is_lost = 0;
135 free_string(name);
137 return is_lost;
140 static void match_after_call(struct expression *expr)
142 struct expression *arg;
143 struct symbol *type;
144 int i;
146 if (lost_in_va_args(expr))
147 tracked = 0;
149 if (tracked) {
150 tracked = 0;
151 return;
154 i = -1;
155 FOR_EACH_PTR(expr->args, arg) {
156 i++;
158 type = get_type(arg);
159 if (!type || type->type != SYM_PTR)
160 continue;
162 call_untracked_callbacks(expr, i);
163 set_state_expr(my_id, arg, &untracked);
164 } END_FOR_EACH_PTR(arg);
168 static void mark_all_params(int return_id, char *return_ranges, int type)
170 struct symbol *arg;
171 int param;
173 param = -1;
174 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
175 param++;
177 if (!arg->ident)
178 continue;
179 sql_insert_return_states(return_id, return_ranges,
180 type, param, "$", "");
181 } END_FOR_EACH_PTR(arg);
185 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr)
187 mark_all_params(return_id, return_ranges, UNTRACKED_PARAM);
190 void mark_all_params_lost(int return_id, char *return_ranges, struct expression *expr)
192 mark_all_params(return_id, return_ranges, LOST_PARAM);
195 static void print_untracked_params(int return_id, char *return_ranges, struct expression *expr)
197 struct sm_state *sm;
198 struct symbol *arg;
199 int param;
200 int type;
202 param = -1;
203 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
204 param++;
206 if (!arg->ident)
207 continue;
209 if (__bail_on_rest_of_function) {
210 /* hairy functions are lost */
211 type = LOST_PARAM;
212 } else if ((sm = get_sm_state(my_id, arg->ident->name, arg))) {
213 if (slist_has_state(sm->possible, &lost))
214 type = LOST_PARAM;
215 else
216 type = UNTRACKED_PARAM;
217 } else {
218 continue;
221 sql_insert_return_states(return_id, return_ranges,
222 type, param, "$", "");
223 } END_FOR_EACH_PTR(arg);
226 static void match_param_assign(struct expression *expr)
228 struct expression *right;
229 struct symbol *type;
230 int param;
232 if (__in_fake_assign)
233 return;
235 right = strip_expr(expr->right);
236 type = get_type(right);
237 if (!type || type->type != SYM_PTR)
238 return;
240 param = get_param_num(right);
241 if (param < 0)
242 return;
244 set_state_expr(my_id, right, &untracked);
248 static void match_param_assign_in_asm(struct statement *stmt)
251 struct expression *expr;
252 struct symbol *type;
253 int state = 0;
254 int param;
256 FOR_EACH_PTR(stmt->asm_inputs, expr) {
257 switch (state) {
258 case 0: /* identifier */
259 case 1: /* constraint */
260 state++;
261 continue;
262 case 2: /* expression */
263 state = 0;
265 expr = strip_expr(expr);
266 type = get_type(expr);
267 if (!type || type->type != SYM_PTR)
268 continue;
269 param = get_param_num(expr);
270 if (param < 0)
271 continue;
272 set_state_expr(my_id, expr, &untracked);
273 continue;
275 } END_FOR_EACH_PTR(expr);
278 static void match_inline_start(struct expression *expr)
280 push_int(&tracked_stack, tracked);
283 static void match_inline_end(struct expression *expr)
285 tracked = pop_int(&tracked_stack);
288 void register_untracked_param(int id)
290 my_id = id;
292 select_return_states_hook(INTERNAL, &assume_tracked);
293 select_return_states_hook(UNTRACKED_PARAM, &mark_untracked);
294 select_return_states_hook(LOST_PARAM, &mark_lost);
295 add_hook(&match_after_call, FUNCTION_CALL_HOOK_AFTER_DB);
297 add_split_return_callback(&print_untracked_params);
299 add_hook(&match_param_assign, ASSIGNMENT_HOOK);
300 add_hook(&match_param_assign_in_asm, ASM_HOOK);
302 add_hook(&match_inline_start, INLINE_FN_START);
303 add_hook(&match_inline_end, INLINE_FN_END);