function_hooks: remove some dead code
[smatch.git] / smatch_function_hooks.c
blobb27e60597678e39a507781aafd52b20b72699f38
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 *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->right, &right);
1004 right = cast_rl(get_type(fake_assign->left), right);
1005 // FIXME: add some sanity checks
1006 // FIXME: preserve the sm state if possible
1007 set_extra_expr_nomod(fake_assign->left, alloc_estate_rl(right));
1008 was_set = true;
1011 if (!was_set)
1012 set_extra_expr_mod(expr, state);
1015 static void set_other_side_state(struct db_callback_info *db_info)
1017 struct expression *expr = db_info->var_expr;
1018 struct smatch_state *state;
1020 if (!db_info->ret_state)
1021 return;
1023 // TODO: faked_assign set ==$ equiv here
1025 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1026 set_extra_expr_nomod(expr, state);
1027 db_info->ret_state = NULL;
1028 db_info->ret_str = NULL;
1031 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
1033 char *str;
1034 long long param;
1035 struct expression *arg;
1036 struct range_list *orig;
1038 // TODO: faked_assign This needs to be handled in the assignment code
1040 str = strstr(ret_string, "==$");
1041 if (!str)
1042 return;
1043 str += 3;
1044 param = strtoll(str, NULL, 10);
1045 arg = get_argument_from_call_expr(call->args, param);
1046 if (!arg)
1047 return;
1048 get_absolute_rl(arg, &orig);
1049 rl = rl_intersection(orig, rl);
1050 if (!rl)
1051 return;
1052 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
1055 static bool impossible_limit(struct db_callback_info *db_info, int param, char *key, char *value)
1057 struct expression *expr = db_info->expr;
1058 struct expression *arg;
1059 struct smatch_state *state;
1060 struct range_list *passed;
1061 struct range_list *limit;
1062 struct symbol *compare_type;
1064 while (expr->type == EXPR_ASSIGNMENT)
1065 expr = strip_expr(expr->right);
1066 if (expr->type != EXPR_CALL)
1067 return false;
1069 arg = get_argument_from_call_expr(expr->args, param);
1070 if (!arg)
1071 return false;
1073 if (strcmp(key, "$") == 0) {
1074 if (!get_implied_rl(arg, &passed))
1075 return false;
1077 compare_type = get_arg_type(expr->fn, param);
1078 } else {
1079 char *name;
1080 struct symbol *sym;
1082 name = get_variable_from_key(arg, key, &sym);
1083 if (!name || !sym)
1084 return false;
1086 state = get_state(SMATCH_EXTRA, name, sym);
1087 if (!state) {
1088 free_string(name);
1089 return false;
1091 passed = estate_rl(state);
1092 if (!passed || is_whole_rl(passed)) {
1093 free_string(name);
1094 return false;
1097 compare_type = get_member_type_from_key(arg, key);
1100 passed = cast_rl(compare_type, passed);
1101 call_results_to_rl(expr, compare_type, value, &limit);
1102 if (!limit || is_whole_rl(limit))
1103 return false;
1104 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
1105 return false;
1106 if (option_debug || local_debug || debug_db)
1107 sm_msg("impossible: %d '%s' limit '%s' == '%s' return='%s'", param, key, show_rl(passed), value, db_info->ret_str);
1108 return true;
1111 static bool is_impossible_data(int type, struct db_callback_info *db_info, int param, char *key, char *value)
1113 if (type == PARAM_LIMIT && impossible_limit(db_info, param, key, value))
1114 return true;
1115 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(db_info->expr, param, key, value)) {
1116 if (local_debug || debug_db)
1117 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
1118 return true;
1120 return false;
1123 static bool func_type_mismatch(struct expression *expr, const char *value)
1125 struct symbol *type;
1127 /* This makes faking returns easier */
1128 if (!value || value[0] == '\0')
1129 return false;
1131 while (expr->type == EXPR_ASSIGNMENT)
1132 expr = strip_expr(expr->right);
1135 * Short cut: We only care about function pointers that are struct
1136 * members.
1139 if (expr->fn->type == EXPR_SYMBOL)
1140 return false;
1142 type = get_type(expr->fn);
1143 if (!type)
1144 return false;
1145 if (type->type == SYM_PTR)
1146 type = get_real_base_type(type);
1148 if (strcmp(type_to_str(type), value) == 0)
1149 return false;
1151 return true;
1154 static void process_return_states(struct db_callback_info *db_info)
1156 struct stree *stree;
1158 set_implied_states(db_info);
1159 set_fresh_mtag_returns(db_info);
1160 parse_fake_calls();
1161 free_ptr_list(&db_info->called);
1162 stree = __pop_fake_cur_stree();
1163 if (debug_db) {
1164 sm_msg("States from DB: %s expr='%s' ret_str='%s' rl='%s' state='%s'",
1165 db_info->cull ? "Culling" : "Merging",
1166 expr_to_str(db_info->expr),
1167 db_info->ret_str, show_rl(db_info->rl),
1168 db_info->ret_state ? db_info->ret_state->name : "<none>");
1169 __print_stree(stree);
1172 if (!db_info->cull) {
1173 merge_fake_stree(&db_info->stree, stree);
1174 db_info->states_merged = true;
1176 free_stree(&stree);
1178 db_info->ret_state = NULL;
1179 db_info->ret_str = NULL;
1182 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
1184 struct db_callback_info *db_info = _info;
1185 struct range_list *var_rl = db_info->rl;
1186 struct range_list *ret_range;
1187 int type, param;
1188 char *ret_str, *key, *value;
1189 struct return_implies_callback *tmp;
1190 int return_id;
1191 int comparison;
1193 if (argc != 6)
1194 return 0;
1196 return_id = atoi(argv[0]);
1197 ret_str = argv[1];
1198 type = atoi(argv[2]);
1199 param = atoi(argv[3]);
1200 key = argv[4];
1201 value = argv[5];
1203 db_info->has_states = 1;
1204 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1205 set_other_side_state(db_info);
1206 process_return_states(db_info);
1207 __push_fake_cur_stree();
1208 db_info->cull = 0;
1210 db_info->prev_return_id = return_id;
1212 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1213 db_info->cull = 1;
1214 if (db_info->cull)
1215 return 0;
1216 if (type == CULL_PATH) {
1217 db_info->cull = 1;
1218 return 0;
1221 if (is_impossible_data(type, db_info, param, key, value)) {
1222 db_info->cull = 1;
1223 return 0;
1226 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1227 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1228 if (!ret_range)
1229 ret_range = alloc_whole_rl(get_type(db_info->expr));
1231 comparison = db_info->comparison;
1232 if (db_info->left)
1233 comparison = flip_comparison(comparison);
1235 if (db_info->true_side) {
1236 if (!possibly_true_rl(var_rl, comparison, ret_range))
1237 return 0;
1238 if (type == PARAM_LIMIT)
1239 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1240 else if (type > PARAM_LIMIT)
1241 set_implied_states(db_info);
1242 filter_by_comparison(&var_rl, comparison, ret_range);
1243 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
1244 } else {
1245 if (!possibly_false_rl(var_rl, comparison, ret_range))
1246 return 0;
1247 if (type == PARAM_LIMIT)
1248 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1249 else if (type > PARAM_LIMIT)
1250 set_implied_states(db_info);
1251 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
1252 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
1255 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
1257 if (type == INTERNAL) {
1258 set_state(-1, "unnull_path", NULL, &true_state);
1259 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1260 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
1263 FOR_EACH_PTR(db_info->callbacks, tmp) {
1264 if (tmp->type == type)
1265 call_db_return_callback(db_info, tmp, param, key, value);
1266 } END_FOR_EACH_PTR(tmp);
1268 fake_return_assignment(db_info, type, param, key, value);
1270 return 0;
1273 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
1275 struct stree *orig_states;
1276 struct stree *true_states;
1277 struct stree *false_states;
1278 struct sm_state *sm;
1279 struct db_callback_info db_info = {};
1280 struct expression *var_expr;
1281 struct expression *call_expr;
1282 struct range_list *rl;
1283 int call_on_left;
1285 orig_states = clone_stree(__get_cur_stree());
1287 /* legacy cruft. need to fix call_implies_callbacks(). */
1288 call_on_left = 1;
1289 call_expr = left;
1290 var_expr = right;
1291 if (left->type != EXPR_CALL) {
1292 call_on_left = 0;
1293 call_expr = right;
1294 var_expr = left;
1297 get_absolute_rl(var_expr, &rl);
1299 db_info.comparison = comparison;
1300 db_info.expr = call_expr;
1301 db_info.rl = rl;
1302 db_info.left = call_on_left;
1303 db_info.callbacks = db_return_states_list;
1304 db_info.var_expr = var_expr;
1306 call_void_fns(return_states_before);
1308 db_info.true_side = 1;
1309 db_info.stree = NULL;
1310 db_info.prev_return_id = -1;
1311 __push_fake_cur_stree();
1312 sql_select_return_states("return_id, return, type, parameter, key, value",
1313 call_expr, db_compare_callback, &db_info);
1314 set_other_side_state(&db_info);
1315 process_return_states(&db_info);
1316 true_states = db_info.stree;
1317 if (!true_states && db_info.has_states) {
1318 __push_fake_cur_stree();
1319 set_path_impossible();
1320 true_states = __pop_fake_cur_stree();
1323 nullify_path();
1324 __unnullify_path();
1325 FOR_EACH_SM(orig_states, sm) {
1326 __set_sm_cur_stree(sm);
1327 } END_FOR_EACH_SM(sm);
1329 db_info.true_side = 0;
1330 db_info.stree = NULL;
1331 db_info.prev_return_id = -1;
1332 db_info.cull = 0;
1333 __push_fake_cur_stree();
1334 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
1335 db_compare_callback, &db_info);
1336 set_other_side_state(&db_info);
1337 process_return_states(&db_info);
1338 false_states = db_info.stree;
1339 if (!false_states && db_info.has_states) {
1340 __push_fake_cur_stree();
1341 set_path_impossible();
1342 false_states = __pop_fake_cur_stree();
1345 nullify_path();
1346 __unnullify_path();
1347 FOR_EACH_SM(orig_states, sm) {
1348 __set_sm_cur_stree(sm);
1349 } END_FOR_EACH_SM(sm);
1351 free_stree(&orig_states);
1353 FOR_EACH_SM(true_states, sm) {
1354 __set_true_false_sm(sm, NULL);
1355 } END_FOR_EACH_SM(sm);
1356 FOR_EACH_SM(false_states, sm) {
1357 __set_true_false_sm(NULL, sm);
1358 } END_FOR_EACH_SM(sm);
1360 free_stree(&true_states);
1361 free_stree(&false_states);
1363 if (!db_info.states_merged)
1364 mark_call_params_untracked(call_expr);
1366 call_return_states_after_hooks(call_expr);
1368 FOR_EACH_SM(implied_true, sm) {
1369 __set_true_false_sm(sm, NULL);
1370 } END_FOR_EACH_SM(sm);
1371 FOR_EACH_SM(implied_false, sm) {
1372 __set_true_false_sm(NULL, sm);
1373 } END_FOR_EACH_SM(sm);
1376 void function_comparison(struct expression *left, int comparison, struct expression *right)
1378 struct expression *var_expr;
1379 struct expression *call_expr;
1380 struct stree *implied_true = NULL;
1381 struct stree *implied_false = NULL;
1382 struct range_list *rl;
1383 sval_t sval;
1384 int call_on_left;
1386 // TODO: faked_assign delete this
1387 // condition calls should be faked and then handled as assignments
1388 // this code is a lazy work around
1390 if (unreachable())
1391 return;
1393 /* legacy cruft. need to fix call_implies_callbacks(). */
1394 call_on_left = 1;
1395 call_expr = left;
1396 var_expr = right;
1397 if (left->type != EXPR_CALL) {
1398 call_on_left = 0;
1399 call_expr = right;
1400 var_expr = left;
1403 get_absolute_rl(var_expr, &rl);
1405 if (rl_to_sval(rl, &sval))
1406 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
1408 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
1409 free_stree(&implied_true);
1410 free_stree(&implied_false);
1413 static void call_ranged_return_hooks(struct db_callback_info *db_info)
1415 struct call_back_list *call_backs;
1416 struct range_list *range_rl;
1417 struct expression *expr;
1418 struct fcall_back *tmp;
1419 const char *fn_name;
1421 expr = strip_expr(db_info->expr);
1422 while (expr->type == EXPR_ASSIGNMENT)
1423 expr = strip_expr(expr->right);
1424 if (expr->type != EXPR_CALL)
1425 return;
1427 fn_name = get_fn_name(expr->fn);
1428 call_backs = get_call_backs(fn_name);
1429 FOR_EACH_PTR(call_backs, tmp) {
1430 if (tmp->type != RANGED_CALL)
1431 continue;
1432 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1433 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1434 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
1435 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1436 } END_FOR_EACH_PTR(tmp);
1438 FOR_EACH_PTR(call_backs, tmp) {
1439 if (tmp->type != RANGED_EXACT)
1440 continue;
1441 if (!estate_rl(db_info->ret_state))
1442 continue;
1444 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1445 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1448 * If there is an returned value out of range then this is not
1449 * an exact match. In other words, "0,4096-ptr_max" is not
1450 * necessarily a valid match.
1453 if (remove_range(estate_rl(db_info->ret_state),
1454 rl_min(range_rl), rl_max(range_rl)))
1455 continue;
1456 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1457 } END_FOR_EACH_PTR(tmp);
1460 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1462 struct db_callback_info *db_info = _info;
1463 struct range_list *ret_range;
1464 int type, param;
1465 char *ret_str, *key, *value;
1466 struct return_implies_callback *tmp;
1467 int return_id;
1469 if (argc != 6)
1470 return 0;
1472 return_id = atoi(argv[0]);
1473 ret_str = argv[1];
1474 type = atoi(argv[2]);
1475 param = atoi(argv[3]);
1476 key = argv[4];
1477 value = argv[5];
1479 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1480 call_ranged_return_hooks(db_info);
1481 set_return_assign_state(db_info);
1482 process_return_states(db_info);
1483 __push_fake_cur_stree();
1484 db_info->cull = 0;
1486 db_info->prev_return_id = return_id;
1488 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1489 db_info->cull = 1;
1490 if (db_info->cull)
1491 return 0;
1492 if (type == CULL_PATH) {
1493 db_info->cull = 1;
1494 return 0;
1496 if (is_impossible_data(type, db_info, param, key, value)) {
1497 db_info->cull = 1;
1498 return 0;
1501 if (type == PARAM_LIMIT)
1502 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1503 else if (type > PARAM_LIMIT)
1504 set_implied_states(db_info);
1506 db_info->handled = 1;
1507 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
1508 if (!ret_range)
1509 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
1510 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
1512 if (type == INTERNAL) {
1513 set_state(-1, "unnull_path", NULL, &true_state);
1514 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
1515 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1516 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
1519 FOR_EACH_PTR(db_return_states_list, tmp) {
1520 if (tmp->type == type)
1521 call_db_return_callback(db_info, tmp, param, key, value);
1522 } END_FOR_EACH_PTR(tmp);
1524 fake_return_assignment(db_info, type, param, key, value);
1526 return 0;
1529 static int db_return_states_assign(struct expression *expr)
1531 struct expression *right;
1532 struct sm_state *sm;
1533 struct db_callback_info db_info = {};
1535 right = strip_expr(expr->right);
1537 db_info.prev_return_id = -1;
1538 db_info.expr = expr;
1539 db_info.stree = NULL;
1540 db_info.handled = 0;
1542 call_void_fns(return_states_before);
1544 __push_fake_cur_stree();
1545 sql_select_return_states("return_id, return, type, parameter, key, value",
1546 right, db_assign_return_states_callback, &db_info);
1547 if (option_debug) {
1548 sm_msg("%s return_id %d return_ranges %s",
1549 db_info.cull ? "culled" : "merging",
1550 db_info.prev_return_id,
1551 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
1553 if (db_info.handled)
1554 call_ranged_return_hooks(&db_info);
1555 set_return_assign_state(&db_info);
1556 process_return_states(&db_info);
1558 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
1559 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1560 set_path_impossible();
1562 FOR_EACH_SM(db_info.stree, sm) {
1563 __set_sm(sm);
1564 } END_FOR_EACH_SM(sm);
1566 free_stree(&db_info.stree);
1568 if (!db_info.states_merged)
1569 mark_call_params_untracked(right);
1571 call_return_states_after_hooks(right);
1573 return db_info.handled;
1576 static bool handle_implied_return(struct expression *expr)
1578 struct range_list *rl;
1580 if (!get_implied_return(expr->right, &rl))
1581 return false;
1582 rl = cast_rl(get_type(expr->left), rl);
1583 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1584 return true;
1587 static void match_assign_call(struct expression *expr)
1589 struct call_back_list *call_backs;
1590 const char *fn_name;
1591 struct expression *right;
1592 int handled = 0;
1593 struct range_list *rl;
1595 if (expr->op != '=')
1596 return;
1598 right = strip_expr(expr->right);
1599 if (is_fake_call(right))
1600 return;
1602 fn_name = get_fn_name(right->fn);
1603 call_backs = get_call_backs(fn_name);
1606 * The ordering here is sort of important.
1607 * One example, of how this matters is that when we do:
1609 * len = strlen(str);
1611 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1612 * They use implied_return and function_assign_hook respectively.
1613 * We want to get the implied return first before we do the function
1614 * assignment hook otherwise we end up writing the wrong thing for len
1615 * in smatch_extra.c because we assume that it already holds the
1616 * strlen() when we haven't set it yet.
1619 if (db_return_states_assign(expr))
1620 handled = 1;
1621 else
1622 handled = assign_ranged_funcs(fn_name, expr, call_backs);
1623 handled |= handle_implied_return(expr);
1626 call_call_backs(call_backs, ASSIGN_CALL, fn_name, expr);
1628 if (handled)
1629 return;
1631 /* assignment wasn't handled at all */
1632 get_absolute_rl(expr->right, &rl);
1633 rl = cast_rl(get_type(expr->left), rl);
1634 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1637 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1639 struct db_callback_info *db_info = _info;
1640 struct range_list *ret_range;
1641 int type, param;
1642 char *ret_str, *key, *value;
1643 struct return_implies_callback *tmp;
1644 int return_id;
1646 if (argc != 6)
1647 return 0;
1649 return_id = atoi(argv[0]);
1650 ret_str = argv[1];
1651 type = atoi(argv[2]);
1652 param = atoi(argv[3]);
1653 key = argv[4];
1654 value = argv[5];
1656 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1657 call_ranged_return_hooks(db_info);
1658 process_return_states(db_info);
1659 __push_fake_cur_stree();
1660 __unnullify_path();
1661 db_info->cull = 0;
1663 db_info->prev_return_id = return_id;
1665 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1666 db_info->cull = 1;
1667 if (db_info->cull)
1668 return 0;
1669 if (type == CULL_PATH) {
1670 db_info->cull = 1;
1671 return 0;
1673 if (is_impossible_data(type, db_info, param, key, value)) {
1674 db_info->cull = 1;
1675 return 0;
1678 if (type == PARAM_LIMIT)
1679 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1680 else if (type > PARAM_LIMIT)
1681 set_implied_states(db_info);
1683 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1684 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1686 if (type == INTERNAL) {
1687 struct smatch_state *state;
1689 set_state(-1, "unnull_path", NULL, &true_state);
1690 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1691 state = alloc_estate_rl(ret_range);
1692 store_return_state(db_info, ret_str, state);
1695 FOR_EACH_PTR(db_return_states_list, tmp) {
1696 if (tmp->type == type)
1697 call_db_return_callback(db_info, tmp, param, key, value);
1698 } END_FOR_EACH_PTR(tmp);
1700 fake_return_assignment(db_info, type, param, key, value);
1702 return 0;
1705 static void db_return_states(struct expression *expr)
1707 struct sm_state *sm;
1708 struct db_callback_info db_info = {};
1710 if (!__get_cur_stree()) /* no return functions */
1711 return;
1713 db_info.prev_return_id = -1;
1714 db_info.expr = expr;
1715 db_info.stree = NULL;
1717 call_void_fns(return_states_before);
1719 __push_fake_cur_stree();
1720 __unnullify_path();
1721 sql_select_return_states("return_id, return, type, parameter, key, value",
1722 expr, db_return_states_callback, &db_info);
1723 call_ranged_return_hooks(&db_info);
1724 process_return_states(&db_info);
1726 FOR_EACH_SM(db_info.stree, sm) {
1727 __set_sm(sm);
1728 } END_FOR_EACH_SM(sm);
1730 free_stree(&db_info.stree);
1732 if (!db_info.states_merged)
1733 mark_call_params_untracked(expr);
1735 call_return_states_after_hooks(expr);
1738 static void db_return_states_call(struct expression *expr)
1740 if (unreachable())
1741 return;
1743 if (is_assigned_call(expr) || is_fake_assigned_call(expr))
1744 return;
1745 if (is_condition_call(expr))
1746 return;
1747 db_return_states(expr);
1750 static void match_function_call_early(struct expression *expr)
1752 call_function_hooks(expr, REGULAR_CALL_EARLY);
1755 static void match_function_call(struct expression *expr)
1757 call_function_hooks(expr, REGULAR_CALL);
1758 db_return_states_call(expr);
1759 /* If we have no database there could be unprocessed fake calls */
1760 parse_fake_calls();
1763 static void match_macro_assign(struct expression *expr)
1765 struct call_back_list *call_backs;
1766 const char *macro;
1767 struct expression *right;
1769 right = strip_expr(expr->right);
1770 macro = get_macro_name(right->pos);
1771 call_backs = search_callback(func_hash, (char *)macro);
1772 if (!call_backs)
1773 return;
1774 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1775 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1778 bool get_implied_return(struct expression *expr, struct range_list **rl)
1780 struct call_back_list *call_backs;
1781 struct fcall_back *tmp;
1782 bool handled = false;
1783 char *fn;
1785 *rl = NULL;
1787 expr = strip_expr(expr);
1788 fn = expr_to_var(expr->fn);
1789 if (!fn)
1790 goto out;
1792 call_backs = search_callback(func_hash, fn);
1794 FOR_EACH_PTR(call_backs, tmp) {
1795 if (tmp->type == IMPLIED_RETURN)
1796 handled |= (tmp->u.implied_return)(expr, tmp->info, rl);
1797 } END_FOR_EACH_PTR(tmp);
1799 out:
1800 free_string(fn);
1801 return handled;
1804 struct range_list *get_range_implications(const char *fn)
1806 struct call_back_list *call_backs;
1807 struct range_list *ret = NULL;
1808 struct fcall_back *tmp;
1810 call_backs = search_callback(func_hash, (char *)fn);
1812 FOR_EACH_PTR(call_backs, tmp) {
1813 if (tmp->type != RANGED_CALL &&
1814 tmp->type != RANGED_EXACT)
1815 continue;
1816 add_ptr_list(&ret, tmp->range);
1817 } END_FOR_EACH_PTR(tmp);
1819 return ret;
1822 void create_function_hook_hash(void)
1824 func_hash = create_function_hashtable(5000);
1827 void register_function_hooks_early(int id)
1829 add_hook(&match_function_call_early, FUNCTION_CALL_HOOK_BEFORE);
1832 void register_function_hooks(int id)
1834 add_function_data((unsigned long *)&fake_calls);
1835 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1836 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1837 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);