db/fixup_kernel.sh: ignore a lot of PCI errors
[smatch.git] / smatch_db.c
blob6d5419feaacf394c6a22e486af0a3c74748dd943
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 "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static sqlite3 *db;
27 static sqlite3 *mem_db;
29 static int return_id;
31 #define sql_insert(table, values...) \
32 do { \
33 if (!mem_db) \
34 break; \
35 if (__inline_fn) { \
36 char buf[1024]; \
37 char *err, *p = buf; \
38 int rc; \
40 p += snprintf(p, buf + sizeof(buf) - p, \
41 "insert into %s values (", #table); \
42 p += snprintf(p, buf + sizeof(buf) - p, values); \
43 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
44 sm_debug("in-mem: %s\n", buf); \
45 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
46 if (rc != SQLITE_OK) { \
47 fprintf(stderr, "SQL error #2: %s\n", err); \
48 fprintf(stderr, "SQL: '%s'\n", buf); \
49 } \
50 break; \
51 } \
52 if (option_info) { \
53 sm_prefix(); \
54 sm_printf("SQL: insert into " #table " values (" values); \
55 sm_printf(");\n"); \
56 } \
57 } while (0)
59 struct def_callback {
60 int hook_type;
61 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
63 ALLOCATOR(def_callback, "definition db hook callbacks");
64 DECLARE_PTR_LIST(callback_list, struct def_callback);
65 static struct callback_list *callbacks;
67 struct member_info_callback {
68 int owner;
69 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
71 ALLOCATOR(member_info_callback, "caller_info callbacks");
72 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
73 static struct member_info_cb_list *member_callbacks;
75 struct returned_state_callback {
76 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
78 ALLOCATOR(returned_state_callback, "returned state callbacks");
79 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
80 static struct returned_state_cb_list *returned_state_callbacks;
82 struct returned_member_callback {
83 int owner;
84 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
86 ALLOCATOR(returned_member_callback, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
88 static struct returned_member_cb_list *returned_member_callbacks;
90 struct call_implies_callback {
91 int type;
92 void (*callback)(struct expression *arg, char *key, char *value);
94 ALLOCATOR(call_implies_callback, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
96 static struct call_implies_cb_list *call_implies_cb_list;
98 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
100 int i;
102 for (i = 0; i < argc; i++) {
103 if (i != 0)
104 printf(", ");
105 sm_printf("%s", argv[i]);
107 sm_printf("\n");
108 return 0;
111 void debug_sql(const char *sql)
113 if (!option_debug)
114 return;
115 sm_msg("%s", sql);
116 sql_exec(print_sql_output, NULL, sql);
119 void debug_mem_sql(const char *sql)
121 if (!option_debug)
122 return;
123 sm_msg("%s", sql);
124 sql_mem_exec(print_sql_output, NULL, sql);
127 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
129 char *err = NULL;
130 int rc;
132 if (option_no_db || !db)
133 return;
135 rc = sqlite3_exec(db, sql, callback, data, &err);
136 if (rc != SQLITE_OK) {
137 fprintf(stderr, "SQL error #2: %s\n", err);
138 fprintf(stderr, "SQL: '%s'\n", sql);
142 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
144 char *err = NULL;
145 int rc;
147 if (!mem_db)
148 return;
150 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
151 if (rc != SQLITE_OK) {
152 fprintf(stderr, "SQL error #2: %s\n", err);
153 fprintf(stderr, "SQL: '%s'\n", sql);
157 void sql_insert_return_states(int return_id, const char *return_ranges,
158 int type, int param, const char *key, const char *value)
160 if (key && strlen(key) >= 80)
161 return;
162 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
163 get_base_file(), get_function(), (unsigned long)__inline_fn,
164 return_id, return_ranges, fn_static(), type, param, key, value);
167 static struct string_list *common_funcs;
168 static int is_common_function(const char *fn)
170 char *tmp;
172 if (strncmp(fn, "__builtin_", 10) == 0)
173 return 1;
175 FOR_EACH_PTR(common_funcs, tmp) {
176 if (strcmp(tmp, fn) == 0)
177 return 1;
178 } END_FOR_EACH_PTR(tmp);
180 return 0;
183 void sql_insert_caller_info(struct expression *call, int type,
184 int param, const char *key, const char *value)
186 char *fn;
188 if (!option_info && !__inline_call)
189 return;
191 if (key && strlen(key) >= 80)
192 return;
194 fn = get_fnptr_name(call->fn);
195 if (!fn)
196 return;
198 if (__inline_call) {
199 mem_sql(NULL, NULL,
200 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
201 get_base_file(), get_function(), fn, (unsigned long)call,
202 is_static(call->fn), type, param, key, value);
205 if (!option_info)
206 return;
208 if (is_common_function(fn))
209 return;
211 sm_msg("SQL_caller_info: insert into caller_info values ("
212 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
213 get_base_file(), get_function(), fn, is_static(call->fn),
214 type, param, key, value);
216 free_string(fn);
219 void sql_insert_function_ptr(const char *fn, const char *struct_name)
221 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
222 struct_name);
225 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
227 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
228 get_function(), (unsigned long)__inline_fn, fn_static(),
229 type, param, key, value);
232 void sql_insert_function_type_size(const char *member, const char *ranges)
234 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
237 void sql_insert_local_values(const char *name, const char *value)
239 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
242 void sql_insert_function_type_value(const char *type, const char *value)
244 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
247 void sql_insert_function_type_info(int param, const char *value)
249 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
250 get_base_file(), get_function(), fn_static(), param, value);
253 void sql_insert_data_info(struct expression *data, int type, const char *value)
255 char *data_name;
257 data_name = get_data_info_name(data);
258 if (!data_name)
259 return;
260 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
263 char *get_static_filter(struct symbol *sym)
265 static char sql_filter[1024];
267 if (sym->ctype.modifiers & MOD_STATIC) {
268 snprintf(sql_filter, sizeof(sql_filter),
269 "file = '%s' and function = '%s' and static = '1'",
270 get_base_file(), sym->ident->name);
271 } else {
272 snprintf(sql_filter, sizeof(sql_filter),
273 "function = '%s' and static = '0'", sym->ident->name);
276 return sql_filter;
279 static int row_count;
280 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
282 if (argc != 1)
283 return 0;
284 row_count = atoi(argv[0]);
285 return 0;
288 static void sql_select_return_states_pointer(const char *cols,
289 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
291 char *ptr;
293 ptr = get_fnptr_name(call->fn);
294 if (!ptr)
295 return;
297 run_sql(callback, info,
298 "select %s from return_states join function_ptr where "
299 "return_states.function == function_ptr.function and ptr = '%s'"
300 "and searchable = 1 order by return_id, type;",
301 cols, ptr);
304 static int is_local_symbol(struct expression *expr)
306 if (expr->type != EXPR_SYMBOL)
307 return 0;
308 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
309 return 0;
310 return 1;
313 void sql_select_return_states(const char *cols, struct expression *call,
314 int (*callback)(void*, int, char**, char**), void *info)
316 if (is_fake_call(call))
317 return;
319 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
320 sql_select_return_states_pointer(cols, call, callback, info);
321 return;
324 if (inlinable(call->fn)) {
325 mem_sql(callback, info,
326 "select %s from return_states where call_id = '%lu' order by return_id, type;",
327 cols, (unsigned long)call);
328 return;
331 row_count = 0;
332 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
333 get_static_filter(call->fn->symbol));
334 if (row_count > 3000)
335 return;
337 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
338 cols, get_static_filter(call->fn->symbol));
341 void sql_select_call_implies(const char *cols, struct expression *call,
342 int (*callback)(void*, int, char**, char**))
344 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
345 return;
347 if (inlinable(call->fn)) {
348 mem_sql(callback, NULL,
349 "select %s from call_implies where call_id = '%lu';",
350 cols, (unsigned long)call);
351 return;
354 run_sql(callback, NULL, "select %s from call_implies where %s;",
355 cols, get_static_filter(call->fn->symbol));
358 void sql_select_caller_info(const char *cols, struct symbol *sym,
359 int (*callback)(void*, int, char**, char**))
361 if (__inline_fn) {
362 mem_sql(callback, NULL,
363 "select %s from caller_info where call_id = %lu;",
364 cols, (unsigned long)__inline_fn);
365 return;
368 run_sql(callback, NULL,
369 "select %s from caller_info where %s order by call_id;",
370 cols, get_static_filter(sym));
373 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
375 struct def_callback *def_callback = __alloc_def_callback(0);
377 def_callback->hook_type = type;
378 def_callback->callback = callback;
379 add_ptr_list(&callbacks, def_callback);
383 * These call backs are used when the --info option is turned on to print struct
384 * member information. For example foo->bar could have a state in
385 * smatch_extra.c and also check_user.c.
387 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
389 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
391 member_callback->owner = owner;
392 member_callback->callback = callback;
393 add_ptr_list(&member_callbacks, member_callback);
396 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
398 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
400 callback->callback = fn;
401 add_ptr_list(&returned_state_callbacks, callback);
404 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))
406 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
408 member_callback->owner = owner;
409 member_callback->callback = callback;
410 add_ptr_list(&returned_member_callbacks, member_callback);
413 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
415 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
417 cb->type = type;
418 cb->callback = callback;
419 add_ptr_list(&call_implies_cb_list, cb);
422 static struct expression *static_call_expr;
423 static struct expression *static_returns_call;
424 static struct symbol *return_type;
425 static struct range_list *return_range_list;
426 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
428 struct range_list *rl;
429 struct expression *call_expr = static_returns_call;
431 if (argc != 1)
432 return 0;
433 call_results_to_rl(call_expr, return_type, argv[0], &rl);
434 return_range_list = rl_union(return_range_list, rl);
435 return 0;
438 struct range_list *db_return_vals(struct expression *expr)
440 char buf[64];
441 struct sm_state *sm;
443 if (is_fake_call(expr))
444 return NULL;
446 snprintf(buf, sizeof(buf), "return %p", expr);
447 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
448 if (sm)
449 return clone_rl(estate_rl(sm->state));
450 static_returns_call = expr;
451 return_type = get_type(expr);
452 if (!return_type)
453 return NULL;
455 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
456 return NULL;
458 return_range_list = NULL;
459 if (inlinable(expr->fn)) {
460 mem_sql(db_return_callback, NULL,
461 "select distinct return from return_states where call_id = '%lu';",
462 (unsigned long)expr);
463 } else {
464 run_sql(db_return_callback, NULL,
465 "select distinct return from return_states where %s;",
466 get_static_filter(expr->fn->symbol));
468 return return_range_list;
471 static void match_call_marker(struct expression *expr)
474 * we just want to record something in the database so that if we have
475 * two calls like: frob(4); frob(some_unkown); then on the receiving
476 * side we know that sometimes frob is called with unknown parameters.
479 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
482 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
483 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
485 struct sm_state *sm;
486 char *name;
487 struct symbol *sym;
488 int len;
489 char printed_name[256];
490 int is_address = 0;
492 expr = strip_expr(expr);
493 if (expr->type == EXPR_PREOP && expr->op == '&') {
494 expr = strip_expr(expr->unop);
495 is_address = 1;
498 name = expr_to_var_sym(expr, &sym);
499 if (!name || !sym)
500 goto free;
502 len = strlen(name);
503 FOR_EACH_SM(stree, sm) {
504 if (sm->sym != sym)
505 continue;
506 if (strcmp(name, sm->name) == 0) {
507 if (is_address)
508 snprintf(printed_name, sizeof(printed_name), "*$");
509 else /* these are already handled. fixme: handle them here */
510 continue;
511 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
512 snprintf(printed_name, sizeof(printed_name), "*$");
513 } else if (strncmp(name, sm->name, len) == 0) {
514 if (is_address)
515 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
516 else
517 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
518 } else {
519 continue;
521 callback(call, param, printed_name, sm);
522 } END_FOR_EACH_SM(sm);
523 free:
524 free_string(name);
527 static void match_call_info(struct expression *call)
529 struct member_info_callback *cb;
530 struct expression *arg;
531 struct stree *stree;
532 char *name;
533 int i;
535 name = get_fnptr_name(call->fn);
536 if (!name)
537 return;
539 FOR_EACH_PTR(member_callbacks, cb) {
540 stree = get_all_states_stree(cb->owner);
541 i = 0;
542 FOR_EACH_PTR(call->args, arg) {
543 print_struct_members(call, arg, i, stree, cb->callback);
544 i++;
545 } END_FOR_EACH_PTR(arg);
546 free_stree(&stree);
547 } END_FOR_EACH_PTR(cb);
549 free_string(name);
552 static int get_param(int param, char **name, struct symbol **sym)
554 struct symbol *arg;
555 int i;
557 i = 0;
558 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
560 * this is a temporary hack to work around a bug (I think in sparse?)
561 * 2.6.37-rc1:fs/reiserfs/journal.o
562 * If there is a function definition without parameter name found
563 * after a function implementation then it causes a crash.
564 * int foo() {}
565 * int bar(char *);
567 if (arg->ident->name < (char *)100)
568 continue;
569 if (i == param) {
570 *name = arg->ident->name;
571 *sym = arg;
572 return TRUE;
574 i++;
575 } END_FOR_EACH_PTR(arg);
577 return FALSE;
580 static struct stree *final_states;
581 static int prev_func_id = -1;
582 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
584 int func_id;
585 long type;
586 long param;
587 char *key;
588 char *value;
589 char *name = NULL;
590 struct symbol *sym = NULL;
591 struct def_callback *def_callback;
592 struct stree *stree;
594 if (argc != 5)
595 return 0;
597 func_id = atoi(argv[0]);
598 errno = 0;
599 type = strtol(argv[1], NULL, 10);
600 param = strtol(argv[2], NULL, 10);
601 if (errno)
602 return 0;
603 key = argv[3];
604 value = argv[4];
607 if (prev_func_id == -1)
608 prev_func_id = func_id;
609 if (func_id != prev_func_id) {
610 stree = __pop_fake_cur_stree();
611 merge_stree(&final_states, stree);
612 free_stree(&stree);
613 __push_fake_cur_stree();
614 __unnullify_path();
615 prev_func_id = func_id;
618 if (param >= 0 && !get_param(param, &name, &sym))
619 return 0;
621 FOR_EACH_PTR(callbacks, def_callback) {
622 if (def_callback->hook_type == type)
623 def_callback->callback(name, sym, key, value);
624 } END_FOR_EACH_PTR(def_callback);
626 return 0;
629 static void get_direct_callers(struct symbol *sym)
631 sql_select_caller_info("call_id, type, parameter, key, value", sym,
632 caller_info_callback);
635 static struct string_list *ptr_names_done;
636 static struct string_list *ptr_names;
638 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
640 insert_string(&ptr_names, alloc_string(argv[0]));
641 return 0;
644 static char *get_next_ptr_name(void)
646 char *ptr;
648 FOR_EACH_PTR(ptr_names, ptr) {
649 if (list_has_string(ptr_names_done, ptr))
650 continue;
651 insert_string(&ptr_names_done, ptr);
652 return ptr;
653 } END_FOR_EACH_PTR(ptr);
654 return NULL;
657 static void get_ptr_names(const char *file, const char *name)
659 char sql_filter[1024];
660 int before, after;
662 if (file) {
663 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
664 file, name);
665 } else {
666 snprintf(sql_filter, 1024, "function = '%s';", name);
669 before = ptr_list_size((struct ptr_list *)ptr_names);
671 run_sql(get_ptr_name, NULL,
672 "select distinct ptr from function_ptr where %s",
673 sql_filter);
675 after = ptr_list_size((struct ptr_list *)ptr_names);
676 if (before == after)
677 return;
679 while ((name = get_next_ptr_name()))
680 get_ptr_names(NULL, name);
683 static void match_data_from_db(struct symbol *sym)
685 struct sm_state *sm;
686 struct stree *stree;
688 if (!sym || !sym->ident)
689 return;
691 __push_fake_cur_stree();
692 __unnullify_path();
693 prev_func_id = -1;
695 if (!__inline_fn) {
696 char *ptr;
698 if (sym->ctype.modifiers & MOD_STATIC)
699 get_ptr_names(get_base_file(), sym->ident->name);
700 else
701 get_ptr_names(NULL, sym->ident->name);
703 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
704 __free_ptr_list((struct ptr_list **)&ptr_names);
705 __free_ptr_list((struct ptr_list **)&ptr_names_done);
706 stree = __pop_fake_cur_stree();
707 free_stree(&stree);
708 return;
711 get_direct_callers(sym);
713 FOR_EACH_PTR(ptr_names, ptr) {
714 run_sql(caller_info_callback, NULL,
715 "select call_id, type, parameter, key, value"
716 " from caller_info where function = '%s' order by call_id",
717 ptr);
718 free_string(ptr);
719 } END_FOR_EACH_PTR(ptr);
721 __free_ptr_list((struct ptr_list **)&ptr_names);
722 __free_ptr_list((struct ptr_list **)&ptr_names_done);
723 } else {
724 get_direct_callers(sym);
727 stree = __pop_fake_cur_stree();
728 merge_stree(&final_states, stree);
729 free_stree(&stree);
731 FOR_EACH_SM(final_states, sm) {
732 __set_sm(sm);
733 } END_FOR_EACH_SM(sm);
735 free_stree(&final_states);
738 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
740 struct expression *call_expr = static_call_expr;
741 struct call_implies_callback *cb;
742 struct expression *arg = NULL;
743 int type;
744 int param;
746 if (argc != 5)
747 return 0;
749 type = atoi(argv[1]);
750 param = atoi(argv[2]);
752 FOR_EACH_PTR(call_implies_cb_list, cb) {
753 if (cb->type != type)
754 continue;
755 if (param != -1) {
756 arg = get_argument_from_call_expr(call_expr->args, param);
757 if (!arg)
758 continue;
760 cb->callback(arg, argv[3], argv[4]);
761 } END_FOR_EACH_PTR(cb);
763 return 0;
766 static void match_call_implies(struct expression *expr)
768 static_call_expr = expr;
769 sql_select_call_implies("function, type, parameter, key, value", expr,
770 call_implies_callbacks);
771 return;
774 static void print_initializer_list(struct expression_list *expr_list,
775 struct symbol *struct_type)
777 struct expression *expr;
778 struct symbol *base_type;
779 char struct_name[256];
781 FOR_EACH_PTR(expr_list, expr) {
782 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
783 print_initializer_list(expr->idx_expression->expr_list, struct_type);
784 continue;
786 if (expr->type != EXPR_IDENTIFIER)
787 continue;
788 if (!expr->expr_ident)
789 continue;
790 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
791 continue;
792 base_type = get_type(expr->ident_expression);
793 if (!base_type || base_type->type != SYM_FN)
794 continue;
795 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
796 struct_type->ident->name, expr->expr_ident->name);
797 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
798 struct_name);
799 } END_FOR_EACH_PTR(expr);
802 static void global_variable(struct symbol *sym)
804 struct symbol *struct_type;
806 if (!sym->ident)
807 return;
808 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
809 return;
810 struct_type = get_base_type(sym);
811 if (!struct_type)
812 return;
813 if (struct_type->type == SYM_ARRAY) {
814 struct_type = get_base_type(struct_type);
815 if (!struct_type)
816 return;
818 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
819 return;
820 print_initializer_list(sym->initializer->expr_list, struct_type);
823 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
825 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
828 static void call_return_state_hooks_conditional(struct expression *expr)
830 struct returned_state_callback *cb;
831 struct range_list *rl;
832 char *return_ranges;
833 int final_pass_orig = final_pass;
835 __push_fake_cur_stree();
837 final_pass = 0;
838 __split_whole_condition(expr->conditional);
839 final_pass = final_pass_orig;
841 if (get_implied_rl(expr->cond_true, &rl))
842 rl = cast_rl(cur_func_return_type(), rl);
843 else
844 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
845 return_ranges = show_rl(rl);
847 return_id++;
848 FOR_EACH_PTR(returned_state_callbacks, cb) {
849 cb->callback(return_id, return_ranges, expr);
850 } END_FOR_EACH_PTR(cb);
852 __push_true_states();
853 __use_false_states();
855 if (get_implied_rl(expr->cond_false, &rl))
856 rl = cast_rl(cur_func_return_type(), rl);
857 else
858 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
859 return_ranges = show_rl(rl);
861 return_id++;
862 FOR_EACH_PTR(returned_state_callbacks, cb) {
863 cb->callback(return_id, return_ranges, expr);
864 } END_FOR_EACH_PTR(cb);
866 __merge_true_states();
867 __free_fake_cur_stree();
870 static void call_return_state_hooks_compare(struct expression *expr)
872 struct returned_state_callback *cb;
873 char *return_ranges;
874 int final_pass_orig = final_pass;
876 __push_fake_cur_stree();
878 final_pass = 0;
879 __split_whole_condition(expr);
880 final_pass = final_pass_orig;
882 return_ranges = alloc_sname("1");
884 return_id++;
885 FOR_EACH_PTR(returned_state_callbacks, cb) {
886 cb->callback(return_id, return_ranges, expr);
887 } END_FOR_EACH_PTR(cb);
889 __push_true_states();
890 __use_false_states();
892 return_ranges = alloc_sname("0");;
893 return_id++;
894 FOR_EACH_PTR(returned_state_callbacks, cb) {
895 cb->callback(return_id, return_ranges, expr);
896 } END_FOR_EACH_PTR(cb);
898 __merge_true_states();
899 __free_fake_cur_stree();
902 static int split_helper(struct sm_state *sm, struct expression *expr)
904 struct returned_state_callback *cb;
905 struct range_list *rl;
906 char *return_ranges;
907 struct sm_state *tmp;
908 int ret = 0;
909 int nr_possible, nr_states;
910 char *compare_str;
911 char buf[128];
912 struct stree *orig_stree;
913 struct sm_state *orig_sm;
915 if (!sm || !sm->merged)
916 return 0;
918 if (too_many_possible(sm))
919 return 0;
921 /* bail if it gets too complicated */
922 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
923 nr_states = stree_count(__get_cur_stree());
925 * the main thing option_info because we don't want to print a
926 * million lines of output. If someone else, like check_locking.c
927 * wants this data, then it doesn't cause a slow down to provide it.
929 if (option_info && nr_states * nr_possible >= 2000)
930 return 0;
932 compare_str = expr_lte_to_param(expr, -1);
934 orig_stree = clone_stree(__get_cur_stree());
935 FOR_EACH_PTR(sm->possible, tmp) {
936 if (tmp->merged)
937 continue;
939 ret = 1;
940 __push_fake_cur_stree();
942 FOR_EACH_SM(orig_stree, orig_sm) {
943 __set_sm(orig_sm);
944 } END_FOR_EACH_SM(orig_sm);
946 overwrite_states_using_pool(tmp);
948 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
949 return_ranges = show_rl(rl);
950 if (compare_str) {
951 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
952 return_ranges = alloc_sname(buf);
955 return_id++;
956 FOR_EACH_PTR(returned_state_callbacks, cb) {
957 cb->callback(return_id, return_ranges, expr);
958 } END_FOR_EACH_PTR(cb);
960 __free_fake_cur_stree();
961 } END_FOR_EACH_PTR(tmp);
963 free_stree(&orig_stree);
964 return ret;
967 static const char *get_return_ranges_str(struct expression *expr)
969 struct range_list *rl;
970 char *return_ranges;
971 sval_t sval;
972 char *compare_str;
973 char *math_str;
974 char buf[128];
976 if (!expr)
977 return alloc_sname("");
979 if (get_implied_value(expr, &sval))
980 return sval_to_str(sval);
982 compare_str = expr_equal_to_param(expr, -1);
983 math_str = get_value_in_terms_of_parameter_math(expr);
985 if (get_implied_rl(expr, &rl)) {
986 rl = cast_rl(cur_func_return_type(), rl);
987 return_ranges = show_rl(rl);
988 } else {
989 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
990 return_ranges = show_rl(rl);
993 if (compare_str) {
994 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
995 return alloc_sname(buf);
998 if (math_str) {
999 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1000 return alloc_sname(buf);
1003 compare_str = expr_lte_to_param(expr, -1);
1004 if (compare_str) {
1005 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1006 return alloc_sname(buf);
1008 return return_ranges;
1011 static int split_positive_from_negative(struct expression *expr)
1013 struct returned_state_callback *cb;
1014 struct range_list *rl;
1015 const char *return_ranges;
1016 int undo;
1018 /* We're going to print the states 3 times */
1019 if (stree_count(__get_cur_stree()) > 10000 / 3)
1020 return 0;
1022 if (!get_implied_rl(expr, &rl) || !rl)
1023 return 0;
1024 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1025 return 0;
1026 /* Forget about INT_MAX and larger */
1027 if (rl_max(rl).value <= 0)
1028 return 0;
1029 if (!sval_is_negative(rl_min(rl)))
1030 return 0;
1032 if (!assume(compare_expression(expr, '>', zero_expr())))
1033 return 0;
1035 return_id++;
1036 return_ranges = get_return_ranges_str(expr);
1037 FOR_EACH_PTR(returned_state_callbacks, cb) {
1038 cb->callback(return_id, (char *)return_ranges, expr);
1039 } END_FOR_EACH_PTR(cb);
1041 end_assume();
1043 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1044 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1046 return_id++;
1047 return_ranges = get_return_ranges_str(expr);
1048 FOR_EACH_PTR(returned_state_callbacks, cb) {
1049 cb->callback(return_id, (char *)return_ranges, expr);
1050 } END_FOR_EACH_PTR(cb);
1052 if (undo)
1053 end_assume();
1056 undo = assume(compare_expression(expr, '<', zero_expr()));
1058 return_id++;
1059 return_ranges = get_return_ranges_str(expr);
1060 FOR_EACH_PTR(returned_state_callbacks, cb) {
1061 cb->callback(return_id, (char *)return_ranges, expr);
1062 } END_FOR_EACH_PTR(cb);
1064 if (undo)
1065 end_assume();
1067 return 1;
1070 static int call_return_state_hooks_split_possible(struct expression *expr)
1072 struct returned_state_callback *cb;
1073 struct range_list *rl;
1074 char *return_ranges;
1075 struct sm_state *sm;
1076 struct sm_state *tmp;
1077 int ret = 0;
1078 int nr_possible, nr_states;
1079 char *compare_str;
1080 char buf[128];
1082 if (!expr || expr_equal_to_param(expr, -1))
1083 return 0;
1085 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1086 if (!sm || !sm->merged)
1087 return 0;
1089 if (too_many_possible(sm))
1090 return 0;
1092 /* bail if it gets too complicated */
1093 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1094 nr_states = stree_count(__get_cur_stree());
1096 * the main thing option_info because we don't want to print a
1097 * million lines of output. If someone else, like check_locking.c
1098 * wants this data, then it doesn't cause a slow down to provide it.
1100 if (option_info && nr_states * nr_possible >= 2000)
1101 return 0;
1104 FOR_EACH_PTR(sm->possible, tmp) {
1105 if (tmp->merged)
1106 continue;
1108 ret = 1;
1109 __push_fake_cur_stree();
1111 overwrite_states_using_pool(tmp);
1113 if (split_positive_from_negative(expr)) {
1114 __free_fake_cur_stree();
1115 continue;
1117 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1118 return_ranges = show_rl(rl);
1120 compare_str = expr_lte_to_param(expr, -1);
1121 if (compare_str) {
1122 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1123 return_ranges = alloc_sname(buf);
1126 return_id++;
1127 FOR_EACH_PTR(returned_state_callbacks, cb) {
1128 cb->callback(return_id, return_ranges, expr);
1129 } END_FOR_EACH_PTR(cb);
1131 __free_fake_cur_stree();
1132 } END_FOR_EACH_PTR(tmp);
1134 return ret;
1137 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1139 struct range_list *rl;
1140 int nr_states;
1141 struct returned_state_callback *cb;
1142 char *return_ranges;
1143 int final_pass_orig = final_pass;
1144 sval_t val;
1146 if (option_project != PROJ_KERNEL)
1147 return 0;
1149 nr_states = stree_count(__get_cur_stree());
1150 if (nr_states > 1500)
1151 return 0;
1153 if (get_value(expr, &val))
1154 return 0;
1155 if (!get_implied_rl(expr, &rl))
1156 return 0;
1157 if (rl_min(rl).value < -4095)
1158 return 0;
1159 if (rl_max(rl).value != 0)
1160 return 0;
1162 __push_fake_cur_stree();
1164 final_pass = 0;
1165 __split_whole_condition(expr);
1166 final_pass = final_pass_orig;
1168 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1170 return_id++;
1171 FOR_EACH_PTR(returned_state_callbacks, cb) {
1172 cb->callback(return_id, return_ranges, expr);
1173 } END_FOR_EACH_PTR(cb);
1175 __push_true_states();
1176 __use_false_states();
1178 return_ranges = alloc_sname("0");;
1179 return_id++;
1180 FOR_EACH_PTR(returned_state_callbacks, cb) {
1181 cb->callback(return_id, return_ranges, expr);
1182 } END_FOR_EACH_PTR(cb);
1184 __merge_true_states();
1185 __free_fake_cur_stree();
1187 return 1;
1190 static int is_boolean(struct expression *expr)
1192 struct range_list *rl;
1194 if (!get_implied_rl(expr, &rl))
1195 return 0;
1196 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1197 return 1;
1198 return 0;
1201 static int is_conditional(struct expression *expr)
1203 if (!expr)
1204 return 0;
1205 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1206 return 1;
1207 return 0;
1210 static int splitable_function_call(struct expression *expr)
1212 struct sm_state *sm;
1213 char buf[64];
1215 if (!expr || expr->type != EXPR_CALL)
1216 return 0;
1217 snprintf(buf, sizeof(buf), "return %p", expr);
1218 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1219 return split_helper(sm, expr);
1222 static void call_return_state_hooks(struct expression *expr)
1224 struct returned_state_callback *cb;
1225 const char *return_ranges;
1226 int nr_states;
1227 sval_t sval;
1229 expr = strip_expr(expr);
1231 if (!get_implied_value(expr, &sval) &&
1232 (is_condition(expr) || is_boolean(expr))) {
1233 call_return_state_hooks_compare(expr);
1234 return;
1235 } else if (is_conditional(expr)) {
1236 call_return_state_hooks_conditional(expr);
1237 return;
1238 } else if (call_return_state_hooks_split_possible(expr)) {
1239 return;
1240 } else if (call_return_state_hooks_split_success_fail(expr)) {
1241 return;
1242 } else if (splitable_function_call(expr)) {
1243 return;
1244 } else if (split_positive_from_negative(expr)) {
1245 return;
1248 return_ranges = get_return_ranges_str(expr);
1250 return_id++;
1251 nr_states = stree_count(__get_cur_stree());
1252 if (nr_states >= 10000) {
1253 match_return_info(return_id, (char *)return_ranges, expr);
1254 return;
1256 FOR_EACH_PTR(returned_state_callbacks, cb) {
1257 cb->callback(return_id, (char *)return_ranges, expr);
1258 } END_FOR_EACH_PTR(cb);
1261 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1263 struct returned_member_callback *cb;
1264 struct stree *stree;
1265 struct sm_state *sm;
1266 struct symbol *type;
1267 char *name;
1268 char member_name[256];
1269 int len;
1271 type = get_type(expr);
1272 if (!type || type->type != SYM_PTR)
1273 return;
1274 name = expr_to_var(expr);
1275 if (!name)
1276 return;
1278 member_name[sizeof(member_name) - 1] = '\0';
1279 strcpy(member_name, "$");
1281 len = strlen(name);
1282 FOR_EACH_PTR(returned_member_callbacks, cb) {
1283 stree = __get_cur_stree();
1284 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1285 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1286 strcpy(member_name, "*$");
1287 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1288 continue;
1290 if (strncmp(sm->name, name, len) != 0)
1291 continue;
1292 if (strncmp(sm->name + len, "->", 2) != 0)
1293 continue;
1294 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1295 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1296 } END_FOR_EACH_SM(sm);
1297 } END_FOR_EACH_PTR(cb);
1299 free_string(name);
1302 static void reset_memdb(void)
1304 mem_sql(NULL, NULL, "delete from caller_info;");
1305 mem_sql(NULL, NULL, "delete from return_states;");
1306 mem_sql(NULL, NULL, "delete from call_implies;");
1309 static void match_end_func_info(struct symbol *sym)
1311 if (__path_is_null())
1312 return;
1313 call_return_state_hooks(NULL);
1314 if (!__inline_fn)
1315 reset_memdb();
1318 static void init_memdb(void)
1320 char *err = NULL;
1321 int rc;
1322 const char *schema_files[] = {
1323 "db/db.schema",
1324 "db/caller_info.schema",
1325 "db/return_states.schema",
1326 "db/function_type_size.schema",
1327 "db/type_size.schema",
1328 "db/call_implies.schema",
1329 "db/function_ptr.schema",
1330 "db/local_values.schema",
1331 "db/function_type_value.schema",
1332 "db/type_value.schema",
1333 "db/function_type_info.schema",
1334 "db/data_info.schema",
1336 static char buf[4096];
1337 int fd;
1338 int ret;
1339 int i;
1341 rc = sqlite3_open(":memory:", &mem_db);
1342 if (rc != SQLITE_OK) {
1343 printf("Error starting In-Memory database.");
1344 return;
1347 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1348 fd = open_data_file(schema_files[i]);
1349 if (fd < 0) {
1350 mem_db = NULL;
1351 return;
1353 ret = read(fd, buf, sizeof(buf));
1354 if (ret == sizeof(buf)) {
1355 printf("Schema file too large: %s (limit %zd bytes)",
1356 schema_files[i], sizeof(buf));
1358 buf[ret] = '\0';
1359 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1360 if (rc != SQLITE_OK) {
1361 fprintf(stderr, "SQL error #2: %s\n", err);
1362 fprintf(stderr, "%s\n", buf);
1367 void open_smatch_db(void)
1369 int rc;
1371 if (option_no_db)
1372 return;
1374 init_memdb();
1376 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1377 if (rc != SQLITE_OK) {
1378 option_no_db = 1;
1379 return;
1381 return;
1384 static void register_common_funcs(void)
1386 struct token *token;
1387 char *func;
1388 char filename[256];
1390 if (option_project == PROJ_NONE)
1391 strcpy(filename, "common_functions");
1392 else
1393 snprintf(filename, 256, "%s.common_functions", option_project_str);
1395 token = get_tokens_file(filename);
1396 if (!token)
1397 return;
1398 if (token_type(token) != TOKEN_STREAMBEGIN)
1399 return;
1400 token = token->next;
1401 while (token_type(token) != TOKEN_STREAMEND) {
1402 if (token_type(token) != TOKEN_IDENT)
1403 return;
1404 func = alloc_string(show_ident(token->ident));
1405 add_ptr_list(&common_funcs, func);
1406 token = token->next;
1408 clear_token_alloc();
1412 void register_definition_db_callbacks(int id)
1414 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1415 add_hook(&global_variable, BASE_HOOK);
1416 add_hook(&global_variable, DECLARATION_HOOK);
1417 add_split_return_callback(match_return_info);
1418 add_split_return_callback(print_returned_struct_members);
1419 add_hook(&call_return_state_hooks, RETURN_HOOK);
1420 add_hook(&match_end_func_info, END_FUNC_HOOK);
1422 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1423 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1425 register_common_funcs();
1428 void register_db_call_marker(int id)
1430 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1433 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1435 struct expression *arg;
1436 char *name = NULL;
1437 char member_name[256];
1439 *sym = NULL;
1441 if (param == -1) {
1442 const char *star = "";
1444 if (expr->type != EXPR_ASSIGNMENT)
1445 return NULL;
1446 name = expr_to_var_sym(expr->left, sym);
1447 if (!name)
1448 return NULL;
1449 if (key[0] == '*') {
1450 star = "*";
1451 key++;
1453 if (strncmp(key, "$", 1) != 0)
1454 return name;
1455 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1456 free_string(name);
1457 return alloc_string(member_name);
1460 while (expr->type == EXPR_ASSIGNMENT)
1461 expr = strip_expr(expr->right);
1462 if (expr->type != EXPR_CALL)
1463 return NULL;
1465 arg = get_argument_from_call_expr(expr->args, param);
1466 if (!arg)
1467 return NULL;
1469 return get_variable_from_key(arg, key, sym);
1472 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1474 char buf[256];
1475 char *tmp;
1477 if (!arg)
1478 return NULL;
1480 arg = strip_expr(arg);
1482 if (strcmp(key, "$") == 0)
1483 return expr_to_var_sym(arg, sym);
1485 if (strcmp(key, "*$") == 0) {
1486 if (arg->type == EXPR_PREOP && arg->op == '&') {
1487 arg = strip_expr(arg->unop);
1488 return expr_to_var_sym(arg, sym);
1489 } else {
1490 tmp = expr_to_var_sym(arg, sym);
1491 if (!tmp)
1492 return NULL;
1493 snprintf(buf, sizeof(buf), "*%s", tmp);
1494 free_string(tmp);
1495 return alloc_string(buf);
1499 if (arg->type == EXPR_PREOP && arg->op == '&') {
1500 arg = strip_expr(arg->unop);
1501 tmp = expr_to_var_sym(arg, sym);
1502 if (!tmp)
1503 return NULL;
1504 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1505 return alloc_string(buf);
1508 tmp = expr_to_var_sym(arg, sym);
1509 if (!tmp)
1510 return NULL;
1511 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1512 free_string(tmp);
1513 return alloc_string(buf);
1516 const char *get_param_name(struct sm_state *sm)
1518 char *param_name;
1519 int name_len;
1520 static char buf[256];
1522 if (!sm->sym || !sm->sym->ident)
1523 return NULL;
1525 param_name = sm->sym->ident->name;
1526 name_len = strlen(param_name);
1528 if (strcmp(sm->name, param_name) == 0) {
1529 return "$";
1530 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1531 strncmp(sm->name, param_name, name_len) == 0) {
1532 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1533 return buf;
1534 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1535 return "*$";
1537 return NULL;
1540 char *get_data_info_name(struct expression *expr)
1542 struct symbol *sym;
1543 char *name;
1544 char buf[256];
1545 char *ret = NULL;
1547 expr = strip_expr(expr);
1548 name = get_member_name(expr);
1549 if (name)
1550 return name;
1551 name = expr_to_var_sym(expr, &sym);
1552 if (!name || !sym)
1553 goto free;
1554 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1555 goto free;
1556 if (sym->ctype.modifiers & MOD_STATIC)
1557 snprintf(buf, sizeof(buf), "static %s", name);
1558 else
1559 snprintf(buf, sizeof(buf), "global %s", name);
1560 ret = alloc_sname(buf);
1561 free:
1562 free_string(name);
1563 return ret;