function_hooks: function comparisons can imply a parameter value
[smatch.git] / smatch_function_hooks.c
blob3a396e1e92f952825b86460a67251f790c3fa64d
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 int call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left)
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;
296 struct sm_state *sm;
298 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
299 return 0;
300 fn = expr->fn->symbol->ident->name;
301 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
302 if (!call_backs)
303 return 0;
304 value_range = alloc_range(sval, sval);
306 /* set true states */
307 __push_fake_cur_stree();
308 FOR_EACH_PTR(call_backs, tmp) {
309 if (tmp->type != RANGED_CALL)
310 continue;
311 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
312 continue;
313 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
314 } END_FOR_EACH_PTR(tmp);
315 tmp_stree = __pop_fake_cur_stree();
316 merge_fake_stree(&true_states, tmp_stree);
317 free_stree(&tmp_stree);
319 /* set false states */
320 __push_fake_cur_stree();
321 FOR_EACH_PTR(call_backs, tmp) {
322 if (tmp->type != RANGED_CALL)
323 continue;
324 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
325 continue;
326 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
327 } END_FOR_EACH_PTR(tmp);
328 tmp_stree = __pop_fake_cur_stree();
329 merge_fake_stree(&false_states, tmp_stree);
330 free_stree(&tmp_stree);
332 FOR_EACH_SM(true_states, sm) {
333 __set_true_false_sm(sm, NULL);
334 } END_FOR_EACH_SM(sm);
335 FOR_EACH_SM(false_states, sm) {
336 __set_true_false_sm(NULL, sm);
337 } END_FOR_EACH_SM(sm);
339 free_stree(&true_states);
340 free_stree(&false_states);
341 return 1;
344 struct db_callback_info {
345 int true_side;
346 int comparison;
347 struct expression *expr;
348 struct range_list *rl;
349 int left;
350 struct stree *stree;
351 struct db_implies_list *callbacks;
353 static int prev_return_id;
355 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
357 char *str;
358 long long param;
359 struct expression *arg;
360 struct range_list *orig;
362 str = strstr(ret_string, "==$");
363 if (!str)
364 return;
365 str += 3;
366 param = strtoll(str, NULL, 10);
367 arg = get_argument_from_call_expr(call->args, param);
368 if (!arg)
369 return;
370 get_absolute_rl(arg, &orig);
371 rl = rl_intersection(orig, rl);
372 if (!rl)
373 return;
374 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
377 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
379 struct db_callback_info *db_info = _info;
380 struct range_list *ret_range;
381 int type, param;
382 char *key, *value;
383 struct return_implies_callback *tmp;
384 struct stree *stree;
385 int return_id;
386 int comparison;
388 if (argc != 6)
389 return 0;
391 return_id = atoi(argv[0]);
392 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
393 ret_range = cast_rl(get_type(db_info->expr), ret_range);
394 if (!ret_range)
395 ret_range = alloc_whole_rl(get_type(db_info->expr));
396 type = atoi(argv[2]);
397 param = atoi(argv[3]);
398 key = argv[4];
399 value = argv[5];
401 if (prev_return_id != -1 && return_id != prev_return_id) {
402 stree = __pop_fake_cur_stree();
403 merge_fake_stree(&db_info->stree, stree);
404 free_stree(&stree);
405 __push_fake_cur_stree();
407 prev_return_id = return_id;
409 comparison = db_info->comparison;
410 if (!db_info->left)
411 comparison = flip_comparison(comparison);
413 if (db_info->true_side) {
414 if (!possibly_true_rl(ret_range, comparison, db_info->rl))
415 return 0;
416 filter_by_comparison(&ret_range, comparison, db_info->rl);
417 } else {
418 if (!possibly_false_rl(ret_range, comparison, db_info->rl))
419 return 0;
420 filter_by_comparison(&ret_range, negate_comparison(comparison), db_info->rl);
423 handle_ret_equals_param(argv[1], ret_range, db_info->expr);
425 FOR_EACH_PTR(db_info->callbacks, tmp) {
426 if (tmp->type == type)
427 tmp->callback(db_info->expr, param, key, value);
428 } END_FOR_EACH_PTR(tmp);
429 return 0;
432 void compare_db_return_states_callbacks(int comparison, struct expression *expr, sval_t sval, int left)
434 struct stree *stree;
435 struct stree *true_states;
436 struct stree *false_states;
437 struct sm_state *sm;
438 struct db_callback_info db_info;
440 db_info.comparison = comparison;
441 db_info.expr = expr;
442 db_info.rl = alloc_rl(sval, sval);
443 db_info.left = left;
444 db_info.callbacks = db_return_states_list;
446 call_return_states_before_hooks();
448 db_info.true_side = 1;
449 db_info.stree = NULL;
450 prev_return_id = -1;
451 __push_fake_cur_stree();
452 sql_select_return_states("return_id, return, type, parameter, key, value", expr,
453 db_compare_callback, &db_info);
454 stree = __pop_fake_cur_stree();
455 merge_fake_stree(&db_info.stree, stree);
456 free_stree(&stree);
457 true_states = db_info.stree;
459 db_info.true_side = 0;
460 db_info.stree = NULL;
461 prev_return_id = -1;
462 __push_fake_cur_stree();
463 sql_select_return_states("return_id, return, type, parameter, key, value", expr,
464 db_compare_callback, &db_info);
465 stree = __pop_fake_cur_stree();
466 merge_fake_stree(&db_info.stree, stree);
467 free_stree(&stree);
468 false_states = db_info.stree;
470 FOR_EACH_SM(true_states, sm) {
471 __set_true_false_sm(sm, NULL);
472 } END_FOR_EACH_SM(sm);
473 FOR_EACH_SM(false_states, sm) {
474 __set_true_false_sm(NULL, sm);
475 } END_FOR_EACH_SM(sm);
477 call_return_states_after_hooks();
479 free_stree(&true_states);
480 free_stree(&false_states);
483 void function_comparison(int comparison, struct expression *expr, sval_t sval, int left)
485 if (call_implies_callbacks(comparison, expr, sval, left))
486 return;
487 compare_db_return_states_callbacks(comparison, expr, sval, left);
490 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
492 struct db_callback_info *db_info = _info;
493 struct range_list *ret_range;
494 int type, param;
495 char *key, *value;
496 struct return_implies_callback *tmp;
497 struct stree *stree;
498 int return_id;
500 if (argc != 6)
501 return 0;
503 return_id = atoi(argv[0]);
504 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), argv[1], &ret_range);
505 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), argv[1]);
506 if (!ret_range)
507 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
508 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
509 type = atoi(argv[2]);
510 param = atoi(argv[3]);
511 key = argv[4];
512 value = argv[5];
514 if (prev_return_id != -1 && return_id != prev_return_id) {
515 stree = __pop_fake_cur_stree();
516 merge_fake_stree(&db_info->stree, stree);
517 free_stree(&stree);
518 __push_fake_cur_stree();
520 prev_return_id = return_id;
522 FOR_EACH_PTR(db_return_states_list, tmp) {
523 if (tmp->type == type)
524 tmp->callback(db_info->expr, param, key, value);
525 } END_FOR_EACH_PTR(tmp);
526 ret_range = cast_rl(get_type(db_info->expr->left), ret_range);
527 set_extra_expr_mod(db_info->expr->left, alloc_estate_rl(ret_range));
529 return 0;
532 static int db_return_states_assign(struct expression *expr)
534 struct expression *right;
535 struct sm_state *sm;
536 struct stree *stree;
537 int handled = 0;
538 struct db_callback_info db_info;
540 right = strip_expr(expr->right);
542 prev_return_id = -1;
543 db_info.expr = expr;
544 db_info.stree = NULL;
546 call_return_states_before_hooks();
548 __push_fake_cur_stree();
549 sql_select_return_states("return_id, return, type, parameter, key, value",
550 right, db_assign_return_states_callback, &db_info);
551 stree = __pop_fake_cur_stree();
552 merge_fake_stree(&db_info.stree, stree);
553 free_stree(&stree);
555 FOR_EACH_SM(db_info.stree, sm) {
556 __set_sm(sm);
557 handled = 1;
558 } END_FOR_EACH_SM(sm);
560 free_stree(&db_info.stree);
561 call_return_states_after_hooks();
563 return handled;
566 static int handle_implied_return(struct expression *expr)
568 struct range_list *rl;
570 if (!get_implied_return(expr->right, &rl))
571 return 0;
572 rl = cast_rl(get_type(expr->left), rl);
573 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
574 return 1;
577 static void match_assign_call(struct expression *expr)
579 struct call_back_list *call_backs;
580 const char *fn;
581 struct expression *right;
582 int handled = 0;
583 struct range_list *rl;
585 if (expr->op != '=')
586 return;
588 right = strip_expr(expr->right);
589 if (right->fn->type != EXPR_SYMBOL || !right->fn->symbol) {
590 handled |= db_return_states_assign(expr);
591 if (!handled)
592 goto assigned_unknown;
593 return;
595 if (is_fake_call(right)) {
596 if (!get_state_expr(SMATCH_EXTRA, expr->left))
597 return;
598 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
599 return;
602 fn = right->fn->symbol->ident->name;
605 * some of these conflict (they try to set smatch extra twice), so we
606 * call them in order from least important to most important.
609 call_backs = search_callback(func_hash, (char *)fn);
610 call_call_backs(call_backs, ASSIGN_CALL, fn, expr);
612 handled |= db_return_states_assign(expr);
613 handled |= assign_ranged_funcs(fn, expr, call_backs);
614 handled |= handle_implied_return(expr);
616 if (handled)
617 return;
619 assigned_unknown:
620 get_absolute_rl(expr->right, &rl);
621 rl = cast_rl(get_type(expr->left), rl);
622 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
625 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
627 struct db_callback_info *db_info = _info;
628 struct range_list *ret_range;
629 int type, param;
630 char *key, *value;
631 struct return_implies_callback *tmp;
632 struct stree *stree;
633 int return_id;
635 if (argc != 6)
636 return 0;
638 return_id = atoi(argv[0]);
639 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), argv[1], &ret_range);
640 ret_range = cast_rl(get_type(db_info->expr), ret_range);
641 type = atoi(argv[2]);
642 param = atoi(argv[3]);
643 key = argv[4];
644 value = argv[5];
646 if (prev_return_id != -1 && return_id != prev_return_id) {
647 stree = __pop_fake_cur_stree();
648 merge_fake_stree(&db_info->stree, stree);
649 free_stree(&stree);
650 __push_fake_cur_stree();
651 __unnullify_path();
653 prev_return_id = return_id;
655 FOR_EACH_PTR(db_return_states_list, tmp) {
656 if (tmp->type == type)
657 tmp->callback(db_info->expr, param, key, value);
658 } END_FOR_EACH_PTR(tmp);
660 return 0;
663 static void db_return_states(struct expression *expr)
665 struct sm_state *sm;
666 struct stree *stree;
667 struct db_callback_info db_info;
669 if (!__get_cur_stree()) /* no return functions */
670 return;
672 prev_return_id = -1;
673 db_info.expr = expr;
674 db_info.stree = NULL;
676 call_return_states_before_hooks();
678 __push_fake_cur_stree();
679 __unnullify_path();
680 sql_select_return_states("return_id, return, type, parameter, key, value",
681 expr, db_return_states_callback, &db_info);
682 stree = __pop_fake_cur_stree();
683 merge_fake_stree(&db_info.stree, stree);
684 free_stree(&stree);
686 FOR_EACH_SM(db_info.stree, sm) {
687 __set_sm(sm);
688 } END_FOR_EACH_SM(sm);
690 free_stree(&db_info.stree);
691 call_return_states_after_hooks();
694 static int is_assigned_call(struct expression *expr)
696 struct expression *tmp;
698 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
699 if (tmp->type == EXPR_ASSIGNMENT && strip_expr(tmp->right) == expr)
700 return 1;
701 if (tmp->pos.line < expr->pos.line)
702 return 0;
703 } END_FOR_EACH_PTR_REVERSE(tmp);
704 return 0;
707 static void db_return_states_call(struct expression *expr)
709 if (is_assigned_call(expr))
710 return;
711 db_return_states(expr);
714 static void match_function_call(struct expression *expr)
716 struct call_back_list *call_backs;
718 if (expr->fn->type == EXPR_SYMBOL && expr->fn->symbol) {
719 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
720 if (call_backs)
721 call_call_backs(call_backs, REGULAR_CALL,
722 expr->fn->symbol->ident->name, expr);
724 db_return_states_call(expr);
727 static void match_macro_assign(struct expression *expr)
729 struct call_back_list *call_backs;
730 const char *macro;
731 struct expression *right;
733 right = strip_expr(expr->right);
734 macro = get_macro_name(right->pos);
735 call_backs = search_callback(func_hash, (char *)macro);
736 if (!call_backs)
737 return;
738 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
739 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
742 int get_implied_return(struct expression *expr, struct range_list **rl)
744 struct call_back_list *call_backs;
745 struct fcall_back *tmp;
746 int handled = 0;
747 char *fn;
749 *rl = NULL;
751 expr = strip_expr(expr);
752 fn = expr_to_var(expr->fn);
753 if (!fn)
754 goto out;
756 call_backs = search_callback(func_hash, fn);
758 FOR_EACH_PTR(call_backs, tmp) {
759 if (tmp->type == IMPLIED_RETURN) {
760 (tmp->u.implied_return)(expr, tmp->info, rl);
761 handled = 1;
763 } END_FOR_EACH_PTR(tmp);
765 out:
766 free_string(fn);
767 return handled;
770 void create_function_hook_hash(void)
772 func_hash = create_function_hashtable(5000);
775 void register_function_hooks(int id)
777 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
778 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
779 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);