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"
21 void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
);
23 ALLOCATOR(def_callback
, "definition db hook callbacks");
24 DECLARE_PTR_LIST(callback_list
, struct def_callback
);
25 static struct callback_list
*callbacks
;
27 struct member_info_callback
{
29 void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
);
31 ALLOCATOR(member_info_callback
, "caller_info callbacks");
32 DECLARE_PTR_LIST(member_info_cb_list
, struct member_info_callback
);
33 static struct member_info_cb_list
*member_callbacks
;
35 struct returned_state_callback
{
36 void (*callback
)(int return_id
, char *return_ranges
, struct expression
*return_expr
, struct state_list
*slist
);
38 ALLOCATOR(returned_state_callback
, "returned state callbacks");
39 DECLARE_PTR_LIST(returned_state_cb_list
, struct returned_state_callback
);
40 static struct returned_state_cb_list
*returned_state_callbacks
;
42 struct returned_member_callback
{
44 void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
);
46 ALLOCATOR(returned_member_callback
, "returned member callbacks");
47 DECLARE_PTR_LIST(returned_member_cb_list
, struct returned_member_callback
);
48 static struct returned_member_cb_list
*returned_member_callbacks
;
50 struct call_implies_callback
{
52 void (*callback
)(struct expression
*arg
, char *value
);
54 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
55 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
56 static struct call_implies_cb_list
*call_implies_cb_list
;
59 int get_return_id(void)
64 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
69 if (option_no_db
|| !db
)
72 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
73 if (rc
!= SQLITE_OK
) {
74 fprintf(stderr
, "SQL error #2: %s\n", err
);
75 fprintf(stderr
, "SQL: '%s'\n", sql
);
79 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
81 struct def_callback
*def_callback
= __alloc_def_callback(0);
83 def_callback
->hook_type
= type
;
84 def_callback
->callback
= callback
;
85 add_ptr_list(&callbacks
, def_callback
);
89 * These call backs are used when the --info option is turned on to print struct
90 * member information. For example foo->bar could have a state in
91 * smatch_extra.c and also check_user.c.
93 void add_member_info_callback(int owner
, void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
95 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
97 member_callback
->owner
= owner
;
98 member_callback
->callback
= callback
;
99 add_ptr_list(&member_callbacks
, member_callback
);
102 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
, struct state_list
*slist
))
104 struct returned_state_callback
*callback
= __alloc_returned_state_callback(0);
106 callback
->callback
= fn
;
107 add_ptr_list(&returned_state_callbacks
, callback
);
110 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
))
112 struct returned_member_callback
*member_callback
= __alloc_returned_member_callback(0);
114 member_callback
->owner
= owner
;
115 member_callback
->callback
= callback
;
116 add_ptr_list(&returned_member_callbacks
, member_callback
);
119 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
121 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
124 cb
->callback
= callback
;
125 add_ptr_list(&call_implies_cb_list
, cb
);
128 static struct symbol
*return_type
;
129 static struct range_list
*return_range_list
;
130 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
135 sm_msg("return type %d", type_positive_bits(return_type
));
136 parse_value_ranges_type(return_type
, argv
[0], &return_range_list
);
140 struct range_list
*db_return_vals(struct expression
*expr
)
143 static char sql_filter
[1024];
145 if (expr
->type
!= EXPR_CALL
)
147 if (expr
->fn
->type
!= EXPR_SYMBOL
)
149 return_type
= get_type(expr
);
152 sym
= expr
->fn
->symbol
;
156 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
157 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
158 get_filename(), sym
->ident
->name
);
160 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
164 return_range_list
= NULL
;
165 run_sql(db_return_callback
, "select return from return_values where %s",
167 return return_range_list
;
170 static void match_call_hack(struct expression
*expr
)
175 * we just want to record something in the database so that if we have
176 * two calls like: frob(4); frob(some_unkown); then on the receiving
177 * side we know that sometimes frob is called with unknown parameters.
180 name
= get_fnptr_name(expr
->fn
);
183 sm_msg("info: call_marker '%s' %s", name
, is_static(expr
->fn
) ? "static" : "global");
187 static void print_struct_members(char *fn
, char *global_static
, struct expression
*expr
, int param
, struct state_list
*slist
,
188 void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
194 char printed_name
[256];
197 expr
= strip_expr(expr
);
198 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
199 expr
= strip_expr(expr
->unop
);
203 name
= get_variable_from_expr(expr
, &sym
);
208 FOR_EACH_PTR(slist
, sm
) {
211 if (strncmp(name
, sm
->name
, len
) || sm
->name
[len
] == '\0')
214 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
216 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
217 callback(fn
, global_static
, param
, printed_name
, sm
->state
);
218 } END_FOR_EACH_PTR(sm
);
223 static void match_call_info(struct expression
*expr
)
225 struct member_info_callback
*cb
;
226 struct expression
*arg
;
227 struct state_list
*slist
;
232 name
= get_fnptr_name(expr
->fn
);
236 if (is_static(expr
->fn
))
237 gs
= (char *)"static";
239 gs
= (char *)"global";
241 FOR_EACH_PTR(member_callbacks
, cb
) {
242 slist
= get_all_states(cb
->owner
);
244 FOR_EACH_PTR(expr
->args
, arg
) {
245 print_struct_members(name
, gs
, arg
, i
, slist
, cb
->callback
);
247 } END_FOR_EACH_PTR(arg
);
249 } END_FOR_EACH_PTR(cb
);
254 static int get_param(int param
, char **name
, struct symbol
**sym
)
260 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
262 * this is a temporary hack to work around a bug (I think in sparse?)
263 * 2.6.37-rc1:fs/reiserfs/journal.o
264 * If there is a function definition without parameter name found
265 * after a function implementation then it causes a crash.
269 if (arg
->ident
->name
< (char *)100)
271 if (i
== param
&& arg
->ident
->name
) {
272 *name
= arg
->ident
->name
;
277 } END_FOR_EACH_PTR(arg
);
282 static struct state_list
*final_states
;
283 static int prev_func_id
= -1;
284 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
290 struct symbol
*sym
= NULL
;
291 struct def_callback
*def_callback
;
296 func_id
= atoi(argv
[0]);
298 type
= strtol(argv
[1], NULL
, 10);
299 param
= strtol(argv
[2], NULL
, 10);
303 if (prev_func_id
== -1)
304 prev_func_id
= func_id
;
305 if (func_id
!= prev_func_id
) {
306 merge_slist(&final_states
, __pop_fake_cur_slist());
307 __push_fake_cur_slist();
309 prev_func_id
= func_id
;
312 if (type
== INTERNAL
)
314 if (param
>= 0 && !get_param(param
, &name
, &sym
))
317 FOR_EACH_PTR(callbacks
, def_callback
) {
318 if (def_callback
->hook_type
== type
)
319 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
320 } END_FOR_EACH_PTR(def_callback
);
325 static void get_direct_callers(struct symbol
*sym
)
327 char sql_filter
[1024];
329 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
330 snprintf(sql_filter
, 1024,
331 "file = '%s' and function = '%s' order by function_id;",
332 get_filename(), sym
->ident
->name
);
334 snprintf(sql_filter
, 1024,
335 "function = '%s' and static = 0 order by function_id;",
339 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
340 " where %s", sql_filter
);
343 static char *ptr_name
;
344 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
347 ptr_name
= alloc_string(argv
[0]);
351 static void get_function_pointer_callers(struct symbol
*sym
)
354 run_sql(get_ptr_name
, "select ptr from function_ptr where function = '%s'",
359 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
360 " where function = '%s' order by function_id", ptr_name
);
363 static void match_data_from_db(struct symbol
*sym
)
367 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
370 __push_fake_cur_slist();
374 get_direct_callers(sym
);
375 get_function_pointer_callers(sym
);
377 merge_slist(&final_states
, __pop_fake_cur_slist());
379 FOR_EACH_PTR(final_states
, sm
) {
381 } END_FOR_EACH_PTR(sm
);
383 free_slist(&final_states
);
386 static void match_function_assign(struct expression
*expr
)
388 struct expression
*right
= expr
->right
;
393 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
394 right
= strip_expr(right
->unop
);
395 if (right
->type
!= EXPR_SYMBOL
)
397 sym
= get_type(right
);
398 if (!sym
|| sym
->type
!= SYM_FN
)
401 fn_name
= get_variable_from_expr(right
, NULL
);
402 ptr_name
= get_fnptr_name(expr
->left
);
403 if (!fn_name
|| !ptr_name
)
406 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name
, fn_name
);
409 free_string(fn_name
);
410 free_string(ptr_name
);
413 static struct expression
*call_implies_call_expr
;
414 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
416 struct call_implies_callback
*cb
;
417 struct expression
*arg
= NULL
;
424 type
= atoi(argv
[1]);
425 param
= atoi(argv
[2]);
427 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
428 if (cb
->type
!= type
)
431 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
435 cb
->callback(arg
, argv
[3]);
436 } END_FOR_EACH_PTR(cb
);
441 static void match_call_implies(struct expression
*expr
)
444 static char sql_filter
[1024];
446 if (expr
->fn
->type
!= EXPR_SYMBOL
)
448 sym
= expr
->fn
->symbol
;
452 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
453 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
454 get_filename(), sym
->ident
->name
);
456 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
460 call_implies_call_expr
= expr
;
461 run_sql(call_implies_callbacks
,
462 "select function, type, parameter, value from call_implies where %s",
467 static void print_initializer_list(struct expression_list
*expr_list
,
468 struct symbol
*struct_type
)
470 struct expression
*expr
;
471 struct symbol
*base_type
;
473 FOR_EACH_PTR(expr_list
, expr
) {
474 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
475 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
478 if (expr
->type
!= EXPR_IDENTIFIER
)
480 if (!expr
->expr_ident
)
482 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
484 base_type
= get_type(expr
->ident_expression
);
485 if (!base_type
|| base_type
->type
!= SYM_FN
)
487 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type
->ident
->name
,
488 expr
->expr_ident
->name
,
489 expr
->ident_expression
->symbol_name
->name
);
490 } END_FOR_EACH_PTR(expr
);
493 static void global_variable(struct symbol
*sym
)
495 struct symbol
*struct_type
;
499 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
501 struct_type
= get_base_type(sym
);
504 if (struct_type
->type
== SYM_ARRAY
) {
505 struct_type
= get_base_type(struct_type
);
509 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
511 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
514 static void match_return_info(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
516 sm_msg("info: return_marker %d '%s' %s", return_id
, return_ranges
,
520 static void call_return_state_hooks(struct expression
*expr
)
522 struct returned_state_callback
*cb
;
523 struct state_list
*slist
;
524 struct range_list
*rl
;
528 return_ranges
= alloc_sname("");
529 } else if (get_implied_range_list(expr
, &rl
)) {
530 rl
= cast_rl(cur_func_return_type(), rl
);
531 return_ranges
= show_ranges(rl
);
533 rl
= whole_range_list(cur_func_return_type());
534 return_ranges
= show_ranges(rl
);
537 slist
= __get_cur_slist();
538 FOR_EACH_PTR(returned_state_callbacks
, cb
) {
539 cb
->callback(get_return_id(), return_ranges
, expr
, slist
);
540 } END_FOR_EACH_PTR(cb
);
543 static void print_returned_struct_members(int return_id
, char *return_ranges
, struct expression
*expr
, struct state_list
*slist
)
545 struct returned_member_callback
*cb
;
546 struct state_list
*my_slist
;
550 char member_name
[256];
553 type
= get_type(expr
);
554 if (!type
|| type
->type
!= SYM_PTR
)
556 type
= get_real_base_type(type
);
557 if (!type
|| type
->type
!= SYM_STRUCT
)
559 name
= get_variable_from_expr(expr
, NULL
);
563 member_name
[sizeof(member_name
) - 1] = '\0';
564 strcpy(member_name
, "$$");
567 FOR_EACH_PTR(returned_member_callbacks
, cb
) {
568 my_slist
= get_all_states_slist(cb
->owner
, slist
);
569 FOR_EACH_PTR(my_slist
, sm
) {
570 if (strncmp(sm
->name
, name
, len
) != 0)
572 if (strncmp(sm
->name
+ len
, "->", 2) != 0)
574 strncpy(member_name
+ 2, sm
->name
+ len
, sizeof(member_name
) - 2);
575 cb
->callback(return_id
, return_ranges
, member_name
, sm
->state
);
576 } END_FOR_EACH_PTR(sm
);
577 free_slist(&my_slist
);
578 } END_FOR_EACH_PTR(cb
);
583 static void match_end_func_info(struct symbol
*sym
)
585 if (__path_is_null())
587 call_return_state_hooks(NULL
);
590 static void match_function_def(struct symbol
*sym
)
595 static void match_return(struct expression
*ret_value
)
600 static void match_end_func(struct symbol
*sym
)
605 void open_smatch_db(void)
607 #ifdef SQLITE_OPEN_READONLY
613 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
614 if (rc
!= SQLITE_OK
) {
625 void register_definition_db_callbacks(int id
)
627 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
628 add_hook(&match_return
, RETURN_HOOK
);
629 add_hook(&match_end_func
, END_FUNC_HOOK
);
632 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
633 add_hook(&match_call_hack
, FUNCTION_CALL_HOOK
);
634 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
635 add_hook(&match_function_assign
, GLOBAL_ASSIGNMENT_HOOK
);
636 add_hook(&global_variable
, BASE_HOOK
);
637 add_hook(&global_variable
, DECLARATION_HOOK
);
638 add_returned_state_callback(match_return_info
);
639 add_returned_state_callback(print_returned_struct_members
);
640 add_hook(&call_return_state_hooks
, RETURN_HOOK
);
641 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
647 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
648 add_hook(&match_call_implies
, FUNCTION_CALL_HOOK
);