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.
32 #include "smatch_slist.h"
33 #include "smatch_extra.h"
34 #include "smatch_function_hashtable.h"
38 struct data_range
*range
;
41 implication_hook
*ranged
;
42 implied_return_hook
*implied_return
;
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
56 #define IMPLIED_RETURN 3
57 #define MACRO_ASSIGN 4
58 #define MACRO_ASSIGN_EXTRA 5
60 struct return_implies_callback
{
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
,
76 struct fcall_back
*cb
;
78 cb
= __alloc_fcall_back(0);
80 cb
->u
.call_back
= call_back
;
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
,
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
,
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
,
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
,
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);
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
*));
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
*));
160 add_ptr_list(&return_states_after
, p
);
163 static void call_return_states_before_hooks(void)
167 FOR_EACH_PTR(return_states_before
, fn
) {
169 } END_FOR_EACH_PTR(fn
);
172 static void call_return_states_after_hooks(void)
176 FOR_EACH_PTR(return_states_after
, 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
;
187 FOR_EACH_PTR(list
, tmp
) {
188 if (tmp
->type
== type
) {
189 (tmp
->u
.call_back
)(fn
, expr
, tmp
->info
);
192 } END_FOR_EACH_PTR(tmp
);
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
)
217 if (ranges_equiv(tmp
->range
, drange
))
218 add_ptr_list(&ret
, tmp
);
219 } END_FOR_EACH_PTR(tmp
);
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
))
230 } END_FOR_EACH_PTR(tmp
);
234 static int assign_ranged_funcs(const char *fn
, struct expression
*expr
,
235 struct call_back_list
*call_backs
)
237 struct fcall_back
*tmp
;
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
;
251 var_name
= expr_to_var_sym(expr
->left
, &sym
);
252 if (!var_name
|| !sym
)
255 FOR_EACH_PTR(call_backs
, tmp
) {
256 if (tmp
->type
!= RANGED_CALL
)
259 if (in_list_exact_sval(handled_ranges
, tmp
->range
))
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
);
275 } END_FOR_EACH_PTR(tmp
);
277 FOR_EACH_SM(final_states
, sm
) {
279 } END_FOR_EACH_SM(sm
);
281 free_stree(&final_states
);
283 free_string(var_name
);
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
;
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
)
301 fn
= expr
->fn
->symbol
->ident
->name
;
302 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
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
)
312 if (!true_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
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
)
325 if (!false_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
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
{
340 struct expression
*expr
;
341 struct range_list
*rl
;
344 struct db_implies_list
*callbacks
;
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
)
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
)
369 struct expression
*arg
;
370 struct range_list
*orig
;
372 str
= strstr(ret_string
, "==$");
376 param
= strtoll(str
, NULL
, 10);
377 arg
= get_argument_from_call_expr(call
->args
, param
);
380 get_absolute_rl(arg
, &orig
);
381 rl
= rl_intersection(orig
, rl
);
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)
397 while (expr
->type
== EXPR_ASSIGNMENT
)
398 expr
= strip_expr(expr
->right
);
399 if (expr
->type
!= EXPR_CALL
)
402 arg
= get_argument_from_call_expr(expr
->args
, param
);
405 if (!get_implied_rl(arg
, &passed
))
407 if (!passed
|| is_whole_rl(passed
))
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
))
415 if (possibly_true_rl(passed
, SPECIAL_EQUAL
, limit
))
417 if (option_debug
|| local_debug
)
418 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param
, key
, show_rl(passed
), value
);
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
;
429 struct return_implies_callback
*tmp
;
437 return_id
= atoi(argv
[0]);
438 type
= atoi(argv
[2]);
439 param
= atoi(argv
[3]);
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();
448 merge_fake_stree(&db_info
->stree
, stree
);
450 __push_fake_cur_stree();
453 db_info
->prev_return_id
= return_id
;
455 if (type
== CULL_PATH
)
457 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
))
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
);
465 ret_range
= alloc_whole_rl(get_type(db_info
->expr
));
467 comparison
= db_info
->comparison
;
469 comparison
= flip_comparison(comparison
);
471 if (db_info
->true_side
) {
472 if (!possibly_true_rl(var_rl
, comparison
, ret_range
))
474 filter_by_comparison(&var_rl
, comparison
, ret_range
);
475 filter_by_comparison(&ret_range
, flip_comparison(comparison
), var_rl
);
477 if (!possibly_false_rl(var_rl
, comparison
, ret_range
))
479 filter_by_comparison(&var_rl
, negate_comparison(comparison
), ret_range
);
480 filter_by_comparison(&ret_range
, flip_comparison(negate_comparison(comparison
)), var_rl
);
483 handle_ret_equals_param(argv
[1], ret_range
, db_info
->expr
);
485 FOR_EACH_PTR(db_info
->callbacks
, tmp
) {
486 if (tmp
->type
== type
)
487 tmp
->callback(db_info
->expr
, param
, key
, value
);
488 } END_FOR_EACH_PTR(tmp
);
490 store_return_state(db_info
, alloc_estate_rl(clone_rl(var_rl
)));
494 static void compare_db_return_states_callbacks(struct expression
*left
, int comparison
, struct expression
*right
, struct stree
*implied_true
, struct stree
*implied_false
)
496 struct stree
*orig_states
;
498 struct stree
*true_states
;
499 struct stree
*false_states
;
501 struct db_callback_info db_info
= {};
502 struct expression
*var_expr
;
503 struct expression
*call_expr
;
504 struct range_list
*rl
;
507 orig_states
= clone_stree(__get_cur_stree());
509 /* legacy cruft. need to fix call_implies_callbacks(). */
513 if (left
->type
!= EXPR_CALL
) {
519 get_absolute_rl(var_expr
, &rl
);
521 db_info
.comparison
= comparison
;
522 db_info
.expr
= call_expr
;
524 db_info
.left
= call_on_left
;
525 db_info
.callbacks
= db_return_states_list
;
526 db_info
.var_expr
= var_expr
;
528 call_return_states_before_hooks();
530 db_info
.true_side
= 1;
531 db_info
.stree
= NULL
;
532 db_info
.prev_return_id
= -1;
533 __push_fake_cur_stree();
534 sql_select_return_states("return_id, return, type, parameter, key, value",
535 call_expr
, db_compare_callback
, &db_info
);
536 set_return_state(db_info
.var_expr
, &db_info
);
537 stree
= __pop_fake_cur_stree();
539 set_return_state(db_info
.var_expr
, &db_info
);
540 merge_fake_stree(&db_info
.stree
, stree
);
543 true_states
= db_info
.stree
;
545 db_info
.true_side
= 0;
546 db_info
.stree
= NULL
;
547 db_info
.prev_return_id
= -1;
549 __push_fake_cur_stree();
550 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr
,
551 db_compare_callback
, &db_info
);
552 stree
= __pop_fake_cur_stree();
554 set_return_state(db_info
.var_expr
, &db_info
);
555 merge_fake_stree(&db_info
.stree
, stree
);
558 false_states
= db_info
.stree
;
561 * FIXME: This is the biggest hack ever. I need to think about
562 * how the fake stacks work a lot more. The problem here is that when
563 * you set the fake stack then it also sets the real stack. That would
564 * not be a problem in most cases because we normally will just
565 * overwrite both the true and false states. The problem comes where we
566 * only overwrite one side, say the true side. Then we use whatever is
567 * in the cur_stree for the other false side and the cur_stree has part
568 * of whatever data was set on the true state.
570 * It should be that setting the fake_cur_stree means that the real
571 * cur_stree stays the same. But for whatever reason that doesn't work.
572 * We also have many different ways of faking an stree. Sometimes we
573 * fake everything.... It is a mess.
577 FOR_EACH_SM(orig_states
, sm
) {
578 __use_orig_if_not_set(sm
);
579 } END_FOR_EACH_SM(sm
);
581 free_stree(&orig_states
);
583 FOR_EACH_SM(true_states
, sm
) {
584 __set_true_false_sm(sm
, NULL
);
585 } END_FOR_EACH_SM(sm
);
586 FOR_EACH_SM(false_states
, sm
) {
587 __set_true_false_sm(NULL
, sm
);
588 } END_FOR_EACH_SM(sm
);
590 free_stree(&true_states
);
591 free_stree(&false_states
);
593 call_return_states_after_hooks();
595 FOR_EACH_SM(implied_true
, sm
) {
596 __set_true_false_sm(sm
, NULL
);
597 } END_FOR_EACH_SM(sm
);
598 FOR_EACH_SM(implied_false
, sm
) {
599 __set_true_false_sm(NULL
, sm
);
600 } END_FOR_EACH_SM(sm
);
603 void function_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
605 struct expression
*var_expr
;
606 struct expression
*call_expr
;
607 struct stree
*implied_true
= NULL
;
608 struct stree
*implied_false
= NULL
;
609 struct range_list
*rl
;
613 /* legacy cruft. need to fix call_implies_callbacks(). */
617 if (left
->type
!= EXPR_CALL
) {
623 get_absolute_rl(var_expr
, &rl
);
625 if (rl_to_sval(rl
, &sval
))
626 call_implies_callbacks(comparison
, call_expr
, sval
, call_on_left
, &implied_true
, &implied_false
);
628 compare_db_return_states_callbacks(left
, comparison
, right
, implied_true
, implied_false
);
629 free_stree(&implied_true
);
630 free_stree(&implied_false
);
633 static int db_assign_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
635 struct db_callback_info
*db_info
= _info
;
636 struct range_list
*ret_range
;
639 struct return_implies_callback
*tmp
;
646 return_id
= atoi(argv
[0]);
647 type
= atoi(argv
[2]);
648 param
= atoi(argv
[3]);
652 if (db_info
->prev_return_id
!= -1 && return_id
!= db_info
->prev_return_id
) {
653 set_return_state(db_info
->expr
->left
, db_info
);
654 stree
= __pop_fake_cur_stree();
656 merge_fake_stree(&db_info
->stree
, stree
);
658 __push_fake_cur_stree();
661 db_info
->prev_return_id
= return_id
;
663 if (type
== CULL_PATH
)
665 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
))
670 call_results_to_rl(db_info
->expr
->right
, get_type(strip_expr(db_info
->expr
->right
)), argv
[1], &ret_range
);
671 __add_comparison_info(db_info
->expr
->left
, strip_expr(db_info
->expr
->right
), argv
[1]);
673 ret_range
= alloc_whole_rl(get_type(strip_expr(db_info
->expr
->right
)));
674 ret_range
= cast_rl(get_type(db_info
->expr
->right
), ret_range
);
676 FOR_EACH_PTR(db_return_states_list
, tmp
) {
677 if (tmp
->type
== type
)
678 tmp
->callback(db_info
->expr
, param
, key
, value
);
679 } END_FOR_EACH_PTR(tmp
);
680 ret_range
= cast_rl(get_type(db_info
->expr
->left
), ret_range
);
681 store_return_state(db_info
, alloc_estate_rl(ret_range
));
686 static int db_return_states_assign(struct expression
*expr
)
688 struct expression
*right
;
692 struct db_callback_info db_info
= {};
694 right
= strip_expr(expr
->right
);
696 db_info
.prev_return_id
= -1;
698 db_info
.stree
= NULL
;
700 call_return_states_before_hooks();
702 __push_fake_cur_stree();
703 sql_select_return_states("return_id, return, type, parameter, key, value",
704 right
, db_assign_return_states_callback
, &db_info
);
706 sm_msg("%s return_id %d return_ranges %s",
707 db_info
.cull
? "culled" : "merging",
708 db_info
.prev_return_id
,
709 db_info
.ret_state
? db_info
.ret_state
->name
: "'<empty'");
711 set_return_state(db_info
.expr
->left
, &db_info
);
712 stree
= __pop_fake_cur_stree();
714 merge_fake_stree(&db_info
.stree
, stree
);
717 FOR_EACH_SM(db_info
.stree
, sm
) {
720 } END_FOR_EACH_SM(sm
);
722 free_stree(&db_info
.stree
);
723 call_return_states_after_hooks();
728 static int handle_implied_return(struct expression
*expr
)
730 struct range_list
*rl
;
732 if (!get_implied_return(expr
->right
, &rl
))
734 rl
= cast_rl(get_type(expr
->left
), rl
);
735 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
739 static void match_assign_call(struct expression
*expr
)
741 struct call_back_list
*call_backs
;
743 struct expression
*right
;
745 struct range_list
*rl
;
750 right
= strip_expr(expr
->right
);
751 if (right
->fn
->type
!= EXPR_SYMBOL
|| !right
->fn
->symbol
) {
752 handled
|= db_return_states_assign(expr
);
754 goto assigned_unknown
;
757 if (is_fake_call(right
)) {
758 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
762 fn
= right
->fn
->symbol
->ident
->name
;
765 * some of these conflict (they try to set smatch extra twice), so we
766 * call them in order from least important to most important.
769 call_backs
= search_callback(func_hash
, (char *)fn
);
770 call_call_backs(call_backs
, ASSIGN_CALL
, fn
, expr
);
772 handled
|= db_return_states_assign(expr
);
773 handled
|= assign_ranged_funcs(fn
, expr
, call_backs
);
774 handled
|= handle_implied_return(expr
);
780 get_absolute_rl(expr
->right
, &rl
);
781 rl
= cast_rl(get_type(expr
->left
), rl
);
782 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
785 static int db_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
787 struct db_callback_info
*db_info
= _info
;
788 struct range_list
*ret_range
;
791 struct return_implies_callback
*tmp
;
799 return_id
= atoi(argv
[0]);
800 type
= atoi(argv
[2]);
801 param
= atoi(argv
[3]);
805 if (db_info
->prev_return_id
!= -1 && return_id
!= db_info
->prev_return_id
) {
806 stree
= __pop_fake_cur_stree();
808 merge_fake_stree(&db_info
->stree
, stree
);
810 __push_fake_cur_stree();
814 db_info
->prev_return_id
= return_id
;
816 if (type
== CULL_PATH
)
818 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
))
823 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), argv
[1], &ret_range
);
824 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
826 FOR_EACH_PTR(db_return_states_list
, tmp
) {
827 if (tmp
->type
== type
)
828 tmp
->callback(db_info
->expr
, param
, key
, value
);
829 } END_FOR_EACH_PTR(tmp
);
832 * We want to store the return values so that we can split the strees
833 * in smatch_db.c. This uses set_state() directly because it's not a
834 * real smatch_extra state.
836 snprintf(buf
, sizeof(buf
), "return %p", db_info
->expr
);
837 set_state(SMATCH_EXTRA
, buf
, NULL
, alloc_estate_rl(ret_range
));
842 static void db_return_states(struct expression
*expr
)
846 struct db_callback_info db_info
= {};
848 if (!__get_cur_stree()) /* no return functions */
851 db_info
.prev_return_id
= -1;
853 db_info
.stree
= NULL
;
855 call_return_states_before_hooks();
857 __push_fake_cur_stree();
859 sql_select_return_states("return_id, return, type, parameter, key, value",
860 expr
, db_return_states_callback
, &db_info
);
861 stree
= __pop_fake_cur_stree();
863 merge_fake_stree(&db_info
.stree
, stree
);
866 FOR_EACH_SM(db_info
.stree
, sm
) {
868 } END_FOR_EACH_SM(sm
);
870 free_stree(&db_info
.stree
);
871 call_return_states_after_hooks();
874 static void db_return_states_call(struct expression
*expr
)
876 if (is_assigned_call(expr
))
878 db_return_states(expr
);
881 static void match_function_call(struct expression
*expr
)
883 struct call_back_list
*call_backs
;
885 if (expr
->fn
->type
== EXPR_SYMBOL
&& expr
->fn
->symbol
) {
886 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
888 call_call_backs(call_backs
, REGULAR_CALL
,
889 expr
->fn
->symbol
->ident
->name
, expr
);
891 db_return_states_call(expr
);
894 static void match_macro_assign(struct expression
*expr
)
896 struct call_back_list
*call_backs
;
898 struct expression
*right
;
900 right
= strip_expr(expr
->right
);
901 macro
= get_macro_name(right
->pos
);
902 call_backs
= search_callback(func_hash
, (char *)macro
);
905 call_call_backs(call_backs
, MACRO_ASSIGN
, macro
, expr
);
906 call_call_backs(call_backs
, MACRO_ASSIGN_EXTRA
, macro
, expr
);
909 int get_implied_return(struct expression
*expr
, struct range_list
**rl
)
911 struct call_back_list
*call_backs
;
912 struct fcall_back
*tmp
;
918 expr
= strip_expr(expr
);
919 fn
= expr_to_var(expr
->fn
);
923 call_backs
= search_callback(func_hash
, fn
);
925 FOR_EACH_PTR(call_backs
, tmp
) {
926 if (tmp
->type
== IMPLIED_RETURN
) {
927 (tmp
->u
.implied_return
)(expr
, tmp
->info
, rl
);
930 } END_FOR_EACH_PTR(tmp
);
937 void create_function_hook_hash(void)
939 func_hash
= create_function_hashtable(5000);
942 void register_function_hooks(int id
)
944 add_hook(&match_function_call
, CALL_HOOK_AFTER_INLINE
);
945 add_hook(&match_assign_call
, CALL_ASSIGNMENT_HOOK
);
946 add_hook(&match_macro_assign
, MACRO_ASSIGNMENT_HOOK
);