extra: remove some dead code
[smatch.git] / smatch_db.c
blob74ff42fd437392f87643bd9924b68108639c4539
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 static int row_count;
222 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
224 if (argc != 1)
225 return 0;
226 row_count = atoi(argv[0]);
227 return 0;
230 void sql_select_return_states(const char *cols, struct expression *call,
231 int (*callback)(void*, int, char**, char**))
233 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
234 return;
236 if (inlinable(call->fn)) {
237 mem_sql(callback,
238 "select %s from return_states where call_id = '%lu';",
239 cols, (unsigned long)call);
240 return;
243 row_count = 0;
244 run_sql(get_row_count, "select count(*) from return_states where %s;",
245 get_static_filter(call->fn->symbol));
246 if (row_count > 1000)
247 return;
249 run_sql(callback, "select %s from return_states where %s;",
250 cols, get_static_filter(call->fn->symbol));
253 void sql_select_call_implies(const char *cols, struct expression *call,
254 int (*callback)(void*, int, char**, char**))
256 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
257 return;
259 if (inlinable(call->fn)) {
260 mem_sql(callback,
261 "select %s from call_implies where call_id = '%lu';",
262 cols, (unsigned long)call);
263 return;
266 run_sql(callback, "select %s from call_implies where %s;",
267 cols, get_static_filter(call->fn->symbol));
270 void sql_select_return_values(const char *cols, struct expression *call,
271 int (*callback)(void*, int, char**, char**))
273 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
274 return;
276 if (inlinable(call->fn)) {
277 mem_sql(callback,
278 "select %s from return_values where call_id = '%lu';",
279 cols, (unsigned long)call);
280 return;
283 run_sql(callback, "select %s from return_values where %s;",
284 cols, get_static_filter(call->fn->symbol));
287 void sql_select_caller_info(const char *cols, struct symbol *sym,
288 int (*callback)(void*, int, char**, char**))
290 if (__inline_fn) {
291 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
292 cols, (unsigned long)__inline_fn);
293 return;
296 run_sql(callback,
297 "select %s from caller_info where %s order by call_id;",
298 cols, get_static_filter(sym));
301 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
303 struct def_callback *def_callback = __alloc_def_callback(0);
305 def_callback->hook_type = type;
306 def_callback->callback = callback;
307 add_ptr_list(&callbacks, def_callback);
311 * These call backs are used when the --info option is turned on to print struct
312 * member information. For example foo->bar could have a state in
313 * smatch_extra.c and also check_user.c.
315 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
317 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
319 member_callback->owner = owner;
320 member_callback->callback = callback;
321 add_ptr_list(&member_callbacks, member_callback);
324 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist))
326 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
328 callback->callback = fn;
329 add_ptr_list(&returned_state_callbacks, callback);
332 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
334 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
336 member_callback->owner = owner;
337 member_callback->callback = callback;
338 add_ptr_list(&returned_member_callbacks, member_callback);
341 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
343 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
345 cb->type = type;
346 cb->callback = callback;
347 add_ptr_list(&call_implies_cb_list, cb);
350 static struct symbol *return_type;
351 static struct range_list *return_range_list;
352 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
354 if (argc != 1)
355 return 0;
356 if (option_debug)
357 sm_msg("return type %d", type_positive_bits(return_type));
358 str_to_rl(return_type, argv[0], &return_range_list);
359 return 0;
362 struct range_list *db_return_vals(struct expression *expr)
364 return_type = get_type(expr);
365 if (!return_type)
366 return NULL;
367 return_range_list = NULL;
368 sql_select_return_values("return", expr, db_return_callback);
369 return return_range_list;
372 static void match_call_marker(struct expression *expr)
375 * we just want to record something in the database so that if we have
376 * two calls like: frob(4); frob(some_unkown); then on the receiving
377 * side we know that sometimes frob is called with unknown parameters.
380 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
383 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
384 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
386 struct sm_state *sm;
387 char *name;
388 struct symbol *sym;
389 int len;
390 char printed_name[256];
391 int is_address = 0;
393 expr = strip_expr(expr);
394 if (expr->type == EXPR_PREOP && expr->op == '&') {
395 expr = strip_expr(expr->unop);
396 is_address = 1;
399 name = expr_to_var_sym(expr, &sym);
400 if (!name || !sym)
401 goto free;
403 len = strlen(name);
404 FOR_EACH_PTR(slist, sm) {
405 if (sm->sym != sym)
406 continue;
407 if (strcmp(name, sm->name) == 0) {
408 if (is_address)
409 snprintf(printed_name, sizeof(printed_name), "*$$");
410 else /* these are already handled. fixme: handle them here */
411 continue;
412 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
413 snprintf(printed_name, sizeof(printed_name), "*$$");
414 } else if (strncmp(name, sm->name, len) == 0) {
415 if (is_address)
416 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
417 else
418 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
419 } else {
420 continue;
422 callback(call, param, printed_name, sm->state);
423 } END_FOR_EACH_PTR(sm);
424 free:
425 free_string(name);
428 static void match_call_info(struct expression *call)
430 struct member_info_callback *cb;
431 struct expression *arg;
432 struct state_list *slist;
433 char *name;
434 int i;
436 name = get_fnptr_name(call->fn);
437 if (!name)
438 return;
440 FOR_EACH_PTR(member_callbacks, cb) {
441 slist = get_all_states(cb->owner);
442 i = 0;
443 FOR_EACH_PTR(call->args, arg) {
444 print_struct_members(call, arg, i, slist, cb->callback);
445 i++;
446 } END_FOR_EACH_PTR(arg);
447 free_slist(&slist);
448 } END_FOR_EACH_PTR(cb);
450 free_string(name);
453 static int get_param(int param, char **name, struct symbol **sym)
455 struct symbol *arg;
456 int i;
458 i = 0;
459 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
461 * this is a temporary hack to work around a bug (I think in sparse?)
462 * 2.6.37-rc1:fs/reiserfs/journal.o
463 * If there is a function definition without parameter name found
464 * after a function implementation then it causes a crash.
465 * int foo() {}
466 * int bar(char *);
468 if (arg->ident->name < (char *)100)
469 continue;
470 if (i == param && arg->ident->name) {
471 *name = arg->ident->name;
472 *sym = arg;
473 return TRUE;
475 i++;
476 } END_FOR_EACH_PTR(arg);
478 return FALSE;
481 static struct state_list *final_states;
482 static int prev_func_id = -1;
483 static int db_callback(void *unused, int argc, char **argv, char **azColName)
485 int func_id;
486 long type;
487 long param;
488 char *name = NULL;
489 struct symbol *sym = NULL;
490 struct def_callback *def_callback;
492 if (argc != 5)
493 return 0;
495 func_id = atoi(argv[0]);
496 errno = 0;
497 type = strtol(argv[1], NULL, 10);
498 param = strtol(argv[2], NULL, 10);
499 if (errno)
500 return 0;
502 if (prev_func_id == -1)
503 prev_func_id = func_id;
504 if (func_id != prev_func_id) {
505 merge_slist(&final_states, __pop_fake_cur_slist());
506 __push_fake_cur_slist();
507 __unnullify_path();
508 prev_func_id = func_id;
511 if (type == INTERNAL)
512 return 0;
513 if (param >= 0 && !get_param(param, &name, &sym))
514 return 0;
516 FOR_EACH_PTR(callbacks, def_callback) {
517 if (def_callback->hook_type == type)
518 def_callback->callback(name, sym, argv[3], argv[4]);
519 } END_FOR_EACH_PTR(def_callback);
521 return 0;
524 static void get_direct_callers(struct symbol *sym)
526 sql_select_caller_info("call_id, type, parameter, key, value", sym,
527 db_callback);
530 static struct string_list *ptr_names_done;
531 static struct string_list *ptr_names;
533 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
535 insert_string(&ptr_names, alloc_string(argv[0]));
536 return 0;
539 static char *get_next_ptr_name(void)
541 char *ptr;
543 FOR_EACH_PTR(ptr_names, ptr) {
544 if (list_has_string(ptr_names_done, ptr))
545 continue;
546 insert_string(&ptr_names_done, ptr);
547 return ptr;
548 } END_FOR_EACH_PTR(ptr);
549 return NULL;
552 static void get_ptr_names(const char *file, const char *name)
554 char sql_filter[1024];
555 int before, after;
557 if (file) {
558 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
559 file, name);
560 } else {
561 snprintf(sql_filter, 1024, "function = '%s';", name);
564 before = ptr_list_size((struct ptr_list *)ptr_names);
566 run_sql(get_ptr_name,
567 "select distinct ptr from function_ptr where %s",
568 sql_filter);
570 after = ptr_list_size((struct ptr_list *)ptr_names);
571 if (before == after)
572 return;
574 while ((name = get_next_ptr_name()))
575 get_ptr_names(NULL, name);
578 static void get_function_pointer_callers(struct symbol *sym)
580 char *ptr;
582 if (sym->ctype.modifiers & MOD_STATIC)
583 get_ptr_names(get_base_file(), sym->ident->name);
584 else
585 get_ptr_names(NULL, sym->ident->name);
587 FOR_EACH_PTR(ptr_names, ptr) {
588 run_sql(db_callback, "select call_id, type, parameter, key, value"
589 " from caller_info where function = '%s' order by call_id",
590 ptr);
591 free_string(ptr);
592 } END_FOR_EACH_PTR(ptr);
594 __free_ptr_list((struct ptr_list **)&ptr_names);
595 __free_ptr_list((struct ptr_list **)&ptr_names_done);
598 static void match_data_from_db(struct symbol *sym)
600 struct sm_state *sm;
602 if (!sym || !sym->ident || !sym->ident->name)
603 return;
605 __push_fake_cur_slist();
606 __unnullify_path();
607 prev_func_id = -1;
609 get_direct_callers(sym);
610 if (!__inline_fn)
611 get_function_pointer_callers(sym);
613 merge_slist(&final_states, __pop_fake_cur_slist());
615 FOR_EACH_PTR(final_states, sm) {
616 __set_sm(sm);
617 } END_FOR_EACH_PTR(sm);
619 free_slist(&final_states);
622 static struct expression *call_implies_call_expr;
623 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
625 struct call_implies_callback *cb;
626 struct expression *arg = NULL;
627 int type;
628 int param;
630 if (argc != 4)
631 return 0;
633 type = atoi(argv[1]);
634 param = atoi(argv[2]);
636 FOR_EACH_PTR(call_implies_cb_list, cb) {
637 if (cb->type != type)
638 continue;
639 if (param != -1) {
640 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
641 if (!arg)
642 continue;
644 cb->callback(arg, argv[3]);
645 } END_FOR_EACH_PTR(cb);
647 return 0;
650 static void match_call_implies(struct expression *expr)
652 call_implies_call_expr = expr;
653 sql_select_call_implies("function, type, parameter, value", expr,
654 call_implies_callbacks);
655 return;
658 static void print_initializer_list(struct expression_list *expr_list,
659 struct symbol *struct_type)
661 struct expression *expr;
662 struct symbol *base_type;
663 char struct_name[256];
665 FOR_EACH_PTR(expr_list, expr) {
666 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
667 print_initializer_list(expr->idx_expression->expr_list, struct_type);
668 continue;
670 if (expr->type != EXPR_IDENTIFIER)
671 continue;
672 if (!expr->expr_ident)
673 continue;
674 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
675 continue;
676 base_type = get_type(expr->ident_expression);
677 if (!base_type || base_type->type != SYM_FN)
678 continue;
679 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
680 struct_type->ident->name, expr->expr_ident->name);
681 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
682 struct_name);
683 } END_FOR_EACH_PTR(expr);
686 static void global_variable(struct symbol *sym)
688 struct symbol *struct_type;
690 if (!sym->ident)
691 return;
692 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
693 return;
694 struct_type = get_base_type(sym);
695 if (!struct_type)
696 return;
697 if (struct_type->type == SYM_ARRAY) {
698 struct_type = get_base_type(struct_type);
699 if (!struct_type)
700 return;
702 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
703 return;
704 print_initializer_list(sym->initializer->expr_list, struct_type);
707 static void match_return_info(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
709 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
712 static int return_id;
713 static void match_function_def(struct symbol *sym)
715 if (!__inline_fn)
716 return_id = 0;
719 static void call_return_state_hooks_compare(struct expression *expr)
721 struct returned_state_callback *cb;
722 struct state_list *slist;
723 char *return_ranges;
724 int final_pass_orig = final_pass;
726 __push_fake_cur_slist();
728 final_pass = 0;
729 __split_whole_condition(expr);
730 final_pass = final_pass_orig;
732 return_ranges = alloc_sname("1");
734 return_id++;
735 slist = __get_cur_slist();
736 FOR_EACH_PTR(returned_state_callbacks, cb) {
737 cb->callback(return_id, return_ranges, expr, slist);
738 } END_FOR_EACH_PTR(cb);
740 __push_true_states();
741 __use_false_states();
743 return_ranges = alloc_sname("0");;
744 return_id++;
745 slist = __get_cur_slist();
746 FOR_EACH_PTR(returned_state_callbacks, cb) {
747 cb->callback(return_id, return_ranges, expr, slist);
748 } END_FOR_EACH_PTR(cb);
750 __merge_true_states();
751 __pop_fake_cur_slist();
754 static int call_return_state_hooks_split_possible(struct expression *expr)
756 struct returned_state_callback *cb;
757 struct state_list *slist;
758 struct range_list *rl;
759 char *return_ranges;
760 struct sm_state *sm;
761 struct sm_state *tmp;
762 int ret = 0;
763 int nr_possible, nr_states;
765 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
766 if (!sm || !sm->merged)
767 return 0;
769 /* bail if it gets too complicated */
770 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
771 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
773 * the main thing option_info because we don't want to print a
774 * million lines of output. If someone else, like check_locking.c
775 * wants this data, then it doesn't cause a slow down to provide it.
777 if (option_info && nr_possible >= 100)
778 return 0;
779 if (option_info && nr_states * nr_possible >= 2000)
780 return 0;
782 FOR_EACH_PTR(sm->possible, tmp) {
783 if (tmp->merged)
784 continue;
786 ret = 1;
787 __push_fake_cur_slist();
789 overwrite_states_using_pool(tmp);
791 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
792 return_ranges = show_rl(rl);
794 return_id++;
795 slist = __get_cur_slist();
796 FOR_EACH_PTR(returned_state_callbacks, cb) {
797 cb->callback(return_id, return_ranges, expr, slist);
798 } END_FOR_EACH_PTR(cb);
800 __pop_fake_cur_slist();
801 } END_FOR_EACH_PTR(tmp);
803 return ret;
806 static void call_return_state_hooks(struct expression *expr)
808 struct returned_state_callback *cb;
809 struct state_list *slist;
810 struct range_list *rl;
811 char *return_ranges;
812 int nr_states;
814 expr = strip_expr(expr);
816 if (!expr) {
817 return_ranges = alloc_sname("");
818 } else if (is_condition(expr)) {
819 call_return_state_hooks_compare(expr);
820 return;
821 } else if (call_return_state_hooks_split_possible(expr)) {
822 return;
823 } else if (get_implied_rl(expr, &rl)) {
824 rl = cast_rl(cur_func_return_type(), rl);
825 return_ranges = show_rl(rl);
826 } else {
827 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
828 return_ranges = show_rl(rl);
831 return_id++;
832 slist = __get_cur_slist();
833 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
834 FOR_EACH_PTR(returned_state_callbacks, cb) {
835 if (nr_states < 10000)
836 cb->callback(return_id, return_ranges, expr, slist);
837 else
838 cb->callback(return_id, return_ranges, expr, NULL);
839 } END_FOR_EACH_PTR(cb);
842 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
844 struct returned_member_callback *cb;
845 struct state_list *my_slist;
846 struct sm_state *sm;
847 struct symbol *type;
848 char *name;
849 char member_name[256];
850 int len;
852 type = get_type(expr);
853 if (!type || type->type != SYM_PTR)
854 return;
855 type = get_real_base_type(type);
856 if (!type || type->type != SYM_STRUCT)
857 return;
858 name = expr_to_var(expr);
859 if (!name)
860 return;
862 member_name[sizeof(member_name) - 1] = '\0';
863 strcpy(member_name, "$$");
865 len = strlen(name);
866 FOR_EACH_PTR(returned_member_callbacks, cb) {
867 my_slist = get_all_states_slist(cb->owner, slist);
868 FOR_EACH_PTR(my_slist, sm) {
869 if (strncmp(sm->name, name, len) != 0)
870 continue;
871 if (strncmp(sm->name + len, "->", 2) != 0)
872 continue;
873 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
874 cb->callback(return_id, return_ranges, member_name, sm->state);
875 } END_FOR_EACH_PTR(sm);
876 free_slist(&my_slist);
877 } END_FOR_EACH_PTR(cb);
879 free_string(name);
882 static void reset_memdb(void)
884 mem_sql(NULL, "delete from caller_info;");
885 mem_sql(NULL, "delete from return_states;");
886 mem_sql(NULL, "delete from call_implies;");
887 mem_sql(NULL, "delete from return_values;");
890 static void match_end_func_info(struct symbol *sym)
892 if (__path_is_null())
893 return;
894 call_return_state_hooks(NULL);
895 if (!__inline_fn)
896 reset_memdb();
899 static void init_memdb(void)
901 char *err = NULL;
902 int rc;
903 const char *schema_files[] = {
904 "db/db.schema",
905 "db/caller_info.schema",
906 "db/return_states.schema",
907 "db/type_size.schema",
908 "db/call_implies.schema",
909 "db/function_ptr.schema",
910 "db/return_values.schema",
911 "db/local_values.schema",
913 static char buf[4096];
914 int fd;
915 int ret;
916 int i;
918 rc = sqlite3_open(":memory:", &mem_db);
919 if (rc != SQLITE_OK) {
920 printf("Error starting In-Memory database.");
921 return;
924 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
925 fd = open_data_file(schema_files[i]);
926 if (fd < 0)
927 continue;
928 ret = read(fd, buf, sizeof(buf));
929 if (ret == sizeof(buf)) {
930 printf("Schema file too large: %s (limit %zd bytes)",
931 schema_files[i], sizeof(buf));
933 buf[ret] = '\0';
934 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
935 if (rc != SQLITE_OK) {
936 fprintf(stderr, "SQL error #2: %s\n", err);
937 fprintf(stderr, "%s\n", buf);
942 void open_smatch_db(void)
944 int rc;
946 if (option_no_db)
947 return;
949 init_memdb();
951 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
952 if (rc != SQLITE_OK) {
953 option_no_db = 1;
954 return;
956 return;
959 static void register_common_funcs(void)
961 struct token *token;
962 char *func;
963 char filename[256];
965 if (option_project == PROJ_NONE)
966 strcpy(filename, "common_functions");
967 else
968 snprintf(filename, 256, "%s.common_functions", option_project_str);
970 token = get_tokens_file(filename);
971 if (!token)
972 return;
973 if (token_type(token) != TOKEN_STREAMBEGIN)
974 return;
975 token = token->next;
976 while (token_type(token) != TOKEN_STREAMEND) {
977 if (token_type(token) != TOKEN_IDENT)
978 return;
979 func = alloc_string(show_ident(token->ident));
980 add_ptr_list(&common_funcs, func);
981 token = token->next;
983 clear_token_alloc();
987 void register_definition_db_callbacks(int id)
989 add_hook(&match_function_def, FUNC_DEF_HOOK);
991 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
992 add_hook(&global_variable, BASE_HOOK);
993 add_hook(&global_variable, DECLARATION_HOOK);
994 add_returned_state_callback(match_return_info);
995 add_returned_state_callback(print_returned_struct_members);
996 add_hook(&call_return_state_hooks, RETURN_HOOK);
997 add_hook(&match_end_func_info, END_FUNC_HOOK);
999 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1000 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1002 register_common_funcs();
1005 void register_db_call_marker(int id)
1007 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1010 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1012 char buf[256];
1013 char *tmp;
1015 if (strcmp(key, "$$") == 0)
1016 return expr_to_var_sym(arg, sym);
1018 if (strcmp(key, "*$$") == 0) {
1019 if (arg->type == EXPR_PREOP && arg->op == '&') {
1020 arg = strip_expr(arg->unop);
1021 return expr_to_var_sym(arg, sym);
1022 } else {
1023 tmp = expr_to_var_sym(arg, sym);
1024 if (!tmp)
1025 return NULL;
1026 snprintf(buf, sizeof(buf), "*%s", tmp);
1027 free_string(tmp);
1028 return alloc_string(buf);
1032 if (arg->type == EXPR_PREOP && arg->op == '&') {
1033 arg = strip_expr(arg->unop);
1034 tmp = expr_to_var_sym(arg, sym);
1035 if (!tmp)
1036 return NULL;
1037 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1038 return alloc_string(buf);
1041 tmp = expr_to_var_sym(arg, sym);
1042 if (!tmp)
1043 return NULL;
1044 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1045 free_string(tmp);
1046 return alloc_string(buf);