Revert "db: don't create unused tables in the in-memory database"
[smatch.git] / smatch_db.c
blobc600002a97ae861a4a3ebf790c6a832095b3e801
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 <unistd.h>
14 #include "smatch.h"
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
18 static sqlite3 *db;
19 static sqlite3 *mem_db;
21 /* like run_sql() but for the in-memory database */
22 #define mem_sql(call_back, sql...) \
23 do { \
24 char sql_txt[1024]; \
25 int rc; \
26 char *err = NULL; \
28 snprintf(sql_txt, sizeof(sql_txt), sql); \
29 sm_debug("in-mem: %s\n", sql_txt); \
30 rc = sqlite3_exec(mem_db, sql_txt, call_back, 0, &err); \
31 if (rc != SQLITE_OK) { \
32 fprintf(stderr, "SQL error #2: %s\n", err); \
33 fprintf(stderr, "SQL: '%s'\n", sql_txt); \
34 } \
35 } while (0)
37 #define sql_insert(table, values...) \
38 do { \
39 if (__inline_fn) { \
40 char buf[1024]; \
41 char *err, *p = buf; \
42 int rc; \
44 p += snprintf(p, buf + sizeof(buf) - p, \
45 "insert into %s values (", #table); \
46 p += snprintf(p, buf + sizeof(buf) - p, values); \
47 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
48 sm_debug("mem-db: %s\n", buf); \
49 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
50 if (rc != SQLITE_OK) { \
51 fprintf(stderr, "SQL error #2: %s\n", err); \
52 fprintf(stderr, "SQL: '%s'\n", buf); \
53 } \
54 return; \
55 } \
56 if (option_info) { \
57 sm_prefix(); \
58 sm_printf("SQL: insert into " #table " values (" values); \
59 sm_printf(");\n"); \
60 } \
61 } while (0)
63 struct def_callback {
64 int hook_type;
65 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
67 ALLOCATOR(def_callback, "definition db hook callbacks");
68 DECLARE_PTR_LIST(callback_list, struct def_callback);
69 static struct callback_list *callbacks;
71 struct member_info_callback {
72 int owner;
73 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
75 ALLOCATOR(member_info_callback, "caller_info callbacks");
76 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
77 static struct member_info_cb_list *member_callbacks;
79 struct returned_state_callback {
80 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
82 ALLOCATOR(returned_state_callback, "returned state callbacks");
83 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
84 static struct returned_state_cb_list *returned_state_callbacks;
86 struct returned_member_callback {
87 int owner;
88 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
90 ALLOCATOR(returned_member_callback, "returned member callbacks");
91 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
92 static struct returned_member_cb_list *returned_member_callbacks;
94 struct call_implies_callback {
95 int type;
96 void (*callback)(struct expression *arg, char *value);
98 ALLOCATOR(call_implies_callback, "call_implies callbacks");
99 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
100 static struct call_implies_cb_list *call_implies_cb_list;
102 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
104 char *err = NULL;
105 int rc;
107 if (option_no_db || !db)
108 return;
110 rc = sqlite3_exec(db, sql, callback, 0, &err);
111 if (rc != SQLITE_OK) {
112 fprintf(stderr, "SQL error #2: %s\n", err);
113 fprintf(stderr, "SQL: '%s'\n", sql);
117 void sql_insert_return_states(int return_id, const char *return_ranges,
118 int type, int param, const char *key, const char *value)
120 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
121 get_filename(), get_function(), (unsigned long)__inline_fn,
122 return_id, return_ranges, fn_static(), type, param, key, value);
125 void sql_insert_caller_info(struct expression *call, int type,
126 int param, const char *key, const char *value)
128 char *fn;
130 if (!option_info && !__inline_call)
131 return;
133 fn = get_fnptr_name(call->fn);
134 if (!fn)
135 return;
137 if (__inline_call) {
138 char buf[1024];
139 char *err;
140 int rc;
142 snprintf(buf, sizeof(buf), "insert into caller_info values ("
143 "'%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
144 get_filename(), get_function(), fn,
145 (unsigned long)call, is_static(call->fn), type, param,
146 key, value);
147 sm_debug("in-mem: %s\n", buf);
148 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
149 if (rc != SQLITE_OK) {
150 fprintf(stderr, "SQL error #2: %s\n", err);
151 fprintf(stderr, "SQL: '%s'\n", buf);
155 if (!option_info)
156 return;
158 if (strncmp(fn, "__builtin_", 10) == 0)
159 return;
161 sm_msg("SQL_caller_info: insert into caller_info values ("
162 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
163 get_filename(), get_function(), fn, is_static(call->fn),
164 type, param, key, value);
166 free_string(fn);
169 void sql_insert_function_ptr(const char *fn, const char *struct_name)
171 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
172 struct_name);
175 void sql_insert_return_values(const char *return_values)
177 sql_insert(return_values, "'%s', '%s', %lu, %d, '%s'", get_filename(),
178 get_function(), (unsigned long)__inline_fn, fn_static(),
179 return_values);
182 void sql_insert_call_implies(int type, int param, int value)
184 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_filename(),
185 get_function(), (unsigned long)__inline_fn, fn_static(),
186 type, param, value);
189 void sql_insert_type_size(const char *member, int size)
191 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
194 static char *get_static_filter(struct symbol *sym)
196 static char sql_filter[1024];
198 if (sym->ctype.modifiers & MOD_STATIC) {
199 snprintf(sql_filter, sizeof(sql_filter),
200 "file = '%s' and function = '%s' and static = '1'",
201 get_filename(), sym->ident->name);
202 } else {
203 snprintf(sql_filter, sizeof(sql_filter),
204 "function = '%s' and static = '0'", sym->ident->name);
207 return sql_filter;
210 void sql_select_return_states(const char *cols, struct expression *call,
211 int (*callback)(void*, int, char**, char**))
213 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
214 return;
216 if (inlinable(call->fn)) {
217 mem_sql(callback,
218 "select %s from return_states where call_id = '%lu';",
219 cols, (unsigned long)call);
220 return;
223 run_sql(callback, "select %s from return_states where %s;",
224 cols, get_static_filter(call->fn->symbol));
227 void sql_select_call_implies(const char *cols, struct expression *call,
228 int (*callback)(void*, int, char**, char**))
230 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
231 return;
233 if (inlinable(call->fn)) {
234 mem_sql(callback,
235 "select %s from call_implies where call_id = '%lu';",
236 cols, (unsigned long)call);
237 return;
240 run_sql(callback, "select %s from call_implies where %s;",
241 cols, get_static_filter(call->fn->symbol));
244 void sql_select_return_values(const char *cols, struct expression *call,
245 int (*callback)(void*, int, char**, char**))
247 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
248 return;
250 if (inlinable(call->fn)) {
251 mem_sql(callback,
252 "select %s from return_values where call_id = '%lu';",
253 cols, (unsigned long)call);
254 return;
257 run_sql(callback, "select %s from return_values where %s;",
258 cols, get_static_filter(call->fn->symbol));
261 void sql_select_caller_info(const char *cols, struct symbol *sym,
262 int (*callback)(void*, int, char**, char**))
264 if (__inline_fn) {
265 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
266 cols, (unsigned long)__inline_fn);
267 return;
270 run_sql(callback,
271 "select %s from caller_info where %s order by call_id;",
272 cols, get_static_filter(sym));
275 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
277 struct def_callback *def_callback = __alloc_def_callback(0);
279 def_callback->hook_type = type;
280 def_callback->callback = callback;
281 add_ptr_list(&callbacks, def_callback);
285 * These call backs are used when the --info option is turned on to print struct
286 * member information. For example foo->bar could have a state in
287 * smatch_extra.c and also check_user.c.
289 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
291 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
293 member_callback->owner = owner;
294 member_callback->callback = callback;
295 add_ptr_list(&member_callbacks, member_callback);
298 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
300 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
302 callback->callback = fn;
303 add_ptr_list(&returned_state_callbacks, callback);
306 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
308 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
310 member_callback->owner = owner;
311 member_callback->callback = callback;
312 add_ptr_list(&returned_member_callbacks, member_callback);
315 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
317 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
319 cb->type = type;
320 cb->callback = callback;
321 add_ptr_list(&call_implies_cb_list, cb);
324 static struct symbol *return_type;
325 static struct range_list *return_range_list;
326 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
328 if (argc != 1)
329 return 0;
330 if (option_debug)
331 sm_msg("return type %d", type_positive_bits(return_type));
332 str_to_rl(return_type, argv[0], &return_range_list);
333 return 0;
336 struct range_list *db_return_vals(struct expression *expr)
338 return_type = get_type(expr);
339 if (!return_type)
340 return NULL;
341 return_range_list = NULL;
342 sql_select_return_values("return", expr, db_return_callback);
343 return return_range_list;
346 static void match_call_marker(struct expression *expr)
349 * we just want to record something in the database so that if we have
350 * two calls like: frob(4); frob(some_unkown); then on the receiving
351 * side we know that sometimes frob is called with unknown parameters.
354 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
357 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
358 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
360 struct sm_state *sm;
361 char *name;
362 struct symbol *sym;
363 int len;
364 char printed_name[256];
365 int is_address = 0;
367 expr = strip_expr(expr);
368 if (expr->type == EXPR_PREOP && expr->op == '&') {
369 expr = strip_expr(expr->unop);
370 is_address = 1;
373 name = expr_to_var_sym(expr, &sym);
374 if (!name || !sym)
375 goto free;
377 len = strlen(name);
378 FOR_EACH_PTR(slist, sm) {
379 if (sm->sym != sym)
380 continue;
381 if (strcmp(name, sm->name) == 0) {
382 if (is_address)
383 snprintf(printed_name, sizeof(printed_name), "*$$");
384 else /* these are already handled. fixme: handle them here */
385 continue;
386 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
387 snprintf(printed_name, sizeof(printed_name), "*$$");
388 } else if (strncmp(name, sm->name, len) == 0) {
389 if (is_address)
390 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
391 else
392 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
393 } else {
394 continue;
396 callback(call, param, printed_name, sm->state);
397 } END_FOR_EACH_PTR(sm);
398 free:
399 free_string(name);
402 static void match_call_info(struct expression *call)
404 struct member_info_callback *cb;
405 struct expression *arg;
406 struct state_list *slist;
407 char *name;
408 int i;
410 name = get_fnptr_name(call->fn);
411 if (!name)
412 return;
414 FOR_EACH_PTR(member_callbacks, cb) {
415 slist = get_all_states(cb->owner);
416 i = 0;
417 FOR_EACH_PTR(call->args, arg) {
418 print_struct_members(call, arg, i, slist, cb->callback);
419 i++;
420 } END_FOR_EACH_PTR(arg);
421 free_slist(&slist);
422 } END_FOR_EACH_PTR(cb);
424 free_string(name);
427 static int get_param(int param, char **name, struct symbol **sym)
429 struct symbol *arg;
430 int i;
432 i = 0;
433 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
435 * this is a temporary hack to work around a bug (I think in sparse?)
436 * 2.6.37-rc1:fs/reiserfs/journal.o
437 * If there is a function definition without parameter name found
438 * after a function implementation then it causes a crash.
439 * int foo() {}
440 * int bar(char *);
442 if (arg->ident->name < (char *)100)
443 continue;
444 if (i == param && arg->ident->name) {
445 *name = arg->ident->name;
446 *sym = arg;
447 return TRUE;
449 i++;
450 } END_FOR_EACH_PTR(arg);
452 return FALSE;
455 static struct state_list *final_states;
456 static int prev_func_id = -1;
457 static int db_callback(void *unused, int argc, char **argv, char **azColName)
459 int func_id;
460 long type;
461 long param;
462 char *name = NULL;
463 struct symbol *sym = NULL;
464 struct def_callback *def_callback;
466 if (argc != 5)
467 return 0;
469 func_id = atoi(argv[0]);
470 errno = 0;
471 type = strtol(argv[1], NULL, 10);
472 param = strtol(argv[2], NULL, 10);
473 if (errno)
474 return 0;
476 if (prev_func_id == -1)
477 prev_func_id = func_id;
478 if (func_id != prev_func_id) {
479 merge_slist(&final_states, __pop_fake_cur_slist());
480 __push_fake_cur_slist();
481 __unnullify_path();
482 prev_func_id = func_id;
485 if (type == INTERNAL)
486 return 0;
487 if (param >= 0 && !get_param(param, &name, &sym))
488 return 0;
490 FOR_EACH_PTR(callbacks, def_callback) {
491 if (def_callback->hook_type == type)
492 def_callback->callback(name, sym, argv[3], argv[4]);
493 } END_FOR_EACH_PTR(def_callback);
495 return 0;
498 static void get_direct_callers(struct symbol *sym)
500 sql_select_caller_info("call_id, type, parameter, key, value", sym,
501 db_callback);
504 static char *ptr_name;
505 static int ptr_cnt;
506 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
508 if (ptr_cnt++) {
509 free_string(ptr_name);
510 ptr_name = NULL;
511 return 0;
513 if (!ptr_name)
514 ptr_name = alloc_string(argv[0]);
515 return 0;
518 static void get_function_pointer_callers(struct symbol *sym)
520 char sql_filter[1024];
522 if (sym->ctype.modifiers & MOD_STATIC) {
523 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
524 get_filename(), sym->ident->name);
525 } else {
526 snprintf(sql_filter, 1024, "function = '%s';",
527 sym->ident->name);
530 ptr_name = NULL;
531 ptr_cnt = 0;
532 run_sql(get_ptr_name,
533 "select distinct ptr from function_ptr where %s",
534 sql_filter);
535 if (!ptr_name)
536 return;
538 run_sql(db_callback, "select call_id, type, parameter, key, value from caller_info"
539 " where function = '%s' order by call_id", ptr_name);
541 free_string(ptr_name);
544 static void match_data_from_db(struct symbol *sym)
546 struct sm_state *sm;
548 if (!sym || !sym->ident || !sym->ident->name)
549 return;
551 __push_fake_cur_slist();
552 __unnullify_path();
553 prev_func_id = -1;
555 get_direct_callers(sym);
556 if (!__inline_fn)
557 get_function_pointer_callers(sym);
559 merge_slist(&final_states, __pop_fake_cur_slist());
561 FOR_EACH_PTR(final_states, sm) {
562 __set_sm(sm);
563 } END_FOR_EACH_PTR(sm);
565 free_slist(&final_states);
568 static void match_function_assign(struct expression *expr)
570 struct expression *right = expr->right;
571 struct symbol *sym;
572 char *fn_name;
573 char *ptr_name;
575 if (right->type == EXPR_PREOP && right->op == '&')
576 right = strip_expr(right->unop);
577 if (right->type != EXPR_SYMBOL)
578 return;
579 sym = get_type(right);
580 if (!sym || sym->type != SYM_FN)
581 return;
583 fn_name = expr_to_var(right);
584 ptr_name = get_fnptr_name(expr->left);
585 if (!fn_name || !ptr_name)
586 goto free;
588 sql_insert_function_ptr(fn_name, ptr_name);
590 free:
591 free_string(fn_name);
592 free_string(ptr_name);
595 static struct expression *call_implies_call_expr;
596 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
598 struct call_implies_callback *cb;
599 struct expression *arg = NULL;
600 int type;
601 int param;
603 if (argc != 4)
604 return 0;
606 type = atoi(argv[1]);
607 param = atoi(argv[2]);
609 FOR_EACH_PTR(call_implies_cb_list, cb) {
610 if (cb->type != type)
611 continue;
612 if (param != -1) {
613 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
614 if (!arg)
615 continue;
617 cb->callback(arg, argv[3]);
618 } END_FOR_EACH_PTR(cb);
620 return 0;
623 static void match_call_implies(struct expression *expr)
625 call_implies_call_expr = expr;
626 sql_select_call_implies("function, type, parameter, value", expr,
627 call_implies_callbacks);
628 return;
631 static void print_initializer_list(struct expression_list *expr_list,
632 struct symbol *struct_type)
634 struct expression *expr;
635 struct symbol *base_type;
636 char struct_name[256];
638 FOR_EACH_PTR(expr_list, expr) {
639 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
640 print_initializer_list(expr->idx_expression->expr_list, struct_type);
641 continue;
643 if (expr->type != EXPR_IDENTIFIER)
644 continue;
645 if (!expr->expr_ident)
646 continue;
647 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
648 continue;
649 base_type = get_type(expr->ident_expression);
650 if (!base_type || base_type->type != SYM_FN)
651 continue;
652 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
653 struct_type->ident->name, expr->expr_ident->name);
654 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
655 struct_name);
656 } END_FOR_EACH_PTR(expr);
659 static void global_variable(struct symbol *sym)
661 struct symbol *struct_type;
663 if (!sym->ident)
664 return;
665 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
666 return;
667 struct_type = get_base_type(sym);
668 if (!struct_type)
669 return;
670 if (struct_type->type == SYM_ARRAY) {
671 struct_type = get_base_type(struct_type);
672 if (!struct_type)
673 return;
675 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
676 return;
677 print_initializer_list(sym->initializer->expr_list, struct_type);
680 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
682 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
685 static int return_id;
686 static void match_function_def(struct symbol *sym)
688 return_id = 0;
691 static void call_return_state_hooks_compare(struct expression *expr)
693 struct returned_state_callback *cb;
694 struct state_list *slist;
695 char *return_ranges;
696 int final_pass_orig = final_pass;
698 __push_fake_cur_slist();
700 final_pass = 0;
701 __split_whole_condition(expr);
702 final_pass = final_pass_orig;
704 return_ranges = alloc_sname("1");
706 return_id++;
707 slist = __get_cur_slist();
708 FOR_EACH_PTR(returned_state_callbacks, cb) {
709 cb->callback(return_id, return_ranges, expr, slist);
710 } END_FOR_EACH_PTR(cb);
712 __push_true_states();
713 __use_false_states();
715 return_ranges = alloc_sname("0");;
716 return_id++;
717 slist = __get_cur_slist();
718 FOR_EACH_PTR(returned_state_callbacks, cb) {
719 cb->callback(return_id, return_ranges, expr, slist);
720 } END_FOR_EACH_PTR(cb);
722 __merge_true_states();
723 __pop_fake_cur_slist();
726 static int call_return_state_hooks_split_possible(struct expression *expr)
728 struct returned_state_callback *cb;
729 struct state_list *slist;
730 struct range_list *rl;
731 char *return_ranges;
732 struct sm_state *sm;
733 struct sm_state *tmp;
734 int ret = 0;
735 int nr_possible, nr_states;
737 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
738 if (!sm || !sm->merged)
739 return 0;
741 /* bail if it gets too complicated */
742 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
743 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
744 if (nr_possible >= 100)
745 return 0;
746 if (nr_states * nr_possible >= 1000)
747 return 0;
749 FOR_EACH_PTR(sm->possible, tmp) {
750 if (tmp->merged)
751 continue;
753 ret = 1;
754 __push_fake_cur_slist();
756 overwrite_states_using_pool(tmp);
758 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
759 return_ranges = show_rl(rl);
761 return_id++;
762 slist = __get_cur_slist();
763 FOR_EACH_PTR(returned_state_callbacks, cb) {
764 cb->callback(return_id, return_ranges, expr, slist);
765 } END_FOR_EACH_PTR(cb);
767 __pop_fake_cur_slist();
768 } END_FOR_EACH_PTR(tmp);
770 return ret;
773 static void call_return_state_hooks(struct expression *expr)
775 struct returned_state_callback *cb;
776 struct state_list *slist;
777 struct range_list *rl;
778 char *return_ranges;
779 int nr_states;
781 expr = strip_expr(expr);
783 if (!expr) {
784 return_ranges = alloc_sname("");
785 } else if (is_condition(expr)) {
786 call_return_state_hooks_compare(expr);
787 return;
788 } else if (call_return_state_hooks_split_possible(expr)) {
789 return;
790 } else if (get_implied_rl(expr, &rl)) {
791 rl = cast_rl(cur_func_return_type(), rl);
792 return_ranges = show_rl(rl);
793 } else {
794 rl = alloc_whole_rl(cur_func_return_type());
795 return_ranges = show_rl(rl);
798 return_id++;
799 slist = __get_cur_slist();
800 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
801 FOR_EACH_PTR(returned_state_callbacks, cb) {
802 if (nr_states < 10000)
803 cb->callback(return_id, return_ranges, expr, slist);
804 else
805 cb->callback(return_id, return_ranges, expr, NULL);
806 } END_FOR_EACH_PTR(cb);
809 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
811 struct returned_member_callback *cb;
812 struct state_list *my_slist;
813 struct sm_state *sm;
814 struct symbol *type;
815 char *name;
816 char member_name[256];
817 int len;
819 type = get_type(expr);
820 if (!type || type->type != SYM_PTR)
821 return;
822 type = get_real_base_type(type);
823 if (!type || type->type != SYM_STRUCT)
824 return;
825 name = expr_to_var(expr);
826 if (!name)
827 return;
829 member_name[sizeof(member_name) - 1] = '\0';
830 strcpy(member_name, "$$");
832 len = strlen(name);
833 FOR_EACH_PTR(returned_member_callbacks, cb) {
834 my_slist = get_all_states_slist(cb->owner, slist);
835 FOR_EACH_PTR(my_slist, sm) {
836 if (strncmp(sm->name, name, len) != 0)
837 continue;
838 if (strncmp(sm->name + len, "->", 2) != 0)
839 continue;
840 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
841 cb->callback(return_id, return_ranges, member_name, sm->state);
842 } END_FOR_EACH_PTR(sm);
843 free_slist(&my_slist);
844 } END_FOR_EACH_PTR(cb);
846 free_string(name);
849 static void reset_memdb(void)
851 mem_sql(NULL, "delete from caller_info;");
852 mem_sql(NULL, "delete from return_states;");
853 mem_sql(NULL, "delete from call_implies;");
854 mem_sql(NULL, "delete from return_values;");
857 static void match_end_func_info(struct symbol *sym)
859 if (__path_is_null())
860 return;
861 call_return_state_hooks(NULL);
862 if (!__inline_fn)
863 reset_memdb();
866 static void init_memdb(void)
868 char *err = NULL;
869 int rc;
870 const char *schema_files[] = {
871 "db/db.schema",
872 "db/caller_info.schema",
873 "db/return_states.schema",
874 "db/type_size.schema",
875 "db/call_implies.schema",
876 "db/function_ptr.schema",
877 "db/return_values.schema",
879 static char buf[4096];
880 int fd;
881 int ret;
882 int i;
884 rc = sqlite3_open(":memory:", &mem_db);
885 if (rc != SQLITE_OK) {
886 printf("Error starting In-Memory database.");
887 return;
890 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
891 fd = open_data_file(schema_files[i]);
892 if (fd < 0)
893 continue;
894 ret = read(fd, buf, sizeof(buf));
895 if (ret == sizeof(buf)) {
896 printf("Schema file too large: %s (limit %zd bytes)",
897 schema_files[i], sizeof(buf));
899 buf[ret] = '\0';
900 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
901 if (rc != SQLITE_OK) {
902 fprintf(stderr, "SQL error #2: %s\n", err);
903 fprintf(stderr, "%s\n", buf);
908 void open_smatch_db(void)
910 int rc;
912 if (option_no_db)
913 return;
915 init_memdb();
917 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
918 if (rc != SQLITE_OK) {
919 option_no_db = 1;
920 return;
922 return;
925 void register_definition_db_callbacks(int id)
927 add_hook(&match_function_def, FUNC_DEF_HOOK);
929 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
930 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
931 add_hook(&global_variable, BASE_HOOK);
932 add_hook(&global_variable, DECLARATION_HOOK);
933 add_returned_state_callback(match_return_info);
934 add_returned_state_callback(print_returned_struct_members);
935 add_hook(&call_return_state_hooks, RETURN_HOOK);
936 add_hook(&match_end_func_info, END_FUNC_HOOK);
938 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
939 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
942 void register_db_call_marker(int id)
944 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
947 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
949 char buf[256];
950 char *tmp;
952 if (strcmp(key, "$$") == 0)
953 return expr_to_var_sym(arg, sym);
955 if (strcmp(key, "*$$") == 0) {
956 if (arg->type == EXPR_PREOP && arg->op == '&') {
957 arg = strip_expr(arg->unop);
958 return expr_to_var_sym(arg, sym);
959 } else {
960 tmp = expr_to_var_sym(arg, sym);
961 if (!tmp)
962 return NULL;
963 snprintf(buf, sizeof(buf), "*%s", tmp);
964 free_string(tmp);
965 return alloc_string(buf);
969 if (arg->type == EXPR_PREOP && arg->op == '&') {
970 arg = strip_expr(arg->unop);
971 tmp = expr_to_var_sym(arg, sym);
972 if (!tmp)
973 return NULL;
974 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
975 return alloc_string(buf);
978 tmp = expr_to_var_sym(arg, sym);
979 if (!tmp)
980 return NULL;
981 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
982 free_string(tmp);
983 return alloc_string(buf);