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:
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.
35 #include "smatch_slist.h"
36 #include "smatch_extra.h"
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
*));
55 add_ptr_list(&untracked_hooks
, p
);
58 static void call_untracked_callbacks(struct expression
*expr
, int param
)
62 FOR_EACH_PTR(untracked_hooks
, fn
) {
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
*));
71 add_ptr_list(&lost_hooks
, p
);
74 static void call_lost_callbacks(struct expression
*expr
, int param
)
78 FOR_EACH_PTR(lost_hooks
, fn
) {
80 } END_FOR_EACH_PTR(fn
);
83 static void assume_tracked(struct expression
*call_expr
, int param
, char *key
, char *value
)
88 static char *get_array_from_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
90 struct expression
*arg
;
92 arg
= get_argument_from_call_expr(expr
->args
, param
);
95 if (arg
->type
!= EXPR_PREOP
|| arg
->op
!= '&')
100 arg
= get_array_base(arg
);
102 return expr_to_var_sym(arg
, sym
);
105 static void mark_untracked_lost(struct expression
*expr
, int param
, const char *key
, int type
)
110 while (expr
->type
== EXPR_ASSIGNMENT
)
111 expr
= strip_expr(expr
->right
);
112 if (expr
->type
!= EXPR_CALL
)
115 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
117 name
= get_array_from_key(expr
, param
, key
, &sym
);
122 if (type
== LOST_PARAM
)
123 call_lost_callbacks(expr
, param
);
124 call_untracked_callbacks(expr
, param
);
125 set_state(my_id
, name
, sym
, &untracked
);
131 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
)
133 mark_untracked_lost(expr
, param
, key
, UNTRACKED_PARAM
);
136 void mark_lost(struct expression
*expr
, int param
, const char *key
, const char *value
)
138 mark_untracked_lost(expr
, param
, key
, LOST_PARAM
);
141 static int lost_in_va_args(struct expression
*expr
)
147 fn
= get_type(expr
->fn
);
148 if (!fn
|| !fn
->variadic
)
152 name
= expr_to_var(expr
->fn
);
153 if (name
&& strstr(name
, "print"))
160 static void match_after_call(struct expression
*expr
)
162 struct expression
*arg
;
166 if (lost_in_va_args(expr
))
175 FOR_EACH_PTR(expr
->args
, arg
) {
178 type
= get_type(arg
);
179 if (!type
|| type
->type
!= SYM_PTR
)
182 call_untracked_callbacks(expr
, i
);
183 set_state_expr(my_id
, arg
, &untracked
);
184 } END_FOR_EACH_PTR(arg
);
188 static void mark_all_params(int return_id
, char *return_ranges
, int type
)
194 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
199 sql_insert_return_states(return_id
, return_ranges
,
200 type
, param
, "$", "");
201 } END_FOR_EACH_PTR(arg
);
205 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
)
207 mark_all_params(return_id
, return_ranges
, UNTRACKED_PARAM
);
210 void mark_all_params_lost(int return_id
, char *return_ranges
, struct expression
*expr
)
212 mark_all_params(return_id
, return_ranges
, LOST_PARAM
);
215 static void print_untracked_params(int return_id
, char *return_ranges
, struct expression
*expr
)
223 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
229 if (__bail_on_rest_of_function
) {
230 /* hairy functions are lost */
232 } else if ((sm
= get_sm_state(my_id
, arg
->ident
->name
, arg
))) {
233 if (slist_has_state(sm
->possible
, &lost
))
236 type
= UNTRACKED_PARAM
;
241 sql_insert_return_states(return_id
, return_ranges
,
242 type
, param
, "$", "");
243 } END_FOR_EACH_PTR(arg
);
246 static void match_param_assign(struct expression
*expr
)
248 struct expression
*right
;
252 if (__in_fake_assign
)
255 right
= strip_expr(expr
->right
);
256 type
= get_type(right
);
257 if (!type
|| type
->type
!= SYM_PTR
)
260 param
= get_param_num(right
);
264 set_state_expr(my_id
, right
, &untracked
);
268 static void match_param_assign_in_asm(struct statement
*stmt
)
271 struct expression
*tmp
, *expr
;
275 FOR_EACH_PTR(stmt
->asm_inputs
, tmp
) {
276 expr
= strip_expr(tmp
->expr
);
277 type
= get_type(expr
);
278 if (!type
|| type
->type
!= SYM_PTR
)
280 param
= get_param_num(expr
);
283 set_state_expr(my_id
, expr
, &untracked
);
284 } END_FOR_EACH_PTR(tmp
);
287 static void match_inline_start(struct expression
*expr
)
289 push_int(&tracked_stack
, tracked
);
292 static void match_inline_end(struct expression
*expr
)
294 tracked
= pop_int(&tracked_stack
);
297 void register_untracked_param(int id
)
301 select_return_states_hook(INTERNAL
, &assume_tracked
);
302 select_return_states_hook(UNTRACKED_PARAM
, &mark_untracked
);
303 select_return_states_hook(LOST_PARAM
, &mark_lost
);
304 add_hook(&match_after_call
, FUNCTION_CALL_HOOK_AFTER_DB
);
306 add_split_return_callback(&print_untracked_params
);
308 add_hook(&match_param_assign
, ASSIGNMENT_HOOK
);
309 add_hook(&match_param_assign_in_asm
, ASM_HOOK
);
311 add_hook(&match_inline_start
, INLINE_FN_START
);
312 add_hook(&match_inline_end
, INLINE_FN_END
);