comparison: handle preops like "if (++a == b)"
[smatch.git] / smatch_db.c
blob8b8b89412bc798319301f68b69e65850d66d33f7
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <string.h>
19 #include <errno.h>
20 #include <sqlite3.h>
21 #include <unistd.h>
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static sqlite3 *db;
27 static sqlite3 *mem_db;
29 static int return_id;
31 #define sql_insert(table, values...) \
32 do { \
33 if (!mem_db) \
34 break; \
35 if (__inline_fn) { \
36 char buf[1024]; \
37 char *err, *p = buf; \
38 int rc; \
40 p += snprintf(p, buf + sizeof(buf) - p, \
41 "insert into %s values (", #table); \
42 p += snprintf(p, buf + sizeof(buf) - p, values); \
43 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
44 sm_debug("in-mem: %s\n", buf); \
45 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
46 if (rc != SQLITE_OK) { \
47 fprintf(stderr, "SQL error #2: %s\n", err); \
48 fprintf(stderr, "SQL: '%s'\n", buf); \
49 } \
50 break; \
51 } \
52 if (option_info) { \
53 sm_prefix(); \
54 sm_printf("SQL: insert into " #table " values (" values); \
55 sm_printf(");\n"); \
56 } \
57 } while (0)
59 struct def_callback {
60 int hook_type;
61 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
63 ALLOCATOR(def_callback, "definition db hook callbacks");
64 DECLARE_PTR_LIST(callback_list, struct def_callback);
65 static struct callback_list *callbacks;
67 struct member_info_callback {
68 int owner;
69 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
71 ALLOCATOR(member_info_callback, "caller_info callbacks");
72 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
73 static struct member_info_cb_list *member_callbacks;
75 struct returned_state_callback {
76 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
78 ALLOCATOR(returned_state_callback, "returned state callbacks");
79 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
80 static struct returned_state_cb_list *returned_state_callbacks;
82 struct returned_member_callback {
83 int owner;
84 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
86 ALLOCATOR(returned_member_callback, "returned member callbacks");
87 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
88 static struct returned_member_cb_list *returned_member_callbacks;
90 struct call_implies_callback {
91 int type;
92 void (*callback)(struct expression *arg, char *key, char *value);
94 ALLOCATOR(call_implies_callback, "call_implies callbacks");
95 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
96 static struct call_implies_cb_list *call_implies_cb_list;
98 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
100 int i;
102 for (i = 0; i < argc; i++) {
103 if (i != 0)
104 printf(", ");
105 sm_printf("%s", argv[i]);
107 sm_printf("\n");
108 return 0;
111 void debug_sql(const char *sql)
113 if (!option_debug)
114 return;
115 sm_msg("%s", sql);
116 sql_exec(print_sql_output, NULL, sql);
119 void debug_mem_sql(const char *sql)
121 if (!option_debug)
122 return;
123 sm_msg("%s", sql);
124 sql_mem_exec(print_sql_output, NULL, sql);
127 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
129 char *err = NULL;
130 int rc;
132 if (option_no_db || !db)
133 return;
135 rc = sqlite3_exec(db, sql, callback, data, &err);
136 if (rc != SQLITE_OK) {
137 fprintf(stderr, "SQL error #2: %s\n", err);
138 fprintf(stderr, "SQL: '%s'\n", sql);
142 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
144 char *err = NULL;
145 int rc;
147 if (!mem_db)
148 return;
150 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
151 if (rc != SQLITE_OK) {
152 fprintf(stderr, "SQL error #2: %s\n", err);
153 fprintf(stderr, "SQL: '%s'\n", sql);
157 void sql_insert_return_states(int return_id, const char *return_ranges,
158 int type, int param, const char *key, const char *value)
160 if (key && strlen(key) >= 80)
161 return;
162 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
163 get_base_file(), get_function(), (unsigned long)__inline_fn,
164 return_id, return_ranges, fn_static(), type, param, key, value);
167 static struct string_list *common_funcs;
168 static int is_common_function(const char *fn)
170 char *tmp;
172 if (strncmp(fn, "__builtin_", 10) == 0)
173 return 1;
175 FOR_EACH_PTR(common_funcs, tmp) {
176 if (strcmp(tmp, fn) == 0)
177 return 1;
178 } END_FOR_EACH_PTR(tmp);
180 return 0;
183 void sql_insert_caller_info(struct expression *call, int type,
184 int param, const char *key, const char *value)
186 char *fn;
188 if (!option_info && !__inline_call)
189 return;
191 if (key && strlen(key) >= 80)
192 return;
194 fn = get_fnptr_name(call->fn);
195 if (!fn)
196 return;
198 if (__inline_call) {
199 mem_sql(NULL, NULL,
200 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
201 get_base_file(), get_function(), fn, (unsigned long)call,
202 is_static(call->fn), type, param, key, value);
205 if (!option_info)
206 return;
208 if (is_common_function(fn))
209 return;
211 sm_msg("SQL_caller_info: insert into caller_info values ("
212 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
213 get_base_file(), get_function(), fn, is_static(call->fn),
214 type, param, key, value);
216 free_string(fn);
219 void sql_insert_function_ptr(const char *fn, const char *struct_name)
221 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
222 struct_name);
225 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
227 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
228 get_function(), (unsigned long)__inline_fn, fn_static(),
229 type, param, key, value);
232 void sql_insert_function_type_size(const char *member, const char *ranges)
234 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
237 void sql_insert_local_values(const char *name, const char *value)
239 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
242 void sql_insert_function_type_value(const char *type, const char *value)
244 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
247 void sql_insert_function_type_info(int param, const char *value)
249 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
250 get_base_file(), get_function(), fn_static(), param, value);
253 void sql_insert_data_info(struct expression *data, int type, const char *value)
255 char *data_name;
257 data_name = get_data_info_name(data);
258 if (!data_name)
259 return;
260 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
263 static char *get_static_filter(struct symbol *sym)
265 static char sql_filter[1024];
267 if (sym->ctype.modifiers & MOD_STATIC) {
268 snprintf(sql_filter, sizeof(sql_filter),
269 "file = '%s' and function = '%s' and static = '1'",
270 get_base_file(), sym->ident->name);
271 } else {
272 snprintf(sql_filter, sizeof(sql_filter),
273 "function = '%s' and static = '0'", sym->ident->name);
276 return sql_filter;
279 static int row_count;
280 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
282 if (argc != 1)
283 return 0;
284 row_count = atoi(argv[0]);
285 return 0;
288 static void sql_select_return_states_pointer(const char *cols,
289 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
291 char *ptr;
293 ptr = get_fnptr_name(call->fn);
294 if (!ptr)
295 return;
297 run_sql(callback, info,
298 "select %s from return_states join function_ptr where "
299 "return_states.function == function_ptr.function and ptr = '%s'"
300 "and searchable = 1 order by return_id, type;",
301 cols, ptr);
304 void sql_select_return_states(const char *cols, struct expression *call,
305 int (*callback)(void*, int, char**, char**), void *info)
307 if (is_fake_call(call))
308 return;
310 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol) {
311 sql_select_return_states_pointer(cols, call, callback, info);
312 return;
315 if (inlinable(call->fn)) {
316 mem_sql(callback, info,
317 "select %s from return_states where call_id = '%lu' order by return_id, type;",
318 cols, (unsigned long)call);
319 return;
322 row_count = 0;
323 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
324 get_static_filter(call->fn->symbol));
325 if (row_count > 3000)
326 return;
328 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
329 cols, get_static_filter(call->fn->symbol));
332 void sql_select_call_implies(const char *cols, struct expression *call,
333 int (*callback)(void*, int, char**, char**))
335 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
336 return;
338 if (inlinable(call->fn)) {
339 mem_sql(callback, NULL,
340 "select %s from call_implies where call_id = '%lu';",
341 cols, (unsigned long)call);
342 return;
345 run_sql(callback, NULL, "select %s from call_implies where %s;",
346 cols, get_static_filter(call->fn->symbol));
349 void sql_select_caller_info(const char *cols, struct symbol *sym,
350 int (*callback)(void*, int, char**, char**))
352 if (__inline_fn) {
353 mem_sql(callback, NULL,
354 "select %s from caller_info where call_id = %lu;",
355 cols, (unsigned long)__inline_fn);
356 return;
359 run_sql(callback, NULL,
360 "select %s from caller_info where %s order by call_id;",
361 cols, get_static_filter(sym));
364 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
366 struct def_callback *def_callback = __alloc_def_callback(0);
368 def_callback->hook_type = type;
369 def_callback->callback = callback;
370 add_ptr_list(&callbacks, def_callback);
374 * These call backs are used when the --info option is turned on to print struct
375 * member information. For example foo->bar could have a state in
376 * smatch_extra.c and also check_user.c.
378 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
380 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
382 member_callback->owner = owner;
383 member_callback->callback = callback;
384 add_ptr_list(&member_callbacks, member_callback);
387 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
389 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
391 callback->callback = fn;
392 add_ptr_list(&returned_state_callbacks, callback);
395 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))
397 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
399 member_callback->owner = owner;
400 member_callback->callback = callback;
401 add_ptr_list(&returned_member_callbacks, member_callback);
404 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
406 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
408 cb->type = type;
409 cb->callback = callback;
410 add_ptr_list(&call_implies_cb_list, cb);
413 static struct expression *static_call_expr;
414 static struct expression *static_returns_call;
415 static struct symbol *return_type;
416 static struct range_list *return_range_list;
417 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
419 struct range_list *rl;
420 struct expression *call_expr = static_returns_call;
422 if (argc != 1)
423 return 0;
424 call_results_to_rl(call_expr, return_type, argv[0], &rl);
425 return_range_list = rl_union(return_range_list, rl);
426 return 0;
429 struct range_list *db_return_vals(struct expression *expr)
431 static_returns_call = expr;
432 return_type = get_type(expr);
433 if (!return_type)
434 return NULL;
435 if (is_fake_call(expr))
436 return NULL;
437 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
438 return NULL;
440 return_range_list = NULL;
441 if (inlinable(expr->fn)) {
442 mem_sql(db_return_callback, NULL,
443 "select distinct return from return_states where call_id = '%lu';",
444 (unsigned long)expr);
445 } else {
446 run_sql(db_return_callback, NULL,
447 "select distinct return from return_states where %s;",
448 get_static_filter(expr->fn->symbol));
450 return return_range_list;
453 static void match_call_marker(struct expression *expr)
456 * we just want to record something in the database so that if we have
457 * two calls like: frob(4); frob(some_unkown); then on the receiving
458 * side we know that sometimes frob is called with unknown parameters.
461 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
464 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
465 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
467 struct sm_state *sm;
468 char *name;
469 struct symbol *sym;
470 int len;
471 char printed_name[256];
472 int is_address = 0;
474 expr = strip_expr(expr);
475 if (expr->type == EXPR_PREOP && expr->op == '&') {
476 expr = strip_expr(expr->unop);
477 is_address = 1;
480 name = expr_to_var_sym(expr, &sym);
481 if (!name || !sym)
482 goto free;
484 len = strlen(name);
485 FOR_EACH_SM(stree, sm) {
486 if (sm->sym != sym)
487 continue;
488 if (strcmp(name, sm->name) == 0) {
489 if (is_address)
490 snprintf(printed_name, sizeof(printed_name), "*$");
491 else /* these are already handled. fixme: handle them here */
492 continue;
493 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
494 snprintf(printed_name, sizeof(printed_name), "*$");
495 } else if (strncmp(name, sm->name, len) == 0) {
496 if (is_address)
497 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
498 else
499 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
500 } else {
501 continue;
503 callback(call, param, printed_name, sm);
504 } END_FOR_EACH_SM(sm);
505 free:
506 free_string(name);
509 static void match_call_info(struct expression *call)
511 struct member_info_callback *cb;
512 struct expression *arg;
513 struct stree *stree;
514 char *name;
515 int i;
517 name = get_fnptr_name(call->fn);
518 if (!name)
519 return;
521 FOR_EACH_PTR(member_callbacks, cb) {
522 stree = get_all_states_stree(cb->owner);
523 i = 0;
524 FOR_EACH_PTR(call->args, arg) {
525 print_struct_members(call, arg, i, stree, cb->callback);
526 i++;
527 } END_FOR_EACH_PTR(arg);
528 free_stree(&stree);
529 } END_FOR_EACH_PTR(cb);
531 free_string(name);
534 static int get_param(int param, char **name, struct symbol **sym)
536 struct symbol *arg;
537 int i;
539 i = 0;
540 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
542 * this is a temporary hack to work around a bug (I think in sparse?)
543 * 2.6.37-rc1:fs/reiserfs/journal.o
544 * If there is a function definition without parameter name found
545 * after a function implementation then it causes a crash.
546 * int foo() {}
547 * int bar(char *);
549 if (arg->ident->name < (char *)100)
550 continue;
551 if (i == param) {
552 *name = arg->ident->name;
553 *sym = arg;
554 return TRUE;
556 i++;
557 } END_FOR_EACH_PTR(arg);
559 return FALSE;
562 static struct stree *final_states;
563 static int prev_func_id = -1;
564 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
566 int func_id;
567 long type;
568 long param;
569 char *key;
570 char *value;
571 char *name = NULL;
572 struct symbol *sym = NULL;
573 struct def_callback *def_callback;
574 struct stree *stree;
576 if (argc != 5)
577 return 0;
579 func_id = atoi(argv[0]);
580 errno = 0;
581 type = strtol(argv[1], NULL, 10);
582 param = strtol(argv[2], NULL, 10);
583 if (errno)
584 return 0;
585 key = argv[3];
586 value = argv[4];
589 if (prev_func_id == -1)
590 prev_func_id = func_id;
591 if (func_id != prev_func_id) {
592 stree = __pop_fake_cur_stree();
593 merge_stree(&final_states, stree);
594 free_stree(&stree);
595 __push_fake_cur_stree();
596 __unnullify_path();
597 prev_func_id = func_id;
600 if (param >= 0 && !get_param(param, &name, &sym))
601 return 0;
603 FOR_EACH_PTR(callbacks, def_callback) {
604 if (def_callback->hook_type == type)
605 def_callback->callback(name, sym, key, value);
606 } END_FOR_EACH_PTR(def_callback);
608 return 0;
611 static void get_direct_callers(struct symbol *sym)
613 sql_select_caller_info("call_id, type, parameter, key, value", sym,
614 caller_info_callback);
617 static struct string_list *ptr_names_done;
618 static struct string_list *ptr_names;
620 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
622 insert_string(&ptr_names, alloc_string(argv[0]));
623 return 0;
626 static char *get_next_ptr_name(void)
628 char *ptr;
630 FOR_EACH_PTR(ptr_names, ptr) {
631 if (list_has_string(ptr_names_done, ptr))
632 continue;
633 insert_string(&ptr_names_done, ptr);
634 return ptr;
635 } END_FOR_EACH_PTR(ptr);
636 return NULL;
639 static void get_ptr_names(const char *file, const char *name)
641 char sql_filter[1024];
642 int before, after;
644 if (file) {
645 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
646 file, name);
647 } else {
648 snprintf(sql_filter, 1024, "function = '%s';", name);
651 before = ptr_list_size((struct ptr_list *)ptr_names);
653 run_sql(get_ptr_name, NULL,
654 "select distinct ptr from function_ptr where %s",
655 sql_filter);
657 after = ptr_list_size((struct ptr_list *)ptr_names);
658 if (before == after)
659 return;
661 while ((name = get_next_ptr_name()))
662 get_ptr_names(NULL, name);
665 static void match_data_from_db(struct symbol *sym)
667 struct sm_state *sm;
668 struct stree *stree;
670 if (!sym || !sym->ident)
671 return;
673 __push_fake_cur_stree();
674 __unnullify_path();
675 prev_func_id = -1;
677 if (!__inline_fn) {
678 char *ptr;
680 if (sym->ctype.modifiers & MOD_STATIC)
681 get_ptr_names(get_base_file(), sym->ident->name);
682 else
683 get_ptr_names(NULL, sym->ident->name);
685 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
686 __free_ptr_list((struct ptr_list **)&ptr_names);
687 __free_ptr_list((struct ptr_list **)&ptr_names_done);
688 stree = __pop_fake_cur_stree();
689 free_stree(&stree);
690 return;
693 get_direct_callers(sym);
695 FOR_EACH_PTR(ptr_names, ptr) {
696 run_sql(caller_info_callback, NULL,
697 "select call_id, type, parameter, key, value"
698 " from caller_info where function = '%s' order by call_id",
699 ptr);
700 free_string(ptr);
701 } END_FOR_EACH_PTR(ptr);
703 __free_ptr_list((struct ptr_list **)&ptr_names);
704 __free_ptr_list((struct ptr_list **)&ptr_names_done);
705 } else {
706 get_direct_callers(sym);
709 stree = __pop_fake_cur_stree();
710 merge_stree(&final_states, stree);
711 free_stree(&stree);
713 FOR_EACH_SM(final_states, sm) {
714 __set_sm(sm);
715 } END_FOR_EACH_SM(sm);
717 free_stree(&final_states);
720 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
722 struct expression *call_expr = static_call_expr;
723 struct call_implies_callback *cb;
724 struct expression *arg = NULL;
725 int type;
726 int param;
728 if (argc != 5)
729 return 0;
731 type = atoi(argv[1]);
732 param = atoi(argv[2]);
734 FOR_EACH_PTR(call_implies_cb_list, cb) {
735 if (cb->type != type)
736 continue;
737 if (param != -1) {
738 arg = get_argument_from_call_expr(call_expr->args, param);
739 if (!arg)
740 continue;
742 cb->callback(arg, argv[3], argv[4]);
743 } END_FOR_EACH_PTR(cb);
745 return 0;
748 static void match_call_implies(struct expression *expr)
750 static_call_expr = expr;
751 sql_select_call_implies("function, type, parameter, key, value", expr,
752 call_implies_callbacks);
753 return;
756 static void print_initializer_list(struct expression_list *expr_list,
757 struct symbol *struct_type)
759 struct expression *expr;
760 struct symbol *base_type;
761 char struct_name[256];
763 FOR_EACH_PTR(expr_list, expr) {
764 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
765 print_initializer_list(expr->idx_expression->expr_list, struct_type);
766 continue;
768 if (expr->type != EXPR_IDENTIFIER)
769 continue;
770 if (!expr->expr_ident)
771 continue;
772 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
773 continue;
774 base_type = get_type(expr->ident_expression);
775 if (!base_type || base_type->type != SYM_FN)
776 continue;
777 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
778 struct_type->ident->name, expr->expr_ident->name);
779 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
780 struct_name);
781 } END_FOR_EACH_PTR(expr);
784 static void global_variable(struct symbol *sym)
786 struct symbol *struct_type;
788 if (!sym->ident)
789 return;
790 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
791 return;
792 struct_type = get_base_type(sym);
793 if (!struct_type)
794 return;
795 if (struct_type->type == SYM_ARRAY) {
796 struct_type = get_base_type(struct_type);
797 if (!struct_type)
798 return;
800 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
801 return;
802 print_initializer_list(sym->initializer->expr_list, struct_type);
805 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
807 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
810 static void call_return_state_hooks_conditional(struct expression *expr)
812 struct returned_state_callback *cb;
813 struct range_list *rl;
814 char *return_ranges;
815 int final_pass_orig = final_pass;
817 __push_fake_cur_stree();
819 final_pass = 0;
820 __split_whole_condition(expr->conditional);
821 final_pass = final_pass_orig;
823 if (get_implied_rl(expr->cond_true, &rl))
824 rl = cast_rl(cur_func_return_type(), rl);
825 else
826 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
827 return_ranges = show_rl(rl);
829 return_id++;
830 FOR_EACH_PTR(returned_state_callbacks, cb) {
831 cb->callback(return_id, return_ranges, expr);
832 } END_FOR_EACH_PTR(cb);
834 __push_true_states();
835 __use_false_states();
837 if (get_implied_rl(expr->cond_false, &rl))
838 rl = cast_rl(cur_func_return_type(), rl);
839 else
840 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
841 return_ranges = show_rl(rl);
843 return_id++;
844 FOR_EACH_PTR(returned_state_callbacks, cb) {
845 cb->callback(return_id, return_ranges, expr);
846 } END_FOR_EACH_PTR(cb);
848 __merge_true_states();
849 __free_fake_cur_stree();
852 static void call_return_state_hooks_compare(struct expression *expr)
854 struct returned_state_callback *cb;
855 char *return_ranges;
856 int final_pass_orig = final_pass;
858 __push_fake_cur_stree();
860 final_pass = 0;
861 __split_whole_condition(expr);
862 final_pass = final_pass_orig;
864 return_ranges = alloc_sname("1");
866 return_id++;
867 FOR_EACH_PTR(returned_state_callbacks, cb) {
868 cb->callback(return_id, return_ranges, expr);
869 } END_FOR_EACH_PTR(cb);
871 __push_true_states();
872 __use_false_states();
874 return_ranges = alloc_sname("0");;
875 return_id++;
876 FOR_EACH_PTR(returned_state_callbacks, cb) {
877 cb->callback(return_id, return_ranges, expr);
878 } END_FOR_EACH_PTR(cb);
880 __merge_true_states();
881 __free_fake_cur_stree();
884 static int split_helper(struct sm_state *sm, struct expression *expr)
886 struct returned_state_callback *cb;
887 struct range_list *rl;
888 char *return_ranges;
889 struct sm_state *tmp;
890 int ret = 0;
891 int nr_possible, nr_states;
892 char *compare_str;
893 char buf[128];
895 if (!sm || !sm->merged)
896 return 0;
898 if (too_many_possible(sm))
899 return 0;
901 /* bail if it gets too complicated */
902 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
903 nr_states = stree_count(__get_cur_stree());
905 * the main thing option_info because we don't want to print a
906 * million lines of output. If someone else, like check_locking.c
907 * wants this data, then it doesn't cause a slow down to provide it.
909 if (option_info && nr_states * nr_possible >= 2000)
910 return 0;
912 compare_str = expr_lte_to_param(expr, -1);
914 FOR_EACH_PTR(sm->possible, tmp) {
915 if (tmp->merged)
916 continue;
918 ret = 1;
919 __push_fake_cur_stree();
921 overwrite_states_using_pool(tmp);
923 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
924 return_ranges = show_rl(rl);
925 if (compare_str) {
926 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
927 return_ranges = alloc_sname(buf);
930 return_id++;
931 FOR_EACH_PTR(returned_state_callbacks, cb) {
932 cb->callback(return_id, return_ranges, expr);
933 } END_FOR_EACH_PTR(cb);
935 __free_fake_cur_stree();
936 } END_FOR_EACH_PTR(tmp);
938 return ret;
941 static int call_return_state_hooks_split_possible(struct expression *expr)
943 struct returned_state_callback *cb;
944 struct range_list *rl;
945 char *return_ranges;
946 struct sm_state *sm;
947 struct sm_state *tmp;
948 int ret = 0;
949 int nr_possible, nr_states;
950 char *compare_str;
951 char buf[128];
953 if (!expr || expr_equal_to_param(expr, -1))
954 return 0;
956 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
957 if (!sm || !sm->merged)
958 return 0;
960 if (too_many_possible(sm))
961 return 0;
963 /* bail if it gets too complicated */
964 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
965 nr_states = stree_count(__get_cur_stree());
967 * the main thing option_info because we don't want to print a
968 * million lines of output. If someone else, like check_locking.c
969 * wants this data, then it doesn't cause a slow down to provide it.
971 if (option_info && nr_states * nr_possible >= 2000)
972 return 0;
975 FOR_EACH_PTR(sm->possible, tmp) {
976 if (tmp->merged)
977 continue;
979 ret = 1;
980 __push_fake_cur_stree();
982 overwrite_states_using_pool(tmp);
984 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
985 return_ranges = show_rl(rl);
987 compare_str = expr_lte_to_param(expr, -1);
988 if (compare_str) {
989 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
990 return_ranges = alloc_sname(buf);
993 return_id++;
994 FOR_EACH_PTR(returned_state_callbacks, cb) {
995 cb->callback(return_id, return_ranges, expr);
996 } END_FOR_EACH_PTR(cb);
998 __free_fake_cur_stree();
999 } END_FOR_EACH_PTR(tmp);
1001 return ret;
1004 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1006 struct range_list *rl;
1007 int nr_states;
1008 struct returned_state_callback *cb;
1009 char *return_ranges;
1010 int final_pass_orig = final_pass;
1011 sval_t val;
1013 if (option_project != PROJ_KERNEL)
1014 return 0;
1016 nr_states = stree_count(__get_cur_stree());
1017 if (nr_states > 1500)
1018 return 0;
1020 if (get_value(expr, &val))
1021 return 0;
1022 if (!get_implied_rl(expr, &rl))
1023 return 0;
1024 if (rl_min(rl).value < -4095)
1025 return 0;
1026 if (rl_max(rl).value != 0)
1027 return 0;
1029 __push_fake_cur_stree();
1031 final_pass = 0;
1032 __split_whole_condition(expr);
1033 final_pass = final_pass_orig;
1035 return_ranges = show_rl(rl_filter(rl, rl_zero()));
1037 return_id++;
1038 FOR_EACH_PTR(returned_state_callbacks, cb) {
1039 cb->callback(return_id, return_ranges, expr);
1040 } END_FOR_EACH_PTR(cb);
1042 __push_true_states();
1043 __use_false_states();
1045 return_ranges = alloc_sname("0");;
1046 return_id++;
1047 FOR_EACH_PTR(returned_state_callbacks, cb) {
1048 cb->callback(return_id, return_ranges, expr);
1049 } END_FOR_EACH_PTR(cb);
1051 __merge_true_states();
1052 __free_fake_cur_stree();
1054 return 1;
1057 static const char *get_return_ranges_str(struct expression *expr)
1059 struct range_list *rl;
1060 char *return_ranges;
1061 sval_t sval;
1062 char *compare_str;
1063 char *math_str;
1064 char buf[128];
1066 if (!expr)
1067 return alloc_sname("");
1069 if (get_implied_value(expr, &sval))
1070 return sval_to_str(sval);
1072 compare_str = expr_equal_to_param(expr, -1);
1073 math_str = get_value_in_terms_of_parameter_math(expr);
1075 if (get_implied_rl(expr, &rl)) {
1076 rl = cast_rl(cur_func_return_type(), rl);
1077 return_ranges = show_rl(rl);
1078 } else {
1079 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1080 return_ranges = show_rl(rl);
1083 if (compare_str) {
1084 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1085 return alloc_sname(buf);
1088 if (math_str) {
1089 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1090 return alloc_sname(buf);
1093 compare_str = expr_lte_to_param(expr, -1);
1094 if (compare_str) {
1095 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1096 return alloc_sname(buf);
1098 return return_ranges;
1101 static int is_boolean(struct expression *expr)
1103 struct range_list *rl;
1105 if (!get_implied_rl(expr, &rl))
1106 return 0;
1107 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1108 return 1;
1109 return 0;
1112 static int is_conditional(struct expression *expr)
1114 if (!expr)
1115 return 0;
1116 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1117 return 1;
1118 return 0;
1121 static int splitable_function_call(struct expression *expr)
1123 struct sm_state *sm;
1124 char buf[64];
1126 if (!expr || expr->type != EXPR_CALL)
1127 return 0;
1128 snprintf(buf, sizeof(buf), "return %p", expr);
1129 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1130 return split_helper(sm, expr);
1133 static void call_return_state_hooks(struct expression *expr)
1135 struct returned_state_callback *cb;
1136 const char *return_ranges;
1137 int nr_states;
1138 sval_t sval;
1140 expr = strip_expr(expr);
1142 if (!get_implied_value(expr, &sval) &&
1143 (is_condition(expr) || is_boolean(expr))) {
1144 call_return_state_hooks_compare(expr);
1145 return;
1146 } else if (is_conditional(expr)) {
1147 call_return_state_hooks_conditional(expr);
1148 return;
1149 } else if (call_return_state_hooks_split_possible(expr)) {
1150 return;
1151 } else if (call_return_state_hooks_split_success_fail(expr)) {
1152 return;
1153 } else if (splitable_function_call(expr)) {
1154 return;
1157 return_ranges = get_return_ranges_str(expr);
1159 return_id++;
1160 nr_states = stree_count(__get_cur_stree());
1161 if (nr_states >= 10000) {
1162 match_return_info(return_id, (char *)return_ranges, expr);
1163 return;
1165 FOR_EACH_PTR(returned_state_callbacks, cb) {
1166 cb->callback(return_id, (char *)return_ranges, expr);
1167 } END_FOR_EACH_PTR(cb);
1170 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1172 struct returned_member_callback *cb;
1173 struct stree *stree;
1174 struct sm_state *sm;
1175 struct symbol *type;
1176 char *name;
1177 char member_name[256];
1178 int len;
1180 type = get_type(expr);
1181 if (!type || type->type != SYM_PTR)
1182 return;
1183 name = expr_to_var(expr);
1184 if (!name)
1185 return;
1187 member_name[sizeof(member_name) - 1] = '\0';
1188 strcpy(member_name, "$");
1190 len = strlen(name);
1191 FOR_EACH_PTR(returned_member_callbacks, cb) {
1192 stree = __get_cur_stree();
1193 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1194 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1195 strcpy(member_name, "*$");
1196 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1197 continue;
1199 if (strncmp(sm->name, name, len) != 0)
1200 continue;
1201 if (strncmp(sm->name + len, "->", 2) != 0)
1202 continue;
1203 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1204 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1205 } END_FOR_EACH_SM(sm);
1206 } END_FOR_EACH_PTR(cb);
1208 free_string(name);
1211 static void reset_memdb(void)
1213 mem_sql(NULL, NULL, "delete from caller_info;");
1214 mem_sql(NULL, NULL, "delete from return_states;");
1215 mem_sql(NULL, NULL, "delete from call_implies;");
1218 static void match_end_func_info(struct symbol *sym)
1220 if (__path_is_null())
1221 return;
1222 call_return_state_hooks(NULL);
1223 if (!__inline_fn)
1224 reset_memdb();
1227 static void init_memdb(void)
1229 char *err = NULL;
1230 int rc;
1231 const char *schema_files[] = {
1232 "db/db.schema",
1233 "db/caller_info.schema",
1234 "db/return_states.schema",
1235 "db/function_type_size.schema",
1236 "db/type_size.schema",
1237 "db/call_implies.schema",
1238 "db/function_ptr.schema",
1239 "db/local_values.schema",
1240 "db/function_type_value.schema",
1241 "db/type_value.schema",
1242 "db/function_type_info.schema",
1243 "db/data_info.schema",
1245 static char buf[4096];
1246 int fd;
1247 int ret;
1248 int i;
1250 rc = sqlite3_open(":memory:", &mem_db);
1251 if (rc != SQLITE_OK) {
1252 printf("Error starting In-Memory database.");
1253 return;
1256 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1257 fd = open_data_file(schema_files[i]);
1258 if (fd < 0) {
1259 mem_db = NULL;
1260 return;
1262 ret = read(fd, buf, sizeof(buf));
1263 if (ret == sizeof(buf)) {
1264 printf("Schema file too large: %s (limit %zd bytes)",
1265 schema_files[i], sizeof(buf));
1267 buf[ret] = '\0';
1268 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1269 if (rc != SQLITE_OK) {
1270 fprintf(stderr, "SQL error #2: %s\n", err);
1271 fprintf(stderr, "%s\n", buf);
1276 void open_smatch_db(void)
1278 int rc;
1280 if (option_no_db)
1281 return;
1283 init_memdb();
1285 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1286 if (rc != SQLITE_OK) {
1287 option_no_db = 1;
1288 return;
1290 return;
1293 static void register_common_funcs(void)
1295 struct token *token;
1296 char *func;
1297 char filename[256];
1299 if (option_project == PROJ_NONE)
1300 strcpy(filename, "common_functions");
1301 else
1302 snprintf(filename, 256, "%s.common_functions", option_project_str);
1304 token = get_tokens_file(filename);
1305 if (!token)
1306 return;
1307 if (token_type(token) != TOKEN_STREAMBEGIN)
1308 return;
1309 token = token->next;
1310 while (token_type(token) != TOKEN_STREAMEND) {
1311 if (token_type(token) != TOKEN_IDENT)
1312 return;
1313 func = alloc_string(show_ident(token->ident));
1314 add_ptr_list(&common_funcs, func);
1315 token = token->next;
1317 clear_token_alloc();
1321 void register_definition_db_callbacks(int id)
1323 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1324 add_hook(&global_variable, BASE_HOOK);
1325 add_hook(&global_variable, DECLARATION_HOOK);
1326 add_split_return_callback(match_return_info);
1327 add_split_return_callback(print_returned_struct_members);
1328 add_hook(&call_return_state_hooks, RETURN_HOOK);
1329 add_hook(&match_end_func_info, END_FUNC_HOOK);
1331 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1332 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1334 register_common_funcs();
1337 void register_db_call_marker(int id)
1339 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1342 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1344 struct expression *arg;
1345 char *name = NULL;
1346 char member_name[256];
1348 *sym = NULL;
1350 if (param == -1) {
1351 const char *star = "";
1353 if (expr->type != EXPR_ASSIGNMENT)
1354 return NULL;
1355 name = expr_to_var_sym(expr->left, sym);
1356 if (!name)
1357 return NULL;
1358 if (key[0] == '*') {
1359 star = "*";
1360 key++;
1362 if (strncmp(key, "$", 1) != 0)
1363 return name;
1364 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1365 free_string(name);
1366 return alloc_string(member_name);
1369 while (expr->type == EXPR_ASSIGNMENT)
1370 expr = strip_expr(expr->right);
1371 if (expr->type != EXPR_CALL)
1372 return NULL;
1374 arg = get_argument_from_call_expr(expr->args, param);
1375 if (!arg)
1376 return NULL;
1378 return get_variable_from_key(arg, key, sym);
1381 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1383 char buf[256];
1384 char *tmp;
1386 arg = strip_expr(arg);
1388 if (strcmp(key, "$") == 0)
1389 return expr_to_var_sym(arg, sym);
1391 if (strcmp(key, "*$") == 0) {
1392 if (arg->type == EXPR_PREOP && arg->op == '&') {
1393 arg = strip_expr(arg->unop);
1394 return expr_to_var_sym(arg, sym);
1395 } else {
1396 tmp = expr_to_var_sym(arg, sym);
1397 if (!tmp)
1398 return NULL;
1399 snprintf(buf, sizeof(buf), "*%s", tmp);
1400 free_string(tmp);
1401 return alloc_string(buf);
1405 if (arg->type == EXPR_PREOP && arg->op == '&') {
1406 arg = strip_expr(arg->unop);
1407 tmp = expr_to_var_sym(arg, sym);
1408 if (!tmp)
1409 return NULL;
1410 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1411 return alloc_string(buf);
1414 tmp = expr_to_var_sym(arg, sym);
1415 if (!tmp)
1416 return NULL;
1417 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1418 free_string(tmp);
1419 return alloc_string(buf);
1422 const char *get_param_name(struct sm_state *sm)
1424 char *param_name;
1425 int name_len;
1426 static char buf[256];
1428 if (!sm->sym->ident)
1429 return NULL;
1431 param_name = sm->sym->ident->name;
1432 name_len = strlen(param_name);
1434 if (strcmp(sm->name, param_name) == 0) {
1435 return "$";
1436 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1437 strncmp(sm->name, param_name, name_len) == 0) {
1438 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1439 return buf;
1440 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1441 return "*$";
1443 return NULL;
1446 char *get_data_info_name(struct expression *expr)
1448 struct symbol *sym;
1449 char *name;
1450 char buf[256];
1451 char *ret = NULL;
1453 expr = strip_expr(expr);
1454 name = get_member_name(expr);
1455 if (name)
1456 return name;
1457 name = expr_to_var_sym(expr, &sym);
1458 if (!name || !sym)
1459 goto free;
1460 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1461 goto free;
1462 if (sym->ctype.modifiers & MOD_STATIC)
1463 snprintf(buf, sizeof(buf), "static %s", name);
1464 else
1465 snprintf(buf, sizeof(buf), "global %s", name);
1466 ret = alloc_sname(buf);
1467 free:
1468 free_string(name);
1469 return ret;