2 * Copyright (C) 2009 Dan Carpenter.
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 * There are several types of function hooks:
20 * add_function_hook() - For any time a function is called.
21 * add_function_assign_hook() - foo = the_function().
22 * add_implied_return_hook() - Calculates the implied return value.
23 * add_macro_assign_hook() - foo = the_macro().
24 * return_implies_state() - For when a return value of 1 implies locked
25 * and 0 implies unlocked. etc. etc.
32 #include "smatch_slist.h"
33 #include "smatch_extra.h"
34 #include "smatch_function_hashtable.h"
38 struct data_range
*range
;
41 implication_hook
*ranged
;
42 implied_return_hook
*implied_return
;
47 ALLOCATOR(fcall_back
, "call backs");
48 DECLARE_PTR_LIST(call_back_list
, struct fcall_back
);
50 DEFINE_FUNCTION_HASHTABLE_STATIC(callback
, struct fcall_back
, struct call_back_list
);
51 static struct hashtable
*func_hash
;
53 int __in_fake_parameter_assign
;
55 #define REGULAR_CALL 0
57 #define RANGED_EXACT 2
59 #define IMPLIED_RETURN 4
60 #define MACRO_ASSIGN 5
61 #define MACRO_ASSIGN_EXTRA 6
63 struct return_implies_callback
{
65 return_implies_hook
*callback
;
67 ALLOCATOR(return_implies_callback
, "return_implies callbacks");
68 DECLARE_PTR_LIST(db_implies_list
, struct return_implies_callback
);
69 static struct db_implies_list
*db_return_states_list
;
71 typedef void (void_fn
)(void);
72 DECLARE_PTR_LIST(void_fn_list
, void_fn
*);
73 static struct void_fn_list
*return_states_before
;
74 static struct void_fn_list
*return_states_after
;
76 static struct fcall_back
*alloc_fcall_back(int type
, void *call_back
,
79 struct fcall_back
*cb
;
81 cb
= __alloc_fcall_back(0);
83 cb
->u
.call_back
= call_back
;
88 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *info
)
90 struct fcall_back
*cb
;
92 cb
= alloc_fcall_back(REGULAR_CALL
, call_back
, info
);
93 add_callback(func_hash
, look_for
, cb
);
96 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
99 struct fcall_back
*cb
;
101 cb
= alloc_fcall_back(ASSIGN_CALL
, call_back
, info
);
102 add_callback(func_hash
, look_for
, cb
);
105 static void register_funcs_from_file_helper(const char *file
,
106 func_hook
*call_back
, void *info
,
113 snprintf(name
, sizeof(name
), "%s.%s", option_project_str
, file
);
114 token
= get_tokens_file(name
);
117 if (token_type(token
) != TOKEN_STREAMBEGIN
)
120 while (token_type(token
) != TOKEN_STREAMEND
) {
121 if (token_type(token
) != TOKEN_IDENT
)
123 func
= show_ident(token
->ident
);
125 add_function_assign_hook(func
, call_back
, info
);
127 add_function_hook(func
, call_back
, info
);
133 void register_func_hooks_from_file(const char *file
,
134 func_hook
*call_back
, void *info
)
136 register_funcs_from_file_helper(file
, call_back
, info
, false);
139 void register_assign_hooks_from_file(const char *file
,
140 func_hook
*call_back
, void *info
)
142 register_funcs_from_file_helper(file
, call_back
, info
, true);
145 void add_implied_return_hook(const char *look_for
,
146 implied_return_hook
*call_back
,
149 struct fcall_back
*cb
;
151 cb
= alloc_fcall_back(IMPLIED_RETURN
, call_back
, info
);
152 add_callback(func_hash
, look_for
, cb
);
155 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
158 struct fcall_back
*cb
;
160 cb
= alloc_fcall_back(MACRO_ASSIGN
, call_back
, info
);
161 add_callback(func_hash
, look_for
, cb
);
164 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
167 struct fcall_back
*cb
;
169 cb
= alloc_fcall_back(MACRO_ASSIGN_EXTRA
, call_back
, info
);
170 add_callback(func_hash
, look_for
, cb
);
173 void return_implies_state(const char *look_for
, long long start
, long long end
,
174 implication_hook
*call_back
, void *info
)
176 struct fcall_back
*cb
;
178 cb
= alloc_fcall_back(RANGED_CALL
, call_back
, info
);
179 cb
->range
= alloc_range_perm(ll_to_sval(start
), ll_to_sval(end
));
180 add_callback(func_hash
, look_for
, cb
);
183 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
184 implication_hook
*call_back
, void *info
)
186 struct fcall_back
*cb
;
188 cb
= alloc_fcall_back(RANGED_CALL
, call_back
, info
);
189 cb
->range
= alloc_range_perm(start
, end
);
190 add_callback(func_hash
, look_for
, cb
);
193 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
194 implication_hook
*call_back
, void *info
)
196 struct fcall_back
*cb
;
198 cb
= alloc_fcall_back(RANGED_EXACT
, call_back
, info
);
199 cb
->range
= alloc_range_perm(start
, end
);
200 add_callback(func_hash
, look_for
, cb
);
203 void select_return_states_hook(int type
, return_implies_hook
*callback
)
205 struct return_implies_callback
*cb
= __alloc_return_implies_callback(0);
208 cb
->callback
= callback
;
209 add_ptr_list(&db_return_states_list
, cb
);
212 void select_return_states_before(void_fn
*fn
)
214 void_fn
**p
= malloc(sizeof(void_fn
*));
216 add_ptr_list(&return_states_before
, p
);
219 void select_return_states_after(void_fn
*fn
)
221 void_fn
**p
= malloc(sizeof(void_fn
*));
223 add_ptr_list(&return_states_after
, p
);
226 static void call_return_states_before_hooks(void)
230 FOR_EACH_PTR(return_states_before
, fn
) {
232 } END_FOR_EACH_PTR(fn
);
235 static void call_return_states_after_hooks(struct expression
*expr
)
239 FOR_EACH_PTR(return_states_after
, fn
) {
241 } END_FOR_EACH_PTR(fn
);
242 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER_DB
);
245 static int call_call_backs(struct call_back_list
*list
, int type
,
246 const char *fn
, struct expression
*expr
)
248 struct fcall_back
*tmp
;
251 FOR_EACH_PTR(list
, tmp
) {
252 if (tmp
->type
== type
) {
253 (tmp
->u
.call_back
)(fn
, expr
, tmp
->info
);
256 } END_FOR_EACH_PTR(tmp
);
261 static void call_ranged_call_backs(struct call_back_list
*list
,
262 const char *fn
, struct expression
*call_expr
,
263 struct expression
*assign_expr
)
265 struct fcall_back
*tmp
;
267 FOR_EACH_PTR(list
, tmp
) {
268 (tmp
->u
.ranged
)(fn
, call_expr
, assign_expr
, tmp
->info
);
269 } END_FOR_EACH_PTR(tmp
);
272 static struct call_back_list
*get_same_ranged_call_backs(struct call_back_list
*list
,
273 struct data_range
*drange
)
275 struct call_back_list
*ret
= NULL
;
276 struct fcall_back
*tmp
;
278 FOR_EACH_PTR(list
, tmp
) {
279 if (tmp
->type
!= RANGED_CALL
&&
280 tmp
->type
!= RANGED_EXACT
)
282 if (ranges_equiv(tmp
->range
, drange
))
283 add_ptr_list(&ret
, tmp
);
284 } END_FOR_EACH_PTR(tmp
);
288 static int in_list_exact_sval(struct range_list
*list
, struct data_range
*drange
)
290 struct data_range
*tmp
;
292 FOR_EACH_PTR(list
, tmp
) {
293 if (ranges_equiv(tmp
, drange
))
295 } END_FOR_EACH_PTR(tmp
);
299 static int assign_ranged_funcs(const char *fn
, struct expression
*expr
,
300 struct call_back_list
*call_backs
)
302 struct fcall_back
*tmp
;
306 struct smatch_state
*estate
;
307 struct stree
*tmp_stree
;
308 struct stree
*final_states
= NULL
;
309 struct range_list
*handled_ranges
= NULL
;
310 struct call_back_list
*same_range_call_backs
= NULL
;
311 struct range_list
*rl
;
317 var_name
= expr_to_var_sym(expr
->left
, &sym
);
318 if (!var_name
|| !sym
)
321 FOR_EACH_PTR(call_backs
, tmp
) {
322 if (tmp
->type
!= RANGED_CALL
&&
323 tmp
->type
!= RANGED_EXACT
)
326 if (in_list_exact_sval(handled_ranges
, tmp
->range
))
328 __push_fake_cur_stree();
329 tack_on(&handled_ranges
, tmp
->range
);
331 same_range_call_backs
= get_same_ranged_call_backs(call_backs
, tmp
->range
);
332 call_ranged_call_backs(same_range_call_backs
, fn
, expr
->right
, expr
);
333 __free_ptr_list((struct ptr_list
**)&same_range_call_backs
);
335 rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
336 rl
= cast_rl(get_type(expr
->left
), rl
);
337 estate
= alloc_estate_rl(rl
);
338 set_extra_mod(var_name
, sym
, expr
->left
, estate
);
340 tmp_stree
= __pop_fake_cur_stree();
341 merge_fake_stree(&final_states
, tmp_stree
);
342 free_stree(&tmp_stree
);
344 } END_FOR_EACH_PTR(tmp
);
346 FOR_EACH_SM(final_states
, sm
) {
348 } END_FOR_EACH_SM(sm
);
350 free_stree(&final_states
);
352 free_string(var_name
);
356 static void call_implies_callbacks(int comparison
, struct expression
*expr
, sval_t sval
, int left
, struct stree
**implied_true
, struct stree
**implied_false
)
358 struct call_back_list
*call_backs
;
359 struct fcall_back
*tmp
;
361 struct data_range
*value_range
;
362 struct stree
*true_states
= NULL
;
363 struct stree
*false_states
= NULL
;
364 struct stree
*tmp_stree
;
366 *implied_true
= NULL
;
367 *implied_false
= NULL
;
368 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
370 fn
= expr
->fn
->symbol
->ident
->name
;
371 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
374 value_range
= alloc_range(sval
, sval
);
376 /* set true states */
377 __push_fake_cur_stree();
378 FOR_EACH_PTR(call_backs
, tmp
) {
379 if (tmp
->type
!= RANGED_CALL
&&
380 tmp
->type
!= RANGED_EXACT
)
382 if (!true_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
384 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
385 } END_FOR_EACH_PTR(tmp
);
386 tmp_stree
= __pop_fake_cur_stree();
387 merge_fake_stree(&true_states
, tmp_stree
);
388 free_stree(&tmp_stree
);
390 /* set false states */
391 __push_fake_cur_stree();
392 FOR_EACH_PTR(call_backs
, tmp
) {
393 if (tmp
->type
!= RANGED_CALL
&&
394 tmp
->type
!= RANGED_EXACT
)
396 if (!false_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
398 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
399 } END_FOR_EACH_PTR(tmp
);
400 tmp_stree
= __pop_fake_cur_stree();
401 merge_fake_stree(&false_states
, tmp_stree
);
402 free_stree(&tmp_stree
);
404 *implied_true
= true_states
;
405 *implied_false
= false_states
;
408 struct db_callback_info
{
411 struct expression
*expr
;
412 struct range_list
*rl
;
415 struct stree
*implied
;
416 struct db_implies_list
*callbacks
;
421 struct smatch_state
*ret_state
;
422 struct expression
*var_expr
;
426 static void set_implied_states(struct db_callback_info
*db_info
)
430 FOR_EACH_SM(db_info
->implied
, sm
) {
432 } END_FOR_EACH_SM(sm
);
434 free_stree(&db_info
->implied
);
437 static void store_return_state(struct db_callback_info
*db_info
, const char *ret_str
, struct smatch_state
*state
)
439 db_info
->ret_str
= alloc_sname(ret_str
),
440 db_info
->ret_state
= state
;
443 static bool fake_a_param_assignment(struct expression
*expr
, const char *ret_str
, struct smatch_state
*orig
)
445 struct expression
*arg
, *left
, *right
, *tmp
, *fake_assign
;
451 if (expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
456 while (right
->type
== EXPR_ASSIGNMENT
)
457 right
= strip_expr(right
->right
);
458 if (!right
|| right
->type
!= EXPR_CALL
)
461 p
= strchr(ret_str
, '[');
466 if (p
[0] == '=' && p
[1] == '=')
471 snprintf(buf
, sizeof(buf
), "%s", p
);
475 param
= strtol(p
, &p
, 10);
482 arg
= get_argument_from_call_expr(right
->args
, param
);
486 /* There should be a get_other_name() function which returns an expr */
487 tmp
= get_assigned_expr(arg
);
492 * This is a sanity check to prevent side effects from evaluating stuff
495 str
= expr_to_chunk_sym_vsl(arg
, NULL
, NULL
);
500 right
= gen_expression_from_key(arg
, buf
);
501 if (!right
) /* Mostly fails for binops like [$0 + 4032] */
503 fake_assign
= assign_expression(left
, '=', right
);
504 __in_fake_parameter_assign
++;
505 __split_expr(fake_assign
);
506 __in_fake_parameter_assign
--;
509 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
510 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
511 * parts have to be considered. We use _nomod() because it's not really
512 * another modification, it's just a clarification.
515 if (estate_rl(orig
)) {
516 struct smatch_state
*faked
;
517 struct range_list
*rl
;
519 faked
= get_extra_state(left
);
520 if (estate_rl(faked
)) {
521 rl
= rl_intersection(estate_rl(faked
), estate_rl(orig
));
523 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
530 static void set_fresh_mtag_returns(struct db_callback_info
*db_info
)
532 struct expression
*expr
= db_info
->expr
->left
;
533 struct smatch_state
*state
;
535 if (!db_info
->ret_state
)
538 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
539 state
= get_mtag_return(db_info
->expr
, state
);
543 set_real_absolute(expr
, state
);
544 set_extra_expr_mod(expr
, state
);
546 db_info
->ret_state
= NULL
;
547 db_info
->ret_str
= NULL
;
550 static void set_return_assign_state(struct db_callback_info
*db_info
)
552 struct expression
*expr
= db_info
->expr
->left
;
553 struct smatch_state
*state
;
555 if (!db_info
->ret_state
)
558 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
559 if (!fake_a_param_assignment(db_info
->expr
, db_info
->ret_str
, state
))
560 set_extra_expr_mod(expr
, state
);
562 db_info
->ret_state
= NULL
;
563 db_info
->ret_str
= NULL
;
566 static void set_other_side_state(struct db_callback_info
*db_info
)
568 struct expression
*expr
= db_info
->var_expr
;
569 struct smatch_state
*state
;
571 if (!db_info
->ret_state
)
574 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
575 set_extra_expr_nomod(expr
, state
);
576 db_info
->ret_state
= NULL
;
577 db_info
->ret_str
= NULL
;
580 static void handle_ret_equals_param(char *ret_string
, struct range_list
*rl
, struct expression
*call
)
584 struct expression
*arg
;
585 struct range_list
*orig
;
587 str
= strstr(ret_string
, "==$");
591 param
= strtoll(str
, NULL
, 10);
592 arg
= get_argument_from_call_expr(call
->args
, param
);
595 get_absolute_rl(arg
, &orig
);
596 rl
= rl_intersection(orig
, rl
);
599 set_extra_expr_nomod(arg
, alloc_estate_rl(rl
));
602 static int impossible_limit(struct expression
*expr
, int param
, char *key
, char *value
)
604 struct expression
*arg
;
605 struct smatch_state
*state
;
606 struct range_list
*passed
;
607 struct range_list
*limit
;
608 struct symbol
*compare_type
;
610 while (expr
->type
== EXPR_ASSIGNMENT
)
611 expr
= strip_expr(expr
->right
);
612 if (expr
->type
!= EXPR_CALL
)
615 arg
= get_argument_from_call_expr(expr
->args
, param
);
619 if (strcmp(key
, "$") == 0) {
620 if (!get_implied_rl(arg
, &passed
))
623 compare_type
= get_arg_type(expr
->fn
, param
);
628 name
= get_variable_from_key(arg
, key
, &sym
);
632 state
= get_state(SMATCH_EXTRA
, name
, sym
);
637 passed
= estate_rl(state
);
638 if (!passed
|| is_whole_rl(passed
)) {
643 compare_type
= get_member_type_from_key(arg
, key
);
646 passed
= cast_rl(compare_type
, passed
);
647 call_results_to_rl(expr
, compare_type
, value
, &limit
);
648 if (!limit
|| is_whole_rl(limit
))
650 if (possibly_true_rl(passed
, SPECIAL_EQUAL
, limit
))
652 if (option_debug
|| local_debug
)
653 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param
, key
, show_rl(passed
), value
);
657 static int is_impossible_data(int type
, struct expression
*expr
, int param
, char *key
, char *value
)
659 if (type
== PARAM_LIMIT
&& impossible_limit(expr
, param
, key
, value
))
661 if (type
== COMPARE_LIMIT
&& param_compare_limit_is_impossible(expr
, param
, key
, value
)) {
663 sm_msg("param_compare_limit_is_impossible: %d %s %s", param
, key
, value
);
669 static int func_type_mismatch(struct expression
*expr
, const char *value
)
673 /* This makes faking returns easier */
674 if (!value
|| value
[0] == '\0')
677 while (expr
->type
== EXPR_ASSIGNMENT
)
678 expr
= strip_expr(expr
->right
);
681 * Short cut: We only care about function pointers that are struct
685 if (expr
->fn
->type
== EXPR_SYMBOL
)
688 type
= get_type(expr
->fn
);
691 if (type
->type
== SYM_PTR
)
692 type
= get_real_base_type(type
);
694 if (strcmp(type_to_str(type
), value
) == 0)
700 static int db_compare_callback(void *_info
, int argc
, char **argv
, char **azColName
)
702 struct db_callback_info
*db_info
= _info
;
703 struct range_list
*var_rl
= db_info
->rl
;
704 struct range_list
*ret_range
;
706 char *ret_str
, *key
, *value
;
707 struct return_implies_callback
*tmp
;
715 return_id
= atoi(argv
[0]);
717 type
= atoi(argv
[2]);
718 param
= atoi(argv
[3]);
722 db_info
->has_states
= 1;
723 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
724 set_other_side_state(db_info
);
725 set_implied_states(db_info
);
726 stree
= __pop_fake_cur_stree();
728 merge_fake_stree(&db_info
->stree
, stree
);
730 __push_fake_cur_stree();
733 db_info
->prev_return_id
= return_id
;
735 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
739 if (type
== CULL_PATH
) {
744 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
749 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), ret_str
, &ret_range
);
750 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
752 ret_range
= alloc_whole_rl(get_type(db_info
->expr
));
754 comparison
= db_info
->comparison
;
756 comparison
= flip_comparison(comparison
);
758 if (db_info
->true_side
) {
759 if (!possibly_true_rl(var_rl
, comparison
, ret_range
))
761 if (type
== PARAM_LIMIT
)
762 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
763 else if (type
> PARAM_LIMIT
)
764 set_implied_states(db_info
);
765 filter_by_comparison(&var_rl
, comparison
, ret_range
);
766 filter_by_comparison(&ret_range
, flip_comparison(comparison
), var_rl
);
768 if (!possibly_false_rl(var_rl
, comparison
, ret_range
))
770 if (type
== PARAM_LIMIT
)
771 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
772 else if (type
> PARAM_LIMIT
)
773 set_implied_states(db_info
);
774 filter_by_comparison(&var_rl
, negate_comparison(comparison
), ret_range
);
775 filter_by_comparison(&ret_range
, flip_comparison(negate_comparison(comparison
)), var_rl
);
778 handle_ret_equals_param(ret_str
, ret_range
, db_info
->expr
);
780 if (type
== INTERNAL
) {
781 set_state(-1, "unnull_path", NULL
, &true_state
);
782 __add_return_comparison(strip_expr(db_info
->expr
), ret_str
);
783 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
784 store_return_state(db_info
, ret_str
, alloc_estate_rl(clone_rl(var_rl
)));
787 FOR_EACH_PTR(db_info
->callbacks
, tmp
) {
788 if (tmp
->type
== type
)
789 tmp
->callback(db_info
->expr
, param
, key
, value
);
790 } END_FOR_EACH_PTR(tmp
);
795 static void compare_db_return_states_callbacks(struct expression
*left
, int comparison
, struct expression
*right
, struct stree
*implied_true
, struct stree
*implied_false
)
797 struct stree
*orig_states
;
799 struct stree
*true_states
;
800 struct stree
*false_states
;
802 struct db_callback_info db_info
= {};
803 struct expression
*var_expr
;
804 struct expression
*call_expr
;
805 struct range_list
*rl
;
808 orig_states
= clone_stree(__get_cur_stree());
810 /* legacy cruft. need to fix call_implies_callbacks(). */
814 if (left
->type
!= EXPR_CALL
) {
820 get_absolute_rl(var_expr
, &rl
);
822 db_info
.comparison
= comparison
;
823 db_info
.expr
= call_expr
;
825 db_info
.left
= call_on_left
;
826 db_info
.callbacks
= db_return_states_list
;
827 db_info
.var_expr
= var_expr
;
829 call_return_states_before_hooks();
831 db_info
.true_side
= 1;
832 db_info
.stree
= NULL
;
833 db_info
.prev_return_id
= -1;
834 __push_fake_cur_stree();
835 sql_select_return_states("return_id, return, type, parameter, key, value",
836 call_expr
, db_compare_callback
, &db_info
);
837 set_other_side_state(&db_info
);
838 set_implied_states(&db_info
);
839 stree
= __pop_fake_cur_stree();
841 merge_fake_stree(&db_info
.stree
, stree
);
843 true_states
= db_info
.stree
;
844 if (!true_states
&& db_info
.has_states
) {
845 __push_fake_cur_stree();
846 set_path_impossible();
847 true_states
= __pop_fake_cur_stree();
852 FOR_EACH_SM(orig_states
, sm
) {
853 __set_sm_cur_stree(sm
);
854 } END_FOR_EACH_SM(sm
);
856 db_info
.true_side
= 0;
857 db_info
.stree
= NULL
;
858 db_info
.prev_return_id
= -1;
860 __push_fake_cur_stree();
861 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr
,
862 db_compare_callback
, &db_info
);
863 set_other_side_state(&db_info
);
864 set_implied_states(&db_info
);
865 stree
= __pop_fake_cur_stree();
867 merge_fake_stree(&db_info
.stree
, stree
);
869 false_states
= db_info
.stree
;
870 if (!false_states
&& db_info
.has_states
) {
871 __push_fake_cur_stree();
872 set_path_impossible();
873 false_states
= __pop_fake_cur_stree();
878 FOR_EACH_SM(orig_states
, sm
) {
879 __set_sm_cur_stree(sm
);
880 } END_FOR_EACH_SM(sm
);
882 free_stree(&orig_states
);
884 FOR_EACH_SM(true_states
, sm
) {
885 __set_true_false_sm(sm
, NULL
);
886 } END_FOR_EACH_SM(sm
);
887 FOR_EACH_SM(false_states
, sm
) {
888 __set_true_false_sm(NULL
, sm
);
889 } END_FOR_EACH_SM(sm
);
891 free_stree(&true_states
);
892 free_stree(&false_states
);
894 call_return_states_after_hooks(call_expr
);
896 FOR_EACH_SM(implied_true
, sm
) {
897 __set_true_false_sm(sm
, NULL
);
898 } END_FOR_EACH_SM(sm
);
899 FOR_EACH_SM(implied_false
, sm
) {
900 __set_true_false_sm(NULL
, sm
);
901 } END_FOR_EACH_SM(sm
);
904 void function_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
906 struct expression
*var_expr
;
907 struct expression
*call_expr
;
908 struct stree
*implied_true
= NULL
;
909 struct stree
*implied_false
= NULL
;
910 struct range_list
*rl
;
917 /* legacy cruft. need to fix call_implies_callbacks(). */
921 if (left
->type
!= EXPR_CALL
) {
927 get_absolute_rl(var_expr
, &rl
);
929 if (rl_to_sval(rl
, &sval
))
930 call_implies_callbacks(comparison
, call_expr
, sval
, call_on_left
, &implied_true
, &implied_false
);
932 compare_db_return_states_callbacks(left
, comparison
, right
, implied_true
, implied_false
);
933 free_stree(&implied_true
);
934 free_stree(&implied_false
);
937 static void call_ranged_return_hooks(struct db_callback_info
*db_info
)
939 struct call_back_list
*call_backs
;
940 struct range_list
*range_rl
;
941 struct expression
*expr
;
942 struct fcall_back
*tmp
;
945 expr
= strip_expr(db_info
->expr
);
946 while (expr
->type
== EXPR_ASSIGNMENT
)
947 expr
= strip_expr(expr
->right
);
948 if (expr
->type
!= EXPR_CALL
||
949 expr
->fn
->type
!= EXPR_SYMBOL
)
952 fn
= expr
->fn
->symbol_name
->name
;
954 call_backs
= search_callback(func_hash
, fn
);
955 FOR_EACH_PTR(call_backs
, tmp
) {
956 if (tmp
->type
!= RANGED_CALL
)
958 range_rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
959 range_rl
= cast_rl(estate_type(db_info
->ret_state
), range_rl
);
960 if (possibly_true_rl(range_rl
, SPECIAL_EQUAL
, estate_rl(db_info
->ret_state
)))
961 (tmp
->u
.ranged
)(fn
, expr
, db_info
->expr
, tmp
->info
);
962 } END_FOR_EACH_PTR(tmp
);
964 FOR_EACH_PTR(call_backs
, tmp
) {
965 if (tmp
->type
!= RANGED_EXACT
)
967 if (!estate_rl(db_info
->ret_state
))
970 range_rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
971 range_rl
= cast_rl(estate_type(db_info
->ret_state
), range_rl
);
974 * If there is an returned value out of range then this is not
975 * an exact match. In other words, "0,4096-ptr_max" is not
976 * necessarily a valid match.
979 if (remove_range(estate_rl(db_info
->ret_state
),
980 rl_min(range_rl
), rl_max(range_rl
)))
982 (tmp
->u
.ranged
)(fn
, expr
, db_info
->expr
, tmp
->info
);
983 } END_FOR_EACH_PTR(tmp
);
986 static int db_assign_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
988 struct db_callback_info
*db_info
= _info
;
989 struct range_list
*ret_range
;
991 char *ret_str
, *key
, *value
;
992 struct return_implies_callback
*tmp
;
999 return_id
= atoi(argv
[0]);
1001 type
= atoi(argv
[2]);
1002 param
= atoi(argv
[3]);
1006 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
1007 call_ranged_return_hooks(db_info
);
1008 set_return_assign_state(db_info
);
1009 set_implied_states(db_info
);
1010 stree
= __pop_fake_cur_stree();
1012 merge_fake_stree(&db_info
->stree
, stree
);
1014 __push_fake_cur_stree();
1017 db_info
->prev_return_id
= return_id
;
1019 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
1023 if (type
== CULL_PATH
) {
1027 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
1032 if (type
== PARAM_LIMIT
)
1033 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
1034 else if (type
> PARAM_LIMIT
)
1035 set_implied_states(db_info
);
1037 db_info
->handled
= 1;
1038 call_results_to_rl(db_info
->expr
->right
, get_type(strip_expr(db_info
->expr
->right
)), ret_str
, &ret_range
);
1040 ret_range
= alloc_whole_rl(get_type(strip_expr(db_info
->expr
->right
)));
1041 ret_range
= cast_rl(get_type(db_info
->expr
->right
), ret_range
);
1043 if (type
== INTERNAL
) {
1044 set_state(-1, "unnull_path", NULL
, &true_state
);
1045 __add_return_comparison(strip_expr(db_info
->expr
->right
), ret_str
);
1046 __add_comparison_info(db_info
->expr
->left
, strip_expr(db_info
->expr
->right
), ret_str
);
1047 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
1048 store_return_state(db_info
, ret_str
, alloc_estate_rl(ret_range
));
1049 set_fresh_mtag_returns(db_info
);
1052 FOR_EACH_PTR(db_return_states_list
, tmp
) {
1053 if (tmp
->type
== type
)
1054 tmp
->callback(db_info
->expr
, param
, key
, value
);
1055 } END_FOR_EACH_PTR(tmp
);
1060 static int db_return_states_assign(struct expression
*expr
)
1062 struct expression
*right
;
1063 struct sm_state
*sm
;
1064 struct stree
*stree
;
1065 struct db_callback_info db_info
= {};
1067 right
= strip_expr(expr
->right
);
1069 db_info
.prev_return_id
= -1;
1070 db_info
.expr
= expr
;
1071 db_info
.stree
= NULL
;
1072 db_info
.handled
= 0;
1074 call_return_states_before_hooks();
1076 __push_fake_cur_stree();
1077 sql_select_return_states("return_id, return, type, parameter, key, value",
1078 right
, db_assign_return_states_callback
, &db_info
);
1080 sm_msg("%s return_id %d return_ranges %s",
1081 db_info
.cull
? "culled" : "merging",
1082 db_info
.prev_return_id
,
1083 db_info
.ret_state
? db_info
.ret_state
->name
: "'<empty>'");
1085 if (db_info
.handled
)
1086 call_ranged_return_hooks(&db_info
);
1087 set_return_assign_state(&db_info
);
1088 set_implied_states(&db_info
);
1089 stree
= __pop_fake_cur_stree();
1091 merge_fake_stree(&db_info
.stree
, stree
);
1094 if (!db_info
.stree
&& db_info
.cull
) { /* this means we culled everything */
1095 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
1096 set_path_impossible();
1098 FOR_EACH_SM(db_info
.stree
, sm
) {
1100 } END_FOR_EACH_SM(sm
);
1102 free_stree(&db_info
.stree
);
1103 call_return_states_after_hooks(right
);
1105 return db_info
.handled
;
1108 static int handle_implied_return(struct expression
*expr
)
1110 struct range_list
*rl
;
1112 if (!get_implied_return(expr
->right
, &rl
))
1114 rl
= cast_rl(get_type(expr
->left
), rl
);
1115 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
1119 static void match_assign_call(struct expression
*expr
)
1121 struct call_back_list
*call_backs
;
1123 struct expression
*right
;
1125 struct range_list
*rl
;
1127 if (expr
->op
!= '=')
1130 right
= strip_expr(expr
->right
);
1131 if (right
->fn
->type
!= EXPR_SYMBOL
|| !right
->fn
->symbol
) {
1132 handled
|= db_return_states_assign(expr
);
1134 goto assigned_unknown
;
1137 if (is_fake_call(right
)) {
1138 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
1142 fn
= right
->fn
->symbol
->ident
->name
;
1143 call_backs
= search_callback(func_hash
, (char *)fn
);
1146 * The ordering here is sort of important.
1147 * One example, of how this matters is that when we do:
1149 * len = strlen(str);
1151 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1152 * They use implied_return and function_assign_hook respectively.
1153 * We want to get the implied return first before we do the function
1154 * assignment hook otherwise we end up writing the wrong thing for len
1155 * in smatch_extra.c because we assume that it already holds the
1156 * strlen() when we haven't set it yet.
1159 if (db_return_states_assign(expr
) == 1)
1162 handled
= assign_ranged_funcs(fn
, expr
, call_backs
);
1163 handled
|= handle_implied_return(expr
);
1166 call_call_backs(call_backs
, ASSIGN_CALL
, fn
, expr
);
1172 get_absolute_rl(expr
->right
, &rl
);
1173 rl
= cast_rl(get_type(expr
->left
), rl
);
1174 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
1177 static int db_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
1179 struct db_callback_info
*db_info
= _info
;
1180 struct range_list
*ret_range
;
1182 char *ret_str
, *key
, *value
;
1183 struct return_implies_callback
*tmp
;
1184 struct stree
*stree
;
1191 return_id
= atoi(argv
[0]);
1193 type
= atoi(argv
[2]);
1194 param
= atoi(argv
[3]);
1198 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
1199 call_ranged_return_hooks(db_info
);
1200 set_implied_states(db_info
);
1201 stree
= __pop_fake_cur_stree();
1203 merge_fake_stree(&db_info
->stree
, stree
);
1205 __push_fake_cur_stree();
1209 db_info
->prev_return_id
= return_id
;
1211 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
1215 if (type
== CULL_PATH
) {
1219 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
1224 if (type
== PARAM_LIMIT
)
1225 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
1226 else if (type
> PARAM_LIMIT
)
1227 set_implied_states(db_info
);
1229 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), ret_str
, &ret_range
);
1230 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
1232 if (type
== INTERNAL
) {
1233 struct smatch_state
*state
;
1235 set_state(-1, "unnull_path", NULL
, &true_state
);
1236 __add_return_comparison(strip_expr(db_info
->expr
), ret_str
);
1237 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
1239 * We want to store the return values so that we can split the strees
1240 * in smatch_db.c. This uses set_state() directly because it's not a
1241 * real smatch_extra state.
1243 snprintf(buf
, sizeof(buf
), "return %p", db_info
->expr
);
1244 state
= alloc_estate_rl(ret_range
);
1245 set_state(SMATCH_EXTRA
, buf
, NULL
, state
);
1246 store_return_state(db_info
, ret_str
, state
);
1249 FOR_EACH_PTR(db_return_states_list
, tmp
) {
1250 if (tmp
->type
== type
)
1251 tmp
->callback(db_info
->expr
, param
, key
, value
);
1252 } END_FOR_EACH_PTR(tmp
);
1258 static void db_return_states(struct expression
*expr
)
1260 struct sm_state
*sm
;
1261 struct stree
*stree
;
1262 struct db_callback_info db_info
= {};
1264 if (!__get_cur_stree()) /* no return functions */
1267 db_info
.prev_return_id
= -1;
1268 db_info
.expr
= expr
;
1269 db_info
.stree
= NULL
;
1271 call_return_states_before_hooks();
1273 __push_fake_cur_stree();
1275 sql_select_return_states("return_id, return, type, parameter, key, value",
1276 expr
, db_return_states_callback
, &db_info
);
1277 call_ranged_return_hooks(&db_info
);
1278 set_implied_states(&db_info
);
1279 stree
= __pop_fake_cur_stree();
1281 merge_fake_stree(&db_info
.stree
, stree
);
1284 FOR_EACH_SM(db_info
.stree
, sm
) {
1286 } END_FOR_EACH_SM(sm
);
1288 free_stree(&db_info
.stree
);
1289 call_return_states_after_hooks(expr
);
1292 static int is_condition_call(struct expression
*expr
)
1294 struct expression
*tmp
;
1296 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
1297 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
1299 if (tmp
->pos
.line
< expr
->pos
.line
)
1301 } END_FOR_EACH_PTR_REVERSE(tmp
);
1306 static void db_return_states_call(struct expression
*expr
)
1311 if (is_assigned_call(expr
) || is_fake_assigned_call(expr
))
1313 if (is_condition_call(expr
))
1315 db_return_states(expr
);
1318 static void match_function_call(struct expression
*expr
)
1320 struct call_back_list
*call_backs
;
1321 struct expression
*fn
;
1323 fn
= strip_expr(expr
->fn
);
1324 if (fn
->type
== EXPR_SYMBOL
&& fn
->symbol
) {
1325 call_backs
= search_callback(func_hash
, (char *)fn
->symbol
->ident
->name
);
1327 call_call_backs(call_backs
, REGULAR_CALL
,
1328 fn
->symbol
->ident
->name
, expr
);
1330 db_return_states_call(expr
);
1333 static void match_macro_assign(struct expression
*expr
)
1335 struct call_back_list
*call_backs
;
1337 struct expression
*right
;
1339 right
= strip_expr(expr
->right
);
1340 macro
= get_macro_name(right
->pos
);
1341 call_backs
= search_callback(func_hash
, (char *)macro
);
1344 call_call_backs(call_backs
, MACRO_ASSIGN
, macro
, expr
);
1345 call_call_backs(call_backs
, MACRO_ASSIGN_EXTRA
, macro
, expr
);
1348 int get_implied_return(struct expression
*expr
, struct range_list
**rl
)
1350 struct call_back_list
*call_backs
;
1351 struct fcall_back
*tmp
;
1357 expr
= strip_expr(expr
);
1358 fn
= expr_to_var(expr
->fn
);
1362 call_backs
= search_callback(func_hash
, fn
);
1364 FOR_EACH_PTR(call_backs
, tmp
) {
1365 if (tmp
->type
== IMPLIED_RETURN
)
1366 handled
|= (tmp
->u
.implied_return
)(expr
, tmp
->info
, rl
);
1367 } END_FOR_EACH_PTR(tmp
);
1374 struct range_list
*get_range_implications(const char *fn
)
1376 struct call_back_list
*call_backs
;
1377 struct range_list
*ret
= NULL
;
1378 struct fcall_back
*tmp
;
1380 call_backs
= search_callback(func_hash
, (char *)fn
);
1382 FOR_EACH_PTR(call_backs
, tmp
) {
1383 if (tmp
->type
!= RANGED_CALL
&&
1384 tmp
->type
!= RANGED_EXACT
)
1386 add_ptr_list(&ret
, tmp
->range
);
1387 } END_FOR_EACH_PTR(tmp
);
1392 void create_function_hook_hash(void)
1394 func_hash
= create_function_hashtable(5000);
1397 void register_function_hooks(int id
)
1399 add_hook(&match_function_call
, CALL_HOOK_AFTER_INLINE
);
1400 add_hook(&match_assign_call
, CALL_ASSIGNMENT_HOOK
);
1401 add_hook(&match_macro_assign
, MACRO_ASSIGNMENT_HOOK
);