dereferences_param: mark more parameters as dereferenced
[smatch.git] / smatch_db.c
blobc4cb1713b159bf74ccfe2c36c7f651d1c0a074ea
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 static int return_id;
34 static void call_return_state_hooks(struct expression *expr);
36 #define SQLITE_CACHE_PAGES 1000
38 struct def_callback {
39 int hook_type;
40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
42 ALLOCATOR(def_callback, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list, struct def_callback);
44 static struct callback_list *select_caller_info_callbacks;
46 struct member_info_callback {
47 int owner;
48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
50 ALLOCATOR(member_info_callback, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
52 static struct member_info_cb_list *member_callbacks;
53 static struct member_info_cb_list *member_callbacks_new;
55 struct return_info_callback {
56 int owner;
57 void (*callback)(int return_id, char *return_ranges,
58 struct expression *returned_expr,
59 int param,
60 const char *printed_name,
61 struct sm_state *sm);
63 ALLOCATOR(return_info_callback, "return_info callbacks");
64 DECLARE_PTR_LIST(return_info_cb_list, struct return_info_callback);
65 static struct return_info_cb_list *return_callbacks;
67 struct returned_state_callback {
68 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
70 ALLOCATOR(returned_state_callback, "returned state callbacks");
71 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
72 static struct returned_state_cb_list *returned_state_callbacks;
74 struct returned_member_callback {
75 int owner;
76 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
78 ALLOCATOR(returned_member_callback, "returned member callbacks");
79 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
80 static struct returned_member_cb_list *returned_member_callbacks;
82 struct db_implies_callback {
83 int type;
84 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
86 ALLOCATOR(db_implies_callback, "return_implies callbacks");
87 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
88 static struct db_implies_cb_list *return_implies_cb_list;
89 static struct db_implies_cb_list *call_implies_cb_list;
91 /* silently truncates if needed. */
92 char *escape_newlines(const char *str)
94 char buf[1024] = "";
95 bool found = false;
96 int i, j;
98 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
99 if (str[i] != '\r' && str[i] != '\n') {
100 buf[j] = str[i];
101 continue;
104 found = true;
105 buf[j++] = '\\';
106 if (j == sizeof(buf))
107 break;
108 buf[j] = 'n';
111 if (!found)
112 return alloc_sname(str);
114 if (j == sizeof(buf))
115 buf[j - 1] = '\0';
116 return alloc_sname(buf);
119 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
121 int i;
123 for (i = 0; i < argc; i++) {
124 if (i != 0)
125 sm_printf(", ");
126 sm_printf("%s", argv[i]);
128 sm_printf("\n");
129 return 0;
132 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
134 char *err = NULL;
135 int rc;
137 if (!db)
138 return;
140 if (option_debug || debug_db) {
141 sm_msg("%s", sql);
142 if (strncasecmp(sql, "select", strlen("select")) == 0)
143 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
146 rc = sqlite3_exec(db, sql, callback, data, &err);
147 if (rc != SQLITE_OK && !parse_error) {
148 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
149 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
150 parse_error = 1;
154 static int replace_count;
155 static char **replace_table;
156 static const char *replace_return_ranges(const char *return_ranges)
158 int i;
160 if (!get_function()) {
161 /* I have no idea why EXPORT_SYMBOL() is here */
162 return return_ranges;
164 for (i = 0; i < replace_count; i += 3) {
165 if (strcmp(replace_table[i + 0], get_function()) == 0) {
166 if (strcmp(replace_table[i + 1], return_ranges) == 0)
167 return replace_table[i + 2];
170 return return_ranges;
174 static char *use_states;
175 static int get_db_state_count(void)
177 struct sm_state *sm;
178 int count = 0;
180 FOR_EACH_SM(__get_cur_stree(), sm) {
181 if (sm->owner == USHRT_MAX)
182 continue;
183 if (use_states[sm->owner])
184 count++;
185 } END_FOR_EACH_SM(sm);
186 return count;
189 void db_ignore_states(int id)
191 use_states[id] = 0;
194 unsigned long long __fn_mtag;
195 static void set_fn_mtag(struct symbol *sym)
197 char buf[128];
199 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
200 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
201 else
202 snprintf(buf, sizeof(buf), "extern %s", get_function());
204 __fn_mtag = str_to_mtag(buf);
207 void sql_insert_return_states(int return_id, const char *return_ranges,
208 int type, int param, const char *key, const char *value)
210 unsigned long long id;
213 if (key && strlen(key) >= 80)
214 return;
215 if (__inline_fn)
216 id = (unsigned long)__inline_fn;
217 else
218 id = __fn_mtag;
220 return_ranges = replace_return_ranges(return_ranges);
221 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
222 get_base_file(), get_function(), id, return_id,
223 return_ranges, fn_static(), type, param, key, value);
226 static struct string_list *common_funcs;
227 static int is_common_function(const char *fn)
229 char *tmp;
231 if (!fn)
232 return 0;
234 if (strncmp(fn, "__builtin_", 10) == 0)
235 return 1;
237 FOR_EACH_PTR(common_funcs, tmp) {
238 if (strcmp(tmp, fn) == 0)
239 return 1;
240 } END_FOR_EACH_PTR(tmp);
242 return 0;
245 static char *function_signature(void)
247 return type_to_str(get_real_base_type(cur_func_sym));
250 void sql_insert_caller_info(struct expression *call, int type,
251 int param, const char *key, const char *value)
253 FILE *tmp_fd = sm_outfd;
254 char *fn;
256 if (!option_info && !__inline_call)
257 return;
259 if (key && strlen(key) >= 80)
260 return;
262 fn = get_fnptr_name(call->fn);
263 if (!fn)
264 return;
266 if (__inline_call) {
267 mem_sql(NULL, NULL,
268 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
269 get_base_file(), get_function(), fn, (unsigned long)call,
270 is_static(call->fn), type, param, key, value);
273 if (!option_info)
274 return;
276 if (strncmp(fn, "__builtin_", 10) == 0)
277 return;
278 if (type != INTERNAL && is_common_function(fn))
279 return;
281 sm_outfd = caller_info_fd;
282 sm_msg("SQL_caller_info: insert into caller_info values ("
283 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
284 get_base_file(), get_function(), fn, is_static(call->fn),
285 type, param, key, value);
286 sm_outfd = tmp_fd;
288 free_string(fn);
291 void sql_insert_function_ptr(const char *fn, const char *struct_name)
293 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
294 get_base_file(), fn, struct_name);
297 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
299 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
300 get_base_file(), get_function(), (unsigned long)__inline_fn,
301 fn_static(), type, param, key, value);
304 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
306 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
307 get_base_file(), get_function(), (unsigned long)__inline_fn,
308 fn_static(), type, param, key, value);
311 void sql_insert_function_type_size(const char *member, const char *ranges)
313 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
316 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
318 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
321 void sql_insert_type_info(int type, const char *member, const char *value)
323 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
326 void sql_insert_local_values(const char *name, const char *value)
328 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
331 void sql_insert_function_type_value(const char *type, const char *value)
333 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
336 void sql_insert_function_type(int param, const char *value)
338 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
339 get_base_file(), get_function(), fn_static(), param, value);
342 void sql_insert_parameter_name(int param, const char *value)
344 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
345 get_base_file(), get_function(), fn_static(), param, value);
348 void sql_insert_data_info(struct expression *data, int type, const char *value)
350 char *data_name;
352 data_name = get_data_info_name(data);
353 if (!data_name)
354 return;
355 sql_insert(data_info, "'%s', '%s', %d, '%s'",
356 is_static(data) ? get_base_file() : "extern",
357 data_name, type, value);
360 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
362 sql_insert(data_info, "'%s', '%s', %d, '%s'",
363 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
364 var, type, value);
367 void sql_save_constraint(const char *con)
369 if (!option_info)
370 return;
372 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
375 void sql_save_constraint_required(const char *data, int op, const char *limit)
377 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
380 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
382 if (!option_info)
383 return;
385 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
386 "select constraints_required.data, constraints_required.op, '%s' from "
387 "constraints_required where bound = '%s';", new_limit, old_limit);
390 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
392 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
395 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
397 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
398 return;
400 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
401 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
402 fn->symbol->ident->name,
403 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
404 type, param, key, value);
407 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
409 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
410 tag, get_filename(), get_function(), get_lineno(),
411 left_name, right_name);
414 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
416 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
419 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
421 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
424 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
426 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
429 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
431 mtag_t *saved_tag = _tag;
432 mtag_t new_tag;
434 new_tag = strtoll(argv[0], NULL, 10);
436 if (!*saved_tag)
437 *saved_tag = new_tag;
438 else if (*saved_tag != new_tag)
439 *saved_tag = -1ULL;
441 return 0;
444 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
446 mtag_t tmp = 0;
448 run_sql(save_mtag, &tmp,
449 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
450 tag, container_offset);
452 if (tmp == 0 || tmp == -1ULL)
453 return 0;
454 *container = tmp;
455 return 1;
458 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
460 mtag_t tmp = 0;
462 run_sql(save_mtag, &tmp,
463 "select tag from mtag_map where container = %lld and container_offset = %d;",
464 container, offset);
466 if (tmp == 0 || tmp == -1ULL)
467 return 0;
468 *tag = tmp;
469 return 1;
472 char *get_static_filter(struct symbol *sym)
474 static char sql_filter[1024];
476 /* This can only happen on buggy code. Return invalid SQL. */
477 if (!sym) {
478 sql_filter[0] = '\0';
479 return sql_filter;
482 if (sym->ctype.modifiers & MOD_STATIC) {
483 snprintf(sql_filter, sizeof(sql_filter),
484 "file = '%s' and function = '%s' and static = '1'",
485 get_base_file(), sym->ident->name);
486 } else {
487 snprintf(sql_filter, sizeof(sql_filter),
488 "function = '%s' and static = '0'", sym->ident->name);
491 return sql_filter;
494 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
496 int *row_count = _row_count;
498 *row_count = 0;
499 if (argc != 1)
500 return 0;
501 *row_count = atoi(argv[0]);
502 return 0;
505 static void mark_call_params_untracked(struct expression *call)
507 struct expression *arg;
508 int i = 0;
510 FOR_EACH_PTR(call->args, arg) {
511 mark_untracked(call, i++, "$", NULL);
512 } END_FOR_EACH_PTR(arg);
515 static void sql_select_return_states_pointer(const char *cols,
516 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
518 char *ptr;
519 int return_count = 0;
521 ptr = get_fnptr_name(call->fn);
522 if (!ptr)
523 return;
525 run_sql(get_row_count, &return_count,
526 "select count(*) from return_states join function_ptr "
527 "where return_states.function == function_ptr.function and "
528 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
529 /* The magic number 100 is just from testing on the kernel. */
530 if (return_count > 100) {
531 mark_call_params_untracked(call);
532 return;
535 run_sql(callback, info,
536 "select %s from return_states join function_ptr where "
537 "return_states.function == function_ptr.function and ptr = '%s' "
538 "and searchable = 1 "
539 "order by function_ptr.file, return_states.file, return_id, type;",
540 cols, ptr);
543 static int is_local_symbol(struct expression *expr)
545 if (expr->type != EXPR_SYMBOL)
546 return 0;
547 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
548 return 0;
549 return 1;
552 void sql_select_return_states(const char *cols, struct expression *call,
553 int (*callback)(void*, int, char**, char**), void *info)
555 struct expression *fn;
556 int row_count = 0;
558 if (is_fake_call(call))
559 return;
561 fn = strip_expr(call->fn);
562 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
563 sql_select_return_states_pointer(cols, call, callback, info);
564 return;
567 if (inlinable(fn)) {
568 mem_sql(callback, info,
569 "select %s from return_states where call_id = '%lu' order by return_id, type;",
570 cols, (unsigned long)call);
571 return;
574 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
575 get_static_filter(fn->symbol));
576 if (row_count > 3000)
577 return;
579 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
580 cols, get_static_filter(fn->symbol));
583 #define CALL_IMPLIES 0
584 #define RETURN_IMPLIES 1
586 struct implies_info {
587 int type;
588 struct db_implies_cb_list *cb_list;
589 struct expression *expr;
590 struct symbol *sym;
593 void sql_select_implies(const char *cols, struct implies_info *info,
594 int (*callback)(void*, int, char**, char**))
596 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
597 mem_sql(callback, info,
598 "select %s from return_implies where call_id = '%lu';",
599 cols, (unsigned long)info->expr);
600 return;
603 run_sql(callback, info, "select %s from %s_implies where %s;",
604 cols,
605 info->type == CALL_IMPLIES ? "call" : "return",
606 get_static_filter(info->sym));
609 struct select_caller_info_data {
610 struct stree *final_states;
611 struct timeval start_time;
612 int prev_func_id;
613 int ignore;
614 int results;
617 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
619 static void sql_select_caller_info(struct select_caller_info_data *data,
620 const char *cols, struct symbol *sym)
622 if (__inline_fn) {
623 mem_sql(caller_info_callback, data,
624 "select %s from caller_info where call_id = %lu;",
625 cols, (unsigned long)__inline_fn);
626 return;
629 if (sym->ident->name && is_common_function(sym->ident->name))
630 return;
631 run_sql(caller_info_callback, data,
632 "select %s from common_caller_info where %s order by call_id;",
633 cols, get_static_filter(sym));
634 if (data->results)
635 return;
637 run_sql(caller_info_callback, data,
638 "select %s from caller_info where %s order by call_id;",
639 cols, get_static_filter(sym));
642 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
644 struct def_callback *def_callback = __alloc_def_callback(0);
646 def_callback->hook_type = type;
647 def_callback->callback = callback;
648 add_ptr_list(&select_caller_info_callbacks, def_callback);
652 * These call backs are used when the --info option is turned on to print struct
653 * member information. For example foo->bar could have a state in
654 * smatch_extra.c and also check_user.c.
656 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
658 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
660 member_callback->owner = owner;
661 member_callback->callback = callback;
662 add_ptr_list(&member_callbacks, member_callback);
665 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
667 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
669 member_callback->owner = owner;
670 member_callback->callback = callback;
671 add_ptr_list(&member_callbacks_new, member_callback);
674 void add_return_info_callback(int owner,
675 void (*callback)(int return_id, char *return_ranges,
676 struct expression *returned_expr,
677 int param,
678 const char *printed_name,
679 struct sm_state *sm))
681 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
683 return_callback->owner = owner;
684 return_callback->callback = callback;
685 add_ptr_list(&return_callbacks, return_callback);
688 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
690 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
692 callback->callback = fn;
693 add_ptr_list(&returned_state_callbacks, callback);
696 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))
698 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
700 member_callback->owner = owner;
701 member_callback->callback = callback;
702 add_ptr_list(&returned_member_callbacks, member_callback);
705 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
707 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
709 cb->type = type;
710 cb->callback = callback;
711 add_ptr_list(&call_implies_cb_list, cb);
714 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
716 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
718 cb->type = type;
719 cb->callback = callback;
720 add_ptr_list(&return_implies_cb_list, cb);
723 struct return_info {
724 struct expression *static_returns_call;
725 struct symbol *return_type;
726 struct range_list *return_range_list;
729 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
731 struct return_info *ret_info = _ret_info;
732 struct range_list *rl;
733 struct expression *call_expr = ret_info->static_returns_call;
735 if (argc != 1)
736 return 0;
737 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
738 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
739 return 0;
742 struct range_list *db_return_vals(struct expression *expr)
744 struct return_info ret_info = {};
745 struct sm_state *sm;
747 if (is_fake_call(expr))
748 return NULL;
750 sm = get_extra_sm_state(expr);
751 if (sm)
752 return clone_rl(estate_rl(sm->state));
753 ret_info.static_returns_call = expr;
754 ret_info.return_type = get_type(expr);
755 if (!ret_info.return_type)
756 return NULL;
758 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
759 return NULL;
761 ret_info.return_range_list = NULL;
762 if (inlinable(expr->fn)) {
763 mem_sql(db_return_callback, &ret_info,
764 "select distinct return from return_states where call_id = '%lu';",
765 (unsigned long)expr);
766 } else {
767 run_sql(db_return_callback, &ret_info,
768 "select distinct return from return_states where %s;",
769 get_static_filter(expr->fn->symbol));
771 return ret_info.return_range_list;
774 struct range_list *db_return_vals_from_str(const char *fn_name)
776 struct return_info ret_info;
778 ret_info.static_returns_call = NULL;
779 ret_info.return_type = &llong_ctype;
780 ret_info.return_range_list = NULL;
782 run_sql(db_return_callback, &ret_info,
783 "select distinct return from return_states where function = '%s';",
784 fn_name);
785 return ret_info.return_range_list;
789 * This is used when we have a function that takes a function pointer as a
790 * parameter. "frob(blah, blah, my_function);" We know that the return values
791 * from frob() come from my_funcion() so we want to find the possible returns
792 * of my_function(), but we don't know which arguments are passed to it.
795 struct range_list *db_return_vals_no_args(struct expression *expr)
797 struct return_info ret_info = {};
799 if (!expr || expr->type != EXPR_SYMBOL)
800 return NULL;
802 ret_info.static_returns_call = expr;
803 ret_info.return_type = get_type(expr);
804 ret_info.return_type = get_real_base_type(ret_info.return_type);
805 if (!ret_info.return_type)
806 return NULL;
808 run_sql(db_return_callback, &ret_info,
809 "select distinct return from return_states where %s;",
810 get_static_filter(expr->symbol));
812 return ret_info.return_range_list;
815 static void match_call_marker(struct expression *expr)
817 struct symbol *type;
819 type = get_type(expr->fn);
820 if (type && type->type == SYM_PTR)
821 type = get_real_base_type(type);
824 * we just want to record something in the database so that if we have
825 * two calls like: frob(4); frob(some_unkown); then on the receiving
826 * side we know that sometimes frob is called with unknown parameters.
829 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
832 int is_recursive_member(const char *name)
834 char buf[256];
835 const char *p, *next;
836 int size;
838 p = strchr(name, '>');
839 if (!p)
840 return 0;
841 p++;
842 while (true) {
843 next = strchr(p, '>');
844 if (!next)
845 return 0;
846 next++;
848 size = next - p;
849 if (size >= sizeof(buf))
850 return 0;
851 memcpy(buf, p, size);
852 buf[size] = '\0';
853 if (strstr(next, buf))
854 return 1;
855 p = next;
859 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
861 struct symbol *sym;
862 const char *sm_name;
863 char *name;
864 bool is_address = false;
865 bool add_star = false;
866 char buf[256];
867 char *ret = NULL;
868 int len;
870 expr = strip_expr(expr);
871 if (!expr)
872 return NULL;
874 if (expr->type == EXPR_PREOP && expr->op == '&') {
875 expr = strip_expr(expr->unop);
876 is_address = true;
879 name = expr_to_var_sym(expr, &sym);
880 if (!name || !sym)
881 goto free;
882 if (sym != sm->sym)
883 goto free;
885 sm_name = sm->name;
886 add_star = false;
887 if (sm_name[0] == '*') {
888 add_star = true;
889 sm_name++;
892 len = strlen(name);
893 if (strncmp(name, sm_name, len) != 0)
894 goto free;
895 if (sm_name[len] == '\0') {
896 snprintf(buf, sizeof(buf), "%s%s$",
897 add_star ? "*" : "", is_address ? "*" : "");
898 } else {
899 if (sm_name[len] != '.' && sm_name[len] != '-')
900 goto free;
901 if (sm_name[len] == '-')
902 len++;
903 // FIXME does is_address really imply that sm_name[len] == '-'
904 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
905 sm_name + len);
908 ret = alloc_sname(buf);
909 free:
910 free_string(name);
911 return ret;
914 static void print_struct_members(struct expression *call, struct expression *expr, int param,
915 int owner,
916 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
917 bool new)
919 struct sm_state *sm;
920 const char *sm_name;
921 char *name;
922 struct symbol *sym;
923 int len;
924 char printed_name[256];
925 int is_address = 0;
926 bool add_star;
927 struct symbol *type;
929 expr = strip_expr(expr);
930 if (!expr)
931 return;
932 type = get_type(expr);
933 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
934 return;
936 if (expr->type == EXPR_PREOP && expr->op == '&') {
937 expr = strip_expr(expr->unop);
938 is_address = 1;
941 name = expr_to_var_sym(expr, &sym);
942 if (!name || !sym)
943 goto free;
945 len = strlen(name);
946 FOR_EACH_SM(__get_cur_stree(), sm) {
947 if (sm->owner != owner || sm->sym != sym)
948 continue;
949 sm_name = sm->name;
950 add_star = false;
951 if (sm_name[0] == '*') {
952 add_star = true;
953 sm_name++;
955 // FIXME: simplify?
956 if (!add_star && strcmp(name, sm_name) == 0) {
957 if (is_address) {
958 snprintf(printed_name, sizeof(printed_name), "*$");
959 } else {
960 if (new)
961 snprintf(printed_name, sizeof(printed_name), "$");
962 else
963 continue;
965 } else if (add_star && strcmp(name, sm_name) == 0) {
966 snprintf(printed_name, sizeof(printed_name), "%s*$",
967 is_address ? "*" : "");
968 } else if (strncmp(name, sm_name, len) == 0) {
969 if (sm_name[len] != '.' && sm_name[len] != '-')
970 continue;
971 if (is_address && sm_name[len] == '.') {
972 snprintf(printed_name, sizeof(printed_name),
973 "%s$->%s", add_star ? "*" : "",
974 sm_name + len + 1);
975 } else if (is_address && sm_name[len] == '-') {
976 snprintf(printed_name, sizeof(printed_name),
977 "%s(*$)%s", add_star ? "*" : "",
978 sm_name + len);
979 } else {
980 snprintf(printed_name, sizeof(printed_name),
981 "%s$%s", add_star ? "*" : "",
982 sm_name + len);
984 } else {
985 continue;
987 if (is_recursive_member(printed_name))
988 continue;
989 callback(call, param, printed_name, sm);
990 } END_FOR_EACH_SM(sm);
991 free:
992 free_string(name);
995 static void match_call_info(struct expression *call)
997 struct member_info_callback *cb;
998 struct expression *arg;
999 int i;
1001 FOR_EACH_PTR(member_callbacks, cb) {
1002 i = -1;
1003 FOR_EACH_PTR(call->args, arg) {
1004 i++;
1005 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1006 } END_FOR_EACH_PTR(arg);
1007 } END_FOR_EACH_PTR(cb);
1010 static struct expression *get_fake_variable(struct expression *expr)
1012 struct expression *tmp;
1014 tmp = expr_get_fake_parent_expr(expr);
1015 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1016 return NULL;
1018 return tmp->left;
1021 static void match_call_info_new(struct expression *call)
1023 struct member_info_callback *cb;
1024 struct expression *arg, *tmp;
1025 int i;
1027 FOR_EACH_PTR(member_callbacks_new, cb) {
1028 i = -1;
1029 FOR_EACH_PTR(call->args, arg) {
1030 i++;
1031 tmp = get_fake_variable(arg);
1032 if (!tmp)
1033 tmp = arg;
1034 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1035 } END_FOR_EACH_PTR(arg);
1036 } END_FOR_EACH_PTR(cb);
1039 static int get_param(int param, char **name, struct symbol **sym)
1041 struct symbol *arg;
1042 int i;
1044 i = 0;
1045 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1047 * this is a temporary hack to work around a bug (I think in sparse?)
1048 * 2.6.37-rc1:fs/reiserfs/journal.o
1049 * If there is a function definition without parameter name found
1050 * after a function implementation then it causes a crash.
1051 * int foo() {}
1052 * int bar(char *);
1054 if (arg->ident->name < (char *)100)
1055 continue;
1056 if (i == param) {
1057 *name = arg->ident->name;
1058 *sym = arg;
1059 return TRUE;
1061 i++;
1062 } END_FOR_EACH_PTR(arg);
1064 return FALSE;
1067 static int function_signature_matches(const char *sig)
1069 char *my_sig;
1071 my_sig = function_signature();
1072 if (!sig || !my_sig)
1073 return 1; /* default to matching */
1074 if (strcmp(my_sig, sig) == 0)
1075 return 1;
1076 return 0;
1079 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1081 struct select_caller_info_data *data = _data;
1082 int func_id;
1083 long type;
1084 long param;
1085 char *key;
1086 char *value;
1087 char *name = NULL;
1088 struct symbol *sym = NULL;
1089 struct def_callback *def_callback;
1090 struct stree *stree;
1091 struct timeval cur_time;
1093 data->results = 1;
1095 if (argc != 5)
1096 return 0;
1098 gettimeofday(&cur_time, NULL);
1099 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1100 return 0;
1102 func_id = atoi(argv[0]);
1103 errno = 0;
1104 type = strtol(argv[1], NULL, 10);
1105 param = strtol(argv[2], NULL, 10);
1106 if (errno)
1107 return 0;
1108 key = argv[3];
1109 value = argv[4];
1111 if (data->prev_func_id == -1)
1112 data->prev_func_id = func_id;
1113 if (func_id != data->prev_func_id) {
1114 stree = __pop_fake_cur_stree();
1115 if (!data->ignore)
1116 merge_stree(&data->final_states, stree);
1117 free_stree(&stree);
1118 __push_fake_cur_stree();
1119 __unnullify_path();
1120 data->prev_func_id = func_id;
1121 data->ignore = 0;
1124 if (data->ignore)
1125 return 0;
1126 if (type == INTERNAL &&
1127 !function_signature_matches(value)) {
1128 data->ignore = 1;
1129 return 0;
1132 if (param >= 0 && !get_param(param, &name, &sym))
1133 return 0;
1135 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1136 if (def_callback->hook_type == type)
1137 def_callback->callback(name, sym, key, value);
1138 } END_FOR_EACH_PTR(def_callback);
1140 return 0;
1143 static struct string_list *ptr_names_done;
1144 static struct string_list *ptr_names;
1146 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1148 insert_string(&ptr_names, alloc_string(argv[0]));
1149 return 0;
1152 static char *get_next_ptr_name(void)
1154 char *ptr;
1156 FOR_EACH_PTR(ptr_names, ptr) {
1157 if (!insert_string(&ptr_names_done, ptr))
1158 continue;
1159 return ptr;
1160 } END_FOR_EACH_PTR(ptr);
1161 return NULL;
1164 static void get_ptr_names(const char *file, const char *name)
1166 char sql_filter[1024];
1167 int before, after;
1169 if (file) {
1170 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1171 file, name);
1172 } else {
1173 snprintf(sql_filter, 1024, "function = '%s';", name);
1176 before = ptr_list_size((struct ptr_list *)ptr_names);
1178 run_sql(get_ptr_name, NULL,
1179 "select distinct ptr from function_ptr where %s",
1180 sql_filter);
1182 after = ptr_list_size((struct ptr_list *)ptr_names);
1183 if (before == after)
1184 return;
1186 while ((name = get_next_ptr_name()))
1187 get_ptr_names(NULL, name);
1190 static void match_data_from_db(struct symbol *sym)
1192 struct select_caller_info_data data = { .prev_func_id = -1 };
1193 struct sm_state *sm;
1194 struct stree *stree;
1195 struct timeval end_time;
1197 if (!sym || !sym->ident)
1198 return;
1200 set_fn_mtag(sym);
1201 gettimeofday(&data.start_time, NULL);
1203 __push_fake_cur_stree();
1204 __unnullify_path();
1206 if (!__inline_fn) {
1207 char *ptr;
1209 if (sym->ctype.modifiers & MOD_STATIC)
1210 get_ptr_names(get_base_file(), sym->ident->name);
1211 else
1212 get_ptr_names(NULL, sym->ident->name);
1214 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1215 __free_ptr_list((struct ptr_list **)&ptr_names);
1216 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1217 __free_fake_cur_stree();
1218 return;
1221 sql_select_caller_info(&data,
1222 "call_id, type, parameter, key, value",
1223 sym);
1226 stree = __pop_fake_cur_stree();
1227 if (!data.ignore)
1228 merge_stree(&data.final_states, stree);
1229 free_stree(&stree);
1230 __push_fake_cur_stree();
1231 __unnullify_path();
1232 data.prev_func_id = -1;
1233 data.ignore = 0;
1234 data.results = 0;
1236 FOR_EACH_PTR(ptr_names, ptr) {
1237 run_sql(caller_info_callback, &data,
1238 "select call_id, type, parameter, key, value"
1239 " from common_caller_info where function = '%s' order by call_id",
1240 ptr);
1241 } END_FOR_EACH_PTR(ptr);
1243 if (data.results) {
1244 FOR_EACH_PTR(ptr_names, ptr) {
1245 free_string(ptr);
1246 } END_FOR_EACH_PTR(ptr);
1247 goto free_ptr_names;
1250 FOR_EACH_PTR(ptr_names, ptr) {
1251 run_sql(caller_info_callback, &data,
1252 "select call_id, type, parameter, key, value"
1253 " from caller_info where function = '%s' order by call_id",
1254 ptr);
1255 free_string(ptr);
1256 } END_FOR_EACH_PTR(ptr);
1258 free_ptr_names:
1259 __free_ptr_list((struct ptr_list **)&ptr_names);
1260 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1261 } else {
1262 sql_select_caller_info(&data,
1263 "call_id, type, parameter, key, value",
1264 sym);
1267 stree = __pop_fake_cur_stree();
1268 if (!data.ignore)
1269 merge_stree(&data.final_states, stree);
1270 free_stree(&stree);
1272 gettimeofday(&end_time, NULL);
1273 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1274 FOR_EACH_SM(data.final_states, sm) {
1275 __set_sm(sm);
1276 } END_FOR_EACH_SM(sm);
1279 free_stree(&data.final_states);
1282 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1284 struct implies_info *info = _info;
1285 struct db_implies_callback *cb;
1286 struct expression *arg = NULL;
1287 int type;
1288 int param;
1290 if (argc != 5)
1291 return 0;
1293 type = atoi(argv[1]);
1294 param = atoi(argv[2]);
1296 FOR_EACH_PTR(info->cb_list, cb) {
1297 if (cb->type != type)
1298 continue;
1299 if (param != -1) {
1300 arg = get_argument_from_call_expr(info->expr->args, param);
1301 if (!arg)
1302 continue;
1304 cb->callback(info->expr, arg, argv[3], argv[4]);
1305 } END_FOR_EACH_PTR(cb);
1307 return 0;
1310 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1312 struct implies_info *info = _info;
1313 struct db_implies_callback *cb;
1314 struct expression *arg;
1315 struct symbol *sym;
1316 char *name;
1317 int type;
1318 int param;
1320 if (argc != 5)
1321 return 0;
1323 type = atoi(argv[1]);
1324 param = atoi(argv[2]);
1326 if (!get_param(param, &name, &sym))
1327 return 0;
1328 arg = symbol_expression(sym);
1329 if (!arg)
1330 return 0;
1332 FOR_EACH_PTR(info->cb_list, cb) {
1333 if (cb->type != type)
1334 continue;
1335 cb->callback(info->expr, arg, argv[3], argv[4]);
1336 } END_FOR_EACH_PTR(cb);
1338 return 0;
1341 static void match_return_implies(struct expression *expr)
1343 struct implies_info info = {
1344 .type = RETURN_IMPLIES,
1345 .cb_list = return_implies_cb_list,
1348 if (expr->fn->type != EXPR_SYMBOL ||
1349 !expr->fn->symbol)
1350 return;
1351 info.expr = expr;
1352 info.sym = expr->fn->symbol;
1353 sql_select_implies("function, type, parameter, key, value", &info,
1354 return_implies_callbacks);
1357 static void match_call_implies(struct symbol *sym)
1359 struct implies_info info = {
1360 .type = CALL_IMPLIES,
1361 .cb_list = call_implies_cb_list,
1364 if (!sym || !sym->ident)
1365 return;
1367 info.sym = sym;
1368 sql_select_implies("function, type, parameter, key, value", &info,
1369 call_implies_callbacks);
1372 static char *get_fn_param_str(struct expression *expr)
1374 struct expression *tmp;
1375 int param;
1376 char buf[32];
1378 tmp = get_assigned_expr(expr);
1379 if (tmp)
1380 expr = tmp;
1381 expr = strip_expr(expr);
1382 if (!expr || expr->type != EXPR_CALL)
1383 return NULL;
1384 expr = strip_expr(expr->fn);
1385 if (!expr || expr->type != EXPR_SYMBOL)
1386 return NULL;
1387 param = get_param_num(expr);
1388 if (param < 0)
1389 return NULL;
1391 snprintf(buf, sizeof(buf), "[r $%d]", param);
1392 return alloc_sname(buf);
1395 static char *get_return_compare_is_param(struct expression *expr)
1397 char *var;
1398 char buf[256];
1399 int comparison;
1400 int param;
1402 param = get_param_num(expr);
1403 if (param < 0)
1404 return NULL;
1406 var = expr_to_var(expr);
1407 if (!var)
1408 return NULL;
1409 snprintf(buf, sizeof(buf), "%s orig", var);
1410 comparison = get_comparison_strings(var, buf);
1411 free_string(var);
1413 if (!comparison)
1414 return NULL;
1416 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1417 return alloc_sname(buf);
1420 static char *get_return_compare_str(struct expression *expr)
1422 char *compare_str;
1424 compare_str = get_return_compare_is_param(expr);
1425 if (compare_str)
1426 return compare_str;
1428 compare_str = expr_lte_to_param(expr, -1);
1429 if (compare_str)
1430 return compare_str;
1432 return expr_param_comparison(expr, -1);
1435 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1437 struct range_list *rl;
1438 char *return_ranges;
1439 sval_t sval;
1440 char *fn_param_str;
1441 char *compare_str;
1442 char *math_str;
1443 char buf[128];
1445 *rl_p = NULL;
1447 if (!expr)
1448 return alloc_sname("");
1450 if (get_implied_value(expr, &sval)) {
1451 sval = sval_cast(cur_func_return_type(), sval);
1452 *rl_p = alloc_rl(sval, sval);
1453 return sval_to_str_or_err_ptr(sval);
1456 fn_param_str = get_fn_param_str(expr);
1457 compare_str = expr_equal_to_param(expr, -1);
1458 math_str = get_value_in_terms_of_parameter_math(expr);
1460 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1461 rl = cast_rl(cur_func_return_type(), rl);
1462 return_ranges = show_rl(rl);
1463 } else if (get_imaginary_absolute(expr, &rl)){
1464 rl = cast_rl(cur_func_return_type(), rl);
1465 return alloc_sname(show_rl(rl));
1466 } else {
1467 get_absolute_rl(expr, &rl);
1468 rl = cast_rl(cur_func_return_type(), rl);
1469 return_ranges = show_rl(rl);
1471 *rl_p = rl;
1473 if (fn_param_str) {
1474 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1475 return alloc_sname(buf);
1477 if (compare_str) {
1478 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1479 return alloc_sname(buf);
1481 if (math_str) {
1482 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1483 return alloc_sname(buf);
1485 compare_str = get_return_compare_str(expr);
1486 if (compare_str) {
1487 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1488 return alloc_sname(buf);
1491 return return_ranges;
1494 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1496 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1499 static bool call_return_state_hooks_conditional(struct expression *expr)
1501 int final_pass_orig = final_pass;
1502 static int recurse;
1504 if (recurse >= 2)
1505 return false;
1506 if (!expr ||
1507 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1508 return false;
1510 recurse++;
1512 __push_fake_cur_stree();
1514 final_pass = 0;
1515 __split_whole_condition(expr->conditional);
1516 final_pass = final_pass_orig;
1518 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1520 __push_true_states();
1521 __use_false_states();
1523 call_return_state_hooks(expr->cond_false);
1525 __merge_true_states();
1526 __free_fake_cur_stree();
1528 recurse--;
1529 return true;
1532 static void call_return_state_hooks_compare(struct expression *expr)
1534 struct returned_state_callback *cb;
1535 char *return_ranges;
1536 int final_pass_orig = final_pass;
1537 sval_t sval = { .type = &int_ctype };
1538 sval_t ret;
1540 if (!get_implied_value(expr, &ret))
1541 ret.value = -1;
1543 __push_fake_cur_stree();
1545 final_pass = 0;
1546 __split_whole_condition(expr);
1547 final_pass = final_pass_orig;
1549 if (ret.value != 0) {
1550 return_ranges = alloc_sname("1");
1551 sval.value = 1;
1552 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1554 return_id++;
1555 FOR_EACH_PTR(returned_state_callbacks, cb) {
1556 cb->callback(return_id, return_ranges, expr);
1557 } END_FOR_EACH_PTR(cb);
1560 __push_true_states();
1561 __use_false_states();
1563 if (ret.value != 1) {
1564 return_ranges = alloc_sname("0");
1565 sval.value = 0;
1566 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1568 return_id++;
1569 FOR_EACH_PTR(returned_state_callbacks, cb) {
1570 cb->callback(return_id, return_ranges, expr);
1571 } END_FOR_EACH_PTR(cb);
1574 __merge_true_states();
1575 __free_fake_cur_stree();
1578 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1580 struct sm_state *tmp;
1582 FOR_EACH_PTR(slist, tmp) {
1583 if (strcmp(tmp->state->name, sm->state->name) == 0)
1584 return 1;
1585 } END_FOR_EACH_PTR(tmp);
1587 return 0;
1590 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1592 struct returned_state_callback *cb;
1593 struct range_list *rl;
1594 char *return_ranges;
1595 struct sm_state *tmp;
1596 int ret = 0;
1597 int nr_possible, nr_states;
1598 char *compare_str;
1599 char buf[128];
1600 struct state_list *already_handled = NULL;
1601 sval_t sval;
1603 if (!sm || !sm->merged)
1604 return 0;
1606 if (too_many_possible(sm))
1607 return 0;
1609 /* bail if it gets too complicated */
1610 nr_possible = 0;
1611 FOR_EACH_PTR(sm->possible, tmp) {
1612 if (tmp->merged)
1613 continue;
1614 if (ptr_in_list(tmp, already_handled))
1615 continue;
1616 add_ptr_list(&already_handled, tmp);
1617 nr_possible++;
1618 } END_FOR_EACH_PTR(tmp);
1619 free_slist(&already_handled);
1620 nr_states = get_db_state_count();
1621 if (nr_states * nr_possible >= 2000)
1622 return 0;
1624 FOR_EACH_PTR(sm->possible, tmp) {
1625 if (tmp->merged)
1626 continue;
1627 if (ptr_in_list(tmp, already_handled))
1628 continue;
1629 add_ptr_list(&already_handled, tmp);
1631 ret = 1;
1632 __push_fake_cur_stree();
1634 overwrite_states_using_pool(sm, tmp);
1636 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1637 return_ranges = show_rl(rl);
1638 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1639 compare_str = get_return_compare_str(expr);
1640 /* ignore obvious stuff like 0 <= param */
1641 /* Is this worthile when we have PARAM_COMPARE? */
1642 if (compare_str &&
1643 strncmp(compare_str, "[=", 2) != 0 &&
1644 rl_to_sval(rl, &sval))
1645 compare_str = NULL;
1646 if (compare_str) {
1647 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1648 return_ranges = alloc_sname(buf);
1651 return_id++;
1652 FOR_EACH_PTR(returned_state_callbacks, cb) {
1653 cb->callback(return_id, return_ranges, expr);
1654 } END_FOR_EACH_PTR(cb);
1656 __free_fake_cur_stree();
1657 } END_FOR_EACH_PTR(tmp);
1659 free_slist(&already_handled);
1661 return ret;
1664 static int call_return_state_hooks_split_possible(struct expression *expr)
1666 struct expression *fake;
1667 struct sm_state *sm;
1669 if (!expr)
1670 return 0;
1672 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1673 if (!sm) {
1674 fake = expr_get_fake_parent_expr(expr);
1675 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1676 return 0;
1677 fake = fake->left;
1678 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1680 return split_possible_helper(sm, expr);
1683 static bool has_possible_negative(struct sm_state *sm)
1685 struct sm_state *tmp;
1687 if (!type_signed(estate_type(sm->state)))
1688 return false;
1690 FOR_EACH_PTR(sm->possible, tmp) {
1691 if (!estate_rl(tmp->state))
1692 continue;
1693 if (sval_is_negative(estate_min(tmp->state)) &&
1694 sval_is_negative(estate_max(tmp->state)))
1695 return true;
1696 } END_FOR_EACH_PTR(tmp);
1698 return false;
1701 static bool has_separate_zero_null(struct sm_state *sm)
1703 struct sm_state *tmp;
1704 sval_t sval;
1706 FOR_EACH_PTR(sm->possible, tmp) {
1707 if (!estate_get_single_value(tmp->state, &sval))
1708 continue;
1709 if (sval.value == 0)
1710 return true;
1711 } END_FOR_EACH_PTR(tmp);
1713 return false;
1716 static int split_positive_from_negative(struct expression *expr)
1718 struct sm_state *sm;
1719 struct returned_state_callback *cb;
1720 struct range_list *rl;
1721 const char *return_ranges;
1722 struct range_list *ret_rl;
1723 bool separate_zero;
1724 int undo;
1726 /* We're going to print the states 3 times */
1727 if (get_db_state_count() > 10000 / 3)
1728 return 0;
1730 if (!get_implied_rl(expr, &rl) || !rl)
1731 return 0;
1732 /* Forget about INT_MAX and larger */
1733 if (rl_max(rl).value <= 0)
1734 return 0;
1735 if (!sval_is_negative(rl_min(rl)))
1736 return 0;
1738 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1739 if (!sm)
1740 return 0;
1741 if (!has_possible_negative(sm))
1742 return 0;
1743 separate_zero = has_separate_zero_null(sm);
1745 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1746 return 0;
1748 return_id++;
1749 return_ranges = get_return_ranges_str(expr, &ret_rl);
1750 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1751 FOR_EACH_PTR(returned_state_callbacks, cb) {
1752 cb->callback(return_id, (char *)return_ranges, expr);
1753 } END_FOR_EACH_PTR(cb);
1755 end_assume();
1757 if (separate_zero) {
1758 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1760 return_id++;
1761 return_ranges = get_return_ranges_str(expr, &ret_rl);
1762 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1763 FOR_EACH_PTR(returned_state_callbacks, cb) {
1764 cb->callback(return_id, (char *)return_ranges, expr);
1765 } END_FOR_EACH_PTR(cb);
1767 if (undo)
1768 end_assume();
1771 undo = assume(compare_expression(expr, '<', zero_expr()));
1773 return_id++;
1774 return_ranges = get_return_ranges_str(expr, &ret_rl);
1775 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1776 FOR_EACH_PTR(returned_state_callbacks, cb) {
1777 cb->callback(return_id, (char *)return_ranges, expr);
1778 } END_FOR_EACH_PTR(cb);
1780 if (undo)
1781 end_assume();
1783 return 1;
1786 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1788 struct returned_state_callback *cb;
1789 struct range_list *rl;
1790 struct range_list *nonnull_rl;
1791 sval_t null_sval;
1792 struct range_list *null_rl = NULL;
1793 char *return_ranges;
1794 struct sm_state *sm;
1795 struct smatch_state *state;
1796 int nr_states;
1797 int final_pass_orig = final_pass;
1799 if (!expr || expr_equal_to_param(expr, -1))
1800 return 0;
1801 if (expr->type == EXPR_CALL)
1802 return 0;
1804 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1805 if (!sm)
1806 return 0;
1807 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1808 return 0;
1809 state = sm->state;
1810 if (!estate_rl(state))
1811 return 0;
1812 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1813 return 0;
1814 if (has_possible_negative(sm))
1815 return 0;
1816 if (!has_separate_zero_null(sm))
1817 return 0;
1819 nr_states = get_db_state_count();
1820 if (option_info && nr_states >= 1500)
1821 return 0;
1823 rl = estate_rl(state);
1825 __push_fake_cur_stree();
1827 final_pass = 0;
1828 __split_whole_condition(expr);
1829 final_pass = final_pass_orig;
1831 nonnull_rl = rl_filter(rl, rl_zero());
1832 return_ranges = show_rl(nonnull_rl);
1833 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1835 return_id++;
1836 FOR_EACH_PTR(returned_state_callbacks, cb) {
1837 cb->callback(return_id, return_ranges, expr);
1838 } END_FOR_EACH_PTR(cb);
1840 __push_true_states();
1841 __use_false_states();
1843 return_ranges = alloc_sname("0");
1844 null_sval = sval_type_val(rl_type(rl), 0);
1845 add_range(&null_rl, null_sval, null_sval);
1846 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1847 return_id++;
1848 FOR_EACH_PTR(returned_state_callbacks, cb) {
1849 cb->callback(return_id, return_ranges, expr);
1850 } END_FOR_EACH_PTR(cb);
1852 __merge_true_states();
1853 __free_fake_cur_stree();
1855 return 1;
1858 static bool is_kernel_success_fail(struct sm_state *sm)
1860 struct sm_state *tmp;
1861 struct range_list *rl;
1862 bool has_zero = false;
1863 bool has_neg = false;
1865 if (!type_signed(estate_type(sm->state)))
1866 return false;
1868 FOR_EACH_PTR(sm->possible, tmp) {
1869 rl = estate_rl(tmp->state);
1870 if (!rl)
1871 return false;
1872 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1873 has_zero = true;
1874 continue;
1876 has_neg = true;
1877 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1878 continue;
1879 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1880 continue;
1881 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1882 continue;
1883 return false;
1884 } END_FOR_EACH_PTR(tmp);
1886 return has_zero && has_neg;
1889 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1891 struct sm_state *sm;
1892 struct range_list *rl;
1893 struct range_list *nonzero_rl;
1894 sval_t zero_sval;
1895 struct range_list *zero_rl = NULL;
1896 int nr_states;
1897 struct returned_state_callback *cb;
1898 char *return_ranges;
1899 int final_pass_orig = final_pass;
1901 if (option_project != PROJ_KERNEL)
1902 return 0;
1904 nr_states = get_db_state_count();
1905 if (nr_states > 2000)
1906 return 0;
1908 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1909 if (!sm)
1910 return 0;
1911 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1912 return 0;
1913 if (!is_kernel_success_fail(sm))
1914 return 0;
1916 rl = estate_rl(sm->state);
1917 if (!rl)
1918 return 0;
1920 __push_fake_cur_stree();
1922 final_pass = 0;
1923 __split_whole_condition(expr);
1924 final_pass = final_pass_orig;
1926 nonzero_rl = rl_filter(rl, rl_zero());
1927 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1928 return_ranges = show_rl(nonzero_rl);
1929 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1931 return_id++;
1932 FOR_EACH_PTR(returned_state_callbacks, cb) {
1933 cb->callback(return_id, return_ranges, expr);
1934 } END_FOR_EACH_PTR(cb);
1936 __push_true_states();
1937 __use_false_states();
1939 return_ranges = alloc_sname("0");
1940 zero_sval = sval_type_val(rl_type(rl), 0);
1941 add_range(&zero_rl, zero_sval, zero_sval);
1942 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1943 return_id++;
1944 FOR_EACH_PTR(returned_state_callbacks, cb) {
1945 cb->callback(return_id, return_ranges, expr);
1946 } END_FOR_EACH_PTR(cb);
1948 __merge_true_states();
1949 __free_fake_cur_stree();
1951 return 1;
1954 static int is_boolean(struct expression *expr)
1956 struct range_list *rl;
1958 if (!get_implied_rl(expr, &rl))
1959 return 0;
1960 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1961 return 1;
1962 return 0;
1965 static int splitable_function_call(struct expression *expr)
1967 struct sm_state *sm;
1969 if (!expr || expr->type != EXPR_CALL)
1970 return 0;
1971 sm = get_extra_sm_state(expr);
1972 return split_possible_helper(sm, expr);
1975 static struct sm_state *find_bool_param(void)
1977 struct stree *start_states;
1978 struct symbol *arg;
1979 struct sm_state *sm, *tmp;
1980 sval_t sval;
1982 start_states = get_start_states();
1984 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1985 if (!arg->ident)
1986 continue;
1987 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1988 if (!sm)
1989 continue;
1990 if (rl_min(estate_rl(sm->state)).value != 0 ||
1991 rl_max(estate_rl(sm->state)).value != 1)
1992 continue;
1993 goto found;
1994 } END_FOR_EACH_PTR_REVERSE(arg);
1996 return NULL;
1998 found:
2000 * Check if it's splitable. If not, then splitting it up is likely not
2001 * useful for the callers.
2003 FOR_EACH_PTR(sm->possible, tmp) {
2004 if (is_merged(tmp))
2005 continue;
2006 if (!estate_get_single_value(tmp->state, &sval))
2007 return NULL;
2008 } END_FOR_EACH_PTR(tmp);
2010 return sm;
2013 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2015 struct returned_state_callback *cb;
2016 struct range_list *ret_rl;
2017 const char *return_ranges;
2018 struct sm_state *tmp;
2019 int ret = 0;
2020 struct state_list *already_handled = NULL;
2022 if (!sm || !sm->merged)
2023 return 0;
2025 if (too_many_possible(sm))
2026 return 0;
2028 FOR_EACH_PTR(sm->possible, tmp) {
2029 if (tmp->merged)
2030 continue;
2031 if (ptr_in_list(tmp, already_handled))
2032 continue;
2033 add_ptr_list(&already_handled, tmp);
2035 ret = 1;
2036 __push_fake_cur_stree();
2038 overwrite_states_using_pool(sm, tmp);
2040 return_ranges = get_return_ranges_str(expr, &ret_rl);
2041 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2042 return_id++;
2043 FOR_EACH_PTR(returned_state_callbacks, cb) {
2044 cb->callback(return_id, (char *)return_ranges, expr);
2045 } END_FOR_EACH_PTR(cb);
2047 __free_fake_cur_stree();
2048 } END_FOR_EACH_PTR(tmp);
2050 free_slist(&already_handled);
2052 return ret;
2055 static int split_by_bool_param(struct expression *expr)
2057 struct sm_state *start_sm, *sm;
2058 sval_t sval;
2060 start_sm = find_bool_param();
2061 if (!start_sm)
2062 return 0;
2063 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2064 if (!sm || estate_get_single_value(sm->state, &sval))
2065 return 0;
2067 if (get_db_state_count() * 2 >= 2000)
2068 return 0;
2070 return split_on_bool_sm(sm, expr);
2073 static int split_by_null_nonnull_param(struct expression *expr)
2075 struct symbol *arg;
2076 struct sm_state *sm;
2077 int nr_possible;
2079 /* function must only take one pointer */
2080 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2081 return 0;
2082 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2083 if (!arg->ident)
2084 return 0;
2085 if (get_real_base_type(arg)->type != SYM_PTR)
2086 return 0;
2088 if (param_was_set_var_sym(arg->ident->name, arg))
2089 return 0;
2090 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2091 if (!sm)
2092 return 0;
2094 if (!has_separate_zero_null(sm))
2095 return 0;
2097 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2098 if (get_db_state_count() * nr_possible >= 2000)
2099 return 0;
2101 return split_on_bool_sm(sm, expr);
2104 struct expression *strip_expr_statement(struct expression *expr)
2106 struct expression *orig = expr;
2107 struct statement *stmt, *last_stmt;
2109 if (!expr)
2110 return NULL;
2111 if (expr->type == EXPR_PREOP && expr->op == '(')
2112 expr = expr->unop;
2113 if (expr->type != EXPR_STATEMENT)
2114 return orig;
2115 stmt = expr->statement;
2116 if (!stmt || stmt->type != STMT_COMPOUND)
2117 return orig;
2119 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2120 if (!last_stmt || last_stmt->type == STMT_LABEL)
2121 last_stmt = last_stmt->label_statement;
2122 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2123 return orig;
2124 return strip_expr(last_stmt->expression);
2127 static bool is_kernel_error_path(struct expression *expr)
2129 struct range_list *rl;
2132 * Splitting up returns requires resources. It also requires resources
2133 * for the caller. It doesn't seem worth it to split anything up.
2135 if (!get_implied_rl(expr, &rl))
2136 return false;
2137 if (rl_type(rl) != &int_ctype)
2138 return false;
2139 if (rl_min(rl).value >= -4095 &&
2140 rl_max(rl).value < 0)
2141 return true;
2142 return false;
2145 static void call_return_state_hooks(struct expression *expr)
2147 struct returned_state_callback *cb;
2148 struct range_list *ret_rl;
2149 const char *return_ranges;
2150 int nr_states;
2151 sval_t sval;
2153 if (__path_is_null())
2154 return;
2156 expr = strip_expr(expr);
2157 expr = strip_expr_statement(expr);
2159 if (is_impossible_path())
2160 goto vanilla;
2162 if (expr && (expr->type == EXPR_COMPARE ||
2163 !get_implied_value(expr, &sval)) &&
2164 (is_condition(expr) || is_boolean(expr))) {
2165 call_return_state_hooks_compare(expr);
2166 return;
2167 } else if (call_return_state_hooks_conditional(expr)) {
2168 return;
2169 } else if (is_kernel_error_path(expr)) {
2170 goto vanilla;
2171 } else if (call_return_state_hooks_split_possible(expr)) {
2172 return;
2173 } else if (split_positive_from_negative(expr)) {
2174 return;
2175 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2176 return;
2177 } else if (call_return_state_hooks_split_success_fail(expr)) {
2178 return;
2179 } else if (splitable_function_call(expr)) {
2180 return;
2181 } else if (split_by_bool_param(expr)) {
2182 } else if (split_by_null_nonnull_param(expr)) {
2183 return;
2186 vanilla:
2187 return_ranges = get_return_ranges_str(expr, &ret_rl);
2188 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2190 return_id++;
2191 nr_states = get_db_state_count();
2192 if (nr_states >= 10000) {
2193 match_return_info(return_id, (char *)return_ranges, expr);
2194 print_limited_param_set(return_id, (char *)return_ranges, expr);
2195 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2196 return;
2198 FOR_EACH_PTR(returned_state_callbacks, cb) {
2199 cb->callback(return_id, (char *)return_ranges, expr);
2200 } END_FOR_EACH_PTR(cb);
2203 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2205 struct returned_member_callback *cb;
2206 struct sm_state *sm;
2207 struct symbol *type;
2208 char *name;
2209 char member_name[256];
2210 int len;
2212 type = get_type(expr);
2213 if (!type || type->type != SYM_PTR)
2214 return;
2215 name = expr_to_var(expr);
2216 if (!name)
2217 return;
2219 len = strlen(name);
2220 FOR_EACH_PTR(returned_member_callbacks, cb) {
2221 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2222 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2223 strcpy(member_name, "*$");
2224 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2225 continue;
2227 if (strncmp(sm->name, name, len) != 0)
2228 continue;
2229 if (strncmp(sm->name + len, "->", 2) != 0)
2230 continue;
2231 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2232 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2233 } END_FOR_EACH_SM(sm);
2234 } END_FOR_EACH_PTR(cb);
2236 free_string(name);
2239 static void print_return_struct_info(int return_id, char *return_ranges,
2240 struct expression *expr,
2241 struct symbol *sym,
2242 struct return_info_callback *cb)
2244 struct sm_state *sm;
2245 const char *printed_name;
2246 int param;
2248 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2249 if (sm->sym && sm->sym == sym) {
2250 param = -1;
2251 } else {
2252 param = get_param_num_from_sym(sm->sym);
2253 if (param < 0)
2254 continue;
2257 printed_name = get_param_name(sm);
2258 if (!printed_name)
2259 continue;
2261 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2262 } END_FOR_EACH_SM(sm);
2265 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2267 struct return_info_callback *cb;
2268 struct expression *tmp;
2269 struct symbol *sym;
2271 tmp = get_fake_variable(expr);
2272 if (tmp)
2273 expr = tmp;
2274 sym = expr_to_sym(expr);
2276 FOR_EACH_PTR(return_callbacks, cb) {
2277 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2278 } END_FOR_EACH_PTR(cb);
2281 static void reset_memdb(struct symbol *sym)
2283 mem_sql(NULL, NULL, "delete from caller_info;");
2284 mem_sql(NULL, NULL, "delete from return_states;");
2285 mem_sql(NULL, NULL, "delete from call_implies;");
2286 mem_sql(NULL, NULL, "delete from return_implies;");
2289 static void match_end_func_info(struct symbol *sym)
2291 if (__path_is_null())
2292 return;
2293 call_return_state_hooks(NULL);
2296 static void match_after_func(struct symbol *sym)
2298 if (!__inline_fn)
2299 reset_memdb(sym);
2302 static void init_memdb(void)
2304 char *err = NULL;
2305 int rc;
2306 const char *schema_files[] = {
2307 "db/db.schema",
2308 "db/caller_info.schema",
2309 "db/common_caller_info.schema",
2310 "db/return_states.schema",
2311 "db/function_type_size.schema",
2312 "db/type_size.schema",
2313 "db/function_type_info.schema",
2314 "db/type_info.schema",
2315 "db/call_implies.schema",
2316 "db/return_implies.schema",
2317 "db/function_ptr.schema",
2318 "db/local_values.schema",
2319 "db/function_type_value.schema",
2320 "db/type_value.schema",
2321 "db/function_type.schema",
2322 "db/data_info.schema",
2323 "db/parameter_name.schema",
2324 "db/constraints.schema",
2325 "db/constraints_required.schema",
2326 "db/fn_ptr_data_link.schema",
2327 "db/fn_data_link.schema",
2328 "db/mtag_about.schema",
2329 "db/mtag_info.schema",
2330 "db/mtag_map.schema",
2331 "db/mtag_data.schema",
2332 "db/mtag_alias.schema",
2334 static char buf[4096];
2335 int fd;
2336 int ret;
2337 int i;
2339 rc = sqlite3_open(":memory:", &mem_db);
2340 if (rc != SQLITE_OK) {
2341 sm_ierror("starting In-Memory database.");
2342 return;
2345 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2346 fd = open_schema_file(schema_files[i]);
2347 if (fd < 0)
2348 continue;
2349 ret = read(fd, buf, sizeof(buf));
2350 if (ret < 0) {
2351 sm_ierror("failed to read: %s", schema_files[i]);
2352 continue;
2354 close(fd);
2355 if (ret == sizeof(buf)) {
2356 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2357 schema_files[i], sizeof(buf));
2358 continue;
2360 buf[ret] = '\0';
2361 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2362 if (rc != SQLITE_OK) {
2363 sm_ierror("SQL error #2: %s", err);
2364 sm_ierror("%s", buf);
2369 static void init_cachedb(void)
2371 char *err = NULL;
2372 int rc;
2373 const char *schema_files[] = {
2374 "db/call_implies.schema",
2375 "db/return_implies.schema",
2376 "db/type_info.schema",
2377 "db/mtag_about.schema",
2378 "db/mtag_data.schema",
2379 "db/mtag_info.schema",
2380 "db/sink_info.schema",
2382 static char buf[4096];
2383 int fd;
2384 int ret;
2385 int i;
2387 rc = sqlite3_open(":memory:", &cache_db);
2388 if (rc != SQLITE_OK) {
2389 sm_ierror("starting In-Memory database.");
2390 return;
2393 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2394 fd = open_schema_file(schema_files[i]);
2395 if (fd < 0)
2396 continue;
2397 ret = read(fd, buf, sizeof(buf));
2398 if (ret < 0) {
2399 sm_ierror("failed to read: %s", schema_files[i]);
2400 continue;
2402 close(fd);
2403 if (ret == sizeof(buf)) {
2404 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2405 schema_files[i], sizeof(buf));
2406 continue;
2408 buf[ret] = '\0';
2409 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2410 if (rc != SQLITE_OK) {
2411 sm_ierror("SQL error #2: %s", err);
2412 sm_ierror("%s", buf);
2417 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2419 static char buf[4096];
2420 char tmp[256];
2421 char *p = buf;
2422 char *table = _table;
2423 int i;
2426 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2427 for (i = 0; i < argc; i++) {
2428 if (i)
2429 p += snprintf(p, 4096 - (p - buf), ", ");
2430 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2431 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2434 p += snprintf(p, 4096 - (p - buf), ");");
2435 if (p - buf > 4096)
2436 return 0;
2438 sm_msg("SQL: %s", buf);
2439 return 0;
2442 static void dump_cache(struct symbol_list *sym_list)
2444 const char *cache_tables[] = {
2445 "type_info", "return_implies", "call_implies", "mtag_data",
2446 "mtag_info", "mtag_about", "sink_info",
2448 char buf[64];
2449 int i;
2451 if (!option_info)
2452 return;
2454 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2455 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2456 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2460 void open_smatch_db(char *db_file)
2462 int rc;
2464 if (option_no_db)
2465 return;
2467 use_states = malloc(num_checks + 1);
2468 memset(use_states, 0xff, num_checks + 1);
2470 init_memdb();
2471 init_cachedb();
2473 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2474 if (rc != SQLITE_OK) {
2475 option_no_db = 1;
2476 return;
2478 run_sql(NULL, NULL,
2479 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2480 return;
2483 static void register_common_funcs(void)
2485 struct token *token;
2486 char *func;
2487 char filename[256];
2489 if (option_project == PROJ_NONE)
2490 strcpy(filename, "common_functions");
2491 else
2492 snprintf(filename, 256, "%s.common_functions", option_project_str);
2494 token = get_tokens_file(filename);
2495 if (!token)
2496 return;
2497 if (token_type(token) != TOKEN_STREAMBEGIN)
2498 return;
2499 token = token->next;
2500 while (token_type(token) != TOKEN_STREAMEND) {
2501 if (token_type(token) != TOKEN_IDENT)
2502 return;
2503 func = alloc_string(show_ident(token->ident));
2504 add_ptr_list(&common_funcs, func);
2505 token = token->next;
2507 clear_token_alloc();
2510 static char *get_next_string(char **str)
2512 static char string[256];
2513 char *start;
2514 char *p = *str;
2515 int len, i, j;
2517 if (*p == '\0')
2518 return NULL;
2519 start = p;
2521 while (*p != '\0' && *p != '\n') {
2522 if (*p == '\\' && *(p + 1) == ' ') {
2523 p += 2;
2524 continue;
2526 if (*p == ' ')
2527 break;
2528 p++;
2531 len = p - start;
2532 if (len >= sizeof(string)) {
2533 memcpy(string, start, sizeof(string));
2534 string[sizeof(string) - 1] = '\0';
2535 sm_ierror("return_fix: '%s' too long", string);
2536 **str = '\0';
2537 return NULL;
2539 memcpy(string, start, len);
2540 string[len] = '\0';
2541 for (i = 0; i < sizeof(string) - 1; i++) {
2542 if (string[i] == '\\' && string[i + 1] == ' ') {
2543 for (j = i; string[j] != '\0'; j++)
2544 string[j] = string[j + 1];
2547 if (*p != '\0')
2548 p++;
2549 *str = p;
2550 return string;
2553 static void register_return_replacements(void)
2555 char *func, *orig, *new;
2556 char filename[256];
2557 char buf[4096];
2558 int fd, ret, i;
2559 char *p;
2561 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2562 fd = open_schema_file(filename);
2563 if (fd < 0)
2564 return;
2565 ret = read(fd, buf, sizeof(buf));
2566 close(fd);
2567 if (ret < 0)
2568 return;
2569 if (ret == sizeof(buf)) {
2570 sm_ierror("file too large: %s (limit %zd bytes)",
2571 filename, sizeof(buf));
2572 return;
2574 buf[ret] = '\0';
2576 p = buf;
2577 while (*p) {
2578 get_next_string(&p);
2579 replace_count++;
2581 if (replace_count == 0 || replace_count % 3 != 0) {
2582 replace_count = 0;
2583 return;
2585 replace_table = malloc(replace_count * sizeof(char *));
2587 p = buf;
2588 i = 0;
2589 while (*p) {
2590 func = alloc_string(get_next_string(&p));
2591 orig = alloc_string(get_next_string(&p));
2592 new = alloc_string(get_next_string(&p));
2594 replace_table[i++] = func;
2595 replace_table[i++] = orig;
2596 replace_table[i++] = new;
2600 void register_definition_db_callbacks(int id)
2602 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2603 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2604 add_split_return_callback(match_return_info);
2605 add_split_return_callback(print_returned_struct_members);
2606 add_split_return_callback(print_return_info);
2607 add_hook(&call_return_state_hooks, RETURN_HOOK);
2608 add_hook(&match_end_func_info, END_FUNC_HOOK);
2609 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2611 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2612 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2613 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2615 register_common_funcs();
2616 register_return_replacements();
2618 add_hook(&dump_cache, END_FILE_HOOK);
2621 void register_db_call_marker(int id)
2623 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2626 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2628 struct expression *arg;
2629 char *name = NULL;
2630 char member_name[256];
2632 *sym = NULL;
2634 if (param == -1) {
2635 const char *star = "";
2637 if (expr->type != EXPR_ASSIGNMENT)
2638 return NULL;
2639 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2640 return NULL;
2641 name = expr_to_var_sym(expr->left, sym);
2642 if (!name)
2643 return NULL;
2644 if (key[0] == '*') {
2645 star = "*";
2646 key++;
2648 if (strncmp(key, "$", 1) != 0)
2649 return name;
2650 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2651 free_string(name);
2652 return alloc_string(member_name);
2655 while (expr->type == EXPR_ASSIGNMENT)
2656 expr = strip_expr(expr->right);
2657 if (expr->type != EXPR_CALL)
2658 return NULL;
2660 arg = get_argument_from_call_expr(expr->args, param);
2661 if (!arg)
2662 return NULL;
2664 return get_variable_from_key(arg, key, sym);
2667 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2669 struct symbol *type;
2670 char buf[256];
2671 char *tmp;
2672 int star_cnt = 0;
2673 bool add_dot = false;
2675 if (!arg)
2676 return NULL;
2678 arg = strip_expr(arg);
2680 if (strcmp(key, "$") == 0)
2681 return expr_to_var_sym(arg, sym);
2683 if (strcmp(key, "*$") == 0) {
2684 if (arg->type == EXPR_PREOP && arg->op == '&') {
2685 arg = strip_expr(arg->unop);
2686 return expr_to_var_sym(arg, sym);
2687 } else {
2688 tmp = expr_to_var_sym(arg, sym);
2689 if (!tmp)
2690 return NULL;
2691 snprintf(buf, sizeof(buf), "*%s", tmp);
2692 free_string(tmp);
2693 return alloc_string(buf);
2697 if (strncmp(key, "(*$)", 4) == 0) {
2698 char buf[64];
2700 if (arg->type == EXPR_PREOP && arg->op == '&') {
2701 arg = strip_expr(arg->unop);
2702 snprintf(buf, sizeof(buf), "$%s", key + 4);
2703 return get_variable_from_key(arg, buf, sym);
2704 } else {
2705 tmp = expr_to_var_sym(arg, sym);
2706 if (!tmp)
2707 return NULL;
2708 snprintf(buf, sizeof(buf), "(*%s)%s", tmp, key + 4);
2709 free_string(tmp);
2710 return alloc_string(buf);
2714 while (key[0] == '*') {
2715 star_cnt++;
2716 key++;
2720 * FIXME: This is a hack.
2721 * We should be able to parse expressions like (*$)->foo and *$->foo.
2723 type = get_type(arg);
2724 if (is_struct_ptr(type))
2725 add_dot = true;
2727 if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt && !add_dot) {
2728 arg = strip_expr(arg->unop);
2729 star_cnt--;
2732 if (arg->type == EXPR_PREOP && arg->op == '&') {
2733 arg = strip_expr(arg->unop);
2734 tmp = expr_to_var_sym(arg, sym);
2735 if (!tmp)
2736 return NULL;
2737 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2738 star_cnt, "**********", tmp, key + 3);
2739 return alloc_string(buf);
2742 tmp = expr_to_var_sym(arg, sym);
2743 if (!tmp)
2744 return NULL;
2745 snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2746 free_string(tmp);
2747 return alloc_string(buf);
2750 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2752 *vsl = NULL;
2754 if (strcmp("$", key) == 0)
2755 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2756 return get_variable_from_key(arg, key, sym);
2759 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2761 int star_cnt = 0;
2762 int name_len;
2763 char buf[256];
2765 name_len = strlen(param_name);
2767 while (state_name[0] == '*') {
2768 star_cnt++;
2769 state_name++;
2772 /* ten out of ten stars! */
2773 if (star_cnt > 10)
2774 return NULL;
2776 if (strncmp(state_name, "(*", 2) == 0 &&
2777 strncmp(state_name + 2, param_name, name_len) == 0 &&
2778 state_name[name_len + 2] == ')') {
2779 snprintf(buf, sizeof(buf), "%.*s(*$)%s", star_cnt, "**********",
2780 state_name + name_len + 3);
2781 return alloc_sname(buf);
2784 if (strcmp(state_name, param_name) == 0) {
2785 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2786 return alloc_sname(buf);
2789 /* check for '-' from "->" */
2790 if (strncmp(state_name, param_name, name_len) == 0 &&
2791 state_name[name_len] == '-') {
2792 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2793 return alloc_sname(buf);
2795 return NULL;
2798 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2800 if (!sym || !sym->ident)
2801 return NULL;
2803 return state_name_to_param_name(name, sym->ident->name);
2806 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2808 struct symbol *type;
2809 const char *sym_name;
2810 int name_len;
2811 static char buf[256];
2814 * mtag_name is different from param_name because mtags can be a struct
2815 * instead of a struct pointer. But we want to treat it like a pointer
2816 * because really an mtag is a pointer. Or in other words, if you pass
2817 * a struct foo then you want to talk about foo.bar but with an mtag
2818 * you want to refer to it as foo->bar.
2822 if (!sym || !sym->ident)
2823 return NULL;
2825 type = get_real_base_type(sym);
2826 if (type && type->type == SYM_BASETYPE)
2827 return "*$";
2829 sym_name = sym->ident->name;
2830 name_len = strlen(sym_name);
2832 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2833 strncmp(state_name, sym_name, name_len) == 0) {
2834 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2835 return buf;
2838 return state_name_to_param_name(state_name, sym_name);
2841 const char *get_mtag_name_expr(struct expression *expr)
2843 char *name;
2844 struct symbol *sym;
2845 const char *ret = NULL;
2847 name = expr_to_var_sym(expr, &sym);
2848 if (!name || !sym)
2849 goto free;
2851 ret = get_mtag_name_var_sym(name, sym);
2852 free:
2853 free_string(name);
2854 return ret;
2857 const char *get_param_name(struct sm_state *sm)
2859 return get_param_name_var_sym(sm->name, sm->sym);
2862 char *get_data_info_name(struct expression *expr)
2864 struct symbol *sym;
2865 char *name;
2866 char buf[256];
2867 char *ret = NULL;
2869 expr = strip_expr(expr);
2870 name = get_member_name(expr);
2871 if (name)
2872 return name;
2873 name = expr_to_var_sym(expr, &sym);
2874 if (!name || !sym)
2875 goto free;
2876 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2877 goto free;
2878 if (sym->ctype.modifiers & MOD_STATIC)
2879 snprintf(buf, sizeof(buf), "static %s", name);
2880 else
2881 snprintf(buf, sizeof(buf), "global %s", name);
2882 ret = alloc_sname(buf);
2883 free:
2884 free_string(name);
2885 return ret;