db: fix return ranges for the return_states callbacks
[smatch.git] / smatch_db.c
blobb226f4ab0b7beb65af47ce423b5c9ba39f91470f
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, 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 {
43 int owner;
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 {
51 int type;
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;
58 static int return_id;
59 int get_return_id(void)
61 return return_id;
64 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
66 char *err = NULL;
67 int rc;
69 if (option_no_db || !db)
70 return;
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);
123 cb->type = type;
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)
132 if (argc != 1)
133 return 0;
134 if (option_debug)
135 sm_msg("return type %d", type_positive_bits(return_type));
136 parse_value_ranges_type(return_type, argv[0], &return_range_list);
137 return 0;
140 struct range_list *db_return_vals(struct expression *expr)
142 struct symbol *sym;
143 static char sql_filter[1024];
145 if (expr->type != EXPR_CALL)
146 return NULL;
147 if (expr->fn->type != EXPR_SYMBOL)
148 return NULL;
149 return_type = get_type(expr);
150 if (!return_type)
151 return NULL;
152 sym = expr->fn->symbol;
153 if (!sym)
154 return NULL;
156 if (sym->ctype.modifiers & MOD_STATIC) {
157 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
158 get_filename(), sym->ident->name);
159 } else {
160 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
161 sym->ident->name);
164 return_range_list = NULL;
165 run_sql(db_return_callback, "select return from return_values where %s",
166 sql_filter);
167 return return_range_list;
170 static void match_call_hack(struct expression *expr)
172 char *name;
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);
181 if (!name)
182 return;
183 sm_msg("info: call_marker '%s' %s", name, is_static(expr->fn) ? "static" : "global");
184 free_string(name);
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))
190 struct sm_state *sm;
191 char *name;
192 struct symbol *sym;
193 int len;
194 char printed_name[256];
195 int is_address = 0;
197 expr = strip_expr(expr);
198 if (expr->type == EXPR_PREOP && expr->op == '&') {
199 expr = strip_expr(expr->unop);
200 is_address = 1;
203 name = get_variable_from_expr(expr, &sym);
204 if (!name || !sym)
205 goto free;
207 len = strlen(name);
208 FOR_EACH_PTR(slist, sm) {
209 if (sm->sym != sym)
210 continue;
211 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
212 continue;
213 if (is_address)
214 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
215 else
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);
219 free:
220 free_string(name);
223 static void match_call_info(struct expression *expr)
225 struct member_info_callback *cb;
226 struct expression *arg;
227 struct state_list *slist;
228 char *name;
229 int i;
230 char *gs;
232 name = get_fnptr_name(expr->fn);
233 if (!name)
234 return;
236 if (is_static(expr->fn))
237 gs = (char *)"static";
238 else
239 gs = (char *)"global";
241 FOR_EACH_PTR(member_callbacks, cb) {
242 slist = get_all_states(cb->owner);
243 i = 0;
244 FOR_EACH_PTR(expr->args, arg) {
245 print_struct_members(name, gs, arg, i, slist, cb->callback);
246 i++;
247 } END_FOR_EACH_PTR(arg);
248 free_slist(&slist);
249 } END_FOR_EACH_PTR(cb);
251 free_string(name);
254 static int get_param(int param, char **name, struct symbol **sym)
256 struct symbol *arg;
257 int i;
259 i = 0;
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.
266 * int foo() {}
267 * int bar(char *);
269 if (arg->ident->name < (char *)100)
270 continue;
271 if (i == param && arg->ident->name) {
272 *name = arg->ident->name;
273 *sym = arg;
274 return TRUE;
276 i++;
277 } END_FOR_EACH_PTR(arg);
279 return FALSE;
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)
286 int func_id;
287 long type;
288 long param;
289 char *name = NULL;
290 struct symbol *sym = NULL;
291 struct def_callback *def_callback;
293 if (argc != 5)
294 return 0;
296 func_id = atoi(argv[0]);
297 errno = 0;
298 type = strtol(argv[1], NULL, 10);
299 param = strtol(argv[2], NULL, 10);
300 if (errno)
301 return 0;
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();
308 __unnullify_path();
309 prev_func_id = func_id;
312 if (type == INTERNAL)
313 return 0;
314 if (param >= 0 && !get_param(param, &name, &sym))
315 return 0;
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);
322 return 0;
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);
333 } else {
334 snprintf(sql_filter, 1024,
335 "function = '%s' and static = 0 order by function_id;",
336 sym->ident->name);
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)
346 if (!ptr_name)
347 ptr_name = alloc_string(argv[0]);
348 return 0;
351 static void get_function_pointer_callers(struct symbol *sym)
353 ptr_name = NULL;
354 run_sql(get_ptr_name, "select ptr from function_ptr where function = '%s'",
355 sym->ident->name);
356 if (!ptr_name)
357 return;
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)
365 struct sm_state *sm;
367 if (!sym || !sym->ident || !sym->ident->name)
368 return;
370 __push_fake_cur_slist();
371 __unnullify_path();
372 prev_func_id = -1;
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) {
380 __set_sm(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;
389 struct symbol *sym;
390 char *fn_name;
391 char *ptr_name;
393 if (right->type == EXPR_PREOP && right->op == '&')
394 right = strip_expr(right->unop);
395 if (right->type != EXPR_SYMBOL)
396 return;
397 sym = get_type(right);
398 if (!sym || sym->type != SYM_FN)
399 return;
401 fn_name = get_variable_from_expr(right, NULL);
402 ptr_name = get_fnptr_name(expr->left);
403 if (!fn_name || !ptr_name)
404 goto free;
406 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
408 free:
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;
418 int type;
419 int param;
421 if (argc != 4)
422 return 0;
424 type = atoi(argv[1]);
425 param = atoi(argv[2]);
427 FOR_EACH_PTR(call_implies_cb_list, cb) {
428 if (cb->type != type)
429 continue;
430 if (param != -1) {
431 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
432 if (!arg)
433 continue;
435 cb->callback(arg, argv[3]);
436 } END_FOR_EACH_PTR(cb);
438 return 0;
441 static void match_call_implies(struct expression *expr)
443 struct symbol *sym;
444 static char sql_filter[1024];
446 if (expr->fn->type != EXPR_SYMBOL)
447 return;
448 sym = expr->fn->symbol;
449 if (!sym)
450 return;
452 if (sym->ctype.modifiers & MOD_STATIC) {
453 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
454 get_filename(), sym->ident->name);
455 } else {
456 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
457 sym->ident->name);
460 call_implies_call_expr = expr;
461 run_sql(call_implies_callbacks,
462 "select function, type, parameter, value from call_implies where %s",
463 sql_filter);
464 return;
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);
476 continue;
478 if (expr->type != EXPR_IDENTIFIER)
479 continue;
480 if (!expr->expr_ident)
481 continue;
482 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
483 continue;
484 base_type = get_type(expr->ident_expression);
485 if (!base_type || base_type->type != SYM_FN)
486 continue;
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;
497 if (!sym->ident)
498 return;
499 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
500 return;
501 struct_type = get_base_type(sym);
502 if (!struct_type)
503 return;
504 if (struct_type->type == SYM_ARRAY) {
505 struct_type = get_base_type(struct_type);
506 if (!struct_type)
507 return;
509 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
510 return;
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,
517 global_static());
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;
525 char *return_ranges;
527 if (!expr) {
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);
532 } else {
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 struct state_list *get_my_states(int owner, struct state_list *source)
545 struct state_list *slist = NULL;
546 struct sm_state *tmp;
548 FOR_EACH_PTR(source, tmp) {
549 if (tmp->owner == owner)
550 add_ptr_list(&slist, tmp);
551 } END_FOR_EACH_PTR(tmp);
553 return slist;
556 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
558 struct returned_member_callback *cb;
559 struct state_list *my_slist;
560 struct sm_state *sm;
561 struct symbol *type;
562 char *name;
563 char member_name[256];
564 int len;
566 type = get_type(expr);
567 if (!type || type->type != SYM_PTR)
568 return;
569 type = get_real_base_type(type);
570 if (!type || type->type != SYM_STRUCT)
571 return;
572 name = get_variable_from_expr(expr, NULL);
573 if (!name)
574 return;
576 member_name[sizeof(member_name) - 1] = '\0';
577 strcpy(member_name, "$$");
579 len = strlen(name);
580 FOR_EACH_PTR(returned_member_callbacks, cb) {
581 my_slist = get_my_states(cb->owner, slist);
582 FOR_EACH_PTR(my_slist, sm) {
583 if (strncmp(sm->name, name, len) != 0)
584 continue;
585 if (strncmp(sm->name + len, "->", 2) != 0)
586 continue;
587 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
588 cb->callback(return_id, return_ranges, member_name, sm->state);
589 } END_FOR_EACH_PTR(sm);
590 free_slist(&my_slist);
591 } END_FOR_EACH_PTR(cb);
593 free_string(name);
596 static void match_end_func_info(struct symbol *sym)
598 if (__path_is_null())
599 return;
600 sm_msg("info: return_marker %d '' %s", get_return_id(), global_static());
603 static void match_function_def(struct symbol *sym)
605 return_id = 0;
608 static void match_return(struct expression *ret_value)
610 return_id++;
613 static void match_end_func(struct symbol *sym)
615 return_id++;
618 void open_smatch_db(void)
620 #ifdef SQLITE_OPEN_READONLY
621 int rc;
623 if (option_no_db)
624 return;
626 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
627 if (rc != SQLITE_OK) {
628 option_no_db = 1;
629 return;
631 return;
632 #else
633 option_no_db = 1;
634 return;
635 #endif
638 void register_definition_db_callbacks(int id)
640 add_hook(&match_function_def, FUNC_DEF_HOOK);
641 add_hook(&match_return, RETURN_HOOK);
642 add_hook(&match_end_func, END_FUNC_HOOK);
644 if (option_info) {
645 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
646 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
647 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
648 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
649 add_hook(&global_variable, BASE_HOOK);
650 add_hook(&global_variable, DECLARATION_HOOK);
651 add_returned_state_callback(match_return_info);
652 add_returned_state_callback(print_returned_struct_members);
653 add_hook(&call_return_state_hooks, RETURN_HOOK);
654 add_hook(&match_end_func_info, END_FUNC_HOOK);
657 if (option_no_db)
658 return;
660 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
661 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);