debug: introduce __smatch_debug_check() to debug a specific check
[smatch.git] / smatch_db.c
blob5c27a132a39d216d54de1a96cea485f15572841c
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 smatch_state *state);
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, 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 *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, sql);
120 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
122 char *err = NULL;
123 int rc;
125 if (option_no_db || !db)
126 return;
128 rc = sqlite3_exec(db, sql, callback, 0, &err);
129 if (rc != SQLITE_OK) {
130 fprintf(stderr, "SQL error #2: %s\n", err);
131 fprintf(stderr, "SQL: '%s'\n", sql);
135 void sql_mem_exec(int (*callback)(void*, int, char**, char**), const char *sql)
137 char *err = NULL;
138 int rc;
140 if (!mem_db)
141 return;
143 rc = sqlite3_exec(mem_db, sql, callback, 0, &err);
144 if (rc != SQLITE_OK) {
145 fprintf(stderr, "SQL error #2: %s\n", err);
146 fprintf(stderr, "SQL: '%s'\n", sql);
150 void sql_insert_return_states(int return_id, const char *return_ranges,
151 int type, int param, const char *key, const char *value)
153 if (key && strlen(key) >= 80)
154 return;
155 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
156 get_base_file(), get_function(), (unsigned long)__inline_fn,
157 return_id, return_ranges, fn_static(), type, param, key, value);
160 static struct string_list *common_funcs;
161 static int is_common_function(const char *fn)
163 char *tmp;
165 if (strncmp(fn, "__builtin_", 10) == 0)
166 return 1;
168 FOR_EACH_PTR(common_funcs, tmp) {
169 if (strcmp(tmp, fn) == 0)
170 return 1;
171 } END_FOR_EACH_PTR(tmp);
173 return 0;
176 void sql_insert_caller_info(struct expression *call, int type,
177 int param, const char *key, const char *value)
179 char *fn;
181 if (!option_info && !__inline_call)
182 return;
184 if (key && strlen(key) >= 80)
185 return;
187 fn = get_fnptr_name(call->fn);
188 if (!fn)
189 return;
191 if (__inline_call) {
192 mem_sql(NULL,
193 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
194 get_base_file(), get_function(), fn, (unsigned long)call,
195 is_static(call->fn), type, param, key, value);
198 if (!option_info)
199 return;
201 if (is_common_function(fn))
202 return;
204 sm_msg("SQL_caller_info: insert into caller_info values ("
205 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
206 get_base_file(), get_function(), fn, is_static(call->fn),
207 type, param, key, value);
209 free_string(fn);
212 void sql_insert_function_ptr(const char *fn, const char *struct_name)
214 sql_insert(function_ptr, "'%s', '%s', '%s'", get_base_file(), fn,
215 struct_name);
218 void sql_insert_call_implies(int type, int param, int value)
220 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
221 get_function(), (unsigned long)__inline_fn, fn_static(),
222 type, param, value);
225 void sql_insert_function_type_size(const char *member, const char *ranges)
227 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
230 void sql_insert_local_values(const char *name, const char *value)
232 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
235 void sql_insert_function_type_value(const char *type, const char *value)
237 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
240 static char *get_static_filter(struct symbol *sym)
242 static char sql_filter[1024];
244 if (sym->ctype.modifiers & MOD_STATIC) {
245 snprintf(sql_filter, sizeof(sql_filter),
246 "file = '%s' and function = '%s' and static = '1'",
247 get_base_file(), sym->ident->name);
248 } else {
249 snprintf(sql_filter, sizeof(sql_filter),
250 "function = '%s' and static = '0'", sym->ident->name);
253 return sql_filter;
256 static int row_count;
257 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
259 if (argc != 1)
260 return 0;
261 row_count = atoi(argv[0]);
262 return 0;
265 static void sql_select_return_states_pointer(const char *cols,
266 struct expression *call, int (*callback)(void*, int, char**, char**))
268 char *ptr;
270 ptr = get_fnptr_name(call);
271 if (!ptr)
272 return;
274 row_count = 0;
275 run_sql(get_row_count,
276 "select count(*) from return_states join function_ptr where "
277 "return_states.function == function_ptr.function and ptr = '%s';",
278 ptr);
279 if (row_count > 1000)
280 return;
282 run_sql(callback,
283 "select %s from return_states join function_ptr where "
284 "return_states.function == function_ptr.function and ptr = '%s' "
285 "order by return_id, type;",
286 cols, ptr);
289 void sql_select_return_states(const char *cols, struct expression *call,
290 int (*callback)(void*, int, char**, char**))
292 if (is_fake_call(call))
293 return;
295 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
296 sql_select_return_states_pointer(cols, call, callback);
297 return;
300 if (inlinable(call->fn)) {
301 mem_sql(callback,
302 "select %s from return_states where call_id = '%lu' order by return_id, type;",
303 cols, (unsigned long)call);
304 return;
307 row_count = 0;
308 run_sql(get_row_count, "select count(*) from return_states where %s;",
309 get_static_filter(call->fn->symbol));
310 if (row_count > 1000)
311 return;
313 run_sql(callback, "select %s from return_states where %s order by return_id, type;",
314 cols, get_static_filter(call->fn->symbol));
317 void sql_select_call_implies(const char *cols, struct expression *call,
318 int (*callback)(void*, int, char**, char**))
320 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
321 return;
323 if (inlinable(call->fn)) {
324 mem_sql(callback,
325 "select %s from call_implies where call_id = '%lu';",
326 cols, (unsigned long)call);
327 return;
330 run_sql(callback, "select %s from call_implies where %s;",
331 cols, get_static_filter(call->fn->symbol));
334 void sql_select_caller_info(const char *cols, struct symbol *sym,
335 int (*callback)(void*, int, char**, char**))
337 if (__inline_fn) {
338 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
339 cols, (unsigned long)__inline_fn);
340 return;
343 run_sql(callback,
344 "select %s from caller_info where %s order by call_id;",
345 cols, get_static_filter(sym));
348 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
350 struct def_callback *def_callback = __alloc_def_callback(0);
352 def_callback->hook_type = type;
353 def_callback->callback = callback;
354 add_ptr_list(&callbacks, def_callback);
358 * These call backs are used when the --info option is turned on to print struct
359 * member information. For example foo->bar could have a state in
360 * smatch_extra.c and also check_user.c.
362 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
364 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
366 member_callback->owner = owner;
367 member_callback->callback = callback;
368 add_ptr_list(&member_callbacks, member_callback);
371 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
373 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
375 callback->callback = fn;
376 add_ptr_list(&returned_state_callbacks, callback);
379 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
381 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
383 member_callback->owner = owner;
384 member_callback->callback = callback;
385 add_ptr_list(&returned_member_callbacks, member_callback);
388 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *value))
390 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
392 cb->type = type;
393 cb->callback = callback;
394 add_ptr_list(&call_implies_cb_list, cb);
397 static struct expression *static_call_expr;
398 static struct expression *static_returns_call;
399 static struct symbol *return_type;
400 static struct range_list *return_range_list;
401 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
403 struct range_list *rl;
404 struct expression *call_expr = static_returns_call;
406 if (argc != 1)
407 return 0;
408 call_results_to_rl(call_expr, return_type, argv[0], &rl);
409 return_range_list = rl_union(return_range_list, rl);
410 return 0;
413 struct range_list *db_return_vals(struct expression *expr)
415 static_returns_call = expr;
416 return_type = get_type(expr);
417 if (!return_type)
418 return NULL;
419 if (is_fake_call(expr))
420 return NULL;
421 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
422 return NULL;
424 return_range_list = NULL;
425 if (inlinable(expr->fn)) {
426 mem_sql(db_return_callback,
427 "select distinct return from return_states where call_id = '%lu';",
428 (unsigned long)expr);
429 } else {
430 run_sql(db_return_callback,
431 "select distinct return from return_states where %s;",
432 get_static_filter(expr->fn->symbol));
434 return return_range_list;
437 static void match_call_marker(struct expression *expr)
440 * we just want to record something in the database so that if we have
441 * two calls like: frob(4); frob(some_unkown); then on the receiving
442 * side we know that sometimes frob is called with unknown parameters.
445 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
448 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
449 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
451 struct sm_state *sm;
452 char *name;
453 struct symbol *sym;
454 int len;
455 char printed_name[256];
456 int is_address = 0;
458 expr = strip_expr(expr);
459 if (expr->type == EXPR_PREOP && expr->op == '&') {
460 expr = strip_expr(expr->unop);
461 is_address = 1;
464 name = expr_to_var_sym(expr, &sym);
465 if (!name || !sym)
466 goto free;
468 len = strlen(name);
469 FOR_EACH_SM(stree, sm) {
470 if (sm->sym != sym)
471 continue;
472 if (strcmp(name, sm->name) == 0) {
473 if (is_address)
474 snprintf(printed_name, sizeof(printed_name), "*$$");
475 else /* these are already handled. fixme: handle them here */
476 continue;
477 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
478 snprintf(printed_name, sizeof(printed_name), "*$$");
479 } else if (strncmp(name, sm->name, len) == 0) {
480 if (is_address)
481 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
482 else
483 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
484 } else {
485 continue;
487 callback(call, param, printed_name, sm->state);
488 } END_FOR_EACH_SM(sm);
489 free:
490 free_string(name);
493 static void match_call_info(struct expression *call)
495 struct member_info_callback *cb;
496 struct expression *arg;
497 struct stree *stree;
498 char *name;
499 int i;
501 name = get_fnptr_name(call->fn);
502 if (!name)
503 return;
505 FOR_EACH_PTR(member_callbacks, cb) {
506 stree = get_all_states_stree(cb->owner);
507 i = 0;
508 FOR_EACH_PTR(call->args, arg) {
509 print_struct_members(call, arg, i, stree, cb->callback);
510 i++;
511 } END_FOR_EACH_PTR(arg);
512 free_stree(&stree);
513 } END_FOR_EACH_PTR(cb);
515 free_string(name);
518 static int get_param(int param, char **name, struct symbol **sym)
520 struct symbol *arg;
521 int i;
523 i = 0;
524 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
526 * this is a temporary hack to work around a bug (I think in sparse?)
527 * 2.6.37-rc1:fs/reiserfs/journal.o
528 * If there is a function definition without parameter name found
529 * after a function implementation then it causes a crash.
530 * int foo() {}
531 * int bar(char *);
533 if (arg->ident->name < (char *)100)
534 continue;
535 if (i == param && arg->ident->name) {
536 *name = arg->ident->name;
537 *sym = arg;
538 return TRUE;
540 i++;
541 } END_FOR_EACH_PTR(arg);
543 return FALSE;
546 static struct stree *final_states;
547 static int prev_func_id = -1;
548 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
550 int func_id;
551 long type;
552 long param;
553 char *name = NULL;
554 struct symbol *sym = NULL;
555 struct def_callback *def_callback;
556 struct stree *stree;
558 if (argc != 5)
559 return 0;
561 func_id = atoi(argv[0]);
562 errno = 0;
563 type = strtol(argv[1], NULL, 10);
564 param = strtol(argv[2], NULL, 10);
565 if (errno)
566 return 0;
568 if (prev_func_id == -1)
569 prev_func_id = func_id;
570 if (func_id != prev_func_id) {
571 stree = __pop_fake_cur_stree();
572 merge_stree(&final_states, stree);
573 free_stree(&stree);
574 __push_fake_cur_stree();
575 __unnullify_path();
576 prev_func_id = func_id;
579 if (type == INTERNAL)
580 return 0;
581 if (param >= 0 && !get_param(param, &name, &sym))
582 return 0;
584 FOR_EACH_PTR(callbacks, def_callback) {
585 if (def_callback->hook_type == type)
586 def_callback->callback(name, sym, argv[3], argv[4]);
587 } END_FOR_EACH_PTR(def_callback);
589 return 0;
592 static void get_direct_callers(struct symbol *sym)
594 sql_select_caller_info("call_id, type, parameter, key, value", sym,
595 caller_info_callback);
598 static struct string_list *ptr_names_done;
599 static struct string_list *ptr_names;
601 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
603 insert_string(&ptr_names, alloc_string(argv[0]));
604 return 0;
607 static char *get_next_ptr_name(void)
609 char *ptr;
611 FOR_EACH_PTR(ptr_names, ptr) {
612 if (list_has_string(ptr_names_done, ptr))
613 continue;
614 insert_string(&ptr_names_done, ptr);
615 return ptr;
616 } END_FOR_EACH_PTR(ptr);
617 return NULL;
620 static void get_ptr_names(const char *file, const char *name)
622 char sql_filter[1024];
623 int before, after;
625 if (file) {
626 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
627 file, name);
628 } else {
629 snprintf(sql_filter, 1024, "function = '%s';", name);
632 before = ptr_list_size((struct ptr_list *)ptr_names);
634 run_sql(get_ptr_name,
635 "select distinct ptr from function_ptr where %s",
636 sql_filter);
638 after = ptr_list_size((struct ptr_list *)ptr_names);
639 if (before == after)
640 return;
642 while ((name = get_next_ptr_name()))
643 get_ptr_names(NULL, name);
646 static void match_data_from_db(struct symbol *sym)
648 struct sm_state *sm;
649 struct stree *stree;
651 if (!sym || !sym->ident || !sym->ident->name)
652 return;
654 __push_fake_cur_stree();
655 __unnullify_path();
656 prev_func_id = -1;
658 if (!__inline_fn) {
659 char *ptr;
661 if (sym->ctype.modifiers & MOD_STATIC)
662 get_ptr_names(get_base_file(), sym->ident->name);
663 else
664 get_ptr_names(NULL, sym->ident->name);
666 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
667 __free_ptr_list((struct ptr_list **)&ptr_names);
668 __free_ptr_list((struct ptr_list **)&ptr_names_done);
669 stree = __pop_fake_cur_stree();
670 free_stree(&stree);
671 return;
674 get_direct_callers(sym);
676 FOR_EACH_PTR(ptr_names, ptr) {
677 run_sql(caller_info_callback, "select call_id, type, parameter, key, value"
678 " from caller_info where function = '%s' order by call_id",
679 ptr);
680 free_string(ptr);
681 } END_FOR_EACH_PTR(ptr);
683 __free_ptr_list((struct ptr_list **)&ptr_names);
684 __free_ptr_list((struct ptr_list **)&ptr_names_done);
685 } else {
686 get_direct_callers(sym);
689 stree = __pop_fake_cur_stree();
690 merge_stree(&final_states, stree);
691 free_stree(&stree);
693 FOR_EACH_SM(final_states, sm) {
694 __set_sm(sm);
695 } END_FOR_EACH_SM(sm);
697 free_stree(&final_states);
700 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
702 struct expression *call_expr = static_call_expr;
703 struct call_implies_callback *cb;
704 struct expression *arg = NULL;
705 int type;
706 int param;
708 if (argc != 4)
709 return 0;
711 type = atoi(argv[1]);
712 param = atoi(argv[2]);
714 FOR_EACH_PTR(call_implies_cb_list, cb) {
715 if (cb->type != type)
716 continue;
717 if (param != -1) {
718 arg = get_argument_from_call_expr(call_expr->args, param);
719 if (!arg)
720 continue;
722 cb->callback(arg, argv[3]);
723 } END_FOR_EACH_PTR(cb);
725 return 0;
728 static void match_call_implies(struct expression *expr)
730 static_call_expr = expr;
731 sql_select_call_implies("function, type, parameter, value", expr,
732 call_implies_callbacks);
733 return;
736 static void print_initializer_list(struct expression_list *expr_list,
737 struct symbol *struct_type)
739 struct expression *expr;
740 struct symbol *base_type;
741 char struct_name[256];
743 FOR_EACH_PTR(expr_list, expr) {
744 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
745 print_initializer_list(expr->idx_expression->expr_list, struct_type);
746 continue;
748 if (expr->type != EXPR_IDENTIFIER)
749 continue;
750 if (!expr->expr_ident)
751 continue;
752 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
753 continue;
754 base_type = get_type(expr->ident_expression);
755 if (!base_type || base_type->type != SYM_FN)
756 continue;
757 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
758 struct_type->ident->name, expr->expr_ident->name);
759 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
760 struct_name);
761 } END_FOR_EACH_PTR(expr);
764 static void global_variable(struct symbol *sym)
766 struct symbol *struct_type;
768 if (!sym->ident)
769 return;
770 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
771 return;
772 struct_type = get_base_type(sym);
773 if (!struct_type)
774 return;
775 if (struct_type->type == SYM_ARRAY) {
776 struct_type = get_base_type(struct_type);
777 if (!struct_type)
778 return;
780 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
781 return;
782 print_initializer_list(sym->initializer->expr_list, struct_type);
785 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
787 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
790 static void call_return_state_hooks_conditional(struct expression *expr)
792 struct returned_state_callback *cb;
793 struct range_list *rl;
794 char *return_ranges;
795 int final_pass_orig = final_pass;
797 __push_fake_cur_stree();
799 final_pass = 0;
800 __split_whole_condition(expr->conditional);
801 final_pass = final_pass_orig;
803 if (get_implied_rl(expr->cond_true, &rl))
804 rl = cast_rl(cur_func_return_type(), rl);
805 else
806 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
807 return_ranges = show_rl(rl);
809 return_id++;
810 FOR_EACH_PTR(returned_state_callbacks, cb) {
811 cb->callback(return_id, return_ranges, expr);
812 } END_FOR_EACH_PTR(cb);
814 __push_true_states();
815 __use_false_states();
817 if (get_implied_rl(expr->cond_false, &rl))
818 rl = cast_rl(cur_func_return_type(), rl);
819 else
820 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
821 return_ranges = show_rl(rl);
823 return_id++;
824 FOR_EACH_PTR(returned_state_callbacks, cb) {
825 cb->callback(return_id, return_ranges, expr);
826 } END_FOR_EACH_PTR(cb);
828 __merge_true_states();
829 __free_fake_cur_stree();
832 static void call_return_state_hooks_compare(struct expression *expr)
834 struct returned_state_callback *cb;
835 char *return_ranges;
836 int final_pass_orig = final_pass;
838 __push_fake_cur_stree();
840 final_pass = 0;
841 __split_whole_condition(expr);
842 final_pass = final_pass_orig;
844 return_ranges = alloc_sname("1");
846 return_id++;
847 FOR_EACH_PTR(returned_state_callbacks, cb) {
848 cb->callback(return_id, return_ranges, expr);
849 } END_FOR_EACH_PTR(cb);
851 __push_true_states();
852 __use_false_states();
854 return_ranges = alloc_sname("0");;
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 __merge_true_states();
861 __free_fake_cur_stree();
864 static int call_return_state_hooks_split_possible(struct expression *expr)
866 struct returned_state_callback *cb;
867 struct range_list *rl;
868 char *return_ranges;
869 struct sm_state *sm;
870 struct sm_state *tmp;
871 int ret = 0;
872 int nr_possible, nr_states;
873 char *compare_str;
874 char buf[128];
876 if (!expr || expr_equal_to_param(expr))
877 return 0;
879 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
880 if (!sm || !sm->merged)
881 return 0;
883 if (too_many_possible(sm))
884 return 0;
886 /* bail if it gets too complicated */
887 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
888 nr_states = stree_count(__get_cur_stree());
890 * the main thing option_info because we don't want to print a
891 * million lines of output. If someone else, like check_locking.c
892 * wants this data, then it doesn't cause a slow down to provide it.
894 if (option_info && nr_states * nr_possible >= 2000)
895 return 0;
897 compare_str = expr_lte_to_param(expr);
899 FOR_EACH_PTR(sm->possible, tmp) {
900 if (tmp->merged)
901 continue;
903 ret = 1;
904 __push_fake_cur_stree();
906 overwrite_states_using_pool(tmp);
908 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
909 return_ranges = show_rl(rl);
910 if (compare_str) {
911 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
912 return_ranges = alloc_sname(buf);
915 return_id++;
916 FOR_EACH_PTR(returned_state_callbacks, cb) {
917 cb->callback(return_id, return_ranges, expr);
918 } END_FOR_EACH_PTR(cb);
920 __free_fake_cur_stree();
921 } END_FOR_EACH_PTR(tmp);
923 return ret;
926 static char *get_return_ranges_str(struct expression *expr)
928 struct range_list *rl;
929 char *return_ranges;
930 char *compare_str;
931 char buf[128];
933 if (!expr)
934 return alloc_sname("");
935 compare_str = expr_equal_to_param(expr);
936 if (compare_str)
937 return compare_str;
939 if (get_implied_rl(expr, &rl)) {
940 rl = cast_rl(cur_func_return_type(), rl);
941 return_ranges = show_rl(rl);
942 } else {
943 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
944 return_ranges = show_rl(rl);
946 compare_str = expr_lte_to_param(expr);
947 if (compare_str) {
948 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
949 return alloc_sname(buf);
951 return return_ranges;
954 static int is_boolean(struct expression *expr)
956 struct range_list *rl;
958 if (!get_implied_rl(expr, &rl))
959 return 0;
960 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
961 return 1;
962 return 0;
965 static int is_conditional(struct expression *expr)
967 if (!expr)
968 return 0;
969 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
970 return 1;
971 return 0;
974 static void call_return_state_hooks(struct expression *expr)
976 struct returned_state_callback *cb;
977 char *return_ranges;
978 int nr_states;
980 expr = strip_expr(expr);
982 if (is_condition(expr) || is_boolean(expr)) {
983 call_return_state_hooks_compare(expr);
984 return;
985 } else if (is_conditional(expr)) {
986 call_return_state_hooks_conditional(expr);
987 return;
988 } else if (call_return_state_hooks_split_possible(expr)) {
989 return;
992 return_ranges = get_return_ranges_str(expr);
994 return_id++;
995 nr_states = stree_count(__get_cur_stree());
996 if (nr_states >= 10000) {
997 match_return_info(return_id, return_ranges, expr);
998 return;
1000 FOR_EACH_PTR(returned_state_callbacks, cb) {
1001 cb->callback(return_id, return_ranges, expr);
1002 } END_FOR_EACH_PTR(cb);
1005 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1007 struct returned_member_callback *cb;
1008 struct stree *stree;
1009 struct sm_state *sm;
1010 struct symbol *type;
1011 char *name;
1012 char member_name[256];
1013 int len;
1015 type = get_type(expr);
1016 if (!type || type->type != SYM_PTR)
1017 return;
1018 name = expr_to_var(expr);
1019 if (!name)
1020 return;
1022 member_name[sizeof(member_name) - 1] = '\0';
1023 strcpy(member_name, "$$");
1025 len = strlen(name);
1026 FOR_EACH_PTR(returned_member_callbacks, cb) {
1027 stree = __get_cur_stree();
1028 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1029 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1030 strcpy(member_name, "*$$");
1031 cb->callback(return_id, return_ranges, member_name, sm->state);
1032 continue;
1034 if (strncmp(sm->name, name, len) != 0)
1035 continue;
1036 if (strncmp(sm->name + len, "->", 2) != 0)
1037 continue;
1038 strcpy(member_name, "$$");
1039 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
1040 cb->callback(return_id, return_ranges, member_name, sm->state);
1041 } END_FOR_EACH_SM(sm);
1042 } END_FOR_EACH_PTR(cb);
1044 free_string(name);
1047 static void reset_memdb(void)
1049 mem_sql(NULL, "delete from caller_info;");
1050 mem_sql(NULL, "delete from return_states;");
1051 mem_sql(NULL, "delete from call_implies;");
1054 static void match_end_func_info(struct symbol *sym)
1056 if (__path_is_null())
1057 return;
1058 call_return_state_hooks(NULL);
1059 if (!__inline_fn)
1060 reset_memdb();
1063 static void init_memdb(void)
1065 char *err = NULL;
1066 int rc;
1067 const char *schema_files[] = {
1068 "db/db.schema",
1069 "db/caller_info.schema",
1070 "db/return_states.schema",
1071 "db/function_type_size.schema",
1072 "db/type_size.schema",
1073 "db/call_implies.schema",
1074 "db/function_ptr.schema",
1075 "db/local_values.schema",
1076 "db/function_type_value.schema",
1077 "db/type_value.schema",
1079 static char buf[4096];
1080 int fd;
1081 int ret;
1082 int i;
1084 rc = sqlite3_open(":memory:", &mem_db);
1085 if (rc != SQLITE_OK) {
1086 printf("Error starting In-Memory database.");
1087 return;
1090 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1091 fd = open_data_file(schema_files[i]);
1092 if (fd < 0) {
1093 mem_db = NULL;
1094 return;
1096 ret = read(fd, buf, sizeof(buf));
1097 if (ret == sizeof(buf)) {
1098 printf("Schema file too large: %s (limit %zd bytes)",
1099 schema_files[i], sizeof(buf));
1101 buf[ret] = '\0';
1102 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1103 if (rc != SQLITE_OK) {
1104 fprintf(stderr, "SQL error #2: %s\n", err);
1105 fprintf(stderr, "%s\n", buf);
1110 void open_smatch_db(void)
1112 int rc;
1114 if (option_no_db)
1115 return;
1117 init_memdb();
1119 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1120 if (rc != SQLITE_OK) {
1121 option_no_db = 1;
1122 return;
1124 return;
1127 static void register_common_funcs(void)
1129 struct token *token;
1130 char *func;
1131 char filename[256];
1133 if (option_project == PROJ_NONE)
1134 strcpy(filename, "common_functions");
1135 else
1136 snprintf(filename, 256, "%s.common_functions", option_project_str);
1138 token = get_tokens_file(filename);
1139 if (!token)
1140 return;
1141 if (token_type(token) != TOKEN_STREAMBEGIN)
1142 return;
1143 token = token->next;
1144 while (token_type(token) != TOKEN_STREAMEND) {
1145 if (token_type(token) != TOKEN_IDENT)
1146 return;
1147 func = alloc_string(show_ident(token->ident));
1148 add_ptr_list(&common_funcs, func);
1149 token = token->next;
1151 clear_token_alloc();
1155 void register_definition_db_callbacks(int id)
1157 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1158 add_hook(&global_variable, BASE_HOOK);
1159 add_hook(&global_variable, DECLARATION_HOOK);
1160 add_split_return_callback(match_return_info);
1161 add_split_return_callback(print_returned_struct_members);
1162 add_hook(&call_return_state_hooks, RETURN_HOOK);
1163 add_hook(&match_end_func_info, END_FUNC_HOOK);
1165 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1166 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1168 register_common_funcs();
1171 void register_db_call_marker(int id)
1173 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1176 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1178 struct expression *arg;
1179 char *name = NULL;
1180 char member_name[256];
1182 *sym = NULL;
1184 if (param == -1) {
1185 const char *star = "";
1187 if (expr->type != EXPR_ASSIGNMENT)
1188 return NULL;
1189 name = expr_to_var_sym(expr->left, sym);
1190 if (!name)
1191 return NULL;
1192 if (key[0] == '*') {
1193 star = "*";
1194 key++;
1196 if (strncmp(key, "$$", 2) != 0)
1197 return name;
1198 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1199 free_string(name);
1200 return alloc_string(member_name);
1203 while (expr->type == EXPR_ASSIGNMENT)
1204 expr = strip_expr(expr->right);
1205 if (expr->type != EXPR_CALL)
1206 return NULL;
1208 arg = get_argument_from_call_expr(expr->args, param);
1209 if (!arg)
1210 return NULL;
1212 return get_variable_from_key(arg, key, sym);
1215 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1217 char buf[256];
1218 char *tmp;
1220 if (strcmp(key, "$$") == 0)
1221 return expr_to_var_sym(arg, sym);
1223 if (strcmp(key, "*$$") == 0) {
1224 if (arg->type == EXPR_PREOP && arg->op == '&') {
1225 arg = strip_expr(arg->unop);
1226 return expr_to_var_sym(arg, sym);
1227 } else {
1228 tmp = expr_to_var_sym(arg, sym);
1229 if (!tmp)
1230 return NULL;
1231 snprintf(buf, sizeof(buf), "*%s", tmp);
1232 free_string(tmp);
1233 return alloc_string(buf);
1237 if (arg->type == EXPR_PREOP && arg->op == '&') {
1238 arg = strip_expr(arg->unop);
1239 tmp = expr_to_var_sym(arg, sym);
1240 if (!tmp)
1241 return NULL;
1242 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1243 return alloc_string(buf);
1246 tmp = expr_to_var_sym(arg, sym);
1247 if (!tmp)
1248 return NULL;
1249 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1250 free_string(tmp);
1251 return alloc_string(buf);
1254 const char *get_param_name(struct sm_state *sm)
1256 char *param_name;
1257 int name_len;
1258 static char buf[256];
1260 if (!sm->sym->ident)
1261 return NULL;
1263 param_name = sm->sym->ident->name;
1264 name_len = strlen(param_name);
1266 if (strcmp(sm->name, param_name) == 0) {
1267 return "$$";
1268 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1269 strncmp(sm->name, param_name, name_len) == 0) {
1270 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1271 return buf;
1272 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1273 return "*$$";
1275 return NULL;