constraints: make get_common_relationship() take an estate
[smatch.git] / smatch_db.c
blobe802dfa089e0b59ed08ed1bc29c3b588ca8980a7
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 struct range_list *return_range_list;
69 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
71 struct range_list *rl = NULL;
73 if (argc != 1)
74 return 0;
76 get_value_ranges(argv[0], &rl);
77 return_range_list = range_list_union(return_range_list, rl);
79 return 0;
82 struct range_list *db_return_vals(struct expression *expr)
84 struct symbol *sym;
85 static char sql_filter[1024];
87 if (expr->type != EXPR_CALL)
88 return NULL;
89 if (expr->fn->type != EXPR_SYMBOL)
90 return NULL;
91 sym = expr->fn->symbol;
92 if (!sym)
93 return NULL;
95 if (sym->ctype.modifiers & MOD_STATIC) {
96 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
97 get_filename(), sym->ident->name);
98 } else {
99 snprintf(sql_filter, 1024, "function = '%s';", sym->ident->name);
102 return_range_list = NULL;
103 run_sql(db_return_callback, "select value from return_info where %s",
104 sql_filter);
105 return return_range_list;
108 static void match_call_hack(struct expression *expr)
110 char *name;
113 * we just want to record something in the database so that if we have
114 * two calls like: frob(4); frob(some_unkown); then on the recieving
115 * side we know that sometimes frob is called with unknown parameters.
118 name = get_fnptr_name(expr->fn);
119 if (!name)
120 return;
121 if (ptr_list_empty(expr->args))
122 return;
123 sm_msg("info: passes param_value '%s' -1 '$$' min-max", name);
124 free_string(name);
127 static void print_struct_members(char *fn, struct expression *expr, int param, struct state_list *slist,
128 void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state))
130 struct sm_state *sm;
131 char *name;
132 struct symbol *sym;
133 int len;
134 char printed_name[256];
135 int is_address = 0;
137 expr = strip_expr(expr);
138 if (expr->type == EXPR_PREOP && expr->op == '&') {
139 expr = strip_expr(expr->unop);
140 is_address = 1;
143 name = get_variable_from_expr(expr, &sym);
144 if (!name || !sym)
145 goto free;
147 len = strlen(name);
148 FOR_EACH_PTR(slist, sm) {
149 if (sm->sym != sym)
150 continue;
151 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
152 continue;
153 if (is_address)
154 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
155 else
156 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
157 callback(fn, param, printed_name, sm->state);
158 } END_FOR_EACH_PTR(sm);
159 free:
160 free_string(name);
163 static void match_call_info(struct expression *expr)
165 struct member_info_callback *cb;
166 struct expression *arg;
167 struct state_list *slist;
168 char *name;
169 int i;
171 name = get_fnptr_name(expr->fn);
172 if (!name)
173 return;
175 FOR_EACH_PTR(member_callbacks, cb) {
176 slist = get_all_states(cb->owner);
177 i = 0;
178 FOR_EACH_PTR(expr->args, arg) {
179 print_struct_members(name, arg, i, slist, cb->callback);
180 i++;
181 } END_FOR_EACH_PTR(arg);
182 } END_FOR_EACH_PTR(cb);
184 free_string(name);
185 free_slist(&slist);
188 static unsigned long call_count;
189 static int db_count_callback(void *unused, int argc, char **argv, char **azColName)
191 call_count += strtoul(argv[0], NULL, 10);
192 return 0;
195 static int get_param(int param, char **name, struct symbol **sym)
197 struct symbol *arg;
198 int i;
200 i = 0;
201 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
203 * this is a temporary hack to work around a bug (I think in sparse?)
204 * 2.6.37-rc1:fs/reiserfs/journal.o
205 * If there is a function definition without parameter name found
206 * after a function implementation then it causes a crash.
207 * int foo() {}
208 * int bar(char *);
210 if (arg->ident->name < (char *)100)
211 continue;
212 if (i == param && arg->ident->name) {
213 *name = arg->ident->name;
214 *sym = arg;
215 return TRUE;
217 i++;
218 } END_FOR_EACH_PTR(arg);
220 return FALSE;
223 static struct state_list *final_states;
224 static int prev_func_id = -1;
225 static int db_callback(void *unused, int argc, char **argv, char **azColName)
227 int func_id;
228 long type;
229 long param;
230 char *name;
231 struct symbol *sym;
232 struct def_callback *def_callback;
234 if (argc != 5)
235 return 0;
237 func_id = atoi(argv[0]);
238 errno = 0;
239 type = strtol(argv[1], NULL, 10);
240 param = strtol(argv[2], NULL, 10);
241 if (errno)
242 return 0;
244 if (prev_func_id == -1)
245 prev_func_id = func_id;
246 if (func_id != prev_func_id) {
247 merge_slist(&final_states, __pop_fake_cur_slist());
248 __push_fake_cur_slist();
249 __unnullify_path();
250 prev_func_id = func_id;
253 if (param == -1 || !get_param(param, &name, &sym))
254 return 0;
256 FOR_EACH_PTR(callbacks, def_callback) {
257 if (def_callback->hook_type == type)
258 def_callback->callback(name, sym, argv[3], argv[4]);
259 } END_FOR_EACH_PTR(def_callback);
261 return 0;
264 static void get_direct_callers(struct symbol *sym)
266 char sql_filter[1024];
268 if (sym->ctype.modifiers & MOD_STATIC) {
269 snprintf(sql_filter, 1024,
270 "file = '%s' and function = '%s' order by function_id;",
271 get_filename(), sym->ident->name);
272 } else {
273 snprintf(sql_filter, 1024,
274 "function = '%s' order by function_id;",
275 sym->ident->name);
278 run_sql(db_count_callback, "select count(*) from caller_info where %s",
279 sql_filter);
280 if (call_count == 0 || call_count > 100)
281 return;
283 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
284 " where %s", sql_filter);
287 static char *ptr_name;
288 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
290 if (!ptr_name)
291 ptr_name = alloc_string(argv[0]);
292 return 0;
295 static void get_function_pointer_callers(struct symbol *sym)
297 ptr_name = NULL;
298 run_sql(get_ptr_name, "select ptr from function_ptr where function = '%s'",
299 sym->ident->name);
300 if (!ptr_name)
301 return;
303 run_sql(db_count_callback, "select count(*) from caller_info where function = '%s'",
304 ptr_name);
305 if (call_count == 0 || call_count > 100)
306 return;
308 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
309 " where function = '%s' order by function_id", ptr_name);
312 static void match_data_from_db(struct symbol *sym)
314 struct sm_state *sm;
316 if (!sym || !sym->ident || !sym->ident->name)
317 return;
319 __push_fake_cur_slist();
320 __unnullify_path();
321 prev_func_id = -1;
323 call_count = 0;
324 get_direct_callers(sym);
325 get_function_pointer_callers(sym);
327 merge_slist(&final_states, __pop_fake_cur_slist());
329 if (call_count > 100) {
330 free_slist(&final_states);
331 return;
333 FOR_EACH_PTR(final_states, sm) {
334 __set_sm(sm);
335 } END_FOR_EACH_PTR(sm);
337 free_slist(&final_states);
340 static void match_function_assign(struct expression *expr)
342 struct expression *right = expr->right;
343 struct symbol *sym;
344 char *fn_name;
345 char *ptr_name;
347 if (right->type == EXPR_PREOP && right->op == '&')
348 right = right->unop;
349 if (right->type != EXPR_SYMBOL)
350 return;
351 sym = get_type(right);
352 if (!sym || sym->type != SYM_FN)
353 return;
355 fn_name = get_variable_from_expr(right, NULL);
356 ptr_name = get_fnptr_name(expr->left);
357 if (!fn_name || !ptr_name)
358 goto free;
360 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
362 free:
363 free_string(fn_name);
364 free_string(ptr_name);
367 static void print_initializer_list(struct expression_list *expr_list,
368 struct symbol *struct_type)
370 struct expression *expr;
371 struct symbol *base_type;
373 FOR_EACH_PTR(expr_list, expr) {
374 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
375 print_initializer_list(expr->idx_expression->expr_list, struct_type);
376 continue;
378 if (expr->type != EXPR_IDENTIFIER)
379 continue;
380 if (!expr->expr_ident)
381 continue;
382 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
383 continue;
384 base_type = get_type(expr->ident_expression);
385 if (!base_type || base_type->type != SYM_FN)
386 continue;
387 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type->ident->name,
388 expr->expr_ident->name,
389 expr->ident_expression->symbol_name->name);
390 } END_FOR_EACH_PTR(expr);
394 static void global_variable(struct symbol *sym)
396 struct symbol *struct_type;
398 if (!sym->ident)
399 return;
400 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
401 return;
402 struct_type = get_base_type(sym);
403 if (!struct_type)
404 return;
405 if (struct_type->type == SYM_ARRAY) {
406 struct_type = get_base_type(struct_type);
407 if (!struct_type)
408 return;
410 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
411 return;
412 print_initializer_list(sym->initializer->expr_list, struct_type);
415 void open_smatch_db(void)
417 #ifdef SQLITE_OPEN_READONLY
418 int rc;
420 if (option_no_db)
421 return;
423 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
424 if (rc != SQLITE_OK) {
425 option_no_db = 1;
426 return;
428 return;
429 #else
430 option_no_db = 1;
431 return;
432 #endif
435 void register_definition_db_callbacks(int id)
437 if (option_info) {
438 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
439 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
440 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
441 add_hook(&global_variable, BASE_HOOK);
442 add_hook(&global_variable, DECLARATION_HOOK);
445 if (option_no_db)
446 return;
448 add_hook(&match_data_from_db, FUNC_DEF_HOOK);