kernel-printf: add printf format %pra for struct range
[smatch.git] / smatch_function_hooks.c
blob2236c3e1ec179f1b6fb8e949859b7292c19a8519
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()
57 * add_cull_hook()
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <ctype.h>
63 #include "smatch.h"
64 #include "smatch_slist.h"
65 #include "smatch_extra.h"
66 #include "smatch_function_hashtable.h"
67 #include "smatch_expression_stacks.h"
69 struct fcall_back {
70 int type;
71 struct data_range *range;
72 union {
73 func_hook *call_back;
74 implication_hook *ranged;
75 implied_return_hook *implied_return;
76 cull_hook *cull_hook;
77 } u;
78 void *info;
81 ALLOCATOR(fcall_back, "call backs");
82 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
84 DEFINE_FUNCTION_HASHTABLE_STATIC(callback, struct fcall_back, struct call_back_list);
85 static struct hashtable *func_hash;
87 unsigned long __in_fake_parameter_assign;
89 enum fn_hook_type {
90 REGULAR_CALL_EARLY,
91 REGULAR_CALL,
92 REGULAR_CALL_LATE,
93 RANGED_CALL,
94 RANGED_EXACT,
95 ASSIGN_CALL,
96 IMPLIED_RETURN,
97 CULL_HOOK,
98 MACRO_ASSIGN,
99 MACRO_ASSIGN_EXTRA,
102 struct param_key_data {
103 param_key_hook *call_back;
104 expr_func *expr_fn;
105 int param;
106 const char *key;
107 void *info;
110 struct param_data {
111 expr_func *call_back;
112 int param;
113 void *info;
116 struct return_implies_callback {
117 int type;
118 bool param_key;
119 union {
120 return_implies_hook *callback;
121 param_key_hook *pk_callback;
124 ALLOCATOR(return_implies_callback, "return_implies callbacks");
125 DECLARE_PTR_LIST(db_implies_list, struct return_implies_callback);
126 static struct db_implies_list *db_return_states_list;
128 static struct void_fn_list *return_states_before;
129 static struct void_fn_list *return_states_after;
130 static struct string_hook_list *return_string_hooks;
132 struct db_callback_info {
133 int true_side;
134 int comparison;
135 struct expression *expr;
136 struct range_list *rl;
137 int left;
138 struct stree *stree;
139 struct stree *implied;
140 struct db_implies_list *callbacks;
141 struct db_implies_list *called;
142 int prev_return_id;
143 int cull;
144 int has_states;
145 bool states_merged;
146 char *ret_str;
147 struct smatch_state *ret_state;
148 struct expression *var_expr;
149 struct expression_list *fake_param_assign_stack;
150 int handled;
153 static struct expression_list *fake_calls;
155 void add_fake_call_after_return(struct expression *call)
157 add_ptr_list(&fake_calls, call);
160 static void parse_fake_calls(void)
162 struct expression_list *list;
163 struct expression *call;
165 list = fake_calls;
166 fake_calls = NULL;
168 FOR_EACH_PTR(list, call) {
169 __split_expr(call);
170 } END_FOR_EACH_PTR(call);
172 __free_ptr_list((struct ptr_list **)&list);
175 static struct fcall_back *alloc_fcall_back(int type, void *call_back,
176 void *info)
178 struct fcall_back *cb;
180 cb = __alloc_fcall_back(0);
181 cb->type = type;
182 cb->u.call_back = call_back;
183 cb->info = info;
184 return cb;
187 const char *get_fn_name(struct expression *fn)
189 fn = strip_expr(fn);
190 if (!fn)
191 return NULL;
192 if (fn->type == EXPR_SYMBOL && fn->symbol)
193 return fn->symbol->ident->name;
194 return get_member_name(fn);
197 static struct call_back_list *get_call_backs(const char *fn_name)
199 if (!fn_name)
200 return NULL;
201 return search_callback(func_hash, (char *)fn_name);
204 void add_function_hook(const char *look_for, func_hook *call_back, void *info)
206 struct fcall_back *cb;
208 cb = alloc_fcall_back(REGULAR_CALL, call_back, info);
209 add_callback(func_hash, look_for, cb);
212 void add_function_hook_early(const char *look_for, func_hook *call_back, void *info)
214 struct fcall_back *cb;
216 cb = alloc_fcall_back(REGULAR_CALL_EARLY, call_back, info);
217 add_callback(func_hash, look_for, cb);
220 void add_function_hook_late(const char *look_for, func_hook *call_back, void *info)
222 struct fcall_back *cb;
224 cb = alloc_fcall_back(REGULAR_CALL_LATE, call_back, info);
225 add_callback(func_hash, look_for, cb);
228 void add_function_assign_hook(const char *look_for, func_hook *call_back,
229 void *info)
231 struct fcall_back *cb;
233 cb = alloc_fcall_back(ASSIGN_CALL, call_back, info);
234 add_callback(func_hash, look_for, cb);
237 static void register_funcs_from_file_helper(const char *file,
238 func_hook *call_back, void *info,
239 bool assign)
241 struct token *token;
242 const char *func;
243 char name[64];
245 snprintf(name, sizeof(name), "%s.%s", option_project_str, file);
246 token = get_tokens_file(name);
247 if (!token)
248 return;
249 if (token_type(token) != TOKEN_STREAMBEGIN)
250 return;
251 token = token->next;
252 while (token_type(token) != TOKEN_STREAMEND) {
253 if (token_type(token) != TOKEN_IDENT)
254 return;
255 func = show_ident(token->ident);
256 if (assign)
257 add_function_assign_hook(func, call_back, info);
258 else
259 add_function_hook(func, call_back, info);
260 token = token->next;
262 clear_token_alloc();
265 void register_func_hooks_from_file(const char *file,
266 func_hook *call_back, void *info)
268 register_funcs_from_file_helper(file, call_back, info, false);
271 void register_assign_hooks_from_file(const char *file,
272 func_hook *call_back, void *info)
274 register_funcs_from_file_helper(file, call_back, info, true);
277 void add_implied_return_hook(const char *look_for,
278 implied_return_hook *call_back,
279 void *info)
281 struct fcall_back *cb;
283 cb = alloc_fcall_back(IMPLIED_RETURN, call_back, info);
284 add_callback(func_hash, look_for, cb);
287 void add_cull_hook(const char *look_for, cull_hook *call_back, void *info)
289 struct fcall_back *cb;
291 cb = alloc_fcall_back(CULL_HOOK, call_back, info);
292 add_callback(func_hash, look_for, cb);
295 static void db_helper(struct expression *expr, param_key_hook *call_back, int param, const char *key, void *info)
297 char *name;
298 struct symbol *sym;
300 if (param == -2) {
301 call_back(expr, key, NULL, info);
302 return;
305 name = get_name_sym_from_param_key(expr, param, key, &sym);
306 if (!name || !sym)
307 goto free;
309 call_back(expr, name, sym, info);
310 free:
311 free_string(name);
314 static struct expression *get_parent_assignment(struct expression *expr)
316 struct expression *parent;
317 int cnt = 0;
319 if (expr->type == EXPR_ASSIGNMENT)
320 return NULL;
322 parent = expr;
323 while (true) {
324 parent = expr_get_fake_or_real_parent_expr(parent);
325 if (!parent || ++cnt >= 5)
326 break;
327 if (parent->type == EXPR_CAST)
328 continue;
329 if (parent->type == EXPR_PREOP && parent->op == '(')
330 continue;
331 break;
334 if (parent && parent->type == EXPR_ASSIGNMENT)
335 return parent;
336 return NULL;
339 static void param_key_function(const char *fn, struct expression *expr, void *data)
341 struct param_key_data *pkd = data;
342 struct expression *parent;
344 parent = get_parent_assignment(expr);
345 if (parent)
346 expr = parent;
348 db_helper(expr, pkd->call_back, pkd->param, pkd->key, pkd->info);
351 static void param_key_expr_function(const char *fn, struct expression *expr, void *data)
353 struct param_key_data *pkd = data;
354 struct expression *parent, *arg;
356 parent = get_parent_assignment(expr);
357 if (parent)
358 expr = parent;
360 arg = gen_expr_from_param_key(expr, pkd->param, pkd->key);
361 if (!arg)
362 return;
363 pkd->expr_fn(arg);
366 static void param_key_implies_function(const char *fn, struct expression *call_expr,
367 struct expression *assign_expr, void *data)
369 struct param_key_data *pkd = data;
371 db_helper(assign_expr ?: call_expr, pkd->call_back, pkd->param, pkd->key, pkd->info);
374 static void param_key_expr_implies_function(const char *fn, struct expression *call_expr,
375 struct expression *assign_expr, void *data)
377 struct param_key_data *pkd = data;
378 struct expression *arg;
380 arg = gen_expr_from_param_key(assign_expr ?: call_expr, pkd->param, pkd->key);
381 if (!arg)
382 return;
383 pkd->expr_fn(arg);
386 static struct param_key_data *alloc_pkd(param_key_hook *call_back, int param, const char *key, void *info)
388 struct param_key_data *pkd;
390 pkd = malloc(sizeof(*pkd));
391 pkd->call_back = call_back;
392 pkd->param = param;
393 pkd->key = alloc_string(key);
394 pkd->info = info;
396 return pkd;
399 static struct param_key_data *alloc_pked(expr_func *call_back, int param, const char *key, void *info)
401 struct param_key_data *pkd;
403 pkd = alloc_pkd(NULL, param, key, info);
404 pkd->expr_fn = call_back;
406 return pkd;
409 void add_function_param_key_hook_early(const char *look_for, param_key_hook *call_back,
410 int param, const char *key, void *info)
412 struct param_key_data *pkd;
414 pkd = alloc_pkd(call_back, param, key, info);
415 add_function_hook_early(look_for, &param_key_function, pkd);
418 void add_function_param_key_hook(const char *look_for, param_key_hook *call_back,
419 int param, const char *key, void *info)
421 struct param_key_data *pkd;
423 pkd = alloc_pkd(call_back, param, key, info);
424 if (param == -1)
425 add_function_assign_hook(look_for, &param_key_function, pkd);
426 else
427 add_function_hook(look_for, &param_key_function, pkd);
430 void add_param_key_expr_hook(const char *look_for, expr_func *call_back,
431 int param, const char *key, void *info)
433 struct param_key_data *pkd;
435 pkd = alloc_pked(call_back, param, key, info);
437 if (param == -1)
438 add_function_assign_hook(look_for, &param_key_expr_function, pkd);
439 else
440 add_function_hook(look_for, &param_key_expr_function, pkd);
443 void add_function_param_key_hook_late(const char *look_for, param_key_hook *call_back,
444 int param, const char *key, void *info)
446 struct param_key_data *pkd;
448 pkd = alloc_pkd(call_back, param, key, info);
449 add_function_hook_late(look_for, &param_key_function, pkd);
452 void return_implies_param_key(const char *look_for, sval_t start, sval_t end,
453 param_key_hook *call_back,
454 int param, const char *key, void *info)
456 struct param_key_data *pkd;
458 pkd = alloc_pkd(call_back, param, key, info);
459 return_implies_state_sval(look_for, start, end, &param_key_implies_function, pkd);
462 void return_implies_param_key_exact(const char *look_for, sval_t start, sval_t end,
463 param_key_hook *call_back,
464 int param, const char *key, void *info)
466 struct param_key_data *pkd;
468 pkd = alloc_pkd(call_back, param, key, info);
469 return_implies_exact(look_for, start, end, &param_key_implies_function, pkd);
472 void return_implies_param_key_expr(const char *look_for, sval_t start, sval_t end,
473 expr_func *call_back,
474 int param, const char *key, void *info)
476 struct param_key_data *pkd;
478 pkd = alloc_pked(call_back, param, key, info);
479 return_implies_state_sval(look_for, start, end, &param_key_expr_implies_function, pkd);
482 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
483 void *info)
485 struct fcall_back *cb;
487 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
488 add_callback(func_hash, look_for, cb);
491 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
492 void *info)
494 struct fcall_back *cb;
496 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
497 add_callback(func_hash, look_for, cb);
500 void return_implies_state(const char *look_for, long long start, long long end,
501 implication_hook *call_back, void *info)
503 struct fcall_back *cb;
505 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
506 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
507 add_callback(func_hash, look_for, cb);
510 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
511 implication_hook *call_back, void *info)
513 struct fcall_back *cb;
515 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
516 cb->range = alloc_range_perm(start, end);
517 add_callback(func_hash, look_for, cb);
520 void return_implies_exact(const char *look_for, sval_t start, sval_t end,
521 implication_hook *call_back, void *info)
523 struct fcall_back *cb;
525 cb = alloc_fcall_back(RANGED_EXACT, call_back, info);
526 cb->range = alloc_range_perm(start, end);
527 add_callback(func_hash, look_for, cb);
530 static struct return_implies_callback *alloc_db_return_callback(int type, bool param_key, void *callback)
532 struct return_implies_callback *cb;
534 cb = __alloc_return_implies_callback(0);
535 cb->type = type;
536 cb->param_key = param_key;
537 cb->callback = callback;
539 return cb;
542 void select_return_states_hook(int type, return_implies_hook *callback)
544 struct return_implies_callback *cb;
546 cb = alloc_db_return_callback(type, false, callback);
547 add_ptr_list(&db_return_states_list, cb);
550 static void call_db_return_callback(struct db_callback_info *db_info,
551 struct return_implies_callback *cb,
552 int param, char *key, char *value)
554 if (cb->param_key) {
555 db_helper(db_info->expr, cb->pk_callback, param, key, NULL);
556 add_ptr_list(&db_info->called, cb);
557 } else {
558 cb->callback(db_info->expr, param, key, value);
562 void select_return_param_key(int type, param_key_hook *callback)
564 struct return_implies_callback *cb;
566 cb = alloc_db_return_callback(type, true, callback);
567 add_ptr_list(&db_return_states_list, cb);
570 void select_return_states_before(void_fn *fn)
572 add_ptr_list(&return_states_before, fn);
575 void select_return_states_after(void_fn *fn)
577 add_ptr_list(&return_states_after, fn);
580 void add_return_string_hook(string_hook *fn)
582 add_ptr_list(&return_string_hooks, fn);
585 static bool call_call_backs(struct call_back_list *list, int type,
586 const char *fn, struct expression *expr)
588 struct fcall_back *tmp;
589 bool handled = false;
591 FOR_EACH_PTR(list, tmp) {
592 if (tmp->type == type) {
593 (tmp->u.call_back)(fn, expr, tmp->info);
594 handled = true;
596 } END_FOR_EACH_PTR(tmp);
598 return handled;
601 static void call_function_hooks(struct expression *expr, enum fn_hook_type type)
603 struct call_back_list *call_backs;
604 const char *fn_name;
606 while (expr->type == EXPR_ASSIGNMENT)
607 expr = strip_expr(expr->right);
608 if (expr->type != EXPR_CALL)
609 return;
611 fn_name = get_fn_name(expr->fn);
612 call_backs = get_call_backs(fn_name);
613 if (!call_backs)
614 return;
616 call_call_backs(call_backs, type, fn_name, expr);
619 static void call_return_states_after_hooks(struct expression *expr)
621 call_void_fns(return_states_after);
622 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER_DB);
623 call_function_hooks(expr, REGULAR_CALL_LATE);
626 static void call_ranged_call_backs(struct call_back_list *list,
627 const char *fn, struct expression *call_expr,
628 struct expression *assign_expr)
630 struct fcall_back *tmp;
632 FOR_EACH_PTR(list, tmp) {
633 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
634 } END_FOR_EACH_PTR(tmp);
637 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
638 struct data_range *drange)
640 struct call_back_list *ret = NULL;
641 struct fcall_back *tmp;
643 FOR_EACH_PTR(list, tmp) {
644 if (tmp->type != RANGED_CALL &&
645 tmp->type != RANGED_EXACT)
646 continue;
647 if (ranges_equiv(tmp->range, drange))
648 add_ptr_list(&ret, tmp);
649 } END_FOR_EACH_PTR(tmp);
650 return ret;
653 static bool in_list_exact_sval(struct range_list *list, struct data_range *drange)
655 struct data_range *tmp;
657 FOR_EACH_PTR(list, tmp) {
658 if (ranges_equiv(tmp, drange))
659 return true;
660 } END_FOR_EACH_PTR(tmp);
661 return false;
665 * The assign_ranged_funcs() function is called when we have no data from the DB.
667 static bool assign_ranged_funcs(const char *fn, struct expression *expr,
668 struct call_back_list *call_backs)
670 struct fcall_back *tmp;
671 struct sm_state *sm;
672 char *var_name;
673 struct symbol *sym;
674 struct smatch_state *estate;
675 struct stree *tmp_stree;
676 struct stree *final_states = NULL;
677 struct range_list *handled_ranges = NULL;
678 struct range_list *unhandled_rl;
679 struct call_back_list *same_range_call_backs = NULL;
680 struct expression *call;
681 struct range_list *rl;
682 int handled = false;
684 if (!call_backs)
685 return false;
687 var_name = expr_to_var_sym(expr->left, &sym);
688 if (!var_name || !sym)
689 goto free;
691 call = strip_expr(expr->right);
693 FOR_EACH_PTR(call_backs, tmp) {
694 if (tmp->type != RANGED_CALL &&
695 tmp->type != RANGED_EXACT)
696 continue;
698 if (in_list_exact_sval(handled_ranges, tmp->range))
699 continue;
700 __push_fake_cur_stree();
701 tack_on(&handled_ranges, tmp->range);
703 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
704 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
705 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
707 rl = alloc_rl(tmp->range->min, tmp->range->max);
708 rl = cast_rl(get_type(expr->left), rl);
709 estate = alloc_estate_rl(rl);
710 set_extra_mod(var_name, sym, expr->left, estate);
712 tmp_stree = __pop_fake_cur_stree();
713 merge_fake_stree(&final_states, tmp_stree);
714 free_stree(&tmp_stree);
715 handled = true;
716 } END_FOR_EACH_PTR(tmp);
718 unhandled_rl = rl_filter(alloc_whole_rl(get_type(call)), handled_ranges);
719 if (unhandled_rl) {
720 __push_fake_cur_stree();
721 rl = cast_rl(get_type(expr->left), unhandled_rl);
722 estate = alloc_estate_rl(rl);
723 set_extra_mod(var_name, sym, expr->left, estate);
724 tmp_stree = __pop_fake_cur_stree();
725 merge_fake_stree(&final_states, tmp_stree);
726 free_stree(&tmp_stree);
729 FOR_EACH_SM(final_states, sm) {
730 __set_sm(sm);
731 } END_FOR_EACH_SM(sm);
733 free_stree(&final_states);
734 free:
735 free_string(var_name);
736 return handled;
739 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
741 struct call_back_list *call_backs;
742 struct fcall_back *tmp;
743 const char *fn_name;
744 struct data_range *value_range;
745 struct stree *true_states = NULL;
746 struct stree *false_states = NULL;
747 struct stree *tmp_stree;
749 *implied_true = NULL;
750 *implied_false = NULL;
751 fn_name = get_fn_name(expr->fn);
752 call_backs = get_call_backs(fn_name);
753 if (!call_backs)
754 return;
755 value_range = alloc_range(sval, sval);
757 /* set true states */
758 __push_fake_cur_stree();
759 FOR_EACH_PTR(call_backs, tmp) {
760 if (tmp->type != RANGED_CALL &&
761 tmp->type != RANGED_EXACT)
762 continue;
763 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
764 continue;
765 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
766 } END_FOR_EACH_PTR(tmp);
767 tmp_stree = __pop_fake_cur_stree();
768 merge_fake_stree(&true_states, tmp_stree);
769 free_stree(&tmp_stree);
771 /* set false states */
772 __push_fake_cur_stree();
773 FOR_EACH_PTR(call_backs, tmp) {
774 if (tmp->type != RANGED_CALL &&
775 tmp->type != RANGED_EXACT)
776 continue;
777 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
778 continue;
779 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
780 } END_FOR_EACH_PTR(tmp);
781 tmp_stree = __pop_fake_cur_stree();
782 merge_fake_stree(&false_states, tmp_stree);
783 free_stree(&tmp_stree);
785 *implied_true = true_states;
786 *implied_false = false_states;
789 static void set_implied_states(struct db_callback_info *db_info)
791 struct sm_state *sm;
793 FOR_EACH_SM(db_info->implied, sm) {
794 __set_sm(sm);
795 } END_FOR_EACH_SM(sm);
797 free_stree(&db_info->implied);
800 static void call_cull_hooks(struct db_callback_info *db_info, struct range_list *rl)
802 struct expression *expr = db_info->expr;
803 struct call_back_list *call_backs;
804 struct fcall_back *tmp;
805 const char *fn_name;
807 while (expr->type == EXPR_ASSIGNMENT)
808 expr = strip_expr(expr->right);
809 if (expr->type != EXPR_CALL)
810 return;
812 fn_name = get_fn_name(expr->fn);
813 if (!fn_name)
814 return;
815 call_backs = search_callback(func_hash, (char *)fn_name);
817 FOR_EACH_PTR(call_backs, tmp) {
818 if (tmp->type != CULL_HOOK)
819 continue;
820 if ((tmp->u.cull_hook)(db_info->expr, rl, tmp->info))
821 db_info->cull = 1;
822 } END_FOR_EACH_PTR(tmp);
825 static void store_return_state(struct db_callback_info *db_info, const char *ret_str, struct smatch_state *state)
827 db_info->ret_str = alloc_sname(ret_str),
828 db_info->ret_state = state;
831 static struct expression_list *unfaked_calls;
833 struct expression *get_unfaked_call(void)
835 return last_ptr_list((struct ptr_list *)unfaked_calls);
838 static void store_unfaked_call(struct expression *expr)
840 push_expression(&unfaked_calls, expr);
843 static void clear_unfaked_call(void)
845 delete_ptr_list_last((struct ptr_list **)&unfaked_calls);
848 void fake_param_assign_helper(struct expression *call, struct expression *fake_assign, bool shallow)
850 store_unfaked_call(call);
851 __in_fake_parameter_assign++;
852 parse_assignment(fake_assign, true);
853 __in_fake_parameter_assign--;
854 clear_unfaked_call();
857 static bool fake_a_param_assignment(struct expression *expr, const char *ret_str, struct smatch_state *orig)
859 struct expression *arg, *left, *right, *tmp, *fake_assign;
860 char *p;
861 int param;
862 char buf[256];
863 char *str;
865 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
866 return false;
867 left = expr->left;
868 right = expr->right;
870 while (right->type == EXPR_ASSIGNMENT)
871 right = strip_expr(right->right);
872 if (!right || right->type != EXPR_CALL)
873 return false;
875 p = strchr(ret_str, '[');
876 if (!p)
877 return false;
879 p++;
880 if (p[0] == '=' && p[1] == '=')
881 p += 2;
882 if (p[0] != '$')
883 return false;
885 snprintf(buf, sizeof(buf), "%s", p);
887 p = buf;
888 p += 1;
889 param = strtol(p, &p, 10);
891 p = strchr(p, ']');
892 if (!p || *p != ']')
893 return false;
894 *p = '\0';
896 arg = get_argument_from_call_expr(right->args, param);
897 if (!arg)
898 return false;
900 /* There should be a get_other_name() function which returns an expr */
901 tmp = get_assigned_expr(arg);
902 if (tmp)
903 arg = tmp;
906 * This is a sanity check to prevent side effects from evaluating stuff
907 * twice.
909 str = expr_to_chunk_sym_vsl(arg, NULL, NULL);
910 if (!str)
911 return false;
912 free_string(str);
914 right = gen_expression_from_key(arg, buf);
915 if (!right) /* Mostly fails for binops like [$0 + 4032] */
916 return false;
917 fake_assign = assign_expression(left, '=', right);
918 fake_param_assign_helper(expr, fake_assign, false);
921 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
922 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
923 * parts have to be considered. We use _nomod() because it's not really
924 * another modification, it's just a clarification.
927 if (estate_rl(orig)) {
928 struct smatch_state *faked;
929 struct range_list *rl;
931 faked = get_extra_state(left);
932 if (estate_rl(faked)) {
933 rl = rl_intersection(estate_rl(faked), estate_rl(orig));
934 if (rl)
935 set_extra_expr_nomod(left, alloc_estate_rl(rl));
939 return true;
942 static void fake_return_assignment(struct db_callback_info *db_info, int type, int param, char *key, char *value)
944 struct expression *call, *left, *right, *assign;
945 int right_param;
947 if (type != PARAM_COMPARE)
948 return;
950 call = db_info->expr;
951 while (call && call->type == EXPR_ASSIGNMENT)
952 call = strip_expr(call->right);
953 if (!call || call->type != EXPR_CALL)
954 return;
956 // TODO: This only handles "$->foo = arg" and not "$->foo = arg->bar".
957 if (param != -1)
958 return;
959 if (!value || strncmp(value, "== $", 4) != 0)
960 return;
961 if (!isdigit(value[4]) || value[5] != '\0')
962 return;
963 right_param = atoi(value + 4);
965 left = gen_expr_from_param_key(db_info->expr, param, key);
966 if (!left)
967 return;
968 right = get_argument_from_call_expr(call->args, right_param);
970 if (strcmp(key, "$") == 0)
971 right = cast_expression(right, get_type(call));
972 assign = assign_expression(left, '=', right);
973 push_expression(&db_info->fake_param_assign_stack, assign);
976 static void set_fresh_mtag_returns(struct db_callback_info *db_info)
978 struct expression *expr;
979 struct smatch_state *state;
981 if (!db_info->ret_state)
982 return;
984 if (!db_info->expr ||
985 db_info->expr->type != EXPR_ASSIGNMENT ||
986 db_info->expr->op != '=')
987 return;
989 expr = db_info->expr->left;
991 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
992 state = get_mtag_return(db_info->expr, state);
993 if (!state)
994 return;
996 set_real_absolute(expr, state);
997 set_extra_expr_mod(expr, state);
1000 static void set_return_assign_state(struct db_callback_info *db_info)
1002 struct expression *expr = db_info->expr->left;
1003 struct expression *fake_assign;
1004 struct smatch_state *state;
1005 bool was_set = false;
1007 if (!db_info->ret_state)
1008 return;
1010 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1011 if (!fake_a_param_assignment(db_info->expr, db_info->ret_str, state)) {
1012 set_extra_expr_mod(expr, state);
1013 was_set = true;
1016 while ((fake_assign = pop_expression(&db_info->fake_param_assign_stack))) {
1017 struct range_list *right;
1020 * Originally, I tried to do this as a assignment to record that
1021 * a = frob(b) implies that "a->foo == b->foo" etc. But that
1022 * caused a problem because then it was recorded that "a->foo"
1023 * was modified and recorded as a PARAM_SET in the database.
1025 * So now, instead of faking an assignment we use
1026 * set_extra_expr_nomod() but it's still recorded as an
1027 * assignment in the ->fake_param_assign_stack for legacy
1028 * reasons and because it's a handy way to store a left/right
1029 * pair.
1032 get_absolute_rl(fake_assign->right, &right);
1033 right = cast_rl(get_type(fake_assign->left), right);
1034 // FIXME: add some sanity checks
1035 // FIXME: preserve the sm state if possible
1036 set_extra_expr_nomod(fake_assign->left, alloc_estate_rl(right));
1037 was_set = true;
1040 if (!was_set)
1041 set_extra_expr_mod(expr, state);
1044 static void set_other_side_state(struct db_callback_info *db_info)
1046 struct expression *expr = db_info->var_expr;
1047 struct smatch_state *state;
1049 if (!db_info->ret_state)
1050 return;
1052 // TODO: faked_assign set ==$ equiv here
1054 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1055 set_extra_expr_nomod(expr, state);
1056 db_info->ret_state = NULL;
1057 db_info->ret_str = NULL;
1060 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
1062 char *str;
1063 long long param;
1064 struct expression *arg;
1065 struct range_list *orig;
1067 // TODO: faked_assign This needs to be handled in the assignment code
1069 str = strstr(ret_string, "==$");
1070 if (!str)
1071 return;
1072 str += 3;
1073 param = strtoll(str, NULL, 10);
1074 arg = get_argument_from_call_expr(call->args, param);
1075 if (!arg)
1076 return;
1077 get_absolute_rl(arg, &orig);
1078 rl = rl_intersection(orig, rl);
1079 if (!rl)
1080 return;
1081 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
1084 static bool impossible_limit(struct db_callback_info *db_info, int param, char *key, char *value)
1086 struct expression *expr = db_info->expr;
1087 struct expression *arg;
1088 struct smatch_state *state;
1089 struct range_list *passed;
1090 struct range_list *limit;
1091 struct symbol *compare_type;
1093 while (expr->type == EXPR_ASSIGNMENT)
1094 expr = strip_expr(expr->right);
1095 if (expr->type != EXPR_CALL)
1096 return false;
1098 arg = get_argument_from_call_expr(expr->args, param);
1099 if (!arg)
1100 return false;
1102 if (strcmp(key, "$") == 0) {
1103 if (!get_implied_rl(arg, &passed))
1104 return false;
1106 compare_type = get_arg_type(expr->fn, param);
1107 } else {
1108 char *name;
1109 struct symbol *sym;
1111 name = get_variable_from_key(arg, key, &sym);
1112 if (!name || !sym)
1113 return false;
1115 state = get_state(SMATCH_EXTRA, name, sym);
1116 if (!state) {
1117 free_string(name);
1118 return false;
1120 passed = estate_rl(state);
1121 if (!passed || is_whole_rl(passed)) {
1122 free_string(name);
1123 return false;
1126 compare_type = get_member_type_from_key(arg, key);
1129 passed = cast_rl(compare_type, passed);
1130 call_results_to_rl(expr, compare_type, value, &limit);
1131 if (!limit || is_whole_rl(limit))
1132 return false;
1133 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
1134 return false;
1135 if (option_debug || local_debug || debug_db)
1136 sm_msg("impossible: %d '%s' limit '%s' == '%s' return='%s'", param, key, show_rl(passed), value, db_info->ret_str);
1137 return true;
1140 static bool is_impossible_data(int type, struct db_callback_info *db_info, int param, char *key, char *value)
1142 if (type == PARAM_LIMIT && impossible_limit(db_info, param, key, value))
1143 return true;
1144 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(db_info->expr, param, key, value)) {
1145 if (local_debug || debug_db)
1146 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
1147 return true;
1149 return false;
1152 static bool func_type_mismatch(struct expression *expr, const char *value)
1154 struct symbol *type;
1156 /* This makes faking returns easier */
1157 if (!value || value[0] == '\0')
1158 return false;
1160 while (expr->type == EXPR_ASSIGNMENT)
1161 expr = strip_expr(expr->right);
1164 * Short cut: We only care about function pointers that are struct
1165 * members.
1168 if (expr->fn->type == EXPR_SYMBOL)
1169 return false;
1171 type = get_type(expr->fn);
1172 if (!type)
1173 return false;
1174 if (type->type == SYM_PTR)
1175 type = get_real_base_type(type);
1177 if (strcmp(type_to_str(type), value) == 0)
1178 return false;
1180 return true;
1183 static void process_return_states(struct db_callback_info *db_info)
1185 struct stree *stree;
1187 set_implied_states(db_info);
1188 set_fresh_mtag_returns(db_info);
1189 parse_fake_calls();
1190 free_ptr_list(&db_info->called);
1191 stree = __pop_fake_cur_stree();
1192 if (debug_db) {
1193 sm_msg("States from DB: %s expr='%s' ret_str='%s' rl='%s' state='%s'",
1194 db_info->cull ? "Culling" : "Merging",
1195 expr_to_str(db_info->expr),
1196 db_info->ret_str, show_rl(db_info->rl),
1197 db_info->ret_state ? db_info->ret_state->name : "<none>");
1198 __print_stree(stree);
1201 if (!db_info->cull) {
1202 merge_fake_stree(&db_info->stree, stree);
1203 db_info->states_merged = true;
1205 free_stree(&stree);
1207 db_info->ret_state = NULL;
1208 db_info->ret_str = NULL;
1211 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
1213 struct db_callback_info *db_info = _info;
1214 struct range_list *var_rl = db_info->rl;
1215 struct range_list *ret_range;
1216 int type, param;
1217 char *ret_str, *key, *value;
1218 struct return_implies_callback *tmp;
1219 int return_id;
1220 int comparison;
1222 if (argc != 6)
1223 return 0;
1225 return_id = atoi(argv[0]);
1226 ret_str = argv[1];
1227 type = atoi(argv[2]);
1228 param = atoi(argv[3]);
1229 key = argv[4];
1230 value = argv[5];
1232 db_info->has_states = 1;
1233 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1234 set_other_side_state(db_info);
1235 process_return_states(db_info);
1236 __push_fake_cur_stree();
1237 db_info->cull = 0;
1239 db_info->prev_return_id = return_id;
1241 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1242 db_info->cull = 1;
1243 if (db_info->cull)
1244 return 0;
1245 if (type == CULL_PATH) {
1246 db_info->cull = 1;
1247 return 0;
1250 if (is_impossible_data(type, db_info, param, key, value)) {
1251 db_info->cull = 1;
1252 return 0;
1255 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1256 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1257 if (!ret_range)
1258 ret_range = alloc_whole_rl(get_type(db_info->expr));
1260 comparison = db_info->comparison;
1261 if (db_info->left)
1262 comparison = flip_comparison(comparison);
1264 if (db_info->true_side) {
1265 if (!possibly_true_rl(var_rl, comparison, ret_range))
1266 return 0;
1267 if (type == PARAM_LIMIT)
1268 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1269 else if (type > PARAM_LIMIT)
1270 set_implied_states(db_info);
1271 filter_by_comparison(&var_rl, comparison, ret_range);
1272 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
1273 } else {
1274 if (!possibly_false_rl(var_rl, comparison, ret_range))
1275 return 0;
1276 if (type == PARAM_LIMIT)
1277 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1278 else if (type > PARAM_LIMIT)
1279 set_implied_states(db_info);
1280 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
1281 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
1284 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
1286 if (type == INTERNAL) {
1287 set_state(-1, "unnull_path", NULL, &true_state);
1288 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1289 call_cull_hooks(db_info, ret_range);
1290 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
1293 FOR_EACH_PTR(db_info->callbacks, tmp) {
1294 if (tmp->type == type)
1295 call_db_return_callback(db_info, tmp, param, key, value);
1296 } END_FOR_EACH_PTR(tmp);
1298 fake_return_assignment(db_info, type, param, key, value);
1300 return 0;
1303 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
1305 struct stree *orig_states;
1306 struct stree *true_states;
1307 struct stree *false_states;
1308 struct sm_state *sm;
1309 struct db_callback_info db_info = {};
1310 struct expression *var_expr;
1311 struct expression *call_expr;
1312 struct range_list *rl;
1313 int call_on_left;
1315 orig_states = clone_stree(__get_cur_stree());
1317 /* legacy cruft. need to fix call_implies_callbacks(). */
1318 call_on_left = 1;
1319 call_expr = left;
1320 var_expr = right;
1321 if (left->type != EXPR_CALL) {
1322 call_on_left = 0;
1323 call_expr = right;
1324 var_expr = left;
1327 get_absolute_rl(var_expr, &rl);
1329 db_info.comparison = comparison;
1330 db_info.expr = call_expr;
1331 db_info.rl = rl;
1332 db_info.left = call_on_left;
1333 db_info.callbacks = db_return_states_list;
1334 db_info.var_expr = var_expr;
1336 call_void_fns(return_states_before);
1338 db_info.true_side = 1;
1339 db_info.stree = NULL;
1340 db_info.prev_return_id = -1;
1341 __push_fake_cur_stree();
1342 sql_select_return_states("return_id, return, type, parameter, key, value",
1343 call_expr, db_compare_callback, &db_info);
1344 set_other_side_state(&db_info);
1345 process_return_states(&db_info);
1346 true_states = db_info.stree;
1347 if (!true_states && db_info.has_states) {
1348 __push_fake_cur_stree();
1349 set_path_impossible();
1350 true_states = __pop_fake_cur_stree();
1353 nullify_path();
1354 __unnullify_path();
1355 FOR_EACH_SM(orig_states, sm) {
1356 __set_sm_cur_stree(sm);
1357 } END_FOR_EACH_SM(sm);
1359 db_info.true_side = 0;
1360 db_info.stree = NULL;
1361 db_info.prev_return_id = -1;
1362 db_info.cull = 0;
1363 __push_fake_cur_stree();
1364 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
1365 db_compare_callback, &db_info);
1366 set_other_side_state(&db_info);
1367 process_return_states(&db_info);
1368 false_states = db_info.stree;
1369 if (!false_states && db_info.has_states) {
1370 __push_fake_cur_stree();
1371 set_path_impossible();
1372 false_states = __pop_fake_cur_stree();
1375 nullify_path();
1376 __unnullify_path();
1377 FOR_EACH_SM(orig_states, sm) {
1378 __set_sm_cur_stree(sm);
1379 } END_FOR_EACH_SM(sm);
1381 free_stree(&orig_states);
1383 FOR_EACH_SM(true_states, sm) {
1384 __set_true_false_sm(sm, NULL);
1385 } END_FOR_EACH_SM(sm);
1386 FOR_EACH_SM(false_states, sm) {
1387 __set_true_false_sm(NULL, sm);
1388 } END_FOR_EACH_SM(sm);
1390 free_stree(&true_states);
1391 free_stree(&false_states);
1393 if (!db_info.states_merged)
1394 mark_call_params_untracked(call_expr);
1396 call_return_states_after_hooks(call_expr);
1398 FOR_EACH_SM(implied_true, sm) {
1399 __set_true_false_sm(sm, NULL);
1400 } END_FOR_EACH_SM(sm);
1401 FOR_EACH_SM(implied_false, sm) {
1402 __set_true_false_sm(NULL, sm);
1403 } END_FOR_EACH_SM(sm);
1406 void function_comparison(struct expression *left, int comparison, struct expression *right)
1408 struct expression *var_expr;
1409 struct expression *call_expr;
1410 struct stree *implied_true = NULL;
1411 struct stree *implied_false = NULL;
1412 struct range_list *rl;
1413 sval_t sval;
1414 int call_on_left;
1416 // TODO: faked_assign delete this
1417 // condition calls should be faked and then handled as assignments
1418 // this code is a lazy work around
1420 if (is_unreachable())
1421 return;
1423 /* legacy cruft. need to fix call_implies_callbacks(). */
1424 call_on_left = 1;
1425 call_expr = left;
1426 var_expr = right;
1427 if (left->type != EXPR_CALL) {
1428 call_on_left = 0;
1429 call_expr = right;
1430 var_expr = left;
1433 get_absolute_rl(var_expr, &rl);
1435 if (rl_to_sval(rl, &sval))
1436 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
1438 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
1439 free_stree(&implied_true);
1440 free_stree(&implied_false);
1443 static void call_ranged_return_hooks(struct db_callback_info *db_info)
1445 struct call_back_list *call_backs;
1446 struct range_list *range_rl;
1447 struct expression *expr;
1448 struct fcall_back *tmp;
1449 const char *fn_name;
1451 expr = strip_expr(db_info->expr);
1452 while (expr->type == EXPR_ASSIGNMENT)
1453 expr = strip_expr(expr->right);
1454 if (expr->type != EXPR_CALL)
1455 return;
1457 fn_name = get_fn_name(expr->fn);
1458 call_backs = get_call_backs(fn_name);
1459 FOR_EACH_PTR(call_backs, tmp) {
1460 if (tmp->type != RANGED_CALL)
1461 continue;
1462 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1463 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1464 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
1465 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1466 } END_FOR_EACH_PTR(tmp);
1468 FOR_EACH_PTR(call_backs, tmp) {
1469 if (tmp->type != RANGED_EXACT)
1470 continue;
1471 if (!estate_rl(db_info->ret_state))
1472 continue;
1474 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1475 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1478 * If there is an returned value out of range then this is not
1479 * an exact match. In other words, "0,4096-ptr_max" is not
1480 * necessarily a valid match.
1483 if (remove_range(estate_rl(db_info->ret_state),
1484 rl_min(range_rl), rl_max(range_rl)))
1485 continue;
1486 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1487 } END_FOR_EACH_PTR(tmp);
1490 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1492 struct db_callback_info *db_info = _info;
1493 struct range_list *ret_range;
1494 int type, param;
1495 char *ret_str, *key, *value;
1496 struct return_implies_callback *tmp;
1497 int return_id;
1499 if (argc != 6)
1500 return 0;
1502 return_id = atoi(argv[0]);
1503 ret_str = argv[1];
1504 type = atoi(argv[2]);
1505 param = atoi(argv[3]);
1506 key = argv[4];
1507 value = argv[5];
1509 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1510 call_ranged_return_hooks(db_info);
1511 set_return_assign_state(db_info);
1512 process_return_states(db_info);
1513 __push_fake_cur_stree();
1514 db_info->cull = 0;
1516 db_info->prev_return_id = return_id;
1518 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1519 db_info->cull = 1;
1520 if (db_info->cull)
1521 return 0;
1522 if (type == CULL_PATH) {
1523 db_info->cull = 1;
1524 return 0;
1526 if (is_impossible_data(type, db_info, param, key, value)) {
1527 db_info->cull = 1;
1528 return 0;
1531 if (type == PARAM_LIMIT)
1532 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1533 else if (type > PARAM_LIMIT)
1534 set_implied_states(db_info);
1536 db_info->handled = 1;
1537 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
1538 if (!ret_range)
1539 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
1540 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
1542 if (type == INTERNAL) {
1543 set_state(-1, "unnull_path", NULL, &true_state);
1544 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
1545 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1546 call_cull_hooks(db_info, ret_range);
1547 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
1550 FOR_EACH_PTR(db_return_states_list, tmp) {
1551 if (tmp->type == type)
1552 call_db_return_callback(db_info, tmp, param, key, value);
1553 } END_FOR_EACH_PTR(tmp);
1555 fake_return_assignment(db_info, type, param, key, value);
1557 return 0;
1560 static int db_return_states_assign(struct expression *expr)
1562 struct expression *right;
1563 struct sm_state *sm;
1564 struct db_callback_info db_info = {};
1566 right = strip_expr(expr->right);
1568 db_info.prev_return_id = -1;
1569 db_info.expr = expr;
1570 db_info.stree = NULL;
1571 db_info.handled = 0;
1573 call_void_fns(return_states_before);
1575 __push_fake_cur_stree();
1576 sql_select_return_states("return_id, return, type, parameter, key, value",
1577 right, db_assign_return_states_callback, &db_info);
1578 if (option_debug) {
1579 sm_msg("%s return_id %d return_ranges %s",
1580 db_info.cull ? "culled" : "merging",
1581 db_info.prev_return_id,
1582 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
1584 if (db_info.handled)
1585 call_ranged_return_hooks(&db_info);
1586 set_return_assign_state(&db_info);
1587 process_return_states(&db_info);
1589 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
1590 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1591 set_path_impossible();
1593 FOR_EACH_SM(db_info.stree, sm) {
1594 __set_sm(sm);
1595 } END_FOR_EACH_SM(sm);
1597 free_stree(&db_info.stree);
1599 if (!db_info.states_merged)
1600 mark_call_params_untracked(right);
1602 call_return_states_after_hooks(right);
1604 return db_info.handled;
1607 static bool handle_implied_return(struct expression *expr)
1609 struct range_list *rl;
1611 if (!get_implied_return(expr->right, &rl))
1612 return false;
1613 rl = cast_rl(get_type(expr->left), rl);
1614 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1615 return true;
1618 static void match_assign_call(struct expression *expr)
1620 struct call_back_list *call_backs;
1621 const char *fn_name;
1622 struct expression *right;
1623 int handled = 0;
1624 struct range_list *rl;
1626 if (__in_fake_parameter_assign)
1627 return;
1629 if (expr->op != '=')
1630 return;
1632 right = strip_expr(expr->right);
1633 if (is_fake_call(right))
1634 return;
1636 fn_name = get_fn_name(right->fn);
1637 call_backs = get_call_backs(fn_name);
1640 * The ordering here is sort of important.
1641 * One example, of how this matters is that when we do:
1643 * len = strlen(str);
1645 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1646 * They use implied_return and function_assign_hook respectively.
1647 * We want to get the implied return first before we do the function
1648 * assignment hook otherwise we end up writing the wrong thing for len
1649 * in smatch_extra.c because we assume that it already holds the
1650 * strlen() when we haven't set it yet.
1653 if (db_return_states_assign(expr))
1654 handled = 1;
1655 else
1656 handled = assign_ranged_funcs(fn_name, expr, call_backs);
1657 handled |= handle_implied_return(expr);
1660 call_call_backs(call_backs, ASSIGN_CALL, fn_name, expr);
1662 if (handled)
1663 return;
1665 /* assignment wasn't handled at all */
1666 get_absolute_rl(expr->right, &rl);
1667 rl = cast_rl(get_type(expr->left), rl);
1668 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1671 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1673 struct db_callback_info *db_info = _info;
1674 struct range_list *ret_range;
1675 int type, param;
1676 char *ret_str, *key, *value;
1677 struct return_implies_callback *tmp;
1678 int return_id;
1680 if (argc != 6)
1681 return 0;
1683 return_id = atoi(argv[0]);
1684 ret_str = argv[1];
1685 type = atoi(argv[2]);
1686 param = atoi(argv[3]);
1687 key = argv[4];
1688 value = argv[5];
1690 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1691 call_ranged_return_hooks(db_info);
1692 process_return_states(db_info);
1693 __push_fake_cur_stree();
1694 __unnullify_path();
1695 db_info->cull = 0;
1697 db_info->prev_return_id = return_id;
1699 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1700 db_info->cull = 1;
1701 if (db_info->cull)
1702 return 0;
1703 if (type == CULL_PATH) {
1704 db_info->cull = 1;
1705 return 0;
1707 if (is_impossible_data(type, db_info, param, key, value)) {
1708 db_info->cull = 1;
1709 return 0;
1712 if (type == PARAM_LIMIT)
1713 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1714 else if (type > PARAM_LIMIT)
1715 set_implied_states(db_info);
1717 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1718 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1720 if (type == INTERNAL) {
1721 struct smatch_state *state;
1723 set_state(-1, "unnull_path", NULL, &true_state);
1724 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1725 call_cull_hooks(db_info, ret_range);
1726 state = alloc_estate_rl(ret_range);
1727 store_return_state(db_info, ret_str, state);
1730 FOR_EACH_PTR(db_return_states_list, tmp) {
1731 if (tmp->type == type)
1732 call_db_return_callback(db_info, tmp, param, key, value);
1733 } END_FOR_EACH_PTR(tmp);
1735 fake_return_assignment(db_info, type, param, key, value);
1737 return 0;
1740 static void db_return_states(struct expression *expr)
1742 struct sm_state *sm;
1743 struct db_callback_info db_info = {};
1745 if (!__get_cur_stree()) /* no return functions */
1746 return;
1748 db_info.prev_return_id = -1;
1749 db_info.expr = expr;
1750 db_info.stree = NULL;
1752 call_void_fns(return_states_before);
1754 __push_fake_cur_stree();
1755 __unnullify_path();
1756 sql_select_return_states("return_id, return, type, parameter, key, value",
1757 expr, db_return_states_callback, &db_info);
1758 call_ranged_return_hooks(&db_info);
1759 process_return_states(&db_info);
1761 FOR_EACH_SM(db_info.stree, sm) {
1762 __set_sm(sm);
1763 } END_FOR_EACH_SM(sm);
1765 free_stree(&db_info.stree);
1767 if (!db_info.states_merged)
1768 mark_call_params_untracked(expr);
1770 call_return_states_after_hooks(expr);
1773 static void db_return_states_call(struct expression *expr)
1775 struct expression *parent;
1777 if (is_unreachable())
1778 return;
1780 parent = get_parent_assignment(expr);
1781 if (parent && parent->op == '=')
1782 return;
1783 if (is_condition_call(expr))
1784 return;
1785 db_return_states(expr);
1788 static void match_function_call_early(struct expression *expr)
1790 call_function_hooks(expr, REGULAR_CALL_EARLY);
1793 static void match_function_call(struct expression *expr)
1795 call_function_hooks(expr, REGULAR_CALL);
1796 db_return_states_call(expr);
1797 /* If we have no database there could be unprocessed fake calls */
1798 parse_fake_calls();
1801 static void match_macro_assign(struct expression *expr)
1803 struct call_back_list *call_backs;
1804 const char *macro;
1805 struct expression *right;
1807 right = strip_expr(expr->right);
1808 macro = get_macro_name(right->pos);
1809 call_backs = search_callback(func_hash, (char *)macro);
1810 if (!call_backs)
1811 return;
1812 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1813 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1816 bool get_implied_return(struct expression *expr, struct range_list **rl)
1818 struct call_back_list *call_backs;
1819 struct fcall_back *tmp;
1820 bool handled = false;
1821 char *fn;
1823 *rl = NULL;
1825 expr = strip_expr(expr);
1826 fn = expr_to_var(expr->fn);
1827 if (!fn)
1828 goto out;
1830 call_backs = search_callback(func_hash, fn);
1832 FOR_EACH_PTR(call_backs, tmp) {
1833 if (tmp->type == IMPLIED_RETURN)
1834 handled |= (tmp->u.implied_return)(expr, tmp->info, rl);
1835 } END_FOR_EACH_PTR(tmp);
1837 out:
1838 free_string(fn);
1839 return handled;
1842 struct range_list *get_range_implications(const char *fn)
1844 struct call_back_list *call_backs;
1845 struct range_list *ret = NULL;
1846 struct fcall_back *tmp;
1848 call_backs = search_callback(func_hash, (char *)fn);
1850 FOR_EACH_PTR(call_backs, tmp) {
1851 if (tmp->type != RANGED_CALL &&
1852 tmp->type != RANGED_EXACT)
1853 continue;
1854 add_ptr_list(&ret, tmp->range);
1855 } END_FOR_EACH_PTR(tmp);
1857 return ret;
1860 void create_function_hook_hash(void)
1862 func_hash = create_function_hashtable(5000);
1865 void register_function_hooks_early(int id)
1867 add_hook(&match_function_call_early, FUNCTION_CALL_HOOK_BEFORE);
1870 void register_function_hooks(int id)
1872 add_function_data((unsigned long *)&fake_calls);
1873 add_function_data((unsigned long *)&__in_fake_parameter_assign);
1874 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1875 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1876 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);