extra: save return values in terms of parameter math
[smatch.git] / smatch_db.c
blob860725a1f186e87ae036470bf2d16f240bfc4047
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 *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, 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', 0", get_base_file(), fn,
215 struct_name);
218 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
220 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
221 get_function(), (unsigned long)__inline_fn, fn_static(),
222 type, param, key, 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 void sql_insert_function_type_info(int param, const char *value)
242 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
243 get_base_file(), get_function(), fn_static(), param, value);
246 static char *get_static_filter(struct symbol *sym)
248 static char sql_filter[1024];
250 if (sym->ctype.modifiers & MOD_STATIC) {
251 snprintf(sql_filter, sizeof(sql_filter),
252 "file = '%s' and function = '%s' and static = '1'",
253 get_base_file(), sym->ident->name);
254 } else {
255 snprintf(sql_filter, sizeof(sql_filter),
256 "function = '%s' and static = '0'", sym->ident->name);
259 return sql_filter;
262 static int row_count;
263 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
265 if (argc != 1)
266 return 0;
267 row_count = atoi(argv[0]);
268 return 0;
271 static void sql_select_return_states_pointer(const char *cols,
272 struct expression *call, int (*callback)(void*, int, char**, char**))
274 char *ptr;
276 ptr = get_fnptr_name(call->fn);
277 if (!ptr)
278 return;
280 run_sql(callback,
281 "select %s from return_states join function_ptr where "
282 "return_states.function == function_ptr.function and ptr = '%s'"
283 "and searchable = 1 order by return_id, type;",
284 cols, ptr);
287 void sql_select_return_states(const char *cols, struct expression *call,
288 int (*callback)(void*, int, char**, char**))
290 if (is_fake_call(call))
291 return;
293 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
294 sql_select_return_states_pointer(cols, call, callback);
295 return;
298 if (inlinable(call->fn)) {
299 mem_sql(callback,
300 "select %s from return_states where call_id = '%lu' order by return_id, type;",
301 cols, (unsigned long)call);
302 return;
305 row_count = 0;
306 run_sql(get_row_count, "select count(*) from return_states where %s;",
307 get_static_filter(call->fn->symbol));
308 if (row_count > 1000)
309 return;
311 run_sql(callback, "select %s from return_states where %s order by return_id, type;",
312 cols, get_static_filter(call->fn->symbol));
315 void sql_select_call_implies(const char *cols, struct expression *call,
316 int (*callback)(void*, int, char**, char**))
318 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
319 return;
321 if (inlinable(call->fn)) {
322 mem_sql(callback,
323 "select %s from call_implies where call_id = '%lu';",
324 cols, (unsigned long)call);
325 return;
328 run_sql(callback, "select %s from call_implies where %s;",
329 cols, get_static_filter(call->fn->symbol));
332 void sql_select_caller_info(const char *cols, struct symbol *sym,
333 int (*callback)(void*, int, char**, char**))
335 if (__inline_fn) {
336 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
337 cols, (unsigned long)__inline_fn);
338 return;
341 run_sql(callback,
342 "select %s from caller_info where %s order by call_id;",
343 cols, get_static_filter(sym));
346 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
348 struct def_callback *def_callback = __alloc_def_callback(0);
350 def_callback->hook_type = type;
351 def_callback->callback = callback;
352 add_ptr_list(&callbacks, def_callback);
356 * These call backs are used when the --info option is turned on to print struct
357 * member information. For example foo->bar could have a state in
358 * smatch_extra.c and also check_user.c.
360 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
362 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
364 member_callback->owner = owner;
365 member_callback->callback = callback;
366 add_ptr_list(&member_callbacks, member_callback);
369 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
371 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
373 callback->callback = fn;
374 add_ptr_list(&returned_state_callbacks, callback);
377 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
379 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
381 member_callback->owner = owner;
382 member_callback->callback = callback;
383 add_ptr_list(&returned_member_callbacks, member_callback);
386 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
388 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
390 cb->type = type;
391 cb->callback = callback;
392 add_ptr_list(&call_implies_cb_list, cb);
395 static struct expression *static_call_expr;
396 static struct expression *static_returns_call;
397 static struct symbol *return_type;
398 static struct range_list *return_range_list;
399 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
401 struct range_list *rl;
402 struct expression *call_expr = static_returns_call;
404 if (argc != 1)
405 return 0;
406 call_results_to_rl(call_expr, return_type, argv[0], &rl);
407 return_range_list = rl_union(return_range_list, rl);
408 return 0;
411 struct range_list *db_return_vals(struct expression *expr)
413 static_returns_call = expr;
414 return_type = get_type(expr);
415 if (!return_type)
416 return NULL;
417 if (is_fake_call(expr))
418 return NULL;
419 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
420 return NULL;
422 return_range_list = NULL;
423 if (inlinable(expr->fn)) {
424 mem_sql(db_return_callback,
425 "select distinct return from return_states where call_id = '%lu';",
426 (unsigned long)expr);
427 } else {
428 run_sql(db_return_callback,
429 "select distinct return from return_states where %s;",
430 get_static_filter(expr->fn->symbol));
432 return return_range_list;
435 static void match_call_marker(struct expression *expr)
438 * we just want to record something in the database so that if we have
439 * two calls like: frob(4); frob(some_unkown); then on the receiving
440 * side we know that sometimes frob is called with unknown parameters.
443 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
446 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
447 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
449 struct sm_state *sm;
450 char *name;
451 struct symbol *sym;
452 int len;
453 char printed_name[256];
454 int is_address = 0;
456 expr = strip_expr(expr);
457 if (expr->type == EXPR_PREOP && expr->op == '&') {
458 expr = strip_expr(expr->unop);
459 is_address = 1;
462 name = expr_to_var_sym(expr, &sym);
463 if (!name || !sym)
464 goto free;
466 len = strlen(name);
467 FOR_EACH_SM(stree, sm) {
468 if (sm->sym != sym)
469 continue;
470 if (strcmp(name, sm->name) == 0) {
471 if (is_address)
472 snprintf(printed_name, sizeof(printed_name), "*$$");
473 else /* these are already handled. fixme: handle them here */
474 continue;
475 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
476 snprintf(printed_name, sizeof(printed_name), "*$$");
477 } else if (strncmp(name, sm->name, len) == 0) {
478 if (is_address)
479 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
480 else
481 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
482 } else {
483 continue;
485 callback(call, param, printed_name, sm->state);
486 } END_FOR_EACH_SM(sm);
487 free:
488 free_string(name);
491 static void match_call_info(struct expression *call)
493 struct member_info_callback *cb;
494 struct expression *arg;
495 struct stree *stree;
496 char *name;
497 int i;
499 name = get_fnptr_name(call->fn);
500 if (!name)
501 return;
503 FOR_EACH_PTR(member_callbacks, cb) {
504 stree = get_all_states_stree(cb->owner);
505 i = 0;
506 FOR_EACH_PTR(call->args, arg) {
507 print_struct_members(call, arg, i, stree, cb->callback);
508 i++;
509 } END_FOR_EACH_PTR(arg);
510 free_stree(&stree);
511 } END_FOR_EACH_PTR(cb);
513 free_string(name);
516 static int get_param(int param, char **name, struct symbol **sym)
518 struct symbol *arg;
519 int i;
521 i = 0;
522 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
524 * this is a temporary hack to work around a bug (I think in sparse?)
525 * 2.6.37-rc1:fs/reiserfs/journal.o
526 * If there is a function definition without parameter name found
527 * after a function implementation then it causes a crash.
528 * int foo() {}
529 * int bar(char *);
531 if (arg->ident->name < (char *)100)
532 continue;
533 if (i == param) {
534 *name = arg->ident->name;
535 *sym = arg;
536 return TRUE;
538 i++;
539 } END_FOR_EACH_PTR(arg);
541 return FALSE;
544 static struct stree *final_states;
545 static int prev_func_id = -1;
546 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
548 int func_id;
549 long type;
550 long param;
551 char *name = NULL;
552 struct symbol *sym = NULL;
553 struct def_callback *def_callback;
554 struct stree *stree;
556 if (argc != 5)
557 return 0;
559 func_id = atoi(argv[0]);
560 errno = 0;
561 type = strtol(argv[1], NULL, 10);
562 param = strtol(argv[2], NULL, 10);
563 if (errno)
564 return 0;
566 if (prev_func_id == -1)
567 prev_func_id = func_id;
568 if (func_id != prev_func_id) {
569 stree = __pop_fake_cur_stree();
570 merge_stree(&final_states, stree);
571 free_stree(&stree);
572 __push_fake_cur_stree();
573 __unnullify_path();
574 prev_func_id = func_id;
577 if (type == INTERNAL)
578 return 0;
579 if (param >= 0 && !get_param(param, &name, &sym))
580 return 0;
582 FOR_EACH_PTR(callbacks, def_callback) {
583 if (def_callback->hook_type == type)
584 def_callback->callback(name, sym, argv[3], argv[4]);
585 } END_FOR_EACH_PTR(def_callback);
587 return 0;
590 static void get_direct_callers(struct symbol *sym)
592 sql_select_caller_info("call_id, type, parameter, key, value", sym,
593 caller_info_callback);
596 static struct string_list *ptr_names_done;
597 static struct string_list *ptr_names;
599 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
601 insert_string(&ptr_names, alloc_string(argv[0]));
602 return 0;
605 static char *get_next_ptr_name(void)
607 char *ptr;
609 FOR_EACH_PTR(ptr_names, ptr) {
610 if (list_has_string(ptr_names_done, ptr))
611 continue;
612 insert_string(&ptr_names_done, ptr);
613 return ptr;
614 } END_FOR_EACH_PTR(ptr);
615 return NULL;
618 static void get_ptr_names(const char *file, const char *name)
620 char sql_filter[1024];
621 int before, after;
623 if (file) {
624 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
625 file, name);
626 } else {
627 snprintf(sql_filter, 1024, "function = '%s';", name);
630 before = ptr_list_size((struct ptr_list *)ptr_names);
632 run_sql(get_ptr_name,
633 "select distinct ptr from function_ptr where %s",
634 sql_filter);
636 after = ptr_list_size((struct ptr_list *)ptr_names);
637 if (before == after)
638 return;
640 while ((name = get_next_ptr_name()))
641 get_ptr_names(NULL, name);
644 static void match_data_from_db(struct symbol *sym)
646 struct sm_state *sm;
647 struct stree *stree;
649 if (!sym || !sym->ident)
650 return;
652 __push_fake_cur_stree();
653 __unnullify_path();
654 prev_func_id = -1;
656 if (!__inline_fn) {
657 char *ptr;
659 if (sym->ctype.modifiers & MOD_STATIC)
660 get_ptr_names(get_base_file(), sym->ident->name);
661 else
662 get_ptr_names(NULL, sym->ident->name);
664 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
665 __free_ptr_list((struct ptr_list **)&ptr_names);
666 __free_ptr_list((struct ptr_list **)&ptr_names_done);
667 stree = __pop_fake_cur_stree();
668 free_stree(&stree);
669 return;
672 get_direct_callers(sym);
674 FOR_EACH_PTR(ptr_names, ptr) {
675 run_sql(caller_info_callback, "select call_id, type, parameter, key, value"
676 " from caller_info where function = '%s' order by call_id",
677 ptr);
678 free_string(ptr);
679 } END_FOR_EACH_PTR(ptr);
681 __free_ptr_list((struct ptr_list **)&ptr_names);
682 __free_ptr_list((struct ptr_list **)&ptr_names_done);
683 } else {
684 get_direct_callers(sym);
687 stree = __pop_fake_cur_stree();
688 merge_stree(&final_states, stree);
689 free_stree(&stree);
691 FOR_EACH_SM(final_states, sm) {
692 __set_sm(sm);
693 } END_FOR_EACH_SM(sm);
695 free_stree(&final_states);
698 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
700 struct expression *call_expr = static_call_expr;
701 struct call_implies_callback *cb;
702 struct expression *arg = NULL;
703 int type;
704 int param;
706 if (argc != 5)
707 return 0;
709 type = atoi(argv[1]);
710 param = atoi(argv[2]);
712 FOR_EACH_PTR(call_implies_cb_list, cb) {
713 if (cb->type != type)
714 continue;
715 if (param != -1) {
716 arg = get_argument_from_call_expr(call_expr->args, param);
717 if (!arg)
718 continue;
720 cb->callback(arg, argv[3], argv[4]);
721 } END_FOR_EACH_PTR(cb);
723 return 0;
726 static void match_call_implies(struct expression *expr)
728 static_call_expr = expr;
729 sql_select_call_implies("function, type, parameter, key, value", expr,
730 call_implies_callbacks);
731 return;
734 static void print_initializer_list(struct expression_list *expr_list,
735 struct symbol *struct_type)
737 struct expression *expr;
738 struct symbol *base_type;
739 char struct_name[256];
741 FOR_EACH_PTR(expr_list, expr) {
742 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
743 print_initializer_list(expr->idx_expression->expr_list, struct_type);
744 continue;
746 if (expr->type != EXPR_IDENTIFIER)
747 continue;
748 if (!expr->expr_ident)
749 continue;
750 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
751 continue;
752 base_type = get_type(expr->ident_expression);
753 if (!base_type || base_type->type != SYM_FN)
754 continue;
755 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
756 struct_type->ident->name, expr->expr_ident->name);
757 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
758 struct_name);
759 } END_FOR_EACH_PTR(expr);
762 static void global_variable(struct symbol *sym)
764 struct symbol *struct_type;
766 if (!sym->ident)
767 return;
768 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
769 return;
770 struct_type = get_base_type(sym);
771 if (!struct_type)
772 return;
773 if (struct_type->type == SYM_ARRAY) {
774 struct_type = get_base_type(struct_type);
775 if (!struct_type)
776 return;
778 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
779 return;
780 print_initializer_list(sym->initializer->expr_list, struct_type);
783 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
785 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
788 static void call_return_state_hooks_conditional(struct expression *expr)
790 struct returned_state_callback *cb;
791 struct range_list *rl;
792 char *return_ranges;
793 int final_pass_orig = final_pass;
795 __push_fake_cur_stree();
797 final_pass = 0;
798 __split_whole_condition(expr->conditional);
799 final_pass = final_pass_orig;
801 if (get_implied_rl(expr->cond_true, &rl))
802 rl = cast_rl(cur_func_return_type(), rl);
803 else
804 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
805 return_ranges = show_rl(rl);
807 return_id++;
808 FOR_EACH_PTR(returned_state_callbacks, cb) {
809 cb->callback(return_id, return_ranges, expr);
810 } END_FOR_EACH_PTR(cb);
812 __push_true_states();
813 __use_false_states();
815 if (get_implied_rl(expr->cond_false, &rl))
816 rl = cast_rl(cur_func_return_type(), rl);
817 else
818 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
819 return_ranges = show_rl(rl);
821 return_id++;
822 FOR_EACH_PTR(returned_state_callbacks, cb) {
823 cb->callback(return_id, return_ranges, expr);
824 } END_FOR_EACH_PTR(cb);
826 __merge_true_states();
827 __free_fake_cur_stree();
830 static void call_return_state_hooks_compare(struct expression *expr)
832 struct returned_state_callback *cb;
833 char *return_ranges;
834 int final_pass_orig = final_pass;
836 __push_fake_cur_stree();
838 final_pass = 0;
839 __split_whole_condition(expr);
840 final_pass = final_pass_orig;
842 return_ranges = alloc_sname("1");
844 return_id++;
845 FOR_EACH_PTR(returned_state_callbacks, cb) {
846 cb->callback(return_id, return_ranges, expr);
847 } END_FOR_EACH_PTR(cb);
849 __push_true_states();
850 __use_false_states();
852 return_ranges = alloc_sname("0");;
853 return_id++;
854 FOR_EACH_PTR(returned_state_callbacks, cb) {
855 cb->callback(return_id, return_ranges, expr);
856 } END_FOR_EACH_PTR(cb);
858 __merge_true_states();
859 __free_fake_cur_stree();
862 static int call_return_state_hooks_split_possible(struct expression *expr)
864 struct returned_state_callback *cb;
865 struct range_list *rl;
866 char *return_ranges;
867 struct sm_state *sm;
868 struct sm_state *tmp;
869 int ret = 0;
870 int nr_possible, nr_states;
871 char *compare_str;
872 char buf[128];
874 if (!expr || expr_equal_to_param(expr, -1))
875 return 0;
877 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
878 if (!sm || !sm->merged)
879 return 0;
881 if (too_many_possible(sm))
882 return 0;
884 /* bail if it gets too complicated */
885 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
886 nr_states = stree_count(__get_cur_stree());
888 * the main thing option_info because we don't want to print a
889 * million lines of output. If someone else, like check_locking.c
890 * wants this data, then it doesn't cause a slow down to provide it.
892 if (option_info && nr_states * nr_possible >= 2000)
893 return 0;
895 compare_str = expr_lte_to_param(expr, -1);
897 FOR_EACH_PTR(sm->possible, tmp) {
898 if (tmp->merged)
899 continue;
901 ret = 1;
902 __push_fake_cur_stree();
904 overwrite_states_using_pool(tmp);
906 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
907 return_ranges = show_rl(rl);
908 if (compare_str) {
909 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
910 return_ranges = alloc_sname(buf);
913 return_id++;
914 FOR_EACH_PTR(returned_state_callbacks, cb) {
915 cb->callback(return_id, return_ranges, expr);
916 } END_FOR_EACH_PTR(cb);
918 __free_fake_cur_stree();
919 } END_FOR_EACH_PTR(tmp);
921 return ret;
924 static char *get_return_ranges_str(struct expression *expr)
926 struct range_list *rl;
927 char *return_ranges;
928 char *compare_str;
929 char buf[128];
931 if (!expr)
932 return alloc_sname("");
933 compare_str = expr_equal_to_param(expr, -1);
934 if (compare_str)
935 return compare_str;
937 compare_str = get_value_in_terms_of_parameter_math(expr);
938 if (compare_str)
939 return compare_str;
941 if (get_implied_rl(expr, &rl)) {
942 rl = cast_rl(cur_func_return_type(), rl);
943 return_ranges = show_rl(rl);
944 } else {
945 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
946 return_ranges = show_rl(rl);
948 compare_str = expr_lte_to_param(expr, -1);
949 if (compare_str) {
950 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
951 return alloc_sname(buf);
953 return return_ranges;
956 static int is_boolean(struct expression *expr)
958 struct range_list *rl;
960 if (!get_implied_rl(expr, &rl))
961 return 0;
962 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
963 return 1;
964 return 0;
967 static int is_conditional(struct expression *expr)
969 if (!expr)
970 return 0;
971 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
972 return 1;
973 return 0;
976 static void call_return_state_hooks(struct expression *expr)
978 struct returned_state_callback *cb;
979 char *return_ranges;
980 int nr_states;
982 expr = strip_expr(expr);
984 if (is_condition(expr) || is_boolean(expr)) {
985 call_return_state_hooks_compare(expr);
986 return;
987 } else if (is_conditional(expr)) {
988 call_return_state_hooks_conditional(expr);
989 return;
990 } else if (call_return_state_hooks_split_possible(expr)) {
991 return;
994 return_ranges = get_return_ranges_str(expr);
996 return_id++;
997 nr_states = stree_count(__get_cur_stree());
998 if (nr_states >= 10000) {
999 match_return_info(return_id, return_ranges, expr);
1000 return;
1002 FOR_EACH_PTR(returned_state_callbacks, cb) {
1003 cb->callback(return_id, return_ranges, expr);
1004 } END_FOR_EACH_PTR(cb);
1007 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1009 struct returned_member_callback *cb;
1010 struct stree *stree;
1011 struct sm_state *sm;
1012 struct symbol *type;
1013 char *name;
1014 char member_name[256];
1015 int len;
1017 type = get_type(expr);
1018 if (!type || type->type != SYM_PTR)
1019 return;
1020 name = expr_to_var(expr);
1021 if (!name)
1022 return;
1024 member_name[sizeof(member_name) - 1] = '\0';
1025 strcpy(member_name, "$$");
1027 len = strlen(name);
1028 FOR_EACH_PTR(returned_member_callbacks, cb) {
1029 stree = __get_cur_stree();
1030 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1031 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1032 strcpy(member_name, "*$$");
1033 cb->callback(return_id, return_ranges, member_name, sm->state);
1034 continue;
1036 if (strncmp(sm->name, name, len) != 0)
1037 continue;
1038 if (strncmp(sm->name + len, "->", 2) != 0)
1039 continue;
1040 strcpy(member_name, "$$");
1041 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
1042 cb->callback(return_id, return_ranges, member_name, sm->state);
1043 } END_FOR_EACH_SM(sm);
1044 } END_FOR_EACH_PTR(cb);
1046 free_string(name);
1049 static void reset_memdb(void)
1051 mem_sql(NULL, "delete from caller_info;");
1052 mem_sql(NULL, "delete from return_states;");
1053 mem_sql(NULL, "delete from call_implies;");
1056 static void match_end_func_info(struct symbol *sym)
1058 if (__path_is_null())
1059 return;
1060 call_return_state_hooks(NULL);
1061 if (!__inline_fn)
1062 reset_memdb();
1065 static void init_memdb(void)
1067 char *err = NULL;
1068 int rc;
1069 const char *schema_files[] = {
1070 "db/db.schema",
1071 "db/caller_info.schema",
1072 "db/return_states.schema",
1073 "db/function_type_size.schema",
1074 "db/type_size.schema",
1075 "db/call_implies.schema",
1076 "db/function_ptr.schema",
1077 "db/local_values.schema",
1078 "db/function_type_value.schema",
1079 "db/type_value.schema",
1080 "db/function_type_info.schema",
1082 static char buf[4096];
1083 int fd;
1084 int ret;
1085 int i;
1087 rc = sqlite3_open(":memory:", &mem_db);
1088 if (rc != SQLITE_OK) {
1089 printf("Error starting In-Memory database.");
1090 return;
1093 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1094 fd = open_data_file(schema_files[i]);
1095 if (fd < 0) {
1096 mem_db = NULL;
1097 return;
1099 ret = read(fd, buf, sizeof(buf));
1100 if (ret == sizeof(buf)) {
1101 printf("Schema file too large: %s (limit %zd bytes)",
1102 schema_files[i], sizeof(buf));
1104 buf[ret] = '\0';
1105 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1106 if (rc != SQLITE_OK) {
1107 fprintf(stderr, "SQL error #2: %s\n", err);
1108 fprintf(stderr, "%s\n", buf);
1113 void open_smatch_db(void)
1115 int rc;
1117 if (option_no_db)
1118 return;
1120 init_memdb();
1122 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1123 if (rc != SQLITE_OK) {
1124 option_no_db = 1;
1125 return;
1127 return;
1130 static void register_common_funcs(void)
1132 struct token *token;
1133 char *func;
1134 char filename[256];
1136 if (option_project == PROJ_NONE)
1137 strcpy(filename, "common_functions");
1138 else
1139 snprintf(filename, 256, "%s.common_functions", option_project_str);
1141 token = get_tokens_file(filename);
1142 if (!token)
1143 return;
1144 if (token_type(token) != TOKEN_STREAMBEGIN)
1145 return;
1146 token = token->next;
1147 while (token_type(token) != TOKEN_STREAMEND) {
1148 if (token_type(token) != TOKEN_IDENT)
1149 return;
1150 func = alloc_string(show_ident(token->ident));
1151 add_ptr_list(&common_funcs, func);
1152 token = token->next;
1154 clear_token_alloc();
1158 void register_definition_db_callbacks(int id)
1160 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1161 add_hook(&global_variable, BASE_HOOK);
1162 add_hook(&global_variable, DECLARATION_HOOK);
1163 add_split_return_callback(match_return_info);
1164 add_split_return_callback(print_returned_struct_members);
1165 add_hook(&call_return_state_hooks, RETURN_HOOK);
1166 add_hook(&match_end_func_info, END_FUNC_HOOK);
1168 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1169 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1171 register_common_funcs();
1174 void register_db_call_marker(int id)
1176 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1179 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1181 struct expression *arg;
1182 char *name = NULL;
1183 char member_name[256];
1185 *sym = NULL;
1187 if (param == -1) {
1188 const char *star = "";
1190 if (expr->type != EXPR_ASSIGNMENT)
1191 return NULL;
1192 name = expr_to_var_sym(expr->left, sym);
1193 if (!name)
1194 return NULL;
1195 if (key[0] == '*') {
1196 star = "*";
1197 key++;
1199 if (strncmp(key, "$$", 2) != 0)
1200 return name;
1201 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1202 free_string(name);
1203 return alloc_string(member_name);
1206 while (expr->type == EXPR_ASSIGNMENT)
1207 expr = strip_expr(expr->right);
1208 if (expr->type != EXPR_CALL)
1209 return NULL;
1211 arg = get_argument_from_call_expr(expr->args, param);
1212 if (!arg)
1213 return NULL;
1215 return get_variable_from_key(arg, key, sym);
1218 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1220 char buf[256];
1221 char *tmp;
1223 if (strcmp(key, "$$") == 0)
1224 return expr_to_var_sym(arg, sym);
1226 if (strcmp(key, "*$$") == 0) {
1227 if (arg->type == EXPR_PREOP && arg->op == '&') {
1228 arg = strip_expr(arg->unop);
1229 return expr_to_var_sym(arg, sym);
1230 } else {
1231 tmp = expr_to_var_sym(arg, sym);
1232 if (!tmp)
1233 return NULL;
1234 snprintf(buf, sizeof(buf), "*%s", tmp);
1235 free_string(tmp);
1236 return alloc_string(buf);
1240 if (arg->type == EXPR_PREOP && arg->op == '&') {
1241 arg = strip_expr(arg->unop);
1242 tmp = expr_to_var_sym(arg, sym);
1243 if (!tmp)
1244 return NULL;
1245 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1246 return alloc_string(buf);
1249 tmp = expr_to_var_sym(arg, sym);
1250 if (!tmp)
1251 return NULL;
1252 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1253 free_string(tmp);
1254 return alloc_string(buf);
1257 const char *get_param_name(struct sm_state *sm)
1259 char *param_name;
1260 int name_len;
1261 static char buf[256];
1263 if (!sm->sym->ident)
1264 return NULL;
1266 param_name = sm->sym->ident->name;
1267 name_len = strlen(param_name);
1269 if (strcmp(sm->name, param_name) == 0) {
1270 return "$$";
1271 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1272 strncmp(sm->name, param_name, name_len) == 0) {
1273 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1274 return buf;
1275 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1276 return "*$$";
1278 return NULL;