validation: delete obsolete dev_hold() check
[smatch.git] / smatch_db.c
blob7189c275145ac59e9165c621d117e80c72099554
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 is_local(struct symbol *sym)
245 if (sym->ctype.modifiers & MOD_STATIC)
246 return true;
247 if ((sym->ctype.modifiers & MOD_EXTERN) &&
248 (sym->ctype.modifiers & MOD_INLINE))
249 return true;
251 if (!sym->definition)
252 return false;
254 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
255 (sym->definition->ctype.modifiers & MOD_INLINE))
256 return true;
258 return false;
261 void db_ignore_states(int id)
263 use_states[id] = 0;
266 unsigned long long __fn_mtag;
267 static void set_fn_mtag(struct symbol *sym)
269 char buf[128];
271 if (is_local(cur_func_sym))
272 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
273 else
274 snprintf(buf, sizeof(buf), "extern %s", get_function());
276 __fn_mtag = str_to_mtag(buf);
279 void sql_insert_return_states(int return_id, const char *return_ranges,
280 int type, int param, const char *key, const char *value)
282 unsigned long long id;
285 if (key && strlen(key) >= 80)
286 return;
287 if (__inline_fn)
288 id = (unsigned long)__inline_fn;
289 else
290 id = __fn_mtag;
292 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
293 get_base_file(), get_function(), id, return_id,
294 return_ranges, fn_static(), type, param, key, value);
297 static struct string_list *common_funcs;
298 static int is_common_function(const char *fn)
300 char *tmp;
302 if (!fn)
303 return 0;
305 if (strncmp(fn, "__builtin_", 10) == 0)
306 return 1;
308 FOR_EACH_PTR(common_funcs, tmp) {
309 if (strcmp(tmp, fn) == 0)
310 return 1;
311 } END_FOR_EACH_PTR(tmp);
313 return 0;
316 static char *function_signature(void)
318 return type_to_str(get_real_base_type(cur_func_sym));
321 void sql_insert_caller_info(struct expression *call, int type,
322 int param, const char *key, const char *value)
324 FILE *tmp_fd = sm_outfd;
325 char *fn;
327 if (!option_info && !__inline_call)
328 return;
329 if (unreachable())
330 return;
332 if (key && strlen(key) >= 80)
333 return;
335 fn = get_fnptr_name(call->fn);
336 if (!fn)
337 return;
339 if (__inline_call) {
340 mem_sql(NULL, NULL,
341 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
342 get_base_file(), get_function(), fn, (unsigned long)call,
343 is_static(call->fn), type, param, key, value);
346 if (!option_info)
347 return;
349 if (strncmp(fn, "__builtin_", 10) == 0)
350 return;
351 if (type != INTERNAL && is_common_function(fn))
352 return;
354 sm_outfd = caller_info_fd;
355 sm_msg("SQL_caller_info: insert into caller_info values ("
356 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
357 get_base_file(), get_function(), fn, is_static(call->fn),
358 type, param, key, value);
359 sm_outfd = tmp_fd;
361 free_string(fn);
364 void sql_insert_function_ptr(const char *fn, const char *struct_name)
366 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
367 get_base_file(), fn, struct_name);
370 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
372 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
373 get_base_file(), get_function(), (unsigned long)__inline_fn,
374 fn_static(), type, param, key, value);
377 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
379 sql_insert_or_ignore(call_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_function_type_size(const char *member, const char *ranges)
386 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
389 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
391 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
394 void sql_insert_type_info(int type, const char *member, const char *value)
396 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
399 void sql_insert_local_values(const char *name, const char *value)
401 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
404 void sql_insert_function_type_value(const char *type, const char *value)
406 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
409 void sql_insert_function_type(int param, const char *value)
411 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
412 get_base_file(), get_function(), fn_static(), param, value);
415 void sql_insert_parameter_name(int param, const char *value)
417 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
418 get_base_file(), get_function(), fn_static(), param, value);
421 void sql_insert_data_info(struct expression *data, int type, const char *value)
423 char *data_name;
425 data_name = get_data_info_name(data);
426 if (!data_name)
427 return;
428 sql_insert(data_info, "'%s', '%s', %d, '%s'",
429 is_static(data) ? get_base_file() : "extern",
430 data_name, type, value);
433 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
435 sql_insert(data_info, "'%s', '%s', %d, '%s'",
436 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
437 var, type, value);
440 void sql_save_constraint(const char *con)
442 if (!option_info)
443 return;
445 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
448 void sql_save_constraint_required(const char *data, int op, const char *limit)
450 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
453 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
455 if (!option_info)
456 return;
458 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
459 "select constraints_required.data, constraints_required.op, '%s' from "
460 "constraints_required where bound = '%s';", new_limit, old_limit);
463 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
465 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
468 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
470 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
471 return;
473 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
474 is_local(fn->symbol) ? get_base_file() : "extern",
475 fn->symbol->ident->name,
476 is_local(fn->symbol),
477 type, param, key, value);
480 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
482 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
483 tag, get_filename(), get_function(), get_lineno(),
484 left_name, right_name);
487 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
489 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
492 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
494 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
497 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
499 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
502 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
504 mtag_t *saved_tag = _tag;
505 mtag_t new_tag;
507 new_tag = strtoll(argv[0], NULL, 10);
509 if (!*saved_tag)
510 *saved_tag = new_tag;
511 else if (*saved_tag != new_tag)
512 *saved_tag = -1ULL;
514 return 0;
517 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
519 mtag_t tmp = 0;
521 run_sql(save_mtag, &tmp,
522 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
523 tag, container_offset);
525 if (tmp == 0 || tmp == -1ULL)
526 return 0;
527 *container = tmp;
528 return 1;
531 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
533 mtag_t tmp = 0;
535 run_sql(save_mtag, &tmp,
536 "select tag from mtag_map where container = %lld and container_offset = %d;",
537 container, offset);
539 if (tmp == 0 || tmp == -1ULL)
540 return 0;
541 *tag = tmp;
542 return 1;
545 char *get_static_filter(struct symbol *sym)
547 static char sql_filter[1024];
549 /* This can only happen on buggy code. Return invalid SQL. */
550 if (!sym) {
551 sql_filter[0] = '\0';
552 return sql_filter;
555 if (is_local(sym)) {
556 snprintf(sql_filter, sizeof(sql_filter),
557 "file = '%s' and function = '%s' and static = '1'",
558 get_base_file(), sym->ident->name);
559 } else {
560 snprintf(sql_filter, sizeof(sql_filter),
561 "function = '%s' and static = '0'", sym->ident->name);
564 return sql_filter;
567 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
569 int *row_count = _row_count;
571 *row_count = 0;
572 if (argc != 1)
573 return 0;
574 *row_count = atoi(argv[0]);
575 return 0;
578 static void mark_call_params_untracked(struct expression *call)
580 struct expression *arg;
581 int i = 0;
583 FOR_EACH_PTR(call->args, arg) {
584 mark_untracked(call, i++, "$", NULL);
585 } END_FOR_EACH_PTR(arg);
588 static void sql_select_return_states_pointer(const char *cols,
589 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
591 char *ptr;
592 int return_count = 0;
594 ptr = get_fnptr_name(call->fn);
595 if (!ptr)
596 return;
598 run_sql(get_row_count, &return_count,
599 "select count(*) from return_states join function_ptr "
600 "where return_states.function == function_ptr.function and "
601 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
602 /* The magic number 100 is just from testing on the kernel. */
603 if (return_count > 100) {
604 mark_call_params_untracked(call);
605 return;
608 run_sql(callback, info,
609 "select %s from return_states join function_ptr where "
610 "return_states.function == function_ptr.function and ptr = '%s' "
611 "and searchable = 1 "
612 "order by function_ptr.file, return_states.file, return_id, type;",
613 cols, ptr);
616 static int is_local_symbol(struct expression *expr)
618 if (expr->type != EXPR_SYMBOL)
619 return 0;
620 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
621 return 0;
622 return 1;
625 bool is_fn_ptr(struct expression *fn)
627 fn = strip_expr(fn);
628 if (fn->type != EXPR_SYMBOL)
629 return true;
630 if (!fn->symbol)
631 return true;
632 if (is_local_symbol(fn))
633 return true;
634 return false;
637 void sql_select_return_states(const char *cols, struct expression *call,
638 int (*callback)(void*, int, char**, char**), void *info)
640 struct expression *fn;
641 int row_count = 0;
643 if (is_fake_call(call))
644 return;
646 fn = strip_expr(call->fn);
647 if (is_fn_ptr(fn)) {
648 sql_select_return_states_pointer(cols, call, callback, info);
649 return;
652 if (inlinable(fn)) {
653 mem_sql(callback, info,
654 "select %s from return_states where call_id = '%lu' order by return_id, type;",
655 cols, (unsigned long)call);
656 return;
659 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
660 get_static_filter(fn->symbol));
661 if (row_count == 0 && fn->symbol && fn->symbol->definition)
662 set_state(my_id, "db_incomplete", NULL, &incomplete);
663 if (row_count > 3000)
664 return;
666 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
667 cols, get_static_filter(fn->symbol));
670 bool db_incomplete(void)
672 return !!get_state(my_id, "db_incomplete", NULL);
675 #define CALL_IMPLIES 0
676 #define RETURN_IMPLIES 1
678 struct implies_info {
679 int type;
680 struct db_implies_cb_list *cb_list;
681 struct expression *expr;
682 struct symbol *sym;
685 void sql_select_implies(const char *cols, struct implies_info *info,
686 int (*callback)(void*, int, char**, char**))
688 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
689 mem_sql(callback, info,
690 "select %s from return_implies where call_id = '%lu';",
691 cols, (unsigned long)info->expr);
692 return;
695 run_sql(callback, info, "select %s from %s_implies where %s;",
696 cols,
697 info->type == CALL_IMPLIES ? "call" : "return",
698 get_static_filter(info->sym));
701 struct select_caller_info_data {
702 struct stree *final_states;
703 struct timeval start_time;
704 int prev_func_id;
705 int ignore;
706 int results;
709 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
711 static void sql_select_caller_info(struct select_caller_info_data *data,
712 const char *cols, struct symbol *sym)
714 if (__inline_fn) {
715 mem_sql(caller_info_callback, data,
716 "select %s from caller_info where call_id = %lu;",
717 cols, (unsigned long)__inline_fn);
718 return;
721 if (sym->ident->name && is_common_function(sym->ident->name))
722 return;
723 run_sql(caller_info_callback, data,
724 "select %s from common_caller_info where %s order by call_id;",
725 cols, get_static_filter(sym));
726 if (data->results)
727 return;
729 run_sql(caller_info_callback, data,
730 "select %s from caller_info where %s order by call_id;",
731 cols, get_static_filter(sym));
734 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
736 struct def_callback *def_callback = __alloc_def_callback(0);
738 def_callback->hook_type = type;
739 def_callback->callback = callback;
740 add_ptr_list(&select_caller_info_callbacks, def_callback);
743 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
745 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
747 callback->hook_type = type;
748 callback->callback = fn;
749 add_ptr_list(&select_caller_name_sym_callbacks, callback);
753 * These call backs are used when the --info option is turned on to print struct
754 * member information. For example foo->bar could have a state in
755 * smatch_extra.c and also check_user.c.
757 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
759 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
761 member_callback->owner = owner;
762 member_callback->callback = callback;
763 add_ptr_list(&member_callbacks, member_callback);
766 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
768 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
770 member_callback->owner = owner;
771 member_callback->callback = callback;
772 add_ptr_list(&member_callbacks_new, member_callback);
775 void add_return_info_callback(int owner,
776 void (*callback)(int return_id, char *return_ranges,
777 struct expression *returned_expr,
778 int param,
779 const char *printed_name,
780 struct sm_state *sm))
782 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
784 return_callback->owner = owner;
785 return_callback->callback = callback;
786 add_ptr_list(&return_callbacks, return_callback);
789 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
791 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
793 callback->callback = fn;
794 add_ptr_list(&returned_state_callbacks, callback);
797 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))
799 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
801 member_callback->owner = owner;
802 member_callback->callback = callback;
803 add_ptr_list(&returned_member_callbacks, member_callback);
806 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
808 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
810 cb->type = type;
811 cb->callback = callback;
812 add_ptr_list(&call_implies_cb_list, cb);
815 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
817 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
819 cb->type = type;
820 cb->callback = callback;
821 add_ptr_list(&return_implies_cb_list, cb);
824 struct return_info {
825 struct expression *static_returns_call;
826 struct symbol *return_type;
827 struct range_list *return_range_list;
830 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
832 struct return_info *ret_info = _ret_info;
833 struct range_list *rl;
834 struct expression *call_expr = ret_info->static_returns_call;
836 if (argc != 1)
837 return 0;
838 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
839 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
840 return 0;
843 struct range_list *db_return_vals(struct expression *expr)
845 struct return_info ret_info = {};
846 struct sm_state *sm;
848 if (is_fake_call(expr))
849 return NULL;
851 sm = get_extra_sm_state(expr);
852 if (sm)
853 return clone_rl(estate_rl(sm->state));
854 ret_info.static_returns_call = expr;
855 ret_info.return_type = get_type(expr);
856 if (!ret_info.return_type)
857 return NULL;
859 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
860 return NULL;
862 ret_info.return_range_list = NULL;
863 if (inlinable(expr->fn)) {
864 mem_sql(db_return_callback, &ret_info,
865 "select distinct return from return_states where call_id = '%lu';",
866 (unsigned long)expr);
867 } else {
868 run_sql(db_return_callback, &ret_info,
869 "select distinct return from return_states where %s;",
870 get_static_filter(expr->fn->symbol));
872 return ret_info.return_range_list;
875 struct range_list *db_return_vals_from_str(const char *fn_name)
877 struct return_info ret_info;
879 ret_info.static_returns_call = NULL;
880 ret_info.return_type = &llong_ctype;
881 ret_info.return_range_list = NULL;
883 run_sql(db_return_callback, &ret_info,
884 "select distinct return from return_states where function = '%s';",
885 fn_name);
886 return ret_info.return_range_list;
890 * This is used when we have a function that takes a function pointer as a
891 * parameter. "frob(blah, blah, my_function);" We know that the return values
892 * from frob() come from my_funcion() so we want to find the possible returns
893 * of my_function(), but we don't know which arguments are passed to it.
896 struct range_list *db_return_vals_no_args(struct expression *expr)
898 struct return_info ret_info = {};
900 if (!expr || expr->type != EXPR_SYMBOL)
901 return NULL;
903 ret_info.static_returns_call = expr;
904 ret_info.return_type = get_type(expr);
905 ret_info.return_type = get_real_base_type(ret_info.return_type);
906 if (!ret_info.return_type)
907 return NULL;
909 run_sql(db_return_callback, &ret_info,
910 "select distinct return from return_states where %s;",
911 get_static_filter(expr->symbol));
913 return ret_info.return_range_list;
916 static void match_call_marker(struct expression *expr)
918 struct symbol *type;
920 type = get_type(expr->fn);
921 if (type && type->type == SYM_PTR)
922 type = get_real_base_type(type);
925 * we just want to record something in the database so that if we have
926 * two calls like: frob(4); frob(some_unkown); then on the receiving
927 * side we know that sometimes frob is called with unknown parameters.
930 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
933 int is_recursive_member(const char *name)
935 char buf[256];
936 const char *p, *next;
937 int size;
939 p = strchr(name, '>');
940 if (!p)
941 return 0;
942 p++;
943 while (true) {
944 next = strchr(p, '>');
945 if (!next)
946 return 0;
947 next++;
949 size = next - p;
950 if (size >= sizeof(buf))
951 return 0;
952 memcpy(buf, p, size);
953 buf[size] = '\0';
954 if (strstr(next, buf))
955 return 1;
956 p = next;
960 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
962 struct symbol *sym;
963 const char *sm_name;
964 char *name;
965 bool is_address = false;
966 bool add_star = false;
967 char buf[256];
968 char *ret = NULL;
969 int len;
971 expr = strip_expr(expr);
972 if (!expr)
973 return NULL;
975 if (expr->type == EXPR_PREOP && expr->op == '&') {
976 expr = strip_expr(expr->unop);
977 is_address = true;
980 name = expr_to_var_sym(expr, &sym);
981 if (!name || !sym)
982 goto free;
983 if (sym != sm->sym)
984 goto free;
986 sm_name = sm->name;
987 add_star = false;
988 if (sm_name[0] == '*') {
989 add_star = true;
990 sm_name++;
993 len = strlen(name);
994 if (strncmp(name, sm_name, len) != 0)
995 goto free;
996 if (sm_name[len] == '\0') {
997 snprintf(buf, sizeof(buf), "%s%s$",
998 add_star ? "*" : "", is_address ? "*" : "");
999 } else {
1000 if (sm_name[len] != '.' && sm_name[len] != '-')
1001 goto free;
1002 if (sm_name[len] == '-')
1003 len++;
1004 // FIXME does is_address really imply that sm_name[len] == '-'
1005 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
1006 sm_name + len);
1009 ret = alloc_sname(buf);
1010 free:
1011 free_string(name);
1012 return ret;
1015 static void print_struct_members(struct expression *call, struct expression *expr, int param,
1016 int owner,
1017 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
1018 bool new)
1020 struct sm_state *sm;
1021 const char *sm_name;
1022 char *name;
1023 struct symbol *sym;
1024 int len;
1025 char printed_name[256];
1026 int is_address = 0;
1027 bool add_star;
1028 struct symbol *type;
1030 expr = strip_expr(expr);
1031 if (!expr)
1032 return;
1033 type = get_type(expr);
1034 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1035 return;
1037 if (expr->type == EXPR_PREOP && expr->op == '&') {
1038 expr = strip_expr(expr->unop);
1039 is_address = 1;
1042 name = expr_to_var_sym(expr, &sym);
1043 if (!name || !sym)
1044 goto free;
1046 len = strlen(name);
1047 FOR_EACH_SM(__get_cur_stree(), sm) {
1048 if (sm->owner != owner || sm->sym != sym)
1049 continue;
1050 sm_name = sm->name;
1051 add_star = false;
1052 if (sm_name[0] == '*') {
1053 add_star = true;
1054 sm_name++;
1056 // FIXME: simplify?
1057 if (!add_star && strcmp(name, sm_name) == 0) {
1058 if (is_address) {
1059 snprintf(printed_name, sizeof(printed_name), "*$");
1060 } else {
1061 if (new)
1062 snprintf(printed_name, sizeof(printed_name), "$");
1063 else
1064 continue;
1066 } else if (add_star && strcmp(name, sm_name) == 0) {
1067 snprintf(printed_name, sizeof(printed_name), "%s*$",
1068 is_address ? "*" : "");
1069 } else if (strncmp(name, sm_name, len) == 0) {
1070 if (sm_name[len] != '.' && sm_name[len] != '-')
1071 continue;
1072 if (is_address && sm_name[len] == '.') {
1073 snprintf(printed_name, sizeof(printed_name),
1074 "%s$->%s", add_star ? "*" : "",
1075 sm_name + len + 1);
1076 } else if (is_address && sm_name[len] == '-') {
1077 snprintf(printed_name, sizeof(printed_name),
1078 "%s(*$)%s", add_star ? "*" : "",
1079 sm_name + len);
1080 } else {
1081 snprintf(printed_name, sizeof(printed_name),
1082 "%s$%s", add_star ? "*" : "",
1083 sm_name + len);
1085 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1086 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1087 continue;
1088 if (is_address && sm_name[len + 1] == '.') {
1089 snprintf(printed_name, sizeof(printed_name),
1090 "&%s$->%s", add_star ? "*" : "",
1091 sm_name + len + 2);
1092 } else if (is_address && sm_name[len] == '-') {
1093 snprintf(printed_name, sizeof(printed_name),
1094 "&%s(*$)%s", add_star ? "*" : "",
1095 sm_name + len + 1);
1096 } else {
1097 snprintf(printed_name, sizeof(printed_name),
1098 "&%s$%s", add_star ? "*" : "",
1099 sm_name + len + 1);
1101 } else {
1102 continue;
1104 if (is_recursive_member(printed_name))
1105 continue;
1106 callback(call, param, printed_name, sm);
1107 } END_FOR_EACH_SM(sm);
1108 free:
1109 free_string(name);
1112 static void match_call_info(struct expression *call)
1114 struct member_info_callback *cb;
1115 struct expression *arg;
1116 int i;
1118 FOR_EACH_PTR(member_callbacks, cb) {
1119 i = -1;
1120 FOR_EACH_PTR(call->args, arg) {
1121 i++;
1122 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1123 } END_FOR_EACH_PTR(arg);
1124 } END_FOR_EACH_PTR(cb);
1127 static struct expression *get_fake_variable(struct expression *expr)
1129 struct expression *tmp;
1131 tmp = expr_get_fake_parent_expr(expr);
1132 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1133 return NULL;
1135 return tmp->left;
1138 static void match_call_info_new(struct expression *call)
1140 struct member_info_callback *cb;
1141 struct expression *arg, *tmp;
1142 int i;
1144 if (!option_info && !__inline_call && !local_debug)
1145 return;
1147 FOR_EACH_PTR(member_callbacks_new, cb) {
1148 i = -1;
1149 FOR_EACH_PTR(call->args, arg) {
1150 i++;
1151 tmp = get_fake_variable(arg);
1152 if (!tmp)
1153 tmp = arg;
1154 __ignore_param_used++;
1155 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1156 __ignore_param_used--;
1157 } END_FOR_EACH_PTR(arg);
1158 } END_FOR_EACH_PTR(cb);
1161 static int get_param(int param, char **name, struct symbol **sym)
1163 struct symbol *arg;
1164 int i;
1166 i = 0;
1167 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1169 * this is a temporary hack to work around a bug (I think in sparse?)
1170 * 2.6.37-rc1:fs/reiserfs/journal.o
1171 * If there is a function definition without parameter name found
1172 * after a function implementation then it causes a crash.
1173 * int foo() {}
1174 * int bar(char *);
1176 if (arg->ident->name < (char *)100)
1177 continue;
1178 if (i == param) {
1179 *name = arg->ident->name;
1180 *sym = arg;
1181 return TRUE;
1183 i++;
1184 } END_FOR_EACH_PTR(arg);
1186 return FALSE;
1189 static int function_signature_matches(const char *sig)
1191 char *my_sig;
1193 my_sig = function_signature();
1194 if (!sig || !my_sig)
1195 return 1; /* default to matching */
1196 if (strcmp(my_sig, sig) == 0)
1197 return 1;
1198 return 0;
1201 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1203 struct select_caller_info_data *data = _data;
1204 int func_id;
1205 long type;
1206 long param;
1207 char *key;
1208 char *value;
1209 char *name = NULL;
1210 struct symbol *sym = NULL;
1211 struct def_callback *def_callback;
1212 struct def_name_sym_callback *ns_callback;
1213 struct stree *stree;
1214 struct timeval cur_time;
1215 char fullname[256];
1216 char *p;
1218 data->results = 1;
1220 if (argc != 5)
1221 return 0;
1223 gettimeofday(&cur_time, NULL);
1224 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1225 return 0;
1227 func_id = atoi(argv[0]);
1228 errno = 0;
1229 type = strtol(argv[1], NULL, 10);
1230 param = strtol(argv[2], NULL, 10);
1231 if (errno)
1232 return 0;
1233 key = argv[3];
1234 value = argv[4];
1236 if (data->prev_func_id == -1)
1237 data->prev_func_id = func_id;
1238 if (func_id != data->prev_func_id) {
1239 stree = __pop_fake_cur_stree();
1240 if (!data->ignore)
1241 merge_stree(&data->final_states, stree);
1242 free_stree(&stree);
1243 __push_fake_cur_stree();
1244 __unnullify_path();
1245 data->prev_func_id = func_id;
1246 data->ignore = 0;
1249 if (data->ignore)
1250 return 0;
1251 if (type == INTERNAL &&
1252 !function_signature_matches(value)) {
1253 data->ignore = 1;
1254 return 0;
1257 if (param >= 0 && !get_param(param, &name, &sym))
1258 return 0;
1260 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1261 if (def_callback->hook_type == type)
1262 def_callback->callback(name, sym, key, value);
1263 } END_FOR_EACH_PTR(def_callback);
1265 p = strchr(key, '$');
1266 if (name && p)
1267 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1268 else
1269 snprintf(fullname, sizeof(fullname), "%s", key);
1271 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1272 if (ns_callback->hook_type == type)
1273 ns_callback->callback(fullname, sym, value);
1274 } END_FOR_EACH_PTR(ns_callback);
1276 return 0;
1279 static struct string_list *ptr_names_done;
1280 static struct string_list *ptr_names;
1282 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1284 insert_string(&ptr_names, alloc_string(argv[0]));
1285 return 0;
1288 static char *get_next_ptr_name(void)
1290 char *ptr;
1292 FOR_EACH_PTR(ptr_names, ptr) {
1293 if (!insert_string(&ptr_names_done, ptr))
1294 continue;
1295 return ptr;
1296 } END_FOR_EACH_PTR(ptr);
1297 return NULL;
1300 static void get_ptr_names(const char *file, const char *name)
1302 char sql_filter[1024];
1303 int before, after;
1305 if (file) {
1306 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1307 file, name);
1308 } else {
1309 snprintf(sql_filter, 1024, "function = '%s';", name);
1312 before = ptr_list_size((struct ptr_list *)ptr_names);
1314 run_sql(get_ptr_name, NULL,
1315 "select distinct ptr from function_ptr where %s",
1316 sql_filter);
1318 after = ptr_list_size((struct ptr_list *)ptr_names);
1319 if (before == after)
1320 return;
1322 while ((name = get_next_ptr_name()))
1323 get_ptr_names(NULL, name);
1326 static void match_data_from_db(struct symbol *sym)
1328 struct select_caller_info_data data = { .prev_func_id = -1 };
1329 struct sm_state *sm;
1330 struct stree *stree;
1331 struct timeval end_time;
1333 if (!sym || !sym->ident)
1334 return;
1336 set_fn_mtag(sym);
1337 gettimeofday(&data.start_time, NULL);
1339 __push_fake_cur_stree();
1340 __unnullify_path();
1342 if (!__inline_fn) {
1343 char *ptr;
1345 if (sym->ctype.modifiers & MOD_STATIC)
1346 get_ptr_names(get_base_file(), sym->ident->name);
1347 else
1348 get_ptr_names(NULL, sym->ident->name);
1350 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1351 __free_ptr_list((struct ptr_list **)&ptr_names);
1352 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1353 __free_fake_cur_stree();
1354 return;
1357 sql_select_caller_info(&data,
1358 "call_id, type, parameter, key, value",
1359 sym);
1362 stree = __pop_fake_cur_stree();
1363 if (!data.ignore)
1364 merge_stree(&data.final_states, stree);
1365 free_stree(&stree);
1366 __push_fake_cur_stree();
1367 __unnullify_path();
1368 data.prev_func_id = -1;
1369 data.ignore = 0;
1370 data.results = 0;
1372 FOR_EACH_PTR(ptr_names, ptr) {
1373 run_sql(caller_info_callback, &data,
1374 "select call_id, type, parameter, key, value"
1375 " from common_caller_info where function = '%s' order by call_id",
1376 ptr);
1377 } END_FOR_EACH_PTR(ptr);
1379 if (data.results) {
1380 FOR_EACH_PTR(ptr_names, ptr) {
1381 free_string(ptr);
1382 } END_FOR_EACH_PTR(ptr);
1383 goto free_ptr_names;
1386 FOR_EACH_PTR(ptr_names, ptr) {
1387 run_sql(caller_info_callback, &data,
1388 "select call_id, type, parameter, key, value"
1389 " from caller_info where function = '%s' order by call_id",
1390 ptr);
1391 free_string(ptr);
1392 } END_FOR_EACH_PTR(ptr);
1394 free_ptr_names:
1395 __free_ptr_list((struct ptr_list **)&ptr_names);
1396 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1397 } else {
1398 sql_select_caller_info(&data,
1399 "call_id, type, parameter, key, value",
1400 sym);
1403 stree = __pop_fake_cur_stree();
1404 if (!data.ignore)
1405 merge_stree(&data.final_states, stree);
1406 free_stree(&stree);
1408 gettimeofday(&end_time, NULL);
1409 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1410 FOR_EACH_SM(data.final_states, sm) {
1411 __set_sm(sm);
1412 } END_FOR_EACH_SM(sm);
1415 free_stree(&data.final_states);
1418 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1420 struct implies_info *info = _info;
1421 struct db_implies_callback *cb;
1422 struct expression *arg = NULL;
1423 int type;
1424 int param;
1426 if (argc != 5)
1427 return 0;
1429 type = atoi(argv[1]);
1430 param = atoi(argv[2]);
1432 FOR_EACH_PTR(info->cb_list, cb) {
1433 if (cb->type != type)
1434 continue;
1435 if (param != -1) {
1436 arg = get_argument_from_call_expr(info->expr->args, param);
1437 if (!arg)
1438 continue;
1440 cb->callback(info->expr, arg, argv[3], argv[4]);
1441 } END_FOR_EACH_PTR(cb);
1443 return 0;
1446 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1448 struct implies_info *info = _info;
1449 struct db_implies_callback *cb;
1450 struct expression *arg;
1451 struct symbol *sym;
1452 char *name;
1453 int type;
1454 int param;
1456 if (argc != 5)
1457 return 0;
1459 type = atoi(argv[1]);
1460 param = atoi(argv[2]);
1462 if (!get_param(param, &name, &sym))
1463 return 0;
1464 arg = symbol_expression(sym);
1465 if (!arg)
1466 return 0;
1468 FOR_EACH_PTR(info->cb_list, cb) {
1469 if (cb->type != type)
1470 continue;
1471 cb->callback(info->expr, arg, argv[3], argv[4]);
1472 } END_FOR_EACH_PTR(cb);
1474 return 0;
1477 static void match_return_implies(struct expression *expr)
1479 struct implies_info info = {
1480 .type = RETURN_IMPLIES,
1481 .cb_list = return_implies_cb_list,
1484 if (expr->fn->type != EXPR_SYMBOL ||
1485 !expr->fn->symbol)
1486 return;
1487 info.expr = expr;
1488 info.sym = expr->fn->symbol;
1489 sql_select_implies("function, type, parameter, key, value", &info,
1490 return_implies_callbacks);
1493 static void match_call_implies(struct symbol *sym)
1495 struct implies_info info = {
1496 .type = CALL_IMPLIES,
1497 .cb_list = call_implies_cb_list,
1500 if (!sym || !sym->ident)
1501 return;
1503 info.sym = sym;
1504 sql_select_implies("function, type, parameter, key, value", &info,
1505 call_implies_callbacks);
1508 static char *get_fn_param_str(struct expression *expr)
1510 struct expression *tmp;
1511 int param;
1512 char buf[32];
1514 tmp = get_assigned_expr(expr);
1515 if (tmp)
1516 expr = tmp;
1517 expr = strip_expr(expr);
1518 if (!expr || expr->type != EXPR_CALL)
1519 return NULL;
1520 expr = strip_expr(expr->fn);
1521 if (!expr || expr->type != EXPR_SYMBOL)
1522 return NULL;
1523 param = get_param_num(expr);
1524 if (param < 0)
1525 return NULL;
1527 snprintf(buf, sizeof(buf), "[r $%d]", param);
1528 return alloc_sname(buf);
1531 static char *get_return_compare_is_param(struct expression *expr)
1533 char *var;
1534 char buf[256];
1535 int comparison;
1536 int param;
1538 param = get_param_num(expr);
1539 if (param < 0)
1540 return NULL;
1542 var = expr_to_var(expr);
1543 if (!var)
1544 return NULL;
1545 snprintf(buf, sizeof(buf), "%s orig", var);
1546 comparison = get_comparison_strings(var, buf);
1547 free_string(var);
1549 if (!comparison)
1550 return NULL;
1552 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1553 return alloc_sname(buf);
1556 static char *get_return_compare_str(struct expression *expr)
1558 char *compare_str;
1560 compare_str = get_return_compare_is_param(expr);
1561 if (compare_str)
1562 return compare_str;
1564 compare_str = expr_lte_to_param(expr, -1);
1565 if (compare_str)
1566 return compare_str;
1568 return expr_param_comparison(expr, -1);
1571 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1573 struct range_list *rl;
1574 char *return_ranges;
1575 sval_t sval;
1576 char *fn_param_str;
1577 char *compare_str;
1578 char *math_str;
1579 char buf[128];
1581 *rl_p = NULL;
1583 if (!expr)
1584 return alloc_sname("");
1586 if (get_implied_value(expr, &sval)) {
1587 sval = sval_cast(cur_func_return_type(), sval);
1588 *rl_p = alloc_rl(sval, sval);
1589 return sval_to_str_or_err_ptr(sval);
1592 fn_param_str = get_fn_param_str(expr);
1593 compare_str = expr_equal_to_param(expr, -1);
1594 math_str = get_value_in_terms_of_parameter_math(expr);
1596 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1597 rl = cast_rl(cur_func_return_type(), rl);
1598 return_ranges = show_rl(rl);
1599 } else if (get_imaginary_absolute(expr, &rl)){
1600 rl = cast_rl(cur_func_return_type(), rl);
1601 return alloc_sname(show_rl(rl));
1602 } else {
1603 get_absolute_rl(expr, &rl);
1604 rl = cast_rl(cur_func_return_type(), rl);
1605 return_ranges = show_rl(rl);
1607 *rl_p = rl;
1609 if (fn_param_str) {
1610 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1611 return alloc_sname(buf);
1613 if (compare_str) {
1614 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1615 return alloc_sname(buf);
1617 if (math_str) {
1618 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1619 return alloc_sname(buf);
1621 compare_str = get_return_compare_str(expr);
1622 if (compare_str) {
1623 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1624 return alloc_sname(buf);
1627 return return_ranges;
1630 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1632 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1635 static bool call_return_state_hooks_conditional(struct expression *expr)
1637 int final_pass_orig = final_pass;
1638 static int recurse;
1640 if (recurse >= 2)
1641 return false;
1642 if (!expr ||
1643 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1644 return false;
1646 recurse++;
1648 __push_fake_cur_stree();
1650 final_pass = 0;
1651 __split_whole_condition(expr->conditional);
1652 final_pass = final_pass_orig;
1654 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1656 __push_true_states();
1657 __use_false_states();
1659 call_return_state_hooks(expr->cond_false);
1661 __merge_true_states();
1662 __free_fake_cur_stree();
1664 recurse--;
1665 return true;
1668 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1670 struct split_data *data = NULL;
1671 struct expression *compare;
1672 struct range_list *rl;
1673 char buf[64];
1674 char *math;
1675 sval_t sval;
1676 bool undo;
1677 int i;
1679 for (i = 0; i < split_count; i++) {
1680 if (strcmp(get_function(), forced_splits[i]->func) == 0) {
1681 data = forced_splits[i];
1682 break;
1685 if (!data)
1686 return false;
1688 // FIXME: this works for copy_to/from_user() because the only thing we
1689 // care about is zero/non-zero
1690 if (strcmp(data->rl, "0") != 0)
1691 return false;
1693 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1694 if (!compare)
1695 return false;
1696 if (get_implied_value(compare, &sval))
1697 return false;
1699 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1700 call_return_states_callbacks("0", expr);
1701 if (undo)
1702 end_assume();
1704 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1705 if (get_implied_rl(expr, &rl)) {
1706 math = strchr(return_ranges, '[');
1707 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1708 } else {
1709 snprintf(buf, sizeof(buf), "%s", return_ranges);
1711 call_return_states_callbacks(buf, expr);
1712 if (undo)
1713 end_assume();
1715 return true;
1718 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1720 struct returned_state_callback *cb;
1722 return_ranges = replace_return_ranges(return_ranges);
1723 if (is_delete_return(return_ranges))
1724 return;
1725 if (is_project_delete_return(expr))
1726 return;
1727 if (handle_forced_split(return_ranges, expr))
1728 return;
1730 return_id++;
1731 FOR_EACH_PTR(returned_state_callbacks, cb) {
1732 cb->callback(return_id, (char *)return_ranges, expr);
1733 } END_FOR_EACH_PTR(cb);
1736 static void call_return_state_hooks_compare(struct expression *expr)
1738 char *return_ranges;
1739 int final_pass_orig = final_pass;
1740 sval_t sval = { .type = &int_ctype };
1741 sval_t ret;
1743 if (!get_implied_value(expr, &ret))
1744 ret.value = -1;
1746 __push_fake_cur_stree();
1748 final_pass = 0;
1749 __split_whole_condition(expr);
1750 final_pass = final_pass_orig;
1752 if (ret.value != 0) {
1753 return_ranges = alloc_sname("1");
1754 sval.value = 1;
1755 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1757 call_return_states_callbacks(return_ranges, expr);
1760 __push_true_states();
1761 __use_false_states();
1763 if (ret.value != 1) {
1764 return_ranges = alloc_sname("0");
1765 sval.value = 0;
1766 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1768 call_return_states_callbacks(return_ranges, expr);
1771 __merge_true_states();
1772 __free_fake_cur_stree();
1775 static bool is_implies_function(struct expression *expr)
1777 struct range_list *rl;
1779 if (!expr)
1780 return false;
1782 rl = get_range_implications(get_function());
1783 if (!rl)
1784 return false;
1786 sm_msg("%s: is implied", __func__);
1787 return true;
1790 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1792 struct sm_state *tmp;
1794 FOR_EACH_PTR(slist, tmp) {
1795 if (strcmp(tmp->state->name, sm->state->name) == 0)
1796 return 1;
1797 } END_FOR_EACH_PTR(tmp);
1799 return 0;
1802 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1804 struct range_list *rl;
1805 char *return_ranges;
1806 struct sm_state *tmp;
1807 int ret = 0;
1808 int nr_possible, nr_states;
1809 char *compare_str;
1810 char buf[128];
1811 struct state_list *already_handled = NULL;
1812 sval_t sval;
1814 if (!sm || !sm->merged)
1815 return 0;
1817 if (too_many_possible(sm) && !is_implies_function(expr))
1818 return 0;
1820 /* bail if it gets too complicated */
1821 nr_possible = 0;
1822 FOR_EACH_PTR(sm->possible, tmp) {
1823 if (tmp->merged)
1824 continue;
1825 if (ptr_in_list(tmp, already_handled))
1826 continue;
1827 add_ptr_list(&already_handled, tmp);
1828 nr_possible++;
1829 } END_FOR_EACH_PTR(tmp);
1830 free_slist(&already_handled);
1831 nr_states = get_db_state_count();
1832 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1833 return 0;
1835 FOR_EACH_PTR(sm->possible, tmp) {
1836 if (!is_leaf(tmp))
1837 continue;
1838 if (ptr_in_list(tmp, already_handled))
1839 continue;
1840 add_ptr_list(&already_handled, tmp);
1842 ret = 1;
1843 __push_fake_cur_stree();
1845 overwrite_states_using_pool(sm, tmp);
1847 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1848 return_ranges = show_rl(rl);
1849 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1850 compare_str = get_return_compare_str(expr);
1851 /* ignore obvious stuff like 0 <= param */
1852 /* Is this worthile when we have PARAM_COMPARE? */
1853 if (compare_str &&
1854 strncmp(compare_str, "[=", 2) != 0 &&
1855 rl_to_sval(rl, &sval))
1856 compare_str = NULL;
1857 if (compare_str) {
1858 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1859 return_ranges = alloc_sname(buf);
1862 call_return_states_callbacks(return_ranges, expr);
1864 __free_fake_cur_stree();
1865 } END_FOR_EACH_PTR(tmp);
1867 free_slist(&already_handled);
1869 return ret;
1872 static int call_return_state_hooks_split_possible(struct expression *expr)
1874 struct expression *fake;
1875 struct sm_state *sm;
1877 if (!expr)
1878 return 0;
1880 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1881 if (!sm) {
1882 fake = expr_get_fake_parent_expr(expr);
1883 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1884 return 0;
1885 fake = fake->left;
1886 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1888 return split_possible_helper(sm, expr);
1891 static bool has_possible_negative(struct sm_state *sm)
1893 struct sm_state *tmp;
1895 if (!type_signed(estate_type(sm->state)))
1896 return false;
1898 FOR_EACH_PTR(sm->possible, tmp) {
1899 if (!estate_rl(tmp->state))
1900 continue;
1901 if (sval_is_negative(estate_min(tmp->state)) &&
1902 sval_is_negative(estate_max(tmp->state)))
1903 return true;
1904 } END_FOR_EACH_PTR(tmp);
1906 return false;
1909 static bool has_separate_zero_null(struct sm_state *sm)
1911 struct sm_state *tmp;
1912 sval_t sval;
1914 FOR_EACH_PTR(sm->possible, tmp) {
1915 if (!estate_get_single_value(tmp->state, &sval))
1916 continue;
1917 if (sval.value == 0)
1918 return true;
1919 } END_FOR_EACH_PTR(tmp);
1921 return false;
1924 static int split_positive_from_negative(struct expression *expr)
1926 struct sm_state *sm;
1927 struct range_list *rl;
1928 const char *return_ranges;
1929 struct range_list *ret_rl;
1930 bool separate_zero;
1931 int undo;
1933 /* We're going to print the states 3 times */
1934 if (get_db_state_count() > 10000 / 3)
1935 return 0;
1937 if (!get_implied_rl(expr, &rl) || !rl)
1938 return 0;
1939 /* Forget about INT_MAX and larger */
1940 if (rl_max(rl).value <= 0)
1941 return 0;
1942 if (!sval_is_negative(rl_min(rl)))
1943 return 0;
1945 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1946 if (!sm)
1947 return 0;
1948 if (!has_possible_negative(sm))
1949 return 0;
1950 separate_zero = has_separate_zero_null(sm);
1952 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1953 return 0;
1955 return_ranges = get_return_ranges_str(expr, &ret_rl);
1956 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1957 call_return_states_callbacks(return_ranges, expr);
1959 end_assume();
1961 if (separate_zero) {
1962 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1964 return_ranges = get_return_ranges_str(expr, &ret_rl);
1965 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1966 call_return_states_callbacks(return_ranges, expr);
1968 if (undo)
1969 end_assume();
1972 undo = assume(compare_expression(expr, '<', zero_expr()));
1974 return_ranges = get_return_ranges_str(expr, &ret_rl);
1975 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1976 call_return_states_callbacks(return_ranges, expr);
1978 if (undo)
1979 end_assume();
1981 return 1;
1984 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1986 struct range_list *rl;
1987 struct range_list *nonnull_rl;
1988 sval_t null_sval;
1989 struct range_list *null_rl = NULL;
1990 char *return_ranges;
1991 struct sm_state *sm;
1992 struct smatch_state *state;
1993 int nr_states;
1994 int final_pass_orig = final_pass;
1996 if (!expr || expr_equal_to_param(expr, -1))
1997 return 0;
1998 if (expr->type == EXPR_CALL)
1999 return 0;
2001 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2002 if (!sm)
2003 return 0;
2004 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2005 return 0;
2006 state = sm->state;
2007 if (!estate_rl(state))
2008 return 0;
2009 if (estate_min(state).value == 0 && estate_max(state).value == 0)
2010 return 0;
2011 if (has_possible_negative(sm))
2012 return 0;
2013 if (!has_separate_zero_null(sm))
2014 return 0;
2016 nr_states = get_db_state_count();
2017 if (option_info && nr_states >= 1500)
2018 return 0;
2020 rl = estate_rl(state);
2022 __push_fake_cur_stree();
2024 final_pass = 0;
2025 __split_whole_condition(expr);
2026 final_pass = final_pass_orig;
2028 nonnull_rl = rl_filter(rl, rl_zero());
2029 return_ranges = show_rl(nonnull_rl);
2030 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2032 call_return_states_callbacks(return_ranges, expr);
2034 __push_true_states();
2035 __use_false_states();
2037 return_ranges = alloc_sname("0");
2038 null_sval = sval_type_val(rl_type(rl), 0);
2039 add_range(&null_rl, null_sval, null_sval);
2040 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2041 call_return_states_callbacks(return_ranges, expr);
2043 __merge_true_states();
2044 __free_fake_cur_stree();
2046 return 1;
2049 static bool is_kernel_success_fail(struct sm_state *sm)
2051 struct sm_state *tmp;
2052 struct range_list *rl;
2053 bool has_zero = false;
2054 bool has_neg = false;
2056 if (!type_signed(estate_type(sm->state)))
2057 return false;
2059 FOR_EACH_PTR(sm->possible, tmp) {
2060 rl = estate_rl(tmp->state);
2061 if (!rl)
2062 return false;
2063 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2064 has_zero = true;
2065 continue;
2067 has_neg = true;
2068 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
2069 continue;
2070 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
2071 continue;
2072 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
2073 continue;
2074 return false;
2075 } END_FOR_EACH_PTR(tmp);
2077 return has_zero && has_neg;
2080 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2082 struct sm_state *sm;
2083 struct range_list *rl;
2084 struct range_list *nonzero_rl;
2085 sval_t zero_sval;
2086 struct range_list *zero_rl = NULL;
2087 int nr_states;
2088 char *return_ranges;
2089 int final_pass_orig = final_pass;
2091 if (option_project != PROJ_KERNEL)
2092 return 0;
2094 nr_states = get_db_state_count();
2095 if (nr_states > 2000)
2096 return 0;
2098 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
2099 if (!sm)
2100 return 0;
2101 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2102 return 0;
2103 if (!is_kernel_success_fail(sm))
2104 return 0;
2106 rl = estate_rl(sm->state);
2107 if (!rl)
2108 return 0;
2110 __push_fake_cur_stree();
2112 final_pass = 0;
2113 __split_whole_condition(expr);
2114 final_pass = final_pass_orig;
2116 nonzero_rl = rl_filter(rl, rl_zero());
2117 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2118 return_ranges = show_rl(nonzero_rl);
2119 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2121 call_return_states_callbacks(return_ranges, expr);
2123 __push_true_states();
2124 __use_false_states();
2126 return_ranges = alloc_sname("0");
2127 zero_sval = sval_type_val(rl_type(rl), 0);
2128 add_range(&zero_rl, zero_sval, zero_sval);
2129 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2130 call_return_states_callbacks(return_ranges, expr);
2132 __merge_true_states();
2133 __free_fake_cur_stree();
2135 return 1;
2138 static int is_boolean(struct expression *expr)
2140 struct range_list *rl;
2142 if (!get_implied_rl(expr, &rl))
2143 return 0;
2144 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2145 return 1;
2146 return 0;
2149 static int splitable_function_call(struct expression *expr)
2151 struct sm_state *sm;
2153 if (!expr || expr->type != EXPR_CALL)
2154 return 0;
2155 sm = get_extra_sm_state(expr);
2156 return split_possible_helper(sm, expr);
2159 static struct sm_state *find_bool_param(void)
2161 struct stree *start_states;
2162 struct symbol *arg;
2163 struct sm_state *sm, *tmp;
2164 sval_t sval;
2166 start_states = get_start_states();
2168 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2169 if (!arg->ident)
2170 continue;
2171 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2172 if (!sm)
2173 continue;
2174 if (rl_min(estate_rl(sm->state)).value != 0 ||
2175 rl_max(estate_rl(sm->state)).value != 1)
2176 continue;
2177 goto found;
2178 } END_FOR_EACH_PTR_REVERSE(arg);
2180 return NULL;
2182 found:
2184 * Check if it's splitable. If not, then splitting it up is likely not
2185 * useful for the callers.
2187 FOR_EACH_PTR(sm->possible, tmp) {
2188 if (is_merged(tmp))
2189 continue;
2190 if (!estate_get_single_value(tmp->state, &sval))
2191 return NULL;
2192 } END_FOR_EACH_PTR(tmp);
2194 return sm;
2197 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2199 struct range_list *ret_rl;
2200 const char *return_ranges;
2201 struct sm_state *tmp;
2202 int ret = 0;
2203 struct state_list *already_handled = NULL;
2205 if (!sm || !sm->merged)
2206 return 0;
2208 if (too_many_possible(sm))
2209 return 0;
2211 FOR_EACH_PTR(sm->possible, tmp) {
2212 if (tmp->merged)
2213 continue;
2214 if (ptr_in_list(tmp, already_handled))
2215 continue;
2216 add_ptr_list(&already_handled, tmp);
2218 ret = 1;
2219 __push_fake_cur_stree();
2221 overwrite_states_using_pool(sm, tmp);
2223 return_ranges = get_return_ranges_str(expr, &ret_rl);
2224 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2225 call_return_states_callbacks(return_ranges, expr);
2227 __free_fake_cur_stree();
2228 } END_FOR_EACH_PTR(tmp);
2230 free_slist(&already_handled);
2232 return ret;
2235 static int split_by_bool_param(struct expression *expr)
2237 struct sm_state *start_sm, *sm;
2238 sval_t sval;
2240 start_sm = find_bool_param();
2241 if (!start_sm)
2242 return 0;
2243 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2244 if (!sm || estate_get_single_value(sm->state, &sval))
2245 return 0;
2247 if (get_db_state_count() * 2 >= 2000)
2248 return 0;
2250 return split_on_bool_sm(sm, expr);
2253 static int split_by_null_nonnull_param(struct expression *expr)
2255 struct symbol *arg;
2256 struct sm_state *sm;
2257 int nr_possible;
2259 /* function must only take one pointer */
2260 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2261 return 0;
2262 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2263 if (!arg->ident)
2264 return 0;
2265 if (get_real_base_type(arg)->type != SYM_PTR)
2266 return 0;
2268 if (param_was_set_var_sym(arg->ident->name, arg))
2269 return 0;
2270 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2271 if (!sm)
2272 return 0;
2274 if (!has_separate_zero_null(sm))
2275 return 0;
2277 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2278 if (get_db_state_count() * nr_possible >= 2000)
2279 return 0;
2281 return split_on_bool_sm(sm, expr);
2284 struct expression *strip_expr_statement(struct expression *expr)
2286 struct expression *orig = expr;
2287 struct statement *stmt, *last_stmt;
2289 if (!expr)
2290 return NULL;
2291 if (expr->type == EXPR_PREOP && expr->op == '(')
2292 expr = expr->unop;
2293 if (expr->type != EXPR_STATEMENT)
2294 return orig;
2295 stmt = expr->statement;
2296 if (!stmt || stmt->type != STMT_COMPOUND)
2297 return orig;
2299 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2300 if (!last_stmt || last_stmt->type == STMT_LABEL)
2301 last_stmt = last_stmt->label_statement;
2302 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2303 return orig;
2304 return strip_expr(last_stmt->expression);
2307 static bool is_kernel_error_path(struct expression *expr)
2309 struct range_list *rl;
2312 * Splitting up returns requires resources. It also requires resources
2313 * for the caller. It doesn't seem worth it to split anything up.
2315 if (!get_implied_rl(expr, &rl))
2316 return false;
2317 if (rl_type(rl) != &int_ctype)
2318 return false;
2319 if (rl_min(rl).value >= -4095 &&
2320 rl_max(rl).value < 0)
2321 return true;
2322 return false;
2325 static void call_return_state_hooks(struct expression *expr)
2327 struct range_list *ret_rl;
2328 const char *return_ranges;
2329 int nr_states;
2330 sval_t sval;
2332 if (__path_is_null())
2333 return;
2335 expr = strip_expr(expr);
2336 expr = strip_expr_statement(expr);
2338 if (is_impossible_path())
2339 goto vanilla;
2341 if (expr && (expr->type == EXPR_COMPARE ||
2342 !get_implied_value(expr, &sval)) &&
2343 (is_condition(expr) || is_boolean(expr))) {
2344 call_return_state_hooks_compare(expr);
2345 return;
2346 } else if (call_return_state_hooks_conditional(expr)) {
2347 return;
2348 } else if (is_kernel_error_path(expr)) {
2349 goto vanilla;
2350 } else if (call_return_state_hooks_split_possible(expr)) {
2351 return;
2352 } else if (split_positive_from_negative(expr)) {
2353 return;
2354 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2355 return;
2356 } else if (call_return_state_hooks_split_success_fail(expr)) {
2357 return;
2358 } else if (splitable_function_call(expr)) {
2359 return;
2360 } else if (split_by_bool_param(expr)) {
2361 } else if (split_by_null_nonnull_param(expr)) {
2362 return;
2365 vanilla:
2366 return_ranges = get_return_ranges_str(expr, &ret_rl);
2367 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2369 nr_states = get_db_state_count();
2370 if (nr_states >= 10000) {
2371 return_id++;
2372 match_return_info(return_id, (char *)return_ranges, expr);
2373 print_limited_param_set(return_id, (char *)return_ranges, expr);
2374 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2375 return;
2377 call_return_states_callbacks(return_ranges, expr);
2380 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2382 struct returned_member_callback *cb;
2383 struct sm_state *sm;
2384 struct symbol *type;
2385 char *name;
2386 char member_name[256];
2387 int len;
2389 type = get_type(expr);
2390 if (!type || type->type != SYM_PTR)
2391 return;
2392 name = expr_to_var(expr);
2393 if (!name)
2394 return;
2396 len = strlen(name);
2397 FOR_EACH_PTR(returned_member_callbacks, cb) {
2398 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2399 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2400 strcpy(member_name, "*$");
2401 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2402 continue;
2404 if (strncmp(sm->name, name, len) != 0)
2405 continue;
2406 if (strncmp(sm->name + len, "->", 2) != 0)
2407 continue;
2408 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2409 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2410 } END_FOR_EACH_SM(sm);
2411 } END_FOR_EACH_PTR(cb);
2413 free_string(name);
2416 static void print_return_struct_info(int return_id, char *return_ranges,
2417 struct expression *expr,
2418 struct symbol *sym,
2419 struct return_info_callback *cb)
2421 struct sm_state *sm;
2422 const char *printed_name;
2423 int param;
2425 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2426 if (sm->sym && sm->sym == sym) {
2427 param = -1;
2428 } else {
2429 param = get_param_num_from_sym(sm->sym);
2430 if (param < 0)
2431 continue;
2434 printed_name = get_param_name(sm);
2435 if (!printed_name)
2436 continue;
2438 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2439 } END_FOR_EACH_SM(sm);
2442 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2444 struct return_info_callback *cb;
2445 struct expression *tmp;
2446 struct symbol *sym;
2448 if (!option_info && !__inline_fn &&
2449 !local_debug && !option_debug)
2450 return;
2452 tmp = get_fake_variable(expr);
2453 if (tmp)
2454 expr = tmp;
2455 sym = expr_to_sym(expr);
2457 FOR_EACH_PTR(return_callbacks, cb) {
2458 __ignore_param_used++;
2459 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2460 __ignore_param_used--;
2461 } END_FOR_EACH_PTR(cb);
2464 static void reset_memdb(struct symbol *sym)
2466 mem_sql(NULL, NULL, "delete from caller_info;");
2467 mem_sql(NULL, NULL, "delete from return_states;");
2468 mem_sql(NULL, NULL, "delete from call_implies;");
2469 mem_sql(NULL, NULL, "delete from return_implies;");
2472 static void match_end_func_info(struct symbol *sym)
2474 if (__path_is_null())
2475 return;
2476 call_return_state_hooks(NULL);
2479 static void match_after_func(struct symbol *sym)
2481 if (!__inline_fn)
2482 reset_memdb(sym);
2485 static void init_memdb(void)
2487 char *err = NULL;
2488 int rc;
2489 const char *schema_files[] = {
2490 "db/db.schema",
2491 "db/caller_info.schema",
2492 "db/common_caller_info.schema",
2493 "db/return_states.schema",
2494 "db/function_type_size.schema",
2495 "db/type_size.schema",
2496 "db/function_type_info.schema",
2497 "db/type_info.schema",
2498 "db/call_implies.schema",
2499 "db/return_implies.schema",
2500 "db/function_ptr.schema",
2501 "db/local_values.schema",
2502 "db/function_type_value.schema",
2503 "db/type_value.schema",
2504 "db/function_type.schema",
2505 "db/data_info.schema",
2506 "db/parameter_name.schema",
2507 "db/constraints.schema",
2508 "db/constraints_required.schema",
2509 "db/fn_ptr_data_link.schema",
2510 "db/fn_data_link.schema",
2511 "db/mtag_about.schema",
2512 "db/mtag_info.schema",
2513 "db/mtag_map.schema",
2514 "db/mtag_data.schema",
2515 "db/mtag_alias.schema",
2517 static char buf[4096];
2518 int fd;
2519 int ret;
2520 int i;
2522 rc = sqlite3_open(":memory:", &mem_db);
2523 if (rc != SQLITE_OK) {
2524 sm_ierror("starting In-Memory database.");
2525 return;
2528 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2529 fd = open_schema_file(schema_files[i]);
2530 if (fd < 0)
2531 continue;
2532 ret = read(fd, buf, sizeof(buf));
2533 if (ret < 0) {
2534 sm_ierror("failed to read: %s", schema_files[i]);
2535 continue;
2537 close(fd);
2538 if (ret == sizeof(buf)) {
2539 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2540 schema_files[i], sizeof(buf));
2541 continue;
2543 buf[ret] = '\0';
2544 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2545 if (rc != SQLITE_OK) {
2546 sm_ierror("SQL error #2: %s", err);
2547 sm_ierror("%s", buf);
2552 static void init_cachedb(void)
2554 char *err = NULL;
2555 int rc;
2556 const char *schema_files[] = {
2557 "db/call_implies.schema",
2558 "db/return_implies.schema",
2559 "db/type_info.schema",
2560 "db/mtag_about.schema",
2561 "db/mtag_data.schema",
2562 "db/mtag_info.schema",
2563 "db/sink_info.schema",
2565 static char buf[4096];
2566 int fd;
2567 int ret;
2568 int i;
2570 rc = sqlite3_open(":memory:", &cache_db);
2571 if (rc != SQLITE_OK) {
2572 sm_ierror("starting In-Memory database.");
2573 return;
2576 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2577 fd = open_schema_file(schema_files[i]);
2578 if (fd < 0)
2579 continue;
2580 ret = read(fd, buf, sizeof(buf));
2581 if (ret < 0) {
2582 sm_ierror("failed to read: %s", schema_files[i]);
2583 continue;
2585 close(fd);
2586 if (ret == sizeof(buf)) {
2587 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2588 schema_files[i], sizeof(buf));
2589 continue;
2591 buf[ret] = '\0';
2592 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2593 if (rc != SQLITE_OK) {
2594 sm_ierror("SQL error #2: %s", err);
2595 sm_ierror("%s", buf);
2600 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2602 static char buf[4096];
2603 char tmp[256];
2604 char *p = buf;
2605 char *table = _table;
2606 int i;
2609 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2610 for (i = 0; i < argc; i++) {
2611 if (i)
2612 p += snprintf(p, 4096 - (p - buf), ", ");
2613 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2614 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2617 p += snprintf(p, 4096 - (p - buf), ");");
2618 if (p - buf > 4096)
2619 return 0;
2621 sm_msg("SQL: %s", buf);
2622 return 0;
2625 static void dump_cache(struct symbol_list *sym_list)
2627 const char *cache_tables[] = {
2628 "type_info", "return_implies", "call_implies", "mtag_data",
2629 "mtag_info", "mtag_about", "sink_info",
2631 char buf[64];
2632 int i;
2634 if (!option_info)
2635 return;
2637 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2638 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2639 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2643 void open_smatch_db(char *db_file)
2645 int rc;
2647 if (option_no_db)
2648 return;
2650 use_states = malloc(num_checks + 1);
2651 memset(use_states, 0xff, num_checks + 1);
2653 init_memdb();
2654 init_cachedb();
2656 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2657 if (rc != SQLITE_OK) {
2658 option_no_db = 1;
2659 return;
2661 run_sql(NULL, NULL,
2662 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2663 return;
2666 static char *get_next_string(char **str)
2668 static char string[256];
2669 char *start;
2670 char *p = *str;
2671 int len, i, j;
2673 if (*p == '\0')
2674 return NULL;
2675 start = p;
2677 while (*p != '\0' && *p != '\n') {
2678 if (*p == '\\' && *(p + 1) == ' ') {
2679 p += 2;
2680 continue;
2682 if (*p == ' ')
2683 break;
2684 p++;
2687 len = p - start;
2688 if (len >= sizeof(string)) {
2689 memcpy(string, start, sizeof(string));
2690 string[sizeof(string) - 1] = '\0';
2691 sm_ierror("return_fix: '%s' too long", string);
2692 **str = '\0';
2693 return NULL;
2695 memcpy(string, start, len);
2696 string[len] = '\0';
2697 for (i = 0; i < sizeof(string) - 1; i++) {
2698 if (string[i] == '\\' && string[i + 1] == ' ') {
2699 for (j = i; string[j] != '\0'; j++)
2700 string[j] = string[j + 1];
2703 if (*p != '\0')
2704 p++;
2705 *str = p;
2706 return string;
2709 static void register_return_deletes(void)
2711 char *func, *ret_str;
2712 char filename[256];
2713 char buf[4096];
2714 int fd, ret, i;
2715 char *p;
2717 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2718 fd = open_schema_file(filename);
2719 if (fd < 0)
2720 return;
2721 ret = read(fd, buf, sizeof(buf));
2722 close(fd);
2723 if (ret < 0)
2724 return;
2725 if (ret == sizeof(buf)) {
2726 sm_ierror("file too large: %s (limit %zd bytes)",
2727 filename, sizeof(buf));
2728 return;
2730 buf[ret] = '\0';
2732 p = buf;
2733 while (*p) {
2734 get_next_string(&p);
2735 delete_count++;
2737 if (delete_count == 0)
2738 return;
2739 if (delete_count % 2 != 0) {
2740 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2741 delete_count = 0;
2742 return;
2744 delete_table = malloc(delete_count * sizeof(char *));
2746 p = buf;
2747 i = 0;
2748 while (*p) {
2749 func = alloc_string(get_next_string(&p));
2750 ret_str = alloc_string(get_next_string(&p));
2752 delete_table[i++] = func;
2753 delete_table[i++] = ret_str;
2757 static void register_return_replacements(void)
2759 char *func, *orig, *new;
2760 char filename[256];
2761 char buf[4096];
2762 int fd, ret, i;
2763 char *p;
2765 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2766 fd = open_schema_file(filename);
2767 if (fd < 0)
2768 return;
2769 ret = read(fd, buf, sizeof(buf));
2770 close(fd);
2771 if (ret < 0)
2772 return;
2773 if (ret == sizeof(buf)) {
2774 sm_ierror("file too large: %s (limit %zd bytes)",
2775 filename, sizeof(buf));
2776 return;
2778 buf[ret] = '\0';
2780 p = buf;
2781 while (*p) {
2782 get_next_string(&p);
2783 replace_count++;
2785 if (replace_count == 0 || replace_count % 3 != 0) {
2786 replace_count = 0;
2787 return;
2789 replace_table = malloc(replace_count * sizeof(char *));
2791 p = buf;
2792 i = 0;
2793 while (*p) {
2794 func = alloc_string(get_next_string(&p));
2795 orig = alloc_string(get_next_string(&p));
2796 new = alloc_string(get_next_string(&p));
2798 replace_table[i++] = func;
2799 replace_table[i++] = orig;
2800 replace_table[i++] = new;
2804 static void register_forced_return_splits(void)
2806 int struct_members = sizeof(struct split_data) / sizeof(char *);
2807 char filename[256];
2808 char buf[4096];
2809 int fd, ret, i;
2810 char *p;
2812 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
2813 fd = open_schema_file(filename);
2814 if (fd < 0)
2815 return;
2816 ret = read(fd, buf, sizeof(buf));
2817 close(fd);
2818 if (ret < 0)
2819 return;
2820 if (ret == sizeof(buf)) {
2821 sm_ierror("file too large: %s (limit %zd bytes)",
2822 filename, sizeof(buf));
2823 return;
2825 buf[ret] = '\0';
2827 p = buf;
2828 while (*p) {
2829 get_next_string(&p);
2830 split_count++;
2832 if (split_count == 0)
2833 return;
2834 if (split_count % struct_members != 0) {
2835 printf("error parsing '%s' split_count=%d\n", filename, split_count);
2836 split_count = 0;
2837 return;
2839 split_count /= struct_members;
2840 forced_splits = malloc(split_count * sizeof(void *));
2842 p = buf;
2843 i = 0;
2844 while (*p) {
2845 struct split_data *split = malloc(sizeof(*split));
2847 split->func = alloc_string(get_next_string(&p));
2848 split->rl = alloc_string(get_next_string(&p));
2849 forced_splits[i++] = split;
2853 void register_definition_db_callbacks(int id)
2855 my_id = id;
2857 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2858 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2859 add_split_return_callback(match_return_info);
2860 add_split_return_callback(print_returned_struct_members);
2861 add_split_return_callback(print_return_info);
2862 add_hook(&call_return_state_hooks, RETURN_HOOK);
2863 add_hook(&match_end_func_info, END_FUNC_HOOK);
2864 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2866 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2867 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2868 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2870 common_funcs = load_strings_from_file(option_project_str, "common_functions");
2871 register_return_deletes();
2872 register_return_replacements();
2873 register_forced_return_splits();
2875 add_hook(&dump_cache, END_FILE_HOOK);
2878 void register_db_call_marker(int id)
2880 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2883 char *get_data_info_name(struct expression *expr)
2885 struct symbol *sym;
2886 char *name;
2887 char buf[256];
2888 char *ret = NULL;
2890 expr = strip_expr(expr);
2891 name = get_member_name(expr);
2892 if (name)
2893 return name;
2894 name = expr_to_var_sym(expr, &sym);
2895 if (!name || !sym)
2896 goto free;
2897 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2898 goto free;
2899 if (sym->ctype.modifiers & MOD_STATIC)
2900 snprintf(buf, sizeof(buf), "static %s", name);
2901 else
2902 snprintf(buf, sizeof(buf), "global %s", name);
2903 ret = alloc_sname(buf);
2904 free:
2905 free_string(name);
2906 return ret;