2 * Copyright (C) 2010 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
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
27 static sqlite3
*mem_db
;
31 #define sql_insert(table, values...) \
37 char *err, *p = buf; \
40 p += snprintf(p, buf + sizeof(buf) - p, \
41 "insert into %s values (", #table); \
42 p += snprintf(p, buf + sizeof(buf) - p, values); \
43 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
44 sm_debug("in-mem: %s\n", buf); \
45 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
46 if (rc != SQLITE_OK) { \
47 fprintf(stderr, "SQL error #2: %s\n", err); \
48 fprintf(stderr, "SQL: '%s'\n", buf); \
54 sm_printf("SQL: insert into " #table " values (" values); \
61 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
63 ALLOCATOR(def_callback
, "definition db hook callbacks");
64 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
65 static struct callback_list
*callbacks
;
67 struct member_info_callback
{
69 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
71 ALLOCATOR(member_info_callback
, "caller_info callbacks");
72 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
73 static struct member_info_cb_list
*member_callbacks
;
75 struct returned_state_callback
{
76 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
78 ALLOCATOR(returned_state_callback
, "returned state callbacks");
79 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
80 static struct returned_state_cb_list
*returned_state_callbacks
;
82 struct returned_member_callback
{
84 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
86 ALLOCATOR(returned_member_callback
, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
88 static struct returned_member_cb_list
*returned_member_callbacks
;
90 struct call_implies_callback
{
92 void (*callback
)(struct expression
*arg
, char *value
);
94 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
96 static struct call_implies_cb_list
*call_implies_cb_list
;
98 static int print_sql_output(void *unused
, int argc
, char **argv
, char **azColName
)
102 for (i
= 0; i
< argc
; i
++) {
105 sm_printf("%s", argv
[i
]);
111 void debug_sql(const char *sql
)
116 sql_exec(print_sql_output
, sql
);
120 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
125 if (option_no_db
|| !db
)
128 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
129 if (rc
!= SQLITE_OK
) {
130 fprintf(stderr
, "SQL error #2: %s\n", err
);
131 fprintf(stderr
, "SQL: '%s'\n", sql
);
135 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
143 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
144 if (rc
!= SQLITE_OK
) {
145 fprintf(stderr
, "SQL error #2: %s\n", err
);
146 fprintf(stderr
, "SQL: '%s'\n", sql
);
150 void sql_insert_return_states(int return_id
, const char *return_ranges
,
151 int type
, int param
, const char *key
, const char *value
)
153 if (key
&& strlen(key
) >= 80)
155 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
156 get_base_file(), get_function(), (unsigned long)__inline_fn
,
157 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
160 static struct string_list
*common_funcs
;
161 static int is_common_function(const char *fn
)
165 if (strncmp(fn
, "__builtin_", 10) == 0)
168 FOR_EACH_PTR(common_funcs
, tmp
) {
169 if (strcmp(tmp
, fn
) == 0)
171 } END_FOR_EACH_PTR(tmp
);
176 void sql_insert_caller_info(struct expression
*call
, int type
,
177 int param
, const char *key
, const char *value
)
181 if (!option_info
&& !__inline_call
)
184 if (key
&& strlen(key
) >= 80)
187 fn
= get_fnptr_name(call
->fn
);
193 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
194 get_base_file(), get_function(), fn
, (unsigned long)call
,
195 is_static(call
->fn
), type
, param
, key
, value
);
201 if (is_common_function(fn
))
204 sm_msg("SQL_caller_info: insert into caller_info values ("
205 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
206 get_base_file(), get_function(), fn
, is_static(call
->fn
),
207 type
, param
, key
, value
);
212 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
214 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
218 void sql_insert_call_implies(int type
, int param
, int value
)
220 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
221 get_function(), (unsigned long)__inline_fn
, fn_static(),
225 void sql_insert_function_type_size(const char *member
, const char *ranges
)
227 sql_insert(function_type_size
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member
, ranges
);
230 void sql_insert_local_values(const char *name
, const char *value
)
232 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
235 void sql_insert_function_type_value(const char *type
, const char *value
)
237 sql_insert(function_type_value
, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type
, value
);
240 static char *get_static_filter(struct symbol
*sym
)
242 static char sql_filter
[1024];
244 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
245 snprintf(sql_filter
, sizeof(sql_filter
),
246 "file = '%s' and function = '%s' and static = '1'",
247 get_base_file(), sym
->ident
->name
);
249 snprintf(sql_filter
, sizeof(sql_filter
),
250 "function = '%s' and static = '0'", sym
->ident
->name
);
256 static int row_count
;
257 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
261 row_count
= atoi(argv
[0]);
265 static void sql_select_return_states_pointer(const char *cols
,
266 struct expression
*call
, int (*callback
)(void*, int, char**, char**))
270 ptr
= get_fnptr_name(call
->fn
);
275 "select %s from return_states join function_ptr where "
276 "return_states.function == function_ptr.function and ptr = '%s'"
277 "and searchable = 1 order by return_id, type;",
281 void sql_select_return_states(const char *cols
, struct expression
*call
,
282 int (*callback
)(void*, int, char**, char**))
284 if (is_fake_call(call
))
287 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
) {
288 sql_select_return_states_pointer(cols
, call
, callback
);
292 if (inlinable(call
->fn
)) {
294 "select %s from return_states where call_id = '%lu' order by return_id, type;",
295 cols
, (unsigned long)call
);
300 run_sql(get_row_count
, "select count(*) from return_states where %s;",
301 get_static_filter(call
->fn
->symbol
));
302 if (row_count
> 1000)
305 run_sql(callback
, "select %s from return_states where %s order by return_id, type;",
306 cols
, get_static_filter(call
->fn
->symbol
));
309 void sql_select_call_implies(const char *cols
, struct expression
*call
,
310 int (*callback
)(void*, int, char**, char**))
312 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
315 if (inlinable(call
->fn
)) {
317 "select %s from call_implies where call_id = '%lu';",
318 cols
, (unsigned long)call
);
322 run_sql(callback
, "select %s from call_implies where %s;",
323 cols
, get_static_filter(call
->fn
->symbol
));
326 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
327 int (*callback
)(void*, int, char**, char**))
330 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
331 cols
, (unsigned long)__inline_fn
);
336 "select %s from caller_info where %s order by call_id;",
337 cols
, get_static_filter(sym
));
340 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
342 struct def_callback
*def_callback
= __alloc_def_callback(0);
344 def_callback
->hook_type
= type
;
345 def_callback
->callback
= callback
;
346 add_ptr_list(&callbacks
, def_callback
);
350 * These call backs are used when the --info option is turned on to print struct
351 * member information. For example foo->bar could have a state in
352 * smatch_extra.c and also check_user.c.
354 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
356 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
358 member_callback
->owner
= owner
;
359 member_callback
->callback
= callback
;
360 add_ptr_list(&member_callbacks
, member_callback
);
363 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
365 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
367 callback
->callback
= fn
;
368 add_ptr_list(&returned_state_callbacks
, callback
);
371 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
373 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
375 member_callback
->owner
= owner
;
376 member_callback
->callback
= callback
;
377 add_ptr_list(&returned_member_callbacks
, member_callback
);
380 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *value
))
382 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
385 cb
->callback
= callback
;
386 add_ptr_list(&call_implies_cb_list
, cb
);
389 static struct expression
*static_call_expr
;
390 static struct expression
*static_returns_call
;
391 static struct symbol
*return_type
;
392 static struct range_list
*return_range_list
;
393 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
395 struct range_list
*rl
;
396 struct expression
*call_expr
= static_returns_call
;
400 call_results_to_rl(call_expr
, return_type
, argv
[0], &rl
);
401 return_range_list
= rl_union(return_range_list
, rl
);
405 struct range_list
*db_return_vals(struct expression
*expr
)
407 static_returns_call
= expr
;
408 return_type
= get_type(expr
);
411 if (is_fake_call(expr
))
413 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
416 return_range_list
= NULL
;
417 if (inlinable(expr
->fn
)) {
418 mem_sql(db_return_callback
,
419 "select distinct return from return_states where call_id = '%lu';",
420 (unsigned long)expr
);
422 run_sql(db_return_callback
,
423 "select distinct return from return_states where %s;",
424 get_static_filter(expr
->fn
->symbol
));
426 return return_range_list
;
429 static void match_call_marker(struct expression
*expr
)
432 * we just want to record something in the database so that if we have
433 * two calls like: frob(4); frob(some_unkown); then on the receiving
434 * side we know that sometimes frob is called with unknown parameters.
437 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
440 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct stree
*stree
,
441 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
447 char printed_name
[256];
450 expr
= strip_expr(expr
);
451 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
452 expr
= strip_expr(expr
->unop
);
456 name
= expr_to_var_sym(expr
, &sym
);
461 FOR_EACH_SM(stree
, sm
) {
464 if (strcmp(name
, sm
->name
) == 0) {
466 snprintf(printed_name
, sizeof(printed_name
), "*$$");
467 else /* these are already handled. fixme: handle them here */
469 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
470 snprintf(printed_name
, sizeof(printed_name
), "*$$");
471 } else if (strncmp(name
, sm
->name
, len
) == 0) {
473 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
475 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
479 callback(call
, param
, printed_name
, sm
->state
);
480 } END_FOR_EACH_SM(sm
);
485 static void match_call_info(struct expression
*call
)
487 struct member_info_callback
*cb
;
488 struct expression
*arg
;
493 name
= get_fnptr_name(call
->fn
);
497 FOR_EACH_PTR(member_callbacks
, cb
) {
498 stree
= get_all_states_stree(cb
->owner
);
500 FOR_EACH_PTR(call
->args
, arg
) {
501 print_struct_members(call
, arg
, i
, stree
, cb
->callback
);
503 } END_FOR_EACH_PTR(arg
);
505 } END_FOR_EACH_PTR(cb
);
510 static int get_param(int param
, char **name
, struct symbol
**sym
)
516 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
518 * this is a temporary hack to work around a bug (I think in sparse?)
519 * 2.6.37-rc1:fs/reiserfs/journal.o
520 * If there is a function definition without parameter name found
521 * after a function implementation then it causes a crash.
525 if (arg
->ident
->name
< (char *)100)
527 if (i
== param
&& arg
->ident
->name
) {
528 *name
= arg
->ident
->name
;
533 } END_FOR_EACH_PTR(arg
);
538 static struct stree
*final_states
;
539 static int prev_func_id
= -1;
540 static int caller_info_callback(void *unused
, int argc
, char **argv
, char **azColName
)
546 struct symbol
*sym
= NULL
;
547 struct def_callback
*def_callback
;
553 func_id
= atoi(argv
[0]);
555 type
= strtol(argv
[1], NULL
, 10);
556 param
= strtol(argv
[2], NULL
, 10);
560 if (prev_func_id
== -1)
561 prev_func_id
= func_id
;
562 if (func_id
!= prev_func_id
) {
563 stree
= __pop_fake_cur_stree();
564 merge_stree(&final_states
, stree
);
566 __push_fake_cur_stree();
568 prev_func_id
= func_id
;
571 if (type
== INTERNAL
)
573 if (param
>= 0 && !get_param(param
, &name
, &sym
))
576 FOR_EACH_PTR(callbacks
, def_callback
) {
577 if (def_callback
->hook_type
== type
)
578 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
579 } END_FOR_EACH_PTR(def_callback
);
584 static void get_direct_callers(struct symbol
*sym
)
586 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
587 caller_info_callback
);
590 static struct string_list
*ptr_names_done
;
591 static struct string_list
*ptr_names
;
593 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
595 insert_string(&ptr_names
, alloc_string(argv
[0]));
599 static char *get_next_ptr_name(void)
603 FOR_EACH_PTR(ptr_names
, ptr
) {
604 if (list_has_string(ptr_names_done
, ptr
))
606 insert_string(&ptr_names_done
, ptr
);
608 } END_FOR_EACH_PTR(ptr
);
612 static void get_ptr_names(const char *file
, const char *name
)
614 char sql_filter
[1024];
618 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
621 snprintf(sql_filter
, 1024, "function = '%s';", name
);
624 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
626 run_sql(get_ptr_name
,
627 "select distinct ptr from function_ptr where %s",
630 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
634 while ((name
= get_next_ptr_name()))
635 get_ptr_names(NULL
, name
);
638 static void match_data_from_db(struct symbol
*sym
)
643 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
646 __push_fake_cur_stree();
653 if (sym
->ctype
.modifiers
& MOD_STATIC
)
654 get_ptr_names(get_base_file(), sym
->ident
->name
);
656 get_ptr_names(NULL
, sym
->ident
->name
);
658 if (ptr_list_size((struct ptr_list
*)ptr_names
) > 20) {
659 __free_ptr_list((struct ptr_list
**)&ptr_names
);
660 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
661 stree
= __pop_fake_cur_stree();
666 get_direct_callers(sym
);
668 FOR_EACH_PTR(ptr_names
, ptr
) {
669 run_sql(caller_info_callback
, "select call_id, type, parameter, key, value"
670 " from caller_info where function = '%s' order by call_id",
673 } END_FOR_EACH_PTR(ptr
);
675 __free_ptr_list((struct ptr_list
**)&ptr_names
);
676 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
678 get_direct_callers(sym
);
681 stree
= __pop_fake_cur_stree();
682 merge_stree(&final_states
, stree
);
685 FOR_EACH_SM(final_states
, sm
) {
687 } END_FOR_EACH_SM(sm
);
689 free_stree(&final_states
);
692 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
694 struct expression
*call_expr
= static_call_expr
;
695 struct call_implies_callback
*cb
;
696 struct expression
*arg
= NULL
;
703 type
= atoi(argv
[1]);
704 param
= atoi(argv
[2]);
706 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
707 if (cb
->type
!= type
)
710 arg
= get_argument_from_call_expr(call_expr
->args
, param
);
714 cb
->callback(arg
, argv
[3]);
715 } END_FOR_EACH_PTR(cb
);
720 static void match_call_implies(struct expression
*expr
)
722 static_call_expr
= expr
;
723 sql_select_call_implies("function, type, parameter, value", expr
,
724 call_implies_callbacks
);
728 static void print_initializer_list(struct expression_list
*expr_list
,
729 struct symbol
*struct_type
)
731 struct expression
*expr
;
732 struct symbol
*base_type
;
733 char struct_name
[256];
735 FOR_EACH_PTR(expr_list
, expr
) {
736 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
737 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
740 if (expr
->type
!= EXPR_IDENTIFIER
)
742 if (!expr
->expr_ident
)
744 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
746 base_type
= get_type(expr
->ident_expression
);
747 if (!base_type
|| base_type
->type
!= SYM_FN
)
749 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
750 struct_type
->ident
->name
, expr
->expr_ident
->name
);
751 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
753 } END_FOR_EACH_PTR(expr
);
756 static void global_variable(struct symbol
*sym
)
758 struct symbol
*struct_type
;
762 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
764 struct_type
= get_base_type(sym
);
767 if (struct_type
->type
== SYM_ARRAY
) {
768 struct_type
= get_base_type(struct_type
);
772 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
774 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
777 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
779 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
782 static void call_return_state_hooks_conditional(struct expression
*expr
)
784 struct returned_state_callback
*cb
;
785 struct range_list
*rl
;
787 int final_pass_orig
= final_pass
;
789 __push_fake_cur_stree();
792 __split_whole_condition(expr
->conditional
);
793 final_pass
= final_pass_orig
;
795 if (get_implied_rl(expr
->cond_true
, &rl
))
796 rl
= cast_rl(cur_func_return_type(), rl
);
798 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
799 return_ranges
= show_rl(rl
);
802 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
803 cb
->callback(return_id
, return_ranges
, expr
);
804 } END_FOR_EACH_PTR(cb
);
806 __push_true_states();
807 __use_false_states();
809 if (get_implied_rl(expr
->cond_false
, &rl
))
810 rl
= cast_rl(cur_func_return_type(), rl
);
812 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
813 return_ranges
= show_rl(rl
);
816 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
817 cb
->callback(return_id
, return_ranges
, expr
);
818 } END_FOR_EACH_PTR(cb
);
820 __merge_true_states();
821 __free_fake_cur_stree();
824 static void call_return_state_hooks_compare(struct expression
*expr
)
826 struct returned_state_callback
*cb
;
828 int final_pass_orig
= final_pass
;
830 __push_fake_cur_stree();
833 __split_whole_condition(expr
);
834 final_pass
= final_pass_orig
;
836 return_ranges
= alloc_sname("1");
839 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
840 cb
->callback(return_id
, return_ranges
, expr
);
841 } END_FOR_EACH_PTR(cb
);
843 __push_true_states();
844 __use_false_states();
846 return_ranges
= alloc_sname("0");;
848 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
849 cb
->callback(return_id
, return_ranges
, expr
);
850 } END_FOR_EACH_PTR(cb
);
852 __merge_true_states();
853 __free_fake_cur_stree();
856 static int call_return_state_hooks_split_possible(struct expression
*expr
)
858 struct returned_state_callback
*cb
;
859 struct range_list
*rl
;
862 struct sm_state
*tmp
;
864 int nr_possible
, nr_states
;
868 if (!expr
|| expr_equal_to_param(expr
))
871 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
872 if (!sm
|| !sm
->merged
)
875 if (too_many_possible(sm
))
878 /* bail if it gets too complicated */
879 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
880 nr_states
= stree_count(__get_cur_stree());
882 * the main thing option_info because we don't want to print a
883 * million lines of output. If someone else, like check_locking.c
884 * wants this data, then it doesn't cause a slow down to provide it.
886 if (option_info
&& nr_states
* nr_possible
>= 2000)
889 compare_str
= expr_lte_to_param(expr
);
891 FOR_EACH_PTR(sm
->possible
, tmp
) {
896 __push_fake_cur_stree();
898 overwrite_states_using_pool(tmp
);
900 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
901 return_ranges
= show_rl(rl
);
903 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
904 return_ranges
= alloc_sname(buf
);
908 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
909 cb
->callback(return_id
, return_ranges
, expr
);
910 } END_FOR_EACH_PTR(cb
);
912 __free_fake_cur_stree();
913 } END_FOR_EACH_PTR(tmp
);
918 static char *get_return_ranges_str(struct expression
*expr
)
920 struct range_list
*rl
;
926 return alloc_sname("");
927 compare_str
= expr_equal_to_param(expr
);
931 if (get_implied_rl(expr
, &rl
)) {
932 rl
= cast_rl(cur_func_return_type(), rl
);
933 return_ranges
= show_rl(rl
);
935 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
936 return_ranges
= show_rl(rl
);
938 compare_str
= expr_lte_to_param(expr
);
940 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
941 return alloc_sname(buf
);
943 return return_ranges
;
946 static int is_boolean(struct expression
*expr
)
948 struct range_list
*rl
;
950 if (!get_implied_rl(expr
, &rl
))
952 if (rl_min(rl
).value
== 0 && rl_max(rl
).value
== 1)
957 static int is_conditional(struct expression
*expr
)
961 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
966 static void call_return_state_hooks(struct expression
*expr
)
968 struct returned_state_callback
*cb
;
972 expr
= strip_expr(expr
);
974 if (is_condition(expr
) || is_boolean(expr
)) {
975 call_return_state_hooks_compare(expr
);
977 } else if (is_conditional(expr
)) {
978 call_return_state_hooks_conditional(expr
);
980 } else if (call_return_state_hooks_split_possible(expr
)) {
984 return_ranges
= get_return_ranges_str(expr
);
987 nr_states
= stree_count(__get_cur_stree());
988 if (nr_states
>= 10000) {
989 match_return_info(return_id
, return_ranges
, expr
);
992 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
993 cb
->callback(return_id
, return_ranges
, expr
);
994 } END_FOR_EACH_PTR(cb
);
997 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
999 struct returned_member_callback
*cb
;
1000 struct stree
*stree
;
1001 struct sm_state
*sm
;
1002 struct symbol
*type
;
1004 char member_name
[256];
1007 type
= get_type(expr
);
1008 if (!type
|| type
->type
!= SYM_PTR
)
1010 name
= expr_to_var(expr
);
1014 member_name
[sizeof(member_name
) - 1] = '\0';
1015 strcpy(member_name
, "$$");
1018 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
1019 stree
= __get_cur_stree();
1020 FOR_EACH_MY_SM(cb
->owner
, stree
, sm
) {
1021 if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, name
) == 0) {
1022 strcpy(member_name
, "*$$");
1023 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
1026 if (strncmp(sm
->name
, name
, len
) != 0)
1028 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
1030 strcpy(member_name
, "$$");
1031 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
1032 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
1033 } END_FOR_EACH_SM(sm
);
1034 } END_FOR_EACH_PTR(cb
);
1039 static void reset_memdb(void)
1041 mem_sql(NULL
, "delete from caller_info;");
1042 mem_sql(NULL
, "delete from return_states;");
1043 mem_sql(NULL
, "delete from call_implies;");
1046 static void match_end_func_info(struct symbol
*sym
)
1048 if (__path_is_null())
1050 call_return_state_hooks(NULL
);
1055 static void init_memdb(void)
1059 const char *schema_files
[] = {
1061 "db/caller_info.schema",
1062 "db/return_states.schema",
1063 "db/function_type_size.schema",
1064 "db/type_size.schema",
1065 "db/call_implies.schema",
1066 "db/function_ptr.schema",
1067 "db/local_values.schema",
1068 "db/function_type_value.schema",
1069 "db/type_value.schema",
1071 static char buf
[4096];
1076 rc
= sqlite3_open(":memory:", &mem_db
);
1077 if (rc
!= SQLITE_OK
) {
1078 printf("Error starting In-Memory database.");
1082 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1083 fd
= open_data_file(schema_files
[i
]);
1088 ret
= read(fd
, buf
, sizeof(buf
));
1089 if (ret
== sizeof(buf
)) {
1090 printf("Schema file too large: %s (limit %zd bytes)",
1091 schema_files
[i
], sizeof(buf
));
1094 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1095 if (rc
!= SQLITE_OK
) {
1096 fprintf(stderr
, "SQL error #2: %s\n", err
);
1097 fprintf(stderr
, "%s\n", buf
);
1102 void open_smatch_db(void)
1111 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1112 if (rc
!= SQLITE_OK
) {
1119 static void register_common_funcs(void)
1121 struct token
*token
;
1125 if (option_project
== PROJ_NONE
)
1126 strcpy(filename
, "common_functions");
1128 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1130 token
= get_tokens_file(filename
);
1133 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1135 token
= token
->next
;
1136 while (token_type(token
) != TOKEN_STREAMEND
) {
1137 if (token_type(token
) != TOKEN_IDENT
)
1139 func
= alloc_string(show_ident(token
->ident
));
1140 add_ptr_list(&common_funcs
, func
);
1141 token
= token
->next
;
1143 clear_token_alloc();
1147 void register_definition_db_callbacks(int id
)
1149 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1150 add_hook(&global_variable
, BASE_HOOK
);
1151 add_hook(&global_variable
, DECLARATION_HOOK
);
1152 add_split_return_callback(match_return_info
);
1153 add_split_return_callback(print_returned_struct_members
);
1154 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1155 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1157 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1158 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1160 register_common_funcs();
1163 void register_db_call_marker(int id
)
1165 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1168 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1170 struct expression
*arg
;
1172 char member_name
[256];
1177 const char *star
= "";
1179 if (expr
->type
!= EXPR_ASSIGNMENT
)
1181 name
= expr_to_var_sym(expr
->left
, sym
);
1184 if (key
[0] == '*') {
1188 if (strncmp(key
, "$$", 2) != 0)
1190 snprintf(member_name
, sizeof(member_name
), "%s%s%s", star
, name
, key
+ 2);
1192 return alloc_string(member_name
);
1195 while (expr
->type
== EXPR_ASSIGNMENT
)
1196 expr
= strip_expr(expr
->right
);
1197 if (expr
->type
!= EXPR_CALL
)
1200 arg
= get_argument_from_call_expr(expr
->args
, param
);
1204 return get_variable_from_key(arg
, key
, sym
);
1207 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1212 if (strcmp(key
, "$$") == 0)
1213 return expr_to_var_sym(arg
, sym
);
1215 if (strcmp(key
, "*$$") == 0) {
1216 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1217 arg
= strip_expr(arg
->unop
);
1218 return expr_to_var_sym(arg
, sym
);
1220 tmp
= expr_to_var_sym(arg
, sym
);
1223 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1225 return alloc_string(buf
);
1229 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1230 arg
= strip_expr(arg
->unop
);
1231 tmp
= expr_to_var_sym(arg
, sym
);
1234 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1235 return alloc_string(buf
);
1238 tmp
= expr_to_var_sym(arg
, sym
);
1241 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1243 return alloc_string(buf
);
1246 const char *get_param_name(struct sm_state
*sm
)
1250 static char buf
[256];
1252 if (!sm
->sym
->ident
)
1255 param_name
= sm
->sym
->ident
->name
;
1256 name_len
= strlen(param_name
);
1258 if (strcmp(sm
->name
, param_name
) == 0) {
1260 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1261 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1262 snprintf(buf
, sizeof(buf
), "$$%s", sm
->name
+ name_len
);
1264 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {