kernel.ignored_macros: add ASSERT() to ignored macros
[smatch.git] / smatch_db.c
blob03b87b12f9634cbaf14876fa9a80fb44cdcc32a6
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;
101 static struct db_implies_cb_list *call_implies_cb_list;
103 struct split_data {
104 const char *func, *rl;
106 static struct split_data **forced_splits;
107 static int split_count;
109 /* silently truncates if needed. */
110 char *escape_newlines(const char *str)
112 char buf[1024] = "";
113 bool found = false;
114 int i, j;
116 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
117 if (str[i] != '\r' && str[i] != '\n') {
118 buf[j] = str[i];
119 continue;
122 found = true;
123 buf[j++] = '\\';
124 if (j == sizeof(buf))
125 break;
126 buf[j] = 'n';
129 if (!found)
130 return alloc_sname(str);
132 if (j == sizeof(buf))
133 buf[j - 1] = '\0';
134 return alloc_sname(buf);
137 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
139 int i;
141 for (i = 0; i < argc; i++) {
142 if (i != 0)
143 sm_printf(", ");
144 sm_printf("%s", argv[i]);
146 sm_printf("\n");
147 return 0;
150 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
152 char *err = NULL;
153 int rc;
155 if (!db)
156 return;
158 if (option_debug || debug_db) {
159 sm_msg("%s", sql);
160 if (strncasecmp(sql, "select", strlen("select")) == 0)
161 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
164 rc = sqlite3_exec(db, sql, callback, data, &err);
165 if (rc != SQLITE_OK && !parse_error) {
166 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
167 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
168 parse_error = 1;
172 static int replace_count;
173 static char **replace_table;
174 static const char *replace_return_ranges(const char *return_ranges)
176 int i;
178 if (!get_function()) {
179 /* I have no idea why EXPORT_SYMBOL() is here */
180 return return_ranges;
182 for (i = 0; i < replace_count; i += 3) {
183 if (strcmp(replace_table[i + 0], get_function()) == 0) {
184 if (strcmp(replace_table[i + 1], return_ranges) == 0)
185 return replace_table[i + 2];
188 return return_ranges;
191 static int delete_count;
192 static char **delete_table;
193 static bool is_delete_return(const char *return_ranges)
195 int i;
197 if (!get_function())
198 return false;
200 for (i = 0; i < delete_count; i += 2) {
201 if (strcmp(delete_table[i], get_function()) == 0 &&
202 strcmp(delete_table[i + 1], return_ranges) == 0)
203 return true;
206 return false;
209 static char *use_states;
210 static int get_db_state_count(void)
212 struct sm_state *sm;
213 int count = 0;
215 FOR_EACH_SM(__get_cur_stree(), sm) {
216 if (sm->owner == USHRT_MAX)
217 continue;
218 if (use_states[sm->owner])
219 count++;
220 } END_FOR_EACH_SM(sm);
221 return count;
224 static bool is_local(struct symbol *sym)
226 if (sym->ctype.modifiers & MOD_STATIC)
227 return true;
228 if ((sym->ctype.modifiers & MOD_EXTERN) &&
229 (sym->ctype.modifiers & MOD_INLINE))
230 return true;
232 if (!sym->definition)
233 return false;
235 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
236 (sym->definition->ctype.modifiers & MOD_INLINE))
237 return true;
239 return false;
242 void db_ignore_states(int id)
244 use_states[id] = 0;
247 unsigned long long __fn_mtag;
248 static void set_fn_mtag(struct symbol *sym)
250 char buf[128];
252 if (is_local(cur_func_sym))
253 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
254 else
255 snprintf(buf, sizeof(buf), "extern %s", get_function());
257 __fn_mtag = str_to_mtag(buf);
260 void sql_insert_return_states(int return_id, const char *return_ranges,
261 int type, int param, const char *key, const char *value)
263 unsigned long long id;
266 if (key && strlen(key) >= 80)
267 return;
268 if (__inline_fn)
269 id = (unsigned long)__inline_fn;
270 else
271 id = __fn_mtag;
273 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
274 get_base_file(), get_function(), id, return_id,
275 return_ranges, fn_static(), type, param, key, value);
278 static struct string_list *common_funcs;
279 static int is_common_function(const char *fn)
281 char *tmp;
283 if (!fn)
284 return 0;
286 if (strncmp(fn, "__builtin_", 10) == 0)
287 return 1;
289 FOR_EACH_PTR(common_funcs, tmp) {
290 if (strcmp(tmp, fn) == 0)
291 return 1;
292 } END_FOR_EACH_PTR(tmp);
294 return 0;
297 static char *function_signature(void)
299 return type_to_str(get_real_base_type(cur_func_sym));
302 void sql_insert_caller_info(struct expression *call, int type,
303 int param, const char *key, const char *value)
305 FILE *tmp_fd = sm_outfd;
306 char *fn;
308 if (!option_info && !__inline_call)
309 return;
310 if (unreachable())
311 return;
313 if (key && strlen(key) >= 80)
314 return;
316 fn = get_fnptr_name(call->fn);
317 if (!fn)
318 return;
320 if (__inline_call) {
321 mem_sql(NULL, NULL,
322 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
323 get_base_file(), get_function(), fn, (unsigned long)call,
324 is_static(call->fn), type, param, key, value);
327 if (!option_info)
328 return;
330 if (strncmp(fn, "__builtin_", 10) == 0)
331 return;
332 if (type != INTERNAL && is_common_function(fn))
333 return;
335 sm_outfd = caller_info_fd;
336 sm_msg("SQL_caller_info: insert into caller_info values ("
337 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
338 get_base_file(), get_function(), fn, is_static(call->fn),
339 type, param, key, value);
340 sm_outfd = tmp_fd;
342 free_string(fn);
345 void sql_insert_function_ptr(const char *fn, const char *struct_name)
347 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
348 get_base_file(), fn, struct_name);
351 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
353 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
354 get_base_file(), get_function(), (unsigned long)__inline_fn,
355 fn_static(), type, param, key, value);
358 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
360 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
361 get_base_file(), get_function(), (unsigned long)__inline_fn,
362 fn_static(), type, param, key, value);
365 void sql_insert_function_type_size(const char *member, const char *ranges)
367 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
370 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
372 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
375 void sql_insert_type_info(int type, const char *member, const char *value)
377 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
380 void sql_insert_local_values(const char *name, const char *value)
382 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
385 void sql_insert_function_type_value(const char *type, const char *value)
387 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
390 void sql_insert_function_type(int param, const char *value)
392 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
393 get_base_file(), get_function(), fn_static(), param, value);
396 void sql_insert_parameter_name(int param, const char *value)
398 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
399 get_base_file(), get_function(), fn_static(), param, value);
402 void sql_insert_data_info(struct expression *data, int type, const char *value)
404 char *data_name;
406 data_name = get_data_info_name(data);
407 if (!data_name)
408 return;
409 sql_insert(data_info, "'%s', '%s', %d, '%s'",
410 is_static(data) ? get_base_file() : "extern",
411 data_name, type, value);
414 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
416 sql_insert(data_info, "'%s', '%s', %d, '%s'",
417 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
418 var, type, value);
421 void sql_save_constraint(const char *con)
423 if (!option_info)
424 return;
426 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
429 void sql_save_constraint_required(const char *data, int op, const char *limit)
431 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
434 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
436 if (!option_info)
437 return;
439 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
440 "select constraints_required.data, constraints_required.op, '%s' from "
441 "constraints_required where bound = '%s';", new_limit, old_limit);
444 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
446 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
449 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
451 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
452 return;
454 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
455 is_local(fn->symbol) ? get_base_file() : "extern",
456 fn->symbol->ident->name,
457 is_local(fn->symbol),
458 type, param, key, value);
461 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
463 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
464 tag, get_filename(), get_function(), get_lineno(),
465 left_name, right_name);
468 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
470 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
473 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
475 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
478 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
480 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
483 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
485 mtag_t *saved_tag = _tag;
486 mtag_t new_tag;
488 new_tag = strtoll(argv[0], NULL, 10);
490 if (!*saved_tag)
491 *saved_tag = new_tag;
492 else if (*saved_tag != new_tag)
493 *saved_tag = -1ULL;
495 return 0;
498 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
500 mtag_t tmp = 0;
502 run_sql(save_mtag, &tmp,
503 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
504 tag, container_offset);
506 if (tmp == 0 || tmp == -1ULL)
507 return 0;
508 *container = tmp;
509 return 1;
512 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
514 mtag_t tmp = 0;
516 run_sql(save_mtag, &tmp,
517 "select tag from mtag_map where container = %lld and container_offset = %d;",
518 container, offset);
520 if (tmp == 0 || tmp == -1ULL)
521 return 0;
522 *tag = tmp;
523 return 1;
526 char *get_static_filter(struct symbol *sym)
528 static char sql_filter[1024];
530 /* This can only happen on buggy code. Return invalid SQL. */
531 if (!sym) {
532 sql_filter[0] = '\0';
533 return sql_filter;
536 if (is_local(sym)) {
537 snprintf(sql_filter, sizeof(sql_filter),
538 "file = '%s' and function = '%s' and static = '1'",
539 get_base_file(), sym->ident->name);
540 } else {
541 snprintf(sql_filter, sizeof(sql_filter),
542 "function = '%s' and static = '0'", sym->ident->name);
545 return sql_filter;
548 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
550 int *row_count = _row_count;
552 *row_count = 0;
553 if (argc != 1)
554 return 0;
555 *row_count = atoi(argv[0]);
556 return 0;
559 static void mark_call_params_untracked(struct expression *call)
561 struct expression *arg;
562 int i = 0;
564 FOR_EACH_PTR(call->args, arg) {
565 mark_untracked(call, i++, "$", NULL);
566 } END_FOR_EACH_PTR(arg);
569 static void sql_select_return_states_pointer(const char *cols,
570 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
572 char *ptr;
573 int return_count = 0;
575 ptr = get_fnptr_name(call->fn);
576 if (!ptr)
577 return;
579 run_sql(get_row_count, &return_count,
580 "select count(*) from return_states join function_ptr "
581 "where return_states.function == function_ptr.function and "
582 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
583 /* The magic number 100 is just from testing on the kernel. */
584 if (return_count > 100) {
585 mark_call_params_untracked(call);
586 return;
589 run_sql(callback, info,
590 "select %s from return_states join function_ptr where "
591 "return_states.function == function_ptr.function and ptr = '%s' "
592 "and searchable = 1 "
593 "order by function_ptr.file, return_states.file, return_id, type;",
594 cols, ptr);
597 static int is_local_symbol(struct expression *expr)
599 if (expr->type != EXPR_SYMBOL)
600 return 0;
601 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
602 return 0;
603 return 1;
606 bool is_fn_ptr(struct expression *fn)
608 fn = strip_expr(fn);
609 if (fn->type != EXPR_SYMBOL)
610 return true;
611 if (!fn->symbol)
612 return true;
613 if (is_local_symbol(fn))
614 return true;
615 return false;
618 void sql_select_return_states(const char *cols, struct expression *call,
619 int (*callback)(void*, int, char**, char**), void *info)
621 struct expression *fn;
622 int row_count = 0;
624 if (is_fake_call(call))
625 return;
627 fn = strip_expr(call->fn);
628 if (is_fn_ptr(fn)) {
629 sql_select_return_states_pointer(cols, call, callback, info);
630 return;
633 if (inlinable(fn)) {
634 mem_sql(callback, info,
635 "select %s from return_states where call_id = '%lu' order by return_id, type;",
636 cols, (unsigned long)call);
637 return;
640 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
641 get_static_filter(fn->symbol));
642 if (row_count == 0 && fn->symbol && fn->symbol->definition)
643 set_state(my_id, "db_incomplete", NULL, &incomplete);
644 if (row_count > 3000)
645 return;
647 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
648 cols, get_static_filter(fn->symbol));
651 bool db_incomplete(void)
653 return !!get_state(my_id, "db_incomplete", NULL);
656 #define CALL_IMPLIES 0
657 #define RETURN_IMPLIES 1
659 struct implies_info {
660 int type;
661 struct db_implies_cb_list *cb_list;
662 struct expression *expr;
663 struct symbol *sym;
666 void sql_select_implies(const char *cols, struct implies_info *info,
667 int (*callback)(void*, int, char**, char**))
669 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
670 mem_sql(callback, info,
671 "select %s from return_implies where call_id = '%lu';",
672 cols, (unsigned long)info->expr);
673 return;
676 run_sql(callback, info, "select %s from %s_implies where %s;",
677 cols,
678 info->type == CALL_IMPLIES ? "call" : "return",
679 get_static_filter(info->sym));
682 struct select_caller_info_data {
683 struct stree *final_states;
684 struct timeval start_time;
685 int prev_func_id;
686 int ignore;
687 int results;
690 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
692 static void sql_select_caller_info(struct select_caller_info_data *data,
693 const char *cols, struct symbol *sym)
695 if (__inline_fn) {
696 mem_sql(caller_info_callback, data,
697 "select %s from caller_info where call_id = %lu;",
698 cols, (unsigned long)__inline_fn);
699 return;
702 if (sym->ident->name && is_common_function(sym->ident->name))
703 return;
704 run_sql(caller_info_callback, data,
705 "select %s from common_caller_info where %s order by call_id;",
706 cols, get_static_filter(sym));
707 if (data->results)
708 return;
710 run_sql(caller_info_callback, data,
711 "select %s from caller_info where %s order by call_id;",
712 cols, get_static_filter(sym));
715 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
717 struct def_callback *def_callback = __alloc_def_callback(0);
719 def_callback->hook_type = type;
720 def_callback->callback = callback;
721 add_ptr_list(&select_caller_info_callbacks, def_callback);
724 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
726 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
728 callback->hook_type = type;
729 callback->callback = fn;
730 add_ptr_list(&select_caller_name_sym_callbacks, callback);
734 * These call backs are used when the --info option is turned on to print struct
735 * member information. For example foo->bar could have a state in
736 * smatch_extra.c and also check_user.c.
738 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
740 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
742 member_callback->owner = owner;
743 member_callback->callback = callback;
744 add_ptr_list(&member_callbacks, member_callback);
747 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
749 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
751 member_callback->owner = owner;
752 member_callback->callback = callback;
753 add_ptr_list(&member_callbacks_new, member_callback);
756 void add_return_info_callback(int owner,
757 void (*callback)(int return_id, char *return_ranges,
758 struct expression *returned_expr,
759 int param,
760 const char *printed_name,
761 struct sm_state *sm))
763 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
765 return_callback->owner = owner;
766 return_callback->callback = callback;
767 add_ptr_list(&return_callbacks, return_callback);
770 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
772 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
774 callback->callback = fn;
775 add_ptr_list(&returned_state_callbacks, callback);
778 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))
780 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
782 member_callback->owner = owner;
783 member_callback->callback = callback;
784 add_ptr_list(&returned_member_callbacks, member_callback);
787 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
789 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
791 cb->type = type;
792 cb->callback = callback;
793 add_ptr_list(&call_implies_cb_list, cb);
796 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
798 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
800 cb->type = type;
801 cb->callback = callback;
802 add_ptr_list(&return_implies_cb_list, cb);
805 struct return_info {
806 struct expression *static_returns_call;
807 struct symbol *return_type;
808 struct range_list *return_range_list;
811 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
813 struct return_info *ret_info = _ret_info;
814 struct range_list *rl;
815 struct expression *call_expr = ret_info->static_returns_call;
817 if (argc != 1)
818 return 0;
819 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
820 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
821 return 0;
824 struct range_list *db_return_vals(struct expression *expr)
826 struct return_info ret_info = {};
827 struct sm_state *sm;
829 if (is_fake_call(expr))
830 return NULL;
832 sm = get_extra_sm_state(expr);
833 if (sm)
834 return clone_rl(estate_rl(sm->state));
835 ret_info.static_returns_call = expr;
836 ret_info.return_type = get_type(expr);
837 if (!ret_info.return_type)
838 return NULL;
840 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
841 return NULL;
843 ret_info.return_range_list = NULL;
844 if (inlinable(expr->fn)) {
845 mem_sql(db_return_callback, &ret_info,
846 "select distinct return from return_states where call_id = '%lu';",
847 (unsigned long)expr);
848 } else {
849 run_sql(db_return_callback, &ret_info,
850 "select distinct return from return_states where %s;",
851 get_static_filter(expr->fn->symbol));
853 return ret_info.return_range_list;
856 struct range_list *db_return_vals_from_str(const char *fn_name)
858 struct return_info ret_info;
860 ret_info.static_returns_call = NULL;
861 ret_info.return_type = &llong_ctype;
862 ret_info.return_range_list = NULL;
864 run_sql(db_return_callback, &ret_info,
865 "select distinct return from return_states where function = '%s';",
866 fn_name);
867 return ret_info.return_range_list;
871 * This is used when we have a function that takes a function pointer as a
872 * parameter. "frob(blah, blah, my_function);" We know that the return values
873 * from frob() come from my_funcion() so we want to find the possible returns
874 * of my_function(), but we don't know which arguments are passed to it.
877 struct range_list *db_return_vals_no_args(struct expression *expr)
879 struct return_info ret_info = {};
881 if (!expr || expr->type != EXPR_SYMBOL)
882 return NULL;
884 ret_info.static_returns_call = expr;
885 ret_info.return_type = get_type(expr);
886 ret_info.return_type = get_real_base_type(ret_info.return_type);
887 if (!ret_info.return_type)
888 return NULL;
890 run_sql(db_return_callback, &ret_info,
891 "select distinct return from return_states where %s;",
892 get_static_filter(expr->symbol));
894 return ret_info.return_range_list;
897 static void match_call_marker(struct expression *expr)
899 struct symbol *type;
901 type = get_type(expr->fn);
902 if (type && type->type == SYM_PTR)
903 type = get_real_base_type(type);
906 * we just want to record something in the database so that if we have
907 * two calls like: frob(4); frob(some_unkown); then on the receiving
908 * side we know that sometimes frob is called with unknown parameters.
911 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
914 int is_recursive_member(const char *name)
916 char buf[256];
917 const char *p, *next;
918 int size;
920 p = strchr(name, '>');
921 if (!p)
922 return 0;
923 p++;
924 while (true) {
925 next = strchr(p, '>');
926 if (!next)
927 return 0;
928 next++;
930 size = next - p;
931 if (size >= sizeof(buf))
932 return 0;
933 memcpy(buf, p, size);
934 buf[size] = '\0';
935 if (strstr(next, buf))
936 return 1;
937 p = next;
941 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
943 struct symbol *sym;
944 const char *sm_name;
945 char *name;
946 bool is_address = false;
947 bool add_star = false;
948 char buf[256];
949 char *ret = NULL;
950 int len;
952 expr = strip_expr(expr);
953 if (!expr)
954 return NULL;
956 if (expr->type == EXPR_PREOP && expr->op == '&') {
957 expr = strip_expr(expr->unop);
958 is_address = true;
961 name = expr_to_var_sym(expr, &sym);
962 if (!name || !sym)
963 goto free;
964 if (sym != sm->sym)
965 goto free;
967 sm_name = sm->name;
968 add_star = false;
969 if (sm_name[0] == '*') {
970 add_star = true;
971 sm_name++;
974 len = strlen(name);
975 if (strncmp(name, sm_name, len) != 0)
976 goto free;
977 if (sm_name[len] == '\0') {
978 snprintf(buf, sizeof(buf), "%s%s$",
979 add_star ? "*" : "", is_address ? "*" : "");
980 } else {
981 if (sm_name[len] != '.' && sm_name[len] != '-')
982 goto free;
983 if (sm_name[len] == '-')
984 len++;
985 // FIXME does is_address really imply that sm_name[len] == '-'
986 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
987 sm_name + len);
990 ret = alloc_sname(buf);
991 free:
992 free_string(name);
993 return ret;
996 static void print_struct_members(struct expression *call, struct expression *expr, int param,
997 int owner,
998 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
999 bool new)
1001 struct sm_state *sm;
1002 const char *sm_name;
1003 char *name;
1004 struct symbol *sym;
1005 int len;
1006 char printed_name[256];
1007 int is_address = 0;
1008 bool add_star;
1009 struct symbol *type;
1011 expr = strip_expr(expr);
1012 if (!expr)
1013 return;
1014 type = get_type(expr);
1015 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1016 return;
1018 if (expr->type == EXPR_PREOP && expr->op == '&') {
1019 expr = strip_expr(expr->unop);
1020 is_address = 1;
1023 name = expr_to_var_sym(expr, &sym);
1024 if (!name || !sym)
1025 goto free;
1027 len = strlen(name);
1028 FOR_EACH_SM(__get_cur_stree(), sm) {
1029 if (sm->owner != owner || sm->sym != sym)
1030 continue;
1031 sm_name = sm->name;
1032 add_star = false;
1033 if (sm_name[0] == '*') {
1034 add_star = true;
1035 sm_name++;
1037 // FIXME: simplify?
1038 if (!add_star && strcmp(name, sm_name) == 0) {
1039 if (is_address) {
1040 snprintf(printed_name, sizeof(printed_name), "*$");
1041 } else {
1042 if (new)
1043 snprintf(printed_name, sizeof(printed_name), "$");
1044 else
1045 continue;
1047 } else if (add_star && strcmp(name, sm_name) == 0) {
1048 snprintf(printed_name, sizeof(printed_name), "%s*$",
1049 is_address ? "*" : "");
1050 } else if (strncmp(name, sm_name, len) == 0) {
1051 if (sm_name[len] != '.' && sm_name[len] != '-')
1052 continue;
1053 if (is_address && sm_name[len] == '.') {
1054 snprintf(printed_name, sizeof(printed_name),
1055 "%s$->%s", add_star ? "*" : "",
1056 sm_name + len + 1);
1057 } else if (is_address && sm_name[len] == '-') {
1058 snprintf(printed_name, sizeof(printed_name),
1059 "%s(*$)%s", add_star ? "*" : "",
1060 sm_name + len);
1061 } else {
1062 snprintf(printed_name, sizeof(printed_name),
1063 "%s$%s", add_star ? "*" : "",
1064 sm_name + len);
1066 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1067 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1068 continue;
1069 if (is_address && sm_name[len + 1] == '.') {
1070 snprintf(printed_name, sizeof(printed_name),
1071 "&%s$->%s", add_star ? "*" : "",
1072 sm_name + len + 2);
1073 } else if (is_address && sm_name[len] == '-') {
1074 snprintf(printed_name, sizeof(printed_name),
1075 "&%s(*$)%s", add_star ? "*" : "",
1076 sm_name + len + 1);
1077 } else {
1078 snprintf(printed_name, sizeof(printed_name),
1079 "&%s$%s", add_star ? "*" : "",
1080 sm_name + len + 1);
1082 } else {
1083 continue;
1085 if (is_recursive_member(printed_name))
1086 continue;
1087 callback(call, param, printed_name, sm);
1088 } END_FOR_EACH_SM(sm);
1089 free:
1090 free_string(name);
1093 static void match_call_info(struct expression *call)
1095 struct member_info_callback *cb;
1096 struct expression *arg;
1097 int i;
1099 FOR_EACH_PTR(member_callbacks, cb) {
1100 i = -1;
1101 FOR_EACH_PTR(call->args, arg) {
1102 i++;
1103 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1104 } END_FOR_EACH_PTR(arg);
1105 } END_FOR_EACH_PTR(cb);
1108 static struct expression *get_fake_variable(struct expression *expr)
1110 struct expression *tmp;
1112 tmp = expr_get_fake_parent_expr(expr);
1113 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1114 return NULL;
1116 return tmp->left;
1119 static void match_call_info_new(struct expression *call)
1121 struct member_info_callback *cb;
1122 struct expression *arg, *tmp;
1123 int i;
1125 if (!option_info && !__inline_call && !local_debug)
1126 return;
1128 FOR_EACH_PTR(member_callbacks_new, cb) {
1129 i = -1;
1130 FOR_EACH_PTR(call->args, arg) {
1131 i++;
1132 tmp = get_fake_variable(arg);
1133 if (!tmp)
1134 tmp = arg;
1135 __ignore_param_used++;
1136 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1137 __ignore_param_used--;
1138 } END_FOR_EACH_PTR(arg);
1139 } END_FOR_EACH_PTR(cb);
1142 static int get_param(int param, char **name, struct symbol **sym)
1144 struct symbol *arg;
1145 int i;
1147 i = 0;
1148 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1150 * this is a temporary hack to work around a bug (I think in sparse?)
1151 * 2.6.37-rc1:fs/reiserfs/journal.o
1152 * If there is a function definition without parameter name found
1153 * after a function implementation then it causes a crash.
1154 * int foo() {}
1155 * int bar(char *);
1157 if (arg->ident->name < (char *)100)
1158 continue;
1159 if (i == param) {
1160 *name = arg->ident->name;
1161 *sym = arg;
1162 return TRUE;
1164 i++;
1165 } END_FOR_EACH_PTR(arg);
1167 return FALSE;
1170 static int function_signature_matches(const char *sig)
1172 char *my_sig;
1174 my_sig = function_signature();
1175 if (!sig || !my_sig)
1176 return 1; /* default to matching */
1177 if (strcmp(my_sig, sig) == 0)
1178 return 1;
1179 return 0;
1182 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1184 struct select_caller_info_data *data = _data;
1185 int func_id;
1186 long type;
1187 long param;
1188 char *key;
1189 char *value;
1190 char *name = NULL;
1191 struct symbol *sym = NULL;
1192 struct def_callback *def_callback;
1193 struct def_name_sym_callback *ns_callback;
1194 struct stree *stree;
1195 struct timeval cur_time;
1196 char fullname[256];
1197 char *p;
1199 data->results = 1;
1201 if (argc != 5)
1202 return 0;
1204 gettimeofday(&cur_time, NULL);
1205 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1206 return 0;
1208 func_id = atoi(argv[0]);
1209 errno = 0;
1210 type = strtol(argv[1], NULL, 10);
1211 param = strtol(argv[2], NULL, 10);
1212 if (errno)
1213 return 0;
1214 key = argv[3];
1215 value = argv[4];
1217 if (data->prev_func_id == -1)
1218 data->prev_func_id = func_id;
1219 if (func_id != data->prev_func_id) {
1220 stree = __pop_fake_cur_stree();
1221 if (!data->ignore)
1222 merge_stree(&data->final_states, stree);
1223 free_stree(&stree);
1224 __push_fake_cur_stree();
1225 __unnullify_path();
1226 data->prev_func_id = func_id;
1227 data->ignore = 0;
1230 if (data->ignore)
1231 return 0;
1232 if (type == INTERNAL &&
1233 !function_signature_matches(value)) {
1234 data->ignore = 1;
1235 return 0;
1238 if (param >= 0 && !get_param(param, &name, &sym))
1239 return 0;
1241 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1242 if (def_callback->hook_type == type)
1243 def_callback->callback(name, sym, key, value);
1244 } END_FOR_EACH_PTR(def_callback);
1246 p = strchr(key, '$');
1247 if (name && p)
1248 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1249 else
1250 snprintf(fullname, sizeof(fullname), "%s", key);
1252 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1253 if (ns_callback->hook_type == type)
1254 ns_callback->callback(fullname, sym, value);
1255 } END_FOR_EACH_PTR(ns_callback);
1257 return 0;
1260 static struct string_list *ptr_names_done;
1261 static struct string_list *ptr_names;
1263 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1265 insert_string(&ptr_names, alloc_string(argv[0]));
1266 return 0;
1269 static char *get_next_ptr_name(void)
1271 char *ptr;
1273 FOR_EACH_PTR(ptr_names, ptr) {
1274 if (!insert_string(&ptr_names_done, ptr))
1275 continue;
1276 return ptr;
1277 } END_FOR_EACH_PTR(ptr);
1278 return NULL;
1281 static void get_ptr_names(const char *file, const char *name)
1283 char sql_filter[1024];
1284 int before, after;
1286 if (file) {
1287 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1288 file, name);
1289 } else {
1290 snprintf(sql_filter, 1024, "function = '%s';", name);
1293 before = ptr_list_size((struct ptr_list *)ptr_names);
1295 run_sql(get_ptr_name, NULL,
1296 "select distinct ptr from function_ptr where %s",
1297 sql_filter);
1299 after = ptr_list_size((struct ptr_list *)ptr_names);
1300 if (before == after)
1301 return;
1303 while ((name = get_next_ptr_name()))
1304 get_ptr_names(NULL, name);
1307 static void match_data_from_db(struct symbol *sym)
1309 struct select_caller_info_data data = { .prev_func_id = -1 };
1310 struct sm_state *sm;
1311 struct stree *stree;
1312 struct timeval end_time;
1314 if (!sym || !sym->ident)
1315 return;
1317 set_fn_mtag(sym);
1318 gettimeofday(&data.start_time, NULL);
1320 __push_fake_cur_stree();
1321 __unnullify_path();
1323 if (!__inline_fn) {
1324 char *ptr;
1326 if (sym->ctype.modifiers & MOD_STATIC)
1327 get_ptr_names(get_base_file(), sym->ident->name);
1328 else
1329 get_ptr_names(NULL, sym->ident->name);
1331 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1332 __free_ptr_list((struct ptr_list **)&ptr_names);
1333 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1334 __free_fake_cur_stree();
1335 return;
1338 sql_select_caller_info(&data,
1339 "call_id, type, parameter, key, value",
1340 sym);
1343 stree = __pop_fake_cur_stree();
1344 if (!data.ignore)
1345 merge_stree(&data.final_states, stree);
1346 free_stree(&stree);
1347 __push_fake_cur_stree();
1348 __unnullify_path();
1349 data.prev_func_id = -1;
1350 data.ignore = 0;
1351 data.results = 0;
1353 FOR_EACH_PTR(ptr_names, ptr) {
1354 run_sql(caller_info_callback, &data,
1355 "select call_id, type, parameter, key, value"
1356 " from common_caller_info where function = '%s' order by call_id",
1357 ptr);
1358 } END_FOR_EACH_PTR(ptr);
1360 if (data.results) {
1361 FOR_EACH_PTR(ptr_names, ptr) {
1362 free_string(ptr);
1363 } END_FOR_EACH_PTR(ptr);
1364 goto free_ptr_names;
1367 FOR_EACH_PTR(ptr_names, ptr) {
1368 run_sql(caller_info_callback, &data,
1369 "select call_id, type, parameter, key, value"
1370 " from caller_info where function = '%s' order by call_id",
1371 ptr);
1372 free_string(ptr);
1373 } END_FOR_EACH_PTR(ptr);
1375 free_ptr_names:
1376 __free_ptr_list((struct ptr_list **)&ptr_names);
1377 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1378 } else {
1379 sql_select_caller_info(&data,
1380 "call_id, type, parameter, key, value",
1381 sym);
1384 stree = __pop_fake_cur_stree();
1385 if (!data.ignore)
1386 merge_stree(&data.final_states, stree);
1387 free_stree(&stree);
1389 gettimeofday(&end_time, NULL);
1390 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1391 FOR_EACH_SM(data.final_states, sm) {
1392 __set_sm(sm);
1393 } END_FOR_EACH_SM(sm);
1396 free_stree(&data.final_states);
1399 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1401 struct implies_info *info = _info;
1402 struct db_implies_callback *cb;
1403 struct expression *arg = NULL;
1404 int type;
1405 int param;
1407 if (argc != 5)
1408 return 0;
1410 type = atoi(argv[1]);
1411 param = atoi(argv[2]);
1413 FOR_EACH_PTR(info->cb_list, cb) {
1414 if (cb->type != type)
1415 continue;
1416 if (param != -1) {
1417 arg = get_argument_from_call_expr(info->expr->args, param);
1418 if (!arg)
1419 continue;
1421 cb->callback(info->expr, arg, argv[3], argv[4]);
1422 } END_FOR_EACH_PTR(cb);
1424 return 0;
1427 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1429 struct implies_info *info = _info;
1430 struct db_implies_callback *cb;
1431 struct expression *arg;
1432 struct symbol *sym;
1433 char *name;
1434 int type;
1435 int param;
1437 if (argc != 5)
1438 return 0;
1440 type = atoi(argv[1]);
1441 param = atoi(argv[2]);
1443 if (!get_param(param, &name, &sym))
1444 return 0;
1445 arg = symbol_expression(sym);
1446 if (!arg)
1447 return 0;
1449 FOR_EACH_PTR(info->cb_list, cb) {
1450 if (cb->type != type)
1451 continue;
1452 cb->callback(info->expr, arg, argv[3], argv[4]);
1453 } END_FOR_EACH_PTR(cb);
1455 return 0;
1458 static void match_return_implies(struct expression *expr)
1460 struct implies_info info = {
1461 .type = RETURN_IMPLIES,
1462 .cb_list = return_implies_cb_list,
1465 if (expr->fn->type != EXPR_SYMBOL ||
1466 !expr->fn->symbol)
1467 return;
1468 info.expr = expr;
1469 info.sym = expr->fn->symbol;
1470 sql_select_implies("function, type, parameter, key, value", &info,
1471 return_implies_callbacks);
1474 static void match_call_implies(struct symbol *sym)
1476 struct implies_info info = {
1477 .type = CALL_IMPLIES,
1478 .cb_list = call_implies_cb_list,
1481 if (!sym || !sym->ident)
1482 return;
1484 info.sym = sym;
1485 sql_select_implies("function, type, parameter, key, value", &info,
1486 call_implies_callbacks);
1489 static char *get_fn_param_str(struct expression *expr)
1491 struct expression *tmp;
1492 int param;
1493 char buf[32];
1495 tmp = get_assigned_expr(expr);
1496 if (tmp)
1497 expr = tmp;
1498 expr = strip_expr(expr);
1499 if (!expr || expr->type != EXPR_CALL)
1500 return NULL;
1501 expr = strip_expr(expr->fn);
1502 if (!expr || expr->type != EXPR_SYMBOL)
1503 return NULL;
1504 param = get_param_num(expr);
1505 if (param < 0)
1506 return NULL;
1508 snprintf(buf, sizeof(buf), "[r $%d]", param);
1509 return alloc_sname(buf);
1512 static char *get_return_compare_is_param(struct expression *expr)
1514 char *var;
1515 char buf[256];
1516 int comparison;
1517 int param;
1519 param = get_param_num(expr);
1520 if (param < 0)
1521 return NULL;
1523 var = expr_to_var(expr);
1524 if (!var)
1525 return NULL;
1526 snprintf(buf, sizeof(buf), "%s orig", var);
1527 comparison = get_comparison_strings(var, buf);
1528 free_string(var);
1530 if (!comparison)
1531 return NULL;
1533 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1534 return alloc_sname(buf);
1537 static char *get_return_compare_str(struct expression *expr)
1539 char *compare_str;
1541 compare_str = get_return_compare_is_param(expr);
1542 if (compare_str)
1543 return compare_str;
1545 compare_str = expr_lte_to_param(expr, -1);
1546 if (compare_str)
1547 return compare_str;
1549 return expr_param_comparison(expr, -1);
1552 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1554 struct range_list *rl;
1555 char *return_ranges;
1556 sval_t sval;
1557 char *fn_param_str;
1558 char *compare_str;
1559 char *math_str;
1560 char buf[128];
1562 *rl_p = NULL;
1564 if (!expr)
1565 return alloc_sname("");
1567 if (get_implied_value(expr, &sval)) {
1568 sval = sval_cast(cur_func_return_type(), sval);
1569 *rl_p = alloc_rl(sval, sval);
1570 return sval_to_str_or_err_ptr(sval);
1573 fn_param_str = get_fn_param_str(expr);
1574 compare_str = expr_equal_to_param(expr, -1);
1575 math_str = get_value_in_terms_of_parameter_math(expr);
1577 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1578 rl = cast_rl(cur_func_return_type(), rl);
1579 return_ranges = show_rl(rl);
1580 } else if (get_imaginary_absolute(expr, &rl)){
1581 rl = cast_rl(cur_func_return_type(), rl);
1582 return alloc_sname(show_rl(rl));
1583 } else {
1584 get_absolute_rl(expr, &rl);
1585 rl = cast_rl(cur_func_return_type(), rl);
1586 return_ranges = show_rl(rl);
1588 *rl_p = rl;
1590 if (fn_param_str) {
1591 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1592 return alloc_sname(buf);
1594 if (compare_str) {
1595 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1596 return alloc_sname(buf);
1598 if (math_str) {
1599 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1600 return alloc_sname(buf);
1602 compare_str = get_return_compare_str(expr);
1603 if (compare_str) {
1604 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1605 return alloc_sname(buf);
1608 return return_ranges;
1611 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1613 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1616 static bool call_return_state_hooks_conditional(struct expression *expr)
1618 int final_pass_orig = final_pass;
1619 static int recurse;
1621 if (recurse >= 2)
1622 return false;
1623 if (!expr ||
1624 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1625 return false;
1627 recurse++;
1629 __push_fake_cur_stree();
1631 final_pass = 0;
1632 __split_whole_condition(expr->conditional);
1633 final_pass = final_pass_orig;
1635 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1637 __push_true_states();
1638 __use_false_states();
1640 call_return_state_hooks(expr->cond_false);
1642 __merge_true_states();
1643 __free_fake_cur_stree();
1645 recurse--;
1646 return true;
1649 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1651 struct split_data *data = NULL;
1652 struct expression *compare;
1653 struct range_list *rl;
1654 char buf[64];
1655 char *math;
1656 sval_t sval;
1657 bool undo;
1658 int i;
1660 for (i = 0; i < split_count; i++) {
1661 if (strcmp(get_function(), forced_splits[i]->func) == 0) {
1662 data = forced_splits[i];
1663 break;
1666 if (!data)
1667 return false;
1669 // FIXME: this works for copy_to/from_user() because the only thing we
1670 // care about is zero/non-zero
1671 if (strcmp(data->rl, "0") != 0)
1672 return false;
1674 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1675 if (!compare)
1676 return false;
1677 if (get_implied_value(compare, &sval))
1678 return false;
1680 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1681 call_return_states_callbacks("0", expr);
1682 if (undo)
1683 end_assume();
1685 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1686 if (get_implied_rl(expr, &rl)) {
1687 math = strchr(return_ranges, '[');
1688 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1689 } else {
1690 snprintf(buf, sizeof(buf), "%s", return_ranges);
1692 call_return_states_callbacks(buf, expr);
1693 if (undo)
1694 end_assume();
1696 return true;
1699 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1701 struct returned_state_callback *cb;
1703 return_ranges = replace_return_ranges(return_ranges);
1704 if (is_delete_return(return_ranges))
1705 return;
1706 if (handle_forced_split(return_ranges, expr))
1707 return;
1709 return_id++;
1710 FOR_EACH_PTR(returned_state_callbacks, cb) {
1711 cb->callback(return_id, (char *)return_ranges, expr);
1712 } END_FOR_EACH_PTR(cb);
1715 static void call_return_state_hooks_compare(struct expression *expr)
1717 char *return_ranges;
1718 int final_pass_orig = final_pass;
1719 sval_t sval = { .type = &int_ctype };
1720 sval_t ret;
1722 if (!get_implied_value(expr, &ret))
1723 ret.value = -1;
1725 __push_fake_cur_stree();
1727 final_pass = 0;
1728 __split_whole_condition(expr);
1729 final_pass = final_pass_orig;
1731 if (ret.value != 0) {
1732 return_ranges = alloc_sname("1");
1733 sval.value = 1;
1734 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1736 call_return_states_callbacks(return_ranges, expr);
1739 __push_true_states();
1740 __use_false_states();
1742 if (ret.value != 1) {
1743 return_ranges = alloc_sname("0");
1744 sval.value = 0;
1745 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1747 call_return_states_callbacks(return_ranges, expr);
1750 __merge_true_states();
1751 __free_fake_cur_stree();
1754 static bool is_implies_function(struct expression *expr)
1756 struct range_list *rl;
1758 if (!expr)
1759 return false;
1761 rl = get_range_implications(get_function());
1762 if (!rl)
1763 return false;
1765 sm_msg("%s: is implied", __func__);
1766 return true;
1769 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1771 struct sm_state *tmp;
1773 FOR_EACH_PTR(slist, tmp) {
1774 if (strcmp(tmp->state->name, sm->state->name) == 0)
1775 return 1;
1776 } END_FOR_EACH_PTR(tmp);
1778 return 0;
1781 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1783 struct range_list *rl;
1784 char *return_ranges;
1785 struct sm_state *tmp;
1786 int ret = 0;
1787 int nr_possible, nr_states;
1788 char *compare_str;
1789 char buf[128];
1790 struct state_list *already_handled = NULL;
1791 sval_t sval;
1793 if (!sm || !sm->merged)
1794 return 0;
1796 if (too_many_possible(sm) && !is_implies_function(expr))
1797 return 0;
1799 /* bail if it gets too complicated */
1800 nr_possible = 0;
1801 FOR_EACH_PTR(sm->possible, tmp) {
1802 if (tmp->merged)
1803 continue;
1804 if (ptr_in_list(tmp, already_handled))
1805 continue;
1806 add_ptr_list(&already_handled, tmp);
1807 nr_possible++;
1808 } END_FOR_EACH_PTR(tmp);
1809 free_slist(&already_handled);
1810 nr_states = get_db_state_count();
1811 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1812 return 0;
1814 FOR_EACH_PTR(sm->possible, tmp) {
1815 if (tmp->merged)
1816 continue;
1817 if (ptr_in_list(tmp, already_handled))
1818 continue;
1819 add_ptr_list(&already_handled, tmp);
1821 ret = 1;
1822 __push_fake_cur_stree();
1824 overwrite_states_using_pool(sm, tmp);
1826 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1827 return_ranges = show_rl(rl);
1828 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1829 compare_str = get_return_compare_str(expr);
1830 /* ignore obvious stuff like 0 <= param */
1831 /* Is this worthile when we have PARAM_COMPARE? */
1832 if (compare_str &&
1833 strncmp(compare_str, "[=", 2) != 0 &&
1834 rl_to_sval(rl, &sval))
1835 compare_str = NULL;
1836 if (compare_str) {
1837 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1838 return_ranges = alloc_sname(buf);
1841 call_return_states_callbacks(return_ranges, expr);
1843 __free_fake_cur_stree();
1844 } END_FOR_EACH_PTR(tmp);
1846 free_slist(&already_handled);
1848 return ret;
1851 static int call_return_state_hooks_split_possible(struct expression *expr)
1853 struct expression *fake;
1854 struct sm_state *sm;
1856 if (!expr)
1857 return 0;
1859 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1860 if (!sm) {
1861 fake = expr_get_fake_parent_expr(expr);
1862 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1863 return 0;
1864 fake = fake->left;
1865 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1867 return split_possible_helper(sm, expr);
1870 static bool has_possible_negative(struct sm_state *sm)
1872 struct sm_state *tmp;
1874 if (!type_signed(estate_type(sm->state)))
1875 return false;
1877 FOR_EACH_PTR(sm->possible, tmp) {
1878 if (!estate_rl(tmp->state))
1879 continue;
1880 if (sval_is_negative(estate_min(tmp->state)) &&
1881 sval_is_negative(estate_max(tmp->state)))
1882 return true;
1883 } END_FOR_EACH_PTR(tmp);
1885 return false;
1888 static bool has_separate_zero_null(struct sm_state *sm)
1890 struct sm_state *tmp;
1891 sval_t sval;
1893 FOR_EACH_PTR(sm->possible, tmp) {
1894 if (!estate_get_single_value(tmp->state, &sval))
1895 continue;
1896 if (sval.value == 0)
1897 return true;
1898 } END_FOR_EACH_PTR(tmp);
1900 return false;
1903 static int split_positive_from_negative(struct expression *expr)
1905 struct sm_state *sm;
1906 struct range_list *rl;
1907 const char *return_ranges;
1908 struct range_list *ret_rl;
1909 bool separate_zero;
1910 int undo;
1912 /* We're going to print the states 3 times */
1913 if (get_db_state_count() > 10000 / 3)
1914 return 0;
1916 if (!get_implied_rl(expr, &rl) || !rl)
1917 return 0;
1918 /* Forget about INT_MAX and larger */
1919 if (rl_max(rl).value <= 0)
1920 return 0;
1921 if (!sval_is_negative(rl_min(rl)))
1922 return 0;
1924 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1925 if (!sm)
1926 return 0;
1927 if (!has_possible_negative(sm))
1928 return 0;
1929 separate_zero = has_separate_zero_null(sm);
1931 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1932 return 0;
1934 return_ranges = get_return_ranges_str(expr, &ret_rl);
1935 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1936 call_return_states_callbacks(return_ranges, expr);
1938 end_assume();
1940 if (separate_zero) {
1941 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1943 return_ranges = get_return_ranges_str(expr, &ret_rl);
1944 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1945 call_return_states_callbacks(return_ranges, expr);
1947 if (undo)
1948 end_assume();
1951 undo = assume(compare_expression(expr, '<', zero_expr()));
1953 return_ranges = get_return_ranges_str(expr, &ret_rl);
1954 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1955 call_return_states_callbacks(return_ranges, expr);
1957 if (undo)
1958 end_assume();
1960 return 1;
1963 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1965 struct range_list *rl;
1966 struct range_list *nonnull_rl;
1967 sval_t null_sval;
1968 struct range_list *null_rl = NULL;
1969 char *return_ranges;
1970 struct sm_state *sm;
1971 struct smatch_state *state;
1972 int nr_states;
1973 int final_pass_orig = final_pass;
1975 if (!expr || expr_equal_to_param(expr, -1))
1976 return 0;
1977 if (expr->type == EXPR_CALL)
1978 return 0;
1980 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1981 if (!sm)
1982 return 0;
1983 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1984 return 0;
1985 state = sm->state;
1986 if (!estate_rl(state))
1987 return 0;
1988 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1989 return 0;
1990 if (has_possible_negative(sm))
1991 return 0;
1992 if (!has_separate_zero_null(sm))
1993 return 0;
1995 nr_states = get_db_state_count();
1996 if (option_info && nr_states >= 1500)
1997 return 0;
1999 rl = estate_rl(state);
2001 __push_fake_cur_stree();
2003 final_pass = 0;
2004 __split_whole_condition(expr);
2005 final_pass = final_pass_orig;
2007 nonnull_rl = rl_filter(rl, rl_zero());
2008 return_ranges = show_rl(nonnull_rl);
2009 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2011 call_return_states_callbacks(return_ranges, expr);
2013 __push_true_states();
2014 __use_false_states();
2016 return_ranges = alloc_sname("0");
2017 null_sval = sval_type_val(rl_type(rl), 0);
2018 add_range(&null_rl, null_sval, null_sval);
2019 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2020 call_return_states_callbacks(return_ranges, expr);
2022 __merge_true_states();
2023 __free_fake_cur_stree();
2025 return 1;
2028 static bool is_kernel_success_fail(struct sm_state *sm)
2030 struct sm_state *tmp;
2031 struct range_list *rl;
2032 bool has_zero = false;
2033 bool has_neg = false;
2035 if (!type_signed(estate_type(sm->state)))
2036 return false;
2038 FOR_EACH_PTR(sm->possible, tmp) {
2039 rl = estate_rl(tmp->state);
2040 if (!rl)
2041 return false;
2042 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2043 has_zero = true;
2044 continue;
2046 has_neg = true;
2047 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
2048 continue;
2049 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
2050 continue;
2051 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
2052 continue;
2053 return false;
2054 } END_FOR_EACH_PTR(tmp);
2056 return has_zero && has_neg;
2059 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2061 struct sm_state *sm;
2062 struct range_list *rl;
2063 struct range_list *nonzero_rl;
2064 sval_t zero_sval;
2065 struct range_list *zero_rl = NULL;
2066 int nr_states;
2067 char *return_ranges;
2068 int final_pass_orig = final_pass;
2070 if (option_project != PROJ_KERNEL)
2071 return 0;
2073 nr_states = get_db_state_count();
2074 if (nr_states > 2000)
2075 return 0;
2077 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2078 if (!sm)
2079 return 0;
2080 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2081 return 0;
2082 if (!is_kernel_success_fail(sm))
2083 return 0;
2085 rl = estate_rl(sm->state);
2086 if (!rl)
2087 return 0;
2089 __push_fake_cur_stree();
2091 final_pass = 0;
2092 __split_whole_condition(expr);
2093 final_pass = final_pass_orig;
2095 nonzero_rl = rl_filter(rl, rl_zero());
2096 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2097 return_ranges = show_rl(nonzero_rl);
2098 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2100 call_return_states_callbacks(return_ranges, expr);
2102 __push_true_states();
2103 __use_false_states();
2105 return_ranges = alloc_sname("0");
2106 zero_sval = sval_type_val(rl_type(rl), 0);
2107 add_range(&zero_rl, zero_sval, zero_sval);
2108 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2109 call_return_states_callbacks(return_ranges, expr);
2111 __merge_true_states();
2112 __free_fake_cur_stree();
2114 return 1;
2117 static int is_boolean(struct expression *expr)
2119 struct range_list *rl;
2121 if (!get_implied_rl(expr, &rl))
2122 return 0;
2123 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2124 return 1;
2125 return 0;
2128 static int splitable_function_call(struct expression *expr)
2130 struct sm_state *sm;
2132 if (!expr || expr->type != EXPR_CALL)
2133 return 0;
2134 sm = get_extra_sm_state(expr);
2135 return split_possible_helper(sm, expr);
2138 static struct sm_state *find_bool_param(void)
2140 struct stree *start_states;
2141 struct symbol *arg;
2142 struct sm_state *sm, *tmp;
2143 sval_t sval;
2145 start_states = get_start_states();
2147 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2148 if (!arg->ident)
2149 continue;
2150 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2151 if (!sm)
2152 continue;
2153 if (rl_min(estate_rl(sm->state)).value != 0 ||
2154 rl_max(estate_rl(sm->state)).value != 1)
2155 continue;
2156 goto found;
2157 } END_FOR_EACH_PTR_REVERSE(arg);
2159 return NULL;
2161 found:
2163 * Check if it's splitable. If not, then splitting it up is likely not
2164 * useful for the callers.
2166 FOR_EACH_PTR(sm->possible, tmp) {
2167 if (is_merged(tmp))
2168 continue;
2169 if (!estate_get_single_value(tmp->state, &sval))
2170 return NULL;
2171 } END_FOR_EACH_PTR(tmp);
2173 return sm;
2176 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2178 struct range_list *ret_rl;
2179 const char *return_ranges;
2180 struct sm_state *tmp;
2181 int ret = 0;
2182 struct state_list *already_handled = NULL;
2184 if (!sm || !sm->merged)
2185 return 0;
2187 if (too_many_possible(sm))
2188 return 0;
2190 FOR_EACH_PTR(sm->possible, tmp) {
2191 if (tmp->merged)
2192 continue;
2193 if (ptr_in_list(tmp, already_handled))
2194 continue;
2195 add_ptr_list(&already_handled, tmp);
2197 ret = 1;
2198 __push_fake_cur_stree();
2200 overwrite_states_using_pool(sm, tmp);
2202 return_ranges = get_return_ranges_str(expr, &ret_rl);
2203 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2204 call_return_states_callbacks(return_ranges, expr);
2206 __free_fake_cur_stree();
2207 } END_FOR_EACH_PTR(tmp);
2209 free_slist(&already_handled);
2211 return ret;
2214 static int split_by_bool_param(struct expression *expr)
2216 struct sm_state *start_sm, *sm;
2217 sval_t sval;
2219 start_sm = find_bool_param();
2220 if (!start_sm)
2221 return 0;
2222 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2223 if (!sm || estate_get_single_value(sm->state, &sval))
2224 return 0;
2226 if (get_db_state_count() * 2 >= 2000)
2227 return 0;
2229 return split_on_bool_sm(sm, expr);
2232 static int split_by_null_nonnull_param(struct expression *expr)
2234 struct symbol *arg;
2235 struct sm_state *sm;
2236 int nr_possible;
2238 /* function must only take one pointer */
2239 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2240 return 0;
2241 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2242 if (!arg->ident)
2243 return 0;
2244 if (get_real_base_type(arg)->type != SYM_PTR)
2245 return 0;
2247 if (param_was_set_var_sym(arg->ident->name, arg))
2248 return 0;
2249 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2250 if (!sm)
2251 return 0;
2253 if (!has_separate_zero_null(sm))
2254 return 0;
2256 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2257 if (get_db_state_count() * nr_possible >= 2000)
2258 return 0;
2260 return split_on_bool_sm(sm, expr);
2263 struct expression *strip_expr_statement(struct expression *expr)
2265 struct expression *orig = expr;
2266 struct statement *stmt, *last_stmt;
2268 if (!expr)
2269 return NULL;
2270 if (expr->type == EXPR_PREOP && expr->op == '(')
2271 expr = expr->unop;
2272 if (expr->type != EXPR_STATEMENT)
2273 return orig;
2274 stmt = expr->statement;
2275 if (!stmt || stmt->type != STMT_COMPOUND)
2276 return orig;
2278 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2279 if (!last_stmt || last_stmt->type == STMT_LABEL)
2280 last_stmt = last_stmt->label_statement;
2281 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2282 return orig;
2283 return strip_expr(last_stmt->expression);
2286 static bool is_kernel_error_path(struct expression *expr)
2288 struct range_list *rl;
2291 * Splitting up returns requires resources. It also requires resources
2292 * for the caller. It doesn't seem worth it to split anything up.
2294 if (!get_implied_rl(expr, &rl))
2295 return false;
2296 if (rl_type(rl) != &int_ctype)
2297 return false;
2298 if (rl_min(rl).value >= -4095 &&
2299 rl_max(rl).value < 0)
2300 return true;
2301 return false;
2304 static void call_return_state_hooks(struct expression *expr)
2306 struct range_list *ret_rl;
2307 const char *return_ranges;
2308 int nr_states;
2309 sval_t sval;
2311 if (__path_is_null())
2312 return;
2314 expr = strip_expr(expr);
2315 expr = strip_expr_statement(expr);
2317 if (is_impossible_path())
2318 goto vanilla;
2320 if (expr && (expr->type == EXPR_COMPARE ||
2321 !get_implied_value(expr, &sval)) &&
2322 (is_condition(expr) || is_boolean(expr))) {
2323 call_return_state_hooks_compare(expr);
2324 return;
2325 } else if (call_return_state_hooks_conditional(expr)) {
2326 return;
2327 } else if (is_kernel_error_path(expr)) {
2328 goto vanilla;
2329 } else if (call_return_state_hooks_split_possible(expr)) {
2330 return;
2331 } else if (split_positive_from_negative(expr)) {
2332 return;
2333 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2334 return;
2335 } else if (call_return_state_hooks_split_success_fail(expr)) {
2336 return;
2337 } else if (splitable_function_call(expr)) {
2338 return;
2339 } else if (split_by_bool_param(expr)) {
2340 } else if (split_by_null_nonnull_param(expr)) {
2341 return;
2344 vanilla:
2345 return_ranges = get_return_ranges_str(expr, &ret_rl);
2346 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2348 nr_states = get_db_state_count();
2349 if (nr_states >= 10000) {
2350 return_id++;
2351 match_return_info(return_id, (char *)return_ranges, expr);
2352 print_limited_param_set(return_id, (char *)return_ranges, expr);
2353 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2354 return;
2356 call_return_states_callbacks(return_ranges, expr);
2359 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2361 struct returned_member_callback *cb;
2362 struct sm_state *sm;
2363 struct symbol *type;
2364 char *name;
2365 char member_name[256];
2366 int len;
2368 type = get_type(expr);
2369 if (!type || type->type != SYM_PTR)
2370 return;
2371 name = expr_to_var(expr);
2372 if (!name)
2373 return;
2375 len = strlen(name);
2376 FOR_EACH_PTR(returned_member_callbacks, cb) {
2377 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2378 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2379 strcpy(member_name, "*$");
2380 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2381 continue;
2383 if (strncmp(sm->name, name, len) != 0)
2384 continue;
2385 if (strncmp(sm->name + len, "->", 2) != 0)
2386 continue;
2387 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2388 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2389 } END_FOR_EACH_SM(sm);
2390 } END_FOR_EACH_PTR(cb);
2392 free_string(name);
2395 static void print_return_struct_info(int return_id, char *return_ranges,
2396 struct expression *expr,
2397 struct symbol *sym,
2398 struct return_info_callback *cb)
2400 struct sm_state *sm;
2401 const char *printed_name;
2402 int param;
2404 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2405 if (sm->sym && sm->sym == sym) {
2406 param = -1;
2407 } else {
2408 param = get_param_num_from_sym(sm->sym);
2409 if (param < 0)
2410 continue;
2413 printed_name = get_param_name(sm);
2414 if (!printed_name)
2415 continue;
2417 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2418 } END_FOR_EACH_SM(sm);
2421 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2423 struct return_info_callback *cb;
2424 struct expression *tmp;
2425 struct symbol *sym;
2427 if (!option_info && !__inline_fn &&
2428 !local_debug && !option_debug)
2429 return;
2431 tmp = get_fake_variable(expr);
2432 if (tmp)
2433 expr = tmp;
2434 sym = expr_to_sym(expr);
2436 FOR_EACH_PTR(return_callbacks, cb) {
2437 __ignore_param_used++;
2438 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2439 __ignore_param_used--;
2440 } END_FOR_EACH_PTR(cb);
2443 static void reset_memdb(struct symbol *sym)
2445 mem_sql(NULL, NULL, "delete from caller_info;");
2446 mem_sql(NULL, NULL, "delete from return_states;");
2447 mem_sql(NULL, NULL, "delete from call_implies;");
2448 mem_sql(NULL, NULL, "delete from return_implies;");
2451 static void match_end_func_info(struct symbol *sym)
2453 if (__path_is_null())
2454 return;
2455 call_return_state_hooks(NULL);
2458 static void match_after_func(struct symbol *sym)
2460 if (!__inline_fn)
2461 reset_memdb(sym);
2464 static void init_memdb(void)
2466 char *err = NULL;
2467 int rc;
2468 const char *schema_files[] = {
2469 "db/db.schema",
2470 "db/caller_info.schema",
2471 "db/common_caller_info.schema",
2472 "db/return_states.schema",
2473 "db/function_type_size.schema",
2474 "db/type_size.schema",
2475 "db/function_type_info.schema",
2476 "db/type_info.schema",
2477 "db/call_implies.schema",
2478 "db/return_implies.schema",
2479 "db/function_ptr.schema",
2480 "db/local_values.schema",
2481 "db/function_type_value.schema",
2482 "db/type_value.schema",
2483 "db/function_type.schema",
2484 "db/data_info.schema",
2485 "db/parameter_name.schema",
2486 "db/constraints.schema",
2487 "db/constraints_required.schema",
2488 "db/fn_ptr_data_link.schema",
2489 "db/fn_data_link.schema",
2490 "db/mtag_about.schema",
2491 "db/mtag_info.schema",
2492 "db/mtag_map.schema",
2493 "db/mtag_data.schema",
2494 "db/mtag_alias.schema",
2496 static char buf[4096];
2497 int fd;
2498 int ret;
2499 int i;
2501 rc = sqlite3_open(":memory:", &mem_db);
2502 if (rc != SQLITE_OK) {
2503 sm_ierror("starting In-Memory database.");
2504 return;
2507 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2508 fd = open_schema_file(schema_files[i]);
2509 if (fd < 0)
2510 continue;
2511 ret = read(fd, buf, sizeof(buf));
2512 if (ret < 0) {
2513 sm_ierror("failed to read: %s", schema_files[i]);
2514 continue;
2516 close(fd);
2517 if (ret == sizeof(buf)) {
2518 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2519 schema_files[i], sizeof(buf));
2520 continue;
2522 buf[ret] = '\0';
2523 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2524 if (rc != SQLITE_OK) {
2525 sm_ierror("SQL error #2: %s", err);
2526 sm_ierror("%s", buf);
2531 static void init_cachedb(void)
2533 char *err = NULL;
2534 int rc;
2535 const char *schema_files[] = {
2536 "db/call_implies.schema",
2537 "db/return_implies.schema",
2538 "db/type_info.schema",
2539 "db/mtag_about.schema",
2540 "db/mtag_data.schema",
2541 "db/mtag_info.schema",
2542 "db/sink_info.schema",
2544 static char buf[4096];
2545 int fd;
2546 int ret;
2547 int i;
2549 rc = sqlite3_open(":memory:", &cache_db);
2550 if (rc != SQLITE_OK) {
2551 sm_ierror("starting In-Memory database.");
2552 return;
2555 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2556 fd = open_schema_file(schema_files[i]);
2557 if (fd < 0)
2558 continue;
2559 ret = read(fd, buf, sizeof(buf));
2560 if (ret < 0) {
2561 sm_ierror("failed to read: %s", schema_files[i]);
2562 continue;
2564 close(fd);
2565 if (ret == sizeof(buf)) {
2566 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2567 schema_files[i], sizeof(buf));
2568 continue;
2570 buf[ret] = '\0';
2571 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2572 if (rc != SQLITE_OK) {
2573 sm_ierror("SQL error #2: %s", err);
2574 sm_ierror("%s", buf);
2579 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2581 static char buf[4096];
2582 char tmp[256];
2583 char *p = buf;
2584 char *table = _table;
2585 int i;
2588 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2589 for (i = 0; i < argc; i++) {
2590 if (i)
2591 p += snprintf(p, 4096 - (p - buf), ", ");
2592 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2593 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2596 p += snprintf(p, 4096 - (p - buf), ");");
2597 if (p - buf > 4096)
2598 return 0;
2600 sm_msg("SQL: %s", buf);
2601 return 0;
2604 static void dump_cache(struct symbol_list *sym_list)
2606 const char *cache_tables[] = {
2607 "type_info", "return_implies", "call_implies", "mtag_data",
2608 "mtag_info", "mtag_about", "sink_info",
2610 char buf[64];
2611 int i;
2613 if (!option_info)
2614 return;
2616 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2617 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2618 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2622 void open_smatch_db(char *db_file)
2624 int rc;
2626 if (option_no_db)
2627 return;
2629 use_states = malloc(num_checks + 1);
2630 memset(use_states, 0xff, num_checks + 1);
2632 init_memdb();
2633 init_cachedb();
2635 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2636 if (rc != SQLITE_OK) {
2637 option_no_db = 1;
2638 return;
2640 run_sql(NULL, NULL,
2641 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2642 return;
2645 static char *get_next_string(char **str)
2647 static char string[256];
2648 char *start;
2649 char *p = *str;
2650 int len, i, j;
2652 if (*p == '\0')
2653 return NULL;
2654 start = p;
2656 while (*p != '\0' && *p != '\n') {
2657 if (*p == '\\' && *(p + 1) == ' ') {
2658 p += 2;
2659 continue;
2661 if (*p == ' ')
2662 break;
2663 p++;
2666 len = p - start;
2667 if (len >= sizeof(string)) {
2668 memcpy(string, start, sizeof(string));
2669 string[sizeof(string) - 1] = '\0';
2670 sm_ierror("return_fix: '%s' too long", string);
2671 **str = '\0';
2672 return NULL;
2674 memcpy(string, start, len);
2675 string[len] = '\0';
2676 for (i = 0; i < sizeof(string) - 1; i++) {
2677 if (string[i] == '\\' && string[i + 1] == ' ') {
2678 for (j = i; string[j] != '\0'; j++)
2679 string[j] = string[j + 1];
2682 if (*p != '\0')
2683 p++;
2684 *str = p;
2685 return string;
2688 static void register_return_deletes(void)
2690 char *func, *ret_str;
2691 char filename[256];
2692 char buf[4096];
2693 int fd, ret, i;
2694 char *p;
2696 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2697 fd = open_schema_file(filename);
2698 if (fd < 0)
2699 return;
2700 ret = read(fd, buf, sizeof(buf));
2701 close(fd);
2702 if (ret < 0)
2703 return;
2704 if (ret == sizeof(buf)) {
2705 sm_ierror("file too large: %s (limit %zd bytes)",
2706 filename, sizeof(buf));
2707 return;
2709 buf[ret] = '\0';
2711 p = buf;
2712 while (*p) {
2713 get_next_string(&p);
2714 delete_count++;
2716 if (delete_count == 0)
2717 return;
2718 if (delete_count % 2 != 0) {
2719 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2720 delete_count = 0;
2721 return;
2723 delete_table = malloc(delete_count * sizeof(char *));
2725 p = buf;
2726 i = 0;
2727 while (*p) {
2728 func = alloc_string(get_next_string(&p));
2729 ret_str = alloc_string(get_next_string(&p));
2731 delete_table[i++] = func;
2732 delete_table[i++] = ret_str;
2736 static void register_return_replacements(void)
2738 char *func, *orig, *new;
2739 char filename[256];
2740 char buf[4096];
2741 int fd, ret, i;
2742 char *p;
2744 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2745 fd = open_schema_file(filename);
2746 if (fd < 0)
2747 return;
2748 ret = read(fd, buf, sizeof(buf));
2749 close(fd);
2750 if (ret < 0)
2751 return;
2752 if (ret == sizeof(buf)) {
2753 sm_ierror("file too large: %s (limit %zd bytes)",
2754 filename, sizeof(buf));
2755 return;
2757 buf[ret] = '\0';
2759 p = buf;
2760 while (*p) {
2761 get_next_string(&p);
2762 replace_count++;
2764 if (replace_count == 0 || replace_count % 3 != 0) {
2765 replace_count = 0;
2766 return;
2768 replace_table = malloc(replace_count * sizeof(char *));
2770 p = buf;
2771 i = 0;
2772 while (*p) {
2773 func = alloc_string(get_next_string(&p));
2774 orig = alloc_string(get_next_string(&p));
2775 new = alloc_string(get_next_string(&p));
2777 replace_table[i++] = func;
2778 replace_table[i++] = orig;
2779 replace_table[i++] = new;
2783 static void register_forced_return_splits(void)
2785 int struct_members = sizeof(struct split_data) / sizeof(char *);
2786 char filename[256];
2787 char buf[4096];
2788 int fd, ret, i;
2789 char *p;
2791 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
2792 fd = open_schema_file(filename);
2793 if (fd < 0)
2794 return;
2795 ret = read(fd, buf, sizeof(buf));
2796 close(fd);
2797 if (ret < 0)
2798 return;
2799 if (ret == sizeof(buf)) {
2800 sm_ierror("file too large: %s (limit %zd bytes)",
2801 filename, sizeof(buf));
2802 return;
2804 buf[ret] = '\0';
2806 p = buf;
2807 while (*p) {
2808 get_next_string(&p);
2809 split_count++;
2811 if (split_count == 0)
2812 return;
2813 if (split_count % struct_members != 0) {
2814 printf("error parsing '%s' split_count=%d\n", filename, split_count);
2815 split_count = 0;
2816 return;
2818 split_count /= struct_members;
2819 forced_splits = malloc(split_count * sizeof(void *));
2821 p = buf;
2822 i = 0;
2823 while (*p) {
2824 struct split_data *split = malloc(sizeof(*split));
2826 split->func = alloc_string(get_next_string(&p));
2827 split->rl = alloc_string(get_next_string(&p));
2828 forced_splits[i++] = split;
2832 void register_definition_db_callbacks(int id)
2834 my_id = id;
2836 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2837 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2838 add_split_return_callback(match_return_info);
2839 add_split_return_callback(print_returned_struct_members);
2840 add_split_return_callback(print_return_info);
2841 add_hook(&call_return_state_hooks, RETURN_HOOK);
2842 add_hook(&match_end_func_info, END_FUNC_HOOK);
2843 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2845 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2846 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2847 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2849 common_funcs = load_strings_from_file(option_project_str, "common_functions");
2850 register_return_deletes();
2851 register_return_replacements();
2852 register_forced_return_splits();
2854 add_hook(&dump_cache, END_FILE_HOOK);
2857 void register_db_call_marker(int id)
2859 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2862 char *get_data_info_name(struct expression *expr)
2864 struct symbol *sym;
2865 char *name;
2866 char buf[256];
2867 char *ret = NULL;
2869 expr = strip_expr(expr);
2870 name = get_member_name(expr);
2871 if (name)
2872 return name;
2873 name = expr_to_var_sym(expr, &sym);
2874 if (!name || !sym)
2875 goto free;
2876 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2877 goto free;
2878 if (sym->ctype.modifiers & MOD_STATIC)
2879 snprintf(buf, sizeof(buf), "static %s", name);
2880 else
2881 snprintf(buf, sizeof(buf), "global %s", name);
2882 ret = alloc_sname(buf);
2883 free:
2884 free_string(name);
2885 return ret;