locking: fix build problem
[smatch.git] / smatch_db.c
blob3203c1f84a3d158c794d5a7ba1d15109d0b382cb
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 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
117 get_base_file(), get_function(), (unsigned long)__inline_fn,
118 return_id, return_ranges, fn_static(), type, param, key, value);
121 static struct string_list *common_funcs;
122 static int is_common_function(const char *fn)
124 char *tmp;
126 if (strncmp(fn, "__builtin_", 10) == 0)
127 return 1;
129 FOR_EACH_PTR(common_funcs, tmp) {
130 if (strcmp(tmp, fn) == 0)
131 return 1;
132 } END_FOR_EACH_PTR(tmp);
134 return 0;
137 void sql_insert_caller_info(struct expression *call, int type,
138 int param, const char *key, const char *value)
140 char *fn;
142 if (!option_info && !__inline_call)
143 return;
145 fn = get_fnptr_name(call->fn);
146 if (!fn)
147 return;
149 if (__inline_call) {
150 mem_sql(NULL,
151 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
152 get_base_file(), get_function(), fn, (unsigned long)call,
153 is_static(call->fn), type, param, key, value);
156 if (!option_info)
157 return;
159 if (is_common_function(fn))
160 return;
162 sm_msg("SQL_caller_info: insert into caller_info values ("
163 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
164 get_base_file(), get_function(), fn, is_static(call->fn),
165 type, param, key, value);
167 free_string(fn);
170 void sql_insert_function_ptr(const char *fn, const char *struct_name)
172 sql_insert(function_ptr, "'%s', '%s', '%s'", get_base_file(), fn,
173 struct_name);
176 void sql_insert_return_values(const char *return_values)
178 sql_insert(return_values, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
179 get_function(), (unsigned long)__inline_fn, fn_static(),
180 return_values);
183 void sql_insert_call_implies(int type, int param, int value)
185 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
186 get_function(), (unsigned long)__inline_fn, fn_static(),
187 type, param, value);
190 void sql_insert_type_size(const char *member, int size)
192 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
195 void sql_insert_local_values(const char *name, const char *value)
197 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
200 static char *get_static_filter(struct symbol *sym)
202 static char sql_filter[1024];
204 if (sym->ctype.modifiers & MOD_STATIC) {
205 snprintf(sql_filter, sizeof(sql_filter),
206 "file = '%s' and function = '%s' and static = '1'",
207 get_base_file(), sym->ident->name);
208 } else {
209 snprintf(sql_filter, sizeof(sql_filter),
210 "function = '%s' and static = '0'", sym->ident->name);
213 return sql_filter;
216 void sql_select_return_states(const char *cols, struct expression *call,
217 int (*callback)(void*, int, char**, char**))
219 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
220 return;
222 if (inlinable(call->fn)) {
223 mem_sql(callback,
224 "select %s from return_states where call_id = '%lu';",
225 cols, (unsigned long)call);
226 return;
229 run_sql(callback, "select %s from return_states where %s;",
230 cols, get_static_filter(call->fn->symbol));
233 void sql_select_call_implies(const char *cols, struct expression *call,
234 int (*callback)(void*, int, char**, char**))
236 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
237 return;
239 if (inlinable(call->fn)) {
240 mem_sql(callback,
241 "select %s from call_implies where call_id = '%lu';",
242 cols, (unsigned long)call);
243 return;
246 run_sql(callback, "select %s from call_implies where %s;",
247 cols, get_static_filter(call->fn->symbol));
250 void sql_select_return_values(const char *cols, struct expression *call,
251 int (*callback)(void*, int, char**, char**))
253 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
254 return;
256 if (inlinable(call->fn)) {
257 mem_sql(callback,
258 "select %s from return_values where call_id = '%lu';",
259 cols, (unsigned long)call);
260 return;
263 run_sql(callback, "select %s from return_values where %s;",
264 cols, get_static_filter(call->fn->symbol));
267 void sql_select_caller_info(const char *cols, struct symbol *sym,
268 int (*callback)(void*, int, char**, char**))
270 if (__inline_fn) {
271 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
272 cols, (unsigned long)__inline_fn);
273 return;
276 run_sql(callback,
277 "select %s from caller_info where %s order by call_id;",
278 cols, get_static_filter(sym));
281 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
283 struct def_callback *def_callback = __alloc_def_callback(0);
285 def_callback->hook_type = type;
286 def_callback->callback = callback;
287 add_ptr_list(&callbacks, def_callback);
291 * These call backs are used when the --info option is turned on to print struct
292 * member information. For example foo->bar could have a state in
293 * smatch_extra.c and also check_user.c.
295 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
297 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
299 member_callback->owner = owner;
300 member_callback->callback = callback;
301 add_ptr_list(&member_callbacks, member_callback);
304 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
306 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
308 callback->callback = fn;
309 add_ptr_list(&returned_state_callbacks, callback);
312 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
314 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
316 member_callback->owner = owner;
317 member_callback->callback = callback;
318 add_ptr_list(&returned_member_callbacks, member_callback);
321 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
323 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
325 cb->type = type;
326 cb->callback = callback;
327 add_ptr_list(&call_implies_cb_list, cb);
330 static struct symbol *return_type;
331 static struct range_list *return_range_list;
332 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
334 if (argc != 1)
335 return 0;
336 if (option_debug)
337 sm_msg("return type %d", type_positive_bits(return_type));
338 str_to_rl(return_type, argv[0], &return_range_list);
339 return 0;
342 struct range_list *db_return_vals(struct expression *expr)
344 return_type = get_type(expr);
345 if (!return_type)
346 return NULL;
347 return_range_list = NULL;
348 sql_select_return_values("return", expr, db_return_callback);
349 return return_range_list;
352 static void match_call_marker(struct expression *expr)
355 * we just want to record something in the database so that if we have
356 * two calls like: frob(4); frob(some_unkown); then on the receiving
357 * side we know that sometimes frob is called with unknown parameters.
360 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
363 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
364 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
366 struct sm_state *sm;
367 char *name;
368 struct symbol *sym;
369 int len;
370 char printed_name[256];
371 int is_address = 0;
373 expr = strip_expr(expr);
374 if (expr->type == EXPR_PREOP && expr->op == '&') {
375 expr = strip_expr(expr->unop);
376 is_address = 1;
379 name = expr_to_var_sym(expr, &sym);
380 if (!name || !sym)
381 goto free;
383 len = strlen(name);
384 FOR_EACH_PTR(slist, sm) {
385 if (sm->sym != sym)
386 continue;
387 if (strcmp(name, sm->name) == 0) {
388 if (is_address)
389 snprintf(printed_name, sizeof(printed_name), "*$$");
390 else /* these are already handled. fixme: handle them here */
391 continue;
392 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
393 snprintf(printed_name, sizeof(printed_name), "*$$");
394 } else if (strncmp(name, sm->name, len) == 0) {
395 if (is_address)
396 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
397 else
398 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
399 } else {
400 continue;
402 callback(call, param, printed_name, sm->state);
403 } END_FOR_EACH_PTR(sm);
404 free:
405 free_string(name);
408 static void match_call_info(struct expression *call)
410 struct member_info_callback *cb;
411 struct expression *arg;
412 struct state_list *slist;
413 char *name;
414 int i;
416 name = get_fnptr_name(call->fn);
417 if (!name)
418 return;
420 FOR_EACH_PTR(member_callbacks, cb) {
421 slist = get_all_states(cb->owner);
422 i = 0;
423 FOR_EACH_PTR(call->args, arg) {
424 print_struct_members(call, arg, i, slist, cb->callback);
425 i++;
426 } END_FOR_EACH_PTR(arg);
427 free_slist(&slist);
428 } END_FOR_EACH_PTR(cb);
430 free_string(name);
433 static int get_param(int param, char **name, struct symbol **sym)
435 struct symbol *arg;
436 int i;
438 i = 0;
439 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
441 * this is a temporary hack to work around a bug (I think in sparse?)
442 * 2.6.37-rc1:fs/reiserfs/journal.o
443 * If there is a function definition without parameter name found
444 * after a function implementation then it causes a crash.
445 * int foo() {}
446 * int bar(char *);
448 if (arg->ident->name < (char *)100)
449 continue;
450 if (i == param && arg->ident->name) {
451 *name = arg->ident->name;
452 *sym = arg;
453 return TRUE;
455 i++;
456 } END_FOR_EACH_PTR(arg);
458 return FALSE;
461 static struct state_list *final_states;
462 static int prev_func_id = -1;
463 static int db_callback(void *unused, int argc, char **argv, char **azColName)
465 int func_id;
466 long type;
467 long param;
468 char *name = NULL;
469 struct symbol *sym = NULL;
470 struct def_callback *def_callback;
472 if (argc != 5)
473 return 0;
475 func_id = atoi(argv[0]);
476 errno = 0;
477 type = strtol(argv[1], NULL, 10);
478 param = strtol(argv[2], NULL, 10);
479 if (errno)
480 return 0;
482 if (prev_func_id == -1)
483 prev_func_id = func_id;
484 if (func_id != prev_func_id) {
485 merge_slist(&final_states, __pop_fake_cur_slist());
486 __push_fake_cur_slist();
487 __unnullify_path();
488 prev_func_id = func_id;
491 if (type == INTERNAL)
492 return 0;
493 if (param >= 0 && !get_param(param, &name, &sym))
494 return 0;
496 FOR_EACH_PTR(callbacks, def_callback) {
497 if (def_callback->hook_type == type)
498 def_callback->callback(name, sym, argv[3], argv[4]);
499 } END_FOR_EACH_PTR(def_callback);
501 return 0;
504 static void get_direct_callers(struct symbol *sym)
506 sql_select_caller_info("call_id, type, parameter, key, value", sym,
507 db_callback);
510 static char *ptr_name;
511 static int ptr_cnt;
512 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
514 if (ptr_cnt++) {
515 free_string(ptr_name);
516 ptr_name = NULL;
517 return 0;
519 if (!ptr_name)
520 ptr_name = alloc_string(argv[0]);
521 return 0;
524 static void get_function_pointer_callers(struct symbol *sym)
526 char sql_filter[1024];
528 if (sym->ctype.modifiers & MOD_STATIC) {
529 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
530 get_base_file(), sym->ident->name);
531 } else {
532 snprintf(sql_filter, 1024, "function = '%s';",
533 sym->ident->name);
536 ptr_name = NULL;
537 ptr_cnt = 0;
538 run_sql(get_ptr_name,
539 "select distinct ptr from function_ptr where %s",
540 sql_filter);
541 if (!ptr_name)
542 return;
544 run_sql(db_callback, "select call_id, type, parameter, key, value from caller_info"
545 " where function = '%s' order by call_id", ptr_name);
547 free_string(ptr_name);
550 static void match_data_from_db(struct symbol *sym)
552 struct sm_state *sm;
554 if (!sym || !sym->ident || !sym->ident->name)
555 return;
557 __push_fake_cur_slist();
558 __unnullify_path();
559 prev_func_id = -1;
561 get_direct_callers(sym);
562 if (!__inline_fn)
563 get_function_pointer_callers(sym);
565 merge_slist(&final_states, __pop_fake_cur_slist());
567 FOR_EACH_PTR(final_states, sm) {
568 __set_sm(sm);
569 } END_FOR_EACH_PTR(sm);
571 free_slist(&final_states);
574 static struct expression *call_implies_call_expr;
575 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
577 struct call_implies_callback *cb;
578 struct expression *arg = NULL;
579 int type;
580 int param;
582 if (argc != 4)
583 return 0;
585 type = atoi(argv[1]);
586 param = atoi(argv[2]);
588 FOR_EACH_PTR(call_implies_cb_list, cb) {
589 if (cb->type != type)
590 continue;
591 if (param != -1) {
592 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
593 if (!arg)
594 continue;
596 cb->callback(arg, argv[3]);
597 } END_FOR_EACH_PTR(cb);
599 return 0;
602 static void match_call_implies(struct expression *expr)
604 call_implies_call_expr = expr;
605 sql_select_call_implies("function, type, parameter, value", expr,
606 call_implies_callbacks);
607 return;
610 static void print_initializer_list(struct expression_list *expr_list,
611 struct symbol *struct_type)
613 struct expression *expr;
614 struct symbol *base_type;
615 char struct_name[256];
617 FOR_EACH_PTR(expr_list, expr) {
618 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
619 print_initializer_list(expr->idx_expression->expr_list, struct_type);
620 continue;
622 if (expr->type != EXPR_IDENTIFIER)
623 continue;
624 if (!expr->expr_ident)
625 continue;
626 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
627 continue;
628 base_type = get_type(expr->ident_expression);
629 if (!base_type || base_type->type != SYM_FN)
630 continue;
631 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
632 struct_type->ident->name, expr->expr_ident->name);
633 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
634 struct_name);
635 } END_FOR_EACH_PTR(expr);
638 static void global_variable(struct symbol *sym)
640 struct symbol *struct_type;
642 if (!sym->ident)
643 return;
644 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
645 return;
646 struct_type = get_base_type(sym);
647 if (!struct_type)
648 return;
649 if (struct_type->type == SYM_ARRAY) {
650 struct_type = get_base_type(struct_type);
651 if (!struct_type)
652 return;
654 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
655 return;
656 print_initializer_list(sym->initializer->expr_list, struct_type);
659 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
661 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
664 static int return_id;
665 static void match_function_def(struct symbol *sym)
667 return_id = 0;
670 static void call_return_state_hooks_compare(struct expression *expr)
672 struct returned_state_callback *cb;
673 struct state_list *slist;
674 char *return_ranges;
675 int final_pass_orig = final_pass;
677 __push_fake_cur_slist();
679 final_pass = 0;
680 __split_whole_condition(expr);
681 final_pass = final_pass_orig;
683 return_ranges = alloc_sname("1");
685 return_id++;
686 slist = __get_cur_slist();
687 FOR_EACH_PTR(returned_state_callbacks, cb) {
688 cb->callback(return_id, return_ranges, expr, slist);
689 } END_FOR_EACH_PTR(cb);
691 __push_true_states();
692 __use_false_states();
694 return_ranges = alloc_sname("0");;
695 return_id++;
696 slist = __get_cur_slist();
697 FOR_EACH_PTR(returned_state_callbacks, cb) {
698 cb->callback(return_id, return_ranges, expr, slist);
699 } END_FOR_EACH_PTR(cb);
701 __merge_true_states();
702 __pop_fake_cur_slist();
705 static int call_return_state_hooks_split_possible(struct expression *expr)
707 struct returned_state_callback *cb;
708 struct state_list *slist;
709 struct range_list *rl;
710 char *return_ranges;
711 struct sm_state *sm;
712 struct sm_state *tmp;
713 int ret = 0;
714 int nr_possible, nr_states;
716 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
717 if (!sm || !sm->merged)
718 return 0;
720 /* bail if it gets too complicated */
721 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
722 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
723 if (nr_possible >= 100)
724 return 0;
725 if (nr_states * nr_possible >= 1000)
726 return 0;
728 FOR_EACH_PTR(sm->possible, tmp) {
729 if (tmp->merged)
730 continue;
732 ret = 1;
733 __push_fake_cur_slist();
735 overwrite_states_using_pool(tmp);
737 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
738 return_ranges = show_rl(rl);
740 return_id++;
741 slist = __get_cur_slist();
742 FOR_EACH_PTR(returned_state_callbacks, cb) {
743 cb->callback(return_id, return_ranges, expr, slist);
744 } END_FOR_EACH_PTR(cb);
746 __pop_fake_cur_slist();
747 } END_FOR_EACH_PTR(tmp);
749 return ret;
752 static void call_return_state_hooks(struct expression *expr)
754 struct returned_state_callback *cb;
755 struct state_list *slist;
756 struct range_list *rl;
757 char *return_ranges;
758 int nr_states;
760 expr = strip_expr(expr);
762 if (!expr) {
763 return_ranges = alloc_sname("");
764 } else if (is_condition(expr)) {
765 call_return_state_hooks_compare(expr);
766 return;
767 } else if (call_return_state_hooks_split_possible(expr)) {
768 return;
769 } else if (get_implied_rl(expr, &rl)) {
770 rl = cast_rl(cur_func_return_type(), rl);
771 return_ranges = show_rl(rl);
772 } else {
773 rl = alloc_whole_rl(cur_func_return_type());
774 return_ranges = show_rl(rl);
777 return_id++;
778 slist = __get_cur_slist();
779 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
780 FOR_EACH_PTR(returned_state_callbacks, cb) {
781 if (nr_states < 10000)
782 cb->callback(return_id, return_ranges, expr, slist);
783 else
784 cb->callback(return_id, return_ranges, expr, NULL);
785 } END_FOR_EACH_PTR(cb);
788 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
790 struct returned_member_callback *cb;
791 struct state_list *my_slist;
792 struct sm_state *sm;
793 struct symbol *type;
794 char *name;
795 char member_name[256];
796 int len;
798 type = get_type(expr);
799 if (!type || type->type != SYM_PTR)
800 return;
801 type = get_real_base_type(type);
802 if (!type || type->type != SYM_STRUCT)
803 return;
804 name = expr_to_var(expr);
805 if (!name)
806 return;
808 member_name[sizeof(member_name) - 1] = '\0';
809 strcpy(member_name, "$$");
811 len = strlen(name);
812 FOR_EACH_PTR(returned_member_callbacks, cb) {
813 my_slist = get_all_states_slist(cb->owner, slist);
814 FOR_EACH_PTR(my_slist, sm) {
815 if (strncmp(sm->name, name, len) != 0)
816 continue;
817 if (strncmp(sm->name + len, "->", 2) != 0)
818 continue;
819 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
820 cb->callback(return_id, return_ranges, member_name, sm->state);
821 } END_FOR_EACH_PTR(sm);
822 free_slist(&my_slist);
823 } END_FOR_EACH_PTR(cb);
825 free_string(name);
828 static void reset_memdb(void)
830 mem_sql(NULL, "delete from caller_info;");
831 mem_sql(NULL, "delete from return_states;");
832 mem_sql(NULL, "delete from call_implies;");
833 mem_sql(NULL, "delete from return_values;");
836 static void match_end_func_info(struct symbol *sym)
838 if (__path_is_null())
839 return;
840 call_return_state_hooks(NULL);
841 if (!__inline_fn)
842 reset_memdb();
845 static void init_memdb(void)
847 char *err = NULL;
848 int rc;
849 const char *schema_files[] = {
850 "db/db.schema",
851 "db/caller_info.schema",
852 "db/return_states.schema",
853 "db/type_size.schema",
854 "db/call_implies.schema",
855 "db/function_ptr.schema",
856 "db/return_values.schema",
857 "db/local_values.schema",
859 static char buf[4096];
860 int fd;
861 int ret;
862 int i;
864 rc = sqlite3_open(":memory:", &mem_db);
865 if (rc != SQLITE_OK) {
866 printf("Error starting In-Memory database.");
867 return;
870 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
871 fd = open_data_file(schema_files[i]);
872 if (fd < 0)
873 continue;
874 ret = read(fd, buf, sizeof(buf));
875 if (ret == sizeof(buf)) {
876 printf("Schema file too large: %s (limit %zd bytes)",
877 schema_files[i], sizeof(buf));
879 buf[ret] = '\0';
880 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
881 if (rc != SQLITE_OK) {
882 fprintf(stderr, "SQL error #2: %s\n", err);
883 fprintf(stderr, "%s\n", buf);
888 void open_smatch_db(void)
890 int rc;
892 if (option_no_db)
893 return;
895 init_memdb();
897 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
898 if (rc != SQLITE_OK) {
899 option_no_db = 1;
900 return;
902 return;
905 static void register_common_funcs(void)
907 struct token *token;
908 char *func;
909 char filename[256];
911 if (option_project == PROJ_NONE)
912 strcpy(filename, "common_functions");
913 else
914 snprintf(filename, 256, "%s.common_functions", option_project_str);
916 token = get_tokens_file(filename);
917 if (!token)
918 return;
919 if (token_type(token) != TOKEN_STREAMBEGIN)
920 return;
921 token = token->next;
922 while (token_type(token) != TOKEN_STREAMEND) {
923 if (token_type(token) != TOKEN_IDENT)
924 return;
925 func = alloc_string(show_ident(token->ident));
926 add_ptr_list(&common_funcs, func);
927 token = token->next;
929 clear_token_alloc();
933 void register_definition_db_callbacks(int id)
935 add_hook(&match_function_def, FUNC_DEF_HOOK);
937 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
938 add_hook(&global_variable, BASE_HOOK);
939 add_hook(&global_variable, DECLARATION_HOOK);
940 add_returned_state_callback(match_return_info);
941 add_returned_state_callback(print_returned_struct_members);
942 add_hook(&call_return_state_hooks, RETURN_HOOK);
943 add_hook(&match_end_func_info, END_FUNC_HOOK);
945 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
946 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
948 register_common_funcs();
951 void register_db_call_marker(int id)
953 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
956 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
958 char buf[256];
959 char *tmp;
961 if (strcmp(key, "$$") == 0)
962 return expr_to_var_sym(arg, sym);
964 if (strcmp(key, "*$$") == 0) {
965 if (arg->type == EXPR_PREOP && arg->op == '&') {
966 arg = strip_expr(arg->unop);
967 return expr_to_var_sym(arg, sym);
968 } else {
969 tmp = expr_to_var_sym(arg, sym);
970 if (!tmp)
971 return NULL;
972 snprintf(buf, sizeof(buf), "*%s", tmp);
973 free_string(tmp);
974 return alloc_string(buf);
978 if (arg->type == EXPR_PREOP && arg->op == '&') {
979 arg = strip_expr(arg->unop);
980 tmp = expr_to_var_sym(arg, sym);
981 if (!tmp)
982 return NULL;
983 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
984 return alloc_string(buf);
987 tmp = expr_to_var_sym(arg, sym);
988 if (!tmp)
989 return NULL;
990 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
991 free_string(tmp);
992 return alloc_string(buf);