states: make debug output more consistent
[smatch.git] / smatch_db.c
blob93b546cc10f31f10cac9324007dec5ec79138da0
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <string.h>
19 #include <errno.h>
20 #include <sqlite3.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert(table, values...) \
33 do { \
34 if (!mem_db) \
35 break; \
36 if (__inline_fn) { \
37 char buf[1024]; \
38 char *err, *p = buf; \
39 int rc; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
50 } \
51 break; \
52 } \
53 if (option_info) { \
54 sm_prefix(); \
55 sm_printf("SQL: insert into " #table " values (" values); \
56 sm_printf(");\n"); \
57 } \
58 } while (0)
60 struct def_callback {
61 int hook_type;
62 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
64 ALLOCATOR(def_callback, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list, struct def_callback);
66 static struct callback_list *callbacks;
68 struct member_info_callback {
69 int owner;
70 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
72 ALLOCATOR(member_info_callback, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
74 static struct member_info_cb_list *member_callbacks;
76 struct returned_state_callback {
77 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
79 ALLOCATOR(returned_state_callback, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
81 static struct returned_state_cb_list *returned_state_callbacks;
83 struct returned_member_callback {
84 int owner;
85 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
87 ALLOCATOR(returned_member_callback, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
89 static struct returned_member_cb_list *returned_member_callbacks;
91 struct call_implies_callback {
92 int type;
93 void (*callback)(struct expression *arg, char *key, char *value);
95 ALLOCATOR(call_implies_callback, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
97 static struct call_implies_cb_list *call_implies_cb_list;
99 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
101 int i;
103 for (i = 0; i < argc; i++) {
104 if (i != 0)
105 printf(", ");
106 sm_printf("%s", argv[i]);
108 sm_printf("\n");
109 return 0;
112 void debug_sql(const char *sql)
114 if (!option_debug)
115 return;
116 sm_msg("%s", sql);
117 sql_exec(print_sql_output, NULL, sql);
120 void debug_mem_sql(const char *sql)
122 if (!option_debug)
123 return;
124 sm_msg("%s", sql);
125 sql_mem_exec(print_sql_output, NULL, sql);
128 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
130 char *err = NULL;
131 int rc;
133 if (option_no_db || !db)
134 return;
136 rc = sqlite3_exec(db, sql, callback, data, &err);
137 if (rc != SQLITE_OK) {
138 fprintf(stderr, "SQL error #2: %s\n", err);
139 fprintf(stderr, "SQL: '%s'\n", sql);
143 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
145 char *err = NULL;
146 int rc;
148 if (!mem_db)
149 return;
151 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
152 if (rc != SQLITE_OK) {
153 fprintf(stderr, "SQL error #2: %s\n", err);
154 fprintf(stderr, "SQL: '%s'\n", sql);
158 void sql_insert_return_states(int return_id, const char *return_ranges,
159 int type, int param, const char *key, const char *value)
161 if (key && strlen(key) >= 80)
162 return;
163 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn,
165 return_id, return_ranges, fn_static(), type, param, key, value);
168 static struct string_list *common_funcs;
169 static int is_common_function(const char *fn)
171 char *tmp;
173 if (strncmp(fn, "__builtin_", 10) == 0)
174 return 1;
176 FOR_EACH_PTR(common_funcs, tmp) {
177 if (strcmp(tmp, fn) == 0)
178 return 1;
179 } END_FOR_EACH_PTR(tmp);
181 return 0;
184 void sql_insert_caller_info(struct expression *call, int type,
185 int param, const char *key, const char *value)
187 char *fn;
189 if (!option_info && !__inline_call)
190 return;
192 if (key && strlen(key) >= 80)
193 return;
195 fn = get_fnptr_name(call->fn);
196 if (!fn)
197 return;
199 if (__inline_call) {
200 mem_sql(NULL, NULL,
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn, (unsigned long)call,
203 is_static(call->fn), type, param, key, value);
206 if (!option_info)
207 return;
209 if (is_common_function(fn))
210 return;
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn, is_static(call->fn),
215 type, param, key, value);
217 free_string(fn);
220 void sql_insert_function_ptr(const char *fn, const char *struct_name)
222 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
223 struct_name);
226 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
228 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn, fn_static(),
230 type, param, key, value);
233 void sql_insert_function_type_size(const char *member, const char *ranges)
235 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
238 void sql_insert_local_values(const char *name, const char *value)
240 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
243 void sql_insert_function_type_value(const char *type, const char *value)
245 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
248 void sql_insert_function_type_info(int param, const char *value)
250 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param, value);
254 void sql_insert_data_info(struct expression *data, int type, const char *value)
256 char *data_name;
258 data_name = get_data_info_name(data);
259 if (!data_name)
260 return;
261 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
264 char *get_static_filter(struct symbol *sym)
266 static char sql_filter[1024];
268 if (sym->ctype.modifiers & MOD_STATIC) {
269 snprintf(sql_filter, sizeof(sql_filter),
270 "file = '%s' and function = '%s' and static = '1'",
271 get_base_file(), sym->ident->name);
272 } else {
273 snprintf(sql_filter, sizeof(sql_filter),
274 "function = '%s' and static = '0'", sym->ident->name);
277 return sql_filter;
280 static int row_count;
281 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
283 if (argc != 1)
284 return 0;
285 row_count = atoi(argv[0]);
286 return 0;
289 static void sql_select_return_states_pointer(const char *cols,
290 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
292 char *ptr;
294 ptr = get_fnptr_name(call->fn);
295 if (!ptr)
296 return;
298 run_sql(callback, info,
299 "select %s from return_states join function_ptr where "
300 "return_states.function == function_ptr.function and ptr = '%s'"
301 "and searchable = 1 order by return_id, type;",
302 cols, ptr);
305 static int is_local_symbol(struct expression *expr)
307 if (expr->type != EXPR_SYMBOL)
308 return 0;
309 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
310 return 0;
311 return 1;
314 void sql_select_return_states(const char *cols, struct expression *call,
315 int (*callback)(void*, int, char**, char**), void *info)
317 if (is_fake_call(call))
318 return;
320 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
321 sql_select_return_states_pointer(cols, call, callback, info);
322 return;
325 if (inlinable(call->fn)) {
326 mem_sql(callback, info,
327 "select %s from return_states where call_id = '%lu' order by return_id, type;",
328 cols, (unsigned long)call);
329 return;
332 row_count = 0;
333 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
334 get_static_filter(call->fn->symbol));
335 if (row_count > 3000)
336 return;
338 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
339 cols, get_static_filter(call->fn->symbol));
342 void sql_select_call_implies(const char *cols, struct expression *call,
343 int (*callback)(void*, int, char**, char**))
345 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
346 return;
348 if (inlinable(call->fn)) {
349 mem_sql(callback, NULL,
350 "select %s from call_implies where call_id = '%lu';",
351 cols, (unsigned long)call);
352 return;
355 run_sql(callback, NULL, "select %s from call_implies where %s;",
356 cols, get_static_filter(call->fn->symbol));
359 void sql_select_caller_info(const char *cols, struct symbol *sym,
360 int (*callback)(void*, int, char**, char**))
362 if (__inline_fn) {
363 mem_sql(callback, NULL,
364 "select %s from caller_info where call_id = %lu;",
365 cols, (unsigned long)__inline_fn);
366 return;
369 run_sql(callback, NULL,
370 "select %s from caller_info where %s order by call_id;",
371 cols, get_static_filter(sym));
374 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
376 struct def_callback *def_callback = __alloc_def_callback(0);
378 def_callback->hook_type = type;
379 def_callback->callback = callback;
380 add_ptr_list(&callbacks, def_callback);
384 * These call backs are used when the --info option is turned on to print struct
385 * member information. For example foo->bar could have a state in
386 * smatch_extra.c and also check_user.c.
388 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
390 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
392 member_callback->owner = owner;
393 member_callback->callback = callback;
394 add_ptr_list(&member_callbacks, member_callback);
397 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
399 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
401 callback->callback = fn;
402 add_ptr_list(&returned_state_callbacks, callback);
405 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
407 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
409 member_callback->owner = owner;
410 member_callback->callback = callback;
411 add_ptr_list(&returned_member_callbacks, member_callback);
414 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
416 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
418 cb->type = type;
419 cb->callback = callback;
420 add_ptr_list(&call_implies_cb_list, cb);
423 static struct expression *static_call_expr;
424 static struct expression *static_returns_call;
425 static struct symbol *return_type;
426 static struct range_list *return_range_list;
427 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
429 struct range_list *rl;
430 struct expression *call_expr = static_returns_call;
432 if (argc != 1)
433 return 0;
434 call_results_to_rl(call_expr, return_type, argv[0], &rl);
435 return_range_list = rl_union(return_range_list, rl);
436 return 0;
439 struct range_list *db_return_vals(struct expression *expr)
441 char buf[64];
442 struct sm_state *sm;
444 if (is_fake_call(expr))
445 return NULL;
447 snprintf(buf, sizeof(buf), "return %p", expr);
448 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
449 if (sm)
450 return clone_rl(estate_rl(sm->state));
451 static_returns_call = expr;
452 return_type = get_type(expr);
453 if (!return_type)
454 return NULL;
456 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
457 return NULL;
459 return_range_list = NULL;
460 if (inlinable(expr->fn)) {
461 mem_sql(db_return_callback, NULL,
462 "select distinct return from return_states where call_id = '%lu';",
463 (unsigned long)expr);
464 } else {
465 run_sql(db_return_callback, NULL,
466 "select distinct return from return_states where %s;",
467 get_static_filter(expr->fn->symbol));
469 return return_range_list;
472 static void match_call_marker(struct expression *expr)
475 * we just want to record something in the database so that if we have
476 * two calls like: frob(4); frob(some_unkown); then on the receiving
477 * side we know that sometimes frob is called with unknown parameters.
480 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
483 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
484 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
486 struct symbol *type;
487 struct sm_state *sm;
488 char *name;
489 struct symbol *sym;
490 int len;
491 char printed_name[256];
492 int is_address = 0;
494 expr = strip_expr(expr);
495 if (expr->type == EXPR_PREOP && expr->op == '&') {
496 expr = strip_expr(expr->unop);
497 is_address = 1;
500 type = get_type(expr);
501 if (type && (type->type != SYM_PTR && type->type != SYM_STRUCT))
502 return;
504 name = expr_to_var_sym(expr, &sym);
505 if (!name || !sym)
506 goto free;
508 len = strlen(name);
509 FOR_EACH_SM(stree, sm) {
510 if (sm->sym != sym)
511 continue;
512 if (strcmp(name, sm->name) == 0) {
513 if (is_address)
514 snprintf(printed_name, sizeof(printed_name), "*$");
515 else /* these are already handled. fixme: handle them here */
516 continue;
517 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
518 snprintf(printed_name, sizeof(printed_name), "*$");
519 } else if (strncmp(name, sm->name, len) == 0) {
520 if (isalnum(sm->name[len]))
521 continue;
522 if (is_address)
523 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
524 else
525 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
526 } else {
527 continue;
529 callback(call, param, printed_name, sm);
530 } END_FOR_EACH_SM(sm);
531 free:
532 free_string(name);
535 static void match_call_info(struct expression *call)
537 struct member_info_callback *cb;
538 struct expression *arg;
539 struct stree *stree;
540 char *name;
541 int i;
543 name = get_fnptr_name(call->fn);
544 if (!name)
545 return;
547 FOR_EACH_PTR(member_callbacks, cb) {
548 stree = get_all_states_stree(cb->owner);
549 i = 0;
550 FOR_EACH_PTR(call->args, arg) {
551 print_struct_members(call, arg, i, stree, cb->callback);
552 i++;
553 } END_FOR_EACH_PTR(arg);
554 free_stree(&stree);
555 } END_FOR_EACH_PTR(cb);
557 free_string(name);
560 static int get_param(int param, char **name, struct symbol **sym)
562 struct symbol *arg;
563 int i;
565 i = 0;
566 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
568 * this is a temporary hack to work around a bug (I think in sparse?)
569 * 2.6.37-rc1:fs/reiserfs/journal.o
570 * If there is a function definition without parameter name found
571 * after a function implementation then it causes a crash.
572 * int foo() {}
573 * int bar(char *);
575 if (arg->ident->name < (char *)100)
576 continue;
577 if (i == param) {
578 *name = arg->ident->name;
579 *sym = arg;
580 return TRUE;
582 i++;
583 } END_FOR_EACH_PTR(arg);
585 return FALSE;
588 static struct stree *final_states;
589 static int prev_func_id = -1;
590 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
592 int func_id;
593 long type;
594 long param;
595 char *key;
596 char *value;
597 char *name = NULL;
598 struct symbol *sym = NULL;
599 struct def_callback *def_callback;
600 struct stree *stree;
602 if (argc != 5)
603 return 0;
605 func_id = atoi(argv[0]);
606 errno = 0;
607 type = strtol(argv[1], NULL, 10);
608 param = strtol(argv[2], NULL, 10);
609 if (errno)
610 return 0;
611 key = argv[3];
612 value = argv[4];
615 if (prev_func_id == -1)
616 prev_func_id = func_id;
617 if (func_id != prev_func_id) {
618 stree = __pop_fake_cur_stree();
619 merge_stree(&final_states, stree);
620 free_stree(&stree);
621 __push_fake_cur_stree();
622 __unnullify_path();
623 prev_func_id = func_id;
626 if (param >= 0 && !get_param(param, &name, &sym))
627 return 0;
629 FOR_EACH_PTR(callbacks, def_callback) {
630 if (def_callback->hook_type == type)
631 def_callback->callback(name, sym, key, value);
632 } END_FOR_EACH_PTR(def_callback);
634 return 0;
637 static void get_direct_callers(struct symbol *sym)
639 sql_select_caller_info("call_id, type, parameter, key, value", sym,
640 caller_info_callback);
643 static struct string_list *ptr_names_done;
644 static struct string_list *ptr_names;
646 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
648 insert_string(&ptr_names, alloc_string(argv[0]));
649 return 0;
652 static char *get_next_ptr_name(void)
654 char *ptr;
656 FOR_EACH_PTR(ptr_names, ptr) {
657 if (list_has_string(ptr_names_done, ptr))
658 continue;
659 insert_string(&ptr_names_done, ptr);
660 return ptr;
661 } END_FOR_EACH_PTR(ptr);
662 return NULL;
665 static void get_ptr_names(const char *file, const char *name)
667 char sql_filter[1024];
668 int before, after;
670 if (file) {
671 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
672 file, name);
673 } else {
674 snprintf(sql_filter, 1024, "function = '%s';", name);
677 before = ptr_list_size((struct ptr_list *)ptr_names);
679 run_sql(get_ptr_name, NULL,
680 "select distinct ptr from function_ptr where %s",
681 sql_filter);
683 after = ptr_list_size((struct ptr_list *)ptr_names);
684 if (before == after)
685 return;
687 while ((name = get_next_ptr_name()))
688 get_ptr_names(NULL, name);
691 static void match_data_from_db(struct symbol *sym)
693 struct sm_state *sm;
694 struct stree *stree;
696 if (!sym || !sym->ident)
697 return;
699 __push_fake_cur_stree();
700 __unnullify_path();
701 prev_func_id = -1;
703 if (!__inline_fn) {
704 char *ptr;
706 if (sym->ctype.modifiers & MOD_STATIC)
707 get_ptr_names(get_base_file(), sym->ident->name);
708 else
709 get_ptr_names(NULL, sym->ident->name);
711 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
712 __free_ptr_list((struct ptr_list **)&ptr_names);
713 __free_ptr_list((struct ptr_list **)&ptr_names_done);
714 stree = __pop_fake_cur_stree();
715 free_stree(&stree);
716 return;
719 get_direct_callers(sym);
721 FOR_EACH_PTR(ptr_names, ptr) {
722 run_sql(caller_info_callback, NULL,
723 "select call_id, type, parameter, key, value"
724 " from caller_info where function = '%s' order by call_id",
725 ptr);
726 free_string(ptr);
727 } END_FOR_EACH_PTR(ptr);
729 __free_ptr_list((struct ptr_list **)&ptr_names);
730 __free_ptr_list((struct ptr_list **)&ptr_names_done);
731 } else {
732 get_direct_callers(sym);
735 stree = __pop_fake_cur_stree();
736 merge_stree(&final_states, stree);
737 free_stree(&stree);
739 FOR_EACH_SM(final_states, sm) {
740 __set_sm(sm);
741 } END_FOR_EACH_SM(sm);
743 free_stree(&final_states);
746 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
748 struct expression *call_expr = static_call_expr;
749 struct call_implies_callback *cb;
750 struct expression *arg = NULL;
751 int type;
752 int param;
754 if (argc != 5)
755 return 0;
757 type = atoi(argv[1]);
758 param = atoi(argv[2]);
760 FOR_EACH_PTR(call_implies_cb_list, cb) {
761 if (cb->type != type)
762 continue;
763 if (param != -1) {
764 arg = get_argument_from_call_expr(call_expr->args, param);
765 if (!arg)
766 continue;
768 cb->callback(arg, argv[3], argv[4]);
769 } END_FOR_EACH_PTR(cb);
771 return 0;
774 static void match_call_implies(struct expression *expr)
776 static_call_expr = expr;
777 sql_select_call_implies("function, type, parameter, key, value", expr,
778 call_implies_callbacks);
779 return;
782 static void print_initializer_list(struct expression_list *expr_list,
783 struct symbol *struct_type)
785 struct expression *expr;
786 struct symbol *base_type;
787 char struct_name[256];
789 FOR_EACH_PTR(expr_list, expr) {
790 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
791 print_initializer_list(expr->idx_expression->expr_list, struct_type);
792 continue;
794 if (expr->type != EXPR_IDENTIFIER)
795 continue;
796 if (!expr->expr_ident)
797 continue;
798 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
799 continue;
800 base_type = get_type(expr->ident_expression);
801 if (!base_type || base_type->type != SYM_FN)
802 continue;
803 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
804 struct_type->ident->name, expr->expr_ident->name);
805 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
806 struct_name);
807 } END_FOR_EACH_PTR(expr);
810 static void global_variable(struct symbol *sym)
812 struct symbol *struct_type;
814 if (!sym->ident)
815 return;
816 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
817 return;
818 struct_type = get_base_type(sym);
819 if (!struct_type)
820 return;
821 if (struct_type->type == SYM_ARRAY) {
822 struct_type = get_base_type(struct_type);
823 if (!struct_type)
824 return;
826 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
827 return;
828 print_initializer_list(sym->initializer->expr_list, struct_type);
831 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
833 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
836 static void call_return_state_hooks_conditional(struct expression *expr)
838 struct returned_state_callback *cb;
839 struct range_list *rl;
840 char *return_ranges;
841 int final_pass_orig = final_pass;
843 __push_fake_cur_stree();
845 final_pass = 0;
846 __split_whole_condition(expr->conditional);
847 final_pass = final_pass_orig;
849 if (get_implied_rl(expr->cond_true, &rl))
850 rl = cast_rl(cur_func_return_type(), rl);
851 else
852 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
853 return_ranges = show_rl(rl);
855 return_id++;
856 FOR_EACH_PTR(returned_state_callbacks, cb) {
857 cb->callback(return_id, return_ranges, expr);
858 } END_FOR_EACH_PTR(cb);
860 __push_true_states();
861 __use_false_states();
863 if (get_implied_rl(expr->cond_false, &rl))
864 rl = cast_rl(cur_func_return_type(), rl);
865 else
866 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
867 return_ranges = show_rl(rl);
869 return_id++;
870 FOR_EACH_PTR(returned_state_callbacks, cb) {
871 cb->callback(return_id, return_ranges, expr);
872 } END_FOR_EACH_PTR(cb);
874 __merge_true_states();
875 __free_fake_cur_stree();
878 static void call_return_state_hooks_compare(struct expression *expr)
880 struct returned_state_callback *cb;
881 char *return_ranges;
882 int final_pass_orig = final_pass;
884 __push_fake_cur_stree();
886 final_pass = 0;
887 __split_whole_condition(expr);
888 final_pass = final_pass_orig;
890 return_ranges = alloc_sname("1");
892 return_id++;
893 FOR_EACH_PTR(returned_state_callbacks, cb) {
894 cb->callback(return_id, return_ranges, expr);
895 } END_FOR_EACH_PTR(cb);
897 __push_true_states();
898 __use_false_states();
900 return_ranges = alloc_sname("0");;
901 return_id++;
902 FOR_EACH_PTR(returned_state_callbacks, cb) {
903 cb->callback(return_id, return_ranges, expr);
904 } END_FOR_EACH_PTR(cb);
906 __merge_true_states();
907 __free_fake_cur_stree();
910 static int split_helper(struct sm_state *sm, struct expression *expr)
912 struct returned_state_callback *cb;
913 struct range_list *rl;
914 char *return_ranges;
915 struct sm_state *tmp;
916 int ret = 0;
917 int nr_possible, nr_states;
918 char *compare_str;
919 char buf[128];
920 struct stree *orig_stree;
921 struct sm_state *orig_sm;
923 if (!sm || !sm->merged)
924 return 0;
926 if (too_many_possible(sm))
927 return 0;
929 /* bail if it gets too complicated */
930 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
931 nr_states = stree_count(__get_cur_stree());
933 * the main thing option_info because we don't want to print a
934 * million lines of output. If someone else, like check_locking.c
935 * wants this data, then it doesn't cause a slow down to provide it.
937 if (option_info && nr_states * nr_possible >= 2000)
938 return 0;
940 compare_str = expr_lte_to_param(expr, -1);
942 orig_stree = clone_stree(__get_cur_stree());
943 FOR_EACH_PTR(sm->possible, tmp) {
944 if (tmp->merged)
945 continue;
947 ret = 1;
948 __push_fake_cur_stree();
950 FOR_EACH_SM(orig_stree, orig_sm) {
951 __set_sm(orig_sm);
952 } END_FOR_EACH_SM(orig_sm);
954 overwrite_states_using_pool(tmp);
956 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
957 return_ranges = show_rl(rl);
958 if (compare_str) {
959 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
960 return_ranges = alloc_sname(buf);
963 return_id++;
964 FOR_EACH_PTR(returned_state_callbacks, cb) {
965 cb->callback(return_id, return_ranges, expr);
966 } END_FOR_EACH_PTR(cb);
968 __free_fake_cur_stree();
969 } END_FOR_EACH_PTR(tmp);
971 free_stree(&orig_stree);
972 return ret;
975 static const char *get_return_ranges_str(struct expression *expr)
977 struct range_list *rl;
978 char *return_ranges;
979 sval_t sval;
980 char *compare_str;
981 char *math_str;
982 char buf[128];
984 if (!expr)
985 return alloc_sname("");
987 if (get_implied_value(expr, &sval))
988 return sval_to_str(sval);
990 compare_str = expr_equal_to_param(expr, -1);
991 math_str = get_value_in_terms_of_parameter_math(expr);
993 if (get_implied_rl(expr, &rl)) {
994 rl = cast_rl(cur_func_return_type(), rl);
995 return_ranges = show_rl(rl);
996 } else {
997 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
998 return_ranges = show_rl(rl);
1001 if (compare_str) {
1002 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1003 return alloc_sname(buf);
1006 if (math_str) {
1007 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1008 return alloc_sname(buf);
1011 compare_str = expr_lte_to_param(expr, -1);
1012 if (compare_str) {
1013 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1014 return alloc_sname(buf);
1016 return return_ranges;
1019 static int split_positive_from_negative(struct expression *expr)
1021 struct returned_state_callback *cb;
1022 struct range_list *rl;
1023 const char *return_ranges;
1024 int undo;
1026 /* We're going to print the states 3 times */
1027 if (stree_count(__get_cur_stree()) > 10000 / 3)
1028 return 0;
1030 if (!get_implied_rl(expr, &rl) || !rl)
1031 return 0;
1032 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1033 return 0;
1034 /* Forget about INT_MAX and larger */
1035 if (rl_max(rl).value <= 0)
1036 return 0;
1037 if (!sval_is_negative(rl_min(rl)))
1038 return 0;
1040 if (!assume(compare_expression(expr, '>', zero_expr())))
1041 return 0;
1043 return_id++;
1044 return_ranges = get_return_ranges_str(expr);
1045 FOR_EACH_PTR(returned_state_callbacks, cb) {
1046 cb->callback(return_id, (char *)return_ranges, expr);
1047 } END_FOR_EACH_PTR(cb);
1049 end_assume();
1051 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1052 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1054 return_id++;
1055 return_ranges = get_return_ranges_str(expr);
1056 FOR_EACH_PTR(returned_state_callbacks, cb) {
1057 cb->callback(return_id, (char *)return_ranges, expr);
1058 } END_FOR_EACH_PTR(cb);
1060 if (undo)
1061 end_assume();
1064 undo = assume(compare_expression(expr, '<', zero_expr()));
1066 return_id++;
1067 return_ranges = get_return_ranges_str(expr);
1068 FOR_EACH_PTR(returned_state_callbacks, cb) {
1069 cb->callback(return_id, (char *)return_ranges, expr);
1070 } END_FOR_EACH_PTR(cb);
1072 if (undo)
1073 end_assume();
1075 return 1;
1078 static int call_return_state_hooks_split_possible(struct expression *expr)
1080 struct returned_state_callback *cb;
1081 struct range_list *rl;
1082 char *return_ranges;
1083 struct sm_state *sm;
1084 struct sm_state *tmp;
1085 int ret = 0;
1086 int nr_possible, nr_states;
1087 char *compare_str;
1088 char buf[128];
1090 if (!expr || expr_equal_to_param(expr, -1))
1091 return 0;
1093 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1094 if (!sm || !sm->merged)
1095 return 0;
1097 if (too_many_possible(sm))
1098 return 0;
1100 /* bail if it gets too complicated */
1101 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1102 nr_states = stree_count(__get_cur_stree());
1104 * the main thing option_info because we don't want to print a
1105 * million lines of output. If someone else, like check_locking.c
1106 * wants this data, then it doesn't cause a slow down to provide it.
1108 if (option_info && nr_states * nr_possible >= 2000)
1109 return 0;
1112 FOR_EACH_PTR(sm->possible, tmp) {
1113 if (tmp->merged)
1114 continue;
1116 ret = 1;
1117 __push_fake_cur_stree();
1119 overwrite_states_using_pool(tmp);
1121 if (split_positive_from_negative(expr)) {
1122 __free_fake_cur_stree();
1123 continue;
1125 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1126 return_ranges = show_rl(rl);
1128 compare_str = expr_lte_to_param(expr, -1);
1129 if (compare_str) {
1130 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1131 return_ranges = alloc_sname(buf);
1134 return_id++;
1135 FOR_EACH_PTR(returned_state_callbacks, cb) {
1136 cb->callback(return_id, return_ranges, expr);
1137 } END_FOR_EACH_PTR(cb);
1139 __free_fake_cur_stree();
1140 } END_FOR_EACH_PTR(tmp);
1142 return ret;
1145 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1147 struct range_list *rl;
1148 int nr_states;
1149 struct returned_state_callback *cb;
1150 char *return_ranges;
1151 int final_pass_orig = final_pass;
1152 sval_t val;
1154 if (option_project != PROJ_KERNEL)
1155 return 0;
1157 nr_states = stree_count(__get_cur_stree());
1158 if (nr_states > 1500)
1159 return 0;
1161 if (get_value(expr, &val))
1162 return 0;
1163 if (!get_implied_rl(expr, &rl))
1164 return 0;
1165 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1166 return 0;
1167 if (rl_max(rl).value != 0)
1168 return 0;
1170 __push_fake_cur_stree();
1172 final_pass = 0;
1173 __split_whole_condition(expr);
1174 final_pass = final_pass_orig;
1176 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1178 return_id++;
1179 FOR_EACH_PTR(returned_state_callbacks, cb) {
1180 cb->callback(return_id, return_ranges, expr);
1181 } END_FOR_EACH_PTR(cb);
1183 __push_true_states();
1184 __use_false_states();
1186 return_ranges = alloc_sname("0");;
1187 return_id++;
1188 FOR_EACH_PTR(returned_state_callbacks, cb) {
1189 cb->callback(return_id, return_ranges, expr);
1190 } END_FOR_EACH_PTR(cb);
1192 __merge_true_states();
1193 __free_fake_cur_stree();
1195 return 1;
1198 static int is_boolean(struct expression *expr)
1200 struct range_list *rl;
1202 if (!get_implied_rl(expr, &rl))
1203 return 0;
1204 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1205 return 1;
1206 return 0;
1209 static int is_conditional(struct expression *expr)
1211 if (!expr)
1212 return 0;
1213 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1214 return 1;
1215 return 0;
1218 static int splitable_function_call(struct expression *expr)
1220 struct sm_state *sm;
1221 char buf[64];
1223 if (!expr || expr->type != EXPR_CALL)
1224 return 0;
1225 snprintf(buf, sizeof(buf), "return %p", expr);
1226 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1227 return split_helper(sm, expr);
1230 static void call_return_state_hooks(struct expression *expr)
1232 struct returned_state_callback *cb;
1233 const char *return_ranges;
1234 int nr_states;
1235 sval_t sval;
1237 expr = strip_expr(expr);
1239 if (is_impossible_path())
1240 goto vanilla;
1242 if (!get_implied_value(expr, &sval) &&
1243 (is_condition(expr) || is_boolean(expr))) {
1244 call_return_state_hooks_compare(expr);
1245 return;
1246 } else if (is_conditional(expr)) {
1247 call_return_state_hooks_conditional(expr);
1248 return;
1249 } else if (call_return_state_hooks_split_possible(expr)) {
1250 return;
1251 } else if (call_return_state_hooks_split_success_fail(expr)) {
1252 return;
1253 } else if (splitable_function_call(expr)) {
1254 return;
1255 } else if (split_positive_from_negative(expr)) {
1256 return;
1259 vanilla:
1260 return_ranges = get_return_ranges_str(expr);
1262 return_id++;
1263 nr_states = stree_count(__get_cur_stree());
1264 if (nr_states >= 10000) {
1265 match_return_info(return_id, (char *)return_ranges, expr);
1266 return;
1268 FOR_EACH_PTR(returned_state_callbacks, cb) {
1269 cb->callback(return_id, (char *)return_ranges, expr);
1270 } END_FOR_EACH_PTR(cb);
1273 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1275 struct returned_member_callback *cb;
1276 struct stree *stree;
1277 struct sm_state *sm;
1278 struct symbol *type;
1279 char *name;
1280 char member_name[256];
1281 int len;
1283 type = get_type(expr);
1284 if (!type || type->type != SYM_PTR)
1285 return;
1286 name = expr_to_var(expr);
1287 if (!name)
1288 return;
1290 member_name[sizeof(member_name) - 1] = '\0';
1291 strcpy(member_name, "$");
1293 len = strlen(name);
1294 FOR_EACH_PTR(returned_member_callbacks, cb) {
1295 stree = __get_cur_stree();
1296 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1297 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1298 strcpy(member_name, "*$");
1299 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1300 continue;
1302 if (strncmp(sm->name, name, len) != 0)
1303 continue;
1304 if (strncmp(sm->name + len, "->", 2) != 0)
1305 continue;
1306 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1307 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1308 } END_FOR_EACH_SM(sm);
1309 } END_FOR_EACH_PTR(cb);
1311 free_string(name);
1314 static void reset_memdb(void)
1316 mem_sql(NULL, NULL, "delete from caller_info;");
1317 mem_sql(NULL, NULL, "delete from return_states;");
1318 mem_sql(NULL, NULL, "delete from call_implies;");
1321 static void match_end_func_info(struct symbol *sym)
1323 if (__path_is_null())
1324 return;
1325 call_return_state_hooks(NULL);
1326 if (!__inline_fn)
1327 reset_memdb();
1330 static void init_memdb(void)
1332 char *err = NULL;
1333 int rc;
1334 const char *schema_files[] = {
1335 "db/db.schema",
1336 "db/caller_info.schema",
1337 "db/return_states.schema",
1338 "db/function_type_size.schema",
1339 "db/type_size.schema",
1340 "db/call_implies.schema",
1341 "db/function_ptr.schema",
1342 "db/local_values.schema",
1343 "db/function_type_value.schema",
1344 "db/type_value.schema",
1345 "db/function_type_info.schema",
1346 "db/data_info.schema",
1348 static char buf[4096];
1349 int fd;
1350 int ret;
1351 int i;
1353 rc = sqlite3_open(":memory:", &mem_db);
1354 if (rc != SQLITE_OK) {
1355 printf("Error starting In-Memory database.");
1356 return;
1359 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1360 fd = open_data_file(schema_files[i]);
1361 if (fd < 0) {
1362 mem_db = NULL;
1363 return;
1365 ret = read(fd, buf, sizeof(buf));
1366 if (ret == sizeof(buf)) {
1367 printf("Schema file too large: %s (limit %zd bytes)",
1368 schema_files[i], sizeof(buf));
1370 buf[ret] = '\0';
1371 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1372 if (rc != SQLITE_OK) {
1373 fprintf(stderr, "SQL error #2: %s\n", err);
1374 fprintf(stderr, "%s\n", buf);
1379 void open_smatch_db(void)
1381 int rc;
1383 if (option_no_db)
1384 return;
1386 init_memdb();
1388 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1389 if (rc != SQLITE_OK) {
1390 option_no_db = 1;
1391 return;
1393 return;
1396 static void register_common_funcs(void)
1398 struct token *token;
1399 char *func;
1400 char filename[256];
1402 if (option_project == PROJ_NONE)
1403 strcpy(filename, "common_functions");
1404 else
1405 snprintf(filename, 256, "%s.common_functions", option_project_str);
1407 token = get_tokens_file(filename);
1408 if (!token)
1409 return;
1410 if (token_type(token) != TOKEN_STREAMBEGIN)
1411 return;
1412 token = token->next;
1413 while (token_type(token) != TOKEN_STREAMEND) {
1414 if (token_type(token) != TOKEN_IDENT)
1415 return;
1416 func = alloc_string(show_ident(token->ident));
1417 add_ptr_list(&common_funcs, func);
1418 token = token->next;
1420 clear_token_alloc();
1424 void register_definition_db_callbacks(int id)
1426 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1427 add_hook(&global_variable, BASE_HOOK);
1428 add_hook(&global_variable, DECLARATION_HOOK);
1429 add_split_return_callback(match_return_info);
1430 add_split_return_callback(print_returned_struct_members);
1431 add_hook(&call_return_state_hooks, RETURN_HOOK);
1432 add_hook(&match_end_func_info, END_FUNC_HOOK);
1434 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1435 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1437 register_common_funcs();
1440 void register_db_call_marker(int id)
1442 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1445 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1447 struct expression *arg;
1448 char *name = NULL;
1449 char member_name[256];
1451 *sym = NULL;
1453 if (param == -1) {
1454 const char *star = "";
1456 if (expr->type != EXPR_ASSIGNMENT)
1457 return NULL;
1458 name = expr_to_var_sym(expr->left, sym);
1459 if (!name)
1460 return NULL;
1461 if (key[0] == '*') {
1462 star = "*";
1463 key++;
1465 if (strncmp(key, "$", 1) != 0)
1466 return name;
1467 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1468 free_string(name);
1469 return alloc_string(member_name);
1472 while (expr->type == EXPR_ASSIGNMENT)
1473 expr = strip_expr(expr->right);
1474 if (expr->type != EXPR_CALL)
1475 return NULL;
1477 arg = get_argument_from_call_expr(expr->args, param);
1478 if (!arg)
1479 return NULL;
1481 return get_variable_from_key(arg, key, sym);
1484 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1486 char buf[256];
1487 char *tmp;
1489 if (!arg)
1490 return NULL;
1492 arg = strip_expr(arg);
1494 if (strcmp(key, "$") == 0)
1495 return expr_to_var_sym(arg, sym);
1497 if (strcmp(key, "*$") == 0) {
1498 if (arg->type == EXPR_PREOP && arg->op == '&') {
1499 arg = strip_expr(arg->unop);
1500 return expr_to_var_sym(arg, sym);
1501 } else {
1502 tmp = expr_to_var_sym(arg, sym);
1503 if (!tmp)
1504 return NULL;
1505 snprintf(buf, sizeof(buf), "*%s", tmp);
1506 free_string(tmp);
1507 return alloc_string(buf);
1511 if (arg->type == EXPR_PREOP && arg->op == '&') {
1512 arg = strip_expr(arg->unop);
1513 tmp = expr_to_var_sym(arg, sym);
1514 if (!tmp)
1515 return NULL;
1516 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1517 return alloc_string(buf);
1520 tmp = expr_to_var_sym(arg, sym);
1521 if (!tmp)
1522 return NULL;
1523 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1524 free_string(tmp);
1525 return alloc_string(buf);
1528 const char *get_param_name(struct sm_state *sm)
1530 char *param_name;
1531 int name_len;
1532 static char buf[256];
1534 if (!sm->sym || !sm->sym->ident)
1535 return NULL;
1537 param_name = sm->sym->ident->name;
1538 name_len = strlen(param_name);
1540 if (strcmp(sm->name, param_name) == 0) {
1541 return "$";
1542 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1543 strncmp(sm->name, param_name, name_len) == 0) {
1544 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1545 return buf;
1546 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1547 return "*$";
1549 return NULL;
1552 char *get_data_info_name(struct expression *expr)
1554 struct symbol *sym;
1555 char *name;
1556 char buf[256];
1557 char *ret = NULL;
1559 expr = strip_expr(expr);
1560 name = get_member_name(expr);
1561 if (name)
1562 return name;
1563 name = expr_to_var_sym(expr, &sym);
1564 if (!name || !sym)
1565 goto free;
1566 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1567 goto free;
1568 if (sym->ctype.modifiers & MOD_STATIC)
1569 snprintf(buf, sizeof(buf), "static %s", name);
1570 else
1571 snprintf(buf, sizeof(buf), "global %s", name);
1572 ret = alloc_sname(buf);
1573 free:
1574 free_string(name);
1575 return ret;