db: use correct variable in mem_sql()
[smatch.git] / smatch_db.c
blobb705cc4c149a33c61293254168e1738e81ffcbc5
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include <unistd.h>
14 #include "smatch.h"
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
18 static sqlite3 *db;
19 static sqlite3 *mem_db;
21 /* like run_sql() but for the in-memory database */
22 #define mem_sql(call_back, sql...) \
23 do { \
24 char sql_txt[1024]; \
25 int rc; \
26 char *err = NULL; \
28 snprintf(sql_txt, sizeof(sql_txt), sql); \
29 sm_debug("in-mem %s\n", sql_txt); \
30 rc = sqlite3_exec(mem_db, sql_txt, call_back, 0, &err); \
31 if (rc != SQLITE_OK) { \
32 fprintf(stderr, "SQL error #2: %s\n", err); \
33 fprintf(stderr, "SQL: '%s'\n", sql_txt); \
34 } \
35 } while (0)
37 #define sql_insert(table, values...) \
38 do { \
39 if (__inline_fn) { \
40 char buf[1024]; \
41 char *err, *p = buf; \
42 int rc; \
44 p += snprintf(p, buf + sizeof(buf) - p, \
45 "insert into %s values (", #table); \
46 p += snprintf(p, buf + sizeof(buf) - p, values); \
47 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
48 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
49 if (rc != SQLITE_OK) { \
50 fprintf(stderr, "SQL error #2: %s\n", err); \
51 fprintf(stderr, "SQL: '%s'\n", buf); \
52 } \
53 return; \
54 } \
55 if (option_info) { \
56 sm_prefix(); \
57 sm_printf("SQL: insert into " #table " values (" values); \
58 sm_printf(");\n"); \
59 } \
60 } while (0)
62 struct def_callback {
63 int hook_type;
64 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
66 ALLOCATOR(def_callback, "definition db hook callbacks");
67 DECLARE_PTR_LIST(callback_list, struct def_callback);
68 static struct callback_list *callbacks;
70 struct member_info_callback {
71 int owner;
72 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
74 ALLOCATOR(member_info_callback, "caller_info callbacks");
75 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
76 static struct member_info_cb_list *member_callbacks;
78 struct returned_state_callback {
79 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr, struct state_list *slist);
81 ALLOCATOR(returned_state_callback, "returned state callbacks");
82 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
83 static struct returned_state_cb_list *returned_state_callbacks;
85 struct returned_member_callback {
86 int owner;
87 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
89 ALLOCATOR(returned_member_callback, "returned member callbacks");
90 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
91 static struct returned_member_cb_list *returned_member_callbacks;
93 struct call_implies_callback {
94 int type;
95 void (*callback)(struct expression *arg, char *value);
97 ALLOCATOR(call_implies_callback, "call_implies callbacks");
98 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
99 static struct call_implies_cb_list *call_implies_cb_list;
101 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
103 char *err = NULL;
104 int rc;
106 if (option_no_db || !db)
107 return;
109 rc = sqlite3_exec(db, sql, callback, 0, &err);
110 if (rc != SQLITE_OK) {
111 fprintf(stderr, "SQL error #2: %s\n", err);
112 fprintf(stderr, "SQL: '%s'\n", sql);
116 void sql_insert_return_states(int return_id, const char *return_ranges,
117 int type, int param, const char *key, const char *value)
119 sql_insert(return_states, "'%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s'",
120 get_filename(), get_function(), return_id, return_ranges,
121 fn_static(), type, param, key, value);
124 void sql_insert_caller_info(struct expression *call, int type,
125 int param, const char *key, const char *value)
127 char *fn;
129 if (!option_info && !__inline_call)
130 return;
132 fn = get_fnptr_name(call->fn);
133 if (!fn)
134 return;
136 if (__inline_call) {
137 char buf[1024];
138 char *err;
139 int rc;
141 snprintf(buf, sizeof(buf), "insert into caller_info values ("
142 "'%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
143 get_filename(), get_function(), fn,
144 (unsigned long)call, is_static(call->fn), type, param,
145 key, value);
146 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
147 if (rc != SQLITE_OK) {
148 fprintf(stderr, "SQL error #2: %s\n", err);
149 fprintf(stderr, "SQL: '%s'\n", buf);
153 if (!option_info)
154 return;
156 sm_msg("SQL_caller_info: insert into caller_info values ("
157 "'%s', '%s', '%s', %%FUNC_ID%%, %d, %d, %d, '%s', '%s');",
158 get_filename(), get_function(), fn, is_static(call->fn),
159 type, param, key, value);
161 free_string(fn);
164 void sql_insert_function_ptr(const char *fn, const char *struct_name)
166 sql_insert(function_ptr, "'%s', '%s', '%s'", get_filename(), fn,
167 struct_name);
170 void sql_insert_return_values(const char *return_values)
172 sql_insert(return_values, "'%s', '%s', %d, '%s'", get_filename(),
173 get_function(), fn_static(), return_values);
176 void sql_insert_call_implies(int type, int param, int value)
178 sql_insert(call_implies, "'%s', '%s', %d, %d, %d, %d", get_filename(),
179 get_function(), fn_static(), type, param, value);
182 void sql_insert_type_size(const char *member, int size)
184 sql_insert(type_size, "'%s', '%s', %d", get_filename(), member, size);
187 static char *get_static_filter(struct symbol *sym)
189 static char sql_filter[1024];
191 if (sym->ctype.modifiers & MOD_STATIC) {
192 snprintf(sql_filter, sizeof(sql_filter),
193 "file = '%s' and function = '%s' and static = '1'",
194 get_filename(), sym->ident->name);
195 } else {
196 snprintf(sql_filter, sizeof(sql_filter),
197 "function = '%s' and static = '0'", sym->ident->name);
200 return sql_filter;
203 void sql_select_return_states(const char *cols, struct expression *call,
204 int (*callback)(void*, int, char**, char**))
206 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
207 return;
209 if (inlinable(call->fn)) {
210 mem_sql(callback, "select %s from return_states where %s;",
211 cols, get_static_filter(call->fn->symbol));
212 return;
215 run_sql(callback, "select %s from return_states where %s;",
216 cols, get_static_filter(call->fn->symbol));
219 void sql_select_call_implies(const char *cols, struct expression *call,
220 int (*callback)(void*, int, char**, char**))
222 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
223 return;
225 if (inlinable(call->fn)) {
226 mem_sql(callback, "select %s from call_implies where %s;",
227 cols, get_static_filter(call->fn->symbol));
228 return;
231 run_sql(callback, "select %s from call_implies where %s;",
232 cols, get_static_filter(call->fn->symbol));
235 void sql_select_return_values(const char *cols, struct expression *call,
236 int (*callback)(void*, int, char**, char**))
238 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
239 return;
241 if (inlinable(call->fn)) {
242 mem_sql(callback, "select %s from return_values where %s;",
243 cols, get_static_filter(call->fn->symbol));
244 return;
247 run_sql(callback, "select %s from return_values where %s;",
248 cols, get_static_filter(call->fn->symbol));
251 void sql_select_caller_info(const char *cols, struct symbol *sym,
252 int (*callback)(void*, int, char**, char**))
254 if (__inline_fn) {
255 mem_sql(callback, "select %s from caller_info where function_id = %lu;",
256 cols, (unsigned long)__inline_fn);
257 return;
260 run_sql(callback,
261 "select %s from caller_info where %s order by function_id;",
262 cols, get_static_filter(sym));
265 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
267 struct def_callback *def_callback = __alloc_def_callback(0);
269 def_callback->hook_type = type;
270 def_callback->callback = callback;
271 add_ptr_list(&callbacks, def_callback);
275 * These call backs are used when the --info option is turned on to print struct
276 * member information. For example foo->bar could have a state in
277 * smatch_extra.c and also check_user.c.
279 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
281 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
283 member_callback->owner = owner;
284 member_callback->callback = callback;
285 add_ptr_list(&member_callbacks, member_callback);
288 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
290 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
292 callback->callback = fn;
293 add_ptr_list(&returned_state_callbacks, callback);
296 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
298 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
300 member_callback->owner = owner;
301 member_callback->callback = callback;
302 add_ptr_list(&returned_member_callbacks, member_callback);
305 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
307 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
309 cb->type = type;
310 cb->callback = callback;
311 add_ptr_list(&call_implies_cb_list, cb);
314 static struct symbol *return_type;
315 static struct range_list *return_range_list;
316 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
318 if (argc != 1)
319 return 0;
320 if (option_debug)
321 sm_msg("return type %d", type_positive_bits(return_type));
322 str_to_rl(return_type, argv[0], &return_range_list);
323 return 0;
326 struct range_list *db_return_vals(struct expression *expr)
328 return_type = get_type(expr);
329 if (!return_type)
330 return NULL;
331 return_range_list = NULL;
332 sql_select_return_values("return", expr, db_return_callback);
333 return return_range_list;
336 static void match_call_marker(struct expression *expr)
339 * we just want to record something in the database so that if we have
340 * two calls like: frob(4); frob(some_unkown); then on the receiving
341 * side we know that sometimes frob is called with unknown parameters.
344 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
347 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
348 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
350 struct sm_state *sm;
351 char *name;
352 struct symbol *sym;
353 int len;
354 char printed_name[256];
355 int is_address = 0;
357 expr = strip_expr(expr);
358 if (expr->type == EXPR_PREOP && expr->op == '&') {
359 expr = strip_expr(expr->unop);
360 is_address = 1;
363 name = expr_to_var_sym(expr, &sym);
364 if (!name || !sym)
365 goto free;
367 len = strlen(name);
368 FOR_EACH_PTR(slist, sm) {
369 if (sm->sym != sym)
370 continue;
371 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
372 continue;
373 if (is_address)
374 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
375 else
376 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
377 callback(call, param, printed_name, sm->state);
378 } END_FOR_EACH_PTR(sm);
379 free:
380 free_string(name);
383 static void match_call_info(struct expression *call)
385 struct member_info_callback *cb;
386 struct expression *arg;
387 struct state_list *slist;
388 char *name;
389 int i;
391 name = get_fnptr_name(call->fn);
392 if (!name)
393 return;
395 FOR_EACH_PTR(member_callbacks, cb) {
396 slist = get_all_states(cb->owner);
397 i = 0;
398 FOR_EACH_PTR(call->args, arg) {
399 print_struct_members(call, arg, i, slist, cb->callback);
400 i++;
401 } END_FOR_EACH_PTR(arg);
402 free_slist(&slist);
403 } END_FOR_EACH_PTR(cb);
405 free_string(name);
408 static int get_param(int param, char **name, struct symbol **sym)
410 struct symbol *arg;
411 int i;
413 i = 0;
414 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
416 * this is a temporary hack to work around a bug (I think in sparse?)
417 * 2.6.37-rc1:fs/reiserfs/journal.o
418 * If there is a function definition without parameter name found
419 * after a function implementation then it causes a crash.
420 * int foo() {}
421 * int bar(char *);
423 if (arg->ident->name < (char *)100)
424 continue;
425 if (i == param && arg->ident->name) {
426 *name = arg->ident->name;
427 *sym = arg;
428 return TRUE;
430 i++;
431 } END_FOR_EACH_PTR(arg);
433 return FALSE;
436 static struct state_list *final_states;
437 static int prev_func_id = -1;
438 static int db_callback(void *unused, int argc, char **argv, char **azColName)
440 int func_id;
441 long type;
442 long param;
443 char *name = NULL;
444 struct symbol *sym = NULL;
445 struct def_callback *def_callback;
447 if (argc != 5)
448 return 0;
450 func_id = atoi(argv[0]);
451 errno = 0;
452 type = strtol(argv[1], NULL, 10);
453 param = strtol(argv[2], NULL, 10);
454 if (errno)
455 return 0;
457 if (prev_func_id == -1)
458 prev_func_id = func_id;
459 if (func_id != prev_func_id) {
460 merge_slist(&final_states, __pop_fake_cur_slist());
461 __push_fake_cur_slist();
462 __unnullify_path();
463 prev_func_id = func_id;
466 if (type == INTERNAL)
467 return 0;
468 if (param >= 0 && !get_param(param, &name, &sym))
469 return 0;
471 FOR_EACH_PTR(callbacks, def_callback) {
472 if (def_callback->hook_type == type)
473 def_callback->callback(name, sym, argv[3], argv[4]);
474 } END_FOR_EACH_PTR(def_callback);
476 return 0;
479 static void get_direct_callers(struct symbol *sym)
481 sql_select_caller_info("function_id, type, parameter, key, value", sym,
482 db_callback);
485 static char *ptr_name;
486 static int ptr_cnt;
487 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
489 if (ptr_cnt++) {
490 free_string(ptr_name);
491 ptr_name = NULL;
492 return 0;
494 if (!ptr_name)
495 ptr_name = alloc_string(argv[0]);
496 return 0;
499 static void get_function_pointer_callers(struct symbol *sym)
501 char sql_filter[1024];
503 if (sym->ctype.modifiers & MOD_STATIC) {
504 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
505 get_filename(), sym->ident->name);
506 } else {
507 snprintf(sql_filter, 1024, "function = '%s';",
508 sym->ident->name);
511 ptr_name = NULL;
512 ptr_cnt = 0;
513 run_sql(get_ptr_name, "select ptr from function_ptr where %s", sql_filter);
514 if (!ptr_name)
515 return;
517 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
518 " where function = '%s' order by function_id", ptr_name);
520 free_string(ptr_name);
523 static void match_data_from_db(struct symbol *sym)
525 struct sm_state *sm;
527 if (!sym || !sym->ident || !sym->ident->name)
528 return;
530 __push_fake_cur_slist();
531 __unnullify_path();
532 prev_func_id = -1;
534 get_direct_callers(sym);
535 get_function_pointer_callers(sym);
537 merge_slist(&final_states, __pop_fake_cur_slist());
539 FOR_EACH_PTR(final_states, sm) {
540 __set_sm(sm);
541 } END_FOR_EACH_PTR(sm);
543 free_slist(&final_states);
546 static void match_function_assign(struct expression *expr)
548 struct expression *right = expr->right;
549 struct symbol *sym;
550 char *fn_name;
551 char *ptr_name;
553 if (right->type == EXPR_PREOP && right->op == '&')
554 right = strip_expr(right->unop);
555 if (right->type != EXPR_SYMBOL)
556 return;
557 sym = get_type(right);
558 if (!sym || sym->type != SYM_FN)
559 return;
561 fn_name = expr_to_var(right);
562 ptr_name = get_fnptr_name(expr->left);
563 if (!fn_name || !ptr_name)
564 goto free;
566 sql_insert_function_ptr(fn_name, ptr_name);
568 free:
569 free_string(fn_name);
570 free_string(ptr_name);
573 static struct expression *call_implies_call_expr;
574 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
576 struct call_implies_callback *cb;
577 struct expression *arg = NULL;
578 int type;
579 int param;
581 if (argc != 4)
582 return 0;
584 type = atoi(argv[1]);
585 param = atoi(argv[2]);
587 FOR_EACH_PTR(call_implies_cb_list, cb) {
588 if (cb->type != type)
589 continue;
590 if (param != -1) {
591 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
592 if (!arg)
593 continue;
595 cb->callback(arg, argv[3]);
596 } END_FOR_EACH_PTR(cb);
598 return 0;
601 static void match_call_implies(struct expression *expr)
603 call_implies_call_expr = expr;
604 sql_select_call_implies("function, type, parameter, value", expr,
605 call_implies_callbacks);
606 return;
609 static void print_initializer_list(struct expression_list *expr_list,
610 struct symbol *struct_type)
612 struct expression *expr;
613 struct symbol *base_type;
614 char struct_name[256];
616 FOR_EACH_PTR(expr_list, expr) {
617 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
618 print_initializer_list(expr->idx_expression->expr_list, struct_type);
619 continue;
621 if (expr->type != EXPR_IDENTIFIER)
622 continue;
623 if (!expr->expr_ident)
624 continue;
625 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
626 continue;
627 base_type = get_type(expr->ident_expression);
628 if (!base_type || base_type->type != SYM_FN)
629 continue;
630 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
631 struct_type->ident->name, expr->expr_ident->name);
632 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
633 struct_name);
634 } END_FOR_EACH_PTR(expr);
637 static void global_variable(struct symbol *sym)
639 struct symbol *struct_type;
641 if (!sym->ident)
642 return;
643 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
644 return;
645 struct_type = get_base_type(sym);
646 if (!struct_type)
647 return;
648 if (struct_type->type == SYM_ARRAY) {
649 struct_type = get_base_type(struct_type);
650 if (!struct_type)
651 return;
653 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
654 return;
655 print_initializer_list(sym->initializer->expr_list, struct_type);
658 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
660 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
663 static int return_id;
664 static void match_function_def(struct symbol *sym)
666 return_id = 0;
669 static void call_return_state_hooks_compare(struct expression *expr)
671 struct returned_state_callback *cb;
672 struct state_list *slist;
673 char *return_ranges;
674 int final_pass_orig = final_pass;
676 __push_fake_cur_slist();
678 final_pass = 0;
679 __split_whole_condition(expr);
680 final_pass = final_pass_orig;
682 return_ranges = alloc_sname("1");
684 return_id++;
685 slist = __get_cur_slist();
686 FOR_EACH_PTR(returned_state_callbacks, cb) {
687 cb->callback(return_id, return_ranges, expr, slist);
688 } END_FOR_EACH_PTR(cb);
690 __push_true_states();
691 __use_false_states();
693 return_ranges = alloc_sname("0");;
694 return_id++;
695 slist = __get_cur_slist();
696 FOR_EACH_PTR(returned_state_callbacks, cb) {
697 cb->callback(return_id, return_ranges, expr, slist);
698 } END_FOR_EACH_PTR(cb);
700 __merge_true_states();
701 __pop_fake_cur_slist();
704 static int call_return_state_hooks_split_possible(struct expression *expr)
706 struct returned_state_callback *cb;
707 struct state_list *slist;
708 struct range_list *rl;
709 char *return_ranges;
710 struct sm_state *sm;
711 struct sm_state *tmp;
712 int ret = 0;
713 int nr_possible, nr_states;
715 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
716 if (!sm || !sm->merged)
717 return 0;
719 /* bail if it gets too complicated */
720 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
721 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
722 if (nr_possible >= 100)
723 return 0;
724 if (nr_states * nr_possible >= 1000)
725 return 0;
727 FOR_EACH_PTR(sm->possible, tmp) {
728 if (tmp->merged)
729 continue;
731 ret = 1;
732 __push_fake_cur_slist();
734 overwrite_states_using_pool(tmp);
736 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
737 return_ranges = show_rl(rl);
739 return_id++;
740 slist = __get_cur_slist();
741 FOR_EACH_PTR(returned_state_callbacks, cb) {
742 cb->callback(return_id, return_ranges, expr, slist);
743 } END_FOR_EACH_PTR(cb);
745 __pop_fake_cur_slist();
746 } END_FOR_EACH_PTR(tmp);
748 return ret;
751 static void call_return_state_hooks(struct expression *expr)
753 struct returned_state_callback *cb;
754 struct state_list *slist;
755 struct range_list *rl;
756 char *return_ranges;
757 int nr_states;
759 expr = strip_expr(expr);
761 if (!expr) {
762 return_ranges = alloc_sname("");
763 } else if (is_condition(expr)) {
764 call_return_state_hooks_compare(expr);
765 return;
766 } else if (call_return_state_hooks_split_possible(expr)) {
767 return;
768 } else if (get_implied_rl(expr, &rl)) {
769 rl = cast_rl(cur_func_return_type(), rl);
770 return_ranges = show_rl(rl);
771 } else {
772 rl = alloc_whole_rl(cur_func_return_type());
773 return_ranges = show_rl(rl);
776 return_id++;
777 slist = __get_cur_slist();
778 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
779 FOR_EACH_PTR(returned_state_callbacks, cb) {
780 if (nr_states < 10000)
781 cb->callback(return_id, return_ranges, expr, slist);
782 else
783 cb->callback(return_id, return_ranges, expr, NULL);
784 } END_FOR_EACH_PTR(cb);
787 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
789 struct returned_member_callback *cb;
790 struct state_list *my_slist;
791 struct sm_state *sm;
792 struct symbol *type;
793 char *name;
794 char member_name[256];
795 int len;
797 type = get_type(expr);
798 if (!type || type->type != SYM_PTR)
799 return;
800 type = get_real_base_type(type);
801 if (!type || type->type != SYM_STRUCT)
802 return;
803 name = expr_to_var(expr);
804 if (!name)
805 return;
807 member_name[sizeof(member_name) - 1] = '\0';
808 strcpy(member_name, "$$");
810 len = strlen(name);
811 FOR_EACH_PTR(returned_member_callbacks, cb) {
812 my_slist = get_all_states_slist(cb->owner, slist);
813 FOR_EACH_PTR(my_slist, sm) {
814 if (strncmp(sm->name, name, len) != 0)
815 continue;
816 if (strncmp(sm->name + len, "->", 2) != 0)
817 continue;
818 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
819 cb->callback(return_id, return_ranges, member_name, sm->state);
820 } END_FOR_EACH_PTR(sm);
821 free_slist(&my_slist);
822 } END_FOR_EACH_PTR(cb);
824 free_string(name);
827 static void match_end_func_info(struct symbol *sym)
829 if (__path_is_null())
830 return;
831 call_return_state_hooks(NULL);
834 static void init_memdb(void)
836 char *err = NULL;
837 int rc;
838 const char *schema_files[] = {
839 "db/db.schema",
840 "db/caller_info.schema",
841 "db/return_states.schema",
842 "db/type_size.schema",
843 "db/call_implies.schema",
844 "db/function_ptr.schema",
845 "db/return_values.schema",
847 static char buf[4096];
848 int fd;
849 int ret;
850 int i;
852 rc = sqlite3_open(":memory:", &mem_db);
853 if (rc != SQLITE_OK) {
854 printf("Error starting In-Memory database.");
855 return;
858 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
859 fd = open_data_file(schema_files[i]);
860 if (fd < 0)
861 continue;
862 ret = read(fd, buf, sizeof(buf));
863 if (ret == sizeof(buf)) {
864 printf("Schema file too large: %s (limit %zd bytes)",
865 schema_files[i], sizeof(buf));
867 buf[ret] = '\0';
868 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
869 if (rc != SQLITE_OK) {
870 fprintf(stderr, "SQL error #2: %s\n", err);
871 fprintf(stderr, "%s\n", buf);
876 void open_smatch_db(void)
878 int rc;
880 if (option_no_db)
881 return;
883 init_memdb();
885 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
886 if (rc != SQLITE_OK) {
887 option_no_db = 1;
888 return;
890 return;
893 void register_definition_db_callbacks(int id)
895 add_hook(&match_function_def, FUNC_DEF_HOOK);
897 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
898 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
899 add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
900 add_hook(&global_variable, BASE_HOOK);
901 add_hook(&global_variable, DECLARATION_HOOK);
902 add_returned_state_callback(match_return_info);
903 add_returned_state_callback(print_returned_struct_members);
904 add_hook(&call_return_state_hooks, RETURN_HOOK);
905 add_hook(&match_end_func_info, END_FUNC_HOOK);
907 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
908 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);
911 void register_db_call_marker(int id)
913 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
916 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
918 char buf[256];
919 char *tmp;
921 if (strcmp(key, "$$") == 0)
922 return expr_to_var_sym(arg, sym);
924 if (strcmp(key, "*$$") == 0) {
925 if (arg->type == EXPR_PREOP && arg->op == '&') {
926 arg = strip_expr(arg->unop);
927 return expr_to_var_sym(arg, sym);
928 } else {
929 tmp = expr_to_var_sym(arg, sym);
930 if (!tmp)
931 return NULL;
932 snprintf(buf, sizeof(buf), "*%s", tmp);
933 free_string(tmp);
934 return alloc_string(buf);
938 if (arg->type == EXPR_PREOP && arg->op == '&') {
939 arg = strip_expr(arg->unop);
940 tmp = expr_to_var_sym(arg, sym);
941 if (!tmp)
942 return NULL;
943 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
944 return alloc_string(buf);
947 tmp = expr_to_var_sym(arg, sym);
948 if (!tmp)
949 return NULL;
950 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
951 free_string(tmp);
952 return alloc_string(buf);