free_strict: silence some kfree_skb() false positives
[smatch.git] / smatch_db.c
blob8c8640dfe15ef83b6dec7dce4661a9dffefc2563
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 /* silently truncates if needed. */
104 char *escape_newlines(const char *str)
106 char buf[1024] = "";
107 bool found = false;
108 int i, j;
110 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
111 if (str[i] != '\r' && str[i] != '\n') {
112 buf[j] = str[i];
113 continue;
116 found = true;
117 buf[j++] = '\\';
118 if (j == sizeof(buf))
119 break;
120 buf[j] = 'n';
123 if (!found)
124 return alloc_sname(str);
126 if (j == sizeof(buf))
127 buf[j - 1] = '\0';
128 return alloc_sname(buf);
131 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
133 int i;
135 for (i = 0; i < argc; i++) {
136 if (i != 0)
137 sm_printf(", ");
138 sm_printf("%s", argv[i]);
140 sm_printf("\n");
141 return 0;
144 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
146 char *err = NULL;
147 int rc;
149 if (!db)
150 return;
152 if (option_debug || debug_db) {
153 sm_msg("%s", sql);
154 if (strncasecmp(sql, "select", strlen("select")) == 0)
155 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
158 rc = sqlite3_exec(db, sql, callback, data, &err);
159 if (rc != SQLITE_OK && !parse_error) {
160 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
161 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
162 parse_error = 1;
166 static int replace_count;
167 static char **replace_table;
168 static const char *replace_return_ranges(const char *return_ranges)
170 int i;
172 if (!get_function()) {
173 /* I have no idea why EXPORT_SYMBOL() is here */
174 return return_ranges;
176 for (i = 0; i < replace_count; i += 3) {
177 if (strcmp(replace_table[i + 0], get_function()) == 0) {
178 if (strcmp(replace_table[i + 1], return_ranges) == 0)
179 return replace_table[i + 2];
182 return return_ranges;
186 static char *use_states;
187 static int get_db_state_count(void)
189 struct sm_state *sm;
190 int count = 0;
192 FOR_EACH_SM(__get_cur_stree(), sm) {
193 if (sm->owner == USHRT_MAX)
194 continue;
195 if (use_states[sm->owner])
196 count++;
197 } END_FOR_EACH_SM(sm);
198 return count;
201 static bool is_local(struct symbol *sym)
203 if (sym->ctype.modifiers & MOD_STATIC)
204 return true;
205 if ((sym->ctype.modifiers & MOD_EXTERN) &&
206 (sym->ctype.modifiers & MOD_INLINE))
207 return true;
209 if (!sym->definition)
210 return false;
212 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
213 (sym->definition->ctype.modifiers & MOD_INLINE))
214 return true;
216 return false;
219 void db_ignore_states(int id)
221 use_states[id] = 0;
224 unsigned long long __fn_mtag;
225 static void set_fn_mtag(struct symbol *sym)
227 char buf[128];
229 if (is_local(cur_func_sym))
230 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
231 else
232 snprintf(buf, sizeof(buf), "extern %s", get_function());
234 __fn_mtag = str_to_mtag(buf);
237 void sql_insert_return_states(int return_id, const char *return_ranges,
238 int type, int param, const char *key, const char *value)
240 unsigned long long id;
243 if (key && strlen(key) >= 80)
244 return;
245 if (__inline_fn)
246 id = (unsigned long)__inline_fn;
247 else
248 id = __fn_mtag;
250 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
251 get_base_file(), get_function(), id, return_id,
252 return_ranges, fn_static(), type, param, key, value);
255 static struct string_list *common_funcs;
256 static int is_common_function(const char *fn)
258 char *tmp;
260 if (!fn)
261 return 0;
263 if (strncmp(fn, "__builtin_", 10) == 0)
264 return 1;
266 FOR_EACH_PTR(common_funcs, tmp) {
267 if (strcmp(tmp, fn) == 0)
268 return 1;
269 } END_FOR_EACH_PTR(tmp);
271 return 0;
274 static char *function_signature(void)
276 return type_to_str(get_real_base_type(cur_func_sym));
279 void sql_insert_caller_info(struct expression *call, int type,
280 int param, const char *key, const char *value)
282 FILE *tmp_fd = sm_outfd;
283 char *fn;
285 if (!option_info && !__inline_call)
286 return;
287 if (unreachable())
288 return;
290 if (key && strlen(key) >= 80)
291 return;
293 fn = get_fnptr_name(call->fn);
294 if (!fn)
295 return;
297 if (__inline_call) {
298 mem_sql(NULL, NULL,
299 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
300 get_base_file(), get_function(), fn, (unsigned long)call,
301 is_static(call->fn), type, param, key, value);
304 if (!option_info)
305 return;
307 if (strncmp(fn, "__builtin_", 10) == 0)
308 return;
309 if (type != INTERNAL && is_common_function(fn))
310 return;
312 sm_outfd = caller_info_fd;
313 sm_msg("SQL_caller_info: insert into caller_info values ("
314 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
315 get_base_file(), get_function(), fn, is_static(call->fn),
316 type, param, key, value);
317 sm_outfd = tmp_fd;
319 free_string(fn);
322 void sql_insert_function_ptr(const char *fn, const char *struct_name)
324 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
325 get_base_file(), fn, struct_name);
328 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
330 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
331 get_base_file(), get_function(), (unsigned long)__inline_fn,
332 fn_static(), type, param, key, value);
335 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
337 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
338 get_base_file(), get_function(), (unsigned long)__inline_fn,
339 fn_static(), type, param, key, value);
342 void sql_insert_function_type_size(const char *member, const char *ranges)
344 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
347 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
349 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
352 void sql_insert_type_info(int type, const char *member, const char *value)
354 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
357 void sql_insert_local_values(const char *name, const char *value)
359 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
362 void sql_insert_function_type_value(const char *type, const char *value)
364 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
367 void sql_insert_function_type(int param, const char *value)
369 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
370 get_base_file(), get_function(), fn_static(), param, value);
373 void sql_insert_parameter_name(int param, const char *value)
375 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
376 get_base_file(), get_function(), fn_static(), param, value);
379 void sql_insert_data_info(struct expression *data, int type, const char *value)
381 char *data_name;
383 data_name = get_data_info_name(data);
384 if (!data_name)
385 return;
386 sql_insert(data_info, "'%s', '%s', %d, '%s'",
387 is_static(data) ? get_base_file() : "extern",
388 data_name, type, value);
391 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
393 sql_insert(data_info, "'%s', '%s', %d, '%s'",
394 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
395 var, type, value);
398 void sql_save_constraint(const char *con)
400 if (!option_info)
401 return;
403 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
406 void sql_save_constraint_required(const char *data, int op, const char *limit)
408 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
411 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
413 if (!option_info)
414 return;
416 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
417 "select constraints_required.data, constraints_required.op, '%s' from "
418 "constraints_required where bound = '%s';", new_limit, old_limit);
421 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
423 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
426 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
428 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
429 return;
431 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
432 is_local(fn->symbol) ? get_base_file() : "extern",
433 fn->symbol->ident->name,
434 is_local(fn->symbol),
435 type, param, key, value);
438 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
440 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
441 tag, get_filename(), get_function(), get_lineno(),
442 left_name, right_name);
445 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
447 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
450 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
452 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
455 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
457 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
460 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
462 mtag_t *saved_tag = _tag;
463 mtag_t new_tag;
465 new_tag = strtoll(argv[0], NULL, 10);
467 if (!*saved_tag)
468 *saved_tag = new_tag;
469 else if (*saved_tag != new_tag)
470 *saved_tag = -1ULL;
472 return 0;
475 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
477 mtag_t tmp = 0;
479 run_sql(save_mtag, &tmp,
480 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
481 tag, container_offset);
483 if (tmp == 0 || tmp == -1ULL)
484 return 0;
485 *container = tmp;
486 return 1;
489 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
491 mtag_t tmp = 0;
493 run_sql(save_mtag, &tmp,
494 "select tag from mtag_map where container = %lld and container_offset = %d;",
495 container, offset);
497 if (tmp == 0 || tmp == -1ULL)
498 return 0;
499 *tag = tmp;
500 return 1;
503 char *get_static_filter(struct symbol *sym)
505 static char sql_filter[1024];
507 /* This can only happen on buggy code. Return invalid SQL. */
508 if (!sym) {
509 sql_filter[0] = '\0';
510 return sql_filter;
513 if (is_local(sym)) {
514 snprintf(sql_filter, sizeof(sql_filter),
515 "file = '%s' and function = '%s' and static = '1'",
516 get_base_file(), sym->ident->name);
517 } else {
518 snprintf(sql_filter, sizeof(sql_filter),
519 "function = '%s' and static = '0'", sym->ident->name);
522 return sql_filter;
525 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
527 int *row_count = _row_count;
529 *row_count = 0;
530 if (argc != 1)
531 return 0;
532 *row_count = atoi(argv[0]);
533 return 0;
536 static void mark_call_params_untracked(struct expression *call)
538 struct expression *arg;
539 int i = 0;
541 FOR_EACH_PTR(call->args, arg) {
542 mark_untracked(call, i++, "$", NULL);
543 } END_FOR_EACH_PTR(arg);
546 static void sql_select_return_states_pointer(const char *cols,
547 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
549 char *ptr;
550 int return_count = 0;
552 ptr = get_fnptr_name(call->fn);
553 if (!ptr)
554 return;
556 run_sql(get_row_count, &return_count,
557 "select count(*) from return_states join function_ptr "
558 "where return_states.function == function_ptr.function and "
559 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
560 /* The magic number 100 is just from testing on the kernel. */
561 if (return_count > 100) {
562 mark_call_params_untracked(call);
563 return;
566 run_sql(callback, info,
567 "select %s from return_states join function_ptr where "
568 "return_states.function == function_ptr.function and ptr = '%s' "
569 "and searchable = 1 "
570 "order by function_ptr.file, return_states.file, return_id, type;",
571 cols, ptr);
574 static int is_local_symbol(struct expression *expr)
576 if (expr->type != EXPR_SYMBOL)
577 return 0;
578 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
579 return 0;
580 return 1;
583 bool is_fn_ptr(struct expression *fn)
585 fn = strip_expr(fn);
586 if (fn->type != EXPR_SYMBOL)
587 return true;
588 if (!fn->symbol)
589 return true;
590 if (is_local_symbol(fn))
591 return true;
592 return false;
595 void sql_select_return_states(const char *cols, struct expression *call,
596 int (*callback)(void*, int, char**, char**), void *info)
598 struct expression *fn;
599 int row_count = 0;
601 if (is_fake_call(call))
602 return;
604 fn = strip_expr(call->fn);
605 if (is_fn_ptr(fn)) {
606 sql_select_return_states_pointer(cols, call, callback, info);
607 return;
610 if (inlinable(fn)) {
611 mem_sql(callback, info,
612 "select %s from return_states where call_id = '%lu' order by return_id, type;",
613 cols, (unsigned long)call);
614 return;
617 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
618 get_static_filter(fn->symbol));
619 if (row_count == 0 && fn->symbol && fn->symbol->definition)
620 set_state(my_id, "db_incomplete", NULL, &incomplete);
621 if (row_count > 3000)
622 return;
624 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
625 cols, get_static_filter(fn->symbol));
628 bool db_incomplete(void)
630 return !!get_state(my_id, "db_incomplete", NULL);
633 #define CALL_IMPLIES 0
634 #define RETURN_IMPLIES 1
636 struct implies_info {
637 int type;
638 struct db_implies_cb_list *cb_list;
639 struct expression *expr;
640 struct symbol *sym;
643 void sql_select_implies(const char *cols, struct implies_info *info,
644 int (*callback)(void*, int, char**, char**))
646 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
647 mem_sql(callback, info,
648 "select %s from return_implies where call_id = '%lu';",
649 cols, (unsigned long)info->expr);
650 return;
653 run_sql(callback, info, "select %s from %s_implies where %s;",
654 cols,
655 info->type == CALL_IMPLIES ? "call" : "return",
656 get_static_filter(info->sym));
659 struct select_caller_info_data {
660 struct stree *final_states;
661 struct timeval start_time;
662 int prev_func_id;
663 int ignore;
664 int results;
667 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
669 static void sql_select_caller_info(struct select_caller_info_data *data,
670 const char *cols, struct symbol *sym)
672 if (__inline_fn) {
673 mem_sql(caller_info_callback, data,
674 "select %s from caller_info where call_id = %lu;",
675 cols, (unsigned long)__inline_fn);
676 return;
679 if (sym->ident->name && is_common_function(sym->ident->name))
680 return;
681 run_sql(caller_info_callback, data,
682 "select %s from common_caller_info where %s order by call_id;",
683 cols, get_static_filter(sym));
684 if (data->results)
685 return;
687 run_sql(caller_info_callback, data,
688 "select %s from caller_info where %s order by call_id;",
689 cols, get_static_filter(sym));
692 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
694 struct def_callback *def_callback = __alloc_def_callback(0);
696 def_callback->hook_type = type;
697 def_callback->callback = callback;
698 add_ptr_list(&select_caller_info_callbacks, def_callback);
701 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
703 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
705 callback->hook_type = type;
706 callback->callback = fn;
707 add_ptr_list(&select_caller_name_sym_callbacks, callback);
711 * These call backs are used when the --info option is turned on to print struct
712 * member information. For example foo->bar could have a state in
713 * smatch_extra.c and also check_user.c.
715 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
717 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
719 member_callback->owner = owner;
720 member_callback->callback = callback;
721 add_ptr_list(&member_callbacks, member_callback);
724 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
726 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
728 member_callback->owner = owner;
729 member_callback->callback = callback;
730 add_ptr_list(&member_callbacks_new, member_callback);
733 void add_return_info_callback(int owner,
734 void (*callback)(int return_id, char *return_ranges,
735 struct expression *returned_expr,
736 int param,
737 const char *printed_name,
738 struct sm_state *sm))
740 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
742 return_callback->owner = owner;
743 return_callback->callback = callback;
744 add_ptr_list(&return_callbacks, return_callback);
747 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
749 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
751 callback->callback = fn;
752 add_ptr_list(&returned_state_callbacks, callback);
755 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))
757 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
759 member_callback->owner = owner;
760 member_callback->callback = callback;
761 add_ptr_list(&returned_member_callbacks, member_callback);
764 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
766 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
768 cb->type = type;
769 cb->callback = callback;
770 add_ptr_list(&call_implies_cb_list, cb);
773 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
775 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
777 cb->type = type;
778 cb->callback = callback;
779 add_ptr_list(&return_implies_cb_list, cb);
782 struct return_info {
783 struct expression *static_returns_call;
784 struct symbol *return_type;
785 struct range_list *return_range_list;
788 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
790 struct return_info *ret_info = _ret_info;
791 struct range_list *rl;
792 struct expression *call_expr = ret_info->static_returns_call;
794 if (argc != 1)
795 return 0;
796 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
797 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
798 return 0;
801 struct range_list *db_return_vals(struct expression *expr)
803 struct return_info ret_info = {};
804 struct sm_state *sm;
806 if (is_fake_call(expr))
807 return NULL;
809 sm = get_extra_sm_state(expr);
810 if (sm)
811 return clone_rl(estate_rl(sm->state));
812 ret_info.static_returns_call = expr;
813 ret_info.return_type = get_type(expr);
814 if (!ret_info.return_type)
815 return NULL;
817 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
818 return NULL;
820 ret_info.return_range_list = NULL;
821 if (inlinable(expr->fn)) {
822 mem_sql(db_return_callback, &ret_info,
823 "select distinct return from return_states where call_id = '%lu';",
824 (unsigned long)expr);
825 } else {
826 run_sql(db_return_callback, &ret_info,
827 "select distinct return from return_states where %s;",
828 get_static_filter(expr->fn->symbol));
830 return ret_info.return_range_list;
833 struct range_list *db_return_vals_from_str(const char *fn_name)
835 struct return_info ret_info;
837 ret_info.static_returns_call = NULL;
838 ret_info.return_type = &llong_ctype;
839 ret_info.return_range_list = NULL;
841 run_sql(db_return_callback, &ret_info,
842 "select distinct return from return_states where function = '%s';",
843 fn_name);
844 return ret_info.return_range_list;
848 * This is used when we have a function that takes a function pointer as a
849 * parameter. "frob(blah, blah, my_function);" We know that the return values
850 * from frob() come from my_funcion() so we want to find the possible returns
851 * of my_function(), but we don't know which arguments are passed to it.
854 struct range_list *db_return_vals_no_args(struct expression *expr)
856 struct return_info ret_info = {};
858 if (!expr || expr->type != EXPR_SYMBOL)
859 return NULL;
861 ret_info.static_returns_call = expr;
862 ret_info.return_type = get_type(expr);
863 ret_info.return_type = get_real_base_type(ret_info.return_type);
864 if (!ret_info.return_type)
865 return NULL;
867 run_sql(db_return_callback, &ret_info,
868 "select distinct return from return_states where %s;",
869 get_static_filter(expr->symbol));
871 return ret_info.return_range_list;
874 static void match_call_marker(struct expression *expr)
876 struct symbol *type;
878 type = get_type(expr->fn);
879 if (type && type->type == SYM_PTR)
880 type = get_real_base_type(type);
883 * we just want to record something in the database so that if we have
884 * two calls like: frob(4); frob(some_unkown); then on the receiving
885 * side we know that sometimes frob is called with unknown parameters.
888 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
891 int is_recursive_member(const char *name)
893 char buf[256];
894 const char *p, *next;
895 int size;
897 p = strchr(name, '>');
898 if (!p)
899 return 0;
900 p++;
901 while (true) {
902 next = strchr(p, '>');
903 if (!next)
904 return 0;
905 next++;
907 size = next - p;
908 if (size >= sizeof(buf))
909 return 0;
910 memcpy(buf, p, size);
911 buf[size] = '\0';
912 if (strstr(next, buf))
913 return 1;
914 p = next;
918 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
920 struct symbol *sym;
921 const char *sm_name;
922 char *name;
923 bool is_address = false;
924 bool add_star = false;
925 char buf[256];
926 char *ret = NULL;
927 int len;
929 expr = strip_expr(expr);
930 if (!expr)
931 return NULL;
933 if (expr->type == EXPR_PREOP && expr->op == '&') {
934 expr = strip_expr(expr->unop);
935 is_address = true;
938 name = expr_to_var_sym(expr, &sym);
939 if (!name || !sym)
940 goto free;
941 if (sym != sm->sym)
942 goto free;
944 sm_name = sm->name;
945 add_star = false;
946 if (sm_name[0] == '*') {
947 add_star = true;
948 sm_name++;
951 len = strlen(name);
952 if (strncmp(name, sm_name, len) != 0)
953 goto free;
954 if (sm_name[len] == '\0') {
955 snprintf(buf, sizeof(buf), "%s%s$",
956 add_star ? "*" : "", is_address ? "*" : "");
957 } else {
958 if (sm_name[len] != '.' && sm_name[len] != '-')
959 goto free;
960 if (sm_name[len] == '-')
961 len++;
962 // FIXME does is_address really imply that sm_name[len] == '-'
963 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
964 sm_name + len);
967 ret = alloc_sname(buf);
968 free:
969 free_string(name);
970 return ret;
973 static void print_struct_members(struct expression *call, struct expression *expr, int param,
974 int owner,
975 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
976 bool new)
978 struct sm_state *sm;
979 const char *sm_name;
980 char *name;
981 struct symbol *sym;
982 int len;
983 char printed_name[256];
984 int is_address = 0;
985 bool add_star;
986 struct symbol *type;
988 expr = strip_expr(expr);
989 if (!expr)
990 return;
991 type = get_type(expr);
992 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
993 return;
995 if (expr->type == EXPR_PREOP && expr->op == '&') {
996 expr = strip_expr(expr->unop);
997 is_address = 1;
1000 name = expr_to_var_sym(expr, &sym);
1001 if (!name || !sym)
1002 goto free;
1004 len = strlen(name);
1005 FOR_EACH_SM(__get_cur_stree(), sm) {
1006 if (sm->owner != owner || sm->sym != sym)
1007 continue;
1008 sm_name = sm->name;
1009 add_star = false;
1010 if (sm_name[0] == '*') {
1011 add_star = true;
1012 sm_name++;
1014 // FIXME: simplify?
1015 if (!add_star && strcmp(name, sm_name) == 0) {
1016 if (is_address) {
1017 snprintf(printed_name, sizeof(printed_name), "*$");
1018 } else {
1019 if (new)
1020 snprintf(printed_name, sizeof(printed_name), "$");
1021 else
1022 continue;
1024 } else if (add_star && strcmp(name, sm_name) == 0) {
1025 snprintf(printed_name, sizeof(printed_name), "%s*$",
1026 is_address ? "*" : "");
1027 } else if (strncmp(name, sm_name, len) == 0) {
1028 if (sm_name[len] != '.' && sm_name[len] != '-')
1029 continue;
1030 if (is_address && sm_name[len] == '.') {
1031 snprintf(printed_name, sizeof(printed_name),
1032 "%s$->%s", add_star ? "*" : "",
1033 sm_name + len + 1);
1034 } else if (is_address && sm_name[len] == '-') {
1035 snprintf(printed_name, sizeof(printed_name),
1036 "%s(*$)%s", add_star ? "*" : "",
1037 sm_name + len);
1038 } else {
1039 snprintf(printed_name, sizeof(printed_name),
1040 "%s$%s", add_star ? "*" : "",
1041 sm_name + len);
1043 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1044 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1045 continue;
1046 if (is_address && sm_name[len + 1] == '.') {
1047 snprintf(printed_name, sizeof(printed_name),
1048 "&%s$->%s", add_star ? "*" : "",
1049 sm_name + len + 2);
1050 } else if (is_address && sm_name[len] == '-') {
1051 snprintf(printed_name, sizeof(printed_name),
1052 "&%s(*$)%s", add_star ? "*" : "",
1053 sm_name + len + 1);
1054 } else {
1055 snprintf(printed_name, sizeof(printed_name),
1056 "&%s$%s", add_star ? "*" : "",
1057 sm_name + len + 1);
1059 } else {
1060 continue;
1062 if (is_recursive_member(printed_name))
1063 continue;
1064 callback(call, param, printed_name, sm);
1065 } END_FOR_EACH_SM(sm);
1066 free:
1067 free_string(name);
1070 static void match_call_info(struct expression *call)
1072 struct member_info_callback *cb;
1073 struct expression *arg;
1074 int i;
1076 FOR_EACH_PTR(member_callbacks, cb) {
1077 i = -1;
1078 FOR_EACH_PTR(call->args, arg) {
1079 i++;
1080 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1081 } END_FOR_EACH_PTR(arg);
1082 } END_FOR_EACH_PTR(cb);
1085 static struct expression *get_fake_variable(struct expression *expr)
1087 struct expression *tmp;
1089 tmp = expr_get_fake_parent_expr(expr);
1090 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1091 return NULL;
1093 return tmp->left;
1096 static void match_call_info_new(struct expression *call)
1098 struct member_info_callback *cb;
1099 struct expression *arg, *tmp;
1100 int i;
1102 if (!option_info && !__inline_call)
1103 return;
1105 FOR_EACH_PTR(member_callbacks_new, cb) {
1106 i = -1;
1107 FOR_EACH_PTR(call->args, arg) {
1108 i++;
1109 tmp = get_fake_variable(arg);
1110 if (!tmp)
1111 tmp = arg;
1112 __ignore_param_used++;
1113 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1114 __ignore_param_used--;
1115 } END_FOR_EACH_PTR(arg);
1116 } END_FOR_EACH_PTR(cb);
1119 static int get_param(int param, char **name, struct symbol **sym)
1121 struct symbol *arg;
1122 int i;
1124 i = 0;
1125 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1127 * this is a temporary hack to work around a bug (I think in sparse?)
1128 * 2.6.37-rc1:fs/reiserfs/journal.o
1129 * If there is a function definition without parameter name found
1130 * after a function implementation then it causes a crash.
1131 * int foo() {}
1132 * int bar(char *);
1134 if (arg->ident->name < (char *)100)
1135 continue;
1136 if (i == param) {
1137 *name = arg->ident->name;
1138 *sym = arg;
1139 return TRUE;
1141 i++;
1142 } END_FOR_EACH_PTR(arg);
1144 return FALSE;
1147 static int function_signature_matches(const char *sig)
1149 char *my_sig;
1151 my_sig = function_signature();
1152 if (!sig || !my_sig)
1153 return 1; /* default to matching */
1154 if (strcmp(my_sig, sig) == 0)
1155 return 1;
1156 return 0;
1159 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1161 struct select_caller_info_data *data = _data;
1162 int func_id;
1163 long type;
1164 long param;
1165 char *key;
1166 char *value;
1167 char *name = NULL;
1168 struct symbol *sym = NULL;
1169 struct def_callback *def_callback;
1170 struct def_name_sym_callback *ns_callback;
1171 struct stree *stree;
1172 struct timeval cur_time;
1173 char fullname[256];
1174 char *p;
1176 data->results = 1;
1178 if (argc != 5)
1179 return 0;
1181 gettimeofday(&cur_time, NULL);
1182 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1183 return 0;
1185 func_id = atoi(argv[0]);
1186 errno = 0;
1187 type = strtol(argv[1], NULL, 10);
1188 param = strtol(argv[2], NULL, 10);
1189 if (errno)
1190 return 0;
1191 key = argv[3];
1192 value = argv[4];
1194 if (data->prev_func_id == -1)
1195 data->prev_func_id = func_id;
1196 if (func_id != data->prev_func_id) {
1197 stree = __pop_fake_cur_stree();
1198 if (!data->ignore)
1199 merge_stree(&data->final_states, stree);
1200 free_stree(&stree);
1201 __push_fake_cur_stree();
1202 __unnullify_path();
1203 data->prev_func_id = func_id;
1204 data->ignore = 0;
1207 if (data->ignore)
1208 return 0;
1209 if (type == INTERNAL &&
1210 !function_signature_matches(value)) {
1211 data->ignore = 1;
1212 return 0;
1215 if (param >= 0 && !get_param(param, &name, &sym))
1216 return 0;
1218 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1219 if (def_callback->hook_type == type)
1220 def_callback->callback(name, sym, key, value);
1221 } END_FOR_EACH_PTR(def_callback);
1223 p = strchr(key, '$');
1224 if (name && p)
1225 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1226 else
1227 snprintf(fullname, sizeof(fullname), "%s", key);
1229 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1230 if (ns_callback->hook_type == type)
1231 ns_callback->callback(fullname, sym, value);
1232 } END_FOR_EACH_PTR(ns_callback);
1234 return 0;
1237 static struct string_list *ptr_names_done;
1238 static struct string_list *ptr_names;
1240 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1242 insert_string(&ptr_names, alloc_string(argv[0]));
1243 return 0;
1246 static char *get_next_ptr_name(void)
1248 char *ptr;
1250 FOR_EACH_PTR(ptr_names, ptr) {
1251 if (!insert_string(&ptr_names_done, ptr))
1252 continue;
1253 return ptr;
1254 } END_FOR_EACH_PTR(ptr);
1255 return NULL;
1258 static void get_ptr_names(const char *file, const char *name)
1260 char sql_filter[1024];
1261 int before, after;
1263 if (file) {
1264 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1265 file, name);
1266 } else {
1267 snprintf(sql_filter, 1024, "function = '%s';", name);
1270 before = ptr_list_size((struct ptr_list *)ptr_names);
1272 run_sql(get_ptr_name, NULL,
1273 "select distinct ptr from function_ptr where %s",
1274 sql_filter);
1276 after = ptr_list_size((struct ptr_list *)ptr_names);
1277 if (before == after)
1278 return;
1280 while ((name = get_next_ptr_name()))
1281 get_ptr_names(NULL, name);
1284 static void match_data_from_db(struct symbol *sym)
1286 struct select_caller_info_data data = { .prev_func_id = -1 };
1287 struct sm_state *sm;
1288 struct stree *stree;
1289 struct timeval end_time;
1291 if (!sym || !sym->ident)
1292 return;
1294 set_fn_mtag(sym);
1295 gettimeofday(&data.start_time, NULL);
1297 __push_fake_cur_stree();
1298 __unnullify_path();
1300 if (!__inline_fn) {
1301 char *ptr;
1303 if (sym->ctype.modifiers & MOD_STATIC)
1304 get_ptr_names(get_base_file(), sym->ident->name);
1305 else
1306 get_ptr_names(NULL, sym->ident->name);
1308 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1309 __free_ptr_list((struct ptr_list **)&ptr_names);
1310 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1311 __free_fake_cur_stree();
1312 return;
1315 sql_select_caller_info(&data,
1316 "call_id, type, parameter, key, value",
1317 sym);
1320 stree = __pop_fake_cur_stree();
1321 if (!data.ignore)
1322 merge_stree(&data.final_states, stree);
1323 free_stree(&stree);
1324 __push_fake_cur_stree();
1325 __unnullify_path();
1326 data.prev_func_id = -1;
1327 data.ignore = 0;
1328 data.results = 0;
1330 FOR_EACH_PTR(ptr_names, ptr) {
1331 run_sql(caller_info_callback, &data,
1332 "select call_id, type, parameter, key, value"
1333 " from common_caller_info where function = '%s' order by call_id",
1334 ptr);
1335 } END_FOR_EACH_PTR(ptr);
1337 if (data.results) {
1338 FOR_EACH_PTR(ptr_names, ptr) {
1339 free_string(ptr);
1340 } END_FOR_EACH_PTR(ptr);
1341 goto free_ptr_names;
1344 FOR_EACH_PTR(ptr_names, ptr) {
1345 run_sql(caller_info_callback, &data,
1346 "select call_id, type, parameter, key, value"
1347 " from caller_info where function = '%s' order by call_id",
1348 ptr);
1349 free_string(ptr);
1350 } END_FOR_EACH_PTR(ptr);
1352 free_ptr_names:
1353 __free_ptr_list((struct ptr_list **)&ptr_names);
1354 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1355 } else {
1356 sql_select_caller_info(&data,
1357 "call_id, type, parameter, key, value",
1358 sym);
1361 stree = __pop_fake_cur_stree();
1362 if (!data.ignore)
1363 merge_stree(&data.final_states, stree);
1364 free_stree(&stree);
1366 gettimeofday(&end_time, NULL);
1367 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1368 FOR_EACH_SM(data.final_states, sm) {
1369 __set_sm(sm);
1370 } END_FOR_EACH_SM(sm);
1373 free_stree(&data.final_states);
1376 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1378 struct implies_info *info = _info;
1379 struct db_implies_callback *cb;
1380 struct expression *arg = NULL;
1381 int type;
1382 int param;
1384 if (argc != 5)
1385 return 0;
1387 type = atoi(argv[1]);
1388 param = atoi(argv[2]);
1390 FOR_EACH_PTR(info->cb_list, cb) {
1391 if (cb->type != type)
1392 continue;
1393 if (param != -1) {
1394 arg = get_argument_from_call_expr(info->expr->args, param);
1395 if (!arg)
1396 continue;
1398 cb->callback(info->expr, arg, argv[3], argv[4]);
1399 } END_FOR_EACH_PTR(cb);
1401 return 0;
1404 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1406 struct implies_info *info = _info;
1407 struct db_implies_callback *cb;
1408 struct expression *arg;
1409 struct symbol *sym;
1410 char *name;
1411 int type;
1412 int param;
1414 if (argc != 5)
1415 return 0;
1417 type = atoi(argv[1]);
1418 param = atoi(argv[2]);
1420 if (!get_param(param, &name, &sym))
1421 return 0;
1422 arg = symbol_expression(sym);
1423 if (!arg)
1424 return 0;
1426 FOR_EACH_PTR(info->cb_list, cb) {
1427 if (cb->type != type)
1428 continue;
1429 cb->callback(info->expr, arg, argv[3], argv[4]);
1430 } END_FOR_EACH_PTR(cb);
1432 return 0;
1435 static void match_return_implies(struct expression *expr)
1437 struct implies_info info = {
1438 .type = RETURN_IMPLIES,
1439 .cb_list = return_implies_cb_list,
1442 if (expr->fn->type != EXPR_SYMBOL ||
1443 !expr->fn->symbol)
1444 return;
1445 info.expr = expr;
1446 info.sym = expr->fn->symbol;
1447 sql_select_implies("function, type, parameter, key, value", &info,
1448 return_implies_callbacks);
1451 static void match_call_implies(struct symbol *sym)
1453 struct implies_info info = {
1454 .type = CALL_IMPLIES,
1455 .cb_list = call_implies_cb_list,
1458 if (!sym || !sym->ident)
1459 return;
1461 info.sym = sym;
1462 sql_select_implies("function, type, parameter, key, value", &info,
1463 call_implies_callbacks);
1466 static char *get_fn_param_str(struct expression *expr)
1468 struct expression *tmp;
1469 int param;
1470 char buf[32];
1472 tmp = get_assigned_expr(expr);
1473 if (tmp)
1474 expr = tmp;
1475 expr = strip_expr(expr);
1476 if (!expr || expr->type != EXPR_CALL)
1477 return NULL;
1478 expr = strip_expr(expr->fn);
1479 if (!expr || expr->type != EXPR_SYMBOL)
1480 return NULL;
1481 param = get_param_num(expr);
1482 if (param < 0)
1483 return NULL;
1485 snprintf(buf, sizeof(buf), "[r $%d]", param);
1486 return alloc_sname(buf);
1489 static char *get_return_compare_is_param(struct expression *expr)
1491 char *var;
1492 char buf[256];
1493 int comparison;
1494 int param;
1496 param = get_param_num(expr);
1497 if (param < 0)
1498 return NULL;
1500 var = expr_to_var(expr);
1501 if (!var)
1502 return NULL;
1503 snprintf(buf, sizeof(buf), "%s orig", var);
1504 comparison = get_comparison_strings(var, buf);
1505 free_string(var);
1507 if (!comparison)
1508 return NULL;
1510 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1511 return alloc_sname(buf);
1514 static char *get_return_compare_str(struct expression *expr)
1516 char *compare_str;
1518 compare_str = get_return_compare_is_param(expr);
1519 if (compare_str)
1520 return compare_str;
1522 compare_str = expr_lte_to_param(expr, -1);
1523 if (compare_str)
1524 return compare_str;
1526 return expr_param_comparison(expr, -1);
1529 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1531 struct range_list *rl;
1532 char *return_ranges;
1533 sval_t sval;
1534 char *fn_param_str;
1535 char *compare_str;
1536 char *math_str;
1537 char buf[128];
1539 *rl_p = NULL;
1541 if (!expr)
1542 return alloc_sname("");
1544 if (get_implied_value(expr, &sval)) {
1545 sval = sval_cast(cur_func_return_type(), sval);
1546 *rl_p = alloc_rl(sval, sval);
1547 return sval_to_str_or_err_ptr(sval);
1550 fn_param_str = get_fn_param_str(expr);
1551 compare_str = expr_equal_to_param(expr, -1);
1552 math_str = get_value_in_terms_of_parameter_math(expr);
1554 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1555 rl = cast_rl(cur_func_return_type(), rl);
1556 return_ranges = show_rl(rl);
1557 } else if (get_imaginary_absolute(expr, &rl)){
1558 rl = cast_rl(cur_func_return_type(), rl);
1559 return alloc_sname(show_rl(rl));
1560 } else {
1561 get_absolute_rl(expr, &rl);
1562 rl = cast_rl(cur_func_return_type(), rl);
1563 return_ranges = show_rl(rl);
1565 *rl_p = rl;
1567 if (fn_param_str) {
1568 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1569 return alloc_sname(buf);
1571 if (compare_str) {
1572 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1573 return alloc_sname(buf);
1575 if (math_str) {
1576 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1577 return alloc_sname(buf);
1579 compare_str = get_return_compare_str(expr);
1580 if (compare_str) {
1581 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1582 return alloc_sname(buf);
1585 return return_ranges;
1588 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1590 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1593 static bool call_return_state_hooks_conditional(struct expression *expr)
1595 int final_pass_orig = final_pass;
1596 static int recurse;
1598 if (recurse >= 2)
1599 return false;
1600 if (!expr ||
1601 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1602 return false;
1604 recurse++;
1606 __push_fake_cur_stree();
1608 final_pass = 0;
1609 __split_whole_condition(expr->conditional);
1610 final_pass = final_pass_orig;
1612 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1614 __push_true_states();
1615 __use_false_states();
1617 call_return_state_hooks(expr->cond_false);
1619 __merge_true_states();
1620 __free_fake_cur_stree();
1622 recurse--;
1623 return true;
1626 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1628 struct returned_state_callback *cb;
1630 return_ranges = replace_return_ranges(return_ranges);
1631 return_id++;
1632 FOR_EACH_PTR(returned_state_callbacks, cb) {
1633 cb->callback(return_id, (char *)return_ranges, expr);
1634 } END_FOR_EACH_PTR(cb);
1637 static void call_return_state_hooks_compare(struct expression *expr)
1639 char *return_ranges;
1640 int final_pass_orig = final_pass;
1641 sval_t sval = { .type = &int_ctype };
1642 sval_t ret;
1644 if (!get_implied_value(expr, &ret))
1645 ret.value = -1;
1647 __push_fake_cur_stree();
1649 final_pass = 0;
1650 __split_whole_condition(expr);
1651 final_pass = final_pass_orig;
1653 if (ret.value != 0) {
1654 return_ranges = alloc_sname("1");
1655 sval.value = 1;
1656 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1658 call_return_states_callbacks(return_ranges, expr);
1661 __push_true_states();
1662 __use_false_states();
1664 if (ret.value != 1) {
1665 return_ranges = alloc_sname("0");
1666 sval.value = 0;
1667 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1669 call_return_states_callbacks(return_ranges, expr);
1672 __merge_true_states();
1673 __free_fake_cur_stree();
1676 static bool is_implies_function(struct expression *expr)
1678 struct range_list *rl;
1680 if (!expr)
1681 return false;
1683 rl = get_range_implications(get_function());
1684 if (!rl)
1685 return false;
1687 sm_msg("%s: is implied", __func__);
1688 return true;
1691 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1693 struct sm_state *tmp;
1695 FOR_EACH_PTR(slist, tmp) {
1696 if (strcmp(tmp->state->name, sm->state->name) == 0)
1697 return 1;
1698 } END_FOR_EACH_PTR(tmp);
1700 return 0;
1703 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1705 struct range_list *rl;
1706 char *return_ranges;
1707 struct sm_state *tmp;
1708 int ret = 0;
1709 int nr_possible, nr_states;
1710 char *compare_str;
1711 char buf[128];
1712 struct state_list *already_handled = NULL;
1713 sval_t sval;
1715 if (!sm || !sm->merged)
1716 return 0;
1718 if (too_many_possible(sm) && !is_implies_function(expr))
1719 return 0;
1721 /* bail if it gets too complicated */
1722 nr_possible = 0;
1723 FOR_EACH_PTR(sm->possible, tmp) {
1724 if (tmp->merged)
1725 continue;
1726 if (ptr_in_list(tmp, already_handled))
1727 continue;
1728 add_ptr_list(&already_handled, tmp);
1729 nr_possible++;
1730 } END_FOR_EACH_PTR(tmp);
1731 free_slist(&already_handled);
1732 nr_states = get_db_state_count();
1733 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1734 return 0;
1736 FOR_EACH_PTR(sm->possible, tmp) {
1737 if (tmp->merged)
1738 continue;
1739 if (ptr_in_list(tmp, already_handled))
1740 continue;
1741 add_ptr_list(&already_handled, tmp);
1743 ret = 1;
1744 __push_fake_cur_stree();
1746 overwrite_states_using_pool(sm, tmp);
1748 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1749 return_ranges = show_rl(rl);
1750 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1751 compare_str = get_return_compare_str(expr);
1752 /* ignore obvious stuff like 0 <= param */
1753 /* Is this worthile when we have PARAM_COMPARE? */
1754 if (compare_str &&
1755 strncmp(compare_str, "[=", 2) != 0 &&
1756 rl_to_sval(rl, &sval))
1757 compare_str = NULL;
1758 if (compare_str) {
1759 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1760 return_ranges = alloc_sname(buf);
1763 call_return_states_callbacks(return_ranges, expr);
1765 __free_fake_cur_stree();
1766 } END_FOR_EACH_PTR(tmp);
1768 free_slist(&already_handled);
1770 return ret;
1773 static int call_return_state_hooks_split_possible(struct expression *expr)
1775 struct expression *fake;
1776 struct sm_state *sm;
1778 if (!expr)
1779 return 0;
1781 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1782 if (!sm) {
1783 fake = expr_get_fake_parent_expr(expr);
1784 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1785 return 0;
1786 fake = fake->left;
1787 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1789 return split_possible_helper(sm, expr);
1792 static bool has_possible_negative(struct sm_state *sm)
1794 struct sm_state *tmp;
1796 if (!type_signed(estate_type(sm->state)))
1797 return false;
1799 FOR_EACH_PTR(sm->possible, tmp) {
1800 if (!estate_rl(tmp->state))
1801 continue;
1802 if (sval_is_negative(estate_min(tmp->state)) &&
1803 sval_is_negative(estate_max(tmp->state)))
1804 return true;
1805 } END_FOR_EACH_PTR(tmp);
1807 return false;
1810 static bool has_separate_zero_null(struct sm_state *sm)
1812 struct sm_state *tmp;
1813 sval_t sval;
1815 FOR_EACH_PTR(sm->possible, tmp) {
1816 if (!estate_get_single_value(tmp->state, &sval))
1817 continue;
1818 if (sval.value == 0)
1819 return true;
1820 } END_FOR_EACH_PTR(tmp);
1822 return false;
1825 static int split_positive_from_negative(struct expression *expr)
1827 struct sm_state *sm;
1828 struct range_list *rl;
1829 const char *return_ranges;
1830 struct range_list *ret_rl;
1831 bool separate_zero;
1832 int undo;
1834 /* We're going to print the states 3 times */
1835 if (get_db_state_count() > 10000 / 3)
1836 return 0;
1838 if (!get_implied_rl(expr, &rl) || !rl)
1839 return 0;
1840 /* Forget about INT_MAX and larger */
1841 if (rl_max(rl).value <= 0)
1842 return 0;
1843 if (!sval_is_negative(rl_min(rl)))
1844 return 0;
1846 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1847 if (!sm)
1848 return 0;
1849 if (!has_possible_negative(sm))
1850 return 0;
1851 separate_zero = has_separate_zero_null(sm);
1853 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1854 return 0;
1856 return_ranges = get_return_ranges_str(expr, &ret_rl);
1857 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1858 call_return_states_callbacks(return_ranges, expr);
1860 end_assume();
1862 if (separate_zero) {
1863 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1865 return_ranges = get_return_ranges_str(expr, &ret_rl);
1866 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1867 call_return_states_callbacks(return_ranges, expr);
1869 if (undo)
1870 end_assume();
1873 undo = assume(compare_expression(expr, '<', zero_expr()));
1875 return_ranges = get_return_ranges_str(expr, &ret_rl);
1876 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1877 call_return_states_callbacks(return_ranges, expr);
1879 if (undo)
1880 end_assume();
1882 return 1;
1885 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1887 struct range_list *rl;
1888 struct range_list *nonnull_rl;
1889 sval_t null_sval;
1890 struct range_list *null_rl = NULL;
1891 char *return_ranges;
1892 struct sm_state *sm;
1893 struct smatch_state *state;
1894 int nr_states;
1895 int final_pass_orig = final_pass;
1897 if (!expr || expr_equal_to_param(expr, -1))
1898 return 0;
1899 if (expr->type == EXPR_CALL)
1900 return 0;
1902 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1903 if (!sm)
1904 return 0;
1905 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1906 return 0;
1907 state = sm->state;
1908 if (!estate_rl(state))
1909 return 0;
1910 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1911 return 0;
1912 if (has_possible_negative(sm))
1913 return 0;
1914 if (!has_separate_zero_null(sm))
1915 return 0;
1917 nr_states = get_db_state_count();
1918 if (option_info && nr_states >= 1500)
1919 return 0;
1921 rl = estate_rl(state);
1923 __push_fake_cur_stree();
1925 final_pass = 0;
1926 __split_whole_condition(expr);
1927 final_pass = final_pass_orig;
1929 nonnull_rl = rl_filter(rl, rl_zero());
1930 return_ranges = show_rl(nonnull_rl);
1931 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1933 call_return_states_callbacks(return_ranges, expr);
1935 __push_true_states();
1936 __use_false_states();
1938 return_ranges = alloc_sname("0");
1939 null_sval = sval_type_val(rl_type(rl), 0);
1940 add_range(&null_rl, null_sval, null_sval);
1941 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1942 call_return_states_callbacks(return_ranges, expr);
1944 __merge_true_states();
1945 __free_fake_cur_stree();
1947 return 1;
1950 static bool is_kernel_success_fail(struct sm_state *sm)
1952 struct sm_state *tmp;
1953 struct range_list *rl;
1954 bool has_zero = false;
1955 bool has_neg = false;
1957 if (!type_signed(estate_type(sm->state)))
1958 return false;
1960 FOR_EACH_PTR(sm->possible, tmp) {
1961 rl = estate_rl(tmp->state);
1962 if (!rl)
1963 return false;
1964 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1965 has_zero = true;
1966 continue;
1968 has_neg = true;
1969 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1970 continue;
1971 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1972 continue;
1973 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1974 continue;
1975 return false;
1976 } END_FOR_EACH_PTR(tmp);
1978 return has_zero && has_neg;
1981 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1983 struct sm_state *sm;
1984 struct range_list *rl;
1985 struct range_list *nonzero_rl;
1986 sval_t zero_sval;
1987 struct range_list *zero_rl = NULL;
1988 int nr_states;
1989 char *return_ranges;
1990 int final_pass_orig = final_pass;
1992 if (option_project != PROJ_KERNEL)
1993 return 0;
1995 nr_states = get_db_state_count();
1996 if (nr_states > 2000)
1997 return 0;
1999 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2000 if (!sm)
2001 return 0;
2002 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2003 return 0;
2004 if (!is_kernel_success_fail(sm))
2005 return 0;
2007 rl = estate_rl(sm->state);
2008 if (!rl)
2009 return 0;
2011 __push_fake_cur_stree();
2013 final_pass = 0;
2014 __split_whole_condition(expr);
2015 final_pass = final_pass_orig;
2017 nonzero_rl = rl_filter(rl, rl_zero());
2018 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2019 return_ranges = show_rl(nonzero_rl);
2020 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2022 call_return_states_callbacks(return_ranges, expr);
2024 __push_true_states();
2025 __use_false_states();
2027 return_ranges = alloc_sname("0");
2028 zero_sval = sval_type_val(rl_type(rl), 0);
2029 add_range(&zero_rl, zero_sval, zero_sval);
2030 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2031 call_return_states_callbacks(return_ranges, expr);
2033 __merge_true_states();
2034 __free_fake_cur_stree();
2036 return 1;
2039 static int is_boolean(struct expression *expr)
2041 struct range_list *rl;
2043 if (!get_implied_rl(expr, &rl))
2044 return 0;
2045 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2046 return 1;
2047 return 0;
2050 static int splitable_function_call(struct expression *expr)
2052 struct sm_state *sm;
2054 if (!expr || expr->type != EXPR_CALL)
2055 return 0;
2056 sm = get_extra_sm_state(expr);
2057 return split_possible_helper(sm, expr);
2060 static struct sm_state *find_bool_param(void)
2062 struct stree *start_states;
2063 struct symbol *arg;
2064 struct sm_state *sm, *tmp;
2065 sval_t sval;
2067 start_states = get_start_states();
2069 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2070 if (!arg->ident)
2071 continue;
2072 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2073 if (!sm)
2074 continue;
2075 if (rl_min(estate_rl(sm->state)).value != 0 ||
2076 rl_max(estate_rl(sm->state)).value != 1)
2077 continue;
2078 goto found;
2079 } END_FOR_EACH_PTR_REVERSE(arg);
2081 return NULL;
2083 found:
2085 * Check if it's splitable. If not, then splitting it up is likely not
2086 * useful for the callers.
2088 FOR_EACH_PTR(sm->possible, tmp) {
2089 if (is_merged(tmp))
2090 continue;
2091 if (!estate_get_single_value(tmp->state, &sval))
2092 return NULL;
2093 } END_FOR_EACH_PTR(tmp);
2095 return sm;
2098 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2100 struct range_list *ret_rl;
2101 const char *return_ranges;
2102 struct sm_state *tmp;
2103 int ret = 0;
2104 struct state_list *already_handled = NULL;
2106 if (!sm || !sm->merged)
2107 return 0;
2109 if (too_many_possible(sm))
2110 return 0;
2112 FOR_EACH_PTR(sm->possible, tmp) {
2113 if (tmp->merged)
2114 continue;
2115 if (ptr_in_list(tmp, already_handled))
2116 continue;
2117 add_ptr_list(&already_handled, tmp);
2119 ret = 1;
2120 __push_fake_cur_stree();
2122 overwrite_states_using_pool(sm, tmp);
2124 return_ranges = get_return_ranges_str(expr, &ret_rl);
2125 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2126 call_return_states_callbacks(return_ranges, expr);
2128 __free_fake_cur_stree();
2129 } END_FOR_EACH_PTR(tmp);
2131 free_slist(&already_handled);
2133 return ret;
2136 static int split_by_bool_param(struct expression *expr)
2138 struct sm_state *start_sm, *sm;
2139 sval_t sval;
2141 start_sm = find_bool_param();
2142 if (!start_sm)
2143 return 0;
2144 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2145 if (!sm || estate_get_single_value(sm->state, &sval))
2146 return 0;
2148 if (get_db_state_count() * 2 >= 2000)
2149 return 0;
2151 return split_on_bool_sm(sm, expr);
2154 static int split_by_null_nonnull_param(struct expression *expr)
2156 struct symbol *arg;
2157 struct sm_state *sm;
2158 int nr_possible;
2160 /* function must only take one pointer */
2161 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2162 return 0;
2163 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2164 if (!arg->ident)
2165 return 0;
2166 if (get_real_base_type(arg)->type != SYM_PTR)
2167 return 0;
2169 if (param_was_set_var_sym(arg->ident->name, arg))
2170 return 0;
2171 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2172 if (!sm)
2173 return 0;
2175 if (!has_separate_zero_null(sm))
2176 return 0;
2178 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2179 if (get_db_state_count() * nr_possible >= 2000)
2180 return 0;
2182 return split_on_bool_sm(sm, expr);
2185 struct expression *strip_expr_statement(struct expression *expr)
2187 struct expression *orig = expr;
2188 struct statement *stmt, *last_stmt;
2190 if (!expr)
2191 return NULL;
2192 if (expr->type == EXPR_PREOP && expr->op == '(')
2193 expr = expr->unop;
2194 if (expr->type != EXPR_STATEMENT)
2195 return orig;
2196 stmt = expr->statement;
2197 if (!stmt || stmt->type != STMT_COMPOUND)
2198 return orig;
2200 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2201 if (!last_stmt || last_stmt->type == STMT_LABEL)
2202 last_stmt = last_stmt->label_statement;
2203 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2204 return orig;
2205 return strip_expr(last_stmt->expression);
2208 static bool is_kernel_error_path(struct expression *expr)
2210 struct range_list *rl;
2213 * Splitting up returns requires resources. It also requires resources
2214 * for the caller. It doesn't seem worth it to split anything up.
2216 if (!get_implied_rl(expr, &rl))
2217 return false;
2218 if (rl_type(rl) != &int_ctype)
2219 return false;
2220 if (rl_min(rl).value >= -4095 &&
2221 rl_max(rl).value < 0)
2222 return true;
2223 return false;
2226 static void call_return_state_hooks(struct expression *expr)
2228 struct range_list *ret_rl;
2229 const char *return_ranges;
2230 int nr_states;
2231 sval_t sval;
2233 if (__path_is_null())
2234 return;
2236 expr = strip_expr(expr);
2237 expr = strip_expr_statement(expr);
2239 if (is_impossible_path())
2240 goto vanilla;
2242 if (expr && (expr->type == EXPR_COMPARE ||
2243 !get_implied_value(expr, &sval)) &&
2244 (is_condition(expr) || is_boolean(expr))) {
2245 call_return_state_hooks_compare(expr);
2246 return;
2247 } else if (call_return_state_hooks_conditional(expr)) {
2248 return;
2249 } else if (is_kernel_error_path(expr)) {
2250 goto vanilla;
2251 } else if (call_return_state_hooks_split_possible(expr)) {
2252 return;
2253 } else if (split_positive_from_negative(expr)) {
2254 return;
2255 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2256 return;
2257 } else if (call_return_state_hooks_split_success_fail(expr)) {
2258 return;
2259 } else if (splitable_function_call(expr)) {
2260 return;
2261 } else if (split_by_bool_param(expr)) {
2262 } else if (split_by_null_nonnull_param(expr)) {
2263 return;
2266 vanilla:
2267 return_ranges = get_return_ranges_str(expr, &ret_rl);
2268 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2270 nr_states = get_db_state_count();
2271 if (nr_states >= 10000) {
2272 return_id++;
2273 match_return_info(return_id, (char *)return_ranges, expr);
2274 print_limited_param_set(return_id, (char *)return_ranges, expr);
2275 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2276 return;
2278 call_return_states_callbacks(return_ranges, expr);
2281 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2283 struct returned_member_callback *cb;
2284 struct sm_state *sm;
2285 struct symbol *type;
2286 char *name;
2287 char member_name[256];
2288 int len;
2290 type = get_type(expr);
2291 if (!type || type->type != SYM_PTR)
2292 return;
2293 name = expr_to_var(expr);
2294 if (!name)
2295 return;
2297 len = strlen(name);
2298 FOR_EACH_PTR(returned_member_callbacks, cb) {
2299 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2300 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2301 strcpy(member_name, "*$");
2302 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2303 continue;
2305 if (strncmp(sm->name, name, len) != 0)
2306 continue;
2307 if (strncmp(sm->name + len, "->", 2) != 0)
2308 continue;
2309 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2310 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2311 } END_FOR_EACH_SM(sm);
2312 } END_FOR_EACH_PTR(cb);
2314 free_string(name);
2317 static void print_return_struct_info(int return_id, char *return_ranges,
2318 struct expression *expr,
2319 struct symbol *sym,
2320 struct return_info_callback *cb)
2322 struct sm_state *sm;
2323 const char *printed_name;
2324 int param;
2326 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2327 if (sm->sym && sm->sym == sym) {
2328 param = -1;
2329 } else {
2330 param = get_param_num_from_sym(sm->sym);
2331 if (param < 0)
2332 continue;
2335 printed_name = get_param_name(sm);
2336 if (!printed_name)
2337 continue;
2339 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2340 } END_FOR_EACH_SM(sm);
2343 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2345 struct return_info_callback *cb;
2346 struct expression *tmp;
2347 struct symbol *sym;
2349 if (!option_info && !__inline_fn &&
2350 !local_debug && !option_debug)
2351 return;
2353 tmp = get_fake_variable(expr);
2354 if (tmp)
2355 expr = tmp;
2356 sym = expr_to_sym(expr);
2358 FOR_EACH_PTR(return_callbacks, cb) {
2359 __ignore_param_used++;
2360 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2361 __ignore_param_used--;
2362 } END_FOR_EACH_PTR(cb);
2365 static void reset_memdb(struct symbol *sym)
2367 mem_sql(NULL, NULL, "delete from caller_info;");
2368 mem_sql(NULL, NULL, "delete from return_states;");
2369 mem_sql(NULL, NULL, "delete from call_implies;");
2370 mem_sql(NULL, NULL, "delete from return_implies;");
2373 static void match_end_func_info(struct symbol *sym)
2375 if (__path_is_null())
2376 return;
2377 call_return_state_hooks(NULL);
2380 static void match_after_func(struct symbol *sym)
2382 if (!__inline_fn)
2383 reset_memdb(sym);
2386 static void init_memdb(void)
2388 char *err = NULL;
2389 int rc;
2390 const char *schema_files[] = {
2391 "db/db.schema",
2392 "db/caller_info.schema",
2393 "db/common_caller_info.schema",
2394 "db/return_states.schema",
2395 "db/function_type_size.schema",
2396 "db/type_size.schema",
2397 "db/function_type_info.schema",
2398 "db/type_info.schema",
2399 "db/call_implies.schema",
2400 "db/return_implies.schema",
2401 "db/function_ptr.schema",
2402 "db/local_values.schema",
2403 "db/function_type_value.schema",
2404 "db/type_value.schema",
2405 "db/function_type.schema",
2406 "db/data_info.schema",
2407 "db/parameter_name.schema",
2408 "db/constraints.schema",
2409 "db/constraints_required.schema",
2410 "db/fn_ptr_data_link.schema",
2411 "db/fn_data_link.schema",
2412 "db/mtag_about.schema",
2413 "db/mtag_info.schema",
2414 "db/mtag_map.schema",
2415 "db/mtag_data.schema",
2416 "db/mtag_alias.schema",
2418 static char buf[4096];
2419 int fd;
2420 int ret;
2421 int i;
2423 rc = sqlite3_open(":memory:", &mem_db);
2424 if (rc != SQLITE_OK) {
2425 sm_ierror("starting In-Memory database.");
2426 return;
2429 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2430 fd = open_schema_file(schema_files[i]);
2431 if (fd < 0)
2432 continue;
2433 ret = read(fd, buf, sizeof(buf));
2434 if (ret < 0) {
2435 sm_ierror("failed to read: %s", schema_files[i]);
2436 continue;
2438 close(fd);
2439 if (ret == sizeof(buf)) {
2440 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2441 schema_files[i], sizeof(buf));
2442 continue;
2444 buf[ret] = '\0';
2445 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2446 if (rc != SQLITE_OK) {
2447 sm_ierror("SQL error #2: %s", err);
2448 sm_ierror("%s", buf);
2453 static void init_cachedb(void)
2455 char *err = NULL;
2456 int rc;
2457 const char *schema_files[] = {
2458 "db/call_implies.schema",
2459 "db/return_implies.schema",
2460 "db/type_info.schema",
2461 "db/mtag_about.schema",
2462 "db/mtag_data.schema",
2463 "db/mtag_info.schema",
2464 "db/sink_info.schema",
2466 static char buf[4096];
2467 int fd;
2468 int ret;
2469 int i;
2471 rc = sqlite3_open(":memory:", &cache_db);
2472 if (rc != SQLITE_OK) {
2473 sm_ierror("starting In-Memory database.");
2474 return;
2477 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2478 fd = open_schema_file(schema_files[i]);
2479 if (fd < 0)
2480 continue;
2481 ret = read(fd, buf, sizeof(buf));
2482 if (ret < 0) {
2483 sm_ierror("failed to read: %s", schema_files[i]);
2484 continue;
2486 close(fd);
2487 if (ret == sizeof(buf)) {
2488 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2489 schema_files[i], sizeof(buf));
2490 continue;
2492 buf[ret] = '\0';
2493 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2494 if (rc != SQLITE_OK) {
2495 sm_ierror("SQL error #2: %s", err);
2496 sm_ierror("%s", buf);
2501 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2503 static char buf[4096];
2504 char tmp[256];
2505 char *p = buf;
2506 char *table = _table;
2507 int i;
2510 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2511 for (i = 0; i < argc; i++) {
2512 if (i)
2513 p += snprintf(p, 4096 - (p - buf), ", ");
2514 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2515 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2518 p += snprintf(p, 4096 - (p - buf), ");");
2519 if (p - buf > 4096)
2520 return 0;
2522 sm_msg("SQL: %s", buf);
2523 return 0;
2526 static void dump_cache(struct symbol_list *sym_list)
2528 const char *cache_tables[] = {
2529 "type_info", "return_implies", "call_implies", "mtag_data",
2530 "mtag_info", "mtag_about", "sink_info",
2532 char buf[64];
2533 int i;
2535 if (!option_info)
2536 return;
2538 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2539 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2540 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2544 void open_smatch_db(char *db_file)
2546 int rc;
2548 if (option_no_db)
2549 return;
2551 use_states = malloc(num_checks + 1);
2552 memset(use_states, 0xff, num_checks + 1);
2554 init_memdb();
2555 init_cachedb();
2557 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2558 if (rc != SQLITE_OK) {
2559 option_no_db = 1;
2560 return;
2562 run_sql(NULL, NULL,
2563 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2564 return;
2567 static char *get_next_string(char **str)
2569 static char string[256];
2570 char *start;
2571 char *p = *str;
2572 int len, i, j;
2574 if (*p == '\0')
2575 return NULL;
2576 start = p;
2578 while (*p != '\0' && *p != '\n') {
2579 if (*p == '\\' && *(p + 1) == ' ') {
2580 p += 2;
2581 continue;
2583 if (*p == ' ')
2584 break;
2585 p++;
2588 len = p - start;
2589 if (len >= sizeof(string)) {
2590 memcpy(string, start, sizeof(string));
2591 string[sizeof(string) - 1] = '\0';
2592 sm_ierror("return_fix: '%s' too long", string);
2593 **str = '\0';
2594 return NULL;
2596 memcpy(string, start, len);
2597 string[len] = '\0';
2598 for (i = 0; i < sizeof(string) - 1; i++) {
2599 if (string[i] == '\\' && string[i + 1] == ' ') {
2600 for (j = i; string[j] != '\0'; j++)
2601 string[j] = string[j + 1];
2604 if (*p != '\0')
2605 p++;
2606 *str = p;
2607 return string;
2610 static void register_return_replacements(void)
2612 char *func, *orig, *new;
2613 char filename[256];
2614 char buf[4096];
2615 int fd, ret, i;
2616 char *p;
2618 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2619 fd = open_schema_file(filename);
2620 if (fd < 0)
2621 return;
2622 ret = read(fd, buf, sizeof(buf));
2623 close(fd);
2624 if (ret < 0)
2625 return;
2626 if (ret == sizeof(buf)) {
2627 sm_ierror("file too large: %s (limit %zd bytes)",
2628 filename, sizeof(buf));
2629 return;
2631 buf[ret] = '\0';
2633 p = buf;
2634 while (*p) {
2635 get_next_string(&p);
2636 replace_count++;
2638 if (replace_count == 0 || replace_count % 3 != 0) {
2639 replace_count = 0;
2640 return;
2642 replace_table = malloc(replace_count * sizeof(char *));
2644 p = buf;
2645 i = 0;
2646 while (*p) {
2647 func = alloc_string(get_next_string(&p));
2648 orig = alloc_string(get_next_string(&p));
2649 new = alloc_string(get_next_string(&p));
2651 replace_table[i++] = func;
2652 replace_table[i++] = orig;
2653 replace_table[i++] = new;
2657 void register_definition_db_callbacks(int id)
2659 my_id = id;
2661 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2662 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2663 add_split_return_callback(match_return_info);
2664 add_split_return_callback(print_returned_struct_members);
2665 add_split_return_callback(print_return_info);
2666 add_hook(&call_return_state_hooks, RETURN_HOOK);
2667 add_hook(&match_end_func_info, END_FUNC_HOOK);
2668 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2670 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2671 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2672 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2674 common_funcs = load_strings_from_file(option_project_str, "common_functions");
2675 register_return_replacements();
2677 add_hook(&dump_cache, END_FILE_HOOK);
2680 void register_db_call_marker(int id)
2682 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2685 char *get_data_info_name(struct expression *expr)
2687 struct symbol *sym;
2688 char *name;
2689 char buf[256];
2690 char *ret = NULL;
2692 expr = strip_expr(expr);
2693 name = get_member_name(expr);
2694 if (name)
2695 return name;
2696 name = expr_to_var_sym(expr, &sym);
2697 if (!name || !sym)
2698 goto free;
2699 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2700 goto free;
2701 if (sym->ctype.modifiers & MOD_STATIC)
2702 snprintf(buf, sizeof(buf), "static %s", name);
2703 else
2704 snprintf(buf, sizeof(buf), "global %s", name);
2705 ret = alloc_sname(buf);
2706 free:
2707 free_string(name);
2708 return ret;