4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
17 #define sql_insert(table, values...) \
21 sm_printf("SQL: insert into " #table " values (" values); \
30 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
32 ALLOCATOR(def_callback
, "definition db hook callbacks");
33 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
34 static struct callback_list
*callbacks
;
36 struct member_info_callback
{
38 void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
);
40 ALLOCATOR(member_info_callback
, "caller_info callbacks");
41 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
42 static struct member_info_cb_list
*member_callbacks
;
44 struct returned_state_callback
{
45 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
, struct state_list
*slist
);
47 ALLOCATOR(returned_state_callback
, "returned state callbacks");
48 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
49 static struct returned_state_cb_list
*returned_state_callbacks
;
51 struct returned_member_callback
{
53 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
55 ALLOCATOR(returned_member_callback
, "returned member callbacks");
56 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
57 static struct returned_member_cb_list
*returned_member_callbacks
;
59 struct call_implies_callback
{
61 void (*callback
)(struct expression
*arg
, char *value
);
63 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
64 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
65 static struct call_implies_cb_list
*call_implies_cb_list
;
67 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
72 if (option_no_db
|| !db
)
75 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
76 if (rc
!= SQLITE_OK
) {
77 fprintf(stderr
, "SQL error #2: %s\n", err
);
78 fprintf(stderr
, "SQL: '%s'\n", sql
);
82 void sql_insert_return_states(int return_id
, const char *return_ranges
,
83 int type
, int param
, const char *key
, const char *value
)
85 sql_insert(return_states
, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
86 get_filename(), get_function(), return_id
, return_ranges
,
87 fn_static(), type
, param
, key
, value
);
90 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
92 struct def_callback
*def_callback
= __alloc_def_callback(0);
94 def_callback
->hook_type
= type
;
95 def_callback
->callback
= callback
;
96 add_ptr_list(&callbacks
, def_callback
);
100 * These call backs are used when the --info option is turned on to print struct
101 * member information. For example foo->bar could have a state in
102 * smatch_extra.c and also check_user.c.
104 void add_member_info_callback(int owner
, void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
106 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
108 member_callback
->owner
= owner
;
109 member_callback
->callback
= callback
;
110 add_ptr_list(&member_callbacks
, member_callback
);
113 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
115 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
117 callback
->callback
= fn
;
118 add_ptr_list(&returned_state_callbacks
, callback
);
121 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
123 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
125 member_callback
->owner
= owner
;
126 member_callback
->callback
= callback
;
127 add_ptr_list(&returned_member_callbacks
, member_callback
);
130 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
132 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
135 cb
->callback
= callback
;
136 add_ptr_list(&call_implies_cb_list
, cb
);
139 static struct symbol
*return_type
;
140 static struct range_list
*return_range_list
;
141 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
146 sm_msg("return type %d", type_positive_bits(return_type
));
147 str_to_rl(return_type
, argv
[0], &return_range_list
);
151 struct range_list
*db_return_vals(struct expression
*expr
)
154 static char sql_filter
[1024];
156 if (expr
->type
!= EXPR_CALL
)
158 if (expr
->fn
->type
!= EXPR_SYMBOL
)
160 return_type
= get_type(expr
);
163 sym
= expr
->fn
->symbol
;
167 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
168 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
169 get_filename(), sym
->ident
->name
);
171 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
175 return_range_list
= NULL
;
176 run_sql(db_return_callback
, "select return from return_values where %s",
178 return return_range_list
;
181 static void match_call_hack(struct expression
*expr
)
186 * we just want to record something in the database so that if we have
187 * two calls like: frob(4); frob(some_unkown); then on the receiving
188 * side we know that sometimes frob is called with unknown parameters.
191 name
= get_fnptr_name(expr
->fn
);
194 sm_msg("info: call_marker '%s' %s", name
, is_static(expr
->fn
) ? "static" : "global");
198 static void print_struct_members(char *fn
, char *global_static
, struct expression
*expr
, int param
, struct state_list
*slist
,
199 void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
205 char printed_name
[256];
208 expr
= strip_expr(expr
);
209 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
210 expr
= strip_expr(expr
->unop
);
214 name
= expr_to_var_sym(expr
, &sym
);
219 FOR_EACH_PTR(slist
, sm
) {
222 if (strncmp(name
, sm
->name
, len
) || sm
->name
[len
] == '\0')
225 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
227 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
228 callback(fn
, global_static
, param
, printed_name
, sm
->state
);
229 } END_FOR_EACH_PTR(sm
);
234 static void match_call_info(struct expression
*expr
)
236 struct member_info_callback
*cb
;
237 struct expression
*arg
;
238 struct state_list
*slist
;
243 name
= get_fnptr_name(expr
->fn
);
247 if (is_static(expr
->fn
))
248 gs
= (char *)"static";
250 gs
= (char *)"global";
252 FOR_EACH_PTR(member_callbacks
, cb
) {
253 slist
= get_all_states(cb
->owner
);
255 FOR_EACH_PTR(expr
->args
, arg
) {
256 print_struct_members(name
, gs
, arg
, i
, slist
, cb
->callback
);
258 } END_FOR_EACH_PTR(arg
);
260 } END_FOR_EACH_PTR(cb
);
265 static int get_param(int param
, char **name
, struct symbol
**sym
)
271 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
273 * this is a temporary hack to work around a bug (I think in sparse?)
274 * 2.6.37-rc1:fs/reiserfs/journal.o
275 * If there is a function definition without parameter name found
276 * after a function implementation then it causes a crash.
280 if (arg
->ident
->name
< (char *)100)
282 if (i
== param
&& arg
->ident
->name
) {
283 *name
= arg
->ident
->name
;
288 } END_FOR_EACH_PTR(arg
);
293 static struct state_list
*final_states
;
294 static int prev_func_id
= -1;
295 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
301 struct symbol
*sym
= NULL
;
302 struct def_callback
*def_callback
;
307 func_id
= atoi(argv
[0]);
309 type
= strtol(argv
[1], NULL
, 10);
310 param
= strtol(argv
[2], NULL
, 10);
314 if (prev_func_id
== -1)
315 prev_func_id
= func_id
;
316 if (func_id
!= prev_func_id
) {
317 merge_slist(&final_states
, __pop_fake_cur_slist());
318 __push_fake_cur_slist();
320 prev_func_id
= func_id
;
323 if (type
== INTERNAL
)
325 if (param
>= 0 && !get_param(param
, &name
, &sym
))
328 FOR_EACH_PTR(callbacks
, def_callback
) {
329 if (def_callback
->hook_type
== type
)
330 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
331 } END_FOR_EACH_PTR(def_callback
);
336 static void get_direct_callers(struct symbol
*sym
)
338 char sql_filter
[1024];
340 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
341 snprintf(sql_filter
, 1024,
342 "file = '%s' and function = '%s' order by function_id;",
343 get_filename(), sym
->ident
->name
);
345 snprintf(sql_filter
, 1024,
346 "function = '%s' and static = 0 order by function_id;",
350 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
351 " where %s", sql_filter
);
354 static char *ptr_name
;
355 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
358 ptr_name
= alloc_string(argv
[0]);
362 static void get_function_pointer_callers(struct symbol
*sym
)
364 char sql_filter
[1024];
366 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
367 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
368 get_filename(), sym
->ident
->name
);
370 snprintf(sql_filter
, 1024, "function = '%s';",
375 run_sql(get_ptr_name
, "select ptr from function_ptr where %s", sql_filter
);
379 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
380 " where function = '%s' order by function_id", ptr_name
);
382 free_string(ptr_name
);
385 static void match_data_from_db(struct symbol
*sym
)
389 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
392 __push_fake_cur_slist();
396 get_direct_callers(sym
);
397 get_function_pointer_callers(sym
);
399 merge_slist(&final_states
, __pop_fake_cur_slist());
401 FOR_EACH_PTR(final_states
, sm
) {
403 } END_FOR_EACH_PTR(sm
);
405 free_slist(&final_states
);
408 static void match_function_assign(struct expression
*expr
)
410 struct expression
*right
= expr
->right
;
415 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
416 right
= strip_expr(right
->unop
);
417 if (right
->type
!= EXPR_SYMBOL
)
419 sym
= get_type(right
);
420 if (!sym
|| sym
->type
!= SYM_FN
)
423 fn_name
= expr_to_var(right
);
424 ptr_name
= get_fnptr_name(expr
->left
);
425 if (!fn_name
|| !ptr_name
)
428 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name
, fn_name
);
431 free_string(fn_name
);
432 free_string(ptr_name
);
435 static struct expression
*call_implies_call_expr
;
436 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
438 struct call_implies_callback
*cb
;
439 struct expression
*arg
= NULL
;
446 type
= atoi(argv
[1]);
447 param
= atoi(argv
[2]);
449 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
450 if (cb
->type
!= type
)
453 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
457 cb
->callback(arg
, argv
[3]);
458 } END_FOR_EACH_PTR(cb
);
463 static void match_call_implies(struct expression
*expr
)
466 static char sql_filter
[1024];
468 if (expr
->fn
->type
!= EXPR_SYMBOL
)
470 sym
= expr
->fn
->symbol
;
474 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
475 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
476 get_filename(), sym
->ident
->name
);
478 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
482 call_implies_call_expr
= expr
;
483 run_sql(call_implies_callbacks
,
484 "select function, type, parameter, value from call_implies where %s",
489 static void print_initializer_list(struct expression_list
*expr_list
,
490 struct symbol
*struct_type
)
492 struct expression
*expr
;
493 struct symbol
*base_type
;
495 FOR_EACH_PTR(expr_list
, expr
) {
496 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
497 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
500 if (expr
->type
!= EXPR_IDENTIFIER
)
502 if (!expr
->expr_ident
)
504 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
506 base_type
= get_type(expr
->ident_expression
);
507 if (!base_type
|| base_type
->type
!= SYM_FN
)
509 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type
->ident
->name
,
510 expr
->expr_ident
->name
,
511 expr
->ident_expression
->symbol_name
->name
);
512 } END_FOR_EACH_PTR(expr
);
515 static void global_variable(struct symbol
*sym
)
517 struct symbol
*struct_type
;
521 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
523 struct_type
= get_base_type(sym
);
526 if (struct_type
->type
== SYM_ARRAY
) {
527 struct_type
= get_base_type(struct_type
);
531 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
533 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
536 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
538 sm_msg("info: return_marker %d '%s' %s", return_id
, return_ranges
,
542 static int return_id
;
543 static void match_function_def(struct symbol
*sym
)
548 static void call_return_state_hooks_compare(struct expression
*expr
)
550 struct returned_state_callback
*cb
;
551 struct state_list
*slist
;
553 int final_pass_orig
= final_pass
;
555 __push_fake_cur_slist();
558 __split_whole_condition(expr
);
559 final_pass
= final_pass_orig
;
561 return_ranges
= alloc_sname("1");
564 slist
= __get_cur_slist();
565 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
566 cb
->callback(return_id
, return_ranges
, expr
, slist
);
567 } END_FOR_EACH_PTR(cb
);
569 __push_true_states();
570 __use_false_states();
572 return_ranges
= alloc_sname("0");;
574 slist
= __get_cur_slist();
575 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
576 cb
->callback(return_id
, return_ranges
, expr
, slist
);
577 } END_FOR_EACH_PTR(cb
);
579 __merge_true_states();
580 __pop_fake_cur_slist();
583 static int call_return_state_hooks_split_possible(struct expression
*expr
)
585 struct returned_state_callback
*cb
;
586 struct state_list
*slist
;
587 struct range_list
*rl
;
590 struct sm_state
*tmp
;
592 int nr_possible
, nr_states
;
594 sm
= get_sm_state_expr(SMATCH_EXTRA
, expr
);
595 if (!sm
|| !sm
->merged
)
598 /* bail if it gets too complicated */
599 nr_possible
= ptr_list_size((struct ptr_list
*)sm
->possible
);
600 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
601 if (nr_possible
>= 100)
603 if (nr_states
* nr_possible
>= 1000)
606 FOR_EACH_PTR(sm
->possible
, tmp
) {
611 __push_fake_cur_slist();
613 overwrite_states_using_pool(tmp
);
615 rl
= cast_rl(cur_func_return_type(), estate_rl(tmp
->state
));
616 return_ranges
= show_rl(rl
);
619 slist
= __get_cur_slist();
620 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
621 cb
->callback(return_id
, return_ranges
, expr
, slist
);
622 } END_FOR_EACH_PTR(cb
);
624 __pop_fake_cur_slist();
625 } END_FOR_EACH_PTR(tmp
);
630 static void call_return_state_hooks(struct expression
*expr
)
632 struct returned_state_callback
*cb
;
633 struct state_list
*slist
;
634 struct range_list
*rl
;
638 expr
= strip_expr(expr
);
641 return_ranges
= alloc_sname("");
642 } else if (is_condition(expr
)) {
643 call_return_state_hooks_compare(expr
);
645 } else if (call_return_state_hooks_split_possible(expr
)) {
647 } else if (get_implied_rl(expr
, &rl
)) {
648 rl
= cast_rl(cur_func_return_type(), rl
);
649 return_ranges
= show_rl(rl
);
651 rl
= alloc_whole_rl(cur_func_return_type());
652 return_ranges
= show_rl(rl
);
656 slist
= __get_cur_slist();
657 nr_states
= ptr_list_size((struct ptr_list
*)__get_cur_slist());
658 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
659 if (nr_states
< 10000)
660 cb
->callback(return_id
, return_ranges
, expr
, slist
);
662 cb
->callback(return_id
, return_ranges
, expr
, NULL
);
663 } END_FOR_EACH_PTR(cb
);
666 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
668 struct returned_member_callback
*cb
;
669 struct state_list
*my_slist
;
673 char member_name
[256];
676 type
= get_type(expr
);
677 if (!type
|| type
->type
!= SYM_PTR
)
679 type
= get_real_base_type(type
);
680 if (!type
|| type
->type
!= SYM_STRUCT
)
682 name
= expr_to_var(expr
);
686 member_name
[sizeof(member_name
) - 1] = '\0';
687 strcpy(member_name
, "$$");
690 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
691 my_slist
= get_all_states_slist(cb
->owner
, slist
);
692 FOR_EACH_PTR(my_slist
, sm
) {
693 if (strncmp(sm
->name
, name
, len
) != 0)
695 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
697 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
698 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
699 } END_FOR_EACH_PTR(sm
);
700 free_slist(&my_slist
);
701 } END_FOR_EACH_PTR(cb
);
706 static void match_end_func_info(struct symbol
*sym
)
708 if (__path_is_null())
710 call_return_state_hooks(NULL
);
713 void open_smatch_db(void)
715 #ifdef SQLITE_OPEN_READONLY
721 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
722 if (rc
!= SQLITE_OK
) {
733 void register_definition_db_callbacks(int id
)
735 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
738 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
739 add_hook(&match_call_hack
, FUNCTION_CALL_HOOK
);
740 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
741 add_hook(&match_function_assign
, GLOBAL_ASSIGNMENT_HOOK
);
742 add_hook(&global_variable
, BASE_HOOK
);
743 add_hook(&global_variable
, DECLARATION_HOOK
);
744 add_returned_state_callback(match_return_info
);
745 add_returned_state_callback(print_returned_struct_members
);
746 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
747 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
753 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
754 add_hook(&match_call_implies
, FUNCTION_CALL_HOOK
);
757 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
)
762 if (strcmp(key
, "$$") == 0)
763 return expr_to_var_sym(arg
, sym
);
765 if (strcmp(key
, "*$$") == 0) {
766 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
767 arg
= strip_expr(arg
->unop
);
768 return expr_to_var_sym(arg
, sym
);
770 tmp
= expr_to_var_sym(arg
, sym
);
773 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
775 return alloc_string(buf
);
779 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
780 arg
= strip_expr(arg
->unop
);
781 tmp
= expr_to_var_sym(arg
, sym
);
784 snprintf(buf
, sizeof(buf
), "%s.%s", tmp
, key
+ 4);
785 return alloc_string(buf
);
788 tmp
= expr_to_var_sym(arg
, sym
);
791 snprintf(buf
, sizeof(buf
), "%s%s", tmp
, key
+ 2);
793 return alloc_string(buf
);