atomic_inc_dec: add this to check_list.h
[smatch.git] / smatch_db.c
blobba46ea862200ba600d2146011f5238db7725e5e8
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 if (sm_name[0] == '&' && strncmp(name, sm_name + 1, len) == 0) {
1015 if (sm_name[len + 1] != '.' && sm_name[len + 1] != '-')
1016 continue;
1017 if (is_address && sm_name[len + 1] == '.') {
1018 snprintf(printed_name, sizeof(printed_name),
1019 "&%s$->%s", add_star ? "*" : "",
1020 sm_name + len + 2);
1021 } else if (is_address && sm_name[len] == '-') {
1022 snprintf(printed_name, sizeof(printed_name),
1023 "&%s(*$)%s", add_star ? "*" : "",
1024 sm_name + len + 1);
1025 } else {
1026 snprintf(printed_name, sizeof(printed_name),
1027 "&%s$%s", add_star ? "*" : "",
1028 sm_name + len + 1);
1030 } else {
1031 continue;
1033 if (is_recursive_member(printed_name))
1034 continue;
1035 callback(call, param, printed_name, sm);
1036 } END_FOR_EACH_SM(sm);
1037 free:
1038 free_string(name);
1041 static void match_call_info(struct expression *call)
1043 struct member_info_callback *cb;
1044 struct expression *arg;
1045 int i;
1047 FOR_EACH_PTR(member_callbacks, cb) {
1048 i = -1;
1049 FOR_EACH_PTR(call->args, arg) {
1050 i++;
1051 print_struct_members(call, arg, i, cb->owner, cb->callback, 0);
1052 } END_FOR_EACH_PTR(arg);
1053 } END_FOR_EACH_PTR(cb);
1056 static struct expression *get_fake_variable(struct expression *expr)
1058 struct expression *tmp;
1060 tmp = expr_get_fake_parent_expr(expr);
1061 if (!tmp || tmp->type != EXPR_ASSIGNMENT)
1062 return NULL;
1064 return tmp->left;
1067 static void match_call_info_new(struct expression *call)
1069 struct member_info_callback *cb;
1070 struct expression *arg, *tmp;
1071 int i;
1073 if (!option_info && !__inline_call)
1074 return;
1076 FOR_EACH_PTR(member_callbacks_new, cb) {
1077 i = -1;
1078 FOR_EACH_PTR(call->args, arg) {
1079 i++;
1080 tmp = get_fake_variable(arg);
1081 if (!tmp)
1082 tmp = arg;
1083 __ignore_param_used++;
1084 print_struct_members(call, tmp, i, cb->owner, cb->callback, 1);
1085 __ignore_param_used--;
1086 } END_FOR_EACH_PTR(arg);
1087 } END_FOR_EACH_PTR(cb);
1090 static int get_param(int param, char **name, struct symbol **sym)
1092 struct symbol *arg;
1093 int i;
1095 i = 0;
1096 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1098 * this is a temporary hack to work around a bug (I think in sparse?)
1099 * 2.6.37-rc1:fs/reiserfs/journal.o
1100 * If there is a function definition without parameter name found
1101 * after a function implementation then it causes a crash.
1102 * int foo() {}
1103 * int bar(char *);
1105 if (arg->ident->name < (char *)100)
1106 continue;
1107 if (i == param) {
1108 *name = arg->ident->name;
1109 *sym = arg;
1110 return TRUE;
1112 i++;
1113 } END_FOR_EACH_PTR(arg);
1115 return FALSE;
1118 static int function_signature_matches(const char *sig)
1120 char *my_sig;
1122 my_sig = function_signature();
1123 if (!sig || !my_sig)
1124 return 1; /* default to matching */
1125 if (strcmp(my_sig, sig) == 0)
1126 return 1;
1127 return 0;
1130 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1132 struct select_caller_info_data *data = _data;
1133 int func_id;
1134 long type;
1135 long param;
1136 char *key;
1137 char *value;
1138 char *name = NULL;
1139 struct symbol *sym = NULL;
1140 struct def_callback *def_callback;
1141 struct stree *stree;
1142 struct timeval cur_time;
1144 data->results = 1;
1146 if (argc != 5)
1147 return 0;
1149 gettimeofday(&cur_time, NULL);
1150 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1151 return 0;
1153 func_id = atoi(argv[0]);
1154 errno = 0;
1155 type = strtol(argv[1], NULL, 10);
1156 param = strtol(argv[2], NULL, 10);
1157 if (errno)
1158 return 0;
1159 key = argv[3];
1160 value = argv[4];
1162 if (data->prev_func_id == -1)
1163 data->prev_func_id = func_id;
1164 if (func_id != data->prev_func_id) {
1165 stree = __pop_fake_cur_stree();
1166 if (!data->ignore)
1167 merge_stree(&data->final_states, stree);
1168 free_stree(&stree);
1169 __push_fake_cur_stree();
1170 __unnullify_path();
1171 data->prev_func_id = func_id;
1172 data->ignore = 0;
1175 if (data->ignore)
1176 return 0;
1177 if (type == INTERNAL &&
1178 !function_signature_matches(value)) {
1179 data->ignore = 1;
1180 return 0;
1183 if (param >= 0 && !get_param(param, &name, &sym))
1184 return 0;
1186 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1187 if (def_callback->hook_type == type)
1188 def_callback->callback(name, sym, key, value);
1189 } END_FOR_EACH_PTR(def_callback);
1191 return 0;
1194 static struct string_list *ptr_names_done;
1195 static struct string_list *ptr_names;
1197 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1199 insert_string(&ptr_names, alloc_string(argv[0]));
1200 return 0;
1203 static char *get_next_ptr_name(void)
1205 char *ptr;
1207 FOR_EACH_PTR(ptr_names, ptr) {
1208 if (!insert_string(&ptr_names_done, ptr))
1209 continue;
1210 return ptr;
1211 } END_FOR_EACH_PTR(ptr);
1212 return NULL;
1215 static void get_ptr_names(const char *file, const char *name)
1217 char sql_filter[1024];
1218 int before, after;
1220 if (file) {
1221 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1222 file, name);
1223 } else {
1224 snprintf(sql_filter, 1024, "function = '%s';", name);
1227 before = ptr_list_size((struct ptr_list *)ptr_names);
1229 run_sql(get_ptr_name, NULL,
1230 "select distinct ptr from function_ptr where %s",
1231 sql_filter);
1233 after = ptr_list_size((struct ptr_list *)ptr_names);
1234 if (before == after)
1235 return;
1237 while ((name = get_next_ptr_name()))
1238 get_ptr_names(NULL, name);
1241 static void match_data_from_db(struct symbol *sym)
1243 struct select_caller_info_data data = { .prev_func_id = -1 };
1244 struct sm_state *sm;
1245 struct stree *stree;
1246 struct timeval end_time;
1248 if (!sym || !sym->ident)
1249 return;
1251 set_fn_mtag(sym);
1252 gettimeofday(&data.start_time, NULL);
1254 __push_fake_cur_stree();
1255 __unnullify_path();
1257 if (!__inline_fn) {
1258 char *ptr;
1260 if (sym->ctype.modifiers & MOD_STATIC)
1261 get_ptr_names(get_base_file(), sym->ident->name);
1262 else
1263 get_ptr_names(NULL, sym->ident->name);
1265 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1266 __free_ptr_list((struct ptr_list **)&ptr_names);
1267 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1268 __free_fake_cur_stree();
1269 return;
1272 sql_select_caller_info(&data,
1273 "call_id, type, parameter, key, value",
1274 sym);
1277 stree = __pop_fake_cur_stree();
1278 if (!data.ignore)
1279 merge_stree(&data.final_states, stree);
1280 free_stree(&stree);
1281 __push_fake_cur_stree();
1282 __unnullify_path();
1283 data.prev_func_id = -1;
1284 data.ignore = 0;
1285 data.results = 0;
1287 FOR_EACH_PTR(ptr_names, ptr) {
1288 run_sql(caller_info_callback, &data,
1289 "select call_id, type, parameter, key, value"
1290 " from common_caller_info where function = '%s' order by call_id",
1291 ptr);
1292 } END_FOR_EACH_PTR(ptr);
1294 if (data.results) {
1295 FOR_EACH_PTR(ptr_names, ptr) {
1296 free_string(ptr);
1297 } END_FOR_EACH_PTR(ptr);
1298 goto free_ptr_names;
1301 FOR_EACH_PTR(ptr_names, ptr) {
1302 run_sql(caller_info_callback, &data,
1303 "select call_id, type, parameter, key, value"
1304 " from caller_info where function = '%s' order by call_id",
1305 ptr);
1306 free_string(ptr);
1307 } END_FOR_EACH_PTR(ptr);
1309 free_ptr_names:
1310 __free_ptr_list((struct ptr_list **)&ptr_names);
1311 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1312 } else {
1313 sql_select_caller_info(&data,
1314 "call_id, type, parameter, key, value",
1315 sym);
1318 stree = __pop_fake_cur_stree();
1319 if (!data.ignore)
1320 merge_stree(&data.final_states, stree);
1321 free_stree(&stree);
1323 gettimeofday(&end_time, NULL);
1324 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1325 FOR_EACH_SM(data.final_states, sm) {
1326 __set_sm(sm);
1327 } END_FOR_EACH_SM(sm);
1330 free_stree(&data.final_states);
1333 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1335 struct implies_info *info = _info;
1336 struct db_implies_callback *cb;
1337 struct expression *arg = NULL;
1338 int type;
1339 int param;
1341 if (argc != 5)
1342 return 0;
1344 type = atoi(argv[1]);
1345 param = atoi(argv[2]);
1347 FOR_EACH_PTR(info->cb_list, cb) {
1348 if (cb->type != type)
1349 continue;
1350 if (param != -1) {
1351 arg = get_argument_from_call_expr(info->expr->args, param);
1352 if (!arg)
1353 continue;
1355 cb->callback(info->expr, arg, argv[3], argv[4]);
1356 } END_FOR_EACH_PTR(cb);
1358 return 0;
1361 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1363 struct implies_info *info = _info;
1364 struct db_implies_callback *cb;
1365 struct expression *arg;
1366 struct symbol *sym;
1367 char *name;
1368 int type;
1369 int param;
1371 if (argc != 5)
1372 return 0;
1374 type = atoi(argv[1]);
1375 param = atoi(argv[2]);
1377 if (!get_param(param, &name, &sym))
1378 return 0;
1379 arg = symbol_expression(sym);
1380 if (!arg)
1381 return 0;
1383 FOR_EACH_PTR(info->cb_list, cb) {
1384 if (cb->type != type)
1385 continue;
1386 cb->callback(info->expr, arg, argv[3], argv[4]);
1387 } END_FOR_EACH_PTR(cb);
1389 return 0;
1392 static void match_return_implies(struct expression *expr)
1394 struct implies_info info = {
1395 .type = RETURN_IMPLIES,
1396 .cb_list = return_implies_cb_list,
1399 if (expr->fn->type != EXPR_SYMBOL ||
1400 !expr->fn->symbol)
1401 return;
1402 info.expr = expr;
1403 info.sym = expr->fn->symbol;
1404 sql_select_implies("function, type, parameter, key, value", &info,
1405 return_implies_callbacks);
1408 static void match_call_implies(struct symbol *sym)
1410 struct implies_info info = {
1411 .type = CALL_IMPLIES,
1412 .cb_list = call_implies_cb_list,
1415 if (!sym || !sym->ident)
1416 return;
1418 info.sym = sym;
1419 sql_select_implies("function, type, parameter, key, value", &info,
1420 call_implies_callbacks);
1423 static char *get_fn_param_str(struct expression *expr)
1425 struct expression *tmp;
1426 int param;
1427 char buf[32];
1429 tmp = get_assigned_expr(expr);
1430 if (tmp)
1431 expr = tmp;
1432 expr = strip_expr(expr);
1433 if (!expr || expr->type != EXPR_CALL)
1434 return NULL;
1435 expr = strip_expr(expr->fn);
1436 if (!expr || expr->type != EXPR_SYMBOL)
1437 return NULL;
1438 param = get_param_num(expr);
1439 if (param < 0)
1440 return NULL;
1442 snprintf(buf, sizeof(buf), "[r $%d]", param);
1443 return alloc_sname(buf);
1446 static char *get_return_compare_is_param(struct expression *expr)
1448 char *var;
1449 char buf[256];
1450 int comparison;
1451 int param;
1453 param = get_param_num(expr);
1454 if (param < 0)
1455 return NULL;
1457 var = expr_to_var(expr);
1458 if (!var)
1459 return NULL;
1460 snprintf(buf, sizeof(buf), "%s orig", var);
1461 comparison = get_comparison_strings(var, buf);
1462 free_string(var);
1464 if (!comparison)
1465 return NULL;
1467 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1468 return alloc_sname(buf);
1471 static char *get_return_compare_str(struct expression *expr)
1473 char *compare_str;
1475 compare_str = get_return_compare_is_param(expr);
1476 if (compare_str)
1477 return compare_str;
1479 compare_str = expr_lte_to_param(expr, -1);
1480 if (compare_str)
1481 return compare_str;
1483 return expr_param_comparison(expr, -1);
1486 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1488 struct range_list *rl;
1489 char *return_ranges;
1490 sval_t sval;
1491 char *fn_param_str;
1492 char *compare_str;
1493 char *math_str;
1494 char buf[128];
1496 *rl_p = NULL;
1498 if (!expr)
1499 return alloc_sname("");
1501 if (get_implied_value(expr, &sval)) {
1502 sval = sval_cast(cur_func_return_type(), sval);
1503 *rl_p = alloc_rl(sval, sval);
1504 return sval_to_str_or_err_ptr(sval);
1507 fn_param_str = get_fn_param_str(expr);
1508 compare_str = expr_equal_to_param(expr, -1);
1509 math_str = get_value_in_terms_of_parameter_math(expr);
1511 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1512 rl = cast_rl(cur_func_return_type(), rl);
1513 return_ranges = show_rl(rl);
1514 } else if (get_imaginary_absolute(expr, &rl)){
1515 rl = cast_rl(cur_func_return_type(), rl);
1516 return alloc_sname(show_rl(rl));
1517 } else {
1518 get_absolute_rl(expr, &rl);
1519 rl = cast_rl(cur_func_return_type(), rl);
1520 return_ranges = show_rl(rl);
1522 *rl_p = rl;
1524 if (fn_param_str) {
1525 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1526 return alloc_sname(buf);
1528 if (compare_str) {
1529 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1530 return alloc_sname(buf);
1532 if (math_str) {
1533 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1534 return alloc_sname(buf);
1536 compare_str = get_return_compare_str(expr);
1537 if (compare_str) {
1538 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1539 return alloc_sname(buf);
1542 return return_ranges;
1545 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1547 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1550 static bool call_return_state_hooks_conditional(struct expression *expr)
1552 int final_pass_orig = final_pass;
1553 static int recurse;
1555 if (recurse >= 2)
1556 return false;
1557 if (!expr ||
1558 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1559 return false;
1561 recurse++;
1563 __push_fake_cur_stree();
1565 final_pass = 0;
1566 __split_whole_condition(expr->conditional);
1567 final_pass = final_pass_orig;
1569 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1571 __push_true_states();
1572 __use_false_states();
1574 call_return_state_hooks(expr->cond_false);
1576 __merge_true_states();
1577 __free_fake_cur_stree();
1579 recurse--;
1580 return true;
1583 static void call_return_state_hooks_compare(struct expression *expr)
1585 struct returned_state_callback *cb;
1586 char *return_ranges;
1587 int final_pass_orig = final_pass;
1588 sval_t sval = { .type = &int_ctype };
1589 sval_t ret;
1591 if (!get_implied_value(expr, &ret))
1592 ret.value = -1;
1594 __push_fake_cur_stree();
1596 final_pass = 0;
1597 __split_whole_condition(expr);
1598 final_pass = final_pass_orig;
1600 if (ret.value != 0) {
1601 return_ranges = alloc_sname("1");
1602 sval.value = 1;
1603 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1605 return_id++;
1606 FOR_EACH_PTR(returned_state_callbacks, cb) {
1607 cb->callback(return_id, return_ranges, expr);
1608 } END_FOR_EACH_PTR(cb);
1611 __push_true_states();
1612 __use_false_states();
1614 if (ret.value != 1) {
1615 return_ranges = alloc_sname("0");
1616 sval.value = 0;
1617 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1619 return_id++;
1620 FOR_EACH_PTR(returned_state_callbacks, cb) {
1621 cb->callback(return_id, return_ranges, expr);
1622 } END_FOR_EACH_PTR(cb);
1625 __merge_true_states();
1626 __free_fake_cur_stree();
1629 static bool is_implies_function(struct expression *expr)
1631 struct range_list *rl;
1633 if (!expr)
1634 return false;
1636 rl = get_range_implications(get_function());
1637 if (!rl)
1638 return false;
1640 sm_msg("%s: is implied", __func__);
1641 return true;
1644 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1646 struct sm_state *tmp;
1648 FOR_EACH_PTR(slist, tmp) {
1649 if (strcmp(tmp->state->name, sm->state->name) == 0)
1650 return 1;
1651 } END_FOR_EACH_PTR(tmp);
1653 return 0;
1656 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1658 struct returned_state_callback *cb;
1659 struct range_list *rl;
1660 char *return_ranges;
1661 struct sm_state *tmp;
1662 int ret = 0;
1663 int nr_possible, nr_states;
1664 char *compare_str;
1665 char buf[128];
1666 struct state_list *already_handled = NULL;
1667 sval_t sval;
1669 if (!sm || !sm->merged)
1670 return 0;
1672 if (too_many_possible(sm) && !is_implies_function(expr))
1673 return 0;
1675 /* bail if it gets too complicated */
1676 nr_possible = 0;
1677 FOR_EACH_PTR(sm->possible, tmp) {
1678 if (tmp->merged)
1679 continue;
1680 if (ptr_in_list(tmp, already_handled))
1681 continue;
1682 add_ptr_list(&already_handled, tmp);
1683 nr_possible++;
1684 } END_FOR_EACH_PTR(tmp);
1685 free_slist(&already_handled);
1686 nr_states = get_db_state_count();
1687 if (nr_states * nr_possible >= 2000 && !is_implies_function(expr))
1688 return 0;
1690 FOR_EACH_PTR(sm->possible, tmp) {
1691 if (tmp->merged)
1692 continue;
1693 if (ptr_in_list(tmp, already_handled))
1694 continue;
1695 add_ptr_list(&already_handled, tmp);
1697 ret = 1;
1698 __push_fake_cur_stree();
1700 overwrite_states_using_pool(sm, tmp);
1702 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1703 return_ranges = show_rl(rl);
1704 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1705 compare_str = get_return_compare_str(expr);
1706 /* ignore obvious stuff like 0 <= param */
1707 /* Is this worthile when we have PARAM_COMPARE? */
1708 if (compare_str &&
1709 strncmp(compare_str, "[=", 2) != 0 &&
1710 rl_to_sval(rl, &sval))
1711 compare_str = NULL;
1712 if (compare_str) {
1713 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1714 return_ranges = alloc_sname(buf);
1717 return_id++;
1718 FOR_EACH_PTR(returned_state_callbacks, cb) {
1719 cb->callback(return_id, return_ranges, expr);
1720 } END_FOR_EACH_PTR(cb);
1722 __free_fake_cur_stree();
1723 } END_FOR_EACH_PTR(tmp);
1725 free_slist(&already_handled);
1727 return ret;
1730 static int call_return_state_hooks_split_possible(struct expression *expr)
1732 struct expression *fake;
1733 struct sm_state *sm;
1735 if (!expr)
1736 return 0;
1738 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1739 if (!sm) {
1740 fake = expr_get_fake_parent_expr(expr);
1741 if (!fake || fake->type != EXPR_ASSIGNMENT || fake->op != '=')
1742 return 0;
1743 fake = fake->left;
1744 sm = get_sm_state_expr(SMATCH_EXTRA, fake);
1746 return split_possible_helper(sm, expr);
1749 static bool has_possible_negative(struct sm_state *sm)
1751 struct sm_state *tmp;
1753 if (!type_signed(estate_type(sm->state)))
1754 return false;
1756 FOR_EACH_PTR(sm->possible, tmp) {
1757 if (!estate_rl(tmp->state))
1758 continue;
1759 if (sval_is_negative(estate_min(tmp->state)) &&
1760 sval_is_negative(estate_max(tmp->state)))
1761 return true;
1762 } END_FOR_EACH_PTR(tmp);
1764 return false;
1767 static bool has_separate_zero_null(struct sm_state *sm)
1769 struct sm_state *tmp;
1770 sval_t sval;
1772 FOR_EACH_PTR(sm->possible, tmp) {
1773 if (!estate_get_single_value(tmp->state, &sval))
1774 continue;
1775 if (sval.value == 0)
1776 return true;
1777 } END_FOR_EACH_PTR(tmp);
1779 return false;
1782 static int split_positive_from_negative(struct expression *expr)
1784 struct sm_state *sm;
1785 struct returned_state_callback *cb;
1786 struct range_list *rl;
1787 const char *return_ranges;
1788 struct range_list *ret_rl;
1789 bool separate_zero;
1790 int undo;
1792 /* We're going to print the states 3 times */
1793 if (get_db_state_count() > 10000 / 3)
1794 return 0;
1796 if (!get_implied_rl(expr, &rl) || !rl)
1797 return 0;
1798 /* Forget about INT_MAX and larger */
1799 if (rl_max(rl).value <= 0)
1800 return 0;
1801 if (!sval_is_negative(rl_min(rl)))
1802 return 0;
1804 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1805 if (!sm)
1806 return 0;
1807 if (!has_possible_negative(sm))
1808 return 0;
1809 separate_zero = has_separate_zero_null(sm);
1811 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1812 return 0;
1814 return_id++;
1815 return_ranges = get_return_ranges_str(expr, &ret_rl);
1816 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1817 FOR_EACH_PTR(returned_state_callbacks, cb) {
1818 cb->callback(return_id, (char *)return_ranges, expr);
1819 } END_FOR_EACH_PTR(cb);
1821 end_assume();
1823 if (separate_zero) {
1824 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1826 return_id++;
1827 return_ranges = get_return_ranges_str(expr, &ret_rl);
1828 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1829 FOR_EACH_PTR(returned_state_callbacks, cb) {
1830 cb->callback(return_id, (char *)return_ranges, expr);
1831 } END_FOR_EACH_PTR(cb);
1833 if (undo)
1834 end_assume();
1837 undo = assume(compare_expression(expr, '<', zero_expr()));
1839 return_id++;
1840 return_ranges = get_return_ranges_str(expr, &ret_rl);
1841 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1842 FOR_EACH_PTR(returned_state_callbacks, cb) {
1843 cb->callback(return_id, (char *)return_ranges, expr);
1844 } END_FOR_EACH_PTR(cb);
1846 if (undo)
1847 end_assume();
1849 return 1;
1852 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1854 struct returned_state_callback *cb;
1855 struct range_list *rl;
1856 struct range_list *nonnull_rl;
1857 sval_t null_sval;
1858 struct range_list *null_rl = NULL;
1859 char *return_ranges;
1860 struct sm_state *sm;
1861 struct smatch_state *state;
1862 int nr_states;
1863 int final_pass_orig = final_pass;
1865 if (!expr || expr_equal_to_param(expr, -1))
1866 return 0;
1867 if (expr->type == EXPR_CALL)
1868 return 0;
1870 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1871 if (!sm)
1872 return 0;
1873 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1874 return 0;
1875 state = sm->state;
1876 if (!estate_rl(state))
1877 return 0;
1878 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1879 return 0;
1880 if (has_possible_negative(sm))
1881 return 0;
1882 if (!has_separate_zero_null(sm))
1883 return 0;
1885 nr_states = get_db_state_count();
1886 if (option_info && nr_states >= 1500)
1887 return 0;
1889 rl = estate_rl(state);
1891 __push_fake_cur_stree();
1893 final_pass = 0;
1894 __split_whole_condition(expr);
1895 final_pass = final_pass_orig;
1897 nonnull_rl = rl_filter(rl, rl_zero());
1898 return_ranges = show_rl(nonnull_rl);
1899 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1901 return_id++;
1902 FOR_EACH_PTR(returned_state_callbacks, cb) {
1903 cb->callback(return_id, return_ranges, expr);
1904 } END_FOR_EACH_PTR(cb);
1906 __push_true_states();
1907 __use_false_states();
1909 return_ranges = alloc_sname("0");
1910 null_sval = sval_type_val(rl_type(rl), 0);
1911 add_range(&null_rl, null_sval, null_sval);
1912 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1913 return_id++;
1914 FOR_EACH_PTR(returned_state_callbacks, cb) {
1915 cb->callback(return_id, return_ranges, expr);
1916 } END_FOR_EACH_PTR(cb);
1918 __merge_true_states();
1919 __free_fake_cur_stree();
1921 return 1;
1924 static bool is_kernel_success_fail(struct sm_state *sm)
1926 struct sm_state *tmp;
1927 struct range_list *rl;
1928 bool has_zero = false;
1929 bool has_neg = false;
1931 if (!type_signed(estate_type(sm->state)))
1932 return false;
1934 FOR_EACH_PTR(sm->possible, tmp) {
1935 rl = estate_rl(tmp->state);
1936 if (!rl)
1937 return false;
1938 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1939 has_zero = true;
1940 continue;
1942 has_neg = true;
1943 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1944 continue;
1945 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1946 continue;
1947 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1948 continue;
1949 return false;
1950 } END_FOR_EACH_PTR(tmp);
1952 return has_zero && has_neg;
1955 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1957 struct sm_state *sm;
1958 struct range_list *rl;
1959 struct range_list *nonzero_rl;
1960 sval_t zero_sval;
1961 struct range_list *zero_rl = NULL;
1962 int nr_states;
1963 struct returned_state_callback *cb;
1964 char *return_ranges;
1965 int final_pass_orig = final_pass;
1967 if (option_project != PROJ_KERNEL)
1968 return 0;
1970 nr_states = get_db_state_count();
1971 if (nr_states > 2000)
1972 return 0;
1974 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1975 if (!sm)
1976 return 0;
1977 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1978 return 0;
1979 if (!is_kernel_success_fail(sm))
1980 return 0;
1982 rl = estate_rl(sm->state);
1983 if (!rl)
1984 return 0;
1986 __push_fake_cur_stree();
1988 final_pass = 0;
1989 __split_whole_condition(expr);
1990 final_pass = final_pass_orig;
1992 nonzero_rl = rl_filter(rl, rl_zero());
1993 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1994 return_ranges = show_rl(nonzero_rl);
1995 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1997 return_id++;
1998 FOR_EACH_PTR(returned_state_callbacks, cb) {
1999 cb->callback(return_id, return_ranges, expr);
2000 } END_FOR_EACH_PTR(cb);
2002 __push_true_states();
2003 __use_false_states();
2005 return_ranges = alloc_sname("0");
2006 zero_sval = sval_type_val(rl_type(rl), 0);
2007 add_range(&zero_rl, zero_sval, zero_sval);
2008 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
2009 return_id++;
2010 FOR_EACH_PTR(returned_state_callbacks, cb) {
2011 cb->callback(return_id, return_ranges, expr);
2012 } END_FOR_EACH_PTR(cb);
2014 __merge_true_states();
2015 __free_fake_cur_stree();
2017 return 1;
2020 static int is_boolean(struct expression *expr)
2022 struct range_list *rl;
2024 if (!get_implied_rl(expr, &rl))
2025 return 0;
2026 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
2027 return 1;
2028 return 0;
2031 static int splitable_function_call(struct expression *expr)
2033 struct sm_state *sm;
2035 if (!expr || expr->type != EXPR_CALL)
2036 return 0;
2037 sm = get_extra_sm_state(expr);
2038 return split_possible_helper(sm, expr);
2041 static struct sm_state *find_bool_param(void)
2043 struct stree *start_states;
2044 struct symbol *arg;
2045 struct sm_state *sm, *tmp;
2046 sval_t sval;
2048 start_states = get_start_states();
2050 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
2051 if (!arg->ident)
2052 continue;
2053 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
2054 if (!sm)
2055 continue;
2056 if (rl_min(estate_rl(sm->state)).value != 0 ||
2057 rl_max(estate_rl(sm->state)).value != 1)
2058 continue;
2059 goto found;
2060 } END_FOR_EACH_PTR_REVERSE(arg);
2062 return NULL;
2064 found:
2066 * Check if it's splitable. If not, then splitting it up is likely not
2067 * useful for the callers.
2069 FOR_EACH_PTR(sm->possible, tmp) {
2070 if (is_merged(tmp))
2071 continue;
2072 if (!estate_get_single_value(tmp->state, &sval))
2073 return NULL;
2074 } END_FOR_EACH_PTR(tmp);
2076 return sm;
2079 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
2081 struct returned_state_callback *cb;
2082 struct range_list *ret_rl;
2083 const char *return_ranges;
2084 struct sm_state *tmp;
2085 int ret = 0;
2086 struct state_list *already_handled = NULL;
2088 if (!sm || !sm->merged)
2089 return 0;
2091 if (too_many_possible(sm))
2092 return 0;
2094 FOR_EACH_PTR(sm->possible, tmp) {
2095 if (tmp->merged)
2096 continue;
2097 if (ptr_in_list(tmp, already_handled))
2098 continue;
2099 add_ptr_list(&already_handled, tmp);
2101 ret = 1;
2102 __push_fake_cur_stree();
2104 overwrite_states_using_pool(sm, tmp);
2106 return_ranges = get_return_ranges_str(expr, &ret_rl);
2107 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2108 return_id++;
2109 FOR_EACH_PTR(returned_state_callbacks, cb) {
2110 cb->callback(return_id, (char *)return_ranges, expr);
2111 } END_FOR_EACH_PTR(cb);
2113 __free_fake_cur_stree();
2114 } END_FOR_EACH_PTR(tmp);
2116 free_slist(&already_handled);
2118 return ret;
2121 static int split_by_bool_param(struct expression *expr)
2123 struct sm_state *start_sm, *sm;
2124 sval_t sval;
2126 start_sm = find_bool_param();
2127 if (!start_sm)
2128 return 0;
2129 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2130 if (!sm || estate_get_single_value(sm->state, &sval))
2131 return 0;
2133 if (get_db_state_count() * 2 >= 2000)
2134 return 0;
2136 return split_on_bool_sm(sm, expr);
2139 static int split_by_null_nonnull_param(struct expression *expr)
2141 struct symbol *arg;
2142 struct sm_state *sm;
2143 int nr_possible;
2145 /* function must only take one pointer */
2146 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2147 return 0;
2148 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2149 if (!arg->ident)
2150 return 0;
2151 if (get_real_base_type(arg)->type != SYM_PTR)
2152 return 0;
2154 if (param_was_set_var_sym(arg->ident->name, arg))
2155 return 0;
2156 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2157 if (!sm)
2158 return 0;
2160 if (!has_separate_zero_null(sm))
2161 return 0;
2163 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2164 if (get_db_state_count() * nr_possible >= 2000)
2165 return 0;
2167 return split_on_bool_sm(sm, expr);
2170 struct expression *strip_expr_statement(struct expression *expr)
2172 struct expression *orig = expr;
2173 struct statement *stmt, *last_stmt;
2175 if (!expr)
2176 return NULL;
2177 if (expr->type == EXPR_PREOP && expr->op == '(')
2178 expr = expr->unop;
2179 if (expr->type != EXPR_STATEMENT)
2180 return orig;
2181 stmt = expr->statement;
2182 if (!stmt || stmt->type != STMT_COMPOUND)
2183 return orig;
2185 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2186 if (!last_stmt || last_stmt->type == STMT_LABEL)
2187 last_stmt = last_stmt->label_statement;
2188 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2189 return orig;
2190 return strip_expr(last_stmt->expression);
2193 static bool is_kernel_error_path(struct expression *expr)
2195 struct range_list *rl;
2198 * Splitting up returns requires resources. It also requires resources
2199 * for the caller. It doesn't seem worth it to split anything up.
2201 if (!get_implied_rl(expr, &rl))
2202 return false;
2203 if (rl_type(rl) != &int_ctype)
2204 return false;
2205 if (rl_min(rl).value >= -4095 &&
2206 rl_max(rl).value < 0)
2207 return true;
2208 return false;
2211 static void call_return_state_hooks(struct expression *expr)
2213 struct returned_state_callback *cb;
2214 struct range_list *ret_rl;
2215 const char *return_ranges;
2216 int nr_states;
2217 sval_t sval;
2219 if (__path_is_null())
2220 return;
2222 expr = strip_expr(expr);
2223 expr = strip_expr_statement(expr);
2225 if (is_impossible_path())
2226 goto vanilla;
2228 if (expr && (expr->type == EXPR_COMPARE ||
2229 !get_implied_value(expr, &sval)) &&
2230 (is_condition(expr) || is_boolean(expr))) {
2231 call_return_state_hooks_compare(expr);
2232 return;
2233 } else if (call_return_state_hooks_conditional(expr)) {
2234 return;
2235 } else if (is_kernel_error_path(expr)) {
2236 goto vanilla;
2237 } else if (call_return_state_hooks_split_possible(expr)) {
2238 return;
2239 } else if (split_positive_from_negative(expr)) {
2240 return;
2241 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2242 return;
2243 } else if (call_return_state_hooks_split_success_fail(expr)) {
2244 return;
2245 } else if (splitable_function_call(expr)) {
2246 return;
2247 } else if (split_by_bool_param(expr)) {
2248 } else if (split_by_null_nonnull_param(expr)) {
2249 return;
2252 vanilla:
2253 return_ranges = get_return_ranges_str(expr, &ret_rl);
2254 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2256 return_id++;
2257 nr_states = get_db_state_count();
2258 if (nr_states >= 10000) {
2259 match_return_info(return_id, (char *)return_ranges, expr);
2260 print_limited_param_set(return_id, (char *)return_ranges, expr);
2261 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2262 return;
2264 FOR_EACH_PTR(returned_state_callbacks, cb) {
2265 cb->callback(return_id, (char *)return_ranges, expr);
2266 } END_FOR_EACH_PTR(cb);
2269 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2271 struct returned_member_callback *cb;
2272 struct sm_state *sm;
2273 struct symbol *type;
2274 char *name;
2275 char member_name[256];
2276 int len;
2278 type = get_type(expr);
2279 if (!type || type->type != SYM_PTR)
2280 return;
2281 name = expr_to_var(expr);
2282 if (!name)
2283 return;
2285 len = strlen(name);
2286 FOR_EACH_PTR(returned_member_callbacks, cb) {
2287 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2288 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2289 strcpy(member_name, "*$");
2290 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2291 continue;
2293 if (strncmp(sm->name, name, len) != 0)
2294 continue;
2295 if (strncmp(sm->name + len, "->", 2) != 0)
2296 continue;
2297 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2298 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2299 } END_FOR_EACH_SM(sm);
2300 } END_FOR_EACH_PTR(cb);
2302 free_string(name);
2305 static void print_return_struct_info(int return_id, char *return_ranges,
2306 struct expression *expr,
2307 struct symbol *sym,
2308 struct return_info_callback *cb)
2310 struct sm_state *sm;
2311 const char *printed_name;
2312 int param;
2314 FOR_EACH_MY_SM(cb->owner, __get_cur_stree(), sm) {
2315 if (sm->sym && sm->sym == sym) {
2316 param = -1;
2317 } else {
2318 param = get_param_num_from_sym(sm->sym);
2319 if (param < 0)
2320 continue;
2323 printed_name = get_param_name(sm);
2324 if (!printed_name)
2325 continue;
2327 cb->callback(return_id, return_ranges, expr, param, printed_name, sm);
2328 } END_FOR_EACH_SM(sm);
2331 static void print_return_info(int return_id, char *return_ranges, struct expression *expr)
2333 struct return_info_callback *cb;
2334 struct expression *tmp;
2335 struct symbol *sym;
2337 if (!option_info && !__inline_fn &&
2338 !local_debug && !option_debug)
2339 return;
2341 tmp = get_fake_variable(expr);
2342 if (tmp)
2343 expr = tmp;
2344 sym = expr_to_sym(expr);
2346 FOR_EACH_PTR(return_callbacks, cb) {
2347 __ignore_param_used++;
2348 print_return_struct_info(return_id, return_ranges, expr, sym, cb);
2349 __ignore_param_used--;
2350 } END_FOR_EACH_PTR(cb);
2353 static void reset_memdb(struct symbol *sym)
2355 mem_sql(NULL, NULL, "delete from caller_info;");
2356 mem_sql(NULL, NULL, "delete from return_states;");
2357 mem_sql(NULL, NULL, "delete from call_implies;");
2358 mem_sql(NULL, NULL, "delete from return_implies;");
2361 static void match_end_func_info(struct symbol *sym)
2363 if (__path_is_null())
2364 return;
2365 call_return_state_hooks(NULL);
2368 static void match_after_func(struct symbol *sym)
2370 if (!__inline_fn)
2371 reset_memdb(sym);
2374 static void init_memdb(void)
2376 char *err = NULL;
2377 int rc;
2378 const char *schema_files[] = {
2379 "db/db.schema",
2380 "db/caller_info.schema",
2381 "db/common_caller_info.schema",
2382 "db/return_states.schema",
2383 "db/function_type_size.schema",
2384 "db/type_size.schema",
2385 "db/function_type_info.schema",
2386 "db/type_info.schema",
2387 "db/call_implies.schema",
2388 "db/return_implies.schema",
2389 "db/function_ptr.schema",
2390 "db/local_values.schema",
2391 "db/function_type_value.schema",
2392 "db/type_value.schema",
2393 "db/function_type.schema",
2394 "db/data_info.schema",
2395 "db/parameter_name.schema",
2396 "db/constraints.schema",
2397 "db/constraints_required.schema",
2398 "db/fn_ptr_data_link.schema",
2399 "db/fn_data_link.schema",
2400 "db/mtag_about.schema",
2401 "db/mtag_info.schema",
2402 "db/mtag_map.schema",
2403 "db/mtag_data.schema",
2404 "db/mtag_alias.schema",
2406 static char buf[4096];
2407 int fd;
2408 int ret;
2409 int i;
2411 rc = sqlite3_open(":memory:", &mem_db);
2412 if (rc != SQLITE_OK) {
2413 sm_ierror("starting In-Memory database.");
2414 return;
2417 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2418 fd = open_schema_file(schema_files[i]);
2419 if (fd < 0)
2420 continue;
2421 ret = read(fd, buf, sizeof(buf));
2422 if (ret < 0) {
2423 sm_ierror("failed to read: %s", schema_files[i]);
2424 continue;
2426 close(fd);
2427 if (ret == sizeof(buf)) {
2428 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2429 schema_files[i], sizeof(buf));
2430 continue;
2432 buf[ret] = '\0';
2433 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2434 if (rc != SQLITE_OK) {
2435 sm_ierror("SQL error #2: %s", err);
2436 sm_ierror("%s", buf);
2441 static void init_cachedb(void)
2443 char *err = NULL;
2444 int rc;
2445 const char *schema_files[] = {
2446 "db/call_implies.schema",
2447 "db/return_implies.schema",
2448 "db/type_info.schema",
2449 "db/mtag_about.schema",
2450 "db/mtag_data.schema",
2451 "db/mtag_info.schema",
2452 "db/sink_info.schema",
2454 static char buf[4096];
2455 int fd;
2456 int ret;
2457 int i;
2459 rc = sqlite3_open(":memory:", &cache_db);
2460 if (rc != SQLITE_OK) {
2461 sm_ierror("starting In-Memory database.");
2462 return;
2465 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2466 fd = open_schema_file(schema_files[i]);
2467 if (fd < 0)
2468 continue;
2469 ret = read(fd, buf, sizeof(buf));
2470 if (ret < 0) {
2471 sm_ierror("failed to read: %s", schema_files[i]);
2472 continue;
2474 close(fd);
2475 if (ret == sizeof(buf)) {
2476 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2477 schema_files[i], sizeof(buf));
2478 continue;
2480 buf[ret] = '\0';
2481 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2482 if (rc != SQLITE_OK) {
2483 sm_ierror("SQL error #2: %s", err);
2484 sm_ierror("%s", buf);
2489 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2491 static char buf[4096];
2492 char tmp[256];
2493 char *p = buf;
2494 char *table = _table;
2495 int i;
2498 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2499 for (i = 0; i < argc; i++) {
2500 if (i)
2501 p += snprintf(p, 4096 - (p - buf), ", ");
2502 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2503 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2506 p += snprintf(p, 4096 - (p - buf), ");");
2507 if (p - buf > 4096)
2508 return 0;
2510 sm_msg("SQL: %s", buf);
2511 return 0;
2514 static void dump_cache(struct symbol_list *sym_list)
2516 const char *cache_tables[] = {
2517 "type_info", "return_implies", "call_implies", "mtag_data",
2518 "mtag_info", "mtag_about", "sink_info",
2520 char buf[64];
2521 int i;
2523 if (!option_info)
2524 return;
2526 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2527 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2528 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2532 void open_smatch_db(char *db_file)
2534 int rc;
2536 if (option_no_db)
2537 return;
2539 use_states = malloc(num_checks + 1);
2540 memset(use_states, 0xff, num_checks + 1);
2542 init_memdb();
2543 init_cachedb();
2545 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2546 if (rc != SQLITE_OK) {
2547 option_no_db = 1;
2548 return;
2550 run_sql(NULL, NULL,
2551 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2552 return;
2555 static char *get_next_string(char **str)
2557 static char string[256];
2558 char *start;
2559 char *p = *str;
2560 int len, i, j;
2562 if (*p == '\0')
2563 return NULL;
2564 start = p;
2566 while (*p != '\0' && *p != '\n') {
2567 if (*p == '\\' && *(p + 1) == ' ') {
2568 p += 2;
2569 continue;
2571 if (*p == ' ')
2572 break;
2573 p++;
2576 len = p - start;
2577 if (len >= sizeof(string)) {
2578 memcpy(string, start, sizeof(string));
2579 string[sizeof(string) - 1] = '\0';
2580 sm_ierror("return_fix: '%s' too long", string);
2581 **str = '\0';
2582 return NULL;
2584 memcpy(string, start, len);
2585 string[len] = '\0';
2586 for (i = 0; i < sizeof(string) - 1; i++) {
2587 if (string[i] == '\\' && string[i + 1] == ' ') {
2588 for (j = i; string[j] != '\0'; j++)
2589 string[j] = string[j + 1];
2592 if (*p != '\0')
2593 p++;
2594 *str = p;
2595 return string;
2598 static void register_return_replacements(void)
2600 char *func, *orig, *new;
2601 char filename[256];
2602 char buf[4096];
2603 int fd, ret, i;
2604 char *p;
2606 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2607 fd = open_schema_file(filename);
2608 if (fd < 0)
2609 return;
2610 ret = read(fd, buf, sizeof(buf));
2611 close(fd);
2612 if (ret < 0)
2613 return;
2614 if (ret == sizeof(buf)) {
2615 sm_ierror("file too large: %s (limit %zd bytes)",
2616 filename, sizeof(buf));
2617 return;
2619 buf[ret] = '\0';
2621 p = buf;
2622 while (*p) {
2623 get_next_string(&p);
2624 replace_count++;
2626 if (replace_count == 0 || replace_count % 3 != 0) {
2627 replace_count = 0;
2628 return;
2630 replace_table = malloc(replace_count * sizeof(char *));
2632 p = buf;
2633 i = 0;
2634 while (*p) {
2635 func = alloc_string(get_next_string(&p));
2636 orig = alloc_string(get_next_string(&p));
2637 new = alloc_string(get_next_string(&p));
2639 replace_table[i++] = func;
2640 replace_table[i++] = orig;
2641 replace_table[i++] = new;
2645 void register_definition_db_callbacks(int id)
2647 my_id = id;
2649 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2650 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2651 add_split_return_callback(match_return_info);
2652 add_split_return_callback(print_returned_struct_members);
2653 add_split_return_callback(print_return_info);
2654 add_hook(&call_return_state_hooks, RETURN_HOOK);
2655 add_hook(&match_end_func_info, END_FUNC_HOOK);
2656 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2658 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2659 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2660 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2662 common_funcs = load_strings_from_file(option_project_str, "common_functions");
2663 register_return_replacements();
2665 add_hook(&dump_cache, END_FILE_HOOK);
2668 void register_db_call_marker(int id)
2670 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2673 char *get_data_info_name(struct expression *expr)
2675 struct symbol *sym;
2676 char *name;
2677 char buf[256];
2678 char *ret = NULL;
2680 expr = strip_expr(expr);
2681 name = get_member_name(expr);
2682 if (name)
2683 return name;
2684 name = expr_to_var_sym(expr, &sym);
2685 if (!name || !sym)
2686 goto free;
2687 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2688 goto free;
2689 if (sym->ctype.modifiers & MOD_STATIC)
2690 snprintf(buf, sizeof(buf), "static %s", name);
2691 else
2692 snprintf(buf, sizeof(buf), "global %s", name);
2693 ret = alloc_sname(buf);
2694 free:
2695 free_string(name);
2696 return ret;