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 call_implies_callback
{
37 void (*callback
)(struct expression
*arg
, char *value
);
39 ALLOCATOR(call_implies_callback
, "call_implies callbacks");
40 DECLARE_PTR_LIST(call_implies_cb_list
, struct call_implies_callback
);
41 static struct call_implies_cb_list
*call_implies_cb_list
;
44 int get_return_id(void)
49 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
54 if (option_no_db
|| !db
)
57 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
58 if (rc
!= SQLITE_OK
) {
59 fprintf(stderr
, "SQL error #2: %s\n", err
);
60 fprintf(stderr
, "SQL: '%s'\n", sql
);
64 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
66 struct def_callback
*def_callback
= __alloc_def_callback(0);
68 def_callback
->hook_type
= type
;
69 def_callback
->callback
= callback
;
70 add_ptr_list(&callbacks
, def_callback
);
74 * These call backs are used when the --info option is turned on to print struct
75 * member information. For example foo->bar could have a state in
76 * smatch_extra.c and also check_user.c.
78 void add_member_info_callback(int owner
, void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
80 struct member_info_callback
*member_callback
= __alloc_member_info_callback(0);
82 member_callback
->owner
= owner
;
83 member_callback
->callback
= callback
;
84 add_ptr_list(&member_callbacks
, member_callback
);
87 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
))
89 struct call_implies_callback
*cb
= __alloc_call_implies_callback(0);
92 cb
->callback
= callback
;
93 add_ptr_list(&call_implies_cb_list
, cb
);
96 static struct range_list
*return_range_list
;
97 static int db_return_callback(void *unused
, int argc
, char **argv
, char **azColName
)
101 get_value_ranges(argv
[0], &return_range_list
);
105 struct range_list
*db_return_vals(struct expression
*expr
)
108 static char sql_filter
[1024];
110 if (expr
->type
!= EXPR_CALL
)
112 if (expr
->fn
->type
!= EXPR_SYMBOL
)
114 sym
= expr
->fn
->symbol
;
118 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
119 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
120 get_filename(), sym
->ident
->name
);
122 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
126 return_range_list
= NULL
;
127 run_sql(db_return_callback
, "select return from return_values where %s",
129 return return_range_list
;
132 static void match_call_hack(struct expression
*expr
)
137 * we just want to record something in the database so that if we have
138 * two calls like: frob(4); frob(some_unkown); then on the receiving
139 * side we know that sometimes frob is called with unknown parameters.
142 name
= get_fnptr_name(expr
->fn
);
145 sm_msg("info: call_marker '%s' %s", name
, is_static(expr
->fn
) ? "static" : "global");
149 static void print_struct_members(char *fn
, char *global_static
, struct expression
*expr
, int param
, struct state_list
*slist
,
150 void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
))
156 char printed_name
[256];
159 expr
= strip_expr(expr
);
160 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
161 expr
= strip_expr(expr
->unop
);
165 name
= get_variable_from_expr(expr
, &sym
);
170 FOR_EACH_PTR(slist
, sm
) {
173 if (strncmp(name
, sm
->name
, len
) || sm
->name
[len
] == '\0')
176 snprintf(printed_name
, sizeof(printed_name
), "$$->%s", sm
->name
+ len
+ 1);
178 snprintf(printed_name
, sizeof(printed_name
), "$$%s", sm
->name
+ len
);
179 callback(fn
, global_static
, param
, printed_name
, sm
->state
);
180 } END_FOR_EACH_PTR(sm
);
185 static void match_call_info(struct expression
*expr
)
187 struct member_info_callback
*cb
;
188 struct expression
*arg
;
189 struct state_list
*slist
;
194 name
= get_fnptr_name(expr
->fn
);
198 if (is_static(expr
->fn
))
199 gs
= (char *)"static";
201 gs
= (char *)"global";
203 FOR_EACH_PTR(member_callbacks
, cb
) {
204 slist
= get_all_states(cb
->owner
);
206 FOR_EACH_PTR(expr
->args
, arg
) {
207 print_struct_members(name
, gs
, arg
, i
, slist
, cb
->callback
);
209 } END_FOR_EACH_PTR(arg
);
210 } END_FOR_EACH_PTR(cb
);
216 static int get_param(int param
, char **name
, struct symbol
**sym
)
222 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
224 * this is a temporary hack to work around a bug (I think in sparse?)
225 * 2.6.37-rc1:fs/reiserfs/journal.o
226 * If there is a function definition without parameter name found
227 * after a function implementation then it causes a crash.
231 if (arg
->ident
->name
< (char *)100)
233 if (i
== param
&& arg
->ident
->name
) {
234 *name
= arg
->ident
->name
;
239 } END_FOR_EACH_PTR(arg
);
244 static struct state_list
*final_states
;
245 static int prev_func_id
= -1;
246 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
252 struct symbol
*sym
= NULL
;
253 struct def_callback
*def_callback
;
258 func_id
= atoi(argv
[0]);
260 type
= strtol(argv
[1], NULL
, 10);
261 param
= strtol(argv
[2], NULL
, 10);
265 if (prev_func_id
== -1)
266 prev_func_id
= func_id
;
267 if (func_id
!= prev_func_id
) {
268 merge_slist(&final_states
, __pop_fake_cur_slist());
269 __push_fake_cur_slist();
271 prev_func_id
= func_id
;
274 if (type
== INTERNAL
)
276 if (param
>= 0 && !get_param(param
, &name
, &sym
))
279 FOR_EACH_PTR(callbacks
, def_callback
) {
280 if (def_callback
->hook_type
== type
)
281 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
282 } END_FOR_EACH_PTR(def_callback
);
287 static void get_direct_callers(struct symbol
*sym
)
289 char sql_filter
[1024];
291 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
292 snprintf(sql_filter
, 1024,
293 "file = '%s' and function = '%s' order by function_id;",
294 get_filename(), sym
->ident
->name
);
296 snprintf(sql_filter
, 1024,
297 "function = '%s' and static = 0 order by function_id;",
301 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
302 " where %s", sql_filter
);
305 static char *ptr_name
;
306 static int get_ptr_name(void *unused
, int argc
, char **argv
, char **azColName
)
309 ptr_name
= alloc_string(argv
[0]);
313 static void get_function_pointer_callers(struct symbol
*sym
)
316 run_sql(get_ptr_name
, "select ptr from function_ptr where function = '%s'",
321 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
322 " where function = '%s' order by function_id", ptr_name
);
325 static void match_data_from_db(struct symbol
*sym
)
329 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
332 __push_fake_cur_slist();
336 get_direct_callers(sym
);
337 get_function_pointer_callers(sym
);
339 merge_slist(&final_states
, __pop_fake_cur_slist());
341 FOR_EACH_PTR(final_states
, sm
) {
343 } END_FOR_EACH_PTR(sm
);
345 free_slist(&final_states
);
348 static void match_function_assign(struct expression
*expr
)
350 struct expression
*right
= expr
->right
;
355 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
357 if (right
->type
!= EXPR_SYMBOL
)
359 sym
= get_type(right
);
360 if (!sym
|| sym
->type
!= SYM_FN
)
363 fn_name
= get_variable_from_expr(right
, NULL
);
364 ptr_name
= get_fnptr_name(expr
->left
);
365 if (!fn_name
|| !ptr_name
)
368 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name
, fn_name
);
371 free_string(fn_name
);
372 free_string(ptr_name
);
375 static struct expression
*call_implies_call_expr
;
376 static int call_implies_callbacks(void *unused
, int argc
, char **argv
, char **azColName
)
378 struct call_implies_callback
*cb
;
379 struct expression
*arg
= NULL
;
386 type
= atoi(argv
[1]);
387 param
= atoi(argv
[2]);
389 FOR_EACH_PTR(call_implies_cb_list
, cb
) {
390 if (cb
->type
!= type
)
393 arg
= get_argument_from_call_expr(call_implies_call_expr
->args
, param
);
397 cb
->callback(arg
, argv
[3]);
398 } END_FOR_EACH_PTR(cb
);
403 static void match_call_implies(struct expression
*expr
)
406 static char sql_filter
[1024];
408 if (expr
->fn
->type
!= EXPR_SYMBOL
)
410 sym
= expr
->fn
->symbol
;
414 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
415 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
416 get_filename(), sym
->ident
->name
);
418 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
422 call_implies_call_expr
= expr
;
423 run_sql(call_implies_callbacks
,
424 "select function, type, parameter, value from call_implies where %s",
429 static void print_initializer_list(struct expression_list
*expr_list
,
430 struct symbol
*struct_type
)
432 struct expression
*expr
;
433 struct symbol
*base_type
;
435 FOR_EACH_PTR(expr_list
, expr
) {
436 if (expr
->type
== EXPR_INDEX
&& expr
->idx_expression
&& expr
->idx_expression
->type
== EXPR_INITIALIZER
) {
437 print_initializer_list(expr
->idx_expression
->expr_list
, struct_type
);
440 if (expr
->type
!= EXPR_IDENTIFIER
)
442 if (!expr
->expr_ident
)
444 if (!expr
->ident_expression
|| !expr
->ident_expression
->symbol_name
)
446 base_type
= get_type(expr
->ident_expression
);
447 if (!base_type
|| base_type
->type
!= SYM_FN
)
449 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type
->ident
->name
,
450 expr
->expr_ident
->name
,
451 expr
->ident_expression
->symbol_name
->name
);
452 } END_FOR_EACH_PTR(expr
);
455 static void global_variable(struct symbol
*sym
)
457 struct symbol
*struct_type
;
461 if (!sym
->initializer
|| sym
->initializer
->type
!= EXPR_INITIALIZER
)
463 struct_type
= get_base_type(sym
);
466 if (struct_type
->type
== SYM_ARRAY
) {
467 struct_type
= get_base_type(struct_type
);
471 if (struct_type
->type
!= SYM_STRUCT
|| !struct_type
->ident
)
473 print_initializer_list(sym
->initializer
->expr_list
, struct_type
);
476 static void match_return_info(struct expression
*ret_value
)
478 struct range_list
*rl
;
480 get_implied_range_list(ret_value
, &rl
);
481 sm_msg("info: return_marker %d '%s' %s",
482 get_return_id(), show_ranges(rl
), global_static());
485 static void match_end_func_info(struct symbol
*sym
)
487 if (__path_is_null())
489 sm_msg("info: return_marker %d '' %s", get_return_id(), global_static());
492 static void match_function_def(struct symbol
*sym
)
497 static void match_return(struct expression
*ret_value
)
502 static void match_end_func(struct symbol
*sym
)
507 void open_smatch_db(void)
509 #ifdef SQLITE_OPEN_READONLY
515 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
516 if (rc
!= SQLITE_OK
) {
527 void register_definition_db_callbacks(int id
)
529 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
530 add_hook(&match_return
, RETURN_HOOK
);
531 add_hook(&match_end_func
, END_FUNC_HOOK
);
534 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
535 add_hook(&match_call_hack
, FUNCTION_CALL_HOOK
);
536 add_hook(&match_function_assign
, ASSIGNMENT_HOOK
);
537 add_hook(&global_variable
, BASE_HOOK
);
538 add_hook(&global_variable
, DECLARATION_HOOK
);
539 add_hook(&match_return_info
, RETURN_HOOK
);
540 add_hook(&match_end_func_info
, END_FUNC_HOOK
);
546 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);
547 add_hook(&match_call_implies
, FUNCTION_CALL_HOOK
);