da9de6d01c03f6f6eac943e1cfd8448257dd125b
[smatch.git] / smatch_db.c
blobda9de6d01c03f6f6eac943e1cfd8448257dd125b
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 sm_msg("info: return_marker %d '%s' %s", return_id, return_ranges,
539 global_static());
542 static int return_id;
543 static void match_function_def(struct symbol *sym)
545 return_id = 0;
548 static void call_return_state_hooks_compare(struct expression *expr)
550 struct returned_state_callback *cb;
551 struct state_list *slist;
552 char *return_ranges;
553 int final_pass_orig = final_pass;
555 __push_fake_cur_slist();
557 final_pass = 0;
558 __split_whole_condition(expr);
559 final_pass = final_pass_orig;
561 return_ranges = alloc_sname("1");
563 return_id++;
564 slist = __get_cur_slist();
565 FOR_EACH_PTR(returned_state_callbacks, cb) {
566 cb->callback(return_id, return_ranges, expr, slist);
567 } END_FOR_EACH_PTR(cb);
569 __push_true_states();
570 __use_false_states();
572 return_ranges = alloc_sname("0");;
573 return_id++;
574 slist = __get_cur_slist();
575 FOR_EACH_PTR(returned_state_callbacks, cb) {
576 cb->callback(return_id, return_ranges, expr, slist);
577 } END_FOR_EACH_PTR(cb);
579 __merge_true_states();
580 __pop_fake_cur_slist();
583 static int call_return_state_hooks_split_possible(struct expression *expr)
585 struct returned_state_callback *cb;
586 struct state_list *slist;
587 struct range_list *rl;
588 char *return_ranges;
589 struct sm_state *sm;
590 struct sm_state *tmp;
591 int ret = 0;
592 int nr_possible, nr_states;
594 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
595 if (!sm || !sm->merged)
596 return 0;
598 /* bail if it gets too complicated */
599 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
600 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
601 if (nr_possible >= 100)
602 return 0;
603 if (nr_states * nr_possible >= 1000)
604 return 0;
606 FOR_EACH_PTR(sm->possible, tmp) {
607 if (tmp->merged)
608 continue;
610 ret = 1;
611 __push_fake_cur_slist();
613 overwrite_states_using_pool(tmp);
615 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
616 return_ranges = show_rl(rl);
618 return_id++;
619 slist = __get_cur_slist();
620 FOR_EACH_PTR(returned_state_callbacks, cb) {
621 cb->callback(return_id, return_ranges, expr, slist);
622 } END_FOR_EACH_PTR(cb);
624 __pop_fake_cur_slist();
625 } END_FOR_EACH_PTR(tmp);
627 return ret;
630 static void call_return_state_hooks(struct expression *expr)
632 struct returned_state_callback *cb;
633 struct state_list *slist;
634 struct range_list *rl;
635 char *return_ranges;
636 int nr_states;
638 expr = strip_expr(expr);
640 if (!expr) {
641 return_ranges = alloc_sname("");
642 } else if (is_condition(expr)) {
643 call_return_state_hooks_compare(expr);
644 return;
645 } else if (call_return_state_hooks_split_possible(expr)) {
646 return;
647 } else if (get_implied_rl(expr, &rl)) {
648 rl = cast_rl(cur_func_return_type(), rl);
649 return_ranges = show_rl(rl);
650 } else {
651 rl = alloc_whole_rl(cur_func_return_type());
652 return_ranges = show_rl(rl);
655 return_id++;
656 slist = __get_cur_slist();
657 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
658 FOR_EACH_PTR(returned_state_callbacks, cb) {
659 if (nr_states < 10000)
660 cb->callback(return_id, return_ranges, expr, slist);
661 else
662 cb->callback(return_id, return_ranges, expr, NULL);
663 } END_FOR_EACH_PTR(cb);
666 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
668 struct returned_member_callback *cb;
669 struct state_list *my_slist;
670 struct sm_state *sm;
671 struct symbol *type;
672 char *name;
673 char member_name[256];
674 int len;
676 type = get_type(expr);
677 if (!type || type->type != SYM_PTR)
678 return;
679 type = get_real_base_type(type);
680 if (!type || type->type != SYM_STRUCT)
681 return;
682 name = expr_to_var(expr);
683 if (!name)
684 return;
686 member_name[sizeof(member_name) - 1] = '\0';
687 strcpy(member_name, "$$");
689 len = strlen(name);
690 FOR_EACH_PTR(returned_member_callbacks, cb) {
691 my_slist = get_all_states_slist(cb->owner, slist);
692 FOR_EACH_PTR(my_slist, sm) {
693 if (strncmp(sm->name, name, len) != 0)
694 continue;
695 if (strncmp(sm->name + len, "->", 2) != 0)
696 continue;
697 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
698 cb->callback(return_id, return_ranges, member_name, sm->state);
699 } END_FOR_EACH_PTR(sm);
700 free_slist(&my_slist);
701 } END_FOR_EACH_PTR(cb);
703 free_string(name);
706 static void match_end_func_info(struct symbol *sym)
708 if (__path_is_null())
709 return;
710 call_return_state_hooks(NULL);
713 void open_smatch_db(void)
715 #ifdef SQLITE_OPEN_READONLY
716 int rc;
718 if (option_no_db)
719 return;
721 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
722 if (rc != SQLITE_OK) {
723 option_no_db = 1;
724 return;
726 return;
727 #else
728 option_no_db = 1;
729 return;
730 #endif
733 void register_definition_db_callbacks(int id)
735 add_hook(&match_function_def, FUNC_DEF_HOOK);
737 if (option_info) {
738 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
739 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
740 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
741 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
742 add_hook(&global_variable, BASE_HOOK);
743 add_hook(&global_variable, DECLARATION_HOOK);
744 add_returned_state_callback(match_return_info);
745 add_returned_state_callback(print_returned_struct_members);
746 add_hook(&call_return_state_hooks, RETURN_HOOK);
747 add_hook(&match_end_func_info, END_FUNC_HOOK);
750 if (option_no_db)
751 return;
753 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
754 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
757 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
759 char buf[256];
760 char *tmp;
762 if (strcmp(key, "$$") == 0)
763 return expr_to_var_sym(arg, sym);
765 if (strcmp(key, "*$$") == 0) {
766 if (arg->type == EXPR_PREOP && arg->op == '&') {
767 arg = strip_expr(arg->unop);
768 return expr_to_var_sym(arg, sym);
769 } else {
770 tmp = expr_to_var_sym(arg, sym);
771 if (!tmp)
772 return NULL;
773 snprintf(buf, sizeof(buf), "*%s", tmp);
774 free_string(tmp);
775 return alloc_string(buf);
779 if (arg->type == EXPR_PREOP && arg->op == '&') {
780 arg = strip_expr(arg->unop);
781 tmp = expr_to_var_sym(arg, sym);
782 if (!tmp)
783 return NULL;
784 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
785 return alloc_string(buf);
788 tmp = expr_to_var_sym(arg, sym);
789 if (!tmp)
790 return NULL;
791 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
792 free_string(tmp);
793 return alloc_string(buf);