flow: introduce outside_of_function() to fix global initializer handling
[smatch.git] / smatch_db.c
blob0216e56eb2922cb13d9cc29a8467102b2baffc73
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include <unistd.h>
14 #include "smatch.h"
15 #include "smatch_slist.h"
16 #include "smatch_extra.h"
18 static sqlite3 *db;
19 static sqlite3 *mem_db;
21 #define sql_insert(table, values...) \
22 do { \
23 if (!mem_db) \
24 break; \
25 if (__inline_fn) { \
26 char buf[1024]; \
27 char *err, *p = buf; \
28 int rc; \
30 p += snprintf(p, buf + sizeof(buf) - p, \
31 "insert into %s values (", #table); \
32 p += snprintf(p, buf + sizeof(buf) - p, values); \
33 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
34 sm_debug("in-mem: %s\n", buf); \
35 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
36 if (rc != SQLITE_OK) { \
37 fprintf(stderr, "SQL error #2: %s\n", err); \
38 fprintf(stderr, "SQL: '%s'\n", buf); \
39 } \
40 break; \
41 } \
42 if (option_info) { \
43 sm_prefix(); \
44 sm_printf("SQL: insert into " #table " values (" values); \
45 sm_printf(");\n"); \
46 } \
47 } while (0)
49 struct def_callback {
50 int hook_type;
51 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
53 ALLOCATOR(def_callback, "definition db hook callbacks");
54 DECLARE_PTR_LIST(callback_list, struct def_callback);
55 static struct callback_list *callbacks;
57 struct member_info_callback {
58 int owner;
59 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
61 ALLOCATOR(member_info_callback, "caller_info callbacks");
62 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
63 static struct member_info_cb_list *member_callbacks;
65 struct returned_state_callback {
66 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
68 ALLOCATOR(returned_state_callback, "returned state callbacks");
69 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
70 static struct returned_state_cb_list *returned_state_callbacks;
72 struct returned_member_callback {
73 int owner;
74 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
76 ALLOCATOR(returned_member_callback, "returned member callbacks");
77 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
78 static struct returned_member_cb_list *returned_member_callbacks;
80 struct call_implies_callback {
81 int type;
82 void (*callback)(struct expression *arg, char *value);
84 ALLOCATOR(call_implies_callback, "call_implies callbacks");
85 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
86 static struct call_implies_cb_list *call_implies_cb_list;
88 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
90 char *err = NULL;
91 int rc;
93 if (option_no_db || !db)
94 return;
96 rc = sqlite3_exec(db, sql, callback, 0, &err);
97 if (rc != SQLITE_OK) {
98 fprintf(stderr, "SQL error #2: %s\n", err);
99 fprintf(stderr, "SQL: '%s'\n", sql);
103 void sql_mem_exec(int (*callback)(void*, int, char**, char**), const char *sql)
105 char *err = NULL;
106 int rc;
108 if (!mem_db)
109 return;
111 rc = sqlite3_exec(mem_db, sql, callback, 0, &err);
112 if (rc != SQLITE_OK) {
113 fprintf(stderr, "SQL error #2: %s\n", err);
114 fprintf(stderr, "SQL: '%s'\n", sql);
118 void sql_insert_return_states(int return_id, const char *return_ranges,
119 int type, int param, const char *key, const char *value)
121 if (key && strlen(key) >= 80)
122 return;
123 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
124 get_base_file(), get_function(), (unsigned long)__inline_fn,
125 return_id, return_ranges, fn_static(), type, param, key, value);
128 static struct string_list *common_funcs;
129 static int is_common_function(const char *fn)
131 char *tmp;
133 if (strncmp(fn, "__builtin_", 10) == 0)
134 return 1;
136 FOR_EACH_PTR(common_funcs, tmp) {
137 if (strcmp(tmp, fn) == 0)
138 return 1;
139 } END_FOR_EACH_PTR(tmp);
141 return 0;
144 void sql_insert_caller_info(struct expression *call, int type,
145 int param, const char *key, const char *value)
147 char *fn;
149 if (!option_info && !__inline_call)
150 return;
152 if (key && strlen(key) >= 80)
153 return;
155 fn = get_fnptr_name(call->fn);
156 if (!fn)
157 return;
159 if (__inline_call) {
160 mem_sql(NULL,
161 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
162 get_base_file(), get_function(), fn, (unsigned long)call,
163 is_static(call->fn), type, param, key, value);
166 if (!option_info)
167 return;
169 if (is_common_function(fn))
170 return;
172 sm_msg("SQL_caller_info: insert into caller_info values ("
173 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
174 get_base_file(), get_function(), fn, is_static(call->fn),
175 type, param, key, value);
177 free_string(fn);
180 void sql_insert_function_ptr(const char *fn, const char *struct_name)
182 sql_insert(function_ptr, "'%s', '%s', '%s'", get_base_file(), fn,
183 struct_name);
186 void sql_insert_call_implies(int type, int param, int value)
188 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
189 get_function(), (unsigned long)__inline_fn, fn_static(),
190 type, param, value);
193 void sql_insert_type_size(const char *member, int size)
195 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
198 void sql_insert_local_values(const char *name, const char *value)
200 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
203 static char *get_static_filter(struct symbol *sym)
205 static char sql_filter[1024];
207 if (sym->ctype.modifiers & MOD_STATIC) {
208 snprintf(sql_filter, sizeof(sql_filter),
209 "file = '%s' and function = '%s' and static = '1'",
210 get_base_file(), sym->ident->name);
211 } else {
212 snprintf(sql_filter, sizeof(sql_filter),
213 "function = '%s' and static = '0'", sym->ident->name);
216 return sql_filter;
219 static int row_count;
220 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
222 if (argc != 1)
223 return 0;
224 row_count = atoi(argv[0]);
225 return 0;
228 void sql_select_return_states(const char *cols, struct expression *call,
229 int (*callback)(void*, int, char**, char**))
231 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
232 return;
234 if (inlinable(call->fn)) {
235 mem_sql(callback,
236 "select %s from return_states where call_id = '%lu';",
237 cols, (unsigned long)call);
238 return;
241 row_count = 0;
242 run_sql(get_row_count, "select count(*) from return_states where %s;",
243 get_static_filter(call->fn->symbol));
244 if (row_count > 1000)
245 return;
247 run_sql(callback, "select %s from return_states where %s;",
248 cols, get_static_filter(call->fn->symbol));
251 void sql_select_call_implies(const char *cols, struct expression *call,
252 int (*callback)(void*, int, char**, char**))
254 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
255 return;
257 if (inlinable(call->fn)) {
258 mem_sql(callback,
259 "select %s from call_implies where call_id = '%lu';",
260 cols, (unsigned long)call);
261 return;
264 run_sql(callback, "select %s from call_implies where %s;",
265 cols, get_static_filter(call->fn->symbol));
268 void sql_select_caller_info(const char *cols, struct symbol *sym,
269 int (*callback)(void*, int, char**, char**))
271 if (__inline_fn) {
272 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
273 cols, (unsigned long)__inline_fn);
274 return;
277 run_sql(callback,
278 "select %s from caller_info where %s order by call_id;",
279 cols, get_static_filter(sym));
282 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
284 struct def_callback *def_callback = __alloc_def_callback(0);
286 def_callback->hook_type = type;
287 def_callback->callback = callback;
288 add_ptr_list(&callbacks, def_callback);
292 * These call backs are used when the --info option is turned on to print struct
293 * member information. For example foo->bar could have a state in
294 * smatch_extra.c and also check_user.c.
296 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
298 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
300 member_callback->owner = owner;
301 member_callback->callback = callback;
302 add_ptr_list(&member_callbacks, member_callback);
305 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
307 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
309 callback->callback = fn;
310 add_ptr_list(&returned_state_callbacks, callback);
313 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
315 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
317 member_callback->owner = owner;
318 member_callback->callback = callback;
319 add_ptr_list(&returned_member_callbacks, member_callback);
322 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *value))
324 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
326 cb->type = type;
327 cb->callback = callback;
328 add_ptr_list(&call_implies_cb_list, cb);
331 static struct expression *static_call_expr;
332 static struct symbol *return_type;
333 static struct range_list *return_range_list;
334 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
336 struct range_list *rl;
338 if (argc != 1)
339 return 0;
340 call_results_to_rl(static_call_expr, return_type, argv[0], &rl);
341 return_range_list = rl_union(return_range_list, rl);
342 return 0;
345 struct range_list *db_return_vals(struct expression *expr)
347 static_call_expr = expr;
348 return_type = get_type(expr);
349 if (!return_type)
350 return NULL;
351 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
352 return NULL;
354 return_range_list = NULL;
355 if (inlinable(expr->fn)) {
356 mem_sql(db_return_callback,
357 "select distinct return from return_states where call_id = '%lu';",
358 (unsigned long)expr);
359 } else {
360 run_sql(db_return_callback,
361 "select distinct return from return_states where %s;",
362 get_static_filter(expr->fn->symbol));
364 return return_range_list;
367 static void match_call_marker(struct expression *expr)
370 * we just want to record something in the database so that if we have
371 * two calls like: frob(4); frob(some_unkown); then on the receiving
372 * side we know that sometimes frob is called with unknown parameters.
375 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
378 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
379 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
381 struct sm_state *sm;
382 char *name;
383 struct symbol *sym;
384 int len;
385 char printed_name[256];
386 int is_address = 0;
388 expr = strip_expr(expr);
389 if (expr->type == EXPR_PREOP && expr->op == '&') {
390 expr = strip_expr(expr->unop);
391 is_address = 1;
394 name = expr_to_var_sym(expr, &sym);
395 if (!name || !sym)
396 goto free;
398 len = strlen(name);
399 FOR_EACH_PTR(slist, sm) {
400 if (sm->sym != sym)
401 continue;
402 if (strcmp(name, sm->name) == 0) {
403 if (is_address)
404 snprintf(printed_name, sizeof(printed_name), "*$$");
405 else /* these are already handled. fixme: handle them here */
406 continue;
407 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
408 snprintf(printed_name, sizeof(printed_name), "*$$");
409 } else if (strncmp(name, sm->name, len) == 0) {
410 if (is_address)
411 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
412 else
413 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
414 } else {
415 continue;
417 callback(call, param, printed_name, sm->state);
418 } END_FOR_EACH_PTR(sm);
419 free:
420 free_string(name);
423 static void match_call_info(struct expression *call)
425 struct member_info_callback *cb;
426 struct expression *arg;
427 struct state_list *slist;
428 char *name;
429 int i;
431 name = get_fnptr_name(call->fn);
432 if (!name)
433 return;
435 FOR_EACH_PTR(member_callbacks, cb) {
436 slist = get_all_states(cb->owner);
437 i = 0;
438 FOR_EACH_PTR(call->args, arg) {
439 print_struct_members(call, arg, i, slist, cb->callback);
440 i++;
441 } END_FOR_EACH_PTR(arg);
442 free_slist(&slist);
443 } END_FOR_EACH_PTR(cb);
445 free_string(name);
448 static int get_param(int param, char **name, struct symbol **sym)
450 struct symbol *arg;
451 int i;
453 i = 0;
454 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
456 * this is a temporary hack to work around a bug (I think in sparse?)
457 * 2.6.37-rc1:fs/reiserfs/journal.o
458 * If there is a function definition without parameter name found
459 * after a function implementation then it causes a crash.
460 * int foo() {}
461 * int bar(char *);
463 if (arg->ident->name < (char *)100)
464 continue;
465 if (i == param && arg->ident->name) {
466 *name = arg->ident->name;
467 *sym = arg;
468 return TRUE;
470 i++;
471 } END_FOR_EACH_PTR(arg);
473 return FALSE;
476 static struct state_list *final_states;
477 static int prev_func_id = -1;
478 static int db_callback(void *unused, int argc, char **argv, char **azColName)
480 int func_id;
481 long type;
482 long param;
483 char *name = NULL;
484 struct symbol *sym = NULL;
485 struct def_callback *def_callback;
486 struct state_list *slist;
488 if (argc != 5)
489 return 0;
491 func_id = atoi(argv[0]);
492 errno = 0;
493 type = strtol(argv[1], NULL, 10);
494 param = strtol(argv[2], NULL, 10);
495 if (errno)
496 return 0;
498 if (prev_func_id == -1)
499 prev_func_id = func_id;
500 if (func_id != prev_func_id) {
501 slist = __pop_fake_cur_slist();
502 merge_slist(&final_states, slist);
503 free_slist(&slist);
504 __push_fake_cur_slist();
505 __unnullify_path();
506 prev_func_id = func_id;
509 if (type == INTERNAL)
510 return 0;
511 if (param >= 0 && !get_param(param, &name, &sym))
512 return 0;
514 FOR_EACH_PTR(callbacks, def_callback) {
515 if (def_callback->hook_type == type)
516 def_callback->callback(name, sym, argv[3], argv[4]);
517 } END_FOR_EACH_PTR(def_callback);
519 return 0;
522 static void get_direct_callers(struct symbol *sym)
524 sql_select_caller_info("call_id, type, parameter, key, value", sym,
525 db_callback);
528 static struct string_list *ptr_names_done;
529 static struct string_list *ptr_names;
531 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
533 insert_string(&ptr_names, alloc_string(argv[0]));
534 return 0;
537 static char *get_next_ptr_name(void)
539 char *ptr;
541 FOR_EACH_PTR(ptr_names, ptr) {
542 if (list_has_string(ptr_names_done, ptr))
543 continue;
544 insert_string(&ptr_names_done, ptr);
545 return ptr;
546 } END_FOR_EACH_PTR(ptr);
547 return NULL;
550 static void get_ptr_names(const char *file, const char *name)
552 char sql_filter[1024];
553 int before, after;
555 if (file) {
556 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
557 file, name);
558 } else {
559 snprintf(sql_filter, 1024, "function = '%s';", name);
562 before = ptr_list_size((struct ptr_list *)ptr_names);
564 run_sql(get_ptr_name,
565 "select distinct ptr from function_ptr where %s",
566 sql_filter);
568 after = ptr_list_size((struct ptr_list *)ptr_names);
569 if (before == after)
570 return;
572 while ((name = get_next_ptr_name()))
573 get_ptr_names(NULL, name);
576 static void get_function_pointer_callers(struct symbol *sym)
578 char *ptr;
580 if (sym->ctype.modifiers & MOD_STATIC)
581 get_ptr_names(get_base_file(), sym->ident->name);
582 else
583 get_ptr_names(NULL, sym->ident->name);
585 FOR_EACH_PTR(ptr_names, ptr) {
586 run_sql(db_callback, "select call_id, type, parameter, key, value"
587 " from caller_info where function = '%s' order by call_id",
588 ptr);
589 free_string(ptr);
590 } END_FOR_EACH_PTR(ptr);
592 __free_ptr_list((struct ptr_list **)&ptr_names);
593 __free_ptr_list((struct ptr_list **)&ptr_names_done);
596 static void match_data_from_db(struct symbol *sym)
598 struct sm_state *sm;
599 struct state_list *slist;
601 if (!sym || !sym->ident || !sym->ident->name)
602 return;
604 __push_fake_cur_slist();
605 __unnullify_path();
606 prev_func_id = -1;
608 get_direct_callers(sym);
609 if (!__inline_fn)
610 get_function_pointer_callers(sym);
612 slist = __pop_fake_cur_slist();
613 merge_slist(&final_states, slist);
614 free_slist(&slist);
616 FOR_EACH_PTR(final_states, sm) {
617 __set_sm(sm);
618 } END_FOR_EACH_PTR(sm);
620 free_slist(&final_states);
623 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
625 struct call_implies_callback *cb;
626 struct expression *arg = NULL;
627 int type;
628 int param;
630 if (argc != 4)
631 return 0;
633 type = atoi(argv[1]);
634 param = atoi(argv[2]);
636 FOR_EACH_PTR(call_implies_cb_list, cb) {
637 if (cb->type != type)
638 continue;
639 if (param != -1) {
640 arg = get_argument_from_call_expr(static_call_expr->args, param);
641 if (!arg)
642 continue;
644 cb->callback(arg, argv[3]);
645 } END_FOR_EACH_PTR(cb);
647 return 0;
650 static void match_call_implies(struct expression *expr)
652 static_call_expr = expr;
653 sql_select_call_implies("function, type, parameter, value", expr,
654 call_implies_callbacks);
655 return;
658 static void print_initializer_list(struct expression_list *expr_list,
659 struct symbol *struct_type)
661 struct expression *expr;
662 struct symbol *base_type;
663 char struct_name[256];
665 FOR_EACH_PTR(expr_list, expr) {
666 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
667 print_initializer_list(expr->idx_expression->expr_list, struct_type);
668 continue;
670 if (expr->type != EXPR_IDENTIFIER)
671 continue;
672 if (!expr->expr_ident)
673 continue;
674 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
675 continue;
676 base_type = get_type(expr->ident_expression);
677 if (!base_type || base_type->type != SYM_FN)
678 continue;
679 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
680 struct_type->ident->name, expr->expr_ident->name);
681 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
682 struct_name);
683 } END_FOR_EACH_PTR(expr);
686 static void global_variable(struct symbol *sym)
688 struct symbol *struct_type;
690 if (!sym->ident)
691 return;
692 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
693 return;
694 struct_type = get_base_type(sym);
695 if (!struct_type)
696 return;
697 if (struct_type->type == SYM_ARRAY) {
698 struct_type = get_base_type(struct_type);
699 if (!struct_type)
700 return;
702 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
703 return;
704 print_initializer_list(sym->initializer->expr_list, struct_type);
707 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
709 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
712 static int return_id;
713 static void match_function_def(struct symbol *sym)
715 if (!__inline_fn)
716 return_id = 0;
719 static void call_return_state_hooks_conditional(struct expression *expr)
721 struct returned_state_callback *cb;
722 struct range_list *rl;
723 char *return_ranges;
724 int final_pass_orig = final_pass;
726 __push_fake_cur_slist();
728 final_pass = 0;
729 __split_whole_condition(expr->conditional);
730 final_pass = final_pass_orig;
732 if (get_implied_rl(expr->cond_true, &rl))
733 rl = cast_rl(cur_func_return_type(), rl);
734 else
735 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
736 return_ranges = show_rl(rl);
738 return_id++;
739 FOR_EACH_PTR(returned_state_callbacks, cb) {
740 cb->callback(return_id, return_ranges, expr);
741 } END_FOR_EACH_PTR(cb);
743 __push_true_states();
744 __use_false_states();
746 if (get_implied_rl(expr->cond_false, &rl))
747 rl = cast_rl(cur_func_return_type(), rl);
748 else
749 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
750 return_ranges = show_rl(rl);
752 return_id++;
753 FOR_EACH_PTR(returned_state_callbacks, cb) {
754 cb->callback(return_id, return_ranges, expr);
755 } END_FOR_EACH_PTR(cb);
757 __merge_true_states();
758 __free_fake_cur_slist();
761 static void call_return_state_hooks_compare(struct expression *expr)
763 struct returned_state_callback *cb;
764 char *return_ranges;
765 int final_pass_orig = final_pass;
767 __push_fake_cur_slist();
769 final_pass = 0;
770 __split_whole_condition(expr);
771 final_pass = final_pass_orig;
773 return_ranges = alloc_sname("1");
775 return_id++;
776 FOR_EACH_PTR(returned_state_callbacks, cb) {
777 cb->callback(return_id, return_ranges, expr);
778 } END_FOR_EACH_PTR(cb);
780 __push_true_states();
781 __use_false_states();
783 return_ranges = alloc_sname("0");;
784 return_id++;
785 FOR_EACH_PTR(returned_state_callbacks, cb) {
786 cb->callback(return_id, return_ranges, expr);
787 } END_FOR_EACH_PTR(cb);
789 __merge_true_states();
790 __free_fake_cur_slist();
793 static int call_return_state_hooks_split_possible(struct expression *expr)
795 struct returned_state_callback *cb;
796 struct range_list *rl;
797 char *return_ranges;
798 struct sm_state *sm;
799 struct sm_state *tmp;
800 int ret = 0;
801 int nr_possible, nr_states;
802 char *compare_str;
803 char buf[128];
805 if (!expr || expr_equal_to_param(expr))
806 return 0;
808 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
809 if (!sm || !sm->merged)
810 return 0;
812 /* bail if it gets too complicated */
813 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
814 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
816 * the main thing option_info because we don't want to print a
817 * million lines of output. If someone else, like check_locking.c
818 * wants this data, then it doesn't cause a slow down to provide it.
820 if (option_info && nr_possible >= 100)
821 return 0;
822 if (option_info && nr_states * nr_possible >= 2000)
823 return 0;
825 compare_str = expr_lte_to_param(expr);
827 FOR_EACH_PTR(sm->possible, tmp) {
828 if (tmp->merged)
829 continue;
831 ret = 1;
832 __push_fake_cur_slist();
834 overwrite_states_using_pool(tmp);
836 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
837 return_ranges = show_rl(rl);
838 if (compare_str) {
839 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
840 return_ranges = alloc_sname(buf);
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 __free_fake_cur_slist();
849 } END_FOR_EACH_PTR(tmp);
851 return ret;
854 static char *get_return_ranges_str(struct expression *expr)
856 struct range_list *rl;
857 char *return_ranges;
858 char *compare_str;
859 char buf[128];
861 if (!expr)
862 return alloc_sname("");
863 compare_str = expr_equal_to_param(expr);
864 if (compare_str)
865 return compare_str;
867 if (get_implied_rl(expr, &rl)) {
868 rl = cast_rl(cur_func_return_type(), rl);
869 return_ranges = show_rl(rl);
870 } else {
871 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
872 return_ranges = show_rl(rl);
874 compare_str = expr_lte_to_param(expr);
875 if (compare_str) {
876 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
877 return alloc_sname(buf);
879 return return_ranges;
882 static int is_conditional(struct expression *expr)
884 if (!expr)
885 return 0;
886 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
887 return 1;
888 return 0;
891 static void call_return_state_hooks(struct expression *expr)
893 struct returned_state_callback *cb;
894 char *return_ranges;
895 int nr_states;
897 expr = strip_expr(expr);
899 if (is_condition(expr)) {
900 call_return_state_hooks_compare(expr);
901 return;
902 } else if (is_conditional(expr)) {
903 call_return_state_hooks_conditional(expr);
904 return;
905 } else if (call_return_state_hooks_split_possible(expr)) {
906 return;
909 return_ranges = get_return_ranges_str(expr);
911 return_id++;
912 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
913 if (nr_states >= 10000) {
914 match_return_info(return_id, return_ranges, expr);
915 return;
917 FOR_EACH_PTR(returned_state_callbacks, cb) {
918 cb->callback(return_id, return_ranges, expr);
919 } END_FOR_EACH_PTR(cb);
922 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
924 struct returned_member_callback *cb;
925 struct state_list *my_slist;
926 struct sm_state *sm;
927 struct symbol *type;
928 char *name;
929 char member_name[256];
930 int len;
932 type = get_type(expr);
933 if (!type || type->type != SYM_PTR)
934 return;
935 name = expr_to_var(expr);
936 if (!name)
937 return;
939 member_name[sizeof(member_name) - 1] = '\0';
940 strcpy(member_name, "$$");
942 len = strlen(name);
943 FOR_EACH_PTR(returned_member_callbacks, cb) {
944 my_slist = get_all_states(cb->owner);
945 FOR_EACH_PTR(my_slist, sm) {
946 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
947 strcpy(member_name, "*$$");
948 cb->callback(return_id, return_ranges, member_name, sm->state);
949 continue;
951 if (strncmp(sm->name, name, len) != 0)
952 continue;
953 if (strncmp(sm->name + len, "->", 2) != 0)
954 continue;
955 strcpy(member_name, "$$");
956 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
957 cb->callback(return_id, return_ranges, member_name, sm->state);
958 } END_FOR_EACH_PTR(sm);
959 free_slist(&my_slist);
960 } END_FOR_EACH_PTR(cb);
962 free_string(name);
965 static void reset_memdb(void)
967 mem_sql(NULL, "delete from caller_info;");
968 mem_sql(NULL, "delete from return_states;");
969 mem_sql(NULL, "delete from call_implies;");
972 static void match_end_func_info(struct symbol *sym)
974 if (__path_is_null())
975 return;
976 call_return_state_hooks(NULL);
977 if (!__inline_fn)
978 reset_memdb();
981 static void init_memdb(void)
983 char *err = NULL;
984 int rc;
985 const char *schema_files[] = {
986 "db/db.schema",
987 "db/caller_info.schema",
988 "db/return_states.schema",
989 "db/type_size.schema",
990 "db/call_implies.schema",
991 "db/function_ptr.schema",
992 "db/local_values.schema",
994 static char buf[4096];
995 int fd;
996 int ret;
997 int i;
999 rc = sqlite3_open(":memory:", &mem_db);
1000 if (rc != SQLITE_OK) {
1001 printf("Error starting In-Memory database.");
1002 return;
1005 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1006 fd = open_data_file(schema_files[i]);
1007 if (fd < 0) {
1008 mem_db = NULL;
1009 return;
1011 ret = read(fd, buf, sizeof(buf));
1012 if (ret == sizeof(buf)) {
1013 printf("Schema file too large: %s (limit %zd bytes)",
1014 schema_files[i], sizeof(buf));
1016 buf[ret] = '\0';
1017 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1018 if (rc != SQLITE_OK) {
1019 fprintf(stderr, "SQL error #2: %s\n", err);
1020 fprintf(stderr, "%s\n", buf);
1025 void open_smatch_db(void)
1027 int rc;
1029 if (option_no_db)
1030 return;
1032 init_memdb();
1034 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1035 if (rc != SQLITE_OK) {
1036 option_no_db = 1;
1037 return;
1039 return;
1042 static void register_common_funcs(void)
1044 struct token *token;
1045 char *func;
1046 char filename[256];
1048 if (option_project == PROJ_NONE)
1049 strcpy(filename, "common_functions");
1050 else
1051 snprintf(filename, 256, "%s.common_functions", option_project_str);
1053 token = get_tokens_file(filename);
1054 if (!token)
1055 return;
1056 if (token_type(token) != TOKEN_STREAMBEGIN)
1057 return;
1058 token = token->next;
1059 while (token_type(token) != TOKEN_STREAMEND) {
1060 if (token_type(token) != TOKEN_IDENT)
1061 return;
1062 func = alloc_string(show_ident(token->ident));
1063 add_ptr_list(&common_funcs, func);
1064 token = token->next;
1066 clear_token_alloc();
1070 void register_definition_db_callbacks(int id)
1072 add_hook(&match_function_def, FUNC_DEF_HOOK);
1074 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1075 add_hook(&global_variable, BASE_HOOK);
1076 add_hook(&global_variable, DECLARATION_HOOK);
1077 add_split_return_callback(match_return_info);
1078 add_split_return_callback(print_returned_struct_members);
1079 add_hook(&call_return_state_hooks, RETURN_HOOK);
1080 add_hook(&match_end_func_info, END_FUNC_HOOK);
1082 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1083 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1085 register_common_funcs();
1088 void register_db_call_marker(int id)
1090 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1093 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1095 struct expression *arg;
1096 char *name = NULL;
1097 char member_name[256];
1099 *sym = NULL;
1101 if (param == -1) {
1102 const char *star = "";
1104 if (expr->type != EXPR_ASSIGNMENT)
1105 return NULL;
1106 name = expr_to_var_sym(expr->left, sym);
1107 if (!name)
1108 return NULL;
1109 if (key[0] == '*') {
1110 star = "*";
1111 key++;
1113 if (strncmp(key, "$$", 2) != 0)
1114 return name;
1115 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 2);
1116 free_string(name);
1117 return alloc_string(member_name);
1120 while (expr->type == EXPR_ASSIGNMENT)
1121 expr = strip_expr(expr->right);
1122 if (expr->type != EXPR_CALL)
1123 return NULL;
1125 arg = get_argument_from_call_expr(expr->args, param);
1126 if (!arg)
1127 return NULL;
1129 return get_variable_from_key(arg, key, sym);
1132 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1134 char buf[256];
1135 char *tmp;
1137 if (strcmp(key, "$$") == 0)
1138 return expr_to_var_sym(arg, sym);
1140 if (strcmp(key, "*$$") == 0) {
1141 if (arg->type == EXPR_PREOP && arg->op == '&') {
1142 arg = strip_expr(arg->unop);
1143 return expr_to_var_sym(arg, sym);
1144 } else {
1145 tmp = expr_to_var_sym(arg, sym);
1146 if (!tmp)
1147 return NULL;
1148 snprintf(buf, sizeof(buf), "*%s", tmp);
1149 free_string(tmp);
1150 return alloc_string(buf);
1154 if (arg->type == EXPR_PREOP && arg->op == '&') {
1155 arg = strip_expr(arg->unop);
1156 tmp = expr_to_var_sym(arg, sym);
1157 if (!tmp)
1158 return NULL;
1159 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1160 return alloc_string(buf);
1163 tmp = expr_to_var_sym(arg, sym);
1164 if (!tmp)
1165 return NULL;
1166 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1167 free_string(tmp);
1168 return alloc_string(buf);
1171 const char *get_param_name(struct sm_state *sm)
1173 char *param_name;
1174 int name_len;
1175 static char buf[256];
1177 if (!sm->sym->ident)
1178 return NULL;
1180 param_name = sm->sym->ident->name;
1181 name_len = strlen(param_name);
1183 if (strcmp(sm->name, param_name) == 0) {
1184 return "$$";
1185 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1186 strncmp(sm->name, param_name, name_len) == 0) {
1187 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1188 return buf;
1189 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1190 return "*$$";
1192 return NULL;