check_list: add a comment about ordering
[smatch.git] / smatch_db.c
blob69780def81e6bf14d76cd9f17dbb6f391a352079
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);
39 #define SQLITE_CACHE_PAGES 1000
41 struct def_callback {
42 int hook_type;
43 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
45 ALLOCATOR(def_callback, "definition db hook callbacks");
46 DECLARE_PTR_LIST(callback_list, struct def_callback);
47 static struct callback_list *select_caller_info_callbacks;
49 struct member_info_callback {
50 int owner;
51 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
53 ALLOCATOR(member_info_callback, "caller_info callbacks");
54 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
55 static struct member_info_cb_list *member_callbacks;
56 static struct member_info_cb_list *member_callbacks_new;
58 struct return_info_callback {
59 int owner;
60 void (*callback)(int return_id, char *return_ranges,
61 struct expression *returned_expr,
62 int param,
63 const char *printed_name,
64 struct sm_state *sm);
66 ALLOCATOR(return_info_callback, "return_info callbacks");
67 DECLARE_PTR_LIST(return_info_cb_list, struct return_info_callback);
68 static struct return_info_cb_list *return_callbacks;
70 struct returned_state_callback {
71 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
73 ALLOCATOR(returned_state_callback, "returned state callbacks");
74 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
75 static struct returned_state_cb_list *returned_state_callbacks;
77 struct returned_member_callback {
78 int owner;
79 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
81 ALLOCATOR(returned_member_callback, "returned member callbacks");
82 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
83 static struct returned_member_cb_list *returned_member_callbacks;
85 struct db_implies_callback {
86 int type;
87 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
89 ALLOCATOR(db_implies_callback, "return_implies callbacks");
90 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
91 static struct db_implies_cb_list *return_implies_cb_list;
92 static struct db_implies_cb_list *call_implies_cb_list;
94 /* silently truncates if needed. */
95 char *escape_newlines(const char *str)
97 char buf[1024] = "";
98 bool found = false;
99 int i, j;
101 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
102 if (str[i] != '\r' && str[i] != '\n') {
103 buf[j] = str[i];
104 continue;
107 found = true;
108 buf[j++] = '\\';
109 if (j == sizeof(buf))
110 break;
111 buf[j] = 'n';
114 if (!found)
115 return alloc_sname(str);
117 if (j == sizeof(buf))
118 buf[j - 1] = '\0';
119 return alloc_sname(buf);
122 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
124 int i;
126 for (i = 0; i < argc; i++) {
127 if (i != 0)
128 sm_printf(", ");
129 sm_printf("%s", argv[i]);
131 sm_printf("\n");
132 return 0;
135 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
137 char *err = NULL;
138 int rc;
140 if (!db)
141 return;
143 if (option_debug || debug_db) {
144 sm_msg("%s", sql);
145 if (strncasecmp(sql, "select", strlen("select")) == 0)
146 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
149 rc = sqlite3_exec(db, sql, callback, data, &err);
150 if (rc != SQLITE_OK && !parse_error) {
151 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
152 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
153 parse_error = 1;
157 static int replace_count;
158 static char **replace_table;
159 static const char *replace_return_ranges(const char *return_ranges)
161 int i;
163 if (!get_function()) {
164 /* I have no idea why EXPORT_SYMBOL() is here */
165 return return_ranges;
167 for (i = 0; i < replace_count; i += 3) {
168 if (strcmp(replace_table[i + 0], get_function()) == 0) {
169 if (strcmp(replace_table[i + 1], return_ranges) == 0)
170 return replace_table[i + 2];
173 return return_ranges;
177 static char *use_states;
178 static int get_db_state_count(void)
180 struct sm_state *sm;
181 int count = 0;
183 FOR_EACH_SM(__get_cur_stree(), sm) {
184 if (sm->owner == USHRT_MAX)
185 continue;
186 if (use_states[sm->owner])
187 count++;
188 } END_FOR_EACH_SM(sm);
189 return count;
192 static bool is_local(struct symbol *sym)
194 if (sym->ctype.modifiers & MOD_STATIC)
195 return true;
196 if ((sym->ctype.modifiers & MOD_EXTERN) &&
197 (sym->ctype.modifiers & MOD_INLINE))
198 return true;
200 if (!sym->definition)
201 return false;
203 if ((sym->definition->ctype.modifiers & MOD_EXTERN) &&
204 (sym->definition->ctype.modifiers & MOD_INLINE))
205 return true;
207 return false;
210 void db_ignore_states(int id)
212 use_states[id] = 0;
215 unsigned long long __fn_mtag;
216 static void set_fn_mtag(struct symbol *sym)
218 char buf[128];
220 if (is_local(cur_func_sym))
221 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
222 else
223 snprintf(buf, sizeof(buf), "extern %s", get_function());
225 __fn_mtag = str_to_mtag(buf);
228 void sql_insert_return_states(int return_id, const char *return_ranges,
229 int type, int param, const char *key, const char *value)
231 unsigned long long id;
234 if (key && strlen(key) >= 80)
235 return;
236 if (__inline_fn)
237 id = (unsigned long)__inline_fn;
238 else
239 id = __fn_mtag;
241 return_ranges = replace_return_ranges(return_ranges);
242 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
243 get_base_file(), get_function(), id, return_id,
244 return_ranges, fn_static(), type, param, key, value);
247 static struct string_list *common_funcs;
248 static int is_common_function(const char *fn)
250 char *tmp;
252 if (!fn)
253 return 0;
255 if (strncmp(fn, "__builtin_", 10) == 0)
256 return 1;
258 FOR_EACH_PTR(common_funcs, tmp) {
259 if (strcmp(tmp, fn) == 0)
260 return 1;
261 } END_FOR_EACH_PTR(tmp);
263 return 0;
266 static char *function_signature(void)
268 return type_to_str(get_real_base_type(cur_func_sym));
271 void sql_insert_caller_info(struct expression *call, int type,
272 int param, const char *key, const char *value)
274 FILE *tmp_fd = sm_outfd;
275 char *fn;
277 if (!option_info && !__inline_call)
278 return;
279 if (unreachable())
280 return;
282 if (key && strlen(key) >= 80)
283 return;
285 fn = get_fnptr_name(call->fn);
286 if (!fn)
287 return;
289 if (__inline_call) {
290 mem_sql(NULL, NULL,
291 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
292 get_base_file(), get_function(), fn, (unsigned long)call,
293 is_static(call->fn), type, param, key, value);
296 if (!option_info)
297 return;
299 if (strncmp(fn, "__builtin_", 10) == 0)
300 return;
301 if (type != INTERNAL && is_common_function(fn))
302 return;
304 sm_outfd = caller_info_fd;
305 sm_msg("SQL_caller_info: insert into caller_info values ("
306 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
307 get_base_file(), get_function(), fn, is_static(call->fn),
308 type, param, key, value);
309 sm_outfd = tmp_fd;
311 free_string(fn);
314 void sql_insert_function_ptr(const char *fn, const char *struct_name)
316 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
317 get_base_file(), fn, struct_name);
320 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
322 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
323 get_base_file(), get_function(), (unsigned long)__inline_fn,
324 fn_static(), type, param, key, value);
327 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
329 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
330 get_base_file(), get_function(), (unsigned long)__inline_fn,
331 fn_static(), type, param, key, value);
334 void sql_insert_function_type_size(const char *member, const char *ranges)
336 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
339 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
341 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
344 void sql_insert_type_info(int type, const char *member, const char *value)
346 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
349 void sql_insert_local_values(const char *name, const char *value)
351 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
354 void sql_insert_function_type_value(const char *type, const char *value)
356 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
359 void sql_insert_function_type(int param, const char *value)
361 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
362 get_base_file(), get_function(), fn_static(), param, value);
365 void sql_insert_parameter_name(int param, const char *value)
367 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
368 get_base_file(), get_function(), fn_static(), param, value);
371 void sql_insert_data_info(struct expression *data, int type, const char *value)
373 char *data_name;
375 data_name = get_data_info_name(data);
376 if (!data_name)
377 return;
378 sql_insert(data_info, "'%s', '%s', %d, '%s'",
379 is_static(data) ? get_base_file() : "extern",
380 data_name, type, value);
383 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
385 sql_insert(data_info, "'%s', '%s', %d, '%s'",
386 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
387 var, type, value);
390 void sql_save_constraint(const char *con)
392 if (!option_info)
393 return;
395 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
398 void sql_save_constraint_required(const char *data, int op, const char *limit)
400 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
403 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
405 if (!option_info)
406 return;
408 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
409 "select constraints_required.data, constraints_required.op, '%s' from "
410 "constraints_required where bound = '%s';", new_limit, old_limit);
413 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
415 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
418 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
420 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
421 return;
423 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
424 is_local(fn->symbol) ? get_base_file() : "extern",
425 fn->symbol->ident->name,
426 is_local(fn->symbol),
427 type, param, key, value);
430 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
432 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
433 tag, get_filename(), get_function(), get_lineno(),
434 left_name, right_name);
437 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
439 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
442 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
444 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
447 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
449 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
452 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
454 mtag_t *saved_tag = _tag;
455 mtag_t new_tag;
457 new_tag = strtoll(argv[0], NULL, 10);
459 if (!*saved_tag)
460 *saved_tag = new_tag;
461 else if (*saved_tag != new_tag)
462 *saved_tag = -1ULL;
464 return 0;
467 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
469 mtag_t tmp = 0;
471 run_sql(save_mtag, &tmp,
472 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
473 tag, container_offset);
475 if (tmp == 0 || tmp == -1ULL)
476 return 0;
477 *container = tmp;
478 return 1;
481 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
483 mtag_t tmp = 0;
485 run_sql(save_mtag, &tmp,
486 "select tag from mtag_map where container = %lld and container_offset = %d;",
487 container, offset);
489 if (tmp == 0 || tmp == -1ULL)
490 return 0;
491 *tag = tmp;
492 return 1;
495 char *get_static_filter(struct symbol *sym)
497 static char sql_filter[1024];
499 /* This can only happen on buggy code. Return invalid SQL. */
500 if (!sym) {
501 sql_filter[0] = '\0';
502 return sql_filter;
505 if (is_local(sym)) {
506 snprintf(sql_filter, sizeof(sql_filter),
507 "file = '%s' and function = '%s' and static = '1'",
508 get_base_file(), sym->ident->name);
509 } else {
510 snprintf(sql_filter, sizeof(sql_filter),
511 "function = '%s' and static = '0'", sym->ident->name);
514 return sql_filter;
517 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
519 int *row_count = _row_count;
521 *row_count = 0;
522 if (argc != 1)
523 return 0;
524 *row_count = atoi(argv[0]);
525 return 0;
528 static void mark_call_params_untracked(struct expression *call)
530 struct expression *arg;
531 int i = 0;
533 FOR_EACH_PTR(call->args, arg) {
534 mark_untracked(call, i++, "$", NULL);
535 } END_FOR_EACH_PTR(arg);
538 static void sql_select_return_states_pointer(const char *cols,
539 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
541 char *ptr;
542 int return_count = 0;
544 ptr = get_fnptr_name(call->fn);
545 if (!ptr)
546 return;
548 run_sql(get_row_count, &return_count,
549 "select count(*) from return_states join function_ptr "
550 "where return_states.function == function_ptr.function and "
551 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
552 /* The magic number 100 is just from testing on the kernel. */
553 if (return_count > 100) {
554 mark_call_params_untracked(call);
555 return;
558 run_sql(callback, info,
559 "select %s from return_states join function_ptr where "
560 "return_states.function == function_ptr.function and ptr = '%s' "
561 "and searchable = 1 "
562 "order by function_ptr.file, return_states.file, return_id, type;",
563 cols, ptr);
566 static int is_local_symbol(struct expression *expr)
568 if (expr->type != EXPR_SYMBOL)
569 return 0;
570 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
571 return 0;
572 return 1;
575 void sql_select_return_states(const char *cols, struct expression *call,
576 int (*callback)(void*, int, char**, char**), void *info)
578 struct expression *fn;
579 int row_count = 0;
581 if (is_fake_call(call))
582 return;
584 fn = strip_expr(call->fn);
585 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
586 sql_select_return_states_pointer(cols, call, callback, info);
587 return;
590 if (inlinable(fn)) {
591 mem_sql(callback, info,
592 "select %s from return_states where call_id = '%lu' order by return_id, type;",
593 cols, (unsigned long)call);
594 return;
597 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
598 get_static_filter(fn->symbol));
599 if (row_count == 0 && fn->symbol && fn->symbol->definition)
600 set_state(my_id, "db_incomplete", NULL, &incomplete);
601 if (row_count > 3000)
602 return;
604 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
605 cols, get_static_filter(fn->symbol));
608 bool db_incomplete(void)
610 return !!get_state(my_id, "db_incomplete", NULL);
613 #define CALL_IMPLIES 0
614 #define RETURN_IMPLIES 1
616 struct implies_info {
617 int type;
618 struct db_implies_cb_list *cb_list;
619 struct expression *expr;
620 struct symbol *sym;
623 void sql_select_implies(const char *cols, struct implies_info *info,
624 int (*callback)(void*, int, char**, char**))
626 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
627 mem_sql(callback, info,
628 "select %s from return_implies where call_id = '%lu';",
629 cols, (unsigned long)info->expr);
630 return;
633 run_sql(callback, info, "select %s from %s_implies where %s;",
634 cols,
635 info->type == CALL_IMPLIES ? "call" : "return",
636 get_static_filter(info->sym));
639 struct select_caller_info_data {
640 struct stree *final_states;
641 struct timeval start_time;
642 int prev_func_id;
643 int ignore;
644 int results;
647 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
649 static void sql_select_caller_info(struct select_caller_info_data *data,
650 const char *cols, struct symbol *sym)
652 if (__inline_fn) {
653 mem_sql(caller_info_callback, data,
654 "select %s from caller_info where call_id = %lu;",
655 cols, (unsigned long)__inline_fn);
656 return;
659 if (sym->ident->name && is_common_function(sym->ident->name))
660 return;
661 run_sql(caller_info_callback, data,
662 "select %s from common_caller_info where %s order by call_id;",
663 cols, get_static_filter(sym));
664 if (data->results)
665 return;
667 run_sql(caller_info_callback, data,
668 "select %s from caller_info where %s order by call_id;",
669 cols, get_static_filter(sym));
672 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
674 struct def_callback *def_callback = __alloc_def_callback(0);
676 def_callback->hook_type = type;
677 def_callback->callback = callback;
678 add_ptr_list(&select_caller_info_callbacks, def_callback);
682 * These call backs are used when the --info option is turned on to print struct
683 * member information. For example foo->bar could have a state in
684 * smatch_extra.c and also check_user.c.
686 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
688 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
690 member_callback->owner = owner;
691 member_callback->callback = callback;
692 add_ptr_list(&member_callbacks, member_callback);
695 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
697 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
699 member_callback->owner = owner;
700 member_callback->callback = callback;
701 add_ptr_list(&member_callbacks_new, member_callback);
704 void add_return_info_callback(int owner,
705 void (*callback)(int return_id, char *return_ranges,
706 struct expression *returned_expr,
707 int param,
708 const char *printed_name,
709 struct sm_state *sm))
711 struct return_info_callback *return_callback = __alloc_return_info_callback(0);
713 return_callback->owner = owner;
714 return_callback->callback = callback;
715 add_ptr_list(&return_callbacks, return_callback);
718 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
720 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
722 callback->callback = fn;
723 add_ptr_list(&returned_state_callbacks, callback);
726 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))
728 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
730 member_callback->owner = owner;
731 member_callback->callback = callback;
732 add_ptr_list(&returned_member_callbacks, member_callback);
735 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
737 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
739 cb->type = type;
740 cb->callback = callback;
741 add_ptr_list(&call_implies_cb_list, cb);
744 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
746 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
748 cb->type = type;
749 cb->callback = callback;
750 add_ptr_list(&return_implies_cb_list, cb);
753 struct return_info {
754 struct expression *static_returns_call;
755 struct symbol *return_type;
756 struct range_list *return_range_list;
759 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
761 struct return_info *ret_info = _ret_info;
762 struct range_list *rl;
763 struct expression *call_expr = ret_info->static_returns_call;
765 if (argc != 1)
766 return 0;
767 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
768 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
769 return 0;
772 struct range_list *db_return_vals(struct expression *expr)
774 struct return_info ret_info = {};
775 struct sm_state *sm;
777 if (is_fake_call(expr))
778 return NULL;
780 sm = get_extra_sm_state(expr);
781 if (sm)
782 return clone_rl(estate_rl(sm->state));
783 ret_info.static_returns_call = expr;
784 ret_info.return_type = get_type(expr);
785 if (!ret_info.return_type)
786 return NULL;
788 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
789 return NULL;
791 ret_info.return_range_list = NULL;
792 if (inlinable(expr->fn)) {
793 mem_sql(db_return_callback, &ret_info,
794 "select distinct return from return_states where call_id = '%lu';",
795 (unsigned long)expr);
796 } else {
797 run_sql(db_return_callback, &ret_info,
798 "select distinct return from return_states where %s;",
799 get_static_filter(expr->fn->symbol));
801 return ret_info.return_range_list;
804 struct range_list *db_return_vals_from_str(const char *fn_name)
806 struct return_info ret_info;
808 ret_info.static_returns_call = NULL;
809 ret_info.return_type = &llong_ctype;
810 ret_info.return_range_list = NULL;
812 run_sql(db_return_callback, &ret_info,
813 "select distinct return from return_states where function = '%s';",
814 fn_name);
815 return ret_info.return_range_list;
819 * This is used when we have a function that takes a function pointer as a
820 * parameter. "frob(blah, blah, my_function);" We know that the return values
821 * from frob() come from my_funcion() so we want to find the possible returns
822 * of my_function(), but we don't know which arguments are passed to it.
825 struct range_list *db_return_vals_no_args(struct expression *expr)
827 struct return_info ret_info = {};
829 if (!expr || expr->type != EXPR_SYMBOL)
830 return NULL;
832 ret_info.static_returns_call = expr;
833 ret_info.return_type = get_type(expr);
834 ret_info.return_type = get_real_base_type(ret_info.return_type);
835 if (!ret_info.return_type)
836 return NULL;
838 run_sql(db_return_callback, &ret_info,
839 "select distinct return from return_states where %s;",
840 get_static_filter(expr->symbol));
842 return ret_info.return_range_list;
845 static void match_call_marker(struct expression *expr)
847 struct symbol *type;
849 type = get_type(expr->fn);
850 if (type && type->type == SYM_PTR)
851 type = get_real_base_type(type);
854 * we just want to record something in the database so that if we have
855 * two calls like: frob(4); frob(some_unkown); then on the receiving
856 * side we know that sometimes frob is called with unknown parameters.
859 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
862 int is_recursive_member(const char *name)
864 char buf[256];
865 const char *p, *next;
866 int size;
868 p = strchr(name, '>');
869 if (!p)
870 return 0;
871 p++;
872 while (true) {
873 next = strchr(p, '>');
874 if (!next)
875 return 0;
876 next++;
878 size = next - p;
879 if (size >= sizeof(buf))
880 return 0;
881 memcpy(buf, p, size);
882 buf[size] = '\0';
883 if (strstr(next, buf))
884 return 1;
885 p = next;
889 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
891 struct symbol *sym;
892 const char *sm_name;
893 char *name;
894 bool is_address = false;
895 bool add_star = false;
896 char buf[256];
897 char *ret = NULL;
898 int len;
900 expr = strip_expr(expr);
901 if (!expr)
902 return NULL;
904 if (expr->type == EXPR_PREOP && expr->op == '&') {
905 expr = strip_expr(expr->unop);
906 is_address = true;
909 name = expr_to_var_sym(expr, &sym);
910 if (!name || !sym)
911 goto free;
912 if (sym != sm->sym)
913 goto free;
915 sm_name = sm->name;
916 add_star = false;
917 if (sm_name[0] == '*') {
918 add_star = true;
919 sm_name++;
922 len = strlen(name);
923 if (strncmp(name, sm_name, len) != 0)
924 goto free;
925 if (sm_name[len] == '\0') {
926 snprintf(buf, sizeof(buf), "%s%s$",
927 add_star ? "*" : "", is_address ? "*" : "");
928 } else {
929 if (sm_name[len] != '.' && sm_name[len] != '-')
930 goto free;
931 if (sm_name[len] == '-')
932 len++;
933 // FIXME does is_address really imply that sm_name[len] == '-'
934 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
935 sm_name + len);
938 ret = alloc_sname(buf);
939 free:
940 free_string(name);
941 return ret;
944 static void print_struct_members(struct expression *call, struct expression *expr, int param,
945 int owner,
946 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
947 bool new)
949 struct sm_state *sm;
950 const char *sm_name;
951 char *name;
952 struct symbol *sym;
953 int len;
954 char printed_name[256];
955 int is_address = 0;
956 bool add_star;
957 struct symbol *type;
959 expr = strip_expr(expr);
960 if (!expr)
961 return;
962 type = get_type(expr);
963 if (!new && type && type_bits(type) < type_bits(&ulong_ctype))
964 return;
966 if (expr->type == EXPR_PREOP && expr->op == '&') {
967 expr = strip_expr(expr->unop);
968 is_address = 1;
971 name = expr_to_var_sym(expr, &sym);
972 if (!name || !sym)
973 goto free;
975 len = strlen(name);
976 FOR_EACH_SM(__get_cur_stree(), sm) {
977 if (sm->owner != owner || sm->sym != sym)
978 continue;
979 sm_name = sm->name;
980 add_star = false;
981 if (sm_name[0] == '*') {
982 add_star = true;
983 sm_name++;
985 // FIXME: simplify?
986 if (!add_star && strcmp(name, sm_name) == 0) {
987 if (is_address) {
988 snprintf(printed_name, sizeof(printed_name), "*$");
989 } else {
990 if (new)
991 snprintf(printed_name, sizeof(printed_name), "$");
992 else
993 continue;
995 } else if (add_star && strcmp(name, sm_name) == 0) {
996 snprintf(printed_name, sizeof(printed_name), "%s*$",
997 is_address ? "*" : "");
998 } else if (strncmp(name, sm_name, len) == 0) {
999 if (sm_name[len] != '.' && sm_name[len] != '-')
1000 continue;
1001 if (is_address && sm_name[len] == '.') {
1002 snprintf(printed_name, sizeof(printed_name),
1003 "%s$->%s", add_star ? "*" : "",
1004 sm_name + len + 1);
1005 } else if (is_address && sm_name[len] == '-') {
1006 snprintf(printed_name, sizeof(printed_name),
1007 "%s(*$)%s", add_star ? "*" : "",
1008 sm_name + len);
1009 } else {
1010 snprintf(printed_name, sizeof(printed_name),
1011 "%s$%s", add_star ? "*" : "",
1012 sm_name + len);
1014 } else {
1015 continue;
1017 if (is_recursive_member(printed_name))
1018 continue;
1019 callback(call, param, printed_name, sm);
1020 } END_FOR_EACH_SM(sm);
1021 free:
1022 free_string(name);
1025 static void match_call_info(struct expression *call)
1027 struct member_info_callback *cb;
1028 struct expression *arg;
1029 int i;
1031 FOR_EACH_PTR(member_callbacks, cb) {
1032 i = -1;
1033 FOR_EACH_PTR(call->args, arg) {
1034 i++;
1035 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1036 } END_FOR_EACH_PTR(arg);
1037 } END_FOR_EACH_PTR(cb);
1040 static struct expression *get_fake_variable(struct expression *expr)
1042 struct expression *tmp;
1044 tmp = expr_get_fake_parent_expr(expr);
1045 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1046 return NULL;
1048 return tmp->left;
1051 static void match_call_info_new(struct expression *call)
1053 struct member_info_callback *cb;
1054 struct expression *arg, *tmp;
1055 int i;
1057 if (!option_info && !__inline_call)
1058 return;
1060 FOR_EACH_PTR(member_callbacks_new, cb) {
1061 i = -1;
1062 FOR_EACH_PTR(call->args, arg) {
1063 i++;
1064 tmp = get_fake_variable(arg);
1065 if (!tmp)
1066 tmp = arg;
1067 __ignore_param_used++;
1068 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1069 __ignore_param_used--;
1070 } END_FOR_EACH_PTR(arg);
1071 } END_FOR_EACH_PTR(cb);
1074 static int get_param(int param, char **name, struct symbol **sym)
1076 struct symbol *arg;
1077 int i;
1079 i = 0;
1080 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1082 * this is a temporary hack to work around a bug (I think in sparse?)
1083 * 2.6.37-rc1:fs/reiserfs/journal.o
1084 * If there is a function definition without parameter name found
1085 * after a function implementation then it causes a crash.
1086 * int foo() {}
1087 * int bar(char *);
1089 if (arg->ident->name < (char *)100)
1090 continue;
1091 if (i == param) {
1092 *name = arg->ident->name;
1093 *sym = arg;
1094 return TRUE;
1096 i++;
1097 } END_FOR_EACH_PTR(arg);
1099 return FALSE;
1102 static int function_signature_matches(const char *sig)
1104 char *my_sig;
1106 my_sig = function_signature();
1107 if (!sig || !my_sig)
1108 return 1; /* default to matching */
1109 if (strcmp(my_sig, sig) == 0)
1110 return 1;
1111 return 0;
1114 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1116 struct select_caller_info_data *data = _data;
1117 int func_id;
1118 long type;
1119 long param;
1120 char *key;
1121 char *value;
1122 char *name = NULL;
1123 struct symbol *sym = NULL;
1124 struct def_callback *def_callback;
1125 struct stree *stree;
1126 struct timeval cur_time;
1128 data->results = 1;
1130 if (argc != 5)
1131 return 0;
1133 gettimeofday(&cur_time, NULL);
1134 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1135 return 0;
1137 func_id = atoi(argv[0]);
1138 errno = 0;
1139 type = strtol(argv[1], NULL, 10);
1140 param = strtol(argv[2], NULL, 10);
1141 if (errno)
1142 return 0;
1143 key = argv[3];
1144 value = argv[4];
1146 if (data->prev_func_id == -1)
1147 data->prev_func_id = func_id;
1148 if (func_id != data->prev_func_id) {
1149 stree = __pop_fake_cur_stree();
1150 if (!data->ignore)
1151 merge_stree(&data->final_states, stree);
1152 free_stree(&stree);
1153 __push_fake_cur_stree();
1154 __unnullify_path();
1155 data->prev_func_id = func_id;
1156 data->ignore = 0;
1159 if (data->ignore)
1160 return 0;
1161 if (type == INTERNAL &&
1162 !function_signature_matches(value)) {
1163 data->ignore = 1;
1164 return 0;
1167 if (param >= 0 && !get_param(param, &name, &sym))
1168 return 0;
1170 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1171 if (def_callback->hook_type == type)
1172 def_callback->callback(name, sym, key, value);
1173 } END_FOR_EACH_PTR(def_callback);
1175 return 0;
1178 static struct string_list *ptr_names_done;
1179 static struct string_list *ptr_names;
1181 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1183 insert_string(&ptr_names, alloc_string(argv[0]));
1184 return 0;
1187 static char *get_next_ptr_name(void)
1189 char *ptr;
1191 FOR_EACH_PTR(ptr_names, ptr) {
1192 if (!insert_string(&ptr_names_done, ptr))
1193 continue;
1194 return ptr;
1195 } END_FOR_EACH_PTR(ptr);
1196 return NULL;
1199 static void get_ptr_names(const char *file, const char *name)
1201 char sql_filter[1024];
1202 int before, after;
1204 if (file) {
1205 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1206 file, name);
1207 } else {
1208 snprintf(sql_filter, 1024, "function = '%s';", name);
1211 before = ptr_list_size((struct ptr_list *)ptr_names);
1213 run_sql(get_ptr_name, NULL,
1214 "select distinct ptr from function_ptr where %s",
1215 sql_filter);
1217 after = ptr_list_size((struct ptr_list *)ptr_names);
1218 if (before == after)
1219 return;
1221 while ((name = get_next_ptr_name()))
1222 get_ptr_names(NULL, name);
1225 static void match_data_from_db(struct symbol *sym)
1227 struct select_caller_info_data data = { .prev_func_id = -1 };
1228 struct sm_state *sm;
1229 struct stree *stree;
1230 struct timeval end_time;
1232 if (!sym || !sym->ident)
1233 return;
1235 set_fn_mtag(sym);
1236 gettimeofday(&data.start_time, NULL);
1238 __push_fake_cur_stree();
1239 __unnullify_path();
1241 if (!__inline_fn) {
1242 char *ptr;
1244 if (sym->ctype.modifiers & MOD_STATIC)
1245 get_ptr_names(get_base_file(), sym->ident->name);
1246 else
1247 get_ptr_names(NULL, sym->ident->name);
1249 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1250 __free_ptr_list((struct ptr_list **)&ptr_names);
1251 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1252 __free_fake_cur_stree();
1253 return;
1256 sql_select_caller_info(&data,
1257 "call_id, type, parameter, key, value",
1258 sym);
1261 stree = __pop_fake_cur_stree();
1262 if (!data.ignore)
1263 merge_stree(&data.final_states, stree);
1264 free_stree(&stree);
1265 __push_fake_cur_stree();
1266 __unnullify_path();
1267 data.prev_func_id = -1;
1268 data.ignore = 0;
1269 data.results = 0;
1271 FOR_EACH_PTR(ptr_names, ptr) {
1272 run_sql(caller_info_callback, &data,
1273 "select call_id, type, parameter, key, value"
1274 " from common_caller_info where function = '%s' order by call_id",
1275 ptr);
1276 } END_FOR_EACH_PTR(ptr);
1278 if (data.results) {
1279 FOR_EACH_PTR(ptr_names, ptr) {
1280 free_string(ptr);
1281 } END_FOR_EACH_PTR(ptr);
1282 goto free_ptr_names;
1285 FOR_EACH_PTR(ptr_names, ptr) {
1286 run_sql(caller_info_callback, &data,
1287 "select call_id, type, parameter, key, value"
1288 " from caller_info where function = '%s' order by call_id",
1289 ptr);
1290 free_string(ptr);
1291 } END_FOR_EACH_PTR(ptr);
1293 free_ptr_names:
1294 __free_ptr_list((struct ptr_list **)&ptr_names);
1295 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1296 } else {
1297 sql_select_caller_info(&data,
1298 "call_id, type, parameter, key, value",
1299 sym);
1302 stree = __pop_fake_cur_stree();
1303 if (!data.ignore)
1304 merge_stree(&data.final_states, stree);
1305 free_stree(&stree);
1307 gettimeofday(&end_time, NULL);
1308 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1309 FOR_EACH_SM(data.final_states, sm) {
1310 __set_sm(sm);
1311 } END_FOR_EACH_SM(sm);
1314 free_stree(&data.final_states);
1317 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1319 struct implies_info *info = _info;
1320 struct db_implies_callback *cb;
1321 struct expression *arg = NULL;
1322 int type;
1323 int param;
1325 if (argc != 5)
1326 return 0;
1328 type = atoi(argv[1]);
1329 param = atoi(argv[2]);
1331 FOR_EACH_PTR(info->cb_list, cb) {
1332 if (cb->type != type)
1333 continue;
1334 if (param != -1) {
1335 arg = get_argument_from_call_expr(info->expr->args, param);
1336 if (!arg)
1337 continue;
1339 cb->callback(info->expr, arg, argv[3], argv[4]);
1340 } END_FOR_EACH_PTR(cb);
1342 return 0;
1345 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1347 struct implies_info *info = _info;
1348 struct db_implies_callback *cb;
1349 struct expression *arg;
1350 struct symbol *sym;
1351 char *name;
1352 int type;
1353 int param;
1355 if (argc != 5)
1356 return 0;
1358 type = atoi(argv[1]);
1359 param = atoi(argv[2]);
1361 if (!get_param(param, &name, &sym))
1362 return 0;
1363 arg = symbol_expression(sym);
1364 if (!arg)
1365 return 0;
1367 FOR_EACH_PTR(info->cb_list, cb) {
1368 if (cb->type != type)
1369 continue;
1370 cb->callback(info->expr, arg, argv[3], argv[4]);
1371 } END_FOR_EACH_PTR(cb);
1373 return 0;
1376 static void match_return_implies(struct expression *expr)
1378 struct implies_info info = {
1379 .type = RETURN_IMPLIES,
1380 .cb_list = return_implies_cb_list,
1383 if (expr->fn->type != EXPR_SYMBOL ||
1384 !expr->fn->symbol)
1385 return;
1386 info.expr = expr;
1387 info.sym = expr->fn->symbol;
1388 sql_select_implies("function, type, parameter, key, value", &info,
1389 return_implies_callbacks);
1392 static void match_call_implies(struct symbol *sym)
1394 struct implies_info info = {
1395 .type = CALL_IMPLIES,
1396 .cb_list = call_implies_cb_list,
1399 if (!sym || !sym->ident)
1400 return;
1402 info.sym = sym;
1403 sql_select_implies("function, type, parameter, key, value", &info,
1404 call_implies_callbacks);
1407 static char *get_fn_param_str(struct expression *expr)
1409 struct expression *tmp;
1410 int param;
1411 char buf[32];
1413 tmp = get_assigned_expr(expr);
1414 if (tmp)
1415 expr = tmp;
1416 expr = strip_expr(expr);
1417 if (!expr || expr->type != EXPR_CALL)
1418 return NULL;
1419 expr = strip_expr(expr->fn);
1420 if (!expr || expr->type != EXPR_SYMBOL)
1421 return NULL;
1422 param = get_param_num(expr);
1423 if (param < 0)
1424 return NULL;
1426 snprintf(buf, sizeof(buf), "[r $%d]", param);
1427 return alloc_sname(buf);
1430 static char *get_return_compare_is_param(struct expression *expr)
1432 char *var;
1433 char buf[256];
1434 int comparison;
1435 int param;
1437 param = get_param_num(expr);
1438 if (param < 0)
1439 return NULL;
1441 var = expr_to_var(expr);
1442 if (!var)
1443 return NULL;
1444 snprintf(buf, sizeof(buf), "%s orig", var);
1445 comparison = get_comparison_strings(var, buf);
1446 free_string(var);
1448 if (!comparison)
1449 return NULL;
1451 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1452 return alloc_sname(buf);
1455 static char *get_return_compare_str(struct expression *expr)
1457 char *compare_str;
1459 compare_str = get_return_compare_is_param(expr);
1460 if (compare_str)
1461 return compare_str;
1463 compare_str = expr_lte_to_param(expr, -1);
1464 if (compare_str)
1465 return compare_str;
1467 return expr_param_comparison(expr, -1);
1470 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1472 struct range_list *rl;
1473 char *return_ranges;
1474 sval_t sval;
1475 char *fn_param_str;
1476 char *compare_str;
1477 char *math_str;
1478 char buf[128];
1480 *rl_p = NULL;
1482 if (!expr)
1483 return alloc_sname("");
1485 if (get_implied_value(expr, &sval)) {
1486 sval = sval_cast(cur_func_return_type(), sval);
1487 *rl_p = alloc_rl(sval, sval);
1488 return sval_to_str_or_err_ptr(sval);
1491 fn_param_str = get_fn_param_str(expr);
1492 compare_str = expr_equal_to_param(expr, -1);
1493 math_str = get_value_in_terms_of_parameter_math(expr);
1495 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1496 rl = cast_rl(cur_func_return_type(), rl);
1497 return_ranges = show_rl(rl);
1498 } else if (get_imaginary_absolute(expr, &rl)){
1499 rl = cast_rl(cur_func_return_type(), rl);
1500 return alloc_sname(show_rl(rl));
1501 } else {
1502 get_absolute_rl(expr, &rl);
1503 rl = cast_rl(cur_func_return_type(), rl);
1504 return_ranges = show_rl(rl);
1506 *rl_p = rl;
1508 if (fn_param_str) {
1509 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1510 return alloc_sname(buf);
1512 if (compare_str) {
1513 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1514 return alloc_sname(buf);
1516 if (math_str) {
1517 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1518 return alloc_sname(buf);
1520 compare_str = get_return_compare_str(expr);
1521 if (compare_str) {
1522 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1523 return alloc_sname(buf);
1526 return return_ranges;
1529 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1531 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1534 static bool call_return_state_hooks_conditional(struct expression *expr)
1536 int final_pass_orig = final_pass;
1537 static int recurse;
1539 if (recurse >= 2)
1540 return false;
1541 if (!expr ||
1542 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1543 return false;
1545 recurse++;
1547 __push_fake_cur_stree();
1549 final_pass = 0;
1550 __split_whole_condition(expr->conditional);
1551 final_pass = final_pass_orig;
1553 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1555 __push_true_states();
1556 __use_false_states();
1558 call_return_state_hooks(expr->cond_false);
1560 __merge_true_states();
1561 __free_fake_cur_stree();
1563 recurse--;
1564 return true;
1567 static void call_return_state_hooks_compare(struct expression *expr)
1569 struct returned_state_callback *cb;
1570 char *return_ranges;
1571 int final_pass_orig = final_pass;
1572 sval_t sval = { .type = &int_ctype };
1573 sval_t ret;
1575 if (!get_implied_value(expr, &ret))
1576 ret.value = -1;
1578 __push_fake_cur_stree();
1580 final_pass = 0;
1581 __split_whole_condition(expr);
1582 final_pass = final_pass_orig;
1584 if (ret.value != 0) {
1585 return_ranges = alloc_sname("1");
1586 sval.value = 1;
1587 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1589 return_id++;
1590 FOR_EACH_PTR(returned_state_callbacks, cb) {
1591 cb->callback(return_id, return_ranges, expr);
1592 } END_FOR_EACH_PTR(cb);
1595 __push_true_states();
1596 __use_false_states();
1598 if (ret.value != 1) {
1599 return_ranges = alloc_sname("0");
1600 sval.value = 0;
1601 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1603 return_id++;
1604 FOR_EACH_PTR(returned_state_callbacks, cb) {
1605 cb->callback(return_id, return_ranges, expr);
1606 } END_FOR_EACH_PTR(cb);
1609 __merge_true_states();
1610 __free_fake_cur_stree();
1613 static bool is_implies_function(struct expression *expr)
1615 struct range_list *rl;
1617 if (!expr)
1618 return false;
1620 rl = get_range_implications(get_function());
1621 if (!rl)
1622 return false;
1624 sm_msg("%s: is implied", __func__);
1625 return true;
1628 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1630 struct sm_state *tmp;
1632 FOR_EACH_PTR(slist, tmp) {
1633 if (strcmp(tmp->state->name, sm->state->name) == 0)
1634 return 1;
1635 } END_FOR_EACH_PTR(tmp);
1637 return 0;
1640 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1642 struct returned_state_callback *cb;
1643 struct range_list *rl;
1644 char *return_ranges;
1645 struct sm_state *tmp;
1646 int ret = 0;
1647 int nr_possible, nr_states;
1648 char *compare_str;
1649 char buf[128];
1650 struct state_list *already_handled = NULL;
1651 sval_t sval;
1653 if (!sm || !sm->merged)
1654 return 0;
1656 if (too_many_possible(sm) && !is_implies_function(expr))
1657 return 0;
1659 /* bail if it gets too complicated */
1660 nr_possible = 0;
1661 FOR_EACH_PTR(sm->possible, tmp) {
1662 if (tmp->merged)
1663 continue;
1664 if (ptr_in_list(tmp, already_handled))
1665 continue;
1666 add_ptr_list(&already_handled, tmp);
1667 nr_possible++;
1668 } END_FOR_EACH_PTR(tmp);
1669 free_slist(&already_handled);
1670 nr_states = get_db_state_count();
1671 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1672 return 0;
1674 FOR_EACH_PTR(sm->possible, tmp) {
1675 if (tmp->merged)
1676 continue;
1677 if (ptr_in_list(tmp, already_handled))
1678 continue;
1679 add_ptr_list(&already_handled, tmp);
1681 ret = 1;
1682 __push_fake_cur_stree();
1684 overwrite_states_using_pool(sm, tmp);
1686 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1687 return_ranges = show_rl(rl);
1688 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1689 compare_str = get_return_compare_str(expr);
1690 /* ignore obvious stuff like 0 <= param */
1691 /* Is this worthile when we have PARAM_COMPARE? */
1692 if (compare_str &&
1693 strncmp(compare_str, "[=", 2) != 0 &&
1694 rl_to_sval(rl, &sval))
1695 compare_str = NULL;
1696 if (compare_str) {
1697 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1698 return_ranges = alloc_sname(buf);
1701 return_id++;
1702 FOR_EACH_PTR(returned_state_callbacks, cb) {
1703 cb->callback(return_id, return_ranges, expr);
1704 } END_FOR_EACH_PTR(cb);
1706 __free_fake_cur_stree();
1707 } END_FOR_EACH_PTR(tmp);
1709 free_slist(&already_handled);
1711 return ret;
1714 static int call_return_state_hooks_split_possible(struct expression *expr)
1716 struct expression *fake;
1717 struct sm_state *sm;
1719 if (!expr)
1720 return 0;
1722 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1723 if (!sm) {
1724 fake = expr_get_fake_parent_expr(expr);
1725 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1726 return 0;
1727 fake = fake->left;
1728 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1730 return split_possible_helper(sm, expr);
1733 static bool has_possible_negative(struct sm_state *sm)
1735 struct sm_state *tmp;
1737 if (!type_signed(estate_type(sm->state)))
1738 return false;
1740 FOR_EACH_PTR(sm->possible, tmp) {
1741 if (!estate_rl(tmp->state))
1742 continue;
1743 if (sval_is_negative(estate_min(tmp->state)) &&
1744 sval_is_negative(estate_max(tmp->state)))
1745 return true;
1746 } END_FOR_EACH_PTR(tmp);
1748 return false;
1751 static bool has_separate_zero_null(struct sm_state *sm)
1753 struct sm_state *tmp;
1754 sval_t sval;
1756 FOR_EACH_PTR(sm->possible, tmp) {
1757 if (!estate_get_single_value(tmp->state, &sval))
1758 continue;
1759 if (sval.value == 0)
1760 return true;
1761 } END_FOR_EACH_PTR(tmp);
1763 return false;
1766 static int split_positive_from_negative(struct expression *expr)
1768 struct sm_state *sm;
1769 struct returned_state_callback *cb;
1770 struct range_list *rl;
1771 const char *return_ranges;
1772 struct range_list *ret_rl;
1773 bool separate_zero;
1774 int undo;
1776 /* We're going to print the states 3 times */
1777 if (get_db_state_count() > 10000 / 3)
1778 return 0;
1780 if (!get_implied_rl(expr, &rl) || !rl)
1781 return 0;
1782 /* Forget about INT_MAX and larger */
1783 if (rl_max(rl).value <= 0)
1784 return 0;
1785 if (!sval_is_negative(rl_min(rl)))
1786 return 0;
1788 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1789 if (!sm)
1790 return 0;
1791 if (!has_possible_negative(sm))
1792 return 0;
1793 separate_zero = has_separate_zero_null(sm);
1795 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1796 return 0;
1798 return_id++;
1799 return_ranges = get_return_ranges_str(expr, &ret_rl);
1800 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1801 FOR_EACH_PTR(returned_state_callbacks, cb) {
1802 cb->callback(return_id, (char *)return_ranges, expr);
1803 } END_FOR_EACH_PTR(cb);
1805 end_assume();
1807 if (separate_zero) {
1808 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1810 return_id++;
1811 return_ranges = get_return_ranges_str(expr, &ret_rl);
1812 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1813 FOR_EACH_PTR(returned_state_callbacks, cb) {
1814 cb->callback(return_id, (char *)return_ranges, expr);
1815 } END_FOR_EACH_PTR(cb);
1817 if (undo)
1818 end_assume();
1821 undo = assume(compare_expression(expr, '<', zero_expr()));
1823 return_id++;
1824 return_ranges = get_return_ranges_str(expr, &ret_rl);
1825 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1826 FOR_EACH_PTR(returned_state_callbacks, cb) {
1827 cb->callback(return_id, (char *)return_ranges, expr);
1828 } END_FOR_EACH_PTR(cb);
1830 if (undo)
1831 end_assume();
1833 return 1;
1836 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1838 struct returned_state_callback *cb;
1839 struct range_list *rl;
1840 struct range_list *nonnull_rl;
1841 sval_t null_sval;
1842 struct range_list *null_rl = NULL;
1843 char *return_ranges;
1844 struct sm_state *sm;
1845 struct smatch_state *state;
1846 int nr_states;
1847 int final_pass_orig = final_pass;
1849 if (!expr || expr_equal_to_param(expr, -1))
1850 return 0;
1851 if (expr->type == EXPR_CALL)
1852 return 0;
1854 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1855 if (!sm)
1856 return 0;
1857 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1858 return 0;
1859 state = sm->state;
1860 if (!estate_rl(state))
1861 return 0;
1862 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1863 return 0;
1864 if (has_possible_negative(sm))
1865 return 0;
1866 if (!has_separate_zero_null(sm))
1867 return 0;
1869 nr_states = get_db_state_count();
1870 if (option_info && nr_states >= 1500)
1871 return 0;
1873 rl = estate_rl(state);
1875 __push_fake_cur_stree();
1877 final_pass = 0;
1878 __split_whole_condition(expr);
1879 final_pass = final_pass_orig;
1881 nonnull_rl = rl_filter(rl, rl_zero());
1882 return_ranges = show_rl(nonnull_rl);
1883 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1885 return_id++;
1886 FOR_EACH_PTR(returned_state_callbacks, cb) {
1887 cb->callback(return_id, return_ranges, expr);
1888 } END_FOR_EACH_PTR(cb);
1890 __push_true_states();
1891 __use_false_states();
1893 return_ranges = alloc_sname("0");
1894 null_sval = sval_type_val(rl_type(rl), 0);
1895 add_range(&null_rl, null_sval, null_sval);
1896 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1897 return_id++;
1898 FOR_EACH_PTR(returned_state_callbacks, cb) {
1899 cb->callback(return_id, return_ranges, expr);
1900 } END_FOR_EACH_PTR(cb);
1902 __merge_true_states();
1903 __free_fake_cur_stree();
1905 return 1;
1908 static bool is_kernel_success_fail(struct sm_state *sm)
1910 struct sm_state *tmp;
1911 struct range_list *rl;
1912 bool has_zero = false;
1913 bool has_neg = false;
1915 if (!type_signed(estate_type(sm->state)))
1916 return false;
1918 FOR_EACH_PTR(sm->possible, tmp) {
1919 rl = estate_rl(tmp->state);
1920 if (!rl)
1921 return false;
1922 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1923 has_zero = true;
1924 continue;
1926 has_neg = true;
1927 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1928 continue;
1929 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1930 continue;
1931 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1932 continue;
1933 return false;
1934 } END_FOR_EACH_PTR(tmp);
1936 return has_zero && has_neg;
1939 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1941 struct sm_state *sm;
1942 struct range_list *rl;
1943 struct range_list *nonzero_rl;
1944 sval_t zero_sval;
1945 struct range_list *zero_rl = NULL;
1946 int nr_states;
1947 struct returned_state_callback *cb;
1948 char *return_ranges;
1949 int final_pass_orig = final_pass;
1951 if (option_project != PROJ_KERNEL)
1952 return 0;
1954 nr_states = get_db_state_count();
1955 if (nr_states > 2000)
1956 return 0;
1958 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1959 if (!sm)
1960 return 0;
1961 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1962 return 0;
1963 if (!is_kernel_success_fail(sm))
1964 return 0;
1966 rl = estate_rl(sm->state);
1967 if (!rl)
1968 return 0;
1970 __push_fake_cur_stree();
1972 final_pass = 0;
1973 __split_whole_condition(expr);
1974 final_pass = final_pass_orig;
1976 nonzero_rl = rl_filter(rl, rl_zero());
1977 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1978 return_ranges = show_rl(nonzero_rl);
1979 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1981 return_id++;
1982 FOR_EACH_PTR(returned_state_callbacks, cb) {
1983 cb->callback(return_id, return_ranges, expr);
1984 } END_FOR_EACH_PTR(cb);
1986 __push_true_states();
1987 __use_false_states();
1989 return_ranges = alloc_sname("0");
1990 zero_sval = sval_type_val(rl_type(rl), 0);
1991 add_range(&zero_rl, zero_sval, zero_sval);
1992 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1993 return_id++;
1994 FOR_EACH_PTR(returned_state_callbacks, cb) {
1995 cb->callback(return_id, return_ranges, expr);
1996 } END_FOR_EACH_PTR(cb);
1998 __merge_true_states();
1999 __free_fake_cur_stree();
2001 return 1;
2004 static int is_boolean(struct expression *expr)
2006 struct range_list *rl;
2008 if (!get_implied_rl(expr, &rl))
2009 return 0;
2010 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2011 return 1;
2012 return 0;
2015 static int splitable_function_call(struct expression *expr)
2017 struct sm_state *sm;
2019 if (!expr || expr->type != EXPR_CALL)
2020 return 0;
2021 sm = get_extra_sm_state(expr);
2022 return split_possible_helper(sm, expr);
2025 static struct sm_state *find_bool_param(void)
2027 struct stree *start_states;
2028 struct symbol *arg;
2029 struct sm_state *sm, *tmp;
2030 sval_t sval;
2032 start_states = get_start_states();
2034 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2035 if (!arg->ident)
2036 continue;
2037 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2038 if (!sm)
2039 continue;
2040 if (rl_min(estate_rl(sm->state)).value != 0 ||
2041 rl_max(estate_rl(sm->state)).value != 1)
2042 continue;
2043 goto found;
2044 } END_FOR_EACH_PTR_REVERSE(arg);
2046 return NULL;
2048 found:
2050 * Check if it's splitable. If not, then splitting it up is likely not
2051 * useful for the callers.
2053 FOR_EACH_PTR(sm->possible, tmp) {
2054 if (is_merged(tmp))
2055 continue;
2056 if (!estate_get_single_value(tmp->state, &sval))
2057 return NULL;
2058 } END_FOR_EACH_PTR(tmp);
2060 return sm;
2063 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2065 struct returned_state_callback *cb;
2066 struct range_list *ret_rl;
2067 const char *return_ranges;
2068 struct sm_state *tmp;
2069 int ret = 0;
2070 struct state_list *already_handled = NULL;
2072 if (!sm || !sm->merged)
2073 return 0;
2075 if (too_many_possible(sm))
2076 return 0;
2078 FOR_EACH_PTR(sm->possible, tmp) {
2079 if (tmp->merged)
2080 continue;
2081 if (ptr_in_list(tmp, already_handled))
2082 continue;
2083 add_ptr_list(&already_handled, tmp);
2085 ret = 1;
2086 __push_fake_cur_stree();
2088 overwrite_states_using_pool(sm, tmp);
2090 return_ranges = get_return_ranges_str(expr, &ret_rl);
2091 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2092 return_id++;
2093 FOR_EACH_PTR(returned_state_callbacks, cb) {
2094 cb->callback(return_id, (char *)return_ranges, expr);
2095 } END_FOR_EACH_PTR(cb);
2097 __free_fake_cur_stree();
2098 } END_FOR_EACH_PTR(tmp);
2100 free_slist(&already_handled);
2102 return ret;
2105 static int split_by_bool_param(struct expression *expr)
2107 struct sm_state *start_sm, *sm;
2108 sval_t sval;
2110 start_sm = find_bool_param();
2111 if (!start_sm)
2112 return 0;
2113 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2114 if (!sm || estate_get_single_value(sm->state, &sval))
2115 return 0;
2117 if (get_db_state_count() * 2 >= 2000)
2118 return 0;
2120 return split_on_bool_sm(sm, expr);
2123 static int split_by_null_nonnull_param(struct expression *expr)
2125 struct symbol *arg;
2126 struct sm_state *sm;
2127 int nr_possible;
2129 /* function must only take one pointer */
2130 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2131 return 0;
2132 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2133 if (!arg->ident)
2134 return 0;
2135 if (get_real_base_type(arg)->type != SYM_PTR)
2136 return 0;
2138 if (param_was_set_var_sym(arg->ident->name, arg))
2139 return 0;
2140 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2141 if (!sm)
2142 return 0;
2144 if (!has_separate_zero_null(sm))
2145 return 0;
2147 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2148 if (get_db_state_count() * nr_possible >= 2000)
2149 return 0;
2151 return split_on_bool_sm(sm, expr);
2154 struct expression *strip_expr_statement(struct expression *expr)
2156 struct expression *orig = expr;
2157 struct statement *stmt, *last_stmt;
2159 if (!expr)
2160 return NULL;
2161 if (expr->type == EXPR_PREOP && expr->op == '(')
2162 expr = expr->unop;
2163 if (expr->type != EXPR_STATEMENT)
2164 return orig;
2165 stmt = expr->statement;
2166 if (!stmt || stmt->type != STMT_COMPOUND)
2167 return orig;
2169 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2170 if (!last_stmt || last_stmt->type == STMT_LABEL)
2171 last_stmt = last_stmt->label_statement;
2172 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2173 return orig;
2174 return strip_expr(last_stmt->expression);
2177 static bool is_kernel_error_path(struct expression *expr)
2179 struct range_list *rl;
2182 * Splitting up returns requires resources. It also requires resources
2183 * for the caller. It doesn't seem worth it to split anything up.
2185 if (!get_implied_rl(expr, &rl))
2186 return false;
2187 if (rl_type(rl) != &int_ctype)
2188 return false;
2189 if (rl_min(rl).value >= -4095 &&
2190 rl_max(rl).value < 0)
2191 return true;
2192 return false;
2195 static void call_return_state_hooks(struct expression *expr)
2197 struct returned_state_callback *cb;
2198 struct range_list *ret_rl;
2199 const char *return_ranges;
2200 int nr_states;
2201 sval_t sval;
2203 if (__path_is_null())
2204 return;
2206 expr = strip_expr(expr);
2207 expr = strip_expr_statement(expr);
2209 if (is_impossible_path())
2210 goto vanilla;
2212 if (expr && (expr->type == EXPR_COMPARE ||
2213 !get_implied_value(expr, &sval)) &&
2214 (is_condition(expr) || is_boolean(expr))) {
2215 call_return_state_hooks_compare(expr);
2216 return;
2217 } else if (call_return_state_hooks_conditional(expr)) {
2218 return;
2219 } else if (is_kernel_error_path(expr)) {
2220 goto vanilla;
2221 } else if (call_return_state_hooks_split_possible(expr)) {
2222 return;
2223 } else if (split_positive_from_negative(expr)) {
2224 return;
2225 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2226 return;
2227 } else if (call_return_state_hooks_split_success_fail(expr)) {
2228 return;
2229 } else if (splitable_function_call(expr)) {
2230 return;
2231 } else if (split_by_bool_param(expr)) {
2232 } else if (split_by_null_nonnull_param(expr)) {
2233 return;
2236 vanilla:
2237 return_ranges = get_return_ranges_str(expr, &ret_rl);
2238 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2240 return_id++;
2241 nr_states = get_db_state_count();
2242 if (nr_states >= 10000) {
2243 match_return_info(return_id, (char *)return_ranges, expr);
2244 print_limited_param_set(return_id, (char *)return_ranges, expr);
2245 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2246 return;
2248 FOR_EACH_PTR(returned_state_callbacks, cb) {
2249 cb->callback(return_id, (char *)return_ranges, expr);
2250 } END_FOR_EACH_PTR(cb);
2253 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2255 struct returned_member_callback *cb;
2256 struct sm_state *sm;
2257 struct symbol *type;
2258 char *name;
2259 char member_name[256];
2260 int len;
2262 type = get_type(expr);
2263 if (!type || type->type != SYM_PTR)
2264 return;
2265 name = expr_to_var(expr);
2266 if (!name)
2267 return;
2269 len = strlen(name);
2270 FOR_EACH_PTR(returned_member_callbacks, cb) {
2271 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2272 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2273 strcpy(member_name, "*$");
2274 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2275 continue;
2277 if (strncmp(sm->name, name, len) != 0)
2278 continue;
2279 if (strncmp(sm->name + len, "->", 2) != 0)
2280 continue;
2281 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2282 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2283 } END_FOR_EACH_SM(sm);
2284 } END_FOR_EACH_PTR(cb);
2286 free_string(name);
2289 static void print_return_struct_info(int return_id, char *return_ranges,
2290 struct expression *expr,
2291 struct symbol *sym,
2292 struct return_info_callback *cb)
2294 struct sm_state *sm;
2295 const char *printed_name;
2296 int param;
2298 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2299 if (sm->sym && sm->sym == sym) {
2300 param = -1;
2301 } else {
2302 param = get_param_num_from_sym(sm->sym);
2303 if (param < 0)
2304 continue;
2307 printed_name = get_param_name(sm);
2308 if (!printed_name)
2309 continue;
2311 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2312 } END_FOR_EACH_SM(sm);
2315 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2317 struct return_info_callback *cb;
2318 struct expression *tmp;
2319 struct symbol *sym;
2321 if (!option_info && !__inline_fn &&
2322 !local_debug && !option_debug)
2323 return;
2325 tmp = get_fake_variable(expr);
2326 if (tmp)
2327 expr = tmp;
2328 sym = expr_to_sym(expr);
2330 FOR_EACH_PTR(return_callbacks, cb) {
2331 __ignore_param_used++;
2332 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2333 __ignore_param_used--;
2334 } END_FOR_EACH_PTR(cb);
2337 static void reset_memdb(struct symbol *sym)
2339 mem_sql(NULL, NULL, "delete from caller_info;");
2340 mem_sql(NULL, NULL, "delete from return_states;");
2341 mem_sql(NULL, NULL, "delete from call_implies;");
2342 mem_sql(NULL, NULL, "delete from return_implies;");
2345 static void match_end_func_info(struct symbol *sym)
2347 if (__path_is_null())
2348 return;
2349 call_return_state_hooks(NULL);
2352 static void match_after_func(struct symbol *sym)
2354 if (!__inline_fn)
2355 reset_memdb(sym);
2358 static void init_memdb(void)
2360 char *err = NULL;
2361 int rc;
2362 const char *schema_files[] = {
2363 "db/db.schema",
2364 "db/caller_info.schema",
2365 "db/common_caller_info.schema",
2366 "db/return_states.schema",
2367 "db/function_type_size.schema",
2368 "db/type_size.schema",
2369 "db/function_type_info.schema",
2370 "db/type_info.schema",
2371 "db/call_implies.schema",
2372 "db/return_implies.schema",
2373 "db/function_ptr.schema",
2374 "db/local_values.schema",
2375 "db/function_type_value.schema",
2376 "db/type_value.schema",
2377 "db/function_type.schema",
2378 "db/data_info.schema",
2379 "db/parameter_name.schema",
2380 "db/constraints.schema",
2381 "db/constraints_required.schema",
2382 "db/fn_ptr_data_link.schema",
2383 "db/fn_data_link.schema",
2384 "db/mtag_about.schema",
2385 "db/mtag_info.schema",
2386 "db/mtag_map.schema",
2387 "db/mtag_data.schema",
2388 "db/mtag_alias.schema",
2390 static char buf[4096];
2391 int fd;
2392 int ret;
2393 int i;
2395 rc = sqlite3_open(":memory:", &mem_db);
2396 if (rc != SQLITE_OK) {
2397 sm_ierror("starting In-Memory database.");
2398 return;
2401 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2402 fd = open_schema_file(schema_files[i]);
2403 if (fd < 0)
2404 continue;
2405 ret = read(fd, buf, sizeof(buf));
2406 if (ret < 0) {
2407 sm_ierror("failed to read: %s", schema_files[i]);
2408 continue;
2410 close(fd);
2411 if (ret == sizeof(buf)) {
2412 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2413 schema_files[i], sizeof(buf));
2414 continue;
2416 buf[ret] = '\0';
2417 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2418 if (rc != SQLITE_OK) {
2419 sm_ierror("SQL error #2: %s", err);
2420 sm_ierror("%s", buf);
2425 static void init_cachedb(void)
2427 char *err = NULL;
2428 int rc;
2429 const char *schema_files[] = {
2430 "db/call_implies.schema",
2431 "db/return_implies.schema",
2432 "db/type_info.schema",
2433 "db/mtag_about.schema",
2434 "db/mtag_data.schema",
2435 "db/mtag_info.schema",
2436 "db/sink_info.schema",
2438 static char buf[4096];
2439 int fd;
2440 int ret;
2441 int i;
2443 rc = sqlite3_open(":memory:", &cache_db);
2444 if (rc != SQLITE_OK) {
2445 sm_ierror("starting In-Memory database.");
2446 return;
2449 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2450 fd = open_schema_file(schema_files[i]);
2451 if (fd < 0)
2452 continue;
2453 ret = read(fd, buf, sizeof(buf));
2454 if (ret < 0) {
2455 sm_ierror("failed to read: %s", schema_files[i]);
2456 continue;
2458 close(fd);
2459 if (ret == sizeof(buf)) {
2460 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2461 schema_files[i], sizeof(buf));
2462 continue;
2464 buf[ret] = '\0';
2465 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2466 if (rc != SQLITE_OK) {
2467 sm_ierror("SQL error #2: %s", err);
2468 sm_ierror("%s", buf);
2473 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2475 static char buf[4096];
2476 char tmp[256];
2477 char *p = buf;
2478 char *table = _table;
2479 int i;
2482 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2483 for (i = 0; i < argc; i++) {
2484 if (i)
2485 p += snprintf(p, 4096 - (p - buf), ", ");
2486 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2487 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2490 p += snprintf(p, 4096 - (p - buf), ");");
2491 if (p - buf > 4096)
2492 return 0;
2494 sm_msg("SQL: %s", buf);
2495 return 0;
2498 static void dump_cache(struct symbol_list *sym_list)
2500 const char *cache_tables[] = {
2501 "type_info", "return_implies", "call_implies", "mtag_data",
2502 "mtag_info", "mtag_about", "sink_info",
2504 char buf[64];
2505 int i;
2507 if (!option_info)
2508 return;
2510 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2511 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2512 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2516 void open_smatch_db(char *db_file)
2518 int rc;
2520 if (option_no_db)
2521 return;
2523 use_states = malloc(num_checks + 1);
2524 memset(use_states, 0xff, num_checks + 1);
2526 init_memdb();
2527 init_cachedb();
2529 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2530 if (rc != SQLITE_OK) {
2531 option_no_db = 1;
2532 return;
2534 run_sql(NULL, NULL,
2535 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2536 return;
2539 static void register_common_funcs(void)
2541 struct token *token;
2542 char *func;
2543 char filename[256];
2545 if (option_project == PROJ_NONE)
2546 strcpy(filename, "common_functions");
2547 else
2548 snprintf(filename, 256, "%s.common_functions", option_project_str);
2550 token = get_tokens_file(filename);
2551 if (!token)
2552 return;
2553 if (token_type(token) != TOKEN_STREAMBEGIN)
2554 return;
2555 token = token->next;
2556 while (token_type(token) != TOKEN_STREAMEND) {
2557 if (token_type(token) != TOKEN_IDENT)
2558 return;
2559 func = alloc_string(show_ident(token->ident));
2560 add_ptr_list(&common_funcs, func);
2561 token = token->next;
2563 clear_token_alloc();
2566 static char *get_next_string(char **str)
2568 static char string[256];
2569 char *start;
2570 char *p = *str;
2571 int len, i, j;
2573 if (*p == '\0')
2574 return NULL;
2575 start = p;
2577 while (*p != '\0' && *p != '\n') {
2578 if (*p == '\\' && *(p + 1) == ' ') {
2579 p += 2;
2580 continue;
2582 if (*p == ' ')
2583 break;
2584 p++;
2587 len = p - start;
2588 if (len >= sizeof(string)) {
2589 memcpy(string, start, sizeof(string));
2590 string[sizeof(string) - 1] = '\0';
2591 sm_ierror("return_fix: '%s' too long", string);
2592 **str = '\0';
2593 return NULL;
2595 memcpy(string, start, len);
2596 string[len] = '\0';
2597 for (i = 0; i < sizeof(string) - 1; i++) {
2598 if (string[i] == '\\' && string[i + 1] == ' ') {
2599 for (j = i; string[j] != '\0'; j++)
2600 string[j] = string[j + 1];
2603 if (*p != '\0')
2604 p++;
2605 *str = p;
2606 return string;
2609 static void register_return_replacements(void)
2611 char *func, *orig, *new;
2612 char filename[256];
2613 char buf[4096];
2614 int fd, ret, i;
2615 char *p;
2617 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2618 fd = open_schema_file(filename);
2619 if (fd < 0)
2620 return;
2621 ret = read(fd, buf, sizeof(buf));
2622 close(fd);
2623 if (ret < 0)
2624 return;
2625 if (ret == sizeof(buf)) {
2626 sm_ierror("file too large: %s (limit %zd bytes)",
2627 filename, sizeof(buf));
2628 return;
2630 buf[ret] = '\0';
2632 p = buf;
2633 while (*p) {
2634 get_next_string(&p);
2635 replace_count++;
2637 if (replace_count == 0 || replace_count % 3 != 0) {
2638 replace_count = 0;
2639 return;
2641 replace_table = malloc(replace_count * sizeof(char *));
2643 p = buf;
2644 i = 0;
2645 while (*p) {
2646 func = alloc_string(get_next_string(&p));
2647 orig = alloc_string(get_next_string(&p));
2648 new = alloc_string(get_next_string(&p));
2650 replace_table[i++] = func;
2651 replace_table[i++] = orig;
2652 replace_table[i++] = new;
2656 void register_definition_db_callbacks(int id)
2658 my_id = id;
2660 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2661 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2662 add_split_return_callback(match_return_info);
2663 add_split_return_callback(print_returned_struct_members);
2664 add_split_return_callback(print_return_info);
2665 add_hook(&call_return_state_hooks, RETURN_HOOK);
2666 add_hook(&match_end_func_info, END_FUNC_HOOK);
2667 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2669 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2670 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2671 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2673 register_common_funcs();
2674 register_return_replacements();
2676 add_hook(&dump_cache, END_FILE_HOOK);
2679 void register_db_call_marker(int id)
2681 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2684 char *get_data_info_name(struct expression *expr)
2686 struct symbol *sym;
2687 char *name;
2688 char buf[256];
2689 char *ret = NULL;
2691 expr = strip_expr(expr);
2692 name = get_member_name(expr);
2693 if (name)
2694 return name;
2695 name = expr_to_var_sym(expr, &sym);
2696 if (!name || !sym)
2697 goto free;
2698 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2699 goto free;
2700 if (sym->ctype.modifiers & MOD_STATIC)
2701 snprintf(buf, sizeof(buf), "static %s", name);
2702 else
2703 snprintf(buf, sizeof(buf), "global %s", name);
2704 ret = alloc_sname(buf);
2705 free:
2706 free_string(name);
2707 return ret;