db: fix slow down caused by recursive function calls
[smatch.git] / smatch_db.c
blob87819ba7b198ee68bebcb53161b44cdb63ff6d23
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 sm_debug("in-mem: %s\n", buf); \
33 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
34 if (rc != SQLITE_OK) { \
35 fprintf(stderr, "SQL error #2: %s\n", err); \
36 fprintf(stderr, "SQL: '%s'\n", buf); \
37 } \
38 return; \
39 } \
40 if (option_info) { \
41 sm_prefix(); \
42 sm_printf("SQL: insert into " #table " values (" values); \
43 sm_printf(");\n"); \
44 } \
45 } while (0)
47 struct def_callback {
48 int hook_type;
49 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
51 ALLOCATOR(def_callback, "definition db hook callbacks");
52 DECLARE_PTR_LIST(callback_list, struct def_callback);
53 static struct callback_list *callbacks;
55 struct member_info_callback {
56 int owner;
57 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
59 ALLOCATOR(member_info_callback, "caller_info callbacks");
60 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
61 static struct member_info_cb_list *member_callbacks;
63 struct returned_state_callback {
64 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
66 ALLOCATOR(returned_state_callback, "returned state callbacks");
67 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
68 static struct returned_state_cb_list *returned_state_callbacks;
70 struct returned_member_callback {
71 int owner;
72 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
74 ALLOCATOR(returned_member_callback, "returned member callbacks");
75 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
76 static struct returned_member_cb_list *returned_member_callbacks;
78 struct call_implies_callback {
79 int type;
80 void (*callback)(struct expression *arg, char *value);
82 ALLOCATOR(call_implies_callback, "call_implies callbacks");
83 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
84 static struct call_implies_cb_list *call_implies_cb_list;
86 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
88 char *err = NULL;
89 int rc;
91 if (option_no_db || !db)
92 return;
94 rc = sqlite3_exec(db, sql, callback, 0, &err);
95 if (rc != SQLITE_OK) {
96 fprintf(stderr, "SQL error #2: %s\n", err);
97 fprintf(stderr, "SQL: '%s'\n", sql);
101 void sql_mem_exec(int (*callback)(void*, int, char**, char**), const char *sql)
103 char *err = NULL;
104 int rc;
106 rc = sqlite3_exec(mem_db, sql, callback, 0, &err);
107 if (rc != SQLITE_OK) {
108 fprintf(stderr, "SQL error #2: %s\n", err);
109 fprintf(stderr, "SQL: '%s'\n", sql);
113 void sql_insert_return_states(int return_id, const char *return_ranges,
114 int type, int param, const char *key, const char *value)
116 if (key && strlen(key) >= 80)
117 return;
118 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
119 get_base_file(), get_function(), (unsigned long)__inline_fn,
120 return_id, return_ranges, fn_static(), type, param, key, value);
123 static struct string_list *common_funcs;
124 static int is_common_function(const char *fn)
126 char *tmp;
128 if (strncmp(fn, "__builtin_", 10) == 0)
129 return 1;
131 FOR_EACH_PTR(common_funcs, tmp) {
132 if (strcmp(tmp, fn) == 0)
133 return 1;
134 } END_FOR_EACH_PTR(tmp);
136 return 0;
139 void sql_insert_caller_info(struct expression *call, int type,
140 int param, const char *key, const char *value)
142 char *fn;
144 if (!option_info && !__inline_call)
145 return;
147 if (key && strlen(key) >= 80)
148 return;
150 fn = get_fnptr_name(call->fn);
151 if (!fn)
152 return;
154 if (__inline_call) {
155 mem_sql(NULL,
156 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
157 get_base_file(), get_function(), fn, (unsigned long)call,
158 is_static(call->fn), type, param, key, value);
161 if (!option_info)
162 return;
164 if (is_common_function(fn))
165 return;
167 sm_msg("SQL_caller_info: insert into caller_info values ("
168 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
169 get_base_file(), get_function(), fn, is_static(call->fn),
170 type, param, key, value);
172 free_string(fn);
175 void sql_insert_function_ptr(const char *fn, const char *struct_name)
177 sql_insert(function_ptr, "'%s', '%s', '%s'", get_base_file(), fn,
178 struct_name);
181 void sql_insert_return_values(const char *return_values)
183 sql_insert(return_values, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
184 get_function(), (unsigned long)__inline_fn, fn_static(),
185 return_values);
188 void sql_insert_call_implies(int type, int param, int value)
190 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
191 get_function(), (unsigned long)__inline_fn, fn_static(),
192 type, param, value);
195 void sql_insert_type_size(const char *member, int size)
197 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
200 void sql_insert_local_values(const char *name, const char *value)
202 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
205 static char *get_static_filter(struct symbol *sym)
207 static char sql_filter[1024];
209 if (sym->ctype.modifiers & MOD_STATIC) {
210 snprintf(sql_filter, sizeof(sql_filter),
211 "file = '%s' and function = '%s' and static = '1'",
212 get_base_file(), sym->ident->name);
213 } else {
214 snprintf(sql_filter, sizeof(sql_filter),
215 "function = '%s' and static = '0'", sym->ident->name);
218 return sql_filter;
221 void sql_select_return_states(const char *cols, struct expression *call,
222 int (*callback)(void*, int, char**, char**))
224 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
225 return;
227 if (inlinable(call->fn)) {
228 mem_sql(callback,
229 "select %s from return_states where call_id = '%lu';",
230 cols, (unsigned long)call);
231 return;
234 run_sql(callback, "select %s from return_states where %s;",
235 cols, get_static_filter(call->fn->symbol));
238 void sql_select_call_implies(const char *cols, struct expression *call,
239 int (*callback)(void*, int, char**, char**))
241 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
242 return;
244 if (inlinable(call->fn)) {
245 mem_sql(callback,
246 "select %s from call_implies where call_id = '%lu';",
247 cols, (unsigned long)call);
248 return;
251 run_sql(callback, "select %s from call_implies where %s;",
252 cols, get_static_filter(call->fn->symbol));
255 void sql_select_return_values(const char *cols, struct expression *call,
256 int (*callback)(void*, int, char**, char**))
258 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
259 return;
261 if (inlinable(call->fn)) {
262 mem_sql(callback,
263 "select %s from return_values where call_id = '%lu';",
264 cols, (unsigned long)call);
265 return;
268 run_sql(callback, "select %s from return_values where %s;",
269 cols, get_static_filter(call->fn->symbol));
272 void sql_select_caller_info(const char *cols, struct symbol *sym,
273 int (*callback)(void*, int, char**, char**))
275 if (__inline_fn) {
276 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
277 cols, (unsigned long)__inline_fn);
278 return;
281 run_sql(callback,
282 "select %s from caller_info where %s order by call_id;",
283 cols, get_static_filter(sym));
286 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
288 struct def_callback *def_callback = __alloc_def_callback(0);
290 def_callback->hook_type = type;
291 def_callback->callback = callback;
292 add_ptr_list(&callbacks, def_callback);
296 * These call backs are used when the --info option is turned on to print struct
297 * member information. For example foo->bar could have a state in
298 * smatch_extra.c and also check_user.c.
300 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
302 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
304 member_callback->owner = owner;
305 member_callback->callback = callback;
306 add_ptr_list(&member_callbacks, member_callback);
309 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
311 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
313 callback->callback = fn;
314 add_ptr_list(&returned_state_callbacks, callback);
317 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
319 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
321 member_callback->owner = owner;
322 member_callback->callback = callback;
323 add_ptr_list(&returned_member_callbacks, member_callback);
326 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
328 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
330 cb->type = type;
331 cb->callback = callback;
332 add_ptr_list(&call_implies_cb_list, cb);
335 static struct symbol *return_type;
336 static struct range_list *return_range_list;
337 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
339 if (argc != 1)
340 return 0;
341 if (option_debug)
342 sm_msg("return type %d", type_positive_bits(return_type));
343 str_to_rl(return_type, argv[0], &return_range_list);
344 return 0;
347 struct range_list *db_return_vals(struct expression *expr)
349 return_type = get_type(expr);
350 if (!return_type)
351 return NULL;
352 return_range_list = NULL;
353 sql_select_return_values("return", expr, db_return_callback);
354 return return_range_list;
357 static void match_call_marker(struct expression *expr)
360 * we just want to record something in the database so that if we have
361 * two calls like: frob(4); frob(some_unkown); then on the receiving
362 * side we know that sometimes frob is called with unknown parameters.
365 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
368 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
369 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
371 struct sm_state *sm;
372 char *name;
373 struct symbol *sym;
374 int len;
375 char printed_name[256];
376 int is_address = 0;
378 expr = strip_expr(expr);
379 if (expr->type == EXPR_PREOP && expr->op == '&') {
380 expr = strip_expr(expr->unop);
381 is_address = 1;
384 name = expr_to_var_sym(expr, &sym);
385 if (!name || !sym)
386 goto free;
388 len = strlen(name);
389 FOR_EACH_PTR(slist, sm) {
390 if (sm->sym != sym)
391 continue;
392 if (strcmp(name, sm->name) == 0) {
393 if (is_address)
394 snprintf(printed_name, sizeof(printed_name), "*$$");
395 else /* these are already handled. fixme: handle them here */
396 continue;
397 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
398 snprintf(printed_name, sizeof(printed_name), "*$$");
399 } else if (strncmp(name, sm->name, len) == 0) {
400 if (is_address)
401 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
402 else
403 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
404 } else {
405 continue;
407 callback(call, param, printed_name, sm->state);
408 } END_FOR_EACH_PTR(sm);
409 free:
410 free_string(name);
413 static void match_call_info(struct expression *call)
415 struct member_info_callback *cb;
416 struct expression *arg;
417 struct state_list *slist;
418 char *name;
419 int i;
421 name = get_fnptr_name(call->fn);
422 if (!name)
423 return;
425 FOR_EACH_PTR(member_callbacks, cb) {
426 slist = get_all_states(cb->owner);
427 i = 0;
428 FOR_EACH_PTR(call->args, arg) {
429 print_struct_members(call, arg, i, slist, cb->callback);
430 i++;
431 } END_FOR_EACH_PTR(arg);
432 free_slist(&slist);
433 } END_FOR_EACH_PTR(cb);
435 free_string(name);
438 static int get_param(int param, char **name, struct symbol **sym)
440 struct symbol *arg;
441 int i;
443 i = 0;
444 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
446 * this is a temporary hack to work around a bug (I think in sparse?)
447 * 2.6.37-rc1:fs/reiserfs/journal.o
448 * If there is a function definition without parameter name found
449 * after a function implementation then it causes a crash.
450 * int foo() {}
451 * int bar(char *);
453 if (arg->ident->name < (char *)100)
454 continue;
455 if (i == param && arg->ident->name) {
456 *name = arg->ident->name;
457 *sym = arg;
458 return TRUE;
460 i++;
461 } END_FOR_EACH_PTR(arg);
463 return FALSE;
466 static struct state_list *final_states;
467 static int prev_func_id = -1;
468 static int db_callback(void *unused, int argc, char **argv, char **azColName)
470 int func_id;
471 long type;
472 long param;
473 char *name = NULL;
474 struct symbol *sym = NULL;
475 struct def_callback *def_callback;
477 if (argc != 5)
478 return 0;
480 func_id = atoi(argv[0]);
481 errno = 0;
482 type = strtol(argv[1], NULL, 10);
483 param = strtol(argv[2], NULL, 10);
484 if (errno)
485 return 0;
487 if (prev_func_id == -1)
488 prev_func_id = func_id;
489 if (func_id != prev_func_id) {
490 merge_slist(&final_states, __pop_fake_cur_slist());
491 __push_fake_cur_slist();
492 __unnullify_path();
493 prev_func_id = func_id;
496 if (type == INTERNAL)
497 return 0;
498 if (param >= 0 && !get_param(param, &name, &sym))
499 return 0;
501 FOR_EACH_PTR(callbacks, def_callback) {
502 if (def_callback->hook_type == type)
503 def_callback->callback(name, sym, argv[3], argv[4]);
504 } END_FOR_EACH_PTR(def_callback);
506 return 0;
509 static void get_direct_callers(struct symbol *sym)
511 sql_select_caller_info("call_id, type, parameter, key, value", sym,
512 db_callback);
515 static char *ptr_name;
516 static int ptr_cnt;
517 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
519 if (ptr_cnt++) {
520 free_string(ptr_name);
521 ptr_name = NULL;
522 return 0;
524 if (!ptr_name)
525 ptr_name = alloc_string(argv[0]);
526 return 0;
529 static void get_function_pointer_callers(struct symbol *sym)
531 char sql_filter[1024];
533 if (sym->ctype.modifiers & MOD_STATIC) {
534 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
535 get_base_file(), sym->ident->name);
536 } else {
537 snprintf(sql_filter, 1024, "function = '%s';",
538 sym->ident->name);
541 ptr_name = NULL;
542 ptr_cnt = 0;
543 run_sql(get_ptr_name,
544 "select distinct ptr from function_ptr where %s",
545 sql_filter);
546 if (!ptr_name)
547 return;
549 run_sql(db_callback, "select call_id, type, parameter, key, value from caller_info"
550 " where function = '%s' order by call_id", ptr_name);
552 free_string(ptr_name);
555 static void match_data_from_db(struct symbol *sym)
557 struct sm_state *sm;
559 if (!sym || !sym->ident || !sym->ident->name)
560 return;
562 __push_fake_cur_slist();
563 __unnullify_path();
564 prev_func_id = -1;
566 get_direct_callers(sym);
567 if (!__inline_fn)
568 get_function_pointer_callers(sym);
570 merge_slist(&final_states, __pop_fake_cur_slist());
572 FOR_EACH_PTR(final_states, sm) {
573 __set_sm(sm);
574 } END_FOR_EACH_PTR(sm);
576 free_slist(&final_states);
579 static struct expression *call_implies_call_expr;
580 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
582 struct call_implies_callback *cb;
583 struct expression *arg = NULL;
584 int type;
585 int param;
587 if (argc != 4)
588 return 0;
590 type = atoi(argv[1]);
591 param = atoi(argv[2]);
593 FOR_EACH_PTR(call_implies_cb_list, cb) {
594 if (cb->type != type)
595 continue;
596 if (param != -1) {
597 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
598 if (!arg)
599 continue;
601 cb->callback(arg, argv[3]);
602 } END_FOR_EACH_PTR(cb);
604 return 0;
607 static void match_call_implies(struct expression *expr)
609 call_implies_call_expr = expr;
610 sql_select_call_implies("function, type, parameter, value", expr,
611 call_implies_callbacks);
612 return;
615 static void print_initializer_list(struct expression_list *expr_list,
616 struct symbol *struct_type)
618 struct expression *expr;
619 struct symbol *base_type;
620 char struct_name[256];
622 FOR_EACH_PTR(expr_list, expr) {
623 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
624 print_initializer_list(expr->idx_expression->expr_list, struct_type);
625 continue;
627 if (expr->type != EXPR_IDENTIFIER)
628 continue;
629 if (!expr->expr_ident)
630 continue;
631 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
632 continue;
633 base_type = get_type(expr->ident_expression);
634 if (!base_type || base_type->type != SYM_FN)
635 continue;
636 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
637 struct_type->ident->name, expr->expr_ident->name);
638 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
639 struct_name);
640 } END_FOR_EACH_PTR(expr);
643 static void global_variable(struct symbol *sym)
645 struct symbol *struct_type;
647 if (!sym->ident)
648 return;
649 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
650 return;
651 struct_type = get_base_type(sym);
652 if (!struct_type)
653 return;
654 if (struct_type->type == SYM_ARRAY) {
655 struct_type = get_base_type(struct_type);
656 if (!struct_type)
657 return;
659 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
660 return;
661 print_initializer_list(sym->initializer->expr_list, struct_type);
664 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
666 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
669 static int return_id;
670 static void match_function_def(struct symbol *sym)
672 return_id = 0;
675 static void call_return_state_hooks_compare(struct expression *expr)
677 struct returned_state_callback *cb;
678 struct state_list *slist;
679 char *return_ranges;
680 int final_pass_orig = final_pass;
682 __push_fake_cur_slist();
684 final_pass = 0;
685 __split_whole_condition(expr);
686 final_pass = final_pass_orig;
688 return_ranges = alloc_sname("1");
690 return_id++;
691 slist = __get_cur_slist();
692 FOR_EACH_PTR(returned_state_callbacks, cb) {
693 cb->callback(return_id, return_ranges, expr, slist);
694 } END_FOR_EACH_PTR(cb);
696 __push_true_states();
697 __use_false_states();
699 return_ranges = alloc_sname("0");;
700 return_id++;
701 slist = __get_cur_slist();
702 FOR_EACH_PTR(returned_state_callbacks, cb) {
703 cb->callback(return_id, return_ranges, expr, slist);
704 } END_FOR_EACH_PTR(cb);
706 __merge_true_states();
707 __pop_fake_cur_slist();
710 static int call_return_state_hooks_split_possible(struct expression *expr)
712 struct returned_state_callback *cb;
713 struct state_list *slist;
714 struct range_list *rl;
715 char *return_ranges;
716 struct sm_state *sm;
717 struct sm_state *tmp;
718 int ret = 0;
719 int nr_possible, nr_states;
721 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
722 if (!sm || !sm->merged)
723 return 0;
725 /* bail if it gets too complicated */
726 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
727 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
728 if (nr_possible >= 100)
729 return 0;
730 if (nr_states * nr_possible >= 1000)
731 return 0;
733 FOR_EACH_PTR(sm->possible, tmp) {
734 if (tmp->merged)
735 continue;
737 ret = 1;
738 __push_fake_cur_slist();
740 overwrite_states_using_pool(tmp);
742 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
743 return_ranges = show_rl(rl);
745 return_id++;
746 slist = __get_cur_slist();
747 FOR_EACH_PTR(returned_state_callbacks, cb) {
748 cb->callback(return_id, return_ranges, expr, slist);
749 } END_FOR_EACH_PTR(cb);
751 __pop_fake_cur_slist();
752 } END_FOR_EACH_PTR(tmp);
754 return ret;
757 static void call_return_state_hooks(struct expression *expr)
759 struct returned_state_callback *cb;
760 struct state_list *slist;
761 struct range_list *rl;
762 char *return_ranges;
763 int nr_states;
765 expr = strip_expr(expr);
767 if (!expr) {
768 return_ranges = alloc_sname("");
769 } else if (is_condition(expr)) {
770 call_return_state_hooks_compare(expr);
771 return;
772 } else if (call_return_state_hooks_split_possible(expr)) {
773 return;
774 } else if (get_implied_rl(expr, &rl)) {
775 rl = cast_rl(cur_func_return_type(), rl);
776 return_ranges = show_rl(rl);
777 } else {
778 rl = alloc_whole_rl(cur_func_return_type());
779 return_ranges = show_rl(rl);
782 return_id++;
783 slist = __get_cur_slist();
784 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
785 FOR_EACH_PTR(returned_state_callbacks, cb) {
786 if (nr_states < 10000)
787 cb->callback(return_id, return_ranges, expr, slist);
788 else
789 cb->callback(return_id, return_ranges, expr, NULL);
790 } END_FOR_EACH_PTR(cb);
793 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
795 struct returned_member_callback *cb;
796 struct state_list *my_slist;
797 struct sm_state *sm;
798 struct symbol *type;
799 char *name;
800 char member_name[256];
801 int len;
803 type = get_type(expr);
804 if (!type || type->type != SYM_PTR)
805 return;
806 type = get_real_base_type(type);
807 if (!type || type->type != SYM_STRUCT)
808 return;
809 name = expr_to_var(expr);
810 if (!name)
811 return;
813 member_name[sizeof(member_name) - 1] = '\0';
814 strcpy(member_name, "$$");
816 len = strlen(name);
817 FOR_EACH_PTR(returned_member_callbacks, cb) {
818 my_slist = get_all_states_slist(cb->owner, slist);
819 FOR_EACH_PTR(my_slist, sm) {
820 if (strncmp(sm->name, name, len) != 0)
821 continue;
822 if (strncmp(sm->name + len, "->", 2) != 0)
823 continue;
824 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
825 cb->callback(return_id, return_ranges, member_name, sm->state);
826 } END_FOR_EACH_PTR(sm);
827 free_slist(&my_slist);
828 } END_FOR_EACH_PTR(cb);
830 free_string(name);
833 static void reset_memdb(void)
835 mem_sql(NULL, "delete from caller_info;");
836 mem_sql(NULL, "delete from return_states;");
837 mem_sql(NULL, "delete from call_implies;");
838 mem_sql(NULL, "delete from return_values;");
841 static void match_end_func_info(struct symbol *sym)
843 if (__path_is_null())
844 return;
845 call_return_state_hooks(NULL);
846 if (!__inline_fn)
847 reset_memdb();
850 static void init_memdb(void)
852 char *err = NULL;
853 int rc;
854 const char *schema_files[] = {
855 "db/db.schema",
856 "db/caller_info.schema",
857 "db/return_states.schema",
858 "db/type_size.schema",
859 "db/call_implies.schema",
860 "db/function_ptr.schema",
861 "db/return_values.schema",
862 "db/local_values.schema",
864 static char buf[4096];
865 int fd;
866 int ret;
867 int i;
869 rc = sqlite3_open(":memory:", &mem_db);
870 if (rc != SQLITE_OK) {
871 printf("Error starting In-Memory database.");
872 return;
875 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
876 fd = open_data_file(schema_files[i]);
877 if (fd < 0)
878 continue;
879 ret = read(fd, buf, sizeof(buf));
880 if (ret == sizeof(buf)) {
881 printf("Schema file too large: %s (limit %zd bytes)",
882 schema_files[i], sizeof(buf));
884 buf[ret] = '\0';
885 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
886 if (rc != SQLITE_OK) {
887 fprintf(stderr, "SQL error #2: %s\n", err);
888 fprintf(stderr, "%s\n", buf);
893 void open_smatch_db(void)
895 int rc;
897 if (option_no_db)
898 return;
900 init_memdb();
902 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
903 if (rc != SQLITE_OK) {
904 option_no_db = 1;
905 return;
907 return;
910 static void register_common_funcs(void)
912 struct token *token;
913 char *func;
914 char filename[256];
916 if (option_project == PROJ_NONE)
917 strcpy(filename, "common_functions");
918 else
919 snprintf(filename, 256, "%s.common_functions", option_project_str);
921 token = get_tokens_file(filename);
922 if (!token)
923 return;
924 if (token_type(token) != TOKEN_STREAMBEGIN)
925 return;
926 token = token->next;
927 while (token_type(token) != TOKEN_STREAMEND) {
928 if (token_type(token) != TOKEN_IDENT)
929 return;
930 func = alloc_string(show_ident(token->ident));
931 add_ptr_list(&common_funcs, func);
932 token = token->next;
934 clear_token_alloc();
938 void register_definition_db_callbacks(int id)
940 add_hook(&match_function_def, FUNC_DEF_HOOK);
942 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
943 add_hook(&global_variable, BASE_HOOK);
944 add_hook(&global_variable, DECLARATION_HOOK);
945 add_returned_state_callback(match_return_info);
946 add_returned_state_callback(print_returned_struct_members);
947 add_hook(&call_return_state_hooks, RETURN_HOOK);
948 add_hook(&match_end_func_info, END_FUNC_HOOK);
950 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
951 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
953 register_common_funcs();
956 void register_db_call_marker(int id)
958 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
961 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
963 char buf[256];
964 char *tmp;
966 if (strcmp(key, "$$") == 0)
967 return expr_to_var_sym(arg, sym);
969 if (strcmp(key, "*$$") == 0) {
970 if (arg->type == EXPR_PREOP && arg->op == '&') {
971 arg = strip_expr(arg->unop);
972 return expr_to_var_sym(arg, sym);
973 } else {
974 tmp = expr_to_var_sym(arg, sym);
975 if (!tmp)
976 return NULL;
977 snprintf(buf, sizeof(buf), "*%s", tmp);
978 free_string(tmp);
979 return alloc_string(buf);
983 if (arg->type == EXPR_PREOP && arg->op == '&') {
984 arg = strip_expr(arg->unop);
985 tmp = expr_to_var_sym(arg, sym);
986 if (!tmp)
987 return NULL;
988 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
989 return alloc_string(buf);
992 tmp = expr_to_var_sym(arg, sym);
993 if (!tmp)
994 return NULL;
995 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
996 free_string(tmp);
997 return alloc_string(buf);