db: move function_ptr to raw SQL
[smatch.git] / smatch_db.c
blob23926e9921a2217f26767d533cee299faf5502fb
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 sql_insert_function_ptr(const char *fn, const char *struct_name)
92 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
93 struct_name);
96 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
98 struct def_callback *def_callback = __alloc_def_callback(0);
100 def_callback->hook_type = type;
101 def_callback->callback = callback;
102 add_ptr_list(&callbacks, def_callback);
106 * These call backs are used when the --info option is turned on to print struct
107 * member information. For example foo->bar could have a state in
108 * smatch_extra.c and also check_user.c.
110 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
112 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
114 member_callback->owner = owner;
115 member_callback->callback = callback;
116 add_ptr_list(&member_callbacks, member_callback);
119 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
121 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
123 callback->callback = fn;
124 add_ptr_list(&returned_state_callbacks, callback);
127 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
129 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
131 member_callback->owner = owner;
132 member_callback->callback = callback;
133 add_ptr_list(&returned_member_callbacks, member_callback);
136 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
138 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
140 cb->type = type;
141 cb->callback = callback;
142 add_ptr_list(&call_implies_cb_list, cb);
145 static struct symbol *return_type;
146 static struct range_list *return_range_list;
147 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
149 if (argc != 1)
150 return 0;
151 if (option_debug)
152 sm_msg("return type %d", type_positive_bits(return_type));
153 str_to_rl(return_type, argv[0], &return_range_list);
154 return 0;
157 struct range_list *db_return_vals(struct expression *expr)
159 struct symbol *sym;
160 static char sql_filter[1024];
162 if (expr->type != EXPR_CALL)
163 return NULL;
164 if (expr->fn->type != EXPR_SYMBOL)
165 return NULL;
166 return_type = get_type(expr);
167 if (!return_type)
168 return NULL;
169 sym = expr->fn->symbol;
170 if (!sym)
171 return NULL;
173 if (sym->ctype.modifiers & MOD_STATIC) {
174 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
175 get_filename(), sym->ident->name);
176 } else {
177 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
178 sym->ident->name);
181 return_range_list = NULL;
182 run_sql(db_return_callback, "select return from return_values where %s",
183 sql_filter);
184 return return_range_list;
187 static void match_call_hack(struct expression *expr)
189 char *name;
192 * we just want to record something in the database so that if we have
193 * two calls like: frob(4); frob(some_unkown); then on the receiving
194 * side we know that sometimes frob is called with unknown parameters.
197 name = get_fnptr_name(expr->fn);
198 if (!name)
199 return;
200 sm_msg("info: call_marker '%s' %s", name, is_static(expr->fn) ? "static" : "global");
201 free_string(name);
204 static void print_struct_members(char *fn, char *global_static, struct expression *expr, int param, struct state_list *slist,
205 void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state))
207 struct sm_state *sm;
208 char *name;
209 struct symbol *sym;
210 int len;
211 char printed_name[256];
212 int is_address = 0;
214 expr = strip_expr(expr);
215 if (expr->type == EXPR_PREOP && expr->op == '&') {
216 expr = strip_expr(expr->unop);
217 is_address = 1;
220 name = expr_to_var_sym(expr, &sym);
221 if (!name || !sym)
222 goto free;
224 len = strlen(name);
225 FOR_EACH_PTR(slist, sm) {
226 if (sm->sym != sym)
227 continue;
228 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
229 continue;
230 if (is_address)
231 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
232 else
233 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
234 callback(fn, global_static, param, printed_name, sm->state);
235 } END_FOR_EACH_PTR(sm);
236 free:
237 free_string(name);
240 static void match_call_info(struct expression *expr)
242 struct member_info_callback *cb;
243 struct expression *arg;
244 struct state_list *slist;
245 char *name;
246 int i;
247 char *gs;
249 name = get_fnptr_name(expr->fn);
250 if (!name)
251 return;
253 if (is_static(expr->fn))
254 gs = (char *)"static";
255 else
256 gs = (char *)"global";
258 FOR_EACH_PTR(member_callbacks, cb) {
259 slist = get_all_states(cb->owner);
260 i = 0;
261 FOR_EACH_PTR(expr->args, arg) {
262 print_struct_members(name, gs, arg, i, slist, cb->callback);
263 i++;
264 } END_FOR_EACH_PTR(arg);
265 free_slist(&slist);
266 } END_FOR_EACH_PTR(cb);
268 free_string(name);
271 static int get_param(int param, char **name, struct symbol **sym)
273 struct symbol *arg;
274 int i;
276 i = 0;
277 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
279 * this is a temporary hack to work around a bug (I think in sparse?)
280 * 2.6.37-rc1:fs/reiserfs/journal.o
281 * If there is a function definition without parameter name found
282 * after a function implementation then it causes a crash.
283 * int foo() {}
284 * int bar(char *);
286 if (arg->ident->name < (char *)100)
287 continue;
288 if (i == param && arg->ident->name) {
289 *name = arg->ident->name;
290 *sym = arg;
291 return TRUE;
293 i++;
294 } END_FOR_EACH_PTR(arg);
296 return FALSE;
299 static struct state_list *final_states;
300 static int prev_func_id = -1;
301 static int db_callback(void *unused, int argc, char **argv, char **azColName)
303 int func_id;
304 long type;
305 long param;
306 char *name = NULL;
307 struct symbol *sym = NULL;
308 struct def_callback *def_callback;
310 if (argc != 5)
311 return 0;
313 func_id = atoi(argv[0]);
314 errno = 0;
315 type = strtol(argv[1], NULL, 10);
316 param = strtol(argv[2], NULL, 10);
317 if (errno)
318 return 0;
320 if (prev_func_id == -1)
321 prev_func_id = func_id;
322 if (func_id != prev_func_id) {
323 merge_slist(&final_states, __pop_fake_cur_slist());
324 __push_fake_cur_slist();
325 __unnullify_path();
326 prev_func_id = func_id;
329 if (type == INTERNAL)
330 return 0;
331 if (param >= 0 && !get_param(param, &name, &sym))
332 return 0;
334 FOR_EACH_PTR(callbacks, def_callback) {
335 if (def_callback->hook_type == type)
336 def_callback->callback(name, sym, argv[3], argv[4]);
337 } END_FOR_EACH_PTR(def_callback);
339 return 0;
342 static void get_direct_callers(struct symbol *sym)
344 char sql_filter[1024];
346 if (sym->ctype.modifiers & MOD_STATIC) {
347 snprintf(sql_filter, 1024,
348 "file = '%s' and function = '%s' order by function_id;",
349 get_filename(), sym->ident->name);
350 } else {
351 snprintf(sql_filter, 1024,
352 "function = '%s' and static = 0 order by function_id;",
353 sym->ident->name);
356 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
357 " where %s", sql_filter);
360 static char *ptr_name;
361 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
363 if (!ptr_name)
364 ptr_name = alloc_string(argv[0]);
365 return 0;
368 static void get_function_pointer_callers(struct symbol *sym)
370 char sql_filter[1024];
372 if (sym->ctype.modifiers & MOD_STATIC) {
373 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
374 get_filename(), sym->ident->name);
375 } else {
376 snprintf(sql_filter, 1024, "function = '%s';",
377 sym->ident->name);
380 ptr_name = NULL;
381 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
382 if (!ptr_name)
383 return;
385 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
386 " where function = '%s' order by function_id", ptr_name);
388 free_string(ptr_name);
391 static void match_data_from_db(struct symbol *sym)
393 struct sm_state *sm;
395 if (!sym || !sym->ident || !sym->ident->name)
396 return;
398 __push_fake_cur_slist();
399 __unnullify_path();
400 prev_func_id = -1;
402 get_direct_callers(sym);
403 get_function_pointer_callers(sym);
405 merge_slist(&final_states, __pop_fake_cur_slist());
407 FOR_EACH_PTR(final_states, sm) {
408 __set_sm(sm);
409 } END_FOR_EACH_PTR(sm);
411 free_slist(&final_states);
414 static void match_function_assign(struct expression *expr)
416 struct expression *right = expr->right;
417 struct symbol *sym;
418 char *fn_name;
419 char *ptr_name;
421 if (right->type == EXPR_PREOP && right->op == '&')
422 right = strip_expr(right->unop);
423 if (right->type != EXPR_SYMBOL)
424 return;
425 sym = get_type(right);
426 if (!sym || sym->type != SYM_FN)
427 return;
429 fn_name = expr_to_var(right);
430 ptr_name = get_fnptr_name(expr->left);
431 if (!fn_name || !ptr_name)
432 goto free;
434 sql_insert_function_ptr(fn_name, ptr_name);
436 free:
437 free_string(fn_name);
438 free_string(ptr_name);
441 static struct expression *call_implies_call_expr;
442 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
444 struct call_implies_callback *cb;
445 struct expression *arg = NULL;
446 int type;
447 int param;
449 if (argc != 4)
450 return 0;
452 type = atoi(argv[1]);
453 param = atoi(argv[2]);
455 FOR_EACH_PTR(call_implies_cb_list, cb) {
456 if (cb->type != type)
457 continue;
458 if (param != -1) {
459 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
460 if (!arg)
461 continue;
463 cb->callback(arg, argv[3]);
464 } END_FOR_EACH_PTR(cb);
466 return 0;
469 static void match_call_implies(struct expression *expr)
471 struct symbol *sym;
472 static char sql_filter[1024];
474 if (expr->fn->type != EXPR_SYMBOL)
475 return;
476 sym = expr->fn->symbol;
477 if (!sym)
478 return;
480 if (sym->ctype.modifiers & MOD_STATIC) {
481 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
482 get_filename(), sym->ident->name);
483 } else {
484 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
485 sym->ident->name);
488 call_implies_call_expr = expr;
489 run_sql(call_implies_callbacks,
490 "select function, type, parameter, value from call_implies where %s",
491 sql_filter);
492 return;
495 static void print_initializer_list(struct expression_list *expr_list,
496 struct symbol *struct_type)
498 struct expression *expr;
499 struct symbol *base_type;
500 char struct_name[256];
502 FOR_EACH_PTR(expr_list, expr) {
503 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
504 print_initializer_list(expr->idx_expression->expr_list, struct_type);
505 continue;
507 if (expr->type != EXPR_IDENTIFIER)
508 continue;
509 if (!expr->expr_ident)
510 continue;
511 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
512 continue;
513 base_type = get_type(expr->ident_expression);
514 if (!base_type || base_type->type != SYM_FN)
515 continue;
516 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
517 struct_type->ident->name, expr->expr_ident->name);
518 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
519 struct_name);
520 } END_FOR_EACH_PTR(expr);
523 static void global_variable(struct symbol *sym)
525 struct symbol *struct_type;
527 if (!sym->ident)
528 return;
529 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
530 return;
531 struct_type = get_base_type(sym);
532 if (!struct_type)
533 return;
534 if (struct_type->type == SYM_ARRAY) {
535 struct_type = get_base_type(struct_type);
536 if (!struct_type)
537 return;
539 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
540 return;
541 print_initializer_list(sym->initializer->expr_list, struct_type);
544 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
546 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
549 static int return_id;
550 static void match_function_def(struct symbol *sym)
552 return_id = 0;
555 static void call_return_state_hooks_compare(struct expression *expr)
557 struct returned_state_callback *cb;
558 struct state_list *slist;
559 char *return_ranges;
560 int final_pass_orig = final_pass;
562 __push_fake_cur_slist();
564 final_pass = 0;
565 __split_whole_condition(expr);
566 final_pass = final_pass_orig;
568 return_ranges = alloc_sname("1");
570 return_id++;
571 slist = __get_cur_slist();
572 FOR_EACH_PTR(returned_state_callbacks, cb) {
573 cb->callback(return_id, return_ranges, expr, slist);
574 } END_FOR_EACH_PTR(cb);
576 __push_true_states();
577 __use_false_states();
579 return_ranges = alloc_sname("0");;
580 return_id++;
581 slist = __get_cur_slist();
582 FOR_EACH_PTR(returned_state_callbacks, cb) {
583 cb->callback(return_id, return_ranges, expr, slist);
584 } END_FOR_EACH_PTR(cb);
586 __merge_true_states();
587 __pop_fake_cur_slist();
590 static int call_return_state_hooks_split_possible(struct expression *expr)
592 struct returned_state_callback *cb;
593 struct state_list *slist;
594 struct range_list *rl;
595 char *return_ranges;
596 struct sm_state *sm;
597 struct sm_state *tmp;
598 int ret = 0;
599 int nr_possible, nr_states;
601 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
602 if (!sm || !sm->merged)
603 return 0;
605 /* bail if it gets too complicated */
606 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
607 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
608 if (nr_possible >= 100)
609 return 0;
610 if (nr_states * nr_possible >= 1000)
611 return 0;
613 FOR_EACH_PTR(sm->possible, tmp) {
614 if (tmp->merged)
615 continue;
617 ret = 1;
618 __push_fake_cur_slist();
620 overwrite_states_using_pool(tmp);
622 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
623 return_ranges = show_rl(rl);
625 return_id++;
626 slist = __get_cur_slist();
627 FOR_EACH_PTR(returned_state_callbacks, cb) {
628 cb->callback(return_id, return_ranges, expr, slist);
629 } END_FOR_EACH_PTR(cb);
631 __pop_fake_cur_slist();
632 } END_FOR_EACH_PTR(tmp);
634 return ret;
637 static void call_return_state_hooks(struct expression *expr)
639 struct returned_state_callback *cb;
640 struct state_list *slist;
641 struct range_list *rl;
642 char *return_ranges;
643 int nr_states;
645 expr = strip_expr(expr);
647 if (!expr) {
648 return_ranges = alloc_sname("");
649 } else if (is_condition(expr)) {
650 call_return_state_hooks_compare(expr);
651 return;
652 } else if (call_return_state_hooks_split_possible(expr)) {
653 return;
654 } else if (get_implied_rl(expr, &rl)) {
655 rl = cast_rl(cur_func_return_type(), rl);
656 return_ranges = show_rl(rl);
657 } else {
658 rl = alloc_whole_rl(cur_func_return_type());
659 return_ranges = show_rl(rl);
662 return_id++;
663 slist = __get_cur_slist();
664 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
665 FOR_EACH_PTR(returned_state_callbacks, cb) {
666 if (nr_states < 10000)
667 cb->callback(return_id, return_ranges, expr, slist);
668 else
669 cb->callback(return_id, return_ranges, expr, NULL);
670 } END_FOR_EACH_PTR(cb);
673 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
675 struct returned_member_callback *cb;
676 struct state_list *my_slist;
677 struct sm_state *sm;
678 struct symbol *type;
679 char *name;
680 char member_name[256];
681 int len;
683 type = get_type(expr);
684 if (!type || type->type != SYM_PTR)
685 return;
686 type = get_real_base_type(type);
687 if (!type || type->type != SYM_STRUCT)
688 return;
689 name = expr_to_var(expr);
690 if (!name)
691 return;
693 member_name[sizeof(member_name) - 1] = '\0';
694 strcpy(member_name, "$$");
696 len = strlen(name);
697 FOR_EACH_PTR(returned_member_callbacks, cb) {
698 my_slist = get_all_states_slist(cb->owner, slist);
699 FOR_EACH_PTR(my_slist, sm) {
700 if (strncmp(sm->name, name, len) != 0)
701 continue;
702 if (strncmp(sm->name + len, "->", 2) != 0)
703 continue;
704 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
705 cb->callback(return_id, return_ranges, member_name, sm->state);
706 } END_FOR_EACH_PTR(sm);
707 free_slist(&my_slist);
708 } END_FOR_EACH_PTR(cb);
710 free_string(name);
713 static void match_end_func_info(struct symbol *sym)
715 if (__path_is_null())
716 return;
717 call_return_state_hooks(NULL);
720 void open_smatch_db(void)
722 #ifdef SQLITE_OPEN_READONLY
723 int rc;
725 if (option_no_db)
726 return;
728 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
729 if (rc != SQLITE_OK) {
730 option_no_db = 1;
731 return;
733 return;
734 #else
735 option_no_db = 1;
736 return;
737 #endif
740 void register_definition_db_callbacks(int id)
742 add_hook(&match_function_def, FUNC_DEF_HOOK);
744 if (option_info) {
745 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
746 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
747 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
748 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
749 add_hook(&global_variable, BASE_HOOK);
750 add_hook(&global_variable, DECLARATION_HOOK);
751 add_returned_state_callback(match_return_info);
752 add_returned_state_callback(print_returned_struct_members);
753 add_hook(&call_return_state_hooks, RETURN_HOOK);
754 add_hook(&match_end_func_info, END_FUNC_HOOK);
757 if (option_no_db)
758 return;
760 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
761 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
764 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
766 char buf[256];
767 char *tmp;
769 if (strcmp(key, "$$") == 0)
770 return expr_to_var_sym(arg, sym);
772 if (strcmp(key, "*$$") == 0) {
773 if (arg->type == EXPR_PREOP && arg->op == '&') {
774 arg = strip_expr(arg->unop);
775 return expr_to_var_sym(arg, sym);
776 } else {
777 tmp = expr_to_var_sym(arg, sym);
778 if (!tmp)
779 return NULL;
780 snprintf(buf, sizeof(buf), "*%s", tmp);
781 free_string(tmp);
782 return alloc_string(buf);
786 if (arg->type == EXPR_PREOP && arg->op == '&') {
787 arg = strip_expr(arg->unop);
788 tmp = expr_to_var_sym(arg, sym);
789 if (!tmp)
790 return NULL;
791 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
792 return alloc_string(buf);
795 tmp = expr_to_var_sym(arg, sym);
796 if (!tmp)
797 return NULL;
798 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
799 free_string(tmp);
800 return alloc_string(buf);