implied: fix a bug handling parameter implications
[smatch.git] / smatch_db.c
blob91a1a0bddbbe18bbdb6b6cdfca9a07d6613f8bdc
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 "
308 "order by function_ptr.file, return_states.file, return_id, type;",
309 cols, ptr);
312 static int is_local_symbol(struct expression *expr)
314 if (expr->type != EXPR_SYMBOL)
315 return 0;
316 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
317 return 0;
318 return 1;
321 void sql_select_return_states(const char *cols, struct expression *call,
322 int (*callback)(void*, int, char**, char**), void *info)
324 if (is_fake_call(call))
325 return;
327 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
328 sql_select_return_states_pointer(cols, call, callback, info);
329 return;
332 if (inlinable(call->fn)) {
333 mem_sql(callback, info,
334 "select %s from return_states where call_id = '%lu' order by return_id, type;",
335 cols, (unsigned long)call);
336 return;
339 row_count = 0;
340 run_sql(get_row_count, info, "select count(*) from return_states where %s;",
341 get_static_filter(call->fn->symbol));
342 if (row_count > 3000)
343 return;
345 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
346 cols, get_static_filter(call->fn->symbol));
349 void sql_select_call_implies(const char *cols, struct expression *call,
350 int (*callback)(void*, int, char**, char**))
352 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
353 return;
355 if (inlinable(call->fn)) {
356 mem_sql(callback, call,
357 "select %s from call_implies where call_id = '%lu';",
358 cols, (unsigned long)call);
359 return;
362 run_sql(callback, call, "select %s from call_implies where %s;",
363 cols, get_static_filter(call->fn->symbol));
366 void sql_select_caller_info(const char *cols, struct symbol *sym,
367 int (*callback)(void*, int, char**, char**))
369 if (__inline_fn) {
370 mem_sql(callback, NULL,
371 "select %s from caller_info where call_id = %lu;",
372 cols, (unsigned long)__inline_fn);
373 return;
376 run_sql(callback, NULL,
377 "select %s from caller_info where %s order by call_id;",
378 cols, get_static_filter(sym));
381 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
383 struct def_callback *def_callback = __alloc_def_callback(0);
385 def_callback->hook_type = type;
386 def_callback->callback = callback;
387 add_ptr_list(&callbacks, def_callback);
391 * These call backs are used when the --info option is turned on to print struct
392 * member information. For example foo->bar could have a state in
393 * smatch_extra.c and also check_user.c.
395 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
397 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
399 member_callback->owner = owner;
400 member_callback->callback = callback;
401 add_ptr_list(&member_callbacks, member_callback);
404 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
406 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
408 callback->callback = fn;
409 add_ptr_list(&returned_state_callbacks, callback);
412 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))
414 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
416 member_callback->owner = owner;
417 member_callback->callback = callback;
418 add_ptr_list(&returned_member_callbacks, member_callback);
421 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
423 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
425 cb->type = type;
426 cb->callback = callback;
427 add_ptr_list(&call_implies_cb_list, cb);
430 struct return_info {
431 struct expression *static_returns_call;
432 struct symbol *return_type;
433 struct range_list *return_range_list;
436 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
438 struct return_info *ret_info = _ret_info;
439 struct range_list *rl;
440 struct expression *call_expr = ret_info->static_returns_call;
442 if (argc != 1)
443 return 0;
444 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
445 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
446 return 0;
449 struct range_list *db_return_vals(struct expression *expr)
451 struct return_info ret_info = {};
452 char buf[64];
453 struct sm_state *sm;
455 if (is_fake_call(expr))
456 return NULL;
458 snprintf(buf, sizeof(buf), "return %p", expr);
459 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
460 if (sm)
461 return clone_rl(estate_rl(sm->state));
462 ret_info.static_returns_call = expr;
463 ret_info.return_type = get_type(expr);
464 if (!ret_info.return_type)
465 return NULL;
467 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
468 return NULL;
470 ret_info.return_range_list = NULL;
471 if (inlinable(expr->fn)) {
472 mem_sql(db_return_callback, &ret_info,
473 "select distinct return from return_states where call_id = '%lu';",
474 (unsigned long)expr);
475 } else {
476 run_sql(db_return_callback, &ret_info,
477 "select distinct return from return_states where %s;",
478 get_static_filter(expr->fn->symbol));
480 return ret_info.return_range_list;
483 struct range_list *db_return_vals_from_str(const char *fn_name)
485 struct return_info ret_info;
487 ret_info.static_returns_call = NULL;
488 ret_info.return_type = &llong_ctype;
489 ret_info.return_range_list = NULL;
491 run_sql(db_return_callback, &ret_info,
492 "select distinct return from return_states where function = '%s';",
493 fn_name);
494 return ret_info.return_range_list;
497 static void match_call_marker(struct expression *expr)
500 * we just want to record something in the database so that if we have
501 * two calls like: frob(4); frob(some_unkown); then on the receiving
502 * side we know that sometimes frob is called with unknown parameters.
505 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
508 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
509 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
511 struct sm_state *sm;
512 char *name;
513 struct symbol *sym;
514 int len;
515 char printed_name[256];
516 int is_address = 0;
518 expr = strip_expr(expr);
519 if (expr->type == EXPR_PREOP && expr->op == '&') {
520 expr = strip_expr(expr->unop);
521 is_address = 1;
524 name = expr_to_var_sym(expr, &sym);
525 if (!name || !sym)
526 goto free;
528 len = strlen(name);
529 FOR_EACH_SM(stree, sm) {
530 if (sm->sym != sym)
531 continue;
532 if (strcmp(name, sm->name) == 0) {
533 if (is_address)
534 snprintf(printed_name, sizeof(printed_name), "*$");
535 else /* these are already handled. fixme: handle them here */
536 continue;
537 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
538 snprintf(printed_name, sizeof(printed_name), "*$");
539 } else if (strncmp(name, sm->name, len) == 0) {
540 if (isalnum(sm->name[len]))
541 continue;
542 if (is_address)
543 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
544 else
545 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
546 } else {
547 continue;
549 callback(call, param, printed_name, sm);
550 } END_FOR_EACH_SM(sm);
551 free:
552 free_string(name);
555 static void match_call_info(struct expression *call)
557 struct member_info_callback *cb;
558 struct expression *arg;
559 struct stree *stree;
560 char *name;
561 int i;
563 name = get_fnptr_name(call->fn);
564 if (!name)
565 return;
567 FOR_EACH_PTR(member_callbacks, cb) {
568 stree = get_all_states_stree(cb->owner);
569 i = 0;
570 FOR_EACH_PTR(call->args, arg) {
571 print_struct_members(call, arg, i, stree, cb->callback);
572 i++;
573 } END_FOR_EACH_PTR(arg);
574 free_stree(&stree);
575 } END_FOR_EACH_PTR(cb);
577 free_string(name);
580 static int get_param(int param, char **name, struct symbol **sym)
582 struct symbol *arg;
583 int i;
585 i = 0;
586 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
588 * this is a temporary hack to work around a bug (I think in sparse?)
589 * 2.6.37-rc1:fs/reiserfs/journal.o
590 * If there is a function definition without parameter name found
591 * after a function implementation then it causes a crash.
592 * int foo() {}
593 * int bar(char *);
595 if (arg->ident->name < (char *)100)
596 continue;
597 if (i == param) {
598 *name = arg->ident->name;
599 *sym = arg;
600 return TRUE;
602 i++;
603 } END_FOR_EACH_PTR(arg);
605 return FALSE;
608 static struct stree *final_states;
609 static int prev_func_id = -1;
610 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
612 int func_id;
613 long type;
614 long param;
615 char *key;
616 char *value;
617 char *name = NULL;
618 struct symbol *sym = NULL;
619 struct def_callback *def_callback;
620 struct stree *stree;
622 if (argc != 5)
623 return 0;
625 func_id = atoi(argv[0]);
626 errno = 0;
627 type = strtol(argv[1], NULL, 10);
628 param = strtol(argv[2], NULL, 10);
629 if (errno)
630 return 0;
631 key = argv[3];
632 value = argv[4];
635 if (prev_func_id == -1)
636 prev_func_id = func_id;
637 if (func_id != prev_func_id) {
638 stree = __pop_fake_cur_stree();
639 merge_stree(&final_states, stree);
640 free_stree(&stree);
641 __push_fake_cur_stree();
642 __unnullify_path();
643 prev_func_id = func_id;
646 if (param >= 0 && !get_param(param, &name, &sym))
647 return 0;
649 FOR_EACH_PTR(callbacks, def_callback) {
650 if (def_callback->hook_type == type)
651 def_callback->callback(name, sym, key, value);
652 } END_FOR_EACH_PTR(def_callback);
654 return 0;
657 static void get_direct_callers(struct symbol *sym)
659 sql_select_caller_info("call_id, type, parameter, key, value", sym,
660 caller_info_callback);
663 static struct string_list *ptr_names_done;
664 static struct string_list *ptr_names;
666 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
668 insert_string(&ptr_names, alloc_string(argv[0]));
669 return 0;
672 static char *get_next_ptr_name(void)
674 char *ptr;
676 FOR_EACH_PTR(ptr_names, ptr) {
677 if (list_has_string(ptr_names_done, ptr))
678 continue;
679 insert_string(&ptr_names_done, ptr);
680 return ptr;
681 } END_FOR_EACH_PTR(ptr);
682 return NULL;
685 static void get_ptr_names(const char *file, const char *name)
687 char sql_filter[1024];
688 int before, after;
690 if (file) {
691 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
692 file, name);
693 } else {
694 snprintf(sql_filter, 1024, "function = '%s';", name);
697 before = ptr_list_size((struct ptr_list *)ptr_names);
699 run_sql(get_ptr_name, NULL,
700 "select distinct ptr from function_ptr where %s",
701 sql_filter);
703 after = ptr_list_size((struct ptr_list *)ptr_names);
704 if (before == after)
705 return;
707 while ((name = get_next_ptr_name()))
708 get_ptr_names(NULL, name);
711 static void match_data_from_db(struct symbol *sym)
713 struct sm_state *sm;
714 struct stree *stree;
716 if (!sym || !sym->ident)
717 return;
719 __push_fake_cur_stree();
720 __unnullify_path();
721 prev_func_id = -1;
723 if (!__inline_fn) {
724 char *ptr;
726 if (sym->ctype.modifiers & MOD_STATIC)
727 get_ptr_names(get_base_file(), sym->ident->name);
728 else
729 get_ptr_names(NULL, sym->ident->name);
731 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
732 __free_ptr_list((struct ptr_list **)&ptr_names);
733 __free_ptr_list((struct ptr_list **)&ptr_names_done);
734 stree = __pop_fake_cur_stree();
735 free_stree(&stree);
736 return;
739 get_direct_callers(sym);
741 FOR_EACH_PTR(ptr_names, ptr) {
742 run_sql(caller_info_callback, NULL,
743 "select call_id, type, parameter, key, value"
744 " from caller_info where function = '%s' order by call_id",
745 ptr);
746 free_string(ptr);
747 } END_FOR_EACH_PTR(ptr);
749 __free_ptr_list((struct ptr_list **)&ptr_names);
750 __free_ptr_list((struct ptr_list **)&ptr_names_done);
751 } else {
752 get_direct_callers(sym);
755 stree = __pop_fake_cur_stree();
756 merge_stree(&final_states, stree);
757 free_stree(&stree);
759 FOR_EACH_SM(final_states, sm) {
760 __set_sm(sm);
761 } END_FOR_EACH_SM(sm);
763 free_stree(&final_states);
766 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
768 struct expression *call_expr = _call;
769 struct call_implies_callback *cb;
770 struct expression *arg = NULL;
771 int type;
772 int param;
774 if (argc != 5)
775 return 0;
777 type = atoi(argv[1]);
778 param = atoi(argv[2]);
780 FOR_EACH_PTR(call_implies_cb_list, cb) {
781 if (cb->type != type)
782 continue;
783 if (param != -1) {
784 arg = get_argument_from_call_expr(call_expr->args, param);
785 if (!arg)
786 continue;
788 cb->callback(arg, argv[3], argv[4]);
789 } END_FOR_EACH_PTR(cb);
791 return 0;
794 static void match_call_implies(struct expression *expr)
796 sql_select_call_implies("function, type, parameter, key, value", expr,
797 call_implies_callbacks);
800 static void print_initializer_list(struct expression_list *expr_list,
801 struct symbol *struct_type)
803 struct expression *expr;
804 struct symbol *base_type;
805 char struct_name[256];
807 FOR_EACH_PTR(expr_list, expr) {
808 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
809 print_initializer_list(expr->idx_expression->expr_list, struct_type);
810 continue;
812 if (expr->type != EXPR_IDENTIFIER)
813 continue;
814 if (!expr->expr_ident)
815 continue;
816 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
817 continue;
818 base_type = get_type(expr->ident_expression);
819 if (!base_type || base_type->type != SYM_FN)
820 continue;
821 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
822 struct_type->ident->name, expr->expr_ident->name);
823 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
824 struct_name);
825 } END_FOR_EACH_PTR(expr);
828 static void global_variable(struct symbol *sym)
830 struct symbol *struct_type;
832 if (!sym->ident)
833 return;
834 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
835 return;
836 struct_type = get_base_type(sym);
837 if (!struct_type)
838 return;
839 if (struct_type->type == SYM_ARRAY) {
840 struct_type = get_base_type(struct_type);
841 if (!struct_type)
842 return;
844 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
845 return;
846 print_initializer_list(sym->initializer->expr_list, struct_type);
849 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
851 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
854 static void call_return_state_hooks_conditional(struct expression *expr)
856 struct returned_state_callback *cb;
857 struct range_list *rl;
858 char *return_ranges;
859 int final_pass_orig = final_pass;
861 __push_fake_cur_stree();
863 final_pass = 0;
864 __split_whole_condition(expr->conditional);
865 final_pass = final_pass_orig;
867 if (get_implied_rl(expr->cond_true, &rl))
868 rl = cast_rl(cur_func_return_type(), rl);
869 else
870 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
871 return_ranges = show_rl(rl);
872 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
874 return_id++;
875 FOR_EACH_PTR(returned_state_callbacks, cb) {
876 cb->callback(return_id, return_ranges, expr);
877 } END_FOR_EACH_PTR(cb);
879 __push_true_states();
880 __use_false_states();
882 if (get_implied_rl(expr->cond_false, &rl))
883 rl = cast_rl(cur_func_return_type(), rl);
884 else
885 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
886 return_ranges = show_rl(rl);
887 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
889 return_id++;
890 FOR_EACH_PTR(returned_state_callbacks, cb) {
891 cb->callback(return_id, return_ranges, expr);
892 } END_FOR_EACH_PTR(cb);
894 __merge_true_states();
895 __free_fake_cur_stree();
898 static void call_return_state_hooks_compare(struct expression *expr)
900 struct returned_state_callback *cb;
901 char *return_ranges;
902 int final_pass_orig = final_pass;
903 sval_t sval = { .type = &int_ctype };
904 sval_t ret;
906 if (!get_implied_value(expr, &ret))
907 ret.value = -1;
909 __push_fake_cur_stree();
911 final_pass = 0;
912 __split_whole_condition(expr);
913 final_pass = final_pass_orig;
915 if (ret.value != 0) {
916 return_ranges = alloc_sname("1");
917 sval.value = 1;
918 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
920 return_id++;
921 FOR_EACH_PTR(returned_state_callbacks, cb) {
922 cb->callback(return_id, return_ranges, expr);
923 } END_FOR_EACH_PTR(cb);
926 __push_true_states();
927 __use_false_states();
929 if (ret.value != 1) {
930 return_ranges = alloc_sname("0");
931 sval.value = 0;
932 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
934 return_id++;
935 FOR_EACH_PTR(returned_state_callbacks, cb) {
936 cb->callback(return_id, return_ranges, expr);
937 } END_FOR_EACH_PTR(cb);
940 __merge_true_states();
941 __free_fake_cur_stree();
944 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
946 struct sm_state *tmp;
948 FOR_EACH_PTR(slist, tmp) {
949 if (strcmp(tmp->state->name, sm->state->name) == 0)
950 return 1;
951 } END_FOR_EACH_PTR(tmp);
953 return 0;
956 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
958 struct returned_state_callback *cb;
959 struct range_list *rl;
960 char *return_ranges;
961 struct sm_state *tmp;
962 int ret = 0;
963 int nr_possible, nr_states;
964 char *compare_str = NULL;
965 char buf[128];
966 struct state_list *already_handled = NULL;
968 if (!sm || !sm->merged)
969 return 0;
971 if (too_many_possible(sm))
972 return 0;
974 /* bail if it gets too complicated */
975 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
976 nr_states = stree_count(__get_cur_stree());
977 if (option_info && nr_states * nr_possible >= 2000)
978 return 0;
980 FOR_EACH_PTR(sm->possible, tmp) {
981 if (tmp->merged)
982 continue;
983 if (ptr_in_list(tmp, already_handled))
984 continue;
985 add_ptr_list(&already_handled, tmp);
987 ret = 1;
988 __push_fake_cur_stree();
990 overwrite_states_using_pool(sm, tmp);
992 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
993 return_ranges = show_rl(rl);
994 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
995 compare_str = expr_lte_to_param(expr, -1);
996 if (compare_str) {
997 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
998 return_ranges = alloc_sname(buf);
1001 return_id++;
1002 FOR_EACH_PTR(returned_state_callbacks, cb) {
1003 cb->callback(return_id, return_ranges, expr);
1004 } END_FOR_EACH_PTR(cb);
1006 __free_fake_cur_stree();
1007 } END_FOR_EACH_PTR(tmp);
1009 free_slist(&already_handled);
1011 return ret;
1014 static int call_return_state_hooks_split_possible(struct expression *expr)
1016 struct sm_state *sm;
1018 if (!expr || expr_equal_to_param(expr, -1))
1019 return 0;
1021 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1022 return split_possible_helper(sm, expr);
1025 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1027 struct range_list *rl;
1028 char *return_ranges;
1029 sval_t sval;
1030 char *compare_str;
1031 char *math_str;
1032 char buf[128];
1034 *rl_p = NULL;
1036 if (!expr)
1037 return alloc_sname("");
1039 if (get_implied_value(expr, &sval)) {
1040 *rl_p = alloc_rl(sval, sval);
1041 return sval_to_str(sval);
1044 compare_str = expr_equal_to_param(expr, -1);
1045 math_str = get_value_in_terms_of_parameter_math(expr);
1047 if (get_implied_rl(expr, &rl)) {
1048 rl = cast_rl(cur_func_return_type(), rl);
1049 return_ranges = show_rl(rl);
1050 } else if (get_imaginary_absolute(expr, &rl)){
1051 rl = cast_rl(cur_func_return_type(), rl);
1052 return alloc_sname(show_rl(rl));
1053 } else {
1054 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1055 return_ranges = show_rl(rl);
1057 *rl_p = rl;
1059 if (compare_str) {
1060 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1061 return alloc_sname(buf);
1064 if (math_str) {
1065 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1066 return alloc_sname(buf);
1069 compare_str = expr_lte_to_param(expr, -1);
1070 if (compare_str) {
1071 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1072 return alloc_sname(buf);
1074 return return_ranges;
1077 static int split_positive_from_negative(struct expression *expr)
1079 struct returned_state_callback *cb;
1080 struct range_list *rl;
1081 const char *return_ranges;
1082 struct range_list *ret_rl;
1083 int undo;
1085 /* We're going to print the states 3 times */
1086 if (stree_count(__get_cur_stree()) > 10000 / 3)
1087 return 0;
1089 if (!get_implied_rl(expr, &rl) || !rl)
1090 return 0;
1091 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1092 return 0;
1093 /* Forget about INT_MAX and larger */
1094 if (rl_max(rl).value <= 0)
1095 return 0;
1096 if (!sval_is_negative(rl_min(rl)))
1097 return 0;
1099 if (!assume(compare_expression(expr, '>', zero_expr())))
1100 return 0;
1102 return_id++;
1103 return_ranges = get_return_ranges_str(expr, &ret_rl);
1104 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1105 FOR_EACH_PTR(returned_state_callbacks, cb) {
1106 cb->callback(return_id, (char *)return_ranges, expr);
1107 } END_FOR_EACH_PTR(cb);
1109 end_assume();
1111 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1112 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1114 return_id++;
1115 return_ranges = get_return_ranges_str(expr, &ret_rl);
1116 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1117 FOR_EACH_PTR(returned_state_callbacks, cb) {
1118 cb->callback(return_id, (char *)return_ranges, expr);
1119 } END_FOR_EACH_PTR(cb);
1121 if (undo)
1122 end_assume();
1125 undo = assume(compare_expression(expr, '<', zero_expr()));
1127 return_id++;
1128 return_ranges = get_return_ranges_str(expr, &ret_rl);
1129 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1130 FOR_EACH_PTR(returned_state_callbacks, cb) {
1131 cb->callback(return_id, (char *)return_ranges, expr);
1132 } END_FOR_EACH_PTR(cb);
1134 if (undo)
1135 end_assume();
1137 return 1;
1140 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1142 struct returned_state_callback *cb;
1143 struct range_list *rl;
1144 struct range_list *nonnull_rl;
1145 sval_t null_sval;
1146 struct range_list *null_rl = NULL;
1147 char *return_ranges;
1148 struct smatch_state *state;
1149 int nr_states;
1150 int final_pass_orig = final_pass;
1152 if (!expr || expr_equal_to_param(expr, -1))
1153 return 0;
1154 if (expr->type == EXPR_CALL)
1155 return 0;
1156 if (!is_pointer(expr))
1157 return 0;
1159 state = get_state_expr(SMATCH_EXTRA, expr);
1160 if (!state || !estate_rl(state))
1161 return 0;
1162 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1163 return 0;
1164 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1165 return 0;
1167 nr_states = stree_count(__get_cur_stree());
1168 if (option_info && nr_states >= 1500)
1169 return 0;
1171 rl = estate_rl(state);
1173 __push_fake_cur_stree();
1175 final_pass = 0;
1176 __split_whole_condition(expr);
1177 final_pass = final_pass_orig;
1179 nonnull_rl = rl_filter(rl, rl_zero());
1180 return_ranges = show_rl(nonnull_rl);
1181 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1183 return_id++;
1184 FOR_EACH_PTR(returned_state_callbacks, cb) {
1185 cb->callback(return_id, return_ranges, expr);
1186 } END_FOR_EACH_PTR(cb);
1188 __push_true_states();
1189 __use_false_states();
1191 return_ranges = alloc_sname("0");
1192 null_sval = sval_type_val(rl_type(rl), 0);
1193 add_range(&null_rl, null_sval, null_sval);
1194 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1195 return_id++;
1196 FOR_EACH_PTR(returned_state_callbacks, cb) {
1197 cb->callback(return_id, return_ranges, expr);
1198 } END_FOR_EACH_PTR(cb);
1200 __merge_true_states();
1201 __free_fake_cur_stree();
1203 return 1;
1206 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1208 struct range_list *rl;
1209 struct range_list *nonzero_rl;
1210 sval_t zero_sval;
1211 struct range_list *zero_rl = NULL;
1212 int nr_states;
1213 struct returned_state_callback *cb;
1214 char *return_ranges;
1215 int final_pass_orig = final_pass;
1216 sval_t val;
1218 if (option_project != PROJ_KERNEL)
1219 return 0;
1221 nr_states = stree_count(__get_cur_stree());
1222 if (nr_states > 1500)
1223 return 0;
1225 if (get_value(expr, &val))
1226 return 0;
1227 if (!get_implied_rl(expr, &rl))
1228 return 0;
1229 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1230 return 0;
1231 if (rl_max(rl).value != 0)
1232 return 0;
1234 __push_fake_cur_stree();
1236 final_pass = 0;
1237 __split_whole_condition(expr);
1238 final_pass = final_pass_orig;
1240 nonzero_rl = rl_filter(rl, rl_zero());
1241 return_ranges = show_rl(nonzero_rl);
1242 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1244 return_id++;
1245 FOR_EACH_PTR(returned_state_callbacks, cb) {
1246 cb->callback(return_id, return_ranges, expr);
1247 } END_FOR_EACH_PTR(cb);
1249 __push_true_states();
1250 __use_false_states();
1252 return_ranges = alloc_sname("0");
1253 zero_sval = sval_type_val(rl_type(rl), 0);
1254 add_range(&zero_rl, zero_sval, zero_sval);
1255 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1256 return_id++;
1257 FOR_EACH_PTR(returned_state_callbacks, cb) {
1258 cb->callback(return_id, return_ranges, expr);
1259 } END_FOR_EACH_PTR(cb);
1261 __merge_true_states();
1262 __free_fake_cur_stree();
1264 return 1;
1267 static int is_boolean(struct expression *expr)
1269 struct range_list *rl;
1271 if (!get_implied_rl(expr, &rl))
1272 return 0;
1273 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1274 return 1;
1275 return 0;
1278 static int is_conditional(struct expression *expr)
1280 if (!expr)
1281 return 0;
1282 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1283 return 1;
1284 return 0;
1287 static int splitable_function_call(struct expression *expr)
1289 struct sm_state *sm;
1290 char buf[64];
1292 if (!expr || expr->type != EXPR_CALL)
1293 return 0;
1294 snprintf(buf, sizeof(buf), "return %p", expr);
1295 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1296 return split_possible_helper(sm, expr);
1299 static struct sm_state *find_bool_param(void)
1301 struct stree *start_states;
1302 struct symbol *arg;
1303 struct sm_state *sm, *tmp;
1304 sval_t sval;
1306 start_states = get_start_states();
1308 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1309 if (!arg->ident)
1310 continue;
1311 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1312 if (!sm)
1313 continue;
1314 if (rl_min(estate_rl(sm->state)).value != 0 ||
1315 rl_max(estate_rl(sm->state)).value != 1)
1316 continue;
1317 goto found;
1318 } END_FOR_EACH_PTR_REVERSE(arg);
1320 return NULL;
1322 found:
1324 * Check if it's splitable. If not, then splitting it up is likely not
1325 * useful for the callers.
1327 FOR_EACH_PTR(sm->possible, tmp) {
1328 if (is_merged(tmp))
1329 continue;
1330 if (!estate_get_single_value(tmp->state, &sval))
1331 return NULL;
1332 } END_FOR_EACH_PTR(tmp);
1334 return sm;
1337 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1339 struct returned_state_callback *cb;
1340 struct range_list *ret_rl;
1341 const char *return_ranges;
1342 struct sm_state *tmp;
1343 int ret = 0;
1344 int nr_possible, nr_states;
1345 char *compare_str = NULL;
1346 char buf[128];
1347 struct state_list *already_handled = NULL;
1349 if (!sm || !sm->merged)
1350 return 0;
1352 if (too_many_possible(sm))
1353 return 0;
1355 /* bail if it gets too complicated */
1356 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1357 nr_states = stree_count(__get_cur_stree());
1358 if (option_info && nr_states * nr_possible >= 2000)
1359 return 0;
1361 FOR_EACH_PTR(sm->possible, tmp) {
1362 if (tmp->merged)
1363 continue;
1364 if (ptr_in_list(tmp, already_handled))
1365 continue;
1366 add_ptr_list(&already_handled, tmp);
1368 ret = 1;
1369 __push_fake_cur_stree();
1371 overwrite_states_using_pool(sm, tmp);
1373 return_ranges = get_return_ranges_str(expr, &ret_rl);
1374 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1375 compare_str = expr_lte_to_param(expr, -1);
1376 if (compare_str) {
1377 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1378 return_ranges = alloc_sname(buf);
1381 return_id++;
1382 FOR_EACH_PTR(returned_state_callbacks, cb) {
1383 cb->callback(return_id, (char *)return_ranges, expr);
1384 } END_FOR_EACH_PTR(cb);
1386 __free_fake_cur_stree();
1387 } END_FOR_EACH_PTR(tmp);
1389 free_slist(&already_handled);
1391 return ret;
1394 static int split_by_bool_param(struct expression *expr)
1396 struct sm_state *start_sm, *sm;
1397 sval_t sval;
1399 start_sm = find_bool_param();
1400 if (!start_sm)
1401 return 0;
1402 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1403 if (!sm || estate_get_single_value(sm->state, &sval))
1404 return 0;
1405 return split_on_bool_sm(sm, expr);
1408 static void call_return_state_hooks(struct expression *expr)
1410 struct returned_state_callback *cb;
1411 struct range_list *ret_rl;
1412 const char *return_ranges;
1413 int nr_states;
1414 sval_t sval;
1416 if (__path_is_null())
1417 return;
1419 expr = strip_expr(expr);
1421 if (is_impossible_path())
1422 goto vanilla;
1424 if (expr && (expr->type == EXPR_COMPARE ||
1425 !get_implied_value(expr, &sval)) &&
1426 (is_condition(expr) || is_boolean(expr))) {
1427 call_return_state_hooks_compare(expr);
1428 return;
1429 } else if (is_conditional(expr)) {
1430 call_return_state_hooks_conditional(expr);
1431 return;
1432 } else if (call_return_state_hooks_split_possible(expr)) {
1433 return;
1434 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1435 return;
1436 } else if (call_return_state_hooks_split_success_fail(expr)) {
1437 return;
1438 } else if (splitable_function_call(expr)) {
1439 return;
1440 } else if (split_positive_from_negative(expr)) {
1441 return;
1442 } else if (split_by_bool_param(expr)) {
1443 return;
1446 vanilla:
1447 return_ranges = get_return_ranges_str(expr, &ret_rl);
1448 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1450 return_id++;
1451 nr_states = stree_count(__get_cur_stree());
1452 if (nr_states >= 10000) {
1453 match_return_info(return_id, (char *)return_ranges, expr);
1454 return;
1456 FOR_EACH_PTR(returned_state_callbacks, cb) {
1457 cb->callback(return_id, (char *)return_ranges, expr);
1458 } END_FOR_EACH_PTR(cb);
1461 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1463 struct returned_member_callback *cb;
1464 struct stree *stree;
1465 struct sm_state *sm;
1466 struct symbol *type;
1467 char *name;
1468 char member_name[256];
1469 int len;
1471 type = get_type(expr);
1472 if (!type || type->type != SYM_PTR)
1473 return;
1474 name = expr_to_var(expr);
1475 if (!name)
1476 return;
1478 member_name[sizeof(member_name) - 1] = '\0';
1479 strcpy(member_name, "$");
1481 len = strlen(name);
1482 FOR_EACH_PTR(returned_member_callbacks, cb) {
1483 stree = __get_cur_stree();
1484 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1485 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1486 strcpy(member_name, "*$");
1487 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1488 continue;
1490 if (strncmp(sm->name, name, len) != 0)
1491 continue;
1492 if (strncmp(sm->name + len, "->", 2) != 0)
1493 continue;
1494 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1495 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1496 } END_FOR_EACH_SM(sm);
1497 } END_FOR_EACH_PTR(cb);
1499 free_string(name);
1502 static void reset_memdb(struct symbol *sym)
1504 mem_sql(NULL, NULL, "delete from caller_info;");
1505 mem_sql(NULL, NULL, "delete from return_states;");
1506 mem_sql(NULL, NULL, "delete from call_implies;");
1509 static void match_end_func_info(struct symbol *sym)
1511 if (__path_is_null())
1512 return;
1513 call_return_state_hooks(NULL);
1516 static void match_after_func(struct symbol *sym)
1518 if (!__inline_fn)
1519 reset_memdb(sym);
1522 static void init_memdb(void)
1524 char *err = NULL;
1525 int rc;
1526 const char *schema_files[] = {
1527 "db/db.schema",
1528 "db/caller_info.schema",
1529 "db/return_states.schema",
1530 "db/function_type_size.schema",
1531 "db/type_size.schema",
1532 "db/call_implies.schema",
1533 "db/function_ptr.schema",
1534 "db/local_values.schema",
1535 "db/function_type_value.schema",
1536 "db/type_value.schema",
1537 "db/function_type_info.schema",
1538 "db/data_info.schema",
1540 static char buf[4096];
1541 int fd;
1542 int ret;
1543 int i;
1545 rc = sqlite3_open(":memory:", &mem_db);
1546 if (rc != SQLITE_OK) {
1547 printf("Error starting In-Memory database.");
1548 return;
1551 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1552 fd = open_data_file(schema_files[i]);
1553 if (fd < 0) {
1554 mem_db = NULL;
1555 return;
1557 ret = read(fd, buf, sizeof(buf));
1558 if (ret == sizeof(buf)) {
1559 printf("Schema file too large: %s (limit %zd bytes)",
1560 schema_files[i], sizeof(buf));
1562 buf[ret] = '\0';
1563 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1564 if (rc != SQLITE_OK) {
1565 fprintf(stderr, "SQL error #2: %s\n", err);
1566 fprintf(stderr, "%s\n", buf);
1571 void open_smatch_db(void)
1573 int rc;
1575 if (option_no_db)
1576 return;
1578 init_memdb();
1580 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1581 if (rc != SQLITE_OK) {
1582 option_no_db = 1;
1583 return;
1585 return;
1588 static void register_common_funcs(void)
1590 struct token *token;
1591 char *func;
1592 char filename[256];
1594 if (option_project == PROJ_NONE)
1595 strcpy(filename, "common_functions");
1596 else
1597 snprintf(filename, 256, "%s.common_functions", option_project_str);
1599 token = get_tokens_file(filename);
1600 if (!token)
1601 return;
1602 if (token_type(token) != TOKEN_STREAMBEGIN)
1603 return;
1604 token = token->next;
1605 while (token_type(token) != TOKEN_STREAMEND) {
1606 if (token_type(token) != TOKEN_IDENT)
1607 return;
1608 func = alloc_string(show_ident(token->ident));
1609 add_ptr_list(&common_funcs, func);
1610 token = token->next;
1612 clear_token_alloc();
1616 void register_definition_db_callbacks(int id)
1618 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1619 add_hook(&global_variable, BASE_HOOK);
1620 add_hook(&global_variable, DECLARATION_HOOK);
1621 add_split_return_callback(match_return_info);
1622 add_split_return_callback(print_returned_struct_members);
1623 add_hook(&call_return_state_hooks, RETURN_HOOK);
1624 add_hook(&match_end_func_info, END_FUNC_HOOK);
1625 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1627 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1628 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1630 register_common_funcs();
1633 void register_db_call_marker(int id)
1635 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1638 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1640 struct expression *arg;
1641 char *name = NULL;
1642 char member_name[256];
1644 *sym = NULL;
1646 if (param == -1) {
1647 const char *star = "";
1649 if (expr->type != EXPR_ASSIGNMENT)
1650 return NULL;
1651 name = expr_to_var_sym(expr->left, sym);
1652 if (!name)
1653 return NULL;
1654 if (key[0] == '*') {
1655 star = "*";
1656 key++;
1658 if (strncmp(key, "$", 1) != 0)
1659 return name;
1660 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1661 free_string(name);
1662 return alloc_string(member_name);
1665 while (expr->type == EXPR_ASSIGNMENT)
1666 expr = strip_expr(expr->right);
1667 if (expr->type != EXPR_CALL)
1668 return NULL;
1670 arg = get_argument_from_call_expr(expr->args, param);
1671 if (!arg)
1672 return NULL;
1674 return get_variable_from_key(arg, key, sym);
1677 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1679 char buf[256];
1680 char *tmp;
1682 if (!arg)
1683 return NULL;
1685 arg = strip_expr(arg);
1687 if (strcmp(key, "$") == 0)
1688 return expr_to_var_sym(arg, sym);
1690 if (strcmp(key, "*$") == 0) {
1691 if (arg->type == EXPR_PREOP && arg->op == '&') {
1692 arg = strip_expr(arg->unop);
1693 return expr_to_var_sym(arg, sym);
1694 } else {
1695 tmp = expr_to_var_sym(arg, sym);
1696 if (!tmp)
1697 return NULL;
1698 snprintf(buf, sizeof(buf), "*%s", tmp);
1699 free_string(tmp);
1700 return alloc_string(buf);
1704 if (arg->type == EXPR_PREOP && arg->op == '&') {
1705 arg = strip_expr(arg->unop);
1706 tmp = expr_to_var_sym(arg, sym);
1707 if (!tmp)
1708 return NULL;
1709 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1710 return alloc_string(buf);
1713 tmp = expr_to_var_sym(arg, sym);
1714 if (!tmp)
1715 return NULL;
1716 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1717 free_string(tmp);
1718 return alloc_string(buf);
1721 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1723 *vsl = NULL;
1725 if (strcmp("$", key) == 0)
1726 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1727 return get_variable_from_key(arg, key, sym);
1730 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1732 char *param_name;
1733 int name_len;
1734 static char buf[256];
1736 if (!sym || !sym->ident)
1737 return NULL;
1739 param_name = sym->ident->name;
1740 name_len = strlen(param_name);
1742 if (strcmp(name, param_name) == 0) {
1743 return "$";
1744 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1745 strncmp(name, param_name, name_len) == 0) {
1746 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1747 return buf;
1748 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1749 return "*$";
1751 return NULL;
1754 const char *get_param_name(struct sm_state *sm)
1756 return get_param_name_var_sym(sm->name, sm->sym);
1759 char *get_data_info_name(struct expression *expr)
1761 struct symbol *sym;
1762 char *name;
1763 char buf[256];
1764 char *ret = NULL;
1766 expr = strip_expr(expr);
1767 name = get_member_name(expr);
1768 if (name)
1769 return name;
1770 name = expr_to_var_sym(expr, &sym);
1771 if (!name || !sym)
1772 goto free;
1773 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1774 goto free;
1775 if (sym->ctype.modifiers & MOD_STATIC)
1776 snprintf(buf, sizeof(buf), "static %s", name);
1777 else
1778 snprintf(buf, sizeof(buf), "global %s", name);
1779 ret = alloc_sname(buf);
1780 free:
1781 free_string(name);
1782 return ret;