function_hooks: add a hack around in compare_db_return_states_callbacks
[smatch.git] / smatch_function_hooks.c
blob0e20d5abb3ae435486b25d6fe84cade02fe4e438
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:
20 * add_function_hook() - For any time a function is called.
21 * add_function_assign_hook() - foo = the_function().
22 * add_implied_return_hook() - Calculates the implied return value.
23 * add_macro_assign_hook() - foo = the_macro().
24 * return_implies_state() - For when a return value of 1 implies locked
25 * and 0 implies unlocked. etc. etc.
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include "smatch.h"
32 #include "smatch_slist.h"
33 #include "smatch_extra.h"
34 #include "smatch_function_hashtable.h"
36 struct fcall_back {
37 int type;
38 struct data_range *range;
39 union {
40 func_hook *call_back;
41 implication_hook *ranged;
42 implied_return_hook *implied_return;
43 } u;
44 void *info;
47 ALLOCATOR(fcall_back, "call backs");
48 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
50 DEFINE_FUNCTION_HASHTABLE_STATIC(callback, struct fcall_back, struct call_back_list);
51 static struct hashtable *func_hash;
53 #define REGULAR_CALL 0
54 #define RANGED_CALL 1
55 #define ASSIGN_CALL 2
56 #define IMPLIED_RETURN 3
57 #define MACRO_ASSIGN 4
58 #define MACRO_ASSIGN_EXTRA 5
60 struct return_implies_callback {
61 int type;
62 return_implies_hook *callback;
64 ALLOCATOR(return_implies_callback, "return_implies callbacks");
65 DECLARE_PTR_LIST(db_implies_list, struct return_implies_callback);
66 static struct db_implies_list *db_return_states_list;
68 typedef void (void_fn)(void);
69 DECLARE_PTR_LIST(void_fn_list, void_fn *);
70 static struct void_fn_list *return_states_before;
71 static struct void_fn_list *return_states_after;
73 static struct fcall_back *alloc_fcall_back(int type, void *call_back,
74 void *info)
76 struct fcall_back *cb;
78 cb = __alloc_fcall_back(0);
79 cb->type = type;
80 cb->u.call_back = call_back;
81 cb->info = info;
82 return cb;
85 void add_function_hook(const char *look_for, func_hook *call_back, void *info)
87 struct fcall_back *cb;
89 cb = alloc_fcall_back(REGULAR_CALL, call_back, info);
90 add_callback(func_hash, look_for, cb);
93 void add_function_assign_hook(const char *look_for, func_hook *call_back,
94 void *info)
96 struct fcall_back *cb;
98 cb = alloc_fcall_back(ASSIGN_CALL, call_back, info);
99 add_callback(func_hash, look_for, cb);
102 void add_implied_return_hook(const char *look_for,
103 implied_return_hook *call_back,
104 void *info)
106 struct fcall_back *cb;
108 cb = alloc_fcall_back(IMPLIED_RETURN, call_back, info);
109 add_callback(func_hash, look_for, cb);
112 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
113 void *info)
115 struct fcall_back *cb;
117 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
118 add_callback(func_hash, look_for, cb);
121 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
122 void *info)
124 struct fcall_back *cb;
126 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
127 add_callback(func_hash, look_for, cb);
130 void return_implies_state(const char *look_for, long long start, long long end,
131 implication_hook *call_back, void *info)
133 struct fcall_back *cb;
135 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
136 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
137 add_callback(func_hash, look_for, cb);
140 void select_return_states_hook(int type, return_implies_hook *callback)
142 struct return_implies_callback *cb = __alloc_return_implies_callback(0);
144 cb->type = type;
145 cb->callback = callback;
146 add_ptr_list(&db_return_states_list, cb);
149 void select_return_states_before(void_fn *fn)
151 void_fn **p = malloc(sizeof(void_fn *));
152 *p = fn;
153 add_ptr_list(&return_states_before, p);
156 void select_return_states_after(void_fn *fn)
158 void_fn **p = malloc(sizeof(void_fn *));
159 *p = fn;
160 add_ptr_list(&return_states_after, p);
163 static void call_return_states_before_hooks(void)
165 void_fn **fn;
167 FOR_EACH_PTR(return_states_before, fn) {
168 (*fn)();
169 } END_FOR_EACH_PTR(fn);
172 static void call_return_states_after_hooks(void)
174 void_fn **fn;
176 FOR_EACH_PTR(return_states_after, fn) {
177 (*fn)();
178 } END_FOR_EACH_PTR(fn);
181 static int call_call_backs(struct call_back_list *list, int type,
182 const char *fn, struct expression *expr)
184 struct fcall_back *tmp;
185 int handled = 0;
187 FOR_EACH_PTR(list, tmp) {
188 if (tmp->type == type) {
189 (tmp->u.call_back)(fn, expr, tmp->info);
190 handled = 1;
192 } END_FOR_EACH_PTR(tmp);
194 return handled;
197 static void call_ranged_call_backs(struct call_back_list *list,
198 const char *fn, struct expression *call_expr,
199 struct expression *assign_expr)
201 struct fcall_back *tmp;
203 FOR_EACH_PTR(list, tmp) {
204 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
205 } END_FOR_EACH_PTR(tmp);
208 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
209 struct data_range *drange)
211 struct call_back_list *ret = NULL;
212 struct fcall_back *tmp;
214 FOR_EACH_PTR(list, tmp) {
215 if (tmp->type != RANGED_CALL)
216 continue;
217 if (ranges_equiv(tmp->range, drange))
218 add_ptr_list(&ret, tmp);
219 } END_FOR_EACH_PTR(tmp);
220 return ret;
223 static int in_list_exact_sval(struct range_list *list, struct data_range *drange)
225 struct data_range *tmp;
227 FOR_EACH_PTR(list, tmp) {
228 if (ranges_equiv(tmp, drange))
229 return 1;
230 } END_FOR_EACH_PTR(tmp);
231 return 0;
234 static int assign_ranged_funcs(const char *fn, struct expression *expr,
235 struct call_back_list *call_backs)
237 struct fcall_back *tmp;
238 struct sm_state *sm;
239 char *var_name;
240 struct symbol *sym;
241 struct smatch_state *estate;
242 struct stree *tmp_stree;
243 struct stree *final_states = NULL;
244 struct range_list *handled_ranges = NULL;
245 struct call_back_list *same_range_call_backs = NULL;
246 int handled = 0;
248 if (!call_backs)
249 return 0;
251 var_name = expr_to_var_sym(expr->left, &sym);
252 if (!var_name || !sym)
253 goto free;
255 FOR_EACH_PTR(call_backs, tmp) {
256 if (tmp->type != RANGED_CALL)
257 continue;
259 if (in_list_exact_sval(handled_ranges, tmp->range))
260 continue;
261 __push_fake_cur_stree();
262 tack_on(&handled_ranges, tmp->range);
264 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
265 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
266 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
268 estate = alloc_estate_range(tmp->range->min, tmp->range->max);
269 set_extra_mod(var_name, sym, estate);
271 tmp_stree = __pop_fake_cur_stree();
272 merge_fake_stree(&final_states, tmp_stree);
273 free_stree(&tmp_stree);
274 handled = 1;
275 } END_FOR_EACH_PTR(tmp);
277 FOR_EACH_SM(final_states, sm) {
278 __set_sm(sm);
279 } END_FOR_EACH_SM(sm);
281 free_stree(&final_states);
282 free:
283 free_string(var_name);
284 return handled;
287 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
289 struct call_back_list *call_backs;
290 struct fcall_back *tmp;
291 const char *fn;
292 struct data_range *value_range;
293 struct stree *true_states = NULL;
294 struct stree *false_states = NULL;
295 struct stree *tmp_stree;
297 *implied_true = NULL;
298 *implied_false = NULL;
299 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
300 return;
301 fn = expr->fn->symbol->ident->name;
302 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
303 if (!call_backs)
304 return;
305 value_range = alloc_range(sval, sval);
307 /* set true states */
308 __push_fake_cur_stree();
309 FOR_EACH_PTR(call_backs, tmp) {
310 if (tmp->type != RANGED_CALL)
311 continue;
312 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
313 continue;
314 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
315 } END_FOR_EACH_PTR(tmp);
316 tmp_stree = __pop_fake_cur_stree();
317 merge_fake_stree(&true_states, tmp_stree);
318 free_stree(&tmp_stree);
320 /* set false states */
321 __push_fake_cur_stree();
322 FOR_EACH_PTR(call_backs, tmp) {
323 if (tmp->type != RANGED_CALL)
324 continue;
325 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
326 continue;
327 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
328 } END_FOR_EACH_PTR(tmp);
329 tmp_stree = __pop_fake_cur_stree();
330 merge_fake_stree(&false_states, tmp_stree);
331 free_stree(&tmp_stree);
333 *implied_true = true_states;
334 *implied_false = false_states;
337 struct db_callback_info {
338 int true_side;
339 int comparison;
340 struct expression *expr;
341 struct range_list *rl;
342 int left;
343 struct stree *stree;
344 struct db_implies_list *callbacks;
345 int prev_return_id;
346 int cull;
347 struct smatch_state *ret_state;
348 struct expression *var_expr;
351 static void store_return_state(struct db_callback_info *db_info, struct smatch_state *state)
353 db_info->ret_state = state;
356 static void set_return_state(struct expression *expr, struct db_callback_info *db_info)
358 if (!db_info->ret_state)
359 return;
361 set_extra_expr_mod(expr, db_info->ret_state);
362 db_info->ret_state = NULL;
365 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
367 char *str;
368 long long param;
369 struct expression *arg;
370 struct range_list *orig;
372 str = strstr(ret_string, "==$");
373 if (!str)
374 return;
375 str += 3;
376 param = strtoll(str, NULL, 10);
377 arg = get_argument_from_call_expr(call->args, param);
378 if (!arg)
379 return;
380 get_absolute_rl(arg, &orig);
381 rl = rl_intersection(orig, rl);
382 if (!rl)
383 return;
384 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
387 static int impossible_limit(struct expression *expr, int param, char *key, char *value)
389 struct expression *arg;
390 struct range_list *passed;
391 struct range_list *limit;
392 struct symbol *compare_type;
394 if (strcmp(key, "$") != 0)
395 return 0;
397 while (expr->type == EXPR_ASSIGNMENT)
398 expr = strip_expr(expr->right);
399 if (expr->type != EXPR_CALL)
400 return 0;
402 arg = get_argument_from_call_expr(expr->args, param);
403 if (!arg)
404 return 0;
405 if (!get_implied_rl(arg, &passed))
406 return 0;
407 if (!passed || is_whole_rl(passed))
408 return 0;
410 compare_type = get_arg_type(expr->fn, param);
411 passed = cast_rl(compare_type, passed);
412 call_results_to_rl(expr, compare_type, value, &limit);
413 if (!limit || is_whole_rl(limit))
414 return 0;
415 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
416 return 0;
417 if (option_debug || local_debug)
418 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param, key, show_rl(passed), value);
419 return 1;
422 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
424 struct db_callback_info *db_info = _info;
425 struct range_list *var_rl = db_info->rl;
426 struct range_list *ret_range;
427 int type, param;
428 char *key, *value;
429 struct return_implies_callback *tmp;
430 struct stree *stree;
431 int return_id;
432 int comparison;
434 if (argc != 6)
435 return 0;
437 return_id = atoi(argv[0]);
438 type = atoi(argv[2]);
439 param = atoi(argv[3]);
440 key = argv[4];
441 value = argv[5];
443 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
444 set_return_state(db_info->var_expr, db_info);
445 stree = __pop_fake_cur_stree();
447 if (!db_info->cull)
448 merge_fake_stree(&db_info->stree, stree);
449 free_stree(&stree);
450 __push_fake_cur_stree();
451 db_info->cull = 0;
453 db_info->prev_return_id = return_id;
455 if (type == CULL_PATH)
456 db_info->cull = 1;
457 if (type == PARAM_LIMIT && impossible_limit(db_info->expr, param, key, value))
458 db_info->cull = 1;
459 if (db_info->cull)
460 return 0;
462 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
463 ret_range = cast_rl(get_type(db_info->expr), ret_range);
464 if (!ret_range)
465 ret_range = alloc_whole_rl(get_type(db_info->expr));
467 comparison = db_info->comparison;
468 if (db_info->left)
469 comparison = flip_comparison(comparison);
471 if (db_info->true_side) {
472 if (!possibly_true_rl(var_rl, comparison, ret_range))
473 return 0;
474 filter_by_comparison(&var_rl, comparison, ret_range);
475 } else {
476 if (!possibly_false_rl(var_rl, comparison, ret_range))
477 return 0;
478 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
481 handle_ret_equals_param(argv[1], ret_range, db_info->expr);
483 FOR_EACH_PTR(db_info->callbacks, tmp) {
484 if (tmp->type == type)
485 tmp->callback(db_info->expr, param, key, value);
486 } END_FOR_EACH_PTR(tmp);
488 store_return_state(db_info, alloc_estate_rl(clone_rl(var_rl)));
489 return 0;
492 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
494 struct stree *orig_states;
495 struct stree *stree;
496 struct stree *true_states;
497 struct stree *false_states;
498 struct sm_state *sm;
499 struct db_callback_info db_info = {};
500 struct expression *var_expr;
501 struct expression *call_expr;
502 struct range_list *rl;
503 int call_on_left;
505 orig_states = clone_stree(__get_cur_stree());
507 /* legacy cruft. need to fix call_implies_callbacks(). */
508 call_on_left = 1;
509 call_expr = left;
510 var_expr = right;
511 if (left->type != EXPR_CALL) {
512 call_on_left = 0;
513 call_expr = right;
514 var_expr = left;
517 get_absolute_rl(var_expr, &rl);
519 db_info.comparison = comparison;
520 db_info.expr = call_expr;
521 db_info.rl = rl;
522 db_info.left = call_on_left;
523 db_info.callbacks = db_return_states_list;
524 db_info.var_expr = var_expr;
526 call_return_states_before_hooks();
528 db_info.true_side = 1;
529 db_info.stree = NULL;
530 db_info.prev_return_id = -1;
531 __push_fake_cur_stree();
532 sql_select_return_states("return_id, return, type, parameter, key, value",
533 call_expr, db_compare_callback, &db_info);
534 set_return_state(db_info.var_expr, &db_info);
535 stree = __pop_fake_cur_stree();
536 if (!db_info.cull) {
537 set_return_state(db_info.var_expr, &db_info);
538 merge_fake_stree(&db_info.stree, stree);
540 free_stree(&stree);
541 true_states = db_info.stree;
543 db_info.true_side = 0;
544 db_info.stree = NULL;
545 db_info.prev_return_id = -1;
546 db_info.cull = 0;
547 __push_fake_cur_stree();
548 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
549 db_compare_callback, &db_info);
550 stree = __pop_fake_cur_stree();
551 if (!db_info.cull) {
552 set_return_state(db_info.var_expr, &db_info);
553 merge_fake_stree(&db_info.stree, stree);
555 free_stree(&stree);
556 false_states = db_info.stree;
559 * FIXME: This is the biggest hack ever. I need to think about
560 * how the fake stacks work a lot more. The problem here is that when
561 * you set the fake stack then it also sets the real stack. That would
562 * not be a problem in most cases because we normally will just
563 * overwrite both the true and false states. The problem comes where we
564 * only overwrite one side, say the true side. Then we use whatever is
565 * in the cur_stree for the other false side and the cur_stree has part
566 * of whatever data was set on the true state.
568 * It should be that setting the fake_cur_stree means that the real
569 * cur_stree stays the same. But for whatever reason that doesn't work.
570 * We also have many different ways of faking an stree. Sometimes we
571 * fake everything.... It is a mess.
575 FOR_EACH_SM(orig_states, sm) {
576 __set_sm(sm);
577 } END_FOR_EACH_SM(sm);
579 free_stree(&orig_states);
581 FOR_EACH_SM(true_states, sm) {
582 __set_true_false_sm(sm, NULL);
583 } END_FOR_EACH_SM(sm);
584 FOR_EACH_SM(false_states, sm) {
585 __set_true_false_sm(NULL, sm);
586 } END_FOR_EACH_SM(sm);
588 free_stree(&true_states);
589 free_stree(&false_states);
591 call_return_states_after_hooks();
593 FOR_EACH_SM(implied_true, sm) {
594 __set_true_false_sm(sm, NULL);
595 } END_FOR_EACH_SM(sm);
596 FOR_EACH_SM(implied_false, sm) {
597 __set_true_false_sm(NULL, sm);
598 } END_FOR_EACH_SM(sm);
601 void function_comparison(struct expression *left, int comparison, struct expression *right)
603 struct expression *var_expr;
604 struct expression *call_expr;
605 struct stree *implied_true = NULL;
606 struct stree *implied_false = NULL;
607 struct range_list *rl;
608 sval_t sval;
609 int call_on_left;
611 /* legacy cruft. need to fix call_implies_callbacks(). */
612 call_on_left = 1;
613 call_expr = left;
614 var_expr = right;
615 if (left->type != EXPR_CALL) {
616 call_on_left = 0;
617 call_expr = right;
618 var_expr = left;
621 get_absolute_rl(var_expr, &rl);
623 if (rl_to_sval(rl, &sval))
624 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
626 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
627 free_stree(&implied_true);
628 free_stree(&implied_false);
631 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
633 struct db_callback_info *db_info = _info;
634 struct range_list *ret_range;
635 int type, param;
636 char *key, *value;
637 struct return_implies_callback *tmp;
638 struct stree *stree;
639 int return_id;
641 if (argc != 6)
642 return 0;
644 return_id = atoi(argv[0]);
645 type = atoi(argv[2]);
646 param = atoi(argv[3]);
647 key = argv[4];
648 value = argv[5];
650 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
651 set_return_state(db_info->expr->left, db_info);
652 stree = __pop_fake_cur_stree();
653 if (!db_info->cull)
654 merge_fake_stree(&db_info->stree, stree);
655 free_stree(&stree);
656 __push_fake_cur_stree();
657 db_info->cull = 0;
659 db_info->prev_return_id = return_id;
661 if (type == CULL_PATH)
662 db_info->cull = 1;
663 if (type == PARAM_LIMIT && impossible_limit(db_info->expr, param, key, value))
664 db_info->cull = 1;
665 if (db_info->cull)
666 return 0;
668 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), argv[1], &ret_range);
669 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), argv[1]);
670 if (!ret_range)
671 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
672 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
674 FOR_EACH_PTR(db_return_states_list, tmp) {
675 if (tmp->type == type)
676 tmp->callback(db_info->expr, param, key, value);
677 } END_FOR_EACH_PTR(tmp);
678 ret_range = cast_rl(get_type(db_info->expr->left), ret_range);
679 store_return_state(db_info, alloc_estate_rl(ret_range));
681 return 0;
684 static int db_return_states_assign(struct expression *expr)
686 struct expression *right;
687 struct sm_state *sm;
688 struct stree *stree;
689 int handled = 0;
690 struct db_callback_info db_info = {};
692 right = strip_expr(expr->right);
694 db_info.prev_return_id = -1;
695 db_info.expr = expr;
696 db_info.stree = NULL;
698 call_return_states_before_hooks();
700 __push_fake_cur_stree();
701 sql_select_return_states("return_id, return, type, parameter, key, value",
702 right, db_assign_return_states_callback, &db_info);
703 if (option_debug) {
704 sm_msg("%s return_id %d return_ranges %s",
705 db_info.cull ? "culled" : "merging",
706 db_info.prev_return_id,
707 db_info.ret_state ? db_info.ret_state->name : "'<empty'");
709 set_return_state(db_info.expr->left, &db_info);
710 stree = __pop_fake_cur_stree();
711 if (!db_info.cull)
712 merge_fake_stree(&db_info.stree, stree);
713 free_stree(&stree);
715 FOR_EACH_SM(db_info.stree, sm) {
716 __set_sm(sm);
717 handled = 1;
718 } END_FOR_EACH_SM(sm);
720 free_stree(&db_info.stree);
721 call_return_states_after_hooks();
723 return handled;
726 static int handle_implied_return(struct expression *expr)
728 struct range_list *rl;
730 if (!get_implied_return(expr->right, &rl))
731 return 0;
732 rl = cast_rl(get_type(expr->left), rl);
733 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
734 return 1;
737 static void match_assign_call(struct expression *expr)
739 struct call_back_list *call_backs;
740 const char *fn;
741 struct expression *right;
742 int handled = 0;
743 struct range_list *rl;
745 if (expr->op != '=')
746 return;
748 right = strip_expr(expr->right);
749 if (right->fn->type != EXPR_SYMBOL || !right->fn->symbol) {
750 handled |= db_return_states_assign(expr);
751 if (!handled)
752 goto assigned_unknown;
753 return;
755 if (is_fake_call(right)) {
756 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
757 return;
760 fn = right->fn->symbol->ident->name;
763 * some of these conflict (they try to set smatch extra twice), so we
764 * call them in order from least important to most important.
767 call_backs = search_callback(func_hash, (char *)fn);
768 call_call_backs(call_backs, ASSIGN_CALL, fn, expr);
770 handled |= db_return_states_assign(expr);
771 handled |= assign_ranged_funcs(fn, expr, call_backs);
772 handled |= handle_implied_return(expr);
774 if (handled)
775 return;
777 assigned_unknown:
778 get_absolute_rl(expr->right, &rl);
779 rl = cast_rl(get_type(expr->left), rl);
780 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
783 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
785 struct db_callback_info *db_info = _info;
786 struct range_list *ret_range;
787 int type, param;
788 char *key, *value;
789 struct return_implies_callback *tmp;
790 struct stree *stree;
791 int return_id;
792 char buf[64];
794 if (argc != 6)
795 return 0;
797 return_id = atoi(argv[0]);
798 type = atoi(argv[2]);
799 param = atoi(argv[3]);
800 key = argv[4];
801 value = argv[5];
803 if (db_info->prev_return_id != -1 && return_id != db_info->prev_return_id) {
804 stree = __pop_fake_cur_stree();
805 if (!db_info->cull)
806 merge_fake_stree(&db_info->stree, stree);
807 free_stree(&stree);
808 __push_fake_cur_stree();
809 __unnullify_path();
810 db_info->cull = 0;
812 db_info->prev_return_id = return_id;
814 if (type == CULL_PATH)
815 db_info->cull = 1;
816 if (type == PARAM_LIMIT && impossible_limit(db_info->expr, param, key, value))
817 db_info->cull = 1;
818 if (db_info->cull)
819 return 0;
821 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
822 ret_range = cast_rl(get_type(db_info->expr), ret_range);
824 FOR_EACH_PTR(db_return_states_list, tmp) {
825 if (tmp->type == type)
826 tmp->callback(db_info->expr, param, key, value);
827 } END_FOR_EACH_PTR(tmp);
830 * We want to store the return values so that we can split the strees
831 * in smatch_db.c. This uses set_state() directly because it's not a
832 * real smatch_extra state.
834 snprintf(buf, sizeof(buf), "return %p", db_info->expr);
835 set_state(SMATCH_EXTRA, buf, NULL, alloc_estate_rl(ret_range));
837 return 0;
840 static void db_return_states(struct expression *expr)
842 struct sm_state *sm;
843 struct stree *stree;
844 struct db_callback_info db_info = {};
846 if (!__get_cur_stree()) /* no return functions */
847 return;
849 db_info.prev_return_id = -1;
850 db_info.expr = expr;
851 db_info.stree = NULL;
853 call_return_states_before_hooks();
855 __push_fake_cur_stree();
856 __unnullify_path();
857 sql_select_return_states("return_id, return, type, parameter, key, value",
858 expr, db_return_states_callback, &db_info);
859 stree = __pop_fake_cur_stree();
860 if (!db_info.cull)
861 merge_fake_stree(&db_info.stree, stree);
862 free_stree(&stree);
864 FOR_EACH_SM(db_info.stree, sm) {
865 __set_sm(sm);
866 } END_FOR_EACH_SM(sm);
868 free_stree(&db_info.stree);
869 call_return_states_after_hooks();
872 static void db_return_states_call(struct expression *expr)
874 if (is_assigned_call(expr))
875 return;
876 db_return_states(expr);
879 static void match_function_call(struct expression *expr)
881 struct call_back_list *call_backs;
883 if (expr->fn->type == EXPR_SYMBOL && expr->fn->symbol) {
884 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
885 if (call_backs)
886 call_call_backs(call_backs, REGULAR_CALL,
887 expr->fn->symbol->ident->name, expr);
889 db_return_states_call(expr);
892 static void match_macro_assign(struct expression *expr)
894 struct call_back_list *call_backs;
895 const char *macro;
896 struct expression *right;
898 right = strip_expr(expr->right);
899 macro = get_macro_name(right->pos);
900 call_backs = search_callback(func_hash, (char *)macro);
901 if (!call_backs)
902 return;
903 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
904 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
907 int get_implied_return(struct expression *expr, struct range_list **rl)
909 struct call_back_list *call_backs;
910 struct fcall_back *tmp;
911 int handled = 0;
912 char *fn;
914 *rl = NULL;
916 expr = strip_expr(expr);
917 fn = expr_to_var(expr->fn);
918 if (!fn)
919 goto out;
921 call_backs = search_callback(func_hash, fn);
923 FOR_EACH_PTR(call_backs, tmp) {
924 if (tmp->type == IMPLIED_RETURN) {
925 (tmp->u.implied_return)(expr, tmp->info, rl);
926 handled = 1;
928 } END_FOR_EACH_PTR(tmp);
930 out:
931 free_string(fn);
932 return handled;
935 void create_function_hook_hash(void)
937 func_hash = create_function_hashtable(5000);
940 void register_function_hooks(int id)
942 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
943 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
944 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);