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"
43 typedef void (untracked_hook
)(struct expression
*call
, int param
);
44 DECLARE_PTR_LIST(untracked_hook_list
, untracked_hook
*);
45 static struct untracked_hook_list
*untracked_hooks
;
47 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
))
49 untracked_hook
**p
= malloc(sizeof(untracked_hook
*));
51 add_ptr_list(&untracked_hooks
, p
);
54 static void call_untracked_callbacks(struct expression
*expr
, int param
)
58 FOR_EACH_PTR(untracked_hooks
, fn
) {
60 } END_FOR_EACH_PTR(fn
);
63 static void assume_tracked(struct expression
*call_expr
, int param
, char *key
, char *value
)
68 static void mark_untracked(struct expression
*expr
, int param
, char *key
, char *value
)
73 while (expr
->type
== EXPR_ASSIGNMENT
)
74 expr
= strip_expr(expr
->right
);
75 if (expr
->type
!= EXPR_CALL
)
78 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
82 call_untracked_callbacks(expr
, param
);
83 set_state(my_id
, name
, sym
, &untracked
);
88 static int lost_in_va_args(struct expression
*expr
)
94 fn
= get_type(expr
->fn
);
95 if (!fn
|| !fn
->variadic
)
99 name
= expr_to_var(expr
->fn
);
100 if (strstr(name
, "print"))
107 static void match_after_call(struct expression
*expr
)
109 struct expression
*arg
;
114 if (lost_in_va_args(expr
))
123 FOR_EACH_PTR(expr
->args
, arg
) {
126 type
= get_type(arg
);
127 if (!type
|| type
->type
!= SYM_PTR
)
130 call_untracked_callbacks(expr
, i
);
131 set_state_expr(my_id
, arg
, &untracked
);
132 } END_FOR_EACH_PTR(arg
);
135 static void print_untracked_params(int return_id
, char *return_ranges
, struct expression
*expr
)
141 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
146 if (!get_state(my_id
, arg
->ident
->name
, arg
))
149 sql_insert_return_states(return_id
, return_ranges
,
150 UNTRACKED_PARAM
, param
, "$", "");
151 } END_FOR_EACH_PTR(arg
);
154 static void match_param_assign(struct expression
*expr
)
156 struct expression
*right
;
160 if (__in_fake_assign
)
163 right
= strip_expr(expr
->right
);
164 type
= get_type(right
);
165 if (!type
|| type
->type
!= SYM_PTR
)
168 param
= get_param_num(right
);
172 set_state_expr(my_id
, right
, &untracked
);
176 static void match_param_assign_in_asm(struct statement
*stmt
)
179 struct expression
*expr
;
184 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
186 case 0: /* identifier */
187 case 1: /* constraint */
190 case 2: /* expression */
193 expr
= strip_expr(expr
);
194 type
= get_type(expr
);
195 if (!type
|| type
->type
!= SYM_PTR
)
197 param
= get_param_num(expr
);
200 set_state_expr(my_id
, expr
, &untracked
);
203 } END_FOR_EACH_PTR(expr
);
206 void register_untracked_param(int id
)
210 select_return_states_hook(INTERNAL
, &assume_tracked
);
211 select_return_states_hook(UNTRACKED_PARAM
, &mark_untracked
);
212 add_hook(&match_after_call
, FUNCTION_CALL_HOOK_AFTER
);
214 add_split_return_callback(&print_untracked_params
);
216 add_hook(&match_param_assign
, ASSIGNMENT_HOOK
);
217 add_hook(&match_param_assign_in_asm
, ASM_HOOK
);