db: drop unused return_values table
[smatch.git] / smatch_db.c
blobdb119ae6bdadf0687272f2689eff71c0bec10f9c
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);
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_call_implies(int type, int param, int value)
183 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
184 get_function(), (unsigned long)__inline_fn, fn_static(),
185 type, param, value);
188 void sql_insert_type_size(const char *member, int size)
190 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
193 void sql_insert_local_values(const char *name, const char *value)
195 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
198 static char *get_static_filter(struct symbol *sym)
200 static char sql_filter[1024];
202 if (sym->ctype.modifiers & MOD_STATIC) {
203 snprintf(sql_filter, sizeof(sql_filter),
204 "file = '%s' and function = '%s' and static = '1'",
205 get_base_file(), sym->ident->name);
206 } else {
207 snprintf(sql_filter, sizeof(sql_filter),
208 "function = '%s' and static = '0'", sym->ident->name);
211 return sql_filter;
214 static int row_count;
215 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
217 if (argc != 1)
218 return 0;
219 row_count = atoi(argv[0]);
220 return 0;
223 void sql_select_return_states(const char *cols, struct expression *call,
224 int (*callback)(void*, int, char**, char**))
226 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
227 return;
229 if (inlinable(call->fn)) {
230 mem_sql(callback,
231 "select %s from return_states where call_id = '%lu';",
232 cols, (unsigned long)call);
233 return;
236 row_count = 0;
237 run_sql(get_row_count, "select count(*) from return_states where %s;",
238 get_static_filter(call->fn->symbol));
239 if (row_count > 1000)
240 return;
242 run_sql(callback, "select %s from return_states where %s;",
243 cols, get_static_filter(call->fn->symbol));
246 void sql_select_call_implies(const char *cols, struct expression *call,
247 int (*callback)(void*, int, char**, char**))
249 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
250 return;
252 if (inlinable(call->fn)) {
253 mem_sql(callback,
254 "select %s from call_implies where call_id = '%lu';",
255 cols, (unsigned long)call);
256 return;
259 run_sql(callback, "select %s from call_implies where %s;",
260 cols, get_static_filter(call->fn->symbol));
263 void sql_select_caller_info(const char *cols, struct symbol *sym,
264 int (*callback)(void*, int, char**, char**))
266 if (__inline_fn) {
267 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
268 cols, (unsigned long)__inline_fn);
269 return;
272 run_sql(callback,
273 "select %s from caller_info where %s order by call_id;",
274 cols, get_static_filter(sym));
277 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
279 struct def_callback *def_callback = __alloc_def_callback(0);
281 def_callback->hook_type = type;
282 def_callback->callback = callback;
283 add_ptr_list(&callbacks, def_callback);
287 * These call backs are used when the --info option is turned on to print struct
288 * member information. For example foo->bar could have a state in
289 * smatch_extra.c and also check_user.c.
291 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
293 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
295 member_callback->owner = owner;
296 member_callback->callback = callback;
297 add_ptr_list(&member_callbacks, member_callback);
300 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
302 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
304 callback->callback = fn;
305 add_ptr_list(&returned_state_callbacks, callback);
308 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
310 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
312 member_callback->owner = owner;
313 member_callback->callback = callback;
314 add_ptr_list(&returned_member_callbacks, member_callback);
317 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
319 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
321 cb->type = type;
322 cb->callback = callback;
323 add_ptr_list(&call_implies_cb_list, cb);
326 static struct symbol *return_type;
327 static struct range_list *return_range_list;
328 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
330 struct range_list *rl;
332 if (argc != 1)
333 return 0;
334 str_to_rl(return_type, argv[0], &rl);
335 return_range_list = rl_union(return_range_list, rl);
336 return 0;
339 struct range_list *db_return_vals(struct expression *expr)
341 return_type = get_type(expr);
342 if (!return_type)
343 return NULL;
344 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
345 return NULL;
347 return_range_list = NULL;
348 if (inlinable(expr->fn)) {
349 mem_sql(db_return_callback,
350 "select distinct return from return_states where call_id = '%lu';",
351 (unsigned long)expr);
352 } else {
353 run_sql(db_return_callback,
354 "select distinct return from return_states where %s;",
355 get_static_filter(expr->fn->symbol));
357 return return_range_list;
360 static void match_call_marker(struct expression *expr)
363 * we just want to record something in the database so that if we have
364 * two calls like: frob(4); frob(some_unkown); then on the receiving
365 * side we know that sometimes frob is called with unknown parameters.
368 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
371 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
372 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
374 struct sm_state *sm;
375 char *name;
376 struct symbol *sym;
377 int len;
378 char printed_name[256];
379 int is_address = 0;
381 expr = strip_expr(expr);
382 if (expr->type == EXPR_PREOP && expr->op == '&') {
383 expr = strip_expr(expr->unop);
384 is_address = 1;
387 name = expr_to_var_sym(expr, &sym);
388 if (!name || !sym)
389 goto free;
391 len = strlen(name);
392 FOR_EACH_PTR(slist, sm) {
393 if (sm->sym != sym)
394 continue;
395 if (strcmp(name, sm->name) == 0) {
396 if (is_address)
397 snprintf(printed_name, sizeof(printed_name), "*$$");
398 else /* these are already handled. fixme: handle them here */
399 continue;
400 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
401 snprintf(printed_name, sizeof(printed_name), "*$$");
402 } else if (strncmp(name, sm->name, len) == 0) {
403 if (is_address)
404 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
405 else
406 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
407 } else {
408 continue;
410 callback(call, param, printed_name, sm->state);
411 } END_FOR_EACH_PTR(sm);
412 free:
413 free_string(name);
416 static void match_call_info(struct expression *call)
418 struct member_info_callback *cb;
419 struct expression *arg;
420 struct state_list *slist;
421 char *name;
422 int i;
424 name = get_fnptr_name(call->fn);
425 if (!name)
426 return;
428 FOR_EACH_PTR(member_callbacks, cb) {
429 slist = get_all_states(cb->owner);
430 i = 0;
431 FOR_EACH_PTR(call->args, arg) {
432 print_struct_members(call, arg, i, slist, cb->callback);
433 i++;
434 } END_FOR_EACH_PTR(arg);
435 free_slist(&slist);
436 } END_FOR_EACH_PTR(cb);
438 free_string(name);
441 static int get_param(int param, char **name, struct symbol **sym)
443 struct symbol *arg;
444 int i;
446 i = 0;
447 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
449 * this is a temporary hack to work around a bug (I think in sparse?)
450 * 2.6.37-rc1:fs/reiserfs/journal.o
451 * If there is a function definition without parameter name found
452 * after a function implementation then it causes a crash.
453 * int foo() {}
454 * int bar(char *);
456 if (arg->ident->name < (char *)100)
457 continue;
458 if (i == param && arg->ident->name) {
459 *name = arg->ident->name;
460 *sym = arg;
461 return TRUE;
463 i++;
464 } END_FOR_EACH_PTR(arg);
466 return FALSE;
469 static struct state_list *final_states;
470 static int prev_func_id = -1;
471 static int db_callback(void *unused, int argc, char **argv, char **azColName)
473 int func_id;
474 long type;
475 long param;
476 char *name = NULL;
477 struct symbol *sym = NULL;
478 struct def_callback *def_callback;
479 struct state_list *slist;
481 if (argc != 5)
482 return 0;
484 func_id = atoi(argv[0]);
485 errno = 0;
486 type = strtol(argv[1], NULL, 10);
487 param = strtol(argv[2], NULL, 10);
488 if (errno)
489 return 0;
491 if (prev_func_id == -1)
492 prev_func_id = func_id;
493 if (func_id != prev_func_id) {
494 slist = __pop_fake_cur_slist();
495 merge_slist(&final_states, slist);
496 free_slist(&slist);
497 __push_fake_cur_slist();
498 __unnullify_path();
499 prev_func_id = func_id;
502 if (type == INTERNAL)
503 return 0;
504 if (param >= 0 && !get_param(param, &name, &sym))
505 return 0;
507 FOR_EACH_PTR(callbacks, def_callback) {
508 if (def_callback->hook_type == type)
509 def_callback->callback(name, sym, argv[3], argv[4]);
510 } END_FOR_EACH_PTR(def_callback);
512 return 0;
515 static void get_direct_callers(struct symbol *sym)
517 sql_select_caller_info("call_id, type, parameter, key, value", sym,
518 db_callback);
521 static struct string_list *ptr_names_done;
522 static struct string_list *ptr_names;
524 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
526 insert_string(&ptr_names, alloc_string(argv[0]));
527 return 0;
530 static char *get_next_ptr_name(void)
532 char *ptr;
534 FOR_EACH_PTR(ptr_names, ptr) {
535 if (list_has_string(ptr_names_done, ptr))
536 continue;
537 insert_string(&ptr_names_done, ptr);
538 return ptr;
539 } END_FOR_EACH_PTR(ptr);
540 return NULL;
543 static void get_ptr_names(const char *file, const char *name)
545 char sql_filter[1024];
546 int before, after;
548 if (file) {
549 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
550 file, name);
551 } else {
552 snprintf(sql_filter, 1024, "function = '%s';", name);
555 before = ptr_list_size((struct ptr_list *)ptr_names);
557 run_sql(get_ptr_name,
558 "select distinct ptr from function_ptr where %s",
559 sql_filter);
561 after = ptr_list_size((struct ptr_list *)ptr_names);
562 if (before == after)
563 return;
565 while ((name = get_next_ptr_name()))
566 get_ptr_names(NULL, name);
569 static void get_function_pointer_callers(struct symbol *sym)
571 char *ptr;
573 if (sym->ctype.modifiers & MOD_STATIC)
574 get_ptr_names(get_base_file(), sym->ident->name);
575 else
576 get_ptr_names(NULL, sym->ident->name);
578 FOR_EACH_PTR(ptr_names, ptr) {
579 run_sql(db_callback, "select call_id, type, parameter, key, value"
580 " from caller_info where function = '%s' order by call_id",
581 ptr);
582 free_string(ptr);
583 } END_FOR_EACH_PTR(ptr);
585 __free_ptr_list((struct ptr_list **)&ptr_names);
586 __free_ptr_list((struct ptr_list **)&ptr_names_done);
589 static void match_data_from_db(struct symbol *sym)
591 struct sm_state *sm;
592 struct state_list *slist;
594 if (!sym || !sym->ident || !sym->ident->name)
595 return;
597 __push_fake_cur_slist();
598 __unnullify_path();
599 prev_func_id = -1;
601 get_direct_callers(sym);
602 if (!__inline_fn)
603 get_function_pointer_callers(sym);
605 slist = __pop_fake_cur_slist();
606 merge_slist(&final_states, slist);
607 free_slist(&slist);
609 FOR_EACH_PTR(final_states, sm) {
610 __set_sm(sm);
611 } END_FOR_EACH_PTR(sm);
613 free_slist(&final_states);
616 static struct expression *call_implies_call_expr;
617 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
619 struct call_implies_callback *cb;
620 struct expression *arg = NULL;
621 int type;
622 int param;
624 if (argc != 4)
625 return 0;
627 type = atoi(argv[1]);
628 param = atoi(argv[2]);
630 FOR_EACH_PTR(call_implies_cb_list, cb) {
631 if (cb->type != type)
632 continue;
633 if (param != -1) {
634 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
635 if (!arg)
636 continue;
638 cb->callback(arg, argv[3]);
639 } END_FOR_EACH_PTR(cb);
641 return 0;
644 static void match_call_implies(struct expression *expr)
646 call_implies_call_expr = expr;
647 sql_select_call_implies("function, type, parameter, value", expr,
648 call_implies_callbacks);
649 return;
652 static void print_initializer_list(struct expression_list *expr_list,
653 struct symbol *struct_type)
655 struct expression *expr;
656 struct symbol *base_type;
657 char struct_name[256];
659 FOR_EACH_PTR(expr_list, expr) {
660 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
661 print_initializer_list(expr->idx_expression->expr_list, struct_type);
662 continue;
664 if (expr->type != EXPR_IDENTIFIER)
665 continue;
666 if (!expr->expr_ident)
667 continue;
668 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
669 continue;
670 base_type = get_type(expr->ident_expression);
671 if (!base_type || base_type->type != SYM_FN)
672 continue;
673 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
674 struct_type->ident->name, expr->expr_ident->name);
675 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
676 struct_name);
677 } END_FOR_EACH_PTR(expr);
680 static void global_variable(struct symbol *sym)
682 struct symbol *struct_type;
684 if (!sym->ident)
685 return;
686 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
687 return;
688 struct_type = get_base_type(sym);
689 if (!struct_type)
690 return;
691 if (struct_type->type == SYM_ARRAY) {
692 struct_type = get_base_type(struct_type);
693 if (!struct_type)
694 return;
696 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
697 return;
698 print_initializer_list(sym->initializer->expr_list, struct_type);
701 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
703 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
706 static int return_id;
707 static void match_function_def(struct symbol *sym)
709 if (!__inline_fn)
710 return_id = 0;
713 static void call_return_state_hooks_compare(struct expression *expr)
715 struct returned_state_callback *cb;
716 char *return_ranges;
717 int final_pass_orig = final_pass;
719 __push_fake_cur_slist();
721 final_pass = 0;
722 __split_whole_condition(expr);
723 final_pass = final_pass_orig;
725 return_ranges = alloc_sname("1");
727 return_id++;
728 FOR_EACH_PTR(returned_state_callbacks, cb) {
729 cb->callback(return_id, return_ranges, expr);
730 } END_FOR_EACH_PTR(cb);
732 __push_true_states();
733 __use_false_states();
735 return_ranges = alloc_sname("0");;
736 return_id++;
737 FOR_EACH_PTR(returned_state_callbacks, cb) {
738 cb->callback(return_id, return_ranges, expr);
739 } END_FOR_EACH_PTR(cb);
741 __merge_true_states();
742 __free_fake_cur_slist();
745 static int call_return_state_hooks_split_possible(struct expression *expr)
747 struct returned_state_callback *cb;
748 struct range_list *rl;
749 char *return_ranges;
750 struct sm_state *sm;
751 struct sm_state *tmp;
752 int ret = 0;
753 int nr_possible, nr_states;
754 char *compare_str;
755 char buf[128];
757 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
758 if (!sm || !sm->merged)
759 return 0;
761 /* bail if it gets too complicated */
762 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
763 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
765 * the main thing option_info because we don't want to print a
766 * million lines of output. If someone else, like check_locking.c
767 * wants this data, then it doesn't cause a slow down to provide it.
769 if (option_info && nr_possible >= 100)
770 return 0;
771 if (option_info && nr_states * nr_possible >= 2000)
772 return 0;
774 compare_str = expr_lte_to_param(expr);
776 FOR_EACH_PTR(sm->possible, tmp) {
777 if (tmp->merged)
778 continue;
780 ret = 1;
781 __push_fake_cur_slist();
783 overwrite_states_using_pool(tmp);
785 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
786 return_ranges = show_rl(rl);
787 if (compare_str) {
788 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
789 return_ranges = alloc_sname(buf);
792 return_id++;
793 FOR_EACH_PTR(returned_state_callbacks, cb) {
794 cb->callback(return_id, return_ranges, expr);
795 } END_FOR_EACH_PTR(cb);
797 __free_fake_cur_slist();
798 } END_FOR_EACH_PTR(tmp);
800 return ret;
803 static char *get_return_ranges_str(struct expression *expr)
805 struct range_list *rl;
806 char *return_ranges;
807 char *compare_str;
808 char buf[128];
810 if (!expr)
811 return alloc_sname("");
812 compare_str = expr_equal_to_param(expr);
813 if (compare_str)
814 return compare_str;
816 if (get_implied_rl(expr, &rl)) {
817 rl = cast_rl(cur_func_return_type(), rl);
818 return_ranges = show_rl(rl);
819 } else {
820 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
821 return_ranges = show_rl(rl);
823 compare_str = expr_lte_to_param(expr);
824 if (compare_str) {
825 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
826 return alloc_sname(buf);
828 return return_ranges;
831 static void call_return_state_hooks(struct expression *expr)
833 struct returned_state_callback *cb;
834 char *return_ranges;
835 int nr_states;
837 expr = strip_expr(expr);
839 if (is_condition(expr)) {
840 call_return_state_hooks_compare(expr);
841 return;
842 } else if (call_return_state_hooks_split_possible(expr)) {
843 return;
846 return_ranges = get_return_ranges_str(expr);
848 return_id++;
849 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
850 if (nr_states >= 10000) {
851 match_return_info(return_id, return_ranges, expr);
852 return;
854 FOR_EACH_PTR(returned_state_callbacks, cb) {
855 cb->callback(return_id, return_ranges, expr);
856 } END_FOR_EACH_PTR(cb);
859 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
861 struct returned_member_callback *cb;
862 struct state_list *my_slist;
863 struct sm_state *sm;
864 struct symbol *type;
865 char *name;
866 char member_name[256];
867 int len;
869 type = get_type(expr);
870 if (!type || type->type != SYM_PTR)
871 return;
872 type = get_real_base_type(type);
873 if (!type || type->type != SYM_STRUCT)
874 return;
875 name = expr_to_var(expr);
876 if (!name)
877 return;
879 member_name[sizeof(member_name) - 1] = '\0';
880 strcpy(member_name, "$$");
882 len = strlen(name);
883 FOR_EACH_PTR(returned_member_callbacks, cb) {
884 my_slist = get_all_states(cb->owner);
885 FOR_EACH_PTR(my_slist, sm) {
886 if (strncmp(sm->name, name, len) != 0)
887 continue;
888 if (strncmp(sm->name + len, "->", 2) != 0)
889 continue;
890 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
891 cb->callback(return_id, return_ranges, member_name, sm->state);
892 } END_FOR_EACH_PTR(sm);
893 free_slist(&my_slist);
894 } END_FOR_EACH_PTR(cb);
896 free_string(name);
899 static void reset_memdb(void)
901 mem_sql(NULL, "delete from caller_info;");
902 mem_sql(NULL, "delete from return_states;");
903 mem_sql(NULL, "delete from call_implies;");
906 static void match_end_func_info(struct symbol *sym)
908 if (__path_is_null())
909 return;
910 call_return_state_hooks(NULL);
911 if (!__inline_fn)
912 reset_memdb();
915 static void init_memdb(void)
917 char *err = NULL;
918 int rc;
919 const char *schema_files[] = {
920 "db/db.schema",
921 "db/caller_info.schema",
922 "db/return_states.schema",
923 "db/type_size.schema",
924 "db/call_implies.schema",
925 "db/function_ptr.schema",
926 "db/local_values.schema",
928 static char buf[4096];
929 int fd;
930 int ret;
931 int i;
933 rc = sqlite3_open(":memory:", &mem_db);
934 if (rc != SQLITE_OK) {
935 printf("Error starting In-Memory database.");
936 return;
939 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
940 fd = open_data_file(schema_files[i]);
941 if (fd < 0)
942 continue;
943 ret = read(fd, buf, sizeof(buf));
944 if (ret == sizeof(buf)) {
945 printf("Schema file too large: %s (limit %zd bytes)",
946 schema_files[i], sizeof(buf));
948 buf[ret] = '\0';
949 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
950 if (rc != SQLITE_OK) {
951 fprintf(stderr, "SQL error #2: %s\n", err);
952 fprintf(stderr, "%s\n", buf);
957 void open_smatch_db(void)
959 int rc;
961 if (option_no_db)
962 return;
964 init_memdb();
966 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
967 if (rc != SQLITE_OK) {
968 option_no_db = 1;
969 return;
971 return;
974 static void register_common_funcs(void)
976 struct token *token;
977 char *func;
978 char filename[256];
980 if (option_project == PROJ_NONE)
981 strcpy(filename, "common_functions");
982 else
983 snprintf(filename, 256, "%s.common_functions", option_project_str);
985 token = get_tokens_file(filename);
986 if (!token)
987 return;
988 if (token_type(token) != TOKEN_STREAMBEGIN)
989 return;
990 token = token->next;
991 while (token_type(token) != TOKEN_STREAMEND) {
992 if (token_type(token) != TOKEN_IDENT)
993 return;
994 func = alloc_string(show_ident(token->ident));
995 add_ptr_list(&common_funcs, func);
996 token = token->next;
998 clear_token_alloc();
1002 void register_definition_db_callbacks(int id)
1004 add_hook(&match_function_def, FUNC_DEF_HOOK);
1006 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1007 add_hook(&global_variable, BASE_HOOK);
1008 add_hook(&global_variable, DECLARATION_HOOK);
1009 add_returned_state_callback(match_return_info);
1010 add_returned_state_callback(print_returned_struct_members);
1011 add_hook(&call_return_state_hooks, RETURN_HOOK);
1012 add_hook(&match_end_func_info, END_FUNC_HOOK);
1014 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1015 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1017 register_common_funcs();
1020 void register_db_call_marker(int id)
1022 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1025 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1027 struct expression *arg;
1028 char *name = NULL;
1029 char member_name[256];
1031 *sym = NULL;
1033 if (param == -1) {
1034 if (expr->type != EXPR_ASSIGNMENT)
1035 return NULL;
1036 name = expr_to_var_sym(expr->left, sym);
1037 if (!name)
1038 return NULL;
1039 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1040 free_string(name);
1041 return alloc_string(member_name);
1044 while (expr->type == EXPR_ASSIGNMENT)
1045 expr = strip_expr(expr->right);
1046 if (expr->type != EXPR_CALL)
1047 return NULL;
1049 arg = get_argument_from_call_expr(expr->args, param);
1050 if (!arg)
1051 return NULL;
1053 return get_variable_from_key(arg, key, sym);
1056 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1058 char buf[256];
1059 char *tmp;
1061 if (strcmp(key, "$$") == 0)
1062 return expr_to_var_sym(arg, sym);
1064 if (strcmp(key, "*$$") == 0) {
1065 if (arg->type == EXPR_PREOP && arg->op == '&') {
1066 arg = strip_expr(arg->unop);
1067 return expr_to_var_sym(arg, sym);
1068 } else {
1069 tmp = expr_to_var_sym(arg, sym);
1070 if (!tmp)
1071 return NULL;
1072 snprintf(buf, sizeof(buf), "*%s", tmp);
1073 free_string(tmp);
1074 return alloc_string(buf);
1078 if (arg->type == EXPR_PREOP && arg->op == '&') {
1079 arg = strip_expr(arg->unop);
1080 tmp = expr_to_var_sym(arg, sym);
1081 if (!tmp)
1082 return NULL;
1083 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1084 return alloc_string(buf);
1087 tmp = expr_to_var_sym(arg, sym);
1088 if (!tmp)
1089 return NULL;
1090 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1091 free_string(tmp);
1092 return alloc_string(buf);
1095 const char *get_param_name(struct sm_state *sm)
1097 char *param_name;
1098 int name_len;
1099 static char buf[256];
1101 if (!sm->sym->ident)
1102 return NULL;
1104 param_name = sm->sym->ident->name;
1105 name_len = strlen(param_name);
1107 if (strcmp(sm->name, param_name) == 0) {
1108 return "$$";
1109 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1110 strncmp(sm->name, param_name, name_len) == 0) {
1111 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1112 return buf;
1113 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1114 return "*$$";
1116 return NULL;