rosenberg: handle bit fields better
[smatch.git] / smatch_db.c
blobc1da7b32ccbc4af3d3858baa1ceb92d85c814e62
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 DECLARE_PTR_LIST(delete_list, delete_hook);
104 static struct delete_list *delete_hooks;
106 struct split_data {
107 const char *func, *rl;
109 static struct split_data **forced_splits;
110 static int split_count;
112 /* silently truncates if needed. */
113 char *escape_newlines(const char *str)
115 char buf[1024] = "";
116 bool found = false;
117 int i, j;
119 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
120 if (str[i] != '\r' && str[i] != '\n') {
121 buf[j] = str[i];
122 continue;
125 found = true;
126 buf[j++] = '\\';
127 if (j == sizeof(buf))
128 break;
129 buf[j] = 'n';
132 if (!found)
133 return alloc_sname(str);
135 if (j == sizeof(buf))
136 buf[j - 1] = '\0';
137 return alloc_sname(buf);
140 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
142 int i;
144 for (i = 0; i < argc; i++) {
145 if (i != 0)
146 sm_printf(", ");
147 sm_printf("%s", argv[i]);
149 sm_printf("\n");
150 return 0;
153 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
155 char *err = NULL;
156 int rc;
158 if (!db)
159 return;
161 if (option_debug || debug_db) {
162 sm_msg("%s", sql);
163 if (strncasecmp(sql, "select", strlen("select")) == 0)
164 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
167 rc = sqlite3_exec(db, sql, callback, data, &err);
168 if (rc != SQLITE_OK && !parse_error) {
169 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
170 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
171 parse_error = 1;
175 static int replace_count;
176 static char **replace_table;
177 static const char *replace_return_ranges(const char *return_ranges)
179 int i;
181 if (!get_function()) {
182 /* I have no idea why EXPORT_SYMBOL() is here */
183 return return_ranges;
185 for (i = 0; i < replace_count; i += 3) {
186 if (strcmp(replace_table[i + 0], get_function()) == 0) {
187 if (strcmp(replace_table[i + 1], return_ranges) == 0)
188 return replace_table[i + 2];
191 return return_ranges;
194 static int delete_count;
195 static char **delete_table;
196 static bool is_delete_return(const char *return_ranges)
198 int i;
200 if (!get_function())
201 return false;
203 for (i = 0; i < delete_count; i += 2) {
204 if (strcmp(delete_table[i], get_function()) == 0 &&
205 strcmp(delete_table[i + 1], return_ranges) == 0)
206 return true;
209 return false;
212 void add_delete_return_hook(delete_hook *hook)
214 add_ptr_list(&delete_hooks, hook);
217 static bool is_project_delete_return(struct expression *expr)
219 delete_hook *hook;
221 FOR_EACH_PTR(delete_hooks, hook) {
222 if (hook(expr))
223 return true;
224 } END_FOR_EACH_PTR(hook);
225 return false;
228 static char *use_states;
229 static int get_db_state_count(void)
231 struct sm_state *sm;
232 int count = 0;
234 FOR_EACH_SM(__get_cur_stree(), sm) {
235 if (sm->owner == USHRT_MAX)
236 continue;
237 if (use_states[sm->owner])
238 count++;
239 } END_FOR_EACH_SM(sm);
240 return count;
243 static bool in_base_file(struct symbol *sym)
245 return sym->pos.stream == base_file_stream;
248 static bool is_local(struct symbol *sym)
250 if (sym->ctype.modifiers & MOD_STATIC)
251 return true;
252 if ((sym->ctype.modifiers & MOD_EXTERN) &&
253 (sym->ctype.modifiers & MOD_INLINE) &&
254 !in_base_file(sym))
255 return true;
257 if (!sym->definition)
258 return false;
260 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
261 (sym->definition->ctype.modifiers & MOD_INLINE) &&
262 !in_base_file(sym->definition))
263 return true;
265 return false;
268 void db_ignore_states(int id)
270 use_states[id] = 0;
273 unsigned long long __fn_mtag;
274 static void set_fn_mtag(struct symbol *sym)
276 char buf[128];
278 if (is_local(cur_func_sym))
279 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
280 else
281 snprintf(buf, sizeof(buf), "extern %s", get_function());
283 __fn_mtag = str_to_mtag(buf);
286 void sql_insert_return_states(int return_id, const char *return_ranges,
287 int type, int param, const char *key, const char *value)
289 unsigned long long id;
292 if (key && strlen(key) >= 80)
293 return;
294 if (__inline_fn)
295 id = (unsigned long)__inline_fn;
296 else
297 id = __fn_mtag;
299 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
300 get_base_file(), get_function(), id, return_id,
301 return_ranges, is_local(cur_func_sym), type, param, key, value);
304 static struct string_list *common_funcs;
305 static int is_common_function(const char *fn)
307 char *tmp;
309 if (!fn)
310 return 0;
312 if (strncmp(fn, "__builtin_", 10) == 0)
313 return 1;
315 FOR_EACH_PTR(common_funcs, tmp) {
316 if (strcmp(tmp, fn) == 0)
317 return 1;
318 } END_FOR_EACH_PTR(tmp);
320 return 0;
323 static char *function_signature(void)
325 return type_to_str(get_real_base_type(cur_func_sym));
328 void sql_insert_caller_info(struct expression *call, int type,
329 int param, const char *key, const char *value)
331 FILE *tmp_fd = sm_outfd;
332 char *fn;
334 if (!option_info && !__inline_call)
335 return;
336 if (unreachable())
337 return;
339 if (key && strlen(key) >= 80)
340 return;
342 fn = get_fnptr_name(call->fn);
343 if (!fn)
344 return;
346 if (__inline_call) {
347 mem_sql(NULL, NULL,
348 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
349 get_base_file(), get_function(), fn, (unsigned long)call,
350 is_static(call->fn), type, param, key, value);
353 if (!option_info)
354 return;
356 if (strncmp(fn, "__builtin_", 10) == 0)
357 return;
358 if (type != INTERNAL && is_common_function(fn))
359 return;
361 sm_outfd = caller_info_fd;
362 sm_msg("SQL_caller_info: insert into caller_info values ("
363 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
364 get_base_file(), get_function(), fn, is_static(call->fn),
365 type, param, key, value);
366 sm_outfd = tmp_fd;
368 free_string(fn);
371 void sql_insert_function_ptr(const char *fn, const char *struct_name)
373 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
374 get_base_file(), fn, struct_name);
377 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
379 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
380 get_base_file(), get_function(), (unsigned long)__inline_fn,
381 fn_static(), type, param, key, value);
384 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
386 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
387 get_base_file(), get_function(), (unsigned long)__inline_fn,
388 fn_static(), type, param, key, value);
391 void sql_insert_function_type_size(const char *member, const char *ranges)
393 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
396 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
398 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
401 void sql_insert_type_info(int type, const char *member, const char *value)
403 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
406 void sql_insert_local_values(const char *name, const char *value)
408 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
411 void sql_insert_function_type_value(const char *type, const char *value)
413 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
416 void sql_insert_function_type(int param, const char *value)
418 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
419 get_base_file(), get_function(), fn_static(), param, value);
422 void sql_insert_parameter_name(int param, const char *value)
424 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
425 get_base_file(), get_function(), fn_static(), param, value);
428 void sql_insert_data_info(struct expression *data, int type, const char *value)
430 char *data_name;
432 data_name = get_data_info_name(data);
433 if (!data_name)
434 return;
435 sql_insert(data_info, "'%s', '%s', %d, '%s'",
436 is_static(data) ? get_base_file() : "extern",
437 data_name, type, value);
440 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
442 sql_insert(data_info, "'%s', '%s', %d, '%s'",
443 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
444 var, type, value);
447 void sql_save_constraint(const char *con)
449 if (!option_info)
450 return;
452 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
455 void sql_save_constraint_required(const char *data, int op, const char *limit)
457 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
460 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
462 if (!option_info)
463 return;
465 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
466 "select constraints_required.data, constraints_required.op, '%s' from "
467 "constraints_required where bound = '%s';", new_limit, old_limit);
470 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
472 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
475 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
477 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
478 return;
480 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
481 is_local(fn->symbol) ? get_base_file() : "extern",
482 fn->symbol->ident->name,
483 is_local(fn->symbol),
484 type, param, key, value);
487 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
489 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
490 tag, get_filename(), get_function(), get_lineno(),
491 left_name, right_name);
494 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
496 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
499 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
501 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
504 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
506 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
509 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
511 mtag_t *saved_tag = _tag;
512 mtag_t new_tag;
514 new_tag = strtoll(argv[0], NULL, 10);
516 if (!*saved_tag)
517 *saved_tag = new_tag;
518 else if (*saved_tag != new_tag)
519 *saved_tag = -1ULL;
521 return 0;
524 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
526 mtag_t tmp = 0;
528 run_sql(save_mtag, &tmp,
529 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
530 tag, container_offset);
532 if (tmp == 0 || tmp == -1ULL)
533 return 0;
534 *container = tmp;
535 return 1;
538 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
540 mtag_t tmp = 0;
542 run_sql(save_mtag, &tmp,
543 "select tag from mtag_map where container = %lld and container_offset = %d;",
544 container, offset);
546 if (tmp == 0 || tmp == -1ULL)
547 return 0;
548 *tag = tmp;
549 return 1;
552 char *get_static_filter(struct symbol *sym)
554 static char sql_filter[1024];
556 /* This can only happen on buggy code. Return invalid SQL. */
557 if (!sym) {
558 sql_filter[0] = '\0';
559 return sql_filter;
562 if (is_local(sym)) {
563 snprintf(sql_filter, sizeof(sql_filter),
564 "file = '%s' and function = '%s' and static = '1'",
565 get_base_file(), sym->ident->name);
566 } else {
567 snprintf(sql_filter, sizeof(sql_filter),
568 "function = '%s' and static = '0'", sym->ident->name);
571 return sql_filter;
574 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
576 int *row_count = _row_count;
578 *row_count = 0;
579 if (argc != 1)
580 return 0;
581 *row_count = atoi(argv[0]);
582 return 0;
585 static void mark_call_params_untracked(struct expression *call)
587 struct expression *arg;
588 int i = 0;
590 FOR_EACH_PTR(call->args, arg) {
591 mark_untracked(call, i++, "$", NULL);
592 } END_FOR_EACH_PTR(arg);
595 static void sql_select_return_states_pointer(const char *cols,
596 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
598 char *ptr;
599 int return_count = 0;
601 ptr = get_fnptr_name(call->fn);
602 if (!ptr)
603 return;
605 run_sql(get_row_count, &return_count,
606 "select count(*) from return_states join function_ptr "
607 "where return_states.function == function_ptr.function and "
608 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
609 /* The magic number 100 is just from testing on the kernel. */
610 if (return_count > 100) {
611 mark_call_params_untracked(call);
612 return;
615 run_sql(callback, info,
616 "select %s from return_states join function_ptr where "
617 "return_states.function == function_ptr.function and ptr = '%s' "
618 "and searchable = 1 "
619 "order by function_ptr.file, return_states.file, return_id, type;",
620 cols, ptr);
623 static int is_local_symbol(struct expression *expr)
625 if (expr->type != EXPR_SYMBOL)
626 return 0;
627 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
628 return 0;
629 return 1;
632 bool is_fn_ptr(struct expression *fn)
634 fn = strip_expr(fn);
635 if (fn->type != EXPR_SYMBOL)
636 return true;
637 if (!fn->symbol)
638 return true;
639 if (is_local_symbol(fn))
640 return true;
641 return false;
644 void sql_select_return_states(const char *cols, struct expression *call,
645 int (*callback)(void*, int, char**, char**), void *info)
647 struct expression *fn;
648 int row_count = 0;
650 if (is_fake_call(call))
651 return;
653 fn = strip_expr(call->fn);
654 if (is_fn_ptr(fn)) {
655 sql_select_return_states_pointer(cols, call, callback, info);
656 return;
659 if (inlinable(fn)) {
660 mem_sql(callback, info,
661 "select %s from return_states where call_id = '%lu' order by return_id, type;",
662 cols, (unsigned long)call);
663 return;
666 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
667 get_static_filter(fn->symbol));
668 if (row_count == 0 && fn->symbol && fn->symbol->definition)
669 set_state(my_id, "db_incomplete", NULL, &incomplete);
670 if (row_count > 3000)
671 return;
673 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
674 cols, get_static_filter(fn->symbol));
677 bool db_incomplete(void)
679 return !!get_state(my_id, "db_incomplete", NULL);
682 #define CALL_IMPLIES 0
683 #define RETURN_IMPLIES 1
685 struct implies_info {
686 int type;
687 struct db_implies_cb_list *cb_list;
688 struct expression *expr;
689 struct symbol *sym;
692 void sql_select_implies(const char *cols, struct implies_info *info,
693 int (*callback)(void*, int, char**, char**))
695 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
696 mem_sql(callback, info,
697 "select %s from return_implies where call_id = '%lu';",
698 cols, (unsigned long)info->expr);
699 return;
702 run_sql(callback, info, "select %s from %s_implies where %s;",
703 cols,
704 info->type == CALL_IMPLIES ? "call" : "return",
705 get_static_filter(info->sym));
708 struct select_caller_info_data {
709 struct stree *final_states;
710 struct timeval start_time;
711 int prev_func_id;
712 int ignore;
713 int results;
716 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
718 static void sql_select_caller_info(struct select_caller_info_data *data,
719 const char *cols, struct symbol *sym)
721 if (__inline_fn) {
722 mem_sql(caller_info_callback, data,
723 "select %s from caller_info where call_id = %lu;",
724 cols, (unsigned long)__inline_fn);
725 return;
728 if (sym->ident->name && is_common_function(sym->ident->name))
729 return;
730 run_sql(caller_info_callback, data,
731 "select %s from common_caller_info where %s order by call_id;",
732 cols, get_static_filter(sym));
733 if (data->results)
734 return;
736 run_sql(caller_info_callback, data,
737 "select %s from caller_info where %s order by call_id;",
738 cols, get_static_filter(sym));
741 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
743 struct def_callback *def_callback = __alloc_def_callback(0);
745 def_callback->hook_type = type;
746 def_callback->callback = callback;
747 add_ptr_list(&select_caller_info_callbacks, def_callback);
750 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
752 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
754 callback->hook_type = type;
755 callback->callback = fn;
756 add_ptr_list(&select_caller_name_sym_callbacks, callback);
760 * These call backs are used when the --info option is turned on to print struct
761 * member information. For example foo->bar could have a state in
762 * smatch_extra.c and also check_user.c.
764 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
766 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
768 member_callback->owner = owner;
769 member_callback->callback = callback;
770 add_ptr_list(&member_callbacks, member_callback);
773 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
775 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
777 member_callback->owner = owner;
778 member_callback->callback = callback;
779 add_ptr_list(&member_callbacks_new, member_callback);
782 void add_return_info_callback(int owner,
783 void (*callback)(int return_id, char *return_ranges,
784 struct expression *returned_expr,
785 int param,
786 const char *printed_name,
787 struct sm_state *sm))
789 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
791 return_callback->owner = owner;
792 return_callback->callback = callback;
793 add_ptr_list(&return_callbacks, return_callback);
796 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
798 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
800 callback->callback = fn;
801 add_ptr_list(&returned_state_callbacks, callback);
804 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))
806 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
808 member_callback->owner = owner;
809 member_callback->callback = callback;
810 add_ptr_list(&returned_member_callbacks, member_callback);
813 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
815 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
817 cb->type = type;
818 cb->callback = callback;
819 add_ptr_list(&call_implies_cb_list, cb);
822 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
824 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
826 cb->type = type;
827 cb->callback = callback;
828 add_ptr_list(&return_implies_cb_list, cb);
831 struct return_info {
832 struct expression *static_returns_call;
833 struct symbol *return_type;
834 struct range_list *return_range_list;
837 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
839 struct return_info *ret_info = _ret_info;
840 struct range_list *rl;
841 struct expression *call_expr = ret_info->static_returns_call;
843 if (argc != 1)
844 return 0;
845 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
846 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
847 return 0;
850 struct range_list *db_return_vals(struct expression *expr)
852 struct return_info ret_info = {};
853 struct sm_state *sm;
855 if (is_fake_call(expr))
856 return NULL;
858 sm = get_extra_sm_state(expr);
859 if (sm)
860 return clone_rl(estate_rl(sm->state));
861 ret_info.static_returns_call = expr;
862 ret_info.return_type = get_type(expr);
863 if (!ret_info.return_type)
864 return NULL;
866 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
867 return NULL;
869 ret_info.return_range_list = NULL;
870 if (inlinable(expr->fn)) {
871 mem_sql(db_return_callback, &ret_info,
872 "select distinct return from return_states where call_id = '%lu';",
873 (unsigned long)expr);
874 } else {
875 run_sql(db_return_callback, &ret_info,
876 "select distinct return from return_states where %s;",
877 get_static_filter(expr->fn->symbol));
879 return ret_info.return_range_list;
882 struct range_list *db_return_vals_from_str(const char *fn_name)
884 struct return_info ret_info;
886 ret_info.static_returns_call = NULL;
887 ret_info.return_type = &llong_ctype;
888 ret_info.return_range_list = NULL;
890 run_sql(db_return_callback, &ret_info,
891 "select distinct return from return_states where function = '%s';",
892 fn_name);
893 return ret_info.return_range_list;
897 * This is used when we have a function that takes a function pointer as a
898 * parameter. "frob(blah, blah, my_function);" We know that the return values
899 * from frob() come from my_funcion() so we want to find the possible returns
900 * of my_function(), but we don't know which arguments are passed to it.
903 struct range_list *db_return_vals_no_args(struct expression *expr)
905 struct return_info ret_info = {};
907 if (!expr || expr->type != EXPR_SYMBOL)
908 return NULL;
910 ret_info.static_returns_call = expr;
911 ret_info.return_type = get_type(expr);
912 ret_info.return_type = get_real_base_type(ret_info.return_type);
913 if (!ret_info.return_type)
914 return NULL;
916 run_sql(db_return_callback, &ret_info,
917 "select distinct return from return_states where %s;",
918 get_static_filter(expr->symbol));
920 return ret_info.return_range_list;
923 static void match_call_marker(struct expression *expr)
925 struct symbol *type;
927 type = get_type(expr->fn);
928 if (type && type->type == SYM_PTR)
929 type = get_real_base_type(type);
932 * we just want to record something in the database so that if we have
933 * two calls like: frob(4); frob(some_unkown); then on the receiving
934 * side we know that sometimes frob is called with unknown parameters.
937 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
940 int is_recursive_member(const char *name)
942 char buf[256];
943 const char *p, *next;
944 int size;
946 p = strchr(name, '>');
947 if (!p)
948 return 0;
949 p++;
950 while (true) {
951 next = strchr(p, '>');
952 if (!next)
953 return 0;
954 next++;
956 size = next - p;
957 if (size >= sizeof(buf))
958 return 0;
959 memcpy(buf, p, size);
960 buf[size] = '\0';
961 if (strstr(next, buf))
962 return 1;
963 p = next;
967 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
969 struct symbol *sym;
970 const char *sm_name;
971 char *name;
972 bool is_address = false;
973 bool add_star = false;
974 char buf[256];
975 char *ret = NULL;
976 int len;
978 expr = strip_expr(expr);
979 if (!expr)
980 return NULL;
982 if (expr->type == EXPR_PREOP && expr->op == '&') {
983 expr = strip_expr(expr->unop);
984 is_address = true;
987 name = expr_to_var_sym(expr, &sym);
988 if (!name || !sym)
989 goto free;
990 if (sym != sm->sym)
991 goto free;
993 sm_name = sm->name;
994 add_star = false;
995 if (sm_name[0] == '*') {
996 add_star = true;
997 sm_name++;
1000 len = strlen(name);
1001 if (strncmp(name, sm_name, len) != 0)
1002 goto free;
1003 if (sm_name[len] == '\0') {
1004 snprintf(buf, sizeof(buf), "%s%s$",
1005 add_star ? "*" : "", is_address ? "*" : "");
1006 } else {
1007 if (sm_name[len] != '.' && sm_name[len] != '-')
1008 goto free;
1009 if (sm_name[len] == '-')
1010 len++;
1011 // FIXME does is_address really imply that sm_name[len] == '-'
1012 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
1013 sm_name + len);
1016 ret = alloc_sname(buf);
1017 free:
1018 free_string(name);
1019 return ret;
1022 static void print_struct_members(struct expression *call, struct expression *expr, int param,
1023 int owner,
1024 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
1025 bool new)
1027 struct sm_state *sm;
1028 const char *sm_name;
1029 char *name;
1030 struct symbol *sym;
1031 int len;
1032 char printed_name[256];
1033 int is_address = 0;
1034 bool add_star;
1035 struct symbol *type;
1037 expr = strip_expr(expr);
1038 if (!expr)
1039 return;
1040 type = get_type(expr);
1041 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1042 return;
1044 if (expr->type == EXPR_PREOP && expr->op == '&') {
1045 expr = strip_expr(expr->unop);
1046 is_address = 1;
1049 name = expr_to_var_sym(expr, &sym);
1050 if (!name || !sym)
1051 goto free;
1053 len = strlen(name);
1054 FOR_EACH_SM(__get_cur_stree(), sm) {
1055 if (sm->owner != owner || sm->sym != sym)
1056 continue;
1057 sm_name = sm->name;
1058 add_star = false;
1059 if (sm_name[0] == '*') {
1060 add_star = true;
1061 sm_name++;
1063 // FIXME: simplify?
1064 if (!add_star && strcmp(name, sm_name) == 0) {
1065 if (is_address) {
1066 snprintf(printed_name, sizeof(printed_name), "*$");
1067 } else {
1068 if (new)
1069 snprintf(printed_name, sizeof(printed_name), "$");
1070 else
1071 continue;
1073 } else if (add_star && strcmp(name, sm_name) == 0) {
1074 snprintf(printed_name, sizeof(printed_name), "%s*$",
1075 is_address ? "*" : "");
1076 } else if (strncmp(name, sm_name, len) == 0) {
1077 if (sm_name[len] != '.' && sm_name[len] != '-')
1078 continue;
1079 if (is_address && sm_name[len] == '.') {
1080 snprintf(printed_name, sizeof(printed_name),
1081 "%s$->%s", add_star ? "*" : "",
1082 sm_name + len + 1);
1083 } else if (is_address && sm_name[len] == '-') {
1084 snprintf(printed_name, sizeof(printed_name),
1085 "%s(*$)%s", add_star ? "*" : "",
1086 sm_name + len);
1087 } else {
1088 snprintf(printed_name, sizeof(printed_name),
1089 "%s$%s", add_star ? "*" : "",
1090 sm_name + len);
1092 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1093 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1094 continue;
1095 if (is_address && sm_name[len + 1] == '.') {
1096 snprintf(printed_name, sizeof(printed_name),
1097 "&%s$->%s", add_star ? "*" : "",
1098 sm_name + len + 2);
1099 } else if (is_address && sm_name[len] == '-') {
1100 snprintf(printed_name, sizeof(printed_name),
1101 "&%s(*$)%s", add_star ? "*" : "",
1102 sm_name + len + 1);
1103 } else {
1104 snprintf(printed_name, sizeof(printed_name),
1105 "&%s$%s", add_star ? "*" : "",
1106 sm_name + len + 1);
1108 } else {
1109 continue;
1111 if (is_recursive_member(printed_name))
1112 continue;
1113 callback(call, param, printed_name, sm);
1114 } END_FOR_EACH_SM(sm);
1115 free:
1116 free_string(name);
1119 static void match_call_info(struct expression *call)
1121 struct member_info_callback *cb;
1122 struct expression *arg;
1123 int i;
1125 FOR_EACH_PTR(member_callbacks, cb) {
1126 i = -1;
1127 FOR_EACH_PTR(call->args, arg) {
1128 i++;
1129 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1130 } END_FOR_EACH_PTR(arg);
1131 } END_FOR_EACH_PTR(cb);
1134 static struct expression *get_fake_variable(struct expression *expr)
1136 struct expression *tmp;
1138 tmp = expr_get_fake_parent_expr(expr);
1139 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1140 return NULL;
1142 return tmp->left;
1145 static void match_call_info_new(struct expression *call)
1147 struct member_info_callback *cb;
1148 struct expression *arg, *tmp;
1149 int i;
1151 if (!option_info && !__inline_call && !local_debug)
1152 return;
1154 FOR_EACH_PTR(member_callbacks_new, cb) {
1155 i = -1;
1156 FOR_EACH_PTR(call->args, arg) {
1157 i++;
1158 tmp = get_fake_variable(arg);
1159 if (!tmp)
1160 tmp = arg;
1161 __ignore_param_used++;
1162 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1163 __ignore_param_used--;
1164 } END_FOR_EACH_PTR(arg);
1165 } END_FOR_EACH_PTR(cb);
1168 static int get_param(int param, char **name, struct symbol **sym)
1170 struct symbol *arg;
1171 int i;
1173 i = 0;
1174 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1176 * this is a temporary hack to work around a bug (I think in sparse?)
1177 * 2.6.37-rc1:fs/reiserfs/journal.o
1178 * If there is a function definition without parameter name found
1179 * after a function implementation then it causes a crash.
1180 * int foo() {}
1181 * int bar(char *);
1183 if (arg->ident->name < (char *)100)
1184 continue;
1185 if (i == param) {
1186 *name = arg->ident->name;
1187 *sym = arg;
1188 return TRUE;
1190 i++;
1191 } END_FOR_EACH_PTR(arg);
1193 return FALSE;
1196 static int function_signature_matches(const char *sig)
1198 char *my_sig;
1200 my_sig = function_signature();
1201 if (!sig || !my_sig)
1202 return 1; /* default to matching */
1203 if (strcmp(my_sig, sig) == 0)
1204 return 1;
1205 return 0;
1208 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1210 struct select_caller_info_data *data = _data;
1211 int func_id;
1212 long type;
1213 long param;
1214 char *key;
1215 char *value;
1216 char *name = NULL;
1217 struct symbol *sym = NULL;
1218 struct def_callback *def_callback;
1219 struct def_name_sym_callback *ns_callback;
1220 struct stree *stree;
1221 struct timeval cur_time;
1222 char fullname[256];
1223 char *p;
1225 data->results = 1;
1227 if (argc != 5)
1228 return 0;
1230 gettimeofday(&cur_time, NULL);
1231 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1232 return 0;
1234 func_id = atoi(argv[0]);
1235 errno = 0;
1236 type = strtol(argv[1], NULL, 10);
1237 param = strtol(argv[2], NULL, 10);
1238 if (errno)
1239 return 0;
1240 key = argv[3];
1241 value = argv[4];
1243 if (data->prev_func_id == -1)
1244 data->prev_func_id = func_id;
1245 if (func_id != data->prev_func_id) {
1246 stree = __pop_fake_cur_stree();
1247 if (!data->ignore)
1248 merge_stree(&data->final_states, stree);
1249 free_stree(&stree);
1250 __push_fake_cur_stree();
1251 __unnullify_path();
1252 data->prev_func_id = func_id;
1253 data->ignore = 0;
1256 if (data->ignore)
1257 return 0;
1258 if (type == INTERNAL &&
1259 !function_signature_matches(value)) {
1260 data->ignore = 1;
1261 return 0;
1264 if (param >= 0 && !get_param(param, &name, &sym))
1265 return 0;
1267 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1268 if (def_callback->hook_type == type)
1269 def_callback->callback(name, sym, key, value);
1270 } END_FOR_EACH_PTR(def_callback);
1272 p = strchr(key, '$');
1273 if (name && p)
1274 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1275 else
1276 snprintf(fullname, sizeof(fullname), "%s", key);
1278 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1279 if (ns_callback->hook_type == type)
1280 ns_callback->callback(fullname, sym, value);
1281 } END_FOR_EACH_PTR(ns_callback);
1283 return 0;
1286 static struct string_list *ptr_names_done;
1287 static struct string_list *ptr_names;
1289 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1291 insert_string(&ptr_names, alloc_string(argv[0]));
1292 return 0;
1295 static char *get_next_ptr_name(void)
1297 char *ptr;
1299 FOR_EACH_PTR(ptr_names, ptr) {
1300 if (!insert_string(&ptr_names_done, ptr))
1301 continue;
1302 return ptr;
1303 } END_FOR_EACH_PTR(ptr);
1304 return NULL;
1307 static void get_ptr_names(const char *file, const char *name)
1309 char sql_filter[1024];
1310 int before, after;
1312 if (file) {
1313 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1314 file, name);
1315 } else {
1316 snprintf(sql_filter, 1024, "function = '%s';", name);
1319 before = ptr_list_size((struct ptr_list *)ptr_names);
1321 run_sql(get_ptr_name, NULL,
1322 "select distinct ptr from function_ptr where %s",
1323 sql_filter);
1325 after = ptr_list_size((struct ptr_list *)ptr_names);
1326 if (before == after)
1327 return;
1329 while ((name = get_next_ptr_name()))
1330 get_ptr_names(NULL, name);
1333 static void match_data_from_db(struct symbol *sym)
1335 struct select_caller_info_data data = { .prev_func_id = -1 };
1336 struct sm_state *sm;
1337 struct stree *stree;
1338 struct timeval end_time;
1340 if (!sym || !sym->ident)
1341 return;
1343 set_fn_mtag(sym);
1344 gettimeofday(&data.start_time, NULL);
1346 __push_fake_cur_stree();
1347 __unnullify_path();
1349 if (!__inline_fn) {
1350 char *ptr;
1352 if (sym->ctype.modifiers & MOD_STATIC)
1353 get_ptr_names(get_base_file(), sym->ident->name);
1354 else
1355 get_ptr_names(NULL, sym->ident->name);
1357 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1358 __free_ptr_list((struct ptr_list **)&ptr_names);
1359 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1360 __free_fake_cur_stree();
1361 return;
1364 sql_select_caller_info(&data,
1365 "call_id, type, parameter, key, value",
1366 sym);
1369 stree = __pop_fake_cur_stree();
1370 if (!data.ignore)
1371 merge_stree(&data.final_states, stree);
1372 free_stree(&stree);
1373 __push_fake_cur_stree();
1374 __unnullify_path();
1375 data.prev_func_id = -1;
1376 data.ignore = 0;
1377 data.results = 0;
1379 FOR_EACH_PTR(ptr_names, ptr) {
1380 run_sql(caller_info_callback, &data,
1381 "select call_id, type, parameter, key, value"
1382 " from common_caller_info where function = '%s' order by call_id",
1383 ptr);
1384 } END_FOR_EACH_PTR(ptr);
1386 if (data.results) {
1387 FOR_EACH_PTR(ptr_names, ptr) {
1388 free_string(ptr);
1389 } END_FOR_EACH_PTR(ptr);
1390 goto free_ptr_names;
1393 FOR_EACH_PTR(ptr_names, ptr) {
1394 run_sql(caller_info_callback, &data,
1395 "select call_id, type, parameter, key, value"
1396 " from caller_info where function = '%s' order by call_id",
1397 ptr);
1398 free_string(ptr);
1399 } END_FOR_EACH_PTR(ptr);
1401 free_ptr_names:
1402 __free_ptr_list((struct ptr_list **)&ptr_names);
1403 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1404 } else {
1405 sql_select_caller_info(&data,
1406 "call_id, type, parameter, key, value",
1407 sym);
1410 stree = __pop_fake_cur_stree();
1411 if (!data.ignore)
1412 merge_stree(&data.final_states, stree);
1413 free_stree(&stree);
1415 gettimeofday(&end_time, NULL);
1416 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1417 FOR_EACH_SM(data.final_states, sm) {
1418 __set_sm(sm);
1419 } END_FOR_EACH_SM(sm);
1422 free_stree(&data.final_states);
1425 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1427 struct implies_info *info = _info;
1428 struct db_implies_callback *cb;
1429 struct expression *arg = NULL;
1430 int type;
1431 int param;
1433 if (argc != 5)
1434 return 0;
1436 type = atoi(argv[1]);
1437 param = atoi(argv[2]);
1439 FOR_EACH_PTR(info->cb_list, cb) {
1440 if (cb->type != type)
1441 continue;
1442 if (param != -1) {
1443 arg = get_argument_from_call_expr(info->expr->args, param);
1444 if (!arg)
1445 continue;
1447 cb->callback(info->expr, arg, argv[3], argv[4]);
1448 } END_FOR_EACH_PTR(cb);
1450 return 0;
1453 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1455 struct implies_info *info = _info;
1456 struct db_implies_callback *cb;
1457 struct expression *arg;
1458 struct symbol *sym;
1459 char *name;
1460 int type;
1461 int param;
1463 if (argc != 5)
1464 return 0;
1466 type = atoi(argv[1]);
1467 param = atoi(argv[2]);
1469 if (!get_param(param, &name, &sym))
1470 return 0;
1471 arg = symbol_expression(sym);
1472 if (!arg)
1473 return 0;
1475 FOR_EACH_PTR(info->cb_list, cb) {
1476 if (cb->type != type)
1477 continue;
1478 cb->callback(info->expr, arg, argv[3], argv[4]);
1479 } END_FOR_EACH_PTR(cb);
1481 return 0;
1484 static void match_return_implies(struct expression *expr)
1486 struct implies_info info = {
1487 .type = RETURN_IMPLIES,
1488 .cb_list = return_implies_cb_list,
1491 if (expr->fn->type != EXPR_SYMBOL ||
1492 !expr->fn->symbol)
1493 return;
1494 info.expr = expr;
1495 info.sym = expr->fn->symbol;
1496 sql_select_implies("function, type, parameter, key, value", &info,
1497 return_implies_callbacks);
1500 static void match_call_implies(struct symbol *sym)
1502 struct implies_info info = {
1503 .type = CALL_IMPLIES,
1504 .cb_list = call_implies_cb_list,
1507 if (!sym || !sym->ident)
1508 return;
1510 info.sym = sym;
1511 sql_select_implies("function, type, parameter, key, value", &info,
1512 call_implies_callbacks);
1515 static char *get_fn_param_str(struct expression *expr)
1517 struct expression *tmp;
1518 int param;
1519 char buf[32];
1521 tmp = get_assigned_expr(expr);
1522 if (tmp)
1523 expr = tmp;
1524 expr = strip_expr(expr);
1525 if (!expr || expr->type != EXPR_CALL)
1526 return NULL;
1527 expr = strip_expr(expr->fn);
1528 if (!expr || expr->type != EXPR_SYMBOL)
1529 return NULL;
1530 param = get_param_num(expr);
1531 if (param < 0)
1532 return NULL;
1534 snprintf(buf, sizeof(buf), "[r $%d]", param);
1535 return alloc_sname(buf);
1538 static char *get_return_compare_is_param(struct expression *expr)
1540 char *var;
1541 char buf[256];
1542 int comparison;
1543 int param;
1545 param = get_param_num(expr);
1546 if (param < 0)
1547 return NULL;
1549 var = expr_to_var(expr);
1550 if (!var)
1551 return NULL;
1552 snprintf(buf, sizeof(buf), "%s orig", var);
1553 comparison = get_comparison_strings(var, buf);
1554 free_string(var);
1556 if (!comparison)
1557 return NULL;
1559 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1560 return alloc_sname(buf);
1563 static char *get_return_compare_str(struct expression *expr)
1565 char *compare_str;
1567 compare_str = get_return_compare_is_param(expr);
1568 if (compare_str)
1569 return compare_str;
1571 compare_str = expr_lte_to_param(expr, -1);
1572 if (compare_str)
1573 return compare_str;
1575 return expr_param_comparison(expr, -1);
1578 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1580 struct range_list *rl;
1581 char *return_ranges;
1582 sval_t sval;
1583 char *fn_param_str;
1584 char *compare_str;
1585 char *math_str;
1586 char buf[128];
1588 *rl_p = NULL;
1590 if (!expr)
1591 return alloc_sname("");
1593 if (get_implied_value(expr, &sval)) {
1594 sval = sval_cast(cur_func_return_type(), sval);
1595 *rl_p = alloc_rl(sval, sval);
1596 return sval_to_str_or_err_ptr(sval);
1599 fn_param_str = get_fn_param_str(expr);
1600 compare_str = expr_equal_to_param(expr, -1);
1601 math_str = get_value_in_terms_of_parameter_math(expr);
1603 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1604 rl = cast_rl(cur_func_return_type(), rl);
1605 return_ranges = show_rl(rl);
1606 } else if (get_imaginary_absolute(expr, &rl)){
1607 rl = cast_rl(cur_func_return_type(), rl);
1608 return alloc_sname(show_rl(rl));
1609 } else {
1610 get_absolute_rl(expr, &rl);
1611 rl = cast_rl(cur_func_return_type(), rl);
1612 return_ranges = show_rl(rl);
1614 *rl_p = rl;
1616 if (fn_param_str) {
1617 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1618 return alloc_sname(buf);
1620 if (compare_str) {
1621 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1622 return alloc_sname(buf);
1624 if (math_str) {
1625 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1626 return alloc_sname(buf);
1628 compare_str = get_return_compare_str(expr);
1629 if (compare_str) {
1630 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1631 return alloc_sname(buf);
1634 return return_ranges;
1637 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1639 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1642 static bool call_return_state_hooks_conditional(struct expression *expr)
1644 int final_pass_orig = final_pass;
1645 static int recurse;
1647 if (recurse >= 2)
1648 return false;
1649 if (!expr ||
1650 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1651 return false;
1653 recurse++;
1655 __push_fake_cur_stree();
1657 final_pass = 0;
1658 __split_whole_condition(expr->conditional);
1659 final_pass = final_pass_orig;
1661 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1663 __push_true_states();
1664 __use_false_states();
1666 call_return_state_hooks(expr->cond_false);
1668 __merge_true_states();
1669 __free_fake_cur_stree();
1671 recurse--;
1672 return true;
1675 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1677 struct split_data *data = NULL;
1678 struct expression *compare;
1679 struct range_list *rl;
1680 char buf[64];
1681 char *math;
1682 sval_t sval;
1683 bool undo;
1684 int i;
1686 for (i = 0; i < split_count; i++) {
1687 if (strcmp(get_function(), forced_splits[i]->func) == 0) {
1688 data = forced_splits[i];
1689 break;
1692 if (!data)
1693 return false;
1695 // FIXME: this works for copy_to/from_user() because the only thing we
1696 // care about is zero/non-zero
1697 if (strcmp(data->rl, "0") != 0)
1698 return false;
1700 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1701 if (!compare)
1702 return false;
1703 if (get_implied_value(compare, &sval))
1704 return false;
1706 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1707 call_return_states_callbacks("0", expr);
1708 if (undo)
1709 end_assume();
1711 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1712 if (get_implied_rl(expr, &rl)) {
1713 math = strchr(return_ranges, '[');
1714 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1715 } else {
1716 snprintf(buf, sizeof(buf), "%s", return_ranges);
1718 call_return_states_callbacks(buf, expr);
1719 if (undo)
1720 end_assume();
1722 return true;
1725 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1727 struct returned_state_callback *cb;
1729 return_ranges = replace_return_ranges(return_ranges);
1730 if (is_delete_return(return_ranges))
1731 return;
1732 if (is_project_delete_return(expr))
1733 return;
1734 if (handle_forced_split(return_ranges, expr))
1735 return;
1737 return_id++;
1738 FOR_EACH_PTR(returned_state_callbacks, cb) {
1739 cb->callback(return_id, (char *)return_ranges, expr);
1740 } END_FOR_EACH_PTR(cb);
1743 static void call_return_state_hooks_compare(struct expression *expr)
1745 char *return_ranges;
1746 int final_pass_orig = final_pass;
1747 sval_t sval = { .type = &int_ctype };
1748 sval_t ret;
1750 if (!get_implied_value(expr, &ret))
1751 ret.value = -1;
1753 __push_fake_cur_stree();
1755 final_pass = 0;
1756 __split_whole_condition(expr);
1757 final_pass = final_pass_orig;
1759 if (ret.value != 0) {
1760 return_ranges = alloc_sname("1");
1761 sval.value = 1;
1762 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1764 call_return_states_callbacks(return_ranges, expr);
1767 __push_true_states();
1768 __use_false_states();
1770 if (ret.value != 1) {
1771 return_ranges = alloc_sname("0");
1772 sval.value = 0;
1773 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1775 call_return_states_callbacks(return_ranges, expr);
1778 __merge_true_states();
1779 __free_fake_cur_stree();
1782 static bool is_implies_function(struct expression *expr)
1784 struct range_list *rl;
1786 if (!expr)
1787 return false;
1789 rl = get_range_implications(get_function());
1790 if (!rl)
1791 return false;
1793 sm_msg("%s: is implied", __func__);
1794 return true;
1797 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1799 struct sm_state *tmp;
1801 FOR_EACH_PTR(slist, tmp) {
1802 if (strcmp(tmp->state->name, sm->state->name) == 0)
1803 return 1;
1804 } END_FOR_EACH_PTR(tmp);
1806 return 0;
1809 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1811 struct range_list *rl;
1812 char *return_ranges;
1813 struct sm_state *tmp;
1814 int ret = 0;
1815 int nr_possible, nr_states;
1816 char *compare_str;
1817 char buf[128];
1818 struct state_list *already_handled = NULL;
1819 sval_t sval;
1821 if (!sm || !sm->merged)
1822 return 0;
1824 if (too_many_possible(sm) && !is_implies_function(expr))
1825 return 0;
1827 /* bail if it gets too complicated */
1828 nr_possible = 0;
1829 FOR_EACH_PTR(sm->possible, tmp) {
1830 if (tmp->merged)
1831 continue;
1832 if (ptr_in_list(tmp, already_handled))
1833 continue;
1834 add_ptr_list(&already_handled, tmp);
1835 nr_possible++;
1836 } END_FOR_EACH_PTR(tmp);
1837 free_slist(&already_handled);
1838 nr_states = get_db_state_count();
1839 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1840 return 0;
1842 FOR_EACH_PTR(sm->possible, tmp) {
1843 if (!is_leaf(tmp))
1844 continue;
1845 if (ptr_in_list(tmp, already_handled))
1846 continue;
1847 add_ptr_list(&already_handled, tmp);
1849 ret = 1;
1850 __push_fake_cur_stree();
1852 overwrite_states_using_pool(sm, tmp);
1854 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1855 return_ranges = show_rl(rl);
1856 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1857 compare_str = get_return_compare_str(expr);
1858 /* ignore obvious stuff like 0 <= param */
1859 /* Is this worthile when we have PARAM_COMPARE? */
1860 if (compare_str &&
1861 strncmp(compare_str, "[=", 2) != 0 &&
1862 rl_to_sval(rl, &sval))
1863 compare_str = NULL;
1864 if (compare_str) {
1865 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1866 return_ranges = alloc_sname(buf);
1869 call_return_states_callbacks(return_ranges, expr);
1871 __free_fake_cur_stree();
1872 } END_FOR_EACH_PTR(tmp);
1874 free_slist(&already_handled);
1876 return ret;
1879 static int call_return_state_hooks_split_possible(struct expression *expr)
1881 struct expression *fake;
1882 struct sm_state *sm;
1884 if (!expr)
1885 return 0;
1887 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1888 if (!sm) {
1889 fake = expr_get_fake_parent_expr(expr);
1890 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1891 return 0;
1892 fake = fake->left;
1893 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1895 return split_possible_helper(sm, expr);
1898 static bool has_possible_negative(struct sm_state *sm)
1900 struct sm_state *tmp;
1902 if (!type_signed(estate_type(sm->state)))
1903 return false;
1905 FOR_EACH_PTR(sm->possible, tmp) {
1906 if (!estate_rl(tmp->state))
1907 continue;
1908 if (sval_is_negative(estate_min(tmp->state)) &&
1909 sval_is_negative(estate_max(tmp->state)))
1910 return true;
1911 } END_FOR_EACH_PTR(tmp);
1913 return false;
1916 static bool has_separate_zero_null(struct sm_state *sm)
1918 struct sm_state *tmp;
1919 sval_t sval;
1921 FOR_EACH_PTR(sm->possible, tmp) {
1922 if (!estate_get_single_value(tmp->state, &sval))
1923 continue;
1924 if (sval.value == 0)
1925 return true;
1926 } END_FOR_EACH_PTR(tmp);
1928 return false;
1931 static int split_positive_from_negative(struct expression *expr)
1933 struct sm_state *sm;
1934 struct range_list *rl;
1935 const char *return_ranges;
1936 struct range_list *ret_rl;
1937 bool separate_zero;
1938 int undo;
1940 /* We're going to print the states 3 times */
1941 if (get_db_state_count() > 10000 / 3)
1942 return 0;
1944 if (!get_implied_rl(expr, &rl) || !rl)
1945 return 0;
1946 /* Forget about INT_MAX and larger */
1947 if (rl_max(rl).value <= 0)
1948 return 0;
1949 if (!sval_is_negative(rl_min(rl)))
1950 return 0;
1952 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1953 if (!sm)
1954 return 0;
1955 if (!has_possible_negative(sm))
1956 return 0;
1957 separate_zero = has_separate_zero_null(sm);
1959 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1960 return 0;
1962 return_ranges = get_return_ranges_str(expr, &ret_rl);
1963 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1964 call_return_states_callbacks(return_ranges, expr);
1966 end_assume();
1968 if (separate_zero) {
1969 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1971 return_ranges = get_return_ranges_str(expr, &ret_rl);
1972 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1973 call_return_states_callbacks(return_ranges, expr);
1975 if (undo)
1976 end_assume();
1979 undo = assume(compare_expression(expr, '<', zero_expr()));
1981 return_ranges = get_return_ranges_str(expr, &ret_rl);
1982 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1983 call_return_states_callbacks(return_ranges, expr);
1985 if (undo)
1986 end_assume();
1988 return 1;
1991 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1993 struct range_list *rl;
1994 struct range_list *nonnull_rl;
1995 sval_t null_sval;
1996 struct range_list *null_rl = NULL;
1997 char *return_ranges;
1998 struct sm_state *sm;
1999 struct smatch_state *state;
2000 int nr_states;
2001 int final_pass_orig = final_pass;
2003 if (!expr || expr_equal_to_param(expr, -1))
2004 return 0;
2005 if (expr->type == EXPR_CALL)
2006 return 0;
2008 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2009 if (!sm)
2010 return 0;
2011 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2012 return 0;
2013 state = sm->state;
2014 if (!estate_rl(state))
2015 return 0;
2016 if (estate_min(state).value == 0 && estate_max(state).value == 0)
2017 return 0;
2018 if (has_possible_negative(sm))
2019 return 0;
2020 if (!has_separate_zero_null(sm))
2021 return 0;
2023 nr_states = get_db_state_count();
2024 if (option_info && nr_states >= 1500)
2025 return 0;
2027 rl = estate_rl(state);
2029 __push_fake_cur_stree();
2031 final_pass = 0;
2032 __split_whole_condition(expr);
2033 final_pass = final_pass_orig;
2035 nonnull_rl = rl_filter(rl, rl_zero());
2036 return_ranges = show_rl(nonnull_rl);
2037 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2039 call_return_states_callbacks(return_ranges, expr);
2041 __push_true_states();
2042 __use_false_states();
2044 return_ranges = alloc_sname("0");
2045 null_sval = sval_type_val(rl_type(rl), 0);
2046 add_range(&null_rl, null_sval, null_sval);
2047 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2048 call_return_states_callbacks(return_ranges, expr);
2050 __merge_true_states();
2051 __free_fake_cur_stree();
2053 return 1;
2056 static bool is_kernel_success_fail(struct sm_state *sm)
2058 struct sm_state *tmp;
2059 struct range_list *rl;
2060 bool has_zero = false;
2061 bool has_neg = false;
2063 if (!type_signed(estate_type(sm->state)))
2064 return false;
2066 FOR_EACH_PTR(sm->possible, tmp) {
2067 rl = estate_rl(tmp->state);
2068 if (!rl)
2069 return false;
2070 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2071 has_zero = true;
2072 continue;
2074 has_neg = true;
2075 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
2076 continue;
2077 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
2078 continue;
2079 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
2080 continue;
2081 return false;
2082 } END_FOR_EACH_PTR(tmp);
2084 return has_zero && has_neg;
2087 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2089 struct sm_state *sm;
2090 struct range_list *rl;
2091 struct range_list *nonzero_rl;
2092 sval_t zero_sval;
2093 struct range_list *zero_rl = NULL;
2094 int nr_states;
2095 char *return_ranges;
2096 int final_pass_orig = final_pass;
2098 if (option_project != PROJ_KERNEL)
2099 return 0;
2101 nr_states = get_db_state_count();
2102 if (nr_states > 2000)
2103 return 0;
2105 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2106 if (!sm)
2107 return 0;
2108 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2109 return 0;
2110 if (!is_kernel_success_fail(sm))
2111 return 0;
2113 rl = estate_rl(sm->state);
2114 if (!rl)
2115 return 0;
2117 __push_fake_cur_stree();
2119 final_pass = 0;
2120 __split_whole_condition(expr);
2121 final_pass = final_pass_orig;
2123 nonzero_rl = rl_filter(rl, rl_zero());
2124 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2125 return_ranges = show_rl(nonzero_rl);
2126 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2128 call_return_states_callbacks(return_ranges, expr);
2130 __push_true_states();
2131 __use_false_states();
2133 return_ranges = alloc_sname("0");
2134 zero_sval = sval_type_val(rl_type(rl), 0);
2135 add_range(&zero_rl, zero_sval, zero_sval);
2136 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2137 call_return_states_callbacks(return_ranges, expr);
2139 __merge_true_states();
2140 __free_fake_cur_stree();
2142 return 1;
2145 static int is_boolean(struct expression *expr)
2147 struct range_list *rl;
2149 if (!get_implied_rl(expr, &rl))
2150 return 0;
2151 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2152 return 1;
2153 return 0;
2156 static int splitable_function_call(struct expression *expr)
2158 struct sm_state *sm;
2160 if (!expr || expr->type != EXPR_CALL)
2161 return 0;
2162 sm = get_extra_sm_state(expr);
2163 return split_possible_helper(sm, expr);
2166 static struct sm_state *find_bool_param(void)
2168 struct stree *start_states;
2169 struct symbol *arg;
2170 struct sm_state *sm, *tmp;
2171 sval_t sval;
2173 start_states = get_start_states();
2175 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2176 if (!arg->ident)
2177 continue;
2178 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2179 if (!sm)
2180 continue;
2181 if (rl_min(estate_rl(sm->state)).value != 0 ||
2182 rl_max(estate_rl(sm->state)).value != 1)
2183 continue;
2184 goto found;
2185 } END_FOR_EACH_PTR_REVERSE(arg);
2187 return NULL;
2189 found:
2191 * Check if it's splitable. If not, then splitting it up is likely not
2192 * useful for the callers.
2194 FOR_EACH_PTR(sm->possible, tmp) {
2195 if (is_merged(tmp))
2196 continue;
2197 if (!estate_get_single_value(tmp->state, &sval))
2198 return NULL;
2199 } END_FOR_EACH_PTR(tmp);
2201 return sm;
2204 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2206 struct range_list *ret_rl;
2207 const char *return_ranges;
2208 struct sm_state *tmp;
2209 int ret = 0;
2210 struct state_list *already_handled = NULL;
2212 if (!sm || !sm->merged)
2213 return 0;
2215 if (too_many_possible(sm))
2216 return 0;
2218 FOR_EACH_PTR(sm->possible, tmp) {
2219 if (tmp->merged)
2220 continue;
2221 if (ptr_in_list(tmp, already_handled))
2222 continue;
2223 add_ptr_list(&already_handled, tmp);
2225 ret = 1;
2226 __push_fake_cur_stree();
2228 overwrite_states_using_pool(sm, tmp);
2230 return_ranges = get_return_ranges_str(expr, &ret_rl);
2231 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2232 call_return_states_callbacks(return_ranges, expr);
2234 __free_fake_cur_stree();
2235 } END_FOR_EACH_PTR(tmp);
2237 free_slist(&already_handled);
2239 return ret;
2242 static int split_by_bool_param(struct expression *expr)
2244 struct sm_state *start_sm, *sm;
2245 sval_t sval;
2247 start_sm = find_bool_param();
2248 if (!start_sm)
2249 return 0;
2250 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2251 if (!sm || estate_get_single_value(sm->state, &sval))
2252 return 0;
2254 if (get_db_state_count() * 2 >= 2000)
2255 return 0;
2257 return split_on_bool_sm(sm, expr);
2260 static int split_by_null_nonnull_param(struct expression *expr)
2262 struct symbol *arg;
2263 struct sm_state *sm;
2264 int nr_possible;
2266 /* function must only take one pointer */
2267 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2268 return 0;
2269 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2270 if (!arg->ident)
2271 return 0;
2272 if (get_real_base_type(arg)->type != SYM_PTR)
2273 return 0;
2275 if (param_was_set_var_sym(arg->ident->name, arg))
2276 return 0;
2277 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2278 if (!sm)
2279 return 0;
2281 if (!has_separate_zero_null(sm))
2282 return 0;
2284 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2285 if (get_db_state_count() * nr_possible >= 2000)
2286 return 0;
2288 return split_on_bool_sm(sm, expr);
2291 struct expression *strip_expr_statement(struct expression *expr)
2293 struct expression *orig = expr;
2294 struct statement *stmt, *last_stmt;
2296 if (!expr)
2297 return NULL;
2298 if (expr->type == EXPR_PREOP && expr->op == '(')
2299 expr = expr->unop;
2300 if (expr->type != EXPR_STATEMENT)
2301 return orig;
2302 stmt = expr->statement;
2303 if (!stmt || stmt->type != STMT_COMPOUND)
2304 return orig;
2306 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2307 if (!last_stmt || last_stmt->type == STMT_LABEL)
2308 last_stmt = last_stmt->label_statement;
2309 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2310 return orig;
2311 return strip_expr(last_stmt->expression);
2314 static bool is_kernel_error_path(struct expression *expr)
2316 struct range_list *rl;
2319 * Splitting up returns requires resources. It also requires resources
2320 * for the caller. It doesn't seem worth it to split anything up.
2322 if (!get_implied_rl(expr, &rl))
2323 return false;
2324 if (rl_type(rl) != &int_ctype)
2325 return false;
2326 if (rl_min(rl).value >= -4095 &&
2327 rl_max(rl).value < 0)
2328 return true;
2329 return false;
2332 static void call_return_state_hooks(struct expression *expr)
2334 struct range_list *ret_rl;
2335 const char *return_ranges;
2336 int nr_states;
2337 sval_t sval;
2339 if (__path_is_null())
2340 return;
2342 expr = strip_expr(expr);
2343 expr = strip_expr_statement(expr);
2345 if (is_impossible_path())
2346 goto vanilla;
2348 if (expr && (expr->type == EXPR_COMPARE ||
2349 !get_implied_value(expr, &sval)) &&
2350 (is_condition(expr) || is_boolean(expr))) {
2351 call_return_state_hooks_compare(expr);
2352 return;
2353 } else if (call_return_state_hooks_conditional(expr)) {
2354 return;
2355 } else if (is_kernel_error_path(expr)) {
2356 goto vanilla;
2357 } else if (call_return_state_hooks_split_possible(expr)) {
2358 return;
2359 } else if (split_positive_from_negative(expr)) {
2360 return;
2361 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2362 return;
2363 } else if (call_return_state_hooks_split_success_fail(expr)) {
2364 return;
2365 } else if (splitable_function_call(expr)) {
2366 return;
2367 } else if (split_by_bool_param(expr)) {
2368 } else if (split_by_null_nonnull_param(expr)) {
2369 return;
2372 vanilla:
2373 return_ranges = get_return_ranges_str(expr, &ret_rl);
2374 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2376 nr_states = get_db_state_count();
2377 if (nr_states >= 10000) {
2378 return_id++;
2379 match_return_info(return_id, (char *)return_ranges, expr);
2380 print_limited_param_set(return_id, (char *)return_ranges, expr);
2381 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2382 return;
2384 call_return_states_callbacks(return_ranges, expr);
2387 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2389 struct returned_member_callback *cb;
2390 struct sm_state *sm;
2391 struct symbol *type;
2392 char *name;
2393 char member_name[256];
2394 int len;
2396 type = get_type(expr);
2397 if (!type || type->type != SYM_PTR)
2398 return;
2399 name = expr_to_var(expr);
2400 if (!name)
2401 return;
2403 len = strlen(name);
2404 FOR_EACH_PTR(returned_member_callbacks, cb) {
2405 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2406 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2407 strcpy(member_name, "*$");
2408 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2409 continue;
2411 if (strncmp(sm->name, name, len) != 0)
2412 continue;
2413 if (strncmp(sm->name + len, "->", 2) != 0)
2414 continue;
2415 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2416 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2417 } END_FOR_EACH_SM(sm);
2418 } END_FOR_EACH_PTR(cb);
2420 free_string(name);
2423 static void print_return_struct_info(int return_id, char *return_ranges,
2424 struct expression *expr,
2425 struct symbol *sym,
2426 struct return_info_callback *cb)
2428 struct sm_state *sm;
2429 const char *printed_name;
2430 int param;
2432 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2433 param = get_param_num_from_sym(sm->sym);
2434 if (param < 0) {
2435 if (sm->sym && sm->sym == sym)
2436 param = -1;
2437 else
2438 continue;
2441 printed_name = get_param_name(sm);
2442 if (!printed_name)
2443 continue;
2445 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2446 } END_FOR_EACH_SM(sm);
2449 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2451 struct return_info_callback *cb;
2452 struct expression *tmp;
2453 struct symbol *sym;
2455 if (!option_info && !__inline_fn &&
2456 !local_debug && !option_debug)
2457 return;
2459 tmp = get_fake_variable(expr);
2460 if (tmp)
2461 expr = tmp;
2462 sym = expr_to_sym(expr);
2464 FOR_EACH_PTR(return_callbacks, cb) {
2465 __ignore_param_used++;
2466 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2467 __ignore_param_used--;
2468 } END_FOR_EACH_PTR(cb);
2471 static void reset_memdb(struct symbol *sym)
2473 mem_sql(NULL, NULL, "delete from caller_info;");
2474 mem_sql(NULL, NULL, "delete from return_states;");
2475 mem_sql(NULL, NULL, "delete from call_implies;");
2476 mem_sql(NULL, NULL, "delete from return_implies;");
2479 static void match_end_func_info(struct symbol *sym)
2481 if (__path_is_null())
2482 return;
2483 call_return_state_hooks(NULL);
2486 static void match_after_func(struct symbol *sym)
2488 if (!__inline_fn)
2489 reset_memdb(sym);
2492 static void init_memdb(void)
2494 char *err = NULL;
2495 int rc;
2496 const char *schema_files[] = {
2497 "db/db.schema",
2498 "db/caller_info.schema",
2499 "db/common_caller_info.schema",
2500 "db/return_states.schema",
2501 "db/function_type_size.schema",
2502 "db/type_size.schema",
2503 "db/function_type_info.schema",
2504 "db/type_info.schema",
2505 "db/call_implies.schema",
2506 "db/return_implies.schema",
2507 "db/function_ptr.schema",
2508 "db/local_values.schema",
2509 "db/function_type_value.schema",
2510 "db/type_value.schema",
2511 "db/function_type.schema",
2512 "db/data_info.schema",
2513 "db/parameter_name.schema",
2514 "db/constraints.schema",
2515 "db/constraints_required.schema",
2516 "db/fn_ptr_data_link.schema",
2517 "db/fn_data_link.schema",
2518 "db/mtag_about.schema",
2519 "db/mtag_info.schema",
2520 "db/mtag_map.schema",
2521 "db/mtag_data.schema",
2522 "db/mtag_alias.schema",
2524 static char buf[4096];
2525 int fd;
2526 int ret;
2527 int i;
2529 rc = sqlite3_open(":memory:", &mem_db);
2530 if (rc != SQLITE_OK) {
2531 sm_ierror("starting In-Memory database.");
2532 return;
2535 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2536 fd = open_schema_file(schema_files[i]);
2537 if (fd < 0)
2538 continue;
2539 ret = read(fd, buf, sizeof(buf));
2540 if (ret < 0) {
2541 sm_ierror("failed to read: %s", schema_files[i]);
2542 continue;
2544 close(fd);
2545 if (ret == sizeof(buf)) {
2546 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2547 schema_files[i], sizeof(buf));
2548 continue;
2550 buf[ret] = '\0';
2551 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2552 if (rc != SQLITE_OK) {
2553 sm_ierror("SQL error #2: %s", err);
2554 sm_ierror("%s", buf);
2559 static void init_cachedb(void)
2561 char *err = NULL;
2562 int rc;
2563 const char *schema_files[] = {
2564 "db/call_implies.schema",
2565 "db/return_implies.schema",
2566 "db/type_info.schema",
2567 "db/mtag_about.schema",
2568 "db/mtag_data.schema",
2569 "db/mtag_info.schema",
2570 "db/sink_info.schema",
2572 static char buf[4096];
2573 int fd;
2574 int ret;
2575 int i;
2577 rc = sqlite3_open(":memory:", &cache_db);
2578 if (rc != SQLITE_OK) {
2579 sm_ierror("starting In-Memory database.");
2580 return;
2583 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2584 fd = open_schema_file(schema_files[i]);
2585 if (fd < 0)
2586 continue;
2587 ret = read(fd, buf, sizeof(buf));
2588 if (ret < 0) {
2589 sm_ierror("failed to read: %s", schema_files[i]);
2590 continue;
2592 close(fd);
2593 if (ret == sizeof(buf)) {
2594 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2595 schema_files[i], sizeof(buf));
2596 continue;
2598 buf[ret] = '\0';
2599 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2600 if (rc != SQLITE_OK) {
2601 sm_ierror("SQL error #2: %s", err);
2602 sm_ierror("%s", buf);
2607 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2609 static char buf[4096];
2610 char tmp[256];
2611 char *p = buf;
2612 char *table = _table;
2613 int i;
2616 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2617 for (i = 0; i < argc; i++) {
2618 if (i)
2619 p += snprintf(p, 4096 - (p - buf), ", ");
2620 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2621 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2624 p += snprintf(p, 4096 - (p - buf), ");");
2625 if (p - buf > 4096)
2626 return 0;
2628 sm_msg("SQL: %s", buf);
2629 return 0;
2632 static void dump_cache(struct symbol_list *sym_list)
2634 const char *cache_tables[] = {
2635 "type_info", "return_implies", "call_implies", "mtag_data",
2636 "mtag_info", "mtag_about", "sink_info",
2638 char buf[64];
2639 int i;
2641 if (!option_info)
2642 return;
2644 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2645 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2646 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2650 void open_smatch_db(char *db_file)
2652 int rc;
2654 if (option_no_db)
2655 return;
2657 use_states = malloc(num_checks + 1);
2658 memset(use_states, 0xff, num_checks + 1);
2660 init_memdb();
2661 init_cachedb();
2663 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2664 if (rc != SQLITE_OK) {
2665 option_no_db = 1;
2666 return;
2668 run_sql(NULL, NULL,
2669 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2670 return;
2673 static char *get_next_string(char **str)
2675 static char string[256];
2676 char *start;
2677 char *p = *str;
2678 int len, i, j;
2680 if (*p == '\0')
2681 return NULL;
2682 start = p;
2684 while (*p != '\0' && *p != '\n') {
2685 if (*p == '\\' && *(p + 1) == ' ') {
2686 p += 2;
2687 continue;
2689 if (*p == ' ')
2690 break;
2691 p++;
2694 len = p - start;
2695 if (len >= sizeof(string)) {
2696 memcpy(string, start, sizeof(string));
2697 string[sizeof(string) - 1] = '\0';
2698 sm_ierror("return_fix: '%s' too long", string);
2699 **str = '\0';
2700 return NULL;
2702 memcpy(string, start, len);
2703 string[len] = '\0';
2704 for (i = 0; i < sizeof(string) - 1; i++) {
2705 if (string[i] == '\\' && string[i + 1] == ' ') {
2706 for (j = i; string[j] != '\0'; j++)
2707 string[j] = string[j + 1];
2710 if (*p != '\0')
2711 p++;
2712 *str = p;
2713 return string;
2716 static void register_return_deletes(void)
2718 char *func, *ret_str;
2719 char filename[256];
2720 char buf[4096];
2721 int fd, ret, i;
2722 char *p;
2724 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2725 fd = open_schema_file(filename);
2726 if (fd < 0)
2727 return;
2728 ret = read(fd, buf, sizeof(buf));
2729 close(fd);
2730 if (ret < 0)
2731 return;
2732 if (ret == sizeof(buf)) {
2733 sm_ierror("file too large: %s (limit %zd bytes)",
2734 filename, sizeof(buf));
2735 return;
2737 buf[ret] = '\0';
2739 p = buf;
2740 while (*p) {
2741 get_next_string(&p);
2742 delete_count++;
2744 if (delete_count == 0)
2745 return;
2746 if (delete_count % 2 != 0) {
2747 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2748 delete_count = 0;
2749 return;
2751 delete_table = malloc(delete_count * sizeof(char *));
2753 p = buf;
2754 i = 0;
2755 while (*p) {
2756 func = alloc_string(get_next_string(&p));
2757 ret_str = alloc_string(get_next_string(&p));
2759 delete_table[i++] = func;
2760 delete_table[i++] = ret_str;
2764 static void register_return_replacements(void)
2766 char *func, *orig, *new;
2767 char filename[256];
2768 char buf[4096];
2769 int fd, ret, i;
2770 char *p;
2772 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2773 fd = open_schema_file(filename);
2774 if (fd < 0)
2775 return;
2776 ret = read(fd, buf, sizeof(buf));
2777 close(fd);
2778 if (ret < 0)
2779 return;
2780 if (ret == sizeof(buf)) {
2781 sm_ierror("file too large: %s (limit %zd bytes)",
2782 filename, sizeof(buf));
2783 return;
2785 buf[ret] = '\0';
2787 p = buf;
2788 while (*p) {
2789 get_next_string(&p);
2790 replace_count++;
2792 if (replace_count == 0 || replace_count % 3 != 0) {
2793 replace_count = 0;
2794 return;
2796 replace_table = malloc(replace_count * sizeof(char *));
2798 p = buf;
2799 i = 0;
2800 while (*p) {
2801 func = alloc_string(get_next_string(&p));
2802 orig = alloc_string(get_next_string(&p));
2803 new = alloc_string(get_next_string(&p));
2805 replace_table[i++] = func;
2806 replace_table[i++] = orig;
2807 replace_table[i++] = new;
2811 static void register_forced_return_splits(void)
2813 int struct_members = sizeof(struct split_data) / sizeof(char *);
2814 char filename[256];
2815 char buf[4096];
2816 int fd, ret, i;
2817 char *p;
2819 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
2820 fd = open_schema_file(filename);
2821 if (fd < 0)
2822 return;
2823 ret = read(fd, buf, sizeof(buf));
2824 close(fd);
2825 if (ret < 0)
2826 return;
2827 if (ret == sizeof(buf)) {
2828 sm_ierror("file too large: %s (limit %zd bytes)",
2829 filename, sizeof(buf));
2830 return;
2832 buf[ret] = '\0';
2834 p = buf;
2835 while (*p) {
2836 get_next_string(&p);
2837 split_count++;
2839 if (split_count == 0)
2840 return;
2841 if (split_count % struct_members != 0) {
2842 printf("error parsing '%s' split_count=%d\n", filename, split_count);
2843 split_count = 0;
2844 return;
2846 split_count /= struct_members;
2847 forced_splits = malloc(split_count * sizeof(void *));
2849 p = buf;
2850 i = 0;
2851 while (*p) {
2852 struct split_data *split = malloc(sizeof(*split));
2854 split->func = alloc_string(get_next_string(&p));
2855 split->rl = alloc_string(get_next_string(&p));
2856 forced_splits[i++] = split;
2860 void register_definition_db_callbacks(int id)
2862 my_id = id;
2864 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2865 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2866 add_split_return_callback(match_return_info);
2867 add_split_return_callback(print_returned_struct_members);
2868 add_split_return_callback(print_return_info);
2869 add_hook(&call_return_state_hooks, RETURN_HOOK);
2870 add_hook(&match_end_func_info, END_FUNC_HOOK);
2871 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2873 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2874 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2875 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2877 common_funcs = load_strings_from_file(option_project_str, "common_functions");
2878 register_return_deletes();
2879 register_return_replacements();
2880 register_forced_return_splits();
2882 add_hook(&dump_cache, END_FILE_HOOK);
2885 void register_db_call_marker(int id)
2887 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2890 char *get_data_info_name(struct expression *expr)
2892 struct symbol *sym;
2893 char *name;
2894 char buf[256];
2895 char *ret = NULL;
2897 expr = strip_expr(expr);
2898 name = get_member_name(expr);
2899 if (name)
2900 return name;
2901 name = expr_to_var_sym(expr, &sym);
2902 if (!name || !sym)
2903 goto free;
2904 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2905 goto free;
2906 if (sym->ctype.modifiers & MOD_STATIC)
2907 snprintf(buf, sizeof(buf), "static %s", name);
2908 else
2909 snprintf(buf, sizeof(buf), "global %s", name);
2910 ret = alloc_sname(buf);
2911 free:
2912 free_string(name);
2913 return ret;