implied: fix get_tf_stacks_from_pool()
[smatch.git] / smatch_function_hooks.c
blob1d2932bae13f8fed02c60773d66be7a22f6b7cc1
1 /*
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.
21 * The param_key hooks are probably the right things to use going forward.
22 * They give you a name/sym pair so it means less code in the checks.
24 * The add_function_hook() functions are trigger for every call. The
25 * "return_implies" are triggered for specific return ranges. The "exact"
26 * variants will be triggered if it's *definitely* in the range where the
27 * others will be triggered if it's *possibly* in the range. The "late"
28 * variants will be triggered after the others have run.
30 * There are a few miscellaneous things like add_function_assign_hook() and
31 * add_macro_assign_hook() which are only triggered for assignments. The
32 * add_implied_return_hook() let's you manually adjust the return range.
34 * Every call:
35 * add_function_param_key_hook_early()
36 * add_function_param_key_hook()
37 * add_function_param_key_hook_late()
38 * add_param_key_expr_hook()
39 * add_function_hook_early()
40 * add_function_hook()
42 * Just for some return ranges:
43 * return_implies_param_key()
44 * return_implies_param_key_expr()
45 * return_implies_param_key_exact()
46 * return_implies_state()
47 * select_return_param_key() (It's weird that this is not in smatch_db.c)
49 * For Assignments:
50 * add_function_assign_hook()
52 * For Macro Assignments:
53 * add_macro_assign_hook()
55 * Manipulate the return range.
56 * add_implied_return_hook()
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <ctype.h>
62 #include "smatch.h"
63 #include "smatch_slist.h"
64 #include "smatch_extra.h"
65 #include "smatch_function_hashtable.h"
66 #include "smatch_expression_stacks.h"
68 struct fcall_back {
69 int type;
70 struct data_range *range;
71 union {
72 func_hook *call_back;
73 implication_hook *ranged;
74 implied_return_hook *implied_return;
75 } u;
76 void *info;
79 ALLOCATOR(fcall_back, "call backs");
80 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
82 DEFINE_FUNCTION_HASHTABLE_STATIC(callback, struct fcall_back, struct call_back_list);
83 static struct hashtable *func_hash;
85 int __in_fake_parameter_assign;
87 enum fn_hook_type {
88 REGULAR_CALL_EARLY,
89 REGULAR_CALL,
90 REGULAR_CALL_LATE,
91 RANGED_CALL,
92 RANGED_EXACT,
93 ASSIGN_CALL,
94 IMPLIED_RETURN,
95 MACRO_ASSIGN,
96 MACRO_ASSIGN_EXTRA,
99 struct param_key_data {
100 param_key_hook *call_back;
101 expr_func *expr_fn;
102 int param;
103 const char *key;
104 void *info;
107 struct param_data {
108 expr_func *call_back;
109 int param;
110 void *info;
113 struct return_implies_callback {
114 int type;
115 bool param_key;
116 union {
117 return_implies_hook *callback;
118 param_key_hook *pk_callback;
121 ALLOCATOR(return_implies_callback, "return_implies callbacks");
122 DECLARE_PTR_LIST(db_implies_list, struct return_implies_callback);
123 static struct db_implies_list *db_return_states_list;
125 static struct void_fn_list *return_states_before;
126 static struct void_fn_list *return_states_after;
127 static struct string_hook_list *return_string_hooks;
129 struct db_callback_info {
130 int true_side;
131 int comparison;
132 struct expression *expr;
133 struct range_list *rl;
134 int left;
135 struct stree *stree;
136 struct stree *implied;
137 struct db_implies_list *callbacks;
138 struct db_implies_list *called;
139 int prev_return_id;
140 int cull;
141 int has_states;
142 bool states_merged;
143 char *ret_str;
144 struct smatch_state *ret_state;
145 struct expression *var_expr;
146 struct expression_list *fake_param_assign_stack;
147 int handled;
150 static struct expression_list *fake_calls;
152 void add_fake_call_after_return(struct expression *call)
154 add_ptr_list(&fake_calls, call);
157 static void parse_fake_calls(void)
159 struct expression_list *list;
160 struct expression *call;
162 list = fake_calls;
163 fake_calls = NULL;
165 FOR_EACH_PTR(list, call) {
166 __split_expr(call);
167 } END_FOR_EACH_PTR(call);
169 __free_ptr_list((struct ptr_list **)&list);
172 static struct fcall_back *alloc_fcall_back(int type, void *call_back,
173 void *info)
175 struct fcall_back *cb;
177 cb = __alloc_fcall_back(0);
178 cb->type = type;
179 cb->u.call_back = call_back;
180 cb->info = info;
181 return cb;
184 static const char *get_fn_name(struct expression *fn)
186 fn = strip_expr(fn);
187 if (!fn)
188 return NULL;
189 if (fn->type == EXPR_SYMBOL && fn->symbol)
190 return fn->symbol->ident->name;
191 return get_member_name(fn);
194 static struct call_back_list *get_call_backs(const char *fn_name)
196 if (!fn_name)
197 return NULL;
198 return search_callback(func_hash, (char *)fn_name);
201 void add_function_hook(const char *look_for, func_hook *call_back, void *info)
203 struct fcall_back *cb;
205 cb = alloc_fcall_back(REGULAR_CALL, call_back, info);
206 add_callback(func_hash, look_for, cb);
209 void add_function_hook_early(const char *look_for, func_hook *call_back, void *info)
211 struct fcall_back *cb;
213 cb = alloc_fcall_back(REGULAR_CALL_EARLY, call_back, info);
214 add_callback(func_hash, look_for, cb);
217 void add_function_hook_late(const char *look_for, func_hook *call_back, void *info)
219 struct fcall_back *cb;
221 cb = alloc_fcall_back(REGULAR_CALL_LATE, call_back, info);
222 add_callback(func_hash, look_for, cb);
225 void add_function_assign_hook(const char *look_for, func_hook *call_back,
226 void *info)
228 struct fcall_back *cb;
230 cb = alloc_fcall_back(ASSIGN_CALL, call_back, info);
231 add_callback(func_hash, look_for, cb);
234 static void register_funcs_from_file_helper(const char *file,
235 func_hook *call_back, void *info,
236 bool assign)
238 struct token *token;
239 const char *func;
240 char name[64];
242 snprintf(name, sizeof(name), "%s.%s", option_project_str, file);
243 token = get_tokens_file(name);
244 if (!token)
245 return;
246 if (token_type(token) != TOKEN_STREAMBEGIN)
247 return;
248 token = token->next;
249 while (token_type(token) != TOKEN_STREAMEND) {
250 if (token_type(token) != TOKEN_IDENT)
251 return;
252 func = show_ident(token->ident);
253 if (assign)
254 add_function_assign_hook(func, call_back, info);
255 else
256 add_function_hook(func, call_back, info);
257 token = token->next;
259 clear_token_alloc();
262 void register_func_hooks_from_file(const char *file,
263 func_hook *call_back, void *info)
265 register_funcs_from_file_helper(file, call_back, info, false);
268 void register_assign_hooks_from_file(const char *file,
269 func_hook *call_back, void *info)
271 register_funcs_from_file_helper(file, call_back, info, true);
274 void add_implied_return_hook(const char *look_for,
275 implied_return_hook *call_back,
276 void *info)
278 struct fcall_back *cb;
280 cb = alloc_fcall_back(IMPLIED_RETURN, call_back, info);
281 add_callback(func_hash, look_for, cb);
284 static void db_helper(struct expression *expr, param_key_hook *call_back, int param, const char *key, void *info)
286 char *name;
287 struct symbol *sym;
289 if (param == -2) {
290 call_back(expr, key, NULL, info);
291 return;
294 name = get_name_sym_from_param_key(expr, param, key, &sym);
295 if (!name || !sym)
296 goto free;
298 call_back(expr, name, sym, info);
299 free:
300 free_string(name);
303 static struct expression *get_parent_assignment(struct expression *expr)
305 struct expression *parent;
306 int cnt = 0;
308 if (expr->type == EXPR_ASSIGNMENT)
309 return NULL;
311 parent = expr_get_fake_parent_expr(expr);
312 if (parent && parent->type == EXPR_ASSIGNMENT)
313 return parent;
315 parent = expr;
316 while (true) {
317 parent = expr_get_parent_expr(parent);
318 if (!parent || ++cnt >= 5)
319 break;
320 if (parent->type == EXPR_CAST)
321 continue;
322 if (parent->type == EXPR_PREOP && parent->op == '(')
323 continue;
324 break;
327 if (parent && parent->type == EXPR_ASSIGNMENT)
328 return parent;
329 return NULL;
332 static void param_key_function(const char *fn, struct expression *expr, void *data)
334 struct param_key_data *pkd = data;
335 struct expression *parent;
337 parent = get_parent_assignment(expr);
338 if (parent)
339 expr = parent;
341 db_helper(expr, pkd->call_back, pkd->param, pkd->key, pkd->info);
344 static void param_key_expr_function(const char *fn, struct expression *expr, void *data)
346 struct param_key_data *pkd = data;
347 struct expression *parent, *arg;
349 parent = get_parent_assignment(expr);
350 if (parent)
351 expr = parent;
353 arg = gen_expr_from_param_key(expr, pkd->param, pkd->key);
354 if (!arg)
355 return;
356 pkd->expr_fn(arg);
359 static void param_key_implies_function(const char *fn, struct expression *call_expr,
360 struct expression *assign_expr, void *data)
362 struct param_key_data *pkd = data;
364 db_helper(assign_expr ?: call_expr, pkd->call_back, pkd->param, pkd->key, pkd->info);
367 static void param_key_expr_implies_function(const char *fn, struct expression *call_expr,
368 struct expression *assign_expr, void *data)
370 struct param_key_data *pkd = data;
371 struct expression *arg;
373 arg = gen_expr_from_param_key(assign_expr ?: call_expr, pkd->param, pkd->key);
374 if (!arg)
375 return;
376 pkd->expr_fn(arg);
379 static struct param_key_data *alloc_pkd(param_key_hook *call_back, int param, const char *key, void *info)
381 struct param_key_data *pkd;
383 pkd = malloc(sizeof(*pkd));
384 pkd->call_back = call_back;
385 pkd->param = param;
386 pkd->key = alloc_string(key);
387 pkd->info = info;
389 return pkd;
392 static struct param_key_data *alloc_pked(expr_func *call_back, int param, const char *key, void *info)
394 struct param_key_data *pkd;
396 pkd = alloc_pkd(NULL, param, key, info);
397 pkd->expr_fn = call_back;
399 return pkd;
402 void add_function_param_key_hook_early(const char *look_for, param_key_hook *call_back,
403 int param, const char *key, void *info)
405 struct param_key_data *pkd;
407 if (param == -1) {
408 printf("pointless early hook for '%s'", look_for);
409 return;
412 pkd = alloc_pkd(call_back, param, key, info);
413 add_function_hook_early(look_for, &param_key_function, pkd);
416 void add_function_param_key_hook(const char *look_for, param_key_hook *call_back,
417 int param, const char *key, void *info)
419 struct param_key_data *pkd;
421 pkd = alloc_pkd(call_back, param, key, info);
422 if (param == -1)
423 add_function_assign_hook(look_for, &param_key_function, pkd);
424 else
425 add_function_hook(look_for, &param_key_function, pkd);
428 void add_param_key_expr_hook(const char *look_for, expr_func *call_back,
429 int param, const char *key, void *info)
431 struct param_key_data *pkd;
433 pkd = alloc_pked(call_back, param, key, info);
435 if (param == -1)
436 add_function_assign_hook(look_for, &param_key_expr_function, pkd);
437 else
438 add_function_hook(look_for, &param_key_expr_function, pkd);
441 void add_function_param_key_hook_late(const char *look_for, param_key_hook *call_back,
442 int param, const char *key, void *info)
444 struct param_key_data *pkd;
446 pkd = alloc_pkd(call_back, param, key, info);
447 add_function_hook_late(look_for, &param_key_function, pkd);
450 void return_implies_param_key(const char *look_for, sval_t start, sval_t end,
451 param_key_hook *call_back,
452 int param, const char *key, void *info)
454 struct param_key_data *pkd;
456 pkd = alloc_pkd(call_back, param, key, info);
457 return_implies_state_sval(look_for, start, end, &param_key_implies_function, pkd);
460 void return_implies_param_key_exact(const char *look_for, sval_t start, sval_t end,
461 param_key_hook *call_back,
462 int param, const char *key, void *info)
464 struct param_key_data *pkd;
466 pkd = alloc_pkd(call_back, param, key, info);
467 return_implies_exact(look_for, start, end, &param_key_implies_function, pkd);
470 void return_implies_param_key_expr(const char *look_for, sval_t start, sval_t end,
471 expr_func *call_back,
472 int param, const char *key, void *info)
474 struct param_key_data *pkd;
476 pkd = alloc_pked(call_back, param, key, info);
477 return_implies_state_sval(look_for, start, end, &param_key_expr_implies_function, pkd);
480 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
481 void *info)
483 struct fcall_back *cb;
485 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
486 add_callback(func_hash, look_for, cb);
489 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
490 void *info)
492 struct fcall_back *cb;
494 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
495 add_callback(func_hash, look_for, cb);
498 void return_implies_state(const char *look_for, long long start, long long end,
499 implication_hook *call_back, void *info)
501 struct fcall_back *cb;
503 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
504 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
505 add_callback(func_hash, look_for, cb);
508 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
509 implication_hook *call_back, void *info)
511 struct fcall_back *cb;
513 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
514 cb->range = alloc_range_perm(start, end);
515 add_callback(func_hash, look_for, cb);
518 void return_implies_exact(const char *look_for, sval_t start, sval_t end,
519 implication_hook *call_back, void *info)
521 struct fcall_back *cb;
523 cb = alloc_fcall_back(RANGED_EXACT, call_back, info);
524 cb->range = alloc_range_perm(start, end);
525 add_callback(func_hash, look_for, cb);
528 static struct return_implies_callback *alloc_db_return_callback(int type, bool param_key, void *callback)
530 struct return_implies_callback *cb;
532 cb = __alloc_return_implies_callback(0);
533 cb->type = type;
534 cb->param_key = param_key;
535 cb->callback = callback;
537 return cb;
540 void select_return_states_hook(int type, return_implies_hook *callback)
542 struct return_implies_callback *cb;
544 cb = alloc_db_return_callback(type, false, callback);
545 add_ptr_list(&db_return_states_list, cb);
548 static void call_db_return_callback(struct db_callback_info *db_info,
549 struct return_implies_callback *cb,
550 int param, char *key, char *value)
552 if (cb->param_key) {
553 db_helper(db_info->expr, cb->pk_callback, param, key, NULL);
554 add_ptr_list(&db_info->called, cb);
555 } else {
556 cb->callback(db_info->expr, param, key, value);
560 void select_return_param_key(int type, param_key_hook *callback)
562 struct return_implies_callback *cb;
564 cb = alloc_db_return_callback(type, true, callback);
565 add_ptr_list(&db_return_states_list, cb);
568 void select_return_states_before(void_fn *fn)
570 add_ptr_list(&return_states_before, fn);
573 void select_return_states_after(void_fn *fn)
575 add_ptr_list(&return_states_after, fn);
578 void add_return_string_hook(string_hook *fn)
580 add_ptr_list(&return_string_hooks, fn);
583 static bool call_call_backs(struct call_back_list *list, int type,
584 const char *fn, struct expression *expr)
586 struct fcall_back *tmp;
587 bool handled = false;
589 FOR_EACH_PTR(list, tmp) {
590 if (tmp->type == type) {
591 (tmp->u.call_back)(fn, expr, tmp->info);
592 handled = true;
594 } END_FOR_EACH_PTR(tmp);
596 return handled;
599 static void call_function_hooks(struct expression *expr, enum fn_hook_type type)
601 struct call_back_list *call_backs;
602 const char *fn_name;
604 while (expr->type == EXPR_ASSIGNMENT)
605 expr = strip_expr(expr->right);
606 if (expr->type != EXPR_CALL)
607 return;
609 fn_name = get_fn_name(expr->fn);
610 call_backs = get_call_backs(fn_name);
611 if (!call_backs)
612 return;
614 call_call_backs(call_backs, type, fn_name, expr);
617 static void call_return_states_after_hooks(struct expression *expr)
619 call_void_fns(return_states_after);
620 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER_DB);
621 call_function_hooks(expr, REGULAR_CALL_LATE);
624 static void call_ranged_call_backs(struct call_back_list *list,
625 const char *fn, struct expression *call_expr,
626 struct expression *assign_expr)
628 struct fcall_back *tmp;
630 FOR_EACH_PTR(list, tmp) {
631 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
632 } END_FOR_EACH_PTR(tmp);
635 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
636 struct data_range *drange)
638 struct call_back_list *ret = NULL;
639 struct fcall_back *tmp;
641 FOR_EACH_PTR(list, tmp) {
642 if (tmp->type != RANGED_CALL &&
643 tmp->type != RANGED_EXACT)
644 continue;
645 if (ranges_equiv(tmp->range, drange))
646 add_ptr_list(&ret, tmp);
647 } END_FOR_EACH_PTR(tmp);
648 return ret;
651 static bool in_list_exact_sval(struct range_list *list, struct data_range *drange)
653 struct data_range *tmp;
655 FOR_EACH_PTR(list, tmp) {
656 if (ranges_equiv(tmp, drange))
657 return true;
658 } END_FOR_EACH_PTR(tmp);
659 return false;
663 * The assign_ranged_funcs() function is called when we have no data from the DB.
665 static bool assign_ranged_funcs(const char *fn, struct expression *expr,
666 struct call_back_list *call_backs)
668 struct fcall_back *tmp;
669 struct sm_state *sm;
670 char *var_name;
671 struct symbol *sym;
672 struct smatch_state *estate;
673 struct stree *tmp_stree;
674 struct stree *final_states = NULL;
675 struct range_list *handled_ranges = NULL;
676 struct range_list *unhandled_rl;
677 struct call_back_list *same_range_call_backs = NULL;
678 struct expression *call;
679 struct range_list *rl;
680 int handled = false;
682 if (!call_backs)
683 return false;
685 var_name = expr_to_var_sym(expr->left, &sym);
686 if (!var_name || !sym)
687 goto free;
689 call = strip_expr(expr->right);
691 FOR_EACH_PTR(call_backs, tmp) {
692 if (tmp->type != RANGED_CALL &&
693 tmp->type != RANGED_EXACT)
694 continue;
696 if (in_list_exact_sval(handled_ranges, tmp->range))
697 continue;
698 __push_fake_cur_stree();
699 tack_on(&handled_ranges, tmp->range);
701 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
702 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
703 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
705 rl = alloc_rl(tmp->range->min, tmp->range->max);
706 rl = cast_rl(get_type(expr->left), rl);
707 estate = alloc_estate_rl(rl);
708 set_extra_mod(var_name, sym, expr->left, estate);
710 tmp_stree = __pop_fake_cur_stree();
711 merge_fake_stree(&final_states, tmp_stree);
712 free_stree(&tmp_stree);
713 handled = true;
714 } END_FOR_EACH_PTR(tmp);
716 unhandled_rl = rl_filter(alloc_whole_rl(get_type(call)), handled_ranges);
717 if (unhandled_rl) {
718 __push_fake_cur_stree();
719 rl = cast_rl(get_type(expr->left), unhandled_rl);
720 estate = alloc_estate_rl(rl);
721 set_extra_mod(var_name, sym, expr->left, estate);
722 tmp_stree = __pop_fake_cur_stree();
723 merge_fake_stree(&final_states, tmp_stree);
724 free_stree(&tmp_stree);
727 FOR_EACH_SM(final_states, sm) {
728 __set_sm(sm);
729 } END_FOR_EACH_SM(sm);
731 free_stree(&final_states);
732 free:
733 free_string(var_name);
734 return handled;
737 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
739 struct call_back_list *call_backs;
740 struct fcall_back *tmp;
741 const char *fn_name;
742 struct data_range *value_range;
743 struct stree *true_states = NULL;
744 struct stree *false_states = NULL;
745 struct stree *tmp_stree;
747 *implied_true = NULL;
748 *implied_false = NULL;
749 fn_name = get_fn_name(expr->fn);
750 call_backs = get_call_backs(fn_name);
751 if (!call_backs)
752 return;
753 value_range = alloc_range(sval, sval);
755 /* set true states */
756 __push_fake_cur_stree();
757 FOR_EACH_PTR(call_backs, tmp) {
758 if (tmp->type != RANGED_CALL &&
759 tmp->type != RANGED_EXACT)
760 continue;
761 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
762 continue;
763 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
764 } END_FOR_EACH_PTR(tmp);
765 tmp_stree = __pop_fake_cur_stree();
766 merge_fake_stree(&true_states, tmp_stree);
767 free_stree(&tmp_stree);
769 /* set false states */
770 __push_fake_cur_stree();
771 FOR_EACH_PTR(call_backs, tmp) {
772 if (tmp->type != RANGED_CALL &&
773 tmp->type != RANGED_EXACT)
774 continue;
775 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
776 continue;
777 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
778 } END_FOR_EACH_PTR(tmp);
779 tmp_stree = __pop_fake_cur_stree();
780 merge_fake_stree(&false_states, tmp_stree);
781 free_stree(&tmp_stree);
783 *implied_true = true_states;
784 *implied_false = false_states;
787 static void set_implied_states(struct db_callback_info *db_info)
789 struct sm_state *sm;
791 FOR_EACH_SM(db_info->implied, sm) {
792 __set_sm(sm);
793 } END_FOR_EACH_SM(sm);
795 free_stree(&db_info->implied);
798 static void store_return_state(struct db_callback_info *db_info, const char *ret_str, struct smatch_state *state)
800 db_info->ret_str = alloc_sname(ret_str),
801 db_info->ret_state = state;
804 static struct expression_list *unfaked_calls;
806 struct expression *get_unfaked_call(void)
808 return last_ptr_list((struct ptr_list *)unfaked_calls);
811 static void store_unfaked_call(struct expression *expr)
813 push_expression(&unfaked_calls, expr);
816 static void clear_unfaked_call(void)
818 delete_ptr_list_last((struct ptr_list **)&unfaked_calls);
821 void fake_param_assign_helper(struct expression *call, struct expression *fake_assign, bool shallow)
823 store_unfaked_call(call);
824 __in_fake_parameter_assign++;
825 parse_assignment(fake_assign, true);
826 __in_fake_parameter_assign--;
827 clear_unfaked_call();
830 static bool fake_a_param_assignment(struct expression *expr, const char *ret_str, struct smatch_state *orig)
832 struct expression *arg, *left, *right, *tmp, *fake_assign;
833 char *p;
834 int param;
835 char buf[256];
836 char *str;
838 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
839 return false;
840 left = expr->left;
841 right = expr->right;
843 while (right->type == EXPR_ASSIGNMENT)
844 right = strip_expr(right->right);
845 if (!right || right->type != EXPR_CALL)
846 return false;
848 p = strchr(ret_str, '[');
849 if (!p)
850 return false;
852 p++;
853 if (p[0] == '=' && p[1] == '=')
854 p += 2;
855 if (p[0] != '$')
856 return false;
858 snprintf(buf, sizeof(buf), "%s", p);
860 p = buf;
861 p += 1;
862 param = strtol(p, &p, 10);
864 p = strchr(p, ']');
865 if (!p || *p != ']')
866 return false;
867 *p = '\0';
869 arg = get_argument_from_call_expr(right->args, param);
870 if (!arg)
871 return false;
873 /* There should be a get_other_name() function which returns an expr */
874 tmp = get_assigned_expr(arg);
875 if (tmp)
876 arg = tmp;
879 * This is a sanity check to prevent side effects from evaluating stuff
880 * twice.
882 str = expr_to_chunk_sym_vsl(arg, NULL, NULL);
883 if (!str)
884 return false;
885 free_string(str);
887 right = gen_expression_from_key(arg, buf);
888 if (!right) /* Mostly fails for binops like [$0 + 4032] */
889 return false;
890 fake_assign = assign_expression(left, '=', right);
891 fake_param_assign_helper(expr, fake_assign, false);
894 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
895 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
896 * parts have to be considered. We use _nomod() because it's not really
897 * another modification, it's just a clarification.
900 if (estate_rl(orig)) {
901 struct smatch_state *faked;
902 struct range_list *rl;
904 faked = get_extra_state(left);
905 if (estate_rl(faked)) {
906 rl = rl_intersection(estate_rl(faked), estate_rl(orig));
907 if (rl)
908 set_extra_expr_nomod(left, alloc_estate_rl(rl));
912 return true;
915 static void fake_return_assignment(struct db_callback_info *db_info, int type, int param, char *key, char *value)
917 struct expression *call, *left, *right, *assign;
918 int right_param;
920 if (type != PARAM_COMPARE)
921 return;
923 call = db_info->expr;
924 while (call && call->type == EXPR_ASSIGNMENT)
925 call = strip_expr(call->right);
926 if (!call || call->type != EXPR_CALL)
927 return;
929 // TODO: This only handles "$->foo = arg" and not "$->foo = arg->bar".
930 if (param != -1)
931 return;
932 if (!value || strncmp(value, "== $", 4) != 0)
933 return;
934 if (!isdigit(value[4]) || value[5] != '\0')
935 return;
936 right_param = atoi(value + 4);
938 left = gen_expr_from_param_key(db_info->expr, param, key);
939 if (!left)
940 return;
941 right = get_argument_from_call_expr(call->args, right_param);
943 assign = assign_expression(left, '=', right);
944 push_expression(&db_info->fake_param_assign_stack, assign);
947 static void set_fresh_mtag_returns(struct db_callback_info *db_info)
949 struct expression *expr;
950 struct smatch_state *state;
952 if (!db_info->ret_state)
953 return;
955 if (!db_info->expr ||
956 db_info->expr->type != EXPR_ASSIGNMENT ||
957 db_info->expr->op != '=')
958 return;
960 expr = db_info->expr->left;
962 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
963 state = get_mtag_return(db_info->expr, state);
964 if (!state)
965 return;
967 set_real_absolute(expr, state);
968 set_extra_expr_mod(expr, state);
971 static void set_return_assign_state(struct db_callback_info *db_info)
973 struct expression *expr = db_info->expr->left;
974 struct expression *fake_assign;
975 struct smatch_state *state;
976 bool was_set = false;
978 if (!db_info->ret_state)
979 return;
981 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
982 if (!fake_a_param_assignment(db_info->expr, db_info->ret_str, state)) {
983 set_extra_expr_mod(expr, state);
984 was_set = true;
987 while ((fake_assign = pop_expression(&db_info->fake_param_assign_stack))) {
988 struct range_list *left, *right;
991 * Originally, I tried to do this as a assignment to record that
992 * a = frob(b) implies that "a->foo == b->foo" etc. But that
993 * caused a problem because then it was recorded that "a->foo"
994 * was modified and recorded as a PARAM_SET in the database.
996 * So now, instead of faking an assignment we use
997 * set_extra_expr_nomod() but it's still recorded as an
998 * assignment in the ->fake_param_assign_stack for legacy
999 * reasons and because it's a handy way to store a left/right
1000 * pair.
1003 get_absolute_rl(fake_assign->left, &left);
1004 get_absolute_rl(fake_assign->right, &right);
1005 right = cast_rl(get_type(fake_assign->left), right);
1006 // FIXME: add some sanity checks
1007 // FIXME: preserve the sm state if possible
1008 set_extra_expr_nomod(fake_assign->left, alloc_estate_rl(right));
1009 was_set = true;
1012 if (!was_set)
1013 set_extra_expr_mod(expr, state);
1016 static void set_other_side_state(struct db_callback_info *db_info)
1018 struct expression *expr = db_info->var_expr;
1019 struct smatch_state *state;
1021 if (!db_info->ret_state)
1022 return;
1024 // TODO: faked_assign set ==$ equiv here
1026 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1027 set_extra_expr_nomod(expr, state);
1028 db_info->ret_state = NULL;
1029 db_info->ret_str = NULL;
1032 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
1034 char *str;
1035 long long param;
1036 struct expression *arg;
1037 struct range_list *orig;
1039 // TODO: faked_assign This needs to be handled in the assignment code
1041 str = strstr(ret_string, "==$");
1042 if (!str)
1043 return;
1044 str += 3;
1045 param = strtoll(str, NULL, 10);
1046 arg = get_argument_from_call_expr(call->args, param);
1047 if (!arg)
1048 return;
1049 get_absolute_rl(arg, &orig);
1050 rl = rl_intersection(orig, rl);
1051 if (!rl)
1052 return;
1053 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
1056 static bool impossible_limit(struct db_callback_info *db_info, int param, char *key, char *value)
1058 struct expression *expr = db_info->expr;
1059 struct expression *arg;
1060 struct smatch_state *state;
1061 struct range_list *passed;
1062 struct range_list *limit;
1063 struct symbol *compare_type;
1065 while (expr->type == EXPR_ASSIGNMENT)
1066 expr = strip_expr(expr->right);
1067 if (expr->type != EXPR_CALL)
1068 return false;
1070 arg = get_argument_from_call_expr(expr->args, param);
1071 if (!arg)
1072 return false;
1074 if (strcmp(key, "$") == 0) {
1075 if (!get_implied_rl(arg, &passed))
1076 return false;
1078 compare_type = get_arg_type(expr->fn, param);
1079 } else {
1080 char *name;
1081 struct symbol *sym;
1083 name = get_variable_from_key(arg, key, &sym);
1084 if (!name || !sym)
1085 return false;
1087 state = get_state(SMATCH_EXTRA, name, sym);
1088 if (!state) {
1089 free_string(name);
1090 return false;
1092 passed = estate_rl(state);
1093 if (!passed || is_whole_rl(passed)) {
1094 free_string(name);
1095 return false;
1098 compare_type = get_member_type_from_key(arg, key);
1101 passed = cast_rl(compare_type, passed);
1102 call_results_to_rl(expr, compare_type, value, &limit);
1103 if (!limit || is_whole_rl(limit))
1104 return false;
1105 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
1106 return false;
1107 if (option_debug || local_debug || debug_db)
1108 sm_msg("impossible: %d '%s' limit '%s' == '%s' return='%s'", param, key, show_rl(passed), value, db_info->ret_str);
1109 return true;
1112 static bool is_impossible_data(int type, struct db_callback_info *db_info, int param, char *key, char *value)
1114 if (type == PARAM_LIMIT && impossible_limit(db_info, param, key, value))
1115 return true;
1116 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(db_info->expr, param, key, value)) {
1117 if (local_debug || debug_db)
1118 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
1119 return true;
1121 return false;
1124 static bool func_type_mismatch(struct expression *expr, const char *value)
1126 struct symbol *type;
1128 /* This makes faking returns easier */
1129 if (!value || value[0] == '\0')
1130 return false;
1132 while (expr->type == EXPR_ASSIGNMENT)
1133 expr = strip_expr(expr->right);
1136 * Short cut: We only care about function pointers that are struct
1137 * members.
1140 if (expr->fn->type == EXPR_SYMBOL)
1141 return false;
1143 type = get_type(expr->fn);
1144 if (!type)
1145 return false;
1146 if (type->type == SYM_PTR)
1147 type = get_real_base_type(type);
1149 if (strcmp(type_to_str(type), value) == 0)
1150 return false;
1152 return true;
1155 static void process_return_states(struct db_callback_info *db_info)
1157 struct stree *stree;
1159 set_implied_states(db_info);
1160 set_fresh_mtag_returns(db_info);
1161 parse_fake_calls();
1162 free_ptr_list(&db_info->called);
1163 stree = __pop_fake_cur_stree();
1164 if (debug_db) {
1165 sm_msg("States from DB: %s expr='%s' ret_str='%s' rl='%s' state='%s'",
1166 db_info->cull ? "Culling" : "Merging",
1167 expr_to_str(db_info->expr),
1168 db_info->ret_str, show_rl(db_info->rl),
1169 db_info->ret_state ? db_info->ret_state->name : "<none>");
1170 __print_stree(stree);
1173 if (!db_info->cull) {
1174 merge_fake_stree(&db_info->stree, stree);
1175 db_info->states_merged = true;
1177 free_stree(&stree);
1179 db_info->ret_state = NULL;
1180 db_info->ret_str = NULL;
1183 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
1185 struct db_callback_info *db_info = _info;
1186 struct range_list *var_rl = db_info->rl;
1187 struct range_list *ret_range;
1188 int type, param;
1189 char *ret_str, *key, *value;
1190 struct return_implies_callback *tmp;
1191 int return_id;
1192 int comparison;
1194 if (argc != 6)
1195 return 0;
1197 return_id = atoi(argv[0]);
1198 ret_str = argv[1];
1199 type = atoi(argv[2]);
1200 param = atoi(argv[3]);
1201 key = argv[4];
1202 value = argv[5];
1204 db_info->has_states = 1;
1205 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1206 set_other_side_state(db_info);
1207 process_return_states(db_info);
1208 __push_fake_cur_stree();
1209 db_info->cull = 0;
1211 db_info->prev_return_id = return_id;
1213 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1214 db_info->cull = 1;
1215 if (db_info->cull)
1216 return 0;
1217 if (type == CULL_PATH) {
1218 db_info->cull = 1;
1219 return 0;
1222 if (is_impossible_data(type, db_info, param, key, value)) {
1223 db_info->cull = 1;
1224 return 0;
1227 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1228 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1229 if (!ret_range)
1230 ret_range = alloc_whole_rl(get_type(db_info->expr));
1232 comparison = db_info->comparison;
1233 if (db_info->left)
1234 comparison = flip_comparison(comparison);
1236 if (db_info->true_side) {
1237 if (!possibly_true_rl(var_rl, comparison, ret_range))
1238 return 0;
1239 if (type == PARAM_LIMIT)
1240 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1241 else if (type > PARAM_LIMIT)
1242 set_implied_states(db_info);
1243 filter_by_comparison(&var_rl, comparison, ret_range);
1244 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
1245 } else {
1246 if (!possibly_false_rl(var_rl, comparison, ret_range))
1247 return 0;
1248 if (type == PARAM_LIMIT)
1249 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1250 else if (type > PARAM_LIMIT)
1251 set_implied_states(db_info);
1252 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
1253 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
1256 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
1258 if (type == INTERNAL) {
1259 set_state(-1, "unnull_path", NULL, &true_state);
1260 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1261 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
1264 FOR_EACH_PTR(db_info->callbacks, tmp) {
1265 if (tmp->type == type)
1266 call_db_return_callback(db_info, tmp, param, key, value);
1267 } END_FOR_EACH_PTR(tmp);
1269 fake_return_assignment(db_info, type, param, key, value);
1271 return 0;
1274 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
1276 struct stree *orig_states;
1277 struct stree *true_states;
1278 struct stree *false_states;
1279 struct sm_state *sm;
1280 struct db_callback_info db_info = {};
1281 struct expression *var_expr;
1282 struct expression *call_expr;
1283 struct range_list *rl;
1284 int call_on_left;
1286 orig_states = clone_stree(__get_cur_stree());
1288 /* legacy cruft. need to fix call_implies_callbacks(). */
1289 call_on_left = 1;
1290 call_expr = left;
1291 var_expr = right;
1292 if (left->type != EXPR_CALL) {
1293 call_on_left = 0;
1294 call_expr = right;
1295 var_expr = left;
1298 get_absolute_rl(var_expr, &rl);
1300 db_info.comparison = comparison;
1301 db_info.expr = call_expr;
1302 db_info.rl = rl;
1303 db_info.left = call_on_left;
1304 db_info.callbacks = db_return_states_list;
1305 db_info.var_expr = var_expr;
1307 call_void_fns(return_states_before);
1309 db_info.true_side = 1;
1310 db_info.stree = NULL;
1311 db_info.prev_return_id = -1;
1312 __push_fake_cur_stree();
1313 sql_select_return_states("return_id, return, type, parameter, key, value",
1314 call_expr, db_compare_callback, &db_info);
1315 set_other_side_state(&db_info);
1316 process_return_states(&db_info);
1317 true_states = db_info.stree;
1318 if (!true_states && db_info.has_states) {
1319 __push_fake_cur_stree();
1320 set_path_impossible();
1321 true_states = __pop_fake_cur_stree();
1324 nullify_path();
1325 __unnullify_path();
1326 FOR_EACH_SM(orig_states, sm) {
1327 __set_sm_cur_stree(sm);
1328 } END_FOR_EACH_SM(sm);
1330 db_info.true_side = 0;
1331 db_info.stree = NULL;
1332 db_info.prev_return_id = -1;
1333 db_info.cull = 0;
1334 __push_fake_cur_stree();
1335 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
1336 db_compare_callback, &db_info);
1337 set_other_side_state(&db_info);
1338 process_return_states(&db_info);
1339 false_states = db_info.stree;
1340 if (!false_states && db_info.has_states) {
1341 __push_fake_cur_stree();
1342 set_path_impossible();
1343 false_states = __pop_fake_cur_stree();
1346 nullify_path();
1347 __unnullify_path();
1348 FOR_EACH_SM(orig_states, sm) {
1349 __set_sm_cur_stree(sm);
1350 } END_FOR_EACH_SM(sm);
1352 free_stree(&orig_states);
1354 FOR_EACH_SM(true_states, sm) {
1355 __set_true_false_sm(sm, NULL);
1356 } END_FOR_EACH_SM(sm);
1357 FOR_EACH_SM(false_states, sm) {
1358 __set_true_false_sm(NULL, sm);
1359 } END_FOR_EACH_SM(sm);
1361 free_stree(&true_states);
1362 free_stree(&false_states);
1364 if (!db_info.states_merged)
1365 mark_call_params_untracked(call_expr);
1367 call_return_states_after_hooks(call_expr);
1369 FOR_EACH_SM(implied_true, sm) {
1370 __set_true_false_sm(sm, NULL);
1371 } END_FOR_EACH_SM(sm);
1372 FOR_EACH_SM(implied_false, sm) {
1373 __set_true_false_sm(NULL, sm);
1374 } END_FOR_EACH_SM(sm);
1377 void function_comparison(struct expression *left, int comparison, struct expression *right)
1379 struct expression *var_expr;
1380 struct expression *call_expr;
1381 struct stree *implied_true = NULL;
1382 struct stree *implied_false = NULL;
1383 struct range_list *rl;
1384 sval_t sval;
1385 int call_on_left;
1387 // TODO: faked_assign delete this
1388 // condition calls should be faked and then handled as assignments
1389 // this code is a lazy work around
1391 if (unreachable())
1392 return;
1394 /* legacy cruft. need to fix call_implies_callbacks(). */
1395 call_on_left = 1;
1396 call_expr = left;
1397 var_expr = right;
1398 if (left->type != EXPR_CALL) {
1399 call_on_left = 0;
1400 call_expr = right;
1401 var_expr = left;
1404 get_absolute_rl(var_expr, &rl);
1406 if (rl_to_sval(rl, &sval))
1407 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
1409 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
1410 free_stree(&implied_true);
1411 free_stree(&implied_false);
1414 static void call_ranged_return_hooks(struct db_callback_info *db_info)
1416 struct call_back_list *call_backs;
1417 struct range_list *range_rl;
1418 struct expression *expr;
1419 struct fcall_back *tmp;
1420 const char *fn_name;
1422 expr = strip_expr(db_info->expr);
1423 while (expr->type == EXPR_ASSIGNMENT)
1424 expr = strip_expr(expr->right);
1425 if (expr->type != EXPR_CALL)
1426 return;
1428 fn_name = get_fn_name(expr->fn);
1429 call_backs = get_call_backs(fn_name);
1430 FOR_EACH_PTR(call_backs, tmp) {
1431 if (tmp->type != RANGED_CALL)
1432 continue;
1433 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1434 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1435 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
1436 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1437 } END_FOR_EACH_PTR(tmp);
1439 FOR_EACH_PTR(call_backs, tmp) {
1440 if (tmp->type != RANGED_EXACT)
1441 continue;
1442 if (!estate_rl(db_info->ret_state))
1443 continue;
1445 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1446 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1449 * If there is an returned value out of range then this is not
1450 * an exact match. In other words, "0,4096-ptr_max" is not
1451 * necessarily a valid match.
1454 if (remove_range(estate_rl(db_info->ret_state),
1455 rl_min(range_rl), rl_max(range_rl)))
1456 continue;
1457 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1458 } END_FOR_EACH_PTR(tmp);
1461 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1463 struct db_callback_info *db_info = _info;
1464 struct range_list *ret_range;
1465 int type, param;
1466 char *ret_str, *key, *value;
1467 struct return_implies_callback *tmp;
1468 int return_id;
1470 if (argc != 6)
1471 return 0;
1473 return_id = atoi(argv[0]);
1474 ret_str = argv[1];
1475 type = atoi(argv[2]);
1476 param = atoi(argv[3]);
1477 key = argv[4];
1478 value = argv[5];
1480 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1481 call_ranged_return_hooks(db_info);
1482 set_return_assign_state(db_info);
1483 process_return_states(db_info);
1484 __push_fake_cur_stree();
1485 db_info->cull = 0;
1487 db_info->prev_return_id = return_id;
1489 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1490 db_info->cull = 1;
1491 if (db_info->cull)
1492 return 0;
1493 if (type == CULL_PATH) {
1494 db_info->cull = 1;
1495 return 0;
1497 if (is_impossible_data(type, db_info, param, key, value)) {
1498 db_info->cull = 1;
1499 return 0;
1502 if (type == PARAM_LIMIT)
1503 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1504 else if (type > PARAM_LIMIT)
1505 set_implied_states(db_info);
1507 db_info->handled = 1;
1508 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
1509 if (!ret_range)
1510 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
1511 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
1513 if (type == INTERNAL) {
1514 set_state(-1, "unnull_path", NULL, &true_state);
1515 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
1516 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1517 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
1520 FOR_EACH_PTR(db_return_states_list, tmp) {
1521 if (tmp->type == type)
1522 call_db_return_callback(db_info, tmp, param, key, value);
1523 } END_FOR_EACH_PTR(tmp);
1525 fake_return_assignment(db_info, type, param, key, value);
1527 return 0;
1530 static int db_return_states_assign(struct expression *expr)
1532 struct expression *right;
1533 struct sm_state *sm;
1534 struct db_callback_info db_info = {};
1536 right = strip_expr(expr->right);
1538 db_info.prev_return_id = -1;
1539 db_info.expr = expr;
1540 db_info.stree = NULL;
1541 db_info.handled = 0;
1543 call_void_fns(return_states_before);
1545 __push_fake_cur_stree();
1546 sql_select_return_states("return_id, return, type, parameter, key, value",
1547 right, db_assign_return_states_callback, &db_info);
1548 if (option_debug) {
1549 sm_msg("%s return_id %d return_ranges %s",
1550 db_info.cull ? "culled" : "merging",
1551 db_info.prev_return_id,
1552 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
1554 if (db_info.handled)
1555 call_ranged_return_hooks(&db_info);
1556 set_return_assign_state(&db_info);
1557 process_return_states(&db_info);
1559 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
1560 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1561 set_path_impossible();
1563 FOR_EACH_SM(db_info.stree, sm) {
1564 __set_sm(sm);
1565 } END_FOR_EACH_SM(sm);
1567 free_stree(&db_info.stree);
1569 if (!db_info.states_merged)
1570 mark_call_params_untracked(right);
1572 call_return_states_after_hooks(right);
1574 return db_info.handled;
1577 static bool handle_implied_return(struct expression *expr)
1579 struct range_list *rl;
1581 if (!get_implied_return(expr->right, &rl))
1582 return false;
1583 rl = cast_rl(get_type(expr->left), rl);
1584 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1585 return true;
1588 static void match_assign_call(struct expression *expr)
1590 struct call_back_list *call_backs;
1591 const char *fn_name;
1592 struct expression *right;
1593 int handled = 0;
1594 struct range_list *rl;
1596 if (expr->op != '=')
1597 return;
1599 right = strip_expr(expr->right);
1600 if (is_fake_call(right))
1601 return;
1603 fn_name = get_fn_name(right->fn);
1604 call_backs = get_call_backs(fn_name);
1607 * The ordering here is sort of important.
1608 * One example, of how this matters is that when we do:
1610 * len = strlen(str);
1612 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1613 * They use implied_return and function_assign_hook respectively.
1614 * We want to get the implied return first before we do the function
1615 * assignment hook otherwise we end up writing the wrong thing for len
1616 * in smatch_extra.c because we assume that it already holds the
1617 * strlen() when we haven't set it yet.
1620 if (db_return_states_assign(expr))
1621 handled = 1;
1622 else
1623 handled = assign_ranged_funcs(fn_name, expr, call_backs);
1624 handled |= handle_implied_return(expr);
1627 call_call_backs(call_backs, ASSIGN_CALL, fn_name, expr);
1629 if (handled)
1630 return;
1632 /* assignment wasn't handled at all */
1633 get_absolute_rl(expr->right, &rl);
1634 rl = cast_rl(get_type(expr->left), rl);
1635 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1638 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1640 struct db_callback_info *db_info = _info;
1641 struct range_list *ret_range;
1642 int type, param;
1643 char *ret_str, *key, *value;
1644 struct return_implies_callback *tmp;
1645 int return_id;
1647 if (argc != 6)
1648 return 0;
1650 return_id = atoi(argv[0]);
1651 ret_str = argv[1];
1652 type = atoi(argv[2]);
1653 param = atoi(argv[3]);
1654 key = argv[4];
1655 value = argv[5];
1657 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1658 call_ranged_return_hooks(db_info);
1659 process_return_states(db_info);
1660 __push_fake_cur_stree();
1661 __unnullify_path();
1662 db_info->cull = 0;
1664 db_info->prev_return_id = return_id;
1666 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1667 db_info->cull = 1;
1668 if (db_info->cull)
1669 return 0;
1670 if (type == CULL_PATH) {
1671 db_info->cull = 1;
1672 return 0;
1674 if (is_impossible_data(type, db_info, param, key, value)) {
1675 db_info->cull = 1;
1676 return 0;
1679 if (type == PARAM_LIMIT)
1680 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1681 else if (type > PARAM_LIMIT)
1682 set_implied_states(db_info);
1684 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1685 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1687 if (type == INTERNAL) {
1688 struct smatch_state *state;
1690 set_state(-1, "unnull_path", NULL, &true_state);
1691 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1692 state = alloc_estate_rl(ret_range);
1693 store_return_state(db_info, ret_str, state);
1696 FOR_EACH_PTR(db_return_states_list, tmp) {
1697 if (tmp->type == type)
1698 call_db_return_callback(db_info, tmp, param, key, value);
1699 } END_FOR_EACH_PTR(tmp);
1701 fake_return_assignment(db_info, type, param, key, value);
1703 return 0;
1706 static void db_return_states(struct expression *expr)
1708 struct sm_state *sm;
1709 struct db_callback_info db_info = {};
1711 if (!__get_cur_stree()) /* no return functions */
1712 return;
1714 db_info.prev_return_id = -1;
1715 db_info.expr = expr;
1716 db_info.stree = NULL;
1718 call_void_fns(return_states_before);
1720 __push_fake_cur_stree();
1721 __unnullify_path();
1722 sql_select_return_states("return_id, return, type, parameter, key, value",
1723 expr, db_return_states_callback, &db_info);
1724 call_ranged_return_hooks(&db_info);
1725 process_return_states(&db_info);
1727 FOR_EACH_SM(db_info.stree, sm) {
1728 __set_sm(sm);
1729 } END_FOR_EACH_SM(sm);
1731 free_stree(&db_info.stree);
1733 if (!db_info.states_merged)
1734 mark_call_params_untracked(expr);
1736 call_return_states_after_hooks(expr);
1739 static void db_return_states_call(struct expression *expr)
1741 if (unreachable())
1742 return;
1744 if (is_assigned_call(expr) || is_fake_assigned_call(expr))
1745 return;
1746 if (is_condition_call(expr))
1747 return;
1748 db_return_states(expr);
1751 static void match_function_call_early(struct expression *expr)
1753 call_function_hooks(expr, REGULAR_CALL_EARLY);
1756 static void match_function_call(struct expression *expr)
1758 call_function_hooks(expr, REGULAR_CALL);
1759 db_return_states_call(expr);
1760 /* If we have no database there could be unprocessed fake calls */
1761 parse_fake_calls();
1764 static void match_macro_assign(struct expression *expr)
1766 struct call_back_list *call_backs;
1767 const char *macro;
1768 struct expression *right;
1770 right = strip_expr(expr->right);
1771 macro = get_macro_name(right->pos);
1772 call_backs = search_callback(func_hash, (char *)macro);
1773 if (!call_backs)
1774 return;
1775 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1776 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1779 bool get_implied_return(struct expression *expr, struct range_list **rl)
1781 struct call_back_list *call_backs;
1782 struct fcall_back *tmp;
1783 bool handled = false;
1784 char *fn;
1786 *rl = NULL;
1788 expr = strip_expr(expr);
1789 fn = expr_to_var(expr->fn);
1790 if (!fn)
1791 goto out;
1793 call_backs = search_callback(func_hash, fn);
1795 FOR_EACH_PTR(call_backs, tmp) {
1796 if (tmp->type == IMPLIED_RETURN)
1797 handled |= (tmp->u.implied_return)(expr, tmp->info, rl);
1798 } END_FOR_EACH_PTR(tmp);
1800 out:
1801 free_string(fn);
1802 return handled;
1805 struct range_list *get_range_implications(const char *fn)
1807 struct call_back_list *call_backs;
1808 struct range_list *ret = NULL;
1809 struct fcall_back *tmp;
1811 call_backs = search_callback(func_hash, (char *)fn);
1813 FOR_EACH_PTR(call_backs, tmp) {
1814 if (tmp->type != RANGED_CALL &&
1815 tmp->type != RANGED_EXACT)
1816 continue;
1817 add_ptr_list(&ret, tmp->range);
1818 } END_FOR_EACH_PTR(tmp);
1820 return ret;
1823 void create_function_hook_hash(void)
1825 func_hash = create_function_hashtable(5000);
1828 void register_function_hooks_early(int id)
1830 add_hook(&match_function_call_early, FUNCTION_CALL_HOOK_BEFORE);
1833 void register_function_hooks(int id)
1835 add_function_data((unsigned long *)&fake_calls);
1836 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1837 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1838 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);