db: use "distinct" to avoid duplicate function pointers
[smatch.git] / smatch_db.c
blobe59b31b493752d4833888950ec79f405bf2a464e
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 get_function_pointer_callers(sym);
558 merge_slist(&final_states, __pop_fake_cur_slist());
560 FOR_EACH_PTR(final_states, sm) {
561 __set_sm(sm);
562 } END_FOR_EACH_PTR(sm);
564 free_slist(&final_states);
567 static void match_function_assign(struct expression *expr)
569 struct expression *right = expr->right;
570 struct symbol *sym;
571 char *fn_name;
572 char *ptr_name;
574 if (right->type == EXPR_PREOP && right->op == '&')
575 right = strip_expr(right->unop);
576 if (right->type != EXPR_SYMBOL)
577 return;
578 sym = get_type(right);
579 if (!sym || sym->type != SYM_FN)
580 return;
582 fn_name = expr_to_var(right);
583 ptr_name = get_fnptr_name(expr->left);
584 if (!fn_name || !ptr_name)
585 goto free;
587 sql_insert_function_ptr(fn_name, ptr_name);
589 free:
590 free_string(fn_name);
591 free_string(ptr_name);
594 static struct expression *call_implies_call_expr;
595 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
597 struct call_implies_callback *cb;
598 struct expression *arg = NULL;
599 int type;
600 int param;
602 if (argc != 4)
603 return 0;
605 type = atoi(argv[1]);
606 param = atoi(argv[2]);
608 FOR_EACH_PTR(call_implies_cb_list, cb) {
609 if (cb->type != type)
610 continue;
611 if (param != -1) {
612 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
613 if (!arg)
614 continue;
616 cb->callback(arg, argv[3]);
617 } END_FOR_EACH_PTR(cb);
619 return 0;
622 static void match_call_implies(struct expression *expr)
624 call_implies_call_expr = expr;
625 sql_select_call_implies("function, type, parameter, value", expr,
626 call_implies_callbacks);
627 return;
630 static void print_initializer_list(struct expression_list *expr_list,
631 struct symbol *struct_type)
633 struct expression *expr;
634 struct symbol *base_type;
635 char struct_name[256];
637 FOR_EACH_PTR(expr_list, expr) {
638 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
639 print_initializer_list(expr->idx_expression->expr_list, struct_type);
640 continue;
642 if (expr->type != EXPR_IDENTIFIER)
643 continue;
644 if (!expr->expr_ident)
645 continue;
646 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
647 continue;
648 base_type = get_type(expr->ident_expression);
649 if (!base_type || base_type->type != SYM_FN)
650 continue;
651 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
652 struct_type->ident->name, expr->expr_ident->name);
653 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
654 struct_name);
655 } END_FOR_EACH_PTR(expr);
658 static void global_variable(struct symbol *sym)
660 struct symbol *struct_type;
662 if (!sym->ident)
663 return;
664 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
665 return;
666 struct_type = get_base_type(sym);
667 if (!struct_type)
668 return;
669 if (struct_type->type == SYM_ARRAY) {
670 struct_type = get_base_type(struct_type);
671 if (!struct_type)
672 return;
674 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
675 return;
676 print_initializer_list(sym->initializer->expr_list, struct_type);
679 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
681 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
684 static int return_id;
685 static void match_function_def(struct symbol *sym)
687 return_id = 0;
690 static void call_return_state_hooks_compare(struct expression *expr)
692 struct returned_state_callback *cb;
693 struct state_list *slist;
694 char *return_ranges;
695 int final_pass_orig = final_pass;
697 __push_fake_cur_slist();
699 final_pass = 0;
700 __split_whole_condition(expr);
701 final_pass = final_pass_orig;
703 return_ranges = alloc_sname("1");
705 return_id++;
706 slist = __get_cur_slist();
707 FOR_EACH_PTR(returned_state_callbacks, cb) {
708 cb->callback(return_id, return_ranges, expr, slist);
709 } END_FOR_EACH_PTR(cb);
711 __push_true_states();
712 __use_false_states();
714 return_ranges = alloc_sname("0");;
715 return_id++;
716 slist = __get_cur_slist();
717 FOR_EACH_PTR(returned_state_callbacks, cb) {
718 cb->callback(return_id, return_ranges, expr, slist);
719 } END_FOR_EACH_PTR(cb);
721 __merge_true_states();
722 __pop_fake_cur_slist();
725 static int call_return_state_hooks_split_possible(struct expression *expr)
727 struct returned_state_callback *cb;
728 struct state_list *slist;
729 struct range_list *rl;
730 char *return_ranges;
731 struct sm_state *sm;
732 struct sm_state *tmp;
733 int ret = 0;
734 int nr_possible, nr_states;
736 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
737 if (!sm || !sm->merged)
738 return 0;
740 /* bail if it gets too complicated */
741 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
742 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
743 if (nr_possible >= 100)
744 return 0;
745 if (nr_states * nr_possible >= 1000)
746 return 0;
748 FOR_EACH_PTR(sm->possible, tmp) {
749 if (tmp->merged)
750 continue;
752 ret = 1;
753 __push_fake_cur_slist();
755 overwrite_states_using_pool(tmp);
757 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
758 return_ranges = show_rl(rl);
760 return_id++;
761 slist = __get_cur_slist();
762 FOR_EACH_PTR(returned_state_callbacks, cb) {
763 cb->callback(return_id, return_ranges, expr, slist);
764 } END_FOR_EACH_PTR(cb);
766 __pop_fake_cur_slist();
767 } END_FOR_EACH_PTR(tmp);
769 return ret;
772 static void call_return_state_hooks(struct expression *expr)
774 struct returned_state_callback *cb;
775 struct state_list *slist;
776 struct range_list *rl;
777 char *return_ranges;
778 int nr_states;
780 expr = strip_expr(expr);
782 if (!expr) {
783 return_ranges = alloc_sname("");
784 } else if (is_condition(expr)) {
785 call_return_state_hooks_compare(expr);
786 return;
787 } else if (call_return_state_hooks_split_possible(expr)) {
788 return;
789 } else if (get_implied_rl(expr, &rl)) {
790 rl = cast_rl(cur_func_return_type(), rl);
791 return_ranges = show_rl(rl);
792 } else {
793 rl = alloc_whole_rl(cur_func_return_type());
794 return_ranges = show_rl(rl);
797 return_id++;
798 slist = __get_cur_slist();
799 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
800 FOR_EACH_PTR(returned_state_callbacks, cb) {
801 if (nr_states < 10000)
802 cb->callback(return_id, return_ranges, expr, slist);
803 else
804 cb->callback(return_id, return_ranges, expr, NULL);
805 } END_FOR_EACH_PTR(cb);
808 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
810 struct returned_member_callback *cb;
811 struct state_list *my_slist;
812 struct sm_state *sm;
813 struct symbol *type;
814 char *name;
815 char member_name[256];
816 int len;
818 type = get_type(expr);
819 if (!type || type->type != SYM_PTR)
820 return;
821 type = get_real_base_type(type);
822 if (!type || type->type != SYM_STRUCT)
823 return;
824 name = expr_to_var(expr);
825 if (!name)
826 return;
828 member_name[sizeof(member_name) - 1] = '\0';
829 strcpy(member_name, "$$");
831 len = strlen(name);
832 FOR_EACH_PTR(returned_member_callbacks, cb) {
833 my_slist = get_all_states_slist(cb->owner, slist);
834 FOR_EACH_PTR(my_slist, sm) {
835 if (strncmp(sm->name, name, len) != 0)
836 continue;
837 if (strncmp(sm->name + len, "->", 2) != 0)
838 continue;
839 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
840 cb->callback(return_id, return_ranges, member_name, sm->state);
841 } END_FOR_EACH_PTR(sm);
842 free_slist(&my_slist);
843 } END_FOR_EACH_PTR(cb);
845 free_string(name);
848 static void reset_memdb(void)
850 mem_sql(NULL, "delete from caller_info;");
851 mem_sql(NULL, "delete from return_states;");
852 mem_sql(NULL, "delete from call_implies;");
853 mem_sql(NULL, "delete from return_values;");
856 static void match_end_func_info(struct symbol *sym)
858 if (__path_is_null())
859 return;
860 call_return_state_hooks(NULL);
861 if (!__inline_fn)
862 reset_memdb();
865 static void init_memdb(void)
867 char *err = NULL;
868 int rc;
869 const char *schema_files[] = {
870 "db/caller_info.schema",
871 "db/return_states.schema",
872 "db/call_implies.schema",
873 "db/return_values.schema",
875 static char buf[4096];
876 int fd;
877 int ret;
878 int i;
880 rc = sqlite3_open(":memory:", &mem_db);
881 if (rc != SQLITE_OK) {
882 printf("Error starting In-Memory database.");
883 return;
886 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
887 fd = open_data_file(schema_files[i]);
888 if (fd < 0)
889 continue;
890 ret = read(fd, buf, sizeof(buf));
891 if (ret == sizeof(buf)) {
892 printf("Schema file too large: %s (limit %zd bytes)",
893 schema_files[i], sizeof(buf));
895 buf[ret] = '\0';
896 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
897 if (rc != SQLITE_OK) {
898 fprintf(stderr, "SQL error #2: %s\n", err);
899 fprintf(stderr, "%s\n", buf);
904 void open_smatch_db(void)
906 int rc;
908 if (option_no_db)
909 return;
911 init_memdb();
913 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
914 if (rc != SQLITE_OK) {
915 option_no_db = 1;
916 return;
918 return;
921 void register_definition_db_callbacks(int id)
923 add_hook(&match_function_def, FUNC_DEF_HOOK);
925 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
926 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
927 add_hook(&global_variable, BASE_HOOK);
928 add_hook(&global_variable, DECLARATION_HOOK);
929 add_returned_state_callback(match_return_info);
930 add_returned_state_callback(print_returned_struct_members);
931 add_hook(&call_return_state_hooks, RETURN_HOOK);
932 add_hook(&match_end_func_info, END_FUNC_HOOK);
934 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
935 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
938 void register_db_call_marker(int id)
940 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
943 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
945 char buf[256];
946 char *tmp;
948 if (strcmp(key, "$$") == 0)
949 return expr_to_var_sym(arg, sym);
951 if (strcmp(key, "*$$") == 0) {
952 if (arg->type == EXPR_PREOP && arg->op == '&') {
953 arg = strip_expr(arg->unop);
954 return expr_to_var_sym(arg, sym);
955 } else {
956 tmp = expr_to_var_sym(arg, sym);
957 if (!tmp)
958 return NULL;
959 snprintf(buf, sizeof(buf), "*%s", tmp);
960 free_string(tmp);
961 return alloc_string(buf);
965 if (arg->type == EXPR_PREOP && arg->op == '&') {
966 arg = strip_expr(arg->unop);
967 tmp = expr_to_var_sym(arg, sym);
968 if (!tmp)
969 return NULL;
970 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
971 return alloc_string(buf);
974 tmp = expr_to_var_sym(arg, sym);
975 if (!tmp)
976 return NULL;
977 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
978 free_string(tmp);
979 return alloc_string(buf);