data/kernel.sizeof_param.remove: add __dynamic_pr_debug()
[smatch.git] / smatch_db.c
blobec06dafd8d46d6bdb2a64aaee3d8a6cae0aea132
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->fn);
271 if (!ptr)
272 return;
274 run_sql(callback,
275 "select %s from return_states join function_ptr where "
276 "return_states.function == function_ptr.function and ptr = '%s'"
277 "and searchable = 1 order by return_id, type;",
278 cols, ptr);
281 void sql_select_return_states(const char *cols, struct expression *call,
282 int (*callback)(void*, int, char**, char**))
284 if (is_fake_call(call))
285 return;
287 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
288 sql_select_return_states_pointer(cols, call, callback);
289 return;
292 if (inlinable(call->fn)) {
293 mem_sql(callback,
294 "select %s from return_states where call_id = '%lu' order by return_id, type;",
295 cols, (unsigned long)call);
296 return;
299 row_count = 0;
300 run_sql(get_row_count, "select count(*) from return_states where %s;",
301 get_static_filter(call->fn->symbol));
302 if (row_count > 1000)
303 return;
305 run_sql(callback, "select %s from return_states where %s order by return_id, type;",
306 cols, get_static_filter(call->fn->symbol));
309 void sql_select_call_implies(const char *cols, struct expression *call,
310 int (*callback)(void*, int, char**, char**))
312 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
313 return;
315 if (inlinable(call->fn)) {
316 mem_sql(callback,
317 "select %s from call_implies where call_id = '%lu';",
318 cols, (unsigned long)call);
319 return;
322 run_sql(callback, "select %s from call_implies where %s;",
323 cols, get_static_filter(call->fn->symbol));
326 void sql_select_caller_info(const char *cols, struct symbol *sym,
327 int (*callback)(void*, int, char**, char**))
329 if (__inline_fn) {
330 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
331 cols, (unsigned long)__inline_fn);
332 return;
335 run_sql(callback,
336 "select %s from caller_info where %s order by call_id;",
337 cols, get_static_filter(sym));
340 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
342 struct def_callback *def_callback = __alloc_def_callback(0);
344 def_callback->hook_type = type;
345 def_callback->callback = callback;
346 add_ptr_list(&callbacks, def_callback);
350 * These call backs are used when the --info option is turned on to print struct
351 * member information. For example foo->bar could have a state in
352 * smatch_extra.c and also check_user.c.
354 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
356 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
358 member_callback->owner = owner;
359 member_callback->callback = callback;
360 add_ptr_list(&member_callbacks, member_callback);
363 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
365 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
367 callback->callback = fn;
368 add_ptr_list(&returned_state_callbacks, callback);
371 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
373 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
375 member_callback->owner = owner;
376 member_callback->callback = callback;
377 add_ptr_list(&returned_member_callbacks, member_callback);
380 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *value))
382 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
384 cb->type = type;
385 cb->callback = callback;
386 add_ptr_list(&call_implies_cb_list, cb);
389 static struct expression *static_call_expr;
390 static struct expression *static_returns_call;
391 static struct symbol *return_type;
392 static struct range_list *return_range_list;
393 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
395 struct range_list *rl;
396 struct expression *call_expr = static_returns_call;
398 if (argc != 1)
399 return 0;
400 call_results_to_rl(call_expr, return_type, argv[0], &rl);
401 return_range_list = rl_union(return_range_list, rl);
402 return 0;
405 struct range_list *db_return_vals(struct expression *expr)
407 static_returns_call = expr;
408 return_type = get_type(expr);
409 if (!return_type)
410 return NULL;
411 if (is_fake_call(expr))
412 return NULL;
413 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
414 return NULL;
416 return_range_list = NULL;
417 if (inlinable(expr->fn)) {
418 mem_sql(db_return_callback,
419 "select distinct return from return_states where call_id = '%lu';",
420 (unsigned long)expr);
421 } else {
422 run_sql(db_return_callback,
423 "select distinct return from return_states where %s;",
424 get_static_filter(expr->fn->symbol));
426 return return_range_list;
429 static void match_call_marker(struct expression *expr)
432 * we just want to record something in the database so that if we have
433 * two calls like: frob(4); frob(some_unkown); then on the receiving
434 * side we know that sometimes frob is called with unknown parameters.
437 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
440 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
441 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
443 struct sm_state *sm;
444 char *name;
445 struct symbol *sym;
446 int len;
447 char printed_name[256];
448 int is_address = 0;
450 expr = strip_expr(expr);
451 if (expr->type == EXPR_PREOP && expr->op == '&') {
452 expr = strip_expr(expr->unop);
453 is_address = 1;
456 name = expr_to_var_sym(expr, &sym);
457 if (!name || !sym)
458 goto free;
460 len = strlen(name);
461 FOR_EACH_SM(stree, sm) {
462 if (sm->sym != sym)
463 continue;
464 if (strcmp(name, sm->name) == 0) {
465 if (is_address)
466 snprintf(printed_name, sizeof(printed_name), "*$$");
467 else /* these are already handled. fixme: handle them here */
468 continue;
469 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
470 snprintf(printed_name, sizeof(printed_name), "*$$");
471 } else if (strncmp(name, sm->name, len) == 0) {
472 if (is_address)
473 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
474 else
475 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
476 } else {
477 continue;
479 callback(call, param, printed_name, sm->state);
480 } END_FOR_EACH_SM(sm);
481 free:
482 free_string(name);
485 static void match_call_info(struct expression *call)
487 struct member_info_callback *cb;
488 struct expression *arg;
489 struct stree *stree;
490 char *name;
491 int i;
493 name = get_fnptr_name(call->fn);
494 if (!name)
495 return;
497 FOR_EACH_PTR(member_callbacks, cb) {
498 stree = get_all_states_stree(cb->owner);
499 i = 0;
500 FOR_EACH_PTR(call->args, arg) {
501 print_struct_members(call, arg, i, stree, cb->callback);
502 i++;
503 } END_FOR_EACH_PTR(arg);
504 free_stree(&stree);
505 } END_FOR_EACH_PTR(cb);
507 free_string(name);
510 static int get_param(int param, char **name, struct symbol **sym)
512 struct symbol *arg;
513 int i;
515 i = 0;
516 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
518 * this is a temporary hack to work around a bug (I think in sparse?)
519 * 2.6.37-rc1:fs/reiserfs/journal.o
520 * If there is a function definition without parameter name found
521 * after a function implementation then it causes a crash.
522 * int foo() {}
523 * int bar(char *);
525 if (arg->ident->name < (char *)100)
526 continue;
527 if (i == param && arg->ident->name) {
528 *name = arg->ident->name;
529 *sym = arg;
530 return TRUE;
532 i++;
533 } END_FOR_EACH_PTR(arg);
535 return FALSE;
538 static struct stree *final_states;
539 static int prev_func_id = -1;
540 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
542 int func_id;
543 long type;
544 long param;
545 char *name = NULL;
546 struct symbol *sym = NULL;
547 struct def_callback *def_callback;
548 struct stree *stree;
550 if (argc != 5)
551 return 0;
553 func_id = atoi(argv[0]);
554 errno = 0;
555 type = strtol(argv[1], NULL, 10);
556 param = strtol(argv[2], NULL, 10);
557 if (errno)
558 return 0;
560 if (prev_func_id == -1)
561 prev_func_id = func_id;
562 if (func_id != prev_func_id) {
563 stree = __pop_fake_cur_stree();
564 merge_stree(&final_states, stree);
565 free_stree(&stree);
566 __push_fake_cur_stree();
567 __unnullify_path();
568 prev_func_id = func_id;
571 if (type == INTERNAL)
572 return 0;
573 if (param >= 0 && !get_param(param, &name, &sym))
574 return 0;
576 FOR_EACH_PTR(callbacks, def_callback) {
577 if (def_callback->hook_type == type)
578 def_callback->callback(name, sym, argv[3], argv[4]);
579 } END_FOR_EACH_PTR(def_callback);
581 return 0;
584 static void get_direct_callers(struct symbol *sym)
586 sql_select_caller_info("call_id, type, parameter, key, value", sym,
587 caller_info_callback);
590 static struct string_list *ptr_names_done;
591 static struct string_list *ptr_names;
593 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
595 insert_string(&ptr_names, alloc_string(argv[0]));
596 return 0;
599 static char *get_next_ptr_name(void)
601 char *ptr;
603 FOR_EACH_PTR(ptr_names, ptr) {
604 if (list_has_string(ptr_names_done, ptr))
605 continue;
606 insert_string(&ptr_names_done, ptr);
607 return ptr;
608 } END_FOR_EACH_PTR(ptr);
609 return NULL;
612 static void get_ptr_names(const char *file, const char *name)
614 char sql_filter[1024];
615 int before, after;
617 if (file) {
618 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
619 file, name);
620 } else {
621 snprintf(sql_filter, 1024, "function = '%s';", name);
624 before = ptr_list_size((struct ptr_list *)ptr_names);
626 run_sql(get_ptr_name,
627 "select distinct ptr from function_ptr where %s",
628 sql_filter);
630 after = ptr_list_size((struct ptr_list *)ptr_names);
631 if (before == after)
632 return;
634 while ((name = get_next_ptr_name()))
635 get_ptr_names(NULL, name);
638 static void match_data_from_db(struct symbol *sym)
640 struct sm_state *sm;
641 struct stree *stree;
643 if (!sym || !sym->ident || !sym->ident->name)
644 return;
646 __push_fake_cur_stree();
647 __unnullify_path();
648 prev_func_id = -1;
650 if (!__inline_fn) {
651 char *ptr;
653 if (sym->ctype.modifiers & MOD_STATIC)
654 get_ptr_names(get_base_file(), sym->ident->name);
655 else
656 get_ptr_names(NULL, sym->ident->name);
658 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
659 __free_ptr_list((struct ptr_list **)&ptr_names);
660 __free_ptr_list((struct ptr_list **)&ptr_names_done);
661 stree = __pop_fake_cur_stree();
662 free_stree(&stree);
663 return;
666 get_direct_callers(sym);
668 FOR_EACH_PTR(ptr_names, ptr) {
669 run_sql(caller_info_callback, "select call_id, type, parameter, key, value"
670 " from caller_info where function = '%s' order by call_id",
671 ptr);
672 free_string(ptr);
673 } END_FOR_EACH_PTR(ptr);
675 __free_ptr_list((struct ptr_list **)&ptr_names);
676 __free_ptr_list((struct ptr_list **)&ptr_names_done);
677 } else {
678 get_direct_callers(sym);
681 stree = __pop_fake_cur_stree();
682 merge_stree(&final_states, stree);
683 free_stree(&stree);
685 FOR_EACH_SM(final_states, sm) {
686 __set_sm(sm);
687 } END_FOR_EACH_SM(sm);
689 free_stree(&final_states);
692 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
694 struct expression *call_expr = static_call_expr;
695 struct call_implies_callback *cb;
696 struct expression *arg = NULL;
697 int type;
698 int param;
700 if (argc != 4)
701 return 0;
703 type = atoi(argv[1]);
704 param = atoi(argv[2]);
706 FOR_EACH_PTR(call_implies_cb_list, cb) {
707 if (cb->type != type)
708 continue;
709 if (param != -1) {
710 arg = get_argument_from_call_expr(call_expr->args, param);
711 if (!arg)
712 continue;
714 cb->callback(arg, argv[3]);
715 } END_FOR_EACH_PTR(cb);
717 return 0;
720 static void match_call_implies(struct expression *expr)
722 static_call_expr = expr;
723 sql_select_call_implies("function, type, parameter, value", expr,
724 call_implies_callbacks);
725 return;
728 static void print_initializer_list(struct expression_list *expr_list,
729 struct symbol *struct_type)
731 struct expression *expr;
732 struct symbol *base_type;
733 char struct_name[256];
735 FOR_EACH_PTR(expr_list, expr) {
736 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
737 print_initializer_list(expr->idx_expression->expr_list, struct_type);
738 continue;
740 if (expr->type != EXPR_IDENTIFIER)
741 continue;
742 if (!expr->expr_ident)
743 continue;
744 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
745 continue;
746 base_type = get_type(expr->ident_expression);
747 if (!base_type || base_type->type != SYM_FN)
748 continue;
749 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
750 struct_type->ident->name, expr->expr_ident->name);
751 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
752 struct_name);
753 } END_FOR_EACH_PTR(expr);
756 static void global_variable(struct symbol *sym)
758 struct symbol *struct_type;
760 if (!sym->ident)
761 return;
762 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
763 return;
764 struct_type = get_base_type(sym);
765 if (!struct_type)
766 return;
767 if (struct_type->type == SYM_ARRAY) {
768 struct_type = get_base_type(struct_type);
769 if (!struct_type)
770 return;
772 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
773 return;
774 print_initializer_list(sym->initializer->expr_list, struct_type);
777 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
779 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
782 static void call_return_state_hooks_conditional(struct expression *expr)
784 struct returned_state_callback *cb;
785 struct range_list *rl;
786 char *return_ranges;
787 int final_pass_orig = final_pass;
789 __push_fake_cur_stree();
791 final_pass = 0;
792 __split_whole_condition(expr->conditional);
793 final_pass = final_pass_orig;
795 if (get_implied_rl(expr->cond_true, &rl))
796 rl = cast_rl(cur_func_return_type(), rl);
797 else
798 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
799 return_ranges = show_rl(rl);
801 return_id++;
802 FOR_EACH_PTR(returned_state_callbacks, cb) {
803 cb->callback(return_id, return_ranges, expr);
804 } END_FOR_EACH_PTR(cb);
806 __push_true_states();
807 __use_false_states();
809 if (get_implied_rl(expr->cond_false, &rl))
810 rl = cast_rl(cur_func_return_type(), rl);
811 else
812 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
813 return_ranges = show_rl(rl);
815 return_id++;
816 FOR_EACH_PTR(returned_state_callbacks, cb) {
817 cb->callback(return_id, return_ranges, expr);
818 } END_FOR_EACH_PTR(cb);
820 __merge_true_states();
821 __free_fake_cur_stree();
824 static void call_return_state_hooks_compare(struct expression *expr)
826 struct returned_state_callback *cb;
827 char *return_ranges;
828 int final_pass_orig = final_pass;
830 __push_fake_cur_stree();
832 final_pass = 0;
833 __split_whole_condition(expr);
834 final_pass = final_pass_orig;
836 return_ranges = alloc_sname("1");
838 return_id++;
839 FOR_EACH_PTR(returned_state_callbacks, cb) {
840 cb->callback(return_id, return_ranges, expr);
841 } END_FOR_EACH_PTR(cb);
843 __push_true_states();
844 __use_false_states();
846 return_ranges = alloc_sname("0");;
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 __merge_true_states();
853 __free_fake_cur_stree();
856 static int call_return_state_hooks_split_possible(struct expression *expr)
858 struct returned_state_callback *cb;
859 struct range_list *rl;
860 char *return_ranges;
861 struct sm_state *sm;
862 struct sm_state *tmp;
863 int ret = 0;
864 int nr_possible, nr_states;
865 char *compare_str;
866 char buf[128];
868 if (!expr || expr_equal_to_param(expr))
869 return 0;
871 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
872 if (!sm || !sm->merged)
873 return 0;
875 if (too_many_possible(sm))
876 return 0;
878 /* bail if it gets too complicated */
879 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
880 nr_states = stree_count(__get_cur_stree());
882 * the main thing option_info because we don't want to print a
883 * million lines of output. If someone else, like check_locking.c
884 * wants this data, then it doesn't cause a slow down to provide it.
886 if (option_info && nr_states * nr_possible >= 2000)
887 return 0;
889 compare_str = expr_lte_to_param(expr);
891 FOR_EACH_PTR(sm->possible, tmp) {
892 if (tmp->merged)
893 continue;
895 ret = 1;
896 __push_fake_cur_stree();
898 overwrite_states_using_pool(tmp);
900 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
901 return_ranges = show_rl(rl);
902 if (compare_str) {
903 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
904 return_ranges = alloc_sname(buf);
907 return_id++;
908 FOR_EACH_PTR(returned_state_callbacks, cb) {
909 cb->callback(return_id, return_ranges, expr);
910 } END_FOR_EACH_PTR(cb);
912 __free_fake_cur_stree();
913 } END_FOR_EACH_PTR(tmp);
915 return ret;
918 static char *get_return_ranges_str(struct expression *expr)
920 struct range_list *rl;
921 char *return_ranges;
922 char *compare_str;
923 char buf[128];
925 if (!expr)
926 return alloc_sname("");
927 compare_str = expr_equal_to_param(expr);
928 if (compare_str)
929 return compare_str;
931 if (get_implied_rl(expr, &rl)) {
932 rl = cast_rl(cur_func_return_type(), rl);
933 return_ranges = show_rl(rl);
934 } else {
935 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
936 return_ranges = show_rl(rl);
938 compare_str = expr_lte_to_param(expr);
939 if (compare_str) {
940 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
941 return alloc_sname(buf);
943 return return_ranges;
946 static int is_boolean(struct expression *expr)
948 struct range_list *rl;
950 if (!get_implied_rl(expr, &rl))
951 return 0;
952 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
953 return 1;
954 return 0;
957 static int is_conditional(struct expression *expr)
959 if (!expr)
960 return 0;
961 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
962 return 1;
963 return 0;
966 static void call_return_state_hooks(struct expression *expr)
968 struct returned_state_callback *cb;
969 char *return_ranges;
970 int nr_states;
972 expr = strip_expr(expr);
974 if (is_condition(expr) || is_boolean(expr)) {
975 call_return_state_hooks_compare(expr);
976 return;
977 } else if (is_conditional(expr)) {
978 call_return_state_hooks_conditional(expr);
979 return;
980 } else if (call_return_state_hooks_split_possible(expr)) {
981 return;
984 return_ranges = get_return_ranges_str(expr);
986 return_id++;
987 nr_states = stree_count(__get_cur_stree());
988 if (nr_states >= 10000) {
989 match_return_info(return_id, return_ranges, expr);
990 return;
992 FOR_EACH_PTR(returned_state_callbacks, cb) {
993 cb->callback(return_id, return_ranges, expr);
994 } END_FOR_EACH_PTR(cb);
997 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
999 struct returned_member_callback *cb;
1000 struct stree *stree;
1001 struct sm_state *sm;
1002 struct symbol *type;
1003 char *name;
1004 char member_name[256];
1005 int len;
1007 type = get_type(expr);
1008 if (!type || type->type != SYM_PTR)
1009 return;
1010 name = expr_to_var(expr);
1011 if (!name)
1012 return;
1014 member_name[sizeof(member_name) - 1] = '\0';
1015 strcpy(member_name, "$$");
1017 len = strlen(name);
1018 FOR_EACH_PTR(returned_member_callbacks, cb) {
1019 stree = __get_cur_stree();
1020 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1021 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1022 strcpy(member_name, "*$$");
1023 cb->callback(return_id, return_ranges, member_name, sm->state);
1024 continue;
1026 if (strncmp(sm->name, name, len) != 0)
1027 continue;
1028 if (strncmp(sm->name + len, "->", 2) != 0)
1029 continue;
1030 strcpy(member_name, "$$");
1031 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
1032 cb->callback(return_id, return_ranges, member_name, sm->state);
1033 } END_FOR_EACH_SM(sm);
1034 } END_FOR_EACH_PTR(cb);
1036 free_string(name);
1039 static void reset_memdb(void)
1041 mem_sql(NULL, "delete from caller_info;");
1042 mem_sql(NULL, "delete from return_states;");
1043 mem_sql(NULL, "delete from call_implies;");
1046 static void match_end_func_info(struct symbol *sym)
1048 if (__path_is_null())
1049 return;
1050 call_return_state_hooks(NULL);
1051 if (!__inline_fn)
1052 reset_memdb();
1055 static void init_memdb(void)
1057 char *err = NULL;
1058 int rc;
1059 const char *schema_files[] = {
1060 "db/db.schema",
1061 "db/caller_info.schema",
1062 "db/return_states.schema",
1063 "db/function_type_size.schema",
1064 "db/type_size.schema",
1065 "db/call_implies.schema",
1066 "db/function_ptr.schema",
1067 "db/local_values.schema",
1068 "db/function_type_value.schema",
1069 "db/type_value.schema",
1071 static char buf[4096];
1072 int fd;
1073 int ret;
1074 int i;
1076 rc = sqlite3_open(":memory:", &mem_db);
1077 if (rc != SQLITE_OK) {
1078 printf("Error starting In-Memory database.");
1079 return;
1082 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1083 fd = open_data_file(schema_files[i]);
1084 if (fd < 0) {
1085 mem_db = NULL;
1086 return;
1088 ret = read(fd, buf, sizeof(buf));
1089 if (ret == sizeof(buf)) {
1090 printf("Schema file too large: %s (limit %zd bytes)",
1091 schema_files[i], sizeof(buf));
1093 buf[ret] = '\0';
1094 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1095 if (rc != SQLITE_OK) {
1096 fprintf(stderr, "SQL error #2: %s\n", err);
1097 fprintf(stderr, "%s\n", buf);
1102 void open_smatch_db(void)
1104 int rc;
1106 if (option_no_db)
1107 return;
1109 init_memdb();
1111 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1112 if (rc != SQLITE_OK) {
1113 option_no_db = 1;
1114 return;
1116 return;
1119 static void register_common_funcs(void)
1121 struct token *token;
1122 char *func;
1123 char filename[256];
1125 if (option_project == PROJ_NONE)
1126 strcpy(filename, "common_functions");
1127 else
1128 snprintf(filename, 256, "%s.common_functions", option_project_str);
1130 token = get_tokens_file(filename);
1131 if (!token)
1132 return;
1133 if (token_type(token) != TOKEN_STREAMBEGIN)
1134 return;
1135 token = token->next;
1136 while (token_type(token) != TOKEN_STREAMEND) {
1137 if (token_type(token) != TOKEN_IDENT)
1138 return;
1139 func = alloc_string(show_ident(token->ident));
1140 add_ptr_list(&common_funcs, func);
1141 token = token->next;
1143 clear_token_alloc();
1147 void register_definition_db_callbacks(int id)
1149 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1150 add_hook(&global_variable, BASE_HOOK);
1151 add_hook(&global_variable, DECLARATION_HOOK);
1152 add_split_return_callback(match_return_info);
1153 add_split_return_callback(print_returned_struct_members);
1154 add_hook(&call_return_state_hooks, RETURN_HOOK);
1155 add_hook(&match_end_func_info, END_FUNC_HOOK);
1157 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1158 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1160 register_common_funcs();
1163 void register_db_call_marker(int id)
1165 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1168 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1170 struct expression *arg;
1171 char *name = NULL;
1172 char member_name[256];
1174 *sym = NULL;
1176 if (param == -1) {
1177 const char *star = "";
1179 if (expr->type != EXPR_ASSIGNMENT)
1180 return NULL;
1181 name = expr_to_var_sym(expr->left, sym);
1182 if (!name)
1183 return NULL;
1184 if (key[0] == '*') {
1185 star = "*";
1186 key++;
1188 if (strncmp(key, "$$", 2) != 0)
1189 return name;
1190 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1191 free_string(name);
1192 return alloc_string(member_name);
1195 while (expr->type == EXPR_ASSIGNMENT)
1196 expr = strip_expr(expr->right);
1197 if (expr->type != EXPR_CALL)
1198 return NULL;
1200 arg = get_argument_from_call_expr(expr->args, param);
1201 if (!arg)
1202 return NULL;
1204 return get_variable_from_key(arg, key, sym);
1207 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1209 char buf[256];
1210 char *tmp;
1212 if (strcmp(key, "$$") == 0)
1213 return expr_to_var_sym(arg, sym);
1215 if (strcmp(key, "*$$") == 0) {
1216 if (arg->type == EXPR_PREOP && arg->op == '&') {
1217 arg = strip_expr(arg->unop);
1218 return expr_to_var_sym(arg, sym);
1219 } else {
1220 tmp = expr_to_var_sym(arg, sym);
1221 if (!tmp)
1222 return NULL;
1223 snprintf(buf, sizeof(buf), "*%s", tmp);
1224 free_string(tmp);
1225 return alloc_string(buf);
1229 if (arg->type == EXPR_PREOP && arg->op == '&') {
1230 arg = strip_expr(arg->unop);
1231 tmp = expr_to_var_sym(arg, sym);
1232 if (!tmp)
1233 return NULL;
1234 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1235 return alloc_string(buf);
1238 tmp = expr_to_var_sym(arg, sym);
1239 if (!tmp)
1240 return NULL;
1241 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1242 free_string(tmp);
1243 return alloc_string(buf);
1246 const char *get_param_name(struct sm_state *sm)
1248 char *param_name;
1249 int name_len;
1250 static char buf[256];
1252 if (!sm->sym->ident)
1253 return NULL;
1255 param_name = sm->sym->ident->name;
1256 name_len = strlen(param_name);
1258 if (strcmp(sm->name, param_name) == 0) {
1259 return "$$";
1260 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1261 strncmp(sm->name, param_name, name_len) == 0) {
1262 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1263 return buf;
1264 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1265 return "*$$";
1267 return NULL;