return_to_param: delete debug code
[smatch.git] / smatch_db.c
blob872a77af8983294837a97f9565ed23439a0287fe
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 <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 struct sqlite3 *smatch_db;
27 struct sqlite3 *mem_db;
28 struct sqlite3 *cache_db;
30 int debug_db;
32 static int return_id;
34 static void call_return_state_hooks(struct expression *expr);
36 #define SQLITE_CACHE_PAGES 1000
38 struct def_callback {
39 int hook_type;
40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
42 ALLOCATOR(def_callback, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list, struct def_callback);
44 static struct callback_list *select_caller_info_callbacks;
46 struct member_info_callback {
47 int owner;
48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
50 ALLOCATOR(member_info_callback, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
52 static struct member_info_cb_list *member_callbacks;
53 static struct member_info_cb_list *member_callbacks_new;
55 struct return_info_callback {
56 int owner;
57 void (*callback)(int return_id, char *return_ranges,
58 struct expression *returned_expr,
59 int param,
60 const char *printed_name,
61 struct sm_state *sm);
63 ALLOCATOR(return_info_callback, "return_info callbacks");
64 DECLARE_PTR_LIST(return_info_cb_list, struct return_info_callback);
65 static struct return_info_cb_list *return_callbacks;
67 struct returned_state_callback {
68 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
70 ALLOCATOR(returned_state_callback, "returned state callbacks");
71 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
72 static struct returned_state_cb_list *returned_state_callbacks;
74 struct returned_member_callback {
75 int owner;
76 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
78 ALLOCATOR(returned_member_callback, "returned member callbacks");
79 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
80 static struct returned_member_cb_list *returned_member_callbacks;
82 struct db_implies_callback {
83 int type;
84 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
86 ALLOCATOR(db_implies_callback, "return_implies callbacks");
87 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
88 static struct db_implies_cb_list *return_implies_cb_list;
89 static struct db_implies_cb_list *call_implies_cb_list;
91 /* silently truncates if needed. */
92 char *escape_newlines(const char *str)
94 char buf[1024] = "";
95 bool found = false;
96 int i, j;
98 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
99 if (str[i] != '\r' && str[i] != '\n') {
100 buf[j] = str[i];
101 continue;
104 found = true;
105 buf[j++] = '\\';
106 if (j == sizeof(buf))
107 break;
108 buf[j] = 'n';
111 if (!found)
112 return alloc_sname(str);
114 if (j == sizeof(buf))
115 buf[j - 1] = '\0';
116 return alloc_sname(buf);
119 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
121 int i;
123 for (i = 0; i < argc; i++) {
124 if (i != 0)
125 sm_printf(", ");
126 sm_printf("%s", argv[i]);
128 sm_printf("\n");
129 return 0;
132 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
134 char *err = NULL;
135 int rc;
137 if (!db)
138 return;
140 if (option_debug || debug_db) {
141 sm_msg("%s", sql);
142 if (strncasecmp(sql, "select", strlen("select")) == 0)
143 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
146 rc = sqlite3_exec(db, sql, callback, data, &err);
147 if (rc != SQLITE_OK && !parse_error) {
148 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
149 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
150 parse_error = 1;
154 static int replace_count;
155 static char **replace_table;
156 static const char *replace_return_ranges(const char *return_ranges)
158 int i;
160 if (!get_function()) {
161 /* I have no idea why EXPORT_SYMBOL() is here */
162 return return_ranges;
164 for (i = 0; i < replace_count; i += 3) {
165 if (strcmp(replace_table[i + 0], get_function()) == 0) {
166 if (strcmp(replace_table[i + 1], return_ranges) == 0)
167 return replace_table[i + 2];
170 return return_ranges;
174 static char *use_states;
175 static int get_db_state_count(void)
177 struct sm_state *sm;
178 int count = 0;
180 FOR_EACH_SM(__get_cur_stree(), sm) {
181 if (sm->owner == USHRT_MAX)
182 continue;
183 if (use_states[sm->owner])
184 count++;
185 } END_FOR_EACH_SM(sm);
186 return count;
189 void db_ignore_states(int id)
191 use_states[id] = 0;
194 unsigned long long __fn_mtag;
195 static void set_fn_mtag(struct symbol *sym)
197 char buf[128];
199 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
200 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
201 else
202 snprintf(buf, sizeof(buf), "extern %s", get_function());
204 __fn_mtag = str_to_mtag(buf);
207 void sql_insert_return_states(int return_id, const char *return_ranges,
208 int type, int param, const char *key, const char *value)
210 unsigned long long id;
213 if (key && strlen(key) >= 80)
214 return;
215 if (__inline_fn)
216 id = (unsigned long)__inline_fn;
217 else
218 id = __fn_mtag;
220 return_ranges = replace_return_ranges(return_ranges);
221 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
222 get_base_file(), get_function(), id, return_id,
223 return_ranges, fn_static(), type, param, key, value);
226 static struct string_list *common_funcs;
227 static int is_common_function(const char *fn)
229 char *tmp;
231 if (!fn)
232 return 0;
234 if (strncmp(fn, "__builtin_", 10) == 0)
235 return 1;
237 FOR_EACH_PTR(common_funcs, tmp) {
238 if (strcmp(tmp, fn) == 0)
239 return 1;
240 } END_FOR_EACH_PTR(tmp);
242 return 0;
245 static char *function_signature(void)
247 return type_to_str(get_real_base_type(cur_func_sym));
250 void sql_insert_caller_info(struct expression *call, int type,
251 int param, const char *key, const char *value)
253 FILE *tmp_fd = sm_outfd;
254 char *fn;
256 if (!option_info && !__inline_call)
257 return;
258 if (unreachable())
259 return;
261 if (key && strlen(key) >= 80)
262 return;
264 fn = get_fnptr_name(call->fn);
265 if (!fn)
266 return;
268 if (__inline_call) {
269 mem_sql(NULL, NULL,
270 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
271 get_base_file(), get_function(), fn, (unsigned long)call,
272 is_static(call->fn), type, param, key, value);
275 if (!option_info)
276 return;
278 if (strncmp(fn, "__builtin_", 10) == 0)
279 return;
280 if (type != INTERNAL && is_common_function(fn))
281 return;
283 sm_outfd = caller_info_fd;
284 sm_msg("SQL_caller_info: insert into caller_info values ("
285 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
286 get_base_file(), get_function(), fn, is_static(call->fn),
287 type, param, key, value);
288 sm_outfd = tmp_fd;
290 free_string(fn);
293 void sql_insert_function_ptr(const char *fn, const char *struct_name)
295 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
296 get_base_file(), fn, struct_name);
299 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
301 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
302 get_base_file(), get_function(), (unsigned long)__inline_fn,
303 fn_static(), type, param, key, value);
306 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
308 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
309 get_base_file(), get_function(), (unsigned long)__inline_fn,
310 fn_static(), type, param, key, value);
313 void sql_insert_function_type_size(const char *member, const char *ranges)
315 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
318 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
320 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
323 void sql_insert_type_info(int type, const char *member, const char *value)
325 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
328 void sql_insert_local_values(const char *name, const char *value)
330 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
333 void sql_insert_function_type_value(const char *type, const char *value)
335 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
338 void sql_insert_function_type(int param, const char *value)
340 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
341 get_base_file(), get_function(), fn_static(), param, value);
344 void sql_insert_parameter_name(int param, const char *value)
346 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
347 get_base_file(), get_function(), fn_static(), param, value);
350 void sql_insert_data_info(struct expression *data, int type, const char *value)
352 char *data_name;
354 data_name = get_data_info_name(data);
355 if (!data_name)
356 return;
357 sql_insert(data_info, "'%s', '%s', %d, '%s'",
358 is_static(data) ? get_base_file() : "extern",
359 data_name, type, value);
362 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
364 sql_insert(data_info, "'%s', '%s', %d, '%s'",
365 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
366 var, type, value);
369 void sql_save_constraint(const char *con)
371 if (!option_info)
372 return;
374 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
377 void sql_save_constraint_required(const char *data, int op, const char *limit)
379 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
382 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
384 if (!option_info)
385 return;
387 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
388 "select constraints_required.data, constraints_required.op, '%s' from "
389 "constraints_required where bound = '%s';", new_limit, old_limit);
392 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
394 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
397 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
399 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
400 return;
402 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
403 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
404 fn->symbol->ident->name,
405 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
406 type, param, key, value);
409 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
411 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
412 tag, get_filename(), get_function(), get_lineno(),
413 left_name, right_name);
416 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
418 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
421 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
423 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
426 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
428 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
431 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
433 mtag_t *saved_tag = _tag;
434 mtag_t new_tag;
436 new_tag = strtoll(argv[0], NULL, 10);
438 if (!*saved_tag)
439 *saved_tag = new_tag;
440 else if (*saved_tag != new_tag)
441 *saved_tag = -1ULL;
443 return 0;
446 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
448 mtag_t tmp = 0;
450 run_sql(save_mtag, &tmp,
451 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
452 tag, container_offset);
454 if (tmp == 0 || tmp == -1ULL)
455 return 0;
456 *container = tmp;
457 return 1;
460 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
462 mtag_t tmp = 0;
464 run_sql(save_mtag, &tmp,
465 "select tag from mtag_map where container = %lld and container_offset = %d;",
466 container, offset);
468 if (tmp == 0 || tmp == -1ULL)
469 return 0;
470 *tag = tmp;
471 return 1;
474 char *get_static_filter(struct symbol *sym)
476 static char sql_filter[1024];
478 /* This can only happen on buggy code. Return invalid SQL. */
479 if (!sym) {
480 sql_filter[0] = '\0';
481 return sql_filter;
484 if (sym->ctype.modifiers & MOD_STATIC) {
485 snprintf(sql_filter, sizeof(sql_filter),
486 "file = '%s' and function = '%s' and static = '1'",
487 get_base_file(), sym->ident->name);
488 } else {
489 snprintf(sql_filter, sizeof(sql_filter),
490 "function = '%s' and static = '0'", sym->ident->name);
493 return sql_filter;
496 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
498 int *row_count = _row_count;
500 *row_count = 0;
501 if (argc != 1)
502 return 0;
503 *row_count = atoi(argv[0]);
504 return 0;
507 static void mark_call_params_untracked(struct expression *call)
509 struct expression *arg;
510 int i = 0;
512 FOR_EACH_PTR(call->args, arg) {
513 mark_untracked(call, i++, "$", NULL);
514 } END_FOR_EACH_PTR(arg);
517 static void sql_select_return_states_pointer(const char *cols,
518 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
520 char *ptr;
521 int return_count = 0;
523 ptr = get_fnptr_name(call->fn);
524 if (!ptr)
525 return;
527 run_sql(get_row_count, &return_count,
528 "select count(*) from return_states join function_ptr "
529 "where return_states.function == function_ptr.function and "
530 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
531 /* The magic number 100 is just from testing on the kernel. */
532 if (return_count > 100) {
533 mark_call_params_untracked(call);
534 return;
537 run_sql(callback, info,
538 "select %s from return_states join function_ptr where "
539 "return_states.function == function_ptr.function and ptr = '%s' "
540 "and searchable = 1 "
541 "order by function_ptr.file, return_states.file, return_id, type;",
542 cols, ptr);
545 static int is_local_symbol(struct expression *expr)
547 if (expr->type != EXPR_SYMBOL)
548 return 0;
549 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
550 return 0;
551 return 1;
554 void sql_select_return_states(const char *cols, struct expression *call,
555 int (*callback)(void*, int, char**, char**), void *info)
557 struct expression *fn;
558 int row_count = 0;
560 if (is_fake_call(call))
561 return;
563 fn = strip_expr(call->fn);
564 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
565 sql_select_return_states_pointer(cols, call, callback, info);
566 return;
569 if (inlinable(fn)) {
570 mem_sql(callback, info,
571 "select %s from return_states where call_id = '%lu' order by return_id, type;",
572 cols, (unsigned long)call);
573 return;
576 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
577 get_static_filter(fn->symbol));
578 if (row_count > 3000)
579 return;
581 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
582 cols, get_static_filter(fn->symbol));
585 #define CALL_IMPLIES 0
586 #define RETURN_IMPLIES 1
588 struct implies_info {
589 int type;
590 struct db_implies_cb_list *cb_list;
591 struct expression *expr;
592 struct symbol *sym;
595 void sql_select_implies(const char *cols, struct implies_info *info,
596 int (*callback)(void*, int, char**, char**))
598 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
599 mem_sql(callback, info,
600 "select %s from return_implies where call_id = '%lu';",
601 cols, (unsigned long)info->expr);
602 return;
605 run_sql(callback, info, "select %s from %s_implies where %s;",
606 cols,
607 info->type == CALL_IMPLIES ? "call" : "return",
608 get_static_filter(info->sym));
611 struct select_caller_info_data {
612 struct stree *final_states;
613 struct timeval start_time;
614 int prev_func_id;
615 int ignore;
616 int results;
619 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
621 static void sql_select_caller_info(struct select_caller_info_data *data,
622 const char *cols, struct symbol *sym)
624 if (__inline_fn) {
625 mem_sql(caller_info_callback, data,
626 "select %s from caller_info where call_id = %lu;",
627 cols, (unsigned long)__inline_fn);
628 return;
631 if (sym->ident->name && is_common_function(sym->ident->name))
632 return;
633 run_sql(caller_info_callback, data,
634 "select %s from common_caller_info where %s order by call_id;",
635 cols, get_static_filter(sym));
636 if (data->results)
637 return;
639 run_sql(caller_info_callback, data,
640 "select %s from caller_info where %s order by call_id;",
641 cols, get_static_filter(sym));
644 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
646 struct def_callback *def_callback = __alloc_def_callback(0);
648 def_callback->hook_type = type;
649 def_callback->callback = callback;
650 add_ptr_list(&select_caller_info_callbacks, def_callback);
654 * These call backs are used when the --info option is turned on to print struct
655 * member information. For example foo->bar could have a state in
656 * smatch_extra.c and also check_user.c.
658 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
660 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
662 member_callback->owner = owner;
663 member_callback->callback = callback;
664 add_ptr_list(&member_callbacks, member_callback);
667 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
669 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
671 member_callback->owner = owner;
672 member_callback->callback = callback;
673 add_ptr_list(&member_callbacks_new, member_callback);
676 void add_return_info_callback(int owner,
677 void (*callback)(int return_id, char *return_ranges,
678 struct expression *returned_expr,
679 int param,
680 const char *printed_name,
681 struct sm_state *sm))
683 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
685 return_callback->owner = owner;
686 return_callback->callback = callback;
687 add_ptr_list(&return_callbacks, return_callback);
690 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
692 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
694 callback->callback = fn;
695 add_ptr_list(&returned_state_callbacks, callback);
698 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))
700 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
702 member_callback->owner = owner;
703 member_callback->callback = callback;
704 add_ptr_list(&returned_member_callbacks, member_callback);
707 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
709 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
711 cb->type = type;
712 cb->callback = callback;
713 add_ptr_list(&call_implies_cb_list, cb);
716 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
718 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
720 cb->type = type;
721 cb->callback = callback;
722 add_ptr_list(&return_implies_cb_list, cb);
725 struct return_info {
726 struct expression *static_returns_call;
727 struct symbol *return_type;
728 struct range_list *return_range_list;
731 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
733 struct return_info *ret_info = _ret_info;
734 struct range_list *rl;
735 struct expression *call_expr = ret_info->static_returns_call;
737 if (argc != 1)
738 return 0;
739 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
740 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
741 return 0;
744 struct range_list *db_return_vals(struct expression *expr)
746 struct return_info ret_info = {};
747 struct sm_state *sm;
749 if (is_fake_call(expr))
750 return NULL;
752 sm = get_extra_sm_state(expr);
753 if (sm)
754 return clone_rl(estate_rl(sm->state));
755 ret_info.static_returns_call = expr;
756 ret_info.return_type = get_type(expr);
757 if (!ret_info.return_type)
758 return NULL;
760 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
761 return NULL;
763 ret_info.return_range_list = NULL;
764 if (inlinable(expr->fn)) {
765 mem_sql(db_return_callback, &ret_info,
766 "select distinct return from return_states where call_id = '%lu';",
767 (unsigned long)expr);
768 } else {
769 run_sql(db_return_callback, &ret_info,
770 "select distinct return from return_states where %s;",
771 get_static_filter(expr->fn->symbol));
773 return ret_info.return_range_list;
776 struct range_list *db_return_vals_from_str(const char *fn_name)
778 struct return_info ret_info;
780 ret_info.static_returns_call = NULL;
781 ret_info.return_type = &llong_ctype;
782 ret_info.return_range_list = NULL;
784 run_sql(db_return_callback, &ret_info,
785 "select distinct return from return_states where function = '%s';",
786 fn_name);
787 return ret_info.return_range_list;
791 * This is used when we have a function that takes a function pointer as a
792 * parameter. "frob(blah, blah, my_function);" We know that the return values
793 * from frob() come from my_funcion() so we want to find the possible returns
794 * of my_function(), but we don't know which arguments are passed to it.
797 struct range_list *db_return_vals_no_args(struct expression *expr)
799 struct return_info ret_info = {};
801 if (!expr || expr->type != EXPR_SYMBOL)
802 return NULL;
804 ret_info.static_returns_call = expr;
805 ret_info.return_type = get_type(expr);
806 ret_info.return_type = get_real_base_type(ret_info.return_type);
807 if (!ret_info.return_type)
808 return NULL;
810 run_sql(db_return_callback, &ret_info,
811 "select distinct return from return_states where %s;",
812 get_static_filter(expr->symbol));
814 return ret_info.return_range_list;
817 static void match_call_marker(struct expression *expr)
819 struct symbol *type;
821 type = get_type(expr->fn);
822 if (type && type->type == SYM_PTR)
823 type = get_real_base_type(type);
826 * we just want to record something in the database so that if we have
827 * two calls like: frob(4); frob(some_unkown); then on the receiving
828 * side we know that sometimes frob is called with unknown parameters.
831 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
834 int is_recursive_member(const char *name)
836 char buf[256];
837 const char *p, *next;
838 int size;
840 p = strchr(name, '>');
841 if (!p)
842 return 0;
843 p++;
844 while (true) {
845 next = strchr(p, '>');
846 if (!next)
847 return 0;
848 next++;
850 size = next - p;
851 if (size >= sizeof(buf))
852 return 0;
853 memcpy(buf, p, size);
854 buf[size] = '\0';
855 if (strstr(next, buf))
856 return 1;
857 p = next;
861 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
863 struct symbol *sym;
864 const char *sm_name;
865 char *name;
866 bool is_address = false;
867 bool add_star = false;
868 char buf[256];
869 char *ret = NULL;
870 int len;
872 expr = strip_expr(expr);
873 if (!expr)
874 return NULL;
876 if (expr->type == EXPR_PREOP && expr->op == '&') {
877 expr = strip_expr(expr->unop);
878 is_address = true;
881 name = expr_to_var_sym(expr, &sym);
882 if (!name || !sym)
883 goto free;
884 if (sym != sm->sym)
885 goto free;
887 sm_name = sm->name;
888 add_star = false;
889 if (sm_name[0] == '*') {
890 add_star = true;
891 sm_name++;
894 len = strlen(name);
895 if (strncmp(name, sm_name, len) != 0)
896 goto free;
897 if (sm_name[len] == '\0') {
898 snprintf(buf, sizeof(buf), "%s%s$",
899 add_star ? "*" : "", is_address ? "*" : "");
900 } else {
901 if (sm_name[len] != '.' && sm_name[len] != '-')
902 goto free;
903 if (sm_name[len] == '-')
904 len++;
905 // FIXME does is_address really imply that sm_name[len] == '-'
906 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
907 sm_name + len);
910 ret = alloc_sname(buf);
911 free:
912 free_string(name);
913 return ret;
916 static void print_struct_members(struct expression *call, struct expression *expr, int param,
917 int owner,
918 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
919 bool new)
921 struct sm_state *sm;
922 const char *sm_name;
923 char *name;
924 struct symbol *sym;
925 int len;
926 char printed_name[256];
927 int is_address = 0;
928 bool add_star;
929 struct symbol *type;
931 expr = strip_expr(expr);
932 if (!expr)
933 return;
934 type = get_type(expr);
935 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
936 return;
938 if (expr->type == EXPR_PREOP && expr->op == '&') {
939 expr = strip_expr(expr->unop);
940 is_address = 1;
943 name = expr_to_var_sym(expr, &sym);
944 if (!name || !sym)
945 goto free;
947 len = strlen(name);
948 FOR_EACH_SM(__get_cur_stree(), sm) {
949 if (sm->owner != owner || sm->sym != sym)
950 continue;
951 sm_name = sm->name;
952 add_star = false;
953 if (sm_name[0] == '*') {
954 add_star = true;
955 sm_name++;
957 // FIXME: simplify?
958 if (!add_star && strcmp(name, sm_name) == 0) {
959 if (is_address) {
960 snprintf(printed_name, sizeof(printed_name), "*$");
961 } else {
962 if (new)
963 snprintf(printed_name, sizeof(printed_name), "$");
964 else
965 continue;
967 } else if (add_star && strcmp(name, sm_name) == 0) {
968 snprintf(printed_name, sizeof(printed_name), "%s*$",
969 is_address ? "*" : "");
970 } else if (strncmp(name, sm_name, len) == 0) {
971 if (sm_name[len] != '.' && sm_name[len] != '-')
972 continue;
973 if (is_address && sm_name[len] == '.') {
974 snprintf(printed_name, sizeof(printed_name),
975 "%s$->%s", add_star ? "*" : "",
976 sm_name + len + 1);
977 } else if (is_address && sm_name[len] == '-') {
978 snprintf(printed_name, sizeof(printed_name),
979 "%s(*$)%s", add_star ? "*" : "",
980 sm_name + len);
981 } else {
982 snprintf(printed_name, sizeof(printed_name),
983 "%s$%s", add_star ? "*" : "",
984 sm_name + len);
986 } else {
987 continue;
989 if (is_recursive_member(printed_name))
990 continue;
991 callback(call, param, printed_name, sm);
992 } END_FOR_EACH_SM(sm);
993 free:
994 free_string(name);
997 static void match_call_info(struct expression *call)
999 struct member_info_callback *cb;
1000 struct expression *arg;
1001 int i;
1003 FOR_EACH_PTR(member_callbacks, cb) {
1004 i = -1;
1005 FOR_EACH_PTR(call->args, arg) {
1006 i++;
1007 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1008 } END_FOR_EACH_PTR(arg);
1009 } END_FOR_EACH_PTR(cb);
1012 static struct expression *get_fake_variable(struct expression *expr)
1014 struct expression *tmp;
1016 tmp = expr_get_fake_parent_expr(expr);
1017 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1018 return NULL;
1020 return tmp->left;
1023 static void match_call_info_new(struct expression *call)
1025 struct member_info_callback *cb;
1026 struct expression *arg, *tmp;
1027 int i;
1029 FOR_EACH_PTR(member_callbacks_new, cb) {
1030 i = -1;
1031 FOR_EACH_PTR(call->args, arg) {
1032 i++;
1033 tmp = get_fake_variable(arg);
1034 if (!tmp)
1035 tmp = arg;
1036 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1037 } END_FOR_EACH_PTR(arg);
1038 } END_FOR_EACH_PTR(cb);
1041 static int get_param(int param, char **name, struct symbol **sym)
1043 struct symbol *arg;
1044 int i;
1046 i = 0;
1047 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1049 * this is a temporary hack to work around a bug (I think in sparse?)
1050 * 2.6.37-rc1:fs/reiserfs/journal.o
1051 * If there is a function definition without parameter name found
1052 * after a function implementation then it causes a crash.
1053 * int foo() {}
1054 * int bar(char *);
1056 if (arg->ident->name < (char *)100)
1057 continue;
1058 if (i == param) {
1059 *name = arg->ident->name;
1060 *sym = arg;
1061 return TRUE;
1063 i++;
1064 } END_FOR_EACH_PTR(arg);
1066 return FALSE;
1069 static int function_signature_matches(const char *sig)
1071 char *my_sig;
1073 my_sig = function_signature();
1074 if (!sig || !my_sig)
1075 return 1; /* default to matching */
1076 if (strcmp(my_sig, sig) == 0)
1077 return 1;
1078 return 0;
1081 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1083 struct select_caller_info_data *data = _data;
1084 int func_id;
1085 long type;
1086 long param;
1087 char *key;
1088 char *value;
1089 char *name = NULL;
1090 struct symbol *sym = NULL;
1091 struct def_callback *def_callback;
1092 struct stree *stree;
1093 struct timeval cur_time;
1095 data->results = 1;
1097 if (argc != 5)
1098 return 0;
1100 gettimeofday(&cur_time, NULL);
1101 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1102 return 0;
1104 func_id = atoi(argv[0]);
1105 errno = 0;
1106 type = strtol(argv[1], NULL, 10);
1107 param = strtol(argv[2], NULL, 10);
1108 if (errno)
1109 return 0;
1110 key = argv[3];
1111 value = argv[4];
1113 if (data->prev_func_id == -1)
1114 data->prev_func_id = func_id;
1115 if (func_id != data->prev_func_id) {
1116 stree = __pop_fake_cur_stree();
1117 if (!data->ignore)
1118 merge_stree(&data->final_states, stree);
1119 free_stree(&stree);
1120 __push_fake_cur_stree();
1121 __unnullify_path();
1122 data->prev_func_id = func_id;
1123 data->ignore = 0;
1126 if (data->ignore)
1127 return 0;
1128 if (type == INTERNAL &&
1129 !function_signature_matches(value)) {
1130 data->ignore = 1;
1131 return 0;
1134 if (param >= 0 && !get_param(param, &name, &sym))
1135 return 0;
1137 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1138 if (def_callback->hook_type == type)
1139 def_callback->callback(name, sym, key, value);
1140 } END_FOR_EACH_PTR(def_callback);
1142 return 0;
1145 static struct string_list *ptr_names_done;
1146 static struct string_list *ptr_names;
1148 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1150 insert_string(&ptr_names, alloc_string(argv[0]));
1151 return 0;
1154 static char *get_next_ptr_name(void)
1156 char *ptr;
1158 FOR_EACH_PTR(ptr_names, ptr) {
1159 if (!insert_string(&ptr_names_done, ptr))
1160 continue;
1161 return ptr;
1162 } END_FOR_EACH_PTR(ptr);
1163 return NULL;
1166 static void get_ptr_names(const char *file, const char *name)
1168 char sql_filter[1024];
1169 int before, after;
1171 if (file) {
1172 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1173 file, name);
1174 } else {
1175 snprintf(sql_filter, 1024, "function = '%s';", name);
1178 before = ptr_list_size((struct ptr_list *)ptr_names);
1180 run_sql(get_ptr_name, NULL,
1181 "select distinct ptr from function_ptr where %s",
1182 sql_filter);
1184 after = ptr_list_size((struct ptr_list *)ptr_names);
1185 if (before == after)
1186 return;
1188 while ((name = get_next_ptr_name()))
1189 get_ptr_names(NULL, name);
1192 static void match_data_from_db(struct symbol *sym)
1194 struct select_caller_info_data data = { .prev_func_id = -1 };
1195 struct sm_state *sm;
1196 struct stree *stree;
1197 struct timeval end_time;
1199 if (!sym || !sym->ident)
1200 return;
1202 set_fn_mtag(sym);
1203 gettimeofday(&data.start_time, NULL);
1205 __push_fake_cur_stree();
1206 __unnullify_path();
1208 if (!__inline_fn) {
1209 char *ptr;
1211 if (sym->ctype.modifiers & MOD_STATIC)
1212 get_ptr_names(get_base_file(), sym->ident->name);
1213 else
1214 get_ptr_names(NULL, sym->ident->name);
1216 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1217 __free_ptr_list((struct ptr_list **)&ptr_names);
1218 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1219 __free_fake_cur_stree();
1220 return;
1223 sql_select_caller_info(&data,
1224 "call_id, type, parameter, key, value",
1225 sym);
1228 stree = __pop_fake_cur_stree();
1229 if (!data.ignore)
1230 merge_stree(&data.final_states, stree);
1231 free_stree(&stree);
1232 __push_fake_cur_stree();
1233 __unnullify_path();
1234 data.prev_func_id = -1;
1235 data.ignore = 0;
1236 data.results = 0;
1238 FOR_EACH_PTR(ptr_names, ptr) {
1239 run_sql(caller_info_callback, &data,
1240 "select call_id, type, parameter, key, value"
1241 " from common_caller_info where function = '%s' order by call_id",
1242 ptr);
1243 } END_FOR_EACH_PTR(ptr);
1245 if (data.results) {
1246 FOR_EACH_PTR(ptr_names, ptr) {
1247 free_string(ptr);
1248 } END_FOR_EACH_PTR(ptr);
1249 goto free_ptr_names;
1252 FOR_EACH_PTR(ptr_names, ptr) {
1253 run_sql(caller_info_callback, &data,
1254 "select call_id, type, parameter, key, value"
1255 " from caller_info where function = '%s' order by call_id",
1256 ptr);
1257 free_string(ptr);
1258 } END_FOR_EACH_PTR(ptr);
1260 free_ptr_names:
1261 __free_ptr_list((struct ptr_list **)&ptr_names);
1262 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1263 } else {
1264 sql_select_caller_info(&data,
1265 "call_id, type, parameter, key, value",
1266 sym);
1269 stree = __pop_fake_cur_stree();
1270 if (!data.ignore)
1271 merge_stree(&data.final_states, stree);
1272 free_stree(&stree);
1274 gettimeofday(&end_time, NULL);
1275 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1276 FOR_EACH_SM(data.final_states, sm) {
1277 __set_sm(sm);
1278 } END_FOR_EACH_SM(sm);
1281 free_stree(&data.final_states);
1284 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1286 struct implies_info *info = _info;
1287 struct db_implies_callback *cb;
1288 struct expression *arg = NULL;
1289 int type;
1290 int param;
1292 if (argc != 5)
1293 return 0;
1295 type = atoi(argv[1]);
1296 param = atoi(argv[2]);
1298 FOR_EACH_PTR(info->cb_list, cb) {
1299 if (cb->type != type)
1300 continue;
1301 if (param != -1) {
1302 arg = get_argument_from_call_expr(info->expr->args, param);
1303 if (!arg)
1304 continue;
1306 cb->callback(info->expr, arg, argv[3], argv[4]);
1307 } END_FOR_EACH_PTR(cb);
1309 return 0;
1312 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1314 struct implies_info *info = _info;
1315 struct db_implies_callback *cb;
1316 struct expression *arg;
1317 struct symbol *sym;
1318 char *name;
1319 int type;
1320 int param;
1322 if (argc != 5)
1323 return 0;
1325 type = atoi(argv[1]);
1326 param = atoi(argv[2]);
1328 if (!get_param(param, &name, &sym))
1329 return 0;
1330 arg = symbol_expression(sym);
1331 if (!arg)
1332 return 0;
1334 FOR_EACH_PTR(info->cb_list, cb) {
1335 if (cb->type != type)
1336 continue;
1337 cb->callback(info->expr, arg, argv[3], argv[4]);
1338 } END_FOR_EACH_PTR(cb);
1340 return 0;
1343 static void match_return_implies(struct expression *expr)
1345 struct implies_info info = {
1346 .type = RETURN_IMPLIES,
1347 .cb_list = return_implies_cb_list,
1350 if (expr->fn->type != EXPR_SYMBOL ||
1351 !expr->fn->symbol)
1352 return;
1353 info.expr = expr;
1354 info.sym = expr->fn->symbol;
1355 sql_select_implies("function, type, parameter, key, value", &info,
1356 return_implies_callbacks);
1359 static void match_call_implies(struct symbol *sym)
1361 struct implies_info info = {
1362 .type = CALL_IMPLIES,
1363 .cb_list = call_implies_cb_list,
1366 if (!sym || !sym->ident)
1367 return;
1369 info.sym = sym;
1370 sql_select_implies("function, type, parameter, key, value", &info,
1371 call_implies_callbacks);
1374 static char *get_fn_param_str(struct expression *expr)
1376 struct expression *tmp;
1377 int param;
1378 char buf[32];
1380 tmp = get_assigned_expr(expr);
1381 if (tmp)
1382 expr = tmp;
1383 expr = strip_expr(expr);
1384 if (!expr || expr->type != EXPR_CALL)
1385 return NULL;
1386 expr = strip_expr(expr->fn);
1387 if (!expr || expr->type != EXPR_SYMBOL)
1388 return NULL;
1389 param = get_param_num(expr);
1390 if (param < 0)
1391 return NULL;
1393 snprintf(buf, sizeof(buf), "[r $%d]", param);
1394 return alloc_sname(buf);
1397 static char *get_return_compare_is_param(struct expression *expr)
1399 char *var;
1400 char buf[256];
1401 int comparison;
1402 int param;
1404 param = get_param_num(expr);
1405 if (param < 0)
1406 return NULL;
1408 var = expr_to_var(expr);
1409 if (!var)
1410 return NULL;
1411 snprintf(buf, sizeof(buf), "%s orig", var);
1412 comparison = get_comparison_strings(var, buf);
1413 free_string(var);
1415 if (!comparison)
1416 return NULL;
1418 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1419 return alloc_sname(buf);
1422 static char *get_return_compare_str(struct expression *expr)
1424 char *compare_str;
1426 compare_str = get_return_compare_is_param(expr);
1427 if (compare_str)
1428 return compare_str;
1430 compare_str = expr_lte_to_param(expr, -1);
1431 if (compare_str)
1432 return compare_str;
1434 return expr_param_comparison(expr, -1);
1437 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1439 struct range_list *rl;
1440 char *return_ranges;
1441 sval_t sval;
1442 char *fn_param_str;
1443 char *compare_str;
1444 char *math_str;
1445 char buf[128];
1447 *rl_p = NULL;
1449 if (!expr)
1450 return alloc_sname("");
1452 if (get_implied_value(expr, &sval)) {
1453 sval = sval_cast(cur_func_return_type(), sval);
1454 *rl_p = alloc_rl(sval, sval);
1455 return sval_to_str_or_err_ptr(sval);
1458 fn_param_str = get_fn_param_str(expr);
1459 compare_str = expr_equal_to_param(expr, -1);
1460 math_str = get_value_in_terms_of_parameter_math(expr);
1462 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1463 rl = cast_rl(cur_func_return_type(), rl);
1464 return_ranges = show_rl(rl);
1465 } else if (get_imaginary_absolute(expr, &rl)){
1466 rl = cast_rl(cur_func_return_type(), rl);
1467 return alloc_sname(show_rl(rl));
1468 } else {
1469 get_absolute_rl(expr, &rl);
1470 rl = cast_rl(cur_func_return_type(), rl);
1471 return_ranges = show_rl(rl);
1473 *rl_p = rl;
1475 if (fn_param_str) {
1476 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1477 return alloc_sname(buf);
1479 if (compare_str) {
1480 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1481 return alloc_sname(buf);
1483 if (math_str) {
1484 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1485 return alloc_sname(buf);
1487 compare_str = get_return_compare_str(expr);
1488 if (compare_str) {
1489 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1490 return alloc_sname(buf);
1493 return return_ranges;
1496 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1498 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1501 static bool call_return_state_hooks_conditional(struct expression *expr)
1503 int final_pass_orig = final_pass;
1504 static int recurse;
1506 if (recurse >= 2)
1507 return false;
1508 if (!expr ||
1509 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1510 return false;
1512 recurse++;
1514 __push_fake_cur_stree();
1516 final_pass = 0;
1517 __split_whole_condition(expr->conditional);
1518 final_pass = final_pass_orig;
1520 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1522 __push_true_states();
1523 __use_false_states();
1525 call_return_state_hooks(expr->cond_false);
1527 __merge_true_states();
1528 __free_fake_cur_stree();
1530 recurse--;
1531 return true;
1534 static void call_return_state_hooks_compare(struct expression *expr)
1536 struct returned_state_callback *cb;
1537 char *return_ranges;
1538 int final_pass_orig = final_pass;
1539 sval_t sval = { .type = &int_ctype };
1540 sval_t ret;
1542 if (!get_implied_value(expr, &ret))
1543 ret.value = -1;
1545 __push_fake_cur_stree();
1547 final_pass = 0;
1548 __split_whole_condition(expr);
1549 final_pass = final_pass_orig;
1551 if (ret.value != 0) {
1552 return_ranges = alloc_sname("1");
1553 sval.value = 1;
1554 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1556 return_id++;
1557 FOR_EACH_PTR(returned_state_callbacks, cb) {
1558 cb->callback(return_id, return_ranges, expr);
1559 } END_FOR_EACH_PTR(cb);
1562 __push_true_states();
1563 __use_false_states();
1565 if (ret.value != 1) {
1566 return_ranges = alloc_sname("0");
1567 sval.value = 0;
1568 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1570 return_id++;
1571 FOR_EACH_PTR(returned_state_callbacks, cb) {
1572 cb->callback(return_id, return_ranges, expr);
1573 } END_FOR_EACH_PTR(cb);
1576 __merge_true_states();
1577 __free_fake_cur_stree();
1580 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1582 struct sm_state *tmp;
1584 FOR_EACH_PTR(slist, tmp) {
1585 if (strcmp(tmp->state->name, sm->state->name) == 0)
1586 return 1;
1587 } END_FOR_EACH_PTR(tmp);
1589 return 0;
1592 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1594 struct returned_state_callback *cb;
1595 struct range_list *rl;
1596 char *return_ranges;
1597 struct sm_state *tmp;
1598 int ret = 0;
1599 int nr_possible, nr_states;
1600 char *compare_str;
1601 char buf[128];
1602 struct state_list *already_handled = NULL;
1603 sval_t sval;
1605 if (!sm || !sm->merged)
1606 return 0;
1608 if (too_many_possible(sm))
1609 return 0;
1611 /* bail if it gets too complicated */
1612 nr_possible = 0;
1613 FOR_EACH_PTR(sm->possible, tmp) {
1614 if (tmp->merged)
1615 continue;
1616 if (ptr_in_list(tmp, already_handled))
1617 continue;
1618 add_ptr_list(&already_handled, tmp);
1619 nr_possible++;
1620 } END_FOR_EACH_PTR(tmp);
1621 free_slist(&already_handled);
1622 nr_states = get_db_state_count();
1623 if (nr_states * nr_possible >= 2000)
1624 return 0;
1626 FOR_EACH_PTR(sm->possible, tmp) {
1627 if (tmp->merged)
1628 continue;
1629 if (ptr_in_list(tmp, already_handled))
1630 continue;
1631 add_ptr_list(&already_handled, tmp);
1633 ret = 1;
1634 __push_fake_cur_stree();
1636 overwrite_states_using_pool(sm, tmp);
1638 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1639 return_ranges = show_rl(rl);
1640 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1641 compare_str = get_return_compare_str(expr);
1642 /* ignore obvious stuff like 0 <= param */
1643 /* Is this worthile when we have PARAM_COMPARE? */
1644 if (compare_str &&
1645 strncmp(compare_str, "[=", 2) != 0 &&
1646 rl_to_sval(rl, &sval))
1647 compare_str = NULL;
1648 if (compare_str) {
1649 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1650 return_ranges = alloc_sname(buf);
1653 return_id++;
1654 FOR_EACH_PTR(returned_state_callbacks, cb) {
1655 cb->callback(return_id, return_ranges, expr);
1656 } END_FOR_EACH_PTR(cb);
1658 __free_fake_cur_stree();
1659 } END_FOR_EACH_PTR(tmp);
1661 free_slist(&already_handled);
1663 return ret;
1666 static int call_return_state_hooks_split_possible(struct expression *expr)
1668 struct expression *fake;
1669 struct sm_state *sm;
1671 if (!expr)
1672 return 0;
1674 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1675 if (!sm) {
1676 fake = expr_get_fake_parent_expr(expr);
1677 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1678 return 0;
1679 fake = fake->left;
1680 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1682 return split_possible_helper(sm, expr);
1685 static bool has_possible_negative(struct sm_state *sm)
1687 struct sm_state *tmp;
1689 if (!type_signed(estate_type(sm->state)))
1690 return false;
1692 FOR_EACH_PTR(sm->possible, tmp) {
1693 if (!estate_rl(tmp->state))
1694 continue;
1695 if (sval_is_negative(estate_min(tmp->state)) &&
1696 sval_is_negative(estate_max(tmp->state)))
1697 return true;
1698 } END_FOR_EACH_PTR(tmp);
1700 return false;
1703 static bool has_separate_zero_null(struct sm_state *sm)
1705 struct sm_state *tmp;
1706 sval_t sval;
1708 FOR_EACH_PTR(sm->possible, tmp) {
1709 if (!estate_get_single_value(tmp->state, &sval))
1710 continue;
1711 if (sval.value == 0)
1712 return true;
1713 } END_FOR_EACH_PTR(tmp);
1715 return false;
1718 static int split_positive_from_negative(struct expression *expr)
1720 struct sm_state *sm;
1721 struct returned_state_callback *cb;
1722 struct range_list *rl;
1723 const char *return_ranges;
1724 struct range_list *ret_rl;
1725 bool separate_zero;
1726 int undo;
1728 /* We're going to print the states 3 times */
1729 if (get_db_state_count() > 10000 / 3)
1730 return 0;
1732 if (!get_implied_rl(expr, &rl) || !rl)
1733 return 0;
1734 /* Forget about INT_MAX and larger */
1735 if (rl_max(rl).value <= 0)
1736 return 0;
1737 if (!sval_is_negative(rl_min(rl)))
1738 return 0;
1740 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1741 if (!sm)
1742 return 0;
1743 if (!has_possible_negative(sm))
1744 return 0;
1745 separate_zero = has_separate_zero_null(sm);
1747 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1748 return 0;
1750 return_id++;
1751 return_ranges = get_return_ranges_str(expr, &ret_rl);
1752 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1753 FOR_EACH_PTR(returned_state_callbacks, cb) {
1754 cb->callback(return_id, (char *)return_ranges, expr);
1755 } END_FOR_EACH_PTR(cb);
1757 end_assume();
1759 if (separate_zero) {
1760 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1762 return_id++;
1763 return_ranges = get_return_ranges_str(expr, &ret_rl);
1764 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1765 FOR_EACH_PTR(returned_state_callbacks, cb) {
1766 cb->callback(return_id, (char *)return_ranges, expr);
1767 } END_FOR_EACH_PTR(cb);
1769 if (undo)
1770 end_assume();
1773 undo = assume(compare_expression(expr, '<', zero_expr()));
1775 return_id++;
1776 return_ranges = get_return_ranges_str(expr, &ret_rl);
1777 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1778 FOR_EACH_PTR(returned_state_callbacks, cb) {
1779 cb->callback(return_id, (char *)return_ranges, expr);
1780 } END_FOR_EACH_PTR(cb);
1782 if (undo)
1783 end_assume();
1785 return 1;
1788 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1790 struct returned_state_callback *cb;
1791 struct range_list *rl;
1792 struct range_list *nonnull_rl;
1793 sval_t null_sval;
1794 struct range_list *null_rl = NULL;
1795 char *return_ranges;
1796 struct sm_state *sm;
1797 struct smatch_state *state;
1798 int nr_states;
1799 int final_pass_orig = final_pass;
1801 if (!expr || expr_equal_to_param(expr, -1))
1802 return 0;
1803 if (expr->type == EXPR_CALL)
1804 return 0;
1806 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1807 if (!sm)
1808 return 0;
1809 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1810 return 0;
1811 state = sm->state;
1812 if (!estate_rl(state))
1813 return 0;
1814 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1815 return 0;
1816 if (has_possible_negative(sm))
1817 return 0;
1818 if (!has_separate_zero_null(sm))
1819 return 0;
1821 nr_states = get_db_state_count();
1822 if (option_info && nr_states >= 1500)
1823 return 0;
1825 rl = estate_rl(state);
1827 __push_fake_cur_stree();
1829 final_pass = 0;
1830 __split_whole_condition(expr);
1831 final_pass = final_pass_orig;
1833 nonnull_rl = rl_filter(rl, rl_zero());
1834 return_ranges = show_rl(nonnull_rl);
1835 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1837 return_id++;
1838 FOR_EACH_PTR(returned_state_callbacks, cb) {
1839 cb->callback(return_id, return_ranges, expr);
1840 } END_FOR_EACH_PTR(cb);
1842 __push_true_states();
1843 __use_false_states();
1845 return_ranges = alloc_sname("0");
1846 null_sval = sval_type_val(rl_type(rl), 0);
1847 add_range(&null_rl, null_sval, null_sval);
1848 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1849 return_id++;
1850 FOR_EACH_PTR(returned_state_callbacks, cb) {
1851 cb->callback(return_id, return_ranges, expr);
1852 } END_FOR_EACH_PTR(cb);
1854 __merge_true_states();
1855 __free_fake_cur_stree();
1857 return 1;
1860 static bool is_kernel_success_fail(struct sm_state *sm)
1862 struct sm_state *tmp;
1863 struct range_list *rl;
1864 bool has_zero = false;
1865 bool has_neg = false;
1867 if (!type_signed(estate_type(sm->state)))
1868 return false;
1870 FOR_EACH_PTR(sm->possible, tmp) {
1871 rl = estate_rl(tmp->state);
1872 if (!rl)
1873 return false;
1874 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1875 has_zero = true;
1876 continue;
1878 has_neg = true;
1879 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1880 continue;
1881 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1882 continue;
1883 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1884 continue;
1885 return false;
1886 } END_FOR_EACH_PTR(tmp);
1888 return has_zero && has_neg;
1891 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1893 struct sm_state *sm;
1894 struct range_list *rl;
1895 struct range_list *nonzero_rl;
1896 sval_t zero_sval;
1897 struct range_list *zero_rl = NULL;
1898 int nr_states;
1899 struct returned_state_callback *cb;
1900 char *return_ranges;
1901 int final_pass_orig = final_pass;
1903 if (option_project != PROJ_KERNEL)
1904 return 0;
1906 nr_states = get_db_state_count();
1907 if (nr_states > 2000)
1908 return 0;
1910 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1911 if (!sm)
1912 return 0;
1913 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1914 return 0;
1915 if (!is_kernel_success_fail(sm))
1916 return 0;
1918 rl = estate_rl(sm->state);
1919 if (!rl)
1920 return 0;
1922 __push_fake_cur_stree();
1924 final_pass = 0;
1925 __split_whole_condition(expr);
1926 final_pass = final_pass_orig;
1928 nonzero_rl = rl_filter(rl, rl_zero());
1929 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1930 return_ranges = show_rl(nonzero_rl);
1931 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1933 return_id++;
1934 FOR_EACH_PTR(returned_state_callbacks, cb) {
1935 cb->callback(return_id, return_ranges, expr);
1936 } END_FOR_EACH_PTR(cb);
1938 __push_true_states();
1939 __use_false_states();
1941 return_ranges = alloc_sname("0");
1942 zero_sval = sval_type_val(rl_type(rl), 0);
1943 add_range(&zero_rl, zero_sval, zero_sval);
1944 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1945 return_id++;
1946 FOR_EACH_PTR(returned_state_callbacks, cb) {
1947 cb->callback(return_id, return_ranges, expr);
1948 } END_FOR_EACH_PTR(cb);
1950 __merge_true_states();
1951 __free_fake_cur_stree();
1953 return 1;
1956 static int is_boolean(struct expression *expr)
1958 struct range_list *rl;
1960 if (!get_implied_rl(expr, &rl))
1961 return 0;
1962 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1963 return 1;
1964 return 0;
1967 static int splitable_function_call(struct expression *expr)
1969 struct sm_state *sm;
1971 if (!expr || expr->type != EXPR_CALL)
1972 return 0;
1973 sm = get_extra_sm_state(expr);
1974 return split_possible_helper(sm, expr);
1977 static struct sm_state *find_bool_param(void)
1979 struct stree *start_states;
1980 struct symbol *arg;
1981 struct sm_state *sm, *tmp;
1982 sval_t sval;
1984 start_states = get_start_states();
1986 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1987 if (!arg->ident)
1988 continue;
1989 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1990 if (!sm)
1991 continue;
1992 if (rl_min(estate_rl(sm->state)).value != 0 ||
1993 rl_max(estate_rl(sm->state)).value != 1)
1994 continue;
1995 goto found;
1996 } END_FOR_EACH_PTR_REVERSE(arg);
1998 return NULL;
2000 found:
2002 * Check if it's splitable. If not, then splitting it up is likely not
2003 * useful for the callers.
2005 FOR_EACH_PTR(sm->possible, tmp) {
2006 if (is_merged(tmp))
2007 continue;
2008 if (!estate_get_single_value(tmp->state, &sval))
2009 return NULL;
2010 } END_FOR_EACH_PTR(tmp);
2012 return sm;
2015 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2017 struct returned_state_callback *cb;
2018 struct range_list *ret_rl;
2019 const char *return_ranges;
2020 struct sm_state *tmp;
2021 int ret = 0;
2022 struct state_list *already_handled = NULL;
2024 if (!sm || !sm->merged)
2025 return 0;
2027 if (too_many_possible(sm))
2028 return 0;
2030 FOR_EACH_PTR(sm->possible, tmp) {
2031 if (tmp->merged)
2032 continue;
2033 if (ptr_in_list(tmp, already_handled))
2034 continue;
2035 add_ptr_list(&already_handled, tmp);
2037 ret = 1;
2038 __push_fake_cur_stree();
2040 overwrite_states_using_pool(sm, tmp);
2042 return_ranges = get_return_ranges_str(expr, &ret_rl);
2043 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2044 return_id++;
2045 FOR_EACH_PTR(returned_state_callbacks, cb) {
2046 cb->callback(return_id, (char *)return_ranges, expr);
2047 } END_FOR_EACH_PTR(cb);
2049 __free_fake_cur_stree();
2050 } END_FOR_EACH_PTR(tmp);
2052 free_slist(&already_handled);
2054 return ret;
2057 static int split_by_bool_param(struct expression *expr)
2059 struct sm_state *start_sm, *sm;
2060 sval_t sval;
2062 start_sm = find_bool_param();
2063 if (!start_sm)
2064 return 0;
2065 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2066 if (!sm || estate_get_single_value(sm->state, &sval))
2067 return 0;
2069 if (get_db_state_count() * 2 >= 2000)
2070 return 0;
2072 return split_on_bool_sm(sm, expr);
2075 static int split_by_null_nonnull_param(struct expression *expr)
2077 struct symbol *arg;
2078 struct sm_state *sm;
2079 int nr_possible;
2081 /* function must only take one pointer */
2082 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2083 return 0;
2084 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2085 if (!arg->ident)
2086 return 0;
2087 if (get_real_base_type(arg)->type != SYM_PTR)
2088 return 0;
2090 if (param_was_set_var_sym(arg->ident->name, arg))
2091 return 0;
2092 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2093 if (!sm)
2094 return 0;
2096 if (!has_separate_zero_null(sm))
2097 return 0;
2099 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2100 if (get_db_state_count() * nr_possible >= 2000)
2101 return 0;
2103 return split_on_bool_sm(sm, expr);
2106 struct expression *strip_expr_statement(struct expression *expr)
2108 struct expression *orig = expr;
2109 struct statement *stmt, *last_stmt;
2111 if (!expr)
2112 return NULL;
2113 if (expr->type == EXPR_PREOP && expr->op == '(')
2114 expr = expr->unop;
2115 if (expr->type != EXPR_STATEMENT)
2116 return orig;
2117 stmt = expr->statement;
2118 if (!stmt || stmt->type != STMT_COMPOUND)
2119 return orig;
2121 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2122 if (!last_stmt || last_stmt->type == STMT_LABEL)
2123 last_stmt = last_stmt->label_statement;
2124 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2125 return orig;
2126 return strip_expr(last_stmt->expression);
2129 static bool is_kernel_error_path(struct expression *expr)
2131 struct range_list *rl;
2134 * Splitting up returns requires resources. It also requires resources
2135 * for the caller. It doesn't seem worth it to split anything up.
2137 if (!get_implied_rl(expr, &rl))
2138 return false;
2139 if (rl_type(rl) != &int_ctype)
2140 return false;
2141 if (rl_min(rl).value >= -4095 &&
2142 rl_max(rl).value < 0)
2143 return true;
2144 return false;
2147 static void call_return_state_hooks(struct expression *expr)
2149 struct returned_state_callback *cb;
2150 struct range_list *ret_rl;
2151 const char *return_ranges;
2152 int nr_states;
2153 sval_t sval;
2155 if (__path_is_null())
2156 return;
2158 expr = strip_expr(expr);
2159 expr = strip_expr_statement(expr);
2161 if (is_impossible_path())
2162 goto vanilla;
2164 if (expr && (expr->type == EXPR_COMPARE ||
2165 !get_implied_value(expr, &sval)) &&
2166 (is_condition(expr) || is_boolean(expr))) {
2167 call_return_state_hooks_compare(expr);
2168 return;
2169 } else if (call_return_state_hooks_conditional(expr)) {
2170 return;
2171 } else if (is_kernel_error_path(expr)) {
2172 goto vanilla;
2173 } else if (call_return_state_hooks_split_possible(expr)) {
2174 return;
2175 } else if (split_positive_from_negative(expr)) {
2176 return;
2177 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2178 return;
2179 } else if (call_return_state_hooks_split_success_fail(expr)) {
2180 return;
2181 } else if (splitable_function_call(expr)) {
2182 return;
2183 } else if (split_by_bool_param(expr)) {
2184 } else if (split_by_null_nonnull_param(expr)) {
2185 return;
2188 vanilla:
2189 return_ranges = get_return_ranges_str(expr, &ret_rl);
2190 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2192 return_id++;
2193 nr_states = get_db_state_count();
2194 if (nr_states >= 10000) {
2195 match_return_info(return_id, (char *)return_ranges, expr);
2196 print_limited_param_set(return_id, (char *)return_ranges, expr);
2197 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2198 return;
2200 FOR_EACH_PTR(returned_state_callbacks, cb) {
2201 cb->callback(return_id, (char *)return_ranges, expr);
2202 } END_FOR_EACH_PTR(cb);
2205 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2207 struct returned_member_callback *cb;
2208 struct sm_state *sm;
2209 struct symbol *type;
2210 char *name;
2211 char member_name[256];
2212 int len;
2214 type = get_type(expr);
2215 if (!type || type->type != SYM_PTR)
2216 return;
2217 name = expr_to_var(expr);
2218 if (!name)
2219 return;
2221 len = strlen(name);
2222 FOR_EACH_PTR(returned_member_callbacks, cb) {
2223 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2224 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2225 strcpy(member_name, "*$");
2226 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2227 continue;
2229 if (strncmp(sm->name, name, len) != 0)
2230 continue;
2231 if (strncmp(sm->name + len, "->", 2) != 0)
2232 continue;
2233 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2234 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2235 } END_FOR_EACH_SM(sm);
2236 } END_FOR_EACH_PTR(cb);
2238 free_string(name);
2241 static void print_return_struct_info(int return_id, char *return_ranges,
2242 struct expression *expr,
2243 struct symbol *sym,
2244 struct return_info_callback *cb)
2246 struct sm_state *sm;
2247 const char *printed_name;
2248 int param;
2250 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2251 if (sm->sym && sm->sym == sym) {
2252 param = -1;
2253 } else {
2254 param = get_param_num_from_sym(sm->sym);
2255 if (param < 0)
2256 continue;
2259 printed_name = get_param_name(sm);
2260 if (!printed_name)
2261 continue;
2263 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2264 } END_FOR_EACH_SM(sm);
2267 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2269 struct return_info_callback *cb;
2270 struct expression *tmp;
2271 struct symbol *sym;
2273 tmp = get_fake_variable(expr);
2274 if (tmp)
2275 expr = tmp;
2276 sym = expr_to_sym(expr);
2278 FOR_EACH_PTR(return_callbacks, cb) {
2279 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2280 } END_FOR_EACH_PTR(cb);
2283 static void reset_memdb(struct symbol *sym)
2285 mem_sql(NULL, NULL, "delete from caller_info;");
2286 mem_sql(NULL, NULL, "delete from return_states;");
2287 mem_sql(NULL, NULL, "delete from call_implies;");
2288 mem_sql(NULL, NULL, "delete from return_implies;");
2291 static void match_end_func_info(struct symbol *sym)
2293 if (__path_is_null())
2294 return;
2295 call_return_state_hooks(NULL);
2298 static void match_after_func(struct symbol *sym)
2300 if (!__inline_fn)
2301 reset_memdb(sym);
2304 static void init_memdb(void)
2306 char *err = NULL;
2307 int rc;
2308 const char *schema_files[] = {
2309 "db/db.schema",
2310 "db/caller_info.schema",
2311 "db/common_caller_info.schema",
2312 "db/return_states.schema",
2313 "db/function_type_size.schema",
2314 "db/type_size.schema",
2315 "db/function_type_info.schema",
2316 "db/type_info.schema",
2317 "db/call_implies.schema",
2318 "db/return_implies.schema",
2319 "db/function_ptr.schema",
2320 "db/local_values.schema",
2321 "db/function_type_value.schema",
2322 "db/type_value.schema",
2323 "db/function_type.schema",
2324 "db/data_info.schema",
2325 "db/parameter_name.schema",
2326 "db/constraints.schema",
2327 "db/constraints_required.schema",
2328 "db/fn_ptr_data_link.schema",
2329 "db/fn_data_link.schema",
2330 "db/mtag_about.schema",
2331 "db/mtag_info.schema",
2332 "db/mtag_map.schema",
2333 "db/mtag_data.schema",
2334 "db/mtag_alias.schema",
2336 static char buf[4096];
2337 int fd;
2338 int ret;
2339 int i;
2341 rc = sqlite3_open(":memory:", &mem_db);
2342 if (rc != SQLITE_OK) {
2343 sm_ierror("starting In-Memory database.");
2344 return;
2347 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2348 fd = open_schema_file(schema_files[i]);
2349 if (fd < 0)
2350 continue;
2351 ret = read(fd, buf, sizeof(buf));
2352 if (ret < 0) {
2353 sm_ierror("failed to read: %s", schema_files[i]);
2354 continue;
2356 close(fd);
2357 if (ret == sizeof(buf)) {
2358 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2359 schema_files[i], sizeof(buf));
2360 continue;
2362 buf[ret] = '\0';
2363 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2364 if (rc != SQLITE_OK) {
2365 sm_ierror("SQL error #2: %s", err);
2366 sm_ierror("%s", buf);
2371 static void init_cachedb(void)
2373 char *err = NULL;
2374 int rc;
2375 const char *schema_files[] = {
2376 "db/call_implies.schema",
2377 "db/return_implies.schema",
2378 "db/type_info.schema",
2379 "db/mtag_about.schema",
2380 "db/mtag_data.schema",
2381 "db/mtag_info.schema",
2382 "db/sink_info.schema",
2384 static char buf[4096];
2385 int fd;
2386 int ret;
2387 int i;
2389 rc = sqlite3_open(":memory:", &cache_db);
2390 if (rc != SQLITE_OK) {
2391 sm_ierror("starting In-Memory database.");
2392 return;
2395 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2396 fd = open_schema_file(schema_files[i]);
2397 if (fd < 0)
2398 continue;
2399 ret = read(fd, buf, sizeof(buf));
2400 if (ret < 0) {
2401 sm_ierror("failed to read: %s", schema_files[i]);
2402 continue;
2404 close(fd);
2405 if (ret == sizeof(buf)) {
2406 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2407 schema_files[i], sizeof(buf));
2408 continue;
2410 buf[ret] = '\0';
2411 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2412 if (rc != SQLITE_OK) {
2413 sm_ierror("SQL error #2: %s", err);
2414 sm_ierror("%s", buf);
2419 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2421 static char buf[4096];
2422 char tmp[256];
2423 char *p = buf;
2424 char *table = _table;
2425 int i;
2428 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2429 for (i = 0; i < argc; i++) {
2430 if (i)
2431 p += snprintf(p, 4096 - (p - buf), ", ");
2432 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2433 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2436 p += snprintf(p, 4096 - (p - buf), ");");
2437 if (p - buf > 4096)
2438 return 0;
2440 sm_msg("SQL: %s", buf);
2441 return 0;
2444 static void dump_cache(struct symbol_list *sym_list)
2446 const char *cache_tables[] = {
2447 "type_info", "return_implies", "call_implies", "mtag_data",
2448 "mtag_info", "mtag_about", "sink_info",
2450 char buf[64];
2451 int i;
2453 if (!option_info)
2454 return;
2456 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2457 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2458 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2462 void open_smatch_db(char *db_file)
2464 int rc;
2466 if (option_no_db)
2467 return;
2469 use_states = malloc(num_checks + 1);
2470 memset(use_states, 0xff, num_checks + 1);
2472 init_memdb();
2473 init_cachedb();
2475 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2476 if (rc != SQLITE_OK) {
2477 option_no_db = 1;
2478 return;
2480 run_sql(NULL, NULL,
2481 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2482 return;
2485 static void register_common_funcs(void)
2487 struct token *token;
2488 char *func;
2489 char filename[256];
2491 if (option_project == PROJ_NONE)
2492 strcpy(filename, "common_functions");
2493 else
2494 snprintf(filename, 256, "%s.common_functions", option_project_str);
2496 token = get_tokens_file(filename);
2497 if (!token)
2498 return;
2499 if (token_type(token) != TOKEN_STREAMBEGIN)
2500 return;
2501 token = token->next;
2502 while (token_type(token) != TOKEN_STREAMEND) {
2503 if (token_type(token) != TOKEN_IDENT)
2504 return;
2505 func = alloc_string(show_ident(token->ident));
2506 add_ptr_list(&common_funcs, func);
2507 token = token->next;
2509 clear_token_alloc();
2512 static char *get_next_string(char **str)
2514 static char string[256];
2515 char *start;
2516 char *p = *str;
2517 int len, i, j;
2519 if (*p == '\0')
2520 return NULL;
2521 start = p;
2523 while (*p != '\0' && *p != '\n') {
2524 if (*p == '\\' && *(p + 1) == ' ') {
2525 p += 2;
2526 continue;
2528 if (*p == ' ')
2529 break;
2530 p++;
2533 len = p - start;
2534 if (len >= sizeof(string)) {
2535 memcpy(string, start, sizeof(string));
2536 string[sizeof(string) - 1] = '\0';
2537 sm_ierror("return_fix: '%s' too long", string);
2538 **str = '\0';
2539 return NULL;
2541 memcpy(string, start, len);
2542 string[len] = '\0';
2543 for (i = 0; i < sizeof(string) - 1; i++) {
2544 if (string[i] == '\\' && string[i + 1] == ' ') {
2545 for (j = i; string[j] != '\0'; j++)
2546 string[j] = string[j + 1];
2549 if (*p != '\0')
2550 p++;
2551 *str = p;
2552 return string;
2555 static void register_return_replacements(void)
2557 char *func, *orig, *new;
2558 char filename[256];
2559 char buf[4096];
2560 int fd, ret, i;
2561 char *p;
2563 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2564 fd = open_schema_file(filename);
2565 if (fd < 0)
2566 return;
2567 ret = read(fd, buf, sizeof(buf));
2568 close(fd);
2569 if (ret < 0)
2570 return;
2571 if (ret == sizeof(buf)) {
2572 sm_ierror("file too large: %s (limit %zd bytes)",
2573 filename, sizeof(buf));
2574 return;
2576 buf[ret] = '\0';
2578 p = buf;
2579 while (*p) {
2580 get_next_string(&p);
2581 replace_count++;
2583 if (replace_count == 0 || replace_count % 3 != 0) {
2584 replace_count = 0;
2585 return;
2587 replace_table = malloc(replace_count * sizeof(char *));
2589 p = buf;
2590 i = 0;
2591 while (*p) {
2592 func = alloc_string(get_next_string(&p));
2593 orig = alloc_string(get_next_string(&p));
2594 new = alloc_string(get_next_string(&p));
2596 replace_table[i++] = func;
2597 replace_table[i++] = orig;
2598 replace_table[i++] = new;
2602 void register_definition_db_callbacks(int id)
2604 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2605 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2606 add_split_return_callback(match_return_info);
2607 add_split_return_callback(print_returned_struct_members);
2608 add_split_return_callback(print_return_info);
2609 add_hook(&call_return_state_hooks, RETURN_HOOK);
2610 add_hook(&match_end_func_info, END_FUNC_HOOK);
2611 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2613 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2614 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2615 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2617 register_common_funcs();
2618 register_return_replacements();
2620 add_hook(&dump_cache, END_FILE_HOOK);
2623 void register_db_call_marker(int id)
2625 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2628 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2630 struct expression *arg;
2631 char *name = NULL;
2632 char member_name[256];
2634 *sym = NULL;
2636 if (param == -1) {
2637 const char *star = "";
2639 if (expr->type != EXPR_ASSIGNMENT)
2640 return NULL;
2641 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2642 return NULL;
2643 name = expr_to_var_sym(expr->left, sym);
2644 if (!name)
2645 return NULL;
2646 if (key[0] == '*') {
2647 star = "*";
2648 key++;
2650 if (strncmp(key, "$", 1) != 0)
2651 return name;
2652 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2653 free_string(name);
2654 return alloc_string(member_name);
2657 while (expr->type == EXPR_ASSIGNMENT)
2658 expr = strip_expr(expr->right);
2659 if (expr->type != EXPR_CALL)
2660 return NULL;
2662 arg = get_argument_from_call_expr(expr->args, param);
2663 if (!arg)
2664 return NULL;
2666 return get_variable_from_key(arg, key, sym);
2669 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2671 struct symbol *type;
2672 char buf[256];
2673 char *tmp;
2674 int star_cnt = 0;
2675 bool add_dot = false;
2677 if (!arg)
2678 return NULL;
2680 arg = strip_expr(arg);
2682 if (strcmp(key, "$") == 0)
2683 return expr_to_var_sym(arg, sym);
2685 if (strcmp(key, "*$") == 0) {
2686 if (arg->type == EXPR_PREOP && arg->op == '&') {
2687 arg = strip_expr(arg->unop);
2688 return expr_to_var_sym(arg, sym);
2689 } else {
2690 tmp = expr_to_var_sym(arg, sym);
2691 if (!tmp)
2692 return NULL;
2693 snprintf(buf, sizeof(buf), "*%s", tmp);
2694 free_string(tmp);
2695 return alloc_string(buf);
2699 if (strncmp(key, "(*$)", 4) == 0) {
2700 char buf[64];
2702 if (arg->type == EXPR_PREOP && arg->op == '&') {
2703 arg = strip_expr(arg->unop);
2704 snprintf(buf, sizeof(buf), "$%s", key + 4);
2705 return get_variable_from_key(arg, buf, sym);
2706 } else {
2707 tmp = expr_to_var_sym(arg, sym);
2708 if (!tmp)
2709 return NULL;
2710 snprintf(buf, sizeof(buf), "(*%s)%s", tmp, key + 4);
2711 free_string(tmp);
2712 return alloc_string(buf);
2716 while (key[0] == '*') {
2717 star_cnt++;
2718 key++;
2722 * FIXME: This is a hack.
2723 * We should be able to parse expressions like (*$)->foo and *$->foo.
2725 type = get_type(arg);
2726 if (is_struct_ptr(type))
2727 add_dot = true;
2729 if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt && !add_dot) {
2730 arg = strip_expr(arg->unop);
2731 star_cnt--;
2734 if (arg->type == EXPR_PREOP && arg->op == '&') {
2735 arg = strip_expr(arg->unop);
2736 tmp = expr_to_var_sym(arg, sym);
2737 if (!tmp)
2738 return NULL;
2739 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2740 star_cnt, "**********", tmp, key + 3);
2741 return alloc_string(buf);
2744 tmp = expr_to_var_sym(arg, sym);
2745 if (!tmp)
2746 return NULL;
2747 snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2748 free_string(tmp);
2749 return alloc_string(buf);
2752 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2754 *vsl = NULL;
2756 if (strcmp("$", key) == 0)
2757 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2758 return get_variable_from_key(arg, key, sym);
2761 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2763 int star_cnt = 0;
2764 int name_len;
2765 char buf[256];
2767 name_len = strlen(param_name);
2769 while (state_name[0] == '*') {
2770 star_cnt++;
2771 state_name++;
2774 /* ten out of ten stars! */
2775 if (star_cnt > 10)
2776 return NULL;
2778 if (strncmp(state_name, "(*", 2) == 0 &&
2779 strncmp(state_name + 2, param_name, name_len) == 0 &&
2780 state_name[name_len + 2] == ')') {
2781 snprintf(buf, sizeof(buf), "%.*s(*$)%s", star_cnt, "**********",
2782 state_name + name_len + 3);
2783 return alloc_sname(buf);
2786 if (strcmp(state_name, param_name) == 0) {
2787 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2788 return alloc_sname(buf);
2791 /* check for '-' from "->" */
2792 if (strncmp(state_name, param_name, name_len) == 0 &&
2793 state_name[name_len] == '-') {
2794 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2795 return alloc_sname(buf);
2797 return NULL;
2800 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2802 if (!sym || !sym->ident)
2803 return NULL;
2805 return state_name_to_param_name(name, sym->ident->name);
2808 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2810 struct symbol *type;
2811 const char *sym_name;
2812 int name_len;
2813 static char buf[256];
2816 * mtag_name is different from param_name because mtags can be a struct
2817 * instead of a struct pointer. But we want to treat it like a pointer
2818 * because really an mtag is a pointer. Or in other words, if you pass
2819 * a struct foo then you want to talk about foo.bar but with an mtag
2820 * you want to refer to it as foo->bar.
2824 if (!sym || !sym->ident)
2825 return NULL;
2827 type = get_real_base_type(sym);
2828 if (type && type->type == SYM_BASETYPE)
2829 return "*$";
2831 sym_name = sym->ident->name;
2832 name_len = strlen(sym_name);
2834 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2835 strncmp(state_name, sym_name, name_len) == 0) {
2836 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2837 return buf;
2840 return state_name_to_param_name(state_name, sym_name);
2843 const char *get_mtag_name_expr(struct expression *expr)
2845 char *name;
2846 struct symbol *sym;
2847 const char *ret = NULL;
2849 name = expr_to_var_sym(expr, &sym);
2850 if (!name || !sym)
2851 goto free;
2853 ret = get_mtag_name_var_sym(name, sym);
2854 free:
2855 free_string(name);
2856 return ret;
2859 const char *get_param_name(struct sm_state *sm)
2861 return get_param_name_var_sym(sm->name, sm->sym);
2864 char *get_data_info_name(struct expression *expr)
2866 struct symbol *sym;
2867 char *name;
2868 char buf[256];
2869 char *ret = NULL;
2871 expr = strip_expr(expr);
2872 name = get_member_name(expr);
2873 if (name)
2874 return name;
2875 name = expr_to_var_sym(expr, &sym);
2876 if (!name || !sym)
2877 goto free;
2878 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2879 goto free;
2880 if (sym->ctype.modifiers & MOD_STATIC)
2881 snprintf(buf, sizeof(buf), "static %s", name);
2882 else
2883 snprintf(buf, sizeof(buf), "global %s", name);
2884 ret = alloc_sname(buf);
2885 free:
2886 free_string(name);
2887 return ret;