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(struct expression
*expr
)
176 FOR_EACH_PTR(return_states_after
, fn
) {
178 } END_FOR_EACH_PTR(fn
);
179 __pass_to_client(expr
, FUNCTION_CALL_HOOK_AFTER_DB
);
182 static int call_call_backs(struct call_back_list
*list
, int type
,
183 const char *fn
, struct expression
*expr
)
185 struct fcall_back
*tmp
;
188 FOR_EACH_PTR(list
, tmp
) {
189 if (tmp
->type
== type
) {
190 (tmp
->u
.call_back
)(fn
, expr
, tmp
->info
);
193 } END_FOR_EACH_PTR(tmp
);
198 static void call_ranged_call_backs(struct call_back_list
*list
,
199 const char *fn
, struct expression
*call_expr
,
200 struct expression
*assign_expr
)
202 struct fcall_back
*tmp
;
204 FOR_EACH_PTR(list
, tmp
) {
205 (tmp
->u
.ranged
)(fn
, call_expr
, assign_expr
, tmp
->info
);
206 } END_FOR_EACH_PTR(tmp
);
209 static struct call_back_list
*get_same_ranged_call_backs(struct call_back_list
*list
,
210 struct data_range
*drange
)
212 struct call_back_list
*ret
= NULL
;
213 struct fcall_back
*tmp
;
215 FOR_EACH_PTR(list
, tmp
) {
216 if (tmp
->type
!= RANGED_CALL
)
218 if (ranges_equiv(tmp
->range
, drange
))
219 add_ptr_list(&ret
, tmp
);
220 } END_FOR_EACH_PTR(tmp
);
224 static int in_list_exact_sval(struct range_list
*list
, struct data_range
*drange
)
226 struct data_range
*tmp
;
228 FOR_EACH_PTR(list
, tmp
) {
229 if (ranges_equiv(tmp
, drange
))
231 } END_FOR_EACH_PTR(tmp
);
235 static int assign_ranged_funcs(const char *fn
, struct expression
*expr
,
236 struct call_back_list
*call_backs
)
238 struct fcall_back
*tmp
;
242 struct smatch_state
*estate
;
243 struct stree
*tmp_stree
;
244 struct stree
*final_states
= NULL
;
245 struct range_list
*handled_ranges
= NULL
;
246 struct call_back_list
*same_range_call_backs
= NULL
;
252 var_name
= expr_to_var_sym(expr
->left
, &sym
);
253 if (!var_name
|| !sym
)
256 FOR_EACH_PTR(call_backs
, tmp
) {
257 if (tmp
->type
!= RANGED_CALL
)
260 if (in_list_exact_sval(handled_ranges
, tmp
->range
))
262 __push_fake_cur_stree();
263 tack_on(&handled_ranges
, tmp
->range
);
265 same_range_call_backs
= get_same_ranged_call_backs(call_backs
, tmp
->range
);
266 call_ranged_call_backs(same_range_call_backs
, fn
, expr
->right
, expr
);
267 __free_ptr_list((struct ptr_list
**)&same_range_call_backs
);
269 estate
= alloc_estate_range(tmp
->range
->min
, tmp
->range
->max
);
270 set_extra_mod(var_name
, sym
, estate
);
272 tmp_stree
= __pop_fake_cur_stree();
273 merge_fake_stree(&final_states
, tmp_stree
);
274 free_stree(&tmp_stree
);
276 } END_FOR_EACH_PTR(tmp
);
278 FOR_EACH_SM(final_states
, sm
) {
280 } END_FOR_EACH_SM(sm
);
282 free_stree(&final_states
);
284 free_string(var_name
);
288 static void call_implies_callbacks(int comparison
, struct expression
*expr
, sval_t sval
, int left
, struct stree
**implied_true
, struct stree
**implied_false
)
290 struct call_back_list
*call_backs
;
291 struct fcall_back
*tmp
;
293 struct data_range
*value_range
;
294 struct stree
*true_states
= NULL
;
295 struct stree
*false_states
= NULL
;
296 struct stree
*tmp_stree
;
298 *implied_true
= NULL
;
299 *implied_false
= NULL
;
300 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
302 fn
= expr
->fn
->symbol
->ident
->name
;
303 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
306 value_range
= alloc_range(sval
, sval
);
308 /* set true states */
309 __push_fake_cur_stree();
310 FOR_EACH_PTR(call_backs
, tmp
) {
311 if (tmp
->type
!= RANGED_CALL
)
313 if (!true_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
315 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
316 } END_FOR_EACH_PTR(tmp
);
317 tmp_stree
= __pop_fake_cur_stree();
318 merge_fake_stree(&true_states
, tmp_stree
);
319 free_stree(&tmp_stree
);
321 /* set false states */
322 __push_fake_cur_stree();
323 FOR_EACH_PTR(call_backs
, tmp
) {
324 if (tmp
->type
!= RANGED_CALL
)
326 if (!false_comparison_range_LR(comparison
, tmp
->range
, value_range
, left
))
328 (tmp
->u
.ranged
)(fn
, expr
, NULL
, tmp
->info
);
329 } END_FOR_EACH_PTR(tmp
);
330 tmp_stree
= __pop_fake_cur_stree();
331 merge_fake_stree(&false_states
, tmp_stree
);
332 free_stree(&tmp_stree
);
334 *implied_true
= true_states
;
335 *implied_false
= false_states
;
338 struct db_callback_info
{
341 struct expression
*expr
;
342 struct range_list
*rl
;
345 struct db_implies_list
*callbacks
;
348 struct smatch_state
*ret_state
;
349 struct expression
*var_expr
;
353 static void store_return_state(struct db_callback_info
*db_info
, struct smatch_state
*state
)
355 db_info
->ret_state
= state
;
358 static void set_return_state(struct expression
*expr
, struct db_callback_info
*db_info
)
360 struct smatch_state
*state
;
362 if (!db_info
->ret_state
)
365 state
= alloc_estate_rl(cast_rl(get_type(expr
), clone_rl(estate_rl(db_info
->ret_state
))));
366 set_extra_expr_mod(expr
, state
);
367 db_info
->ret_state
= NULL
;
370 static void handle_ret_equals_param(char *ret_string
, struct range_list
*rl
, struct expression
*call
)
374 struct expression
*arg
;
375 struct range_list
*orig
;
377 str
= strstr(ret_string
, "==$");
381 param
= strtoll(str
, NULL
, 10);
382 arg
= get_argument_from_call_expr(call
->args
, param
);
385 get_absolute_rl(arg
, &orig
);
386 rl
= rl_intersection(orig
, rl
);
389 set_extra_expr_nomod(arg
, alloc_estate_rl(rl
));
392 static int impossible_limit(struct expression
*expr
, int param
, char *key
, char *value
)
394 struct expression
*arg
;
395 struct range_list
*passed
;
396 struct range_list
*limit
;
397 struct symbol
*compare_type
;
399 if (strcmp(key
, "$") != 0)
402 while (expr
->type
== EXPR_ASSIGNMENT
)
403 expr
= strip_expr(expr
->right
);
404 if (expr
->type
!= EXPR_CALL
)
407 arg
= get_argument_from_call_expr(expr
->args
, param
);
410 if (!get_implied_rl(arg
, &passed
))
412 if (!passed
|| is_whole_rl(passed
))
415 compare_type
= get_arg_type(expr
->fn
, param
);
416 passed
= cast_rl(compare_type
, passed
);
417 call_results_to_rl(expr
, compare_type
, value
, &limit
);
418 if (!limit
|| is_whole_rl(limit
))
420 if (possibly_true_rl(passed
, SPECIAL_EQUAL
, limit
))
422 if (option_debug
|| local_debug
)
423 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param
, key
, show_rl(passed
), value
);
427 static int db_compare_callback(void *_info
, int argc
, char **argv
, char **azColName
)
429 struct db_callback_info
*db_info
= _info
;
430 struct range_list
*var_rl
= db_info
->rl
;
431 struct range_list
*ret_range
;
434 struct return_implies_callback
*tmp
;
442 return_id
= atoi(argv
[0]);
443 type
= atoi(argv
[2]);
444 param
= atoi(argv
[3]);
448 if (db_info
->prev_return_id
!= -1 && return_id
!= db_info
->prev_return_id
) {
449 set_return_state(db_info
->var_expr
, db_info
);
450 stree
= __pop_fake_cur_stree();
453 merge_fake_stree(&db_info
->stree
, stree
);
455 __push_fake_cur_stree();
458 db_info
->prev_return_id
= return_id
;
462 if (type
== CULL_PATH
) {
466 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
)) {
471 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), argv
[1], &ret_range
);
472 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
474 ret_range
= alloc_whole_rl(get_type(db_info
->expr
));
476 comparison
= db_info
->comparison
;
478 comparison
= flip_comparison(comparison
);
480 if (db_info
->true_side
) {
481 if (!possibly_true_rl(var_rl
, comparison
, ret_range
))
483 if (type
== PARAM_LIMIT
)
484 param_limit_implications(db_info
->expr
, param
, key
, value
);
485 filter_by_comparison(&var_rl
, comparison
, ret_range
);
486 filter_by_comparison(&ret_range
, flip_comparison(comparison
), var_rl
);
488 if (!possibly_false_rl(var_rl
, comparison
, ret_range
))
490 if (type
== PARAM_LIMIT
)
491 param_limit_implications(db_info
->expr
, param
, key
, value
);
492 filter_by_comparison(&var_rl
, negate_comparison(comparison
), ret_range
);
493 filter_by_comparison(&ret_range
, flip_comparison(negate_comparison(comparison
)), var_rl
);
496 handle_ret_equals_param(argv
[1], ret_range
, db_info
->expr
);
498 if (type
== INTERNAL
)
499 set_state(-1, "unnull_path", NULL
, &true_state
);
501 FOR_EACH_PTR(db_info
->callbacks
, tmp
) {
502 if (tmp
->type
== type
)
503 tmp
->callback(db_info
->expr
, param
, key
, value
);
504 } END_FOR_EACH_PTR(tmp
);
506 store_return_state(db_info
, alloc_estate_rl(clone_rl(var_rl
)));
510 static void compare_db_return_states_callbacks(struct expression
*left
, int comparison
, struct expression
*right
, struct stree
*implied_true
, struct stree
*implied_false
)
512 struct stree
*orig_states
;
514 struct stree
*true_states
;
515 struct stree
*false_states
;
517 struct db_callback_info db_info
= {};
518 struct expression
*var_expr
;
519 struct expression
*call_expr
;
520 struct range_list
*rl
;
523 orig_states
= clone_stree(__get_cur_stree());
525 /* legacy cruft. need to fix call_implies_callbacks(). */
529 if (left
->type
!= EXPR_CALL
) {
535 get_absolute_rl(var_expr
, &rl
);
537 db_info
.comparison
= comparison
;
538 db_info
.expr
= call_expr
;
540 db_info
.left
= call_on_left
;
541 db_info
.callbacks
= db_return_states_list
;
542 db_info
.var_expr
= var_expr
;
544 call_return_states_before_hooks();
546 db_info
.true_side
= 1;
547 db_info
.stree
= NULL
;
548 db_info
.prev_return_id
= -1;
549 __push_fake_cur_stree();
550 sql_select_return_states("return_id, return, type, parameter, key, value",
551 call_expr
, db_compare_callback
, &db_info
);
552 set_return_state(db_info
.var_expr
, &db_info
);
553 stree
= __pop_fake_cur_stree();
555 set_return_state(db_info
.var_expr
, &db_info
);
556 merge_fake_stree(&db_info
.stree
, stree
);
559 true_states
= db_info
.stree
;
563 FOR_EACH_SM(orig_states
, sm
) {
564 __set_sm_cur_stree(sm
);
565 } END_FOR_EACH_SM(sm
);
567 db_info
.true_side
= 0;
568 db_info
.stree
= NULL
;
569 db_info
.prev_return_id
= -1;
571 __push_fake_cur_stree();
572 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr
,
573 db_compare_callback
, &db_info
);
574 stree
= __pop_fake_cur_stree();
576 set_return_state(db_info
.var_expr
, &db_info
);
577 merge_fake_stree(&db_info
.stree
, stree
);
580 false_states
= db_info
.stree
;
584 FOR_EACH_SM(orig_states
, sm
) {
585 __set_sm_cur_stree(sm
);
586 } END_FOR_EACH_SM(sm
);
588 free_stree(&orig_states
);
590 FOR_EACH_SM(true_states
, sm
) {
591 __set_true_false_sm(sm
, NULL
);
592 } END_FOR_EACH_SM(sm
);
593 FOR_EACH_SM(false_states
, sm
) {
594 __set_true_false_sm(NULL
, sm
);
595 } END_FOR_EACH_SM(sm
);
597 free_stree(&true_states
);
598 free_stree(&false_states
);
600 call_return_states_after_hooks(call_expr
);
602 FOR_EACH_SM(implied_true
, sm
) {
603 __set_true_false_sm(sm
, NULL
);
604 } END_FOR_EACH_SM(sm
);
605 FOR_EACH_SM(implied_false
, sm
) {
606 __set_true_false_sm(NULL
, sm
);
607 } END_FOR_EACH_SM(sm
);
610 void function_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
612 struct expression
*var_expr
;
613 struct expression
*call_expr
;
614 struct stree
*implied_true
= NULL
;
615 struct stree
*implied_false
= NULL
;
616 struct range_list
*rl
;
623 /* legacy cruft. need to fix call_implies_callbacks(). */
627 if (left
->type
!= EXPR_CALL
) {
633 get_absolute_rl(var_expr
, &rl
);
635 if (rl_to_sval(rl
, &sval
))
636 call_implies_callbacks(comparison
, call_expr
, sval
, call_on_left
, &implied_true
, &implied_false
);
638 compare_db_return_states_callbacks(left
, comparison
, right
, implied_true
, implied_false
);
639 free_stree(&implied_true
);
640 free_stree(&implied_false
);
643 static void call_ranged_return_hooks(struct db_callback_info
*db_info
)
645 struct call_back_list
*call_backs
;
646 struct expression
*expr
;
647 struct fcall_back
*tmp
;
650 expr
= strip_expr(db_info
->expr
);
651 while (expr
->type
== EXPR_ASSIGNMENT
)
652 expr
= strip_expr(expr
->right
);
653 if (expr
->type
!= EXPR_CALL
||
654 expr
->fn
->type
!= EXPR_SYMBOL
)
657 fn
= expr
->fn
->symbol_name
->name
;
659 call_backs
= search_callback(func_hash
, fn
);
660 FOR_EACH_PTR(call_backs
, tmp
) {
661 struct range_list
*range_rl
= NULL
;
663 if (tmp
->type
!= RANGED_CALL
)
665 add_range(&range_rl
, tmp
->range
->min
, tmp
->range
->max
);
666 range_rl
= cast_rl(estate_type(db_info
->ret_state
), range_rl
);
667 if (possibly_true_rl(range_rl
, SPECIAL_EQUAL
, estate_rl(db_info
->ret_state
))) {
668 if (!possibly_true_rl(rl_invert(range_rl
), SPECIAL_EQUAL
, estate_rl(db_info
->ret_state
)))
669 (tmp
->u
.ranged
)(fn
, expr
, db_info
->expr
, tmp
->info
);
671 db_info
->handled
= -1;
673 } END_FOR_EACH_PTR(tmp
);
676 static int db_assign_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
678 struct db_callback_info
*db_info
= _info
;
679 struct range_list
*ret_range
;
682 struct return_implies_callback
*tmp
;
689 return_id
= atoi(argv
[0]);
690 type
= atoi(argv
[2]);
691 param
= atoi(argv
[3]);
695 if (db_info
->prev_return_id
!= -1 && return_id
!= db_info
->prev_return_id
) {
696 call_ranged_return_hooks(db_info
);
697 set_return_state(db_info
->expr
->left
, db_info
);
698 stree
= __pop_fake_cur_stree();
700 merge_fake_stree(&db_info
->stree
, stree
);
702 __push_fake_cur_stree();
705 db_info
->prev_return_id
= return_id
;
709 if (type
== CULL_PATH
) {
713 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
)) {
718 if (type
== PARAM_LIMIT
)
719 param_limit_implications(db_info
->expr
, param
, key
, value
);
721 db_info
->handled
= 1;
722 call_results_to_rl(db_info
->expr
->right
, get_type(strip_expr(db_info
->expr
->right
)), argv
[1], &ret_range
);
723 __add_comparison_info(db_info
->expr
->left
, strip_expr(db_info
->expr
->right
), argv
[1]);
725 ret_range
= alloc_whole_rl(get_type(strip_expr(db_info
->expr
->right
)));
726 ret_range
= cast_rl(get_type(db_info
->expr
->right
), ret_range
);
728 if (type
== INTERNAL
)
729 set_state(-1, "unnull_path", NULL
, &true_state
);
731 FOR_EACH_PTR(db_return_states_list
, tmp
) {
732 if (tmp
->type
== type
)
733 tmp
->callback(db_info
->expr
, param
, key
, value
);
734 } END_FOR_EACH_PTR(tmp
);
735 store_return_state(db_info
, alloc_estate_rl(ret_range
));
740 static int db_return_states_assign(struct expression
*expr
)
742 struct expression
*right
;
745 struct db_callback_info db_info
= {};
747 right
= strip_expr(expr
->right
);
749 db_info
.prev_return_id
= -1;
751 db_info
.stree
= NULL
;
754 call_return_states_before_hooks();
756 __push_fake_cur_stree();
757 sql_select_return_states("return_id, return, type, parameter, key, value",
758 right
, db_assign_return_states_callback
, &db_info
);
760 sm_msg("%s return_id %d return_ranges %s",
761 db_info
.cull
? "culled" : "merging",
762 db_info
.prev_return_id
,
763 db_info
.ret_state
? db_info
.ret_state
->name
: "'<empty>'");
766 call_ranged_return_hooks(&db_info
);
767 set_return_state(db_info
.expr
->left
, &db_info
);
768 stree
= __pop_fake_cur_stree();
770 merge_fake_stree(&db_info
.stree
, stree
);
773 if (!db_info
.stree
&& db_info
.cull
) /* this means we culled everything */
774 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
775 FOR_EACH_SM(db_info
.stree
, sm
) {
777 } END_FOR_EACH_SM(sm
);
779 free_stree(&db_info
.stree
);
780 call_return_states_after_hooks(right
);
782 return db_info
.handled
;
785 static int handle_implied_return(struct expression
*expr
)
787 struct range_list
*rl
;
789 if (!get_implied_return(expr
->right
, &rl
))
791 rl
= cast_rl(get_type(expr
->left
), rl
);
792 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
796 static void match_assign_call(struct expression
*expr
)
798 struct call_back_list
*call_backs
;
800 struct expression
*right
;
802 struct range_list
*rl
;
807 right
= strip_expr(expr
->right
);
808 if (right
->fn
->type
!= EXPR_SYMBOL
|| !right
->fn
->symbol
) {
809 handled
|= db_return_states_assign(expr
);
811 goto assigned_unknown
;
814 if (is_fake_call(right
)) {
815 set_extra_expr_mod(expr
->left
, alloc_estate_whole(get_type(expr
->left
)));
819 fn
= right
->fn
->symbol
->ident
->name
;
822 * some of these conflict (they try to set smatch extra twice), so we
823 * call them in order from least important to most important.
826 call_backs
= search_callback(func_hash
, (char *)fn
);
827 call_call_backs(call_backs
, ASSIGN_CALL
, fn
, expr
);
829 if (db_return_states_assign(expr
) == 1)
832 handled
= assign_ranged_funcs(fn
, expr
, call_backs
);
833 handled
|= handle_implied_return(expr
);
839 get_absolute_rl(expr
->right
, &rl
);
840 rl
= cast_rl(get_type(expr
->left
), rl
);
841 set_extra_expr_mod(expr
->left
, alloc_estate_rl(rl
));
844 static int db_return_states_callback(void *_info
, int argc
, char **argv
, char **azColName
)
846 struct db_callback_info
*db_info
= _info
;
847 struct range_list
*ret_range
;
850 struct return_implies_callback
*tmp
;
858 return_id
= atoi(argv
[0]);
859 type
= atoi(argv
[2]);
860 param
= atoi(argv
[3]);
864 if (db_info
->prev_return_id
!= -1 && return_id
!= db_info
->prev_return_id
) {
865 stree
= __pop_fake_cur_stree();
867 merge_fake_stree(&db_info
->stree
, stree
);
869 __push_fake_cur_stree();
873 db_info
->prev_return_id
= return_id
;
877 if (type
== CULL_PATH
) {
881 if (type
== PARAM_LIMIT
&& impossible_limit(db_info
->expr
, param
, key
, value
)) {
886 if (type
== PARAM_LIMIT
)
887 param_limit_implications(db_info
->expr
, param
, key
, value
);
889 call_results_to_rl(db_info
->expr
, get_type(strip_expr(db_info
->expr
)), argv
[1], &ret_range
);
890 ret_range
= cast_rl(get_type(db_info
->expr
), ret_range
);
892 if (type
== INTERNAL
)
893 set_state(-1, "unnull_path", NULL
, &true_state
);
895 FOR_EACH_PTR(db_return_states_list
, tmp
) {
896 if (tmp
->type
== type
)
897 tmp
->callback(db_info
->expr
, param
, key
, value
);
898 } END_FOR_EACH_PTR(tmp
);
901 * We want to store the return values so that we can split the strees
902 * in smatch_db.c. This uses set_state() directly because it's not a
903 * real smatch_extra state.
905 snprintf(buf
, sizeof(buf
), "return %p", db_info
->expr
);
906 set_state(SMATCH_EXTRA
, buf
, NULL
, alloc_estate_rl(ret_range
));
911 static void db_return_states(struct expression
*expr
)
915 struct db_callback_info db_info
= {};
917 if (!__get_cur_stree()) /* no return functions */
920 db_info
.prev_return_id
= -1;
922 db_info
.stree
= NULL
;
924 call_return_states_before_hooks();
926 __push_fake_cur_stree();
928 sql_select_return_states("return_id, return, type, parameter, key, value",
929 expr
, db_return_states_callback
, &db_info
);
930 stree
= __pop_fake_cur_stree();
932 merge_fake_stree(&db_info
.stree
, stree
);
935 FOR_EACH_SM(db_info
.stree
, sm
) {
937 } END_FOR_EACH_SM(sm
);
939 free_stree(&db_info
.stree
);
940 call_return_states_after_hooks(expr
);
943 static int is_condition_call(struct expression
*expr
)
945 struct expression
*tmp
;
947 FOR_EACH_PTR_REVERSE(big_condition_stack
, tmp
) {
948 if (expr
== tmp
|| expr
->parent
== tmp
)
950 if (tmp
->pos
.line
< expr
->pos
.line
)
952 } END_FOR_EACH_PTR_REVERSE(tmp
);
957 static void db_return_states_call(struct expression
*expr
)
962 if (is_assigned_call(expr
))
964 if (is_condition_call(expr
))
966 db_return_states(expr
);
969 static void match_function_call(struct expression
*expr
)
971 struct call_back_list
*call_backs
;
973 if (expr
->fn
->type
== EXPR_SYMBOL
&& expr
->fn
->symbol
) {
974 call_backs
= search_callback(func_hash
, (char *)expr
->fn
->symbol
->ident
->name
);
976 call_call_backs(call_backs
, REGULAR_CALL
,
977 expr
->fn
->symbol
->ident
->name
, expr
);
979 db_return_states_call(expr
);
982 static void match_macro_assign(struct expression
*expr
)
984 struct call_back_list
*call_backs
;
986 struct expression
*right
;
988 right
= strip_expr(expr
->right
);
989 macro
= get_macro_name(right
->pos
);
990 call_backs
= search_callback(func_hash
, (char *)macro
);
993 call_call_backs(call_backs
, MACRO_ASSIGN
, macro
, expr
);
994 call_call_backs(call_backs
, MACRO_ASSIGN_EXTRA
, macro
, expr
);
997 int get_implied_return(struct expression
*expr
, struct range_list
**rl
)
999 struct call_back_list
*call_backs
;
1000 struct fcall_back
*tmp
;
1006 expr
= strip_expr(expr
);
1007 fn
= expr_to_var(expr
->fn
);
1011 call_backs
= search_callback(func_hash
, fn
);
1013 FOR_EACH_PTR(call_backs
, tmp
) {
1014 if (tmp
->type
== IMPLIED_RETURN
) {
1015 (tmp
->u
.implied_return
)(expr
, tmp
->info
, rl
);
1018 } END_FOR_EACH_PTR(tmp
);
1025 void create_function_hook_hash(void)
1027 func_hash
= create_function_hashtable(5000);
1030 void register_function_hooks(int id
)
1032 add_hook(&match_function_call
, CALL_HOOK_AFTER_INLINE
);
1033 add_hook(&match_assign_call
, CALL_ASSIGNMENT_HOOK
);
1034 add_hook(&match_macro_assign
, MACRO_ASSIGNMENT_HOOK
);