conditions: handle comma condtions
[smatch.git] / smatch_db.c
blobe93271df79854079725acd0c39a299a92dac64b8
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_data_info_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 > 3000)
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 *key;
563 char *value;
564 char *name = NULL;
565 struct symbol *sym = NULL;
566 struct def_callback *def_callback;
567 struct stree *stree;
569 if (argc != 5)
570 return 0;
572 func_id = atoi(argv[0]);
573 errno = 0;
574 type = strtol(argv[1], NULL, 10);
575 param = strtol(argv[2], NULL, 10);
576 if (errno)
577 return 0;
578 key = argv[3];
579 value = argv[4];
582 if (prev_func_id == -1)
583 prev_func_id = func_id;
584 if (func_id != prev_func_id) {
585 stree = __pop_fake_cur_stree();
586 merge_stree(&final_states, stree);
587 free_stree(&stree);
588 __push_fake_cur_stree();
589 __unnullify_path();
590 prev_func_id = func_id;
593 if (param >= 0 && !get_param(param, &name, &sym))
594 return 0;
596 FOR_EACH_PTR(callbacks, def_callback) {
597 if (def_callback->hook_type == type)
598 def_callback->callback(name, sym, key, value);
599 } END_FOR_EACH_PTR(def_callback);
601 return 0;
604 static void get_direct_callers(struct symbol *sym)
606 sql_select_caller_info("call_id, type, parameter, key, value", sym,
607 caller_info_callback);
610 static struct string_list *ptr_names_done;
611 static struct string_list *ptr_names;
613 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
615 insert_string(&ptr_names, alloc_string(argv[0]));
616 return 0;
619 static char *get_next_ptr_name(void)
621 char *ptr;
623 FOR_EACH_PTR(ptr_names, ptr) {
624 if (list_has_string(ptr_names_done, ptr))
625 continue;
626 insert_string(&ptr_names_done, ptr);
627 return ptr;
628 } END_FOR_EACH_PTR(ptr);
629 return NULL;
632 static void get_ptr_names(const char *file, const char *name)
634 char sql_filter[1024];
635 int before, after;
637 if (file) {
638 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
639 file, name);
640 } else {
641 snprintf(sql_filter, 1024, "function = '%s';", name);
644 before = ptr_list_size((struct ptr_list *)ptr_names);
646 run_sql(get_ptr_name, NULL,
647 "select distinct ptr from function_ptr where %s",
648 sql_filter);
650 after = ptr_list_size((struct ptr_list *)ptr_names);
651 if (before == after)
652 return;
654 while ((name = get_next_ptr_name()))
655 get_ptr_names(NULL, name);
658 static void match_data_from_db(struct symbol *sym)
660 struct sm_state *sm;
661 struct stree *stree;
663 if (!sym || !sym->ident)
664 return;
666 __push_fake_cur_stree();
667 __unnullify_path();
668 prev_func_id = -1;
670 if (!__inline_fn) {
671 char *ptr;
673 if (sym->ctype.modifiers & MOD_STATIC)
674 get_ptr_names(get_base_file(), sym->ident->name);
675 else
676 get_ptr_names(NULL, sym->ident->name);
678 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
679 __free_ptr_list((struct ptr_list **)&ptr_names);
680 __free_ptr_list((struct ptr_list **)&ptr_names_done);
681 stree = __pop_fake_cur_stree();
682 free_stree(&stree);
683 return;
686 get_direct_callers(sym);
688 FOR_EACH_PTR(ptr_names, ptr) {
689 run_sql(caller_info_callback, NULL,
690 "select call_id, type, parameter, key, value"
691 " from caller_info where function = '%s' order by call_id",
692 ptr);
693 free_string(ptr);
694 } END_FOR_EACH_PTR(ptr);
696 __free_ptr_list((struct ptr_list **)&ptr_names);
697 __free_ptr_list((struct ptr_list **)&ptr_names_done);
698 } else {
699 get_direct_callers(sym);
702 stree = __pop_fake_cur_stree();
703 merge_stree(&final_states, stree);
704 free_stree(&stree);
706 FOR_EACH_SM(final_states, sm) {
707 __set_sm(sm);
708 } END_FOR_EACH_SM(sm);
710 free_stree(&final_states);
713 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
715 struct expression *call_expr = static_call_expr;
716 struct call_implies_callback *cb;
717 struct expression *arg = NULL;
718 int type;
719 int param;
721 if (argc != 5)
722 return 0;
724 type = atoi(argv[1]);
725 param = atoi(argv[2]);
727 FOR_EACH_PTR(call_implies_cb_list, cb) {
728 if (cb->type != type)
729 continue;
730 if (param != -1) {
731 arg = get_argument_from_call_expr(call_expr->args, param);
732 if (!arg)
733 continue;
735 cb->callback(arg, argv[3], argv[4]);
736 } END_FOR_EACH_PTR(cb);
738 return 0;
741 static void match_call_implies(struct expression *expr)
743 static_call_expr = expr;
744 sql_select_call_implies("function, type, parameter, key, value", expr,
745 call_implies_callbacks);
746 return;
749 static void print_initializer_list(struct expression_list *expr_list,
750 struct symbol *struct_type)
752 struct expression *expr;
753 struct symbol *base_type;
754 char struct_name[256];
756 FOR_EACH_PTR(expr_list, expr) {
757 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
758 print_initializer_list(expr->idx_expression->expr_list, struct_type);
759 continue;
761 if (expr->type != EXPR_IDENTIFIER)
762 continue;
763 if (!expr->expr_ident)
764 continue;
765 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
766 continue;
767 base_type = get_type(expr->ident_expression);
768 if (!base_type || base_type->type != SYM_FN)
769 continue;
770 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
771 struct_type->ident->name, expr->expr_ident->name);
772 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
773 struct_name);
774 } END_FOR_EACH_PTR(expr);
777 static void global_variable(struct symbol *sym)
779 struct symbol *struct_type;
781 if (!sym->ident)
782 return;
783 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
784 return;
785 struct_type = get_base_type(sym);
786 if (!struct_type)
787 return;
788 if (struct_type->type == SYM_ARRAY) {
789 struct_type = get_base_type(struct_type);
790 if (!struct_type)
791 return;
793 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
794 return;
795 print_initializer_list(sym->initializer->expr_list, struct_type);
798 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
800 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
803 static void call_return_state_hooks_conditional(struct expression *expr)
805 struct returned_state_callback *cb;
806 struct range_list *rl;
807 char *return_ranges;
808 int final_pass_orig = final_pass;
810 __push_fake_cur_stree();
812 final_pass = 0;
813 __split_whole_condition(expr->conditional);
814 final_pass = final_pass_orig;
816 if (get_implied_rl(expr->cond_true, &rl))
817 rl = cast_rl(cur_func_return_type(), rl);
818 else
819 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
820 return_ranges = show_rl(rl);
822 return_id++;
823 FOR_EACH_PTR(returned_state_callbacks, cb) {
824 cb->callback(return_id, return_ranges, expr);
825 } END_FOR_EACH_PTR(cb);
827 __push_true_states();
828 __use_false_states();
830 if (get_implied_rl(expr->cond_false, &rl))
831 rl = cast_rl(cur_func_return_type(), rl);
832 else
833 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
834 return_ranges = show_rl(rl);
836 return_id++;
837 FOR_EACH_PTR(returned_state_callbacks, cb) {
838 cb->callback(return_id, return_ranges, expr);
839 } END_FOR_EACH_PTR(cb);
841 __merge_true_states();
842 __free_fake_cur_stree();
845 static void call_return_state_hooks_compare(struct expression *expr)
847 struct returned_state_callback *cb;
848 char *return_ranges;
849 int final_pass_orig = final_pass;
851 __push_fake_cur_stree();
853 final_pass = 0;
854 __split_whole_condition(expr);
855 final_pass = final_pass_orig;
857 return_ranges = alloc_sname("1");
859 return_id++;
860 FOR_EACH_PTR(returned_state_callbacks, cb) {
861 cb->callback(return_id, return_ranges, expr);
862 } END_FOR_EACH_PTR(cb);
864 __push_true_states();
865 __use_false_states();
867 return_ranges = alloc_sname("0");;
868 return_id++;
869 FOR_EACH_PTR(returned_state_callbacks, cb) {
870 cb->callback(return_id, return_ranges, expr);
871 } END_FOR_EACH_PTR(cb);
873 __merge_true_states();
874 __free_fake_cur_stree();
877 static int split_helper(struct sm_state *sm, struct expression *expr)
879 struct returned_state_callback *cb;
880 struct range_list *rl;
881 char *return_ranges;
882 struct sm_state *tmp;
883 int ret = 0;
884 int nr_possible, nr_states;
885 char *compare_str;
886 char buf[128];
888 if (!sm || !sm->merged)
889 return 0;
891 if (too_many_possible(sm))
892 return 0;
894 /* bail if it gets too complicated */
895 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
896 nr_states = stree_count(__get_cur_stree());
898 * the main thing option_info because we don't want to print a
899 * million lines of output. If someone else, like check_locking.c
900 * wants this data, then it doesn't cause a slow down to provide it.
902 if (option_info && nr_states * nr_possible >= 2000)
903 return 0;
905 compare_str = expr_lte_to_param(expr, -1);
907 FOR_EACH_PTR(sm->possible, tmp) {
908 if (tmp->merged)
909 continue;
911 ret = 1;
912 __push_fake_cur_stree();
914 overwrite_states_using_pool(tmp);
916 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
917 return_ranges = show_rl(rl);
918 if (compare_str) {
919 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
920 return_ranges = alloc_sname(buf);
923 return_id++;
924 FOR_EACH_PTR(returned_state_callbacks, cb) {
925 cb->callback(return_id, return_ranges, expr);
926 } END_FOR_EACH_PTR(cb);
928 __free_fake_cur_stree();
929 } END_FOR_EACH_PTR(tmp);
931 return ret;
934 static int call_return_state_hooks_split_possible(struct expression *expr)
936 struct returned_state_callback *cb;
937 struct range_list *rl;
938 char *return_ranges;
939 struct sm_state *sm;
940 struct sm_state *tmp;
941 int ret = 0;
942 int nr_possible, nr_states;
943 char *compare_str;
944 char buf[128];
946 if (!expr || expr_equal_to_param(expr, -1))
947 return 0;
949 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
950 if (!sm || !sm->merged)
951 return 0;
953 if (too_many_possible(sm))
954 return 0;
956 /* bail if it gets too complicated */
957 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
958 nr_states = stree_count(__get_cur_stree());
960 * the main thing option_info because we don't want to print a
961 * million lines of output. If someone else, like check_locking.c
962 * wants this data, then it doesn't cause a slow down to provide it.
964 if (option_info && nr_states * nr_possible >= 2000)
965 return 0;
968 FOR_EACH_PTR(sm->possible, tmp) {
969 if (tmp->merged)
970 continue;
972 ret = 1;
973 __push_fake_cur_stree();
975 overwrite_states_using_pool(tmp);
977 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
978 return_ranges = show_rl(rl);
980 compare_str = expr_lte_to_param(expr, -1);
981 if (compare_str) {
982 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
983 return_ranges = alloc_sname(buf);
986 return_id++;
987 FOR_EACH_PTR(returned_state_callbacks, cb) {
988 cb->callback(return_id, return_ranges, expr);
989 } END_FOR_EACH_PTR(cb);
991 __free_fake_cur_stree();
992 } END_FOR_EACH_PTR(tmp);
994 return ret;
997 static const char *get_return_ranges_str(struct expression *expr)
999 struct range_list *rl;
1000 char *return_ranges;
1001 sval_t sval;
1002 char *compare_str;
1003 char *math_str;
1004 char buf[128];
1006 if (!expr)
1007 return alloc_sname("");
1009 if (get_implied_value(expr, &sval))
1010 return sval_to_str(sval);
1012 compare_str = expr_equal_to_param(expr, -1);
1013 math_str = get_value_in_terms_of_parameter_math(expr);
1015 if (get_implied_rl(expr, &rl)) {
1016 rl = cast_rl(cur_func_return_type(), rl);
1017 return_ranges = show_rl(rl);
1018 } else {
1019 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1020 return_ranges = show_rl(rl);
1023 if (compare_str) {
1024 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1025 return alloc_sname(buf);
1028 if (math_str) {
1029 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1030 return alloc_sname(buf);
1033 compare_str = expr_lte_to_param(expr, -1);
1034 if (compare_str) {
1035 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1036 return alloc_sname(buf);
1038 return return_ranges;
1041 static int is_boolean(struct expression *expr)
1043 struct range_list *rl;
1045 if (!get_implied_rl(expr, &rl))
1046 return 0;
1047 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1048 return 1;
1049 return 0;
1052 static int is_conditional(struct expression *expr)
1054 if (!expr)
1055 return 0;
1056 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1057 return 1;
1058 return 0;
1061 static int splitable_function_call(struct expression *expr)
1063 struct sm_state *sm;
1064 char buf[64];
1066 if (!expr || expr->type != EXPR_CALL)
1067 return 0;
1068 snprintf(buf, sizeof(buf), "return %p", expr);
1069 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1070 return split_helper(sm, expr);
1073 static void call_return_state_hooks(struct expression *expr)
1075 struct returned_state_callback *cb;
1076 const char *return_ranges;
1077 int nr_states;
1079 expr = strip_expr(expr);
1081 if (is_condition(expr) || is_boolean(expr)) {
1082 call_return_state_hooks_compare(expr);
1083 return;
1084 } else if (is_conditional(expr)) {
1085 call_return_state_hooks_conditional(expr);
1086 return;
1087 } else if (call_return_state_hooks_split_possible(expr)) {
1088 return;
1089 } else if (splitable_function_call(expr)) {
1090 return;
1093 return_ranges = get_return_ranges_str(expr);
1095 return_id++;
1096 nr_states = stree_count(__get_cur_stree());
1097 if (nr_states >= 10000) {
1098 match_return_info(return_id, (char *)return_ranges, expr);
1099 return;
1101 FOR_EACH_PTR(returned_state_callbacks, cb) {
1102 cb->callback(return_id, (char *)return_ranges, expr);
1103 } END_FOR_EACH_PTR(cb);
1106 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1108 struct returned_member_callback *cb;
1109 struct stree *stree;
1110 struct sm_state *sm;
1111 struct symbol *type;
1112 char *name;
1113 char member_name[256];
1114 int len;
1116 type = get_type(expr);
1117 if (!type || type->type != SYM_PTR)
1118 return;
1119 name = expr_to_var(expr);
1120 if (!name)
1121 return;
1123 member_name[sizeof(member_name) - 1] = '\0';
1124 strcpy(member_name, "$");
1126 len = strlen(name);
1127 FOR_EACH_PTR(returned_member_callbacks, cb) {
1128 stree = __get_cur_stree();
1129 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1130 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1131 strcpy(member_name, "*$");
1132 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1133 continue;
1135 if (strncmp(sm->name, name, len) != 0)
1136 continue;
1137 if (strncmp(sm->name + len, "->", 2) != 0)
1138 continue;
1139 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1140 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1141 } END_FOR_EACH_SM(sm);
1142 } END_FOR_EACH_PTR(cb);
1144 free_string(name);
1147 static void reset_memdb(void)
1149 mem_sql(NULL, NULL, "delete from caller_info;");
1150 mem_sql(NULL, NULL, "delete from return_states;");
1151 mem_sql(NULL, NULL, "delete from call_implies;");
1154 static void match_end_func_info(struct symbol *sym)
1156 if (__path_is_null())
1157 return;
1158 call_return_state_hooks(NULL);
1159 if (!__inline_fn)
1160 reset_memdb();
1163 static void init_memdb(void)
1165 char *err = NULL;
1166 int rc;
1167 const char *schema_files[] = {
1168 "db/db.schema",
1169 "db/caller_info.schema",
1170 "db/return_states.schema",
1171 "db/function_type_size.schema",
1172 "db/type_size.schema",
1173 "db/call_implies.schema",
1174 "db/function_ptr.schema",
1175 "db/local_values.schema",
1176 "db/function_type_value.schema",
1177 "db/type_value.schema",
1178 "db/function_type_info.schema",
1179 "db/data_info.schema",
1181 static char buf[4096];
1182 int fd;
1183 int ret;
1184 int i;
1186 rc = sqlite3_open(":memory:", &mem_db);
1187 if (rc != SQLITE_OK) {
1188 printf("Error starting In-Memory database.");
1189 return;
1192 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1193 fd = open_data_file(schema_files[i]);
1194 if (fd < 0) {
1195 mem_db = NULL;
1196 return;
1198 ret = read(fd, buf, sizeof(buf));
1199 if (ret == sizeof(buf)) {
1200 printf("Schema file too large: %s (limit %zd bytes)",
1201 schema_files[i], sizeof(buf));
1203 buf[ret] = '\0';
1204 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1205 if (rc != SQLITE_OK) {
1206 fprintf(stderr, "SQL error #2: %s\n", err);
1207 fprintf(stderr, "%s\n", buf);
1212 void open_smatch_db(void)
1214 int rc;
1216 if (option_no_db)
1217 return;
1219 init_memdb();
1221 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1222 if (rc != SQLITE_OK) {
1223 option_no_db = 1;
1224 return;
1226 return;
1229 static void register_common_funcs(void)
1231 struct token *token;
1232 char *func;
1233 char filename[256];
1235 if (option_project == PROJ_NONE)
1236 strcpy(filename, "common_functions");
1237 else
1238 snprintf(filename, 256, "%s.common_functions", option_project_str);
1240 token = get_tokens_file(filename);
1241 if (!token)
1242 return;
1243 if (token_type(token) != TOKEN_STREAMBEGIN)
1244 return;
1245 token = token->next;
1246 while (token_type(token) != TOKEN_STREAMEND) {
1247 if (token_type(token) != TOKEN_IDENT)
1248 return;
1249 func = alloc_string(show_ident(token->ident));
1250 add_ptr_list(&common_funcs, func);
1251 token = token->next;
1253 clear_token_alloc();
1257 void register_definition_db_callbacks(int id)
1259 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1260 add_hook(&global_variable, BASE_HOOK);
1261 add_hook(&global_variable, DECLARATION_HOOK);
1262 add_split_return_callback(match_return_info);
1263 add_split_return_callback(print_returned_struct_members);
1264 add_hook(&call_return_state_hooks, RETURN_HOOK);
1265 add_hook(&match_end_func_info, END_FUNC_HOOK);
1267 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1268 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1270 register_common_funcs();
1273 void register_db_call_marker(int id)
1275 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1278 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1280 struct expression *arg;
1281 char *name = NULL;
1282 char member_name[256];
1284 *sym = NULL;
1286 if (param == -1) {
1287 const char *star = "";
1289 if (expr->type != EXPR_ASSIGNMENT)
1290 return NULL;
1291 name = expr_to_var_sym(expr->left, sym);
1292 if (!name)
1293 return NULL;
1294 if (key[0] == '*') {
1295 star = "*";
1296 key++;
1298 if (strncmp(key, "$", 1) != 0)
1299 return name;
1300 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1301 free_string(name);
1302 return alloc_string(member_name);
1305 while (expr->type == EXPR_ASSIGNMENT)
1306 expr = strip_expr(expr->right);
1307 if (expr->type != EXPR_CALL)
1308 return NULL;
1310 arg = get_argument_from_call_expr(expr->args, param);
1311 if (!arg)
1312 return NULL;
1314 return get_variable_from_key(arg, key, sym);
1317 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1319 char buf[256];
1320 char *tmp;
1322 if (strcmp(key, "$") == 0)
1323 return expr_to_var_sym(arg, sym);
1325 if (strcmp(key, "*$") == 0) {
1326 if (arg->type == EXPR_PREOP && arg->op == '&') {
1327 arg = strip_expr(arg->unop);
1328 return expr_to_var_sym(arg, sym);
1329 } else {
1330 tmp = expr_to_var_sym(arg, sym);
1331 if (!tmp)
1332 return NULL;
1333 snprintf(buf, sizeof(buf), "*%s", tmp);
1334 free_string(tmp);
1335 return alloc_string(buf);
1339 if (arg->type == EXPR_PREOP && arg->op == '&') {
1340 arg = strip_expr(arg->unop);
1341 tmp = expr_to_var_sym(arg, sym);
1342 if (!tmp)
1343 return NULL;
1344 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1345 return alloc_string(buf);
1348 tmp = expr_to_var_sym(arg, sym);
1349 if (!tmp)
1350 return NULL;
1351 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1352 free_string(tmp);
1353 return alloc_string(buf);
1356 const char *get_param_name(struct sm_state *sm)
1358 char *param_name;
1359 int name_len;
1360 static char buf[256];
1362 if (!sm->sym->ident)
1363 return NULL;
1365 param_name = sm->sym->ident->name;
1366 name_len = strlen(param_name);
1368 if (strcmp(sm->name, param_name) == 0) {
1369 return "$";
1370 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1371 strncmp(sm->name, param_name, name_len) == 0) {
1372 snprintf(buf, sizeof(buf), "$%s", sm->name + name_len);
1373 return buf;
1374 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1375 return "*$";
1377 return NULL;
1380 char *get_data_info_name(struct expression *expr)
1382 struct symbol *sym;
1383 char *name;
1384 char buf[256];
1385 char *ret = NULL;
1387 expr = strip_expr(expr);
1388 name = get_member_name(expr);
1389 if (name)
1390 return name;
1391 name = expr_to_var_sym(expr, &sym);
1392 if (!name || !sym)
1393 goto free;
1394 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1395 goto free;
1396 if (sym->ctype.modifiers & MOD_STATIC)
1397 snprintf(buf, sizeof(buf), "static %s", name);
1398 else
1399 snprintf(buf, sizeof(buf), "global %s", name);
1400 ret = alloc_sname(buf);
1401 free:
1402 free_string(name);
1403 return ret;