user_data2: improve handling when we return user data ranges
[smatch.git] / smatch_db.c
blobb6d0bde316943a6627abab7ec170605a9c1e927e
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 get_row_count(void *_row_count, int argc, char **argv, char **azColName)
288 int *row_count = _row_count;
290 *row_count = 0;
291 if (argc != 1)
292 return 0;
293 *row_count = atoi(argv[0]);
294 return 0;
297 static void sql_select_return_states_pointer(const char *cols,
298 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
300 char *ptr;
302 ptr = get_fnptr_name(call->fn);
303 if (!ptr)
304 return;
306 run_sql(callback, info,
307 "select %s from return_states join function_ptr where "
308 "return_states.function == function_ptr.function and ptr = '%s' "
309 "and searchable = 1 "
310 "order by function_ptr.file, return_states.file, return_id, type;",
311 cols, ptr);
314 static int is_local_symbol(struct expression *expr)
316 if (expr->type != EXPR_SYMBOL)
317 return 0;
318 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
319 return 0;
320 return 1;
323 void sql_select_return_states(const char *cols, struct expression *call,
324 int (*callback)(void*, int, char**, char**), void *info)
326 int row_count;
328 if (is_fake_call(call))
329 return;
331 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
332 sql_select_return_states_pointer(cols, call, callback, info);
333 return;
336 if (inlinable(call->fn)) {
337 mem_sql(callback, info,
338 "select %s from return_states where call_id = '%lu' order by return_id, type;",
339 cols, (unsigned long)call);
340 return;
343 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
344 get_static_filter(call->fn->symbol));
345 if (row_count > 3000)
346 return;
348 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
349 cols, get_static_filter(call->fn->symbol));
352 void sql_select_call_implies(const char *cols, struct expression *call,
353 int (*callback)(void*, int, char**, char**))
355 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
356 return;
358 if (inlinable(call->fn)) {
359 mem_sql(callback, call,
360 "select %s from call_implies where call_id = '%lu';",
361 cols, (unsigned long)call);
362 return;
365 run_sql(callback, call, "select %s from call_implies where %s;",
366 cols, get_static_filter(call->fn->symbol));
369 void sql_select_caller_info(const char *cols, struct symbol *sym,
370 int (*callback)(void*, int, char**, char**))
372 if (__inline_fn) {
373 mem_sql(callback, NULL,
374 "select %s from caller_info where call_id = %lu;",
375 cols, (unsigned long)__inline_fn);
376 return;
379 run_sql(callback, NULL,
380 "select %s from caller_info where %s order by call_id;",
381 cols, get_static_filter(sym));
384 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
386 struct def_callback *def_callback = __alloc_def_callback(0);
388 def_callback->hook_type = type;
389 def_callback->callback = callback;
390 add_ptr_list(&callbacks, def_callback);
394 * These call backs are used when the --info option is turned on to print struct
395 * member information. For example foo->bar could have a state in
396 * smatch_extra.c and also check_user.c.
398 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
400 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
402 member_callback->owner = owner;
403 member_callback->callback = callback;
404 add_ptr_list(&member_callbacks, member_callback);
407 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
409 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
411 callback->callback = fn;
412 add_ptr_list(&returned_state_callbacks, callback);
415 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))
417 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
419 member_callback->owner = owner;
420 member_callback->callback = callback;
421 add_ptr_list(&returned_member_callbacks, member_callback);
424 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
426 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
428 cb->type = type;
429 cb->callback = callback;
430 add_ptr_list(&call_implies_cb_list, cb);
433 struct return_info {
434 struct expression *static_returns_call;
435 struct symbol *return_type;
436 struct range_list *return_range_list;
439 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
441 struct return_info *ret_info = _ret_info;
442 struct range_list *rl;
443 struct expression *call_expr = ret_info->static_returns_call;
445 if (argc != 1)
446 return 0;
447 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
448 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
449 return 0;
452 struct range_list *db_return_vals(struct expression *expr)
454 struct return_info ret_info = {};
455 char buf[64];
456 struct sm_state *sm;
458 if (is_fake_call(expr))
459 return NULL;
461 snprintf(buf, sizeof(buf), "return %p", expr);
462 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
463 if (sm)
464 return clone_rl(estate_rl(sm->state));
465 ret_info.static_returns_call = expr;
466 ret_info.return_type = get_type(expr);
467 if (!ret_info.return_type)
468 return NULL;
470 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
471 return NULL;
473 ret_info.return_range_list = NULL;
474 if (inlinable(expr->fn)) {
475 mem_sql(db_return_callback, &ret_info,
476 "select distinct return from return_states where call_id = '%lu';",
477 (unsigned long)expr);
478 } else {
479 run_sql(db_return_callback, &ret_info,
480 "select distinct return from return_states where %s;",
481 get_static_filter(expr->fn->symbol));
483 return ret_info.return_range_list;
486 struct range_list *db_return_vals_from_str(const char *fn_name)
488 struct return_info ret_info;
490 ret_info.static_returns_call = NULL;
491 ret_info.return_type = &llong_ctype;
492 ret_info.return_range_list = NULL;
494 run_sql(db_return_callback, &ret_info,
495 "select distinct return from return_states where function = '%s';",
496 fn_name);
497 return ret_info.return_range_list;
500 static void match_call_marker(struct expression *expr)
503 * we just want to record something in the database so that if we have
504 * two calls like: frob(4); frob(some_unkown); then on the receiving
505 * side we know that sometimes frob is called with unknown parameters.
508 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
511 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
512 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
514 struct sm_state *sm;
515 char *name;
516 struct symbol *sym;
517 int len;
518 char printed_name[256];
519 int is_address = 0;
521 expr = strip_expr(expr);
522 if (expr->type == EXPR_PREOP && expr->op == '&') {
523 expr = strip_expr(expr->unop);
524 is_address = 1;
527 name = expr_to_var_sym(expr, &sym);
528 if (!name || !sym)
529 goto free;
531 len = strlen(name);
532 FOR_EACH_SM(stree, sm) {
533 if (sm->sym != sym)
534 continue;
535 if (strcmp(name, sm->name) == 0) {
536 if (is_address)
537 snprintf(printed_name, sizeof(printed_name), "*$");
538 else /* these are already handled. fixme: handle them here */
539 continue;
540 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
541 snprintf(printed_name, sizeof(printed_name), "*$");
542 } else if (strncmp(name, sm->name, len) == 0) {
543 if (isalnum(sm->name[len]))
544 continue;
545 if (is_address)
546 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
547 else
548 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
549 } else {
550 continue;
552 callback(call, param, printed_name, sm);
553 } END_FOR_EACH_SM(sm);
554 free:
555 free_string(name);
558 static void match_call_info(struct expression *call)
560 struct member_info_callback *cb;
561 struct expression *arg;
562 struct stree *stree;
563 char *name;
564 int i;
566 name = get_fnptr_name(call->fn);
567 if (!name)
568 return;
570 FOR_EACH_PTR(member_callbacks, cb) {
571 stree = get_all_states_stree(cb->owner);
572 i = 0;
573 FOR_EACH_PTR(call->args, arg) {
574 print_struct_members(call, arg, i, stree, cb->callback);
575 i++;
576 } END_FOR_EACH_PTR(arg);
577 free_stree(&stree);
578 } END_FOR_EACH_PTR(cb);
580 free_string(name);
583 static int get_param(int param, char **name, struct symbol **sym)
585 struct symbol *arg;
586 int i;
588 i = 0;
589 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
591 * this is a temporary hack to work around a bug (I think in sparse?)
592 * 2.6.37-rc1:fs/reiserfs/journal.o
593 * If there is a function definition without parameter name found
594 * after a function implementation then it causes a crash.
595 * int foo() {}
596 * int bar(char *);
598 if (arg->ident->name < (char *)100)
599 continue;
600 if (i == param) {
601 *name = arg->ident->name;
602 *sym = arg;
603 return TRUE;
605 i++;
606 } END_FOR_EACH_PTR(arg);
608 return FALSE;
611 static struct stree *final_states;
612 static int prev_func_id = -1;
613 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
615 int func_id;
616 long type;
617 long param;
618 char *key;
619 char *value;
620 char *name = NULL;
621 struct symbol *sym = NULL;
622 struct def_callback *def_callback;
623 struct stree *stree;
625 if (argc != 5)
626 return 0;
628 func_id = atoi(argv[0]);
629 errno = 0;
630 type = strtol(argv[1], NULL, 10);
631 param = strtol(argv[2], NULL, 10);
632 if (errno)
633 return 0;
634 key = argv[3];
635 value = argv[4];
638 if (prev_func_id == -1)
639 prev_func_id = func_id;
640 if (func_id != prev_func_id) {
641 stree = __pop_fake_cur_stree();
642 merge_stree(&final_states, stree);
643 free_stree(&stree);
644 __push_fake_cur_stree();
645 __unnullify_path();
646 prev_func_id = func_id;
649 if (param >= 0 && !get_param(param, &name, &sym))
650 return 0;
652 FOR_EACH_PTR(callbacks, def_callback) {
653 if (def_callback->hook_type == type)
654 def_callback->callback(name, sym, key, value);
655 } END_FOR_EACH_PTR(def_callback);
657 return 0;
660 static void get_direct_callers(struct symbol *sym)
662 sql_select_caller_info("call_id, type, parameter, key, value", sym,
663 caller_info_callback);
666 static struct string_list *ptr_names_done;
667 static struct string_list *ptr_names;
669 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
671 insert_string(&ptr_names, alloc_string(argv[0]));
672 return 0;
675 static char *get_next_ptr_name(void)
677 char *ptr;
679 FOR_EACH_PTR(ptr_names, ptr) {
680 if (list_has_string(ptr_names_done, ptr))
681 continue;
682 insert_string(&ptr_names_done, ptr);
683 return ptr;
684 } END_FOR_EACH_PTR(ptr);
685 return NULL;
688 static void get_ptr_names(const char *file, const char *name)
690 char sql_filter[1024];
691 int before, after;
693 if (file) {
694 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
695 file, name);
696 } else {
697 snprintf(sql_filter, 1024, "function = '%s';", name);
700 before = ptr_list_size((struct ptr_list *)ptr_names);
702 run_sql(get_ptr_name, NULL,
703 "select distinct ptr from function_ptr where %s",
704 sql_filter);
706 after = ptr_list_size((struct ptr_list *)ptr_names);
707 if (before == after)
708 return;
710 while ((name = get_next_ptr_name()))
711 get_ptr_names(NULL, name);
714 static void match_data_from_db(struct symbol *sym)
716 struct sm_state *sm;
717 struct stree *stree;
719 if (!sym || !sym->ident)
720 return;
722 __push_fake_cur_stree();
723 __unnullify_path();
724 prev_func_id = -1;
726 if (!__inline_fn) {
727 char *ptr;
729 if (sym->ctype.modifiers & MOD_STATIC)
730 get_ptr_names(get_base_file(), sym->ident->name);
731 else
732 get_ptr_names(NULL, sym->ident->name);
734 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
735 __free_ptr_list((struct ptr_list **)&ptr_names);
736 __free_ptr_list((struct ptr_list **)&ptr_names_done);
737 stree = __pop_fake_cur_stree();
738 free_stree(&stree);
739 return;
742 get_direct_callers(sym);
744 FOR_EACH_PTR(ptr_names, ptr) {
745 run_sql(caller_info_callback, NULL,
746 "select call_id, type, parameter, key, value"
747 " from caller_info where function = '%s' order by call_id",
748 ptr);
749 free_string(ptr);
750 } END_FOR_EACH_PTR(ptr);
752 __free_ptr_list((struct ptr_list **)&ptr_names);
753 __free_ptr_list((struct ptr_list **)&ptr_names_done);
754 } else {
755 get_direct_callers(sym);
758 stree = __pop_fake_cur_stree();
759 merge_stree(&final_states, stree);
760 free_stree(&stree);
762 FOR_EACH_SM(final_states, sm) {
763 __set_sm(sm);
764 } END_FOR_EACH_SM(sm);
766 free_stree(&final_states);
769 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
771 struct expression *call_expr = _call;
772 struct call_implies_callback *cb;
773 struct expression *arg = NULL;
774 int type;
775 int param;
777 if (argc != 5)
778 return 0;
780 type = atoi(argv[1]);
781 param = atoi(argv[2]);
783 FOR_EACH_PTR(call_implies_cb_list, cb) {
784 if (cb->type != type)
785 continue;
786 if (param != -1) {
787 arg = get_argument_from_call_expr(call_expr->args, param);
788 if (!arg)
789 continue;
791 cb->callback(arg, argv[3], argv[4]);
792 } END_FOR_EACH_PTR(cb);
794 return 0;
797 static void match_call_implies(struct expression *expr)
799 sql_select_call_implies("function, type, parameter, key, value", expr,
800 call_implies_callbacks);
803 static void print_initializer_list(struct expression_list *expr_list,
804 struct symbol *struct_type)
806 struct expression *expr;
807 struct symbol *base_type;
808 char struct_name[256];
810 FOR_EACH_PTR(expr_list, expr) {
811 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
812 print_initializer_list(expr->idx_expression->expr_list, struct_type);
813 continue;
815 if (expr->type != EXPR_IDENTIFIER)
816 continue;
817 if (!expr->expr_ident)
818 continue;
819 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
820 continue;
821 base_type = get_type(expr->ident_expression);
822 if (!base_type || base_type->type != SYM_FN)
823 continue;
824 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
825 struct_type->ident->name, expr->expr_ident->name);
826 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
827 struct_name);
828 } END_FOR_EACH_PTR(expr);
831 static void global_variable(struct symbol *sym)
833 struct symbol *struct_type;
835 if (!sym->ident)
836 return;
837 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
838 return;
839 struct_type = get_base_type(sym);
840 if (!struct_type)
841 return;
842 if (struct_type->type == SYM_ARRAY) {
843 struct_type = get_base_type(struct_type);
844 if (!struct_type)
845 return;
847 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
848 return;
849 print_initializer_list(sym->initializer->expr_list, struct_type);
852 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
854 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", "");
857 static void call_return_state_hooks_conditional(struct expression *expr)
859 struct returned_state_callback *cb;
860 struct range_list *rl;
861 char *return_ranges;
862 int final_pass_orig = final_pass;
864 __push_fake_cur_stree();
866 final_pass = 0;
867 __split_whole_condition(expr->conditional);
868 final_pass = final_pass_orig;
870 if (get_implied_rl(expr->cond_true, &rl))
871 rl = cast_rl(cur_func_return_type(), rl);
872 else
873 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
874 return_ranges = show_rl(rl);
875 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
877 return_id++;
878 FOR_EACH_PTR(returned_state_callbacks, cb) {
879 cb->callback(return_id, return_ranges, expr);
880 } END_FOR_EACH_PTR(cb);
882 __push_true_states();
883 __use_false_states();
885 if (get_implied_rl(expr->cond_false, &rl))
886 rl = cast_rl(cur_func_return_type(), rl);
887 else
888 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
889 return_ranges = show_rl(rl);
890 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
892 return_id++;
893 FOR_EACH_PTR(returned_state_callbacks, cb) {
894 cb->callback(return_id, return_ranges, expr);
895 } END_FOR_EACH_PTR(cb);
897 __merge_true_states();
898 __free_fake_cur_stree();
901 static void call_return_state_hooks_compare(struct expression *expr)
903 struct returned_state_callback *cb;
904 char *return_ranges;
905 int final_pass_orig = final_pass;
906 sval_t sval = { .type = &int_ctype };
907 sval_t ret;
909 if (!get_implied_value(expr, &ret))
910 ret.value = -1;
912 __push_fake_cur_stree();
914 final_pass = 0;
915 __split_whole_condition(expr);
916 final_pass = final_pass_orig;
918 if (ret.value != 0) {
919 return_ranges = alloc_sname("1");
920 sval.value = 1;
921 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
923 return_id++;
924 FOR_EACH_PTR(returned_state_callbacks, cb) {
925 cb->callback(return_id, return_ranges, expr);
926 } END_FOR_EACH_PTR(cb);
929 __push_true_states();
930 __use_false_states();
932 if (ret.value != 1) {
933 return_ranges = alloc_sname("0");
934 sval.value = 0;
935 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
937 return_id++;
938 FOR_EACH_PTR(returned_state_callbacks, cb) {
939 cb->callback(return_id, return_ranges, expr);
940 } END_FOR_EACH_PTR(cb);
943 __merge_true_states();
944 __free_fake_cur_stree();
947 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
949 struct sm_state *tmp;
951 FOR_EACH_PTR(slist, tmp) {
952 if (strcmp(tmp->state->name, sm->state->name) == 0)
953 return 1;
954 } END_FOR_EACH_PTR(tmp);
956 return 0;
959 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
961 struct returned_state_callback *cb;
962 struct range_list *rl;
963 char *return_ranges;
964 struct sm_state *tmp;
965 int ret = 0;
966 int nr_possible, nr_states;
967 char *compare_str = NULL;
968 char buf[128];
969 struct state_list *already_handled = NULL;
971 if (!sm || !sm->merged)
972 return 0;
974 if (too_many_possible(sm))
975 return 0;
977 /* bail if it gets too complicated */
978 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
979 nr_states = stree_count(__get_cur_stree());
980 if (option_info && nr_states * nr_possible >= 2000)
981 return 0;
983 FOR_EACH_PTR(sm->possible, tmp) {
984 if (tmp->merged)
985 continue;
986 if (ptr_in_list(tmp, already_handled))
987 continue;
988 add_ptr_list(&already_handled, tmp);
990 ret = 1;
991 __push_fake_cur_stree();
993 overwrite_states_using_pool(sm, tmp);
995 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
996 return_ranges = show_rl(rl);
997 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
998 compare_str = expr_lte_to_param(expr, -1);
999 if (compare_str) {
1000 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1001 return_ranges = alloc_sname(buf);
1004 return_id++;
1005 FOR_EACH_PTR(returned_state_callbacks, cb) {
1006 cb->callback(return_id, return_ranges, expr);
1007 } END_FOR_EACH_PTR(cb);
1009 __free_fake_cur_stree();
1010 } END_FOR_EACH_PTR(tmp);
1012 free_slist(&already_handled);
1014 return ret;
1017 static int call_return_state_hooks_split_possible(struct expression *expr)
1019 struct sm_state *sm;
1021 if (!expr || expr_equal_to_param(expr, -1))
1022 return 0;
1024 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1025 return split_possible_helper(sm, expr);
1028 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1030 struct range_list *rl;
1031 char *return_ranges;
1032 sval_t sval;
1033 char *compare_str;
1034 char *math_str;
1035 char buf[128];
1037 *rl_p = NULL;
1039 if (!expr)
1040 return alloc_sname("");
1042 if (get_implied_value(expr, &sval)) {
1043 *rl_p = alloc_rl(sval, sval);
1044 return sval_to_str(sval);
1047 compare_str = expr_equal_to_param(expr, -1);
1048 math_str = get_value_in_terms_of_parameter_math(expr);
1050 if (get_implied_rl(expr, &rl)) {
1051 rl = cast_rl(cur_func_return_type(), rl);
1052 return_ranges = show_rl(rl);
1053 } else if (get_imaginary_absolute(expr, &rl)){
1054 rl = cast_rl(cur_func_return_type(), rl);
1055 return alloc_sname(show_rl(rl));
1056 } else {
1057 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1058 return_ranges = show_rl(rl);
1060 *rl_p = rl;
1062 if (compare_str) {
1063 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1064 return alloc_sname(buf);
1067 if (math_str) {
1068 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1069 return alloc_sname(buf);
1072 compare_str = expr_lte_to_param(expr, -1);
1073 if (compare_str) {
1074 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1075 return alloc_sname(buf);
1077 return return_ranges;
1080 static int split_positive_from_negative(struct expression *expr)
1082 struct returned_state_callback *cb;
1083 struct range_list *rl;
1084 const char *return_ranges;
1085 struct range_list *ret_rl;
1086 int undo;
1088 /* We're going to print the states 3 times */
1089 if (stree_count(__get_cur_stree()) > 10000 / 3)
1090 return 0;
1092 if (!get_implied_rl(expr, &rl) || !rl)
1093 return 0;
1094 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1095 return 0;
1096 /* Forget about INT_MAX and larger */
1097 if (rl_max(rl).value <= 0)
1098 return 0;
1099 if (!sval_is_negative(rl_min(rl)))
1100 return 0;
1102 if (!assume(compare_expression(expr, '>', zero_expr())))
1103 return 0;
1105 return_id++;
1106 return_ranges = get_return_ranges_str(expr, &ret_rl);
1107 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1108 FOR_EACH_PTR(returned_state_callbacks, cb) {
1109 cb->callback(return_id, (char *)return_ranges, expr);
1110 } END_FOR_EACH_PTR(cb);
1112 end_assume();
1114 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1115 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1117 return_id++;
1118 return_ranges = get_return_ranges_str(expr, &ret_rl);
1119 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1120 FOR_EACH_PTR(returned_state_callbacks, cb) {
1121 cb->callback(return_id, (char *)return_ranges, expr);
1122 } END_FOR_EACH_PTR(cb);
1124 if (undo)
1125 end_assume();
1128 undo = assume(compare_expression(expr, '<', zero_expr()));
1130 return_id++;
1131 return_ranges = get_return_ranges_str(expr, &ret_rl);
1132 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1133 FOR_EACH_PTR(returned_state_callbacks, cb) {
1134 cb->callback(return_id, (char *)return_ranges, expr);
1135 } END_FOR_EACH_PTR(cb);
1137 if (undo)
1138 end_assume();
1140 return 1;
1143 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1145 struct returned_state_callback *cb;
1146 struct range_list *rl;
1147 struct range_list *nonnull_rl;
1148 sval_t null_sval;
1149 struct range_list *null_rl = NULL;
1150 char *return_ranges;
1151 struct smatch_state *state;
1152 int nr_states;
1153 int final_pass_orig = final_pass;
1155 if (!expr || expr_equal_to_param(expr, -1))
1156 return 0;
1157 if (expr->type == EXPR_CALL)
1158 return 0;
1159 if (!is_pointer(expr))
1160 return 0;
1162 state = get_state_expr(SMATCH_EXTRA, expr);
1163 if (!state || !estate_rl(state))
1164 return 0;
1165 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1166 return 0;
1167 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1168 return 0;
1170 nr_states = stree_count(__get_cur_stree());
1171 if (option_info && nr_states >= 1500)
1172 return 0;
1174 rl = estate_rl(state);
1176 __push_fake_cur_stree();
1178 final_pass = 0;
1179 __split_whole_condition(expr);
1180 final_pass = final_pass_orig;
1182 nonnull_rl = rl_filter(rl, rl_zero());
1183 return_ranges = show_rl(nonnull_rl);
1184 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1186 return_id++;
1187 FOR_EACH_PTR(returned_state_callbacks, cb) {
1188 cb->callback(return_id, return_ranges, expr);
1189 } END_FOR_EACH_PTR(cb);
1191 __push_true_states();
1192 __use_false_states();
1194 return_ranges = alloc_sname("0");
1195 null_sval = sval_type_val(rl_type(rl), 0);
1196 add_range(&null_rl, null_sval, null_sval);
1197 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1198 return_id++;
1199 FOR_EACH_PTR(returned_state_callbacks, cb) {
1200 cb->callback(return_id, return_ranges, expr);
1201 } END_FOR_EACH_PTR(cb);
1203 __merge_true_states();
1204 __free_fake_cur_stree();
1206 return 1;
1209 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1211 struct range_list *rl;
1212 struct range_list *nonzero_rl;
1213 sval_t zero_sval;
1214 struct range_list *zero_rl = NULL;
1215 int nr_states;
1216 struct returned_state_callback *cb;
1217 char *return_ranges;
1218 int final_pass_orig = final_pass;
1219 sval_t val;
1221 if (option_project != PROJ_KERNEL)
1222 return 0;
1224 nr_states = stree_count(__get_cur_stree());
1225 if (nr_states > 1500)
1226 return 0;
1228 if (get_value(expr, &val))
1229 return 0;
1230 if (!get_implied_rl(expr, &rl))
1231 return 0;
1232 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1233 return 0;
1234 if (rl_max(rl).value != 0)
1235 return 0;
1237 __push_fake_cur_stree();
1239 final_pass = 0;
1240 __split_whole_condition(expr);
1241 final_pass = final_pass_orig;
1243 nonzero_rl = rl_filter(rl, rl_zero());
1244 return_ranges = show_rl(nonzero_rl);
1245 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1247 return_id++;
1248 FOR_EACH_PTR(returned_state_callbacks, cb) {
1249 cb->callback(return_id, return_ranges, expr);
1250 } END_FOR_EACH_PTR(cb);
1252 __push_true_states();
1253 __use_false_states();
1255 return_ranges = alloc_sname("0");
1256 zero_sval = sval_type_val(rl_type(rl), 0);
1257 add_range(&zero_rl, zero_sval, zero_sval);
1258 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1259 return_id++;
1260 FOR_EACH_PTR(returned_state_callbacks, cb) {
1261 cb->callback(return_id, return_ranges, expr);
1262 } END_FOR_EACH_PTR(cb);
1264 __merge_true_states();
1265 __free_fake_cur_stree();
1267 return 1;
1270 static int is_boolean(struct expression *expr)
1272 struct range_list *rl;
1274 if (!get_implied_rl(expr, &rl))
1275 return 0;
1276 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1277 return 1;
1278 return 0;
1281 static int is_conditional(struct expression *expr)
1283 if (!expr)
1284 return 0;
1285 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1286 return 1;
1287 return 0;
1290 static int splitable_function_call(struct expression *expr)
1292 struct sm_state *sm;
1293 char buf[64];
1295 if (!expr || expr->type != EXPR_CALL)
1296 return 0;
1297 snprintf(buf, sizeof(buf), "return %p", expr);
1298 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1299 return split_possible_helper(sm, expr);
1302 static struct sm_state *find_bool_param(void)
1304 struct stree *start_states;
1305 struct symbol *arg;
1306 struct sm_state *sm, *tmp;
1307 sval_t sval;
1309 start_states = get_start_states();
1311 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1312 if (!arg->ident)
1313 continue;
1314 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1315 if (!sm)
1316 continue;
1317 if (rl_min(estate_rl(sm->state)).value != 0 ||
1318 rl_max(estate_rl(sm->state)).value != 1)
1319 continue;
1320 goto found;
1321 } END_FOR_EACH_PTR_REVERSE(arg);
1323 return NULL;
1325 found:
1327 * Check if it's splitable. If not, then splitting it up is likely not
1328 * useful for the callers.
1330 FOR_EACH_PTR(sm->possible, tmp) {
1331 if (is_merged(tmp))
1332 continue;
1333 if (!estate_get_single_value(tmp->state, &sval))
1334 return NULL;
1335 } END_FOR_EACH_PTR(tmp);
1337 return sm;
1340 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1342 struct returned_state_callback *cb;
1343 struct range_list *ret_rl;
1344 const char *return_ranges;
1345 struct sm_state *tmp;
1346 int ret = 0;
1347 int nr_possible, nr_states;
1348 char *compare_str = NULL;
1349 char buf[128];
1350 struct state_list *already_handled = NULL;
1352 if (!sm || !sm->merged)
1353 return 0;
1355 if (too_many_possible(sm))
1356 return 0;
1358 /* bail if it gets too complicated */
1359 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1360 nr_states = stree_count(__get_cur_stree());
1361 if (option_info && nr_states * nr_possible >= 2000)
1362 return 0;
1364 FOR_EACH_PTR(sm->possible, tmp) {
1365 if (tmp->merged)
1366 continue;
1367 if (ptr_in_list(tmp, already_handled))
1368 continue;
1369 add_ptr_list(&already_handled, tmp);
1371 ret = 1;
1372 __push_fake_cur_stree();
1374 overwrite_states_using_pool(sm, tmp);
1376 return_ranges = get_return_ranges_str(expr, &ret_rl);
1377 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1378 compare_str = expr_lte_to_param(expr, -1);
1379 if (compare_str) {
1380 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1381 return_ranges = alloc_sname(buf);
1384 return_id++;
1385 FOR_EACH_PTR(returned_state_callbacks, cb) {
1386 cb->callback(return_id, (char *)return_ranges, expr);
1387 } END_FOR_EACH_PTR(cb);
1389 __free_fake_cur_stree();
1390 } END_FOR_EACH_PTR(tmp);
1392 free_slist(&already_handled);
1394 return ret;
1397 static int split_by_bool_param(struct expression *expr)
1399 struct sm_state *start_sm, *sm;
1400 sval_t sval;
1402 start_sm = find_bool_param();
1403 if (!start_sm)
1404 return 0;
1405 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1406 if (!sm || estate_get_single_value(sm->state, &sval))
1407 return 0;
1408 return split_on_bool_sm(sm, expr);
1411 static void call_return_state_hooks(struct expression *expr)
1413 struct returned_state_callback *cb;
1414 struct range_list *ret_rl;
1415 const char *return_ranges;
1416 int nr_states;
1417 sval_t sval;
1419 if (__path_is_null())
1420 return;
1422 expr = strip_expr(expr);
1424 if (is_impossible_path())
1425 goto vanilla;
1427 if (expr && (expr->type == EXPR_COMPARE ||
1428 !get_implied_value(expr, &sval)) &&
1429 (is_condition(expr) || is_boolean(expr))) {
1430 call_return_state_hooks_compare(expr);
1431 return;
1432 } else if (is_conditional(expr)) {
1433 call_return_state_hooks_conditional(expr);
1434 return;
1435 } else if (call_return_state_hooks_split_possible(expr)) {
1436 return;
1437 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1438 return;
1439 } else if (call_return_state_hooks_split_success_fail(expr)) {
1440 return;
1441 } else if (splitable_function_call(expr)) {
1442 return;
1443 } else if (split_positive_from_negative(expr)) {
1444 return;
1445 } else if (split_by_bool_param(expr)) {
1446 return;
1449 vanilla:
1450 return_ranges = get_return_ranges_str(expr, &ret_rl);
1451 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1453 return_id++;
1454 nr_states = stree_count(__get_cur_stree());
1455 if (nr_states >= 10000) {
1456 match_return_info(return_id, (char *)return_ranges, expr);
1457 return;
1459 FOR_EACH_PTR(returned_state_callbacks, cb) {
1460 cb->callback(return_id, (char *)return_ranges, expr);
1461 } END_FOR_EACH_PTR(cb);
1464 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1466 struct returned_member_callback *cb;
1467 struct stree *stree;
1468 struct sm_state *sm;
1469 struct symbol *type;
1470 char *name;
1471 char member_name[256];
1472 int len;
1474 type = get_type(expr);
1475 if (!type || type->type != SYM_PTR)
1476 return;
1477 name = expr_to_var(expr);
1478 if (!name)
1479 return;
1481 member_name[sizeof(member_name) - 1] = '\0';
1482 strcpy(member_name, "$");
1484 len = strlen(name);
1485 FOR_EACH_PTR(returned_member_callbacks, cb) {
1486 stree = __get_cur_stree();
1487 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1488 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1489 strcpy(member_name, "*$");
1490 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1491 continue;
1493 if (strncmp(sm->name, name, len) != 0)
1494 continue;
1495 if (strncmp(sm->name + len, "->", 2) != 0)
1496 continue;
1497 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1498 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1499 } END_FOR_EACH_SM(sm);
1500 } END_FOR_EACH_PTR(cb);
1502 free_string(name);
1505 static void reset_memdb(struct symbol *sym)
1507 mem_sql(NULL, NULL, "delete from caller_info;");
1508 mem_sql(NULL, NULL, "delete from return_states;");
1509 mem_sql(NULL, NULL, "delete from call_implies;");
1512 static void match_end_func_info(struct symbol *sym)
1514 if (__path_is_null())
1515 return;
1516 call_return_state_hooks(NULL);
1519 static void match_after_func(struct symbol *sym)
1521 if (!__inline_fn)
1522 reset_memdb(sym);
1525 static void init_memdb(void)
1527 char *err = NULL;
1528 int rc;
1529 const char *schema_files[] = {
1530 "db/db.schema",
1531 "db/caller_info.schema",
1532 "db/return_states.schema",
1533 "db/function_type_size.schema",
1534 "db/type_size.schema",
1535 "db/call_implies.schema",
1536 "db/function_ptr.schema",
1537 "db/local_values.schema",
1538 "db/function_type_value.schema",
1539 "db/type_value.schema",
1540 "db/function_type_info.schema",
1541 "db/data_info.schema",
1543 static char buf[4096];
1544 int fd;
1545 int ret;
1546 int i;
1548 rc = sqlite3_open(":memory:", &mem_db);
1549 if (rc != SQLITE_OK) {
1550 printf("Error starting In-Memory database.");
1551 return;
1554 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1555 fd = open_data_file(schema_files[i]);
1556 if (fd < 0) {
1557 mem_db = NULL;
1558 return;
1560 ret = read(fd, buf, sizeof(buf));
1561 if (ret == sizeof(buf)) {
1562 printf("Schema file too large: %s (limit %zd bytes)",
1563 schema_files[i], sizeof(buf));
1565 buf[ret] = '\0';
1566 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1567 if (rc != SQLITE_OK) {
1568 fprintf(stderr, "SQL error #2: %s\n", err);
1569 fprintf(stderr, "%s\n", buf);
1574 void open_smatch_db(void)
1576 int rc;
1578 if (option_no_db)
1579 return;
1581 init_memdb();
1583 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1584 if (rc != SQLITE_OK) {
1585 option_no_db = 1;
1586 return;
1588 return;
1591 static void register_common_funcs(void)
1593 struct token *token;
1594 char *func;
1595 char filename[256];
1597 if (option_project == PROJ_NONE)
1598 strcpy(filename, "common_functions");
1599 else
1600 snprintf(filename, 256, "%s.common_functions", option_project_str);
1602 token = get_tokens_file(filename);
1603 if (!token)
1604 return;
1605 if (token_type(token) != TOKEN_STREAMBEGIN)
1606 return;
1607 token = token->next;
1608 while (token_type(token) != TOKEN_STREAMEND) {
1609 if (token_type(token) != TOKEN_IDENT)
1610 return;
1611 func = alloc_string(show_ident(token->ident));
1612 add_ptr_list(&common_funcs, func);
1613 token = token->next;
1615 clear_token_alloc();
1619 void register_definition_db_callbacks(int id)
1621 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1622 add_hook(&global_variable, BASE_HOOK);
1623 add_hook(&global_variable, DECLARATION_HOOK);
1624 add_split_return_callback(match_return_info);
1625 add_split_return_callback(print_returned_struct_members);
1626 add_hook(&call_return_state_hooks, RETURN_HOOK);
1627 add_hook(&match_end_func_info, END_FUNC_HOOK);
1628 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1630 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1631 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1633 register_common_funcs();
1636 void register_db_call_marker(int id)
1638 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1641 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1643 struct expression *arg;
1644 char *name = NULL;
1645 char member_name[256];
1647 *sym = NULL;
1649 if (param == -1) {
1650 const char *star = "";
1652 if (expr->type != EXPR_ASSIGNMENT)
1653 return NULL;
1654 name = expr_to_var_sym(expr->left, sym);
1655 if (!name)
1656 return NULL;
1657 if (key[0] == '*') {
1658 star = "*";
1659 key++;
1661 if (strncmp(key, "$", 1) != 0)
1662 return name;
1663 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1664 free_string(name);
1665 return alloc_string(member_name);
1668 while (expr->type == EXPR_ASSIGNMENT)
1669 expr = strip_expr(expr->right);
1670 if (expr->type != EXPR_CALL)
1671 return NULL;
1673 arg = get_argument_from_call_expr(expr->args, param);
1674 if (!arg)
1675 return NULL;
1677 return get_variable_from_key(arg, key, sym);
1680 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1682 char buf[256];
1683 char *tmp;
1685 if (!arg)
1686 return NULL;
1688 arg = strip_expr(arg);
1690 if (strcmp(key, "$") == 0)
1691 return expr_to_var_sym(arg, sym);
1693 if (strcmp(key, "*$") == 0) {
1694 if (arg->type == EXPR_PREOP && arg->op == '&') {
1695 arg = strip_expr(arg->unop);
1696 return expr_to_var_sym(arg, sym);
1697 } else {
1698 tmp = expr_to_var_sym(arg, sym);
1699 if (!tmp)
1700 return NULL;
1701 snprintf(buf, sizeof(buf), "*%s", tmp);
1702 free_string(tmp);
1703 return alloc_string(buf);
1707 if (arg->type == EXPR_PREOP && arg->op == '&') {
1708 arg = strip_expr(arg->unop);
1709 tmp = expr_to_var_sym(arg, sym);
1710 if (!tmp)
1711 return NULL;
1712 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1713 return alloc_string(buf);
1716 tmp = expr_to_var_sym(arg, sym);
1717 if (!tmp)
1718 return NULL;
1719 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1720 free_string(tmp);
1721 return alloc_string(buf);
1724 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1726 *vsl = NULL;
1728 if (strcmp("$", key) == 0)
1729 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1730 return get_variable_from_key(arg, key, sym);
1733 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1735 char *param_name;
1736 int name_len;
1737 static char buf[256];
1739 if (!sym || !sym->ident)
1740 return NULL;
1742 param_name = sym->ident->name;
1743 name_len = strlen(param_name);
1745 if (strcmp(name, param_name) == 0) {
1746 return "$";
1747 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1748 strncmp(name, param_name, name_len) == 0) {
1749 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1750 return buf;
1751 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1752 return "*$";
1754 return NULL;
1757 const char *get_param_name(struct sm_state *sm)
1759 return get_param_name_var_sym(sm->name, sm->sym);
1762 char *get_data_info_name(struct expression *expr)
1764 struct symbol *sym;
1765 char *name;
1766 char buf[256];
1767 char *ret = NULL;
1769 expr = strip_expr(expr);
1770 name = get_member_name(expr);
1771 if (name)
1772 return name;
1773 name = expr_to_var_sym(expr, &sym);
1774 if (!name || !sym)
1775 goto free;
1776 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1777 goto free;
1778 if (sym->ctype.modifiers & MOD_STATIC)
1779 snprintf(buf, sizeof(buf), "static %s", name);
1780 else
1781 snprintf(buf, sizeof(buf), "global %s", name);
1782 ret = alloc_sname(buf);
1783 free:
1784 free_string(name);
1785 return ret;