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...) \
25 char *err, *p = buf; \
28 p += snprintf(p, buf + sizeof(buf) - p, \
29 "insert into %s values (", #table); \
30 p += snprintf(p, buf + sizeof(buf) - p, values); \
31 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
32 sm_debug("in-mem: %s\n", buf); \
33 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
34 if (rc != SQLITE_OK) { \
35 fprintf(stderr, "SQL error #2: %s\n", err); \
36 fprintf(stderr, "SQL: '%s'\n", buf); \
42 sm_printf("SQL: insert into " #table " values (" values); \
49 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
51 ALLOCATOR(def_callback
, "definition db hook callbacks");
52 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
53 static struct callback_list
*callbacks
;
55 struct member_info_callback
{
57 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
59 ALLOCATOR(member_info_callback
, "caller_info callbacks");
60 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
61 static struct member_info_cb_list
*member_callbacks
;
63 struct returned_state_callback
{
64 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
);
66 ALLOCATOR(returned_state_callback
, "returned state callbacks");
67 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
68 static struct returned_state_cb_list
*returned_state_callbacks
;
70 struct returned_member_callback
{
72 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
74 ALLOCATOR(returned_member_callback
, "returned member callbacks");
75 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
76 static struct returned_member_cb_list
*returned_member_callbacks
;
78 struct call_implies_callback
{
80 void (*callback
)(struct expression
*arg
, char *value
);
82 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
83 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
84 static struct call_implies_cb_list
*call_implies_cb_list
;
86 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
91 if (option_no_db
|| !db
)
94 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
95 if (rc
!= SQLITE_OK
) {
96 fprintf(stderr
, "SQL error #2: %s\n", err
);
97 fprintf(stderr
, "SQL: '%s'\n", sql
);
101 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
106 rc
= sqlite3_exec(mem_db
, sql
, callback
, 0, &err
);
107 if (rc
!= SQLITE_OK
) {
108 fprintf(stderr
, "SQL error #2: %s\n", err
);
109 fprintf(stderr
, "SQL: '%s'\n", sql
);
113 void sql_insert_return_states(int return_id
, const char *return_ranges
,
114 int type
, int param
, const char *key
, const char *value
)
116 if (key
&& strlen(key
) >= 80)
118 sql_insert(return_states
, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
119 get_base_file(), get_function(), (unsigned long)__inline_fn
,
120 return_id
, return_ranges
, fn_static(), type
, param
, key
, value
);
123 static struct string_list
*common_funcs
;
124 static int is_common_function(const char *fn
)
128 if (strncmp(fn
, "__builtin_", 10) == 0)
131 FOR_EACH_PTR(common_funcs
, tmp
) {
132 if (strcmp(tmp
, fn
) == 0)
134 } END_FOR_EACH_PTR(tmp
);
139 void sql_insert_caller_info(struct expression
*call
, int type
,
140 int param
, const char *key
, const char *value
)
144 if (!option_info
&& !__inline_call
)
147 if (key
&& strlen(key
) >= 80)
150 fn
= get_fnptr_name(call
->fn
);
156 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
157 get_base_file(), get_function(), fn
, (unsigned long)call
,
158 is_static(call
->fn
), type
, param
, key
, value
);
164 if (is_common_function(fn
))
167 sm_msg("SQL_caller_info: insert into caller_info values ("
168 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
169 get_base_file(), get_function(), fn
, is_static(call
->fn
),
170 type
, param
, key
, value
);
175 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
177 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_base_file(), fn
,
181 void sql_insert_return_values(const char *return_values
)
183 sql_insert(return_values
, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
184 get_function(), (unsigned long)__inline_fn
, fn_static(),
188 void sql_insert_call_implies(int type
, int param
, int value
)
190 sql_insert(call_implies
, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
191 get_function(), (unsigned long)__inline_fn
, fn_static(),
195 void sql_insert_type_size(const char *member
, int size
)
197 sql_insert(type_size
, "'%s', '%s', %d", get_base_file(), member
, size
);
200 void sql_insert_local_values(const char *name
, const char *value
)
202 sql_insert(local_values
, "'%s', '%s', '%s'", get_base_file(), name
, value
);
205 static char *get_static_filter(struct symbol
*sym
)
207 static char sql_filter
[1024];
209 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
210 snprintf(sql_filter
, sizeof(sql_filter
),
211 "file = '%s' and function = '%s' and static = '1'",
212 get_base_file(), sym
->ident
->name
);
214 snprintf(sql_filter
, sizeof(sql_filter
),
215 "function = '%s' and static = '0'", sym
->ident
->name
);
221 static int row_count
;
222 static int get_row_count(void *unused
, int argc
, char **argv
, char **azColName
)
226 row_count
= atoi(argv
[0]);
230 void sql_select_return_states(const char *cols
, struct expression
*call
,
231 int (*callback
)(void*, int, char**, char**))
233 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
236 if (inlinable(call
->fn
)) {
238 "select %s from return_states where call_id = '%lu';",
239 cols
, (unsigned long)call
);
244 run_sql(get_row_count
, "select count(*) from return_states where %s;",
245 get_static_filter(call
->fn
->symbol
));
246 if (row_count
> 1000)
249 run_sql(callback
, "select %s from return_states where %s;",
250 cols
, get_static_filter(call
->fn
->symbol
));
253 void sql_select_call_implies(const char *cols
, struct expression
*call
,
254 int (*callback
)(void*, int, char**, char**))
256 if (call
->fn
->type
!= EXPR_SYMBOL
|| !call
->fn
->symbol
)
259 if (inlinable(call
->fn
)) {
261 "select %s from call_implies where call_id = '%lu';",
262 cols
, (unsigned long)call
);
266 run_sql(callback
, "select %s from call_implies where %s;",
267 cols
, get_static_filter(call
->fn
->symbol
));
270 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
271 int (*callback
)(void*, int, char**, char**))
274 mem_sql(callback
, "select %s from caller_info where call_id = %lu;",
275 cols
, (unsigned long)__inline_fn
);
280 "select %s from caller_info where %s order by call_id;",
281 cols
, get_static_filter(sym
));
284 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
286 struct def_callback
*def_callback
= __alloc_def_callback(0);
288 def_callback
->hook_type
= type
;
289 def_callback
->callback
= callback
;
290 add_ptr_list(&callbacks
, def_callback
);
294 * These call backs are used when the --info option is turned on to print struct
295 * member information. For example foo->bar could have a state in
296 * smatch_extra.c and also check_user.c.
298 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
300 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
302 member_callback
->owner
= owner
;
303 member_callback
->callback
= callback
;
304 add_ptr_list(&member_callbacks
, member_callback
);
307 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
))
309 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
311 callback
->callback
= fn
;
312 add_ptr_list(&returned_state_callbacks
, callback
);
315 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
317 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
319 member_callback
->owner
= owner
;
320 member_callback
->callback
= callback
;
321 add_ptr_list(&returned_member_callbacks
, member_callback
);
324 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
326 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
329 cb
->callback
= callback
;
330 add_ptr_list(&call_implies_cb_list
, cb
);
333 static struct symbol
*return_type
;
334 static struct range_list
*return_range_list
;
335 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
337 struct range_list
*rl
;
341 str_to_rl(return_type
, argv
[0], &rl
);
342 return_range_list
= rl_union(return_range_list
, rl
);
346 struct range_list
*db_return_vals(struct expression
*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 struct expression
*call_implies_call_expr
;
624 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
626 struct call_implies_callback
*cb
;
627 struct expression
*arg
= NULL
;
634 type
= atoi(argv
[1]);
635 param
= atoi(argv
[2]);
637 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
638 if (cb
->type
!= type
)
641 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
645 cb
->callback(arg
, argv
[3]);
646 } END_FOR_EACH_PTR(cb
);
651 static void match_call_implies(struct expression
*expr
)
653 call_implies_call_expr
= expr
;
654 sql_select_call_implies("function, type, parameter, value", expr
,
655 call_implies_callbacks
);
659 static void print_initializer_list(struct expression_list
*expr_list
,
660 struct symbol
*struct_type
)
662 struct expression
*expr
;
663 struct symbol
*base_type
;
664 char struct_name
[256];
666 FOR_EACH_PTR(expr_list
, expr
) {
667 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
668 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
671 if (expr
->type
!= EXPR_IDENTIFIER
)
673 if (!expr
->expr_ident
)
675 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
677 base_type
= get_type(expr
->ident_expression
);
678 if (!base_type
|| base_type
->type
!= SYM_FN
)
680 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
681 struct_type
->ident
->name
, expr
->expr_ident
->name
);
682 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
684 } END_FOR_EACH_PTR(expr
);
687 static void global_variable(struct symbol
*sym
)
689 struct symbol
*struct_type
;
693 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
695 struct_type
= get_base_type(sym
);
698 if (struct_type
->type
== SYM_ARRAY
) {
699 struct_type
= get_base_type(struct_type
);
703 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
705 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
708 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
)
710 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
713 static int return_id
;
714 static void match_function_def(struct symbol
*sym
)
720 static void call_return_state_hooks_compare(struct expression
*expr
)
722 struct returned_state_callback
*cb
;
724 int final_pass_orig
= final_pass
;
726 __push_fake_cur_slist();
729 __split_whole_condition(expr
);
730 final_pass
= final_pass_orig
;
732 return_ranges
= alloc_sname("1");
735 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
736 cb
->callback(return_id
, return_ranges
, expr
);
737 } END_FOR_EACH_PTR(cb
);
739 __push_true_states();
740 __use_false_states();
742 return_ranges
= alloc_sname("0");;
744 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
745 cb
->callback(return_id
, return_ranges
, expr
);
746 } END_FOR_EACH_PTR(cb
);
748 __merge_true_states();
749 __free_fake_cur_slist();
752 static int call_return_state_hooks_split_possible(struct expression
*expr
)
754 struct returned_state_callback
*cb
;
755 struct range_list
*rl
;
758 struct sm_state
*tmp
;
760 int nr_possible
, nr_states
;
764 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
765 if (!sm
|| !sm
->merged
)
768 /* bail if it gets too complicated */
769 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
770 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
772 * the main thing option_info because we don't want to print a
773 * million lines of output. If someone else, like check_locking.c
774 * wants this data, then it doesn't cause a slow down to provide it.
776 if (option_info
&& nr_possible
>= 100)
778 if (option_info
&& nr_states
* nr_possible
>= 2000)
781 compare_str
= expr_lte_to_param(expr
);
783 FOR_EACH_PTR(sm
->possible
, tmp
) {
788 __push_fake_cur_slist();
790 overwrite_states_using_pool(tmp
);
792 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
793 return_ranges
= show_rl(rl
);
795 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
796 return_ranges
= alloc_sname(buf
);
800 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
801 cb
->callback(return_id
, return_ranges
, expr
);
802 } END_FOR_EACH_PTR(cb
);
804 __free_fake_cur_slist();
805 } END_FOR_EACH_PTR(tmp
);
810 static char *get_return_ranges_str(struct expression
*expr
)
812 struct range_list
*rl
;
818 return alloc_sname("");
819 compare_str
= expr_equal_to_param(expr
);
823 if (get_implied_rl(expr
, &rl
)) {
824 rl
= cast_rl(cur_func_return_type(), rl
);
825 return_ranges
= show_rl(rl
);
827 rl
= cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr
)));
828 return_ranges
= show_rl(rl
);
830 compare_str
= expr_lte_to_param(expr
);
832 snprintf(buf
, sizeof(buf
), "%s%s", return_ranges
, compare_str
);
833 return alloc_sname(buf
);
835 return return_ranges
;
838 static void call_return_state_hooks(struct expression
*expr
)
840 struct returned_state_callback
*cb
;
844 expr
= strip_expr(expr
);
846 if (is_condition(expr
)) {
847 call_return_state_hooks_compare(expr
);
849 } else if (call_return_state_hooks_split_possible(expr
)) {
853 return_ranges
= get_return_ranges_str(expr
);
856 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
857 if (nr_states
>= 10000) {
858 match_return_info(return_id
, return_ranges
, expr
);
861 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
862 cb
->callback(return_id
, return_ranges
, expr
);
863 } END_FOR_EACH_PTR(cb
);
866 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
)
868 struct returned_member_callback
*cb
;
869 struct state_list
*my_slist
;
873 char member_name
[256];
876 type
= get_type(expr
);
877 if (!type
|| type
->type
!= SYM_PTR
)
879 type
= get_real_base_type(type
);
880 if (!type
|| type
->type
!= SYM_STRUCT
)
882 name
= expr_to_var(expr
);
886 member_name
[sizeof(member_name
) - 1] = '\0';
887 strcpy(member_name
, "$$");
890 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
891 my_slist
= get_all_states(cb
->owner
);
892 FOR_EACH_PTR(my_slist
, sm
) {
893 if (strncmp(sm
->name
, name
, len
) != 0)
895 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
897 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
898 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
899 } END_FOR_EACH_PTR(sm
);
900 free_slist(&my_slist
);
901 } END_FOR_EACH_PTR(cb
);
906 static void reset_memdb(void)
908 mem_sql(NULL
, "delete from caller_info;");
909 mem_sql(NULL
, "delete from return_states;");
910 mem_sql(NULL
, "delete from call_implies;");
911 mem_sql(NULL
, "delete from return_values;");
914 static void match_end_func_info(struct symbol
*sym
)
916 if (__path_is_null())
918 call_return_state_hooks(NULL
);
923 static void init_memdb(void)
927 const char *schema_files
[] = {
929 "db/caller_info.schema",
930 "db/return_states.schema",
931 "db/type_size.schema",
932 "db/call_implies.schema",
933 "db/function_ptr.schema",
934 "db/return_values.schema",
935 "db/local_values.schema",
937 static char buf
[4096];
942 rc
= sqlite3_open(":memory:", &mem_db
);
943 if (rc
!= SQLITE_OK
) {
944 printf("Error starting In-Memory database.");
948 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
949 fd
= open_data_file(schema_files
[i
]);
952 ret
= read(fd
, buf
, sizeof(buf
));
953 if (ret
== sizeof(buf
)) {
954 printf("Schema file too large: %s (limit %zd bytes)",
955 schema_files
[i
], sizeof(buf
));
958 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
959 if (rc
!= SQLITE_OK
) {
960 fprintf(stderr
, "SQL error #2: %s\n", err
);
961 fprintf(stderr
, "%s\n", buf
);
966 void open_smatch_db(void)
975 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
976 if (rc
!= SQLITE_OK
) {
983 static void register_common_funcs(void)
989 if (option_project
== PROJ_NONE
)
990 strcpy(filename
, "common_functions");
992 snprintf(filename
, 256, "%s.common_functions", option_project_str
);
994 token
= get_tokens_file(filename
);
997 if (token_type(token
) != TOKEN_STREAMBEGIN
)
1000 while (token_type(token
) != TOKEN_STREAMEND
) {
1001 if (token_type(token
) != TOKEN_IDENT
)
1003 func
= alloc_string(show_ident(token
->ident
));
1004 add_ptr_list(&common_funcs
, func
);
1005 token
= token
->next
;
1007 clear_token_alloc();
1011 void register_definition_db_callbacks(int id
)
1013 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1015 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1016 add_hook(&global_variable
, BASE_HOOK
);
1017 add_hook(&global_variable
, DECLARATION_HOOK
);
1018 add_returned_state_callback(match_return_info
);
1019 add_returned_state_callback(print_returned_struct_members
);
1020 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
1021 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
1023 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
1024 add_hook(&match_call_implies
, CALL_HOOK_AFTER_INLINE
);
1026 register_common_funcs();
1029 void register_db_call_marker(int id
)
1031 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
1034 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
)
1036 struct expression
*arg
;
1038 char member_name
[256];
1043 if (expr
->type
!= EXPR_ASSIGNMENT
)
1045 name
= expr_to_var_sym(expr
->left
, sym
);
1048 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1050 return alloc_string(member_name
);
1053 while (expr
->type
== EXPR_ASSIGNMENT
)
1054 expr
= strip_expr(expr
->right
);
1055 if (expr
->type
!= EXPR_CALL
)
1058 arg
= get_argument_from_call_expr(expr
->args
, param
);
1062 return get_variable_from_key(arg
, key
, sym
);
1065 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
1070 if (strcmp(key
, "$$") == 0)
1071 return expr_to_var_sym(arg
, sym
);
1073 if (strcmp(key
, "*$$") == 0) {
1074 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1075 arg
= strip_expr(arg
->unop
);
1076 return expr_to_var_sym(arg
, sym
);
1078 tmp
= expr_to_var_sym(arg
, sym
);
1081 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1083 return alloc_string(buf
);
1087 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1088 arg
= strip_expr(arg
->unop
);
1089 tmp
= expr_to_var_sym(arg
, sym
);
1092 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
1093 return alloc_string(buf
);
1096 tmp
= expr_to_var_sym(arg
, sym
);
1099 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
1101 return alloc_string(buf
);
1104 const char *get_param_name(struct sm_state
*sm
)
1108 static char buf
[256];
1110 if (!sm
->sym
->ident
)
1113 param_name
= sm
->sym
->ident
->name
;
1114 name_len
= strlen(param_name
);
1116 if (strcmp(sm
->name
, param_name
) == 0) {
1118 } else if (sm
->name
[name_len
] == '-' && /* check for '-' from "->" */
1119 strncmp(sm
->name
, param_name
, name_len
) == 0) {
1120 snprintf(buf
, sizeof(buf
), "$$%s", sm
->name
+ name_len
);
1122 } else if (sm
->name
[0] == '*' && strcmp(sm
->name
+ 1, param_name
) == 0) {