4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
19 static sqlite3
*mem_db
;
21 #define sql_insert(table, values...) \
27 char *err, *p = buf; \
30 p += snprintf(p, buf + sizeof(buf) - p, \
31 "insert into %s values (", #table); \
32 p += snprintf(p, buf + sizeof(buf) - p, values); \
33 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
34 sm_debug("in-mem: %s\n", buf); \
35 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
36 if (rc != SQLITE_OK) { \
37 fprintf(stderr, "SQL error #2: %s\n", err); \
38 fprintf(stderr, "SQL: '%s'\n", buf); \
44 sm_printf("SQL: insert into " #table " values (" values); \
51 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
53 ALLOCATOR(def_callback
, "definition db hook callbacks");
54 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
55 static struct callback_list
*callbacks
;
57 struct member_info_callback
{
59 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
61 ALLOCATOR(member_info_callback
, "caller_info callbacks");
62 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
63 static struct member_info_cb_list
*member_callbacks
;
65 struct returned_state_callback
{
66 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
68 ALLOCATOR(returned_state_callback
, "returned state callbacks");
69 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
70 static struct returned_state_cb_list
*returned_state_callbacks
;
72 struct returned_member_callback
{
74 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
76 ALLOCATOR(returned_member_callback
, "returned member callbacks");
77 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
78 static struct returned_member_cb_list
*returned_member_callbacks
;
80 struct call_implies_callback
{
82 void (*callback
)(struct expression
*arg
, char *value
);
84 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
85 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
86 static struct call_implies_cb_list
*call_implies_cb_list
;
88 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
93 if (option_no_db
|| !db
)
96 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
97 if (rc
!= SQLITE_OK
) {
98 fprintf(stderr
, "SQL error #2: %s\n", err
);
99 fprintf(stderr
, "SQL: '%s'\n", sql
);
103 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
111 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
112 if (rc
!= SQLITE_OK
) {
113 fprintf(stderr
, "SQL error #2: %s\n", err
);
114 fprintf(stderr
, "SQL: '%s'\n", sql
);
118 void sql_insert_return_states(int return_id
, const char *return_ranges
,
119 int type
, int param
, const char *key
, const char *value
)
121 if (key
&& strlen(key
) >= 80)
123 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
124 get_base_file(), get_function(), (unsigned long)__inline_fn
,
125 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
128 static struct string_list
*common_funcs
;
129 static int is_common_function(const char *fn
)
133 if (strncmp(fn
, "__builtin_", 10) == 0)
136 FOR_EACH_PTR(common_funcs
, tmp
) {
137 if (strcmp(tmp
, fn
) == 0)
139 } END_FOR_EACH_PTR(tmp
);
144 void sql_insert_caller_info(struct expression
*call
, int type
,
145 int param
, const char *key
, const char *value
)
149 if (!option_info
&& !__inline_call
)
152 if (key
&& strlen(key
) >= 80)
155 fn
= get_fnptr_name(call
->fn
);
161 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
162 get_base_file(), get_function(), fn
, (unsigned long)call
,
163 is_static(call
->fn
), type
, param
, key
, value
);
169 if (is_common_function(fn
))
172 sm_msg("SQL_caller_info: insert into caller_info values ("
173 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
174 get_base_file(), get_function(), fn
, is_static(call
->fn
),
175 type
, param
, key
, value
);
180 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
182 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
186 void sql_insert_call_implies(int type
, int param
, int value
)
188 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
189 get_function(), (unsigned long)__inline_fn
, fn_static(),
193 void sql_insert_type_size(const char *member
, int size
)
195 sql_insert(type_size
, "'%s', '%s', %d", get_base_file(), member
, size
);
198 void sql_insert_local_values(const char *name
, const char *value
)
200 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
203 static char *get_static_filter(struct symbol
*sym
)
205 static char sql_filter
[1024];
207 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
208 snprintf(sql_filter
, sizeof(sql_filter
),
209 "file = '%s' and function = '%s' and static = '1'",
210 get_base_file(), sym
->ident
->name
);
212 snprintf(sql_filter
, sizeof(sql_filter
),
213 "function = '%s' and static = '0'", sym
->ident
->name
);
219 static int row_count
;
220 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
224 row_count
= atoi(argv
[0]);
228 void sql_select_return_states(const char *cols
, struct expression
*call
,
229 int (*callback
)(void*, int, char**, char**))
231 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
234 if (inlinable(call
->fn
)) {
236 "select %s from return_states where call_id = '%lu';",
237 cols
, (unsigned long)call
);
242 run_sql(get_row_count
, "select count(*) from return_states where %s;",
243 get_static_filter(call
->fn
->symbol
));
244 if (row_count
> 1000)
247 run_sql(callback
, "select %s from return_states where %s;",
248 cols
, get_static_filter(call
->fn
->symbol
));
251 void sql_select_call_implies(const char *cols
, struct expression
*call
,
252 int (*callback
)(void*, int, char**, char**))
254 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
257 if (inlinable(call
->fn
)) {
259 "select %s from call_implies where call_id = '%lu';",
260 cols
, (unsigned long)call
);
264 run_sql(callback
, "select %s from call_implies where %s;",
265 cols
, get_static_filter(call
->fn
->symbol
));
268 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
269 int (*callback
)(void*, int, char**, char**))
272 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
273 cols
, (unsigned long)__inline_fn
);
278 "select %s from caller_info where %s order by call_id;",
279 cols
, get_static_filter(sym
));
282 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
284 struct def_callback
*def_callback
= __alloc_def_callback(0);
286 def_callback
->hook_type
= type
;
287 def_callback
->callback
= callback
;
288 add_ptr_list(&callbacks
, def_callback
);
292 * These call backs are used when the --info option is turned on to print struct
293 * member information. For example foo->bar could have a state in
294 * smatch_extra.c and also check_user.c.
296 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
298 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
300 member_callback
->owner
= owner
;
301 member_callback
->callback
= callback
;
302 add_ptr_list(&member_callbacks
, member_callback
);
305 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
307 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
309 callback
->callback
= fn
;
310 add_ptr_list(&returned_state_callbacks
, callback
);
313 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
315 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
317 member_callback
->owner
= owner
;
318 member_callback
->callback
= callback
;
319 add_ptr_list(&returned_member_callbacks
, member_callback
);
322 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *value
))
324 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
327 cb
->callback
= callback
;
328 add_ptr_list(&call_implies_cb_list
, cb
);
331 static struct expression
*static_call_expr
;
332 static struct symbol
*return_type
;
333 static struct range_list
*return_range_list
;
334 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
336 struct range_list
*rl
;
340 call_results_to_rl(static_call_expr
, return_type
, argv
[0], &rl
);
341 return_range_list
= rl_union(return_range_list
, rl
);
345 struct range_list
*db_return_vals(struct expression
*expr
)
347 static_call_expr
= expr
;
348 return_type
= get_type(expr
);
351 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
354 return_range_list
= NULL
;
355 if (inlinable(expr
->fn
)) {
356 mem_sql(db_return_callback
,
357 "select distinct return from return_states where call_id = '%lu';",
358 (unsigned long)expr
);
360 run_sql(db_return_callback
,
361 "select distinct return from return_states where %s;",
362 get_static_filter(expr
->fn
->symbol
));
364 return return_range_list
;
367 static void match_call_marker(struct expression
*expr
)
370 * we just want to record something in the database so that if we have
371 * two calls like: frob(4); frob(some_unkown); then on the receiving
372 * side we know that sometimes frob is called with unknown parameters.
375 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
378 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct state_list
*slist
,
379 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
385 char printed_name
[256];
388 expr
= strip_expr(expr
);
389 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
390 expr
= strip_expr(expr
->unop
);
394 name
= expr_to_var_sym(expr
, &sym
);
399 FOR_EACH_PTR(slist
, sm
) {
402 if (strcmp(name
, sm
->name
) == 0) {
404 snprintf(printed_name
, sizeof(printed_name
), "*$$");
405 else /* these are already handled. fixme: handle them here */
407 } else if (sm
->name
[0] == '*' && strcmp(name
, sm
->name
+ 1) == 0) {
408 snprintf(printed_name
, sizeof(printed_name
), "*$$");
409 } else if (strncmp(name
, sm
->name
, len
) == 0) {
411 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
413 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
417 callback(call
, param
, printed_name
, sm
->state
);
418 } END_FOR_EACH_PTR(sm
);
423 static void match_call_info(struct expression
*call
)
425 struct member_info_callback
*cb
;
426 struct expression
*arg
;
427 struct state_list
*slist
;
431 name
= get_fnptr_name(call
->fn
);
435 FOR_EACH_PTR(member_callbacks
, cb
) {
436 slist
= get_all_states(cb
->owner
);
438 FOR_EACH_PTR(call
->args
, arg
) {
439 print_struct_members(call
, arg
, i
, slist
, cb
->callback
);
441 } END_FOR_EACH_PTR(arg
);
443 } END_FOR_EACH_PTR(cb
);
448 static int get_param(int param
, char **name
, struct symbol
**sym
)
454 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
456 * this is a temporary hack to work around a bug (I think in sparse?)
457 * 2.6.37-rc1:fs/reiserfs/journal.o
458 * If there is a function definition without parameter name found
459 * after a function implementation then it causes a crash.
463 if (arg
->ident
->name
< (char *)100)
465 if (i
== param
&& arg
->ident
->name
) {
466 *name
= arg
->ident
->name
;
471 } END_FOR_EACH_PTR(arg
);
476 static struct state_list
*final_states
;
477 static int prev_func_id
= -1;
478 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
484 struct symbol
*sym
= NULL
;
485 struct def_callback
*def_callback
;
486 struct state_list
*slist
;
491 func_id
= atoi(argv
[0]);
493 type
= strtol(argv
[1], NULL
, 10);
494 param
= strtol(argv
[2], NULL
, 10);
498 if (prev_func_id
== -1)
499 prev_func_id
= func_id
;
500 if (func_id
!= prev_func_id
) {
501 slist
= __pop_fake_cur_slist();
502 merge_slist(&final_states
, slist
);
504 __push_fake_cur_slist();
506 prev_func_id
= func_id
;
509 if (type
== INTERNAL
)
511 if (param
>= 0 && !get_param(param
, &name
, &sym
))
514 FOR_EACH_PTR(callbacks
, def_callback
) {
515 if (def_callback
->hook_type
== type
)
516 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
517 } END_FOR_EACH_PTR(def_callback
);
522 static void get_direct_callers(struct symbol
*sym
)
524 sql_select_caller_info("call_id, type, parameter, key, value", sym
,
528 static struct string_list
*ptr_names_done
;
529 static struct string_list
*ptr_names
;
531 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
533 insert_string(&ptr_names
, alloc_string(argv
[0]));
537 static char *get_next_ptr_name(void)
541 FOR_EACH_PTR(ptr_names
, ptr
) {
542 if (list_has_string(ptr_names_done
, ptr
))
544 insert_string(&ptr_names_done
, ptr
);
546 } END_FOR_EACH_PTR(ptr
);
550 static void get_ptr_names(const char *file
, const char *name
)
552 char sql_filter
[1024];
556 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
559 snprintf(sql_filter
, 1024, "function = '%s';", name
);
562 before
= ptr_list_size((struct ptr_list
*)ptr_names
);
564 run_sql(get_ptr_name
,
565 "select distinct ptr from function_ptr where %s",
568 after
= ptr_list_size((struct ptr_list
*)ptr_names
);
572 while ((name
= get_next_ptr_name()))
573 get_ptr_names(NULL
, name
);
576 static void get_function_pointer_callers(struct symbol
*sym
)
580 if (sym
->ctype
.modifiers
& MOD_STATIC
)
581 get_ptr_names(get_base_file(), sym
->ident
->name
);
583 get_ptr_names(NULL
, sym
->ident
->name
);
585 FOR_EACH_PTR(ptr_names
, ptr
) {
586 run_sql(db_callback
, "select call_id, type, parameter, key, value"
587 " from caller_info where function = '%s' order by call_id",
590 } END_FOR_EACH_PTR(ptr
);
592 __free_ptr_list((struct ptr_list
**)&ptr_names
);
593 __free_ptr_list((struct ptr_list
**)&ptr_names_done
);
596 static void match_data_from_db(struct symbol
*sym
)
599 struct state_list
*slist
;
601 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
604 __push_fake_cur_slist();
608 get_direct_callers(sym
);
610 get_function_pointer_callers(sym
);
612 slist
= __pop_fake_cur_slist();
613 merge_slist(&final_states
, slist
);
616 FOR_EACH_PTR(final_states
, sm
) {
618 } END_FOR_EACH_PTR(sm
);
620 free_slist(&final_states
);
623 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
625 struct call_implies_callback
*cb
;
626 struct expression
*arg
= NULL
;
633 type
= atoi(argv
[1]);
634 param
= atoi(argv
[2]);
636 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
637 if (cb
->type
!= type
)
640 arg
= get_argument_from_call_expr(static_call_expr
->args
, param
);
644 cb
->callback(arg
, argv
[3]);
645 } END_FOR_EACH_PTR(cb
);
650 static void match_call_implies(struct expression
*expr
)
652 static_call_expr
= expr
;
653 sql_select_call_implies("function, type, parameter, value", expr
,
654 call_implies_callbacks
);
658 static void print_initializer_list(struct expression_list
*expr_list
,
659 struct symbol
*struct_type
)
661 struct expression
*expr
;
662 struct symbol
*base_type
;
663 char struct_name
[256];
665 FOR_EACH_PTR(expr_list
, expr
) {
666 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
667 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
670 if (expr
->type
!= EXPR_IDENTIFIER
)
672 if (!expr
->expr_ident
)
674 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
676 base_type
= get_type(expr
->ident_expression
);
677 if (!base_type
|| base_type
->type
!= SYM_FN
)
679 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
680 struct_type
->ident
->name
, expr
->expr_ident
->name
);
681 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
683 } END_FOR_EACH_PTR(expr
);
686 static void global_variable(struct symbol
*sym
)
688 struct symbol
*struct_type
;
692 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
694 struct_type
= get_base_type(sym
);
697 if (struct_type
->type
== SYM_ARRAY
) {
698 struct_type
= get_base_type(struct_type
);
702 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
704 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
707 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
709 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
712 static int return_id
;
713 static void match_function_def(struct symbol
*sym
)
719 static void call_return_state_hooks_conditional(struct expression
*expr
)
721 struct returned_state_callback
*cb
;
722 struct range_list
*rl
;
724 int final_pass_orig
= final_pass
;
726 __push_fake_cur_slist();
729 __split_whole_condition(expr
->conditional
);
730 final_pass
= final_pass_orig
;
732 if (get_implied_rl(expr
->cond_true
, &rl
))
733 rl
= cast_rl(cur_func_return_type(), rl
);
735 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_true
)));
736 return_ranges
= show_rl(rl
);
739 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
740 cb
->callback(return_id
, return_ranges
, expr
);
741 } END_FOR_EACH_PTR(cb
);
743 __push_true_states();
744 __use_false_states();
746 if (get_implied_rl(expr
->cond_false
, &rl
))
747 rl
= cast_rl(cur_func_return_type(), rl
);
749 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
->cond_false
)));
750 return_ranges
= show_rl(rl
);
753 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
754 cb
->callback(return_id
, return_ranges
, expr
);
755 } END_FOR_EACH_PTR(cb
);
757 __merge_true_states();
758 __free_fake_cur_slist();
761 static void call_return_state_hooks_compare(struct expression
*expr
)
763 struct returned_state_callback
*cb
;
765 int final_pass_orig
= final_pass
;
767 __push_fake_cur_slist();
770 __split_whole_condition(expr
);
771 final_pass
= final_pass_orig
;
773 return_ranges
= alloc_sname("1");
776 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
777 cb
->callback(return_id
, return_ranges
, expr
);
778 } END_FOR_EACH_PTR(cb
);
780 __push_true_states();
781 __use_false_states();
783 return_ranges
= alloc_sname("0");;
785 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
786 cb
->callback(return_id
, return_ranges
, expr
);
787 } END_FOR_EACH_PTR(cb
);
789 __merge_true_states();
790 __free_fake_cur_slist();
793 static int call_return_state_hooks_split_possible(struct expression
*expr
)
795 struct returned_state_callback
*cb
;
796 struct range_list
*rl
;
799 struct sm_state
*tmp
;
801 int nr_possible
, nr_states
;
805 if (!expr
|| expr_equal_to_param(expr
))
808 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
809 if (!sm
|| !sm
->merged
)
812 /* bail if it gets too complicated */
813 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
814 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
816 * the main thing option_info because we don't want to print a
817 * million lines of output. If someone else, like check_locking.c
818 * wants this data, then it doesn't cause a slow down to provide it.
820 if (option_info
&& nr_possible
>= 100)
822 if (option_info
&& nr_states
* nr_possible
>= 2000)
825 compare_str
= expr_lte_to_param(expr
);
827 FOR_EACH_PTR(sm
->possible
, tmp
) {
832 __push_fake_cur_slist();
834 overwrite_states_using_pool(tmp
);
836 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
837 return_ranges
= show_rl(rl
);
839 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
840 return_ranges
= alloc_sname(buf
);
844 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
845 cb
->callback(return_id
, return_ranges
, expr
);
846 } END_FOR_EACH_PTR(cb
);
848 __free_fake_cur_slist();
849 } END_FOR_EACH_PTR(tmp
);
854 static char *get_return_ranges_str(struct expression
*expr
)
856 struct range_list
*rl
;
862 return alloc_sname("");
863 compare_str
= expr_equal_to_param(expr
);
867 if (get_implied_rl(expr
, &rl
)) {
868 rl
= cast_rl(cur_func_return_type(), rl
);
869 return_ranges
= show_rl(rl
);
871 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
872 return_ranges
= show_rl(rl
);
874 compare_str
= expr_lte_to_param(expr
);
876 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
877 return alloc_sname(buf
);
879 return return_ranges
;
882 static int is_conditional(struct expression
*expr
)
886 if (expr
->type
== EXPR_CONDITIONAL
|| expr
->type
== EXPR_SELECT
)
891 static void call_return_state_hooks(struct expression
*expr
)
893 struct returned_state_callback
*cb
;
897 expr
= strip_expr(expr
);
899 if (is_condition(expr
)) {
900 call_return_state_hooks_compare(expr
);
902 } else if (is_conditional(expr
)) {
903 call_return_state_hooks_conditional(expr
);
905 } else if (call_return_state_hooks_split_possible(expr
)) {
909 return_ranges
= get_return_ranges_str(expr
);
912 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
913 if (nr_states
>= 10000) {
914 match_return_info(return_id
, return_ranges
, expr
);
917 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
918 cb
->callback(return_id
, return_ranges
, expr
);
919 } END_FOR_EACH_PTR(cb
);
922 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
924 struct returned_member_callback
*cb
;
925 struct state_list
*my_slist
;
929 char member_name
[256];
932 type
= get_type(expr
);
933 if (!type
|| type
->type
!= SYM_PTR
)
935 type
= get_real_base_type(type
);
936 if (!type
|| type
->type
!= SYM_STRUCT
)
938 name
= expr_to_var(expr
);
942 member_name
[sizeof(member_name
) - 1] = '\0';
943 strcpy(member_name
, "$$");
946 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
947 my_slist
= get_all_states(cb
->owner
);
948 FOR_EACH_PTR(my_slist
, sm
) {
949 if (strncmp(sm
->name
, name
, len
) != 0)
951 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
953 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
954 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
955 } END_FOR_EACH_PTR(sm
);
956 free_slist(&my_slist
);
957 } END_FOR_EACH_PTR(cb
);
962 static void reset_memdb(void)
964 mem_sql(NULL
, "delete from caller_info;");
965 mem_sql(NULL
, "delete from return_states;");
966 mem_sql(NULL
, "delete from call_implies;");
969 static void match_end_func_info(struct symbol
*sym
)
971 if (__path_is_null())
973 call_return_state_hooks(NULL
);
978 static void init_memdb(void)
982 const char *schema_files
[] = {
984 "db/caller_info.schema",
985 "db/return_states.schema",
986 "db/type_size.schema",
987 "db/call_implies.schema",
988 "db/function_ptr.schema",
989 "db/local_values.schema",
991 static char buf
[4096];
996 rc
= sqlite3_open(":memory:", &mem_db
);
997 if (rc
!= SQLITE_OK
) {
998 printf("Error starting In-Memory database.");
1002 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
1003 fd
= open_data_file(schema_files
[i
]);
1008 ret
= read(fd
, buf
, sizeof(buf
));
1009 if (ret
== sizeof(buf
)) {
1010 printf("Schema file too large: %s (limit %zd bytes)",
1011 schema_files
[i
], sizeof(buf
));
1014 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
1015 if (rc
!= SQLITE_OK
) {
1016 fprintf(stderr
, "SQL error #2: %s\n", err
);
1017 fprintf(stderr
, "%s\n", buf
);
1022 void open_smatch_db(void)
1031 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
1032 if (rc
!= SQLITE_OK
) {
1039 static void register_common_funcs(void)
1041 struct token
*token
;
1045 if (option_project
== PROJ_NONE
)
1046 strcpy(filename
, "common_functions");
1048 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
1050 token
= get_tokens_file(filename
);
1053 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1055 token
= token
->next
;
1056 while (token_type(token
) != TOKEN_STREAMEND
) {
1057 if (token_type(token
) != TOKEN_IDENT
)
1059 func
= alloc_string(show_ident(token
->ident
));
1060 add_ptr_list(&common_funcs
, func
);
1061 token
= token
->next
;
1063 clear_token_alloc();
1067 void register_definition_db_callbacks(int id
)
1069 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1071 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1072 add_hook(&global_variable
, BASE_HOOK
);
1073 add_hook(&global_variable
, DECLARATION_HOOK
);
1074 add_split_return_callback(match_return_info
);
1075 add_split_return_callback(print_returned_struct_members
);
1076 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1077 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1079 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1080 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1082 register_common_funcs();
1085 void register_db_call_marker(int id
)
1087 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1090 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1092 struct expression
*arg
;
1094 char member_name
[256];
1099 if (expr
->type
!= EXPR_ASSIGNMENT
)
1101 name
= expr_to_var_sym(expr
->left
, sym
);
1104 if (strncmp(key
, "$$", 2) != 0)
1106 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1108 return alloc_string(member_name
);
1111 while (expr
->type
== EXPR_ASSIGNMENT
)
1112 expr
= strip_expr(expr
->right
);
1113 if (expr
->type
!= EXPR_CALL
)
1116 arg
= get_argument_from_call_expr(expr
->args
, param
);
1120 return get_variable_from_key(arg
, key
, sym
);
1123 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1128 if (strcmp(key
, "$$") == 0)
1129 return expr_to_var_sym(arg
, sym
);
1131 if (strcmp(key
, "*$$") == 0) {
1132 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1133 arg
= strip_expr(arg
->unop
);
1134 return expr_to_var_sym(arg
, sym
);
1136 tmp
= expr_to_var_sym(arg
, sym
);
1139 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1141 return alloc_string(buf
);
1145 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1146 arg
= strip_expr(arg
->unop
);
1147 tmp
= expr_to_var_sym(arg
, sym
);
1150 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1151 return alloc_string(buf
);
1154 tmp
= expr_to_var_sym(arg
, sym
);
1157 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1159 return alloc_string(buf
);
1162 const char *get_param_name(struct sm_state
*sm
)
1166 static char buf
[256];
1168 if (!sm
->sym
->ident
)
1171 param_name
= sm
->sym
->ident
->name
;
1172 name_len
= strlen(param_name
);
1174 if (strcmp(sm
->name
, param_name
) == 0) {
1176 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1177 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1178 snprintf(buf
, sizeof(buf
), "$$%s", sm
->name
+ name_len
);
1180 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {