flow: fix some parent transition fallout
[smatch.git] / smatch_db.c
blobca24b7bf833acb2a889d32c18dfff202871ea7dc
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <string.h>
19 #include <errno.h>
20 #include <sqlite3.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static sqlite3 *db;
28 static sqlite3 *mem_db;
30 static int return_id;
32 #define sql_insert(table, values...) \
33 do { \
34 if (!mem_db) \
35 break; \
36 if (__inline_fn) { \
37 char buf[1024]; \
38 char *err, *p = buf; \
39 int rc; \
41 p += snprintf(p, buf + sizeof(buf) - p, \
42 "insert into %s values (", #table); \
43 p += snprintf(p, buf + sizeof(buf) - p, values); \
44 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
45 sm_debug("in-mem: %s\n", buf); \
46 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err); \
47 if (rc != SQLITE_OK) { \
48 fprintf(stderr, "SQL error #2: %s\n", err); \
49 fprintf(stderr, "SQL: '%s'\n", buf); \
50 parse_error = 1; \
51 } \
52 break; \
53 } \
54 if (option_info) { \
55 sm_prefix(); \
56 sm_printf("SQL: insert into " #table " values (" values); \
57 sm_printf(");\n"); \
58 } \
59 } while (0)
61 struct def_callback {
62 int hook_type;
63 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
65 ALLOCATOR(def_callback, "definition db hook callbacks");
66 DECLARE_PTR_LIST(callback_list, struct def_callback);
67 static struct callback_list *callbacks;
69 struct member_info_callback {
70 int owner;
71 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
73 ALLOCATOR(member_info_callback, "caller_info callbacks");
74 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
75 static struct member_info_cb_list *member_callbacks;
77 struct returned_state_callback {
78 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
80 ALLOCATOR(returned_state_callback, "returned state callbacks");
81 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
82 static struct returned_state_cb_list *returned_state_callbacks;
84 struct returned_member_callback {
85 int owner;
86 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
88 ALLOCATOR(returned_member_callback, "returned member callbacks");
89 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
90 static struct returned_member_cb_list *returned_member_callbacks;
92 struct call_implies_callback {
93 int type;
94 void (*callback)(struct expression *arg, char *key, char *value);
96 ALLOCATOR(call_implies_callback, "call_implies callbacks");
97 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
98 static struct call_implies_cb_list *call_implies_cb_list;
100 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
102 int i;
104 for (i = 0; i < argc; i++) {
105 if (i != 0)
106 printf(", ");
107 sm_printf("%s", argv[i]);
109 sm_printf("\n");
110 return 0;
113 void debug_sql(const char *sql)
115 if (!option_debug)
116 return;
117 sm_msg("%s", sql);
118 sql_exec(print_sql_output, NULL, sql);
121 void debug_mem_sql(const char *sql)
123 if (!option_debug)
124 return;
125 sm_msg("%s", sql);
126 sql_mem_exec(print_sql_output, NULL, sql);
129 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
131 char *err = NULL;
132 int rc;
134 if (option_no_db || !db)
135 return;
137 rc = sqlite3_exec(db, sql, callback, data, &err);
138 if (rc != SQLITE_OK && !parse_error) {
139 fprintf(stderr, "SQL error #2: %s\n", err);
140 fprintf(stderr, "SQL: '%s'\n", sql);
141 parse_error = 1;
145 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql)
147 char *err = NULL;
148 int rc;
150 if (!mem_db)
151 return;
153 rc = sqlite3_exec(mem_db, sql, callback, data, &err);
154 if (rc != SQLITE_OK) {
155 fprintf(stderr, "SQL error #2: %s\n", err);
156 fprintf(stderr, "SQL: '%s'\n", sql);
157 parse_error = 1;
161 void sql_insert_return_states(int return_id, const char *return_ranges,
162 int type, int param, const char *key, const char *value)
164 if (key && strlen(key) >= 80)
165 return;
166 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
167 get_base_file(), get_function(), (unsigned long)__inline_fn,
168 return_id, return_ranges, fn_static(), type, param, key, value);
171 static struct string_list *common_funcs;
172 static int is_common_function(const char *fn)
174 char *tmp;
176 if (strncmp(fn, "__builtin_", 10) == 0)
177 return 1;
179 FOR_EACH_PTR(common_funcs, tmp) {
180 if (strcmp(tmp, fn) == 0)
181 return 1;
182 } END_FOR_EACH_PTR(tmp);
184 return 0;
187 void sql_insert_caller_info(struct expression *call, int type,
188 int param, const char *key, const char *value)
190 char *fn;
192 if (!option_info && !__inline_call)
193 return;
195 if (key && strlen(key) >= 80)
196 return;
198 fn = get_fnptr_name(call->fn);
199 if (!fn)
200 return;
202 if (__inline_call) {
203 mem_sql(NULL, NULL,
204 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
205 get_base_file(), get_function(), fn, (unsigned long)call,
206 is_static(call->fn), type, param, key, value);
209 if (!option_info)
210 return;
212 if (is_common_function(fn))
213 return;
215 sm_msg("SQL_caller_info: insert into caller_info values ("
216 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
217 get_base_file(), get_function(), fn, is_static(call->fn),
218 type, param, key, value);
220 free_string(fn);
223 void sql_insert_function_ptr(const char *fn, const char *struct_name)
225 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
226 struct_name);
229 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
231 sql_insert(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', %s", get_base_file(),
232 get_function(), (unsigned long)__inline_fn, fn_static(),
233 type, param, key, value);
236 void sql_insert_function_type_size(const char *member, const char *ranges)
238 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
241 void sql_insert_local_values(const char *name, const char *value)
243 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
246 void sql_insert_function_type_value(const char *type, const char *value)
248 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
251 void sql_insert_function_type(int param, const char *value)
253 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
254 get_base_file(), get_function(), fn_static(), param, value);
257 void sql_insert_parameter_name(int param, const char *value)
259 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
260 get_base_file(), get_function(), fn_static(), param, value);
263 void sql_insert_data_info(struct expression *data, int type, const char *value)
265 char *data_name;
267 data_name = get_data_info_name(data);
268 if (!data_name)
269 return;
270 sql_insert(data_info, "'%s', '%s', %d, '%s'",
271 is_static(data) ? get_base_file() : "extern",
272 data_name, type, value);
275 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
277 sql_insert(data_info, "'%s', '%s', %d, '%s'",
278 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
279 var, type, value);
282 char *get_static_filter(struct symbol *sym)
284 static char sql_filter[1024];
286 /* This can only happen on buggy code. Return invalid SQL. */
287 if (!sym) {
288 sql_filter[0] = '\0';
289 return sql_filter;
292 if (sym->ctype.modifiers & MOD_STATIC) {
293 snprintf(sql_filter, sizeof(sql_filter),
294 "file = '%s' and function = '%s' and static = '1'",
295 get_base_file(), sym->ident->name);
296 } else {
297 snprintf(sql_filter, sizeof(sql_filter),
298 "function = '%s' and static = '0'", sym->ident->name);
301 return sql_filter;
304 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
306 int *row_count = _row_count;
308 *row_count = 0;
309 if (argc != 1)
310 return 0;
311 *row_count = atoi(argv[0]);
312 return 0;
315 static void sql_select_return_states_pointer(const char *cols,
316 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
318 char *ptr;
319 int return_count;
321 ptr = get_fnptr_name(call->fn);
322 if (!ptr)
323 return;
325 run_sql(get_row_count, &return_count,
326 "select count(*) from return_states join function_ptr "
327 "where return_states.function == function_ptr.function and "
328 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
329 /* The magic number 100 is just from testing on the kernel. */
330 if (return_count > 100)
331 return;
333 run_sql(callback, info,
334 "select %s from return_states join function_ptr where "
335 "return_states.function == function_ptr.function and ptr = '%s' "
336 "and searchable = 1 "
337 "order by function_ptr.file, return_states.file, return_id, type;",
338 cols, ptr);
341 static int is_local_symbol(struct expression *expr)
343 if (expr->type != EXPR_SYMBOL)
344 return 0;
345 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
346 return 0;
347 return 1;
350 void sql_select_return_states(const char *cols, struct expression *call,
351 int (*callback)(void*, int, char**, char**), void *info)
353 int row_count;
355 if (is_fake_call(call))
356 return;
358 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
359 sql_select_return_states_pointer(cols, call, callback, info);
360 return;
363 if (inlinable(call->fn)) {
364 mem_sql(callback, info,
365 "select %s from return_states where call_id = '%lu' order by return_id, type;",
366 cols, (unsigned long)call);
367 return;
370 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
371 get_static_filter(call->fn->symbol));
372 if (row_count > 3000)
373 return;
375 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
376 cols, get_static_filter(call->fn->symbol));
379 void sql_select_call_implies(const char *cols, struct expression *call,
380 int (*callback)(void*, int, char**, char**))
382 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
383 return;
385 if (inlinable(call->fn)) {
386 mem_sql(callback, call,
387 "select %s from call_implies where call_id = '%lu';",
388 cols, (unsigned long)call);
389 return;
392 run_sql(callback, call, "select %s from call_implies where %s;",
393 cols, get_static_filter(call->fn->symbol));
396 void sql_select_caller_info(const char *cols, struct symbol *sym,
397 int (*callback)(void*, int, char**, char**))
399 if (__inline_fn) {
400 mem_sql(callback, NULL,
401 "select %s from caller_info where call_id = %lu;",
402 cols, (unsigned long)__inline_fn);
403 return;
406 run_sql(callback, NULL,
407 "select %s from caller_info where %s order by call_id;",
408 cols, get_static_filter(sym));
411 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
413 struct def_callback *def_callback = __alloc_def_callback(0);
415 def_callback->hook_type = type;
416 def_callback->callback = callback;
417 add_ptr_list(&callbacks, def_callback);
421 * These call backs are used when the --info option is turned on to print struct
422 * member information. For example foo->bar could have a state in
423 * smatch_extra.c and also check_user.c.
425 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
427 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
429 member_callback->owner = owner;
430 member_callback->callback = callback;
431 add_ptr_list(&member_callbacks, member_callback);
434 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
436 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
438 callback->callback = fn;
439 add_ptr_list(&returned_state_callbacks, callback);
442 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))
444 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
446 member_callback->owner = owner;
447 member_callback->callback = callback;
448 add_ptr_list(&returned_member_callbacks, member_callback);
451 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value))
453 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
455 cb->type = type;
456 cb->callback = callback;
457 add_ptr_list(&call_implies_cb_list, cb);
460 struct return_info {
461 struct expression *static_returns_call;
462 struct symbol *return_type;
463 struct range_list *return_range_list;
466 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
468 struct return_info *ret_info = _ret_info;
469 struct range_list *rl;
470 struct expression *call_expr = ret_info->static_returns_call;
472 if (argc != 1)
473 return 0;
474 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
475 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
476 return 0;
479 struct range_list *db_return_vals(struct expression *expr)
481 struct return_info ret_info = {};
482 char buf[64];
483 struct sm_state *sm;
485 if (is_fake_call(expr))
486 return NULL;
488 snprintf(buf, sizeof(buf), "return %p", expr);
489 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
490 if (sm)
491 return clone_rl(estate_rl(sm->state));
492 ret_info.static_returns_call = expr;
493 ret_info.return_type = get_type(expr);
494 if (!ret_info.return_type)
495 return NULL;
497 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
498 return NULL;
500 ret_info.return_range_list = NULL;
501 if (inlinable(expr->fn)) {
502 mem_sql(db_return_callback, &ret_info,
503 "select distinct return from return_states where call_id = '%lu';",
504 (unsigned long)expr);
505 } else {
506 run_sql(db_return_callback, &ret_info,
507 "select distinct return from return_states where %s;",
508 get_static_filter(expr->fn->symbol));
510 return ret_info.return_range_list;
513 struct range_list *db_return_vals_from_str(const char *fn_name)
515 struct return_info ret_info;
517 ret_info.static_returns_call = NULL;
518 ret_info.return_type = &llong_ctype;
519 ret_info.return_range_list = NULL;
521 run_sql(db_return_callback, &ret_info,
522 "select distinct return from return_states where function = '%s';",
523 fn_name);
524 return ret_info.return_range_list;
527 static void match_call_marker(struct expression *expr)
530 * we just want to record something in the database so that if we have
531 * two calls like: frob(4); frob(some_unkown); then on the receiving
532 * side we know that sometimes frob is called with unknown parameters.
535 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", "");
538 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
539 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
541 struct sm_state *sm;
542 char *name;
543 struct symbol *sym;
544 int len;
545 char printed_name[256];
546 int is_address = 0;
548 expr = strip_expr(expr);
549 if (expr->type == EXPR_PREOP && expr->op == '&') {
550 expr = strip_expr(expr->unop);
551 is_address = 1;
554 name = expr_to_var_sym(expr, &sym);
555 if (!name || !sym)
556 goto free;
558 len = strlen(name);
559 FOR_EACH_SM(stree, sm) {
560 if (sm->sym != sym)
561 continue;
562 if (strcmp(name, sm->name) == 0) {
563 if (is_address)
564 snprintf(printed_name, sizeof(printed_name), "*$");
565 else /* these are already handled. fixme: handle them here */
566 continue;
567 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
568 snprintf(printed_name, sizeof(printed_name), "*$");
569 } else if (strncmp(name, sm->name, len) == 0) {
570 if (isalnum(sm->name[len]))
571 continue;
572 if (is_address)
573 snprintf(printed_name, sizeof(printed_name), "$->%s", sm->name + len + 1);
574 else
575 snprintf(printed_name, sizeof(printed_name), "$%s", sm->name + len);
576 } else {
577 continue;
579 callback(call, param, printed_name, sm);
580 } END_FOR_EACH_SM(sm);
581 free:
582 free_string(name);
585 static void match_call_info(struct expression *call)
587 struct member_info_callback *cb;
588 struct expression *arg;
589 struct stree *stree;
590 char *name;
591 int i;
593 name = get_fnptr_name(call->fn);
594 if (!name)
595 return;
597 FOR_EACH_PTR(member_callbacks, cb) {
598 stree = get_all_states_stree(cb->owner);
599 i = 0;
600 FOR_EACH_PTR(call->args, arg) {
601 print_struct_members(call, arg, i, stree, cb->callback);
602 i++;
603 } END_FOR_EACH_PTR(arg);
604 free_stree(&stree);
605 } END_FOR_EACH_PTR(cb);
607 free_string(name);
610 static int get_param(int param, char **name, struct symbol **sym)
612 struct symbol *arg;
613 int i;
615 i = 0;
616 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
618 * this is a temporary hack to work around a bug (I think in sparse?)
619 * 2.6.37-rc1:fs/reiserfs/journal.o
620 * If there is a function definition without parameter name found
621 * after a function implementation then it causes a crash.
622 * int foo() {}
623 * int bar(char *);
625 if (arg->ident->name < (char *)100)
626 continue;
627 if (i == param) {
628 *name = arg->ident->name;
629 *sym = arg;
630 return TRUE;
632 i++;
633 } END_FOR_EACH_PTR(arg);
635 return FALSE;
638 static struct stree *final_states;
639 static int prev_func_id = -1;
640 static int caller_info_callback(void *unused, int argc, char **argv, char **azColName)
642 int func_id;
643 long type;
644 long param;
645 char *key;
646 char *value;
647 char *name = NULL;
648 struct symbol *sym = NULL;
649 struct def_callback *def_callback;
650 struct stree *stree;
652 if (argc != 5)
653 return 0;
655 func_id = atoi(argv[0]);
656 errno = 0;
657 type = strtol(argv[1], NULL, 10);
658 param = strtol(argv[2], NULL, 10);
659 if (errno)
660 return 0;
661 key = argv[3];
662 value = argv[4];
665 if (prev_func_id == -1)
666 prev_func_id = func_id;
667 if (func_id != prev_func_id) {
668 stree = __pop_fake_cur_stree();
669 merge_stree(&final_states, stree);
670 free_stree(&stree);
671 __push_fake_cur_stree();
672 __unnullify_path();
673 prev_func_id = func_id;
676 if (param >= 0 && !get_param(param, &name, &sym))
677 return 0;
679 FOR_EACH_PTR(callbacks, def_callback) {
680 if (def_callback->hook_type == type)
681 def_callback->callback(name, sym, key, value);
682 } END_FOR_EACH_PTR(def_callback);
684 return 0;
687 static void get_direct_callers(struct symbol *sym)
689 sql_select_caller_info("call_id, type, parameter, key, value", sym,
690 caller_info_callback);
693 static struct string_list *ptr_names_done;
694 static struct string_list *ptr_names;
696 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
698 insert_string(&ptr_names, alloc_string(argv[0]));
699 return 0;
702 static char *get_next_ptr_name(void)
704 char *ptr;
706 FOR_EACH_PTR(ptr_names, ptr) {
707 if (list_has_string(ptr_names_done, ptr))
708 continue;
709 insert_string(&ptr_names_done, ptr);
710 return ptr;
711 } END_FOR_EACH_PTR(ptr);
712 return NULL;
715 static void get_ptr_names(const char *file, const char *name)
717 char sql_filter[1024];
718 int before, after;
720 if (file) {
721 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
722 file, name);
723 } else {
724 snprintf(sql_filter, 1024, "function = '%s';", name);
727 before = ptr_list_size((struct ptr_list *)ptr_names);
729 run_sql(get_ptr_name, NULL,
730 "select distinct ptr from function_ptr where %s",
731 sql_filter);
733 after = ptr_list_size((struct ptr_list *)ptr_names);
734 if (before == after)
735 return;
737 while ((name = get_next_ptr_name()))
738 get_ptr_names(NULL, name);
741 static void match_data_from_db(struct symbol *sym)
743 struct sm_state *sm;
744 struct stree *stree;
746 if (!sym || !sym->ident)
747 return;
749 __push_fake_cur_stree();
750 __unnullify_path();
751 prev_func_id = -1;
753 if (!__inline_fn) {
754 char *ptr;
756 if (sym->ctype.modifiers & MOD_STATIC)
757 get_ptr_names(get_base_file(), sym->ident->name);
758 else
759 get_ptr_names(NULL, sym->ident->name);
761 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
762 __free_ptr_list((struct ptr_list **)&ptr_names);
763 __free_ptr_list((struct ptr_list **)&ptr_names_done);
764 stree = __pop_fake_cur_stree();
765 free_stree(&stree);
766 return;
769 get_direct_callers(sym);
771 FOR_EACH_PTR(ptr_names, ptr) {
772 run_sql(caller_info_callback, NULL,
773 "select call_id, type, parameter, key, value"
774 " from caller_info where function = '%s' order by call_id",
775 ptr);
776 free_string(ptr);
777 } END_FOR_EACH_PTR(ptr);
779 __free_ptr_list((struct ptr_list **)&ptr_names);
780 __free_ptr_list((struct ptr_list **)&ptr_names_done);
781 } else {
782 get_direct_callers(sym);
785 stree = __pop_fake_cur_stree();
786 merge_stree(&final_states, stree);
787 free_stree(&stree);
789 FOR_EACH_SM(final_states, sm) {
790 __set_sm(sm);
791 } END_FOR_EACH_SM(sm);
793 free_stree(&final_states);
796 static int call_implies_callbacks(void *_call, int argc, char **argv, char **azColName)
798 struct expression *call_expr = _call;
799 struct call_implies_callback *cb;
800 struct expression *arg = NULL;
801 int type;
802 int param;
804 if (argc != 5)
805 return 0;
807 type = atoi(argv[1]);
808 param = atoi(argv[2]);
810 FOR_EACH_PTR(call_implies_cb_list, cb) {
811 if (cb->type != type)
812 continue;
813 if (param != -1) {
814 arg = get_argument_from_call_expr(call_expr->args, param);
815 if (!arg)
816 continue;
818 cb->callback(arg, argv[3], argv[4]);
819 } END_FOR_EACH_PTR(cb);
821 return 0;
824 static void match_call_implies(struct expression *expr)
826 sql_select_call_implies("function, type, parameter, key, value", expr,
827 call_implies_callbacks);
830 static void print_initializer_list(struct expression_list *expr_list,
831 struct symbol *struct_type)
833 struct expression *expr;
834 struct symbol *base_type;
835 char struct_name[256];
837 FOR_EACH_PTR(expr_list, expr) {
838 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
839 print_initializer_list(expr->idx_expression->expr_list, struct_type);
840 continue;
842 if (expr->type != EXPR_IDENTIFIER)
843 continue;
844 if (!expr->expr_ident)
845 continue;
846 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
847 continue;
848 base_type = get_type(expr->ident_expression);
849 if (!base_type || base_type->type != SYM_FN)
850 continue;
851 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
852 struct_type->ident->name, expr->expr_ident->name);
853 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
854 struct_name);
855 } END_FOR_EACH_PTR(expr);
858 static void global_variable(struct symbol *sym)
860 struct symbol *struct_type;
862 if (!sym->ident)
863 return;
864 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
865 return;
866 struct_type = get_base_type(sym);
867 if (!struct_type)
868 return;
869 if (struct_type->type == SYM_ARRAY) {
870 struct_type = get_base_type(struct_type);
871 if (!struct_type)
872 return;
874 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
875 return;
876 print_initializer_list(sym->initializer->expr_list, struct_type);
879 static char *function_signature(void)
881 return type_to_str(get_real_base_type(cur_func_sym));
884 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
886 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
889 static void call_return_state_hooks_conditional(struct expression *expr)
891 struct returned_state_callback *cb;
892 struct range_list *rl;
893 char *return_ranges;
894 int final_pass_orig = final_pass;
896 __push_fake_cur_stree();
898 final_pass = 0;
899 __split_whole_condition(expr->conditional);
900 final_pass = final_pass_orig;
902 if (get_implied_rl(expr->cond_true, &rl))
903 rl = cast_rl(cur_func_return_type(), rl);
904 else
905 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
906 return_ranges = show_rl(rl);
907 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
909 return_id++;
910 FOR_EACH_PTR(returned_state_callbacks, cb) {
911 cb->callback(return_id, return_ranges, expr);
912 } END_FOR_EACH_PTR(cb);
914 __push_true_states();
915 __use_false_states();
917 if (get_implied_rl(expr->cond_false, &rl))
918 rl = cast_rl(cur_func_return_type(), rl);
919 else
920 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
921 return_ranges = show_rl(rl);
922 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
924 return_id++;
925 FOR_EACH_PTR(returned_state_callbacks, cb) {
926 cb->callback(return_id, return_ranges, expr);
927 } END_FOR_EACH_PTR(cb);
929 __merge_true_states();
930 __free_fake_cur_stree();
933 static void call_return_state_hooks_compare(struct expression *expr)
935 struct returned_state_callback *cb;
936 char *return_ranges;
937 int final_pass_orig = final_pass;
938 sval_t sval = { .type = &int_ctype };
939 sval_t ret;
941 if (!get_implied_value(expr, &ret))
942 ret.value = -1;
944 __push_fake_cur_stree();
946 final_pass = 0;
947 __split_whole_condition(expr);
948 final_pass = final_pass_orig;
950 if (ret.value != 0) {
951 return_ranges = alloc_sname("1");
952 sval.value = 1;
953 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
955 return_id++;
956 FOR_EACH_PTR(returned_state_callbacks, cb) {
957 cb->callback(return_id, return_ranges, expr);
958 } END_FOR_EACH_PTR(cb);
961 __push_true_states();
962 __use_false_states();
964 if (ret.value != 1) {
965 return_ranges = alloc_sname("0");
966 sval.value = 0;
967 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
969 return_id++;
970 FOR_EACH_PTR(returned_state_callbacks, cb) {
971 cb->callback(return_id, return_ranges, expr);
972 } END_FOR_EACH_PTR(cb);
975 __merge_true_states();
976 __free_fake_cur_stree();
979 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
981 struct sm_state *tmp;
983 FOR_EACH_PTR(slist, tmp) {
984 if (strcmp(tmp->state->name, sm->state->name) == 0)
985 return 1;
986 } END_FOR_EACH_PTR(tmp);
988 return 0;
991 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
993 struct returned_state_callback *cb;
994 struct range_list *rl;
995 char *return_ranges;
996 struct sm_state *tmp;
997 int ret = 0;
998 int nr_possible, nr_states;
999 char *compare_str = NULL;
1000 char buf[128];
1001 struct state_list *already_handled = NULL;
1003 if (!sm || !sm->merged)
1004 return 0;
1006 if (too_many_possible(sm))
1007 return 0;
1009 /* bail if it gets too complicated */
1010 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1011 nr_states = stree_count(__get_cur_stree());
1012 if (nr_states * nr_possible >= 2000)
1013 return 0;
1015 FOR_EACH_PTR(sm->possible, tmp) {
1016 if (tmp->merged)
1017 continue;
1018 if (ptr_in_list(tmp, already_handled))
1019 continue;
1020 add_ptr_list(&already_handled, tmp);
1022 ret = 1;
1023 __push_fake_cur_stree();
1025 overwrite_states_using_pool(sm, tmp);
1027 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1028 return_ranges = show_rl(rl);
1029 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1030 compare_str = expr_lte_to_param(expr, -1);
1031 if (compare_str) {
1032 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1033 return_ranges = alloc_sname(buf);
1036 return_id++;
1037 FOR_EACH_PTR(returned_state_callbacks, cb) {
1038 cb->callback(return_id, return_ranges, expr);
1039 } END_FOR_EACH_PTR(cb);
1041 __free_fake_cur_stree();
1042 } END_FOR_EACH_PTR(tmp);
1044 free_slist(&already_handled);
1046 return ret;
1049 static int call_return_state_hooks_split_possible(struct expression *expr)
1051 struct sm_state *sm;
1053 if (!expr || expr_equal_to_param(expr, -1))
1054 return 0;
1056 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1057 return split_possible_helper(sm, expr);
1060 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1062 struct range_list *rl;
1063 char *return_ranges;
1064 sval_t sval;
1065 char *compare_str;
1066 char *math_str;
1067 char buf[128];
1069 *rl_p = NULL;
1071 if (!expr)
1072 return alloc_sname("");
1074 if (get_implied_value(expr, &sval)) {
1075 *rl_p = alloc_rl(sval, sval);
1076 return sval_to_str(sval);
1079 compare_str = expr_equal_to_param(expr, -1);
1080 math_str = get_value_in_terms_of_parameter_math(expr);
1082 if (get_implied_rl(expr, &rl)) {
1083 rl = cast_rl(cur_func_return_type(), rl);
1084 return_ranges = show_rl(rl);
1085 } else if (get_imaginary_absolute(expr, &rl)){
1086 rl = cast_rl(cur_func_return_type(), rl);
1087 return alloc_sname(show_rl(rl));
1088 } else {
1089 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1090 return_ranges = show_rl(rl);
1092 *rl_p = rl;
1094 if (compare_str) {
1095 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1096 return alloc_sname(buf);
1099 if (math_str) {
1100 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1101 return alloc_sname(buf);
1104 compare_str = expr_lte_to_param(expr, -1);
1105 if (compare_str) {
1106 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1107 return alloc_sname(buf);
1109 return return_ranges;
1112 static int split_positive_from_negative(struct expression *expr)
1114 struct returned_state_callback *cb;
1115 struct range_list *rl;
1116 const char *return_ranges;
1117 struct range_list *ret_rl;
1118 int undo;
1120 /* We're going to print the states 3 times */
1121 if (stree_count(__get_cur_stree()) > 10000 / 3)
1122 return 0;
1124 if (!get_implied_rl(expr, &rl) || !rl)
1125 return 0;
1126 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1127 return 0;
1128 /* Forget about INT_MAX and larger */
1129 if (rl_max(rl).value <= 0)
1130 return 0;
1131 if (!sval_is_negative(rl_min(rl)))
1132 return 0;
1134 if (!assume(compare_expression(expr, '>', zero_expr())))
1135 return 0;
1137 return_id++;
1138 return_ranges = get_return_ranges_str(expr, &ret_rl);
1139 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1140 FOR_EACH_PTR(returned_state_callbacks, cb) {
1141 cb->callback(return_id, (char *)return_ranges, expr);
1142 } END_FOR_EACH_PTR(cb);
1144 end_assume();
1146 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1147 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1149 return_id++;
1150 return_ranges = get_return_ranges_str(expr, &ret_rl);
1151 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1152 FOR_EACH_PTR(returned_state_callbacks, cb) {
1153 cb->callback(return_id, (char *)return_ranges, expr);
1154 } END_FOR_EACH_PTR(cb);
1156 if (undo)
1157 end_assume();
1160 undo = assume(compare_expression(expr, '<', zero_expr()));
1162 return_id++;
1163 return_ranges = get_return_ranges_str(expr, &ret_rl);
1164 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1165 FOR_EACH_PTR(returned_state_callbacks, cb) {
1166 cb->callback(return_id, (char *)return_ranges, expr);
1167 } END_FOR_EACH_PTR(cb);
1169 if (undo)
1170 end_assume();
1172 return 1;
1175 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1177 struct returned_state_callback *cb;
1178 struct range_list *rl;
1179 struct range_list *nonnull_rl;
1180 sval_t null_sval;
1181 struct range_list *null_rl = NULL;
1182 char *return_ranges;
1183 struct smatch_state *state;
1184 int nr_states;
1185 int final_pass_orig = final_pass;
1187 if (!expr || expr_equal_to_param(expr, -1))
1188 return 0;
1189 if (expr->type == EXPR_CALL)
1190 return 0;
1191 if (!is_pointer(expr))
1192 return 0;
1194 state = get_state_expr(SMATCH_EXTRA, expr);
1195 if (!state || !estate_rl(state))
1196 return 0;
1197 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1198 return 0;
1199 if (!rl_has_sval(estate_rl(state), sval_type_val(estate_type(state), 0)))
1200 return 0;
1202 nr_states = stree_count(__get_cur_stree());
1203 if (option_info && nr_states >= 1500)
1204 return 0;
1206 rl = estate_rl(state);
1208 __push_fake_cur_stree();
1210 final_pass = 0;
1211 __split_whole_condition(expr);
1212 final_pass = final_pass_orig;
1214 nonnull_rl = rl_filter(rl, rl_zero());
1215 return_ranges = show_rl(nonnull_rl);
1216 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1218 return_id++;
1219 FOR_EACH_PTR(returned_state_callbacks, cb) {
1220 cb->callback(return_id, return_ranges, expr);
1221 } END_FOR_EACH_PTR(cb);
1223 __push_true_states();
1224 __use_false_states();
1226 return_ranges = alloc_sname("0");
1227 null_sval = sval_type_val(rl_type(rl), 0);
1228 add_range(&null_rl, null_sval, null_sval);
1229 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1230 return_id++;
1231 FOR_EACH_PTR(returned_state_callbacks, cb) {
1232 cb->callback(return_id, return_ranges, expr);
1233 } END_FOR_EACH_PTR(cb);
1235 __merge_true_states();
1236 __free_fake_cur_stree();
1238 return 1;
1241 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1243 struct range_list *rl;
1244 struct range_list *nonzero_rl;
1245 sval_t zero_sval;
1246 struct range_list *zero_rl = NULL;
1247 int nr_states;
1248 struct returned_state_callback *cb;
1249 char *return_ranges;
1250 int final_pass_orig = final_pass;
1251 sval_t val;
1253 if (option_project != PROJ_KERNEL)
1254 return 0;
1256 nr_states = stree_count(__get_cur_stree());
1257 if (nr_states > 1500)
1258 return 0;
1260 if (get_value(expr, &val))
1261 return 0;
1262 if (!get_implied_rl(expr, &rl))
1263 return 0;
1264 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1265 return 0;
1266 if (rl_max(rl).value != 0)
1267 return 0;
1269 __push_fake_cur_stree();
1271 final_pass = 0;
1272 __split_whole_condition(expr);
1273 final_pass = final_pass_orig;
1275 nonzero_rl = rl_filter(rl, rl_zero());
1276 return_ranges = show_rl(nonzero_rl);
1277 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1279 return_id++;
1280 FOR_EACH_PTR(returned_state_callbacks, cb) {
1281 cb->callback(return_id, return_ranges, expr);
1282 } END_FOR_EACH_PTR(cb);
1284 __push_true_states();
1285 __use_false_states();
1287 return_ranges = alloc_sname("0");
1288 zero_sval = sval_type_val(rl_type(rl), 0);
1289 add_range(&zero_rl, zero_sval, zero_sval);
1290 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1291 return_id++;
1292 FOR_EACH_PTR(returned_state_callbacks, cb) {
1293 cb->callback(return_id, return_ranges, expr);
1294 } END_FOR_EACH_PTR(cb);
1296 __merge_true_states();
1297 __free_fake_cur_stree();
1299 return 1;
1302 static int is_boolean(struct expression *expr)
1304 struct range_list *rl;
1306 if (!get_implied_rl(expr, &rl))
1307 return 0;
1308 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1309 return 1;
1310 return 0;
1313 static int is_conditional(struct expression *expr)
1315 if (!expr)
1316 return 0;
1317 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1318 return 1;
1319 return 0;
1322 static int splitable_function_call(struct expression *expr)
1324 struct sm_state *sm;
1325 char buf[64];
1327 if (!expr || expr->type != EXPR_CALL)
1328 return 0;
1329 snprintf(buf, sizeof(buf), "return %p", expr);
1330 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1331 return split_possible_helper(sm, expr);
1334 static struct sm_state *find_bool_param(void)
1336 struct stree *start_states;
1337 struct symbol *arg;
1338 struct sm_state *sm, *tmp;
1339 sval_t sval;
1341 start_states = get_start_states();
1343 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1344 if (!arg->ident)
1345 continue;
1346 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1347 if (!sm)
1348 continue;
1349 if (rl_min(estate_rl(sm->state)).value != 0 ||
1350 rl_max(estate_rl(sm->state)).value != 1)
1351 continue;
1352 goto found;
1353 } END_FOR_EACH_PTR_REVERSE(arg);
1355 return NULL;
1357 found:
1359 * Check if it's splitable. If not, then splitting it up is likely not
1360 * useful for the callers.
1362 FOR_EACH_PTR(sm->possible, tmp) {
1363 if (is_merged(tmp))
1364 continue;
1365 if (!estate_get_single_value(tmp->state, &sval))
1366 return NULL;
1367 } END_FOR_EACH_PTR(tmp);
1369 return sm;
1372 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1374 struct returned_state_callback *cb;
1375 struct range_list *ret_rl;
1376 const char *return_ranges;
1377 struct sm_state *tmp;
1378 int ret = 0;
1379 int nr_possible, nr_states;
1380 char *compare_str = NULL;
1381 char buf[128];
1382 struct state_list *already_handled = NULL;
1384 if (!sm || !sm->merged)
1385 return 0;
1387 if (too_many_possible(sm))
1388 return 0;
1390 /* bail if it gets too complicated */
1391 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1392 nr_states = stree_count(__get_cur_stree());
1393 if (nr_states * nr_possible >= 2000)
1394 return 0;
1396 FOR_EACH_PTR(sm->possible, tmp) {
1397 if (tmp->merged)
1398 continue;
1399 if (ptr_in_list(tmp, already_handled))
1400 continue;
1401 add_ptr_list(&already_handled, tmp);
1403 ret = 1;
1404 __push_fake_cur_stree();
1406 overwrite_states_using_pool(sm, tmp);
1408 return_ranges = get_return_ranges_str(expr, &ret_rl);
1409 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1410 compare_str = expr_lte_to_param(expr, -1);
1411 if (compare_str) {
1412 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1413 return_ranges = alloc_sname(buf);
1416 return_id++;
1417 FOR_EACH_PTR(returned_state_callbacks, cb) {
1418 cb->callback(return_id, (char *)return_ranges, expr);
1419 } END_FOR_EACH_PTR(cb);
1421 __free_fake_cur_stree();
1422 } END_FOR_EACH_PTR(tmp);
1424 free_slist(&already_handled);
1426 return ret;
1429 static int split_by_bool_param(struct expression *expr)
1431 struct sm_state *start_sm, *sm;
1432 sval_t sval;
1434 start_sm = find_bool_param();
1435 if (!start_sm)
1436 return 0;
1437 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1438 if (!sm || estate_get_single_value(sm->state, &sval))
1439 return 0;
1440 return split_on_bool_sm(sm, expr);
1443 static int split_by_null_nonnull_param(struct expression *expr)
1445 struct symbol *arg;
1446 struct sm_state *sm;
1448 /* function must only take one pointer */
1449 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1450 return 0;
1451 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1452 if (!arg->ident)
1453 return 0;
1454 if (get_real_base_type(arg)->type != SYM_PTR)
1455 return 0;
1457 if (param_was_set_var_sym(arg->ident->name, arg))
1458 return 0;
1459 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1460 if (!sm)
1461 return 0;
1463 return split_on_bool_sm(sm, expr);
1466 static void call_return_state_hooks(struct expression *expr)
1468 struct returned_state_callback *cb;
1469 struct range_list *ret_rl;
1470 const char *return_ranges;
1471 int nr_states;
1472 sval_t sval;
1474 if (__path_is_null())
1475 return;
1477 expr = strip_expr(expr);
1479 if (is_impossible_path())
1480 goto vanilla;
1482 if (expr && (expr->type == EXPR_COMPARE ||
1483 !get_implied_value(expr, &sval)) &&
1484 (is_condition(expr) || is_boolean(expr))) {
1485 call_return_state_hooks_compare(expr);
1486 return;
1487 } else if (is_conditional(expr)) {
1488 call_return_state_hooks_conditional(expr);
1489 return;
1490 } else if (call_return_state_hooks_split_possible(expr)) {
1491 return;
1492 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1493 return;
1494 } else if (call_return_state_hooks_split_success_fail(expr)) {
1495 return;
1496 } else if (splitable_function_call(expr)) {
1497 return;
1498 } else if (split_positive_from_negative(expr)) {
1499 return;
1500 } else if (split_by_bool_param(expr)) {
1501 } else if (split_by_null_nonnull_param(expr)) {
1502 return;
1505 vanilla:
1506 return_ranges = get_return_ranges_str(expr, &ret_rl);
1507 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1509 return_id++;
1510 nr_states = stree_count(__get_cur_stree());
1511 if (nr_states >= 10000) {
1512 match_return_info(return_id, (char *)return_ranges, expr);
1513 return;
1515 FOR_EACH_PTR(returned_state_callbacks, cb) {
1516 cb->callback(return_id, (char *)return_ranges, expr);
1517 } END_FOR_EACH_PTR(cb);
1520 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1522 struct returned_member_callback *cb;
1523 struct stree *stree;
1524 struct sm_state *sm;
1525 struct symbol *type;
1526 char *name;
1527 char member_name[256];
1528 int len;
1530 type = get_type(expr);
1531 if (!type || type->type != SYM_PTR)
1532 return;
1533 name = expr_to_var(expr);
1534 if (!name)
1535 return;
1537 member_name[sizeof(member_name) - 1] = '\0';
1538 strcpy(member_name, "$");
1540 len = strlen(name);
1541 FOR_EACH_PTR(returned_member_callbacks, cb) {
1542 stree = __get_cur_stree();
1543 FOR_EACH_MY_SM(cb->owner, stree, sm) {
1544 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
1545 strcpy(member_name, "*$");
1546 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1547 continue;
1549 if (strncmp(sm->name, name, len) != 0)
1550 continue;
1551 if (strncmp(sm->name + len, "->", 2) != 0)
1552 continue;
1553 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
1554 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
1555 } END_FOR_EACH_SM(sm);
1556 } END_FOR_EACH_PTR(cb);
1558 free_string(name);
1561 static void reset_memdb(struct symbol *sym)
1563 mem_sql(NULL, NULL, "delete from caller_info;");
1564 mem_sql(NULL, NULL, "delete from return_states;");
1565 mem_sql(NULL, NULL, "delete from call_implies;");
1568 static void match_end_func_info(struct symbol *sym)
1570 if (__path_is_null())
1571 return;
1572 call_return_state_hooks(NULL);
1575 static void match_after_func(struct symbol *sym)
1577 if (!__inline_fn)
1578 reset_memdb(sym);
1581 static void init_memdb(void)
1583 char *err = NULL;
1584 int rc;
1585 const char *schema_files[] = {
1586 "db/db.schema",
1587 "db/caller_info.schema",
1588 "db/return_states.schema",
1589 "db/function_type_size.schema",
1590 "db/type_size.schema",
1591 "db/call_implies.schema",
1592 "db/function_ptr.schema",
1593 "db/local_values.schema",
1594 "db/function_type_value.schema",
1595 "db/type_value.schema",
1596 "db/function_type.schema",
1597 "db/data_info.schema",
1598 "db/parameter_name.schema",
1600 static char buf[4096];
1601 int fd;
1602 int ret;
1603 int i;
1605 rc = sqlite3_open(":memory:", &mem_db);
1606 if (rc != SQLITE_OK) {
1607 printf("Error starting In-Memory database.");
1608 return;
1611 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
1612 fd = open_data_file(schema_files[i]);
1613 if (fd < 0) {
1614 mem_db = NULL;
1615 return;
1617 ret = read(fd, buf, sizeof(buf));
1618 if (ret == sizeof(buf)) {
1619 printf("Schema file too large: %s (limit %zd bytes)",
1620 schema_files[i], sizeof(buf));
1622 buf[ret] = '\0';
1623 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
1624 if (rc != SQLITE_OK) {
1625 fprintf(stderr, "SQL error #2: %s\n", err);
1626 fprintf(stderr, "%s\n", buf);
1631 void open_smatch_db(void)
1633 int rc;
1635 if (option_no_db)
1636 return;
1638 init_memdb();
1640 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
1641 if (rc != SQLITE_OK) {
1642 option_no_db = 1;
1643 return;
1645 return;
1648 static void register_common_funcs(void)
1650 struct token *token;
1651 char *func;
1652 char filename[256];
1654 if (option_project == PROJ_NONE)
1655 strcpy(filename, "common_functions");
1656 else
1657 snprintf(filename, 256, "%s.common_functions", option_project_str);
1659 token = get_tokens_file(filename);
1660 if (!token)
1661 return;
1662 if (token_type(token) != TOKEN_STREAMBEGIN)
1663 return;
1664 token = token->next;
1665 while (token_type(token) != TOKEN_STREAMEND) {
1666 if (token_type(token) != TOKEN_IDENT)
1667 return;
1668 func = alloc_string(show_ident(token->ident));
1669 add_ptr_list(&common_funcs, func);
1670 token = token->next;
1672 clear_token_alloc();
1676 void register_definition_db_callbacks(int id)
1678 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1679 add_hook(&global_variable, BASE_HOOK);
1680 add_hook(&global_variable, DECLARATION_HOOK);
1681 add_split_return_callback(match_return_info);
1682 add_split_return_callback(print_returned_struct_members);
1683 add_hook(&call_return_state_hooks, RETURN_HOOK);
1684 add_hook(&match_end_func_info, END_FUNC_HOOK);
1685 add_hook(&match_after_func, AFTER_FUNC_HOOK);
1687 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
1688 add_hook(&match_call_implies, CALL_HOOK_AFTER_INLINE);
1690 register_common_funcs();
1693 void register_db_call_marker(int id)
1695 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
1698 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
1700 struct expression *arg;
1701 char *name = NULL;
1702 char member_name[256];
1704 *sym = NULL;
1706 if (param == -1) {
1707 const char *star = "";
1709 if (expr->type != EXPR_ASSIGNMENT)
1710 return NULL;
1711 name = expr_to_var_sym(expr->left, sym);
1712 if (!name)
1713 return NULL;
1714 if (key[0] == '*') {
1715 star = "*";
1716 key++;
1718 if (strncmp(key, "$", 1) != 0)
1719 return name;
1720 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
1721 free_string(name);
1722 return alloc_string(member_name);
1725 while (expr->type == EXPR_ASSIGNMENT)
1726 expr = strip_expr(expr->right);
1727 if (expr->type != EXPR_CALL)
1728 return NULL;
1730 arg = get_argument_from_call_expr(expr->args, param);
1731 if (!arg)
1732 return NULL;
1734 return get_variable_from_key(arg, key, sym);
1737 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
1739 char buf[256];
1740 char *tmp;
1742 if (!arg)
1743 return NULL;
1745 arg = strip_expr(arg);
1747 if (strcmp(key, "$") == 0)
1748 return expr_to_var_sym(arg, sym);
1750 if (strcmp(key, "*$") == 0) {
1751 if (arg->type == EXPR_PREOP && arg->op == '&') {
1752 arg = strip_expr(arg->unop);
1753 return expr_to_var_sym(arg, sym);
1754 } else {
1755 tmp = expr_to_var_sym(arg, sym);
1756 if (!tmp)
1757 return NULL;
1758 snprintf(buf, sizeof(buf), "*%s", tmp);
1759 free_string(tmp);
1760 return alloc_string(buf);
1764 if (arg->type == EXPR_PREOP && arg->op == '&') {
1765 arg = strip_expr(arg->unop);
1766 tmp = expr_to_var_sym(arg, sym);
1767 if (!tmp)
1768 return NULL;
1769 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
1770 return alloc_string(buf);
1773 tmp = expr_to_var_sym(arg, sym);
1774 if (!tmp)
1775 return NULL;
1776 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
1777 free_string(tmp);
1778 return alloc_string(buf);
1781 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
1783 *vsl = NULL;
1785 if (strcmp("$", key) == 0)
1786 return expr_to_chunk_sym_vsl(arg, sym, vsl);
1787 return get_variable_from_key(arg, key, sym);
1790 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
1792 char *param_name;
1793 int name_len;
1794 static char buf[256];
1796 if (!sym || !sym->ident)
1797 return NULL;
1799 param_name = sym->ident->name;
1800 name_len = strlen(param_name);
1802 if (strcmp(name, param_name) == 0) {
1803 return "$";
1804 } else if (name[name_len] == '-' && /* check for '-' from "->" */
1805 strncmp(name, param_name, name_len) == 0) {
1806 snprintf(buf, sizeof(buf), "$%s", name + name_len);
1807 return buf;
1808 } else if (name[0] == '*' && strcmp(name + 1, param_name) == 0) {
1809 return "*$";
1811 return NULL;
1814 const char *get_param_name(struct sm_state *sm)
1816 return get_param_name_var_sym(sm->name, sm->sym);
1819 char *get_data_info_name(struct expression *expr)
1821 struct symbol *sym;
1822 char *name;
1823 char buf[256];
1824 char *ret = NULL;
1826 expr = strip_expr(expr);
1827 name = get_member_name(expr);
1828 if (name)
1829 return name;
1830 name = expr_to_var_sym(expr, &sym);
1831 if (!name || !sym)
1832 goto free;
1833 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
1834 goto free;
1835 if (sym->ctype.modifiers & MOD_STATIC)
1836 snprintf(buf, sizeof(buf), "static %s", name);
1837 else
1838 snprintf(buf, sizeof(buf), "global %s", name);
1839 ret = alloc_sname(buf);
1840 free:
1841 free_string(name);
1842 return ret;