db: caller info needs to record the -1 parameters
[smatch.git] / smatch_db.c
blob1f9a2810d0c40d4b631206446e9817c1ec27510c
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include "smatch.h"
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
17 static sqlite3 *db;
19 struct def_callback {
20 int hook_type;
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 {
28 int owner;
29 void (*callback)(char *fn, 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 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
37 char *err = NULL;
38 int rc;
40 if (option_no_db || !db)
41 return;
43 rc = sqlite3_exec(db, sql, callback, 0, &err);
44 if (rc != SQLITE_OK) {
45 fprintf(stderr, "SQL error #2: %s\n", err);
46 exit(1);
50 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
52 struct def_callback *def_callback = __alloc_def_callback(0);
54 def_callback->hook_type = type;
55 def_callback->callback = callback;
56 add_ptr_list(&callbacks, def_callback);
59 void add_member_info_callback(int owner, void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state))
61 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
63 member_callback->owner = owner;
64 member_callback->callback = callback;
65 add_ptr_list(&member_callbacks, member_callback);
68 static void match_call_hack(struct expression *expr)
70 char *name;
73 * we just want to record something in the database so that if we have
74 * two calls like: frob(4); frob(some_unkown); then on the recieving
75 * side we know that sometimes frob is called with unknown parameters.
78 name = get_fnptr_name(expr->fn);
79 if (!name)
80 return;
81 if (ptr_list_empty(expr->args))
82 return;
83 sm_msg("info: passes param_value '%s' -1 '$$' min-max", name);
84 free_string(name);
87 static void print_struct_members(char *fn, struct expression *expr, int param, struct state_list *slist,
88 void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state))
90 struct sm_state *sm;
91 char *name;
92 struct symbol *sym;
93 int len;
94 char printed_name[256];
95 int is_address = 0;
97 expr = strip_expr(expr);
98 if (expr->type == EXPR_PREOP && expr->op == '&') {
99 expr = strip_expr(expr->unop);
100 is_address = 1;
103 name = get_variable_from_expr(expr, &sym);
104 if (!name || !sym)
105 goto free;
107 len = strlen(name);
108 FOR_EACH_PTR(slist, sm) {
109 if (sm->sym != sym)
110 continue;
111 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
112 continue;
113 if (is_address)
114 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
115 else
116 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
117 callback(fn, param, printed_name, sm->state);
118 } END_FOR_EACH_PTR(sm);
119 free:
120 free_string(name);
123 static void match_call_info(struct expression *expr)
125 struct member_info_callback *cb;
126 struct expression *arg;
127 struct state_list *slist;
128 char *name;
129 int i;
131 name = get_fnptr_name(expr->fn);
132 if (!name)
133 return;
135 FOR_EACH_PTR(member_callbacks, cb) {
136 slist = get_all_states(cb->owner);
137 i = 0;
138 FOR_EACH_PTR(expr->args, arg) {
139 print_struct_members(name, arg, i, slist, cb->callback);
140 i++;
141 } END_FOR_EACH_PTR(arg);
142 } END_FOR_EACH_PTR(cb);
144 free_string(name);
145 free_slist(&slist);
148 static unsigned long call_count;
149 static int db_count_callback(void *unused, int argc, char **argv, char **azColName)
151 call_count += strtoul(argv[0], NULL, 10);
152 return 0;
155 static int get_param(int param, char **name, struct symbol **sym)
157 struct symbol *arg;
158 int i;
160 i = 0;
161 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
163 * this is a temporary hack to work around a bug (I think in sparse?)
164 * 2.6.37-rc1:fs/reiserfs/journal.o
165 * If there is a function definition without parameter name found
166 * after a function implementation then it causes a crash.
167 * int foo() {}
168 * int bar(char *);
170 if (arg->ident->name < (char *)100)
171 continue;
172 if (i == param && arg->ident->name) {
173 *name = arg->ident->name;
174 *sym = arg;
175 return TRUE;
177 i++;
178 } END_FOR_EACH_PTR(arg);
180 return FALSE;
183 static struct state_list *final_states;
184 static int prev_func_id = -1;
185 static int db_callback(void *unused, int argc, char **argv, char **azColName)
187 int func_id;
188 long type;
189 long param;
190 char *name;
191 struct symbol *sym;
192 struct def_callback *def_callback;
194 if (argc != 5)
195 return 0;
197 func_id = atoi(argv[0]);
198 errno = 0;
199 type = strtol(argv[1], NULL, 10);
200 param = strtol(argv[2], NULL, 10);
201 if (errno)
202 return 0;
204 if (prev_func_id == -1)
205 prev_func_id = func_id;
206 if (func_id != prev_func_id) {
207 merge_slist(&final_states, __pop_fake_cur_slist());
208 __push_fake_cur_slist();
209 prev_func_id = func_id;
212 if (param == -1 || !get_param(param, &name, &sym))
213 return 0;
215 FOR_EACH_PTR(callbacks, def_callback) {
216 if (def_callback->hook_type == type)
217 def_callback->callback(name, sym, argv[3], argv[4]);
218 } END_FOR_EACH_PTR(def_callback);
220 return 0;
223 static void get_direct_callers(struct symbol *sym)
225 char sql_filter[1024];
227 if (sym->ctype.modifiers & MOD_STATIC) {
228 snprintf(sql_filter, 1024,
229 "file = '%s' and function = '%s' order by function_id;",
230 get_filename(), sym->ident->name);
231 } else {
232 snprintf(sql_filter, 1024,
233 "function = '%s' order by function_id;",
234 sym->ident->name);
237 run_sql(db_count_callback, "select count(*) from caller_info where %s",
238 sql_filter);
239 if (call_count == 0 || call_count > 100)
240 return;
242 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
243 " where %s", sql_filter);
246 static char *ptr_name;
247 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
249 if (!ptr_name)
250 ptr_name = alloc_string(argv[0]);
251 return 0;
254 static void get_function_pointer_callers(struct symbol *sym)
256 ptr_name = NULL;
257 run_sql(get_ptr_name, "select ptr from function_ptr where function = '%s'",
258 sym->ident->name);
259 if (!ptr_name)
260 return;
262 run_sql(db_count_callback, "select count(*) from caller_info where function = '%s'",
263 ptr_name);
264 if (call_count == 0 || call_count > 100)
265 return;
267 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
268 " where function = '%s' order by function_id", ptr_name);
271 static void match_data_from_db(struct symbol *sym)
273 struct sm_state *sm;
275 if (!sym || !sym->ident || !sym->ident->name)
276 return;
278 __push_fake_cur_slist();
279 prev_func_id = -1;
281 call_count = 0;
282 get_direct_callers(sym);
283 get_function_pointer_callers(sym);
285 merge_slist(&final_states, __pop_fake_cur_slist());
287 if (call_count > 100) {
288 free_slist(&final_states);
289 return;
291 FOR_EACH_PTR(final_states, sm) {
292 __set_sm(sm);
293 } END_FOR_EACH_PTR(sm);
295 free_slist(&final_states);
298 static void match_function_assign(struct expression *expr)
300 struct expression *right = expr->right;
301 struct symbol *sym;
302 char *fn_name;
303 char *ptr_name;
305 if (right->type == EXPR_PREOP && right->op == '&')
306 right = right->unop;
307 if (right->type != EXPR_SYMBOL)
308 return;
309 sym = get_type(right);
310 if (!sym || sym->type != SYM_FN)
311 return;
313 fn_name = get_variable_from_expr(right, NULL);
314 ptr_name = get_fnptr_name(expr->left);
315 if (!fn_name || !ptr_name)
316 goto free;
318 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
320 free:
321 free_string(fn_name);
322 free_string(ptr_name);
325 static void print_initializer_list(struct expression_list *expr_list,
326 struct symbol *struct_type)
328 struct expression *expr;
329 struct symbol *base_type;
331 FOR_EACH_PTR(expr_list, expr) {
332 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
333 print_initializer_list(expr->idx_expression->expr_list, struct_type);
334 continue;
336 if (expr->type != EXPR_IDENTIFIER)
337 continue;
338 if (!expr->expr_ident)
339 continue;
340 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
341 continue;
342 base_type = get_type(expr->ident_expression);
343 if (!base_type || base_type->type != SYM_FN)
344 continue;
345 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type->ident->name,
346 expr->expr_ident->name,
347 expr->ident_expression->symbol_name->name);
348 } END_FOR_EACH_PTR(expr);
352 static void global_variable(struct symbol *sym)
354 struct symbol *struct_type;
356 if (!sym->ident)
357 return;
358 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
359 return;
360 struct_type = get_base_type(sym);
361 if (!struct_type)
362 return;
363 if (struct_type->type == SYM_ARRAY) {
364 struct_type = get_base_type(struct_type);
365 if (!struct_type)
366 return;
367 sm_msg("here in sets_fn_ptr %d %p", struct_type->type, struct_type->ident);
369 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
370 return;
371 print_initializer_list(sym->initializer->expr_list, struct_type);
374 void open_smatch_db(void)
376 #ifdef SQLITE_OPEN_READONLY
377 int rc;
379 if (option_no_db)
380 return;
382 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
383 if (rc != SQLITE_OK) {
384 option_no_db = 1;
385 return;
387 return;
388 #else
389 option_no_db = 1;
390 return;
391 #endif
394 void register_definition_db_callbacks(int id)
396 if (option_info) {
397 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
398 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
399 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
400 add_hook(&global_variable, BASE_HOOK);
401 add_hook(&global_variable, DECLARATION_HOOK);
404 if (option_no_db)
405 return;
407 add_hook(&match_data_from_db, FUNC_DEF_HOOK);