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 param_key_data
{
64 param_key_hook
*call_back
;
70 struct return_implies_callback
{
74 return_implies_hook
*callback
;
75 param_key_hook
*pk_callback
;
78 ALLOCATOR(return_implies_callback
, "return_implies callbacks");
79 DECLARE_PTR_LIST(db_implies_list
, struct return_implies_callback
);
80 static struct db_implies_list
*db_return_states_list
;
82 typedef void (void_fn
)(void);
83 DECLARE_PTR_LIST(void_fn_list
, void_fn
*);
84 static struct void_fn_list
*return_states_before
;
85 static struct void_fn_list
*return_states_after
;
87 static struct fcall_back
*alloc_fcall_back(int type
, void *call_back
,
90 struct fcall_back
*cb
;
92 cb
= __alloc_fcall_back(0);
94 cb
->u
.call_back
= call_back
;
99 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *info
)
101 struct fcall_back
*cb
;
103 cb
= alloc_fcall_back(REGULAR_CALL
, call_back
, info
);
104 add_callback(func_hash
, look_for
, cb
);
107 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
110 struct fcall_back
*cb
;
112 cb
= alloc_fcall_back(ASSIGN_CALL
, call_back
, info
);
113 add_callback(func_hash
, look_for
, cb
);
116 static void register_funcs_from_file_helper(const char *file
,
117 func_hook
*call_back
, void *info
,
124 snprintf(name
, sizeof(name
), "%s.%s", option_project_str
, file
);
125 token
= get_tokens_file(name
);
128 if (token_type(token
) != TOKEN_STREAMBEGIN
)
131 while (token_type(token
) != TOKEN_STREAMEND
) {
132 if (token_type(token
) != TOKEN_IDENT
)
134 func
= show_ident(token
->ident
);
136 add_function_assign_hook(func
, call_back
, info
);
138 add_function_hook(func
, call_back
, info
);
144 void register_func_hooks_from_file(const char *file
,
145 func_hook
*call_back
, void *info
)
147 register_funcs_from_file_helper(file
, call_back
, info
, false);
150 void register_assign_hooks_from_file(const char *file
,
151 func_hook
*call_back
, void *info
)
153 register_funcs_from_file_helper(file
, call_back
, info
, true);
156 void add_implied_return_hook(const char *look_for
,
157 implied_return_hook
*call_back
,
160 struct fcall_back
*cb
;
162 cb
= alloc_fcall_back(IMPLIED_RETURN
, call_back
, info
);
163 add_callback(func_hash
, look_for
, cb
);
166 static void db_helper(struct expression
*expr
, param_key_hook
*call_back
, int param
, const char *key
, void *info
)
172 call_back(expr
, key
, NULL
, info
);
176 name
= get_name_sym_from_key(expr
, param
, key
, &sym
);
180 call_back(expr
, name
, sym
, info
);
185 static void param_key_function(const char *fn
, struct expression
*expr
, void *data
)
187 struct param_key_data
*pkd
= data
;
188 struct expression
*parent
;
193 parent
= expr_get_parent_expr(parent
);
194 if (!parent
|| ++cnt
>= 5)
196 if (parent
->type
== EXPR_CAST
)
198 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
203 if (parent
&& parent
->type
== EXPR_ASSIGNMENT
)
206 db_helper(expr
, pkd
->call_back
, pkd
->param
, pkd
->key
, pkd
->info
);
209 static void param_key_implies_function(const char *fn
, struct expression
*call_expr
,
210 struct expression
*assign_expr
, void *data
)
212 struct param_key_data
*pkd
= data
;
214 db_helper(assign_expr
?: call_expr
, pkd
->call_back
, pkd
->param
, pkd
->key
, pkd
->info
);
217 static struct param_key_data
*alloc_pkd(param_key_hook
*call_back
, int param
, const char *key
, void *info
)
219 struct param_key_data
*pkd
;
221 pkd
= malloc(sizeof(*pkd
));
222 pkd
->call_back
= call_back
;
224 pkd
->key
= alloc_string(key
);
230 void add_function_param_key_hook(const char *look_for
, param_key_hook
*call_back
,
231 int param
, const char *key
, void *info
)
233 struct param_key_data
*pkd
;
235 pkd
= alloc_pkd(call_back
, param
, key
, info
);
236 add_function_hook(look_for
, ¶m_key_function
, pkd
);
239 void return_implies_param_key(const char *look_for
, sval_t start
, sval_t end
,
240 param_key_hook
*call_back
,
241 int param
, const char *key
, void *info
)
243 struct param_key_data
*pkd
;
245 pkd
= alloc_pkd(call_back
, param
, key
, info
);
246 return_implies_state_sval(look_for
, start
, end
, ¶m_key_implies_function
, pkd
);
249 void return_implies_param_key_exact(const char *look_for
, sval_t start
, sval_t end
,
250 param_key_hook
*call_back
,
251 int param
, const char *key
, void *info
)
253 struct param_key_data
*pkd
;
255 pkd
= alloc_pkd(call_back
, param
, key
, info
);
256 return_implies_exact(look_for
, start
, end
, ¶m_key_implies_function
, pkd
);
259 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
262 struct fcall_back
*cb
;
264 cb
= alloc_fcall_back(MACRO_ASSIGN
, call_back
, info
);
265 add_callback(func_hash
, look_for
, cb
);
268 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
271 struct fcall_back
*cb
;
273 cb
= alloc_fcall_back(MACRO_ASSIGN_EXTRA
, call_back
, info
);
274 add_callback(func_hash
, look_for
, cb
);
277 void return_implies_state(const char *look_for
, long long start
, long long end
,
278 implication_hook
*call_back
, void *info
)
280 struct fcall_back
*cb
;
282 cb
= alloc_fcall_back(RANGED_CALL
, call_back
, info
);
283 cb
->range
= alloc_range_perm(ll_to_sval(start
), ll_to_sval(end
));
284 add_callback(func_hash
, look_for
, cb
);
287 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
288 implication_hook
*call_back
, void *info
)
290 struct fcall_back
*cb
;
292 cb
= alloc_fcall_back(RANGED_CALL
, call_back
, info
);
293 cb
->range
= alloc_range_perm(start
, end
);
294 add_callback(func_hash
, look_for
, cb
);
297 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
298 implication_hook
*call_back
, void *info
)
300 struct fcall_back
*cb
;
302 cb
= alloc_fcall_back(RANGED_EXACT
, call_back
, info
);
303 cb
->range
= alloc_range_perm(start
, end
);
304 add_callback(func_hash
, look_for
, cb
);
307 static struct return_implies_callback
*alloc_db_return_callback(int type
, bool param_key
, void *callback
)
309 struct return_implies_callback
*cb
;
311 cb
= __alloc_return_implies_callback(0);
313 cb
->param_key
= param_key
;
314 cb
->callback
= callback
;
319 void select_return_states_hook(int type
, return_implies_hook
*callback
)
321 struct return_implies_callback
*cb
;
323 cb
= alloc_db_return_callback(type
, false, callback
);
324 add_ptr_list(&db_return_states_list
, cb
);
327 static void call_db_return_callback(struct return_implies_callback
*cb
,
328 struct expression
*expr
, int param
, char *key
, char *value
)
331 // FIXME check if cb->pk_callback was already called
332 db_helper(expr
, cb
->pk_callback
, param
, key
, NULL
);
334 cb
->callback(expr
, param
, key
, value
);
338 void select_return_param_key(int type
, param_key_hook
*callback
)
340 struct return_implies_callback
*cb
;
342 cb
= alloc_db_return_callback(type
, true, callback
);
343 add_ptr_list(&db_return_states_list
, cb
);
346 void select_return_states_before(void_fn
*fn
)
348 void_fn
**p
= malloc(sizeof(void_fn
*));
350 add_ptr_list(&return_states_before
, p
);
353 void select_return_states_after(void_fn
*fn
)
355 void_fn
**p
= malloc(sizeof(void_fn
*));
357 add_ptr_list(&return_states_after
, p
);
360 static void call_return_states_before_hooks(void)
364 FOR_EACH_PTR(return_states_before
, fn
) {
366 } END_FOR_EACH_PTR(fn
);
369 static void call_return_states_after_hooks(struct expression
*expr
)
373 FOR_EACH_PTR(return_states_after
, fn
) {
375 } END_FOR_EACH_PTR(fn
);
376 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER_DB
);
379 static int call_call_backs(struct call_back_list
*list
, int type
,
380 const char *fn
, struct expression
*expr
)
382 struct fcall_back
*tmp
;
385 FOR_EACH_PTR(list
, tmp
) {
386 if (tmp
->type
== type
) {
387 (tmp
->u
.call_back
)(fn
, expr
, tmp
->info
);
390 } END_FOR_EACH_PTR(tmp
);
395 static void call_ranged_call_backs(struct call_back_list
*list
,
396 const char *fn
, struct expression
*call_expr
,
397 struct expression
*assign_expr
)
399 struct fcall_back
*tmp
;
401 FOR_EACH_PTR(list
, tmp
) {
402 (tmp
->u
.ranged
)(fn
, call_expr
, assign_expr
, tmp
->info
);
403 } END_FOR_EACH_PTR(tmp
);
406 static struct call_back_list
*get_same_ranged_call_backs(struct call_back_list
*list
,
407 struct data_range
*drange
)
409 struct call_back_list
*ret
= NULL
;
410 struct fcall_back
*tmp
;
412 FOR_EACH_PTR(list
, tmp
) {
413 if (tmp
->type
!= RANGED_CALL
&&
414 tmp
->type
!= RANGED_EXACT
)
416 if (ranges_equiv(tmp
->range
, drange
))
417 add_ptr_list(&ret
, tmp
);
418 } END_FOR_EACH_PTR(tmp
);
422 static int in_list_exact_sval(struct range_list
*list
, struct data_range
*drange
)
424 struct data_range
*tmp
;
426 FOR_EACH_PTR(list
, tmp
) {
427 if (ranges_equiv(tmp
, drange
))
429 } END_FOR_EACH_PTR(tmp
);
433 static int assign_ranged_funcs(const char *fn
, struct expression
*expr
,
434 struct call_back_list
*call_backs
)
436 struct fcall_back
*tmp
;
440 struct smatch_state
*estate
;
441 struct stree
*tmp_stree
;
442 struct stree
*final_states
= NULL
;
443 struct range_list
*handled_ranges
= NULL
;
444 struct call_back_list
*same_range_call_backs
= NULL
;
445 struct range_list
*rl
;
451 var_name
= expr_to_var_sym(expr
->left
, &sym
);
452 if (!var_name
|| !sym
)
455 FOR_EACH_PTR(call_backs
, tmp
) {
456 if (tmp
->type
!= RANGED_CALL
&&
457 tmp
->type
!= RANGED_EXACT
)
460 if (in_list_exact_sval(handled_ranges
, tmp
->range
))
462 __push_fake_cur_stree();
463 tack_on(&handled_ranges
, tmp
->range
);
465 same_range_call_backs
= get_same_ranged_call_backs(call_backs
, tmp
->range
);
466 call_ranged_call_backs(same_range_call_backs
, fn
, expr
->right
, expr
);
467 __free_ptr_list((struct ptr_list
**)&same_range_call_backs
);
469 rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
470 rl
= cast_rl(get_type(expr
->left
), rl
);
471 estate
= alloc_estate_rl(rl
);
472 set_extra_mod(var_name
, sym
, expr
->left
, estate
);
474 tmp_stree
= __pop_fake_cur_stree();
475 merge_fake_stree(&final_states
, tmp_stree
);
476 free_stree(&tmp_stree
);
478 } END_FOR_EACH_PTR(tmp
);
480 FOR_EACH_SM(final_states
, sm
) {
482 } END_FOR_EACH_SM(sm
);
484 free_stree(&final_states
);
486 free_string(var_name
);
490 static void call_implies_callbacks(int comparison
, struct expression
*expr
, sval_t sval
, int left
, struct stree
**implied_true
, struct stree
**implied_false
)
492 struct call_back_list
*call_backs
;
493 struct fcall_back
*tmp
;
495 struct data_range
*value_range
;
496 struct stree
*true_states
= NULL
;
497 struct stree
*false_states
= NULL
;
498 struct stree
*tmp_stree
;
500 *implied_true
= NULL
;
501 *implied_false
= NULL
;
502 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
504 fn
= expr
->fn
->symbol
->ident
->name
;
505 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
508 value_range
= alloc_range(sval
, sval
);
510 /* set true states */
511 __push_fake_cur_stree();
512 FOR_EACH_PTR(call_backs
, tmp
) {
513 if (tmp
->type
!= RANGED_CALL
&&
514 tmp
->type
!= RANGED_EXACT
)
516 if (!true_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
518 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
519 } END_FOR_EACH_PTR(tmp
);
520 tmp_stree
= __pop_fake_cur_stree();
521 merge_fake_stree(&true_states
, tmp_stree
);
522 free_stree(&tmp_stree
);
524 /* set false states */
525 __push_fake_cur_stree();
526 FOR_EACH_PTR(call_backs
, tmp
) {
527 if (tmp
->type
!= RANGED_CALL
&&
528 tmp
->type
!= RANGED_EXACT
)
530 if (!false_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
532 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
533 } END_FOR_EACH_PTR(tmp
);
534 tmp_stree
= __pop_fake_cur_stree();
535 merge_fake_stree(&false_states
, tmp_stree
);
536 free_stree(&tmp_stree
);
538 *implied_true
= true_states
;
539 *implied_false
= false_states
;
542 struct db_callback_info
{
545 struct expression
*expr
;
546 struct range_list
*rl
;
549 struct stree
*implied
;
550 struct db_implies_list
*callbacks
;
555 struct smatch_state
*ret_state
;
556 struct expression
*var_expr
;
560 static void set_implied_states(struct db_callback_info
*db_info
)
564 FOR_EACH_SM(db_info
->implied
, sm
) {
566 } END_FOR_EACH_SM(sm
);
568 free_stree(&db_info
->implied
);
571 static void store_return_state(struct db_callback_info
*db_info
, const char *ret_str
, struct smatch_state
*state
)
573 db_info
->ret_str
= alloc_sname(ret_str
),
574 db_info
->ret_state
= state
;
577 static bool fake_a_param_assignment(struct expression
*expr
, const char *ret_str
, struct smatch_state
*orig
)
579 struct expression
*arg
, *left
, *right
, *tmp
, *fake_assign
;
585 if (expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
590 while (right
->type
== EXPR_ASSIGNMENT
)
591 right
= strip_expr(right
->right
);
592 if (!right
|| right
->type
!= EXPR_CALL
)
595 p
= strchr(ret_str
, '[');
600 if (p
[0] == '=' && p
[1] == '=')
605 snprintf(buf
, sizeof(buf
), "%s", p
);
609 param
= strtol(p
, &p
, 10);
616 arg
= get_argument_from_call_expr(right
->args
, param
);
620 /* There should be a get_other_name() function which returns an expr */
621 tmp
= get_assigned_expr(arg
);
626 * This is a sanity check to prevent side effects from evaluating stuff
629 str
= expr_to_chunk_sym_vsl(arg
, NULL
, NULL
);
634 right
= gen_expression_from_key(arg
, buf
);
635 if (!right
) /* Mostly fails for binops like [$0 + 4032] */
637 fake_assign
= assign_expression(left
, '=', right
);
638 __in_fake_parameter_assign
++;
639 __split_expr(fake_assign
);
640 __in_fake_parameter_assign
--;
643 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
644 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
645 * parts have to be considered. We use _nomod() because it's not really
646 * another modification, it's just a clarification.
649 if (estate_rl(orig
)) {
650 struct smatch_state
*faked
;
651 struct range_list
*rl
;
653 faked
= get_extra_state(left
);
654 if (estate_rl(faked
)) {
655 rl
= rl_intersection(estate_rl(faked
), estate_rl(orig
));
657 set_extra_expr_nomod(expr
, alloc_estate_rl(rl
));
664 static void set_fresh_mtag_returns(struct db_callback_info
*db_info
)
666 struct expression
*expr
= db_info
->expr
->left
;
667 struct smatch_state
*state
;
669 if (!db_info
->ret_state
)
672 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
673 state
= get_mtag_return(db_info
->expr
, state
);
677 set_real_absolute(expr
, state
);
678 set_extra_expr_mod(expr
, state
);
680 db_info
->ret_state
= NULL
;
681 db_info
->ret_str
= NULL
;
684 static void set_return_assign_state(struct db_callback_info
*db_info
)
686 struct expression
*expr
= db_info
->expr
->left
;
687 struct smatch_state
*state
;
689 if (!db_info
->ret_state
)
692 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
693 if (!fake_a_param_assignment(db_info
->expr
, db_info
->ret_str
, state
))
694 set_extra_expr_mod(expr
, state
);
696 db_info
->ret_state
= NULL
;
697 db_info
->ret_str
= NULL
;
700 static void set_other_side_state(struct db_callback_info
*db_info
)
702 struct expression
*expr
= db_info
->var_expr
;
703 struct smatch_state
*state
;
705 if (!db_info
->ret_state
)
708 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
709 set_extra_expr_nomod(expr
, state
);
710 db_info
->ret_state
= NULL
;
711 db_info
->ret_str
= NULL
;
714 static void handle_ret_equals_param(char *ret_string
, struct range_list
*rl
, struct expression
*call
)
718 struct expression
*arg
;
719 struct range_list
*orig
;
721 str
= strstr(ret_string
, "==$");
725 param
= strtoll(str
, NULL
, 10);
726 arg
= get_argument_from_call_expr(call
->args
, param
);
729 get_absolute_rl(arg
, &orig
);
730 rl
= rl_intersection(orig
, rl
);
733 set_extra_expr_nomod(arg
, alloc_estate_rl(rl
));
736 static int impossible_limit(struct expression
*expr
, int param
, char *key
, char *value
)
738 struct expression
*arg
;
739 struct smatch_state
*state
;
740 struct range_list
*passed
;
741 struct range_list
*limit
;
742 struct symbol
*compare_type
;
744 while (expr
->type
== EXPR_ASSIGNMENT
)
745 expr
= strip_expr(expr
->right
);
746 if (expr
->type
!= EXPR_CALL
)
749 arg
= get_argument_from_call_expr(expr
->args
, param
);
753 if (strcmp(key
, "$") == 0) {
754 if (!get_implied_rl(arg
, &passed
))
757 compare_type
= get_arg_type(expr
->fn
, param
);
762 name
= get_variable_from_key(arg
, key
, &sym
);
766 state
= get_state(SMATCH_EXTRA
, name
, sym
);
771 passed
= estate_rl(state
);
772 if (!passed
|| is_whole_rl(passed
)) {
777 compare_type
= get_member_type_from_key(arg
, key
);
780 passed
= cast_rl(compare_type
, passed
);
781 call_results_to_rl(expr
, compare_type
, value
, &limit
);
782 if (!limit
|| is_whole_rl(limit
))
784 if (possibly_true_rl(passed
, SPECIAL_EQUAL
, limit
))
786 if (option_debug
|| local_debug
)
787 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param
, key
, show_rl(passed
), value
);
791 static int is_impossible_data(int type
, struct expression
*expr
, int param
, char *key
, char *value
)
793 if (type
== PARAM_LIMIT
&& impossible_limit(expr
, param
, key
, value
))
795 if (type
== COMPARE_LIMIT
&& param_compare_limit_is_impossible(expr
, param
, key
, value
)) {
797 sm_msg("param_compare_limit_is_impossible: %d %s %s", param
, key
, value
);
803 static int func_type_mismatch(struct expression
*expr
, const char *value
)
807 /* This makes faking returns easier */
808 if (!value
|| value
[0] == '\0')
811 while (expr
->type
== EXPR_ASSIGNMENT
)
812 expr
= strip_expr(expr
->right
);
815 * Short cut: We only care about function pointers that are struct
819 if (expr
->fn
->type
== EXPR_SYMBOL
)
822 type
= get_type(expr
->fn
);
825 if (type
->type
== SYM_PTR
)
826 type
= get_real_base_type(type
);
828 if (strcmp(type_to_str(type
), value
) == 0)
834 static int db_compare_callback(void *_info
, int argc
, char **argv
, char **azColName
)
836 struct db_callback_info
*db_info
= _info
;
837 struct range_list
*var_rl
= db_info
->rl
;
838 struct range_list
*ret_range
;
840 char *ret_str
, *key
, *value
;
841 struct return_implies_callback
*tmp
;
849 return_id
= atoi(argv
[0]);
851 type
= atoi(argv
[2]);
852 param
= atoi(argv
[3]);
856 db_info
->has_states
= 1;
857 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
858 set_other_side_state(db_info
);
859 set_implied_states(db_info
);
860 stree
= __pop_fake_cur_stree();
862 merge_fake_stree(&db_info
->stree
, stree
);
864 __push_fake_cur_stree();
867 db_info
->prev_return_id
= return_id
;
869 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
873 if (type
== CULL_PATH
) {
878 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
883 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), ret_str
, &ret_range
);
884 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
886 ret_range
= alloc_whole_rl(get_type(db_info
->expr
));
888 comparison
= db_info
->comparison
;
890 comparison
= flip_comparison(comparison
);
892 if (db_info
->true_side
) {
893 if (!possibly_true_rl(var_rl
, comparison
, ret_range
))
895 if (type
== PARAM_LIMIT
)
896 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
897 else if (type
> PARAM_LIMIT
)
898 set_implied_states(db_info
);
899 filter_by_comparison(&var_rl
, comparison
, ret_range
);
900 filter_by_comparison(&ret_range
, flip_comparison(comparison
), var_rl
);
902 if (!possibly_false_rl(var_rl
, comparison
, ret_range
))
904 if (type
== PARAM_LIMIT
)
905 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
906 else if (type
> PARAM_LIMIT
)
907 set_implied_states(db_info
);
908 filter_by_comparison(&var_rl
, negate_comparison(comparison
), ret_range
);
909 filter_by_comparison(&ret_range
, flip_comparison(negate_comparison(comparison
)), var_rl
);
912 handle_ret_equals_param(ret_str
, ret_range
, db_info
->expr
);
914 if (type
== INTERNAL
) {
915 set_state(-1, "unnull_path", NULL
, &true_state
);
916 __add_return_comparison(strip_expr(db_info
->expr
), ret_str
);
917 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
918 store_return_state(db_info
, ret_str
, alloc_estate_rl(clone_rl(var_rl
)));
921 FOR_EACH_PTR(db_info
->callbacks
, tmp
) {
922 if (tmp
->type
== type
)
923 call_db_return_callback(tmp
, db_info
->expr
, param
, key
, value
);
924 } END_FOR_EACH_PTR(tmp
);
929 static void compare_db_return_states_callbacks(struct expression
*left
, int comparison
, struct expression
*right
, struct stree
*implied_true
, struct stree
*implied_false
)
931 struct stree
*orig_states
;
933 struct stree
*true_states
;
934 struct stree
*false_states
;
936 struct db_callback_info db_info
= {};
937 struct expression
*var_expr
;
938 struct expression
*call_expr
;
939 struct range_list
*rl
;
942 orig_states
= clone_stree(__get_cur_stree());
944 /* legacy cruft. need to fix call_implies_callbacks(). */
948 if (left
->type
!= EXPR_CALL
) {
954 get_absolute_rl(var_expr
, &rl
);
956 db_info
.comparison
= comparison
;
957 db_info
.expr
= call_expr
;
959 db_info
.left
= call_on_left
;
960 db_info
.callbacks
= db_return_states_list
;
961 db_info
.var_expr
= var_expr
;
963 call_return_states_before_hooks();
965 db_info
.true_side
= 1;
966 db_info
.stree
= NULL
;
967 db_info
.prev_return_id
= -1;
968 __push_fake_cur_stree();
969 sql_select_return_states("return_id, return, type, parameter, key, value",
970 call_expr
, db_compare_callback
, &db_info
);
971 set_other_side_state(&db_info
);
972 set_implied_states(&db_info
);
973 stree
= __pop_fake_cur_stree();
975 merge_fake_stree(&db_info
.stree
, stree
);
977 true_states
= db_info
.stree
;
978 if (!true_states
&& db_info
.has_states
) {
979 __push_fake_cur_stree();
980 set_path_impossible();
981 true_states
= __pop_fake_cur_stree();
986 FOR_EACH_SM(orig_states
, sm
) {
987 __set_sm_cur_stree(sm
);
988 } END_FOR_EACH_SM(sm
);
990 db_info
.true_side
= 0;
991 db_info
.stree
= NULL
;
992 db_info
.prev_return_id
= -1;
994 __push_fake_cur_stree();
995 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr
,
996 db_compare_callback
, &db_info
);
997 set_other_side_state(&db_info
);
998 set_implied_states(&db_info
);
999 stree
= __pop_fake_cur_stree();
1001 merge_fake_stree(&db_info
.stree
, stree
);
1003 false_states
= db_info
.stree
;
1004 if (!false_states
&& db_info
.has_states
) {
1005 __push_fake_cur_stree();
1006 set_path_impossible();
1007 false_states
= __pop_fake_cur_stree();
1012 FOR_EACH_SM(orig_states
, sm
) {
1013 __set_sm_cur_stree(sm
);
1014 } END_FOR_EACH_SM(sm
);
1016 free_stree(&orig_states
);
1018 FOR_EACH_SM(true_states
, sm
) {
1019 __set_true_false_sm(sm
, NULL
);
1020 } END_FOR_EACH_SM(sm
);
1021 FOR_EACH_SM(false_states
, sm
) {
1022 __set_true_false_sm(NULL
, sm
);
1023 } END_FOR_EACH_SM(sm
);
1025 free_stree(&true_states
);
1026 free_stree(&false_states
);
1028 call_return_states_after_hooks(call_expr
);
1030 FOR_EACH_SM(implied_true
, sm
) {
1031 __set_true_false_sm(sm
, NULL
);
1032 } END_FOR_EACH_SM(sm
);
1033 FOR_EACH_SM(implied_false
, sm
) {
1034 __set_true_false_sm(NULL
, sm
);
1035 } END_FOR_EACH_SM(sm
);
1038 void function_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1040 struct expression
*var_expr
;
1041 struct expression
*call_expr
;
1042 struct stree
*implied_true
= NULL
;
1043 struct stree
*implied_false
= NULL
;
1044 struct range_list
*rl
;
1051 /* legacy cruft. need to fix call_implies_callbacks(). */
1055 if (left
->type
!= EXPR_CALL
) {
1061 get_absolute_rl(var_expr
, &rl
);
1063 if (rl_to_sval(rl
, &sval
))
1064 call_implies_callbacks(comparison
, call_expr
, sval
, call_on_left
, &implied_true
, &implied_false
);
1066 compare_db_return_states_callbacks(left
, comparison
, right
, implied_true
, implied_false
);
1067 free_stree(&implied_true
);
1068 free_stree(&implied_false
);
1071 static void call_ranged_return_hooks(struct db_callback_info
*db_info
)
1073 struct call_back_list
*call_backs
;
1074 struct range_list
*range_rl
;
1075 struct expression
*expr
;
1076 struct fcall_back
*tmp
;
1079 expr
= strip_expr(db_info
->expr
);
1080 while (expr
->type
== EXPR_ASSIGNMENT
)
1081 expr
= strip_expr(expr
->right
);
1082 if (expr
->type
!= EXPR_CALL
||
1083 expr
->fn
->type
!= EXPR_SYMBOL
)
1086 fn
= expr
->fn
->symbol_name
->name
;
1088 call_backs
= search_callback(func_hash
, fn
);
1089 FOR_EACH_PTR(call_backs
, tmp
) {
1090 if (tmp
->type
!= RANGED_CALL
)
1092 range_rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
1093 range_rl
= cast_rl(estate_type(db_info
->ret_state
), range_rl
);
1094 if (possibly_true_rl(range_rl
, SPECIAL_EQUAL
, estate_rl(db_info
->ret_state
)))
1095 (tmp
->u
.ranged
)(fn
, expr
, db_info
->expr
, tmp
->info
);
1096 } END_FOR_EACH_PTR(tmp
);
1098 FOR_EACH_PTR(call_backs
, tmp
) {
1099 if (tmp
->type
!= RANGED_EXACT
)
1101 if (!estate_rl(db_info
->ret_state
))
1104 range_rl
= alloc_rl(tmp
->range
->min
, tmp
->range
->max
);
1105 range_rl
= cast_rl(estate_type(db_info
->ret_state
), range_rl
);
1108 * If there is an returned value out of range then this is not
1109 * an exact match. In other words, "0,4096-ptr_max" is not
1110 * necessarily a valid match.
1113 if (remove_range(estate_rl(db_info
->ret_state
),
1114 rl_min(range_rl
), rl_max(range_rl
)))
1116 (tmp
->u
.ranged
)(fn
, expr
, db_info
->expr
, tmp
->info
);
1117 } END_FOR_EACH_PTR(tmp
);
1120 static int db_assign_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
1122 struct db_callback_info
*db_info
= _info
;
1123 struct range_list
*ret_range
;
1125 char *ret_str
, *key
, *value
;
1126 struct return_implies_callback
*tmp
;
1127 struct stree
*stree
;
1133 return_id
= atoi(argv
[0]);
1135 type
= atoi(argv
[2]);
1136 param
= atoi(argv
[3]);
1140 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
1141 call_ranged_return_hooks(db_info
);
1142 set_return_assign_state(db_info
);
1143 set_implied_states(db_info
);
1144 stree
= __pop_fake_cur_stree();
1146 merge_fake_stree(&db_info
->stree
, stree
);
1148 __push_fake_cur_stree();
1151 db_info
->prev_return_id
= return_id
;
1153 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
1157 if (type
== CULL_PATH
) {
1161 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
1166 if (type
== PARAM_LIMIT
)
1167 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
1168 else if (type
> PARAM_LIMIT
)
1169 set_implied_states(db_info
);
1171 db_info
->handled
= 1;
1172 call_results_to_rl(db_info
->expr
->right
, get_type(strip_expr(db_info
->expr
->right
)), ret_str
, &ret_range
);
1174 ret_range
= alloc_whole_rl(get_type(strip_expr(db_info
->expr
->right
)));
1175 ret_range
= cast_rl(get_type(db_info
->expr
->right
), ret_range
);
1177 if (type
== INTERNAL
) {
1178 set_state(-1, "unnull_path", NULL
, &true_state
);
1179 __add_return_comparison(strip_expr(db_info
->expr
->right
), ret_str
);
1180 __add_comparison_info(db_info
->expr
->left
, strip_expr(db_info
->expr
->right
), ret_str
);
1181 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
1182 store_return_state(db_info
, ret_str
, alloc_estate_rl(ret_range
));
1183 set_fresh_mtag_returns(db_info
);
1186 FOR_EACH_PTR(db_return_states_list
, tmp
) {
1187 if (tmp
->type
== type
)
1188 call_db_return_callback(tmp
, db_info
->expr
, param
, key
, value
);
1189 } END_FOR_EACH_PTR(tmp
);
1194 static int db_return_states_assign(struct expression
*expr
)
1196 struct expression
*right
;
1197 struct sm_state
*sm
;
1198 struct stree
*stree
;
1199 struct db_callback_info db_info
= {};
1201 right
= strip_expr(expr
->right
);
1203 db_info
.prev_return_id
= -1;
1204 db_info
.expr
= expr
;
1205 db_info
.stree
= NULL
;
1206 db_info
.handled
= 0;
1208 call_return_states_before_hooks();
1210 __push_fake_cur_stree();
1211 sql_select_return_states("return_id, return, type, parameter, key, value",
1212 right
, db_assign_return_states_callback
, &db_info
);
1214 sm_msg("%s return_id %d return_ranges %s",
1215 db_info
.cull
? "culled" : "merging",
1216 db_info
.prev_return_id
,
1217 db_info
.ret_state
? db_info
.ret_state
->name
: "'<empty>'");
1219 if (db_info
.handled
)
1220 call_ranged_return_hooks(&db_info
);
1221 set_return_assign_state(&db_info
);
1222 set_implied_states(&db_info
);
1223 stree
= __pop_fake_cur_stree();
1225 merge_fake_stree(&db_info
.stree
, stree
);
1228 if (!db_info
.stree
&& db_info
.cull
) { /* this means we culled everything */
1229 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
1230 set_path_impossible();
1232 FOR_EACH_SM(db_info
.stree
, sm
) {
1234 } END_FOR_EACH_SM(sm
);
1236 free_stree(&db_info
.stree
);
1237 call_return_states_after_hooks(right
);
1239 return db_info
.handled
;
1242 static int handle_implied_return(struct expression
*expr
)
1244 struct range_list
*rl
;
1246 if (!get_implied_return(expr
->right
, &rl
))
1248 rl
= cast_rl(get_type(expr
->left
), rl
);
1249 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
1253 static void match_assign_call(struct expression
*expr
)
1255 struct call_back_list
*call_backs
;
1257 struct expression
*right
;
1259 struct range_list
*rl
;
1261 if (expr
->op
!= '=')
1264 right
= strip_expr(expr
->right
);
1265 if (right
->fn
->type
!= EXPR_SYMBOL
|| !right
->fn
->symbol
) {
1266 handled
|= db_return_states_assign(expr
);
1268 goto assigned_unknown
;
1271 if (is_fake_call(right
))
1274 fn
= right
->fn
->symbol
->ident
->name
;
1275 call_backs
= search_callback(func_hash
, (char *)fn
);
1278 * The ordering here is sort of important.
1279 * One example, of how this matters is that when we do:
1281 * len = strlen(str);
1283 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1284 * They use implied_return and function_assign_hook respectively.
1285 * We want to get the implied return first before we do the function
1286 * assignment hook otherwise we end up writing the wrong thing for len
1287 * in smatch_extra.c because we assume that it already holds the
1288 * strlen() when we haven't set it yet.
1291 if (db_return_states_assign(expr
) == 1)
1294 handled
= assign_ranged_funcs(fn
, expr
, call_backs
);
1295 handled
|= handle_implied_return(expr
);
1298 call_call_backs(call_backs
, ASSIGN_CALL
, fn
, expr
);
1304 get_absolute_rl(expr
->right
, &rl
);
1305 rl
= cast_rl(get_type(expr
->left
), rl
);
1306 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
1309 static int db_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
1311 struct db_callback_info
*db_info
= _info
;
1312 struct range_list
*ret_range
;
1314 char *ret_str
, *key
, *value
;
1315 struct return_implies_callback
*tmp
;
1316 struct stree
*stree
;
1323 return_id
= atoi(argv
[0]);
1325 type
= atoi(argv
[2]);
1326 param
= atoi(argv
[3]);
1330 if (db_info
->prev_return_id
!= -1 && type
== INTERNAL
) {
1331 call_ranged_return_hooks(db_info
);
1332 set_implied_states(db_info
);
1333 stree
= __pop_fake_cur_stree();
1335 merge_fake_stree(&db_info
->stree
, stree
);
1337 __push_fake_cur_stree();
1341 db_info
->prev_return_id
= return_id
;
1343 if (type
== INTERNAL
&& func_type_mismatch(db_info
->expr
, value
))
1347 if (type
== CULL_PATH
) {
1351 if (is_impossible_data(type
, db_info
->expr
, param
, key
, value
)) {
1356 if (type
== PARAM_LIMIT
)
1357 param_limit_implications(db_info
->expr
, param
, key
, value
, &db_info
->implied
);
1358 else if (type
> PARAM_LIMIT
)
1359 set_implied_states(db_info
);
1361 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), ret_str
, &ret_range
);
1362 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
1364 if (type
== INTERNAL
) {
1365 struct smatch_state
*state
;
1367 set_state(-1, "unnull_path", NULL
, &true_state
);
1368 __add_return_comparison(strip_expr(db_info
->expr
), ret_str
);
1369 __add_return_to_param_mapping(db_info
->expr
, ret_str
);
1371 * We want to store the return values so that we can split the strees
1372 * in smatch_db.c. This uses set_state() directly because it's not a
1373 * real smatch_extra state.
1375 snprintf(buf
, sizeof(buf
), "return %p", db_info
->expr
);
1376 state
= alloc_estate_rl(ret_range
);
1377 set_state(SMATCH_EXTRA
, buf
, NULL
, state
);
1378 store_return_state(db_info
, ret_str
, state
);
1381 FOR_EACH_PTR(db_return_states_list
, tmp
) {
1382 if (tmp
->type
== type
)
1383 call_db_return_callback(tmp
, db_info
->expr
, param
, key
, value
);
1384 } END_FOR_EACH_PTR(tmp
);
1390 static void db_return_states(struct expression
*expr
)
1392 struct sm_state
*sm
;
1393 struct stree
*stree
;
1394 struct db_callback_info db_info
= {};
1396 if (!__get_cur_stree()) /* no return functions */
1399 db_info
.prev_return_id
= -1;
1400 db_info
.expr
= expr
;
1401 db_info
.stree
= NULL
;
1403 call_return_states_before_hooks();
1405 __push_fake_cur_stree();
1407 sql_select_return_states("return_id, return, type, parameter, key, value",
1408 expr
, db_return_states_callback
, &db_info
);
1409 call_ranged_return_hooks(&db_info
);
1410 set_implied_states(&db_info
);
1411 stree
= __pop_fake_cur_stree();
1413 merge_fake_stree(&db_info
.stree
, stree
);
1416 FOR_EACH_SM(db_info
.stree
, sm
) {
1418 } END_FOR_EACH_SM(sm
);
1420 free_stree(&db_info
.stree
);
1421 call_return_states_after_hooks(expr
);
1424 static int is_condition_call(struct expression
*expr
)
1426 struct expression
*tmp
;
1428 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
1429 if (expr
== tmp
|| expr_get_parent_expr(expr
) == tmp
)
1431 if (tmp
->pos
.line
< expr
->pos
.line
)
1433 } END_FOR_EACH_PTR_REVERSE(tmp
);
1438 static void db_return_states_call(struct expression
*expr
)
1443 if (is_assigned_call(expr
) || is_fake_assigned_call(expr
))
1445 if (is_condition_call(expr
))
1447 db_return_states(expr
);
1450 static void match_function_call(struct expression
*expr
)
1452 struct call_back_list
*call_backs
;
1453 struct expression
*fn
;
1455 fn
= strip_expr(expr
->fn
);
1456 if (fn
->type
== EXPR_SYMBOL
&& fn
->symbol
) {
1457 call_backs
= search_callback(func_hash
, (char *)fn
->symbol
->ident
->name
);
1459 call_call_backs(call_backs
, REGULAR_CALL
,
1460 fn
->symbol
->ident
->name
, expr
);
1462 db_return_states_call(expr
);
1465 static void match_macro_assign(struct expression
*expr
)
1467 struct call_back_list
*call_backs
;
1469 struct expression
*right
;
1471 right
= strip_expr(expr
->right
);
1472 macro
= get_macro_name(right
->pos
);
1473 call_backs
= search_callback(func_hash
, (char *)macro
);
1476 call_call_backs(call_backs
, MACRO_ASSIGN
, macro
, expr
);
1477 call_call_backs(call_backs
, MACRO_ASSIGN_EXTRA
, macro
, expr
);
1480 int get_implied_return(struct expression
*expr
, struct range_list
**rl
)
1482 struct call_back_list
*call_backs
;
1483 struct fcall_back
*tmp
;
1489 expr
= strip_expr(expr
);
1490 fn
= expr_to_var(expr
->fn
);
1494 call_backs
= search_callback(func_hash
, fn
);
1496 FOR_EACH_PTR(call_backs
, tmp
) {
1497 if (tmp
->type
== IMPLIED_RETURN
)
1498 handled
|= (tmp
->u
.implied_return
)(expr
, tmp
->info
, rl
);
1499 } END_FOR_EACH_PTR(tmp
);
1506 struct range_list
*get_range_implications(const char *fn
)
1508 struct call_back_list
*call_backs
;
1509 struct range_list
*ret
= NULL
;
1510 struct fcall_back
*tmp
;
1512 call_backs
= search_callback(func_hash
, (char *)fn
);
1514 FOR_EACH_PTR(call_backs
, tmp
) {
1515 if (tmp
->type
!= RANGED_CALL
&&
1516 tmp
->type
!= RANGED_EXACT
)
1518 add_ptr_list(&ret
, tmp
->range
);
1519 } END_FOR_EACH_PTR(tmp
);
1524 void create_function_hook_hash(void)
1526 func_hash
= create_function_hashtable(5000);
1529 void register_function_hooks(int id
)
1531 add_hook(&match_function_call
, CALL_HOOK_AFTER_INLINE
);
1532 add_hook(&match_assign_call
, CALL_ASSIGNMENT_HOOK
);
1533 add_hook(&match_macro_assign
, MACRO_ASSIGNMENT_HOOK
);