comparison: create expr_equal/lte_to_param() functions
[smatch.git] / smatch_db.c
blob6785ac7b45fcd4c696bf920c7b53aa5976d5117e
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_return_values(const char *return_values)
183 sql_insert(return_values, "'%s', '%s', %lu, %d, '%s'", get_base_file(),
184 get_function(), (unsigned long)__inline_fn, fn_static(),
185 return_values);
188 void sql_insert_call_implies(int type, int param, int value)
190 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, %d", get_base_file(),
191 get_function(), (unsigned long)__inline_fn, fn_static(),
192 type, param, value);
195 void sql_insert_type_size(const char *member, int size)
197 sql_insert(type_size, "'%s', '%s', %d", get_base_file(), member, size);
200 void sql_insert_local_values(const char *name, const char *value)
202 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
205 static char *get_static_filter(struct symbol *sym)
207 static char sql_filter[1024];
209 if (sym->ctype.modifiers & MOD_STATIC) {
210 snprintf(sql_filter, sizeof(sql_filter),
211 "file = '%s' and function = '%s' and static = '1'",
212 get_base_file(), sym->ident->name);
213 } else {
214 snprintf(sql_filter, sizeof(sql_filter),
215 "function = '%s' and static = '0'", sym->ident->name);
218 return sql_filter;
221 static int row_count;
222 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
224 if (argc != 1)
225 return 0;
226 row_count = atoi(argv[0]);
227 return 0;
230 void sql_select_return_states(const char *cols, struct expression *call,
231 int (*callback)(void*, int, char**, char**))
233 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
234 return;
236 if (inlinable(call->fn)) {
237 mem_sql(callback,
238 "select %s from return_states where call_id = '%lu';",
239 cols, (unsigned long)call);
240 return;
243 row_count = 0;
244 run_sql(get_row_count, "select count(*) from return_states where %s;",
245 get_static_filter(call->fn->symbol));
246 if (row_count > 1000)
247 return;
249 run_sql(callback, "select %s from return_states where %s;",
250 cols, get_static_filter(call->fn->symbol));
253 void sql_select_call_implies(const char *cols, struct expression *call,
254 int (*callback)(void*, int, char**, char**))
256 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
257 return;
259 if (inlinable(call->fn)) {
260 mem_sql(callback,
261 "select %s from call_implies where call_id = '%lu';",
262 cols, (unsigned long)call);
263 return;
266 run_sql(callback, "select %s from call_implies where %s;",
267 cols, get_static_filter(call->fn->symbol));
270 void sql_select_caller_info(const char *cols, struct symbol *sym,
271 int (*callback)(void*, int, char**, char**))
273 if (__inline_fn) {
274 mem_sql(callback, "select %s from caller_info where call_id = %lu;",
275 cols, (unsigned long)__inline_fn);
276 return;
279 run_sql(callback,
280 "select %s from caller_info where %s order by call_id;",
281 cols, get_static_filter(sym));
284 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
286 struct def_callback *def_callback = __alloc_def_callback(0);
288 def_callback->hook_type = type;
289 def_callback->callback = callback;
290 add_ptr_list(&callbacks, def_callback);
294 * These call backs are used when the --info option is turned on to print struct
295 * member information. For example foo->bar could have a state in
296 * smatch_extra.c and also check_user.c.
298 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct smatch_state *state))
300 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
302 member_callback->owner = owner;
303 member_callback->callback = callback;
304 add_ptr_list(&member_callbacks, member_callback);
307 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
309 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
311 callback->callback = fn;
312 add_ptr_list(&returned_state_callbacks, callback);
315 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state))
317 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
319 member_callback->owner = owner;
320 member_callback->callback = callback;
321 add_ptr_list(&returned_member_callbacks, member_callback);
324 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
326 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
328 cb->type = type;
329 cb->callback = callback;
330 add_ptr_list(&call_implies_cb_list, cb);
333 static struct symbol *return_type;
334 static struct range_list *return_range_list;
335 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
337 struct range_list *rl;
339 if (argc != 1)
340 return 0;
341 str_to_rl(return_type, argv[0], &rl);
342 return_range_list = rl_union(return_range_list, rl);
343 return 0;
346 struct range_list *db_return_vals(struct expression *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 struct expression *call_implies_call_expr;
624 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
626 struct call_implies_callback *cb;
627 struct expression *arg = NULL;
628 int type;
629 int param;
631 if (argc != 4)
632 return 0;
634 type = atoi(argv[1]);
635 param = atoi(argv[2]);
637 FOR_EACH_PTR(call_implies_cb_list, cb) {
638 if (cb->type != type)
639 continue;
640 if (param != -1) {
641 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
642 if (!arg)
643 continue;
645 cb->callback(arg, argv[3]);
646 } END_FOR_EACH_PTR(cb);
648 return 0;
651 static void match_call_implies(struct expression *expr)
653 call_implies_call_expr = expr;
654 sql_select_call_implies("function, type, parameter, value", expr,
655 call_implies_callbacks);
656 return;
659 static void print_initializer_list(struct expression_list *expr_list,
660 struct symbol *struct_type)
662 struct expression *expr;
663 struct symbol *base_type;
664 char struct_name[256];
666 FOR_EACH_PTR(expr_list, expr) {
667 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
668 print_initializer_list(expr->idx_expression->expr_list, struct_type);
669 continue;
671 if (expr->type != EXPR_IDENTIFIER)
672 continue;
673 if (!expr->expr_ident)
674 continue;
675 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
676 continue;
677 base_type = get_type(expr->ident_expression);
678 if (!base_type || base_type->type != SYM_FN)
679 continue;
680 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
681 struct_type->ident->name, expr->expr_ident->name);
682 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
683 struct_name);
684 } END_FOR_EACH_PTR(expr);
687 static void global_variable(struct symbol *sym)
689 struct symbol *struct_type;
691 if (!sym->ident)
692 return;
693 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
694 return;
695 struct_type = get_base_type(sym);
696 if (!struct_type)
697 return;
698 if (struct_type->type == SYM_ARRAY) {
699 struct_type = get_base_type(struct_type);
700 if (!struct_type)
701 return;
703 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
704 return;
705 print_initializer_list(sym->initializer->expr_list, struct_type);
708 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
710 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
713 static int return_id;
714 static void match_function_def(struct symbol *sym)
716 if (!__inline_fn)
717 return_id = 0;
720 static void call_return_state_hooks_compare(struct expression *expr)
722 struct returned_state_callback *cb;
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);
730 final_pass = final_pass_orig;
732 return_ranges = alloc_sname("1");
734 return_id++;
735 FOR_EACH_PTR(returned_state_callbacks, cb) {
736 cb->callback(return_id, return_ranges, expr);
737 } END_FOR_EACH_PTR(cb);
739 __push_true_states();
740 __use_false_states();
742 return_ranges = alloc_sname("0");;
743 return_id++;
744 FOR_EACH_PTR(returned_state_callbacks, cb) {
745 cb->callback(return_id, return_ranges, expr);
746 } END_FOR_EACH_PTR(cb);
748 __merge_true_states();
749 __free_fake_cur_slist();
752 static int call_return_state_hooks_split_possible(struct expression *expr)
754 struct returned_state_callback *cb;
755 struct range_list *rl;
756 char *return_ranges;
757 struct sm_state *sm;
758 struct sm_state *tmp;
759 int ret = 0;
760 int nr_possible, nr_states;
762 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
763 if (!sm || !sm->merged)
764 return 0;
766 /* bail if it gets too complicated */
767 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
768 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
770 * the main thing option_info because we don't want to print a
771 * million lines of output. If someone else, like check_locking.c
772 * wants this data, then it doesn't cause a slow down to provide it.
774 if (option_info && nr_possible >= 100)
775 return 0;
776 if (option_info && nr_states * nr_possible >= 2000)
777 return 0;
779 FOR_EACH_PTR(sm->possible, tmp) {
780 if (tmp->merged)
781 continue;
783 ret = 1;
784 __push_fake_cur_slist();
786 overwrite_states_using_pool(tmp);
788 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
789 return_ranges = show_rl(rl);
791 return_id++;
792 FOR_EACH_PTR(returned_state_callbacks, cb) {
793 cb->callback(return_id, return_ranges, expr);
794 } END_FOR_EACH_PTR(cb);
796 __free_fake_cur_slist();
797 } END_FOR_EACH_PTR(tmp);
799 return ret;
802 static void call_return_state_hooks(struct expression *expr)
804 struct returned_state_callback *cb;
805 struct range_list *rl;
806 char *return_ranges;
807 int nr_states;
809 expr = strip_expr(expr);
811 if (!expr) {
812 return_ranges = alloc_sname("");
813 } else if (is_condition(expr)) {
814 call_return_state_hooks_compare(expr);
815 return;
816 } else if (call_return_state_hooks_split_possible(expr)) {
817 return;
818 } else if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
819 rl = cast_rl(cur_func_return_type(), rl);
820 return_ranges = show_rl(rl);
821 } else {
822 return_ranges = range_comparison_to_param(expr);
823 if (!return_ranges) {
824 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
825 return_ranges = show_rl(rl);
829 return_id++;
830 nr_states = ptr_list_size((struct ptr_list *)__get_cur_slist());
831 if (nr_states >= 10000) {
832 match_return_info(return_id, return_ranges, expr);
833 return;
835 FOR_EACH_PTR(returned_state_callbacks, cb) {
836 cb->callback(return_id, return_ranges, expr);
837 } END_FOR_EACH_PTR(cb);
840 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
842 struct returned_member_callback *cb;
843 struct state_list *my_slist;
844 struct sm_state *sm;
845 struct symbol *type;
846 char *name;
847 char member_name[256];
848 int len;
850 type = get_type(expr);
851 if (!type || type->type != SYM_PTR)
852 return;
853 type = get_real_base_type(type);
854 if (!type || type->type != SYM_STRUCT)
855 return;
856 name = expr_to_var(expr);
857 if (!name)
858 return;
860 member_name[sizeof(member_name) - 1] = '\0';
861 strcpy(member_name, "$$");
863 len = strlen(name);
864 FOR_EACH_PTR(returned_member_callbacks, cb) {
865 my_slist = get_all_states(cb->owner);
866 FOR_EACH_PTR(my_slist, sm) {
867 if (strncmp(sm->name, name, len) != 0)
868 continue;
869 if (strncmp(sm->name + len, "->", 2) != 0)
870 continue;
871 strncpy(member_name + 2, sm->name + len, sizeof(member_name) - 2);
872 cb->callback(return_id, return_ranges, member_name, sm->state);
873 } END_FOR_EACH_PTR(sm);
874 free_slist(&my_slist);
875 } END_FOR_EACH_PTR(cb);
877 free_string(name);
880 static void reset_memdb(void)
882 mem_sql(NULL, "delete from caller_info;");
883 mem_sql(NULL, "delete from return_states;");
884 mem_sql(NULL, "delete from call_implies;");
885 mem_sql(NULL, "delete from return_values;");
888 static void match_end_func_info(struct symbol *sym)
890 if (__path_is_null())
891 return;
892 call_return_state_hooks(NULL);
893 if (!__inline_fn)
894 reset_memdb();
897 static void init_memdb(void)
899 char *err = NULL;
900 int rc;
901 const char *schema_files[] = {
902 "db/db.schema",
903 "db/caller_info.schema",
904 "db/return_states.schema",
905 "db/type_size.schema",
906 "db/call_implies.schema",
907 "db/function_ptr.schema",
908 "db/return_values.schema",
909 "db/local_values.schema",
911 static char buf[4096];
912 int fd;
913 int ret;
914 int i;
916 rc = sqlite3_open(":memory:", &mem_db);
917 if (rc != SQLITE_OK) {
918 printf("Error starting In-Memory database.");
919 return;
922 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
923 fd = open_data_file(schema_files[i]);
924 if (fd < 0)
925 continue;
926 ret = read(fd, buf, sizeof(buf));
927 if (ret == sizeof(buf)) {
928 printf("Schema file too large: %s (limit %zd bytes)",
929 schema_files[i], sizeof(buf));
931 buf[ret] = '\0';
932 rc = sqlite3_exec(mem_db, buf, NULL, 0, &err);
933 if (rc != SQLITE_OK) {
934 fprintf(stderr, "SQL error #2: %s\n", err);
935 fprintf(stderr, "%s\n", buf);
940 void open_smatch_db(void)
942 int rc;
944 if (option_no_db)
945 return;
947 init_memdb();
949 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
950 if (rc != SQLITE_OK) {
951 option_no_db = 1;
952 return;
954 return;
957 static void register_common_funcs(void)
959 struct token *token;
960 char *func;
961 char filename[256];
963 if (option_project == PROJ_NONE)
964 strcpy(filename, "common_functions");
965 else
966 snprintf(filename, 256, "%s.common_functions", option_project_str);
968 token = get_tokens_file(filename);
969 if (!token)
970 return;
971 if (token_type(token) != TOKEN_STREAMBEGIN)
972 return;
973 token = token->next;
974 while (token_type(token) != TOKEN_STREAMEND) {
975 if (token_type(token) != TOKEN_IDENT)
976 return;
977 func = alloc_string(show_ident(token->ident));
978 add_ptr_list(&common_funcs, func);
979 token = token->next;
981 clear_token_alloc();
985 void register_definition_db_callbacks(int id)
987 add_hook(&match_function_def, FUNC_DEF_HOOK);
989 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
990 add_hook(&global_variable, BASE_HOOK);
991 add_hook(&global_variable, DECLARATION_HOOK);
992 add_returned_state_callback(match_return_info);
993 add_returned_state_callback(print_returned_struct_members);
994 add_hook(&call_return_state_hooks, RETURN_HOOK);
995 add_hook(&match_end_func_info, END_FUNC_HOOK);
997 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
998 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1000 register_common_funcs();
1003 void register_db_call_marker(int id)
1005 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1008 char *get_variable_from_key(struct expression *arg, char *key, struct symbol **sym)
1010 char buf[256];
1011 char *tmp;
1013 if (strcmp(key, "$$") == 0)
1014 return expr_to_var_sym(arg, sym);
1016 if (strcmp(key, "*$$") == 0) {
1017 if (arg->type == EXPR_PREOP && arg->op == '&') {
1018 arg = strip_expr(arg->unop);
1019 return expr_to_var_sym(arg, sym);
1020 } else {
1021 tmp = expr_to_var_sym(arg, sym);
1022 if (!tmp)
1023 return NULL;
1024 snprintf(buf, sizeof(buf), "*%s", tmp);
1025 free_string(tmp);
1026 return alloc_string(buf);
1030 if (arg->type == EXPR_PREOP && arg->op == '&') {
1031 arg = strip_expr(arg->unop);
1032 tmp = expr_to_var_sym(arg, sym);
1033 if (!tmp)
1034 return NULL;
1035 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 4);
1036 return alloc_string(buf);
1039 tmp = expr_to_var_sym(arg, sym);
1040 if (!tmp)
1041 return NULL;
1042 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 2);
1043 free_string(tmp);
1044 return alloc_string(buf);
1047 const char *get_param_name(struct sm_state *sm)
1049 char *param_name;
1050 int name_len;
1051 static char buf[256];
1053 if (!sm->sym->ident)
1054 return NULL;
1056 param_name = sm->sym->ident->name;
1057 name_len = strlen(param_name);
1059 if (strcmp(sm->name, param_name) == 0) {
1060 return "$$";
1061 } else if (sm->name[name_len] == '-' && /* check for '-' from "->" */
1062 strncmp(sm->name, param_name, name_len) == 0) {
1063 snprintf(buf, sizeof(buf), "$$%s", sm->name + name_len);
1064 return buf;
1065 } else if (sm->name[0] == '*' && strcmp(sm->name + 1, param_name) == 0) {
1066 return "*$$";
1068 return NULL;