comparison: handle divide "a = b / 2"
[smatch.git] / smatch_db.c
blob9be9f54eec71c94ce7b98e06ed71c629750e165b
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 (__inline_fn) { \
24 char buf[1024]; \
25 char *err, *p = buf; \
26 int rc; \
28 p += snprintf(p, buf + sizeof(buf) - p, \
29 "insert into %s values (", #table); \
30 p += snprintf(p, buf + sizeof(buf) - p, values); \
31 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
32 sm_debug("in-mem: %s\n", buf); \
33 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err); \
34 if (rc != SQLITE_OK) { \
35 fprintf(stderr, "SQL error #2: %s\n", err); \
36 fprintf(stderr, "SQL: '%s'\n", buf); \
37 } \
38 return; \
39 } \
40 if (option_info) { \
41 sm_prefix(); \
42 sm_printf("SQL: insert into " #table " values (" values); \
43 sm_printf(");\n"); \
44 } \
45 } while (0)
47 struct def_callback {
48 int hook_type;
49 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
51 ALLOCATOR(def_callback, "definition db hook callbacks");
52 DECLARE_PTR_LIST(callback_list, struct def_callback);
53 static struct callback_list *callbacks;
55 struct member_info_callback {
56 int owner;
57 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state);
59 ALLOCATOR(member_info_callback, "caller_info callbacks");
60 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
61 static struct member_info_cb_list *member_callbacks;
63 struct returned_state_callback {
64 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
66 ALLOCATOR(returned_state_callback, "returned state callbacks");
67 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
68 static struct returned_state_cb_list *returned_state_callbacks;
70 struct returned_member_callback {
71 int owner;
72 void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state);
74 ALLOCATOR(returned_member_callback, "returned member callbacks");
75 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
76 static struct returned_member_cb_list *returned_member_callbacks;
78 struct call_implies_callback {
79 int type;
80 void (*callback)(struct expression *arg, char *value);
82 ALLOCATOR(call_implies_callback, "call_implies callbacks");
83 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
84 static struct call_implies_cb_list *call_implies_cb_list;
86 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
88 char *err = NULL;
89 int rc;
91 if (option_no_db || !db)
92 return;
94 rc = sqlite3_exec(db, sql, callback, 0, &err);
95 if (rc != SQLITE_OK) {
96 fprintf(stderr, "SQL error #2: %s\n", err);
97 fprintf(stderr, "SQL: '%s'\n", sql);
101 void sql_mem_exec(int (*callback)(void*, int, char**, char**), const char *sql)
103 char *err = NULL;
104 int rc;
106 rc = sqlite3_exec(mem_db, sql, callback, 0, &err);
107 if (rc != SQLITE_OK) {
108 fprintf(stderr, "SQL error #2: %s\n", err);
109 fprintf(stderr, "SQL: '%s'\n", sql);
113 void sql_insert_return_states(int return_id, const char *return_ranges,
114 int type, int param, const char *key, const char *value)
116 if (key && strlen(key) >= 80)
117 return;
118 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
119 get_base_file(), get_function(), (unsigned long)__inline_fn,
120 return_id, return_ranges, fn_static(), type, param, key, value);
123 static struct string_list *common_funcs;
124 static int is_common_function(const char *fn)
126 char *tmp;
128 if (strncmp(fn, "__builtin_", 10) == 0)
129 return 1;
131 FOR_EACH_PTR(common_funcs, tmp) {
132 if (strcmp(tmp, fn) == 0)
133 return 1;
134 } END_FOR_EACH_PTR(tmp);
136 return 0;
139 void sql_insert_caller_info(struct expression *call, int type,
140 int param, const char *key, const char *value)
142 char *fn;
144 if (!option_info && !__inline_call)
145 return;
147 if (key && strlen(key) >= 80)
148 return;
150 fn = get_fnptr_name(call->fn);
151 if (!fn)
152 return;
154 if (__inline_call) {
155 mem_sql(NULL,
156 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
157 get_base_file(), get_function(), fn, (unsigned long)call,
158 is_static(call->fn), type, param, key, value);
161 if (!option_info)
162 return;
164 if (is_common_function(fn))
165 return;
167 sm_msg("SQL_caller_info: insert into caller_info values ("
168 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
169 get_base_file(), get_function(), fn, is_static(call->fn),
170 type, param, key, value);
172 free_string(fn);
175 void sql_insert_function_ptr(const char *fn, const char *struct_name)
177 sql_insert(function_ptr, "'%s', '%s', '%s'", get_base_file(), fn,
178 struct_name);
181 void sql_insert_call_implies(int type, int param, int value)
183 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
184 get_function(), (unsigned long)__inline_fn, fn_static(),
185 type, param, value);
188 void sql_insert_type_size(const char *member, int size)
190 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
193 void sql_insert_local_values(const char *name, const char *value)
195 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
198 static char *get_static_filter(struct symbol *sym)
200 static char sql_filter[1024];
202 if (sym->ctype.modifiers & MOD_STATIC) {
203 snprintf(sql_filter, sizeof(sql_filter),
204 "file = '%s' and function = '%s' and static = '1'",
205 get_base_file(), sym->ident->name);
206 } else {
207 snprintf(sql_filter, sizeof(sql_filter),
208 "function = '%s' and static = '0'", sym->ident->name);
211 return sql_filter;
214 static int row_count;
215 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
217 if (argc != 1)
218 return 0;
219 row_count = atoi(argv[0]);
220 return 0;
223 void sql_select_return_states(const char *cols, struct expression *call,
224 int (*callback)(void*, int, char**, char**))
226 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
227 return;
229 if (inlinable(call->fn)) {
230 mem_sql(callback,
231 "select %s from return_states where call_id = '%lu';",
232 cols, (unsigned long)call);
233 return;
236 row_count = 0;
237 run_sql(get_row_count, "select count(*) from return_states where %s;",
238 get_static_filter(call->fn->symbol));
239 if (row_count > 1000)
240 return;
242 run_sql(callback, "select %s from return_states where %s;",
243 cols, get_static_filter(call->fn->symbol));
246 void sql_select_call_implies(const char *cols, struct expression *call,
247 int (*callback)(void*, int, char**, char**))
249 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
250 return;
252 if (inlinable(call->fn)) {
253 mem_sql(callback,
254 "select %s from call_implies where call_id = '%lu';",
255 cols, (unsigned long)call);
256 return;
259 run_sql(callback, "select %s from call_implies where %s;",
260 cols, get_static_filter(call->fn->symbol));
263 void sql_select_caller_info(const char *cols, struct symbol *sym,
264 int (*callback)(void*, int, char**, char**))
266 if (__inline_fn) {
267 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
268 cols, (unsigned long)__inline_fn);
269 return;
272 run_sql(callback,
273 "select %s from caller_info where %s order by call_id;",
274 cols, get_static_filter(sym));
277 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
279 struct def_callback *def_callback = __alloc_def_callback(0);
281 def_callback->hook_type = type;
282 def_callback->callback = callback;
283 add_ptr_list(&callbacks, def_callback);
287 * These call backs are used when the --info option is turned on to print struct
288 * member information. For example foo->bar could have a state in
289 * smatch_extra.c and also check_user.c.
291 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
293 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
295 member_callback->owner = owner;
296 member_callback->callback = callback;
297 add_ptr_list(&member_callbacks, member_callback);
300 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
302 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
304 callback->callback = fn;
305 add_ptr_list(&returned_state_callbacks, callback);
308 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
310 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
312 member_callback->owner = owner;
313 member_callback->callback = callback;
314 add_ptr_list(&returned_member_callbacks, member_callback);
317 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *value))
319 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
321 cb->type = type;
322 cb->callback = callback;
323 add_ptr_list(&call_implies_cb_list, cb);
326 static struct expression *static_call_expr;
327 static struct symbol *return_type;
328 static struct range_list *return_range_list;
329 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
331 struct range_list *rl;
333 if (argc != 1)
334 return 0;
335 call_results_to_rl(static_call_expr, return_type, argv[0], &rl);
336 return_range_list = rl_union(return_range_list, rl);
337 return 0;
340 struct range_list *db_return_vals(struct expression *expr)
342 static_call_expr = expr;
343 return_type = get_type(expr);
344 if (!return_type)
345 return NULL;
346 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
347 return NULL;
349 return_range_list = NULL;
350 if (inlinable(expr->fn)) {
351 mem_sql(db_return_callback,
352 "select distinct return from return_states where call_id = '%lu';",
353 (unsigned long)expr);
354 } else {
355 run_sql(db_return_callback,
356 "select distinct return from return_states where %s;",
357 get_static_filter(expr->fn->symbol));
359 return return_range_list;
362 static void match_call_marker(struct expression *expr)
365 * we just want to record something in the database so that if we have
366 * two calls like: frob(4); frob(some_unkown); then on the receiving
367 * side we know that sometimes frob is called with unknown parameters.
370 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
373 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct state_list *slist,
374 void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
376 struct sm_state *sm;
377 char *name;
378 struct symbol *sym;
379 int len;
380 char printed_name[256];
381 int is_address = 0;
383 expr = strip_expr(expr);
384 if (expr->type == EXPR_PREOP && expr->op == '&') {
385 expr = strip_expr(expr->unop);
386 is_address = 1;
389 name = expr_to_var_sym(expr, &sym);
390 if (!name || !sym)
391 goto free;
393 len = strlen(name);
394 FOR_EACH_PTR(slist, sm) {
395 if (sm->sym != sym)
396 continue;
397 if (strcmp(name, sm->name) == 0) {
398 if (is_address)
399 snprintf(printed_name, sizeof(printed_name), "*$$");
400 else /* these are already handled. fixme: handle them here */
401 continue;
402 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
403 snprintf(printed_name, sizeof(printed_name), "*$$");
404 } else if (strncmp(name, sm->name, len) == 0) {
405 if (is_address)
406 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
407 else
408 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
409 } else {
410 continue;
412 callback(call, param, printed_name, sm->state);
413 } END_FOR_EACH_PTR(sm);
414 free:
415 free_string(name);
418 static void match_call_info(struct expression *call)
420 struct member_info_callback *cb;
421 struct expression *arg;
422 struct state_list *slist;
423 char *name;
424 int i;
426 name = get_fnptr_name(call->fn);
427 if (!name)
428 return;
430 FOR_EACH_PTR(member_callbacks, cb) {
431 slist = get_all_states(cb->owner);
432 i = 0;
433 FOR_EACH_PTR(call->args, arg) {
434 print_struct_members(call, arg, i, slist, cb->callback);
435 i++;
436 } END_FOR_EACH_PTR(arg);
437 free_slist(&slist);
438 } END_FOR_EACH_PTR(cb);
440 free_string(name);
443 static int get_param(int param, char **name, struct symbol **sym)
445 struct symbol *arg;
446 int i;
448 i = 0;
449 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
451 * this is a temporary hack to work around a bug (I think in sparse?)
452 * 2.6.37-rc1:fs/reiserfs/journal.o
453 * If there is a function definition without parameter name found
454 * after a function implementation then it causes a crash.
455 * int foo() {}
456 * int bar(char *);
458 if (arg->ident->name < (char *)100)
459 continue;
460 if (i == param && arg->ident->name) {
461 *name = arg->ident->name;
462 *sym = arg;
463 return TRUE;
465 i++;
466 } END_FOR_EACH_PTR(arg);
468 return FALSE;
471 static struct state_list *final_states;
472 static int prev_func_id = -1;
473 static int db_callback(void *unused, int argc, char **argv, char **azColName)
475 int func_id;
476 long type;
477 long param;
478 char *name = NULL;
479 struct symbol *sym = NULL;
480 struct def_callback *def_callback;
481 struct state_list *slist;
483 if (argc != 5)
484 return 0;
486 func_id = atoi(argv[0]);
487 errno = 0;
488 type = strtol(argv[1], NULL, 10);
489 param = strtol(argv[2], NULL, 10);
490 if (errno)
491 return 0;
493 if (prev_func_id == -1)
494 prev_func_id = func_id;
495 if (func_id != prev_func_id) {
496 slist = __pop_fake_cur_slist();
497 merge_slist(&final_states, slist);
498 free_slist(&slist);
499 __push_fake_cur_slist();
500 __unnullify_path();
501 prev_func_id = func_id;
504 if (type == INTERNAL)
505 return 0;
506 if (param >= 0 && !get_param(param, &name, &sym))
507 return 0;
509 FOR_EACH_PTR(callbacks, def_callback) {
510 if (def_callback->hook_type == type)
511 def_callback->callback(name, sym, argv[3], argv[4]);
512 } END_FOR_EACH_PTR(def_callback);
514 return 0;
517 static void get_direct_callers(struct symbol *sym)
519 sql_select_caller_info("call_id, type, parameter, key, value", sym,
520 db_callback);
523 static struct string_list *ptr_names_done;
524 static struct string_list *ptr_names;
526 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
528 insert_string(&ptr_names, alloc_string(argv[0]));
529 return 0;
532 static char *get_next_ptr_name(void)
534 char *ptr;
536 FOR_EACH_PTR(ptr_names, ptr) {
537 if (list_has_string(ptr_names_done, ptr))
538 continue;
539 insert_string(&ptr_names_done, ptr);
540 return ptr;
541 } END_FOR_EACH_PTR(ptr);
542 return NULL;
545 static void get_ptr_names(const char *file, const char *name)
547 char sql_filter[1024];
548 int before, after;
550 if (file) {
551 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
552 file, name);
553 } else {
554 snprintf(sql_filter, 1024, "function = '%s';", name);
557 before = ptr_list_size((struct ptr_list *)ptr_names);
559 run_sql(get_ptr_name,
560 "select distinct ptr from function_ptr where %s",
561 sql_filter);
563 after = ptr_list_size((struct ptr_list *)ptr_names);
564 if (before == after)
565 return;
567 while ((name = get_next_ptr_name()))
568 get_ptr_names(NULL, name);
571 static void get_function_pointer_callers(struct symbol *sym)
573 char *ptr;
575 if (sym->ctype.modifiers & MOD_STATIC)
576 get_ptr_names(get_base_file(), sym->ident->name);
577 else
578 get_ptr_names(NULL, sym->ident->name);
580 FOR_EACH_PTR(ptr_names, ptr) {
581 run_sql(db_callback, "select call_id, type, parameter, key, value"
582 " from caller_info where function = '%s' order by call_id",
583 ptr);
584 free_string(ptr);
585 } END_FOR_EACH_PTR(ptr);
587 __free_ptr_list((struct ptr_list **)&ptr_names);
588 __free_ptr_list((struct ptr_list **)&ptr_names_done);
591 static void match_data_from_db(struct symbol *sym)
593 struct sm_state *sm;
594 struct state_list *slist;
596 if (!sym || !sym->ident || !sym->ident->name)
597 return;
599 __push_fake_cur_slist();
600 __unnullify_path();
601 prev_func_id = -1;
603 get_direct_callers(sym);
604 if (!__inline_fn)
605 get_function_pointer_callers(sym);
607 slist = __pop_fake_cur_slist();
608 merge_slist(&final_states, slist);
609 free_slist(&slist);
611 FOR_EACH_PTR(final_states, sm) {
612 __set_sm(sm);
613 } END_FOR_EACH_PTR(sm);
615 free_slist(&final_states);
618 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
620 struct call_implies_callback *cb;
621 struct expression *arg = NULL;
622 int type;
623 int param;
625 if (argc != 4)
626 return 0;
628 type = atoi(argv[1]);
629 param = atoi(argv[2]);
631 FOR_EACH_PTR(call_implies_cb_list, cb) {
632 if (cb->type != type)
633 continue;
634 if (param != -1) {
635 arg = get_argument_from_call_expr(static_call_expr->args, param);
636 if (!arg)
637 continue;
639 cb->callback(arg, argv[3]);
640 } END_FOR_EACH_PTR(cb);
642 return 0;
645 static void match_call_implies(struct expression *expr)
647 static_call_expr = expr;
648 sql_select_call_implies("function, type, parameter, value", expr,
649 call_implies_callbacks);
650 return;
653 static void print_initializer_list(struct expression_list *expr_list,
654 struct symbol *struct_type)
656 struct expression *expr;
657 struct symbol *base_type;
658 char struct_name[256];
660 FOR_EACH_PTR(expr_list, expr) {
661 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
662 print_initializer_list(expr->idx_expression->expr_list, struct_type);
663 continue;
665 if (expr->type != EXPR_IDENTIFIER)
666 continue;
667 if (!expr->expr_ident)
668 continue;
669 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
670 continue;
671 base_type = get_type(expr->ident_expression);
672 if (!base_type || base_type->type != SYM_FN)
673 continue;
674 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
675 struct_type->ident->name, expr->expr_ident->name);
676 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
677 struct_name);
678 } END_FOR_EACH_PTR(expr);
681 static void global_variable(struct symbol *sym)
683 struct symbol *struct_type;
685 if (!sym->ident)
686 return;
687 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
688 return;
689 struct_type = get_base_type(sym);
690 if (!struct_type)
691 return;
692 if (struct_type->type == SYM_ARRAY) {
693 struct_type = get_base_type(struct_type);
694 if (!struct_type)
695 return;
697 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
698 return;
699 print_initializer_list(sym->initializer->expr_list, struct_type);
702 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
704 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
707 static int return_id;
708 static void match_function_def(struct symbol *sym)
710 if (!__inline_fn)
711 return_id = 0;
714 static void call_return_state_hooks_conditional(struct expression *expr)
716 struct returned_state_callback *cb;
717 struct range_list *rl;
718 char *return_ranges;
719 int final_pass_orig = final_pass;
721 __push_fake_cur_slist();
723 final_pass = 0;
724 __split_whole_condition(expr->conditional);
725 final_pass = final_pass_orig;
727 if (get_implied_rl(expr->cond_true, &rl))
728 rl = cast_rl(cur_func_return_type(), rl);
729 else
730 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
731 return_ranges = show_rl(rl);
733 return_id++;
734 FOR_EACH_PTR(returned_state_callbacks, cb) {
735 cb->callback(return_id, return_ranges, expr);
736 } END_FOR_EACH_PTR(cb);
738 __push_true_states();
739 __use_false_states();
741 if (get_implied_rl(expr->cond_false, &rl))
742 rl = cast_rl(cur_func_return_type(), rl);
743 else
744 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
745 return_ranges = show_rl(rl);
747 return_id++;
748 FOR_EACH_PTR(returned_state_callbacks, cb) {
749 cb->callback(return_id, return_ranges, expr);
750 } END_FOR_EACH_PTR(cb);
752 __merge_true_states();
753 __free_fake_cur_slist();
756 static void call_return_state_hooks_compare(struct expression *expr)
758 struct returned_state_callback *cb;
759 char *return_ranges;
760 int final_pass_orig = final_pass;
762 __push_fake_cur_slist();
764 final_pass = 0;
765 __split_whole_condition(expr);
766 final_pass = final_pass_orig;
768 return_ranges = alloc_sname("1");
770 return_id++;
771 FOR_EACH_PTR(returned_state_callbacks, cb) {
772 cb->callback(return_id, return_ranges, expr);
773 } END_FOR_EACH_PTR(cb);
775 __push_true_states();
776 __use_false_states();
778 return_ranges = alloc_sname("0");;
779 return_id++;
780 FOR_EACH_PTR(returned_state_callbacks, cb) {
781 cb->callback(return_id, return_ranges, expr);
782 } END_FOR_EACH_PTR(cb);
784 __merge_true_states();
785 __free_fake_cur_slist();
788 static int call_return_state_hooks_split_possible(struct expression *expr)
790 struct returned_state_callback *cb;
791 struct range_list *rl;
792 char *return_ranges;
793 struct sm_state *sm;
794 struct sm_state *tmp;
795 int ret = 0;
796 int nr_possible, nr_states;
797 char *compare_str;
798 char buf[128];
800 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
801 if (!sm || !sm->merged)
802 return 0;
804 /* bail if it gets too complicated */
805 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
806 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
808 * the main thing option_info because we don't want to print a
809 * million lines of output. If someone else, like check_locking.c
810 * wants this data, then it doesn't cause a slow down to provide it.
812 if (option_info && nr_possible >= 100)
813 return 0;
814 if (option_info && nr_states * nr_possible >= 2000)
815 return 0;
817 compare_str = expr_lte_to_param(expr);
819 FOR_EACH_PTR(sm->possible, tmp) {
820 if (tmp->merged)
821 continue;
823 ret = 1;
824 __push_fake_cur_slist();
826 overwrite_states_using_pool(tmp);
828 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
829 return_ranges = show_rl(rl);
830 if (compare_str) {
831 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
832 return_ranges = alloc_sname(buf);
835 return_id++;
836 FOR_EACH_PTR(returned_state_callbacks, cb) {
837 cb->callback(return_id, return_ranges, expr);
838 } END_FOR_EACH_PTR(cb);
840 __free_fake_cur_slist();
841 } END_FOR_EACH_PTR(tmp);
843 return ret;
846 static char *get_return_ranges_str(struct expression *expr)
848 struct range_list *rl;
849 char *return_ranges;
850 char *compare_str;
851 char buf[128];
853 if (!expr)
854 return alloc_sname("");
855 compare_str = expr_equal_to_param(expr);
856 if (compare_str)
857 return compare_str;
859 if (get_implied_rl(expr, &rl)) {
860 rl = cast_rl(cur_func_return_type(), rl);
861 return_ranges = show_rl(rl);
862 } else {
863 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
864 return_ranges = show_rl(rl);
866 compare_str = expr_lte_to_param(expr);
867 if (compare_str) {
868 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
869 return alloc_sname(buf);
871 return return_ranges;
874 static int is_conditional(struct expression *expr)
876 if (!expr)
877 return 0;
878 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
879 return 1;
880 return 0;
883 static void call_return_state_hooks(struct expression *expr)
885 struct returned_state_callback *cb;
886 char *return_ranges;
887 int nr_states;
889 expr = strip_expr(expr);
891 if (is_condition(expr)) {
892 call_return_state_hooks_compare(expr);
893 return;
894 } else if (is_conditional(expr)) {
895 call_return_state_hooks_conditional(expr);
896 return;
897 } else if (call_return_state_hooks_split_possible(expr)) {
898 return;
901 return_ranges = get_return_ranges_str(expr);
903 return_id++;
904 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
905 if (nr_states >= 10000) {
906 match_return_info(return_id, return_ranges, expr);
907 return;
909 FOR_EACH_PTR(returned_state_callbacks, cb) {
910 cb->callback(return_id, return_ranges, expr);
911 } END_FOR_EACH_PTR(cb);
914 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
916 struct returned_member_callback *cb;
917 struct state_list *my_slist;
918 struct sm_state *sm;
919 struct symbol *type;
920 char *name;
921 char member_name[256];
922 int len;
924 type = get_type(expr);
925 if (!type || type->type != SYM_PTR)
926 return;
927 type = get_real_base_type(type);
928 if (!type || type->type != SYM_STRUCT)
929 return;
930 name = expr_to_var(expr);
931 if (!name)
932 return;
934 member_name[sizeof(member_name) - 1] = '\0';
935 strcpy(member_name, "$$");
937 len = strlen(name);
938 FOR_EACH_PTR(returned_member_callbacks, cb) {
939 my_slist = get_all_states(cb->owner);
940 FOR_EACH_PTR(my_slist, sm) {
941 if (strncmp(sm->name, name, len) != 0)
942 continue;
943 if (strncmp(sm->name + len, "->", 2) != 0)
944 continue;
945 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
946 cb->callback(return_id, return_ranges, member_name, sm->state);
947 } END_FOR_EACH_PTR(sm);
948 free_slist(&my_slist);
949 } END_FOR_EACH_PTR(cb);
951 free_string(name);
954 static void reset_memdb(void)
956 mem_sql(NULL, "delete from caller_info;");
957 mem_sql(NULL, "delete from return_states;");
958 mem_sql(NULL, "delete from call_implies;");
961 static void match_end_func_info(struct symbol *sym)
963 if (__path_is_null())
964 return;
965 call_return_state_hooks(NULL);
966 if (!__inline_fn)
967 reset_memdb();
970 static void init_memdb(void)
972 char *err = NULL;
973 int rc;
974 const char *schema_files[] = {
975 "db/db.schema",
976 "db/caller_info.schema",
977 "db/return_states.schema",
978 "db/type_size.schema",
979 "db/call_implies.schema",
980 "db/function_ptr.schema",
981 "db/local_values.schema",
983 static char buf[4096];
984 int fd;
985 int ret;
986 int i;
988 rc = sqlite3_open(":memory:", &mem_db);
989 if (rc != SQLITE_OK) {
990 printf("Error starting In-Memory database.");
991 return;
994 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
995 fd = open_data_file(schema_files[i]);
996 if (fd < 0)
997 continue;
998 ret = read(fd, buf, sizeof(buf));
999 if (ret == sizeof(buf)) {
1000 printf("Schema file too large: %s (limit %zd bytes)",
1001 schema_files[i], sizeof(buf));
1003 buf[ret] = '\0';
1004 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
1005 if (rc != SQLITE_OK) {
1006 fprintf(stderr, "SQL error #2: %s\n", err);
1007 fprintf(stderr, "%s\n", buf);
1012 void open_smatch_db(void)
1014 int rc;
1016 if (option_no_db)
1017 return;
1019 init_memdb();
1021 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1022 if (rc != SQLITE_OK) {
1023 option_no_db = 1;
1024 return;
1026 return;
1029 static void register_common_funcs(void)
1031 struct token *token;
1032 char *func;
1033 char filename[256];
1035 if (option_project == PROJ_NONE)
1036 strcpy(filename, "common_functions");
1037 else
1038 snprintf(filename, 256, "%s.common_functions", option_project_str);
1040 token = get_tokens_file(filename);
1041 if (!token)
1042 return;
1043 if (token_type(token) != TOKEN_STREAMBEGIN)
1044 return;
1045 token = token->next;
1046 while (token_type(token) != TOKEN_STREAMEND) {
1047 if (token_type(token) != TOKEN_IDENT)
1048 return;
1049 func = alloc_string(show_ident(token->ident));
1050 add_ptr_list(&common_funcs, func);
1051 token = token->next;
1053 clear_token_alloc();
1057 void register_definition_db_callbacks(int id)
1059 add_hook(&match_function_def, FUNC_DEF_HOOK);
1061 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1062 add_hook(&global_variable, BASE_HOOK);
1063 add_hook(&global_variable, DECLARATION_HOOK);
1064 add_returned_state_callback(match_return_info);
1065 add_returned_state_callback(print_returned_struct_members);
1066 add_hook(&call_return_state_hooks, RETURN_HOOK);
1067 add_hook(&match_end_func_info, END_FUNC_HOOK);
1069 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1070 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1072 register_common_funcs();
1075 void register_db_call_marker(int id)
1077 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1080 char *return_state_to_var_sym(struct expression *expr, int param, char *key, struct symbol **sym)
1082 struct expression *arg;
1083 char *name = NULL;
1084 char member_name[256];
1086 *sym = NULL;
1088 if (param == -1) {
1089 if (expr->type != EXPR_ASSIGNMENT)
1090 return NULL;
1091 name = expr_to_var_sym(expr->left, sym);
1092 if (!name)
1093 return NULL;
1094 if (strncmp(key, "$$", 2) != 0)
1095 return name;
1096 snprintf(member_name, sizeof(member_name), "%s%s", name, key + 2);
1097 free_string(name);
1098 return alloc_string(member_name);
1101 while (expr->type == EXPR_ASSIGNMENT)
1102 expr = strip_expr(expr->right);
1103 if (expr->type != EXPR_CALL)
1104 return NULL;
1106 arg = get_argument_from_call_expr(expr->args, param);
1107 if (!arg)
1108 return NULL;
1110 return get_variable_from_key(arg, key, sym);
1113 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1115 char buf[256];
1116 char *tmp;
1118 if (strcmp(key, "$$") == 0)
1119 return expr_to_var_sym(arg, sym);
1121 if (strcmp(key, "*$$") == 0) {
1122 if (arg->type == EXPR_PREOP && arg->op == '&') {
1123 arg = strip_expr(arg->unop);
1124 return expr_to_var_sym(arg, sym);
1125 } else {
1126 tmp = expr_to_var_sym(arg, sym);
1127 if (!tmp)
1128 return NULL;
1129 snprintf(buf, sizeof(buf), "*%s", tmp);
1130 free_string(tmp);
1131 return alloc_string(buf);
1135 if (arg->type == EXPR_PREOP && arg->op == '&') {
1136 arg = strip_expr(arg->unop);
1137 tmp = expr_to_var_sym(arg, sym);
1138 if (!tmp)
1139 return NULL;
1140 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1141 return alloc_string(buf);
1144 tmp = expr_to_var_sym(arg, sym);
1145 if (!tmp)
1146 return NULL;
1147 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1148 free_string(tmp);
1149 return alloc_string(buf);
1152 const char *get_param_name(struct sm_state *sm)
1154 char *param_name;
1155 int name_len;
1156 static char buf[256];
1158 if (!sm->sym->ident)
1159 return NULL;
1161 param_name = sm->sym->ident->name;
1162 name_len = strlen(param_name);
1164 if (strcmp(sm->name, param_name) == 0) {
1165 return "$$";
1166 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1167 strncmp(sm->name, param_name, name_len) == 0) {
1168 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1169 return buf;
1170 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1171 return "*$$";
1173 return NULL;