slist: Silence a smatch warning
[smatch.git] / smatch_db.c
blob81dbf6f0990a3309232aec28a724be9ddb5c90fb
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 } \
51 break; \
52 } \
53 if (option_info) { \
54 sm_prefix(); \
55 sm_printf("SQL: insert into " #table " values (" values); \
56 sm_printf(");\n"); \
57 } \
58 } while (0)
60 struct def_callback {
61 int hook_type;
62 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
64 ALLOCATOR(def_callback, "definition db hook callbacks");
65 DECLARE_PTR_LIST(callback_list, struct def_callback);
66 static struct callback_list *callbacks;
68 struct member_info_callback {
69 int owner;
70 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
72 ALLOCATOR(member_info_callback, "caller_info callbacks");
73 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
74 static struct member_info_cb_list *member_callbacks;
76 struct returned_state_callback {
77 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
79 ALLOCATOR(returned_state_callback, "returned state callbacks");
80 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
81 static struct returned_state_cb_list *returned_state_callbacks;
83 struct returned_member_callback {
84 int owner;
85 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
87 ALLOCATOR(returned_member_callback, "returned member callbacks");
88 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
89 static struct returned_member_cb_list *returned_member_callbacks;
91 struct call_implies_callback {
92 int type;
93 void (*callback)(struct expression *arg, char *key, char *value);
95 ALLOCATOR(call_implies_callback, "call_implies callbacks");
96 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
97 static struct call_implies_cb_list *call_implies_cb_list;
99 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
101 int i;
103 for (i = 0; i < argc; i++) {
104 if (i != 0)
105 printf(", ");
106 sm_printf("%s", argv[i]);
108 sm_printf("\n");
109 return 0;
112 void debug_sql(const char *sql)
114 if (!option_debug)
115 return;
116 sm_msg("%s", sql);
117 sql_exec(print_sql_output, NULL, sql);
120 void debug_mem_sql(const char *sql)
122 if (!option_debug)
123 return;
124 sm_msg("%s", sql);
125 sql_mem_exec(print_sql_output, NULL, sql);
128 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
130 char *err = NULL;
131 int rc;
133 if (option_no_db || !db)
134 return;
136 rc = sqlite3_exec(db, sql, callback, data, &err);
137 if (rc != SQLITE_OK) {
138 fprintf(stderr, "SQL error #2: %s\n", err);
139 fprintf(stderr, "SQL: '%s'\n", sql);
143 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
145 char *err = NULL;
146 int rc;
148 if (!mem_db)
149 return;
151 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
152 if (rc != SQLITE_OK) {
153 fprintf(stderr, "SQL error #2: %s\n", err);
154 fprintf(stderr, "SQL: '%s'\n", sql);
158 void sql_insert_return_states(int return_id, const char *return_ranges,
159 int type, int param, const char *key, const char *value)
161 if (key && strlen(key) >= 80)
162 return;
163 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
164 get_base_file(), get_function(), (unsigned long)__inline_fn,
165 return_id, return_ranges, fn_static(), type, param, key, value);
168 static struct string_list *common_funcs;
169 static int is_common_function(const char *fn)
171 char *tmp;
173 if (strncmp(fn, "__builtin_", 10) == 0)
174 return 1;
176 FOR_EACH_PTR(common_funcs, tmp) {
177 if (strcmp(tmp, fn) == 0)
178 return 1;
179 } END_FOR_EACH_PTR(tmp);
181 return 0;
184 void sql_insert_caller_info(struct expression *call, int type,
185 int param, const char *key, const char *value)
187 char *fn;
189 if (!option_info && !__inline_call)
190 return;
192 if (key && strlen(key) >= 80)
193 return;
195 fn = get_fnptr_name(call->fn);
196 if (!fn)
197 return;
199 if (__inline_call) {
200 mem_sql(NULL, NULL,
201 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
202 get_base_file(), get_function(), fn, (unsigned long)call,
203 is_static(call->fn), type, param, key, value);
206 if (!option_info)
207 return;
209 if (is_common_function(fn))
210 return;
212 sm_msg("SQL_caller_info: insert into caller_info values ("
213 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
214 get_base_file(), get_function(), fn, is_static(call->fn),
215 type, param, key, value);
217 free_string(fn);
220 void sql_insert_function_ptr(const char *fn, const char *struct_name)
222 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
223 struct_name);
226 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
228 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
229 get_function(), (unsigned long)__inline_fn, fn_static(),
230 type, param, key, value);
233 void sql_insert_function_type_size(const char *member, const char *ranges)
235 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
238 void sql_insert_local_values(const char *name, const char *value)
240 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
243 void sql_insert_function_type_value(const char *type, const char *value)
245 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
248 void sql_insert_function_type_info(int param, const char *value)
250 sql_insert(function_type_info, "'%s', '%s', %d, %d, '%s'",
251 get_base_file(), get_function(), fn_static(), param, value);
254 void sql_insert_data_info(struct expression *data, int type, const char *value)
256 char *data_name;
258 data_name = get_data_info_name(data);
259 if (!data_name)
260 return;
261 sql_insert(data_info, "'%s', '%s', %d, '%s'", get_base_file(), data_name, type, value);
264 char *get_static_filter(struct symbol *sym)
266 static char sql_filter[1024];
268 /* This can only happen on buggy code. Return invalid SQL. */
269 if (!sym) {
270 sql_filter[0] = '\0';
271 return sql_filter;
274 if (sym->ctype.modifiers & MOD_STATIC) {
275 snprintf(sql_filter, sizeof(sql_filter),
276 "file = '%s' and function = '%s' and static = '1'",
277 get_base_file(), sym->ident->name);
278 } else {
279 snprintf(sql_filter, sizeof(sql_filter),
280 "function = '%s' and static = '0'", sym->ident->name);
283 return sql_filter;
286 static int row_count;
287 static int get_row_count(void *unused, int argc, char **argv, char **azColName)
289 if (argc != 1)
290 return 0;
291 row_count = atoi(argv[0]);
292 return 0;
295 static void sql_select_return_states_pointer(const char *cols,
296 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
298 char *ptr;
300 ptr = get_fnptr_name(call->fn);
301 if (!ptr)
302 return;
304 run_sql(callback, info,
305 "select %s from return_states join function_ptr where "
306 "return_states.function == function_ptr.function and ptr = '%s'"
307 "and searchable = 1 order by return_id, type;",
308 cols, ptr);
311 static int is_local_symbol(struct expression *expr)
313 if (expr->type != EXPR_SYMBOL)
314 return 0;
315 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
316 return 0;
317 return 1;
320 void sql_select_return_states(const char *cols, struct expression *call,
321 int (*callback)(void*, int, char**, char**), void *info)
323 if (is_fake_call(call))
324 return;
326 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
327 sql_select_return_states_pointer(cols, call, callback, info);
328 return;
331 if (inlinable(call->fn)) {
332 mem_sql(callback, info,
333 "select %s from return_states where call_id = '%lu' order by return_id, type;",
334 cols, (unsigned long)call);
335 return;
338 row_count = 0;
339 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
340 get_static_filter(call->fn->symbol));
341 if (row_count > 3000)
342 return;
344 run_sql(callback, info, "select %s from return_states where %s order by return_id, type;",
345 cols, get_static_filter(call->fn->symbol));
348 void sql_select_call_implies(const char *cols, struct expression *call,
349 int (*callback)(void*, int, char**, char**))
351 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
352 return;
354 if (inlinable(call->fn)) {
355 mem_sql(callback, call,
356 "select %s from call_implies where call_id = '%lu';",
357 cols, (unsigned long)call);
358 return;
361 run_sql(callback, call, "select %s from call_implies where %s;",
362 cols, get_static_filter(call->fn->symbol));
365 void sql_select_caller_info(const char *cols, struct symbol *sym,
366 int (*callback)(void*, int, char**, char**))
368 if (__inline_fn) {
369 mem_sql(callback, NULL,
370 "select %s from caller_info where call_id = %lu;",
371 cols, (unsigned long)__inline_fn);
372 return;
375 run_sql(callback, NULL,
376 "select %s from caller_info where %s order by call_id;",
377 cols, get_static_filter(sym));
380 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
382 struct def_callback *def_callback = __alloc_def_callback(0);
384 def_callback->hook_type = type;
385 def_callback->callback = callback;
386 add_ptr_list(&callbacks, def_callback);
390 * These call backs are used when the --info option is turned on to print struct
391 * member information. For example foo->bar could have a state in
392 * smatch_extra.c and also check_user.c.
394 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
396 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
398 member_callback->owner = owner;
399 member_callback->callback = callback;
400 add_ptr_list(&member_callbacks, member_callback);
403 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
405 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
407 callback->callback = fn;
408 add_ptr_list(&returned_state_callbacks, callback);
411 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))
413 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
415 member_callback->owner = owner;
416 member_callback->callback = callback;
417 add_ptr_list(&returned_member_callbacks, member_callback);
420 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
422 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
424 cb->type = type;
425 cb->callback = callback;
426 add_ptr_list(&call_implies_cb_list, cb);
429 struct return_info {
430 struct expression *static_returns_call;
431 struct symbol *return_type;
432 struct range_list *return_range_list;
435 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
437 struct return_info *ret_info = _ret_info;
438 struct range_list *rl;
439 struct expression *call_expr = ret_info->static_returns_call;
441 if (argc != 1)
442 return 0;
443 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
444 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
445 return 0;
448 struct range_list *db_return_vals(struct expression *expr)
450 struct return_info ret_info = {};
451 char buf[64];
452 struct sm_state *sm;
454 if (is_fake_call(expr))
455 return NULL;
457 snprintf(buf, sizeof(buf), "return %p", expr);
458 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
459 if (sm)
460 return clone_rl(estate_rl(sm->state));
461 ret_info.static_returns_call = expr;
462 ret_info.return_type = get_type(expr);
463 if (!ret_info.return_type)
464 return NULL;
466 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
467 return NULL;
469 ret_info.return_range_list = NULL;
470 if (inlinable(expr->fn)) {
471 mem_sql(db_return_callback, &ret_info,
472 "select distinct return from return_states where call_id = '%lu';",
473 (unsigned long)expr);
474 } else {
475 run_sql(db_return_callback, &ret_info,
476 "select distinct return from return_states where %s;",
477 get_static_filter(expr->fn->symbol));
479 return ret_info.return_range_list;
482 struct range_list *db_return_vals_from_str(const char *fn_name)
484 struct return_info ret_info;
486 ret_info.static_returns_call = NULL;
487 ret_info.return_type = &llong_ctype;
488 ret_info.return_range_list = NULL;
490 run_sql(db_return_callback, &ret_info,
491 "select distinct return from return_states where function = '%s';",
492 fn_name);
493 return ret_info.return_range_list;
496 static void match_call_marker(struct expression *expr)
499 * we just want to record something in the database so that if we have
500 * two calls like: frob(4); frob(some_unkown); then on the receiving
501 * side we know that sometimes frob is called with unknown parameters.
504 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
507 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
508 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
510 struct sm_state *sm;
511 char *name;
512 struct symbol *sym;
513 int len;
514 char printed_name[256];
515 int is_address = 0;
517 expr = strip_expr(expr);
518 if (expr->type == EXPR_PREOP && expr->op == '&') {
519 expr = strip_expr(expr->unop);
520 is_address = 1;
523 name = expr_to_var_sym(expr, &sym);
524 if (!name || !sym)
525 goto free;
527 len = strlen(name);
528 FOR_EACH_SM(stree, sm) {
529 if (sm->sym != sym)
530 continue;
531 if (strcmp(name, sm->name) == 0) {
532 if (is_address)
533 snprintf(printed_name, sizeof(printed_name), "*$");
534 else /* these are already handled. fixme: handle them here */
535 continue;
536 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
537 snprintf(printed_name, sizeof(printed_name), "*$");
538 } else if (strncmp(name, sm->name, len) == 0) {
539 if (isalnum(sm->name[len]))
540 continue;
541 if (is_address)
542 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
543 else
544 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
545 } else {
546 continue;
548 callback(call, param, printed_name, sm);
549 } END_FOR_EACH_SM(sm);
550 free:
551 free_string(name);
554 static void match_call_info(struct expression *call)
556 struct member_info_callback *cb;
557 struct expression *arg;
558 struct stree *stree;
559 char *name;
560 int i;
562 name = get_fnptr_name(call->fn);
563 if (!name)
564 return;
566 FOR_EACH_PTR(member_callbacks, cb) {
567 stree = get_all_states_stree(cb->owner);
568 i = 0;
569 FOR_EACH_PTR(call->args, arg) {
570 print_struct_members(call, arg, i, stree, cb->callback);
571 i++;
572 } END_FOR_EACH_PTR(arg);
573 free_stree(&stree);
574 } END_FOR_EACH_PTR(cb);
576 free_string(name);
579 static int get_param(int param, char **name, struct symbol **sym)
581 struct symbol *arg;
582 int i;
584 i = 0;
585 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
587 * this is a temporary hack to work around a bug (I think in sparse?)
588 * 2.6.37-rc1:fs/reiserfs/journal.o
589 * If there is a function definition without parameter name found
590 * after a function implementation then it causes a crash.
591 * int foo() {}
592 * int bar(char *);
594 if (arg->ident->name < (char *)100)
595 continue;
596 if (i == param) {
597 *name = arg->ident->name;
598 *sym = arg;
599 return TRUE;
601 i++;
602 } END_FOR_EACH_PTR(arg);
604 return FALSE;
607 static struct stree *final_states;
608 static int prev_func_id = -1;
609 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
611 int func_id;
612 long type;
613 long param;
614 char *key;
615 char *value;
616 char *name = NULL;
617 struct symbol *sym = NULL;
618 struct def_callback *def_callback;
619 struct stree *stree;
621 if (argc != 5)
622 return 0;
624 func_id = atoi(argv[0]);
625 errno = 0;
626 type = strtol(argv[1], NULL, 10);
627 param = strtol(argv[2], NULL, 10);
628 if (errno)
629 return 0;
630 key = argv[3];
631 value = argv[4];
634 if (prev_func_id == -1)
635 prev_func_id = func_id;
636 if (func_id != prev_func_id) {
637 stree = __pop_fake_cur_stree();
638 merge_stree(&final_states, stree);
639 free_stree(&stree);
640 __push_fake_cur_stree();
641 __unnullify_path();
642 prev_func_id = func_id;
645 if (param >= 0 && !get_param(param, &name, &sym))
646 return 0;
648 FOR_EACH_PTR(callbacks, def_callback) {
649 if (def_callback->hook_type == type)
650 def_callback->callback(name, sym, key, value);
651 } END_FOR_EACH_PTR(def_callback);
653 return 0;
656 static void get_direct_callers(struct symbol *sym)
658 sql_select_caller_info("call_id, type, parameter, key, value", sym,
659 caller_info_callback);
662 static struct string_list *ptr_names_done;
663 static struct string_list *ptr_names;
665 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
667 insert_string(&ptr_names, alloc_string(argv[0]));
668 return 0;
671 static char *get_next_ptr_name(void)
673 char *ptr;
675 FOR_EACH_PTR(ptr_names, ptr) {
676 if (list_has_string(ptr_names_done, ptr))
677 continue;
678 insert_string(&ptr_names_done, ptr);
679 return ptr;
680 } END_FOR_EACH_PTR(ptr);
681 return NULL;
684 static void get_ptr_names(const char *file, const char *name)
686 char sql_filter[1024];
687 int before, after;
689 if (file) {
690 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
691 file, name);
692 } else {
693 snprintf(sql_filter, 1024, "function = '%s';", name);
696 before = ptr_list_size((struct ptr_list *)ptr_names);
698 run_sql(get_ptr_name, NULL,
699 "select distinct ptr from function_ptr where %s",
700 sql_filter);
702 after = ptr_list_size((struct ptr_list *)ptr_names);
703 if (before == after)
704 return;
706 while ((name = get_next_ptr_name()))
707 get_ptr_names(NULL, name);
710 static void match_data_from_db(struct symbol *sym)
712 struct sm_state *sm;
713 struct stree *stree;
715 if (!sym || !sym->ident)
716 return;
718 __push_fake_cur_stree();
719 __unnullify_path();
720 prev_func_id = -1;
722 if (!__inline_fn) {
723 char *ptr;
725 if (sym->ctype.modifiers & MOD_STATIC)
726 get_ptr_names(get_base_file(), sym->ident->name);
727 else
728 get_ptr_names(NULL, sym->ident->name);
730 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
731 __free_ptr_list((struct ptr_list **)&ptr_names);
732 __free_ptr_list((struct ptr_list **)&ptr_names_done);
733 stree = __pop_fake_cur_stree();
734 free_stree(&stree);
735 return;
738 get_direct_callers(sym);
740 FOR_EACH_PTR(ptr_names, ptr) {
741 run_sql(caller_info_callback, NULL,
742 "select call_id, type, parameter, key, value"
743 " from caller_info where function = '%s' order by call_id",
744 ptr);
745 free_string(ptr);
746 } END_FOR_EACH_PTR(ptr);
748 __free_ptr_list((struct ptr_list **)&ptr_names);
749 __free_ptr_list((struct ptr_list **)&ptr_names_done);
750 } else {
751 get_direct_callers(sym);
754 stree = __pop_fake_cur_stree();
755 merge_stree(&final_states, stree);
756 free_stree(&stree);
758 FOR_EACH_SM(final_states, sm) {
759 __set_sm(sm);
760 } END_FOR_EACH_SM(sm);
762 free_stree(&final_states);
765 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
767 struct expression *call_expr = _call;
768 struct call_implies_callback *cb;
769 struct expression *arg = NULL;
770 int type;
771 int param;
773 if (argc != 5)
774 return 0;
776 type = atoi(argv[1]);
777 param = atoi(argv[2]);
779 FOR_EACH_PTR(call_implies_cb_list, cb) {
780 if (cb->type != type)
781 continue;
782 if (param != -1) {
783 arg = get_argument_from_call_expr(call_expr->args, param);
784 if (!arg)
785 continue;
787 cb->callback(arg, argv[3], argv[4]);
788 } END_FOR_EACH_PTR(cb);
790 return 0;
793 static void match_call_implies(struct expression *expr)
795 sql_select_call_implies("function, type, parameter, key, value", expr,
796 call_implies_callbacks);
799 static void print_initializer_list(struct expression_list *expr_list,
800 struct symbol *struct_type)
802 struct expression *expr;
803 struct symbol *base_type;
804 char struct_name[256];
806 FOR_EACH_PTR(expr_list, expr) {
807 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
808 print_initializer_list(expr->idx_expression->expr_list, struct_type);
809 continue;
811 if (expr->type != EXPR_IDENTIFIER)
812 continue;
813 if (!expr->expr_ident)
814 continue;
815 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
816 continue;
817 base_type = get_type(expr->ident_expression);
818 if (!base_type || base_type->type != SYM_FN)
819 continue;
820 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
821 struct_type->ident->name, expr->expr_ident->name);
822 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
823 struct_name);
824 } END_FOR_EACH_PTR(expr);
827 static void global_variable(struct symbol *sym)
829 struct symbol *struct_type;
831 if (!sym->ident)
832 return;
833 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
834 return;
835 struct_type = get_base_type(sym);
836 if (!struct_type)
837 return;
838 if (struct_type->type == SYM_ARRAY) {
839 struct_type = get_base_type(struct_type);
840 if (!struct_type)
841 return;
843 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
844 return;
845 print_initializer_list(sym->initializer->expr_list, struct_type);
848 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
850 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
853 static void call_return_state_hooks_conditional(struct expression *expr)
855 struct returned_state_callback *cb;
856 struct range_list *rl;
857 char *return_ranges;
858 int final_pass_orig = final_pass;
860 __push_fake_cur_stree();
862 final_pass = 0;
863 __split_whole_condition(expr->conditional);
864 final_pass = final_pass_orig;
866 if (get_implied_rl(expr->cond_true, &rl))
867 rl = cast_rl(cur_func_return_type(), rl);
868 else
869 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
870 return_ranges = show_rl(rl);
871 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
873 return_id++;
874 FOR_EACH_PTR(returned_state_callbacks, cb) {
875 cb->callback(return_id, return_ranges, expr);
876 } END_FOR_EACH_PTR(cb);
878 __push_true_states();
879 __use_false_states();
881 if (get_implied_rl(expr->cond_false, &rl))
882 rl = cast_rl(cur_func_return_type(), rl);
883 else
884 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
885 return_ranges = show_rl(rl);
886 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
888 return_id++;
889 FOR_EACH_PTR(returned_state_callbacks, cb) {
890 cb->callback(return_id, return_ranges, expr);
891 } END_FOR_EACH_PTR(cb);
893 __merge_true_states();
894 __free_fake_cur_stree();
897 static void call_return_state_hooks_compare(struct expression *expr)
899 struct returned_state_callback *cb;
900 char *return_ranges;
901 int final_pass_orig = final_pass;
902 sval_t sval = { .type = &int_ctype };
903 sval_t ret;
905 if (!get_implied_value(expr, &ret))
906 ret.value = -1;
908 __push_fake_cur_stree();
910 final_pass = 0;
911 __split_whole_condition(expr);
912 final_pass = final_pass_orig;
914 if (ret.value != 0) {
915 return_ranges = alloc_sname("1");
916 sval.value = 1;
917 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
919 return_id++;
920 FOR_EACH_PTR(returned_state_callbacks, cb) {
921 cb->callback(return_id, return_ranges, expr);
922 } END_FOR_EACH_PTR(cb);
925 __push_true_states();
926 __use_false_states();
928 if (ret.value != 1) {
929 return_ranges = alloc_sname("0");
930 sval.value = 0;
931 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
933 return_id++;
934 FOR_EACH_PTR(returned_state_callbacks, cb) {
935 cb->callback(return_id, return_ranges, expr);
936 } END_FOR_EACH_PTR(cb);
939 __merge_true_states();
940 __free_fake_cur_stree();
943 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
945 struct sm_state *tmp;
947 FOR_EACH_PTR(slist, tmp) {
948 if (strcmp(tmp->state->name, sm->state->name) == 0)
949 return 1;
950 } END_FOR_EACH_PTR(tmp);
952 return 0;
955 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
957 struct returned_state_callback *cb;
958 struct range_list *rl;
959 char *return_ranges;
960 struct sm_state *tmp;
961 int ret = 0;
962 int nr_possible, nr_states;
963 char *compare_str = NULL;
964 char buf[128];
965 struct state_list *already_handled = NULL;
967 if (!sm || !sm->merged)
968 return 0;
970 if (too_many_possible(sm))
971 return 0;
973 /* bail if it gets too complicated */
974 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
975 nr_states = stree_count(__get_cur_stree());
976 if (option_info && nr_states * nr_possible >= 2000)
977 return 0;
979 FOR_EACH_PTR(sm->possible, tmp) {
980 if (tmp->merged)
981 continue;
982 if (ptr_in_list(tmp, already_handled))
983 continue;
984 add_ptr_list(&already_handled, tmp);
986 ret = 1;
987 __push_fake_cur_stree();
989 overwrite_states_using_pool(sm, tmp);
991 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
992 return_ranges = show_rl(rl);
993 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
994 compare_str = expr_lte_to_param(expr, -1);
995 if (compare_str) {
996 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
997 return_ranges = alloc_sname(buf);
1000 return_id++;
1001 FOR_EACH_PTR(returned_state_callbacks, cb) {
1002 cb->callback(return_id, return_ranges, expr);
1003 } END_FOR_EACH_PTR(cb);
1005 __free_fake_cur_stree();
1006 } END_FOR_EACH_PTR(tmp);
1008 free_slist(&already_handled);
1010 return ret;
1013 static int call_return_state_hooks_split_possible(struct expression *expr)
1015 struct sm_state *sm;
1017 if (!expr || expr_equal_to_param(expr, -1))
1018 return 0;
1020 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1021 return split_possible_helper(sm, expr);
1024 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1026 struct range_list *rl;
1027 char *return_ranges;
1028 sval_t sval;
1029 char *compare_str;
1030 char *math_str;
1031 char buf[128];
1033 *rl_p = NULL;
1035 if (!expr)
1036 return alloc_sname("");
1038 if (get_implied_value(expr, &sval)) {
1039 *rl_p = alloc_rl(sval, sval);
1040 return sval_to_str(sval);
1043 compare_str = expr_equal_to_param(expr, -1);
1044 math_str = get_value_in_terms_of_parameter_math(expr);
1046 if (get_implied_rl(expr, &rl)) {
1047 rl = cast_rl(cur_func_return_type(), rl);
1048 return_ranges = show_rl(rl);
1049 } else if (get_imaginary_absolute(expr, &rl)){
1050 rl = cast_rl(cur_func_return_type(), rl);
1051 return alloc_sname(show_rl(rl));
1052 } else {
1053 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1054 return_ranges = show_rl(rl);
1056 *rl_p = rl;
1058 if (compare_str) {
1059 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1060 return alloc_sname(buf);
1063 if (math_str) {
1064 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1065 return alloc_sname(buf);
1068 compare_str = expr_lte_to_param(expr, -1);
1069 if (compare_str) {
1070 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1071 return alloc_sname(buf);
1073 return return_ranges;
1076 static int split_positive_from_negative(struct expression *expr)
1078 struct returned_state_callback *cb;
1079 struct range_list *rl;
1080 const char *return_ranges;
1081 struct range_list *ret_rl;
1082 int undo;
1084 /* We're going to print the states 3 times */
1085 if (stree_count(__get_cur_stree()) > 10000 / 3)
1086 return 0;
1088 if (!get_implied_rl(expr, &rl) || !rl)
1089 return 0;
1090 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1091 return 0;
1092 /* Forget about INT_MAX and larger */
1093 if (rl_max(rl).value <= 0)
1094 return 0;
1095 if (!sval_is_negative(rl_min(rl)))
1096 return 0;
1098 if (!assume(compare_expression(expr, '>', zero_expr())))
1099 return 0;
1101 return_id++;
1102 return_ranges = get_return_ranges_str(expr, &ret_rl);
1103 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1104 FOR_EACH_PTR(returned_state_callbacks, cb) {
1105 cb->callback(return_id, (char *)return_ranges, expr);
1106 } END_FOR_EACH_PTR(cb);
1108 end_assume();
1110 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1111 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1113 return_id++;
1114 return_ranges = get_return_ranges_str(expr, &ret_rl);
1115 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1116 FOR_EACH_PTR(returned_state_callbacks, cb) {
1117 cb->callback(return_id, (char *)return_ranges, expr);
1118 } END_FOR_EACH_PTR(cb);
1120 if (undo)
1121 end_assume();
1124 undo = assume(compare_expression(expr, '<', zero_expr()));
1126 return_id++;
1127 return_ranges = get_return_ranges_str(expr, &ret_rl);
1128 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1129 FOR_EACH_PTR(returned_state_callbacks, cb) {
1130 cb->callback(return_id, (char *)return_ranges, expr);
1131 } END_FOR_EACH_PTR(cb);
1133 if (undo)
1134 end_assume();
1136 return 1;
1139 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1141 struct returned_state_callback *cb;
1142 struct range_list *rl;
1143 struct range_list *nonnull_rl;
1144 sval_t null_sval;
1145 struct range_list *null_rl = NULL;
1146 char *return_ranges;
1147 struct smatch_state *state;
1148 int nr_states;
1149 int final_pass_orig = final_pass;
1151 if (!expr || expr_equal_to_param(expr, -1))
1152 return 0;
1153 if (expr->type == EXPR_CALL)
1154 return 0;
1155 if (!is_pointer(expr))
1156 return 0;
1158 state = get_state_expr(SMATCH_EXTRA, expr);
1159 if (!state || !estate_rl(state))
1160 return 0;
1161 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1162 return 0;
1163 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1164 return 0;
1166 nr_states = stree_count(__get_cur_stree());
1167 if (option_info && nr_states >= 1500)
1168 return 0;
1170 rl = estate_rl(state);
1172 __push_fake_cur_stree();
1174 final_pass = 0;
1175 __split_whole_condition(expr);
1176 final_pass = final_pass_orig;
1178 nonnull_rl = rl_filter(rl, rl_zero());
1179 return_ranges = show_rl(nonnull_rl);
1180 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1182 return_id++;
1183 FOR_EACH_PTR(returned_state_callbacks, cb) {
1184 cb->callback(return_id, return_ranges, expr);
1185 } END_FOR_EACH_PTR(cb);
1187 __push_true_states();
1188 __use_false_states();
1190 return_ranges = alloc_sname("0");
1191 null_sval = sval_type_val(rl_type(rl), 0);
1192 add_range(&null_rl, null_sval, null_sval);
1193 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1194 return_id++;
1195 FOR_EACH_PTR(returned_state_callbacks, cb) {
1196 cb->callback(return_id, return_ranges, expr);
1197 } END_FOR_EACH_PTR(cb);
1199 __merge_true_states();
1200 __free_fake_cur_stree();
1202 return 1;
1205 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1207 struct range_list *rl;
1208 struct range_list *nonzero_rl;
1209 sval_t zero_sval;
1210 struct range_list *zero_rl = NULL;
1211 int nr_states;
1212 struct returned_state_callback *cb;
1213 char *return_ranges;
1214 int final_pass_orig = final_pass;
1215 sval_t val;
1217 if (option_project != PROJ_KERNEL)
1218 return 0;
1220 nr_states = stree_count(__get_cur_stree());
1221 if (nr_states > 1500)
1222 return 0;
1224 if (get_value(expr, &val))
1225 return 0;
1226 if (!get_implied_rl(expr, &rl))
1227 return 0;
1228 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1229 return 0;
1230 if (rl_max(rl).value != 0)
1231 return 0;
1233 __push_fake_cur_stree();
1235 final_pass = 0;
1236 __split_whole_condition(expr);
1237 final_pass = final_pass_orig;
1239 nonzero_rl = rl_filter(rl, rl_zero());
1240 return_ranges = show_rl(nonzero_rl);
1241 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1243 return_id++;
1244 FOR_EACH_PTR(returned_state_callbacks, cb) {
1245 cb->callback(return_id, return_ranges, expr);
1246 } END_FOR_EACH_PTR(cb);
1248 __push_true_states();
1249 __use_false_states();
1251 return_ranges = alloc_sname("0");
1252 zero_sval = sval_type_val(rl_type(rl), 0);
1253 add_range(&zero_rl, zero_sval, zero_sval);
1254 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1255 return_id++;
1256 FOR_EACH_PTR(returned_state_callbacks, cb) {
1257 cb->callback(return_id, return_ranges, expr);
1258 } END_FOR_EACH_PTR(cb);
1260 __merge_true_states();
1261 __free_fake_cur_stree();
1263 return 1;
1266 static int is_boolean(struct expression *expr)
1268 struct range_list *rl;
1270 if (!get_implied_rl(expr, &rl))
1271 return 0;
1272 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1273 return 1;
1274 return 0;
1277 static int is_conditional(struct expression *expr)
1279 if (!expr)
1280 return 0;
1281 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1282 return 1;
1283 return 0;
1286 static int splitable_function_call(struct expression *expr)
1288 struct sm_state *sm;
1289 char buf[64];
1291 if (!expr || expr->type != EXPR_CALL)
1292 return 0;
1293 snprintf(buf, sizeof(buf), "return %p", expr);
1294 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1295 return split_possible_helper(sm, expr);
1298 static struct sm_state *find_bool_param(void)
1300 struct stree *start_states;
1301 struct symbol *arg;
1302 struct sm_state *sm, *tmp;
1303 sval_t sval;
1305 start_states = get_start_states();
1307 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1308 if (!arg->ident || !arg->ident->name)
1309 continue;
1310 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1311 if (!sm)
1312 continue;
1313 if (rl_min(estate_rl(sm->state)).value != 0 ||
1314 rl_max(estate_rl(sm->state)).value != 1)
1315 continue;
1316 goto found;
1317 } END_FOR_EACH_PTR_REVERSE(arg);
1319 return NULL;
1321 found:
1323 * Check if it's splitable. If not, then splitting it up is likely not
1324 * useful for the callers.
1326 FOR_EACH_PTR(sm->possible, tmp) {
1327 if (is_merged(tmp))
1328 continue;
1329 if (!estate_get_single_value(tmp->state, &sval))
1330 return NULL;
1331 } END_FOR_EACH_PTR(tmp);
1333 return sm;
1336 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1338 struct returned_state_callback *cb;
1339 struct range_list *ret_rl;
1340 const char *return_ranges;
1341 struct sm_state *tmp;
1342 int ret = 0;
1343 int nr_possible, nr_states;
1344 char *compare_str = NULL;
1345 char buf[128];
1346 struct state_list *already_handled = NULL;
1348 if (!sm || !sm->merged)
1349 return 0;
1351 if (too_many_possible(sm))
1352 return 0;
1354 /* bail if it gets too complicated */
1355 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1356 nr_states = stree_count(__get_cur_stree());
1357 if (option_info && nr_states * nr_possible >= 2000)
1358 return 0;
1360 FOR_EACH_PTR(sm->possible, tmp) {
1361 if (tmp->merged)
1362 continue;
1363 if (ptr_in_list(tmp, already_handled))
1364 continue;
1365 add_ptr_list(&already_handled, tmp);
1367 ret = 1;
1368 __push_fake_cur_stree();
1370 overwrite_states_using_pool(sm, tmp);
1372 return_ranges = get_return_ranges_str(expr, &ret_rl);
1373 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1374 compare_str = expr_lte_to_param(expr, -1);
1375 if (compare_str) {
1376 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1377 return_ranges = alloc_sname(buf);
1380 return_id++;
1381 FOR_EACH_PTR(returned_state_callbacks, cb) {
1382 cb->callback(return_id, (char *)return_ranges, expr);
1383 } END_FOR_EACH_PTR(cb);
1385 __free_fake_cur_stree();
1386 } END_FOR_EACH_PTR(tmp);
1388 free_slist(&already_handled);
1390 return ret;
1393 static int split_by_bool_param(struct expression *expr)
1395 struct sm_state *start_sm, *sm;
1396 sval_t sval;
1398 start_sm = find_bool_param();
1399 if (!start_sm)
1400 return 0;
1401 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1402 if (!sm || estate_get_single_value(sm->state, &sval))
1403 return 0;
1404 return split_on_bool_sm(sm, expr);
1407 static void call_return_state_hooks(struct expression *expr)
1409 struct returned_state_callback *cb;
1410 struct range_list *ret_rl;
1411 const char *return_ranges;
1412 int nr_states;
1413 sval_t sval;
1415 if (__path_is_null())
1416 return;
1418 expr = strip_expr(expr);
1420 if (is_impossible_path())
1421 goto vanilla;
1423 if (expr && (expr->type == EXPR_COMPARE ||
1424 !get_implied_value(expr, &sval)) &&
1425 (is_condition(expr) || is_boolean(expr))) {
1426 call_return_state_hooks_compare(expr);
1427 return;
1428 } else if (is_conditional(expr)) {
1429 call_return_state_hooks_conditional(expr);
1430 return;
1431 } else if (call_return_state_hooks_split_possible(expr)) {
1432 return;
1433 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1434 return;
1435 } else if (call_return_state_hooks_split_success_fail(expr)) {
1436 return;
1437 } else if (splitable_function_call(expr)) {
1438 return;
1439 } else if (split_positive_from_negative(expr)) {
1440 return;
1441 } else if (split_by_bool_param(expr)) {
1442 return;
1445 vanilla:
1446 return_ranges = get_return_ranges_str(expr, &ret_rl);
1447 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1449 return_id++;
1450 nr_states = stree_count(__get_cur_stree());
1451 if (nr_states >= 10000) {
1452 match_return_info(return_id, (char *)return_ranges, expr);
1453 return;
1455 FOR_EACH_PTR(returned_state_callbacks, cb) {
1456 cb->callback(return_id, (char *)return_ranges, expr);
1457 } END_FOR_EACH_PTR(cb);
1460 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1462 struct returned_member_callback *cb;
1463 struct stree *stree;
1464 struct sm_state *sm;
1465 struct symbol *type;
1466 char *name;
1467 char member_name[256];
1468 int len;
1470 type = get_type(expr);
1471 if (!type || type->type != SYM_PTR)
1472 return;
1473 name = expr_to_var(expr);
1474 if (!name)
1475 return;
1477 member_name[sizeof(member_name) - 1] = '\0';
1478 strcpy(member_name, "$");
1480 len = strlen(name);
1481 FOR_EACH_PTR(returned_member_callbacks, cb) {
1482 stree = __get_cur_stree();
1483 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1484 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1485 strcpy(member_name, "*$");
1486 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1487 continue;
1489 if (strncmp(sm->name, name, len) != 0)
1490 continue;
1491 if (strncmp(sm->name + len, "->", 2) != 0)
1492 continue;
1493 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1494 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1495 } END_FOR_EACH_SM(sm);
1496 } END_FOR_EACH_PTR(cb);
1498 free_string(name);
1501 static void reset_memdb(struct symbol *sym)
1503 mem_sql(NULL, NULL, "delete from caller_info;");
1504 mem_sql(NULL, NULL, "delete from return_states;");
1505 mem_sql(NULL, NULL, "delete from call_implies;");
1508 static void match_end_func_info(struct symbol *sym)
1510 if (__path_is_null())
1511 return;
1512 call_return_state_hooks(NULL);
1515 static void match_after_func(struct symbol *sym)
1517 if (!__inline_fn)
1518 reset_memdb(sym);
1521 static void init_memdb(void)
1523 char *err = NULL;
1524 int rc;
1525 const char *schema_files[] = {
1526 "db/db.schema",
1527 "db/caller_info.schema",
1528 "db/return_states.schema",
1529 "db/function_type_size.schema",
1530 "db/type_size.schema",
1531 "db/call_implies.schema",
1532 "db/function_ptr.schema",
1533 "db/local_values.schema",
1534 "db/function_type_value.schema",
1535 "db/type_value.schema",
1536 "db/function_type_info.schema",
1537 "db/data_info.schema",
1539 static char buf[4096];
1540 int fd;
1541 int ret;
1542 int i;
1544 rc = sqlite3_open(":memory:", &mem_db);
1545 if (rc != SQLITE_OK) {
1546 printf("Error starting In-Memory database.");
1547 return;
1550 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1551 fd = open_data_file(schema_files[i]);
1552 if (fd < 0) {
1553 mem_db = NULL;
1554 return;
1556 ret = read(fd, buf, sizeof(buf));
1557 if (ret == sizeof(buf)) {
1558 printf("Schema file too large: %s (limit %zd bytes)",
1559 schema_files[i], sizeof(buf));
1561 buf[ret] = '\0';
1562 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1563 if (rc != SQLITE_OK) {
1564 fprintf(stderr, "SQL error #2: %s\n", err);
1565 fprintf(stderr, "%s\n", buf);
1570 void open_smatch_db(void)
1572 int rc;
1574 if (option_no_db)
1575 return;
1577 init_memdb();
1579 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1580 if (rc != SQLITE_OK) {
1581 option_no_db = 1;
1582 return;
1584 return;
1587 static void register_common_funcs(void)
1589 struct token *token;
1590 char *func;
1591 char filename[256];
1593 if (option_project == PROJ_NONE)
1594 strcpy(filename, "common_functions");
1595 else
1596 snprintf(filename, 256, "%s.common_functions", option_project_str);
1598 token = get_tokens_file(filename);
1599 if (!token)
1600 return;
1601 if (token_type(token) != TOKEN_STREAMBEGIN)
1602 return;
1603 token = token->next;
1604 while (token_type(token) != TOKEN_STREAMEND) {
1605 if (token_type(token) != TOKEN_IDENT)
1606 return;
1607 func = alloc_string(show_ident(token->ident));
1608 add_ptr_list(&common_funcs, func);
1609 token = token->next;
1611 clear_token_alloc();
1615 void register_definition_db_callbacks(int id)
1617 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1618 add_hook(&global_variable, BASE_HOOK);
1619 add_hook(&global_variable, DECLARATION_HOOK);
1620 add_split_return_callback(match_return_info);
1621 add_split_return_callback(print_returned_struct_members);
1622 add_hook(&call_return_state_hooks, RETURN_HOOK);
1623 add_hook(&match_end_func_info, END_FUNC_HOOK);
1624 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1626 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1627 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1629 register_common_funcs();
1632 void register_db_call_marker(int id)
1634 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1637 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1639 struct expression *arg;
1640 char *name = NULL;
1641 char member_name[256];
1643 *sym = NULL;
1645 if (param == -1) {
1646 const char *star = "";
1648 if (expr->type != EXPR_ASSIGNMENT)
1649 return NULL;
1650 name = expr_to_var_sym(expr->left, sym);
1651 if (!name)
1652 return NULL;
1653 if (key[0] == '*') {
1654 star = "*";
1655 key++;
1657 if (strncmp(key, "$", 1) != 0)
1658 return name;
1659 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1660 free_string(name);
1661 return alloc_string(member_name);
1664 while (expr->type == EXPR_ASSIGNMENT)
1665 expr = strip_expr(expr->right);
1666 if (expr->type != EXPR_CALL)
1667 return NULL;
1669 arg = get_argument_from_call_expr(expr->args, param);
1670 if (!arg)
1671 return NULL;
1673 return get_variable_from_key(arg, key, sym);
1676 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1678 char buf[256];
1679 char *tmp;
1681 if (!arg)
1682 return NULL;
1684 arg = strip_expr(arg);
1686 if (strcmp(key, "$") == 0)
1687 return expr_to_var_sym(arg, sym);
1689 if (strcmp(key, "*$") == 0) {
1690 if (arg->type == EXPR_PREOP && arg->op == '&') {
1691 arg = strip_expr(arg->unop);
1692 return expr_to_var_sym(arg, sym);
1693 } else {
1694 tmp = expr_to_var_sym(arg, sym);
1695 if (!tmp)
1696 return NULL;
1697 snprintf(buf, sizeof(buf), "*%s", tmp);
1698 free_string(tmp);
1699 return alloc_string(buf);
1703 if (arg->type == EXPR_PREOP && arg->op == '&') {
1704 arg = strip_expr(arg->unop);
1705 tmp = expr_to_var_sym(arg, sym);
1706 if (!tmp)
1707 return NULL;
1708 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1709 return alloc_string(buf);
1712 tmp = expr_to_var_sym(arg, sym);
1713 if (!tmp)
1714 return NULL;
1715 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1716 free_string(tmp);
1717 return alloc_string(buf);
1720 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1722 *vsl = NULL;
1724 if (strcmp("$", key) == 0)
1725 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1726 return get_variable_from_key(arg, key, sym);
1729 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1731 char *param_name;
1732 int name_len;
1733 static char buf[256];
1735 if (!sym || !sym->ident)
1736 return NULL;
1738 param_name = sym->ident->name;
1739 name_len = strlen(param_name);
1741 if (strcmp(name, param_name) == 0) {
1742 return "$";
1743 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1744 strncmp(name, param_name, name_len) == 0) {
1745 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1746 return buf;
1747 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1748 return "*$";
1750 return NULL;
1753 const char *get_param_name(struct sm_state *sm)
1755 return get_param_name_var_sym(sm->name, sm->sym);
1758 char *get_data_info_name(struct expression *expr)
1760 struct symbol *sym;
1761 char *name;
1762 char buf[256];
1763 char *ret = NULL;
1765 expr = strip_expr(expr);
1766 name = get_member_name(expr);
1767 if (name)
1768 return name;
1769 name = expr_to_var_sym(expr, &sym);
1770 if (!name || !sym)
1771 goto free;
1772 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1773 goto free;
1774 if (sym->ctype.modifiers & MOD_STATIC)
1775 snprintf(buf, sizeof(buf), "static %s", name);
1776 else
1777 snprintf(buf, sizeof(buf), "global %s", name);
1778 ret = alloc_sname(buf);
1779 free:
1780 free_string(name);
1781 return ret;