db: only allow functions to be saved to one function pointer
[smatch.git] / smatch_db.c
blob9da9a4a47b4bfb7f057c62a7a1f5ee57a96a087e
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 #define sql_insert(table, values...) \
22 do { \
23 if (__inline_fn) { \
24 char buf[1024]; \
25 char *err, *p = buf; \
26 int rc; \
28 p += snprintf(p, buf + sizeof(buf) - p, \
29 "insert into %s values (", #table); \
30 p += snprintf(p, buf + sizeof(buf) - p, values); \
31 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
32 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
33 if (rc != SQLITE_OK) { \
34 fprintf(stderr, "SQL error #2: %s\n", err); \
35 fprintf(stderr, "SQL: '%s'\n", buf); \
36 } \
37 return; \
38 } \
39 if (option_info) { \
40 sm_prefix(); \
41 sm_printf("SQL: insert into " #table " values (" values); \
42 sm_printf(");\n"); \
43 } \
44 } while (0)
46 struct def_callback {
47 int hook_type;
48 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
50 ALLOCATOR(def_callback, "definition db hook callbacks");
51 DECLARE_PTR_LIST(callback_list, struct def_callback);
52 static struct callback_list *callbacks;
54 struct member_info_callback {
55 int owner;
56 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
58 ALLOCATOR(member_info_callback, "caller_info callbacks");
59 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
60 static struct member_info_cb_list *member_callbacks;
62 struct returned_state_callback {
63 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
65 ALLOCATOR(returned_state_callback, "returned state callbacks");
66 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
67 static struct returned_state_cb_list *returned_state_callbacks;
69 struct returned_member_callback {
70 int owner;
71 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
73 ALLOCATOR(returned_member_callback, "returned member callbacks");
74 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
75 static struct returned_member_cb_list *returned_member_callbacks;
77 struct call_implies_callback {
78 int type;
79 void (*callback)(struct expression *arg, char *value);
81 ALLOCATOR(call_implies_callback, "call_implies callbacks");
82 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
83 static struct call_implies_cb_list *call_implies_cb_list;
85 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
87 char *err = NULL;
88 int rc;
90 if (option_no_db || !db)
91 return;
93 rc = sqlite3_exec(db, sql, callback, 0, &err);
94 if (rc != SQLITE_OK) {
95 fprintf(stderr, "SQL error #2: %s\n", err);
96 fprintf(stderr, "SQL: '%s'\n", sql);
100 void sql_insert_return_states(int return_id, const char *return_ranges,
101 int type, int param, const char *key, const char *value)
103 sql_insert(return_states, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
104 get_filename(), get_function(), return_id, return_ranges,
105 fn_static(), type, param, key, value);
108 void sql_insert_caller_info(struct expression *call, int type,
109 int param, const char *key, const char *value)
111 char *fn;
113 if (!option_info && !__inline_call)
114 return;
116 fn = get_fnptr_name(call->fn);
117 if (!fn)
118 return;
120 if (__inline_call) {
121 char buf[1024];
122 char *err;
123 int rc;
125 snprintf(buf, sizeof(buf), "insert into caller_info values ("
126 "'%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
127 get_filename(), get_function(), fn,
128 (unsigned long)call, is_static(call->fn), type, param,
129 key, value);
130 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
131 if (rc != SQLITE_OK) {
132 fprintf(stderr, "SQL error #2: %s\n", err);
133 fprintf(stderr, "SQL: '%s'\n", buf);
135 return;
138 sm_msg("SQL_caller_info: insert into caller_info values ("
139 "'%s', '%s', '%s', %%FUNC_ID%%, %d, %d, %d, '%s', '%s');",
140 get_filename(), get_function(), fn, is_static(call->fn),
141 type, param, key, value);
143 free_string(fn);
146 void sql_insert_function_ptr(const char *fn, const char *struct_name)
148 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
149 struct_name);
152 void sql_insert_return_values(const char *return_values)
154 sql_insert(return_values, "'%s', '%s', %d, '%s'", get_filename(),
155 get_function(), fn_static(), return_values);
158 void sql_insert_call_implies(int type, int param, int value)
160 sql_insert(call_implies, "'%s', '%s', %d, %d, %d, %d", get_filename(),
161 get_function(), fn_static(), type, param, value);
164 void sql_insert_type_size(const char *member, int size)
166 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
169 static char *get_static_filter(struct symbol *sym)
171 static char sql_filter[1024];
173 if (sym->ctype.modifiers & MOD_STATIC) {
174 snprintf(sql_filter, sizeof(sql_filter),
175 "file = '%s' and function = '%s' and static = '1';",
176 get_filename(), sym->ident->name);
177 } else {
178 snprintf(sql_filter, sizeof(sql_filter),
179 "function = '%s' and static = '0';", sym->ident->name);
182 return sql_filter;
185 void sql_select_return_states(const char *cols, struct expression *call,
186 int (*callback)(void*, int, char**, char**))
188 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
189 return;
191 run_sql(callback, "select %s from return_states where %s",
192 cols, get_static_filter(call->fn->symbol));
195 void sql_select_return_values(const char *cols, struct expression *call,
196 int (*callback)(void*, int, char**, char**))
198 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
199 return;
201 run_sql(callback, "select %s from return_values where %s",
202 cols, get_static_filter(call->fn->symbol));
205 void sql_select_caller_info(const char *cols, struct symbol *sym,
206 int (*callback)(void*, int, char**, char**))
208 run_sql(callback,
209 "select %s from caller_info where %s order by function_id;",
210 cols, get_static_filter(sym));
213 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
215 struct def_callback *def_callback = __alloc_def_callback(0);
217 def_callback->hook_type = type;
218 def_callback->callback = callback;
219 add_ptr_list(&callbacks, def_callback);
223 * These call backs are used when the --info option is turned on to print struct
224 * member information. For example foo->bar could have a state in
225 * smatch_extra.c and also check_user.c.
227 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
229 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
231 member_callback->owner = owner;
232 member_callback->callback = callback;
233 add_ptr_list(&member_callbacks, member_callback);
236 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
238 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
240 callback->callback = fn;
241 add_ptr_list(&returned_state_callbacks, callback);
244 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
246 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
248 member_callback->owner = owner;
249 member_callback->callback = callback;
250 add_ptr_list(&returned_member_callbacks, member_callback);
253 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
255 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
257 cb->type = type;
258 cb->callback = callback;
259 add_ptr_list(&call_implies_cb_list, cb);
262 static struct symbol *return_type;
263 static struct range_list *return_range_list;
264 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
266 if (argc != 1)
267 return 0;
268 if (option_debug)
269 sm_msg("return type %d", type_positive_bits(return_type));
270 str_to_rl(return_type, argv[0], &return_range_list);
271 return 0;
274 struct range_list *db_return_vals(struct expression *expr)
276 return_type = get_type(expr);
277 if (!return_type)
278 return NULL;
279 return_range_list = NULL;
280 sql_select_return_values("return", expr, db_return_callback);
281 return return_range_list;
284 static void match_call_marker(struct expression *expr)
287 * we just want to record something in the database so that if we have
288 * two calls like: frob(4); frob(some_unkown); then on the receiving
289 * side we know that sometimes frob is called with unknown parameters.
292 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
295 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
296 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
298 struct sm_state *sm;
299 char *name;
300 struct symbol *sym;
301 int len;
302 char printed_name[256];
303 int is_address = 0;
305 expr = strip_expr(expr);
306 if (expr->type == EXPR_PREOP && expr->op == '&') {
307 expr = strip_expr(expr->unop);
308 is_address = 1;
311 name = expr_to_var_sym(expr, &sym);
312 if (!name || !sym)
313 goto free;
315 len = strlen(name);
316 FOR_EACH_PTR(slist, sm) {
317 if (sm->sym != sym)
318 continue;
319 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
320 continue;
321 if (is_address)
322 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
323 else
324 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
325 callback(call, param, printed_name, sm->state);
326 } END_FOR_EACH_PTR(sm);
327 free:
328 free_string(name);
331 static void match_call_info(struct expression *call)
333 struct member_info_callback *cb;
334 struct expression *arg;
335 struct state_list *slist;
336 char *name;
337 int i;
339 name = get_fnptr_name(call->fn);
340 if (!name)
341 return;
343 FOR_EACH_PTR(member_callbacks, cb) {
344 slist = get_all_states(cb->owner);
345 i = 0;
346 FOR_EACH_PTR(call->args, arg) {
347 print_struct_members(call, arg, i, slist, cb->callback);
348 i++;
349 } END_FOR_EACH_PTR(arg);
350 free_slist(&slist);
351 } END_FOR_EACH_PTR(cb);
353 free_string(name);
356 static int get_param(int param, char **name, struct symbol **sym)
358 struct symbol *arg;
359 int i;
361 i = 0;
362 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
364 * this is a temporary hack to work around a bug (I think in sparse?)
365 * 2.6.37-rc1:fs/reiserfs/journal.o
366 * If there is a function definition without parameter name found
367 * after a function implementation then it causes a crash.
368 * int foo() {}
369 * int bar(char *);
371 if (arg->ident->name < (char *)100)
372 continue;
373 if (i == param && arg->ident->name) {
374 *name = arg->ident->name;
375 *sym = arg;
376 return TRUE;
378 i++;
379 } END_FOR_EACH_PTR(arg);
381 return FALSE;
384 static struct state_list *final_states;
385 static int prev_func_id = -1;
386 static int db_callback(void *unused, int argc, char **argv, char **azColName)
388 int func_id;
389 long type;
390 long param;
391 char *name = NULL;
392 struct symbol *sym = NULL;
393 struct def_callback *def_callback;
395 if (argc != 5)
396 return 0;
398 func_id = atoi(argv[0]);
399 errno = 0;
400 type = strtol(argv[1], NULL, 10);
401 param = strtol(argv[2], NULL, 10);
402 if (errno)
403 return 0;
405 if (prev_func_id == -1)
406 prev_func_id = func_id;
407 if (func_id != prev_func_id) {
408 merge_slist(&final_states, __pop_fake_cur_slist());
409 __push_fake_cur_slist();
410 __unnullify_path();
411 prev_func_id = func_id;
414 if (type == INTERNAL)
415 return 0;
416 if (param >= 0 && !get_param(param, &name, &sym))
417 return 0;
419 FOR_EACH_PTR(callbacks, def_callback) {
420 if (def_callback->hook_type == type)
421 def_callback->callback(name, sym, argv[3], argv[4]);
422 } END_FOR_EACH_PTR(def_callback);
424 return 0;
427 static void get_direct_callers(struct symbol *sym)
429 sql_select_caller_info("function_id, type, parameter, key, value", sym,
430 db_callback);
433 static char *ptr_name;
434 static int ptr_cnt;
435 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
437 if (ptr_cnt++) {
438 free_string(ptr_name);
439 ptr_name = NULL;
440 return 0;
442 if (!ptr_name)
443 ptr_name = alloc_string(argv[0]);
444 return 0;
447 static void get_function_pointer_callers(struct symbol *sym)
449 char sql_filter[1024];
451 if (sym->ctype.modifiers & MOD_STATIC) {
452 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
453 get_filename(), sym->ident->name);
454 } else {
455 snprintf(sql_filter, 1024, "function = '%s';",
456 sym->ident->name);
459 ptr_name = NULL;
460 ptr_cnt = 0;
461 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
462 if (!ptr_name)
463 return;
465 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
466 " where function = '%s' order by function_id", ptr_name);
468 free_string(ptr_name);
471 static void match_data_from_db(struct symbol *sym)
473 struct sm_state *sm;
475 if (!sym || !sym->ident || !sym->ident->name)
476 return;
478 __push_fake_cur_slist();
479 __unnullify_path();
480 prev_func_id = -1;
482 get_direct_callers(sym);
483 get_function_pointer_callers(sym);
485 merge_slist(&final_states, __pop_fake_cur_slist());
487 FOR_EACH_PTR(final_states, sm) {
488 __set_sm(sm);
489 } END_FOR_EACH_PTR(sm);
491 free_slist(&final_states);
494 static void match_function_assign(struct expression *expr)
496 struct expression *right = expr->right;
497 struct symbol *sym;
498 char *fn_name;
499 char *ptr_name;
501 if (right->type == EXPR_PREOP && right->op == '&')
502 right = strip_expr(right->unop);
503 if (right->type != EXPR_SYMBOL)
504 return;
505 sym = get_type(right);
506 if (!sym || sym->type != SYM_FN)
507 return;
509 fn_name = expr_to_var(right);
510 ptr_name = get_fnptr_name(expr->left);
511 if (!fn_name || !ptr_name)
512 goto free;
514 sql_insert_function_ptr(fn_name, ptr_name);
516 free:
517 free_string(fn_name);
518 free_string(ptr_name);
521 static struct expression *call_implies_call_expr;
522 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
524 struct call_implies_callback *cb;
525 struct expression *arg = NULL;
526 int type;
527 int param;
529 if (argc != 4)
530 return 0;
532 type = atoi(argv[1]);
533 param = atoi(argv[2]);
535 FOR_EACH_PTR(call_implies_cb_list, cb) {
536 if (cb->type != type)
537 continue;
538 if (param != -1) {
539 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
540 if (!arg)
541 continue;
543 cb->callback(arg, argv[3]);
544 } END_FOR_EACH_PTR(cb);
546 return 0;
549 static void match_call_implies(struct expression *expr)
551 struct symbol *sym;
552 static char sql_filter[1024];
554 if (expr->fn->type != EXPR_SYMBOL)
555 return;
556 sym = expr->fn->symbol;
557 if (!sym)
558 return;
560 if (sym->ctype.modifiers & MOD_STATIC) {
561 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
562 get_filename(), sym->ident->name);
563 } else {
564 snprintf(sql_filter, 1024, "function = '%s' and static = 0;",
565 sym->ident->name);
568 call_implies_call_expr = expr;
569 run_sql(call_implies_callbacks,
570 "select function, type, parameter, value from call_implies where %s",
571 sql_filter);
572 return;
575 static void print_initializer_list(struct expression_list *expr_list,
576 struct symbol *struct_type)
578 struct expression *expr;
579 struct symbol *base_type;
580 char struct_name[256];
582 FOR_EACH_PTR(expr_list, expr) {
583 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
584 print_initializer_list(expr->idx_expression->expr_list, struct_type);
585 continue;
587 if (expr->type != EXPR_IDENTIFIER)
588 continue;
589 if (!expr->expr_ident)
590 continue;
591 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
592 continue;
593 base_type = get_type(expr->ident_expression);
594 if (!base_type || base_type->type != SYM_FN)
595 continue;
596 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
597 struct_type->ident->name, expr->expr_ident->name);
598 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
599 struct_name);
600 } END_FOR_EACH_PTR(expr);
603 static void global_variable(struct symbol *sym)
605 struct symbol *struct_type;
607 if (!sym->ident)
608 return;
609 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
610 return;
611 struct_type = get_base_type(sym);
612 if (!struct_type)
613 return;
614 if (struct_type->type == SYM_ARRAY) {
615 struct_type = get_base_type(struct_type);
616 if (!struct_type)
617 return;
619 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
620 return;
621 print_initializer_list(sym->initializer->expr_list, struct_type);
624 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
626 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
629 static int return_id;
630 static void match_function_def(struct symbol *sym)
632 return_id = 0;
635 static void call_return_state_hooks_compare(struct expression *expr)
637 struct returned_state_callback *cb;
638 struct state_list *slist;
639 char *return_ranges;
640 int final_pass_orig = final_pass;
642 __push_fake_cur_slist();
644 final_pass = 0;
645 __split_whole_condition(expr);
646 final_pass = final_pass_orig;
648 return_ranges = alloc_sname("1");
650 return_id++;
651 slist = __get_cur_slist();
652 FOR_EACH_PTR(returned_state_callbacks, cb) {
653 cb->callback(return_id, return_ranges, expr, slist);
654 } END_FOR_EACH_PTR(cb);
656 __push_true_states();
657 __use_false_states();
659 return_ranges = alloc_sname("0");;
660 return_id++;
661 slist = __get_cur_slist();
662 FOR_EACH_PTR(returned_state_callbacks, cb) {
663 cb->callback(return_id, return_ranges, expr, slist);
664 } END_FOR_EACH_PTR(cb);
666 __merge_true_states();
667 __pop_fake_cur_slist();
670 static int call_return_state_hooks_split_possible(struct expression *expr)
672 struct returned_state_callback *cb;
673 struct state_list *slist;
674 struct range_list *rl;
675 char *return_ranges;
676 struct sm_state *sm;
677 struct sm_state *tmp;
678 int ret = 0;
679 int nr_possible, nr_states;
681 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
682 if (!sm || !sm->merged)
683 return 0;
685 /* bail if it gets too complicated */
686 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
687 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
688 if (nr_possible >= 100)
689 return 0;
690 if (nr_states * nr_possible >= 1000)
691 return 0;
693 FOR_EACH_PTR(sm->possible, tmp) {
694 if (tmp->merged)
695 continue;
697 ret = 1;
698 __push_fake_cur_slist();
700 overwrite_states_using_pool(tmp);
702 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
703 return_ranges = show_rl(rl);
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 __pop_fake_cur_slist();
712 } END_FOR_EACH_PTR(tmp);
714 return ret;
717 static void call_return_state_hooks(struct expression *expr)
719 struct returned_state_callback *cb;
720 struct state_list *slist;
721 struct range_list *rl;
722 char *return_ranges;
723 int nr_states;
725 expr = strip_expr(expr);
727 if (!expr) {
728 return_ranges = alloc_sname("");
729 } else if (is_condition(expr)) {
730 call_return_state_hooks_compare(expr);
731 return;
732 } else if (call_return_state_hooks_split_possible(expr)) {
733 return;
734 } else if (get_implied_rl(expr, &rl)) {
735 rl = cast_rl(cur_func_return_type(), rl);
736 return_ranges = show_rl(rl);
737 } else {
738 rl = alloc_whole_rl(cur_func_return_type());
739 return_ranges = show_rl(rl);
742 return_id++;
743 slist = __get_cur_slist();
744 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
745 FOR_EACH_PTR(returned_state_callbacks, cb) {
746 if (nr_states < 10000)
747 cb->callback(return_id, return_ranges, expr, slist);
748 else
749 cb->callback(return_id, return_ranges, expr, NULL);
750 } END_FOR_EACH_PTR(cb);
753 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
755 struct returned_member_callback *cb;
756 struct state_list *my_slist;
757 struct sm_state *sm;
758 struct symbol *type;
759 char *name;
760 char member_name[256];
761 int len;
763 type = get_type(expr);
764 if (!type || type->type != SYM_PTR)
765 return;
766 type = get_real_base_type(type);
767 if (!type || type->type != SYM_STRUCT)
768 return;
769 name = expr_to_var(expr);
770 if (!name)
771 return;
773 member_name[sizeof(member_name) - 1] = '\0';
774 strcpy(member_name, "$$");
776 len = strlen(name);
777 FOR_EACH_PTR(returned_member_callbacks, cb) {
778 my_slist = get_all_states_slist(cb->owner, slist);
779 FOR_EACH_PTR(my_slist, sm) {
780 if (strncmp(sm->name, name, len) != 0)
781 continue;
782 if (strncmp(sm->name + len, "->", 2) != 0)
783 continue;
784 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
785 cb->callback(return_id, return_ranges, member_name, sm->state);
786 } END_FOR_EACH_PTR(sm);
787 free_slist(&my_slist);
788 } END_FOR_EACH_PTR(cb);
790 free_string(name);
793 static void match_end_func_info(struct symbol *sym)
795 if (__path_is_null())
796 return;
797 call_return_state_hooks(NULL);
800 static void init_memdb(void)
802 char *err = NULL;
803 int rc;
804 const char *schema_files[] = {
805 "db/db.schema",
806 "db/caller_info.schema",
807 "db/return_states.schema",
808 "db/type_size.schema",
809 "db/call_implies.schema",
810 "db/function_ptr.schema",
811 "db/return_values.schema",
813 static char buf[4096];
814 int fd;
815 int ret;
816 int i;
818 rc = sqlite3_open(":memory:", &mem_db);
819 if (rc != SQLITE_OK) {
820 printf("Error starting In-Memory database.");
821 return;
824 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
825 fd = open_data_file(schema_files[i]);
826 if (fd < 0)
827 continue;
828 ret = read(fd, buf, sizeof(buf));
829 if (ret == sizeof(buf)) {
830 printf("Schema file too large: %s (limit %ld bytes)",
831 schema_files[i], sizeof(buf));
833 buf[ret] = '\0';
834 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
835 if (rc != SQLITE_OK) {
836 fprintf(stderr, "SQL error #2: %s\n", err);
837 fprintf(stderr, "%s\n", buf);
842 void open_smatch_db(void)
844 int rc;
846 if (option_no_db)
847 return;
849 init_memdb();
851 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
852 if (rc != SQLITE_OK) {
853 option_no_db = 1;
854 return;
856 return;
859 void register_definition_db_callbacks(int id)
861 add_hook(&match_function_def, FUNC_DEF_HOOK);
863 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
864 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
865 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
866 add_hook(&global_variable, BASE_HOOK);
867 add_hook(&global_variable, DECLARATION_HOOK);
868 add_returned_state_callback(match_return_info);
869 add_returned_state_callback(print_returned_struct_members);
870 add_hook(&call_return_state_hooks, RETURN_HOOK);
871 add_hook(&match_end_func_info, END_FUNC_HOOK);
873 if (option_no_db)
874 return;
876 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
877 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
880 void register_db_call_marker(int id)
882 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
885 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
887 char buf[256];
888 char *tmp;
890 if (strcmp(key, "$$") == 0)
891 return expr_to_var_sym(arg, sym);
893 if (strcmp(key, "*$$") == 0) {
894 if (arg->type == EXPR_PREOP && arg->op == '&') {
895 arg = strip_expr(arg->unop);
896 return expr_to_var_sym(arg, sym);
897 } else {
898 tmp = expr_to_var_sym(arg, sym);
899 if (!tmp)
900 return NULL;
901 snprintf(buf, sizeof(buf), "*%s", tmp);
902 free_string(tmp);
903 return alloc_string(buf);
907 if (arg->type == EXPR_PREOP && arg->op == '&') {
908 arg = strip_expr(arg->unop);
909 tmp = expr_to_var_sym(arg, sym);
910 if (!tmp)
911 return NULL;
912 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
913 return alloc_string(buf);
916 tmp = expr_to_var_sym(arg, sym);
917 if (!tmp)
918 return NULL;
919 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
920 free_string(tmp);
921 return alloc_string(buf);