flow: don't call scope_guard hooks twice at the end of functions
[smatch.git] / smatch_function_hooks.c
blob7f06178f10bc1ff888d5b951f12c8490a3d26bf8
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;
977 if (!db_info->ret_state)
978 return;
980 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
981 if (!fake_a_param_assignment(db_info->expr, db_info->ret_str, state))
982 set_extra_expr_mod(expr, state);
984 while ((fake_assign = pop_expression(&db_info->fake_param_assign_stack))) {
985 struct range_list *left, *right;
988 * Originally, I tried to do this as a assignment to record that
989 * a = frob(b) implies that "a->foo == b->foo" etc. But that
990 * caused a problem because then it was recorded that "a->foo"
991 * was modified and recorded as a PARAM_SET in the database.
993 * So now, instead of faking an assignment we use
994 * set_extra_expr_nomod() but it's still recorded as an
995 * assignment in the ->fake_param_assign_stack for legacy
996 * reasons and because it's a handy way to store a left/right
997 * pair.
1000 get_absolute_rl(fake_assign->left, &left);
1001 get_absolute_rl(fake_assign->right, &right);
1002 right = cast_rl(get_type(fake_assign->left), right);
1003 // FIXME: add some sanity checks
1004 // FIXME: preserve the sm state if possible
1005 set_extra_expr_nomod(fake_assign->left, alloc_estate_rl(right));
1009 static void set_other_side_state(struct db_callback_info *db_info)
1011 struct expression *expr = db_info->var_expr;
1012 struct smatch_state *state;
1014 if (!db_info->ret_state)
1015 return;
1017 // TODO: faked_assign set ==$ equiv here
1019 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1020 set_extra_expr_nomod(expr, state);
1021 db_info->ret_state = NULL;
1022 db_info->ret_str = NULL;
1025 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
1027 char *str;
1028 long long param;
1029 struct expression *arg;
1030 struct range_list *orig;
1032 // TODO: faked_assign This needs to be handled in the assignment code
1034 str = strstr(ret_string, "==$");
1035 if (!str)
1036 return;
1037 str += 3;
1038 param = strtoll(str, NULL, 10);
1039 arg = get_argument_from_call_expr(call->args, param);
1040 if (!arg)
1041 return;
1042 get_absolute_rl(arg, &orig);
1043 rl = rl_intersection(orig, rl);
1044 if (!rl)
1045 return;
1046 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
1049 static bool impossible_limit(struct db_callback_info *db_info, int param, char *key, char *value)
1051 struct expression *expr = db_info->expr;
1052 struct expression *arg;
1053 struct smatch_state *state;
1054 struct range_list *passed;
1055 struct range_list *limit;
1056 struct symbol *compare_type;
1058 while (expr->type == EXPR_ASSIGNMENT)
1059 expr = strip_expr(expr->right);
1060 if (expr->type != EXPR_CALL)
1061 return false;
1063 arg = get_argument_from_call_expr(expr->args, param);
1064 if (!arg)
1065 return false;
1067 if (strcmp(key, "$") == 0) {
1068 if (!get_implied_rl(arg, &passed))
1069 return false;
1071 compare_type = get_arg_type(expr->fn, param);
1072 } else {
1073 char *name;
1074 struct symbol *sym;
1076 name = get_variable_from_key(arg, key, &sym);
1077 if (!name || !sym)
1078 return false;
1080 state = get_state(SMATCH_EXTRA, name, sym);
1081 if (!state) {
1082 free_string(name);
1083 return false;
1085 passed = estate_rl(state);
1086 if (!passed || is_whole_rl(passed)) {
1087 free_string(name);
1088 return false;
1091 compare_type = get_member_type_from_key(arg, key);
1094 passed = cast_rl(compare_type, passed);
1095 call_results_to_rl(expr, compare_type, value, &limit);
1096 if (!limit || is_whole_rl(limit))
1097 return false;
1098 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
1099 return false;
1100 if (option_debug || local_debug || debug_db)
1101 sm_msg("impossible: %d '%s' limit '%s' == '%s' return='%s'", param, key, show_rl(passed), value, db_info->ret_str);
1102 return true;
1105 static bool is_impossible_data(int type, struct db_callback_info *db_info, int param, char *key, char *value)
1107 if (type == PARAM_LIMIT && impossible_limit(db_info, param, key, value))
1108 return true;
1109 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(db_info->expr, param, key, value)) {
1110 if (local_debug || debug_db)
1111 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
1112 return true;
1114 return false;
1117 static bool func_type_mismatch(struct expression *expr, const char *value)
1119 struct symbol *type;
1121 /* This makes faking returns easier */
1122 if (!value || value[0] == '\0')
1123 return false;
1125 while (expr->type == EXPR_ASSIGNMENT)
1126 expr = strip_expr(expr->right);
1129 * Short cut: We only care about function pointers that are struct
1130 * members.
1133 if (expr->fn->type == EXPR_SYMBOL)
1134 return false;
1136 type = get_type(expr->fn);
1137 if (!type)
1138 return false;
1139 if (type->type == SYM_PTR)
1140 type = get_real_base_type(type);
1142 if (strcmp(type_to_str(type), value) == 0)
1143 return false;
1145 return true;
1148 static void process_return_states(struct db_callback_info *db_info)
1150 struct stree *stree;
1152 set_implied_states(db_info);
1153 set_fresh_mtag_returns(db_info);
1154 parse_fake_calls();
1155 free_ptr_list(&db_info->called);
1156 stree = __pop_fake_cur_stree();
1157 if (debug_db) {
1158 sm_msg("States from DB: %s expr='%s' ret_str='%s' rl='%s' state='%s'",
1159 db_info->cull ? "Culling" : "Merging",
1160 expr_to_str(db_info->expr),
1161 db_info->ret_str, show_rl(db_info->rl),
1162 db_info->ret_state ? db_info->ret_state->name : "<none>");
1163 __print_stree(stree);
1166 if (!db_info->cull) {
1167 merge_fake_stree(&db_info->stree, stree);
1168 db_info->states_merged = true;
1170 free_stree(&stree);
1172 db_info->ret_state = NULL;
1173 db_info->ret_str = NULL;
1176 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
1178 struct db_callback_info *db_info = _info;
1179 struct range_list *var_rl = db_info->rl;
1180 struct range_list *ret_range;
1181 int type, param;
1182 char *ret_str, *key, *value;
1183 struct return_implies_callback *tmp;
1184 int return_id;
1185 int comparison;
1187 if (argc != 6)
1188 return 0;
1190 return_id = atoi(argv[0]);
1191 ret_str = argv[1];
1192 type = atoi(argv[2]);
1193 param = atoi(argv[3]);
1194 key = argv[4];
1195 value = argv[5];
1197 db_info->has_states = 1;
1198 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1199 set_other_side_state(db_info);
1200 process_return_states(db_info);
1201 __push_fake_cur_stree();
1202 db_info->cull = 0;
1204 db_info->prev_return_id = return_id;
1206 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1207 db_info->cull = 1;
1208 if (db_info->cull)
1209 return 0;
1210 if (type == CULL_PATH) {
1211 db_info->cull = 1;
1212 return 0;
1215 if (is_impossible_data(type, db_info, param, key, value)) {
1216 db_info->cull = 1;
1217 return 0;
1220 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1221 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1222 if (!ret_range)
1223 ret_range = alloc_whole_rl(get_type(db_info->expr));
1225 comparison = db_info->comparison;
1226 if (db_info->left)
1227 comparison = flip_comparison(comparison);
1229 if (db_info->true_side) {
1230 if (!possibly_true_rl(var_rl, comparison, ret_range))
1231 return 0;
1232 if (type == PARAM_LIMIT)
1233 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1234 else if (type > PARAM_LIMIT)
1235 set_implied_states(db_info);
1236 filter_by_comparison(&var_rl, comparison, ret_range);
1237 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
1238 } else {
1239 if (!possibly_false_rl(var_rl, comparison, ret_range))
1240 return 0;
1241 if (type == PARAM_LIMIT)
1242 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1243 else if (type > PARAM_LIMIT)
1244 set_implied_states(db_info);
1245 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
1246 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
1249 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
1251 if (type == INTERNAL) {
1252 set_state(-1, "unnull_path", NULL, &true_state);
1253 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1254 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
1257 FOR_EACH_PTR(db_info->callbacks, tmp) {
1258 if (tmp->type == type)
1259 call_db_return_callback(db_info, tmp, param, key, value);
1260 } END_FOR_EACH_PTR(tmp);
1262 fake_return_assignment(db_info, type, param, key, value);
1264 return 0;
1267 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
1269 struct stree *orig_states;
1270 struct stree *true_states;
1271 struct stree *false_states;
1272 struct sm_state *sm;
1273 struct db_callback_info db_info = {};
1274 struct expression *var_expr;
1275 struct expression *call_expr;
1276 struct range_list *rl;
1277 int call_on_left;
1279 orig_states = clone_stree(__get_cur_stree());
1281 /* legacy cruft. need to fix call_implies_callbacks(). */
1282 call_on_left = 1;
1283 call_expr = left;
1284 var_expr = right;
1285 if (left->type != EXPR_CALL) {
1286 call_on_left = 0;
1287 call_expr = right;
1288 var_expr = left;
1291 get_absolute_rl(var_expr, &rl);
1293 db_info.comparison = comparison;
1294 db_info.expr = call_expr;
1295 db_info.rl = rl;
1296 db_info.left = call_on_left;
1297 db_info.callbacks = db_return_states_list;
1298 db_info.var_expr = var_expr;
1300 call_void_fns(return_states_before);
1302 db_info.true_side = 1;
1303 db_info.stree = NULL;
1304 db_info.prev_return_id = -1;
1305 __push_fake_cur_stree();
1306 sql_select_return_states("return_id, return, type, parameter, key, value",
1307 call_expr, db_compare_callback, &db_info);
1308 set_other_side_state(&db_info);
1309 process_return_states(&db_info);
1310 true_states = db_info.stree;
1311 if (!true_states && db_info.has_states) {
1312 __push_fake_cur_stree();
1313 set_path_impossible();
1314 true_states = __pop_fake_cur_stree();
1317 nullify_path();
1318 __unnullify_path();
1319 FOR_EACH_SM(orig_states, sm) {
1320 __set_sm_cur_stree(sm);
1321 } END_FOR_EACH_SM(sm);
1323 db_info.true_side = 0;
1324 db_info.stree = NULL;
1325 db_info.prev_return_id = -1;
1326 db_info.cull = 0;
1327 __push_fake_cur_stree();
1328 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
1329 db_compare_callback, &db_info);
1330 set_other_side_state(&db_info);
1331 process_return_states(&db_info);
1332 false_states = db_info.stree;
1333 if (!false_states && db_info.has_states) {
1334 __push_fake_cur_stree();
1335 set_path_impossible();
1336 false_states = __pop_fake_cur_stree();
1339 nullify_path();
1340 __unnullify_path();
1341 FOR_EACH_SM(orig_states, sm) {
1342 __set_sm_cur_stree(sm);
1343 } END_FOR_EACH_SM(sm);
1345 free_stree(&orig_states);
1347 FOR_EACH_SM(true_states, sm) {
1348 __set_true_false_sm(sm, NULL);
1349 } END_FOR_EACH_SM(sm);
1350 FOR_EACH_SM(false_states, sm) {
1351 __set_true_false_sm(NULL, sm);
1352 } END_FOR_EACH_SM(sm);
1354 free_stree(&true_states);
1355 free_stree(&false_states);
1357 if (!db_info.states_merged)
1358 mark_call_params_untracked(call_expr);
1360 call_return_states_after_hooks(call_expr);
1362 FOR_EACH_SM(implied_true, sm) {
1363 __set_true_false_sm(sm, NULL);
1364 } END_FOR_EACH_SM(sm);
1365 FOR_EACH_SM(implied_false, sm) {
1366 __set_true_false_sm(NULL, sm);
1367 } END_FOR_EACH_SM(sm);
1370 void function_comparison(struct expression *left, int comparison, struct expression *right)
1372 struct expression *var_expr;
1373 struct expression *call_expr;
1374 struct stree *implied_true = NULL;
1375 struct stree *implied_false = NULL;
1376 struct range_list *rl;
1377 sval_t sval;
1378 int call_on_left;
1380 // TODO: faked_assign delete this
1381 // condition calls should be faked and then handled as assignments
1382 // this code is a lazy work around
1384 if (unreachable())
1385 return;
1387 /* legacy cruft. need to fix call_implies_callbacks(). */
1388 call_on_left = 1;
1389 call_expr = left;
1390 var_expr = right;
1391 if (left->type != EXPR_CALL) {
1392 call_on_left = 0;
1393 call_expr = right;
1394 var_expr = left;
1397 get_absolute_rl(var_expr, &rl);
1399 if (rl_to_sval(rl, &sval))
1400 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
1402 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
1403 free_stree(&implied_true);
1404 free_stree(&implied_false);
1407 static void call_ranged_return_hooks(struct db_callback_info *db_info)
1409 struct call_back_list *call_backs;
1410 struct range_list *range_rl;
1411 struct expression *expr;
1412 struct fcall_back *tmp;
1413 const char *fn_name;
1415 expr = strip_expr(db_info->expr);
1416 while (expr->type == EXPR_ASSIGNMENT)
1417 expr = strip_expr(expr->right);
1418 if (expr->type != EXPR_CALL)
1419 return;
1421 fn_name = get_fn_name(expr->fn);
1422 call_backs = get_call_backs(fn_name);
1423 FOR_EACH_PTR(call_backs, tmp) {
1424 if (tmp->type != RANGED_CALL)
1425 continue;
1426 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1427 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1428 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
1429 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1430 } END_FOR_EACH_PTR(tmp);
1432 FOR_EACH_PTR(call_backs, tmp) {
1433 if (tmp->type != RANGED_EXACT)
1434 continue;
1435 if (!estate_rl(db_info->ret_state))
1436 continue;
1438 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1439 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1442 * If there is an returned value out of range then this is not
1443 * an exact match. In other words, "0,4096-ptr_max" is not
1444 * necessarily a valid match.
1447 if (remove_range(estate_rl(db_info->ret_state),
1448 rl_min(range_rl), rl_max(range_rl)))
1449 continue;
1450 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1451 } END_FOR_EACH_PTR(tmp);
1454 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1456 struct db_callback_info *db_info = _info;
1457 struct range_list *ret_range;
1458 int type, param;
1459 char *ret_str, *key, *value;
1460 struct return_implies_callback *tmp;
1461 int return_id;
1463 if (argc != 6)
1464 return 0;
1466 return_id = atoi(argv[0]);
1467 ret_str = argv[1];
1468 type = atoi(argv[2]);
1469 param = atoi(argv[3]);
1470 key = argv[4];
1471 value = argv[5];
1473 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1474 call_ranged_return_hooks(db_info);
1475 set_return_assign_state(db_info);
1476 process_return_states(db_info);
1477 __push_fake_cur_stree();
1478 db_info->cull = 0;
1480 db_info->prev_return_id = return_id;
1482 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1483 db_info->cull = 1;
1484 if (db_info->cull)
1485 return 0;
1486 if (type == CULL_PATH) {
1487 db_info->cull = 1;
1488 return 0;
1490 if (is_impossible_data(type, db_info, param, key, value)) {
1491 db_info->cull = 1;
1492 return 0;
1495 if (type == PARAM_LIMIT)
1496 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1497 else if (type > PARAM_LIMIT)
1498 set_implied_states(db_info);
1500 db_info->handled = 1;
1501 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
1502 if (!ret_range)
1503 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
1504 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
1506 if (type == INTERNAL) {
1507 set_state(-1, "unnull_path", NULL, &true_state);
1508 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
1509 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1510 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
1513 FOR_EACH_PTR(db_return_states_list, tmp) {
1514 if (tmp->type == type)
1515 call_db_return_callback(db_info, tmp, param, key, value);
1516 } END_FOR_EACH_PTR(tmp);
1518 fake_return_assignment(db_info, type, param, key, value);
1520 return 0;
1523 static int db_return_states_assign(struct expression *expr)
1525 struct expression *right;
1526 struct sm_state *sm;
1527 struct db_callback_info db_info = {};
1529 right = strip_expr(expr->right);
1531 db_info.prev_return_id = -1;
1532 db_info.expr = expr;
1533 db_info.stree = NULL;
1534 db_info.handled = 0;
1536 call_void_fns(return_states_before);
1538 __push_fake_cur_stree();
1539 sql_select_return_states("return_id, return, type, parameter, key, value",
1540 right, db_assign_return_states_callback, &db_info);
1541 if (option_debug) {
1542 sm_msg("%s return_id %d return_ranges %s",
1543 db_info.cull ? "culled" : "merging",
1544 db_info.prev_return_id,
1545 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
1547 if (db_info.handled)
1548 call_ranged_return_hooks(&db_info);
1549 set_return_assign_state(&db_info);
1550 process_return_states(&db_info);
1552 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
1553 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1554 set_path_impossible();
1556 FOR_EACH_SM(db_info.stree, sm) {
1557 __set_sm(sm);
1558 } END_FOR_EACH_SM(sm);
1560 free_stree(&db_info.stree);
1562 if (!db_info.states_merged)
1563 mark_call_params_untracked(right);
1565 call_return_states_after_hooks(right);
1567 return db_info.handled;
1570 static bool handle_implied_return(struct expression *expr)
1572 struct range_list *rl;
1574 if (!get_implied_return(expr->right, &rl))
1575 return false;
1576 rl = cast_rl(get_type(expr->left), rl);
1577 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1578 return true;
1581 static void match_assign_call(struct expression *expr)
1583 struct call_back_list *call_backs;
1584 const char *fn_name;
1585 struct expression *right;
1586 int handled = 0;
1587 struct range_list *rl;
1589 if (expr->op != '=')
1590 return;
1592 right = strip_expr(expr->right);
1593 if (is_fake_call(right))
1594 return;
1596 fn_name = get_fn_name(right->fn);
1597 call_backs = get_call_backs(fn_name);
1600 * The ordering here is sort of important.
1601 * One example, of how this matters is that when we do:
1603 * len = strlen(str);
1605 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1606 * They use implied_return and function_assign_hook respectively.
1607 * We want to get the implied return first before we do the function
1608 * assignment hook otherwise we end up writing the wrong thing for len
1609 * in smatch_extra.c because we assume that it already holds the
1610 * strlen() when we haven't set it yet.
1613 if (db_return_states_assign(expr))
1614 handled = 1;
1615 else
1616 handled = assign_ranged_funcs(fn_name, expr, call_backs);
1617 handled |= handle_implied_return(expr);
1620 call_call_backs(call_backs, ASSIGN_CALL, fn_name, expr);
1622 if (handled)
1623 return;
1625 /* assignment wasn't handled at all */
1626 get_absolute_rl(expr->right, &rl);
1627 rl = cast_rl(get_type(expr->left), rl);
1628 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1631 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1633 struct db_callback_info *db_info = _info;
1634 struct range_list *ret_range;
1635 int type, param;
1636 char *ret_str, *key, *value;
1637 struct return_implies_callback *tmp;
1638 int return_id;
1640 if (argc != 6)
1641 return 0;
1643 return_id = atoi(argv[0]);
1644 ret_str = argv[1];
1645 type = atoi(argv[2]);
1646 param = atoi(argv[3]);
1647 key = argv[4];
1648 value = argv[5];
1650 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1651 call_ranged_return_hooks(db_info);
1652 process_return_states(db_info);
1653 __push_fake_cur_stree();
1654 __unnullify_path();
1655 db_info->cull = 0;
1657 db_info->prev_return_id = return_id;
1659 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1660 db_info->cull = 1;
1661 if (db_info->cull)
1662 return 0;
1663 if (type == CULL_PATH) {
1664 db_info->cull = 1;
1665 return 0;
1667 if (is_impossible_data(type, db_info, param, key, value)) {
1668 db_info->cull = 1;
1669 return 0;
1672 if (type == PARAM_LIMIT)
1673 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1674 else if (type > PARAM_LIMIT)
1675 set_implied_states(db_info);
1677 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1678 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1680 if (type == INTERNAL) {
1681 struct smatch_state *state;
1683 set_state(-1, "unnull_path", NULL, &true_state);
1684 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1685 state = alloc_estate_rl(ret_range);
1686 store_return_state(db_info, ret_str, state);
1689 FOR_EACH_PTR(db_return_states_list, tmp) {
1690 if (tmp->type == type)
1691 call_db_return_callback(db_info, tmp, param, key, value);
1692 } END_FOR_EACH_PTR(tmp);
1694 fake_return_assignment(db_info, type, param, key, value);
1696 return 0;
1699 static void db_return_states(struct expression *expr)
1701 struct sm_state *sm;
1702 struct db_callback_info db_info = {};
1704 if (!__get_cur_stree()) /* no return functions */
1705 return;
1707 db_info.prev_return_id = -1;
1708 db_info.expr = expr;
1709 db_info.stree = NULL;
1711 call_void_fns(return_states_before);
1713 __push_fake_cur_stree();
1714 __unnullify_path();
1715 sql_select_return_states("return_id, return, type, parameter, key, value",
1716 expr, db_return_states_callback, &db_info);
1717 call_ranged_return_hooks(&db_info);
1718 process_return_states(&db_info);
1720 FOR_EACH_SM(db_info.stree, sm) {
1721 __set_sm(sm);
1722 } END_FOR_EACH_SM(sm);
1724 free_stree(&db_info.stree);
1726 if (!db_info.states_merged)
1727 mark_call_params_untracked(expr);
1729 call_return_states_after_hooks(expr);
1732 static void db_return_states_call(struct expression *expr)
1734 if (unreachable())
1735 return;
1737 if (is_assigned_call(expr) || is_fake_assigned_call(expr))
1738 return;
1739 if (is_condition_call(expr))
1740 return;
1741 db_return_states(expr);
1744 static void match_function_call_early(struct expression *expr)
1746 call_function_hooks(expr, REGULAR_CALL_EARLY);
1749 static void match_function_call(struct expression *expr)
1751 call_function_hooks(expr, REGULAR_CALL);
1752 db_return_states_call(expr);
1753 /* If we have no database there could be unprocessed fake calls */
1754 parse_fake_calls();
1757 static void match_macro_assign(struct expression *expr)
1759 struct call_back_list *call_backs;
1760 const char *macro;
1761 struct expression *right;
1763 right = strip_expr(expr->right);
1764 macro = get_macro_name(right->pos);
1765 call_backs = search_callback(func_hash, (char *)macro);
1766 if (!call_backs)
1767 return;
1768 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1769 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1772 bool get_implied_return(struct expression *expr, struct range_list **rl)
1774 struct call_back_list *call_backs;
1775 struct fcall_back *tmp;
1776 bool handled = false;
1777 char *fn;
1779 *rl = NULL;
1781 expr = strip_expr(expr);
1782 fn = expr_to_var(expr->fn);
1783 if (!fn)
1784 goto out;
1786 call_backs = search_callback(func_hash, fn);
1788 FOR_EACH_PTR(call_backs, tmp) {
1789 if (tmp->type == IMPLIED_RETURN)
1790 handled |= (tmp->u.implied_return)(expr, tmp->info, rl);
1791 } END_FOR_EACH_PTR(tmp);
1793 out:
1794 free_string(fn);
1795 return handled;
1798 struct range_list *get_range_implications(const char *fn)
1800 struct call_back_list *call_backs;
1801 struct range_list *ret = NULL;
1802 struct fcall_back *tmp;
1804 call_backs = search_callback(func_hash, (char *)fn);
1806 FOR_EACH_PTR(call_backs, tmp) {
1807 if (tmp->type != RANGED_CALL &&
1808 tmp->type != RANGED_EXACT)
1809 continue;
1810 add_ptr_list(&ret, tmp->range);
1811 } END_FOR_EACH_PTR(tmp);
1813 return ret;
1816 void create_function_hook_hash(void)
1818 func_hash = create_function_hashtable(5000);
1821 void register_function_hooks_early(int id)
1823 add_hook(&match_function_call_early, FUNCTION_CALL_HOOK_BEFORE);
1826 void register_function_hooks(int id)
1828 add_function_data((unsigned long *)&fake_calls);
1829 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1830 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1831 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);