free: check for passing or returning freed memory
[smatch.git] / smatch_db.c
blob3b40a65c4574cdc788b63a8bc15311088bd79c1c
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, struct expression *expr, char *printed_name, struct smatch_state *state);
86 ALLOCATOR(returned_member_callback, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
88 static struct returned_member_cb_list *returned_member_callbacks;
90 struct call_implies_callback {
91 int type;
92 void (*callback)(struct expression *arg, char *key, char *value);
94 ALLOCATOR(call_implies_callback, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
96 static struct call_implies_cb_list *call_implies_cb_list;
98 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
100 int i;
102 for (i = 0; i < argc; i++) {
103 if (i != 0)
104 printf(", ");
105 sm_printf("%s", argv[i]);
107 sm_printf("\n");
108 return 0;
111 void debug_sql(const char *sql)
113 if (!option_debug)
114 return;
115 sm_msg("%s", sql);
116 sql_exec(print_sql_output, NULL, sql);
120 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, 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, data, &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**), void *data, const char *sql)
137 char *err = NULL;
138 int rc;
140 if (!mem_db)
141 return;
143 rc = sqlite3_exec(mem_db, sql, callback, data, &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, 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 void sql_insert_data_info(struct expression *data, int type, const char *value)
248 char *data_name;
250 data_name = get_member_name(data);
251 if (!data_name)
252 return;
253 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
256 static char *get_static_filter(struct symbol *sym)
258 static char sql_filter[1024];
260 if (sym->ctype.modifiers & MOD_STATIC) {
261 snprintf(sql_filter, sizeof(sql_filter),
262 "file = '%s' and function = '%s' and static = '1'",
263 get_base_file(), sym->ident->name);
264 } else {
265 snprintf(sql_filter, sizeof(sql_filter),
266 "function = '%s' and static = '0'", sym->ident->name);
269 return sql_filter;
272 static int row_count;
273 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
275 if (argc != 1)
276 return 0;
277 row_count = atoi(argv[0]);
278 return 0;
281 static void sql_select_return_states_pointer(const char *cols,
282 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
284 char *ptr;
286 ptr = get_fnptr_name(call->fn);
287 if (!ptr)
288 return;
290 run_sql(callback, info,
291 "select %s from return_states join function_ptr where "
292 "return_states.function == function_ptr.function and ptr = '%s'"
293 "and searchable = 1 order by return_id, type;",
294 cols, ptr);
297 void sql_select_return_states(const char *cols, struct expression *call,
298 int (*callback)(void*, int, char**, char**), void *info)
300 if (is_fake_call(call))
301 return;
303 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
304 sql_select_return_states_pointer(cols, call, callback, info);
305 return;
308 if (inlinable(call->fn)) {
309 mem_sql(callback, info,
310 "select %s from return_states where call_id = '%lu' order by return_id, type;",
311 cols, (unsigned long)call);
312 return;
315 row_count = 0;
316 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
317 get_static_filter(call->fn->symbol));
318 if (row_count > 1000)
319 return;
321 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
322 cols, get_static_filter(call->fn->symbol));
325 void sql_select_call_implies(const char *cols, struct expression *call,
326 int (*callback)(void*, int, char**, char**))
328 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
329 return;
331 if (inlinable(call->fn)) {
332 mem_sql(callback, NULL,
333 "select %s from call_implies where call_id = '%lu';",
334 cols, (unsigned long)call);
335 return;
338 run_sql(callback, NULL, "select %s from call_implies where %s;",
339 cols, get_static_filter(call->fn->symbol));
342 void sql_select_caller_info(const char *cols, struct symbol *sym,
343 int (*callback)(void*, int, char**, char**))
345 if (__inline_fn) {
346 mem_sql(callback, NULL,
347 "select %s from caller_info where call_id = %lu;",
348 cols, (unsigned long)__inline_fn);
349 return;
352 run_sql(callback, NULL,
353 "select %s from caller_info where %s order by call_id;",
354 cols, get_static_filter(sym));
357 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
359 struct def_callback *def_callback = __alloc_def_callback(0);
361 def_callback->hook_type = type;
362 def_callback->callback = callback;
363 add_ptr_list(&callbacks, def_callback);
367 * These call backs are used when the --info option is turned on to print struct
368 * member information. For example foo->bar could have a state in
369 * smatch_extra.c and also check_user.c.
371 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
373 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
375 member_callback->owner = owner;
376 member_callback->callback = callback;
377 add_ptr_list(&member_callbacks, member_callback);
380 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
382 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
384 callback->callback = fn;
385 add_ptr_list(&returned_state_callbacks, callback);
388 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
390 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
392 member_callback->owner = owner;
393 member_callback->callback = callback;
394 add_ptr_list(&returned_member_callbacks, member_callback);
397 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
399 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
401 cb->type = type;
402 cb->callback = callback;
403 add_ptr_list(&call_implies_cb_list, cb);
406 static struct expression *static_call_expr;
407 static struct expression *static_returns_call;
408 static struct symbol *return_type;
409 static struct range_list *return_range_list;
410 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
412 struct range_list *rl;
413 struct expression *call_expr = static_returns_call;
415 if (argc != 1)
416 return 0;
417 call_results_to_rl(call_expr, return_type, argv[0], &rl);
418 return_range_list = rl_union(return_range_list, rl);
419 return 0;
422 struct range_list *db_return_vals(struct expression *expr)
424 static_returns_call = expr;
425 return_type = get_type(expr);
426 if (!return_type)
427 return NULL;
428 if (is_fake_call(expr))
429 return NULL;
430 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
431 return NULL;
433 return_range_list = NULL;
434 if (inlinable(expr->fn)) {
435 mem_sql(db_return_callback, NULL,
436 "select distinct return from return_states where call_id = '%lu';",
437 (unsigned long)expr);
438 } else {
439 run_sql(db_return_callback, NULL,
440 "select distinct return from return_states where %s;",
441 get_static_filter(expr->fn->symbol));
443 return return_range_list;
446 static void match_call_marker(struct expression *expr)
449 * we just want to record something in the database so that if we have
450 * two calls like: frob(4); frob(some_unkown); then on the receiving
451 * side we know that sometimes frob is called with unknown parameters.
454 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
457 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
458 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
460 struct sm_state *sm;
461 char *name;
462 struct symbol *sym;
463 int len;
464 char printed_name[256];
465 int is_address = 0;
467 expr = strip_expr(expr);
468 if (expr->type == EXPR_PREOP && expr->op == '&') {
469 expr = strip_expr(expr->unop);
470 is_address = 1;
473 name = expr_to_var_sym(expr, &sym);
474 if (!name || !sym)
475 goto free;
477 len = strlen(name);
478 FOR_EACH_SM(stree, sm) {
479 if (sm->sym != sym)
480 continue;
481 if (strcmp(name, sm->name) == 0) {
482 if (is_address)
483 snprintf(printed_name, sizeof(printed_name), "*$$");
484 else /* these are already handled. fixme: handle them here */
485 continue;
486 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
487 snprintf(printed_name, sizeof(printed_name), "*$$");
488 } else if (strncmp(name, sm->name, len) == 0) {
489 if (is_address)
490 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
491 else
492 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
493 } else {
494 continue;
496 callback(call, param, printed_name, sm->state);
497 } END_FOR_EACH_SM(sm);
498 free:
499 free_string(name);
502 static void match_call_info(struct expression *call)
504 struct member_info_callback *cb;
505 struct expression *arg;
506 struct stree *stree;
507 char *name;
508 int i;
510 name = get_fnptr_name(call->fn);
511 if (!name)
512 return;
514 FOR_EACH_PTR(member_callbacks, cb) {
515 stree = get_all_states_stree(cb->owner);
516 i = 0;
517 FOR_EACH_PTR(call->args, arg) {
518 print_struct_members(call, arg, i, stree, cb->callback);
519 i++;
520 } END_FOR_EACH_PTR(arg);
521 free_stree(&stree);
522 } END_FOR_EACH_PTR(cb);
524 free_string(name);
527 static int get_param(int param, char **name, struct symbol **sym)
529 struct symbol *arg;
530 int i;
532 i = 0;
533 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
535 * this is a temporary hack to work around a bug (I think in sparse?)
536 * 2.6.37-rc1:fs/reiserfs/journal.o
537 * If there is a function definition without parameter name found
538 * after a function implementation then it causes a crash.
539 * int foo() {}
540 * int bar(char *);
542 if (arg->ident->name < (char *)100)
543 continue;
544 if (i == param) {
545 *name = arg->ident->name;
546 *sym = arg;
547 return TRUE;
549 i++;
550 } END_FOR_EACH_PTR(arg);
552 return FALSE;
555 static struct stree *final_states;
556 static int prev_func_id = -1;
557 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
559 int func_id;
560 long type;
561 long param;
562 char *name = NULL;
563 struct symbol *sym = NULL;
564 struct def_callback *def_callback;
565 struct stree *stree;
567 if (argc != 5)
568 return 0;
570 func_id = atoi(argv[0]);
571 errno = 0;
572 type = strtol(argv[1], NULL, 10);
573 param = strtol(argv[2], NULL, 10);
574 if (errno)
575 return 0;
577 if (prev_func_id == -1)
578 prev_func_id = func_id;
579 if (func_id != prev_func_id) {
580 stree = __pop_fake_cur_stree();
581 merge_stree(&final_states, stree);
582 free_stree(&stree);
583 __push_fake_cur_stree();
584 __unnullify_path();
585 prev_func_id = func_id;
588 if (type == INTERNAL)
589 return 0;
590 if (param >= 0 && !get_param(param, &name, &sym))
591 return 0;
593 FOR_EACH_PTR(callbacks, def_callback) {
594 if (def_callback->hook_type == type)
595 def_callback->callback(name, sym, argv[3], argv[4]);
596 } END_FOR_EACH_PTR(def_callback);
598 return 0;
601 static void get_direct_callers(struct symbol *sym)
603 sql_select_caller_info("call_id, type, parameter, key, value", sym,
604 caller_info_callback);
607 static struct string_list *ptr_names_done;
608 static struct string_list *ptr_names;
610 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
612 insert_string(&ptr_names, alloc_string(argv[0]));
613 return 0;
616 static char *get_next_ptr_name(void)
618 char *ptr;
620 FOR_EACH_PTR(ptr_names, ptr) {
621 if (list_has_string(ptr_names_done, ptr))
622 continue;
623 insert_string(&ptr_names_done, ptr);
624 return ptr;
625 } END_FOR_EACH_PTR(ptr);
626 return NULL;
629 static void get_ptr_names(const char *file, const char *name)
631 char sql_filter[1024];
632 int before, after;
634 if (file) {
635 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
636 file, name);
637 } else {
638 snprintf(sql_filter, 1024, "function = '%s';", name);
641 before = ptr_list_size((struct ptr_list *)ptr_names);
643 run_sql(get_ptr_name, NULL,
644 "select distinct ptr from function_ptr where %s",
645 sql_filter);
647 after = ptr_list_size((struct ptr_list *)ptr_names);
648 if (before == after)
649 return;
651 while ((name = get_next_ptr_name()))
652 get_ptr_names(NULL, name);
655 static void match_data_from_db(struct symbol *sym)
657 struct sm_state *sm;
658 struct stree *stree;
660 if (!sym || !sym->ident)
661 return;
663 __push_fake_cur_stree();
664 __unnullify_path();
665 prev_func_id = -1;
667 if (!__inline_fn) {
668 char *ptr;
670 if (sym->ctype.modifiers & MOD_STATIC)
671 get_ptr_names(get_base_file(), sym->ident->name);
672 else
673 get_ptr_names(NULL, sym->ident->name);
675 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
676 __free_ptr_list((struct ptr_list **)&ptr_names);
677 __free_ptr_list((struct ptr_list **)&ptr_names_done);
678 stree = __pop_fake_cur_stree();
679 free_stree(&stree);
680 return;
683 get_direct_callers(sym);
685 FOR_EACH_PTR(ptr_names, ptr) {
686 run_sql(caller_info_callback, NULL,
687 "select call_id, type, parameter, key, value"
688 " from caller_info where function = '%s' order by call_id",
689 ptr);
690 free_string(ptr);
691 } END_FOR_EACH_PTR(ptr);
693 __free_ptr_list((struct ptr_list **)&ptr_names);
694 __free_ptr_list((struct ptr_list **)&ptr_names_done);
695 } else {
696 get_direct_callers(sym);
699 stree = __pop_fake_cur_stree();
700 merge_stree(&final_states, stree);
701 free_stree(&stree);
703 FOR_EACH_SM(final_states, sm) {
704 __set_sm(sm);
705 } END_FOR_EACH_SM(sm);
707 free_stree(&final_states);
710 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
712 struct expression *call_expr = static_call_expr;
713 struct call_implies_callback *cb;
714 struct expression *arg = NULL;
715 int type;
716 int param;
718 if (argc != 5)
719 return 0;
721 type = atoi(argv[1]);
722 param = atoi(argv[2]);
724 FOR_EACH_PTR(call_implies_cb_list, cb) {
725 if (cb->type != type)
726 continue;
727 if (param != -1) {
728 arg = get_argument_from_call_expr(call_expr->args, param);
729 if (!arg)
730 continue;
732 cb->callback(arg, argv[3], argv[4]);
733 } END_FOR_EACH_PTR(cb);
735 return 0;
738 static void match_call_implies(struct expression *expr)
740 static_call_expr = expr;
741 sql_select_call_implies("function, type, parameter, key, value", expr,
742 call_implies_callbacks);
743 return;
746 static void print_initializer_list(struct expression_list *expr_list,
747 struct symbol *struct_type)
749 struct expression *expr;
750 struct symbol *base_type;
751 char struct_name[256];
753 FOR_EACH_PTR(expr_list, expr) {
754 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
755 print_initializer_list(expr->idx_expression->expr_list, struct_type);
756 continue;
758 if (expr->type != EXPR_IDENTIFIER)
759 continue;
760 if (!expr->expr_ident)
761 continue;
762 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
763 continue;
764 base_type = get_type(expr->ident_expression);
765 if (!base_type || base_type->type != SYM_FN)
766 continue;
767 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
768 struct_type->ident->name, expr->expr_ident->name);
769 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
770 struct_name);
771 } END_FOR_EACH_PTR(expr);
774 static void global_variable(struct symbol *sym)
776 struct symbol *struct_type;
778 if (!sym->ident)
779 return;
780 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
781 return;
782 struct_type = get_base_type(sym);
783 if (!struct_type)
784 return;
785 if (struct_type->type == SYM_ARRAY) {
786 struct_type = get_base_type(struct_type);
787 if (!struct_type)
788 return;
790 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
791 return;
792 print_initializer_list(sym->initializer->expr_list, struct_type);
795 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
797 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
800 static void call_return_state_hooks_conditional(struct expression *expr)
802 struct returned_state_callback *cb;
803 struct range_list *rl;
804 char *return_ranges;
805 int final_pass_orig = final_pass;
807 __push_fake_cur_stree();
809 final_pass = 0;
810 __split_whole_condition(expr->conditional);
811 final_pass = final_pass_orig;
813 if (get_implied_rl(expr->cond_true, &rl))
814 rl = cast_rl(cur_func_return_type(), rl);
815 else
816 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
817 return_ranges = show_rl(rl);
819 return_id++;
820 FOR_EACH_PTR(returned_state_callbacks, cb) {
821 cb->callback(return_id, return_ranges, expr);
822 } END_FOR_EACH_PTR(cb);
824 __push_true_states();
825 __use_false_states();
827 if (get_implied_rl(expr->cond_false, &rl))
828 rl = cast_rl(cur_func_return_type(), rl);
829 else
830 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
831 return_ranges = show_rl(rl);
833 return_id++;
834 FOR_EACH_PTR(returned_state_callbacks, cb) {
835 cb->callback(return_id, return_ranges, expr);
836 } END_FOR_EACH_PTR(cb);
838 __merge_true_states();
839 __free_fake_cur_stree();
842 static void call_return_state_hooks_compare(struct expression *expr)
844 struct returned_state_callback *cb;
845 char *return_ranges;
846 int final_pass_orig = final_pass;
848 __push_fake_cur_stree();
850 final_pass = 0;
851 __split_whole_condition(expr);
852 final_pass = final_pass_orig;
854 return_ranges = alloc_sname("1");
856 return_id++;
857 FOR_EACH_PTR(returned_state_callbacks, cb) {
858 cb->callback(return_id, return_ranges, expr);
859 } END_FOR_EACH_PTR(cb);
861 __push_true_states();
862 __use_false_states();
864 return_ranges = alloc_sname("0");;
865 return_id++;
866 FOR_EACH_PTR(returned_state_callbacks, cb) {
867 cb->callback(return_id, return_ranges, expr);
868 } END_FOR_EACH_PTR(cb);
870 __merge_true_states();
871 __free_fake_cur_stree();
874 static int call_return_state_hooks_split_possible(struct expression *expr)
876 struct returned_state_callback *cb;
877 struct range_list *rl;
878 char *return_ranges;
879 struct sm_state *sm;
880 struct sm_state *tmp;
881 int ret = 0;
882 int nr_possible, nr_states;
883 char *compare_str;
884 char buf[128];
886 if (!expr || expr_equal_to_param(expr, -1))
887 return 0;
889 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
890 if (!sm || !sm->merged)
891 return 0;
893 if (too_many_possible(sm))
894 return 0;
896 /* bail if it gets too complicated */
897 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
898 nr_states = stree_count(__get_cur_stree());
900 * the main thing option_info because we don't want to print a
901 * million lines of output. If someone else, like check_locking.c
902 * wants this data, then it doesn't cause a slow down to provide it.
904 if (option_info && nr_states * nr_possible >= 2000)
905 return 0;
907 compare_str = expr_lte_to_param(expr, -1);
909 FOR_EACH_PTR(sm->possible, tmp) {
910 if (tmp->merged)
911 continue;
913 ret = 1;
914 __push_fake_cur_stree();
916 overwrite_states_using_pool(tmp);
918 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
919 return_ranges = show_rl(rl);
920 if (compare_str) {
921 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
922 return_ranges = alloc_sname(buf);
925 return_id++;
926 FOR_EACH_PTR(returned_state_callbacks, cb) {
927 cb->callback(return_id, return_ranges, expr);
928 } END_FOR_EACH_PTR(cb);
930 __free_fake_cur_stree();
931 } END_FOR_EACH_PTR(tmp);
933 return ret;
936 static const char *get_return_ranges_str(struct expression *expr)
938 struct range_list *rl;
939 char *return_ranges;
940 sval_t sval;
941 char *compare_str;
942 char *math_str;
943 char buf[128];
945 if (!expr)
946 return alloc_sname("");
948 if (get_implied_value(expr, &sval))
949 return sval_to_str(sval);
951 compare_str = expr_equal_to_param(expr, -1);
952 math_str = get_value_in_terms_of_parameter_math(expr);
954 if (get_implied_rl(expr, &rl)) {
955 rl = cast_rl(cur_func_return_type(), rl);
956 return_ranges = show_rl(rl);
957 } else {
958 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
959 return_ranges = show_rl(rl);
962 if (compare_str) {
963 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
964 return alloc_sname(buf);
967 if (math_str) {
968 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
969 return alloc_sname(buf);
972 compare_str = expr_lte_to_param(expr, -1);
973 if (compare_str) {
974 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
975 return alloc_sname(buf);
977 return return_ranges;
980 static int is_boolean(struct expression *expr)
982 struct range_list *rl;
984 if (!get_implied_rl(expr, &rl))
985 return 0;
986 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
987 return 1;
988 return 0;
991 static int is_conditional(struct expression *expr)
993 if (!expr)
994 return 0;
995 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
996 return 1;
997 return 0;
1000 static void call_return_state_hooks(struct expression *expr)
1002 struct returned_state_callback *cb;
1003 const char *return_ranges;
1004 int nr_states;
1006 expr = strip_expr(expr);
1008 if (is_condition(expr) || is_boolean(expr)) {
1009 call_return_state_hooks_compare(expr);
1010 return;
1011 } else if (is_conditional(expr)) {
1012 call_return_state_hooks_conditional(expr);
1013 return;
1014 } else if (call_return_state_hooks_split_possible(expr)) {
1015 return;
1018 return_ranges = get_return_ranges_str(expr);
1020 return_id++;
1021 nr_states = stree_count(__get_cur_stree());
1022 if (nr_states >= 10000) {
1023 match_return_info(return_id, (char *)return_ranges, expr);
1024 return;
1026 FOR_EACH_PTR(returned_state_callbacks, cb) {
1027 cb->callback(return_id, (char *)return_ranges, expr);
1028 } END_FOR_EACH_PTR(cb);
1031 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1033 struct returned_member_callback *cb;
1034 struct stree *stree;
1035 struct sm_state *sm;
1036 struct symbol *type;
1037 char *name;
1038 char member_name[256];
1039 int len;
1041 type = get_type(expr);
1042 if (!type || type->type != SYM_PTR)
1043 return;
1044 name = expr_to_var(expr);
1045 if (!name)
1046 return;
1048 member_name[sizeof(member_name) - 1] = '\0';
1049 strcpy(member_name, "$$");
1051 len = strlen(name);
1052 FOR_EACH_PTR(returned_member_callbacks, cb) {
1053 stree = __get_cur_stree();
1054 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1055 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1056 strcpy(member_name, "*$$");
1057 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1058 continue;
1060 if (strncmp(sm->name, name, len) != 0)
1061 continue;
1062 if (strncmp(sm->name + len, "->", 2) != 0)
1063 continue;
1064 strcpy(member_name, "$$");
1065 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
1066 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1067 } END_FOR_EACH_SM(sm);
1068 } END_FOR_EACH_PTR(cb);
1070 free_string(name);
1073 static void reset_memdb(void)
1075 mem_sql(NULL, NULL, "delete from caller_info;");
1076 mem_sql(NULL, NULL, "delete from return_states;");
1077 mem_sql(NULL, NULL, "delete from call_implies;");
1080 static void match_end_func_info(struct symbol *sym)
1082 if (__path_is_null())
1083 return;
1084 call_return_state_hooks(NULL);
1085 if (!__inline_fn)
1086 reset_memdb();
1089 static void init_memdb(void)
1091 char *err = NULL;
1092 int rc;
1093 const char *schema_files[] = {
1094 "db/db.schema",
1095 "db/caller_info.schema",
1096 "db/return_states.schema",
1097 "db/function_type_size.schema",
1098 "db/type_size.schema",
1099 "db/call_implies.schema",
1100 "db/function_ptr.schema",
1101 "db/local_values.schema",
1102 "db/function_type_value.schema",
1103 "db/type_value.schema",
1104 "db/function_type_info.schema",
1105 "db/data_info.schema",
1107 static char buf[4096];
1108 int fd;
1109 int ret;
1110 int i;
1112 rc = sqlite3_open(":memory:", &mem_db);
1113 if (rc != SQLITE_OK) {
1114 printf("Error starting In-Memory database.");
1115 return;
1118 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1119 fd = open_data_file(schema_files[i]);
1120 if (fd < 0) {
1121 mem_db = NULL;
1122 return;
1124 ret = read(fd, buf, sizeof(buf));
1125 if (ret == sizeof(buf)) {
1126 printf("Schema file too large: %s (limit %zd bytes)",
1127 schema_files[i], sizeof(buf));
1129 buf[ret] = '\0';
1130 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1131 if (rc != SQLITE_OK) {
1132 fprintf(stderr, "SQL error #2: %s\n", err);
1133 fprintf(stderr, "%s\n", buf);
1138 void open_smatch_db(void)
1140 int rc;
1142 if (option_no_db)
1143 return;
1145 init_memdb();
1147 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1148 if (rc != SQLITE_OK) {
1149 option_no_db = 1;
1150 return;
1152 return;
1155 static void register_common_funcs(void)
1157 struct token *token;
1158 char *func;
1159 char filename[256];
1161 if (option_project == PROJ_NONE)
1162 strcpy(filename, "common_functions");
1163 else
1164 snprintf(filename, 256, "%s.common_functions", option_project_str);
1166 token = get_tokens_file(filename);
1167 if (!token)
1168 return;
1169 if (token_type(token) != TOKEN_STREAMBEGIN)
1170 return;
1171 token = token->next;
1172 while (token_type(token) != TOKEN_STREAMEND) {
1173 if (token_type(token) != TOKEN_IDENT)
1174 return;
1175 func = alloc_string(show_ident(token->ident));
1176 add_ptr_list(&common_funcs, func);
1177 token = token->next;
1179 clear_token_alloc();
1183 void register_definition_db_callbacks(int id)
1185 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1186 add_hook(&global_variable, BASE_HOOK);
1187 add_hook(&global_variable, DECLARATION_HOOK);
1188 add_split_return_callback(match_return_info);
1189 add_split_return_callback(print_returned_struct_members);
1190 add_hook(&call_return_state_hooks, RETURN_HOOK);
1191 add_hook(&match_end_func_info, END_FUNC_HOOK);
1193 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1194 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1196 register_common_funcs();
1199 void register_db_call_marker(int id)
1201 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1204 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1206 struct expression *arg;
1207 char *name = NULL;
1208 char member_name[256];
1210 *sym = NULL;
1212 if (param == -1) {
1213 const char *star = "";
1215 if (expr->type != EXPR_ASSIGNMENT)
1216 return NULL;
1217 name = expr_to_var_sym(expr->left, sym);
1218 if (!name)
1219 return NULL;
1220 if (key[0] == '*') {
1221 star = "*";
1222 key++;
1224 if (strncmp(key, "$$", 2) != 0)
1225 return name;
1226 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1227 free_string(name);
1228 return alloc_string(member_name);
1231 while (expr->type == EXPR_ASSIGNMENT)
1232 expr = strip_expr(expr->right);
1233 if (expr->type != EXPR_CALL)
1234 return NULL;
1236 arg = get_argument_from_call_expr(expr->args, param);
1237 if (!arg)
1238 return NULL;
1240 return get_variable_from_key(arg, key, sym);
1243 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1245 char buf[256];
1246 char *tmp;
1248 if (strcmp(key, "$$") == 0)
1249 return expr_to_var_sym(arg, sym);
1251 if (strcmp(key, "*$$") == 0) {
1252 if (arg->type == EXPR_PREOP && arg->op == '&') {
1253 arg = strip_expr(arg->unop);
1254 return expr_to_var_sym(arg, sym);
1255 } else {
1256 tmp = expr_to_var_sym(arg, sym);
1257 if (!tmp)
1258 return NULL;
1259 snprintf(buf, sizeof(buf), "*%s", tmp);
1260 free_string(tmp);
1261 return alloc_string(buf);
1265 if (arg->type == EXPR_PREOP && arg->op == '&') {
1266 arg = strip_expr(arg->unop);
1267 tmp = expr_to_var_sym(arg, sym);
1268 if (!tmp)
1269 return NULL;
1270 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1271 return alloc_string(buf);
1274 tmp = expr_to_var_sym(arg, sym);
1275 if (!tmp)
1276 return NULL;
1277 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1278 free_string(tmp);
1279 return alloc_string(buf);
1282 const char *get_param_name(struct sm_state *sm)
1284 char *param_name;
1285 int name_len;
1286 static char buf[256];
1288 if (!sm->sym->ident)
1289 return NULL;
1291 param_name = sm->sym->ident->name;
1292 name_len = strlen(param_name);
1294 if (strcmp(sm->name, param_name) == 0) {
1295 return "$$";
1296 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1297 strncmp(sm->name, param_name, name_len) == 0) {
1298 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1299 return buf;
1300 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1301 return "*$$";
1303 return NULL;