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 sm_printf("SQL: insert into " #table " values (" values); \
32 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
34 ALLOCATOR(def_callback
, "definition db hook callbacks");
35 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
36 static struct callback_list
*callbacks
;
38 struct member_info_callback
{
40 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
);
42 ALLOCATOR(member_info_callback
, "caller_info callbacks");
43 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
44 static struct member_info_cb_list
*member_callbacks
;
46 struct returned_state_callback
{
47 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
, struct state_list
*slist
);
49 ALLOCATOR(returned_state_callback
, "returned state callbacks");
50 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
51 static struct returned_state_cb_list
*returned_state_callbacks
;
53 struct returned_member_callback
{
55 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
57 ALLOCATOR(returned_member_callback
, "returned member callbacks");
58 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
59 static struct returned_member_cb_list
*returned_member_callbacks
;
61 struct call_implies_callback
{
63 void (*callback
)(struct expression
*arg
, char *value
);
65 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
66 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
67 static struct call_implies_cb_list
*call_implies_cb_list
;
69 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
74 if (option_no_db
|| !db
)
77 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
78 if (rc
!= SQLITE_OK
) {
79 fprintf(stderr
, "SQL error #2: %s\n", err
);
80 fprintf(stderr
, "SQL: '%s'\n", sql
);
84 void sql_insert_return_states(int return_id
, const char *return_ranges
,
85 int type
, int param
, const char *key
, const char *value
)
87 sql_insert(return_states
, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
88 get_filename(), get_function(), return_id
, return_ranges
,
89 fn_static(), type
, param
, key
, value
);
92 void sql_insert_caller_info(struct expression
*call
, int type
,
93 int param
, const char *key
, const char *value
)
100 fn
= get_fnptr_name(call
->fn
);
104 sm_msg("SQL_caller_info: insert into caller_info values ("
105 "'%s', '%s', '%s', %%FUNC_ID%%, %d, %d, %d, '%s', '%s');",
106 get_filename(), get_function(), fn
, is_static(call
->fn
),
107 type
, param
, key
, value
);
112 void sql_insert_function_ptr(const char *fn
, const char *struct_name
)
114 sql_insert(function_ptr
, "'%s', '%s', '%s'", get_filename(), fn
,
118 void sql_insert_return_values(const char *return_values
)
120 sql_insert(return_values
, "'%s', '%s', %d, '%s'", get_filename(),
121 get_function(), fn_static(), return_values
);
124 void sql_insert_call_implies(int type
, int param
, int value
)
126 sql_insert(call_implies
, "'%s', '%s', %d, %d, %d, %d", get_filename(),
127 get_function(), fn_static(), type
, param
, value
);
130 void sql_insert_type_size(const char *member
, int size
)
132 sql_insert(type_size
, "'%s', '%s', %d", get_filename(), member
, size
);
135 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
137 struct def_callback
*def_callback
= __alloc_def_callback(0);
139 def_callback
->hook_type
= type
;
140 def_callback
->callback
= callback
;
141 add_ptr_list(&callbacks
, def_callback
);
145 * These call backs are used when the --info option is turned on to print struct
146 * member information. For example foo->bar could have a state in
147 * smatch_extra.c and also check_user.c.
149 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
151 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
153 member_callback
->owner
= owner
;
154 member_callback
->callback
= callback
;
155 add_ptr_list(&member_callbacks
, member_callback
);
158 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
160 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
162 callback
->callback
= fn
;
163 add_ptr_list(&returned_state_callbacks
, callback
);
166 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
168 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
170 member_callback
->owner
= owner
;
171 member_callback
->callback
= callback
;
172 add_ptr_list(&returned_member_callbacks
, member_callback
);
175 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
177 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
180 cb
->callback
= callback
;
181 add_ptr_list(&call_implies_cb_list
, cb
);
184 static struct symbol
*return_type
;
185 static struct range_list
*return_range_list
;
186 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
191 sm_msg("return type %d", type_positive_bits(return_type
));
192 str_to_rl(return_type
, argv
[0], &return_range_list
);
196 struct range_list
*db_return_vals(struct expression
*expr
)
199 static char sql_filter
[1024];
201 if (expr
->type
!= EXPR_CALL
)
203 if (expr
->fn
->type
!= EXPR_SYMBOL
)
205 return_type
= get_type(expr
);
208 sym
= expr
->fn
->symbol
;
212 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
213 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
214 get_filename(), sym
->ident
->name
);
216 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
220 return_range_list
= NULL
;
221 run_sql(db_return_callback
, "select return from return_values where %s",
223 return return_range_list
;
226 static void match_call_marker(struct expression
*expr
)
229 * we just want to record something in the database so that if we have
230 * two calls like: frob(4); frob(some_unkown); then on the receiving
231 * side we know that sometimes frob is called with unknown parameters.
234 sql_insert_caller_info(expr
, INTERNAL
, -1, "%call_marker%", "");
237 static void print_struct_members(struct expression
*call
, struct expression
*expr
, int param
, struct state_list
*slist
,
238 void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
))
244 char printed_name
[256];
247 expr
= strip_expr(expr
);
248 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
249 expr
= strip_expr(expr
->unop
);
253 name
= expr_to_var_sym(expr
, &sym
);
258 FOR_EACH_PTR(slist
, sm
) {
261 if (strncmp(name
, sm
->name
, len
) || sm
->name
[len
] == '\0')
264 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
266 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
267 callback(call
, param
, printed_name
, sm
->state
);
268 } END_FOR_EACH_PTR(sm
);
273 static void match_call_info(struct expression
*call
)
275 struct member_info_callback
*cb
;
276 struct expression
*arg
;
277 struct state_list
*slist
;
281 name
= get_fnptr_name(call
->fn
);
285 FOR_EACH_PTR(member_callbacks
, cb
) {
286 slist
= get_all_states(cb
->owner
);
288 FOR_EACH_PTR(call
->args
, arg
) {
289 print_struct_members(call
, arg
, i
, slist
, cb
->callback
);
291 } END_FOR_EACH_PTR(arg
);
293 } END_FOR_EACH_PTR(cb
);
298 static int get_param(int param
, char **name
, struct symbol
**sym
)
304 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
306 * this is a temporary hack to work around a bug (I think in sparse?)
307 * 2.6.37-rc1:fs/reiserfs/journal.o
308 * If there is a function definition without parameter name found
309 * after a function implementation then it causes a crash.
313 if (arg
->ident
->name
< (char *)100)
315 if (i
== param
&& arg
->ident
->name
) {
316 *name
= arg
->ident
->name
;
321 } END_FOR_EACH_PTR(arg
);
326 static struct state_list
*final_states
;
327 static int prev_func_id
= -1;
328 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
334 struct symbol
*sym
= NULL
;
335 struct def_callback
*def_callback
;
340 func_id
= atoi(argv
[0]);
342 type
= strtol(argv
[1], NULL
, 10);
343 param
= strtol(argv
[2], NULL
, 10);
347 if (prev_func_id
== -1)
348 prev_func_id
= func_id
;
349 if (func_id
!= prev_func_id
) {
350 merge_slist(&final_states
, __pop_fake_cur_slist());
351 __push_fake_cur_slist();
353 prev_func_id
= func_id
;
356 if (type
== INTERNAL
)
358 if (param
>= 0 && !get_param(param
, &name
, &sym
))
361 FOR_EACH_PTR(callbacks
, def_callback
) {
362 if (def_callback
->hook_type
== type
)
363 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
364 } END_FOR_EACH_PTR(def_callback
);
369 static void get_direct_callers(struct symbol
*sym
)
371 char sql_filter
[1024];
373 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
374 snprintf(sql_filter
, 1024,
375 "file = '%s' and function = '%s' order by function_id;",
376 get_filename(), sym
->ident
->name
);
378 snprintf(sql_filter
, 1024,
379 "function = '%s' and static = 0 order by function_id;",
383 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
384 " where %s", sql_filter
);
387 static char *ptr_name
;
388 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
391 ptr_name
= alloc_string(argv
[0]);
395 static void get_function_pointer_callers(struct symbol
*sym
)
397 char sql_filter
[1024];
399 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
400 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
401 get_filename(), sym
->ident
->name
);
403 snprintf(sql_filter
, 1024, "function = '%s';",
408 run_sql(get_ptr_name
, "select ptr from function_ptr where %s", sql_filter
);
412 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
413 " where function = '%s' order by function_id", ptr_name
);
415 free_string(ptr_name
);
418 static void match_data_from_db(struct symbol
*sym
)
422 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
425 __push_fake_cur_slist();
429 get_direct_callers(sym
);
430 get_function_pointer_callers(sym
);
432 merge_slist(&final_states
, __pop_fake_cur_slist());
434 FOR_EACH_PTR(final_states
, sm
) {
436 } END_FOR_EACH_PTR(sm
);
438 free_slist(&final_states
);
441 static void match_function_assign(struct expression
*expr
)
443 struct expression
*right
= expr
->right
;
448 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
449 right
= strip_expr(right
->unop
);
450 if (right
->type
!= EXPR_SYMBOL
)
452 sym
= get_type(right
);
453 if (!sym
|| sym
->type
!= SYM_FN
)
456 fn_name
= expr_to_var(right
);
457 ptr_name
= get_fnptr_name(expr
->left
);
458 if (!fn_name
|| !ptr_name
)
461 sql_insert_function_ptr(fn_name
, ptr_name
);
464 free_string(fn_name
);
465 free_string(ptr_name
);
468 static struct expression
*call_implies_call_expr
;
469 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
471 struct call_implies_callback
*cb
;
472 struct expression
*arg
= NULL
;
479 type
= atoi(argv
[1]);
480 param
= atoi(argv
[2]);
482 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
483 if (cb
->type
!= type
)
486 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
490 cb
->callback(arg
, argv
[3]);
491 } END_FOR_EACH_PTR(cb
);
496 static void match_call_implies(struct expression
*expr
)
499 static char sql_filter
[1024];
501 if (expr
->fn
->type
!= EXPR_SYMBOL
)
503 sym
= expr
->fn
->symbol
;
507 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
508 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
509 get_filename(), sym
->ident
->name
);
511 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
515 call_implies_call_expr
= expr
;
516 run_sql(call_implies_callbacks
,
517 "select function, type, parameter, value from call_implies where %s",
522 static void print_initializer_list(struct expression_list
*expr_list
,
523 struct symbol
*struct_type
)
525 struct expression
*expr
;
526 struct symbol
*base_type
;
527 char struct_name
[256];
529 FOR_EACH_PTR(expr_list
, expr
) {
530 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
531 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
534 if (expr
->type
!= EXPR_IDENTIFIER
)
536 if (!expr
->expr_ident
)
538 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
540 base_type
= get_type(expr
->ident_expression
);
541 if (!base_type
|| base_type
->type
!= SYM_FN
)
543 snprintf(struct_name
, sizeof(struct_name
), "(struct %s)->%s",
544 struct_type
->ident
->name
, expr
->expr_ident
->name
);
545 sql_insert_function_ptr(expr
->ident_expression
->symbol_name
->name
,
547 } END_FOR_EACH_PTR(expr
);
550 static void global_variable(struct symbol
*sym
)
552 struct symbol
*struct_type
;
556 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
558 struct_type
= get_base_type(sym
);
561 if (struct_type
->type
== SYM_ARRAY
) {
562 struct_type
= get_base_type(struct_type
);
566 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
568 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
571 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
573 sql_insert_return_states(return_id
, return_ranges
, INTERNAL
, -1, "", "");
576 static int return_id
;
577 static void match_function_def(struct symbol
*sym
)
582 static void call_return_state_hooks_compare(struct expression
*expr
)
584 struct returned_state_callback
*cb
;
585 struct state_list
*slist
;
587 int final_pass_orig
= final_pass
;
589 __push_fake_cur_slist();
592 __split_whole_condition(expr
);
593 final_pass
= final_pass_orig
;
595 return_ranges
= alloc_sname("1");
598 slist
= __get_cur_slist();
599 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
600 cb
->callback(return_id
, return_ranges
, expr
, slist
);
601 } END_FOR_EACH_PTR(cb
);
603 __push_true_states();
604 __use_false_states();
606 return_ranges
= alloc_sname("0");;
608 slist
= __get_cur_slist();
609 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
610 cb
->callback(return_id
, return_ranges
, expr
, slist
);
611 } END_FOR_EACH_PTR(cb
);
613 __merge_true_states();
614 __pop_fake_cur_slist();
617 static int call_return_state_hooks_split_possible(struct expression
*expr
)
619 struct returned_state_callback
*cb
;
620 struct state_list
*slist
;
621 struct range_list
*rl
;
624 struct sm_state
*tmp
;
626 int nr_possible
, nr_states
;
628 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
629 if (!sm
|| !sm
->merged
)
632 /* bail if it gets too complicated */
633 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
634 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
635 if (nr_possible
>= 100)
637 if (nr_states
* nr_possible
>= 1000)
640 FOR_EACH_PTR(sm
->possible
, tmp
) {
645 __push_fake_cur_slist();
647 overwrite_states_using_pool(tmp
);
649 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
650 return_ranges
= show_rl(rl
);
653 slist
= __get_cur_slist();
654 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
655 cb
->callback(return_id
, return_ranges
, expr
, slist
);
656 } END_FOR_EACH_PTR(cb
);
658 __pop_fake_cur_slist();
659 } END_FOR_EACH_PTR(tmp
);
664 static void call_return_state_hooks(struct expression
*expr
)
666 struct returned_state_callback
*cb
;
667 struct state_list
*slist
;
668 struct range_list
*rl
;
672 expr
= strip_expr(expr
);
675 return_ranges
= alloc_sname("");
676 } else if (is_condition(expr
)) {
677 call_return_state_hooks_compare(expr
);
679 } else if (call_return_state_hooks_split_possible(expr
)) {
681 } else if (get_implied_rl(expr
, &rl
)) {
682 rl
= cast_rl(cur_func_return_type(), rl
);
683 return_ranges
= show_rl(rl
);
685 rl
= alloc_whole_rl(cur_func_return_type());
686 return_ranges
= show_rl(rl
);
690 slist
= __get_cur_slist();
691 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
692 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
693 if (nr_states
< 10000)
694 cb
->callback(return_id
, return_ranges
, expr
, slist
);
696 cb
->callback(return_id
, return_ranges
, expr
, NULL
);
697 } END_FOR_EACH_PTR(cb
);
700 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
702 struct returned_member_callback
*cb
;
703 struct state_list
*my_slist
;
707 char member_name
[256];
710 type
= get_type(expr
);
711 if (!type
|| type
->type
!= SYM_PTR
)
713 type
= get_real_base_type(type
);
714 if (!type
|| type
->type
!= SYM_STRUCT
)
716 name
= expr_to_var(expr
);
720 member_name
[sizeof(member_name
) - 1] = '\0';
721 strcpy(member_name
, "$$");
724 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
725 my_slist
= get_all_states_slist(cb
->owner
, slist
);
726 FOR_EACH_PTR(my_slist
, sm
) {
727 if (strncmp(sm
->name
, name
, len
) != 0)
729 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
731 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
732 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
733 } END_FOR_EACH_PTR(sm
);
734 free_slist(&my_slist
);
735 } END_FOR_EACH_PTR(cb
);
740 static void match_end_func_info(struct symbol
*sym
)
742 if (__path_is_null())
744 call_return_state_hooks(NULL
);
747 static void init_memdb(void)
751 const char *schema_files
[] = {
753 "db/caller_info.schema",
754 "db/return_states.schema",
755 "db/type_size.schema",
756 "db/call_implies.schema",
757 "db/function_ptr.schema",
758 "db/return_values.schema",
760 static char buf
[4096];
765 rc
= sqlite3_open(":memory:", &mem_db
);
766 if (rc
!= SQLITE_OK
) {
767 printf("Error starting In-Memory database.");
771 for (i
= 0; i
< ARRAY_SIZE(schema_files
); i
++) {
772 fd
= open_data_file(schema_files
[i
]);
775 ret
= read(fd
, buf
, sizeof(buf
));
776 if (ret
== sizeof(buf
)) {
777 printf("Schema file too large: %s (limit %ld bytes)",
778 schema_files
[i
], sizeof(buf
));
781 rc
= sqlite3_exec(mem_db
, buf
, NULL
, 0, &err
);
782 if (rc
!= SQLITE_OK
) {
783 fprintf(stderr
, "SQL error #2: %s\n", err
);
784 fprintf(stderr
, "%s\n", buf
);
789 void open_smatch_db(void)
798 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
799 if (rc
!= SQLITE_OK
) {
806 void register_definition_db_callbacks(int id
)
808 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
811 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
812 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
813 add_hook(&match_function_assign
, GLOBAL_ASSIGNMENT_HOOK
);
814 add_hook(&global_variable
, BASE_HOOK
);
815 add_hook(&global_variable
, DECLARATION_HOOK
);
816 add_returned_state_callback(match_return_info
);
817 add_returned_state_callback(print_returned_struct_members
);
818 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
819 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
825 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
826 add_hook(&match_call_implies
, FUNCTION_CALL_HOOK
);
829 void register_db_call_marker(int id
)
833 add_hook(&match_call_marker
, FUNCTION_CALL_HOOK
);
836 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
841 if (strcmp(key
, "$$") == 0)
842 return expr_to_var_sym(arg
, sym
);
844 if (strcmp(key
, "*$$") == 0) {
845 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
846 arg
= strip_expr(arg
->unop
);
847 return expr_to_var_sym(arg
, sym
);
849 tmp
= expr_to_var_sym(arg
, sym
);
852 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
854 return alloc_string(buf
);
858 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
859 arg
= strip_expr(arg
->unop
);
860 tmp
= expr_to_var_sym(arg
, sym
);
863 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
864 return alloc_string(buf
);
867 tmp
= expr_to_var_sym(arg
, sym
);
870 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
872 return alloc_string(buf
);