db: prevent slow downs splitting returns apart
[smatch.git] / smatch_db.c
blob86a47d388100a6f637922c3393ca70c81a2f882e
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 <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert(table, values...) \
33 do { \
34 if (!mem_db) \
35 break; \
36 if (__inline_fn) { \
37 char buf[1024]; \
38 char *err, *p = buf; \
39 int rc; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
50 parse_error = 1; \
51 } \
52 break; \
53 } \
54 if (option_info) { \
55 sm_prefix(); \
56 sm_printf("SQL: insert into " #table " values (" values); \
57 sm_printf(");\n"); \
58 } \
59 } while (0)
61 struct def_callback {
62 int hook_type;
63 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
65 ALLOCATOR(def_callback, "definition db hook callbacks");
66 DECLARE_PTR_LIST(callback_list, struct def_callback);
67 static struct callback_list *callbacks;
69 struct member_info_callback {
70 int owner;
71 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
73 ALLOCATOR(member_info_callback, "caller_info callbacks");
74 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
75 static struct member_info_cb_list *member_callbacks;
77 struct returned_state_callback {
78 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
80 ALLOCATOR(returned_state_callback, "returned state callbacks");
81 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
82 static struct returned_state_cb_list *returned_state_callbacks;
84 struct returned_member_callback {
85 int owner;
86 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
88 ALLOCATOR(returned_member_callback, "returned member callbacks");
89 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
90 static struct returned_member_cb_list *returned_member_callbacks;
92 struct call_implies_callback {
93 int type;
94 void (*callback)(struct expression *arg, char *key, char *value);
96 ALLOCATOR(call_implies_callback, "call_implies callbacks");
97 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
98 static struct call_implies_cb_list *call_implies_cb_list;
100 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
102 int i;
104 for (i = 0; i < argc; i++) {
105 if (i != 0)
106 printf(", ");
107 sm_printf("%s", argv[i]);
109 sm_printf("\n");
110 return 0;
113 void debug_sql(const char *sql)
115 if (!option_debug)
116 return;
117 sm_msg("%s", sql);
118 sql_exec(print_sql_output, NULL, sql);
121 void debug_mem_sql(const char *sql)
123 if (!option_debug)
124 return;
125 sm_msg("%s", sql);
126 sql_mem_exec(print_sql_output, NULL, sql);
129 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
131 char *err = NULL;
132 int rc;
134 if (option_no_db || !db)
135 return;
137 rc = sqlite3_exec(db, sql, callback, data, &err);
138 if (rc != SQLITE_OK && !parse_error) {
139 fprintf(stderr, "SQL error #2: %s\n", err);
140 fprintf(stderr, "SQL: '%s'\n", sql);
141 parse_error = 1;
145 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
147 char *err = NULL;
148 int rc;
150 if (!mem_db)
151 return;
153 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
154 if (rc != SQLITE_OK) {
155 fprintf(stderr, "SQL error #2: %s\n", err);
156 fprintf(stderr, "SQL: '%s'\n", sql);
157 parse_error = 1;
161 void sql_insert_return_states(int return_id, const char *return_ranges,
162 int type, int param, const char *key, const char *value)
164 if (key && strlen(key) >= 80)
165 return;
166 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
167 get_base_file(), get_function(), (unsigned long)__inline_fn,
168 return_id, return_ranges, fn_static(), type, param, key, value);
171 static struct string_list *common_funcs;
172 static int is_common_function(const char *fn)
174 char *tmp;
176 if (strncmp(fn, "__builtin_", 10) == 0)
177 return 1;
179 FOR_EACH_PTR(common_funcs, tmp) {
180 if (strcmp(tmp, fn) == 0)
181 return 1;
182 } END_FOR_EACH_PTR(tmp);
184 return 0;
187 void sql_insert_caller_info(struct expression *call, int type,
188 int param, const char *key, const char *value)
190 char *fn;
192 if (!option_info && !__inline_call)
193 return;
195 if (key && strlen(key) >= 80)
196 return;
198 fn = get_fnptr_name(call->fn);
199 if (!fn)
200 return;
202 if (__inline_call) {
203 mem_sql(NULL, NULL,
204 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
205 get_base_file(), get_function(), fn, (unsigned long)call,
206 is_static(call->fn), type, param, key, value);
209 if (!option_info)
210 return;
212 if (is_common_function(fn))
213 return;
215 sm_msg("SQL_caller_info: insert into caller_info values ("
216 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
217 get_base_file(), get_function(), fn, is_static(call->fn),
218 type, param, key, value);
220 free_string(fn);
223 void sql_insert_function_ptr(const char *fn, const char *struct_name)
225 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
226 struct_name);
229 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
231 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
232 get_function(), (unsigned long)__inline_fn, fn_static(),
233 type, param, key, value);
236 void sql_insert_function_type_size(const char *member, const char *ranges)
238 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
241 void sql_insert_local_values(const char *name, const char *value)
243 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
246 void sql_insert_function_type_value(const char *type, const char *value)
248 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
251 void sql_insert_function_type(int param, const char *value)
253 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
254 get_base_file(), get_function(), fn_static(), param, value);
257 void sql_insert_data_info(struct expression *data, int type, const char *value)
259 char *data_name;
261 data_name = get_data_info_name(data);
262 if (!data_name)
263 return;
264 sql_insert(data_info, "'%s', '%s', %d, '%s'",
265 is_static(data) ? get_base_file() : "extern",
266 data_name, type, value);
269 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
271 sql_insert(data_info, "'%s', '%s', %d, '%s'",
272 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
273 var, type, value);
276 char *get_static_filter(struct symbol *sym)
278 static char sql_filter[1024];
280 /* This can only happen on buggy code. Return invalid SQL. */
281 if (!sym) {
282 sql_filter[0] = '\0';
283 return sql_filter;
286 if (sym->ctype.modifiers & MOD_STATIC) {
287 snprintf(sql_filter, sizeof(sql_filter),
288 "file = '%s' and function = '%s' and static = '1'",
289 get_base_file(), sym->ident->name);
290 } else {
291 snprintf(sql_filter, sizeof(sql_filter),
292 "function = '%s' and static = '0'", sym->ident->name);
295 return sql_filter;
298 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
300 int *row_count = _row_count;
302 *row_count = 0;
303 if (argc != 1)
304 return 0;
305 *row_count = atoi(argv[0]);
306 return 0;
309 static void sql_select_return_states_pointer(const char *cols,
310 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
312 char *ptr;
313 int return_count;
315 ptr = get_fnptr_name(call->fn);
316 if (!ptr)
317 return;
319 run_sql(get_row_count, &return_count,
320 "select count(*) from return_states join function_ptr "
321 "where return_states.function == function_ptr.function and "
322 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
323 /* The magic number 100 is just from testing on the kernel. */
324 if (return_count > 100)
325 return;
327 run_sql(callback, info,
328 "select %s from return_states join function_ptr where "
329 "return_states.function == function_ptr.function and ptr = '%s' "
330 "and searchable = 1 "
331 "order by function_ptr.file, return_states.file, return_id, type;",
332 cols, ptr);
335 static int is_local_symbol(struct expression *expr)
337 if (expr->type != EXPR_SYMBOL)
338 return 0;
339 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
340 return 0;
341 return 1;
344 void sql_select_return_states(const char *cols, struct expression *call,
345 int (*callback)(void*, int, char**, char**), void *info)
347 int row_count;
349 if (is_fake_call(call))
350 return;
352 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
353 sql_select_return_states_pointer(cols, call, callback, info);
354 return;
357 if (inlinable(call->fn)) {
358 mem_sql(callback, info,
359 "select %s from return_states where call_id = '%lu' order by return_id, type;",
360 cols, (unsigned long)call);
361 return;
364 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
365 get_static_filter(call->fn->symbol));
366 if (row_count > 3000)
367 return;
369 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
370 cols, get_static_filter(call->fn->symbol));
373 void sql_select_call_implies(const char *cols, struct expression *call,
374 int (*callback)(void*, int, char**, char**))
376 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
377 return;
379 if (inlinable(call->fn)) {
380 mem_sql(callback, call,
381 "select %s from call_implies where call_id = '%lu';",
382 cols, (unsigned long)call);
383 return;
386 run_sql(callback, call, "select %s from call_implies where %s;",
387 cols, get_static_filter(call->fn->symbol));
390 void sql_select_caller_info(const char *cols, struct symbol *sym,
391 int (*callback)(void*, int, char**, char**))
393 if (__inline_fn) {
394 mem_sql(callback, NULL,
395 "select %s from caller_info where call_id = %lu;",
396 cols, (unsigned long)__inline_fn);
397 return;
400 run_sql(callback, NULL,
401 "select %s from caller_info where %s order by call_id;",
402 cols, get_static_filter(sym));
405 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
407 struct def_callback *def_callback = __alloc_def_callback(0);
409 def_callback->hook_type = type;
410 def_callback->callback = callback;
411 add_ptr_list(&callbacks, def_callback);
415 * These call backs are used when the --info option is turned on to print struct
416 * member information. For example foo->bar could have a state in
417 * smatch_extra.c and also check_user.c.
419 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
421 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
423 member_callback->owner = owner;
424 member_callback->callback = callback;
425 add_ptr_list(&member_callbacks, member_callback);
428 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
430 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
432 callback->callback = fn;
433 add_ptr_list(&returned_state_callbacks, callback);
436 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))
438 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
440 member_callback->owner = owner;
441 member_callback->callback = callback;
442 add_ptr_list(&returned_member_callbacks, member_callback);
445 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
447 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
449 cb->type = type;
450 cb->callback = callback;
451 add_ptr_list(&call_implies_cb_list, cb);
454 struct return_info {
455 struct expression *static_returns_call;
456 struct symbol *return_type;
457 struct range_list *return_range_list;
460 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
462 struct return_info *ret_info = _ret_info;
463 struct range_list *rl;
464 struct expression *call_expr = ret_info->static_returns_call;
466 if (argc != 1)
467 return 0;
468 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
469 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
470 return 0;
473 struct range_list *db_return_vals(struct expression *expr)
475 struct return_info ret_info = {};
476 char buf[64];
477 struct sm_state *sm;
479 if (is_fake_call(expr))
480 return NULL;
482 snprintf(buf, sizeof(buf), "return %p", expr);
483 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
484 if (sm)
485 return clone_rl(estate_rl(sm->state));
486 ret_info.static_returns_call = expr;
487 ret_info.return_type = get_type(expr);
488 if (!ret_info.return_type)
489 return NULL;
491 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
492 return NULL;
494 ret_info.return_range_list = NULL;
495 if (inlinable(expr->fn)) {
496 mem_sql(db_return_callback, &ret_info,
497 "select distinct return from return_states where call_id = '%lu';",
498 (unsigned long)expr);
499 } else {
500 run_sql(db_return_callback, &ret_info,
501 "select distinct return from return_states where %s;",
502 get_static_filter(expr->fn->symbol));
504 return ret_info.return_range_list;
507 struct range_list *db_return_vals_from_str(const char *fn_name)
509 struct return_info ret_info;
511 ret_info.static_returns_call = NULL;
512 ret_info.return_type = &llong_ctype;
513 ret_info.return_range_list = NULL;
515 run_sql(db_return_callback, &ret_info,
516 "select distinct return from return_states where function = '%s';",
517 fn_name);
518 return ret_info.return_range_list;
521 static void match_call_marker(struct expression *expr)
524 * we just want to record something in the database so that if we have
525 * two calls like: frob(4); frob(some_unkown); then on the receiving
526 * side we know that sometimes frob is called with unknown parameters.
529 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
532 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
533 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
535 struct sm_state *sm;
536 char *name;
537 struct symbol *sym;
538 int len;
539 char printed_name[256];
540 int is_address = 0;
542 expr = strip_expr(expr);
543 if (expr->type == EXPR_PREOP && expr->op == '&') {
544 expr = strip_expr(expr->unop);
545 is_address = 1;
548 name = expr_to_var_sym(expr, &sym);
549 if (!name || !sym)
550 goto free;
552 len = strlen(name);
553 FOR_EACH_SM(stree, sm) {
554 if (sm->sym != sym)
555 continue;
556 if (strcmp(name, sm->name) == 0) {
557 if (is_address)
558 snprintf(printed_name, sizeof(printed_name), "*$");
559 else /* these are already handled. fixme: handle them here */
560 continue;
561 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
562 snprintf(printed_name, sizeof(printed_name), "*$");
563 } else if (strncmp(name, sm->name, len) == 0) {
564 if (isalnum(sm->name[len]))
565 continue;
566 if (is_address)
567 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
568 else
569 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
570 } else {
571 continue;
573 callback(call, param, printed_name, sm);
574 } END_FOR_EACH_SM(sm);
575 free:
576 free_string(name);
579 static void match_call_info(struct expression *call)
581 struct member_info_callback *cb;
582 struct expression *arg;
583 struct stree *stree;
584 char *name;
585 int i;
587 name = get_fnptr_name(call->fn);
588 if (!name)
589 return;
591 FOR_EACH_PTR(member_callbacks, cb) {
592 stree = get_all_states_stree(cb->owner);
593 i = 0;
594 FOR_EACH_PTR(call->args, arg) {
595 print_struct_members(call, arg, i, stree, cb->callback);
596 i++;
597 } END_FOR_EACH_PTR(arg);
598 free_stree(&stree);
599 } END_FOR_EACH_PTR(cb);
601 free_string(name);
604 static int get_param(int param, char **name, struct symbol **sym)
606 struct symbol *arg;
607 int i;
609 i = 0;
610 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
612 * this is a temporary hack to work around a bug (I think in sparse?)
613 * 2.6.37-rc1:fs/reiserfs/journal.o
614 * If there is a function definition without parameter name found
615 * after a function implementation then it causes a crash.
616 * int foo() {}
617 * int bar(char *);
619 if (arg->ident->name < (char *)100)
620 continue;
621 if (i == param) {
622 *name = arg->ident->name;
623 *sym = arg;
624 return TRUE;
626 i++;
627 } END_FOR_EACH_PTR(arg);
629 return FALSE;
632 static struct stree *final_states;
633 static int prev_func_id = -1;
634 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
636 int func_id;
637 long type;
638 long param;
639 char *key;
640 char *value;
641 char *name = NULL;
642 struct symbol *sym = NULL;
643 struct def_callback *def_callback;
644 struct stree *stree;
646 if (argc != 5)
647 return 0;
649 func_id = atoi(argv[0]);
650 errno = 0;
651 type = strtol(argv[1], NULL, 10);
652 param = strtol(argv[2], NULL, 10);
653 if (errno)
654 return 0;
655 key = argv[3];
656 value = argv[4];
659 if (prev_func_id == -1)
660 prev_func_id = func_id;
661 if (func_id != prev_func_id) {
662 stree = __pop_fake_cur_stree();
663 merge_stree(&final_states, stree);
664 free_stree(&stree);
665 __push_fake_cur_stree();
666 __unnullify_path();
667 prev_func_id = func_id;
670 if (param >= 0 && !get_param(param, &name, &sym))
671 return 0;
673 FOR_EACH_PTR(callbacks, def_callback) {
674 if (def_callback->hook_type == type)
675 def_callback->callback(name, sym, key, value);
676 } END_FOR_EACH_PTR(def_callback);
678 return 0;
681 static void get_direct_callers(struct symbol *sym)
683 sql_select_caller_info("call_id, type, parameter, key, value", sym,
684 caller_info_callback);
687 static struct string_list *ptr_names_done;
688 static struct string_list *ptr_names;
690 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
692 insert_string(&ptr_names, alloc_string(argv[0]));
693 return 0;
696 static char *get_next_ptr_name(void)
698 char *ptr;
700 FOR_EACH_PTR(ptr_names, ptr) {
701 if (list_has_string(ptr_names_done, ptr))
702 continue;
703 insert_string(&ptr_names_done, ptr);
704 return ptr;
705 } END_FOR_EACH_PTR(ptr);
706 return NULL;
709 static void get_ptr_names(const char *file, const char *name)
711 char sql_filter[1024];
712 int before, after;
714 if (file) {
715 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
716 file, name);
717 } else {
718 snprintf(sql_filter, 1024, "function = '%s';", name);
721 before = ptr_list_size((struct ptr_list *)ptr_names);
723 run_sql(get_ptr_name, NULL,
724 "select distinct ptr from function_ptr where %s",
725 sql_filter);
727 after = ptr_list_size((struct ptr_list *)ptr_names);
728 if (before == after)
729 return;
731 while ((name = get_next_ptr_name()))
732 get_ptr_names(NULL, name);
735 static void match_data_from_db(struct symbol *sym)
737 struct sm_state *sm;
738 struct stree *stree;
740 if (!sym || !sym->ident)
741 return;
743 __push_fake_cur_stree();
744 __unnullify_path();
745 prev_func_id = -1;
747 if (!__inline_fn) {
748 char *ptr;
750 if (sym->ctype.modifiers & MOD_STATIC)
751 get_ptr_names(get_base_file(), sym->ident->name);
752 else
753 get_ptr_names(NULL, sym->ident->name);
755 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
756 __free_ptr_list((struct ptr_list **)&ptr_names);
757 __free_ptr_list((struct ptr_list **)&ptr_names_done);
758 stree = __pop_fake_cur_stree();
759 free_stree(&stree);
760 return;
763 get_direct_callers(sym);
765 FOR_EACH_PTR(ptr_names, ptr) {
766 run_sql(caller_info_callback, NULL,
767 "select call_id, type, parameter, key, value"
768 " from caller_info where function = '%s' order by call_id",
769 ptr);
770 free_string(ptr);
771 } END_FOR_EACH_PTR(ptr);
773 __free_ptr_list((struct ptr_list **)&ptr_names);
774 __free_ptr_list((struct ptr_list **)&ptr_names_done);
775 } else {
776 get_direct_callers(sym);
779 stree = __pop_fake_cur_stree();
780 merge_stree(&final_states, stree);
781 free_stree(&stree);
783 FOR_EACH_SM(final_states, sm) {
784 __set_sm(sm);
785 } END_FOR_EACH_SM(sm);
787 free_stree(&final_states);
790 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
792 struct expression *call_expr = _call;
793 struct call_implies_callback *cb;
794 struct expression *arg = NULL;
795 int type;
796 int param;
798 if (argc != 5)
799 return 0;
801 type = atoi(argv[1]);
802 param = atoi(argv[2]);
804 FOR_EACH_PTR(call_implies_cb_list, cb) {
805 if (cb->type != type)
806 continue;
807 if (param != -1) {
808 arg = get_argument_from_call_expr(call_expr->args, param);
809 if (!arg)
810 continue;
812 cb->callback(arg, argv[3], argv[4]);
813 } END_FOR_EACH_PTR(cb);
815 return 0;
818 static void match_call_implies(struct expression *expr)
820 sql_select_call_implies("function, type, parameter, key, value", expr,
821 call_implies_callbacks);
824 static void print_initializer_list(struct expression_list *expr_list,
825 struct symbol *struct_type)
827 struct expression *expr;
828 struct symbol *base_type;
829 char struct_name[256];
831 FOR_EACH_PTR(expr_list, expr) {
832 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
833 print_initializer_list(expr->idx_expression->expr_list, struct_type);
834 continue;
836 if (expr->type != EXPR_IDENTIFIER)
837 continue;
838 if (!expr->expr_ident)
839 continue;
840 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
841 continue;
842 base_type = get_type(expr->ident_expression);
843 if (!base_type || base_type->type != SYM_FN)
844 continue;
845 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
846 struct_type->ident->name, expr->expr_ident->name);
847 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
848 struct_name);
849 } END_FOR_EACH_PTR(expr);
852 static void global_variable(struct symbol *sym)
854 struct symbol *struct_type;
856 if (!sym->ident)
857 return;
858 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
859 return;
860 struct_type = get_base_type(sym);
861 if (!struct_type)
862 return;
863 if (struct_type->type == SYM_ARRAY) {
864 struct_type = get_base_type(struct_type);
865 if (!struct_type)
866 return;
868 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
869 return;
870 print_initializer_list(sym->initializer->expr_list, struct_type);
873 static char *function_signature(void)
875 return type_to_str(get_real_base_type(cur_func_sym));
878 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
880 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
883 static void call_return_state_hooks_conditional(struct expression *expr)
885 struct returned_state_callback *cb;
886 struct range_list *rl;
887 char *return_ranges;
888 int final_pass_orig = final_pass;
890 __push_fake_cur_stree();
892 final_pass = 0;
893 __split_whole_condition(expr->conditional);
894 final_pass = final_pass_orig;
896 if (get_implied_rl(expr->cond_true, &rl))
897 rl = cast_rl(cur_func_return_type(), rl);
898 else
899 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
900 return_ranges = show_rl(rl);
901 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
903 return_id++;
904 FOR_EACH_PTR(returned_state_callbacks, cb) {
905 cb->callback(return_id, return_ranges, expr);
906 } END_FOR_EACH_PTR(cb);
908 __push_true_states();
909 __use_false_states();
911 if (get_implied_rl(expr->cond_false, &rl))
912 rl = cast_rl(cur_func_return_type(), rl);
913 else
914 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
915 return_ranges = show_rl(rl);
916 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
918 return_id++;
919 FOR_EACH_PTR(returned_state_callbacks, cb) {
920 cb->callback(return_id, return_ranges, expr);
921 } END_FOR_EACH_PTR(cb);
923 __merge_true_states();
924 __free_fake_cur_stree();
927 static void call_return_state_hooks_compare(struct expression *expr)
929 struct returned_state_callback *cb;
930 char *return_ranges;
931 int final_pass_orig = final_pass;
932 sval_t sval = { .type = &int_ctype };
933 sval_t ret;
935 if (!get_implied_value(expr, &ret))
936 ret.value = -1;
938 __push_fake_cur_stree();
940 final_pass = 0;
941 __split_whole_condition(expr);
942 final_pass = final_pass_orig;
944 if (ret.value != 0) {
945 return_ranges = alloc_sname("1");
946 sval.value = 1;
947 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
949 return_id++;
950 FOR_EACH_PTR(returned_state_callbacks, cb) {
951 cb->callback(return_id, return_ranges, expr);
952 } END_FOR_EACH_PTR(cb);
955 __push_true_states();
956 __use_false_states();
958 if (ret.value != 1) {
959 return_ranges = alloc_sname("0");
960 sval.value = 0;
961 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
963 return_id++;
964 FOR_EACH_PTR(returned_state_callbacks, cb) {
965 cb->callback(return_id, return_ranges, expr);
966 } END_FOR_EACH_PTR(cb);
969 __merge_true_states();
970 __free_fake_cur_stree();
973 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
975 struct sm_state *tmp;
977 FOR_EACH_PTR(slist, tmp) {
978 if (strcmp(tmp->state->name, sm->state->name) == 0)
979 return 1;
980 } END_FOR_EACH_PTR(tmp);
982 return 0;
985 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
987 struct returned_state_callback *cb;
988 struct range_list *rl;
989 char *return_ranges;
990 struct sm_state *tmp;
991 int ret = 0;
992 int nr_possible, nr_states;
993 char *compare_str = NULL;
994 char buf[128];
995 struct state_list *already_handled = NULL;
997 if (!sm || !sm->merged)
998 return 0;
1000 if (too_many_possible(sm))
1001 return 0;
1003 /* bail if it gets too complicated */
1004 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1005 nr_states = stree_count(__get_cur_stree());
1006 if (nr_states * nr_possible >= 2000)
1007 return 0;
1009 FOR_EACH_PTR(sm->possible, tmp) {
1010 if (tmp->merged)
1011 continue;
1012 if (ptr_in_list(tmp, already_handled))
1013 continue;
1014 add_ptr_list(&already_handled, tmp);
1016 ret = 1;
1017 __push_fake_cur_stree();
1019 overwrite_states_using_pool(sm, tmp);
1021 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1022 return_ranges = show_rl(rl);
1023 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1024 compare_str = expr_lte_to_param(expr, -1);
1025 if (compare_str) {
1026 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1027 return_ranges = alloc_sname(buf);
1030 return_id++;
1031 FOR_EACH_PTR(returned_state_callbacks, cb) {
1032 cb->callback(return_id, return_ranges, expr);
1033 } END_FOR_EACH_PTR(cb);
1035 __free_fake_cur_stree();
1036 } END_FOR_EACH_PTR(tmp);
1038 free_slist(&already_handled);
1040 return ret;
1043 static int call_return_state_hooks_split_possible(struct expression *expr)
1045 struct sm_state *sm;
1047 if (!expr || expr_equal_to_param(expr, -1))
1048 return 0;
1050 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1051 return split_possible_helper(sm, expr);
1054 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1056 struct range_list *rl;
1057 char *return_ranges;
1058 sval_t sval;
1059 char *compare_str;
1060 char *math_str;
1061 char buf[128];
1063 *rl_p = NULL;
1065 if (!expr)
1066 return alloc_sname("");
1068 if (get_implied_value(expr, &sval)) {
1069 *rl_p = alloc_rl(sval, sval);
1070 return sval_to_str(sval);
1073 compare_str = expr_equal_to_param(expr, -1);
1074 math_str = get_value_in_terms_of_parameter_math(expr);
1076 if (get_implied_rl(expr, &rl)) {
1077 rl = cast_rl(cur_func_return_type(), rl);
1078 return_ranges = show_rl(rl);
1079 } else if (get_imaginary_absolute(expr, &rl)){
1080 rl = cast_rl(cur_func_return_type(), rl);
1081 return alloc_sname(show_rl(rl));
1082 } else {
1083 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1084 return_ranges = show_rl(rl);
1086 *rl_p = rl;
1088 if (compare_str) {
1089 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1090 return alloc_sname(buf);
1093 if (math_str) {
1094 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1095 return alloc_sname(buf);
1098 compare_str = expr_lte_to_param(expr, -1);
1099 if (compare_str) {
1100 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1101 return alloc_sname(buf);
1103 return return_ranges;
1106 static int split_positive_from_negative(struct expression *expr)
1108 struct returned_state_callback *cb;
1109 struct range_list *rl;
1110 const char *return_ranges;
1111 struct range_list *ret_rl;
1112 int undo;
1114 /* We're going to print the states 3 times */
1115 if (stree_count(__get_cur_stree()) > 10000 / 3)
1116 return 0;
1118 if (!get_implied_rl(expr, &rl) || !rl)
1119 return 0;
1120 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1121 return 0;
1122 /* Forget about INT_MAX and larger */
1123 if (rl_max(rl).value <= 0)
1124 return 0;
1125 if (!sval_is_negative(rl_min(rl)))
1126 return 0;
1128 if (!assume(compare_expression(expr, '>', zero_expr())))
1129 return 0;
1131 return_id++;
1132 return_ranges = get_return_ranges_str(expr, &ret_rl);
1133 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1134 FOR_EACH_PTR(returned_state_callbacks, cb) {
1135 cb->callback(return_id, (char *)return_ranges, expr);
1136 } END_FOR_EACH_PTR(cb);
1138 end_assume();
1140 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1141 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1143 return_id++;
1144 return_ranges = get_return_ranges_str(expr, &ret_rl);
1145 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1146 FOR_EACH_PTR(returned_state_callbacks, cb) {
1147 cb->callback(return_id, (char *)return_ranges, expr);
1148 } END_FOR_EACH_PTR(cb);
1150 if (undo)
1151 end_assume();
1154 undo = assume(compare_expression(expr, '<', zero_expr()));
1156 return_id++;
1157 return_ranges = get_return_ranges_str(expr, &ret_rl);
1158 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1159 FOR_EACH_PTR(returned_state_callbacks, cb) {
1160 cb->callback(return_id, (char *)return_ranges, expr);
1161 } END_FOR_EACH_PTR(cb);
1163 if (undo)
1164 end_assume();
1166 return 1;
1169 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1171 struct returned_state_callback *cb;
1172 struct range_list *rl;
1173 struct range_list *nonnull_rl;
1174 sval_t null_sval;
1175 struct range_list *null_rl = NULL;
1176 char *return_ranges;
1177 struct smatch_state *state;
1178 int nr_states;
1179 int final_pass_orig = final_pass;
1181 if (!expr || expr_equal_to_param(expr, -1))
1182 return 0;
1183 if (expr->type == EXPR_CALL)
1184 return 0;
1185 if (!is_pointer(expr))
1186 return 0;
1188 state = get_state_expr(SMATCH_EXTRA, expr);
1189 if (!state || !estate_rl(state))
1190 return 0;
1191 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1192 return 0;
1193 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1194 return 0;
1196 nr_states = stree_count(__get_cur_stree());
1197 if (option_info && nr_states >= 1500)
1198 return 0;
1200 rl = estate_rl(state);
1202 __push_fake_cur_stree();
1204 final_pass = 0;
1205 __split_whole_condition(expr);
1206 final_pass = final_pass_orig;
1208 nonnull_rl = rl_filter(rl, rl_zero());
1209 return_ranges = show_rl(nonnull_rl);
1210 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1212 return_id++;
1213 FOR_EACH_PTR(returned_state_callbacks, cb) {
1214 cb->callback(return_id, return_ranges, expr);
1215 } END_FOR_EACH_PTR(cb);
1217 __push_true_states();
1218 __use_false_states();
1220 return_ranges = alloc_sname("0");
1221 null_sval = sval_type_val(rl_type(rl), 0);
1222 add_range(&null_rl, null_sval, null_sval);
1223 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1224 return_id++;
1225 FOR_EACH_PTR(returned_state_callbacks, cb) {
1226 cb->callback(return_id, return_ranges, expr);
1227 } END_FOR_EACH_PTR(cb);
1229 __merge_true_states();
1230 __free_fake_cur_stree();
1232 return 1;
1235 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1237 struct range_list *rl;
1238 struct range_list *nonzero_rl;
1239 sval_t zero_sval;
1240 struct range_list *zero_rl = NULL;
1241 int nr_states;
1242 struct returned_state_callback *cb;
1243 char *return_ranges;
1244 int final_pass_orig = final_pass;
1245 sval_t val;
1247 if (option_project != PROJ_KERNEL)
1248 return 0;
1250 nr_states = stree_count(__get_cur_stree());
1251 if (nr_states > 1500)
1252 return 0;
1254 if (get_value(expr, &val))
1255 return 0;
1256 if (!get_implied_rl(expr, &rl))
1257 return 0;
1258 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1259 return 0;
1260 if (rl_max(rl).value != 0)
1261 return 0;
1263 __push_fake_cur_stree();
1265 final_pass = 0;
1266 __split_whole_condition(expr);
1267 final_pass = final_pass_orig;
1269 nonzero_rl = rl_filter(rl, rl_zero());
1270 return_ranges = show_rl(nonzero_rl);
1271 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1273 return_id++;
1274 FOR_EACH_PTR(returned_state_callbacks, cb) {
1275 cb->callback(return_id, return_ranges, expr);
1276 } END_FOR_EACH_PTR(cb);
1278 __push_true_states();
1279 __use_false_states();
1281 return_ranges = alloc_sname("0");
1282 zero_sval = sval_type_val(rl_type(rl), 0);
1283 add_range(&zero_rl, zero_sval, zero_sval);
1284 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1285 return_id++;
1286 FOR_EACH_PTR(returned_state_callbacks, cb) {
1287 cb->callback(return_id, return_ranges, expr);
1288 } END_FOR_EACH_PTR(cb);
1290 __merge_true_states();
1291 __free_fake_cur_stree();
1293 return 1;
1296 static int is_boolean(struct expression *expr)
1298 struct range_list *rl;
1300 if (!get_implied_rl(expr, &rl))
1301 return 0;
1302 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1303 return 1;
1304 return 0;
1307 static int is_conditional(struct expression *expr)
1309 if (!expr)
1310 return 0;
1311 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1312 return 1;
1313 return 0;
1316 static int splitable_function_call(struct expression *expr)
1318 struct sm_state *sm;
1319 char buf[64];
1321 if (!expr || expr->type != EXPR_CALL)
1322 return 0;
1323 snprintf(buf, sizeof(buf), "return %p", expr);
1324 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1325 return split_possible_helper(sm, expr);
1328 static struct sm_state *find_bool_param(void)
1330 struct stree *start_states;
1331 struct symbol *arg;
1332 struct sm_state *sm, *tmp;
1333 sval_t sval;
1335 start_states = get_start_states();
1337 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1338 if (!arg->ident)
1339 continue;
1340 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1341 if (!sm)
1342 continue;
1343 if (rl_min(estate_rl(sm->state)).value != 0 ||
1344 rl_max(estate_rl(sm->state)).value != 1)
1345 continue;
1346 goto found;
1347 } END_FOR_EACH_PTR_REVERSE(arg);
1349 return NULL;
1351 found:
1353 * Check if it's splitable. If not, then splitting it up is likely not
1354 * useful for the callers.
1356 FOR_EACH_PTR(sm->possible, tmp) {
1357 if (is_merged(tmp))
1358 continue;
1359 if (!estate_get_single_value(tmp->state, &sval))
1360 return NULL;
1361 } END_FOR_EACH_PTR(tmp);
1363 return sm;
1366 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1368 struct returned_state_callback *cb;
1369 struct range_list *ret_rl;
1370 const char *return_ranges;
1371 struct sm_state *tmp;
1372 int ret = 0;
1373 int nr_possible, nr_states;
1374 char *compare_str = NULL;
1375 char buf[128];
1376 struct state_list *already_handled = NULL;
1378 if (!sm || !sm->merged)
1379 return 0;
1381 if (too_many_possible(sm))
1382 return 0;
1384 /* bail if it gets too complicated */
1385 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1386 nr_states = stree_count(__get_cur_stree());
1387 if (nr_states * nr_possible >= 2000)
1388 return 0;
1390 FOR_EACH_PTR(sm->possible, tmp) {
1391 if (tmp->merged)
1392 continue;
1393 if (ptr_in_list(tmp, already_handled))
1394 continue;
1395 add_ptr_list(&already_handled, tmp);
1397 ret = 1;
1398 __push_fake_cur_stree();
1400 overwrite_states_using_pool(sm, tmp);
1402 return_ranges = get_return_ranges_str(expr, &ret_rl);
1403 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1404 compare_str = expr_lte_to_param(expr, -1);
1405 if (compare_str) {
1406 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1407 return_ranges = alloc_sname(buf);
1410 return_id++;
1411 FOR_EACH_PTR(returned_state_callbacks, cb) {
1412 cb->callback(return_id, (char *)return_ranges, expr);
1413 } END_FOR_EACH_PTR(cb);
1415 __free_fake_cur_stree();
1416 } END_FOR_EACH_PTR(tmp);
1418 free_slist(&already_handled);
1420 return ret;
1423 static int split_by_bool_param(struct expression *expr)
1425 struct sm_state *start_sm, *sm;
1426 sval_t sval;
1428 start_sm = find_bool_param();
1429 if (!start_sm)
1430 return 0;
1431 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1432 if (!sm || estate_get_single_value(sm->state, &sval))
1433 return 0;
1434 return split_on_bool_sm(sm, expr);
1437 static int split_by_null_nonnull_param(struct expression *expr)
1439 struct symbol *arg;
1440 struct sm_state *sm;
1442 /* function must only take one pointer */
1443 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1444 return 0;
1445 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1446 if (!arg->ident)
1447 return 0;
1448 if (get_real_base_type(arg)->type != SYM_PTR)
1449 return 0;
1451 if (param_was_set_var_sym(arg->ident->name, arg))
1452 return 0;
1453 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1454 if (!sm)
1455 return 0;
1457 return split_on_bool_sm(sm, expr);
1460 static void call_return_state_hooks(struct expression *expr)
1462 struct returned_state_callback *cb;
1463 struct range_list *ret_rl;
1464 const char *return_ranges;
1465 int nr_states;
1466 sval_t sval;
1468 if (__path_is_null())
1469 return;
1471 expr = strip_expr(expr);
1473 if (is_impossible_path())
1474 goto vanilla;
1476 if (expr && (expr->type == EXPR_COMPARE ||
1477 !get_implied_value(expr, &sval)) &&
1478 (is_condition(expr) || is_boolean(expr))) {
1479 call_return_state_hooks_compare(expr);
1480 return;
1481 } else if (is_conditional(expr)) {
1482 call_return_state_hooks_conditional(expr);
1483 return;
1484 } else if (call_return_state_hooks_split_possible(expr)) {
1485 return;
1486 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1487 return;
1488 } else if (call_return_state_hooks_split_success_fail(expr)) {
1489 return;
1490 } else if (splitable_function_call(expr)) {
1491 return;
1492 } else if (split_positive_from_negative(expr)) {
1493 return;
1494 } else if (split_by_bool_param(expr)) {
1495 } else if (split_by_null_nonnull_param(expr)) {
1496 return;
1499 vanilla:
1500 return_ranges = get_return_ranges_str(expr, &ret_rl);
1501 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1503 return_id++;
1504 nr_states = stree_count(__get_cur_stree());
1505 if (nr_states >= 10000) {
1506 match_return_info(return_id, (char *)return_ranges, expr);
1507 return;
1509 FOR_EACH_PTR(returned_state_callbacks, cb) {
1510 cb->callback(return_id, (char *)return_ranges, expr);
1511 } END_FOR_EACH_PTR(cb);
1514 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1516 struct returned_member_callback *cb;
1517 struct stree *stree;
1518 struct sm_state *sm;
1519 struct symbol *type;
1520 char *name;
1521 char member_name[256];
1522 int len;
1524 type = get_type(expr);
1525 if (!type || type->type != SYM_PTR)
1526 return;
1527 name = expr_to_var(expr);
1528 if (!name)
1529 return;
1531 member_name[sizeof(member_name) - 1] = '\0';
1532 strcpy(member_name, "$");
1534 len = strlen(name);
1535 FOR_EACH_PTR(returned_member_callbacks, cb) {
1536 stree = __get_cur_stree();
1537 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1538 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1539 strcpy(member_name, "*$");
1540 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1541 continue;
1543 if (strncmp(sm->name, name, len) != 0)
1544 continue;
1545 if (strncmp(sm->name + len, "->", 2) != 0)
1546 continue;
1547 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1548 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1549 } END_FOR_EACH_SM(sm);
1550 } END_FOR_EACH_PTR(cb);
1552 free_string(name);
1555 static void reset_memdb(struct symbol *sym)
1557 mem_sql(NULL, NULL, "delete from caller_info;");
1558 mem_sql(NULL, NULL, "delete from return_states;");
1559 mem_sql(NULL, NULL, "delete from call_implies;");
1562 static void match_end_func_info(struct symbol *sym)
1564 if (__path_is_null())
1565 return;
1566 call_return_state_hooks(NULL);
1569 static void match_after_func(struct symbol *sym)
1571 if (!__inline_fn)
1572 reset_memdb(sym);
1575 static void init_memdb(void)
1577 char *err = NULL;
1578 int rc;
1579 const char *schema_files[] = {
1580 "db/db.schema",
1581 "db/caller_info.schema",
1582 "db/return_states.schema",
1583 "db/function_type_size.schema",
1584 "db/type_size.schema",
1585 "db/call_implies.schema",
1586 "db/function_ptr.schema",
1587 "db/local_values.schema",
1588 "db/function_type_value.schema",
1589 "db/type_value.schema",
1590 "db/function_type.schema",
1591 "db/data_info.schema",
1593 static char buf[4096];
1594 int fd;
1595 int ret;
1596 int i;
1598 rc = sqlite3_open(":memory:", &mem_db);
1599 if (rc != SQLITE_OK) {
1600 printf("Error starting In-Memory database.");
1601 return;
1604 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1605 fd = open_data_file(schema_files[i]);
1606 if (fd < 0) {
1607 mem_db = NULL;
1608 return;
1610 ret = read(fd, buf, sizeof(buf));
1611 if (ret == sizeof(buf)) {
1612 printf("Schema file too large: %s (limit %zd bytes)",
1613 schema_files[i], sizeof(buf));
1615 buf[ret] = '\0';
1616 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1617 if (rc != SQLITE_OK) {
1618 fprintf(stderr, "SQL error #2: %s\n", err);
1619 fprintf(stderr, "%s\n", buf);
1624 void open_smatch_db(void)
1626 int rc;
1628 if (option_no_db)
1629 return;
1631 init_memdb();
1633 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1634 if (rc != SQLITE_OK) {
1635 option_no_db = 1;
1636 return;
1638 return;
1641 static void register_common_funcs(void)
1643 struct token *token;
1644 char *func;
1645 char filename[256];
1647 if (option_project == PROJ_NONE)
1648 strcpy(filename, "common_functions");
1649 else
1650 snprintf(filename, 256, "%s.common_functions", option_project_str);
1652 token = get_tokens_file(filename);
1653 if (!token)
1654 return;
1655 if (token_type(token) != TOKEN_STREAMBEGIN)
1656 return;
1657 token = token->next;
1658 while (token_type(token) != TOKEN_STREAMEND) {
1659 if (token_type(token) != TOKEN_IDENT)
1660 return;
1661 func = alloc_string(show_ident(token->ident));
1662 add_ptr_list(&common_funcs, func);
1663 token = token->next;
1665 clear_token_alloc();
1669 void register_definition_db_callbacks(int id)
1671 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1672 add_hook(&global_variable, BASE_HOOK);
1673 add_hook(&global_variable, DECLARATION_HOOK);
1674 add_split_return_callback(match_return_info);
1675 add_split_return_callback(print_returned_struct_members);
1676 add_hook(&call_return_state_hooks, RETURN_HOOK);
1677 add_hook(&match_end_func_info, END_FUNC_HOOK);
1678 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1680 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1681 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1683 register_common_funcs();
1686 void register_db_call_marker(int id)
1688 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1691 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1693 struct expression *arg;
1694 char *name = NULL;
1695 char member_name[256];
1697 *sym = NULL;
1699 if (param == -1) {
1700 const char *star = "";
1702 if (expr->type != EXPR_ASSIGNMENT)
1703 return NULL;
1704 name = expr_to_var_sym(expr->left, sym);
1705 if (!name)
1706 return NULL;
1707 if (key[0] == '*') {
1708 star = "*";
1709 key++;
1711 if (strncmp(key, "$", 1) != 0)
1712 return name;
1713 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1714 free_string(name);
1715 return alloc_string(member_name);
1718 while (expr->type == EXPR_ASSIGNMENT)
1719 expr = strip_expr(expr->right);
1720 if (expr->type != EXPR_CALL)
1721 return NULL;
1723 arg = get_argument_from_call_expr(expr->args, param);
1724 if (!arg)
1725 return NULL;
1727 return get_variable_from_key(arg, key, sym);
1730 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1732 char buf[256];
1733 char *tmp;
1735 if (!arg)
1736 return NULL;
1738 arg = strip_expr(arg);
1740 if (strcmp(key, "$") == 0)
1741 return expr_to_var_sym(arg, sym);
1743 if (strcmp(key, "*$") == 0) {
1744 if (arg->type == EXPR_PREOP && arg->op == '&') {
1745 arg = strip_expr(arg->unop);
1746 return expr_to_var_sym(arg, sym);
1747 } else {
1748 tmp = expr_to_var_sym(arg, sym);
1749 if (!tmp)
1750 return NULL;
1751 snprintf(buf, sizeof(buf), "*%s", tmp);
1752 free_string(tmp);
1753 return alloc_string(buf);
1757 if (arg->type == EXPR_PREOP && arg->op == '&') {
1758 arg = strip_expr(arg->unop);
1759 tmp = expr_to_var_sym(arg, sym);
1760 if (!tmp)
1761 return NULL;
1762 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1763 return alloc_string(buf);
1766 tmp = expr_to_var_sym(arg, sym);
1767 if (!tmp)
1768 return NULL;
1769 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1770 free_string(tmp);
1771 return alloc_string(buf);
1774 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1776 *vsl = NULL;
1778 if (strcmp("$", key) == 0)
1779 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1780 return get_variable_from_key(arg, key, sym);
1783 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1785 char *param_name;
1786 int name_len;
1787 static char buf[256];
1789 if (!sym || !sym->ident)
1790 return NULL;
1792 param_name = sym->ident->name;
1793 name_len = strlen(param_name);
1795 if (strcmp(name, param_name) == 0) {
1796 return "$";
1797 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1798 strncmp(name, param_name, name_len) == 0) {
1799 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1800 return buf;
1801 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1802 return "*$";
1804 return NULL;
1807 const char *get_param_name(struct sm_state *sm)
1809 return get_param_name_var_sym(sm->name, sm->sym);
1812 char *get_data_info_name(struct expression *expr)
1814 struct symbol *sym;
1815 char *name;
1816 char buf[256];
1817 char *ret = NULL;
1819 expr = strip_expr(expr);
1820 name = get_member_name(expr);
1821 if (name)
1822 return name;
1823 name = expr_to_var_sym(expr, &sym);
1824 if (!name || !sym)
1825 goto free;
1826 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1827 goto free;
1828 if (sym->ctype.modifiers & MOD_STATIC)
1829 snprintf(buf, sizeof(buf), "static %s", name);
1830 else
1831 snprintf(buf, sizeof(buf), "global %s", name);
1832 ret = alloc_sname(buf);
1833 free:
1834 free_string(name);
1835 return ret;