kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / smatch_function_hooks.c
blobbd61a76e3a35f9c385ebb8c397a6749b763e9b81
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 pkd = alloc_pkd(call_back, param, key, info);
408 add_function_hook_early(look_for, &param_key_function, pkd);
411 void add_function_param_key_hook(const char *look_for, param_key_hook *call_back,
412 int param, const char *key, void *info)
414 struct param_key_data *pkd;
416 pkd = alloc_pkd(call_back, param, key, info);
417 if (param == -1)
418 add_function_assign_hook(look_for, &param_key_function, pkd);
419 else
420 add_function_hook(look_for, &param_key_function, pkd);
423 void add_param_key_expr_hook(const char *look_for, expr_func *call_back,
424 int param, const char *key, void *info)
426 struct param_key_data *pkd;
428 pkd = alloc_pked(call_back, param, key, info);
430 if (param == -1)
431 add_function_assign_hook(look_for, &param_key_expr_function, pkd);
432 else
433 add_function_hook(look_for, &param_key_expr_function, pkd);
436 void add_function_param_key_hook_late(const char *look_for, param_key_hook *call_back,
437 int param, const char *key, void *info)
439 struct param_key_data *pkd;
441 pkd = alloc_pkd(call_back, param, key, info);
442 add_function_hook_late(look_for, &param_key_function, pkd);
445 void return_implies_param_key(const char *look_for, sval_t start, sval_t end,
446 param_key_hook *call_back,
447 int param, const char *key, void *info)
449 struct param_key_data *pkd;
451 pkd = alloc_pkd(call_back, param, key, info);
452 return_implies_state_sval(look_for, start, end, &param_key_implies_function, pkd);
455 void return_implies_param_key_exact(const char *look_for, sval_t start, sval_t end,
456 param_key_hook *call_back,
457 int param, const char *key, void *info)
459 struct param_key_data *pkd;
461 pkd = alloc_pkd(call_back, param, key, info);
462 return_implies_exact(look_for, start, end, &param_key_implies_function, pkd);
465 void return_implies_param_key_expr(const char *look_for, sval_t start, sval_t end,
466 expr_func *call_back,
467 int param, const char *key, void *info)
469 struct param_key_data *pkd;
471 pkd = alloc_pked(call_back, param, key, info);
472 return_implies_state_sval(look_for, start, end, &param_key_expr_implies_function, pkd);
475 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
476 void *info)
478 struct fcall_back *cb;
480 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
481 add_callback(func_hash, look_for, cb);
484 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
485 void *info)
487 struct fcall_back *cb;
489 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
490 add_callback(func_hash, look_for, cb);
493 void return_implies_state(const char *look_for, long long start, long long end,
494 implication_hook *call_back, void *info)
496 struct fcall_back *cb;
498 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
499 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
500 add_callback(func_hash, look_for, cb);
503 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
504 implication_hook *call_back, void *info)
506 struct fcall_back *cb;
508 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
509 cb->range = alloc_range_perm(start, end);
510 add_callback(func_hash, look_for, cb);
513 void return_implies_exact(const char *look_for, sval_t start, sval_t end,
514 implication_hook *call_back, void *info)
516 struct fcall_back *cb;
518 cb = alloc_fcall_back(RANGED_EXACT, call_back, info);
519 cb->range = alloc_range_perm(start, end);
520 add_callback(func_hash, look_for, cb);
523 static struct return_implies_callback *alloc_db_return_callback(int type, bool param_key, void *callback)
525 struct return_implies_callback *cb;
527 cb = __alloc_return_implies_callback(0);
528 cb->type = type;
529 cb->param_key = param_key;
530 cb->callback = callback;
532 return cb;
535 void select_return_states_hook(int type, return_implies_hook *callback)
537 struct return_implies_callback *cb;
539 cb = alloc_db_return_callback(type, false, callback);
540 add_ptr_list(&db_return_states_list, cb);
543 static void call_db_return_callback(struct db_callback_info *db_info,
544 struct return_implies_callback *cb,
545 int param, char *key, char *value)
547 if (cb->param_key) {
548 db_helper(db_info->expr, cb->pk_callback, param, key, NULL);
549 add_ptr_list(&db_info->called, cb);
550 } else {
551 cb->callback(db_info->expr, param, key, value);
555 void select_return_param_key(int type, param_key_hook *callback)
557 struct return_implies_callback *cb;
559 cb = alloc_db_return_callback(type, true, callback);
560 add_ptr_list(&db_return_states_list, cb);
563 void select_return_states_before(void_fn *fn)
565 add_ptr_list(&return_states_before, fn);
568 void select_return_states_after(void_fn *fn)
570 add_ptr_list(&return_states_after, fn);
573 void add_return_string_hook(string_hook *fn)
575 add_ptr_list(&return_string_hooks, fn);
578 static bool call_call_backs(struct call_back_list *list, int type,
579 const char *fn, struct expression *expr)
581 struct fcall_back *tmp;
582 bool handled = false;
584 FOR_EACH_PTR(list, tmp) {
585 if (tmp->type == type) {
586 (tmp->u.call_back)(fn, expr, tmp->info);
587 handled = true;
589 } END_FOR_EACH_PTR(tmp);
591 return handled;
594 static void call_function_hooks(struct expression *expr, enum fn_hook_type type)
596 struct call_back_list *call_backs;
597 const char *fn_name;
599 while (expr->type == EXPR_ASSIGNMENT)
600 expr = strip_expr(expr->right);
601 if (expr->type != EXPR_CALL)
602 return;
604 fn_name = get_fn_name(expr->fn);
605 call_backs = get_call_backs(fn_name);
606 if (!call_backs)
607 return;
609 call_call_backs(call_backs, type, fn_name, expr);
612 static void call_return_states_after_hooks(struct expression *expr)
614 call_void_fns(return_states_after);
615 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER_DB);
616 call_function_hooks(expr, REGULAR_CALL_LATE);
619 static void call_ranged_call_backs(struct call_back_list *list,
620 const char *fn, struct expression *call_expr,
621 struct expression *assign_expr)
623 struct fcall_back *tmp;
625 FOR_EACH_PTR(list, tmp) {
626 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
627 } END_FOR_EACH_PTR(tmp);
630 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
631 struct data_range *drange)
633 struct call_back_list *ret = NULL;
634 struct fcall_back *tmp;
636 FOR_EACH_PTR(list, tmp) {
637 if (tmp->type != RANGED_CALL &&
638 tmp->type != RANGED_EXACT)
639 continue;
640 if (ranges_equiv(tmp->range, drange))
641 add_ptr_list(&ret, tmp);
642 } END_FOR_EACH_PTR(tmp);
643 return ret;
646 static bool in_list_exact_sval(struct range_list *list, struct data_range *drange)
648 struct data_range *tmp;
650 FOR_EACH_PTR(list, tmp) {
651 if (ranges_equiv(tmp, drange))
652 return true;
653 } END_FOR_EACH_PTR(tmp);
654 return false;
658 * The assign_ranged_funcs() function is called when we have no data from the DB.
660 static bool assign_ranged_funcs(const char *fn, struct expression *expr,
661 struct call_back_list *call_backs)
663 struct fcall_back *tmp;
664 struct sm_state *sm;
665 char *var_name;
666 struct symbol *sym;
667 struct smatch_state *estate;
668 struct stree *tmp_stree;
669 struct stree *final_states = NULL;
670 struct range_list *handled_ranges = NULL;
671 struct range_list *unhandled_rl;
672 struct call_back_list *same_range_call_backs = NULL;
673 struct expression *call;
674 struct range_list *rl;
675 int handled = false;
677 if (!call_backs)
678 return false;
680 var_name = expr_to_var_sym(expr->left, &sym);
681 if (!var_name || !sym)
682 goto free;
684 call = strip_expr(expr->right);
686 FOR_EACH_PTR(call_backs, tmp) {
687 if (tmp->type != RANGED_CALL &&
688 tmp->type != RANGED_EXACT)
689 continue;
691 if (in_list_exact_sval(handled_ranges, tmp->range))
692 continue;
693 __push_fake_cur_stree();
694 tack_on(&handled_ranges, tmp->range);
696 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
697 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
698 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
700 rl = alloc_rl(tmp->range->min, tmp->range->max);
701 rl = cast_rl(get_type(expr->left), rl);
702 estate = alloc_estate_rl(rl);
703 set_extra_mod(var_name, sym, expr->left, estate);
705 tmp_stree = __pop_fake_cur_stree();
706 merge_fake_stree(&final_states, tmp_stree);
707 free_stree(&tmp_stree);
708 handled = true;
709 } END_FOR_EACH_PTR(tmp);
711 unhandled_rl = rl_filter(alloc_whole_rl(get_type(call)), handled_ranges);
712 if (unhandled_rl) {
713 __push_fake_cur_stree();
714 rl = cast_rl(get_type(expr->left), unhandled_rl);
715 estate = alloc_estate_rl(rl);
716 set_extra_mod(var_name, sym, expr->left, estate);
717 tmp_stree = __pop_fake_cur_stree();
718 merge_fake_stree(&final_states, tmp_stree);
719 free_stree(&tmp_stree);
722 FOR_EACH_SM(final_states, sm) {
723 __set_sm(sm);
724 } END_FOR_EACH_SM(sm);
726 free_stree(&final_states);
727 free:
728 free_string(var_name);
729 return handled;
732 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
734 struct call_back_list *call_backs;
735 struct fcall_back *tmp;
736 const char *fn_name;
737 struct data_range *value_range;
738 struct stree *true_states = NULL;
739 struct stree *false_states = NULL;
740 struct stree *tmp_stree;
742 *implied_true = NULL;
743 *implied_false = NULL;
744 fn_name = get_fn_name(expr->fn);
745 call_backs = get_call_backs(fn_name);
746 if (!call_backs)
747 return;
748 value_range = alloc_range(sval, sval);
750 /* set true states */
751 __push_fake_cur_stree();
752 FOR_EACH_PTR(call_backs, tmp) {
753 if (tmp->type != RANGED_CALL &&
754 tmp->type != RANGED_EXACT)
755 continue;
756 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
757 continue;
758 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
759 } END_FOR_EACH_PTR(tmp);
760 tmp_stree = __pop_fake_cur_stree();
761 merge_fake_stree(&true_states, tmp_stree);
762 free_stree(&tmp_stree);
764 /* set false states */
765 __push_fake_cur_stree();
766 FOR_EACH_PTR(call_backs, tmp) {
767 if (tmp->type != RANGED_CALL &&
768 tmp->type != RANGED_EXACT)
769 continue;
770 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
771 continue;
772 (tmp->u.ranged)(fn_name, expr, NULL, tmp->info);
773 } END_FOR_EACH_PTR(tmp);
774 tmp_stree = __pop_fake_cur_stree();
775 merge_fake_stree(&false_states, tmp_stree);
776 free_stree(&tmp_stree);
778 *implied_true = true_states;
779 *implied_false = false_states;
782 static void set_implied_states(struct db_callback_info *db_info)
784 struct sm_state *sm;
786 FOR_EACH_SM(db_info->implied, sm) {
787 __set_sm(sm);
788 } END_FOR_EACH_SM(sm);
790 free_stree(&db_info->implied);
793 static void store_return_state(struct db_callback_info *db_info, const char *ret_str, struct smatch_state *state)
795 db_info->ret_str = alloc_sname(ret_str),
796 db_info->ret_state = state;
799 static struct expression_list *unfaked_calls;
801 struct expression *get_unfaked_call(void)
803 return last_ptr_list((struct ptr_list *)unfaked_calls);
806 static void store_unfaked_call(struct expression *expr)
808 push_expression(&unfaked_calls, expr);
811 static void clear_unfaked_call(void)
813 delete_ptr_list_last((struct ptr_list **)&unfaked_calls);
816 void fake_param_assign_helper(struct expression *call, struct expression *fake_assign, bool shallow)
818 store_unfaked_call(call);
819 __in_fake_parameter_assign++;
820 parse_assignment(fake_assign, true);
821 __in_fake_parameter_assign--;
822 clear_unfaked_call();
825 static bool fake_a_param_assignment(struct expression *expr, const char *ret_str, struct smatch_state *orig)
827 struct expression *arg, *left, *right, *tmp, *fake_assign;
828 char *p;
829 int param;
830 char buf[256];
831 char *str;
833 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
834 return false;
835 left = expr->left;
836 right = expr->right;
838 while (right->type == EXPR_ASSIGNMENT)
839 right = strip_expr(right->right);
840 if (!right || right->type != EXPR_CALL)
841 return false;
843 p = strchr(ret_str, '[');
844 if (!p)
845 return false;
847 p++;
848 if (p[0] == '=' && p[1] == '=')
849 p += 2;
850 if (p[0] != '$')
851 return false;
853 snprintf(buf, sizeof(buf), "%s", p);
855 p = buf;
856 p += 1;
857 param = strtol(p, &p, 10);
859 p = strchr(p, ']');
860 if (!p || *p != ']')
861 return false;
862 *p = '\0';
864 arg = get_argument_from_call_expr(right->args, param);
865 if (!arg)
866 return false;
868 /* There should be a get_other_name() function which returns an expr */
869 tmp = get_assigned_expr(arg);
870 if (tmp)
871 arg = tmp;
874 * This is a sanity check to prevent side effects from evaluating stuff
875 * twice.
877 str = expr_to_chunk_sym_vsl(arg, NULL, NULL);
878 if (!str)
879 return false;
880 free_string(str);
882 right = gen_expression_from_key(arg, buf);
883 if (!right) /* Mostly fails for binops like [$0 + 4032] */
884 return false;
885 fake_assign = assign_expression(left, '=', right);
886 fake_param_assign_helper(expr, fake_assign, false);
889 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
890 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
891 * parts have to be considered. We use _nomod() because it's not really
892 * another modification, it's just a clarification.
895 if (estate_rl(orig)) {
896 struct smatch_state *faked;
897 struct range_list *rl;
899 faked = get_extra_state(left);
900 if (estate_rl(faked)) {
901 rl = rl_intersection(estate_rl(faked), estate_rl(orig));
902 if (rl)
903 set_extra_expr_nomod(left, alloc_estate_rl(rl));
907 return true;
910 static void fake_return_assignment(struct db_callback_info *db_info, int type, int param, char *key, char *value)
912 struct expression *call, *left, *right, *assign;
913 int right_param;
915 if (type != PARAM_COMPARE)
916 return;
918 call = db_info->expr;
919 while (call && call->type == EXPR_ASSIGNMENT)
920 call = strip_expr(call->right);
921 if (!call || call->type != EXPR_CALL)
922 return;
924 // TODO: This only handles "$->foo = arg" and not "$->foo = arg->bar".
925 if (param != -1)
926 return;
927 if (!value || strncmp(value, "== $", 4) != 0)
928 return;
929 if (!isdigit(value[4]) || value[5] != '\0')
930 return;
931 right_param = atoi(value + 4);
933 left = gen_expr_from_param_key(db_info->expr, param, key);
934 if (!left)
935 return;
936 right = get_argument_from_call_expr(call->args, right_param);
938 assign = assign_expression(left, '=', right);
939 push_expression(&db_info->fake_param_assign_stack, assign);
942 static void set_fresh_mtag_returns(struct db_callback_info *db_info)
944 struct expression *expr;
945 struct smatch_state *state;
947 if (!db_info->ret_state)
948 return;
950 if (!db_info->expr ||
951 db_info->expr->type != EXPR_ASSIGNMENT ||
952 db_info->expr->op != '=')
953 return;
955 expr = db_info->expr->left;
957 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
958 state = get_mtag_return(db_info->expr, state);
959 if (!state)
960 return;
962 set_real_absolute(expr, state);
963 set_extra_expr_mod(expr, state);
966 static void set_return_assign_state(struct db_callback_info *db_info)
968 struct expression *expr = db_info->expr->left;
969 struct expression *fake_assign;
970 struct smatch_state *state;
971 bool was_set = false;
973 if (!db_info->ret_state)
974 return;
976 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
977 if (!fake_a_param_assignment(db_info->expr, db_info->ret_str, state)) {
978 set_extra_expr_mod(expr, state);
979 was_set = true;
982 while ((fake_assign = pop_expression(&db_info->fake_param_assign_stack))) {
983 struct range_list *right;
986 * Originally, I tried to do this as a assignment to record that
987 * a = frob(b) implies that "a->foo == b->foo" etc. But that
988 * caused a problem because then it was recorded that "a->foo"
989 * was modified and recorded as a PARAM_SET in the database.
991 * So now, instead of faking an assignment we use
992 * set_extra_expr_nomod() but it's still recorded as an
993 * assignment in the ->fake_param_assign_stack for legacy
994 * reasons and because it's a handy way to store a left/right
995 * pair.
998 get_absolute_rl(fake_assign->right, &right);
999 right = cast_rl(get_type(fake_assign->left), right);
1000 // FIXME: add some sanity checks
1001 // FIXME: preserve the sm state if possible
1002 set_extra_expr_nomod(fake_assign->left, alloc_estate_rl(right));
1003 was_set = true;
1006 if (!was_set)
1007 set_extra_expr_mod(expr, state);
1010 static void set_other_side_state(struct db_callback_info *db_info)
1012 struct expression *expr = db_info->var_expr;
1013 struct smatch_state *state;
1015 if (!db_info->ret_state)
1016 return;
1018 // TODO: faked_assign set ==$ equiv here
1020 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
1021 set_extra_expr_nomod(expr, state);
1022 db_info->ret_state = NULL;
1023 db_info->ret_str = NULL;
1026 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
1028 char *str;
1029 long long param;
1030 struct expression *arg;
1031 struct range_list *orig;
1033 // TODO: faked_assign This needs to be handled in the assignment code
1035 str = strstr(ret_string, "==$");
1036 if (!str)
1037 return;
1038 str += 3;
1039 param = strtoll(str, NULL, 10);
1040 arg = get_argument_from_call_expr(call->args, param);
1041 if (!arg)
1042 return;
1043 get_absolute_rl(arg, &orig);
1044 rl = rl_intersection(orig, rl);
1045 if (!rl)
1046 return;
1047 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
1050 static bool impossible_limit(struct db_callback_info *db_info, int param, char *key, char *value)
1052 struct expression *expr = db_info->expr;
1053 struct expression *arg;
1054 struct smatch_state *state;
1055 struct range_list *passed;
1056 struct range_list *limit;
1057 struct symbol *compare_type;
1059 while (expr->type == EXPR_ASSIGNMENT)
1060 expr = strip_expr(expr->right);
1061 if (expr->type != EXPR_CALL)
1062 return false;
1064 arg = get_argument_from_call_expr(expr->args, param);
1065 if (!arg)
1066 return false;
1068 if (strcmp(key, "$") == 0) {
1069 if (!get_implied_rl(arg, &passed))
1070 return false;
1072 compare_type = get_arg_type(expr->fn, param);
1073 } else {
1074 char *name;
1075 struct symbol *sym;
1077 name = get_variable_from_key(arg, key, &sym);
1078 if (!name || !sym)
1079 return false;
1081 state = get_state(SMATCH_EXTRA, name, sym);
1082 if (!state) {
1083 free_string(name);
1084 return false;
1086 passed = estate_rl(state);
1087 if (!passed || is_whole_rl(passed)) {
1088 free_string(name);
1089 return false;
1092 compare_type = get_member_type_from_key(arg, key);
1095 passed = cast_rl(compare_type, passed);
1096 call_results_to_rl(expr, compare_type, value, &limit);
1097 if (!limit || is_whole_rl(limit))
1098 return false;
1099 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
1100 return false;
1101 if (option_debug || local_debug || debug_db)
1102 sm_msg("impossible: %d '%s' limit '%s' == '%s' return='%s'", param, key, show_rl(passed), value, db_info->ret_str);
1103 return true;
1106 static bool is_impossible_data(int type, struct db_callback_info *db_info, int param, char *key, char *value)
1108 if (type == PARAM_LIMIT && impossible_limit(db_info, param, key, value))
1109 return true;
1110 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(db_info->expr, param, key, value)) {
1111 if (local_debug || debug_db)
1112 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
1113 return true;
1115 return false;
1118 static bool func_type_mismatch(struct expression *expr, const char *value)
1120 struct symbol *type;
1122 /* This makes faking returns easier */
1123 if (!value || value[0] == '\0')
1124 return false;
1126 while (expr->type == EXPR_ASSIGNMENT)
1127 expr = strip_expr(expr->right);
1130 * Short cut: We only care about function pointers that are struct
1131 * members.
1134 if (expr->fn->type == EXPR_SYMBOL)
1135 return false;
1137 type = get_type(expr->fn);
1138 if (!type)
1139 return false;
1140 if (type->type == SYM_PTR)
1141 type = get_real_base_type(type);
1143 if (strcmp(type_to_str(type), value) == 0)
1144 return false;
1146 return true;
1149 static void process_return_states(struct db_callback_info *db_info)
1151 struct stree *stree;
1153 set_implied_states(db_info);
1154 set_fresh_mtag_returns(db_info);
1155 parse_fake_calls();
1156 free_ptr_list(&db_info->called);
1157 stree = __pop_fake_cur_stree();
1158 if (debug_db) {
1159 sm_msg("States from DB: %s expr='%s' ret_str='%s' rl='%s' state='%s'",
1160 db_info->cull ? "Culling" : "Merging",
1161 expr_to_str(db_info->expr),
1162 db_info->ret_str, show_rl(db_info->rl),
1163 db_info->ret_state ? db_info->ret_state->name : "<none>");
1164 __print_stree(stree);
1167 if (!db_info->cull) {
1168 merge_fake_stree(&db_info->stree, stree);
1169 db_info->states_merged = true;
1171 free_stree(&stree);
1173 db_info->ret_state = NULL;
1174 db_info->ret_str = NULL;
1177 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
1179 struct db_callback_info *db_info = _info;
1180 struct range_list *var_rl = db_info->rl;
1181 struct range_list *ret_range;
1182 int type, param;
1183 char *ret_str, *key, *value;
1184 struct return_implies_callback *tmp;
1185 int return_id;
1186 int comparison;
1188 if (argc != 6)
1189 return 0;
1191 return_id = atoi(argv[0]);
1192 ret_str = argv[1];
1193 type = atoi(argv[2]);
1194 param = atoi(argv[3]);
1195 key = argv[4];
1196 value = argv[5];
1198 db_info->has_states = 1;
1199 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1200 set_other_side_state(db_info);
1201 process_return_states(db_info);
1202 __push_fake_cur_stree();
1203 db_info->cull = 0;
1205 db_info->prev_return_id = return_id;
1207 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1208 db_info->cull = 1;
1209 if (db_info->cull)
1210 return 0;
1211 if (type == CULL_PATH) {
1212 db_info->cull = 1;
1213 return 0;
1216 if (is_impossible_data(type, db_info, param, key, value)) {
1217 db_info->cull = 1;
1218 return 0;
1221 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1222 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1223 if (!ret_range)
1224 ret_range = alloc_whole_rl(get_type(db_info->expr));
1226 comparison = db_info->comparison;
1227 if (db_info->left)
1228 comparison = flip_comparison(comparison);
1230 if (db_info->true_side) {
1231 if (!possibly_true_rl(var_rl, comparison, ret_range))
1232 return 0;
1233 if (type == PARAM_LIMIT)
1234 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1235 else if (type > PARAM_LIMIT)
1236 set_implied_states(db_info);
1237 filter_by_comparison(&var_rl, comparison, ret_range);
1238 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
1239 } else {
1240 if (!possibly_false_rl(var_rl, comparison, ret_range))
1241 return 0;
1242 if (type == PARAM_LIMIT)
1243 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1244 else if (type > PARAM_LIMIT)
1245 set_implied_states(db_info);
1246 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
1247 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
1250 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
1252 if (type == INTERNAL) {
1253 set_state(-1, "unnull_path", NULL, &true_state);
1254 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1255 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
1258 FOR_EACH_PTR(db_info->callbacks, tmp) {
1259 if (tmp->type == type)
1260 call_db_return_callback(db_info, tmp, param, key, value);
1261 } END_FOR_EACH_PTR(tmp);
1263 fake_return_assignment(db_info, type, param, key, value);
1265 return 0;
1268 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
1270 struct stree *orig_states;
1271 struct stree *true_states;
1272 struct stree *false_states;
1273 struct sm_state *sm;
1274 struct db_callback_info db_info = {};
1275 struct expression *var_expr;
1276 struct expression *call_expr;
1277 struct range_list *rl;
1278 int call_on_left;
1280 orig_states = clone_stree(__get_cur_stree());
1282 /* legacy cruft. need to fix call_implies_callbacks(). */
1283 call_on_left = 1;
1284 call_expr = left;
1285 var_expr = right;
1286 if (left->type != EXPR_CALL) {
1287 call_on_left = 0;
1288 call_expr = right;
1289 var_expr = left;
1292 get_absolute_rl(var_expr, &rl);
1294 db_info.comparison = comparison;
1295 db_info.expr = call_expr;
1296 db_info.rl = rl;
1297 db_info.left = call_on_left;
1298 db_info.callbacks = db_return_states_list;
1299 db_info.var_expr = var_expr;
1301 call_void_fns(return_states_before);
1303 db_info.true_side = 1;
1304 db_info.stree = NULL;
1305 db_info.prev_return_id = -1;
1306 __push_fake_cur_stree();
1307 sql_select_return_states("return_id, return, type, parameter, key, value",
1308 call_expr, db_compare_callback, &db_info);
1309 set_other_side_state(&db_info);
1310 process_return_states(&db_info);
1311 true_states = db_info.stree;
1312 if (!true_states && db_info.has_states) {
1313 __push_fake_cur_stree();
1314 set_path_impossible();
1315 true_states = __pop_fake_cur_stree();
1318 nullify_path();
1319 __unnullify_path();
1320 FOR_EACH_SM(orig_states, sm) {
1321 __set_sm_cur_stree(sm);
1322 } END_FOR_EACH_SM(sm);
1324 db_info.true_side = 0;
1325 db_info.stree = NULL;
1326 db_info.prev_return_id = -1;
1327 db_info.cull = 0;
1328 __push_fake_cur_stree();
1329 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
1330 db_compare_callback, &db_info);
1331 set_other_side_state(&db_info);
1332 process_return_states(&db_info);
1333 false_states = db_info.stree;
1334 if (!false_states && db_info.has_states) {
1335 __push_fake_cur_stree();
1336 set_path_impossible();
1337 false_states = __pop_fake_cur_stree();
1340 nullify_path();
1341 __unnullify_path();
1342 FOR_EACH_SM(orig_states, sm) {
1343 __set_sm_cur_stree(sm);
1344 } END_FOR_EACH_SM(sm);
1346 free_stree(&orig_states);
1348 FOR_EACH_SM(true_states, sm) {
1349 __set_true_false_sm(sm, NULL);
1350 } END_FOR_EACH_SM(sm);
1351 FOR_EACH_SM(false_states, sm) {
1352 __set_true_false_sm(NULL, sm);
1353 } END_FOR_EACH_SM(sm);
1355 free_stree(&true_states);
1356 free_stree(&false_states);
1358 if (!db_info.states_merged)
1359 mark_call_params_untracked(call_expr);
1361 call_return_states_after_hooks(call_expr);
1363 FOR_EACH_SM(implied_true, sm) {
1364 __set_true_false_sm(sm, NULL);
1365 } END_FOR_EACH_SM(sm);
1366 FOR_EACH_SM(implied_false, sm) {
1367 __set_true_false_sm(NULL, sm);
1368 } END_FOR_EACH_SM(sm);
1371 void function_comparison(struct expression *left, int comparison, struct expression *right)
1373 struct expression *var_expr;
1374 struct expression *call_expr;
1375 struct stree *implied_true = NULL;
1376 struct stree *implied_false = NULL;
1377 struct range_list *rl;
1378 sval_t sval;
1379 int call_on_left;
1381 // TODO: faked_assign delete this
1382 // condition calls should be faked and then handled as assignments
1383 // this code is a lazy work around
1385 if (unreachable())
1386 return;
1388 /* legacy cruft. need to fix call_implies_callbacks(). */
1389 call_on_left = 1;
1390 call_expr = left;
1391 var_expr = right;
1392 if (left->type != EXPR_CALL) {
1393 call_on_left = 0;
1394 call_expr = right;
1395 var_expr = left;
1398 get_absolute_rl(var_expr, &rl);
1400 if (rl_to_sval(rl, &sval))
1401 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
1403 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
1404 free_stree(&implied_true);
1405 free_stree(&implied_false);
1408 static void call_ranged_return_hooks(struct db_callback_info *db_info)
1410 struct call_back_list *call_backs;
1411 struct range_list *range_rl;
1412 struct expression *expr;
1413 struct fcall_back *tmp;
1414 const char *fn_name;
1416 expr = strip_expr(db_info->expr);
1417 while (expr->type == EXPR_ASSIGNMENT)
1418 expr = strip_expr(expr->right);
1419 if (expr->type != EXPR_CALL)
1420 return;
1422 fn_name = get_fn_name(expr->fn);
1423 call_backs = get_call_backs(fn_name);
1424 FOR_EACH_PTR(call_backs, tmp) {
1425 if (tmp->type != RANGED_CALL)
1426 continue;
1427 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1428 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1429 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
1430 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1431 } END_FOR_EACH_PTR(tmp);
1433 FOR_EACH_PTR(call_backs, tmp) {
1434 if (tmp->type != RANGED_EXACT)
1435 continue;
1436 if (!estate_rl(db_info->ret_state))
1437 continue;
1439 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
1440 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
1443 * If there is an returned value out of range then this is not
1444 * an exact match. In other words, "0,4096-ptr_max" is not
1445 * necessarily a valid match.
1448 if (remove_range(estate_rl(db_info->ret_state),
1449 rl_min(range_rl), rl_max(range_rl)))
1450 continue;
1451 (tmp->u.ranged)(fn_name, expr, db_info->expr, tmp->info);
1452 } END_FOR_EACH_PTR(tmp);
1455 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1457 struct db_callback_info *db_info = _info;
1458 struct range_list *ret_range;
1459 int type, param;
1460 char *ret_str, *key, *value;
1461 struct return_implies_callback *tmp;
1462 int return_id;
1464 if (argc != 6)
1465 return 0;
1467 return_id = atoi(argv[0]);
1468 ret_str = argv[1];
1469 type = atoi(argv[2]);
1470 param = atoi(argv[3]);
1471 key = argv[4];
1472 value = argv[5];
1474 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1475 call_ranged_return_hooks(db_info);
1476 set_return_assign_state(db_info);
1477 process_return_states(db_info);
1478 __push_fake_cur_stree();
1479 db_info->cull = 0;
1481 db_info->prev_return_id = return_id;
1483 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1484 db_info->cull = 1;
1485 if (db_info->cull)
1486 return 0;
1487 if (type == CULL_PATH) {
1488 db_info->cull = 1;
1489 return 0;
1491 if (is_impossible_data(type, db_info, param, key, value)) {
1492 db_info->cull = 1;
1493 return 0;
1496 if (type == PARAM_LIMIT)
1497 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1498 else if (type > PARAM_LIMIT)
1499 set_implied_states(db_info);
1501 db_info->handled = 1;
1502 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
1503 if (!ret_range)
1504 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
1505 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
1507 if (type == INTERNAL) {
1508 set_state(-1, "unnull_path", NULL, &true_state);
1509 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
1510 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1511 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
1514 FOR_EACH_PTR(db_return_states_list, tmp) {
1515 if (tmp->type == type)
1516 call_db_return_callback(db_info, tmp, param, key, value);
1517 } END_FOR_EACH_PTR(tmp);
1519 fake_return_assignment(db_info, type, param, key, value);
1521 return 0;
1524 static int db_return_states_assign(struct expression *expr)
1526 struct expression *right;
1527 struct sm_state *sm;
1528 struct db_callback_info db_info = {};
1530 right = strip_expr(expr->right);
1532 db_info.prev_return_id = -1;
1533 db_info.expr = expr;
1534 db_info.stree = NULL;
1535 db_info.handled = 0;
1537 call_void_fns(return_states_before);
1539 __push_fake_cur_stree();
1540 sql_select_return_states("return_id, return, type, parameter, key, value",
1541 right, db_assign_return_states_callback, &db_info);
1542 if (option_debug) {
1543 sm_msg("%s return_id %d return_ranges %s",
1544 db_info.cull ? "culled" : "merging",
1545 db_info.prev_return_id,
1546 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
1548 if (db_info.handled)
1549 call_ranged_return_hooks(&db_info);
1550 set_return_assign_state(&db_info);
1551 process_return_states(&db_info);
1553 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
1554 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1555 set_path_impossible();
1557 FOR_EACH_SM(db_info.stree, sm) {
1558 __set_sm(sm);
1559 } END_FOR_EACH_SM(sm);
1561 free_stree(&db_info.stree);
1563 if (!db_info.states_merged)
1564 mark_call_params_untracked(right);
1566 call_return_states_after_hooks(right);
1568 return db_info.handled;
1571 static bool handle_implied_return(struct expression *expr)
1573 struct range_list *rl;
1575 if (!get_implied_return(expr->right, &rl))
1576 return false;
1577 rl = cast_rl(get_type(expr->left), rl);
1578 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1579 return true;
1582 static void match_assign_call(struct expression *expr)
1584 struct call_back_list *call_backs;
1585 const char *fn_name;
1586 struct expression *right;
1587 int handled = 0;
1588 struct range_list *rl;
1590 if (expr->op != '=')
1591 return;
1593 right = strip_expr(expr->right);
1594 if (is_fake_call(right))
1595 return;
1597 fn_name = get_fn_name(right->fn);
1598 call_backs = get_call_backs(fn_name);
1601 * The ordering here is sort of important.
1602 * One example, of how this matters is that when we do:
1604 * len = strlen(str);
1606 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1607 * They use implied_return and function_assign_hook respectively.
1608 * We want to get the implied return first before we do the function
1609 * assignment hook otherwise we end up writing the wrong thing for len
1610 * in smatch_extra.c because we assume that it already holds the
1611 * strlen() when we haven't set it yet.
1614 if (db_return_states_assign(expr))
1615 handled = 1;
1616 else
1617 handled = assign_ranged_funcs(fn_name, expr, call_backs);
1618 handled |= handle_implied_return(expr);
1621 call_call_backs(call_backs, ASSIGN_CALL, fn_name, expr);
1623 if (handled)
1624 return;
1626 /* assignment wasn't handled at all */
1627 get_absolute_rl(expr->right, &rl);
1628 rl = cast_rl(get_type(expr->left), rl);
1629 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1632 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1634 struct db_callback_info *db_info = _info;
1635 struct range_list *ret_range;
1636 int type, param;
1637 char *ret_str, *key, *value;
1638 struct return_implies_callback *tmp;
1639 int return_id;
1641 if (argc != 6)
1642 return 0;
1644 return_id = atoi(argv[0]);
1645 ret_str = argv[1];
1646 type = atoi(argv[2]);
1647 param = atoi(argv[3]);
1648 key = argv[4];
1649 value = argv[5];
1651 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1652 call_ranged_return_hooks(db_info);
1653 process_return_states(db_info);
1654 __push_fake_cur_stree();
1655 __unnullify_path();
1656 db_info->cull = 0;
1658 db_info->prev_return_id = return_id;
1660 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1661 db_info->cull = 1;
1662 if (db_info->cull)
1663 return 0;
1664 if (type == CULL_PATH) {
1665 db_info->cull = 1;
1666 return 0;
1668 if (is_impossible_data(type, db_info, param, key, value)) {
1669 db_info->cull = 1;
1670 return 0;
1673 if (type == PARAM_LIMIT)
1674 param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1675 else if (type > PARAM_LIMIT)
1676 set_implied_states(db_info);
1678 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1679 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1681 if (type == INTERNAL) {
1682 struct smatch_state *state;
1684 set_state(-1, "unnull_path", NULL, &true_state);
1685 call_string_hooks(return_string_hooks, db_info->expr, ret_str);
1686 state = alloc_estate_rl(ret_range);
1687 store_return_state(db_info, ret_str, state);
1690 FOR_EACH_PTR(db_return_states_list, tmp) {
1691 if (tmp->type == type)
1692 call_db_return_callback(db_info, tmp, param, key, value);
1693 } END_FOR_EACH_PTR(tmp);
1695 fake_return_assignment(db_info, type, param, key, value);
1697 return 0;
1700 static void db_return_states(struct expression *expr)
1702 struct sm_state *sm;
1703 struct db_callback_info db_info = {};
1705 if (!__get_cur_stree()) /* no return functions */
1706 return;
1708 db_info.prev_return_id = -1;
1709 db_info.expr = expr;
1710 db_info.stree = NULL;
1712 call_void_fns(return_states_before);
1714 __push_fake_cur_stree();
1715 __unnullify_path();
1716 sql_select_return_states("return_id, return, type, parameter, key, value",
1717 expr, db_return_states_callback, &db_info);
1718 call_ranged_return_hooks(&db_info);
1719 process_return_states(&db_info);
1721 FOR_EACH_SM(db_info.stree, sm) {
1722 __set_sm(sm);
1723 } END_FOR_EACH_SM(sm);
1725 free_stree(&db_info.stree);
1727 if (!db_info.states_merged)
1728 mark_call_params_untracked(expr);
1730 call_return_states_after_hooks(expr);
1733 static void db_return_states_call(struct expression *expr)
1735 if (unreachable())
1736 return;
1738 if (is_assigned_call(expr) || is_fake_assigned_call(expr))
1739 return;
1740 if (is_condition_call(expr))
1741 return;
1742 db_return_states(expr);
1745 static void match_function_call_early(struct expression *expr)
1747 call_function_hooks(expr, REGULAR_CALL_EARLY);
1750 static void match_function_call(struct expression *expr)
1752 call_function_hooks(expr, REGULAR_CALL);
1753 db_return_states_call(expr);
1754 /* If we have no database there could be unprocessed fake calls */
1755 parse_fake_calls();
1758 static void match_macro_assign(struct expression *expr)
1760 struct call_back_list *call_backs;
1761 const char *macro;
1762 struct expression *right;
1764 right = strip_expr(expr->right);
1765 macro = get_macro_name(right->pos);
1766 call_backs = search_callback(func_hash, (char *)macro);
1767 if (!call_backs)
1768 return;
1769 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1770 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1773 bool get_implied_return(struct expression *expr, struct range_list **rl)
1775 struct call_back_list *call_backs;
1776 struct fcall_back *tmp;
1777 bool handled = false;
1778 char *fn;
1780 *rl = NULL;
1782 expr = strip_expr(expr);
1783 fn = expr_to_var(expr->fn);
1784 if (!fn)
1785 goto out;
1787 call_backs = search_callback(func_hash, fn);
1789 FOR_EACH_PTR(call_backs, tmp) {
1790 if (tmp->type == IMPLIED_RETURN)
1791 handled |= (tmp->u.implied_return)(expr, tmp->info, rl);
1792 } END_FOR_EACH_PTR(tmp);
1794 out:
1795 free_string(fn);
1796 return handled;
1799 struct range_list *get_range_implications(const char *fn)
1801 struct call_back_list *call_backs;
1802 struct range_list *ret = NULL;
1803 struct fcall_back *tmp;
1805 call_backs = search_callback(func_hash, (char *)fn);
1807 FOR_EACH_PTR(call_backs, tmp) {
1808 if (tmp->type != RANGED_CALL &&
1809 tmp->type != RANGED_EXACT)
1810 continue;
1811 add_ptr_list(&ret, tmp->range);
1812 } END_FOR_EACH_PTR(tmp);
1814 return ret;
1817 void create_function_hook_hash(void)
1819 func_hash = create_function_hashtable(5000);
1822 void register_function_hooks_early(int id)
1824 add_hook(&match_function_call_early, FUNCTION_CALL_HOOK_BEFORE);
1827 void register_function_hooks(int id)
1829 add_function_data((unsigned long *)&fake_calls);
1830 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1831 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1832 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);