flow: more inline fall out
[smatch.git] / smatch_db.c
blobb6fb7a9235db532d3d29f9dde3729287b2c49d68
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("mem-db: %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 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
117 get_filename(), get_function(), (unsigned long)__inline_fn,
118 return_id, return_ranges, fn_static(), type, param, key, value);
121 void sql_insert_caller_info(struct expression *call, int type,
122 int param, const char *key, const char *value)
124 char *fn;
126 if (!option_info && !__inline_call)
127 return;
129 fn = get_fnptr_name(call->fn);
130 if (!fn)
131 return;
133 if (__inline_call) {
134 mem_sql(NULL,
135 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
136 get_filename(), get_function(), fn, (unsigned long)call,
137 is_static(call->fn), type, param, key, value);
140 if (!option_info)
141 return;
143 if (strncmp(fn, "__builtin_", 10) == 0)
144 return;
146 sm_msg("SQL_caller_info: insert into caller_info values ("
147 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
148 get_filename(), get_function(), fn, is_static(call->fn),
149 type, param, key, value);
151 free_string(fn);
154 void sql_insert_function_ptr(const char *fn, const char *struct_name)
156 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
157 struct_name);
160 void sql_insert_return_values(const char *return_values)
162 sql_insert(return_values, "'%s', '%s', %lu, %d, '%s'", get_filename(),
163 get_function(), (unsigned long)__inline_fn, fn_static(),
164 return_values);
167 void sql_insert_call_implies(int type, int param, int value)
169 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_filename(),
170 get_function(), (unsigned long)__inline_fn, fn_static(),
171 type, param, value);
174 void sql_insert_type_size(const char *member, int size)
176 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
179 static char *get_static_filter(struct symbol *sym)
181 static char sql_filter[1024];
183 if (sym->ctype.modifiers & MOD_STATIC) {
184 snprintf(sql_filter, sizeof(sql_filter),
185 "file = '%s' and function = '%s' and static = '1'",
186 get_filename(), sym->ident->name);
187 } else {
188 snprintf(sql_filter, sizeof(sql_filter),
189 "function = '%s' and static = '0'", sym->ident->name);
192 return sql_filter;
195 void sql_select_return_states(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 if (inlinable(call->fn)) {
202 mem_sql(callback,
203 "select %s from return_states where call_id = '%lu';",
204 cols, (unsigned long)call);
205 return;
208 run_sql(callback, "select %s from return_states where %s;",
209 cols, get_static_filter(call->fn->symbol));
212 void sql_select_call_implies(const char *cols, struct expression *call,
213 int (*callback)(void*, int, char**, char**))
215 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
216 return;
218 if (inlinable(call->fn)) {
219 mem_sql(callback,
220 "select %s from call_implies where call_id = '%lu';",
221 cols, (unsigned long)call);
222 return;
225 run_sql(callback, "select %s from call_implies where %s;",
226 cols, get_static_filter(call->fn->symbol));
229 void sql_select_return_values(const char *cols, struct expression *call,
230 int (*callback)(void*, int, char**, char**))
232 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
233 return;
235 if (inlinable(call->fn)) {
236 mem_sql(callback,
237 "select %s from return_values where call_id = '%lu';",
238 cols, (unsigned long)call);
239 return;
242 run_sql(callback, "select %s from return_values where %s;",
243 cols, get_static_filter(call->fn->symbol));
246 void sql_select_caller_info(const char *cols, struct symbol *sym,
247 int (*callback)(void*, int, char**, char**))
249 if (__inline_fn) {
250 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
251 cols, (unsigned long)__inline_fn);
252 return;
255 run_sql(callback,
256 "select %s from caller_info where %s order by call_id;",
257 cols, get_static_filter(sym));
260 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
262 struct def_callback *def_callback = __alloc_def_callback(0);
264 def_callback->hook_type = type;
265 def_callback->callback = callback;
266 add_ptr_list(&callbacks, def_callback);
270 * These call backs are used when the --info option is turned on to print struct
271 * member information. For example foo->bar could have a state in
272 * smatch_extra.c and also check_user.c.
274 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
276 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
278 member_callback->owner = owner;
279 member_callback->callback = callback;
280 add_ptr_list(&member_callbacks, member_callback);
283 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
285 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
287 callback->callback = fn;
288 add_ptr_list(&returned_state_callbacks, callback);
291 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
293 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
295 member_callback->owner = owner;
296 member_callback->callback = callback;
297 add_ptr_list(&returned_member_callbacks, member_callback);
300 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
302 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
304 cb->type = type;
305 cb->callback = callback;
306 add_ptr_list(&call_implies_cb_list, cb);
309 static struct symbol *return_type;
310 static struct range_list *return_range_list;
311 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
313 if (argc != 1)
314 return 0;
315 if (option_debug)
316 sm_msg("return type %d", type_positive_bits(return_type));
317 str_to_rl(return_type, argv[0], &return_range_list);
318 return 0;
321 struct range_list *db_return_vals(struct expression *expr)
323 return_type = get_type(expr);
324 if (!return_type)
325 return NULL;
326 return_range_list = NULL;
327 sql_select_return_values("return", expr, db_return_callback);
328 return return_range_list;
331 static void match_call_marker(struct expression *expr)
334 * we just want to record something in the database so that if we have
335 * two calls like: frob(4); frob(some_unkown); then on the receiving
336 * side we know that sometimes frob is called with unknown parameters.
339 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
342 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
343 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
345 struct sm_state *sm;
346 char *name;
347 struct symbol *sym;
348 int len;
349 char printed_name[256];
350 int is_address = 0;
352 expr = strip_expr(expr);
353 if (expr->type == EXPR_PREOP && expr->op == '&') {
354 expr = strip_expr(expr->unop);
355 is_address = 1;
358 name = expr_to_var_sym(expr, &sym);
359 if (!name || !sym)
360 goto free;
362 len = strlen(name);
363 FOR_EACH_PTR(slist, sm) {
364 if (sm->sym != sym)
365 continue;
366 if (strcmp(name, sm->name) == 0) {
367 if (is_address)
368 snprintf(printed_name, sizeof(printed_name), "*$$");
369 else /* these are already handled. fixme: handle them here */
370 continue;
371 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
372 snprintf(printed_name, sizeof(printed_name), "*$$");
373 } else if (strncmp(name, sm->name, len) == 0) {
374 if (is_address)
375 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
376 else
377 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
378 } else {
379 continue;
381 callback(call, param, printed_name, sm->state);
382 } END_FOR_EACH_PTR(sm);
383 free:
384 free_string(name);
387 static void match_call_info(struct expression *call)
389 struct member_info_callback *cb;
390 struct expression *arg;
391 struct state_list *slist;
392 char *name;
393 int i;
395 name = get_fnptr_name(call->fn);
396 if (!name)
397 return;
399 FOR_EACH_PTR(member_callbacks, cb) {
400 slist = get_all_states(cb->owner);
401 i = 0;
402 FOR_EACH_PTR(call->args, arg) {
403 print_struct_members(call, arg, i, slist, cb->callback);
404 i++;
405 } END_FOR_EACH_PTR(arg);
406 free_slist(&slist);
407 } END_FOR_EACH_PTR(cb);
409 free_string(name);
412 static int get_param(int param, char **name, struct symbol **sym)
414 struct symbol *arg;
415 int i;
417 i = 0;
418 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
420 * this is a temporary hack to work around a bug (I think in sparse?)
421 * 2.6.37-rc1:fs/reiserfs/journal.o
422 * If there is a function definition without parameter name found
423 * after a function implementation then it causes a crash.
424 * int foo() {}
425 * int bar(char *);
427 if (arg->ident->name < (char *)100)
428 continue;
429 if (i == param && arg->ident->name) {
430 *name = arg->ident->name;
431 *sym = arg;
432 return TRUE;
434 i++;
435 } END_FOR_EACH_PTR(arg);
437 return FALSE;
440 static struct state_list *final_states;
441 static int prev_func_id = -1;
442 static int db_callback(void *unused, int argc, char **argv, char **azColName)
444 int func_id;
445 long type;
446 long param;
447 char *name = NULL;
448 struct symbol *sym = NULL;
449 struct def_callback *def_callback;
451 if (argc != 5)
452 return 0;
454 func_id = atoi(argv[0]);
455 errno = 0;
456 type = strtol(argv[1], NULL, 10);
457 param = strtol(argv[2], NULL, 10);
458 if (errno)
459 return 0;
461 if (prev_func_id == -1)
462 prev_func_id = func_id;
463 if (func_id != prev_func_id) {
464 merge_slist(&final_states, __pop_fake_cur_slist());
465 __push_fake_cur_slist();
466 __unnullify_path();
467 prev_func_id = func_id;
470 if (type == INTERNAL)
471 return 0;
472 if (param >= 0 && !get_param(param, &name, &sym))
473 return 0;
475 FOR_EACH_PTR(callbacks, def_callback) {
476 if (def_callback->hook_type == type)
477 def_callback->callback(name, sym, argv[3], argv[4]);
478 } END_FOR_EACH_PTR(def_callback);
480 return 0;
483 static void get_direct_callers(struct symbol *sym)
485 sql_select_caller_info("call_id, type, parameter, key, value", sym,
486 db_callback);
489 static char *ptr_name;
490 static int ptr_cnt;
491 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
493 if (ptr_cnt++) {
494 free_string(ptr_name);
495 ptr_name = NULL;
496 return 0;
498 if (!ptr_name)
499 ptr_name = alloc_string(argv[0]);
500 return 0;
503 static void get_function_pointer_callers(struct symbol *sym)
505 char sql_filter[1024];
507 if (sym->ctype.modifiers & MOD_STATIC) {
508 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
509 get_filename(), sym->ident->name);
510 } else {
511 snprintf(sql_filter, 1024, "function = '%s';",
512 sym->ident->name);
515 ptr_name = NULL;
516 ptr_cnt = 0;
517 run_sql(get_ptr_name,
518 "select distinct ptr from function_ptr where %s",
519 sql_filter);
520 if (!ptr_name)
521 return;
523 run_sql(db_callback, "select call_id, type, parameter, key, value from caller_info"
524 " where function = '%s' order by call_id", ptr_name);
526 free_string(ptr_name);
529 static void match_data_from_db(struct symbol *sym)
531 struct sm_state *sm;
533 if (!sym || !sym->ident || !sym->ident->name)
534 return;
536 __push_fake_cur_slist();
537 __unnullify_path();
538 prev_func_id = -1;
540 get_direct_callers(sym);
541 if (!__inline_fn)
542 get_function_pointer_callers(sym);
544 merge_slist(&final_states, __pop_fake_cur_slist());
546 FOR_EACH_PTR(final_states, sm) {
547 __set_sm(sm);
548 } END_FOR_EACH_PTR(sm);
550 free_slist(&final_states);
553 static void match_function_assign(struct expression *expr)
555 struct expression *right = expr->right;
556 struct symbol *sym;
557 char *fn_name;
558 char *ptr_name;
560 if (right->type == EXPR_PREOP && right->op == '&')
561 right = strip_expr(right->unop);
562 if (right->type != EXPR_SYMBOL)
563 return;
564 sym = get_type(right);
565 if (!sym || sym->type != SYM_FN)
566 return;
568 fn_name = expr_to_var(right);
569 ptr_name = get_fnptr_name(expr->left);
570 if (!fn_name || !ptr_name)
571 goto free;
573 sql_insert_function_ptr(fn_name, ptr_name);
575 free:
576 free_string(fn_name);
577 free_string(ptr_name);
580 static struct expression *call_implies_call_expr;
581 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
583 struct call_implies_callback *cb;
584 struct expression *arg = NULL;
585 int type;
586 int param;
588 if (argc != 4)
589 return 0;
591 type = atoi(argv[1]);
592 param = atoi(argv[2]);
594 FOR_EACH_PTR(call_implies_cb_list, cb) {
595 if (cb->type != type)
596 continue;
597 if (param != -1) {
598 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
599 if (!arg)
600 continue;
602 cb->callback(arg, argv[3]);
603 } END_FOR_EACH_PTR(cb);
605 return 0;
608 static void match_call_implies(struct expression *expr)
610 call_implies_call_expr = expr;
611 sql_select_call_implies("function, type, parameter, value", expr,
612 call_implies_callbacks);
613 return;
616 static void print_initializer_list(struct expression_list *expr_list,
617 struct symbol *struct_type)
619 struct expression *expr;
620 struct symbol *base_type;
621 char struct_name[256];
623 FOR_EACH_PTR(expr_list, expr) {
624 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
625 print_initializer_list(expr->idx_expression->expr_list, struct_type);
626 continue;
628 if (expr->type != EXPR_IDENTIFIER)
629 continue;
630 if (!expr->expr_ident)
631 continue;
632 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
633 continue;
634 base_type = get_type(expr->ident_expression);
635 if (!base_type || base_type->type != SYM_FN)
636 continue;
637 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
638 struct_type->ident->name, expr->expr_ident->name);
639 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
640 struct_name);
641 } END_FOR_EACH_PTR(expr);
644 static void global_variable(struct symbol *sym)
646 struct symbol *struct_type;
648 if (!sym->ident)
649 return;
650 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
651 return;
652 struct_type = get_base_type(sym);
653 if (!struct_type)
654 return;
655 if (struct_type->type == SYM_ARRAY) {
656 struct_type = get_base_type(struct_type);
657 if (!struct_type)
658 return;
660 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
661 return;
662 print_initializer_list(sym->initializer->expr_list, struct_type);
665 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
667 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
670 static int return_id;
671 static void match_function_def(struct symbol *sym)
673 return_id = 0;
676 static void call_return_state_hooks_compare(struct expression *expr)
678 struct returned_state_callback *cb;
679 struct state_list *slist;
680 char *return_ranges;
681 int final_pass_orig = final_pass;
683 __push_fake_cur_slist();
685 final_pass = 0;
686 __split_whole_condition(expr);
687 final_pass = final_pass_orig;
689 return_ranges = alloc_sname("1");
691 return_id++;
692 slist = __get_cur_slist();
693 FOR_EACH_PTR(returned_state_callbacks, cb) {
694 cb->callback(return_id, return_ranges, expr, slist);
695 } END_FOR_EACH_PTR(cb);
697 __push_true_states();
698 __use_false_states();
700 return_ranges = alloc_sname("0");;
701 return_id++;
702 slist = __get_cur_slist();
703 FOR_EACH_PTR(returned_state_callbacks, cb) {
704 cb->callback(return_id, return_ranges, expr, slist);
705 } END_FOR_EACH_PTR(cb);
707 __merge_true_states();
708 __pop_fake_cur_slist();
711 static int call_return_state_hooks_split_possible(struct expression *expr)
713 struct returned_state_callback *cb;
714 struct state_list *slist;
715 struct range_list *rl;
716 char *return_ranges;
717 struct sm_state *sm;
718 struct sm_state *tmp;
719 int ret = 0;
720 int nr_possible, nr_states;
722 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
723 if (!sm || !sm->merged)
724 return 0;
726 /* bail if it gets too complicated */
727 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
728 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
729 if (nr_possible >= 100)
730 return 0;
731 if (nr_states * nr_possible >= 1000)
732 return 0;
734 FOR_EACH_PTR(sm->possible, tmp) {
735 if (tmp->merged)
736 continue;
738 ret = 1;
739 __push_fake_cur_slist();
741 overwrite_states_using_pool(tmp);
743 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
744 return_ranges = show_rl(rl);
746 return_id++;
747 slist = __get_cur_slist();
748 FOR_EACH_PTR(returned_state_callbacks, cb) {
749 cb->callback(return_id, return_ranges, expr, slist);
750 } END_FOR_EACH_PTR(cb);
752 __pop_fake_cur_slist();
753 } END_FOR_EACH_PTR(tmp);
755 return ret;
758 static void call_return_state_hooks(struct expression *expr)
760 struct returned_state_callback *cb;
761 struct state_list *slist;
762 struct range_list *rl;
763 char *return_ranges;
764 int nr_states;
766 expr = strip_expr(expr);
768 if (!expr) {
769 return_ranges = alloc_sname("");
770 } else if (is_condition(expr)) {
771 call_return_state_hooks_compare(expr);
772 return;
773 } else if (call_return_state_hooks_split_possible(expr)) {
774 return;
775 } else if (get_implied_rl(expr, &rl)) {
776 rl = cast_rl(cur_func_return_type(), rl);
777 return_ranges = show_rl(rl);
778 } else {
779 rl = alloc_whole_rl(cur_func_return_type());
780 return_ranges = show_rl(rl);
783 return_id++;
784 slist = __get_cur_slist();
785 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
786 FOR_EACH_PTR(returned_state_callbacks, cb) {
787 if (nr_states < 10000)
788 cb->callback(return_id, return_ranges, expr, slist);
789 else
790 cb->callback(return_id, return_ranges, expr, NULL);
791 } END_FOR_EACH_PTR(cb);
794 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
796 struct returned_member_callback *cb;
797 struct state_list *my_slist;
798 struct sm_state *sm;
799 struct symbol *type;
800 char *name;
801 char member_name[256];
802 int len;
804 type = get_type(expr);
805 if (!type || type->type != SYM_PTR)
806 return;
807 type = get_real_base_type(type);
808 if (!type || type->type != SYM_STRUCT)
809 return;
810 name = expr_to_var(expr);
811 if (!name)
812 return;
814 member_name[sizeof(member_name) - 1] = '\0';
815 strcpy(member_name, "$$");
817 len = strlen(name);
818 FOR_EACH_PTR(returned_member_callbacks, cb) {
819 my_slist = get_all_states_slist(cb->owner, slist);
820 FOR_EACH_PTR(my_slist, sm) {
821 if (strncmp(sm->name, name, len) != 0)
822 continue;
823 if (strncmp(sm->name + len, "->", 2) != 0)
824 continue;
825 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
826 cb->callback(return_id, return_ranges, member_name, sm->state);
827 } END_FOR_EACH_PTR(sm);
828 free_slist(&my_slist);
829 } END_FOR_EACH_PTR(cb);
831 free_string(name);
834 static void reset_memdb(void)
836 mem_sql(NULL, "delete from caller_info;");
837 mem_sql(NULL, "delete from return_states;");
838 mem_sql(NULL, "delete from call_implies;");
839 mem_sql(NULL, "delete from return_values;");
842 static void match_end_func_info(struct symbol *sym)
844 if (__path_is_null())
845 return;
846 call_return_state_hooks(NULL);
847 if (!__inline_fn)
848 reset_memdb();
851 static void init_memdb(void)
853 char *err = NULL;
854 int rc;
855 const char *schema_files[] = {
856 "db/db.schema",
857 "db/caller_info.schema",
858 "db/return_states.schema",
859 "db/type_size.schema",
860 "db/call_implies.schema",
861 "db/function_ptr.schema",
862 "db/return_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 void register_definition_db_callbacks(int id)
912 add_hook(&match_function_def, FUNC_DEF_HOOK);
914 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
915 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
916 add_hook(&global_variable, BASE_HOOK);
917 add_hook(&global_variable, DECLARATION_HOOK);
918 add_returned_state_callback(match_return_info);
919 add_returned_state_callback(print_returned_struct_members);
920 add_hook(&call_return_state_hooks, RETURN_HOOK);
921 add_hook(&match_end_func_info, END_FUNC_HOOK);
923 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
924 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
927 void register_db_call_marker(int id)
929 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
932 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
934 char buf[256];
935 char *tmp;
937 if (strcmp(key, "$$") == 0)
938 return expr_to_var_sym(arg, sym);
940 if (strcmp(key, "*$$") == 0) {
941 if (arg->type == EXPR_PREOP && arg->op == '&') {
942 arg = strip_expr(arg->unop);
943 return expr_to_var_sym(arg, sym);
944 } else {
945 tmp = expr_to_var_sym(arg, sym);
946 if (!tmp)
947 return NULL;
948 snprintf(buf, sizeof(buf), "*%s", tmp);
949 free_string(tmp);
950 return alloc_string(buf);
954 if (arg->type == EXPR_PREOP && arg->op == '&') {
955 arg = strip_expr(arg->unop);
956 tmp = expr_to_var_sym(arg, sym);
957 if (!tmp)
958 return NULL;
959 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
960 return alloc_string(buf);
963 tmp = expr_to_var_sym(arg, sym);
964 if (!tmp)
965 return NULL;
966 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
967 free_string(tmp);
968 return alloc_string(buf);