param_key: fix container of when no struct member is referenced
[smatch.git] / smatch_db.c
blobe2d0a7e0420f398f63d5a62714fa103b467920a4
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 struct sqlite3 *smatch_db;
27 struct sqlite3 *mem_db;
28 struct sqlite3 *cache_db;
30 int debug_db;
32 STATE(incomplete);
33 static int my_id;
35 static int return_id;
37 static void call_return_state_hooks(struct expression *expr);
38 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr);
40 #define SQLITE_CACHE_PAGES 1000
42 struct def_callback {
43 int hook_type;
44 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
46 ALLOCATOR(def_callback, "definition db hook callbacks");
47 DECLARE_PTR_LIST(callback_list, struct def_callback);
48 static struct callback_list *select_caller_info_callbacks;
50 struct def_name_sym_callback {
51 int hook_type;
52 void (*callback)(const char *name, struct symbol *sym, char *value);
54 ALLOCATOR(def_name_sym_callback, "definition db hook callbacks");
55 DECLARE_PTR_LIST(name_sym_callback_list, struct def_name_sym_callback);
56 static struct name_sym_callback_list *select_caller_name_sym_callbacks;
58 struct member_info_callback {
59 int owner;
60 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
62 ALLOCATOR(member_info_callback, "caller_info callbacks");
63 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
64 static struct member_info_cb_list *member_callbacks;
65 static struct member_info_cb_list *member_callbacks_new;
67 struct return_info_callback {
68 int owner;
69 void (*callback)(int return_id, char *return_ranges,
70 struct expression *returned_expr,
71 int param,
72 const char *printed_name,
73 struct sm_state *sm);
75 ALLOCATOR(return_info_callback, "return_info callbacks");
76 DECLARE_PTR_LIST(return_info_cb_list, struct return_info_callback);
77 static struct return_info_cb_list *return_callbacks;
79 struct returned_state_callback {
80 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
82 ALLOCATOR(returned_state_callback, "returned state callbacks");
83 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
84 static struct returned_state_cb_list *returned_state_callbacks;
86 struct returned_member_callback {
87 int owner;
88 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
90 ALLOCATOR(returned_member_callback, "returned member callbacks");
91 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
92 static struct returned_member_cb_list *returned_member_callbacks;
94 struct db_implies_callback {
95 int type;
96 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
98 ALLOCATOR(db_implies_callback, "return_implies callbacks");
99 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
100 static struct db_implies_cb_list *return_implies_cb_list_early;
101 static struct db_implies_cb_list *return_implies_cb_list_late;
102 static struct db_implies_cb_list *call_implies_cb_list;
104 DECLARE_PTR_LIST(delete_list, delete_hook);
105 static struct delete_list *delete_hooks;
107 struct split_data {
108 const char *func, *rl;
110 static struct split_data **forced_splits;
111 static int split_count;
113 /* silently truncates if needed. */
114 char *escape_newlines(const char *str)
116 char buf[1024] = "";
117 bool found = false;
118 int i, j;
120 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
121 if (str[i] != '\r' && str[i] != '\n') {
122 buf[j] = str[i];
123 continue;
126 found = true;
127 buf[j++] = '\\';
128 if (j == sizeof(buf))
129 break;
130 buf[j] = 'n';
133 if (!found)
134 return alloc_sname(str);
136 if (j == sizeof(buf))
137 buf[j - 1] = '\0';
138 return alloc_sname(buf);
141 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
143 int i;
145 for (i = 0; i < argc; i++) {
146 if (i != 0)
147 sm_printf(", ");
148 sm_printf("%s", argv[i]);
150 sm_printf("\n");
151 return 0;
154 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
156 char *err = NULL;
157 int rc;
159 if (!db)
160 return;
162 if (option_debug || debug_db) {
163 sm_msg("%s", sql);
164 if (strncasecmp(sql, "select", strlen("select")) == 0)
165 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
168 rc = sqlite3_exec(db, sql, callback, data, &err);
169 if (rc != SQLITE_OK && !parse_error) {
170 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
171 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
172 parse_error = 1;
176 static int replace_count;
177 static char **replace_table;
178 static const char *replace_return_ranges(const char *return_ranges)
180 int i;
182 if (!get_function()) {
183 /* I have no idea why EXPORT_SYMBOL() is here */
184 return return_ranges;
186 for (i = 0; i < replace_count; i += 3) {
187 if (strcmp(replace_table[i + 0], get_function()) == 0) {
188 if (strcmp(replace_table[i + 1], return_ranges) == 0)
189 return replace_table[i + 2];
192 return return_ranges;
195 static int delete_count;
196 static char **delete_table;
197 static bool is_delete_return(const char *return_ranges)
199 int i;
201 if (!get_function())
202 return false;
204 for (i = 0; i < delete_count; i += 2) {
205 if (strcmp(delete_table[i], get_function()) == 0 &&
206 strcmp(delete_table[i + 1], return_ranges) == 0)
207 return true;
210 return false;
213 void add_delete_return_hook(delete_hook *hook)
215 add_ptr_list(&delete_hooks, hook);
218 static bool is_project_delete_return(struct expression *expr)
220 delete_hook *hook;
222 FOR_EACH_PTR(delete_hooks, hook) {
223 if (hook(expr))
224 return true;
225 } END_FOR_EACH_PTR(hook);
226 return false;
229 static char *use_states;
230 static int get_db_state_count(void)
232 struct sm_state *sm;
233 int count = 0;
235 FOR_EACH_SM(__get_cur_stree(), sm) {
236 if (sm->owner == USHRT_MAX)
237 continue;
238 if (use_states[sm->owner])
239 count++;
240 } END_FOR_EACH_SM(sm);
241 return count;
244 static bool in_base_file(struct symbol *sym)
246 return sym->pos.stream == base_file_stream;
249 static bool is_local(struct symbol *sym)
251 if (sym->ctype.modifiers & MOD_STATIC)
252 return true;
253 if ((sym->ctype.modifiers & MOD_EXTERN) &&
254 (sym->ctype.modifiers & MOD_INLINE) &&
255 !in_base_file(sym))
256 return true;
258 if (!sym->definition)
259 return false;
261 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
262 (sym->definition->ctype.modifiers & MOD_INLINE) &&
263 !in_base_file(sym->definition))
264 return true;
266 return false;
269 void db_ignore_states(int id)
271 use_states[id] = 0;
274 unsigned long long __fn_mtag;
275 static void set_fn_mtag(struct symbol *sym)
277 char buf[128];
279 if (is_local(cur_func_sym))
280 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
281 else
282 snprintf(buf, sizeof(buf), "extern %s", get_function());
284 __fn_mtag = str_to_mtag(buf);
287 void sql_insert_return_states(int return_id, const char *return_ranges,
288 int type, int param, const char *key, const char *value)
290 unsigned long long id;
293 if (key && strlen(key) >= 80)
294 return;
295 if (__inline_fn)
296 id = (unsigned long)__inline_fn;
297 else
298 id = __fn_mtag;
300 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
301 get_base_file(), get_function(), id, return_id,
302 return_ranges, is_local(cur_func_sym), type, param, key, value);
305 static struct string_list *common_funcs;
306 static int is_common_function(const char *fn)
308 char *tmp;
310 if (!fn)
311 return 0;
313 if (strncmp(fn, "__builtin_", 10) == 0)
314 return 1;
316 FOR_EACH_PTR(common_funcs, tmp) {
317 if (strcmp(tmp, fn) == 0)
318 return 1;
319 } END_FOR_EACH_PTR(tmp);
321 return 0;
324 static char *function_signature(void)
326 return type_to_str(get_real_base_type(cur_func_sym));
329 void sql_insert_caller_info(struct expression *call, int type,
330 int param, const char *key, const char *value)
332 FILE *tmp_fd = sm_outfd;
333 char *fn;
335 if (!option_info && !__inline_call)
336 return;
337 if (unreachable())
338 return;
340 if (key && strlen(key) >= 80)
341 return;
343 fn = get_fnptr_name(call->fn);
344 if (!fn)
345 return;
347 if (__inline_call) {
348 mem_sql(NULL, NULL,
349 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
350 get_base_file(), get_function(), fn, (unsigned long)call,
351 is_static(call->fn), type, param, key, value);
354 if (!option_info)
355 return;
357 if (strncmp(fn, "__builtin_", 10) == 0)
358 return;
359 if (type != INTERNAL && is_common_function(fn))
360 return;
362 sm_outfd = caller_info_fd;
363 sm_msg("SQL_caller_info: insert into caller_info values ("
364 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
365 get_base_file(), get_function(), fn, is_static(call->fn),
366 type, param, key, value);
367 sm_outfd = tmp_fd;
369 free_string(fn);
372 void sql_insert_function_ptr(const char *fn, const char *struct_name)
374 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
375 get_base_file(), fn, struct_name);
378 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
380 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
381 get_base_file(), get_function(), (unsigned long)__inline_fn,
382 fn_static(), type, param, key, value);
385 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
387 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
388 get_base_file(), get_function(), (unsigned long)__inline_fn,
389 fn_static(), type, param, key, value);
392 void sql_insert_function_type_size(const char *member, const char *ranges)
394 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
397 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
399 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
402 void sql_insert_type_info(int type, const char *member, const char *value)
404 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
407 void sql_insert_local_values(const char *name, const char *value)
409 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
412 void sql_insert_function_type_value(const char *type, const char *value)
414 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
417 void sql_insert_function_type(int param, const char *value)
419 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
420 get_base_file(), get_function(), fn_static(), param, value);
423 void sql_insert_parameter_name(int param, const char *value)
425 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
426 get_base_file(), get_function(), fn_static(), param, value);
429 void sql_insert_data_info(struct expression *data, int type, const char *value)
431 char *data_name;
433 data_name = get_data_info_name(data);
434 if (!data_name)
435 return;
436 sql_insert(data_info, "'%s', '%s', %d, '%s'",
437 is_static(data) ? get_base_file() : "extern",
438 data_name, type, value);
441 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
443 sql_insert(data_info, "'%s', '%s', %d, '%s'",
444 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
445 var, type, value);
448 void sql_save_constraint(const char *con)
450 if (!option_info)
451 return;
453 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
456 void sql_save_constraint_required(const char *data, int op, const char *limit)
458 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
461 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
463 if (!option_info)
464 return;
466 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
467 "select constraints_required.data, constraints_required.op, '%s' from "
468 "constraints_required where bound = '%s';", new_limit, old_limit);
471 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
473 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
476 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
478 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
479 return;
481 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
482 is_local(fn->symbol) ? get_base_file() : "extern",
483 fn->symbol->ident->name,
484 is_local(fn->symbol),
485 type, param, key, value);
488 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
490 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
491 tag, get_filename(), get_function(), get_lineno(),
492 left_name, right_name);
495 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
497 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
500 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
502 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
505 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
507 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
510 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
512 mtag_t *saved_tag = _tag;
513 mtag_t new_tag;
515 new_tag = strtoll(argv[0], NULL, 10);
517 if (!*saved_tag)
518 *saved_tag = new_tag;
519 else if (*saved_tag != new_tag)
520 *saved_tag = -1ULL;
522 return 0;
525 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
527 mtag_t tmp = 0;
529 run_sql(save_mtag, &tmp,
530 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
531 tag, container_offset);
533 if (tmp == 0 || tmp == -1ULL)
534 return 0;
535 *container = tmp;
536 return 1;
539 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
541 mtag_t tmp = 0;
543 run_sql(save_mtag, &tmp,
544 "select tag from mtag_map where container = %lld and container_offset = %d;",
545 container, offset);
547 if (tmp == 0 || tmp == -1ULL)
548 return 0;
549 *tag = tmp;
550 return 1;
553 char *get_static_filter(struct symbol *sym)
555 static char sql_filter[1024];
557 /* This can only happen on buggy code. Return invalid SQL. */
558 if (!sym) {
559 sql_filter[0] = '\0';
560 return sql_filter;
563 if (is_local(sym)) {
564 snprintf(sql_filter, sizeof(sql_filter),
565 "file = '%s' and function = '%s' and static = '1'",
566 get_base_file(), sym->ident->name);
567 } else {
568 snprintf(sql_filter, sizeof(sql_filter),
569 "function = '%s' and static = '0'", sym->ident->name);
572 return sql_filter;
575 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
577 int *row_count = _row_count;
579 *row_count = 0;
580 if (argc != 1)
581 return 0;
582 *row_count = atoi(argv[0]);
583 return 0;
586 static void mark_call_params_untracked(struct expression *call)
588 struct expression *arg;
589 int i = 0;
591 FOR_EACH_PTR(call->args, arg) {
592 mark_untracked(call, i++, "$", NULL);
593 } END_FOR_EACH_PTR(arg);
596 static void sql_select_return_states_pointer(const char *cols,
597 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
599 char *ptr;
600 int return_count = 0;
602 ptr = get_fnptr_name(call->fn);
603 if (!ptr)
604 return;
606 run_sql(get_row_count, &return_count,
607 "select count(*) from return_states join function_ptr "
608 "where return_states.function == function_ptr.function and "
609 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
610 /* The magic number 100 is just from testing on the kernel. */
611 if (return_count == 0 || return_count > 100) {
612 run_sql(callback, info,
613 "select distinct %s from return_states join function_ptr where "
614 "return_states.function == function_ptr.function and ptr = '%s' "
615 "and searchable = 1 and type = %d "
616 "order by function_ptr.file, return_states.file, return_id, type;",
617 cols, ptr, INTERNAL);
618 mark_call_params_untracked(call);
619 return;
622 run_sql(callback, info,
623 "select %s from return_states join function_ptr where "
624 "return_states.function == function_ptr.function and ptr = '%s' "
625 "and searchable = 1 "
626 "order by function_ptr.file, return_states.file, return_id, type;",
627 cols, ptr);
630 static int is_local_symbol(struct expression *expr)
632 if (expr->type != EXPR_SYMBOL)
633 return 0;
634 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
635 return 0;
636 return 1;
639 bool is_fn_ptr(struct expression *fn)
641 fn = strip_expr(fn);
642 if (fn->type != EXPR_SYMBOL)
643 return true;
644 if (!fn->symbol)
645 return true;
646 if (is_local_symbol(fn))
647 return true;
648 return false;
651 void sql_select_return_states(const char *cols, struct expression *call,
652 int (*callback)(void*, int, char**, char**), void *info)
654 struct expression *fn;
655 int row_count = 0;
657 if (is_fake_call(call))
658 return;
660 fn = strip_expr(call->fn);
661 if (is_fn_ptr(fn)) {
662 sql_select_return_states_pointer(cols, call, callback, info);
663 return;
666 if (inlinable(fn)) {
667 mem_sql(callback, info,
668 "select %s from return_states where call_id = '%lu' order by return_id, type;",
669 cols, (unsigned long)call);
670 return;
673 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
674 get_static_filter(fn->symbol));
675 if (row_count == 0 && fn->symbol && fn->symbol->definition)
676 set_state(my_id, "db_incomplete", NULL, &incomplete);
677 if (row_count == 0 || row_count > 3000) {
678 mark_call_params_untracked(call);
679 return;
682 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
683 cols, get_static_filter(fn->symbol));
686 bool db_incomplete(void)
688 return !!get_state(my_id, "db_incomplete", NULL);
691 #define CALL_IMPLIES 0
692 #define RETURN_IMPLIES 1
694 struct implies_info {
695 int type;
696 struct db_implies_cb_list *cb_list;
697 struct expression *expr;
698 struct symbol *sym;
701 void sql_select_implies(const char *cols, struct implies_info *info,
702 int (*callback)(void*, int, char**, char**))
704 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
705 mem_sql(callback, info,
706 "select %s from return_implies where call_id = '%lu';",
707 cols, (unsigned long)info->expr);
708 return;
711 run_sql(callback, info, "select %s from %s_implies where %s;",
712 cols,
713 info->type == CALL_IMPLIES ? "call" : "return",
714 get_static_filter(info->sym));
717 struct select_caller_info_data {
718 struct stree *final_states;
719 struct timeval start_time;
720 int prev_func_id;
721 int ignore;
722 int results;
725 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
727 static void sql_select_caller_info(struct select_caller_info_data *data,
728 const char *cols, struct symbol *sym)
730 if (__inline_fn) {
731 mem_sql(caller_info_callback, data,
732 "select %s from caller_info where call_id = %lu;",
733 cols, (unsigned long)__inline_fn);
734 return;
737 if (sym->ident->name && is_common_function(sym->ident->name))
738 return;
739 run_sql(caller_info_callback, data,
740 "select %s from common_caller_info where %s order by call_id;",
741 cols, get_static_filter(sym));
742 if (data->results)
743 return;
745 run_sql(caller_info_callback, data,
746 "select %s from caller_info where %s order by call_id;",
747 cols, get_static_filter(sym));
750 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
752 struct def_callback *def_callback = __alloc_def_callback(0);
754 def_callback->hook_type = type;
755 def_callback->callback = callback;
756 add_ptr_list(&select_caller_info_callbacks, def_callback);
759 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type)
761 struct def_name_sym_callback *callback = __alloc_def_name_sym_callback(0);
763 callback->hook_type = type;
764 callback->callback = fn;
765 add_ptr_list(&select_caller_name_sym_callbacks, callback);
769 * These call backs are used when the --info option is turned on to print struct
770 * member information. For example foo->bar could have a state in
771 * smatch_extra.c and also check_user.c.
773 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
775 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
777 member_callback->owner = owner;
778 member_callback->callback = callback;
779 add_ptr_list(&member_callbacks, member_callback);
782 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
784 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
786 member_callback->owner = owner;
787 member_callback->callback = callback;
788 add_ptr_list(&member_callbacks_new, member_callback);
791 void add_return_info_callback(int owner,
792 void (*callback)(int return_id, char *return_ranges,
793 struct expression *returned_expr,
794 int param,
795 const char *printed_name,
796 struct sm_state *sm))
798 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
800 return_callback->owner = owner;
801 return_callback->callback = callback;
802 add_ptr_list(&return_callbacks, return_callback);
805 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
807 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
809 callback->callback = fn;
810 add_ptr_list(&returned_state_callbacks, callback);
813 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))
815 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
817 member_callback->owner = owner;
818 member_callback->callback = callback;
819 add_ptr_list(&returned_member_callbacks, member_callback);
822 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
824 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
826 cb->type = type;
827 cb->callback = callback;
828 add_ptr_list(&call_implies_cb_list, cb);
831 void select_return_implies_hook_early(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
833 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
835 cb->type = type;
836 cb->callback = callback;
837 add_ptr_list(&return_implies_cb_list_early, cb);
840 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
842 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
844 cb->type = type;
845 cb->callback = callback;
846 add_ptr_list(&return_implies_cb_list_late, cb);
849 struct return_info {
850 struct expression *static_returns_call;
851 struct symbol *return_type;
852 struct range_list *return_range_list;
855 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
857 struct return_info *ret_info = _ret_info;
858 struct range_list *rl;
859 struct expression *call_expr = ret_info->static_returns_call;
861 if (argc != 1)
862 return 0;
863 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
864 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
865 return 0;
868 static struct expression *cached_expr, *cached_no_args;
869 static const char *cached_str;
870 static struct range_list *cached_rl, *cached_str_rl, *cached_no_args_rl;
872 static void clear_cached_return_vals(void)
874 cached_expr = NULL;
875 cached_rl = NULL;
876 cached_str = NULL;
877 cached_str_rl = NULL;
878 cached_no_args = NULL;
879 cached_no_args_rl = NULL;
882 struct range_list *db_return_vals(struct expression *expr)
884 struct return_info ret_info = {};
885 struct sm_state *sm;
887 if (!expr)
888 return NULL;
890 if (is_fake_call(expr))
891 return NULL;
893 if (expr == cached_expr)
894 return clone_rl(cached_rl);
896 cached_expr = expr;
897 cached_rl = NULL;
899 sm = get_extra_sm_state(expr);
900 if (sm) {
901 cached_rl = clone_rl(estate_rl(sm->state));
902 return clone_rl(estate_rl(sm->state));
904 ret_info.static_returns_call = expr;
905 ret_info.return_type = get_type(expr);
906 if (!ret_info.return_type)
907 return NULL;
909 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
910 return NULL;
912 ret_info.return_range_list = NULL;
913 if (inlinable(expr->fn)) {
914 mem_sql(db_return_callback, &ret_info,
915 "select distinct return from return_states where call_id = '%lu';",
916 (unsigned long)expr);
917 } else {
918 run_sql(db_return_callback, &ret_info,
919 "select distinct return from return_states where %s;",
920 get_static_filter(expr->fn->symbol));
922 cached_rl = clone_rl(ret_info.return_range_list);
923 return ret_info.return_range_list;
926 struct range_list *db_return_vals_from_str(const char *fn_name)
928 struct return_info ret_info;
930 if (!fn_name)
931 return NULL;
932 if (fn_name == cached_str)
933 return clone_rl(cached_str_rl);
934 cached_str = fn_name;
935 cached_str_rl = NULL;
937 ret_info.static_returns_call = NULL;
938 ret_info.return_type = &llong_ctype;
939 ret_info.return_range_list = NULL;
941 run_sql(db_return_callback, &ret_info,
942 "select distinct return from return_states where function = '%s';",
943 fn_name);
944 cached_str_rl = clone_rl(ret_info.return_range_list);
945 return ret_info.return_range_list;
949 * This is used when we have a function that takes a function pointer as a
950 * parameter. "frob(blah, blah, my_function);" We know that the return values
951 * from frob() come from my_funcion() so we want to find the possible returns
952 * of my_function(), but we don't know which arguments are passed to it.
955 struct range_list *db_return_vals_no_args(struct expression *expr)
957 struct return_info ret_info = {};
959 if (!expr || expr->type != EXPR_SYMBOL)
960 return NULL;
962 if (expr == cached_no_args)
963 return clone_rl(cached_no_args_rl);
964 cached_no_args = expr;
965 cached_no_args_rl = NULL;
967 ret_info.static_returns_call = expr;
968 ret_info.return_type = get_type(expr);
969 ret_info.return_type = get_real_base_type(ret_info.return_type);
970 if (!ret_info.return_type)
971 return NULL;
973 run_sql(db_return_callback, &ret_info,
974 "select distinct return from return_states where %s;",
975 get_static_filter(expr->symbol));
977 cached_no_args_rl = clone_rl(ret_info.return_range_list);
978 return ret_info.return_range_list;
981 static void match_call_marker(struct expression *expr)
983 struct symbol *type;
985 type = get_type(expr->fn);
986 if (type && type->type == SYM_PTR)
987 type = get_real_base_type(type);
990 * we just want to record something in the database so that if we have
991 * two calls like: frob(4); frob(some_unkown); then on the receiving
992 * side we know that sometimes frob is called with unknown parameters.
995 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
998 int is_recursive_member(const char *name)
1000 char buf[256];
1001 const char *p, *next;
1002 int size;
1004 p = strchr(name, '>');
1005 if (!p)
1006 return 0;
1007 p++;
1008 while (true) {
1009 next = strchr(p, '>');
1010 if (!next)
1011 return 0;
1012 next++;
1014 size = next - p;
1015 if (size >= sizeof(buf))
1016 return 0;
1017 memcpy(buf, p, size);
1018 buf[size] = '\0';
1019 if (strstr(next, buf))
1020 return 1;
1021 p = next;
1025 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
1027 struct symbol *sym;
1028 const char *sm_name;
1029 char *name;
1030 bool is_address = false;
1031 bool add_star = false;
1032 char buf[256];
1033 char *ret = NULL;
1034 int len;
1036 expr = strip_expr(expr);
1037 if (!expr)
1038 return NULL;
1040 if (expr->type == EXPR_PREOP && expr->op == '&') {
1041 expr = strip_expr(expr->unop);
1042 is_address = true;
1045 name = expr_to_var_sym(expr, &sym);
1046 if (!name || !sym)
1047 goto free;
1048 if (sym != sm->sym)
1049 goto free;
1051 sm_name = sm->name;
1052 add_star = false;
1053 if (sm_name[0] == '*') {
1054 add_star = true;
1055 sm_name++;
1058 len = strlen(name);
1059 if (strncmp(name, sm_name, len) != 0)
1060 goto free;
1061 if (sm_name[len] == '\0') {
1062 snprintf(buf, sizeof(buf), "%s%s$",
1063 add_star ? "*" : "", is_address ? "*" : "");
1064 } else {
1065 if (sm_name[len] != '.' && sm_name[len] != '-')
1066 goto free;
1067 if (sm_name[len] == '-')
1068 len++;
1069 // FIXME does is_address really imply that sm_name[len] == '-'
1070 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
1071 sm_name + len);
1074 ret = alloc_sname(buf);
1075 free:
1076 free_string(name);
1077 return ret;
1080 static void print_struct_members(struct expression *call, struct expression *expr, int param,
1081 int owner,
1082 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
1083 bool new)
1085 struct sm_state *sm;
1086 const char *sm_name;
1087 char *name;
1088 struct symbol *sym;
1089 int len;
1090 char printed_name[256];
1091 int is_address = 0;
1092 bool add_star;
1093 struct symbol *type;
1095 expr = strip_expr(expr);
1096 if (!expr)
1097 return;
1098 type = get_type(expr);
1099 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
1100 return;
1102 if (expr->type == EXPR_PREOP && expr->op == '&') {
1103 expr = strip_expr(expr->unop);
1104 is_address = 1;
1107 name = expr_to_var_sym(expr, &sym);
1108 if (!name || !sym)
1109 goto free;
1111 len = strlen(name);
1112 FOR_EACH_SM(__get_cur_stree(), sm) {
1113 if (sm->owner != owner || sm->sym != sym)
1114 continue;
1115 sm_name = sm->name;
1116 add_star = false;
1117 if (sm_name[0] == '*') {
1118 add_star = true;
1119 sm_name++;
1121 // FIXME: simplify?
1122 if (!add_star && strcmp(name, sm_name) == 0) {
1123 if (is_address) {
1124 snprintf(printed_name, sizeof(printed_name), "*$");
1125 } else {
1126 if (new)
1127 snprintf(printed_name, sizeof(printed_name), "$");
1128 else
1129 continue;
1131 } else if (add_star && strcmp(name, sm_name) == 0) {
1132 snprintf(printed_name, sizeof(printed_name), "%s*$",
1133 is_address ? "*" : "");
1134 } else if (strncmp(name, sm_name, len) == 0) {
1135 if (sm_name[len] != '.' && sm_name[len] != '-')
1136 continue;
1137 if (is_address && sm_name[len] == '.') {
1138 snprintf(printed_name, sizeof(printed_name),
1139 "%s$->%s", add_star ? "*" : "",
1140 sm_name + len + 1);
1141 } else if (is_address && sm_name[len] == '-') {
1142 snprintf(printed_name, sizeof(printed_name),
1143 "%s(*$)%s", add_star ? "*" : "",
1144 sm_name + len);
1145 } else {
1146 snprintf(printed_name, sizeof(printed_name),
1147 "%s$%s", add_star ? "*" : "",
1148 sm_name + len);
1150 } else if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1151 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1152 continue;
1153 if (is_address && sm_name[len + 1] == '.') {
1154 snprintf(printed_name, sizeof(printed_name),
1155 "&%s$->%s", add_star ? "*" : "",
1156 sm_name + len + 2);
1157 } else if (is_address && sm_name[len] == '-') {
1158 snprintf(printed_name, sizeof(printed_name),
1159 "&%s(*$)%s", add_star ? "*" : "",
1160 sm_name + len + 1);
1161 } else {
1162 snprintf(printed_name, sizeof(printed_name),
1163 "&%s$%s", add_star ? "*" : "",
1164 sm_name + len + 1);
1166 } else {
1167 continue;
1169 if (is_recursive_member(printed_name))
1170 continue;
1171 callback(call, param, printed_name, sm);
1172 } END_FOR_EACH_SM(sm);
1173 free:
1174 free_string(name);
1177 static void match_call_info(struct expression *call)
1179 struct member_info_callback *cb;
1180 struct expression *arg;
1181 int i;
1183 FOR_EACH_PTR(member_callbacks, cb) {
1184 i = -1;
1185 FOR_EACH_PTR(call->args, arg) {
1186 i++;
1187 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1188 } END_FOR_EACH_PTR(arg);
1189 } END_FOR_EACH_PTR(cb);
1192 static struct expression *get_fake_variable(struct expression *expr)
1194 struct expression *tmp;
1196 tmp = expr_get_fake_parent_expr(expr);
1197 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1198 return NULL;
1200 return tmp->left;
1203 static struct sm_state *get_returned_sm(struct expression *expr)
1205 struct expression *fake;
1207 fake = get_fake_variable(expr);
1208 if (fake)
1209 expr = fake;
1211 return get_sm_state_expr(SMATCH_EXTRA, expr);
1214 static void match_call_info_new(struct expression *call)
1216 struct member_info_callback *cb;
1217 struct expression *arg, *tmp;
1218 int i;
1220 if (!option_info && !__inline_call && !local_debug)
1221 return;
1223 FOR_EACH_PTR(member_callbacks_new, cb) {
1224 i = -1;
1225 FOR_EACH_PTR(call->args, arg) {
1226 i++;
1227 tmp = get_fake_variable(arg);
1228 if (!tmp)
1229 tmp = arg;
1230 __ignore_param_used++;
1231 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1232 __ignore_param_used--;
1233 } END_FOR_EACH_PTR(arg);
1234 } END_FOR_EACH_PTR(cb);
1237 static int get_param(int param, char **name, struct symbol **sym)
1239 struct symbol *arg;
1240 int i;
1242 i = 0;
1243 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1245 * this is a temporary hack to work around a bug (I think in sparse?)
1246 * 2.6.37-rc1:fs/reiserfs/journal.o
1247 * If there is a function definition without parameter name found
1248 * after a function implementation then it causes a crash.
1249 * int foo() {}
1250 * int bar(char *);
1252 if (arg->ident->name < (char *)100)
1253 continue;
1254 if (i == param) {
1255 *name = arg->ident->name;
1256 *sym = arg;
1257 return TRUE;
1259 i++;
1260 } END_FOR_EACH_PTR(arg);
1262 return FALSE;
1265 static int function_signature_matches(const char *sig)
1267 char *my_sig;
1269 my_sig = function_signature();
1270 if (!sig || !my_sig)
1271 return 1; /* default to matching */
1272 if (strcmp(my_sig, sig) == 0)
1273 return 1;
1274 return 0;
1277 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1279 struct select_caller_info_data *data = _data;
1280 int func_id;
1281 long type;
1282 long param;
1283 char *key;
1284 char *value;
1285 char *name = NULL;
1286 struct symbol *sym = NULL;
1287 struct def_callback *def_callback;
1288 struct def_name_sym_callback *ns_callback;
1289 struct stree *stree;
1290 struct timeval cur_time;
1291 char fullname[256];
1292 char *p;
1294 data->results = 1;
1296 if (argc != 5)
1297 return 0;
1299 gettimeofday(&cur_time, NULL);
1300 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1301 return 0;
1303 func_id = atoi(argv[0]);
1304 errno = 0;
1305 type = strtol(argv[1], NULL, 10);
1306 param = strtol(argv[2], NULL, 10);
1307 if (errno)
1308 return 0;
1309 key = argv[3];
1310 value = argv[4];
1312 if (data->prev_func_id == -1)
1313 data->prev_func_id = func_id;
1314 if (func_id != data->prev_func_id) {
1315 stree = __pop_fake_cur_stree();
1316 if (!data->ignore)
1317 merge_stree(&data->final_states, stree);
1318 free_stree(&stree);
1319 __push_fake_cur_stree();
1320 __unnullify_path();
1321 data->prev_func_id = func_id;
1322 data->ignore = 0;
1325 if (data->ignore)
1326 return 0;
1327 if (type == INTERNAL &&
1328 !function_signature_matches(value)) {
1329 data->ignore = 1;
1330 return 0;
1333 if (param >= 0 && !get_param(param, &name, &sym))
1334 return 0;
1336 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1337 if (def_callback->hook_type == type)
1338 def_callback->callback(name, sym, key, value);
1339 } END_FOR_EACH_PTR(def_callback);
1341 p = strchr(key, '$');
1342 if (name && p)
1343 snprintf(fullname, sizeof(fullname), "%.*s%s%s", (int)(p - key), key, name, p + 1);
1344 else
1345 snprintf(fullname, sizeof(fullname), "%s", key);
1347 FOR_EACH_PTR(select_caller_name_sym_callbacks, ns_callback) {
1348 if (ns_callback->hook_type == type)
1349 ns_callback->callback(fullname, sym, value);
1350 } END_FOR_EACH_PTR(ns_callback);
1352 return 0;
1355 static struct string_list *ptr_names_done;
1356 static struct string_list *ptr_names;
1358 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1360 insert_string(&ptr_names, alloc_string(argv[0]));
1361 return 0;
1364 static char *get_next_ptr_name(void)
1366 char *ptr;
1368 FOR_EACH_PTR(ptr_names, ptr) {
1369 if (!insert_string(&ptr_names_done, ptr))
1370 continue;
1371 return ptr;
1372 } END_FOR_EACH_PTR(ptr);
1373 return NULL;
1376 static void get_ptr_names(const char *file, const char *name)
1378 char sql_filter[1024];
1379 int before, after;
1381 if (file) {
1382 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1383 file, name);
1384 } else {
1385 snprintf(sql_filter, 1024, "function = '%s';", name);
1388 before = ptr_list_size((struct ptr_list *)ptr_names);
1390 run_sql(get_ptr_name, NULL,
1391 "select distinct ptr from function_ptr where %s",
1392 sql_filter);
1394 after = ptr_list_size((struct ptr_list *)ptr_names);
1395 if (before == after)
1396 return;
1398 while ((name = get_next_ptr_name()))
1399 get_ptr_names(NULL, name);
1402 static void match_data_from_db(struct symbol *sym)
1404 struct select_caller_info_data data = { .prev_func_id = -1 };
1405 struct sm_state *sm;
1406 struct stree *stree;
1407 struct timeval end_time;
1409 if (!sym || !sym->ident)
1410 return;
1412 set_fn_mtag(sym);
1413 gettimeofday(&data.start_time, NULL);
1415 __push_fake_cur_stree();
1416 __unnullify_path();
1418 if (!__inline_fn) {
1419 char *ptr;
1421 if (sym->ctype.modifiers & MOD_STATIC)
1422 get_ptr_names(get_base_file(), sym->ident->name);
1423 else
1424 get_ptr_names(NULL, sym->ident->name);
1426 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1427 __free_ptr_list((struct ptr_list **)&ptr_names);
1428 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1429 __free_fake_cur_stree();
1430 return;
1433 sql_select_caller_info(&data,
1434 "call_id, type, parameter, key, value",
1435 sym);
1438 stree = __pop_fake_cur_stree();
1439 if (!data.ignore)
1440 merge_stree(&data.final_states, stree);
1441 free_stree(&stree);
1442 __push_fake_cur_stree();
1443 __unnullify_path();
1444 data.prev_func_id = -1;
1445 data.ignore = 0;
1446 data.results = 0;
1448 FOR_EACH_PTR(ptr_names, ptr) {
1449 run_sql(caller_info_callback, &data,
1450 "select call_id, type, parameter, key, value"
1451 " from common_caller_info where function = '%s' order by call_id",
1452 ptr);
1453 } END_FOR_EACH_PTR(ptr);
1455 if (data.results) {
1456 FOR_EACH_PTR(ptr_names, ptr) {
1457 free_string(ptr);
1458 } END_FOR_EACH_PTR(ptr);
1459 goto free_ptr_names;
1462 FOR_EACH_PTR(ptr_names, ptr) {
1463 run_sql(caller_info_callback, &data,
1464 "select call_id, type, parameter, key, value"
1465 " from caller_info where function = '%s' order by call_id",
1466 ptr);
1467 free_string(ptr);
1468 } END_FOR_EACH_PTR(ptr);
1470 free_ptr_names:
1471 __free_ptr_list((struct ptr_list **)&ptr_names);
1472 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1473 } else {
1474 sql_select_caller_info(&data,
1475 "call_id, type, parameter, key, value",
1476 sym);
1479 stree = __pop_fake_cur_stree();
1480 if (!data.ignore)
1481 merge_stree(&data.final_states, stree);
1482 free_stree(&stree);
1484 gettimeofday(&end_time, NULL);
1485 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1486 FOR_EACH_SM(data.final_states, sm) {
1487 __set_sm(sm);
1488 } END_FOR_EACH_SM(sm);
1491 free_stree(&data.final_states);
1494 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1496 struct implies_info *info = _info;
1497 struct db_implies_callback *cb;
1498 struct expression *arg = NULL;
1499 int type;
1500 int param;
1502 if (argc != 5)
1503 return 0;
1505 type = atoi(argv[1]);
1506 param = atoi(argv[2]);
1508 FOR_EACH_PTR(info->cb_list, cb) {
1509 if (cb->type != type)
1510 continue;
1511 if (param != -1) {
1512 arg = get_argument_from_call_expr(info->expr->args, param);
1513 if (!arg)
1514 continue;
1516 cb->callback(info->expr, arg, argv[3], argv[4]);
1517 } END_FOR_EACH_PTR(cb);
1519 return 0;
1522 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1524 struct implies_info *info = _info;
1525 struct db_implies_callback *cb;
1526 struct expression *arg;
1527 struct symbol *sym;
1528 char *name;
1529 int type;
1530 int param;
1532 if (argc != 5)
1533 return 0;
1535 type = atoi(argv[1]);
1536 param = atoi(argv[2]);
1538 if (!get_param(param, &name, &sym))
1539 return 0;
1540 arg = symbol_expression(sym);
1541 if (!arg)
1542 return 0;
1544 FOR_EACH_PTR(info->cb_list, cb) {
1545 if (cb->type != type)
1546 continue;
1547 cb->callback(info->expr, arg, argv[3], argv[4]);
1548 } END_FOR_EACH_PTR(cb);
1550 return 0;
1553 static void match_return_implies_helper(struct expression *expr, struct db_implies_cb_list *cb_list)
1555 struct implies_info info = {
1556 .type = RETURN_IMPLIES,
1557 .cb_list = cb_list,
1560 if (expr->fn->type != EXPR_SYMBOL ||
1561 !expr->fn->symbol)
1562 return;
1563 info.expr = expr;
1564 info.sym = expr->fn->symbol;
1565 sql_select_implies("function, type, parameter, key, value", &info,
1566 return_implies_callbacks);
1569 static void match_return_implies_early(struct expression *expr)
1571 match_return_implies_helper(expr, return_implies_cb_list_early);
1574 static void match_return_implies_late(struct expression *expr)
1576 match_return_implies_helper(expr, return_implies_cb_list_late);
1579 static void match_call_implies(struct symbol *sym)
1581 struct implies_info info = {
1582 .type = CALL_IMPLIES,
1583 .cb_list = call_implies_cb_list,
1586 if (!sym || !sym->ident)
1587 return;
1589 info.sym = sym;
1590 sql_select_implies("function, type, parameter, key, value", &info,
1591 call_implies_callbacks);
1594 static char *get_fn_param_str(struct expression *expr)
1596 struct expression *tmp;
1597 int param;
1598 char buf[32];
1600 tmp = get_assigned_expr(expr);
1601 if (tmp)
1602 expr = tmp;
1603 expr = strip_expr(expr);
1604 if (!expr || expr->type != EXPR_CALL)
1605 return NULL;
1606 expr = strip_expr(expr->fn);
1607 if (!expr || expr->type != EXPR_SYMBOL)
1608 return NULL;
1609 param = get_param_num(expr);
1610 if (param < 0)
1611 return NULL;
1613 snprintf(buf, sizeof(buf), "[r $%d]", param);
1614 return alloc_sname(buf);
1617 static char *get_return_compare_is_param(struct expression *expr)
1619 char *var;
1620 char buf[256];
1621 int comparison;
1622 int param;
1624 param = get_param_num(expr);
1625 if (param < 0)
1626 return NULL;
1628 var = expr_to_var(expr);
1629 if (!var)
1630 return NULL;
1631 snprintf(buf, sizeof(buf), "%s orig", var);
1632 comparison = get_comparison_strings(var, buf);
1633 free_string(var);
1635 if (!comparison)
1636 return NULL;
1638 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1639 return alloc_sname(buf);
1642 static char *get_return_compare_str(struct expression *expr)
1644 char *compare_str;
1646 compare_str = get_return_compare_is_param(expr);
1647 if (compare_str)
1648 return compare_str;
1650 compare_str = expr_lte_to_param(expr, -1);
1651 if (compare_str)
1652 return compare_str;
1654 return expr_param_comparison(expr, -1);
1657 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1659 struct expression *fake;
1660 struct range_list *rl;
1661 const char *return_ranges;
1662 sval_t sval;
1663 const char *container_of_str;
1664 char *fn_param_str;
1665 char *compare_str;
1666 char *math_str;
1667 char buf[128];
1669 *rl_p = NULL;
1671 if (!expr)
1672 return alloc_sname("");
1674 fake = get_fake_variable(expr);
1675 if (fake)
1676 expr = fake;
1678 container_of_str = get_container_of_str(expr);
1680 if (get_implied_value(expr, &sval)) {
1681 sval = sval_cast(cur_func_return_type(), sval);
1682 *rl_p = alloc_rl(sval, sval);
1683 return_ranges = sval_to_str_or_err_ptr(sval);
1684 if (container_of_str) {
1685 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1686 return alloc_sname(buf);
1688 return return_ranges;
1691 fn_param_str = get_fn_param_str(expr);
1692 compare_str = expr_equal_to_param(expr, -1);
1693 math_str = get_value_in_terms_of_parameter_math(expr);
1695 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1696 rl = cast_rl(cur_func_return_type(), rl);
1697 return_ranges = show_rl(rl);
1698 } else if (get_imaginary_absolute(expr, &rl)){
1699 rl = cast_rl(cur_func_return_type(), rl);
1700 return alloc_sname(show_rl(rl));
1701 } else {
1702 get_absolute_rl(expr, &rl);
1703 rl = cast_rl(cur_func_return_type(), rl);
1704 return_ranges = show_rl(rl);
1706 *rl_p = rl;
1708 if (container_of_str) {
1709 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, container_of_str);
1710 return alloc_sname(buf);
1712 if (fn_param_str) {
1713 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1714 return alloc_sname(buf);
1716 if (compare_str) {
1717 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1718 return alloc_sname(buf);
1720 if (math_str) {
1721 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1722 return alloc_sname(buf);
1724 compare_str = get_return_compare_str(expr);
1725 if (compare_str) {
1726 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1727 return alloc_sname(buf);
1730 return return_ranges;
1733 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1735 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1738 static bool call_return_state_hooks_conditional(struct expression *expr)
1740 int final_pass_orig = final_pass;
1741 static int recurse;
1743 if (recurse >= 2)
1744 return false;
1745 if (!expr ||
1746 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1747 return false;
1749 recurse++;
1751 __push_fake_cur_stree();
1753 final_pass = 0;
1754 __split_whole_condition(expr->conditional);
1755 final_pass = final_pass_orig;
1757 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1759 __push_true_states();
1760 __use_false_states();
1762 call_return_state_hooks(expr->cond_false);
1764 __merge_true_states();
1765 __free_fake_cur_stree();
1767 recurse--;
1768 return true;
1771 static bool handle_forced_split(const char *return_ranges, struct expression *expr)
1773 struct split_data *data = NULL;
1774 struct expression *compare;
1775 struct range_list *rl;
1776 char buf[64];
1777 char *math;
1778 sval_t sval;
1779 bool undo;
1780 int i;
1782 for (i = 0; i < split_count; i++) {
1783 if (strcmp(get_function(), forced_splits[i]->func) == 0) {
1784 data = forced_splits[i];
1785 break;
1788 if (!data)
1789 return false;
1791 // FIXME: this works for copy_to/from_user() because the only thing we
1792 // care about is zero/non-zero
1793 if (strcmp(data->rl, "0") != 0)
1794 return false;
1796 compare = compare_expression(expr, SPECIAL_EQUAL, zero_expr());
1797 if (!compare)
1798 return false;
1799 if (get_implied_value(compare, &sval))
1800 return false;
1802 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1803 call_return_states_callbacks("0", expr);
1804 if (undo)
1805 end_assume();
1807 undo = assume(compare_expression(expr, SPECIAL_NOTEQUAL, zero_expr()));
1808 if (get_implied_rl(expr, &rl)) {
1809 math = strchr(return_ranges, '[');
1810 snprintf(buf, sizeof(buf), "%s%s", show_rl(rl), math ?: "");
1811 } else {
1812 snprintf(buf, sizeof(buf), "%s", return_ranges);
1814 call_return_states_callbacks(buf, expr);
1815 if (undo)
1816 end_assume();
1818 return true;
1821 static void call_return_states_callbacks(const char *return_ranges, struct expression *expr)
1823 struct returned_state_callback *cb;
1825 return_ranges = replace_return_ranges(return_ranges);
1826 if (is_delete_return(return_ranges))
1827 return;
1828 if (is_project_delete_return(expr))
1829 return;
1830 if (handle_forced_split(return_ranges, expr))
1831 return;
1833 return_id++;
1834 FOR_EACH_PTR(returned_state_callbacks, cb) {
1835 cb->callback(return_id, (char *)return_ranges, expr);
1836 } END_FOR_EACH_PTR(cb);
1839 static void call_return_state_hooks_compare(struct expression *expr)
1841 char *return_ranges;
1842 int final_pass_orig = final_pass;
1843 sval_t sval = { .type = &int_ctype };
1844 sval_t ret;
1846 if (!get_implied_value(expr, &ret))
1847 ret.value = -1;
1849 __push_fake_cur_stree();
1851 final_pass = 0;
1852 __split_whole_condition(expr);
1853 final_pass = final_pass_orig;
1855 if (ret.value != 0) {
1856 return_ranges = alloc_sname("1");
1857 sval.value = 1;
1858 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1860 call_return_states_callbacks(return_ranges, expr);
1863 __push_true_states();
1864 __use_false_states();
1866 if (ret.value != 1) {
1867 return_ranges = alloc_sname("0");
1868 sval.value = 0;
1869 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1871 call_return_states_callbacks(return_ranges, expr);
1874 __merge_true_states();
1875 __free_fake_cur_stree();
1878 static bool is_implies_function(struct expression *expr)
1880 struct range_list *rl;
1882 if (!expr)
1883 return false;
1885 rl = get_range_implications(get_function());
1886 if (!rl)
1887 return false;
1889 sm_msg("%s: is implied", __func__);
1890 return true;
1893 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1895 struct sm_state *tmp;
1897 FOR_EACH_PTR(slist, tmp) {
1898 if (strcmp(tmp->state->name, sm->state->name) == 0)
1899 return 1;
1900 } END_FOR_EACH_PTR(tmp);
1902 return 0;
1905 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1907 struct range_list *rl;
1908 char *return_ranges;
1909 struct sm_state *tmp;
1910 int ret = 0;
1911 int nr_possible, nr_states;
1912 char *compare_str;
1913 char buf[128];
1914 struct state_list *already_handled = NULL;
1915 sval_t sval;
1917 if (!sm || !sm->merged)
1918 return 0;
1920 if (too_many_possible(sm) && !is_implies_function(expr))
1921 return 0;
1923 /* bail if it gets too complicated */
1924 nr_possible = 0;
1925 FOR_EACH_PTR(sm->possible, tmp) {
1926 if (tmp->merged)
1927 continue;
1928 if (ptr_in_list(tmp, already_handled))
1929 continue;
1930 add_ptr_list(&already_handled, tmp);
1931 nr_possible++;
1932 } END_FOR_EACH_PTR(tmp);
1933 free_slist(&already_handled);
1934 nr_states = get_db_state_count();
1935 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1936 return 0;
1938 FOR_EACH_PTR(sm->possible, tmp) {
1939 if (!is_leaf(tmp))
1940 continue;
1941 if (ptr_in_list(tmp, already_handled))
1942 continue;
1943 add_ptr_list(&already_handled, tmp);
1945 ret = 1;
1946 __push_fake_cur_stree();
1948 overwrite_states_using_pool(sm, tmp);
1950 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1951 return_ranges = show_rl(rl);
1952 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1953 compare_str = get_return_compare_str(expr);
1954 /* ignore obvious stuff like 0 <= param */
1955 /* Is this worthile when we have PARAM_COMPARE? */
1956 if (compare_str &&
1957 strncmp(compare_str, "[=", 2) != 0 &&
1958 rl_to_sval(rl, &sval))
1959 compare_str = NULL;
1960 if (compare_str) {
1961 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1962 return_ranges = alloc_sname(buf);
1965 call_return_states_callbacks(return_ranges, expr);
1967 __free_fake_cur_stree();
1968 } END_FOR_EACH_PTR(tmp);
1970 free_slist(&already_handled);
1972 return ret;
1975 static int call_return_state_hooks_split_possible(struct expression *expr)
1977 struct sm_state *sm;
1979 if (!expr)
1980 return 0;
1982 sm = get_returned_sm(expr);
1983 return split_possible_helper(sm, expr);
1986 static bool has_empty_state(struct sm_state *sm)
1988 struct sm_state *tmp;
1990 FOR_EACH_PTR(sm->possible, tmp) {
1991 if (!estate_rl(tmp->state))
1992 return true;
1993 } END_FOR_EACH_PTR(tmp);
1995 return false;
1998 static bool has_possible_negative(struct sm_state *sm)
2000 struct sm_state *tmp;
2002 if (!type_signed(estate_type(sm->state)))
2003 return false;
2005 FOR_EACH_PTR(sm->possible, tmp) {
2006 if (!estate_rl(tmp->state))
2007 continue;
2008 if (sval_is_negative(estate_min(tmp->state)) &&
2009 sval_is_negative(estate_max(tmp->state)))
2010 return true;
2011 } END_FOR_EACH_PTR(tmp);
2013 return false;
2016 static bool has_separate_zero_null(struct sm_state *sm)
2018 struct sm_state *tmp;
2019 sval_t sval;
2021 FOR_EACH_PTR(sm->possible, tmp) {
2022 if (!estate_get_single_value(tmp->state, &sval))
2023 continue;
2024 if (sval.value == 0)
2025 return true;
2026 } END_FOR_EACH_PTR(tmp);
2028 return false;
2031 static int split_positive_from_negative(struct expression *expr)
2033 struct sm_state *sm;
2034 struct range_list *rl;
2035 const char *return_ranges;
2036 struct range_list *ret_rl;
2037 bool separate_zero;
2038 int undo;
2040 /* We're going to print the states 3 times */
2041 if (get_db_state_count() > 10000 / 3)
2042 return 0;
2044 if (!get_implied_rl(expr, &rl) || !rl)
2045 return 0;
2046 /* Forget about INT_MAX and larger */
2047 if (rl_max(rl).value <= 0)
2048 return 0;
2049 if (!sval_is_negative(rl_min(rl)))
2050 return 0;
2052 sm = get_returned_sm(expr);
2053 if (!sm)
2054 return 0;
2055 if (has_empty_state(sm))
2056 return 0;
2057 if (!has_possible_negative(sm))
2058 return 0;
2059 separate_zero = has_separate_zero_null(sm);
2061 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
2062 return 0;
2064 return_ranges = get_return_ranges_str(expr, &ret_rl);
2065 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2066 call_return_states_callbacks(return_ranges, expr);
2068 end_assume();
2070 if (separate_zero) {
2071 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
2073 return_ranges = get_return_ranges_str(expr, &ret_rl);
2074 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2075 call_return_states_callbacks(return_ranges, expr);
2077 if (undo)
2078 end_assume();
2081 undo = assume(compare_expression(expr, '<', zero_expr()));
2083 return_ranges = get_return_ranges_str(expr, &ret_rl);
2084 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2085 call_return_states_callbacks(return_ranges, expr);
2087 if (undo)
2088 end_assume();
2090 return 1;
2093 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
2095 struct range_list *rl;
2096 struct range_list *nonnull_rl;
2097 sval_t null_sval;
2098 struct range_list *null_rl = NULL;
2099 char *return_ranges;
2100 struct sm_state *sm;
2101 struct smatch_state *state;
2102 int nr_states;
2103 int final_pass_orig = final_pass;
2105 if (!expr || expr_equal_to_param(expr, -1))
2106 return 0;
2107 if (expr->type == EXPR_CALL)
2108 return 0;
2110 sm = get_returned_sm(expr);
2111 if (!sm)
2112 return 0;
2113 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2114 return 0;
2115 state = sm->state;
2116 if (!estate_rl(state))
2117 return 0;
2118 if (estate_min(state).value == 0 && estate_max(state).value == 0)
2119 return 0;
2120 if (has_possible_negative(sm))
2121 return 0;
2122 if (!has_separate_zero_null(sm))
2123 return 0;
2125 nr_states = get_db_state_count();
2126 if (option_info && nr_states >= 1500)
2127 return 0;
2129 rl = estate_rl(state);
2131 __push_fake_cur_stree();
2133 final_pass = 0;
2134 __split_whole_condition(expr);
2135 final_pass = final_pass_orig;
2137 nonnull_rl = rl_filter(rl, rl_zero());
2138 return_ranges = show_rl(nonnull_rl);
2139 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
2141 call_return_states_callbacks(return_ranges, expr);
2143 __push_true_states();
2144 __use_false_states();
2146 return_ranges = alloc_sname("0");
2147 null_sval = sval_type_val(rl_type(rl), 0);
2148 add_range(&null_rl, null_sval, null_sval);
2149 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
2150 call_return_states_callbacks(return_ranges, expr);
2152 __merge_true_states();
2153 __free_fake_cur_stree();
2155 return 1;
2158 static bool is_neg_and_pos_err_code(struct range_list *rl)
2160 struct data_range *tmp, *last;
2162 if (option_project != PROJ_KERNEL)
2163 return false;
2164 if (!rl)
2165 return false;
2167 /* Assume s32min-(14),(-12)-(-1),1-s32max is an error code. */
2168 last = last_ptr_list((struct ptr_list *)rl);
2169 if (last->max.value >= 0 &&
2170 (last->min.value != 1 ||
2171 last->max.value != INT_MAX))
2172 return false;
2175 FOR_EACH_PTR(rl, tmp) {
2176 if (tmp == last)
2177 break;
2178 if (tmp->min.value != INT_MIN && tmp->min.value < -4095)
2179 return false;
2180 if (tmp->max.value < -4095 || tmp->max.value >= 0)
2181 return false;
2182 } END_FOR_EACH_PTR(tmp);
2184 return true;
2187 static bool is_kernel_success_fail(struct sm_state *sm)
2189 struct sm_state *tmp;
2190 struct range_list *rl;
2191 bool has_zero = false;
2192 bool has_neg = false;
2194 if (!type_signed(estate_type(sm->state)))
2195 return false;
2197 FOR_EACH_PTR(sm->possible, tmp) {
2198 rl = estate_rl(tmp->state);
2199 if (!rl)
2200 return false;
2201 if (!is_leaf(tmp))
2202 continue;
2203 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
2204 has_zero = true;
2205 continue;
2207 has_neg = true;
2208 if (is_neg_and_pos_err_code(estate_rl(tmp->state)))
2209 continue;
2210 return false;
2211 } END_FOR_EACH_PTR(tmp);
2213 return has_zero && has_neg;
2216 static int call_return_state_hooks_split_success_fail(struct expression *expr)
2218 struct expression *tmp_ret;
2219 struct sm_state *sm;
2220 struct range_list *rl;
2221 struct range_list *nonzero_rl;
2222 sval_t zero_sval;
2223 struct range_list *zero_rl = NULL;
2224 int nr_states;
2225 char *return_ranges;
2226 int final_pass_orig = final_pass;
2228 if (option_project != PROJ_KERNEL)
2229 return 0;
2231 nr_states = get_db_state_count();
2232 if (nr_states > 2000)
2233 return 0;
2235 tmp_ret = get_fake_variable(expr);
2236 if (!tmp_ret)
2237 tmp_ret = expr;
2238 sm = get_returned_sm(tmp_ret);
2239 if (!sm)
2240 return 0;
2241 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
2242 return 0;
2243 if (!is_kernel_success_fail(sm))
2244 return 0;
2246 rl = estate_rl(sm->state);
2247 if (!rl)
2248 return 0;
2250 __push_fake_cur_stree();
2252 final_pass = 0;
2253 __split_whole_condition(tmp_ret);
2254 final_pass = final_pass_orig;
2256 nonzero_rl = rl_filter(rl, rl_zero());
2257 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
2258 return_ranges = show_rl(nonzero_rl);
2259 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
2261 call_return_states_callbacks(return_ranges, expr);
2263 __push_true_states();
2264 __use_false_states();
2266 return_ranges = alloc_sname("0");
2267 zero_sval = sval_type_val(rl_type(rl), 0);
2268 add_range(&zero_rl, zero_sval, zero_sval);
2269 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2270 call_return_states_callbacks(return_ranges, expr);
2272 __merge_true_states();
2273 __free_fake_cur_stree();
2275 return 1;
2278 static int is_boolean(struct expression *expr)
2280 struct range_list *rl;
2282 if (!get_implied_rl(expr, &rl))
2283 return 0;
2284 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2285 return 1;
2286 return 0;
2289 static int splitable_function_call(struct expression *expr)
2291 struct sm_state *sm;
2293 if (!expr || expr->type != EXPR_CALL)
2294 return 0;
2295 sm = get_extra_sm_state(expr);
2296 return split_possible_helper(sm, expr);
2299 static struct sm_state *find_bool_param(void)
2301 struct stree *start_states;
2302 struct symbol *arg;
2303 struct sm_state *sm, *tmp;
2304 sval_t sval;
2306 start_states = get_start_states();
2308 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2309 if (!arg->ident)
2310 continue;
2311 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2312 if (!sm)
2313 continue;
2314 if (rl_min(estate_rl(sm->state)).value != 0 ||
2315 rl_max(estate_rl(sm->state)).value != 1)
2316 continue;
2317 goto found;
2318 } END_FOR_EACH_PTR_REVERSE(arg);
2320 return NULL;
2322 found:
2324 * Check if it's splitable. If not, then splitting it up is likely not
2325 * useful for the callers.
2327 FOR_EACH_PTR(sm->possible, tmp) {
2328 if (is_merged(tmp))
2329 continue;
2330 if (!estate_get_single_value(tmp->state, &sval))
2331 return NULL;
2332 } END_FOR_EACH_PTR(tmp);
2334 return sm;
2337 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2339 struct range_list *ret_rl;
2340 const char *return_ranges;
2341 struct sm_state *tmp;
2342 int ret = 0;
2343 struct state_list *already_handled = NULL;
2345 if (!sm || !sm->merged)
2346 return 0;
2348 if (too_many_possible(sm))
2349 return 0;
2351 FOR_EACH_PTR(sm->possible, tmp) {
2352 if (tmp->merged)
2353 continue;
2354 if (ptr_in_list(tmp, already_handled))
2355 continue;
2356 add_ptr_list(&already_handled, tmp);
2358 ret = 1;
2359 __push_fake_cur_stree();
2361 overwrite_states_using_pool(sm, tmp);
2363 return_ranges = get_return_ranges_str(expr, &ret_rl);
2364 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2365 call_return_states_callbacks(return_ranges, expr);
2367 __free_fake_cur_stree();
2368 } END_FOR_EACH_PTR(tmp);
2370 free_slist(&already_handled);
2372 return ret;
2375 static int split_by_bool_param(struct expression *expr)
2377 struct sm_state *start_sm, *sm;
2378 sval_t sval;
2380 start_sm = find_bool_param();
2381 if (!start_sm)
2382 return 0;
2383 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2384 if (!sm || estate_get_single_value(sm->state, &sval))
2385 return 0;
2387 if (get_db_state_count() * 2 >= 2000)
2388 return 0;
2390 return split_on_bool_sm(sm, expr);
2393 static int split_by_null_nonnull_param(struct expression *expr)
2395 struct symbol *arg;
2396 struct sm_state *sm;
2397 int nr_possible;
2399 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2400 if (!arg || !arg->ident)
2401 return 0;
2402 if (get_real_base_type(arg)->type != SYM_PTR)
2403 return 0;
2405 if (param_was_set_var_sym(arg->ident->name, arg))
2406 return 0;
2407 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2408 if (!sm)
2409 return 0;
2411 if (!has_separate_zero_null(sm))
2412 return 0;
2414 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2415 if (get_db_state_count() * nr_possible >= 2000)
2416 return 0;
2418 return split_on_bool_sm(sm, expr);
2421 static void call_hooks_based_on_pool(struct expression *expr, struct sm_state *gate_sm, struct sm_state *pool_sm)
2423 struct range_list *ret_rl;
2424 const char *return_ranges;
2426 __push_fake_cur_stree();
2428 overwrite_states_using_pool(gate_sm, pool_sm);
2430 return_ranges = get_return_ranges_str(expr, &ret_rl);
2431 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2432 call_return_states_callbacks(return_ranges, expr);
2434 __free_fake_cur_stree();
2437 static bool split_by_impossible(struct expression *expr)
2439 static int impossible_id;
2440 struct sm_state *sm, *tmp;
2441 int nr_states;
2443 if (!impossible_id)
2444 impossible_id = id_from_name("register_impossible_return");
2445 if (!impossible_id)
2446 return false;
2449 * The only states for register_impossible_return are &impossible,
2450 * &undefined and &merged. This function will break otherwise.
2453 sm = get_sm_state(impossible_id, "impossible", NULL);
2454 if (!sm || sm->state != &merged)
2455 return false;
2457 nr_states = get_db_state_count();
2458 if (nr_states >= 1000)
2459 return false;
2461 /* handle possible */
2462 FOR_EACH_PTR(sm->possible, tmp) {
2463 if (!is_leaf(tmp))
2464 continue;
2465 if (tmp->state != &undefined)
2466 continue;
2467 call_hooks_based_on_pool(expr, sm, tmp);
2468 goto impossible;
2469 } END_FOR_EACH_PTR(tmp);
2471 impossible:
2472 /* handle impossible */
2473 FOR_EACH_PTR(sm->possible, tmp) {
2474 if (!is_leaf(tmp))
2475 continue;
2476 if (strcmp(tmp->state->name, "impossible") != 0)
2477 continue;
2478 call_hooks_based_on_pool(expr, sm, tmp);
2479 return true;
2480 } END_FOR_EACH_PTR(tmp);
2482 return false;
2485 struct expression *strip_expr_statement(struct expression *expr)
2487 struct expression *orig = expr;
2488 struct statement *stmt, *last_stmt;
2490 if (!expr)
2491 return NULL;
2492 if (expr->type == EXPR_PREOP && expr->op == '(')
2493 expr = expr->unop;
2494 if (expr->type != EXPR_STATEMENT)
2495 return orig;
2496 stmt = expr->statement;
2497 if (!stmt || stmt->type != STMT_COMPOUND)
2498 return orig;
2500 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2501 if (!last_stmt || last_stmt->type == STMT_LABEL)
2502 last_stmt = last_stmt->label_statement;
2503 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2504 return orig;
2505 return strip_expr(last_stmt->expression);
2508 static bool is_kernel_error_path(struct expression *expr)
2510 struct range_list *rl;
2512 if (option_project != PROJ_KERNEL)
2513 return false;
2515 if (!get_implied_rl(expr, &rl))
2516 return false;
2517 if (rl_type(rl) != &int_ctype)
2518 return false;
2519 if (!is_neg_and_pos_err_code(rl))
2520 return false;
2521 return true;
2524 static void call_return_state_hooks(struct expression *expr)
2526 struct range_list *ret_rl;
2527 const char *return_ranges;
2528 int nr_states;
2529 sval_t sval;
2531 if (debug_db) {
2532 struct range_list *rl = NULL;
2534 get_absolute_rl(expr, &rl);
2535 sm_msg("RETURN: expr='%s' rl='%s' %lu states%s", expr_to_str(expr),
2536 show_rl(rl), stree_count(__get_cur_stree()),
2537 is_impossible_path() ? " (impossible path)" : "");
2541 if (__path_is_null())
2542 return;
2544 if (is_impossible_path())
2545 goto vanilla;
2547 if (expr && (expr->type == EXPR_COMPARE ||
2548 !get_implied_value(expr, &sval)) &&
2549 (is_condition(expr) || is_boolean(expr))) {
2550 call_return_state_hooks_compare(expr);
2551 if (debug_db)
2552 sm_msg("%s: bool", __func__);
2553 return;
2554 } else if (call_return_state_hooks_conditional(expr)) {
2555 if (debug_db)
2556 sm_msg("%s: condition", __func__);
2557 return;
2558 } else if (is_kernel_error_path(expr)) {
2559 if (debug_db)
2560 sm_msg("%s: kernel error path", __func__);
2561 goto vanilla;
2562 } else if (call_return_state_hooks_split_success_fail(expr)) {
2563 if (debug_db)
2564 sm_msg("%s: success_fail", __func__);
2565 return;
2566 } else if (call_return_state_hooks_split_possible(expr)) {
2567 if (debug_db)
2568 sm_msg("%s: split_possible", __func__);
2569 return;
2570 } else if (split_positive_from_negative(expr)) {
2571 if (debug_db)
2572 sm_msg("%s: positive negative", __func__);
2573 return;
2574 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2575 if (debug_db)
2576 sm_msg("%s: split zero non-zero", __func__);
2577 return;
2578 } else if (splitable_function_call(expr)) {
2579 if (debug_db)
2580 sm_msg("%s: split_function_call", __func__);
2581 return;
2582 } else if (split_by_bool_param(expr)) {
2583 if (debug_db)
2584 sm_msg("%s: bool param", __func__);
2585 return;
2586 } else if (split_by_null_nonnull_param(expr)) {
2587 if (debug_db)
2588 sm_msg("%s: null non-null param", __func__);
2589 return;
2590 } else if (split_by_impossible(expr)) {
2591 if (debug_db)
2592 sm_msg("%s: split by impossible", __func__);
2593 return;
2596 vanilla:
2597 return_ranges = get_return_ranges_str(expr, &ret_rl);
2598 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2600 nr_states = get_db_state_count();
2601 if (nr_states >= 10000) {
2602 return_id++;
2603 match_return_info(return_id, (char *)return_ranges, expr);
2604 print_limited_param_set(return_id, (char *)return_ranges, expr);
2605 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2606 return;
2608 call_return_states_callbacks(return_ranges, expr);
2609 if (debug_db)
2610 sm_msg("%s: vanilla", __func__);
2613 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2615 struct returned_member_callback *cb;
2616 struct sm_state *sm;
2617 struct symbol *type;
2618 char *name;
2619 char member_name[256];
2620 int len;
2622 type = get_type(expr);
2623 if (!type || type->type != SYM_PTR)
2624 return;
2625 name = expr_to_var(expr);
2626 if (!name)
2627 return;
2629 len = strlen(name);
2630 FOR_EACH_PTR(returned_member_callbacks, cb) {
2631 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2632 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2633 strcpy(member_name, "*$");
2634 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2635 continue;
2637 if (strncmp(sm->name, name, len) != 0)
2638 continue;
2639 if (strncmp(sm->name + len, "->", 2) != 0)
2640 continue;
2641 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2642 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2643 } END_FOR_EACH_SM(sm);
2644 } END_FOR_EACH_PTR(cb);
2646 free_string(name);
2649 static void print_return_struct_info(int return_id, char *return_ranges,
2650 struct expression *expr,
2651 struct symbol *sym,
2652 struct return_info_callback *cb)
2654 struct sm_state *sm;
2655 const char *printed_name;
2656 int param;
2658 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2659 param = get_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2660 if (!printed_name)
2661 continue;
2662 if (param < 0)
2663 continue;
2664 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2665 } END_FOR_EACH_SM(sm);
2667 /* always print returned states after processing param states */
2668 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2669 param = get_param_key_from_var_sym(sm->name, sm->sym, expr, &printed_name);
2670 if (!printed_name)
2671 continue;
2672 if (param != -1)
2673 continue;
2674 cb->callback(return_id, return_ranges, expr, -1, printed_name, sm);
2675 } END_FOR_EACH_SM(sm);
2678 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2680 struct return_info_callback *cb;
2681 struct expression *tmp;
2682 struct symbol *sym;
2684 if (!option_info && !__inline_fn &&
2685 !local_debug && !option_debug)
2686 return;
2688 tmp = get_fake_variable(expr);
2689 if (tmp)
2690 expr = tmp;
2691 sym = expr_to_sym(expr);
2693 FOR_EACH_PTR(return_callbacks, cb) {
2694 __ignore_param_used++;
2695 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2696 __ignore_param_used--;
2697 } END_FOR_EACH_PTR(cb);
2700 static void reset_memdb(struct symbol *sym)
2702 mem_sql(NULL, NULL, "delete from caller_info;");
2703 mem_sql(NULL, NULL, "delete from return_states;");
2704 mem_sql(NULL, NULL, "delete from call_implies;");
2705 mem_sql(NULL, NULL, "delete from return_implies;");
2708 static void match_end_func_info(struct symbol *sym)
2710 if (__path_is_null())
2711 return;
2712 call_return_state_hooks(NULL);
2715 static void match_after_func(struct symbol *sym)
2717 clear_cached_return_vals();
2718 if (!__inline_fn)
2719 reset_memdb(sym);
2722 static void init_memdb(void)
2724 char *err = NULL;
2725 int rc;
2726 const char *schema_files[] = {
2727 "db/db.schema",
2728 "db/caller_info.schema",
2729 "db/common_caller_info.schema",
2730 "db/return_states.schema",
2731 "db/function_type_size.schema",
2732 "db/type_size.schema",
2733 "db/function_type_info.schema",
2734 "db/type_info.schema",
2735 "db/call_implies.schema",
2736 "db/return_implies.schema",
2737 "db/function_ptr.schema",
2738 "db/local_values.schema",
2739 "db/function_type_value.schema",
2740 "db/type_value.schema",
2741 "db/function_type.schema",
2742 "db/data_info.schema",
2743 "db/parameter_name.schema",
2744 "db/constraints.schema",
2745 "db/constraints_required.schema",
2746 "db/fn_ptr_data_link.schema",
2747 "db/fn_data_link.schema",
2748 "db/mtag_about.schema",
2749 "db/mtag_info.schema",
2750 "db/mtag_map.schema",
2751 "db/mtag_data.schema",
2752 "db/mtag_alias.schema",
2754 static char buf[4096];
2755 int fd;
2756 int ret;
2757 int i;
2759 rc = sqlite3_open(":memory:", &mem_db);
2760 if (rc != SQLITE_OK) {
2761 sm_ierror("starting In-Memory database.");
2762 return;
2765 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2766 fd = open_schema_file(schema_files[i]);
2767 if (fd < 0)
2768 continue;
2769 ret = read(fd, buf, sizeof(buf));
2770 if (ret < 0) {
2771 sm_ierror("failed to read: %s", schema_files[i]);
2772 continue;
2774 close(fd);
2775 if (ret == sizeof(buf)) {
2776 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2777 schema_files[i], sizeof(buf));
2778 continue;
2780 buf[ret] = '\0';
2781 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2782 if (rc != SQLITE_OK) {
2783 sm_ierror("SQL error #2: %s", err);
2784 sm_ierror("%s", buf);
2789 static void init_cachedb(void)
2791 char *err = NULL;
2792 int rc;
2793 const char *schema_files[] = {
2794 "db/call_implies.schema",
2795 "db/return_implies.schema",
2796 "db/type_info.schema",
2797 "db/mtag_about.schema",
2798 "db/mtag_data.schema",
2799 "db/mtag_info.schema",
2800 "db/sink_info.schema",
2802 static char buf[4096];
2803 int fd;
2804 int ret;
2805 int i;
2807 rc = sqlite3_open(":memory:", &cache_db);
2808 if (rc != SQLITE_OK) {
2809 sm_ierror("starting In-Memory database.");
2810 return;
2813 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2814 fd = open_schema_file(schema_files[i]);
2815 if (fd < 0)
2816 continue;
2817 ret = read(fd, buf, sizeof(buf));
2818 if (ret < 0) {
2819 sm_ierror("failed to read: %s", schema_files[i]);
2820 continue;
2822 close(fd);
2823 if (ret == sizeof(buf)) {
2824 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2825 schema_files[i], sizeof(buf));
2826 continue;
2828 buf[ret] = '\0';
2829 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2830 if (rc != SQLITE_OK) {
2831 sm_ierror("SQL error #2: %s", err);
2832 sm_ierror("%s", buf);
2837 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2839 static char buf[4096];
2840 char tmp[256];
2841 char *p = buf;
2842 char *table = _table;
2843 int i;
2846 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2847 for (i = 0; i < argc; i++) {
2848 if (i)
2849 p += snprintf(p, 4096 - (p - buf), ", ");
2850 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2851 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2854 p += snprintf(p, 4096 - (p - buf), ");");
2855 if (p - buf > 4096)
2856 return 0;
2858 sm_msg("SQL: %s", buf);
2859 return 0;
2862 static void dump_cache(struct symbol_list *sym_list)
2864 const char *cache_tables[] = {
2865 "type_info", "return_implies", "call_implies", "mtag_data",
2866 "mtag_info", "mtag_about", "sink_info",
2868 char buf[64];
2869 int i;
2871 if (!option_info)
2872 return;
2874 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2875 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2876 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2880 void open_smatch_db(char *db_file)
2882 int rc;
2884 if (option_no_db)
2885 return;
2887 use_states = malloc(num_checks + 1);
2888 memset(use_states, 0xff, num_checks + 1);
2890 init_memdb();
2891 init_cachedb();
2893 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2894 if (rc != SQLITE_OK) {
2895 option_no_db = 1;
2896 return;
2898 run_sql(NULL, NULL,
2899 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2900 return;
2903 static char *get_next_string(char **str)
2905 static char string[256];
2906 char *start;
2907 char *p = *str;
2908 int len, i, j;
2910 if (*p == '\0')
2911 return NULL;
2912 start = p;
2914 while (*p != '\0' && *p != '\n') {
2915 if (*p == '\\' && *(p + 1) == ' ') {
2916 p += 2;
2917 continue;
2919 if (*p == ' ')
2920 break;
2921 p++;
2924 len = p - start;
2925 if (len >= sizeof(string)) {
2926 memcpy(string, start, sizeof(string));
2927 string[sizeof(string) - 1] = '\0';
2928 sm_ierror("return_fix: '%s' too long", string);
2929 **str = '\0';
2930 return NULL;
2932 memcpy(string, start, len);
2933 string[len] = '\0';
2934 for (i = 0; i < sizeof(string) - 1; i++) {
2935 if (string[i] == '\\' && string[i + 1] == ' ') {
2936 for (j = i; string[j] != '\0'; j++)
2937 string[j] = string[j + 1];
2940 if (*p != '\0')
2941 p++;
2942 *str = p;
2943 return string;
2946 static void register_return_deletes(void)
2948 char *func, *ret_str;
2949 char filename[256];
2950 char buf[4096];
2951 int fd, ret, i;
2952 char *p;
2954 snprintf(filename, 256, "db/%s.delete.return_states", option_project_str);
2955 fd = open_schema_file(filename);
2956 if (fd < 0)
2957 return;
2958 ret = read(fd, buf, sizeof(buf));
2959 close(fd);
2960 if (ret < 0)
2961 return;
2962 if (ret == sizeof(buf)) {
2963 sm_ierror("file too large: %s (limit %zd bytes)",
2964 filename, sizeof(buf));
2965 return;
2967 buf[ret] = '\0';
2969 p = buf;
2970 while (*p) {
2971 get_next_string(&p);
2972 delete_count++;
2974 if (delete_count == 0)
2975 return;
2976 if (delete_count % 2 != 0) {
2977 printf("error parsing '%s' delete_count=%d\n", filename, delete_count);
2978 delete_count = 0;
2979 return;
2981 delete_table = malloc(delete_count * sizeof(char *));
2983 p = buf;
2984 i = 0;
2985 while (*p) {
2986 func = alloc_string(get_next_string(&p));
2987 ret_str = alloc_string(get_next_string(&p));
2989 delete_table[i++] = func;
2990 delete_table[i++] = ret_str;
2994 #define RETURN_FIX_SIZE 8196
2995 static void register_return_replacements(void)
2997 char *func, *orig, *new;
2998 char filename[256];
2999 int fd, ret, i;
3000 char *buf;
3001 char *p;
3003 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
3004 fd = open_schema_file(filename);
3005 if (fd < 0)
3006 return;
3007 buf = malloc(RETURN_FIX_SIZE);
3008 ret = read(fd, buf, RETURN_FIX_SIZE);
3009 close(fd);
3010 if (ret < 0) {
3011 free(buf);
3012 return;
3014 if (ret == RETURN_FIX_SIZE) {
3015 sm_ierror("file too large: %s (limit %d bytes)",
3016 filename, RETURN_FIX_SIZE);
3017 free(buf);
3018 return;
3020 buf[ret] = '\0';
3022 p = buf;
3023 while (*p) {
3024 get_next_string(&p);
3025 replace_count++;
3027 if (replace_count == 0) {
3028 free(buf);
3029 return;
3031 if (replace_count % 3 != 0) {
3032 printf("error parsing '%s' replace_count=%d\n", filename, replace_count);
3033 replace_count = 0;
3034 free(buf);
3035 return;
3037 replace_table = malloc(replace_count * sizeof(char *));
3039 p = buf;
3040 i = 0;
3041 while (*p) {
3042 func = alloc_string(get_next_string(&p));
3043 orig = alloc_string(get_next_string(&p));
3044 new = alloc_string(get_next_string(&p));
3046 replace_table[i++] = func;
3047 replace_table[i++] = orig;
3048 replace_table[i++] = new;
3050 free(buf);
3053 static void register_forced_return_splits(void)
3055 int struct_members = sizeof(struct split_data) / sizeof(char *);
3056 char filename[256];
3057 char buf[4096];
3058 int fd, ret, i;
3059 char *p;
3061 snprintf(filename, 256, "db/%s.forced_return_splits", option_project_str);
3062 fd = open_schema_file(filename);
3063 if (fd < 0)
3064 return;
3065 ret = read(fd, buf, sizeof(buf));
3066 close(fd);
3067 if (ret < 0)
3068 return;
3069 if (ret == sizeof(buf)) {
3070 sm_ierror("file too large: %s (limit %zd bytes)",
3071 filename, sizeof(buf));
3072 return;
3074 buf[ret] = '\0';
3076 p = buf;
3077 while (*p) {
3078 get_next_string(&p);
3079 split_count++;
3081 if (split_count == 0)
3082 return;
3083 if (split_count % struct_members != 0) {
3084 printf("error parsing '%s' split_count=%d\n", filename, split_count);
3085 split_count = 0;
3086 return;
3088 split_count /= struct_members;
3089 forced_splits = malloc(split_count * sizeof(void *));
3091 p = buf;
3092 i = 0;
3093 while (*p) {
3094 struct split_data *split = malloc(sizeof(*split));
3096 split->func = alloc_string(get_next_string(&p));
3097 split->rl = alloc_string(get_next_string(&p));
3098 forced_splits[i++] = split;
3102 void register_definition_db_callbacks(int id)
3104 my_id = id;
3106 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
3107 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
3108 add_split_return_callback(match_return_info);
3109 add_split_return_callback(print_returned_struct_members);
3110 add_split_return_callback(print_return_info);
3111 add_hook(&call_return_state_hooks, RETURN_HOOK);
3112 add_hook(&match_end_func_info, END_FUNC_HOOK);
3113 add_hook(&match_after_func, AFTER_FUNC_HOOK);
3115 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
3116 add_hook(&match_call_implies, FUNC_DEF_HOOK);
3117 add_hook(&match_return_implies_early, CALL_HOOK_AFTER_INLINE);
3119 common_funcs = load_strings_from_file(option_project_str, "common_functions");
3120 register_return_deletes();
3121 register_return_replacements();
3122 register_forced_return_splits();
3124 add_hook(&dump_cache, END_FILE_HOOK);
3127 void register_definition_db_callbacks_late(int id)
3129 add_hook(&match_return_implies_late, CALL_HOOK_AFTER_INLINE);
3132 void register_db_call_marker(int id)
3134 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
3137 char *get_data_info_name(struct expression *expr)
3139 struct symbol *sym;
3140 char *name;
3141 char buf[256];
3142 char *ret = NULL;
3144 expr = strip_expr(expr);
3145 name = get_member_name(expr);
3146 if (name)
3147 return name;
3148 name = expr_to_var_sym(expr, &sym);
3149 if (!name || !sym)
3150 goto free;
3151 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
3152 goto free;
3153 if (sym->ctype.modifiers & MOD_STATIC)
3154 snprintf(buf, sizeof(buf), "static %s", name);
3155 else
3156 snprintf(buf, sizeof(buf), "global %s", name);
3157 ret = alloc_sname(buf);
3158 free:
3159 free_string(name);
3160 return ret;