db: cleanup caller_info_callback() a little
[smatch.git] / smatch_db.c
blob2a8a96d249e43f91c826b82b29180c3ba01ea97b
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 (type == INTERNAL)
594 return 0;
595 if (param >= 0 && !get_param(param, &name, &sym))
596 return 0;
598 FOR_EACH_PTR(callbacks, def_callback) {
599 if (def_callback->hook_type == type)
600 def_callback->callback(name, sym, key, value);
601 } END_FOR_EACH_PTR(def_callback);
603 return 0;
606 static void get_direct_callers(struct symbol *sym)
608 sql_select_caller_info("call_id, type, parameter, key, value", sym,
609 caller_info_callback);
612 static struct string_list *ptr_names_done;
613 static struct string_list *ptr_names;
615 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
617 insert_string(&ptr_names, alloc_string(argv[0]));
618 return 0;
621 static char *get_next_ptr_name(void)
623 char *ptr;
625 FOR_EACH_PTR(ptr_names, ptr) {
626 if (list_has_string(ptr_names_done, ptr))
627 continue;
628 insert_string(&ptr_names_done, ptr);
629 return ptr;
630 } END_FOR_EACH_PTR(ptr);
631 return NULL;
634 static void get_ptr_names(const char *file, const char *name)
636 char sql_filter[1024];
637 int before, after;
639 if (file) {
640 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
641 file, name);
642 } else {
643 snprintf(sql_filter, 1024, "function = '%s';", name);
646 before = ptr_list_size((struct ptr_list *)ptr_names);
648 run_sql(get_ptr_name, NULL,
649 "select distinct ptr from function_ptr where %s",
650 sql_filter);
652 after = ptr_list_size((struct ptr_list *)ptr_names);
653 if (before == after)
654 return;
656 while ((name = get_next_ptr_name()))
657 get_ptr_names(NULL, name);
660 static void match_data_from_db(struct symbol *sym)
662 struct sm_state *sm;
663 struct stree *stree;
665 if (!sym || !sym->ident)
666 return;
668 __push_fake_cur_stree();
669 __unnullify_path();
670 prev_func_id = -1;
672 if (!__inline_fn) {
673 char *ptr;
675 if (sym->ctype.modifiers & MOD_STATIC)
676 get_ptr_names(get_base_file(), sym->ident->name);
677 else
678 get_ptr_names(NULL, sym->ident->name);
680 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
681 __free_ptr_list((struct ptr_list **)&ptr_names);
682 __free_ptr_list((struct ptr_list **)&ptr_names_done);
683 stree = __pop_fake_cur_stree();
684 free_stree(&stree);
685 return;
688 get_direct_callers(sym);
690 FOR_EACH_PTR(ptr_names, ptr) {
691 run_sql(caller_info_callback, NULL,
692 "select call_id, type, parameter, key, value"
693 " from caller_info where function = '%s' order by call_id",
694 ptr);
695 free_string(ptr);
696 } END_FOR_EACH_PTR(ptr);
698 __free_ptr_list((struct ptr_list **)&ptr_names);
699 __free_ptr_list((struct ptr_list **)&ptr_names_done);
700 } else {
701 get_direct_callers(sym);
704 stree = __pop_fake_cur_stree();
705 merge_stree(&final_states, stree);
706 free_stree(&stree);
708 FOR_EACH_SM(final_states, sm) {
709 __set_sm(sm);
710 } END_FOR_EACH_SM(sm);
712 free_stree(&final_states);
715 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
717 struct expression *call_expr = static_call_expr;
718 struct call_implies_callback *cb;
719 struct expression *arg = NULL;
720 int type;
721 int param;
723 if (argc != 5)
724 return 0;
726 type = atoi(argv[1]);
727 param = atoi(argv[2]);
729 FOR_EACH_PTR(call_implies_cb_list, cb) {
730 if (cb->type != type)
731 continue;
732 if (param != -1) {
733 arg = get_argument_from_call_expr(call_expr->args, param);
734 if (!arg)
735 continue;
737 cb->callback(arg, argv[3], argv[4]);
738 } END_FOR_EACH_PTR(cb);
740 return 0;
743 static void match_call_implies(struct expression *expr)
745 static_call_expr = expr;
746 sql_select_call_implies("function, type, parameter, key, value", expr,
747 call_implies_callbacks);
748 return;
751 static void print_initializer_list(struct expression_list *expr_list,
752 struct symbol *struct_type)
754 struct expression *expr;
755 struct symbol *base_type;
756 char struct_name[256];
758 FOR_EACH_PTR(expr_list, expr) {
759 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
760 print_initializer_list(expr->idx_expression->expr_list, struct_type);
761 continue;
763 if (expr->type != EXPR_IDENTIFIER)
764 continue;
765 if (!expr->expr_ident)
766 continue;
767 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
768 continue;
769 base_type = get_type(expr->ident_expression);
770 if (!base_type || base_type->type != SYM_FN)
771 continue;
772 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
773 struct_type->ident->name, expr->expr_ident->name);
774 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
775 struct_name);
776 } END_FOR_EACH_PTR(expr);
779 static void global_variable(struct symbol *sym)
781 struct symbol *struct_type;
783 if (!sym->ident)
784 return;
785 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
786 return;
787 struct_type = get_base_type(sym);
788 if (!struct_type)
789 return;
790 if (struct_type->type == SYM_ARRAY) {
791 struct_type = get_base_type(struct_type);
792 if (!struct_type)
793 return;
795 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
796 return;
797 print_initializer_list(sym->initializer->expr_list, struct_type);
800 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
802 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
805 static void call_return_state_hooks_conditional(struct expression *expr)
807 struct returned_state_callback *cb;
808 struct range_list *rl;
809 char *return_ranges;
810 int final_pass_orig = final_pass;
812 __push_fake_cur_stree();
814 final_pass = 0;
815 __split_whole_condition(expr->conditional);
816 final_pass = final_pass_orig;
818 if (get_implied_rl(expr->cond_true, &rl))
819 rl = cast_rl(cur_func_return_type(), rl);
820 else
821 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
822 return_ranges = show_rl(rl);
824 return_id++;
825 FOR_EACH_PTR(returned_state_callbacks, cb) {
826 cb->callback(return_id, return_ranges, expr);
827 } END_FOR_EACH_PTR(cb);
829 __push_true_states();
830 __use_false_states();
832 if (get_implied_rl(expr->cond_false, &rl))
833 rl = cast_rl(cur_func_return_type(), rl);
834 else
835 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
836 return_ranges = show_rl(rl);
838 return_id++;
839 FOR_EACH_PTR(returned_state_callbacks, cb) {
840 cb->callback(return_id, return_ranges, expr);
841 } END_FOR_EACH_PTR(cb);
843 __merge_true_states();
844 __free_fake_cur_stree();
847 static void call_return_state_hooks_compare(struct expression *expr)
849 struct returned_state_callback *cb;
850 char *return_ranges;
851 int final_pass_orig = final_pass;
853 __push_fake_cur_stree();
855 final_pass = 0;
856 __split_whole_condition(expr);
857 final_pass = final_pass_orig;
859 return_ranges = alloc_sname("1");
861 return_id++;
862 FOR_EACH_PTR(returned_state_callbacks, cb) {
863 cb->callback(return_id, return_ranges, expr);
864 } END_FOR_EACH_PTR(cb);
866 __push_true_states();
867 __use_false_states();
869 return_ranges = alloc_sname("0");;
870 return_id++;
871 FOR_EACH_PTR(returned_state_callbacks, cb) {
872 cb->callback(return_id, return_ranges, expr);
873 } END_FOR_EACH_PTR(cb);
875 __merge_true_states();
876 __free_fake_cur_stree();
879 static int split_helper(struct sm_state *sm, struct expression *expr)
881 struct returned_state_callback *cb;
882 struct range_list *rl;
883 char *return_ranges;
884 struct sm_state *tmp;
885 int ret = 0;
886 int nr_possible, nr_states;
887 char *compare_str;
888 char buf[128];
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 int call_return_state_hooks_split_possible(struct expression *expr)
938 struct returned_state_callback *cb;
939 struct range_list *rl;
940 char *return_ranges;
941 struct sm_state *sm;
942 struct sm_state *tmp;
943 int ret = 0;
944 int nr_possible, nr_states;
945 char *compare_str;
946 char buf[128];
948 if (!expr || expr_equal_to_param(expr, -1))
949 return 0;
951 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
952 if (!sm || !sm->merged)
953 return 0;
955 if (too_many_possible(sm))
956 return 0;
958 /* bail if it gets too complicated */
959 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
960 nr_states = stree_count(__get_cur_stree());
962 * the main thing option_info because we don't want to print a
963 * million lines of output. If someone else, like check_locking.c
964 * wants this data, then it doesn't cause a slow down to provide it.
966 if (option_info && nr_states * nr_possible >= 2000)
967 return 0;
970 FOR_EACH_PTR(sm->possible, tmp) {
971 if (tmp->merged)
972 continue;
974 ret = 1;
975 __push_fake_cur_stree();
977 overwrite_states_using_pool(tmp);
979 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
980 return_ranges = show_rl(rl);
982 compare_str = expr_lte_to_param(expr, -1);
983 if (compare_str) {
984 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
985 return_ranges = alloc_sname(buf);
988 return_id++;
989 FOR_EACH_PTR(returned_state_callbacks, cb) {
990 cb->callback(return_id, return_ranges, expr);
991 } END_FOR_EACH_PTR(cb);
993 __free_fake_cur_stree();
994 } END_FOR_EACH_PTR(tmp);
996 return ret;
999 static const char *get_return_ranges_str(struct expression *expr)
1001 struct range_list *rl;
1002 char *return_ranges;
1003 sval_t sval;
1004 char *compare_str;
1005 char *math_str;
1006 char buf[128];
1008 if (!expr)
1009 return alloc_sname("");
1011 if (get_implied_value(expr, &sval))
1012 return sval_to_str(sval);
1014 compare_str = expr_equal_to_param(expr, -1);
1015 math_str = get_value_in_terms_of_parameter_math(expr);
1017 if (get_implied_rl(expr, &rl)) {
1018 rl = cast_rl(cur_func_return_type(), rl);
1019 return_ranges = show_rl(rl);
1020 } else {
1021 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1022 return_ranges = show_rl(rl);
1025 if (compare_str) {
1026 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1027 return alloc_sname(buf);
1030 if (math_str) {
1031 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1032 return alloc_sname(buf);
1035 compare_str = expr_lte_to_param(expr, -1);
1036 if (compare_str) {
1037 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1038 return alloc_sname(buf);
1040 return return_ranges;
1043 static int is_boolean(struct expression *expr)
1045 struct range_list *rl;
1047 if (!get_implied_rl(expr, &rl))
1048 return 0;
1049 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1050 return 1;
1051 return 0;
1054 static int is_conditional(struct expression *expr)
1056 if (!expr)
1057 return 0;
1058 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1059 return 1;
1060 return 0;
1063 static int splitable_function_call(struct expression *expr)
1065 struct sm_state *sm;
1066 char buf[64];
1068 if (!expr || expr->type != EXPR_CALL)
1069 return 0;
1070 snprintf(buf, sizeof(buf), "return %p", expr);
1071 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1072 return split_helper(sm, expr);
1075 static void call_return_state_hooks(struct expression *expr)
1077 struct returned_state_callback *cb;
1078 const char *return_ranges;
1079 int nr_states;
1081 expr = strip_expr(expr);
1083 if (is_condition(expr) || is_boolean(expr)) {
1084 call_return_state_hooks_compare(expr);
1085 return;
1086 } else if (is_conditional(expr)) {
1087 call_return_state_hooks_conditional(expr);
1088 return;
1089 } else if (call_return_state_hooks_split_possible(expr)) {
1090 return;
1091 } else if (splitable_function_call(expr)) {
1092 return;
1095 return_ranges = get_return_ranges_str(expr);
1097 return_id++;
1098 nr_states = stree_count(__get_cur_stree());
1099 if (nr_states >= 10000) {
1100 match_return_info(return_id, (char *)return_ranges, expr);
1101 return;
1103 FOR_EACH_PTR(returned_state_callbacks, cb) {
1104 cb->callback(return_id, (char *)return_ranges, expr);
1105 } END_FOR_EACH_PTR(cb);
1108 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1110 struct returned_member_callback *cb;
1111 struct stree *stree;
1112 struct sm_state *sm;
1113 struct symbol *type;
1114 char *name;
1115 char member_name[256];
1116 int len;
1118 type = get_type(expr);
1119 if (!type || type->type != SYM_PTR)
1120 return;
1121 name = expr_to_var(expr);
1122 if (!name)
1123 return;
1125 member_name[sizeof(member_name) - 1] = '\0';
1126 strcpy(member_name, "$$");
1128 len = strlen(name);
1129 FOR_EACH_PTR(returned_member_callbacks, cb) {
1130 stree = __get_cur_stree();
1131 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1132 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1133 strcpy(member_name, "*$$");
1134 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1135 continue;
1137 if (strncmp(sm->name, name, len) != 0)
1138 continue;
1139 if (strncmp(sm->name + len, "->", 2) != 0)
1140 continue;
1141 strcpy(member_name, "$$");
1142 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
1143 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1144 } END_FOR_EACH_SM(sm);
1145 } END_FOR_EACH_PTR(cb);
1147 free_string(name);
1150 static void reset_memdb(void)
1152 mem_sql(NULL, NULL, "delete from caller_info;");
1153 mem_sql(NULL, NULL, "delete from return_states;");
1154 mem_sql(NULL, NULL, "delete from call_implies;");
1157 static void match_end_func_info(struct symbol *sym)
1159 if (__path_is_null())
1160 return;
1161 call_return_state_hooks(NULL);
1162 if (!__inline_fn)
1163 reset_memdb();
1166 static void init_memdb(void)
1168 char *err = NULL;
1169 int rc;
1170 const char *schema_files[] = {
1171 "db/db.schema",
1172 "db/caller_info.schema",
1173 "db/return_states.schema",
1174 "db/function_type_size.schema",
1175 "db/type_size.schema",
1176 "db/call_implies.schema",
1177 "db/function_ptr.schema",
1178 "db/local_values.schema",
1179 "db/function_type_value.schema",
1180 "db/type_value.schema",
1181 "db/function_type_info.schema",
1182 "db/data_info.schema",
1184 static char buf[4096];
1185 int fd;
1186 int ret;
1187 int i;
1189 rc = sqlite3_open(":memory:", &mem_db);
1190 if (rc != SQLITE_OK) {
1191 printf("Error starting In-Memory database.");
1192 return;
1195 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1196 fd = open_data_file(schema_files[i]);
1197 if (fd < 0) {
1198 mem_db = NULL;
1199 return;
1201 ret = read(fd, buf, sizeof(buf));
1202 if (ret == sizeof(buf)) {
1203 printf("Schema file too large: %s (limit %zd bytes)",
1204 schema_files[i], sizeof(buf));
1206 buf[ret] = '\0';
1207 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1208 if (rc != SQLITE_OK) {
1209 fprintf(stderr, "SQL error #2: %s\n", err);
1210 fprintf(stderr, "%s\n", buf);
1215 void open_smatch_db(void)
1217 int rc;
1219 if (option_no_db)
1220 return;
1222 init_memdb();
1224 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1225 if (rc != SQLITE_OK) {
1226 option_no_db = 1;
1227 return;
1229 return;
1232 static void register_common_funcs(void)
1234 struct token *token;
1235 char *func;
1236 char filename[256];
1238 if (option_project == PROJ_NONE)
1239 strcpy(filename, "common_functions");
1240 else
1241 snprintf(filename, 256, "%s.common_functions", option_project_str);
1243 token = get_tokens_file(filename);
1244 if (!token)
1245 return;
1246 if (token_type(token) != TOKEN_STREAMBEGIN)
1247 return;
1248 token = token->next;
1249 while (token_type(token) != TOKEN_STREAMEND) {
1250 if (token_type(token) != TOKEN_IDENT)
1251 return;
1252 func = alloc_string(show_ident(token->ident));
1253 add_ptr_list(&common_funcs, func);
1254 token = token->next;
1256 clear_token_alloc();
1260 void register_definition_db_callbacks(int id)
1262 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1263 add_hook(&global_variable, BASE_HOOK);
1264 add_hook(&global_variable, DECLARATION_HOOK);
1265 add_split_return_callback(match_return_info);
1266 add_split_return_callback(print_returned_struct_members);
1267 add_hook(&call_return_state_hooks, RETURN_HOOK);
1268 add_hook(&match_end_func_info, END_FUNC_HOOK);
1270 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1271 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1273 register_common_funcs();
1276 void register_db_call_marker(int id)
1278 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1281 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1283 struct expression *arg;
1284 char *name = NULL;
1285 char member_name[256];
1287 *sym = NULL;
1289 if (param == -1) {
1290 const char *star = "";
1292 if (expr->type != EXPR_ASSIGNMENT)
1293 return NULL;
1294 name = expr_to_var_sym(expr->left, sym);
1295 if (!name)
1296 return NULL;
1297 if (key[0] == '*') {
1298 star = "*";
1299 key++;
1301 if (strncmp(key, "$$", 2) != 0)
1302 return name;
1303 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1304 free_string(name);
1305 return alloc_string(member_name);
1308 while (expr->type == EXPR_ASSIGNMENT)
1309 expr = strip_expr(expr->right);
1310 if (expr->type != EXPR_CALL)
1311 return NULL;
1313 arg = get_argument_from_call_expr(expr->args, param);
1314 if (!arg)
1315 return NULL;
1317 return get_variable_from_key(arg, key, sym);
1320 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1322 char buf[256];
1323 char *tmp;
1325 if (strcmp(key, "$$") == 0)
1326 return expr_to_var_sym(arg, sym);
1328 if (strcmp(key, "*$$") == 0) {
1329 if (arg->type == EXPR_PREOP && arg->op == '&') {
1330 arg = strip_expr(arg->unop);
1331 return expr_to_var_sym(arg, sym);
1332 } else {
1333 tmp = expr_to_var_sym(arg, sym);
1334 if (!tmp)
1335 return NULL;
1336 snprintf(buf, sizeof(buf), "*%s", tmp);
1337 free_string(tmp);
1338 return alloc_string(buf);
1342 if (arg->type == EXPR_PREOP && arg->op == '&') {
1343 arg = strip_expr(arg->unop);
1344 tmp = expr_to_var_sym(arg, sym);
1345 if (!tmp)
1346 return NULL;
1347 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1348 return alloc_string(buf);
1351 tmp = expr_to_var_sym(arg, sym);
1352 if (!tmp)
1353 return NULL;
1354 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1355 free_string(tmp);
1356 return alloc_string(buf);
1359 const char *get_param_name(struct sm_state *sm)
1361 char *param_name;
1362 int name_len;
1363 static char buf[256];
1365 if (!sm->sym->ident)
1366 return NULL;
1368 param_name = sm->sym->ident->name;
1369 name_len = strlen(param_name);
1371 if (strcmp(sm->name, param_name) == 0) {
1372 return "$$";
1373 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1374 strncmp(sm->name, param_name, name_len) == 0) {
1375 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1376 return buf;
1377 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1378 return "*$$";
1380 return NULL;
1383 char *get_data_info_name(struct expression *expr)
1385 struct symbol *sym;
1386 char *name;
1387 char buf[256];
1388 char *ret = NULL;
1390 expr = strip_expr(expr);
1391 name = get_member_name(expr);
1392 if (name)
1393 return name;
1394 name = expr_to_var_sym(expr, &sym);
1395 if (!name || !sym)
1396 goto free;
1397 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1398 goto free;
1399 if (sym->ctype.modifiers & MOD_STATIC)
1400 snprintf(buf, sizeof(buf), "static %s", name);
1401 else
1402 snprintf(buf, sizeof(buf), "global %s", name);
1403 ret = alloc_sname(buf);
1404 free:
1405 free_string(name);
1406 return ret;