implied: introduce assume(condition)
[smatch.git] / smatch_db.c
blobe8749a0d2723634e6069ebcf8f9908ae6cf05c92
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 int call_return_state_hooks_split_possible(struct expression *expr)
969 struct returned_state_callback *cb;
970 struct range_list *rl;
971 char *return_ranges;
972 struct sm_state *sm;
973 struct sm_state *tmp;
974 int ret = 0;
975 int nr_possible, nr_states;
976 char *compare_str;
977 char buf[128];
979 if (!expr || expr_equal_to_param(expr, -1))
980 return 0;
982 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
983 if (!sm || !sm->merged)
984 return 0;
986 if (too_many_possible(sm))
987 return 0;
989 /* bail if it gets too complicated */
990 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
991 nr_states = stree_count(__get_cur_stree());
993 * the main thing option_info because we don't want to print a
994 * million lines of output. If someone else, like check_locking.c
995 * wants this data, then it doesn't cause a slow down to provide it.
997 if (option_info && nr_states * nr_possible >= 2000)
998 return 0;
1001 FOR_EACH_PTR(sm->possible, tmp) {
1002 if (tmp->merged)
1003 continue;
1005 ret = 1;
1006 __push_fake_cur_stree();
1008 overwrite_states_using_pool(tmp);
1010 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1011 return_ranges = show_rl(rl);
1013 compare_str = expr_lte_to_param(expr, -1);
1014 if (compare_str) {
1015 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1016 return_ranges = alloc_sname(buf);
1019 return_id++;
1020 FOR_EACH_PTR(returned_state_callbacks, cb) {
1021 cb->callback(return_id, return_ranges, expr);
1022 } END_FOR_EACH_PTR(cb);
1024 __free_fake_cur_stree();
1025 } END_FOR_EACH_PTR(tmp);
1027 return ret;
1030 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1032 struct range_list *rl;
1033 int nr_states;
1034 struct returned_state_callback *cb;
1035 char *return_ranges;
1036 int final_pass_orig = final_pass;
1037 sval_t val;
1039 if (option_project != PROJ_KERNEL)
1040 return 0;
1042 nr_states = stree_count(__get_cur_stree());
1043 if (nr_states > 1500)
1044 return 0;
1046 if (get_value(expr, &val))
1047 return 0;
1048 if (!get_implied_rl(expr, &rl))
1049 return 0;
1050 if (rl_min(rl).value < -4095)
1051 return 0;
1052 if (rl_max(rl).value != 0)
1053 return 0;
1055 __push_fake_cur_stree();
1057 final_pass = 0;
1058 __split_whole_condition(expr);
1059 final_pass = final_pass_orig;
1061 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1063 return_id++;
1064 FOR_EACH_PTR(returned_state_callbacks, cb) {
1065 cb->callback(return_id, return_ranges, expr);
1066 } END_FOR_EACH_PTR(cb);
1068 __push_true_states();
1069 __use_false_states();
1071 return_ranges = alloc_sname("0");;
1072 return_id++;
1073 FOR_EACH_PTR(returned_state_callbacks, cb) {
1074 cb->callback(return_id, return_ranges, expr);
1075 } END_FOR_EACH_PTR(cb);
1077 __merge_true_states();
1078 __free_fake_cur_stree();
1080 return 1;
1083 static const char *get_return_ranges_str(struct expression *expr)
1085 struct range_list *rl;
1086 char *return_ranges;
1087 sval_t sval;
1088 char *compare_str;
1089 char *math_str;
1090 char buf[128];
1092 if (!expr)
1093 return alloc_sname("");
1095 if (get_implied_value(expr, &sval))
1096 return sval_to_str(sval);
1098 compare_str = expr_equal_to_param(expr, -1);
1099 math_str = get_value_in_terms_of_parameter_math(expr);
1101 if (get_implied_rl(expr, &rl)) {
1102 rl = cast_rl(cur_func_return_type(), rl);
1103 return_ranges = show_rl(rl);
1104 } else {
1105 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1106 return_ranges = show_rl(rl);
1109 if (compare_str) {
1110 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1111 return alloc_sname(buf);
1114 if (math_str) {
1115 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1116 return alloc_sname(buf);
1119 compare_str = expr_lte_to_param(expr, -1);
1120 if (compare_str) {
1121 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1122 return alloc_sname(buf);
1124 return return_ranges;
1127 static int is_boolean(struct expression *expr)
1129 struct range_list *rl;
1131 if (!get_implied_rl(expr, &rl))
1132 return 0;
1133 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1134 return 1;
1135 return 0;
1138 static int is_conditional(struct expression *expr)
1140 if (!expr)
1141 return 0;
1142 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1143 return 1;
1144 return 0;
1147 static int splitable_function_call(struct expression *expr)
1149 struct sm_state *sm;
1150 char buf[64];
1152 if (!expr || expr->type != EXPR_CALL)
1153 return 0;
1154 snprintf(buf, sizeof(buf), "return %p", expr);
1155 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1156 return split_helper(sm, expr);
1159 static void call_return_state_hooks(struct expression *expr)
1161 struct returned_state_callback *cb;
1162 const char *return_ranges;
1163 int nr_states;
1164 sval_t sval;
1166 expr = strip_expr(expr);
1168 if (!get_implied_value(expr, &sval) &&
1169 (is_condition(expr) || is_boolean(expr))) {
1170 call_return_state_hooks_compare(expr);
1171 return;
1172 } else if (is_conditional(expr)) {
1173 call_return_state_hooks_conditional(expr);
1174 return;
1175 } else if (call_return_state_hooks_split_possible(expr)) {
1176 return;
1177 } else if (call_return_state_hooks_split_success_fail(expr)) {
1178 return;
1179 } else if (splitable_function_call(expr)) {
1180 return;
1183 return_ranges = get_return_ranges_str(expr);
1185 return_id++;
1186 nr_states = stree_count(__get_cur_stree());
1187 if (nr_states >= 10000) {
1188 match_return_info(return_id, (char *)return_ranges, expr);
1189 return;
1191 FOR_EACH_PTR(returned_state_callbacks, cb) {
1192 cb->callback(return_id, (char *)return_ranges, expr);
1193 } END_FOR_EACH_PTR(cb);
1196 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1198 struct returned_member_callback *cb;
1199 struct stree *stree;
1200 struct sm_state *sm;
1201 struct symbol *type;
1202 char *name;
1203 char member_name[256];
1204 int len;
1206 type = get_type(expr);
1207 if (!type || type->type != SYM_PTR)
1208 return;
1209 name = expr_to_var(expr);
1210 if (!name)
1211 return;
1213 member_name[sizeof(member_name) - 1] = '\0';
1214 strcpy(member_name, "$");
1216 len = strlen(name);
1217 FOR_EACH_PTR(returned_member_callbacks, cb) {
1218 stree = __get_cur_stree();
1219 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1220 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1221 strcpy(member_name, "*$");
1222 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1223 continue;
1225 if (strncmp(sm->name, name, len) != 0)
1226 continue;
1227 if (strncmp(sm->name + len, "->", 2) != 0)
1228 continue;
1229 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1230 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1231 } END_FOR_EACH_SM(sm);
1232 } END_FOR_EACH_PTR(cb);
1234 free_string(name);
1237 static void reset_memdb(void)
1239 mem_sql(NULL, NULL, "delete from caller_info;");
1240 mem_sql(NULL, NULL, "delete from return_states;");
1241 mem_sql(NULL, NULL, "delete from call_implies;");
1244 static void match_end_func_info(struct symbol *sym)
1246 if (__path_is_null())
1247 return;
1248 call_return_state_hooks(NULL);
1249 if (!__inline_fn)
1250 reset_memdb();
1253 static void init_memdb(void)
1255 char *err = NULL;
1256 int rc;
1257 const char *schema_files[] = {
1258 "db/db.schema",
1259 "db/caller_info.schema",
1260 "db/return_states.schema",
1261 "db/function_type_size.schema",
1262 "db/type_size.schema",
1263 "db/call_implies.schema",
1264 "db/function_ptr.schema",
1265 "db/local_values.schema",
1266 "db/function_type_value.schema",
1267 "db/type_value.schema",
1268 "db/function_type_info.schema",
1269 "db/data_info.schema",
1271 static char buf[4096];
1272 int fd;
1273 int ret;
1274 int i;
1276 rc = sqlite3_open(":memory:", &mem_db);
1277 if (rc != SQLITE_OK) {
1278 printf("Error starting In-Memory database.");
1279 return;
1282 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1283 fd = open_data_file(schema_files[i]);
1284 if (fd < 0) {
1285 mem_db = NULL;
1286 return;
1288 ret = read(fd, buf, sizeof(buf));
1289 if (ret == sizeof(buf)) {
1290 printf("Schema file too large: %s (limit %zd bytes)",
1291 schema_files[i], sizeof(buf));
1293 buf[ret] = '\0';
1294 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1295 if (rc != SQLITE_OK) {
1296 fprintf(stderr, "SQL error #2: %s\n", err);
1297 fprintf(stderr, "%s\n", buf);
1302 void open_smatch_db(void)
1304 int rc;
1306 if (option_no_db)
1307 return;
1309 init_memdb();
1311 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1312 if (rc != SQLITE_OK) {
1313 option_no_db = 1;
1314 return;
1316 return;
1319 static void register_common_funcs(void)
1321 struct token *token;
1322 char *func;
1323 char filename[256];
1325 if (option_project == PROJ_NONE)
1326 strcpy(filename, "common_functions");
1327 else
1328 snprintf(filename, 256, "%s.common_functions", option_project_str);
1330 token = get_tokens_file(filename);
1331 if (!token)
1332 return;
1333 if (token_type(token) != TOKEN_STREAMBEGIN)
1334 return;
1335 token = token->next;
1336 while (token_type(token) != TOKEN_STREAMEND) {
1337 if (token_type(token) != TOKEN_IDENT)
1338 return;
1339 func = alloc_string(show_ident(token->ident));
1340 add_ptr_list(&common_funcs, func);
1341 token = token->next;
1343 clear_token_alloc();
1347 void register_definition_db_callbacks(int id)
1349 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1350 add_hook(&global_variable, BASE_HOOK);
1351 add_hook(&global_variable, DECLARATION_HOOK);
1352 add_split_return_callback(match_return_info);
1353 add_split_return_callback(print_returned_struct_members);
1354 add_hook(&call_return_state_hooks, RETURN_HOOK);
1355 add_hook(&match_end_func_info, END_FUNC_HOOK);
1357 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1358 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1360 register_common_funcs();
1363 void register_db_call_marker(int id)
1365 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1368 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1370 struct expression *arg;
1371 char *name = NULL;
1372 char member_name[256];
1374 *sym = NULL;
1376 if (param == -1) {
1377 const char *star = "";
1379 if (expr->type != EXPR_ASSIGNMENT)
1380 return NULL;
1381 name = expr_to_var_sym(expr->left, sym);
1382 if (!name)
1383 return NULL;
1384 if (key[0] == '*') {
1385 star = "*";
1386 key++;
1388 if (strncmp(key, "$", 1) != 0)
1389 return name;
1390 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1391 free_string(name);
1392 return alloc_string(member_name);
1395 while (expr->type == EXPR_ASSIGNMENT)
1396 expr = strip_expr(expr->right);
1397 if (expr->type != EXPR_CALL)
1398 return NULL;
1400 arg = get_argument_from_call_expr(expr->args, param);
1401 if (!arg)
1402 return NULL;
1404 return get_variable_from_key(arg, key, sym);
1407 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1409 char buf[256];
1410 char *tmp;
1412 if (!arg)
1413 return NULL;
1415 arg = strip_expr(arg);
1417 if (strcmp(key, "$") == 0)
1418 return expr_to_var_sym(arg, sym);
1420 if (strcmp(key, "*$") == 0) {
1421 if (arg->type == EXPR_PREOP && arg->op == '&') {
1422 arg = strip_expr(arg->unop);
1423 return expr_to_var_sym(arg, sym);
1424 } else {
1425 tmp = expr_to_var_sym(arg, sym);
1426 if (!tmp)
1427 return NULL;
1428 snprintf(buf, sizeof(buf), "*%s", tmp);
1429 free_string(tmp);
1430 return alloc_string(buf);
1434 if (arg->type == EXPR_PREOP && arg->op == '&') {
1435 arg = strip_expr(arg->unop);
1436 tmp = expr_to_var_sym(arg, sym);
1437 if (!tmp)
1438 return NULL;
1439 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1440 return alloc_string(buf);
1443 tmp = expr_to_var_sym(arg, sym);
1444 if (!tmp)
1445 return NULL;
1446 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1447 free_string(tmp);
1448 return alloc_string(buf);
1451 const char *get_param_name(struct sm_state *sm)
1453 char *param_name;
1454 int name_len;
1455 static char buf[256];
1457 if (!sm->sym || !sm->sym->ident)
1458 return NULL;
1460 param_name = sm->sym->ident->name;
1461 name_len = strlen(param_name);
1463 if (strcmp(sm->name, param_name) == 0) {
1464 return "$";
1465 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1466 strncmp(sm->name, param_name, name_len) == 0) {
1467 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1468 return buf;
1469 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1470 return "*$";
1472 return NULL;
1475 char *get_data_info_name(struct expression *expr)
1477 struct symbol *sym;
1478 char *name;
1479 char buf[256];
1480 char *ret = NULL;
1482 expr = strip_expr(expr);
1483 name = get_member_name(expr);
1484 if (name)
1485 return name;
1486 name = expr_to_var_sym(expr, &sym);
1487 if (!name || !sym)
1488 goto free;
1489 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1490 goto free;
1491 if (sym->ctype.modifiers & MOD_STATIC)
1492 snprintf(buf, sizeof(buf), "static %s", name);
1493 else
1494 snprintf(buf, sizeof(buf), "global %s", name);
1495 ret = alloc_sname(buf);
1496 free:
1497 free_string(name);
1498 return ret;