hooks: add a warning to detect unimplemented hooks
[smatch.git] / smatch_db.c
blob814e3a3a4c7cf5f97e9e72ebcf2497da4a70cb53
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 unsigned long long get_base_file_id(void)
289 return str_to_llu_hash(get_base_file());
292 void sql_insert_return_states(int return_id, const char *return_ranges,
293 int type, int param, const char *key, const char *value)
295 unsigned long long id;
298 if (key && strlen(key) >= 80)
299 return;
300 if (__inline_fn)
301 id = (unsigned long)__inline_fn;
302 else
303 id = __fn_mtag;
305 sql_insert(return_states, "0x%llx, '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
306 get_base_file_id(), get_function(), id, return_id,
307 return_ranges, is_local(cur_func_sym), type, param, key, value);
310 static struct string_list *common_funcs;
311 static int is_common_function(const char *fn)
313 char *tmp;
315 if (!fn)
316 return 0;
318 if (strncmp(fn, "__builtin_", 10) == 0)
319 return 1;
321 FOR_EACH_PTR(common_funcs, tmp) {
322 if (strcmp(tmp, fn) == 0)
323 return 1;
324 } END_FOR_EACH_PTR(tmp);
326 return 0;
329 static char *function_signature(void)
331 return type_to_str(get_real_base_type(cur_func_sym));
334 void sql_insert_caller_info(struct expression *call, int type,
335 int param, const char *key, const char *value)
337 FILE *tmp_fd = sm_outfd;
338 char *fn;
340 if (!option_info && !__inline_call)
341 return;
342 if (unreachable())
343 return;
345 if (key && strlen(key) >= 80)
346 return;
348 fn = get_fnptr_name(call->fn);
349 if (!fn)
350 return;
352 if (__inline_call) {
353 mem_sql(NULL, NULL,
354 "insert into caller_info values (0x%llx, '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
355 get_base_file_id(), get_function(), fn, (unsigned long)call,
356 is_static(call->fn), type, param, key, value);
359 if (!option_info)
360 return;
362 if (strncmp(fn, "__builtin_", 10) == 0)
363 return;
364 if (type != INTERNAL && is_common_function(fn))
365 return;
367 sm_outfd = caller_info_fd;
368 sm_msg("SQL_caller_info: insert into caller_info values ("
369 "0x%llx, '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
370 get_base_file_id(), get_function(), fn, is_static(call->fn),
371 type, param, key, value);
372 sm_outfd = tmp_fd;
374 free_string(fn);
377 void sql_insert_function_ptr(const char *fn, const char *struct_name)
379 sql_insert_or_ignore(function_ptr, "0x%llx, '%s', '%s', 0",
380 get_base_file_id(), fn, struct_name);
383 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
385 sql_insert_or_ignore(return_implies, "0x%llx, '%s', %lu, %d, %d, %d, '%s', '%s'",
386 get_base_file_id(), get_function(), (unsigned long)__inline_fn,
387 fn_static(), type, param, key, value);
390 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
392 sql_insert_or_ignore(call_implies, "0x%llx, '%s', %lu, %d, %d, %d, '%s', '%s'",
393 get_base_file_id(), get_function(), (unsigned long)__inline_fn,
394 fn_static(), type, param, key, value);
397 void sql_insert_function_type_size(const char *member, const char *ranges)
399 sql_insert(function_type_size, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), member, ranges);
402 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
404 sql_insert(function_type_info, "0x%llx, '%s', %d, '%s', '%s', '%s'", get_base_file_id(), get_function(), type, struct_type, member, value);
407 void sql_insert_type_info(int type, const char *member, const char *value)
409 sql_insert_cache(type_info, "0x%llx, %d, '%s', '%s'", get_base_file_id(), type, member, value);
412 void sql_insert_local_values(const char *name, const char *value)
414 sql_insert(local_values, "0x%llx, '%s', '%s'", get_base_file_id(), name, value);
417 void sql_insert_function_type_value(const char *type, const char *value)
419 sql_insert(function_type_value, "0x%llx, '%s', '%s', '%s'", get_base_file_id(), get_function(), type, value);
422 void sql_insert_function_type(int param, const char *value)
424 sql_insert(function_type, "0x%llx, '%s', %d, %d, '%s'",
425 get_base_file_id(), get_function(), fn_static(), param, value);
428 void sql_insert_parameter_name(int param, const char *value)
430 sql_insert(parameter_name, "0x%llx, '%s', %d, %d, '%s'",
431 get_base_file_id(), get_function(), fn_static(), param, value);
434 void sql_insert_data_info(struct expression *data, int type, const char *value)
436 char *data_name;
438 data_name = get_data_info_name(data);
439 if (!data_name)
440 return;
441 sql_insert(data_info, "0x%llx, '%s', %d, '%s'",
442 is_static(data) ? get_base_file_id() : 0,
443 data_name, type, value);
446 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
448 sql_insert(data_info, "0x%llx, '%s', %d, '%s'",
449 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file_id() : 0,
450 var, type, value);
453 void sql_save_constraint(const char *con)
455 if (!option_info)
456 return;
458 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
461 void sql_save_constraint_required(const char *data, int op, const char *limit)
463 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
466 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
468 if (!option_info)
469 return;
471 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
472 "select constraints_required.data, constraints_required.op, '%s' from "
473 "constraints_required where bound = '%s';", new_limit, old_limit);
476 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
478 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
481 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
483 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
484 return;
486 sql_insert(fn_data_link, "0x%llx, '%s', %d, %d, %d, '%s', '%s'",
487 is_local(fn->symbol) ? get_base_file_id() : 0,
488 fn->symbol->ident->name,
489 is_local(fn->symbol),
490 type, param, key, value);
493 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
495 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
496 tag, get_filename(), get_function(), get_lineno(),
497 left_name, right_name);
500 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
502 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
505 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
507 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
510 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
512 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
515 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
517 mtag_t *saved_tag = _tag;
518 mtag_t new_tag;
520 new_tag = strtoll(argv[0], NULL, 10);
522 if (!*saved_tag)
523 *saved_tag = new_tag;
524 else if (*saved_tag != new_tag)
525 *saved_tag = -1ULL;
527 return 0;
530 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
532 mtag_t tmp = 0;
534 run_sql(save_mtag, &tmp,
535 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
536 tag, container_offset);
538 if (tmp == 0 || tmp == -1ULL)
539 return 0;
540 *container = tmp;
541 return 1;
544 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
546 mtag_t tmp = 0;
548 run_sql(save_mtag, &tmp,
549 "select tag from mtag_map where container = %lld and container_offset = %d;",
550 container, offset);
552 if (tmp == 0 || tmp == -1ULL)
553 return 0;
554 *tag = tmp;
555 return 1;
558 char *get_static_filter(struct symbol *sym)
560 static char sql_filter[1024];
562 /* This can only happen on buggy code. Return invalid SQL. */
563 if (!sym) {
564 sql_filter[0] = '\0';
565 return sql_filter;
568 if (is_local(sym)) {
569 snprintf(sql_filter, sizeof(sql_filter),
570 "file = 0x%llx and function = '%s' and static = '1'",
571 get_base_file_id(), sym->ident->name);
572 } else {
573 snprintf(sql_filter, sizeof(sql_filter),
574 "function = '%s' and static = '0'", sym->ident->name);
577 return sql_filter;
580 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
582 int *row_count = _row_count;
584 *row_count = 0;
585 if (argc != 1)
586 return 0;
587 *row_count = atoi(argv[0]);
588 return 0;
591 static void mark_call_params_untracked(struct expression *call)
593 struct expression *arg;
594 int i = 0;
596 FOR_EACH_PTR(call->args, arg) {
597 mark_untracked(call, i++, "$", NULL);
598 } END_FOR_EACH_PTR(arg);
601 static void sql_select_return_states_pointer(const char *cols,
602 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
604 char *ptr;
605 int return_count = 0;
607 ptr = get_fnptr_name(call->fn);
608 if (!ptr)
609 return;
611 run_sql(get_row_count, &return_count,
612 "select count(*) from return_states join function_ptr "
613 "where return_states.function == function_ptr.function and "
614 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
615 /* The magic number 100 is just from testing on the kernel. */
616 if (return_count == 0 || return_count > 100) {
617 run_sql(callback, info,
618 "select distinct %s from return_states join function_ptr where "
619 "return_states.function == function_ptr.function and ptr = '%s' "
620 "and searchable = 1 and type = %d "
621 "order by function_ptr.file, return_states.file, return_id, type;",
622 cols, ptr, INTERNAL);
623 mark_call_params_untracked(call);
624 return;
627 run_sql(callback, info,
628 "select %s from return_states join function_ptr where "
629 "return_states.function == function_ptr.function and ptr = '%s' "
630 "and searchable = 1 "
631 "order by function_ptr.file, return_states.file, return_id, type;",
632 cols, ptr);
635 static int is_local_symbol(struct expression *expr)
637 if (expr->type != EXPR_SYMBOL)
638 return 0;
639 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
640 return 0;
641 return 1;
644 bool is_fn_ptr(struct expression *fn)
646 fn = strip_expr(fn);
647 if (fn->type != EXPR_SYMBOL)
648 return true;
649 if (!fn->symbol)
650 return true;
651 if (is_local_symbol(fn))
652 return true;
653 return false;
656 void sql_select_return_states(const char *cols, struct expression *call,
657 int (*callback)(void*, int, char**, char**), void *info)
659 struct expression *fn;
660 int row_count = 0;
662 if (is_fake_call(call))
663 return;
665 fn = strip_expr(call->fn);
666 if (is_fn_ptr(fn)) {
667 sql_select_return_states_pointer(cols, call, callback, info);
668 return;
671 if (inlinable(fn)) {
672 mem_sql(callback, info,
673 "select %s from return_states where call_id = '%lu' order by return_id, type;",
674 cols, (unsigned long)call);
675 return;
678 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
679 get_static_filter(fn->symbol));
680 if (row_count == 0 && fn->symbol && fn->symbol->definition)
681 set_state(my_id, "db_incomplete", NULL, &incomplete);
682 if (row_count == 0 || row_count > 3000) {
683 mark_call_params_untracked(call);
684 return;
687 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
688 cols, get_static_filter(fn->symbol));
691 bool db_incomplete(void)
693 return !!get_state(my_id, "db_incomplete", NULL);
696 #define CALL_IMPLIES 0
697 #define RETURN_IMPLIES 1
699 struct implies_info {
700 int type;
701 struct db_implies_cb_list *cb_list;
702 struct expression *expr;
703 struct symbol *sym;
706 void sql_select_implies(const char *cols, struct implies_info *info,
707 int (*callback)(void*, int, char**, char**))
709 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
710 mem_sql(callback, info,
711 "select %s from return_implies where call_id = '%lu';",
712 cols, (unsigned long)info->expr);
713 return;
716 run_sql(callback, info, "select %s from %s_implies where %s;",
717 cols,
718 info->type == CALL_IMPLIES ? "call" : "return",
719 get_static_filter(info->sym));
722 struct select_caller_info_data {
723 struct stree *final_states;
724 struct timeval start_time;
725 int prev_func_id;
726 int ignore;
727 int results;
730 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
732 static void sql_select_caller_info(struct select_caller_info_data *data,
733 const char *cols, struct symbol *sym)
735 if (__inline_fn) {
736 mem_sql(caller_info_callback, data,
737 "select %s from caller_info where call_id = %lu;",
738 cols, (unsigned long)__inline_fn);
739 return;
742 if (is_common_function(sym->ident->name))
743 return;
744 run_sql(caller_info_callback, data,
745 "select %s from common_caller_info where %s order by call_id;",
746 cols, get_static_filter(sym));
747 if (data->results)
748 return;
750 run_sql(caller_info_callback, data,
751 "select %s from caller_info where %s order by call_id;",
752 cols, get_static_filter(sym));
755 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
757 struct def_callback *def_callback = __alloc_def_callback(0);
759 def_callback->hook_type = type;
760 def_callback->callback = callback;
761 add_ptr_list(&select_caller_info_callbacks, def_callback);
764 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
766 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
768 callback->hook_type = type;
769 callback->callback = fn;
770 add_ptr_list(&select_caller_name_sym_callbacks, callback);
774 * These call backs are used when the --info option is turned on to print struct
775 * member information. For example foo->bar could have a state in
776 * smatch_extra.c and also check_user.c.
778 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
780 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
782 member_callback->owner = owner;
783 member_callback->callback = callback;
784 add_ptr_list(&member_callbacks, member_callback);
787 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
789 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
791 member_callback->owner = owner;
792 member_callback->callback = callback;
793 add_ptr_list(&member_callbacks_new, member_callback);
796 void add_return_info_callback(int owner,
797 void (*callback)(int return_id, char *return_ranges,
798 struct expression *returned_expr,
799 int param,
800 const char *printed_name,
801 struct sm_state *sm))
803 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
805 return_callback->owner = owner;
806 return_callback->callback = callback;
807 add_ptr_list(&return_callbacks, return_callback);
810 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
812 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
814 callback->callback = fn;
815 add_ptr_list(&returned_state_callbacks, callback);
818 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))
820 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
822 member_callback->owner = owner;
823 member_callback->callback = callback;
824 add_ptr_list(&returned_member_callbacks, member_callback);
827 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
829 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
831 cb->type = type;
832 cb->callback = callback;
833 add_ptr_list(&call_implies_cb_list, cb);
836 void select_return_implies_hook_early(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
838 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
840 cb->type = type;
841 cb->callback = callback;
842 add_ptr_list(&return_implies_cb_list_early, cb);
845 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
847 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
849 cb->type = type;
850 cb->callback = callback;
851 add_ptr_list(&return_implies_cb_list_late, cb);
854 struct return_info {
855 struct expression *static_returns_call;
856 struct symbol *return_type;
857 struct range_list *return_range_list;
860 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
862 struct return_info *ret_info = _ret_info;
863 struct range_list *rl;
864 struct expression *call_expr = ret_info->static_returns_call;
866 if (argc != 1)
867 return 0;
868 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
869 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
870 return 0;
873 static struct expression *cached_expr, *cached_no_args;
874 static const char *cached_str;
875 static struct range_list *cached_rl, *cached_str_rl, *cached_no_args_rl;
877 static void clear_cached_return_vals(void)
879 cached_expr = NULL;
880 cached_rl = NULL;
881 cached_str = NULL;
882 cached_str_rl = NULL;
883 cached_no_args = NULL;
884 cached_no_args_rl = NULL;
887 struct range_list *db_return_vals(struct expression *expr)
889 struct return_info ret_info = {};
890 struct sm_state *sm;
892 if (!expr)
893 return NULL;
895 if (is_fake_call(expr))
896 return NULL;
898 if (expr == cached_expr)
899 return clone_rl(cached_rl);
901 cached_expr = expr;
902 cached_rl = NULL;
904 sm = get_extra_sm_state(expr);
905 if (sm) {
906 cached_rl = clone_rl(estate_rl(sm->state));
907 return clone_rl(estate_rl(sm->state));
909 ret_info.static_returns_call = expr;
910 ret_info.return_type = get_type(expr);
911 if (!ret_info.return_type)
912 return NULL;
914 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
915 return NULL;
917 ret_info.return_range_list = NULL;
918 if (inlinable(expr->fn)) {
919 mem_sql(db_return_callback, &ret_info,
920 "select distinct return from return_states where call_id = '%lu';",
921 (unsigned long)expr);
922 } else {
923 run_sql(db_return_callback, &ret_info,
924 "select distinct return from return_states where %s;",
925 get_static_filter(expr->fn->symbol));
927 cached_rl = clone_rl(ret_info.return_range_list);
928 return ret_info.return_range_list;
931 struct range_list *db_return_vals_from_str(const char *fn_name)
933 struct return_info ret_info;
935 if (!fn_name)
936 return NULL;
937 if (fn_name == cached_str)
938 return clone_rl(cached_str_rl);
939 cached_str = fn_name;
940 cached_str_rl = NULL;
942 ret_info.static_returns_call = NULL;
943 ret_info.return_type = &llong_ctype;
944 ret_info.return_range_list = NULL;
946 run_sql(db_return_callback, &ret_info,
947 "select distinct return from return_states where function = '%s';",
948 fn_name);
949 cached_str_rl = clone_rl(ret_info.return_range_list);
950 return ret_info.return_range_list;
954 * This is used when we have a function that takes a function pointer as a
955 * parameter. "frob(blah, blah, my_function);" We know that the return values
956 * from frob() come from my_funcion() so we want to find the possible returns
957 * of my_function(), but we don't know which arguments are passed to it.
960 struct range_list *db_return_vals_no_args(struct expression *expr)
962 struct return_info ret_info = {};
964 if (!expr || expr->type != EXPR_SYMBOL)
965 return NULL;
967 if (expr == cached_no_args)
968 return clone_rl(cached_no_args_rl);
969 cached_no_args = expr;
970 cached_no_args_rl = NULL;
972 ret_info.static_returns_call = expr;
973 ret_info.return_type = get_type(expr);
974 ret_info.return_type = get_real_base_type(ret_info.return_type);
975 if (!ret_info.return_type)
976 return NULL;
978 run_sql(db_return_callback, &ret_info,
979 "select distinct return from return_states where %s;",
980 get_static_filter(expr->symbol));
982 cached_no_args_rl = clone_rl(ret_info.return_range_list);
983 return ret_info.return_range_list;
986 static void match_call_marker(struct expression *expr)
988 struct symbol *type;
990 type = get_type(expr->fn);
991 if (type && type->type == SYM_PTR)
992 type = get_real_base_type(type);
995 * we just want to record something in the database so that if we have
996 * two calls like: frob(4); frob(some_unkown); then on the receiving
997 * side we know that sometimes frob is called with unknown parameters.
1000 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
1003 int is_recursive_member(const char *name)
1005 char buf[256];
1006 const char *p, *next;
1007 int size;
1009 p = strchr(name, '>');
1010 if (!p)
1011 return 0;
1012 p++;
1013 while (true) {
1014 next = strchr(p, '>');
1015 if (!next)
1016 return 0;
1017 next++;
1019 size = next - p;
1020 if (size >= sizeof(buf))
1021 return 0;
1022 memcpy(buf, p, size);
1023 buf[size] = '\0';
1024 if (strstr(next, buf))
1025 return 1;
1026 p = next;
1030 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
1032 struct symbol *sym;
1033 const char *sm_name;
1034 char *name;
1035 bool is_address = false;
1036 bool add_star = false;
1037 char buf[256];
1038 char *ret = NULL;
1039 int len;
1041 expr = strip_expr(expr);
1042 if (!expr)
1043 return NULL;
1045 if (expr->type == EXPR_PREOP && expr->op == '&') {
1046 expr = strip_expr(expr->unop);
1047 is_address = true;
1050 name = expr_to_var_sym(expr, &sym);
1051 if (!name || !sym)
1052 goto free;
1053 if (sym != sm->sym)
1054 goto free;
1056 sm_name = sm->name;
1057 add_star = false;
1058 if (sm_name[0] == '*') {
1059 add_star = true;
1060 sm_name++;
1063 len = strlen(name);
1064 if (strncmp(name, sm_name, len) != 0)
1065 goto free;
1066 if (sm_name[len] == '\0') {
1067 snprintf(buf, sizeof(buf), "%s%s$",
1068 add_star ? "*" : "", is_address ? "*" : "");
1069 } else {
1070 if (sm_name[len] != '.' && sm_name[len] != '-')
1071 goto free;
1072 if (sm_name[len] == '-')
1073 len++;
1074 // FIXME does is_address really imply that sm_name[len] == '-'
1075 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
1076 sm_name + len);
1079 ret = alloc_sname(buf);
1080 free:
1081 free_string(name);
1082 return ret;
1085 static void print_struct_members(struct expression *call, struct expression *expr, int param,
1086 int owner,
1087 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
1088 bool new)
1090 struct sm_state *sm;
1091 const char *sm_name;
1092 char *name;
1093 struct symbol *sym;
1094 int len;
1095 char printed_name[256];
1096 int is_address = 0;
1097 bool add_star;
1098 struct symbol *type;
1100 expr = strip_expr(expr);
1101 if (!expr)
1102 return;
1103 type = get_type(expr);
1104 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1105 return;
1107 if (expr->type == EXPR_PREOP && expr->op == '&') {
1108 expr = strip_expr(expr->unop);
1109 is_address = 1;
1112 name = expr_to_var_sym(expr, &sym);
1113 if (!name || !sym)
1114 goto free;
1116 len = strlen(name);
1117 FOR_EACH_SM(__get_cur_stree(), sm) {
1118 if (sm->owner != owner || sm->sym != sym)
1119 continue;
1121 sm_name = sm->name;
1122 add_star = false;
1123 if (sm_name[0] == '*') {
1124 add_star = true;
1125 sm_name++;
1127 // FIXME: simplify?
1128 if (!add_star && strcmp(name, sm_name) == 0) {
1129 if (is_address) {
1130 snprintf(printed_name, sizeof(printed_name), "*$");
1131 } else {
1132 if (new)
1133 snprintf(printed_name, sizeof(printed_name), "$");
1134 else
1135 continue;
1137 } else if (add_star && strcmp(name, sm_name) == 0) {
1138 snprintf(printed_name, sizeof(printed_name), "%s*$",
1139 is_address ? "*" : "");
1140 } else if (strncmp(name, sm_name, len) == 0) {
1141 if (sm_name[len] != '.' && sm_name[len] != '-')
1142 continue;
1143 if (is_address && sm_name[len] == '.') {
1144 snprintf(printed_name, sizeof(printed_name),
1145 "%s$->%s", add_star ? "*" : "",
1146 sm_name + len + 1);
1147 } else if (is_address && sm_name[len] == '-') {
1148 snprintf(printed_name, sizeof(printed_name),
1149 "%s(*$)%s", add_star ? "*" : "",
1150 sm_name + len);
1151 } else {
1152 snprintf(printed_name, sizeof(printed_name),
1153 "%s$%s", add_star ? "*" : "",
1154 sm_name + len);
1156 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1157 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1158 continue;
1159 if (is_address && sm_name[len + 1] == '.') {
1160 snprintf(printed_name, sizeof(printed_name),
1161 "&%s$->%s", add_star ? "*" : "",
1162 sm_name + len + 2);
1163 } else if (is_address && sm_name[len] == '-') {
1164 snprintf(printed_name, sizeof(printed_name),
1165 "&%s(*$)%s", add_star ? "*" : "",
1166 sm_name + len + 1);
1167 } else {
1168 snprintf(printed_name, sizeof(printed_name),
1169 "&%s$%s", add_star ? "*" : "",
1170 sm_name + len + 1);
1172 } else {
1173 continue;
1175 if (is_recursive_member(printed_name))
1176 continue;
1177 callback(call, param, printed_name, sm);
1178 } END_FOR_EACH_SM(sm);
1179 free:
1180 free_string(name);
1183 static void match_call_info(struct expression *call)
1185 struct member_info_callback *cb;
1186 struct expression *arg;
1187 int i;
1189 FOR_EACH_PTR(member_callbacks, cb) {
1190 i = -1;
1191 FOR_EACH_PTR(call->args, arg) {
1192 i++;
1193 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1194 } END_FOR_EACH_PTR(arg);
1195 } END_FOR_EACH_PTR(cb);
1198 static struct expression *get_fake_variable(struct expression *expr)
1200 struct expression *tmp;
1202 tmp = expr_get_fake_parent_expr(expr);
1203 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1204 return NULL;
1206 return tmp->left;
1209 static struct sm_state *get_returned_sm(struct expression *expr)
1211 struct expression *fake;
1213 fake = get_fake_variable(expr);
1214 if (fake)
1215 expr = fake;
1217 return get_sm_state_expr(SMATCH_EXTRA, expr);
1220 static void match_call_info_new(struct expression *call)
1222 struct member_info_callback *cb;
1223 struct expression *arg, *tmp;
1224 int i;
1226 if (!option_info && !__inline_call && !local_debug)
1227 return;
1229 FOR_EACH_PTR(member_callbacks_new, cb) {
1230 i = -1;
1231 FOR_EACH_PTR(call->args, arg) {
1232 i++;
1233 tmp = get_fake_variable(arg);
1234 if (!tmp)
1235 tmp = arg;
1236 __ignore_param_used++;
1237 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1238 __ignore_param_used--;
1239 } END_FOR_EACH_PTR(arg);
1240 } END_FOR_EACH_PTR(cb);
1243 static int get_param(int param, char **name, struct symbol **sym)
1245 struct symbol *arg;
1246 int i;
1248 i = 0;
1249 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1250 if (i == param) {
1251 *name = arg->ident->name;
1252 *sym = arg;
1253 return TRUE;
1255 i++;
1256 } END_FOR_EACH_PTR(arg);
1258 return FALSE;
1261 static int function_signature_matches(const char *sig)
1263 char *my_sig;
1265 my_sig = function_signature();
1266 if (!sig || !my_sig)
1267 return 1; /* default to matching */
1268 if (strcmp(my_sig, sig) == 0)
1269 return 1;
1270 return 0;
1273 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1275 struct select_caller_info_data *data = _data;
1276 int func_id;
1277 long type;
1278 long param;
1279 char *key;
1280 char *value;
1281 char *name = NULL;
1282 struct symbol *sym = NULL;
1283 struct def_callback *def_callback;
1284 struct def_name_sym_callback *ns_callback;
1285 struct stree *stree;
1286 struct timeval cur_time;
1287 char fullname[256];
1288 char *p;
1290 data->results = 1;
1292 if (argc != 5)
1293 return 0;
1295 gettimeofday(&cur_time, NULL);
1296 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1297 return 0;
1299 func_id = atoi(argv[0]);
1300 errno = 0;
1301 type = strtol(argv[1], NULL, 10);
1302 param = strtol(argv[2], NULL, 10);
1303 if (errno)
1304 return 0;
1305 key = argv[3];
1306 value = argv[4];
1308 if (data->prev_func_id == -1)
1309 data->prev_func_id = func_id;
1310 if (func_id != data->prev_func_id) {
1311 stree = __pop_fake_cur_stree();
1312 if (!data->ignore)
1313 merge_stree(&data->final_states, stree);
1314 free_stree(&stree);
1315 __push_fake_cur_stree();
1316 __unnullify_path();
1317 data->prev_func_id = func_id;
1318 data->ignore = 0;
1321 if (data->ignore)
1322 return 0;
1323 if (type == INTERNAL &&
1324 !function_signature_matches(value)) {
1325 data->ignore = 1;
1326 return 0;
1329 if (param >= 0 && !get_param(param, &name, &sym))
1330 return 0;
1332 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1333 if (def_callback->hook_type == type)
1334 def_callback->callback(name, sym, key, value);
1335 } END_FOR_EACH_PTR(def_callback);
1337 p = strchr(key, '$');
1338 if (name && p)
1339 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1340 else
1341 snprintf(fullname, sizeof(fullname), "%s", key);
1343 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1344 if (ns_callback->hook_type == type)
1345 ns_callback->callback(fullname, sym, value);
1346 } END_FOR_EACH_PTR(ns_callback);
1348 return 0;
1351 static struct string_list *ptr_names_done;
1352 static struct string_list *ptr_names;
1354 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1356 insert_string(&ptr_names, alloc_string(argv[0]));
1357 return 0;
1360 static char *get_next_ptr_name(void)
1362 char *ptr;
1364 FOR_EACH_PTR(ptr_names, ptr) {
1365 if (!insert_string(&ptr_names_done, ptr))
1366 continue;
1367 return ptr;
1368 } END_FOR_EACH_PTR(ptr);
1369 return NULL;
1372 static void get_ptr_names(unsigned long long file, const char *name)
1374 char sql_filter[1024];
1375 int before, after;
1377 if (file) {
1378 snprintf(sql_filter, 1024, "file = 0x%llx and function = '%s';",
1379 file, name);
1380 } else {
1381 snprintf(sql_filter, 1024, "function = '%s';", name);
1384 before = ptr_list_size((struct ptr_list *)ptr_names);
1386 run_sql(get_ptr_name, NULL,
1387 "select distinct ptr from function_ptr where %s",
1388 sql_filter);
1390 after = ptr_list_size((struct ptr_list *)ptr_names);
1391 if (before == after)
1392 return;
1394 while ((name = get_next_ptr_name()))
1395 get_ptr_names(0, name);
1398 static void match_data_from_db(struct symbol *sym)
1400 struct select_caller_info_data data = { .prev_func_id = -1 };
1401 struct sm_state *sm;
1402 struct stree *stree;
1403 struct timeval end_time;
1405 if (!sym || !sym->ident)
1406 return;
1408 set_fn_mtag(sym);
1409 gettimeofday(&data.start_time, NULL);
1411 __push_fake_cur_stree();
1412 __unnullify_path();
1414 if (!__inline_fn) {
1415 char *ptr;
1417 if (sym->ctype.modifiers & MOD_STATIC)
1418 get_ptr_names(get_base_file_id(), sym->ident->name);
1419 else
1420 get_ptr_names(0, sym->ident->name);
1422 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1423 __free_ptr_list((struct ptr_list **)&ptr_names);
1424 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1425 __free_fake_cur_stree();
1426 return;
1429 sql_select_caller_info(&data,
1430 "call_id, type, parameter, key, value",
1431 sym);
1434 stree = __pop_fake_cur_stree();
1435 if (!data.ignore)
1436 merge_stree(&data.final_states, stree);
1437 free_stree(&stree);
1438 __push_fake_cur_stree();
1439 __unnullify_path();
1440 data.prev_func_id = -1;
1441 data.ignore = 0;
1442 data.results = 0;
1444 FOR_EACH_PTR(ptr_names, ptr) {
1445 run_sql(caller_info_callback, &data,
1446 "select call_id, type, parameter, key, value"
1447 " from common_caller_info where function = '%s' order by call_id",
1448 ptr);
1449 } END_FOR_EACH_PTR(ptr);
1451 if (data.results) {
1452 FOR_EACH_PTR(ptr_names, ptr) {
1453 free_string(ptr);
1454 } END_FOR_EACH_PTR(ptr);
1455 goto free_ptr_names;
1458 FOR_EACH_PTR(ptr_names, ptr) {
1459 run_sql(caller_info_callback, &data,
1460 "select call_id, type, parameter, key, value"
1461 " from caller_info where function = '%s' order by call_id",
1462 ptr);
1463 free_string(ptr);
1464 } END_FOR_EACH_PTR(ptr);
1466 free_ptr_names:
1467 __free_ptr_list((struct ptr_list **)&ptr_names);
1468 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1469 } else {
1470 sql_select_caller_info(&data,
1471 "call_id, type, parameter, key, value",
1472 sym);
1475 stree = __pop_fake_cur_stree();
1476 if (!data.ignore)
1477 merge_stree(&data.final_states, stree);
1478 free_stree(&stree);
1480 gettimeofday(&end_time, NULL);
1481 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1482 FOR_EACH_SM(data.final_states, sm) {
1483 __set_sm(sm);
1484 } END_FOR_EACH_SM(sm);
1487 free_stree(&data.final_states);
1490 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1492 struct implies_info *info = _info;
1493 struct db_implies_callback *cb;
1494 struct expression *arg = NULL;
1495 int type;
1496 int param;
1498 if (argc != 5)
1499 return 0;
1501 type = atoi(argv[1]);
1502 param = atoi(argv[2]);
1504 FOR_EACH_PTR(info->cb_list, cb) {
1505 if (cb->type != type)
1506 continue;
1507 if (param != -1) {
1508 arg = get_argument_from_call_expr(info->expr->args, param);
1509 if (!arg)
1510 continue;
1512 cb->callback(info->expr, arg, argv[3], argv[4]);
1513 } END_FOR_EACH_PTR(cb);
1515 return 0;
1518 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1520 struct implies_info *info = _info;
1521 struct db_implies_callback *cb;
1522 struct expression *arg;
1523 struct symbol *sym;
1524 char *name;
1525 int type;
1526 int param;
1528 if (argc != 5)
1529 return 0;
1531 type = atoi(argv[1]);
1532 param = atoi(argv[2]);
1534 if (!get_param(param, &name, &sym))
1535 return 0;
1536 arg = symbol_expression(sym);
1537 if (!arg)
1538 return 0;
1540 FOR_EACH_PTR(info->cb_list, cb) {
1541 if (cb->type != type)
1542 continue;
1543 cb->callback(info->expr, arg, argv[3], argv[4]);
1544 } END_FOR_EACH_PTR(cb);
1546 return 0;
1549 static void match_return_implies_helper(struct expression *expr, struct db_implies_cb_list *cb_list)
1551 struct implies_info info = {
1552 .type = RETURN_IMPLIES,
1553 .cb_list = cb_list,
1556 if (expr->fn->type != EXPR_SYMBOL ||
1557 !expr->fn->symbol)
1558 return;
1559 info.expr = expr;
1560 info.sym = expr->fn->symbol;
1561 sql_select_implies("function, type, parameter, key, value", &info,
1562 return_implies_callbacks);
1565 static void match_return_implies_early(struct expression *expr)
1567 match_return_implies_helper(expr, return_implies_cb_list_early);
1570 static void match_return_implies_late(struct expression *expr)
1572 match_return_implies_helper(expr, return_implies_cb_list_late);
1575 static void match_call_implies(struct symbol *sym)
1577 struct implies_info info = {
1578 .type = CALL_IMPLIES,
1579 .cb_list = call_implies_cb_list,
1582 if (!sym || !sym->ident)
1583 return;
1585 info.sym = sym;
1586 sql_select_implies("function, type, parameter, key, value", &info,
1587 call_implies_callbacks);
1590 static char *get_fn_param_str(struct expression *expr)
1592 struct expression *tmp;
1593 int param;
1594 char buf[32];
1596 tmp = get_assigned_expr(expr);
1597 if (tmp)
1598 expr = tmp;
1599 expr = strip_expr(expr);
1600 if (!expr || expr->type != EXPR_CALL)
1601 return NULL;
1602 expr = strip_expr(expr->fn);
1603 if (!expr || expr->type != EXPR_SYMBOL)
1604 return NULL;
1605 param = get_param_num(expr);
1606 if (param < 0)
1607 return NULL;
1609 snprintf(buf, sizeof(buf), "[r $%d]", param);
1610 return alloc_sname(buf);
1613 static char *get_return_compare_is_param(struct expression *expr)
1615 char *var;
1616 char buf[256];
1617 int comparison;
1618 int param;
1620 param = get_param_num(expr);
1621 if (param < 0)
1622 return NULL;
1624 var = expr_to_var(expr);
1625 if (!var)
1626 return NULL;
1627 snprintf(buf, sizeof(buf), "%s orig", var);
1628 comparison = get_comparison_strings(var, buf);
1629 free_string(var);
1631 if (!comparison)
1632 return NULL;
1634 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1635 return alloc_sname(buf);
1638 static char *get_return_compare_str(struct expression *expr)
1640 char *compare_str;
1642 compare_str = get_return_compare_is_param(expr);
1643 if (compare_str)
1644 return compare_str;
1646 compare_str = expr_lte_to_param(expr, -1);
1647 if (compare_str)
1648 return compare_str;
1650 return expr_param_comparison(expr, -1);
1653 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1655 struct expression *fake;
1656 struct range_list *rl;
1657 const char *return_ranges;
1658 sval_t sval;
1659 const char *container_of_str;
1660 char *fn_param_str;
1661 char *compare_str;
1662 char *math_str;
1663 char buf[128];
1665 *rl_p = NULL;
1667 if (!expr)
1668 return alloc_sname("");
1670 fake = get_fake_variable(expr);
1671 if (fake)
1672 expr = fake;
1674 container_of_str = get_container_of_str(expr);
1676 if (get_implied_value(expr, &sval)) {
1677 sval = sval_cast(cur_func_return_type(), sval);
1678 *rl_p = alloc_rl(sval, sval);
1679 return_ranges = sval_to_str_or_err_ptr(sval);
1680 if (container_of_str) {
1681 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1682 return alloc_sname(buf);
1684 return return_ranges;
1687 fn_param_str = get_fn_param_str(expr);
1688 compare_str = expr_equal_to_param(expr, -1);
1689 math_str = get_value_in_terms_of_parameter_math(expr);
1691 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1692 rl = cast_rl(cur_func_return_type(), rl);
1693 return_ranges = show_rl(rl);
1694 } else if (get_imaginary_absolute(expr, &rl)){
1695 rl = cast_rl(cur_func_return_type(), rl);
1696 return alloc_sname(show_rl(rl));
1697 } else {
1698 get_absolute_rl(expr, &rl);
1699 rl = cast_rl(cur_func_return_type(), rl);
1700 return_ranges = show_rl(rl);
1702 *rl_p = rl;
1704 if (container_of_str) {
1705 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1706 return alloc_sname(buf);
1708 if (fn_param_str) {
1709 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1710 return alloc_sname(buf);
1712 if (compare_str) {
1713 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1714 return alloc_sname(buf);
1716 if (math_str) {
1717 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1718 return alloc_sname(buf);
1720 compare_str = get_return_compare_str(expr);
1721 if (compare_str) {
1722 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1723 return alloc_sname(buf);
1726 return return_ranges;
1729 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1731 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1734 static bool call_return_state_hooks_conditional(struct expression *expr)
1736 int final_pass_orig = final_pass;
1737 static int recurse;
1739 if (recurse >= 2)
1740 return false;
1741 if (!expr ||
1742 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1743 return false;
1745 recurse++;
1747 __push_fake_cur_stree();
1749 final_pass = 0;
1750 __split_whole_condition(expr->conditional);
1751 final_pass = final_pass_orig;
1753 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1755 __push_true_states();
1756 __use_false_states();
1758 call_return_state_hooks(expr->cond_false);
1760 __merge_true_states();
1761 __free_fake_cur_stree();
1763 recurse--;
1764 return true;
1767 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1769 struct split_data *data = NULL;
1770 struct expression *compare;
1771 struct range_list *rl;
1772 char buf[64];
1773 char *math;
1774 sval_t sval;
1775 bool undo;
1776 int i;
1778 for (i = 0; i < split_count; i++) {
1779 if (get_function() &&
1780 strcmp(get_function(), forced_splits[i]->func) == 0) {
1781 data = forced_splits[i];
1782 break;
1785 if (!data)
1786 return false;
1788 // FIXME: this works for copy_to/from_user() because the only thing we
1789 // care about is zero/non-zero
1790 if (strcmp(data->rl, "0") != 0)
1791 return false;
1793 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1794 if (!compare)
1795 return false;
1796 if (get_implied_value(compare, &sval))
1797 return false;
1799 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1800 call_return_states_callbacks("0", expr);
1801 if (undo)
1802 end_assume();
1804 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1805 if (get_implied_rl(expr, &rl)) {
1806 math = strchr(return_ranges, '[');
1807 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1808 } else {
1809 snprintf(buf, sizeof(buf), "%s", return_ranges);
1811 call_return_states_callbacks(buf, expr);
1812 if (undo)
1813 end_assume();
1815 return true;
1818 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1820 struct returned_state_callback *cb;
1822 return_ranges = replace_return_ranges(return_ranges);
1823 if (is_delete_return(return_ranges))
1824 return;
1825 if (is_project_delete_return(expr))
1826 return;
1827 if (handle_forced_split(return_ranges, expr))
1828 return;
1830 return_id++;
1831 FOR_EACH_PTR(returned_state_callbacks, cb) {
1832 cb->callback(return_id, (char *)return_ranges, expr);
1833 } END_FOR_EACH_PTR(cb);
1836 static void call_return_state_hooks_compare(struct expression *expr)
1838 char *return_ranges;
1839 int final_pass_orig = final_pass;
1840 sval_t sval = { .type = &int_ctype };
1841 sval_t ret;
1843 if (!get_implied_value(expr, &ret))
1844 ret.value = -1;
1846 __push_fake_cur_stree();
1848 final_pass = 0;
1849 __split_whole_condition(expr);
1850 final_pass = final_pass_orig;
1852 if (ret.value != 0) {
1853 return_ranges = alloc_sname("1");
1854 sval.value = 1;
1855 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1857 call_return_states_callbacks(return_ranges, expr);
1860 __push_true_states();
1861 __use_false_states();
1863 if (ret.value != 1) {
1864 return_ranges = alloc_sname("0");
1865 sval.value = 0;
1866 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1868 call_return_states_callbacks(return_ranges, expr);
1871 __merge_true_states();
1872 __free_fake_cur_stree();
1875 static bool is_implies_function(struct expression *expr)
1877 struct range_list *rl;
1879 if (!expr)
1880 return false;
1882 rl = get_range_implications(get_function());
1883 if (!rl)
1884 return false;
1886 sm_msg("%s: is implied", __func__);
1887 return true;
1890 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1892 struct sm_state *tmp;
1894 FOR_EACH_PTR(slist, tmp) {
1895 if (strcmp(tmp->state->name, sm->state->name) == 0)
1896 return 1;
1897 } END_FOR_EACH_PTR(tmp);
1899 return 0;
1902 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1904 struct range_list *rl;
1905 char *return_ranges;
1906 struct sm_state *tmp;
1907 int ret = 0;
1908 int nr_possible, nr_states;
1909 char *compare_str;
1910 char buf[128];
1911 struct state_list *already_handled = NULL;
1912 sval_t sval;
1914 if (!sm || !sm->merged)
1915 return 0;
1917 if (too_many_possible(sm) && !is_implies_function(expr))
1918 return 0;
1920 /* bail if it gets too complicated */
1921 nr_possible = 0;
1922 FOR_EACH_PTR(sm->possible, tmp) {
1923 if (tmp->merged)
1924 continue;
1925 if (ptr_in_list(tmp, already_handled))
1926 continue;
1927 add_ptr_list(&already_handled, tmp);
1928 nr_possible++;
1929 } END_FOR_EACH_PTR(tmp);
1930 free_slist(&already_handled);
1931 nr_states = get_db_state_count();
1932 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1933 return 0;
1935 FOR_EACH_PTR(sm->possible, tmp) {
1936 if (!is_leaf(tmp))
1937 continue;
1938 if (ptr_in_list(tmp, already_handled))
1939 continue;
1940 add_ptr_list(&already_handled, tmp);
1942 ret = 1;
1943 __push_fake_cur_stree();
1945 overwrite_states_using_pool(sm, tmp);
1947 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1948 return_ranges = show_rl(rl);
1949 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1950 compare_str = get_return_compare_str(expr);
1951 /* ignore obvious stuff like 0 <= param */
1952 /* Is this worthile when we have PARAM_COMPARE? */
1953 if (compare_str &&
1954 strncmp(compare_str, "[=", 2) != 0 &&
1955 rl_to_sval(rl, &sval))
1956 compare_str = NULL;
1957 if (compare_str) {
1958 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1959 return_ranges = alloc_sname(buf);
1962 call_return_states_callbacks(return_ranges, expr);
1964 __free_fake_cur_stree();
1965 } END_FOR_EACH_PTR(tmp);
1967 free_slist(&already_handled);
1969 return ret;
1972 static int call_return_state_hooks_split_possible(struct expression *expr)
1974 struct sm_state *sm;
1976 if (!expr)
1977 return 0;
1979 sm = get_returned_sm(expr);
1980 return split_possible_helper(sm, expr);
1983 static bool has_empty_state(struct sm_state *sm)
1985 struct sm_state *tmp;
1987 FOR_EACH_PTR(sm->possible, tmp) {
1988 if (!estate_rl(tmp->state))
1989 return true;
1990 } END_FOR_EACH_PTR(tmp);
1992 return false;
1995 static bool has_possible_negative(struct sm_state *sm)
1997 struct sm_state *tmp;
1999 if (!type_signed(estate_type(sm->state)))
2000 return false;
2002 FOR_EACH_PTR(sm->possible, tmp) {
2003 if (!estate_rl(tmp->state))
2004 continue;
2005 if (sval_is_negative(estate_min(tmp->state)) &&
2006 sval_is_negative(estate_max(tmp->state)))
2007 return true;
2008 } END_FOR_EACH_PTR(tmp);
2010 return false;
2013 static bool has_separate_zero_null(struct sm_state *sm)
2015 struct sm_state *tmp;
2016 sval_t sval;
2018 FOR_EACH_PTR(sm->possible, tmp) {
2019 if (!estate_get_single_value(tmp->state, &sval))
2020 continue;
2021 if (sval.value == 0)
2022 return true;
2023 } END_FOR_EACH_PTR(tmp);
2025 return false;
2028 static int split_positive_from_negative(struct expression *expr)
2030 struct sm_state *sm;
2031 struct range_list *rl;
2032 const char *return_ranges;
2033 struct range_list *ret_rl;
2034 bool separate_zero;
2035 int undo;
2037 /* We're going to print the states 3 times */
2038 if (get_db_state_count() > 10000 / 3)
2039 return 0;
2041 if (!get_implied_rl(expr, &rl) || !rl)
2042 return 0;
2043 /* Forget about INT_MAX and larger */
2044 if (rl_max(rl).value <= 0)
2045 return 0;
2046 if (!sval_is_negative(rl_min(rl)))
2047 return 0;
2049 sm = get_returned_sm(expr);
2050 if (!sm)
2051 return 0;
2052 if (has_empty_state(sm))
2053 return 0;
2054 if (!has_possible_negative(sm))
2055 return 0;
2056 separate_zero = has_separate_zero_null(sm);
2058 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
2059 return 0;
2061 return_ranges = get_return_ranges_str(expr, &ret_rl);
2062 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2063 call_return_states_callbacks(return_ranges, expr);
2065 end_assume();
2067 if (separate_zero) {
2068 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
2070 return_ranges = get_return_ranges_str(expr, &ret_rl);
2071 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2072 call_return_states_callbacks(return_ranges, expr);
2074 if (undo)
2075 end_assume();
2078 undo = assume(compare_expression(expr, '<', zero_expr()));
2080 return_ranges = get_return_ranges_str(expr, &ret_rl);
2081 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2082 call_return_states_callbacks(return_ranges, expr);
2084 if (undo)
2085 end_assume();
2087 return 1;
2090 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
2092 struct range_list *rl;
2093 struct range_list *nonnull_rl;
2094 sval_t null_sval;
2095 struct range_list *null_rl = NULL;
2096 char *return_ranges;
2097 struct sm_state *sm;
2098 struct smatch_state *state;
2099 int nr_states;
2100 int final_pass_orig = final_pass;
2102 if (!expr || expr_equal_to_param(expr, -1))
2103 return 0;
2104 if (expr->type == EXPR_CALL)
2105 return 0;
2107 sm = get_returned_sm(expr);
2108 if (!sm)
2109 return 0;
2110 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2111 return 0;
2112 state = sm->state;
2113 if (!estate_rl(state))
2114 return 0;
2115 if (estate_min(state).value == 0 && estate_max(state).value == 0)
2116 return 0;
2117 if (has_possible_negative(sm))
2118 return 0;
2119 if (!has_separate_zero_null(sm))
2120 return 0;
2122 nr_states = get_db_state_count();
2123 if (option_info && nr_states >= 1500)
2124 return 0;
2126 rl = estate_rl(state);
2128 __push_fake_cur_stree();
2130 final_pass = 0;
2131 __split_whole_condition(expr);
2132 final_pass = final_pass_orig;
2134 nonnull_rl = rl_filter(rl, rl_zero());
2135 return_ranges = show_rl(nonnull_rl);
2136 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2138 call_return_states_callbacks(return_ranges, expr);
2140 __push_true_states();
2141 __use_false_states();
2143 return_ranges = alloc_sname("0");
2144 null_sval = sval_type_val(rl_type(rl), 0);
2145 add_range(&null_rl, null_sval, null_sval);
2146 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2147 call_return_states_callbacks(return_ranges, expr);
2149 __merge_true_states();
2150 __free_fake_cur_stree();
2152 return 1;
2155 static bool is_neg_and_pos_err_code(struct range_list *rl)
2157 struct data_range *tmp, *last;
2159 if (option_project != PROJ_KERNEL)
2160 return false;
2161 if (!rl)
2162 return false;
2164 /* Assume s32min-(14),(-12)-(-1),1-s32max is an error code. */
2165 last = last_ptr_list((struct ptr_list *)rl);
2166 if (last->max.value >= 0 &&
2167 (last->min.value != 1 ||
2168 last->max.value != INT_MAX))
2169 return false;
2172 FOR_EACH_PTR(rl, tmp) {
2173 if (tmp == last)
2174 break;
2175 if (tmp->min.value != INT_MIN && tmp->min.value < -4095)
2176 return false;
2177 if (tmp->max.value < -4095 || tmp->max.value >= 0)
2178 return false;
2179 } END_FOR_EACH_PTR(tmp);
2181 return true;
2184 static bool is_kernel_success_fail(struct sm_state *sm)
2186 struct sm_state *tmp;
2187 struct range_list *rl;
2188 bool has_zero = false;
2189 bool has_neg = false;
2191 if (!type_signed(estate_type(sm->state)))
2192 return false;
2194 FOR_EACH_PTR(sm->possible, tmp) {
2195 rl = estate_rl(tmp->state);
2196 if (!rl)
2197 return false;
2198 if (!is_leaf(tmp))
2199 continue;
2200 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2201 has_zero = true;
2202 continue;
2204 has_neg = true;
2205 if (is_neg_and_pos_err_code(estate_rl(tmp->state)))
2206 continue;
2207 return false;
2208 } END_FOR_EACH_PTR(tmp);
2210 return has_zero && has_neg;
2213 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2215 struct expression *tmp_ret;
2216 struct sm_state *sm;
2217 struct range_list *rl;
2218 struct range_list *nonzero_rl;
2219 sval_t zero_sval;
2220 struct range_list *zero_rl = NULL;
2221 int nr_states;
2222 char *return_ranges;
2223 int final_pass_orig = final_pass;
2225 if (option_project != PROJ_KERNEL)
2226 return 0;
2228 nr_states = get_db_state_count();
2229 if (nr_states > 2000)
2230 return 0;
2232 tmp_ret = get_fake_variable(expr);
2233 if (!tmp_ret)
2234 tmp_ret = expr;
2235 sm = get_returned_sm(tmp_ret);
2236 if (!sm)
2237 return 0;
2238 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2239 return 0;
2240 if (!is_kernel_success_fail(sm))
2241 return 0;
2243 rl = estate_rl(sm->state);
2244 if (!rl)
2245 return 0;
2247 __push_fake_cur_stree();
2249 final_pass = 0;
2250 __split_whole_condition(tmp_ret);
2251 final_pass = final_pass_orig;
2253 nonzero_rl = rl_filter(rl, rl_zero());
2254 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2255 return_ranges = show_rl(nonzero_rl);
2256 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2258 call_return_states_callbacks(return_ranges, expr);
2260 __push_true_states();
2261 __use_false_states();
2263 return_ranges = alloc_sname("0");
2264 zero_sval = sval_type_val(rl_type(rl), 0);
2265 add_range(&zero_rl, zero_sval, zero_sval);
2266 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2267 call_return_states_callbacks(return_ranges, expr);
2269 __merge_true_states();
2270 __free_fake_cur_stree();
2272 return 1;
2275 static int is_boolean(struct expression *expr)
2277 struct range_list *rl;
2279 if (!get_implied_rl(expr, &rl))
2280 return 0;
2281 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2282 return 1;
2283 return 0;
2286 static int splitable_function_call(struct expression *expr)
2288 struct sm_state *sm;
2290 if (!expr || expr->type != EXPR_CALL)
2291 return 0;
2292 sm = get_extra_sm_state(expr);
2293 return split_possible_helper(sm, expr);
2296 static struct sm_state *find_bool_param(void)
2298 struct stree *start_states;
2299 struct symbol *arg;
2300 struct sm_state *sm, *tmp;
2301 sval_t sval;
2303 start_states = get_start_states();
2305 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2306 if (!arg->ident)
2307 continue;
2308 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2309 if (!sm)
2310 continue;
2311 if (rl_min(estate_rl(sm->state)).value != 0 ||
2312 rl_max(estate_rl(sm->state)).value != 1)
2313 continue;
2314 goto found;
2315 } END_FOR_EACH_PTR_REVERSE(arg);
2317 return NULL;
2319 found:
2321 * Check if it's splitable. If not, then splitting it up is likely not
2322 * useful for the callers.
2324 FOR_EACH_PTR(sm->possible, tmp) {
2325 if (is_merged(tmp))
2326 continue;
2327 if (!estate_get_single_value(tmp->state, &sval))
2328 return NULL;
2329 } END_FOR_EACH_PTR(tmp);
2331 return sm;
2334 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2336 struct range_list *ret_rl;
2337 const char *return_ranges;
2338 struct sm_state *tmp;
2339 int ret = 0;
2340 struct state_list *already_handled = NULL;
2342 if (!sm || !sm->merged)
2343 return 0;
2345 if (too_many_possible(sm))
2346 return 0;
2348 FOR_EACH_PTR(sm->possible, tmp) {
2349 if (tmp->merged)
2350 continue;
2351 if (ptr_in_list(tmp, already_handled))
2352 continue;
2353 add_ptr_list(&already_handled, tmp);
2355 ret = 1;
2356 __push_fake_cur_stree();
2358 overwrite_states_using_pool(sm, tmp);
2360 return_ranges = get_return_ranges_str(expr, &ret_rl);
2361 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2362 call_return_states_callbacks(return_ranges, expr);
2364 __free_fake_cur_stree();
2365 } END_FOR_EACH_PTR(tmp);
2367 free_slist(&already_handled);
2369 return ret;
2372 static int split_by_bool_param(struct expression *expr)
2374 struct sm_state *start_sm, *sm;
2375 sval_t sval;
2377 start_sm = find_bool_param();
2378 if (!start_sm)
2379 return 0;
2380 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2381 if (!sm || estate_get_single_value(sm->state, &sval))
2382 return 0;
2384 if (get_db_state_count() * 2 >= 2000)
2385 return 0;
2387 return split_on_bool_sm(sm, expr);
2390 static int split_by_null_nonnull_param(struct expression *expr)
2392 struct symbol *arg;
2393 struct sm_state *sm;
2394 int nr_possible;
2396 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2397 if (!arg || !arg->ident)
2398 return 0;
2399 if (get_real_base_type(arg)->type != SYM_PTR)
2400 return 0;
2402 if (param_was_set_var_sym(arg->ident->name, arg))
2403 return 0;
2404 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2405 if (!sm)
2406 return 0;
2408 if (!has_separate_zero_null(sm))
2409 return 0;
2411 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2412 if (get_db_state_count() * nr_possible >= 2000)
2413 return 0;
2415 return split_on_bool_sm(sm, expr);
2418 static void call_hooks_based_on_pool(struct expression *expr, struct sm_state *gate_sm, struct sm_state *pool_sm)
2420 struct range_list *ret_rl;
2421 const char *return_ranges;
2423 __push_fake_cur_stree();
2425 overwrite_states_using_pool(gate_sm, pool_sm);
2427 return_ranges = get_return_ranges_str(expr, &ret_rl);
2428 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2429 call_return_states_callbacks(return_ranges, expr);
2431 __free_fake_cur_stree();
2434 static bool split_by_impossible(struct expression *expr)
2436 static int impossible_id;
2437 struct sm_state *sm, *tmp;
2438 int nr_states;
2440 if (!impossible_id)
2441 impossible_id = id_from_name("register_impossible_return");
2442 if (!impossible_id)
2443 return false;
2446 * The only states for register_impossible_return are &impossible,
2447 * &undefined and &merged. This function will break otherwise.
2450 sm = get_sm_state(impossible_id, "impossible", NULL);
2451 if (!sm || sm->state != &merged)
2452 return false;
2454 nr_states = get_db_state_count();
2455 if (nr_states >= 1000)
2456 return false;
2458 /* handle possible */
2459 FOR_EACH_PTR(sm->possible, tmp) {
2460 if (!is_leaf(tmp))
2461 continue;
2462 if (tmp->state != &undefined)
2463 continue;
2464 call_hooks_based_on_pool(expr, sm, tmp);
2465 goto impossible;
2466 } END_FOR_EACH_PTR(tmp);
2468 impossible:
2469 /* handle impossible */
2470 FOR_EACH_PTR(sm->possible, tmp) {
2471 if (!is_leaf(tmp))
2472 continue;
2473 if (strcmp(tmp->state->name, "impossible") != 0)
2474 continue;
2475 call_hooks_based_on_pool(expr, sm, tmp);
2476 return true;
2477 } END_FOR_EACH_PTR(tmp);
2479 return false;
2482 struct expression *strip_expr_statement(struct expression *expr)
2484 struct expression *orig = expr;
2485 struct statement *stmt, *last_stmt;
2487 if (!expr)
2488 return NULL;
2489 if (expr->type == EXPR_PREOP && expr->op == '(')
2490 expr = expr->unop;
2491 if (expr->type != EXPR_STATEMENT)
2492 return orig;
2493 stmt = expr->statement;
2494 if (!stmt || stmt->type != STMT_COMPOUND)
2495 return orig;
2497 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2498 if (!last_stmt || last_stmt->type == STMT_LABEL)
2499 last_stmt = last_stmt->label_statement;
2500 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2501 return orig;
2502 return strip_expr(last_stmt->expression);
2505 static bool is_kernel_error_path(struct expression *expr)
2507 struct range_list *rl;
2509 if (option_project != PROJ_KERNEL)
2510 return false;
2512 if (!get_implied_rl(expr, &rl))
2513 return false;
2514 if (rl_type(rl) != &int_ctype)
2515 return false;
2516 if (!is_neg_and_pos_err_code(rl))
2517 return false;
2518 return true;
2521 static void call_return_state_hooks(struct expression *expr)
2523 struct range_list *ret_rl;
2524 const char *return_ranges;
2525 int nr_states;
2526 sval_t sval;
2528 if (debug_db) {
2529 struct range_list *rl = NULL;
2531 get_absolute_rl(expr, &rl);
2532 sm_msg("RETURN: expr='%s' rl='%s' %lu states%s", expr_to_str(expr),
2533 show_rl(rl), stree_count(__get_cur_stree()),
2534 is_impossible_path() ? " (impossible path)" : "");
2537 if (__path_is_null())
2538 return;
2540 if (is_impossible_path())
2541 goto vanilla;
2543 if (expr && (expr->type == EXPR_COMPARE ||
2544 !get_implied_value(expr, &sval)) &&
2545 (is_condition(expr) || is_boolean(expr))) {
2546 call_return_state_hooks_compare(expr);
2547 if (debug_db)
2548 sm_msg("%s: bool", __func__);
2549 return;
2550 } else if (call_return_state_hooks_conditional(expr)) {
2551 if (debug_db)
2552 sm_msg("%s: condition", __func__);
2553 return;
2554 } else if (is_kernel_error_path(expr)) {
2555 if (debug_db)
2556 sm_msg("%s: kernel error path", __func__);
2557 goto vanilla;
2558 } else if (call_return_state_hooks_split_success_fail(expr)) {
2559 if (debug_db)
2560 sm_msg("%s: success_fail", __func__);
2561 return;
2562 } else if (call_return_state_hooks_split_possible(expr)) {
2563 if (debug_db)
2564 sm_msg("%s: split_possible", __func__);
2565 return;
2566 } else if (split_positive_from_negative(expr)) {
2567 if (debug_db)
2568 sm_msg("%s: positive negative", __func__);
2569 return;
2570 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2571 if (debug_db)
2572 sm_msg("%s: split zero non-zero", __func__);
2573 return;
2574 } else if (splitable_function_call(expr)) {
2575 if (debug_db)
2576 sm_msg("%s: split_function_call", __func__);
2577 return;
2578 } else if (split_by_bool_param(expr)) {
2579 if (debug_db)
2580 sm_msg("%s: bool param", __func__);
2581 return;
2582 } else if (split_by_null_nonnull_param(expr)) {
2583 if (debug_db)
2584 sm_msg("%s: null non-null param", __func__);
2585 return;
2586 } else if (split_by_impossible(expr)) {
2587 if (debug_db)
2588 sm_msg("%s: split by impossible", __func__);
2589 return;
2592 vanilla:
2593 return_ranges = get_return_ranges_str(expr, &ret_rl);
2594 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2596 nr_states = get_db_state_count();
2597 if (nr_states >= 10000) {
2598 return_id++;
2599 match_return_info(return_id, (char *)return_ranges, expr);
2600 print_limited_param_set(return_id, (char *)return_ranges, expr);
2601 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2602 return;
2604 call_return_states_callbacks(return_ranges, expr);
2605 if (debug_db)
2606 sm_msg("%s: vanilla", __func__);
2609 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2611 struct returned_member_callback *cb;
2612 struct sm_state *sm;
2613 struct symbol *type;
2614 char *name;
2615 char member_name[256];
2616 int len;
2618 type = get_type(expr);
2619 if (!type || type->type != SYM_PTR)
2620 return;
2621 name = expr_to_var(expr);
2622 if (!name)
2623 return;
2625 len = strlen(name);
2626 FOR_EACH_PTR(returned_member_callbacks, cb) {
2627 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2628 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2629 strcpy(member_name, "*$");
2630 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2631 continue;
2633 if (strncmp(sm->name, name, len) != 0)
2634 continue;
2635 if (strncmp(sm->name + len, "->", 2) != 0)
2636 continue;
2637 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2638 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2639 } END_FOR_EACH_SM(sm);
2640 } END_FOR_EACH_PTR(cb);
2642 free_string(name);
2645 static void print_return_struct_info(int return_id, char *return_ranges,
2646 struct expression *expr,
2647 struct symbol *sym,
2648 struct return_info_callback *cb)
2650 struct sm_state *sm;
2651 const char *printed_name;
2652 int param;
2654 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2655 param = get_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2656 if (!printed_name)
2657 continue;
2658 if (param < 0)
2659 continue;
2660 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2661 } END_FOR_EACH_SM(sm);
2663 /* always print returned states after processing param states */
2664 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2665 param = get_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2666 if (!printed_name)
2667 continue;
2668 if (param != -1)
2669 continue;
2670 cb->callback(return_id, return_ranges, expr, -1, printed_name, sm);
2671 } END_FOR_EACH_SM(sm);
2674 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2676 struct return_info_callback *cb;
2677 struct expression *tmp;
2678 struct symbol *sym;
2680 if (!option_info && !__inline_fn &&
2681 !local_debug && !option_debug)
2682 return;
2684 tmp = get_fake_variable(expr);
2685 if (tmp)
2686 expr = tmp;
2687 sym = expr_to_sym(expr);
2689 FOR_EACH_PTR(return_callbacks, cb) {
2690 __ignore_param_used++;
2691 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2692 __ignore_param_used--;
2693 } END_FOR_EACH_PTR(cb);
2696 static void reset_memdb(struct symbol *sym)
2698 mem_sql(NULL, NULL, "delete from caller_info;");
2699 mem_sql(NULL, NULL, "delete from return_states;");
2700 mem_sql(NULL, NULL, "delete from call_implies;");
2701 mem_sql(NULL, NULL, "delete from return_implies;");
2704 static void match_end_func_info(struct symbol *sym)
2706 if (__path_is_null())
2707 return;
2708 call_return_state_hooks(NULL);
2711 static void match_after_func(struct symbol *sym)
2713 clear_cached_return_vals();
2714 if (!__inline_fn)
2715 reset_memdb(sym);
2718 static void init_memdb(void)
2720 char *err = NULL;
2721 int rc;
2722 const char *schema_files[] = {
2723 "db/db.schema",
2724 "db/caller_info.schema",
2725 "db/common_caller_info.schema",
2726 "db/return_states.schema",
2727 "db/function_type_size.schema",
2728 "db/type_size.schema",
2729 "db/function_type_info.schema",
2730 "db/type_info.schema",
2731 "db/call_implies.schema",
2732 "db/return_implies.schema",
2733 "db/function_ptr.schema",
2734 "db/local_values.schema",
2735 "db/function_type_value.schema",
2736 "db/type_value.schema",
2737 "db/function_type.schema",
2738 "db/data_info.schema",
2739 "db/parameter_name.schema",
2740 "db/constraints.schema",
2741 "db/constraints_required.schema",
2742 "db/fn_ptr_data_link.schema",
2743 "db/fn_data_link.schema",
2744 "db/mtag_about.schema",
2745 "db/mtag_info.schema",
2746 "db/mtag_map.schema",
2747 "db/mtag_data.schema",
2748 "db/mtag_alias.schema",
2750 static char buf[4096];
2751 int fd;
2752 int ret;
2753 int i;
2755 rc = sqlite3_open(":memory:", &mem_db);
2756 if (rc != SQLITE_OK) {
2757 sm_ierror("starting In-Memory database.");
2758 return;
2761 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2762 fd = open_schema_file(schema_files[i]);
2763 if (fd < 0)
2764 continue;
2765 ret = read(fd, buf, sizeof(buf));
2766 if (ret < 0) {
2767 sm_ierror("failed to read: %s", schema_files[i]);
2768 continue;
2770 close(fd);
2771 if (ret == sizeof(buf)) {
2772 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2773 schema_files[i], sizeof(buf));
2774 continue;
2776 buf[ret] = '\0';
2777 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2778 if (rc != SQLITE_OK) {
2779 sm_ierror("SQL error #2: %s", err);
2780 sm_ierror("%s", buf);
2785 static void init_cachedb(void)
2787 char *err = NULL;
2788 int rc;
2789 const char *schema_files[] = {
2790 "db/call_implies.schema",
2791 "db/return_implies.schema",
2792 "db/type_info.schema",
2793 "db/mtag_about.schema",
2794 "db/mtag_data.schema",
2795 "db/mtag_info.schema",
2796 "db/sink_info.schema",
2797 "db/hash_string.schema",
2799 static char buf[4096];
2800 int fd;
2801 int ret;
2802 int i;
2804 rc = sqlite3_open(":memory:", &cache_db);
2805 if (rc != SQLITE_OK) {
2806 sm_ierror("starting In-Memory database.");
2807 return;
2810 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2811 fd = open_schema_file(schema_files[i]);
2812 if (fd < 0)
2813 continue;
2814 ret = read(fd, buf, sizeof(buf));
2815 if (ret < 0) {
2816 sm_ierror("failed to read: %s", schema_files[i]);
2817 continue;
2819 close(fd);
2820 if (ret == sizeof(buf)) {
2821 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2822 schema_files[i], sizeof(buf));
2823 continue;
2825 buf[ret] = '\0';
2826 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2827 if (rc != SQLITE_OK) {
2828 sm_ierror("SQL error #2: %s", err);
2829 sm_ierror("%s", buf);
2834 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2836 static char buf[4096];
2837 char tmp[256];
2838 char *p = buf;
2839 char *table = _table;
2840 int i;
2843 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2844 for (i = 0; i < argc; i++) {
2845 if (i)
2846 p += snprintf(p, 4096 - (p - buf), ", ");
2847 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2848 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2851 p += snprintf(p, 4096 - (p - buf), ");");
2852 if (p - buf > 4096)
2853 return 0;
2855 sm_msg("SQL: %s", buf);
2856 return 0;
2859 static void dump_cache(struct symbol_list *sym_list)
2861 const char *cache_tables[] = {
2862 "type_info", "return_implies", "call_implies", "mtag_data",
2863 "mtag_info", "mtag_about", "sink_info", "hash_string",
2865 char buf[64];
2866 int i;
2868 if (!option_info)
2869 return;
2871 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2872 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2873 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2877 void open_smatch_db(char *db_file)
2879 int rc;
2881 if (option_no_db)
2882 return;
2884 use_states = malloc(num_checks + 1);
2885 memset(use_states, 0xff, num_checks + 1);
2887 init_memdb();
2888 init_cachedb();
2890 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2891 if (rc != SQLITE_OK) {
2892 option_no_db = 1;
2893 return;
2895 run_sql(NULL, NULL,
2896 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2897 return;
2900 static char *get_next_string(char **str)
2902 static char string[256];
2903 char *start;
2904 char *p = *str;
2905 int len, i, j;
2907 if (*p == '\0')
2908 return NULL;
2909 start = p;
2911 while (*p != '\0' && *p != '\n') {
2912 if (*p == '\\' && *(p + 1) == ' ') {
2913 p += 2;
2914 continue;
2916 if (*p == ' ')
2917 break;
2918 p++;
2921 len = p - start;
2922 if (len >= sizeof(string)) {
2923 memcpy(string, start, sizeof(string));
2924 string[sizeof(string) - 1] = '\0';
2925 sm_ierror("return_fix: '%s' too long", string);
2926 **str = '\0';
2927 return NULL;
2929 memcpy(string, start, len);
2930 string[len] = '\0';
2931 for (i = 0; i < sizeof(string) - 1; i++) {
2932 if (string[i] == '\\' && string[i + 1] == ' ') {
2933 for (j = i; string[j] != '\0'; j++)
2934 string[j] = string[j + 1];
2937 if (*p != '\0')
2938 p++;
2939 *str = p;
2940 return string;
2943 static void register_return_deletes(void)
2945 char *func, *ret_str;
2946 char filename[256];
2947 char buf[4096];
2948 int fd, ret, i;
2949 char *p;
2951 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2952 fd = open_schema_file(filename);
2953 if (fd < 0)
2954 return;
2955 ret = read(fd, buf, sizeof(buf));
2956 close(fd);
2957 if (ret < 0)
2958 return;
2959 if (ret == sizeof(buf)) {
2960 sm_ierror("file too large: %s (limit %zd bytes)",
2961 filename, sizeof(buf));
2962 return;
2964 buf[ret] = '\0';
2966 p = buf;
2967 while (*p) {
2968 get_next_string(&p);
2969 delete_count++;
2971 if (delete_count == 0)
2972 return;
2973 if (delete_count % 2 != 0) {
2974 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2975 delete_count = 0;
2976 return;
2978 delete_table = malloc(delete_count * sizeof(char *));
2980 p = buf;
2981 i = 0;
2982 while (*p) {
2983 func = alloc_string(get_next_string(&p));
2984 ret_str = alloc_string(get_next_string(&p));
2986 delete_table[i++] = func;
2987 delete_table[i++] = ret_str;
2991 #define RETURN_FIX_SIZE 8196
2992 static void register_return_replacements(void)
2994 char *func, *orig, *new;
2995 char filename[256];
2996 int fd, ret, i;
2997 char *buf;
2998 char *p;
3000 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
3001 fd = open_schema_file(filename);
3002 if (fd < 0)
3003 return;
3004 buf = malloc(RETURN_FIX_SIZE);
3005 ret = read(fd, buf, RETURN_FIX_SIZE);
3006 close(fd);
3007 if (ret < 0) {
3008 free(buf);
3009 return;
3011 if (ret == RETURN_FIX_SIZE) {
3012 sm_ierror("file too large: %s (limit %d bytes)",
3013 filename, RETURN_FIX_SIZE);
3014 free(buf);
3015 return;
3017 buf[ret] = '\0';
3019 p = buf;
3020 while (*p) {
3021 get_next_string(&p);
3022 replace_count++;
3024 if (replace_count == 0) {
3025 free(buf);
3026 return;
3028 if (replace_count % 3 != 0) {
3029 printf("error parsing '%s' replace_count=%d\n", filename, replace_count);
3030 replace_count = 0;
3031 free(buf);
3032 return;
3034 replace_table = malloc(replace_count * sizeof(char *));
3036 p = buf;
3037 i = 0;
3038 while (*p) {
3039 func = alloc_string(get_next_string(&p));
3040 orig = alloc_string(get_next_string(&p));
3041 new = alloc_string(get_next_string(&p));
3043 replace_table[i++] = func;
3044 replace_table[i++] = orig;
3045 replace_table[i++] = new;
3047 free(buf);
3050 static void register_forced_return_splits(void)
3052 int struct_members = sizeof(struct split_data) / sizeof(char *);
3053 char filename[256];
3054 char buf[4096];
3055 int fd, ret, i;
3056 char *p;
3058 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
3059 fd = open_schema_file(filename);
3060 if (fd < 0)
3061 return;
3062 ret = read(fd, buf, sizeof(buf));
3063 close(fd);
3064 if (ret < 0)
3065 return;
3066 if (ret == sizeof(buf)) {
3067 sm_ierror("file too large: %s (limit %zd bytes)",
3068 filename, sizeof(buf));
3069 return;
3071 buf[ret] = '\0';
3073 p = buf;
3074 while (*p) {
3075 get_next_string(&p);
3076 split_count++;
3078 if (split_count == 0)
3079 return;
3080 if (split_count % struct_members != 0) {
3081 printf("error parsing '%s' split_count=%d\n", filename, split_count);
3082 split_count = 0;
3083 return;
3085 split_count /= struct_members;
3086 forced_splits = malloc(split_count * sizeof(void *));
3088 p = buf;
3089 i = 0;
3090 while (*p) {
3091 struct split_data *split = malloc(sizeof(*split));
3093 split->func = alloc_string(get_next_string(&p));
3094 split->rl = alloc_string(get_next_string(&p));
3095 forced_splits[i++] = split;
3099 void register_definition_db_callbacks(int id)
3101 my_id = id;
3103 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
3104 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
3105 add_split_return_callback(match_return_info);
3106 add_split_return_callback(print_returned_struct_members);
3107 add_split_return_callback(print_return_info);
3108 add_hook(&call_return_state_hooks, RETURN_HOOK);
3109 add_hook(&match_end_func_info, END_FUNC_HOOK);
3110 add_hook(&match_after_func, AFTER_FUNC_HOOK);
3112 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
3113 add_hook(&match_call_implies, FUNC_DEF_HOOK);
3114 add_hook(&match_return_implies_early, CALL_HOOK_AFTER_INLINE);
3116 common_funcs = load_strings_from_file(option_project_str, "common_functions");
3117 register_return_deletes();
3118 register_return_replacements();
3119 register_forced_return_splits();
3121 add_hook(&dump_cache, END_FILE_HOOK);
3124 void register_definition_db_callbacks_late(int id)
3126 add_hook(&match_return_implies_late, CALL_HOOK_AFTER_INLINE);
3129 void register_db_call_marker(int id)
3131 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
3134 char *get_data_info_name(struct expression *expr)
3136 struct symbol *sym;
3137 char *name;
3138 char buf[256];
3139 char *ret = NULL;
3141 expr = strip_expr(expr);
3142 name = get_member_name(expr);
3143 if (name)
3144 return name;
3145 name = expr_to_var_sym(expr, &sym);
3146 if (!name || !sym)
3147 goto free;
3148 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
3149 goto free;
3150 if (sym->ctype.modifiers & MOD_STATIC)
3151 snprintf(buf, sizeof(buf), "static %s", name);
3152 else
3153 snprintf(buf, sizeof(buf), "global %s", name);
3154 ret = alloc_sname(buf);
3155 free:
3156 free_string(name);
3157 return ret;