db: move return_states to raw SQL
[smatch.git] / smatch_db.c
blobeb8f8343b9f761722ef3c302278a70bb3fdcc1f8
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 #define sql_insert(table, values...) \
18 do { \
19 if (option_info) { \
20 sm_prefix(); \
21 sm_printf("SQL: insert into " #table " values (" values); \
22 sm_printf(");\n"); \
23 } \
24 } while (0)
26 static sqlite3 *db;
28 struct def_callback {
29 int hook_type;
30 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
32 ALLOCATOR(def_callback, "definition db hook callbacks");
33 DECLARE_PTR_LIST(callback_list, struct def_callback);
34 static struct callback_list *callbacks;
36 struct member_info_callback {
37 int owner;
38 void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state);
40 ALLOCATOR(member_info_callback, "caller_info callbacks");
41 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
42 static struct member_info_cb_list *member_callbacks;
44 struct returned_state_callback {
45 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
47 ALLOCATOR(returned_state_callback, "returned state callbacks");
48 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
49 static struct returned_state_cb_list *returned_state_callbacks;
51 struct returned_member_callback {
52 int owner;
53 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
55 ALLOCATOR(returned_member_callback, "returned member callbacks");
56 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
57 static struct returned_member_cb_list *returned_member_callbacks;
59 struct call_implies_callback {
60 int type;
61 void (*callback)(struct expression *arg, char *value);
63 ALLOCATOR(call_implies_callback, "call_implies callbacks");
64 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
65 static struct call_implies_cb_list *call_implies_cb_list;
67 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
69 char *err = NULL;
70 int rc;
72 if (option_no_db || !db)
73 return;
75 rc = sqlite3_exec(db, sql, callback, 0, &err);
76 if (rc != SQLITE_OK) {
77 fprintf(stderr, "SQL error #2: %s\n", err);
78 fprintf(stderr, "SQL: '%s'\n", sql);
82 void sql_insert_return_states(int return_id, const char *return_ranges,
83 int type, int param, const char *key, const char *value)
85 sql_insert(return_states, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
86 get_filename(), get_function(), return_id, return_ranges,
87 fn_static(), type, param, key, value);
90 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
92 struct def_callback *def_callback = __alloc_def_callback(0);
94 def_callback->hook_type = type;
95 def_callback->callback = callback;
96 add_ptr_list(&callbacks, def_callback);
100 * These call backs are used when the --info option is turned on to print struct
101 * member information. For example foo->bar could have a state in
102 * smatch_extra.c and also check_user.c.
104 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
106 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
108 member_callback->owner = owner;
109 member_callback->callback = callback;
110 add_ptr_list(&member_callbacks, member_callback);
113 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
115 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
117 callback->callback = fn;
118 add_ptr_list(&returned_state_callbacks, callback);
121 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
123 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
125 member_callback->owner = owner;
126 member_callback->callback = callback;
127 add_ptr_list(&returned_member_callbacks, member_callback);
130 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
132 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
134 cb->type = type;
135 cb->callback = callback;
136 add_ptr_list(&call_implies_cb_list, cb);
139 static struct symbol *return_type;
140 static struct range_list *return_range_list;
141 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
143 if (argc != 1)
144 return 0;
145 if (option_debug)
146 sm_msg("return type %d", type_positive_bits(return_type));
147 str_to_rl(return_type, argv[0], &return_range_list);
148 return 0;
151 struct range_list *db_return_vals(struct expression *expr)
153 struct symbol *sym;
154 static char sql_filter[1024];
156 if (expr->type != EXPR_CALL)
157 return NULL;
158 if (expr->fn->type != EXPR_SYMBOL)
159 return NULL;
160 return_type = get_type(expr);
161 if (!return_type)
162 return NULL;
163 sym = expr->fn->symbol;
164 if (!sym)
165 return NULL;
167 if (sym->ctype.modifiers & MOD_STATIC) {
168 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
169 get_filename(), sym->ident->name);
170 } else {
171 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
172 sym->ident->name);
175 return_range_list = NULL;
176 run_sql(db_return_callback, "select return from return_values where %s",
177 sql_filter);
178 return return_range_list;
181 static void match_call_hack(struct expression *expr)
183 char *name;
186 * we just want to record something in the database so that if we have
187 * two calls like: frob(4); frob(some_unkown); then on the receiving
188 * side we know that sometimes frob is called with unknown parameters.
191 name = get_fnptr_name(expr->fn);
192 if (!name)
193 return;
194 sm_msg("info: call_marker '%s' %s", name, is_static(expr->fn) ? "static" : "global");
195 free_string(name);
198 static void print_struct_members(char *fn, char *global_static, struct expression *expr, int param, struct state_list *slist,
199 void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
201 struct sm_state *sm;
202 char *name;
203 struct symbol *sym;
204 int len;
205 char printed_name[256];
206 int is_address = 0;
208 expr = strip_expr(expr);
209 if (expr->type == EXPR_PREOP && expr->op == '&') {
210 expr = strip_expr(expr->unop);
211 is_address = 1;
214 name = expr_to_var_sym(expr, &sym);
215 if (!name || !sym)
216 goto free;
218 len = strlen(name);
219 FOR_EACH_PTR(slist, sm) {
220 if (sm->sym != sym)
221 continue;
222 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
223 continue;
224 if (is_address)
225 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
226 else
227 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
228 callback(fn, global_static, param, printed_name, sm->state);
229 } END_FOR_EACH_PTR(sm);
230 free:
231 free_string(name);
234 static void match_call_info(struct expression *expr)
236 struct member_info_callback *cb;
237 struct expression *arg;
238 struct state_list *slist;
239 char *name;
240 int i;
241 char *gs;
243 name = get_fnptr_name(expr->fn);
244 if (!name)
245 return;
247 if (is_static(expr->fn))
248 gs = (char *)"static";
249 else
250 gs = (char *)"global";
252 FOR_EACH_PTR(member_callbacks, cb) {
253 slist = get_all_states(cb->owner);
254 i = 0;
255 FOR_EACH_PTR(expr->args, arg) {
256 print_struct_members(name, gs, arg, i, slist, cb->callback);
257 i++;
258 } END_FOR_EACH_PTR(arg);
259 free_slist(&slist);
260 } END_FOR_EACH_PTR(cb);
262 free_string(name);
265 static int get_param(int param, char **name, struct symbol **sym)
267 struct symbol *arg;
268 int i;
270 i = 0;
271 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
273 * this is a temporary hack to work around a bug (I think in sparse?)
274 * 2.6.37-rc1:fs/reiserfs/journal.o
275 * If there is a function definition without parameter name found
276 * after a function implementation then it causes a crash.
277 * int foo() {}
278 * int bar(char *);
280 if (arg->ident->name < (char *)100)
281 continue;
282 if (i == param && arg->ident->name) {
283 *name = arg->ident->name;
284 *sym = arg;
285 return TRUE;
287 i++;
288 } END_FOR_EACH_PTR(arg);
290 return FALSE;
293 static struct state_list *final_states;
294 static int prev_func_id = -1;
295 static int db_callback(void *unused, int argc, char **argv, char **azColName)
297 int func_id;
298 long type;
299 long param;
300 char *name = NULL;
301 struct symbol *sym = NULL;
302 struct def_callback *def_callback;
304 if (argc != 5)
305 return 0;
307 func_id = atoi(argv[0]);
308 errno = 0;
309 type = strtol(argv[1], NULL, 10);
310 param = strtol(argv[2], NULL, 10);
311 if (errno)
312 return 0;
314 if (prev_func_id == -1)
315 prev_func_id = func_id;
316 if (func_id != prev_func_id) {
317 merge_slist(&final_states, __pop_fake_cur_slist());
318 __push_fake_cur_slist();
319 __unnullify_path();
320 prev_func_id = func_id;
323 if (type == INTERNAL)
324 return 0;
325 if (param >= 0 && !get_param(param, &name, &sym))
326 return 0;
328 FOR_EACH_PTR(callbacks, def_callback) {
329 if (def_callback->hook_type == type)
330 def_callback->callback(name, sym, argv[3], argv[4]);
331 } END_FOR_EACH_PTR(def_callback);
333 return 0;
336 static void get_direct_callers(struct symbol *sym)
338 char sql_filter[1024];
340 if (sym->ctype.modifiers & MOD_STATIC) {
341 snprintf(sql_filter, 1024,
342 "file = '%s' and function = '%s' order by function_id;",
343 get_filename(), sym->ident->name);
344 } else {
345 snprintf(sql_filter, 1024,
346 "function = '%s' and static = 0 order by function_id;",
347 sym->ident->name);
350 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
351 " where %s", sql_filter);
354 static char *ptr_name;
355 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
357 if (!ptr_name)
358 ptr_name = alloc_string(argv[0]);
359 return 0;
362 static void get_function_pointer_callers(struct symbol *sym)
364 char sql_filter[1024];
366 if (sym->ctype.modifiers & MOD_STATIC) {
367 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
368 get_filename(), sym->ident->name);
369 } else {
370 snprintf(sql_filter, 1024, "function = '%s';",
371 sym->ident->name);
374 ptr_name = NULL;
375 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
376 if (!ptr_name)
377 return;
379 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
380 " where function = '%s' order by function_id", ptr_name);
382 free_string(ptr_name);
385 static void match_data_from_db(struct symbol *sym)
387 struct sm_state *sm;
389 if (!sym || !sym->ident || !sym->ident->name)
390 return;
392 __push_fake_cur_slist();
393 __unnullify_path();
394 prev_func_id = -1;
396 get_direct_callers(sym);
397 get_function_pointer_callers(sym);
399 merge_slist(&final_states, __pop_fake_cur_slist());
401 FOR_EACH_PTR(final_states, sm) {
402 __set_sm(sm);
403 } END_FOR_EACH_PTR(sm);
405 free_slist(&final_states);
408 static void match_function_assign(struct expression *expr)
410 struct expression *right = expr->right;
411 struct symbol *sym;
412 char *fn_name;
413 char *ptr_name;
415 if (right->type == EXPR_PREOP && right->op == '&')
416 right = strip_expr(right->unop);
417 if (right->type != EXPR_SYMBOL)
418 return;
419 sym = get_type(right);
420 if (!sym || sym->type != SYM_FN)
421 return;
423 fn_name = expr_to_var(right);
424 ptr_name = get_fnptr_name(expr->left);
425 if (!fn_name || !ptr_name)
426 goto free;
428 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
430 free:
431 free_string(fn_name);
432 free_string(ptr_name);
435 static struct expression *call_implies_call_expr;
436 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
438 struct call_implies_callback *cb;
439 struct expression *arg = NULL;
440 int type;
441 int param;
443 if (argc != 4)
444 return 0;
446 type = atoi(argv[1]);
447 param = atoi(argv[2]);
449 FOR_EACH_PTR(call_implies_cb_list, cb) {
450 if (cb->type != type)
451 continue;
452 if (param != -1) {
453 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
454 if (!arg)
455 continue;
457 cb->callback(arg, argv[3]);
458 } END_FOR_EACH_PTR(cb);
460 return 0;
463 static void match_call_implies(struct expression *expr)
465 struct symbol *sym;
466 static char sql_filter[1024];
468 if (expr->fn->type != EXPR_SYMBOL)
469 return;
470 sym = expr->fn->symbol;
471 if (!sym)
472 return;
474 if (sym->ctype.modifiers & MOD_STATIC) {
475 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
476 get_filename(), sym->ident->name);
477 } else {
478 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
479 sym->ident->name);
482 call_implies_call_expr = expr;
483 run_sql(call_implies_callbacks,
484 "select function, type, parameter, value from call_implies where %s",
485 sql_filter);
486 return;
489 static void print_initializer_list(struct expression_list *expr_list,
490 struct symbol *struct_type)
492 struct expression *expr;
493 struct symbol *base_type;
495 FOR_EACH_PTR(expr_list, expr) {
496 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
497 print_initializer_list(expr->idx_expression->expr_list, struct_type);
498 continue;
500 if (expr->type != EXPR_IDENTIFIER)
501 continue;
502 if (!expr->expr_ident)
503 continue;
504 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
505 continue;
506 base_type = get_type(expr->ident_expression);
507 if (!base_type || base_type->type != SYM_FN)
508 continue;
509 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type->ident->name,
510 expr->expr_ident->name,
511 expr->ident_expression->symbol_name->name);
512 } END_FOR_EACH_PTR(expr);
515 static void global_variable(struct symbol *sym)
517 struct symbol *struct_type;
519 if (!sym->ident)
520 return;
521 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
522 return;
523 struct_type = get_base_type(sym);
524 if (!struct_type)
525 return;
526 if (struct_type->type == SYM_ARRAY) {
527 struct_type = get_base_type(struct_type);
528 if (!struct_type)
529 return;
531 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
532 return;
533 print_initializer_list(sym->initializer->expr_list, struct_type);
536 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
538 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
541 static int return_id;
542 static void match_function_def(struct symbol *sym)
544 return_id = 0;
547 static void call_return_state_hooks_compare(struct expression *expr)
549 struct returned_state_callback *cb;
550 struct state_list *slist;
551 char *return_ranges;
552 int final_pass_orig = final_pass;
554 __push_fake_cur_slist();
556 final_pass = 0;
557 __split_whole_condition(expr);
558 final_pass = final_pass_orig;
560 return_ranges = alloc_sname("1");
562 return_id++;
563 slist = __get_cur_slist();
564 FOR_EACH_PTR(returned_state_callbacks, cb) {
565 cb->callback(return_id, return_ranges, expr, slist);
566 } END_FOR_EACH_PTR(cb);
568 __push_true_states();
569 __use_false_states();
571 return_ranges = alloc_sname("0");;
572 return_id++;
573 slist = __get_cur_slist();
574 FOR_EACH_PTR(returned_state_callbacks, cb) {
575 cb->callback(return_id, return_ranges, expr, slist);
576 } END_FOR_EACH_PTR(cb);
578 __merge_true_states();
579 __pop_fake_cur_slist();
582 static int call_return_state_hooks_split_possible(struct expression *expr)
584 struct returned_state_callback *cb;
585 struct state_list *slist;
586 struct range_list *rl;
587 char *return_ranges;
588 struct sm_state *sm;
589 struct sm_state *tmp;
590 int ret = 0;
591 int nr_possible, nr_states;
593 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
594 if (!sm || !sm->merged)
595 return 0;
597 /* bail if it gets too complicated */
598 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
599 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
600 if (nr_possible >= 100)
601 return 0;
602 if (nr_states * nr_possible >= 1000)
603 return 0;
605 FOR_EACH_PTR(sm->possible, tmp) {
606 if (tmp->merged)
607 continue;
609 ret = 1;
610 __push_fake_cur_slist();
612 overwrite_states_using_pool(tmp);
614 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
615 return_ranges = show_rl(rl);
617 return_id++;
618 slist = __get_cur_slist();
619 FOR_EACH_PTR(returned_state_callbacks, cb) {
620 cb->callback(return_id, return_ranges, expr, slist);
621 } END_FOR_EACH_PTR(cb);
623 __pop_fake_cur_slist();
624 } END_FOR_EACH_PTR(tmp);
626 return ret;
629 static void call_return_state_hooks(struct expression *expr)
631 struct returned_state_callback *cb;
632 struct state_list *slist;
633 struct range_list *rl;
634 char *return_ranges;
635 int nr_states;
637 expr = strip_expr(expr);
639 if (!expr) {
640 return_ranges = alloc_sname("");
641 } else if (is_condition(expr)) {
642 call_return_state_hooks_compare(expr);
643 return;
644 } else if (call_return_state_hooks_split_possible(expr)) {
645 return;
646 } else if (get_implied_rl(expr, &rl)) {
647 rl = cast_rl(cur_func_return_type(), rl);
648 return_ranges = show_rl(rl);
649 } else {
650 rl = alloc_whole_rl(cur_func_return_type());
651 return_ranges = show_rl(rl);
654 return_id++;
655 slist = __get_cur_slist();
656 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
657 FOR_EACH_PTR(returned_state_callbacks, cb) {
658 if (nr_states < 10000)
659 cb->callback(return_id, return_ranges, expr, slist);
660 else
661 cb->callback(return_id, return_ranges, expr, NULL);
662 } END_FOR_EACH_PTR(cb);
665 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
667 struct returned_member_callback *cb;
668 struct state_list *my_slist;
669 struct sm_state *sm;
670 struct symbol *type;
671 char *name;
672 char member_name[256];
673 int len;
675 type = get_type(expr);
676 if (!type || type->type != SYM_PTR)
677 return;
678 type = get_real_base_type(type);
679 if (!type || type->type != SYM_STRUCT)
680 return;
681 name = expr_to_var(expr);
682 if (!name)
683 return;
685 member_name[sizeof(member_name) - 1] = '\0';
686 strcpy(member_name, "$$");
688 len = strlen(name);
689 FOR_EACH_PTR(returned_member_callbacks, cb) {
690 my_slist = get_all_states_slist(cb->owner, slist);
691 FOR_EACH_PTR(my_slist, sm) {
692 if (strncmp(sm->name, name, len) != 0)
693 continue;
694 if (strncmp(sm->name + len, "->", 2) != 0)
695 continue;
696 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
697 cb->callback(return_id, return_ranges, member_name, sm->state);
698 } END_FOR_EACH_PTR(sm);
699 free_slist(&my_slist);
700 } END_FOR_EACH_PTR(cb);
702 free_string(name);
705 static void match_end_func_info(struct symbol *sym)
707 if (__path_is_null())
708 return;
709 call_return_state_hooks(NULL);
712 void open_smatch_db(void)
714 #ifdef SQLITE_OPEN_READONLY
715 int rc;
717 if (option_no_db)
718 return;
720 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
721 if (rc != SQLITE_OK) {
722 option_no_db = 1;
723 return;
725 return;
726 #else
727 option_no_db = 1;
728 return;
729 #endif
732 void register_definition_db_callbacks(int id)
734 add_hook(&match_function_def, FUNC_DEF_HOOK);
736 if (option_info) {
737 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
738 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
739 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
740 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
741 add_hook(&global_variable, BASE_HOOK);
742 add_hook(&global_variable, DECLARATION_HOOK);
743 add_returned_state_callback(match_return_info);
744 add_returned_state_callback(print_returned_struct_members);
745 add_hook(&call_return_state_hooks, RETURN_HOOK);
746 add_hook(&match_end_func_info, END_FUNC_HOOK);
749 if (option_no_db)
750 return;
752 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
753 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
756 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
758 char buf[256];
759 char *tmp;
761 if (strcmp(key, "$$") == 0)
762 return expr_to_var_sym(arg, sym);
764 if (strcmp(key, "*$$") == 0) {
765 if (arg->type == EXPR_PREOP && arg->op == '&') {
766 arg = strip_expr(arg->unop);
767 return expr_to_var_sym(arg, sym);
768 } else {
769 tmp = expr_to_var_sym(arg, sym);
770 if (!tmp)
771 return NULL;
772 snprintf(buf, sizeof(buf), "*%s", tmp);
773 free_string(tmp);
774 return alloc_string(buf);
778 if (arg->type == EXPR_PREOP && arg->op == '&') {
779 arg = strip_expr(arg->unop);
780 tmp = expr_to_var_sym(arg, sym);
781 if (!tmp)
782 return NULL;
783 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
784 return alloc_string(buf);
787 tmp = expr_to_var_sym(arg, sym);
788 if (!tmp)
789 return NULL;
790 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
791 free_string(tmp);
792 return alloc_string(buf);