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 struct int_stack
*tracked_stack
;
49 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
))
51 untracked_hook
**p
= malloc(sizeof(untracked_hook
*));
53 add_ptr_list(&untracked_hooks
, p
);
56 static void call_untracked_callbacks(struct expression
*expr
, int param
)
60 FOR_EACH_PTR(untracked_hooks
, fn
) {
62 } END_FOR_EACH_PTR(fn
);
65 static void assume_tracked(struct expression
*call_expr
, int param
, char *key
, char *value
)
70 static void mark_untracked(struct expression
*expr
, int param
, char *key
, char *value
)
75 while (expr
->type
== EXPR_ASSIGNMENT
)
76 expr
= strip_expr(expr
->right
);
77 if (expr
->type
!= EXPR_CALL
)
80 name
= return_state_to_var_sym(expr
, param
, key
, &sym
);
84 call_untracked_callbacks(expr
, param
);
85 set_state(my_id
, name
, sym
, &untracked
);
90 static int lost_in_va_args(struct expression
*expr
)
96 fn
= get_type(expr
->fn
);
97 if (!fn
|| !fn
->variadic
)
101 name
= expr_to_var(expr
->fn
);
102 if (strstr(name
, "print"))
109 static void match_after_call(struct expression
*expr
)
111 struct expression
*arg
;
115 if (lost_in_va_args(expr
))
124 FOR_EACH_PTR(expr
->args
, arg
) {
127 type
= get_type(arg
);
128 if (!type
|| type
->type
!= SYM_PTR
)
131 call_untracked_callbacks(expr
, i
);
132 set_state_expr(my_id
, arg
, &untracked
);
133 } END_FOR_EACH_PTR(arg
);
136 static void print_untracked_params(int return_id
, char *return_ranges
, struct expression
*expr
)
142 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
147 if (!get_state(my_id
, arg
->ident
->name
, arg
) &&
148 !__bail_on_rest_of_function
) /* hairy functions are untrackable */
151 sql_insert_return_states(return_id
, return_ranges
,
152 UNTRACKED_PARAM
, param
, "$", "");
153 } END_FOR_EACH_PTR(arg
);
156 static void match_param_assign(struct expression
*expr
)
158 struct expression
*right
;
162 if (__in_fake_assign
)
165 right
= strip_expr(expr
->right
);
166 type
= get_type(right
);
167 if (!type
|| type
->type
!= SYM_PTR
)
170 param
= get_param_num(right
);
174 set_state_expr(my_id
, right
, &untracked
);
178 static void match_param_assign_in_asm(struct statement
*stmt
)
181 struct expression
*expr
;
186 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
188 case 0: /* identifier */
189 case 1: /* constraint */
192 case 2: /* expression */
195 expr
= strip_expr(expr
);
196 type
= get_type(expr
);
197 if (!type
|| type
->type
!= SYM_PTR
)
199 param
= get_param_num(expr
);
202 set_state_expr(my_id
, expr
, &untracked
);
205 } END_FOR_EACH_PTR(expr
);
208 static void match_inline_start(struct expression
*expr
)
210 push_int(&tracked_stack
, tracked
);
213 static void match_inline_end(struct expression
*expr
)
215 tracked
= pop_int(&tracked_stack
);
218 void register_untracked_param(int id
)
222 select_return_states_hook(INTERNAL
, &assume_tracked
);
223 select_return_states_hook(UNTRACKED_PARAM
, &mark_untracked
);
224 add_hook(&match_after_call
, FUNCTION_CALL_HOOK_AFTER_DB
);
226 add_split_return_callback(&print_untracked_params
);
228 add_hook(&match_param_assign
, ASSIGNMENT_HOOK
);
229 add_hook(&match_param_assign_in_asm
, ASM_HOOK
);
231 add_hook(&match_inline_start
, INLINE_FN_START
);
232 add_hook(&match_inline_end
, INLINE_FN_END
);