db: don't load silly amounts of caller info
[smatch.git] / smatch_db.c
blobec37ee1669616882493f987b069ba2dac1169c15
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 STATE(incomplete);
33 static int my_id;
35 static int return_id;
37 static void call_return_state_hooks(struct expression *expr);
38 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr);
40 #define SQLITE_CACHE_PAGES 1000
42 struct def_callback {
43 int hook_type;
44 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
46 ALLOCATOR(def_callback, "definition db hook callbacks");
47 DECLARE_PTR_LIST(callback_list, struct def_callback);
48 static struct callback_list *select_caller_info_callbacks;
50 struct def_name_sym_callback {
51 int hook_type;
52 void (*callback)(const char *name, struct symbol *sym, char *value);
54 ALLOCATOR(def_name_sym_callback, "definition db hook callbacks");
55 DECLARE_PTR_LIST(name_sym_callback_list, struct def_name_sym_callback);
56 static struct name_sym_callback_list *select_caller_name_sym_callbacks;
58 struct member_info_callback {
59 int owner;
60 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
62 ALLOCATOR(member_info_callback, "caller_info callbacks");
63 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
64 static struct member_info_cb_list *member_callbacks;
65 static struct member_info_cb_list *member_callbacks_new;
67 struct return_info_callback {
68 int owner;
69 void (*callback)(int return_id, char *return_ranges,
70 struct expression *returned_expr,
71 int param,
72 const char *printed_name,
73 struct sm_state *sm);
75 ALLOCATOR(return_info_callback, "return_info callbacks");
76 DECLARE_PTR_LIST(return_info_cb_list, struct return_info_callback);
77 static struct return_info_cb_list *return_callbacks;
79 struct returned_state_callback {
80 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
82 ALLOCATOR(returned_state_callback, "returned state callbacks");
83 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
84 static struct returned_state_cb_list *returned_state_callbacks;
86 struct returned_member_callback {
87 int owner;
88 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
90 ALLOCATOR(returned_member_callback, "returned member callbacks");
91 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
92 static struct returned_member_cb_list *returned_member_callbacks;
94 struct db_implies_callback {
95 int type;
96 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
98 ALLOCATOR(db_implies_callback, "return_implies callbacks");
99 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
100 static struct db_implies_cb_list *return_implies_cb_list_early;
101 static struct db_implies_cb_list *return_implies_cb_list_late;
102 static struct db_implies_cb_list *call_implies_cb_list;
104 DECLARE_PTR_LIST(delete_list, delete_hook);
105 static struct delete_list *delete_hooks;
107 struct split_data {
108 const char *func, *rl;
110 static struct split_data **forced_splits;
111 static int split_count;
113 /* silently truncates if needed. */
114 char *escape_newlines(const char *str)
116 char buf[1024] = "";
117 bool found = false;
118 int i, j;
120 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
121 if (str[i] != '\r' && str[i] != '\n') {
122 buf[j] = str[i];
123 continue;
126 found = true;
127 buf[j++] = '\\';
128 if (j == sizeof(buf))
129 break;
130 buf[j] = 'n';
133 if (!found)
134 return alloc_sname(str);
136 if (j == sizeof(buf))
137 buf[j - 1] = '\0';
138 return alloc_sname(buf);
141 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
143 int i;
145 for (i = 0; i < argc; i++) {
146 if (i != 0)
147 sm_printf(", ");
148 sm_printf("%s", argv[i]);
150 sm_printf("\n");
151 return 0;
154 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
156 char *err = NULL;
157 int rc;
159 if (!db)
160 return;
162 if (option_debug || debug_db) {
163 sm_msg("%s", sql);
164 if (strncasecmp(sql, "select", strlen("select")) == 0)
165 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
168 rc = sqlite3_exec(db, sql, callback, data, &err);
169 if (rc != SQLITE_OK && !parse_error) {
170 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
171 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
172 parse_error = 1;
176 static int replace_count;
177 static char **replace_table;
178 static const char *replace_return_ranges(const char *return_ranges)
180 int i;
182 if (!get_function()) {
183 /* I have no idea why EXPORT_SYMBOL() is here */
184 return return_ranges;
186 for (i = 0; i < replace_count; i += 3) {
187 if (strcmp(replace_table[i + 0], get_function()) == 0) {
188 if (strcmp(replace_table[i + 1], return_ranges) == 0)
189 return replace_table[i + 2];
192 return return_ranges;
195 static int delete_count;
196 static char **delete_table;
197 static bool is_delete_return(const char *return_ranges)
199 int i;
201 if (!get_function())
202 return false;
204 for (i = 0; i < delete_count; i += 2) {
205 if (strcmp(delete_table[i], get_function()) == 0 &&
206 strcmp(delete_table[i + 1], return_ranges) == 0)
207 return true;
210 return false;
213 void add_delete_return_hook(delete_hook *hook)
215 add_ptr_list(&delete_hooks, hook);
218 static bool is_project_delete_return(struct expression *expr)
220 delete_hook *hook;
222 FOR_EACH_PTR(delete_hooks, hook) {
223 if (hook(expr))
224 return true;
225 } END_FOR_EACH_PTR(hook);
226 return false;
229 static char *use_states;
230 static int get_db_state_count(void)
232 struct sm_state *sm;
233 int count = 0;
235 FOR_EACH_SM(__get_cur_stree(), sm) {
236 if (sm->owner == USHRT_MAX)
237 continue;
238 if (use_states[sm->owner])
239 count++;
240 } END_FOR_EACH_SM(sm);
241 return count;
244 static bool in_base_file(struct symbol *sym)
246 return sym->pos.stream == base_file_stream;
249 static bool is_local(struct symbol *sym)
251 if (sym->ctype.modifiers & MOD_STATIC)
252 return true;
253 if ((sym->ctype.modifiers & MOD_EXTERN) &&
254 (sym->ctype.modifiers & MOD_INLINE) &&
255 !in_base_file(sym))
256 return true;
258 if (!sym->definition)
259 return false;
261 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
262 (sym->definition->ctype.modifiers & MOD_INLINE) &&
263 !in_base_file(sym->definition))
264 return true;
266 return false;
269 void db_ignore_states(int id)
271 use_states[id] = 0;
274 unsigned long long __fn_mtag;
275 static void set_fn_mtag(struct symbol *sym)
277 char buf[128];
279 if (is_local(cur_func_sym))
280 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
281 else
282 snprintf(buf, sizeof(buf), "extern %s", get_function());
284 __fn_mtag = str_to_mtag(buf);
287 void sql_insert_return_states(int return_id, const char *return_ranges,
288 int type, int param, const char *key, const char *value)
290 unsigned long long id;
293 if (key && strlen(key) >= 80)
294 return;
295 if (__inline_fn)
296 id = (unsigned long)__inline_fn;
297 else
298 id = __fn_mtag;
300 sql_insert(return_states, "0x%llx, '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
301 get_base_file_id(), get_function(), id, return_id,
302 return_ranges, is_local(cur_func_sym), type, param, key, value);
305 static struct string_list *common_funcs;
306 static int is_common_function(const char *fn)
308 char *tmp;
310 if (!fn)
311 return 0;
313 if (strncmp(fn, "__builtin_", 10) == 0)
314 return 1;
316 FOR_EACH_PTR(common_funcs, tmp) {
317 if (strcmp(tmp, fn) == 0)
318 return 1;
319 } END_FOR_EACH_PTR(tmp);
321 return 0;
324 static char *function_signature(void)
326 return type_to_str(get_real_base_type(cur_func_sym));
329 void sql_insert_caller_info(struct expression *call, int type,
330 int param, const char *key, const char *value)
332 FILE *tmp_fd = sm_outfd;
333 char *fn;
335 if (!option_info && !__inline_call)
336 return;
337 if (unreachable())
338 return;
340 if (key && strlen(key) >= 80)
341 return;
343 fn = get_fnptr_name(call->fn);
344 if (!fn)
345 return;
347 if (__inline_call) {
348 mem_sql(NULL, NULL,
349 "insert into caller_info values (0x%llx, '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
350 get_base_file_id(), get_function(), fn, (unsigned long)call,
351 is_static(call->fn), type, param, key, value);
354 if (!option_info)
355 return;
357 if (strncmp(fn, "__builtin_", 10) == 0)
358 return;
359 if (type != INTERNAL && is_common_function(fn))
360 return;
362 sm_outfd = caller_info_fd;
363 sm_msg("SQL_caller_info: insert into caller_info values ("
364 "0x%llx, '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
365 get_base_file_id(), get_function(), fn, is_static(call->fn),
366 type, param, key, value);
367 sm_outfd = tmp_fd;
369 free_string(fn);
372 void sql_insert_function_ptr(const char *fn, const char *struct_name)
374 sql_insert_or_ignore(function_ptr, "0x%llx, '%s', '%s', 0",
375 get_base_file_id(), fn, struct_name);
378 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
380 unsigned long long id;
382 if (__inline_fn)
383 id = (unsigned long)__inline_fn;
384 else
385 id = __fn_mtag;
387 sql_insert_or_ignore(return_implies, "0x%llx, '%s', %llu, %d, %d, %d, '%s', '%s'",
388 get_base_file_id(), get_function(), id, fn_static(), type,
389 param, key, value);
392 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
394 sql_insert_or_ignore(call_implies, "0x%llx, '%s', %lu, %d, %d, %d, '%s', '%s'",
395 get_base_file_id(), get_function(), (unsigned long)__inline_fn,
396 fn_static(), type, param, key, value);
399 void sql_insert_function_type_size(const char *member, const char *ranges)
401 sql_insert(function_type_size, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), member, ranges);
404 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
406 sql_insert(function_type_info, "0x%llx, '%s', %d, '%s', '%s', '%s'", get_base_file_id(), get_function(), type, struct_type, member, value);
409 void sql_insert_type_info(int type, const char *member, const char *value)
411 sql_insert_cache(type_info, "0x%llx, %d, '%s', '%s'", get_base_file_id(), type, member, value);
414 void sql_insert_local_values(const char *name, const char *value)
416 sql_insert(local_values, "0x%llx, '%s', '%s'", get_base_file_id(), name, value);
419 void sql_insert_function_type_value(const char *type, const char *value)
421 sql_insert(function_type_value, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), type, value);
424 void sql_insert_function_type(int param, const char *value)
426 sql_insert(function_type, "0x%llx, '%s', %d, %d, '%s'",
427 get_base_file_id(), get_function(), fn_static(), param, value);
430 void sql_insert_parameter_name(int param, const char *value)
432 sql_insert(parameter_name, "0x%llx, '%s', %d, %d, '%s'",
433 get_base_file_id(), get_function(), fn_static(), param, value);
436 void sql_insert_data_info(struct expression *data, int type, const char *value)
438 char *data_name;
440 data_name = get_data_info_name(data);
441 if (!data_name)
442 return;
443 sql_insert(data_info, "0x%llx, '%s', %d, '%s'",
444 is_static(data) ? get_base_file_id() : 0,
445 data_name, type, value);
448 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
450 sql_insert(data_info, "0x%llx, '%s', %d, '%s'",
451 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file_id() : 0,
452 var, type, value);
455 void sql_save_constraint(const char *con)
457 if (!option_info)
458 return;
460 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
463 void sql_save_constraint_required(const char *data, int op, const char *limit)
465 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
468 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
470 if (!option_info)
471 return;
473 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
474 "select constraints_required.data, constraints_required.op, '%s' from "
475 "constraints_required where bound = '%s';", new_limit, old_limit);
478 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
480 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
483 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
485 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
486 return;
488 sql_insert(fn_data_link, "0x%llx, '%s', %d, %d, %d, '%s', '%s'",
489 is_local(fn->symbol) ? get_base_file_id() : 0,
490 fn->symbol->ident->name,
491 is_local(fn->symbol),
492 type, param, key, value);
495 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
497 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
498 tag, get_filename(), get_function(), get_lineno(),
499 left_name, right_name);
502 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
504 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
507 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
509 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
512 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
514 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
517 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
519 mtag_t *saved_tag = _tag;
520 mtag_t new_tag;
522 new_tag = strtoll(argv[0], NULL, 10);
524 if (!*saved_tag)
525 *saved_tag = new_tag;
526 else if (*saved_tag != new_tag)
527 *saved_tag = -1ULL;
529 return 0;
532 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
534 mtag_t tmp = 0;
536 run_sql(save_mtag, &tmp,
537 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
538 tag, container_offset);
540 if (tmp == 0 || tmp == -1ULL)
541 return 0;
542 *container = tmp;
543 return 1;
546 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
548 mtag_t tmp = 0;
550 run_sql(save_mtag, &tmp,
551 "select tag from mtag_map where container = %lld and container_offset = %d;",
552 container, offset);
554 if (tmp == 0 || tmp == -1ULL)
555 return 0;
556 *tag = tmp;
557 return 1;
560 char *get_static_filter(struct symbol *sym)
562 static char sql_filter[1024];
564 /* This can only happen on buggy code. Return invalid SQL. */
565 if (!sym) {
566 sql_filter[0] = '\0';
567 return sql_filter;
570 if (is_local(sym)) {
571 snprintf(sql_filter, sizeof(sql_filter),
572 "file = 0x%llx and function = '%s' and static = '1'",
573 get_base_file_id(), sym->ident->name);
574 } else {
575 snprintf(sql_filter, sizeof(sql_filter),
576 "function = '%s' and static = '0'", sym->ident->name);
579 return sql_filter;
582 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
584 int *row_count = _row_count;
586 *row_count = 0;
587 if (argc != 1)
588 return 0;
589 *row_count = atoi(argv[0]);
590 return 0;
593 static void sql_select_return_states_pointer(const char *cols,
594 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
596 char *ptr;
597 int return_count = 0;
599 ptr = get_fnptr_name(call->fn);
600 if (!ptr)
601 return;
603 run_sql(get_row_count, &return_count,
604 "select count(*) from return_states join function_ptr "
605 "where return_states.function == function_ptr.function and "
606 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
607 /* The magic number 100 is just from testing on the kernel. */
608 if (return_count == 0 || return_count > 100) {
609 run_sql(callback, info,
610 "select distinct %s from return_states join function_ptr where "
611 "return_states.function == function_ptr.function and ptr = '%s' "
612 "and searchable = 1 and type = %d "
613 "order by function_ptr.file, return_states.file, return_id, type;",
614 cols, ptr, INTERNAL);
615 mark_call_params_untracked(call);
616 return;
619 run_sql(callback, info,
620 "select %s from return_states join function_ptr where "
621 "return_states.function == function_ptr.function and ptr = '%s' "
622 "and searchable = 1 "
623 "order by function_ptr.file, return_states.file, return_id, type;",
624 cols, ptr);
627 static int is_local_symbol(struct expression *expr)
629 if (expr->type != EXPR_SYMBOL)
630 return 0;
631 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
632 return 0;
633 return 1;
636 bool is_fn_ptr(struct expression *fn)
638 fn = strip_expr(fn);
639 if (fn->type != EXPR_SYMBOL)
640 return true;
641 if (!fn->symbol)
642 return true;
643 if (is_local_symbol(fn))
644 return true;
645 return false;
648 void sql_select_return_states(const char *cols, struct expression *call,
649 int (*callback)(void*, int, char**, char**), void *info)
651 struct expression *fn;
652 int row_count = 0;
654 if (is_fake_call(call))
655 return;
657 fn = strip_expr(call->fn);
658 if (is_fn_ptr(fn)) {
659 sql_select_return_states_pointer(cols, call, callback, info);
660 return;
663 if (inlinable(fn)) {
664 mem_sql(callback, info,
665 "select %s from return_states where call_id = '%lu' order by return_id, type;",
666 cols, (unsigned long)call);
667 return;
670 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
671 get_static_filter(fn->symbol));
672 if (row_count == 0 && fn->symbol && fn->symbol->definition &&
673 !(fn->symbol->ident && strncmp(fn->symbol->ident->name, "__smatch", 8)))
674 set_state(my_id, "db_incomplete", NULL, &incomplete);
675 if (row_count == 0 || row_count > 3000) {
676 mark_call_params_untracked(call);
677 return;
680 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
681 cols, get_static_filter(fn->symbol));
684 bool db_incomplete(void)
686 return !!get_state(my_id, "db_incomplete", NULL);
689 #define CALL_IMPLIES 0
690 #define RETURN_IMPLIES 1
692 struct implies_info {
693 int type;
694 struct db_implies_cb_list *cb_list;
695 struct expression *expr;
696 struct symbol *sym;
699 void sql_select_implies(const char *cols, struct implies_info *info,
700 int (*callback)(void*, int, char**, char**))
702 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
703 mem_sql(callback, info,
704 "select %s from return_implies where call_id = '%lu';",
705 cols, (unsigned long)info->expr);
706 return;
709 run_sql(callback, info, "select %s from %s_implies where %s;",
710 cols,
711 info->type == CALL_IMPLIES ? "call" : "return",
712 get_static_filter(info->sym));
715 struct select_caller_info_data {
716 struct stree *final_states;
717 struct timeval start_time;
718 int prev_func_id;
719 int ignore;
720 int results;
723 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
725 static bool too_much_caller_info_data(struct symbol *sym)
727 int count = 0;
729 run_sql(get_row_count, &count,
730 "select count(*) from caller_info where %s;",
731 get_static_filter(sym));
732 if (count > 5000)
733 return true;
734 return false;
737 static void sql_select_caller_info(struct select_caller_info_data *data,
738 const char *cols, struct symbol *sym)
740 if (__inline_fn) {
741 mem_sql(caller_info_callback, data,
742 "select %s from caller_info where call_id = %lu;",
743 cols, (unsigned long)__inline_fn);
744 return;
747 if (is_common_function(sym->ident->name))
748 return;
749 run_sql(caller_info_callback, data,
750 "select %s from common_caller_info where %s order by call_id;",
751 cols, get_static_filter(sym));
752 if (data->results)
753 return;
755 if (too_much_caller_info_data(sym))
756 return;
758 run_sql(caller_info_callback, data,
759 "select %s from caller_info where %s order by call_id;",
760 cols, get_static_filter(sym));
763 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
765 struct def_callback *def_callback = __alloc_def_callback(0);
767 def_callback->hook_type = type;
768 def_callback->callback = callback;
769 add_ptr_list(&select_caller_info_callbacks, def_callback);
772 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
774 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
776 callback->hook_type = type;
777 callback->callback = fn;
778 add_ptr_list(&select_caller_name_sym_callbacks, callback);
782 * These call backs are used when the --info option is turned on to print struct
783 * member information. For example foo->bar could have a state in
784 * smatch_extra.c and also check_user.c.
786 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
788 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
790 member_callback->owner = owner;
791 member_callback->callback = callback;
792 add_ptr_list(&member_callbacks, member_callback);
795 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
797 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
799 member_callback->owner = owner;
800 member_callback->callback = callback;
801 add_ptr_list(&member_callbacks_new, member_callback);
804 void add_return_info_callback(int owner,
805 void (*callback)(int return_id, char *return_ranges,
806 struct expression *returned_expr,
807 int param,
808 const char *printed_name,
809 struct sm_state *sm))
811 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
813 return_callback->owner = owner;
814 return_callback->callback = callback;
815 add_ptr_list(&return_callbacks, return_callback);
818 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
820 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
822 callback->callback = fn;
823 add_ptr_list(&returned_state_callbacks, callback);
826 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))
828 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
830 member_callback->owner = owner;
831 member_callback->callback = callback;
832 add_ptr_list(&returned_member_callbacks, member_callback);
835 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
837 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
839 cb->type = type;
840 cb->callback = callback;
841 add_ptr_list(&call_implies_cb_list, cb);
844 void select_return_implies_hook_early(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
846 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
848 cb->type = type;
849 cb->callback = callback;
850 add_ptr_list(&return_implies_cb_list_early, cb);
853 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
855 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
857 cb->type = type;
858 cb->callback = callback;
859 add_ptr_list(&return_implies_cb_list_late, cb);
862 struct return_info {
863 struct expression *static_returns_call;
864 struct symbol *return_type;
865 struct range_list *return_range_list;
868 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
870 struct return_info *ret_info = _ret_info;
871 struct range_list *rl;
872 struct expression *call_expr = ret_info->static_returns_call;
874 if (argc != 1)
875 return 0;
876 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
877 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
878 return 0;
881 static struct expression *cached_expr, *cached_no_args;
882 static const char *cached_str;
883 static struct range_list *cached_rl, *cached_str_rl, *cached_no_args_rl;
885 static void clear_cached_return_vals(void)
887 cached_expr = NULL;
888 cached_rl = NULL;
889 cached_str = NULL;
890 cached_str_rl = NULL;
891 cached_no_args = NULL;
892 cached_no_args_rl = NULL;
895 struct range_list *db_return_vals(struct expression *expr)
897 struct return_info ret_info = {};
898 struct sm_state *sm;
900 if (!expr)
901 return NULL;
903 if (is_fake_call(expr))
904 return NULL;
906 if (expr == cached_expr)
907 return clone_rl(cached_rl);
909 cached_expr = expr;
910 cached_rl = NULL;
912 sm = get_extra_sm_state(expr);
913 if (sm) {
914 cached_rl = clone_rl(estate_rl(sm->state));
915 return clone_rl(estate_rl(sm->state));
917 ret_info.static_returns_call = expr;
918 ret_info.return_type = get_type(expr);
919 if (!ret_info.return_type)
920 return NULL;
922 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
923 return NULL;
925 ret_info.return_range_list = NULL;
926 if (inlinable(expr->fn)) {
927 mem_sql(db_return_callback, &ret_info,
928 "select distinct return from return_states where call_id = '%lu';",
929 (unsigned long)expr);
930 } else {
931 run_sql(db_return_callback, &ret_info,
932 "select distinct return from return_states where %s;",
933 get_static_filter(expr->fn->symbol));
935 cached_rl = clone_rl(ret_info.return_range_list);
936 return ret_info.return_range_list;
939 struct range_list *db_return_vals_from_str(const char *fn_name)
941 struct return_info ret_info;
943 if (!fn_name)
944 return NULL;
945 if (fn_name == cached_str)
946 return clone_rl(cached_str_rl);
947 cached_str = fn_name;
948 cached_str_rl = NULL;
950 ret_info.static_returns_call = NULL;
951 ret_info.return_type = &llong_ctype;
952 ret_info.return_range_list = NULL;
954 run_sql(db_return_callback, &ret_info,
955 "select distinct return from return_states where function = '%s';",
956 fn_name);
957 cached_str_rl = clone_rl(ret_info.return_range_list);
958 return ret_info.return_range_list;
962 * This is used when we have a function that takes a function pointer as a
963 * parameter. "frob(blah, blah, my_function);" We know that the return values
964 * from frob() come from my_funcion() so we want to find the possible returns
965 * of my_function(), but we don't know which arguments are passed to it.
968 struct range_list *db_return_vals_no_args(struct expression *expr)
970 struct return_info ret_info = {};
972 if (!expr || expr->type != EXPR_SYMBOL)
973 return NULL;
975 if (expr == cached_no_args)
976 return clone_rl(cached_no_args_rl);
977 cached_no_args = expr;
978 cached_no_args_rl = NULL;
980 ret_info.static_returns_call = expr;
981 ret_info.return_type = get_type(expr);
982 ret_info.return_type = get_real_base_type(ret_info.return_type);
983 if (!ret_info.return_type)
984 return NULL;
986 run_sql(db_return_callback, &ret_info,
987 "select distinct return from return_states where %s;",
988 get_static_filter(expr->symbol));
990 cached_no_args_rl = clone_rl(ret_info.return_range_list);
991 return ret_info.return_range_list;
994 static void match_call_marker(struct expression *expr)
996 struct symbol *type;
998 type = get_type(expr->fn);
999 if (type && type->type == SYM_PTR)
1000 type = get_real_base_type(type);
1003 * we just want to record something in the database so that if we have
1004 * two calls like: frob(4); frob(some_unkown); then on the receiving
1005 * side we know that sometimes frob is called with unknown parameters.
1008 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
1011 int is_recursive_member(const char *name)
1013 char buf[256];
1014 const char *p, *next;
1015 int size;
1017 p = strchr(name, '>');
1018 if (!p)
1019 return 0;
1020 p++;
1021 while (true) {
1022 next = strchr(p, '>');
1023 if (!next)
1024 return 0;
1025 next++;
1027 size = next - p;
1028 if (size >= sizeof(buf))
1029 return 0;
1030 memcpy(buf, p, size);
1031 buf[size] = '\0';
1032 if (strstr(next, buf))
1033 return 1;
1034 p = next;
1038 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
1040 struct symbol *sym;
1041 const char *sm_name;
1042 char *name;
1043 bool is_address = false;
1044 bool add_star = false;
1045 char buf[256];
1046 char *ret = NULL;
1047 int len;
1049 expr = strip_expr(expr);
1050 if (!expr)
1051 return NULL;
1053 if (expr->type == EXPR_PREOP && expr->op == '&') {
1054 expr = strip_expr(expr->unop);
1055 is_address = true;
1058 name = expr_to_var_sym(expr, &sym);
1059 if (!name || !sym)
1060 goto free;
1061 if (sym != sm->sym)
1062 goto free;
1064 sm_name = sm->name;
1065 add_star = false;
1066 if (sm_name[0] == '*') {
1067 add_star = true;
1068 sm_name++;
1071 len = strlen(name);
1072 if (strncmp(name, sm_name, len) != 0)
1073 goto free;
1074 if (sm_name[len] == '\0') {
1075 snprintf(buf, sizeof(buf), "%s%s$",
1076 add_star ? "*" : "", is_address ? "*" : "");
1077 } else {
1078 if (sm_name[len] != '.' && sm_name[len] != '-')
1079 goto free;
1080 if (sm_name[len] == '-')
1081 len++;
1082 // FIXME does is_address really imply that sm_name[len] == '-'
1083 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
1084 sm_name + len);
1087 ret = alloc_sname(buf);
1088 free:
1089 free_string(name);
1090 return ret;
1093 static void print_struct_members(struct expression *call, struct expression *expr, int param,
1094 int owner,
1095 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
1096 bool new)
1098 struct sm_state *sm;
1099 const char *sm_name;
1100 char *name;
1101 struct symbol *sym;
1102 int len;
1103 char printed_name[256];
1104 int is_address = 0;
1105 bool add_star;
1106 struct symbol *type;
1108 expr = strip_expr(expr);
1109 if (!expr)
1110 return;
1111 type = get_type(expr);
1112 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1113 return;
1115 if (expr->type == EXPR_PREOP && expr->op == '&') {
1116 expr = strip_expr(expr->unop);
1117 is_address = 1;
1120 name = expr_to_var_sym(expr, &sym);
1121 if (!name || !sym)
1122 goto free;
1124 len = strlen(name);
1125 FOR_EACH_SM(__get_cur_stree(), sm) {
1126 if (sm->owner != owner || sm->sym != sym)
1127 continue;
1129 sm_name = sm->name;
1130 add_star = false;
1131 if (sm_name[0] == '*') {
1132 add_star = true;
1133 sm_name++;
1135 // FIXME: simplify?
1136 if (!add_star && strcmp(name, sm_name) == 0) {
1137 if (is_address) {
1138 snprintf(printed_name, sizeof(printed_name), "*$");
1139 } else {
1140 if (new)
1141 snprintf(printed_name, sizeof(printed_name), "$");
1142 else
1143 continue;
1145 } else if (add_star && strcmp(name, sm_name) == 0) {
1146 snprintf(printed_name, sizeof(printed_name), "%s*$",
1147 is_address ? "*" : "");
1148 } else if (strncmp(name, sm_name, len) == 0) {
1149 if (sm_name[len] != '.' && sm_name[len] != '-')
1150 continue;
1151 if (is_address && sm_name[len] == '.') {
1152 snprintf(printed_name, sizeof(printed_name),
1153 "%s$->%s", add_star ? "*" : "",
1154 sm_name + len + 1);
1155 } else if (is_address && sm_name[len] == '-') {
1156 snprintf(printed_name, sizeof(printed_name),
1157 "%s(*$)%s", add_star ? "*" : "",
1158 sm_name + len);
1159 } else {
1160 snprintf(printed_name, sizeof(printed_name),
1161 "%s$%s", add_star ? "*" : "",
1162 sm_name + len);
1164 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1165 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1166 continue;
1167 if (is_address && sm_name[len + 1] == '.') {
1168 snprintf(printed_name, sizeof(printed_name),
1169 "&%s$->%s", add_star ? "*" : "",
1170 sm_name + len + 2);
1171 } else if (is_address && sm_name[len] == '-') {
1172 snprintf(printed_name, sizeof(printed_name),
1173 "&%s(*$)%s", add_star ? "*" : "",
1174 sm_name + len + 1);
1175 } else {
1176 snprintf(printed_name, sizeof(printed_name),
1177 "&%s$%s", add_star ? "*" : "",
1178 sm_name + len + 1);
1180 } else {
1181 continue;
1183 if (is_recursive_member(printed_name))
1184 continue;
1185 callback(call, param, printed_name, sm);
1186 } END_FOR_EACH_SM(sm);
1187 free:
1188 free_string(name);
1191 static void match_call_info(struct expression *call)
1193 struct member_info_callback *cb;
1194 struct expression *arg;
1195 int i;
1197 FOR_EACH_PTR(member_callbacks, cb) {
1198 i = -1;
1199 FOR_EACH_PTR(call->args, arg) {
1200 i++;
1201 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1202 } END_FOR_EACH_PTR(arg);
1203 } END_FOR_EACH_PTR(cb);
1206 static struct expression *get_fake_variable(struct expression *expr)
1208 struct expression *tmp;
1210 tmp = expr_get_fake_parent_expr(expr);
1211 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1212 return NULL;
1214 return tmp->left;
1217 static struct sm_state *get_returned_sm(struct expression *expr)
1219 struct expression *fake;
1221 fake = get_fake_variable(expr);
1222 if (fake)
1223 expr = fake;
1225 return get_sm_state_expr(SMATCH_EXTRA, expr);
1228 static void match_call_info_new(struct expression *call)
1230 struct member_info_callback *cb;
1231 struct expression *arg, *tmp;
1232 int i;
1234 if (!option_info && !__inline_call && !local_debug)
1235 return;
1237 FOR_EACH_PTR(member_callbacks_new, cb) {
1238 i = -1;
1239 FOR_EACH_PTR(call->args, arg) {
1240 i++;
1241 tmp = get_fake_variable(arg);
1242 if (!tmp)
1243 tmp = arg;
1244 __ignore_param_used++;
1245 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1246 __ignore_param_used--;
1247 } END_FOR_EACH_PTR(arg);
1248 } END_FOR_EACH_PTR(cb);
1251 static int get_param(int param, char **name, struct symbol **sym)
1253 struct symbol *arg;
1254 int i;
1256 i = 0;
1257 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1258 if (i == param) {
1259 *name = arg->ident->name;
1260 *sym = arg;
1261 return TRUE;
1263 i++;
1264 } END_FOR_EACH_PTR(arg);
1266 return FALSE;
1269 static int function_signature_matches(const char *sig)
1271 char *my_sig;
1273 my_sig = function_signature();
1274 if (!sig || !my_sig)
1275 return 1; /* default to matching */
1276 if (strcmp(my_sig, sig) == 0)
1277 return 1;
1278 return 0;
1281 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1283 struct select_caller_info_data *data = _data;
1284 int func_id;
1285 long type;
1286 long param;
1287 char *key;
1288 char *value;
1289 char *name = NULL;
1290 struct symbol *sym = NULL;
1291 struct def_callback *def_callback;
1292 struct def_name_sym_callback *ns_callback;
1293 struct stree *stree;
1294 struct timeval cur_time;
1295 char fullname[256];
1296 char *p;
1298 data->results = 1;
1300 if (argc != 5)
1301 return 0;
1303 gettimeofday(&cur_time, NULL);
1304 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1305 return 0;
1307 func_id = atoi(argv[0]);
1308 errno = 0;
1309 type = strtol(argv[1], NULL, 10);
1310 param = strtol(argv[2], NULL, 10);
1311 if (errno)
1312 return 0;
1313 key = argv[3];
1314 value = argv[4];
1316 if (data->prev_func_id == -1)
1317 data->prev_func_id = func_id;
1318 if (func_id != data->prev_func_id) {
1319 stree = __pop_fake_cur_stree();
1320 if (!data->ignore)
1321 merge_stree(&data->final_states, stree);
1322 free_stree(&stree);
1323 __push_fake_cur_stree();
1324 __unnullify_path();
1325 data->prev_func_id = func_id;
1326 data->ignore = 0;
1329 if (data->ignore)
1330 return 0;
1331 if (type == INTERNAL &&
1332 !function_signature_matches(value)) {
1333 data->ignore = 1;
1334 return 0;
1337 if (param >= 0 && !get_param(param, &name, &sym))
1338 return 0;
1340 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1341 if (def_callback->hook_type == type)
1342 def_callback->callback(name, sym, key, value);
1343 } END_FOR_EACH_PTR(def_callback);
1345 p = strchr(key, '$');
1346 if (name && p)
1347 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1348 else
1349 snprintf(fullname, sizeof(fullname), "%s", key);
1351 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1352 if (ns_callback->hook_type == type)
1353 ns_callback->callback(fullname, sym, value);
1354 } END_FOR_EACH_PTR(ns_callback);
1356 return 0;
1359 static struct string_list *ptr_names_done;
1360 static struct string_list *ptr_names;
1362 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1364 insert_string(&ptr_names, alloc_string(argv[0]));
1365 return 0;
1368 static char *get_next_ptr_name(void)
1370 char *ptr;
1372 FOR_EACH_PTR(ptr_names, ptr) {
1373 if (!insert_string(&ptr_names_done, ptr))
1374 continue;
1375 return ptr;
1376 } END_FOR_EACH_PTR(ptr);
1377 return NULL;
1380 static void get_ptr_names(unsigned long long file, const char *name)
1382 char sql_filter[1024];
1383 int before, after;
1385 if (file) {
1386 snprintf(sql_filter, 1024, "file = 0x%llx and function = '%s';",
1387 file, name);
1388 } else {
1389 snprintf(sql_filter, 1024, "function = '%s';", name);
1392 before = ptr_list_size((struct ptr_list *)ptr_names);
1394 run_sql(get_ptr_name, NULL,
1395 "select distinct ptr from function_ptr where %s",
1396 sql_filter);
1398 after = ptr_list_size((struct ptr_list *)ptr_names);
1399 if (before == after)
1400 return;
1402 while ((name = get_next_ptr_name()))
1403 get_ptr_names(0, name);
1406 static void match_data_from_db(struct symbol *sym)
1408 struct select_caller_info_data data = { .prev_func_id = -1 };
1409 struct sm_state *sm;
1410 struct stree *stree;
1411 struct timeval end_time;
1413 if (!sym || !sym->ident)
1414 return;
1416 set_fn_mtag(sym);
1417 gettimeofday(&data.start_time, NULL);
1419 __push_fake_cur_stree();
1420 __unnullify_path();
1422 if (!__inline_fn) {
1423 char *ptr;
1425 if (sym->ctype.modifiers & MOD_STATIC)
1426 get_ptr_names(get_base_file_id(), sym->ident->name);
1427 else
1428 get_ptr_names(0, sym->ident->name);
1430 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1431 __free_ptr_list((struct ptr_list **)&ptr_names);
1432 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1433 __free_fake_cur_stree();
1434 return;
1437 sql_select_caller_info(&data,
1438 "call_id, type, parameter, key, value",
1439 sym);
1442 stree = __pop_fake_cur_stree();
1443 if (!data.ignore)
1444 merge_stree(&data.final_states, stree);
1445 free_stree(&stree);
1446 __push_fake_cur_stree();
1447 __unnullify_path();
1448 data.prev_func_id = -1;
1449 data.ignore = 0;
1450 data.results = 0;
1452 FOR_EACH_PTR(ptr_names, ptr) {
1453 run_sql(caller_info_callback, &data,
1454 "select call_id, type, parameter, key, value"
1455 " from common_caller_info where function = '%s' order by call_id",
1456 ptr);
1457 } END_FOR_EACH_PTR(ptr);
1459 if (data.results) {
1460 FOR_EACH_PTR(ptr_names, ptr) {
1461 free_string(ptr);
1462 } END_FOR_EACH_PTR(ptr);
1463 goto free_ptr_names;
1466 FOR_EACH_PTR(ptr_names, ptr) {
1467 run_sql(caller_info_callback, &data,
1468 "select call_id, type, parameter, key, value"
1469 " from caller_info where function = '%s' order by call_id",
1470 ptr);
1471 free_string(ptr);
1472 } END_FOR_EACH_PTR(ptr);
1474 free_ptr_names:
1475 __free_ptr_list((struct ptr_list **)&ptr_names);
1476 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1477 } else {
1478 sql_select_caller_info(&data,
1479 "call_id, type, parameter, key, value",
1480 sym);
1483 stree = __pop_fake_cur_stree();
1484 if (!data.ignore)
1485 merge_stree(&data.final_states, stree);
1486 free_stree(&stree);
1488 gettimeofday(&end_time, NULL);
1489 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1490 FOR_EACH_SM(data.final_states, sm) {
1491 __set_sm(sm);
1492 } END_FOR_EACH_SM(sm);
1495 free_stree(&data.final_states);
1498 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1500 struct implies_info *info = _info;
1501 struct db_implies_callback *cb;
1502 struct expression *arg = NULL;
1503 int type;
1504 int param;
1506 if (argc != 5)
1507 return 0;
1509 type = atoi(argv[1]);
1510 param = atoi(argv[2]);
1512 /* The caller doesn't pass the assignment so -1 can't be useful */
1513 if (param == -1)
1514 return 0;
1515 if (param >= 0) {
1516 arg = get_argument_from_call_expr(info->expr->args, param);
1517 if (!arg)
1518 return 0;
1521 FOR_EACH_PTR(info->cb_list, cb) {
1522 if (cb->type != type)
1523 continue;
1524 cb->callback(info->expr, arg, argv[3], argv[4]);
1525 } END_FOR_EACH_PTR(cb);
1527 return 0;
1530 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1532 struct implies_info *info = _info;
1533 struct db_implies_callback *cb;
1534 struct expression *arg;
1535 struct symbol *sym;
1536 char *name;
1537 int type;
1538 int param;
1540 if (argc != 5)
1541 return 0;
1543 type = atoi(argv[1]);
1544 param = atoi(argv[2]);
1546 if (!get_param(param, &name, &sym))
1547 return 0;
1548 arg = symbol_expression(sym);
1549 if (!arg)
1550 return 0;
1552 FOR_EACH_PTR(info->cb_list, cb) {
1553 if (cb->type != type)
1554 continue;
1555 cb->callback(info->expr, arg, argv[3], argv[4]);
1556 } END_FOR_EACH_PTR(cb);
1558 return 0;
1561 static void match_return_implies_helper(struct expression *expr, struct db_implies_cb_list *cb_list)
1563 struct implies_info info = {
1564 .type = RETURN_IMPLIES,
1565 .cb_list = cb_list,
1568 if (expr->fn->type != EXPR_SYMBOL ||
1569 !expr->fn->symbol)
1570 return;
1571 info.expr = expr;
1572 info.sym = expr->fn->symbol;
1573 sql_select_implies("function, type, parameter, key, value", &info,
1574 return_implies_callbacks);
1577 static void match_return_implies_early(struct expression *expr)
1579 match_return_implies_helper(expr, return_implies_cb_list_early);
1582 static void match_return_implies_late(struct expression *expr)
1584 match_return_implies_helper(expr, return_implies_cb_list_late);
1587 static void match_call_implies(struct symbol *sym)
1589 struct implies_info info = {
1590 .type = CALL_IMPLIES,
1591 .cb_list = call_implies_cb_list,
1594 if (!sym || !sym->ident)
1595 return;
1597 info.sym = sym;
1598 sql_select_implies("function, type, parameter, key, value", &info,
1599 call_implies_callbacks);
1602 static char *get_fn_param_str(struct expression *expr)
1604 struct expression *tmp;
1605 int param;
1606 char buf[32];
1608 tmp = get_assigned_expr(expr);
1609 if (tmp)
1610 expr = tmp;
1611 expr = strip_expr(expr);
1612 if (!expr || expr->type != EXPR_CALL)
1613 return NULL;
1614 expr = strip_expr(expr->fn);
1615 if (!expr || expr->type != EXPR_SYMBOL)
1616 return NULL;
1617 param = get_param_num(expr);
1618 if (param < 0)
1619 return NULL;
1621 snprintf(buf, sizeof(buf), "[r $%d]", param);
1622 return alloc_sname(buf);
1625 static char *get_return_compare_is_param(struct expression *expr)
1627 char *var;
1628 char buf[256];
1629 int comparison;
1630 int param;
1632 param = get_param_num(expr);
1633 if (param < 0)
1634 return NULL;
1636 var = expr_to_var(expr);
1637 if (!var)
1638 return NULL;
1639 snprintf(buf, sizeof(buf), "%s orig", var);
1640 comparison = get_comparison_strings(var, buf);
1641 free_string(var);
1643 if (!comparison)
1644 return NULL;
1646 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1647 return alloc_sname(buf);
1650 static char *get_return_compare_str(struct expression *expr)
1652 char *compare_str;
1654 compare_str = get_return_compare_is_param(expr);
1655 if (compare_str)
1656 return compare_str;
1658 compare_str = expr_lte_to_param(expr, -1);
1659 if (compare_str)
1660 return compare_str;
1662 return expr_param_comparison(expr, -1);
1665 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1667 struct expression *fake;
1668 struct range_list *rl;
1669 const char *return_ranges;
1670 sval_t sval;
1671 const char *container_of_str;
1672 const char *math_str;
1673 char *fn_param_str;
1674 char *compare_str;
1675 char buf[128];
1677 *rl_p = NULL;
1679 if (!expr)
1680 return alloc_sname("");
1682 fake = get_fake_variable(expr);
1683 if (fake)
1684 expr = fake;
1686 container_of_str = get_container_of_str(expr);
1688 if (get_implied_value(expr, &sval)) {
1689 sval = sval_cast(cur_func_return_type(), sval);
1690 *rl_p = alloc_rl(sval, sval);
1691 return_ranges = sval_to_str_or_err_ptr(sval);
1692 if (container_of_str) {
1693 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1694 return alloc_sname(buf);
1696 return return_ranges;
1699 fn_param_str = get_fn_param_str(expr);
1700 math_str = get_param_key_swap_dollar(expr);
1701 compare_str = expr_equal_to_param(expr, -1);
1702 if (!math_str)
1703 math_str = get_value_in_terms_of_parameter_math(expr);
1705 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1706 rl = cast_rl(cur_func_return_type(), rl);
1707 return_ranges = show_rl(rl);
1708 } else if (get_imaginary_absolute(expr, &rl)){
1709 rl = cast_rl(cur_func_return_type(), rl);
1710 return alloc_sname(show_rl(rl));
1711 } else {
1712 get_absolute_rl(expr, &rl);
1713 rl = cast_rl(cur_func_return_type(), rl);
1714 return_ranges = show_rl(rl);
1716 *rl_p = rl;
1718 if (container_of_str) {
1719 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1720 return alloc_sname(buf);
1722 if (fn_param_str) {
1723 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1724 return alloc_sname(buf);
1726 if (compare_str) {
1727 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1728 return alloc_sname(buf);
1730 if (math_str) {
1731 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1732 return alloc_sname(buf);
1734 compare_str = get_return_compare_str(expr);
1735 if (compare_str) {
1736 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1737 return alloc_sname(buf);
1740 return return_ranges;
1743 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1745 char line_number[16];
1747 snprintf(line_number, sizeof(line_number), "%d", get_lineno());
1748 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, line_number, function_signature());
1751 static bool call_return_state_hooks_conditional(struct expression *expr)
1753 int final_pass_orig = final_pass;
1754 static int recurse;
1756 if (recurse >= 2)
1757 return false;
1758 if (!expr ||
1759 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1760 return false;
1762 recurse++;
1764 __push_fake_cur_stree();
1766 final_pass = 0;
1767 __split_whole_condition(expr->conditional);
1768 final_pass = final_pass_orig;
1770 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1772 __push_true_states();
1773 __use_false_states();
1775 call_return_state_hooks(expr->cond_false);
1777 __merge_true_states();
1778 __free_fake_cur_stree();
1780 recurse--;
1781 return true;
1784 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1786 struct split_data *data = NULL;
1787 struct expression *compare;
1788 struct range_list *rl;
1789 char buf[64];
1790 char *math;
1791 sval_t sval;
1792 bool undo;
1793 int i;
1795 for (i = 0; i < split_count; i++) {
1796 if (get_function() &&
1797 strcmp(get_function(), forced_splits[i]->func) == 0) {
1798 data = forced_splits[i];
1799 break;
1802 if (!data)
1803 return false;
1805 // FIXME: this works for copy_to/from_user() because the only thing we
1806 // care about is zero/non-zero
1807 if (strcmp(data->rl, "0") != 0)
1808 return false;
1810 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1811 if (!compare)
1812 return false;
1813 if (get_implied_value(compare, &sval))
1814 return false;
1816 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1817 call_return_states_callbacks("0", expr);
1818 if (undo)
1819 end_assume();
1821 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1822 if (get_implied_rl(expr, &rl)) {
1823 math = strchr(return_ranges, '[');
1824 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1825 } else {
1826 snprintf(buf, sizeof(buf), "%s", return_ranges);
1828 call_return_states_callbacks(buf, expr);
1829 if (undo)
1830 end_assume();
1832 return true;
1835 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1837 struct returned_state_callback *cb;
1839 return_ranges = replace_return_ranges(return_ranges);
1840 if (is_delete_return(return_ranges))
1841 return;
1842 if (is_project_delete_return(expr))
1843 return;
1844 if (handle_forced_split(return_ranges, expr))
1845 return;
1847 return_id++;
1848 FOR_EACH_PTR(returned_state_callbacks, cb) {
1849 cb->callback(return_id, (char *)return_ranges, expr);
1850 } END_FOR_EACH_PTR(cb);
1853 static void call_return_state_hooks_compare(struct expression *expr)
1855 char *return_ranges;
1856 int final_pass_orig = final_pass;
1857 sval_t sval = { .type = &int_ctype };
1858 sval_t ret;
1860 if (!get_implied_value(expr, &ret))
1861 ret.value = -1;
1863 __push_fake_cur_stree();
1865 final_pass = 0;
1866 __split_whole_condition(expr);
1867 final_pass = final_pass_orig;
1869 if (ret.value != 0) {
1870 return_ranges = alloc_sname("1");
1871 sval.value = 1;
1872 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1874 call_return_states_callbacks(return_ranges, expr);
1877 __push_true_states();
1878 __use_false_states();
1880 if (ret.value != 1) {
1881 return_ranges = alloc_sname("0");
1882 sval.value = 0;
1883 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1885 call_return_states_callbacks(return_ranges, expr);
1888 __merge_true_states();
1889 __free_fake_cur_stree();
1892 static bool is_implies_function(struct expression *expr)
1894 struct range_list *rl;
1896 if (!expr)
1897 return false;
1899 rl = get_range_implications(get_function());
1900 if (!rl)
1901 return false;
1903 sm_msg("%s: is implied", __func__);
1904 return true;
1907 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1909 struct sm_state *tmp;
1911 FOR_EACH_PTR(slist, tmp) {
1912 if (strcmp(tmp->state->name, sm->state->name) == 0)
1913 return 1;
1914 } END_FOR_EACH_PTR(tmp);
1916 return 0;
1919 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1921 struct range_list *rl;
1922 char *return_ranges;
1923 struct sm_state *tmp;
1924 int ret = 0;
1925 int nr_possible, nr_states;
1926 char *compare_str;
1927 char buf[128];
1928 struct state_list *already_handled = NULL;
1929 sval_t sval;
1931 if (!sm || !sm->merged)
1932 return 0;
1934 if (too_many_possible(sm) && !is_implies_function(expr))
1935 return 0;
1937 /* bail if it gets too complicated */
1938 nr_possible = 0;
1939 FOR_EACH_PTR(sm->possible, tmp) {
1940 if (!is_leaf(tmp))
1941 continue;
1942 if (ptr_in_list(tmp, already_handled))
1943 continue;
1944 add_ptr_list(&already_handled, tmp);
1945 nr_possible++;
1946 } END_FOR_EACH_PTR(tmp);
1947 free_slist(&already_handled);
1948 nr_states = get_db_state_count();
1949 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1950 return 0;
1952 FOR_EACH_PTR(sm->possible, tmp) {
1953 if (!is_leaf(tmp))
1954 continue;
1955 if (ptr_in_list(tmp, already_handled))
1956 continue;
1957 add_ptr_list(&already_handled, tmp);
1959 ret = 1;
1960 __push_fake_cur_stree();
1962 overwrite_states_using_pool(sm, tmp);
1964 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1965 return_ranges = show_rl(rl);
1966 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1967 compare_str = get_return_compare_str(expr);
1968 /* ignore obvious stuff like 0 <= param */
1969 /* Is this worthile when we have PARAM_COMPARE? */
1970 if (compare_str &&
1971 strncmp(compare_str, "[=", 2) != 0 &&
1972 rl_to_sval(rl, &sval))
1973 compare_str = NULL;
1974 if (compare_str) {
1975 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1976 return_ranges = alloc_sname(buf);
1979 call_return_states_callbacks(return_ranges, expr);
1981 __free_fake_cur_stree();
1982 } END_FOR_EACH_PTR(tmp);
1984 free_slist(&already_handled);
1986 return ret;
1989 static int call_return_state_hooks_split_possible(struct expression *expr)
1991 struct sm_state *sm;
1993 if (!expr)
1994 return 0;
1996 sm = get_returned_sm(expr);
1997 return split_possible_helper(sm, expr);
2000 static bool has_empty_state(struct sm_state *sm)
2002 struct sm_state *tmp;
2004 FOR_EACH_PTR(sm->possible, tmp) {
2005 if (!estate_rl(tmp->state))
2006 return true;
2007 } END_FOR_EACH_PTR(tmp);
2009 return false;
2012 static bool has_possible_negative(struct sm_state *sm)
2014 struct sm_state *tmp;
2016 if (!type_signed(estate_type(sm->state)))
2017 return false;
2019 FOR_EACH_PTR(sm->possible, tmp) {
2020 if (!estate_rl(tmp->state))
2021 continue;
2022 if (sval_is_negative(estate_min(tmp->state)) &&
2023 sval_is_negative(estate_max(tmp->state)))
2024 return true;
2025 } END_FOR_EACH_PTR(tmp);
2027 return false;
2030 static bool has_separate_zero_null(struct sm_state *sm)
2032 struct sm_state *tmp;
2033 sval_t sval;
2035 FOR_EACH_PTR(sm->possible, tmp) {
2036 if (!estate_get_single_value(tmp->state, &sval))
2037 continue;
2038 if (sval.value == 0)
2039 return true;
2040 } END_FOR_EACH_PTR(tmp);
2042 return false;
2045 static int split_positive_from_negative(struct expression *expr)
2047 struct sm_state *sm;
2048 struct range_list *rl;
2049 const char *return_ranges;
2050 struct range_list *ret_rl;
2051 bool separate_zero;
2052 int undo;
2054 /* We're going to print the states 3 times */
2055 if (get_db_state_count() > 10000 / 3)
2056 return 0;
2058 if (!get_implied_rl(expr, &rl) || !rl)
2059 return 0;
2060 /* Forget about INT_MAX and larger */
2061 if (rl_max(rl).value <= 0)
2062 return 0;
2063 if (!sval_is_negative(rl_min(rl)))
2064 return 0;
2066 sm = get_returned_sm(expr);
2067 if (!sm)
2068 return 0;
2069 if (has_empty_state(sm))
2070 return 0;
2071 if (!has_possible_negative(sm))
2072 return 0;
2073 separate_zero = has_separate_zero_null(sm);
2075 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
2076 return 0;
2078 return_ranges = get_return_ranges_str(expr, &ret_rl);
2079 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2080 call_return_states_callbacks(return_ranges, expr);
2082 end_assume();
2084 if (separate_zero) {
2085 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
2087 return_ranges = get_return_ranges_str(expr, &ret_rl);
2088 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2089 call_return_states_callbacks(return_ranges, expr);
2091 if (undo)
2092 end_assume();
2095 undo = assume(compare_expression(expr, '<', zero_expr()));
2097 return_ranges = get_return_ranges_str(expr, &ret_rl);
2098 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2099 call_return_states_callbacks(return_ranges, expr);
2101 if (undo)
2102 end_assume();
2104 return 1;
2107 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
2109 struct range_list *rl;
2110 struct range_list *nonnull_rl;
2111 sval_t null_sval;
2112 struct range_list *null_rl = NULL;
2113 char *return_ranges;
2114 struct sm_state *sm;
2115 struct smatch_state *state;
2116 int nr_states;
2117 int final_pass_orig = final_pass;
2119 if (!expr || expr_equal_to_param(expr, -1))
2120 return 0;
2121 if (expr->type == EXPR_CALL)
2122 return 0;
2124 sm = get_returned_sm(expr);
2125 if (!sm)
2126 return 0;
2127 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2128 return 0;
2129 state = sm->state;
2130 if (!estate_rl(state))
2131 return 0;
2132 if (estate_min(state).value == 0 && estate_max(state).value == 0)
2133 return 0;
2134 if (has_possible_negative(sm))
2135 return 0;
2136 if (!has_separate_zero_null(sm))
2137 return 0;
2139 nr_states = get_db_state_count();
2140 if (option_info && nr_states >= 1500)
2141 return 0;
2143 rl = estate_rl(state);
2145 __push_fake_cur_stree();
2147 final_pass = 0;
2148 __split_whole_condition(expr);
2149 final_pass = final_pass_orig;
2151 nonnull_rl = rl_filter(rl, rl_zero());
2152 return_ranges = show_rl(nonnull_rl);
2153 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2155 call_return_states_callbacks(return_ranges, expr);
2157 __push_true_states();
2158 __use_false_states();
2160 return_ranges = alloc_sname("0");
2161 null_sval = sval_type_val(rl_type(rl), 0);
2162 add_range(&null_rl, null_sval, null_sval);
2163 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2164 call_return_states_callbacks(return_ranges, expr);
2166 __merge_true_states();
2167 __free_fake_cur_stree();
2169 return 1;
2172 static bool is_neg_and_pos_err_code(struct range_list *rl)
2174 struct data_range *tmp, *last;
2176 if (option_project != PROJ_KERNEL)
2177 return false;
2178 if (!rl)
2179 return false;
2181 /* Assume s32min-(14),(-12)-(-1),1-s32max is an error code. */
2182 last = last_ptr_list((struct ptr_list *)rl);
2183 if (last->max.value >= 0 &&
2184 (last->min.value != 1 ||
2185 last->max.value != INT_MAX))
2186 return false;
2189 FOR_EACH_PTR(rl, tmp) {
2190 if (tmp == last)
2191 break;
2192 if (tmp->min.value != INT_MIN && tmp->min.value < -4095)
2193 return false;
2194 if (tmp->max.value < -4095 || tmp->max.value >= 0)
2195 return false;
2196 } END_FOR_EACH_PTR(tmp);
2198 return true;
2201 static bool is_kernel_success_fail(struct sm_state *sm)
2203 struct sm_state *tmp;
2204 struct range_list *rl;
2205 bool has_zero = false;
2206 bool has_neg = false;
2208 if (!sm)
2209 return false;
2211 if (!type_signed(estate_type(sm->state)))
2212 return false;
2214 FOR_EACH_PTR(sm->possible, tmp) {
2215 rl = estate_rl(tmp->state);
2216 if (!rl)
2217 return false;
2218 if (!is_leaf(tmp))
2219 continue;
2220 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2221 has_zero = true;
2222 continue;
2224 has_neg = true;
2225 if (is_neg_and_pos_err_code(estate_rl(tmp->state)))
2226 continue;
2227 return false;
2228 } END_FOR_EACH_PTR(tmp);
2230 return has_zero && has_neg;
2233 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2235 struct expression *tmp_ret;
2236 struct sm_state *sm;
2237 struct range_list *rl;
2238 struct range_list *nonzero_rl;
2239 sval_t zero_sval;
2240 struct range_list *zero_rl = NULL;
2241 int nr_states;
2242 char *return_ranges;
2243 int final_pass_orig = final_pass;
2245 if (option_project != PROJ_KERNEL)
2246 return 0;
2248 nr_states = get_db_state_count();
2249 if (nr_states > 2000)
2250 return 0;
2252 tmp_ret = get_fake_variable(expr);
2253 if (!tmp_ret)
2254 tmp_ret = expr;
2255 sm = get_returned_sm(tmp_ret);
2256 if (!sm)
2257 return 0;
2258 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2259 return 0;
2260 if (!is_kernel_success_fail(sm))
2261 return 0;
2263 rl = estate_rl(sm->state);
2264 if (!rl)
2265 return 0;
2267 __push_fake_cur_stree();
2269 final_pass = 0;
2270 __split_whole_condition(tmp_ret);
2271 final_pass = final_pass_orig;
2273 nonzero_rl = rl_filter(rl, rl_zero());
2274 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2275 return_ranges = show_rl(nonzero_rl);
2276 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2278 call_return_states_callbacks(return_ranges, expr);
2280 __push_true_states();
2281 __use_false_states();
2283 return_ranges = alloc_sname("0");
2284 zero_sval = sval_type_val(rl_type(rl), 0);
2285 add_range(&zero_rl, zero_sval, zero_sval);
2286 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2287 call_return_states_callbacks(return_ranges, expr);
2289 __merge_true_states();
2290 __free_fake_cur_stree();
2292 return 1;
2295 static int is_boolean(struct expression *expr)
2297 struct range_list *rl;
2299 if (!get_implied_rl(expr, &rl))
2300 return 0;
2301 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2302 return 1;
2303 return 0;
2306 static int splitable_function_call(struct expression *expr)
2308 struct sm_state *sm;
2310 if (!expr || expr->type != EXPR_CALL)
2311 return 0;
2312 sm = get_extra_sm_state(expr);
2313 return split_possible_helper(sm, expr);
2316 static struct sm_state *find_bool_param(void)
2318 struct stree *start_states;
2319 struct symbol *arg;
2320 struct sm_state *sm, *tmp;
2321 sval_t sval;
2323 start_states = get_start_states();
2325 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2326 if (!arg->ident)
2327 continue;
2328 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2329 if (!sm)
2330 continue;
2331 if (rl_min(estate_rl(sm->state)).value != 0 ||
2332 rl_max(estate_rl(sm->state)).value != 1)
2333 continue;
2334 goto found;
2335 } END_FOR_EACH_PTR_REVERSE(arg);
2337 return NULL;
2339 found:
2341 * Check if it's splitable. If not, then splitting it up is likely not
2342 * useful for the callers.
2344 FOR_EACH_PTR(sm->possible, tmp) {
2345 if (is_merged(tmp))
2346 continue;
2347 if (!estate_get_single_value(tmp->state, &sval))
2348 return NULL;
2349 } END_FOR_EACH_PTR(tmp);
2351 return sm;
2354 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2356 struct range_list *ret_rl;
2357 const char *return_ranges;
2358 struct sm_state *tmp;
2359 int ret = 0;
2360 struct state_list *already_handled = NULL;
2362 if (!sm || !sm->merged)
2363 return 0;
2365 if (too_many_possible(sm))
2366 return 0;
2368 FOR_EACH_PTR(sm->possible, tmp) {
2369 if (!is_leaf(tmp))
2370 continue;
2371 if (ptr_in_list(tmp, already_handled))
2372 continue;
2373 add_ptr_list(&already_handled, tmp);
2375 ret = 1;
2376 __push_fake_cur_stree();
2378 overwrite_states_using_pool(sm, tmp);
2380 return_ranges = get_return_ranges_str(expr, &ret_rl);
2381 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2382 call_return_states_callbacks(return_ranges, expr);
2384 __free_fake_cur_stree();
2385 } END_FOR_EACH_PTR(tmp);
2387 free_slist(&already_handled);
2389 return ret;
2392 static int split_by_bool_param(struct expression *expr)
2394 struct sm_state *start_sm, *sm;
2395 sval_t sval;
2397 start_sm = find_bool_param();
2398 if (!start_sm)
2399 return 0;
2400 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2401 if (!sm || estate_get_single_value(sm->state, &sval))
2402 return 0;
2404 if (get_db_state_count() * 2 >= 2000)
2405 return 0;
2407 return split_on_bool_sm(sm, expr);
2410 static int split_by_null_nonnull_param(struct expression *expr)
2412 struct symbol *arg;
2413 struct sm_state *sm;
2414 int nr_possible;
2416 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2417 if (!arg || !arg->ident)
2418 return 0;
2419 if (get_real_base_type(arg)->type != SYM_PTR)
2420 return 0;
2422 if (param_was_set_var_sym(arg->ident->name, arg))
2423 return 0;
2424 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2425 if (!sm)
2426 return 0;
2428 if (!has_separate_zero_null(sm))
2429 return 0;
2431 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2432 if (get_db_state_count() * nr_possible >= 2000)
2433 return 0;
2435 return split_on_bool_sm(sm, expr);
2438 static void call_hooks_based_on_pool(struct expression *expr, struct sm_state *gate_sm, struct sm_state *pool_sm)
2440 struct range_list *ret_rl;
2441 const char *return_ranges;
2443 __push_fake_cur_stree();
2445 overwrite_states_using_pool(gate_sm, pool_sm);
2447 return_ranges = get_return_ranges_str(expr, &ret_rl);
2448 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2449 call_return_states_callbacks(return_ranges, expr);
2451 __free_fake_cur_stree();
2454 static bool split_by_impossible(struct expression *expr)
2456 static int impossible_id;
2457 struct sm_state *sm, *tmp;
2458 int nr_states;
2460 if (!impossible_id)
2461 impossible_id = id_from_name("register_impossible_return");
2462 if (!impossible_id)
2463 return false;
2466 * The only states for register_impossible_return are &impossible,
2467 * &undefined and &merged. This function will break otherwise.
2470 sm = get_sm_state(impossible_id, "impossible", NULL);
2471 if (!sm || sm->state != &merged)
2472 return false;
2474 nr_states = get_db_state_count();
2475 if (nr_states >= 1000)
2476 return false;
2478 /* handle possible */
2479 FOR_EACH_PTR(sm->possible, tmp) {
2480 if (!is_leaf(tmp))
2481 continue;
2482 if (tmp->state != &undefined)
2483 continue;
2484 call_hooks_based_on_pool(expr, sm, tmp);
2485 goto impossible;
2486 } END_FOR_EACH_PTR(tmp);
2488 impossible:
2489 /* handle impossible */
2490 FOR_EACH_PTR(sm->possible, tmp) {
2491 if (!is_leaf(tmp))
2492 continue;
2493 if (strcmp(tmp->state->name, "impossible") != 0)
2494 continue;
2495 call_hooks_based_on_pool(expr, sm, tmp);
2496 return true;
2497 } END_FOR_EACH_PTR(tmp);
2499 return false;
2502 struct expression *strip_expr_statement(struct expression *expr)
2504 struct expression *orig = expr;
2505 struct statement *stmt, *last_stmt;
2507 if (!expr)
2508 return NULL;
2509 if (expr->type == EXPR_PREOP && expr->op == '(')
2510 expr = expr->unop;
2511 if (expr->type != EXPR_STATEMENT)
2512 return orig;
2513 stmt = expr->statement;
2514 if (!stmt || stmt->type != STMT_COMPOUND)
2515 return orig;
2517 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2518 if (!last_stmt || last_stmt->type == STMT_LABEL)
2519 last_stmt = last_stmt->label_statement;
2520 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2521 return orig;
2522 return strip_expr(last_stmt->expression);
2525 static bool is_kernel_error_path(struct expression *expr)
2527 struct range_list *rl;
2529 if (option_project != PROJ_KERNEL)
2530 return false;
2532 if (!get_implied_rl(expr, &rl))
2533 return false;
2534 if (rl_type(rl) != &int_ctype)
2535 return false;
2536 if (!is_neg_and_pos_err_code(rl))
2537 return false;
2538 return true;
2541 static void call_return_state_hooks(struct expression *expr)
2543 struct range_list *ret_rl;
2544 const char *return_ranges;
2545 int nr_states;
2546 sval_t sval;
2548 if (debug_db) {
2549 struct range_list *rl = NULL;
2551 get_absolute_rl(expr, &rl);
2552 sm_msg("RETURN: expr='%s' rl='%s' %lu states%s", expr_to_str(expr),
2553 show_rl(rl), stree_count(__get_cur_stree()),
2554 is_impossible_path() ? " (impossible path)" : "");
2557 if (__path_is_null())
2558 return;
2560 if (is_impossible_path())
2561 goto vanilla;
2563 if (expr && (expr->type == EXPR_COMPARE ||
2564 !get_implied_value(expr, &sval)) &&
2565 (is_condition(expr) || is_boolean(expr))) {
2566 call_return_state_hooks_compare(expr);
2567 if (debug_db)
2568 sm_msg("%s: bool", __func__);
2569 return;
2570 } else if (call_return_state_hooks_conditional(expr)) {
2571 if (debug_db)
2572 sm_msg("%s: condition", __func__);
2573 return;
2574 } else if (is_kernel_error_path(expr)) {
2575 if (debug_db)
2576 sm_msg("%s: kernel error path", __func__);
2577 goto vanilla;
2578 } else if (call_return_state_hooks_split_success_fail(expr)) {
2579 if (debug_db)
2580 sm_msg("%s: success_fail", __func__);
2581 return;
2582 } else if (call_return_state_hooks_split_possible(expr)) {
2583 if (debug_db)
2584 sm_msg("%s: split_possible", __func__);
2585 return;
2586 } else if (split_positive_from_negative(expr)) {
2587 if (debug_db)
2588 sm_msg("%s: positive negative", __func__);
2589 return;
2590 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2591 if (debug_db)
2592 sm_msg("%s: split zero non-zero", __func__);
2593 return;
2594 } else if (splitable_function_call(expr)) {
2595 if (debug_db)
2596 sm_msg("%s: split_function_call", __func__);
2597 return;
2598 } else if (split_by_bool_param(expr)) {
2599 if (debug_db)
2600 sm_msg("%s: bool param", __func__);
2601 return;
2602 } else if (split_by_null_nonnull_param(expr)) {
2603 if (debug_db)
2604 sm_msg("%s: null non-null param", __func__);
2605 return;
2606 } else if (split_by_impossible(expr)) {
2607 if (debug_db)
2608 sm_msg("%s: split by impossible", __func__);
2609 return;
2612 vanilla:
2613 return_ranges = get_return_ranges_str(expr, &ret_rl);
2614 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2616 nr_states = get_db_state_count();
2617 if (nr_states >= 10000) {
2618 return_id++;
2619 match_return_info(return_id, (char *)return_ranges, expr);
2620 print_limited_param_set(return_id, (char *)return_ranges, expr);
2621 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2622 return;
2624 call_return_states_callbacks(return_ranges, expr);
2625 if (debug_db)
2626 sm_msg("%s: vanilla", __func__);
2629 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2631 struct returned_member_callback *cb;
2632 struct sm_state *sm;
2633 struct symbol *type;
2634 char *name;
2635 char member_name[256];
2636 int len;
2638 type = get_type(expr);
2639 if (!type || type->type != SYM_PTR)
2640 return;
2641 name = expr_to_var(expr);
2642 if (!name)
2643 return;
2645 len = strlen(name);
2646 FOR_EACH_PTR(returned_member_callbacks, cb) {
2647 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2648 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2649 strcpy(member_name, "*$");
2650 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2651 continue;
2653 if (strncmp(sm->name, name, len) != 0)
2654 continue;
2655 if (strncmp(sm->name + len, "->", 2) != 0)
2656 continue;
2657 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2658 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2659 } END_FOR_EACH_SM(sm);
2660 } END_FOR_EACH_PTR(cb);
2662 free_string(name);
2665 static void print_return_struct_info(int return_id, char *return_ranges,
2666 struct expression *expr,
2667 struct symbol *sym,
2668 struct return_info_callback *cb)
2670 struct sm_state *sm;
2671 const char *printed_name;
2672 int param;
2674 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2675 param = get_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2676 if (!printed_name)
2677 continue;
2678 if (param < 0)
2679 continue;
2680 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2681 } END_FOR_EACH_SM(sm);
2683 /* always print returned states after processing param states */
2684 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2685 param = get_return_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2686 if (param != -1 || !printed_name)
2687 continue;
2688 cb->callback(return_id, return_ranges, expr, -1, printed_name, sm);
2689 } END_FOR_EACH_SM(sm);
2692 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2694 struct return_info_callback *cb;
2695 struct expression *tmp;
2696 struct symbol *sym;
2698 if (!option_info && !__inline_fn &&
2699 !local_debug && !option_debug)
2700 return;
2702 tmp = get_fake_variable(expr);
2703 if (tmp)
2704 expr = tmp;
2705 sym = expr_to_sym(expr);
2707 FOR_EACH_PTR(return_callbacks, cb) {
2708 __ignore_param_used++;
2709 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2710 __ignore_param_used--;
2711 } END_FOR_EACH_PTR(cb);
2714 static void reset_memdb(struct symbol *sym)
2716 mem_sql(NULL, NULL, "delete from caller_info;");
2717 mem_sql(NULL, NULL, "delete from return_states;");
2718 mem_sql(NULL, NULL, "delete from call_implies;");
2719 mem_sql(NULL, NULL, "delete from return_implies;");
2722 static void match_end_func_info(struct symbol *sym)
2724 if (__path_is_null())
2725 return;
2726 call_return_state_hooks(NULL);
2729 static void match_after_func(struct symbol *sym)
2731 clear_cached_return_vals();
2732 if (!__inline_fn)
2733 reset_memdb(sym);
2736 static void init_memdb(void)
2738 char *err = NULL;
2739 int rc;
2740 const char *schema_files[] = {
2741 "db/db.schema",
2742 "db/caller_info.schema",
2743 "db/common_caller_info.schema",
2744 "db/return_states.schema",
2745 "db/function_type_size.schema",
2746 "db/type_size.schema",
2747 "db/function_type_info.schema",
2748 "db/type_info.schema",
2749 "db/call_implies.schema",
2750 "db/return_implies.schema",
2751 "db/function_ptr.schema",
2752 "db/local_values.schema",
2753 "db/function_type_value.schema",
2754 "db/type_value.schema",
2755 "db/function_type.schema",
2756 "db/data_info.schema",
2757 "db/parameter_name.schema",
2758 "db/constraints.schema",
2759 "db/constraints_required.schema",
2760 "db/fn_ptr_data_link.schema",
2761 "db/fn_data_link.schema",
2762 "db/mtag_about.schema",
2763 "db/mtag_info.schema",
2764 "db/mtag_map.schema",
2765 "db/mtag_data.schema",
2766 "db/mtag_alias.schema",
2768 static char buf[4096];
2769 int fd;
2770 int ret;
2771 int i;
2773 rc = sqlite3_open(":memory:", &mem_db);
2774 if (rc != SQLITE_OK) {
2775 sm_ierror("starting In-Memory database.");
2776 return;
2779 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2780 fd = open_schema_file(schema_files[i]);
2781 if (fd < 0)
2782 continue;
2783 ret = read(fd, buf, sizeof(buf));
2784 if (ret < 0) {
2785 sm_ierror("failed to read: %s", schema_files[i]);
2786 continue;
2788 close(fd);
2789 if (ret == sizeof(buf)) {
2790 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2791 schema_files[i], sizeof(buf));
2792 continue;
2794 buf[ret] = '\0';
2795 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2796 if (rc != SQLITE_OK) {
2797 sm_ierror("SQL error #2: %s", err);
2798 sm_ierror("%s", buf);
2803 static void init_cachedb(void)
2805 char *err = NULL;
2806 int rc;
2807 const char *schema_files[] = {
2808 "db/call_implies.schema",
2809 "db/return_implies.schema",
2810 "db/type_info.schema",
2811 "db/mtag_about.schema",
2812 "db/mtag_data.schema",
2813 "db/mtag_info.schema",
2814 "db/sink_info.schema",
2815 "db/hash_string.schema",
2817 static char buf[4096];
2818 int fd;
2819 int ret;
2820 int i;
2822 rc = sqlite3_open(":memory:", &cache_db);
2823 if (rc != SQLITE_OK) {
2824 sm_ierror("starting In-Memory database.");
2825 return;
2828 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2829 fd = open_schema_file(schema_files[i]);
2830 if (fd < 0)
2831 continue;
2832 ret = read(fd, buf, sizeof(buf));
2833 if (ret < 0) {
2834 sm_ierror("failed to read: %s", schema_files[i]);
2835 continue;
2837 close(fd);
2838 if (ret == sizeof(buf)) {
2839 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2840 schema_files[i], sizeof(buf));
2841 continue;
2843 buf[ret] = '\0';
2844 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2845 if (rc != SQLITE_OK) {
2846 sm_ierror("SQL error #2: %s", err);
2847 sm_ierror("%s", buf);
2852 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2854 static char buf[4096];
2855 char tmp[256];
2856 char *p = buf;
2857 char *table = _table;
2858 int i;
2861 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2862 for (i = 0; i < argc; i++) {
2863 if (i)
2864 p += snprintf(p, 4096 - (p - buf), ", ");
2865 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2866 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2869 p += snprintf(p, 4096 - (p - buf), ");");
2870 if (p - buf > 4096)
2871 return 0;
2873 sm_msg("SQL: %s", buf);
2874 return 0;
2877 static void dump_cache(struct symbol_list *sym_list)
2879 const char *cache_tables[] = {
2880 "type_info", "return_implies", "call_implies", "mtag_data",
2881 "mtag_info", "mtag_about", "sink_info", "hash_string",
2883 char buf[64];
2884 int i;
2886 if (!option_info)
2887 return;
2889 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2890 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2891 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2895 void open_smatch_db(char *db_file)
2897 int rc;
2899 if (option_no_db)
2900 return;
2902 use_states = malloc(num_checks);
2903 memset(use_states, 0xff, num_checks);
2905 init_memdb();
2906 init_cachedb();
2908 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2909 if (rc != SQLITE_OK) {
2910 option_no_db = 1;
2911 return;
2913 run_sql(NULL, NULL,
2914 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2915 return;
2918 static char *get_next_string(char **str)
2920 static char string[256];
2921 char *start;
2922 char *p = *str;
2923 int len, i, j;
2925 if (*p == '\0')
2926 return NULL;
2927 start = p;
2929 while (*p != '\0' && *p != '\n') {
2930 if (*p == '\\' && *(p + 1) == ' ') {
2931 p += 2;
2932 continue;
2934 if (*p == ' ')
2935 break;
2936 p++;
2939 len = p - start;
2940 if (len >= sizeof(string)) {
2941 memcpy(string, start, sizeof(string));
2942 string[sizeof(string) - 1] = '\0';
2943 sm_ierror("return_fix: '%s' too long", string);
2944 **str = '\0';
2945 return NULL;
2947 memcpy(string, start, len);
2948 string[len] = '\0';
2949 for (i = 0; i < sizeof(string) - 1; i++) {
2950 if (string[i] == '\\' && string[i + 1] == ' ') {
2951 for (j = i; string[j] != '\0'; j++)
2952 string[j] = string[j + 1];
2955 if (*p != '\0')
2956 p++;
2957 *str = p;
2958 return string;
2961 static void register_return_deletes(void)
2963 char *func, *ret_str;
2964 char filename[256];
2965 char buf[4096];
2966 int fd, ret, i;
2967 char *p;
2969 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2970 fd = open_schema_file(filename);
2971 if (fd < 0)
2972 return;
2973 ret = read(fd, buf, sizeof(buf));
2974 close(fd);
2975 if (ret < 0)
2976 return;
2977 if (ret == sizeof(buf)) {
2978 sm_ierror("file too large: %s (limit %zd bytes)",
2979 filename, sizeof(buf));
2980 return;
2982 buf[ret] = '\0';
2984 p = buf;
2985 while (*p) {
2986 get_next_string(&p);
2987 delete_count++;
2989 if (delete_count == 0)
2990 return;
2991 if (delete_count % 2 != 0) {
2992 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2993 delete_count = 0;
2994 return;
2996 delete_table = malloc(delete_count * sizeof(char *));
2998 p = buf;
2999 i = 0;
3000 while (*p) {
3001 func = alloc_string(get_next_string(&p));
3002 ret_str = alloc_string(get_next_string(&p));
3004 delete_table[i++] = func;
3005 delete_table[i++] = ret_str;
3009 #define RETURN_FIX_SIZE 8196
3010 static void register_return_replacements(void)
3012 char *func, *orig, *new;
3013 char filename[256];
3014 int fd, ret, i;
3015 char *buf;
3016 char *p;
3018 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
3019 fd = open_schema_file(filename);
3020 if (fd < 0)
3021 return;
3022 buf = malloc(RETURN_FIX_SIZE);
3023 ret = read(fd, buf, RETURN_FIX_SIZE);
3024 close(fd);
3025 if (ret < 0) {
3026 free(buf);
3027 return;
3029 if (ret == RETURN_FIX_SIZE) {
3030 sm_ierror("file too large: %s (limit %d bytes)",
3031 filename, RETURN_FIX_SIZE);
3032 free(buf);
3033 return;
3035 buf[ret] = '\0';
3037 p = buf;
3038 while (*p) {
3039 get_next_string(&p);
3040 replace_count++;
3042 if (replace_count == 0) {
3043 free(buf);
3044 return;
3046 if (replace_count % 3 != 0) {
3047 printf("error parsing '%s' replace_count=%d\n", filename, replace_count);
3048 replace_count = 0;
3049 free(buf);
3050 return;
3052 replace_table = malloc(replace_count * sizeof(char *));
3054 p = buf;
3055 i = 0;
3056 while (*p) {
3057 func = alloc_string(get_next_string(&p));
3058 orig = alloc_string(get_next_string(&p));
3059 new = alloc_string(get_next_string(&p));
3061 replace_table[i++] = func;
3062 replace_table[i++] = orig;
3063 replace_table[i++] = new;
3065 free(buf);
3068 static void register_forced_return_splits(void)
3070 int struct_members = sizeof(struct split_data) / sizeof(char *);
3071 char filename[256];
3072 char buf[4096];
3073 int fd, ret, i;
3074 char *p;
3076 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
3077 fd = open_schema_file(filename);
3078 if (fd < 0)
3079 return;
3080 ret = read(fd, buf, sizeof(buf));
3081 close(fd);
3082 if (ret < 0)
3083 return;
3084 if (ret == sizeof(buf)) {
3085 sm_ierror("file too large: %s (limit %zd bytes)",
3086 filename, sizeof(buf));
3087 return;
3089 buf[ret] = '\0';
3091 p = buf;
3092 while (*p) {
3093 get_next_string(&p);
3094 split_count++;
3096 if (split_count == 0)
3097 return;
3098 if (split_count % struct_members != 0) {
3099 printf("error parsing '%s' split_count=%d\n", filename, split_count);
3100 split_count = 0;
3101 return;
3103 split_count /= struct_members;
3104 forced_splits = malloc(split_count * sizeof(void *));
3106 p = buf;
3107 i = 0;
3108 while (*p) {
3109 struct split_data *split = malloc(sizeof(*split));
3111 split->func = alloc_string(get_next_string(&p));
3112 split->rl = alloc_string(get_next_string(&p));
3113 forced_splits[i++] = split;
3117 void register_definition_db_callbacks(int id)
3119 my_id = id;
3121 add_hook(&match_call_info, FUNCTION_CALL_HOOK_BEFORE);
3122 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK_BEFORE);
3123 add_split_return_callback(match_return_info);
3124 add_split_return_callback(print_returned_struct_members);
3125 add_split_return_callback(print_return_info);
3126 add_hook(&call_return_state_hooks, RETURN_HOOK);
3127 add_hook(&match_end_func_info, END_FUNC_HOOK);
3128 add_hook(&match_after_func, AFTER_FUNC_HOOK);
3130 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
3131 add_hook(&match_call_implies, FUNC_DEF_HOOK);
3132 add_hook(&match_return_implies_early, CALL_HOOK_AFTER_INLINE);
3134 common_funcs = load_strings_from_file(option_project_str, "common_functions");
3135 register_return_deletes();
3136 register_return_replacements();
3137 register_forced_return_splits();
3139 add_hook(&dump_cache, END_FILE_HOOK);
3142 void register_definition_db_callbacks_late(int id)
3144 add_hook(&match_return_implies_late, CALL_HOOK_AFTER_INLINE);
3147 void register_db_call_marker(int id)
3149 add_hook(&match_call_marker, FUNCTION_CALL_HOOK_BEFORE);
3152 char *get_data_info_name(struct expression *expr)
3154 struct symbol *sym;
3155 char *name;
3156 char buf[256];
3157 char *ret = NULL;
3159 expr = strip_expr(expr);
3160 name = get_member_name(expr);
3161 if (name)
3162 return name;
3163 name = expr_to_var_sym(expr, &sym);
3164 if (!name || !sym)
3165 goto free;
3166 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
3167 goto free;
3168 if (sym->ctype.modifiers & MOD_STATIC)
3169 snprintf(buf, sizeof(buf), "static %s", name);
3170 else
3171 snprintf(buf, sizeof(buf), "global %s", name);
3172 ret = alloc_sname(buf);
3173 free:
3174 free_string(name);
3175 return ret;