math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_db.c
blob3ad569f53d9d81ee7202de80961ed68bc1b6ed86
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 static int return_id;
32 #define SQLITE_CACHE_PAGES 1000
34 struct def_callback {
35 int hook_type;
36 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
38 ALLOCATOR(def_callback, "definition db hook callbacks");
39 DECLARE_PTR_LIST(callback_list, struct def_callback);
40 static struct callback_list *select_caller_info_callbacks;
42 struct member_info_callback {
43 int owner;
44 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
46 ALLOCATOR(member_info_callback, "caller_info callbacks");
47 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
48 static struct member_info_cb_list *member_callbacks;
50 struct returned_state_callback {
51 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
53 ALLOCATOR(returned_state_callback, "returned state callbacks");
54 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
55 static struct returned_state_cb_list *returned_state_callbacks;
57 struct returned_member_callback {
58 int owner;
59 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
61 ALLOCATOR(returned_member_callback, "returned member callbacks");
62 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
63 static struct returned_member_cb_list *returned_member_callbacks;
65 struct db_implies_callback {
66 int type;
67 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
69 ALLOCATOR(db_implies_callback, "return_implies callbacks");
70 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
71 static struct db_implies_cb_list *return_implies_cb_list;
72 static struct db_implies_cb_list *call_implies_cb_list;
74 /* silently truncates if needed. */
75 char *escape_newlines(const char *str)
77 char buf[1024] = "";
78 bool found = false;
79 int i, j;
81 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
82 if (str[i] != '\n') {
83 buf[j] = str[i];
84 continue;
87 found = true;
88 buf[j++] = '\\';
89 if (j == sizeof(buf))
90 break;
91 buf[j] = 'n';
94 if (!found)
95 return alloc_sname(str);
97 if (j == sizeof(buf))
98 buf[j - 1] = '\0';
99 return alloc_sname(buf);
102 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
104 int i;
106 for (i = 0; i < argc; i++) {
107 if (i != 0)
108 printf(", ");
109 sm_printf("%s", argv[i]);
111 sm_printf("\n");
112 return 0;
115 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
117 char *err = NULL;
118 int rc;
120 if (!db)
121 return;
123 if (option_debug) {
124 sm_msg("%s", sql);
125 if (strncasecmp(sql, "select", strlen("select")) == 0)
126 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
129 rc = sqlite3_exec(db, sql, callback, data, &err);
130 if (rc != SQLITE_OK && !parse_error) {
131 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
132 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
133 parse_error = 1;
137 static int replace_count;
138 static char **replace_table;
139 static const char *replace_return_ranges(const char *return_ranges)
141 int i;
143 if (!get_function()) {
144 /* I have no idea why EXPORT_SYMBOL() is here */
145 return return_ranges;
147 for (i = 0; i < replace_count; i += 3) {
148 if (strcmp(replace_table[i + 0], get_function()) == 0) {
149 if (strcmp(replace_table[i + 1], return_ranges) == 0)
150 return replace_table[i + 2];
153 return return_ranges;
157 static char *use_states;
158 static int get_db_state_count(void)
160 struct sm_state *sm;
161 int count = 0;
163 FOR_EACH_SM(__get_cur_stree(), sm) {
164 if (sm->owner == USHRT_MAX)
165 continue;
166 if (use_states[sm->owner])
167 count++;
168 } END_FOR_EACH_SM(sm);
169 return count;
172 void db_ignore_states(int id)
174 use_states[id] = 0;
177 void sql_insert_return_states(int return_id, const char *return_ranges,
178 int type, int param, const char *key, const char *value)
180 if (key && strlen(key) >= 80)
181 return;
182 return_ranges = replace_return_ranges(return_ranges);
183 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
184 get_base_file(), get_function(), (unsigned long)__inline_fn,
185 return_id, return_ranges, fn_static(), type, param, key, value);
188 static struct string_list *common_funcs;
189 static int is_common_function(const char *fn)
191 char *tmp;
193 if (!fn)
194 return 0;
196 if (strncmp(fn, "__builtin_", 10) == 0)
197 return 1;
199 FOR_EACH_PTR(common_funcs, tmp) {
200 if (strcmp(tmp, fn) == 0)
201 return 1;
202 } END_FOR_EACH_PTR(tmp);
204 return 0;
207 static char *function_signature(void)
209 return type_to_str(get_real_base_type(cur_func_sym));
212 void sql_insert_caller_info(struct expression *call, int type,
213 int param, const char *key, const char *value)
215 FILE *tmp_fd = sm_outfd;
216 char *fn;
218 if (!option_info && !__inline_call)
219 return;
221 if (key && strlen(key) >= 80)
222 return;
224 fn = get_fnptr_name(call->fn);
225 if (!fn)
226 return;
228 if (__inline_call) {
229 mem_sql(NULL, NULL,
230 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
231 get_base_file(), get_function(), fn, (unsigned long)call,
232 is_static(call->fn), type, param, key, value);
235 if (!option_info)
236 return;
238 if (strncmp(fn, "__builtin_", 10) == 0)
239 return;
240 if (type != INTERNAL && is_common_function(fn))
241 return;
243 sm_outfd = caller_info_fd;
244 sm_msg("SQL_caller_info: insert into caller_info values ("
245 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
246 get_base_file(), get_function(), fn, is_static(call->fn),
247 type, param, key, value);
248 sm_outfd = tmp_fd;
250 free_string(fn);
253 void sql_insert_function_ptr(const char *fn, const char *struct_name)
255 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
256 struct_name);
259 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
261 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
262 get_base_file(), get_function(), (unsigned long)__inline_fn,
263 fn_static(), type, param, key, value);
266 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
268 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
269 get_base_file(), get_function(), (unsigned long)__inline_fn,
270 fn_static(), type, param, key, value);
273 void sql_insert_function_type_size(const char *member, const char *ranges)
275 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
278 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
280 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
283 void sql_insert_type_info(int type, const char *member, const char *value)
285 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
288 void sql_insert_local_values(const char *name, const char *value)
290 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
293 void sql_insert_function_type_value(const char *type, const char *value)
295 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
298 void sql_insert_function_type(int param, const char *value)
300 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
301 get_base_file(), get_function(), fn_static(), param, value);
304 void sql_insert_parameter_name(int param, const char *value)
306 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
307 get_base_file(), get_function(), fn_static(), param, value);
310 void sql_insert_data_info(struct expression *data, int type, const char *value)
312 char *data_name;
314 data_name = get_data_info_name(data);
315 if (!data_name)
316 return;
317 sql_insert(data_info, "'%s', '%s', %d, '%s'",
318 is_static(data) ? get_base_file() : "extern",
319 data_name, type, value);
322 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
324 sql_insert(data_info, "'%s', '%s', %d, '%s'",
325 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
326 var, type, value);
329 void sql_save_constraint(const char *con)
331 if (!option_info)
332 return;
334 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
337 void sql_save_constraint_required(const char *data, int op, const char *limit)
339 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
342 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
344 if (!option_info)
345 return;
347 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
348 "select constraints_required.data, constraints_required.op, '%s' from "
349 "constraints_required where bound = '%s';", new_limit, old_limit);
352 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
354 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
357 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
359 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
360 return;
362 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
363 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
364 fn->symbol->ident->name,
365 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
366 type, param, key, value);
369 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
371 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
372 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
375 void sql_insert_mtag_data(mtag_t tag, const char *var, int offset, int type, const char *value)
377 sql_insert(mtag_data, "%lld, '%s', %d, %d, '%s'", tag, var, offset, type, value);
380 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
382 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
385 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
387 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
390 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
392 mtag_t *saved_tag = _tag;
393 mtag_t new_tag;
395 new_tag = strtoll(argv[0], NULL, 10);
397 if (!*saved_tag)
398 *saved_tag = new_tag;
399 else if (*saved_tag != new_tag)
400 *saved_tag = -1ULL;
402 return 0;
405 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
407 mtag_t tmp = 0;
409 run_sql(save_mtag, &tmp,
410 "select container from mtag_map where tag = %lld and offset = %d;",
411 tag, offset);
413 if (tmp == 0 || tmp == -1ULL)
414 return 0;
415 *container = tmp;
416 return 1;
419 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
421 mtag_t tmp = 0;
423 run_sql(save_mtag, &tmp,
424 "select tag from mtag_map where container = %lld and offset = %d;",
425 container, offset);
427 if (tmp == 0 || tmp == -1ULL)
428 return 0;
429 *tag = tmp;
430 return 1;
433 char *get_static_filter(struct symbol *sym)
435 static char sql_filter[1024];
437 /* This can only happen on buggy code. Return invalid SQL. */
438 if (!sym) {
439 sql_filter[0] = '\0';
440 return sql_filter;
443 if (sym->ctype.modifiers & MOD_STATIC) {
444 snprintf(sql_filter, sizeof(sql_filter),
445 "file = '%s' and function = '%s' and static = '1'",
446 get_base_file(), sym->ident->name);
447 } else {
448 snprintf(sql_filter, sizeof(sql_filter),
449 "function = '%s' and static = '0'", sym->ident->name);
452 return sql_filter;
455 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
457 int *row_count = _row_count;
459 *row_count = 0;
460 if (argc != 1)
461 return 0;
462 *row_count = atoi(argv[0]);
463 return 0;
466 static void mark_call_params_untracked(struct expression *call)
468 struct expression *arg;
469 int i = 0;
471 FOR_EACH_PTR(call->args, arg) {
472 mark_untracked(call, i++, "$", NULL);
473 } END_FOR_EACH_PTR(arg);
476 static void sql_select_return_states_pointer(const char *cols,
477 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
479 char *ptr;
480 int return_count = 0;
482 ptr = get_fnptr_name(call->fn);
483 if (!ptr)
484 return;
486 run_sql(get_row_count, &return_count,
487 "select count(*) from return_states join function_ptr "
488 "where return_states.function == function_ptr.function and "
489 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
490 /* The magic number 100 is just from testing on the kernel. */
491 if (return_count > 100) {
492 mark_call_params_untracked(call);
493 return;
496 run_sql(callback, info,
497 "select %s from return_states join function_ptr where "
498 "return_states.function == function_ptr.function and ptr = '%s' "
499 "and searchable = 1 "
500 "order by function_ptr.file, return_states.file, return_id, type;",
501 cols, ptr);
504 static int is_local_symbol(struct expression *expr)
506 if (expr->type != EXPR_SYMBOL)
507 return 0;
508 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
509 return 0;
510 return 1;
513 void sql_select_return_states(const char *cols, struct expression *call,
514 int (*callback)(void*, int, char**, char**), void *info)
516 int row_count = 0;
518 if (is_fake_call(call))
519 return;
521 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
522 sql_select_return_states_pointer(cols, call, callback, info);
523 return;
526 if (inlinable(call->fn)) {
527 mem_sql(callback, info,
528 "select %s from return_states where call_id = '%lu' order by return_id, type;",
529 cols, (unsigned long)call);
530 return;
533 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
534 get_static_filter(call->fn->symbol));
535 if (row_count > 3000)
536 return;
538 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
539 cols, get_static_filter(call->fn->symbol));
542 #define CALL_IMPLIES 0
543 #define RETURN_IMPLIES 1
545 struct implies_info {
546 int type;
547 struct db_implies_cb_list *cb_list;
548 struct expression *expr;
549 struct symbol *sym;
552 void sql_select_implies(const char *cols, struct implies_info *info,
553 int (*callback)(void*, int, char**, char**))
555 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
556 mem_sql(callback, info,
557 "select %s from return_implies where call_id = '%lu';",
558 cols, (unsigned long)info->expr);
559 return;
562 run_sql(callback, info, "select %s from %s_implies where %s;",
563 cols,
564 info->type == CALL_IMPLIES ? "call" : "return",
565 get_static_filter(info->sym));
568 struct select_caller_info_data {
569 struct stree *final_states;
570 struct timeval start_time;
571 int prev_func_id;
572 int ignore;
573 int results;
576 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
578 static void sql_select_caller_info(struct select_caller_info_data *data,
579 const char *cols, struct symbol *sym)
581 if (__inline_fn) {
582 mem_sql(caller_info_callback, data,
583 "select %s from caller_info where call_id = %lu;",
584 cols, (unsigned long)__inline_fn);
585 return;
588 if (sym->ident->name && is_common_function(sym->ident->name))
589 return;
590 run_sql(caller_info_callback, data,
591 "select %s from common_caller_info where %s order by call_id;",
592 cols, get_static_filter(sym));
593 if (data->results)
594 return;
596 run_sql(caller_info_callback, data,
597 "select %s from caller_info where %s order by call_id;",
598 cols, get_static_filter(sym));
601 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
603 struct def_callback *def_callback = __alloc_def_callback(0);
605 def_callback->hook_type = type;
606 def_callback->callback = callback;
607 add_ptr_list(&select_caller_info_callbacks, def_callback);
611 * These call backs are used when the --info option is turned on to print struct
612 * member information. For example foo->bar could have a state in
613 * smatch_extra.c and also check_user.c.
615 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
617 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
619 member_callback->owner = owner;
620 member_callback->callback = callback;
621 add_ptr_list(&member_callbacks, member_callback);
624 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
626 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
628 callback->callback = fn;
629 add_ptr_list(&returned_state_callbacks, callback);
632 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))
634 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
636 member_callback->owner = owner;
637 member_callback->callback = callback;
638 add_ptr_list(&returned_member_callbacks, member_callback);
641 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
643 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
645 cb->type = type;
646 cb->callback = callback;
647 add_ptr_list(&call_implies_cb_list, cb);
650 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
652 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
654 cb->type = type;
655 cb->callback = callback;
656 add_ptr_list(&return_implies_cb_list, cb);
659 struct return_info {
660 struct expression *static_returns_call;
661 struct symbol *return_type;
662 struct range_list *return_range_list;
665 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
667 struct return_info *ret_info = _ret_info;
668 struct range_list *rl;
669 struct expression *call_expr = ret_info->static_returns_call;
671 if (argc != 1)
672 return 0;
673 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
674 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
675 return 0;
678 struct range_list *db_return_vals(struct expression *expr)
680 struct return_info ret_info = {};
681 char buf[64];
682 struct sm_state *sm;
684 if (is_fake_call(expr))
685 return NULL;
687 snprintf(buf, sizeof(buf), "return %p", expr);
688 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
689 if (sm)
690 return clone_rl(estate_rl(sm->state));
691 ret_info.static_returns_call = expr;
692 ret_info.return_type = get_type(expr);
693 if (!ret_info.return_type)
694 return NULL;
696 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
697 return NULL;
699 ret_info.return_range_list = NULL;
700 if (inlinable(expr->fn)) {
701 mem_sql(db_return_callback, &ret_info,
702 "select distinct return from return_states where call_id = '%lu';",
703 (unsigned long)expr);
704 } else {
705 run_sql(db_return_callback, &ret_info,
706 "select distinct return from return_states where %s;",
707 get_static_filter(expr->fn->symbol));
709 return ret_info.return_range_list;
712 struct range_list *db_return_vals_from_str(const char *fn_name)
714 struct return_info ret_info;
716 ret_info.static_returns_call = NULL;
717 ret_info.return_type = &llong_ctype;
718 ret_info.return_range_list = NULL;
720 run_sql(db_return_callback, &ret_info,
721 "select distinct return from return_states where function = '%s';",
722 fn_name);
723 return ret_info.return_range_list;
726 static void match_call_marker(struct expression *expr)
728 struct symbol *type;
730 type = get_type(expr->fn);
731 if (type && type->type == SYM_PTR)
732 type = get_real_base_type(type);
735 * we just want to record something in the database so that if we have
736 * two calls like: frob(4); frob(some_unkown); then on the receiving
737 * side we know that sometimes frob is called with unknown parameters.
740 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
743 static char *show_offset(int offset)
745 static char buf[64];
747 buf[0] = '\0';
748 if (offset != -1)
749 snprintf(buf, sizeof(buf), "(-%d)", offset);
750 return buf;
753 int is_recursive_member(const char *name)
755 char buf[256];
756 const char *p, *next;
757 int size;
759 p = strchr(name, '>');
760 if (!p)
761 return 0;
762 p++;
763 while (true) {
764 next = strchr(p, '>');
765 if (!next)
766 return 0;
767 next++;
769 size = next - p;
770 if (size >= sizeof(buf))
771 return 0;
772 memcpy(buf, p, size);
773 buf[size] = '\0';
774 if (strstr(next, buf))
775 return 1;
776 p = next;
780 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
781 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
783 struct sm_state *sm;
784 char *name;
785 struct symbol *sym;
786 int len;
787 char printed_name[256];
788 int is_address = 0;
789 struct symbol *type;
791 expr = strip_expr(expr);
792 if (!expr)
793 return;
794 type = get_type(expr);
795 if (type && type_bits(type) < type_bits(&ulong_ctype))
796 return;
798 if (expr->type == EXPR_PREOP && expr->op == '&') {
799 expr = strip_expr(expr->unop);
800 is_address = 1;
803 name = expr_to_var_sym(expr, &sym);
804 if (!name || !sym)
805 goto free;
807 len = strlen(name);
808 FOR_EACH_SM(stree, sm) {
809 if (sm->sym != sym)
810 continue;
811 if (strcmp(name, sm->name) == 0) {
812 if (is_address)
813 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
814 else /* these are already handled. fixme: handle them here */
815 continue;
816 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
817 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
818 } else if (strncmp(name, sm->name, len) == 0) {
819 if (isalnum(sm->name[len]))
820 continue;
821 if (is_address)
822 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
823 else
824 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
825 } else {
826 continue;
828 if (is_recursive_member(printed_name))
829 continue;
830 callback(call, param, printed_name, sm);
831 } END_FOR_EACH_SM(sm);
832 free:
833 free_string(name);
836 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
838 char **container = _container;
839 static char buf[256];
841 snprintf(buf, sizeof(buf), "%s", argv[0]);
842 *container = buf;
843 return 0;
846 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
847 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
849 struct expression *tmp;
850 char *container = NULL;
851 int offset;
852 int holder_offset;
853 char *p;
855 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
856 return;
859 * We can't use the in-mem DB because we have to parse the function
860 * first, then we know if it takes a container, then we know to pass it
861 * the container data.
864 run_sql(&param_used_callback, &container,
865 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
866 get_static_filter(call->fn->symbol), CONTAINER, param);
867 if (!container)
868 return;
870 p = strchr(container, '-');
871 if (!p)
872 return;
873 offset = atoi(p);
874 p = strchr(p, ')');
875 if (!p)
876 return;
877 p++;
879 tmp = get_assigned_expr(expr);
880 if (tmp)
881 expr = tmp;
883 if (expr->type != EXPR_PREOP || expr->op != '&')
884 return;
885 expr = strip_expr(expr->unop);
886 holder_offset = get_member_offset_from_deref(expr);
887 if (-holder_offset != offset)
888 return;
890 expr = strip_expr(expr->deref);
891 if (expr->type == EXPR_PREOP && expr->op == '*')
892 expr = strip_expr(expr->unop);
894 print_struct_members(call, expr, param, holder_offset, stree, callback);
897 static void match_call_info(struct expression *call)
899 struct member_info_callback *cb;
900 struct expression *arg;
901 struct stree *stree;
902 char *name;
903 int i;
905 name = get_fnptr_name(call->fn);
906 if (!name)
907 return;
909 FOR_EACH_PTR(member_callbacks, cb) {
910 stree = get_all_states_stree(cb->owner);
911 i = 0;
912 FOR_EACH_PTR(call->args, arg) {
913 print_struct_members(call, arg, i, -1, stree, cb->callback);
914 print_container_struct_members(call, arg, i, stree, cb->callback);
915 i++;
916 } END_FOR_EACH_PTR(arg);
917 free_stree(&stree);
918 } END_FOR_EACH_PTR(cb);
920 free_string(name);
923 static int get_param(int param, char **name, struct symbol **sym)
925 struct symbol *arg;
926 int i;
928 i = 0;
929 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
931 * this is a temporary hack to work around a bug (I think in sparse?)
932 * 2.6.37-rc1:fs/reiserfs/journal.o
933 * If there is a function definition without parameter name found
934 * after a function implementation then it causes a crash.
935 * int foo() {}
936 * int bar(char *);
938 if (arg->ident->name < (char *)100)
939 continue;
940 if (i == param) {
941 *name = arg->ident->name;
942 *sym = arg;
943 return TRUE;
945 i++;
946 } END_FOR_EACH_PTR(arg);
948 return FALSE;
951 static int function_signature_matches(const char *sig)
953 char *my_sig;
955 my_sig = function_signature();
956 if (!sig || !my_sig)
957 return 1; /* default to matching */
958 if (strcmp(my_sig, sig) == 0)
959 return 1;
960 return 0;
963 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
965 struct select_caller_info_data *data = _data;
966 int func_id;
967 long type;
968 long param;
969 char *key;
970 char *value;
971 char *name = NULL;
972 struct symbol *sym = NULL;
973 struct def_callback *def_callback;
974 struct stree *stree;
975 struct timeval cur_time;
977 data->results = 1;
979 if (argc != 5)
980 return 0;
982 gettimeofday(&cur_time, NULL);
983 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
984 return 0;
986 func_id = atoi(argv[0]);
987 errno = 0;
988 type = strtol(argv[1], NULL, 10);
989 param = strtol(argv[2], NULL, 10);
990 if (errno)
991 return 0;
992 key = argv[3];
993 value = argv[4];
995 if (data->prev_func_id == -1)
996 data->prev_func_id = func_id;
997 if (func_id != data->prev_func_id) {
998 stree = __pop_fake_cur_stree();
999 if (!data->ignore)
1000 merge_stree(&data->final_states, stree);
1001 free_stree(&stree);
1002 __push_fake_cur_stree();
1003 __unnullify_path();
1004 data->prev_func_id = func_id;
1005 data->ignore = 0;
1008 if (data->ignore)
1009 return 0;
1010 if (type == INTERNAL &&
1011 !function_signature_matches(value)) {
1012 data->ignore = 1;
1013 return 0;
1016 if (param >= 0 && !get_param(param, &name, &sym))
1017 return 0;
1019 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1020 if (def_callback->hook_type == type)
1021 def_callback->callback(name, sym, key, value);
1022 } END_FOR_EACH_PTR(def_callback);
1024 return 0;
1027 static struct string_list *ptr_names_done;
1028 static struct string_list *ptr_names;
1030 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1032 insert_string(&ptr_names, alloc_string(argv[0]));
1033 return 0;
1036 static char *get_next_ptr_name(void)
1038 char *ptr;
1040 FOR_EACH_PTR(ptr_names, ptr) {
1041 if (!insert_string(&ptr_names_done, ptr))
1042 continue;
1043 return ptr;
1044 } END_FOR_EACH_PTR(ptr);
1045 return NULL;
1048 static void get_ptr_names(const char *file, const char *name)
1050 char sql_filter[1024];
1051 int before, after;
1053 if (file) {
1054 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1055 file, name);
1056 } else {
1057 snprintf(sql_filter, 1024, "function = '%s';", name);
1060 before = ptr_list_size((struct ptr_list *)ptr_names);
1062 run_sql(get_ptr_name, NULL,
1063 "select distinct ptr from function_ptr where %s",
1064 sql_filter);
1066 after = ptr_list_size((struct ptr_list *)ptr_names);
1067 if (before == after)
1068 return;
1070 while ((name = get_next_ptr_name()))
1071 get_ptr_names(NULL, name);
1074 static void match_data_from_db(struct symbol *sym)
1076 struct select_caller_info_data data = { .prev_func_id = -1 };
1077 struct sm_state *sm;
1078 struct stree *stree;
1079 struct timeval end_time;
1081 if (!sym || !sym->ident)
1082 return;
1084 gettimeofday(&data.start_time, NULL);
1086 __push_fake_cur_stree();
1087 __unnullify_path();
1089 if (!__inline_fn) {
1090 char *ptr;
1092 if (sym->ctype.modifiers & MOD_STATIC)
1093 get_ptr_names(get_base_file(), sym->ident->name);
1094 else
1095 get_ptr_names(NULL, sym->ident->name);
1097 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1098 __free_ptr_list((struct ptr_list **)&ptr_names);
1099 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1100 stree = __pop_fake_cur_stree();
1101 free_stree(&stree);
1102 return;
1105 sql_select_caller_info(&data,
1106 "call_id, type, parameter, key, value",
1107 sym);
1110 stree = __pop_fake_cur_stree();
1111 if (!data.ignore)
1112 merge_stree(&data.final_states, stree);
1113 free_stree(&stree);
1114 __push_fake_cur_stree();
1115 __unnullify_path();
1116 data.prev_func_id = -1;
1117 data.ignore = 0;
1119 FOR_EACH_PTR(ptr_names, ptr) {
1120 run_sql(caller_info_callback, &data,
1121 "select call_id, type, parameter, key, value"
1122 " from common_caller_info where function = '%s' order by call_id",
1123 ptr);
1124 } END_FOR_EACH_PTR(ptr);
1126 if (data.results) {
1127 FOR_EACH_PTR(ptr_names, ptr) {
1128 free_string(ptr);
1129 } END_FOR_EACH_PTR(ptr);
1130 goto free_ptr_names;
1133 FOR_EACH_PTR(ptr_names, ptr) {
1134 run_sql(caller_info_callback, &data,
1135 "select call_id, type, parameter, key, value"
1136 " from caller_info where function = '%s' order by call_id",
1137 ptr);
1138 free_string(ptr);
1139 } END_FOR_EACH_PTR(ptr);
1141 free_ptr_names:
1142 __free_ptr_list((struct ptr_list **)&ptr_names);
1143 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1144 } else {
1145 sql_select_caller_info(&data,
1146 "call_id, type, parameter, key, value",
1147 sym);
1150 stree = __pop_fake_cur_stree();
1151 if (!data.ignore)
1152 merge_stree(&data.final_states, stree);
1153 free_stree(&stree);
1155 gettimeofday(&end_time, NULL);
1156 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1157 FOR_EACH_SM(data.final_states, sm) {
1158 __set_sm(sm);
1159 } END_FOR_EACH_SM(sm);
1162 free_stree(&data.final_states);
1165 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1167 struct implies_info *info = _info;
1168 struct db_implies_callback *cb;
1169 struct expression *arg = NULL;
1170 int type;
1171 int param;
1173 if (argc != 5)
1174 return 0;
1176 type = atoi(argv[1]);
1177 param = atoi(argv[2]);
1179 FOR_EACH_PTR(info->cb_list, cb) {
1180 if (cb->type != type)
1181 continue;
1182 if (param != -1) {
1183 arg = get_argument_from_call_expr(info->expr->args, param);
1184 if (!arg)
1185 continue;
1187 cb->callback(info->expr, arg, argv[3], argv[4]);
1188 } END_FOR_EACH_PTR(cb);
1190 return 0;
1193 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1195 struct implies_info *info = _info;
1196 struct db_implies_callback *cb;
1197 struct expression *arg;
1198 struct symbol *sym;
1199 char *name;
1200 int type;
1201 int param;
1203 if (argc != 5)
1204 return 0;
1206 type = atoi(argv[1]);
1207 param = atoi(argv[2]);
1209 if (!get_param(param, &name, &sym))
1210 return 0;
1211 arg = symbol_expression(sym);
1212 if (!arg)
1213 return 0;
1215 FOR_EACH_PTR(info->cb_list, cb) {
1216 if (cb->type != type)
1217 continue;
1218 cb->callback(info->expr, arg, argv[3], argv[4]);
1219 } END_FOR_EACH_PTR(cb);
1221 return 0;
1224 static void match_return_implies(struct expression *expr)
1226 struct implies_info info = {
1227 .type = RETURN_IMPLIES,
1228 .cb_list = return_implies_cb_list,
1231 if (expr->fn->type != EXPR_SYMBOL ||
1232 !expr->fn->symbol)
1233 return;
1234 info.expr = expr;
1235 info.sym = expr->fn->symbol;
1236 sql_select_implies("function, type, parameter, key, value", &info,
1237 return_implies_callbacks);
1240 static void match_call_implies(struct symbol *sym)
1242 struct implies_info info = {
1243 .type = CALL_IMPLIES,
1244 .cb_list = call_implies_cb_list,
1247 if (!sym || !sym->ident)
1248 return;
1250 info.sym = sym;
1251 sql_select_implies("function, type, parameter, key, value", &info,
1252 call_implies_callbacks);
1255 static void print_initializer_list(struct expression_list *expr_list,
1256 struct symbol *struct_type)
1258 struct expression *expr;
1259 struct symbol *base_type;
1260 char struct_name[256];
1262 FOR_EACH_PTR(expr_list, expr) {
1263 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1264 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1265 continue;
1267 if (expr->type != EXPR_IDENTIFIER)
1268 continue;
1269 if (!expr->expr_ident)
1270 continue;
1271 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1272 continue;
1273 base_type = get_type(expr->ident_expression);
1274 if (!base_type || base_type->type != SYM_FN)
1275 continue;
1276 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1277 struct_type->ident->name, expr->expr_ident->name);
1278 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1279 struct_name);
1280 } END_FOR_EACH_PTR(expr);
1283 static void global_variable(struct symbol *sym)
1285 struct symbol *struct_type;
1287 if (!sym->ident)
1288 return;
1289 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1290 return;
1291 struct_type = get_base_type(sym);
1292 if (!struct_type)
1293 return;
1294 if (struct_type->type == SYM_ARRAY) {
1295 struct_type = get_base_type(struct_type);
1296 if (!struct_type)
1297 return;
1299 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1300 return;
1301 print_initializer_list(sym->initializer->expr_list, struct_type);
1304 static char *get_return_compare_is_param(struct expression *expr)
1306 char *var;
1307 char buf[256];
1308 int comparison;
1309 int param;
1311 param = get_param_num(expr);
1312 if (param < 0)
1313 return NULL;
1315 var = expr_to_var(expr);
1316 if (!var)
1317 return NULL;
1318 snprintf(buf, sizeof(buf), "%s orig", var);
1319 comparison = get_comparison_strings(var, buf);
1320 free_string(var);
1322 if (!comparison)
1323 return NULL;
1325 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1326 return alloc_sname(buf);
1329 static char *get_return_compare_str(struct expression *expr)
1331 char *compare_str;
1333 compare_str = get_return_compare_is_param(expr);
1334 if (compare_str)
1335 return compare_str;
1337 compare_str = expr_lte_to_param(expr, -1);
1338 if (compare_str)
1339 return compare_str;
1341 return expr_param_comparison(expr, -1);
1344 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1346 struct range_list *rl;
1347 char *return_ranges;
1348 sval_t sval;
1349 char *compare_str;
1350 char *math_str;
1351 char buf[128];
1353 *rl_p = NULL;
1355 if (!expr)
1356 return alloc_sname("");
1358 if (get_implied_value(expr, &sval)) {
1359 sval = sval_cast(cur_func_return_type(), sval);
1360 *rl_p = alloc_rl(sval, sval);
1361 return sval_to_str_or_err_ptr(sval);
1364 compare_str = expr_equal_to_param(expr, -1);
1365 math_str = get_value_in_terms_of_parameter_math(expr);
1367 if (get_implied_rl(expr, &rl)) {
1368 rl = cast_rl(cur_func_return_type(), rl);
1369 return_ranges = show_rl(rl);
1370 } else if (get_imaginary_absolute(expr, &rl)){
1371 rl = cast_rl(cur_func_return_type(), rl);
1372 return alloc_sname(show_rl(rl));
1373 } else {
1374 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1375 return_ranges = show_rl(rl);
1377 *rl_p = rl;
1379 if (compare_str) {
1380 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1381 return alloc_sname(buf);
1383 if (math_str) {
1384 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1385 return alloc_sname(buf);
1387 compare_str = get_return_compare_str(expr);
1388 if (compare_str) {
1389 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1390 return alloc_sname(buf);
1393 return return_ranges;
1396 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1398 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1401 static void call_return_state_hooks_conditional(struct expression *expr)
1403 struct returned_state_callback *cb;
1404 struct range_list *rl;
1405 const char *return_ranges;
1406 int final_pass_orig = final_pass;
1408 __push_fake_cur_stree();
1410 final_pass = 0;
1411 __split_whole_condition(expr->conditional);
1412 final_pass = final_pass_orig;
1414 return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &rl);
1416 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1418 return_id++;
1419 FOR_EACH_PTR(returned_state_callbacks, cb) {
1420 cb->callback(return_id, (char *)return_ranges, expr->cond_true);
1421 } END_FOR_EACH_PTR(cb);
1423 __push_true_states();
1424 __use_false_states();
1426 return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1427 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1429 return_id++;
1430 FOR_EACH_PTR(returned_state_callbacks, cb) {
1431 cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1432 } END_FOR_EACH_PTR(cb);
1434 __merge_true_states();
1435 __free_fake_cur_stree();
1438 static void call_return_state_hooks_compare(struct expression *expr)
1440 struct returned_state_callback *cb;
1441 char *return_ranges;
1442 int final_pass_orig = final_pass;
1443 sval_t sval = { .type = &int_ctype };
1444 sval_t ret;
1446 if (!get_implied_value(expr, &ret))
1447 ret.value = -1;
1449 __push_fake_cur_stree();
1451 final_pass = 0;
1452 __split_whole_condition(expr);
1453 final_pass = final_pass_orig;
1455 if (ret.value != 0) {
1456 return_ranges = alloc_sname("1");
1457 sval.value = 1;
1458 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1460 return_id++;
1461 FOR_EACH_PTR(returned_state_callbacks, cb) {
1462 cb->callback(return_id, return_ranges, expr);
1463 } END_FOR_EACH_PTR(cb);
1466 __push_true_states();
1467 __use_false_states();
1469 if (ret.value != 1) {
1470 return_ranges = alloc_sname("0");
1471 sval.value = 0;
1472 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1474 return_id++;
1475 FOR_EACH_PTR(returned_state_callbacks, cb) {
1476 cb->callback(return_id, return_ranges, expr);
1477 } END_FOR_EACH_PTR(cb);
1480 __merge_true_states();
1481 __free_fake_cur_stree();
1484 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1486 struct sm_state *tmp;
1488 FOR_EACH_PTR(slist, tmp) {
1489 if (strcmp(tmp->state->name, sm->state->name) == 0)
1490 return 1;
1491 } END_FOR_EACH_PTR(tmp);
1493 return 0;
1496 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1498 struct returned_state_callback *cb;
1499 struct range_list *rl;
1500 char *return_ranges;
1501 struct sm_state *tmp;
1502 int ret = 0;
1503 int nr_possible, nr_states;
1504 char *compare_str;
1505 char buf[128];
1506 struct state_list *already_handled = NULL;
1507 sval_t sval;
1509 if (!sm || !sm->merged)
1510 return 0;
1512 if (too_many_possible(sm))
1513 return 0;
1515 /* bail if it gets too complicated */
1516 nr_possible = 0;
1517 FOR_EACH_PTR(sm->possible, tmp) {
1518 if (tmp->merged)
1519 continue;
1520 nr_possible++;
1521 } END_FOR_EACH_PTR(tmp);
1522 nr_states = get_db_state_count();
1523 if (nr_states * nr_possible >= 2000)
1524 return 0;
1526 FOR_EACH_PTR(sm->possible, tmp) {
1527 if (tmp->merged)
1528 continue;
1529 if (ptr_in_list(tmp, already_handled))
1530 continue;
1531 add_ptr_list(&already_handled, tmp);
1533 ret = 1;
1534 __push_fake_cur_stree();
1536 overwrite_states_using_pool(sm, tmp);
1538 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1539 return_ranges = show_rl(rl);
1540 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1541 if (!rl_to_sval(rl, &sval)) {
1542 compare_str = get_return_compare_str(expr);
1543 if (compare_str) {
1544 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1545 return_ranges = alloc_sname(buf);
1549 return_id++;
1550 FOR_EACH_PTR(returned_state_callbacks, cb) {
1551 cb->callback(return_id, return_ranges, expr);
1552 } END_FOR_EACH_PTR(cb);
1554 __free_fake_cur_stree();
1555 } END_FOR_EACH_PTR(tmp);
1557 free_slist(&already_handled);
1559 return ret;
1562 static int call_return_state_hooks_split_possible(struct expression *expr)
1564 struct sm_state *sm;
1566 if (!expr || expr_equal_to_param(expr, -1))
1567 return 0;
1569 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1570 return split_possible_helper(sm, expr);
1573 static bool has_possible_negative(struct sm_state *sm)
1575 struct sm_state *tmp;
1577 FOR_EACH_PTR(sm->possible, tmp) {
1578 if (!estate_rl(tmp->state))
1579 continue;
1580 if (sval_is_negative(estate_min(tmp->state)) &&
1581 sval_is_negative(estate_max(tmp->state)))
1582 return true;
1583 } END_FOR_EACH_PTR(tmp);
1585 return false;
1588 static bool has_possible_zero_null(struct sm_state *sm)
1590 struct sm_state *tmp;
1591 sval_t sval;
1593 FOR_EACH_PTR(sm->possible, tmp) {
1594 if (!estate_get_single_value(tmp->state, &sval))
1595 continue;
1596 if (sval.value == 0)
1597 return true;
1598 } END_FOR_EACH_PTR(tmp);
1600 return false;
1603 static int split_positive_from_negative(struct expression *expr)
1605 struct sm_state *sm;
1606 struct returned_state_callback *cb;
1607 struct range_list *rl;
1608 const char *return_ranges;
1609 struct range_list *ret_rl;
1610 int undo;
1611 bool has_zero;
1613 /* We're going to print the states 3 times */
1614 if (get_db_state_count() > 10000 / 3)
1615 return 0;
1617 if (!get_implied_rl(expr, &rl) || !rl)
1618 return 0;
1619 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1620 return 0;
1621 /* Forget about INT_MAX and larger */
1622 if (rl_max(rl).value <= 0)
1623 return 0;
1624 if (!sval_is_negative(rl_min(rl)))
1625 return 0;
1627 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1628 if (!sm)
1629 return 0;
1630 if (!has_possible_negative(sm))
1631 return 0;
1632 has_zero = has_possible_zero_null(sm);
1634 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1635 return 0;
1637 return_id++;
1638 return_ranges = get_return_ranges_str(expr, &ret_rl);
1639 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1640 FOR_EACH_PTR(returned_state_callbacks, cb) {
1641 cb->callback(return_id, (char *)return_ranges, expr);
1642 } END_FOR_EACH_PTR(cb);
1644 end_assume();
1646 if (has_zero) {
1647 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1649 return_id++;
1650 return_ranges = get_return_ranges_str(expr, &ret_rl);
1651 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1652 FOR_EACH_PTR(returned_state_callbacks, cb) {
1653 cb->callback(return_id, (char *)return_ranges, expr);
1654 } END_FOR_EACH_PTR(cb);
1656 if (undo)
1657 end_assume();
1660 undo = assume(compare_expression(expr, '<', zero_expr()));
1662 return_id++;
1663 return_ranges = get_return_ranges_str(expr, &ret_rl);
1664 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1665 FOR_EACH_PTR(returned_state_callbacks, cb) {
1666 cb->callback(return_id, (char *)return_ranges, expr);
1667 } END_FOR_EACH_PTR(cb);
1669 if (undo)
1670 end_assume();
1672 return 1;
1675 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1677 struct returned_state_callback *cb;
1678 struct range_list *rl;
1679 struct range_list *nonnull_rl;
1680 sval_t null_sval;
1681 struct range_list *null_rl = NULL;
1682 char *return_ranges;
1683 struct sm_state *sm;
1684 struct smatch_state *state;
1685 int nr_states;
1686 int final_pass_orig = final_pass;
1688 if (!expr || expr_equal_to_param(expr, -1))
1689 return 0;
1690 if (expr->type == EXPR_CALL)
1691 return 0;
1693 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1694 if (!sm)
1695 return 0;
1696 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1697 return 0;
1698 state = sm->state;
1699 if (!estate_rl(state))
1700 return 0;
1701 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1702 return 0;
1703 if (!has_possible_zero_null(sm))
1704 return 0;
1706 nr_states = get_db_state_count();
1707 if (option_info && nr_states >= 1500)
1708 return 0;
1710 rl = estate_rl(state);
1712 __push_fake_cur_stree();
1714 final_pass = 0;
1715 __split_whole_condition(expr);
1716 final_pass = final_pass_orig;
1718 nonnull_rl = rl_filter(rl, rl_zero());
1719 return_ranges = show_rl(nonnull_rl);
1720 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1722 return_id++;
1723 FOR_EACH_PTR(returned_state_callbacks, cb) {
1724 cb->callback(return_id, return_ranges, expr);
1725 } END_FOR_EACH_PTR(cb);
1727 __push_true_states();
1728 __use_false_states();
1730 return_ranges = alloc_sname("0");
1731 null_sval = sval_type_val(rl_type(rl), 0);
1732 add_range(&null_rl, null_sval, null_sval);
1733 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1734 return_id++;
1735 FOR_EACH_PTR(returned_state_callbacks, cb) {
1736 cb->callback(return_id, return_ranges, expr);
1737 } END_FOR_EACH_PTR(cb);
1739 __merge_true_states();
1740 __free_fake_cur_stree();
1742 return 1;
1745 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1747 struct sm_state *sm;
1748 struct range_list *rl;
1749 struct range_list *nonzero_rl;
1750 sval_t zero_sval;
1751 struct range_list *zero_rl = NULL;
1752 int nr_states;
1753 struct returned_state_callback *cb;
1754 char *return_ranges;
1755 int final_pass_orig = final_pass;
1757 if (option_project != PROJ_KERNEL)
1758 return 0;
1760 nr_states = get_db_state_count();
1761 if (nr_states > 1500)
1762 return 0;
1764 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1765 if (!sm)
1766 return 0;
1767 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1768 return 0;
1770 rl = estate_rl(sm->state);
1771 if (!rl)
1772 return 0;
1774 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1775 return 0;
1776 if (rl_max(rl).value != 0)
1777 return 0;
1778 if (!has_possible_zero_null(sm))
1779 return 0;
1781 __push_fake_cur_stree();
1783 final_pass = 0;
1784 __split_whole_condition(expr);
1785 final_pass = final_pass_orig;
1787 nonzero_rl = rl_filter(rl, rl_zero());
1788 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1789 return_ranges = show_rl(nonzero_rl);
1790 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1792 return_id++;
1793 FOR_EACH_PTR(returned_state_callbacks, cb) {
1794 cb->callback(return_id, return_ranges, expr);
1795 } END_FOR_EACH_PTR(cb);
1797 __push_true_states();
1798 __use_false_states();
1800 return_ranges = alloc_sname("0");
1801 zero_sval = sval_type_val(rl_type(rl), 0);
1802 add_range(&zero_rl, zero_sval, zero_sval);
1803 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1804 return_id++;
1805 FOR_EACH_PTR(returned_state_callbacks, cb) {
1806 cb->callback(return_id, return_ranges, expr);
1807 } END_FOR_EACH_PTR(cb);
1809 __merge_true_states();
1810 __free_fake_cur_stree();
1812 return 1;
1815 static int is_boolean(struct expression *expr)
1817 struct range_list *rl;
1819 if (!get_implied_rl(expr, &rl))
1820 return 0;
1821 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1822 return 1;
1823 return 0;
1826 static int is_conditional(struct expression *expr)
1828 if (!expr)
1829 return 0;
1830 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1831 return 1;
1832 return 0;
1835 static int splitable_function_call(struct expression *expr)
1837 struct sm_state *sm;
1838 char buf[64];
1840 if (!expr || expr->type != EXPR_CALL)
1841 return 0;
1842 snprintf(buf, sizeof(buf), "return %p", expr);
1843 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1844 return split_possible_helper(sm, expr);
1847 static struct sm_state *find_bool_param(void)
1849 struct stree *start_states;
1850 struct symbol *arg;
1851 struct sm_state *sm, *tmp;
1852 sval_t sval;
1854 start_states = get_start_states();
1856 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1857 if (!arg->ident)
1858 continue;
1859 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1860 if (!sm)
1861 continue;
1862 if (rl_min(estate_rl(sm->state)).value != 0 ||
1863 rl_max(estate_rl(sm->state)).value != 1)
1864 continue;
1865 goto found;
1866 } END_FOR_EACH_PTR_REVERSE(arg);
1868 return NULL;
1870 found:
1872 * Check if it's splitable. If not, then splitting it up is likely not
1873 * useful for the callers.
1875 FOR_EACH_PTR(sm->possible, tmp) {
1876 if (is_merged(tmp))
1877 continue;
1878 if (!estate_get_single_value(tmp->state, &sval))
1879 return NULL;
1880 } END_FOR_EACH_PTR(tmp);
1882 return sm;
1885 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1887 struct returned_state_callback *cb;
1888 struct range_list *ret_rl;
1889 const char *return_ranges;
1890 struct sm_state *tmp;
1891 int ret = 0;
1892 int nr_possible, nr_states;
1893 char *compare_str = NULL;
1894 char buf[128];
1895 struct state_list *already_handled = NULL;
1897 if (!sm || !sm->merged)
1898 return 0;
1900 if (too_many_possible(sm))
1901 return 0;
1903 /* bail if it gets too complicated */
1904 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1905 nr_states = get_db_state_count();
1906 if (nr_states * nr_possible >= 2000)
1907 return 0;
1909 FOR_EACH_PTR(sm->possible, tmp) {
1910 if (tmp->merged)
1911 continue;
1912 if (ptr_in_list(tmp, already_handled))
1913 continue;
1914 add_ptr_list(&already_handled, tmp);
1916 ret = 1;
1917 __push_fake_cur_stree();
1919 overwrite_states_using_pool(sm, tmp);
1921 return_ranges = get_return_ranges_str(expr, &ret_rl);
1922 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1923 compare_str = get_return_compare_str(expr);
1924 if (compare_str) {
1925 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1926 return_ranges = alloc_sname(buf);
1929 return_id++;
1930 FOR_EACH_PTR(returned_state_callbacks, cb) {
1931 cb->callback(return_id, (char *)return_ranges, expr);
1932 } END_FOR_EACH_PTR(cb);
1934 __free_fake_cur_stree();
1935 } END_FOR_EACH_PTR(tmp);
1937 free_slist(&already_handled);
1939 return ret;
1942 static int split_by_bool_param(struct expression *expr)
1944 struct sm_state *start_sm, *sm;
1945 sval_t sval;
1947 start_sm = find_bool_param();
1948 if (!start_sm)
1949 return 0;
1950 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1951 if (!sm || estate_get_single_value(sm->state, &sval))
1952 return 0;
1953 return split_on_bool_sm(sm, expr);
1956 static int split_by_null_nonnull_param(struct expression *expr)
1958 struct symbol *arg;
1959 struct sm_state *sm;
1960 sval_t zero = {
1961 .type = &ulong_ctype,
1964 /* function must only take one pointer */
1965 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1966 return 0;
1967 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1968 if (!arg->ident)
1969 return 0;
1970 if (get_real_base_type(arg)->type != SYM_PTR)
1971 return 0;
1973 if (param_was_set_var_sym(arg->ident->name, arg))
1974 return 0;
1975 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1976 if (!sm)
1977 return 0;
1979 if (!rl_has_sval(estate_rl(sm->state), zero))
1980 return 0;
1982 return split_on_bool_sm(sm, expr);
1985 struct expression *strip_expr_statement(struct expression *expr)
1987 struct expression *orig = expr;
1988 struct statement *stmt, *last_stmt;
1990 if (!expr)
1991 return NULL;
1992 if (expr->type == EXPR_PREOP && expr->op == '(')
1993 expr = expr->unop;
1994 if (expr->type != EXPR_STATEMENT)
1995 return orig;
1996 stmt = expr->statement;
1997 if (!stmt || stmt->type != STMT_COMPOUND)
1998 return orig;
2000 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2001 if (!last_stmt || last_stmt->type == STMT_LABEL)
2002 last_stmt = last_stmt->label_statement;
2003 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2004 return orig;
2005 return strip_expr(last_stmt->expression);
2008 static void call_return_state_hooks(struct expression *expr)
2010 struct returned_state_callback *cb;
2011 struct range_list *ret_rl;
2012 const char *return_ranges;
2013 int nr_states;
2014 sval_t sval;
2016 if (__path_is_null())
2017 return;
2019 expr = strip_expr(expr);
2020 expr = strip_expr_statement(expr);
2022 if (is_impossible_path())
2023 goto vanilla;
2025 if (expr && (expr->type == EXPR_COMPARE ||
2026 !get_implied_value(expr, &sval)) &&
2027 (is_condition(expr) || is_boolean(expr))) {
2028 call_return_state_hooks_compare(expr);
2029 return;
2030 } else if (is_conditional(expr)) {
2031 call_return_state_hooks_conditional(expr);
2032 return;
2033 } else if (call_return_state_hooks_split_possible(expr)) {
2034 return;
2035 } else if (split_positive_from_negative(expr)) {
2036 return;
2037 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2038 return;
2039 } else if (call_return_state_hooks_split_success_fail(expr)) {
2040 return;
2041 } else if (splitable_function_call(expr)) {
2042 return;
2043 } else if (split_by_bool_param(expr)) {
2044 } else if (split_by_null_nonnull_param(expr)) {
2045 return;
2048 vanilla:
2049 return_ranges = get_return_ranges_str(expr, &ret_rl);
2050 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2052 return_id++;
2053 nr_states = get_db_state_count();
2054 if (nr_states >= 10000) {
2055 match_return_info(return_id, (char *)return_ranges, expr);
2056 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2057 return;
2059 FOR_EACH_PTR(returned_state_callbacks, cb) {
2060 cb->callback(return_id, (char *)return_ranges, expr);
2061 } END_FOR_EACH_PTR(cb);
2064 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2066 struct returned_member_callback *cb;
2067 struct stree *stree;
2068 struct sm_state *sm;
2069 struct symbol *type;
2070 char *name;
2071 char member_name[256];
2072 int len;
2074 type = get_type(expr);
2075 if (!type || type->type != SYM_PTR)
2076 return;
2077 name = expr_to_var(expr);
2078 if (!name)
2079 return;
2081 member_name[sizeof(member_name) - 1] = '\0';
2082 strcpy(member_name, "$");
2084 len = strlen(name);
2085 FOR_EACH_PTR(returned_member_callbacks, cb) {
2086 stree = __get_cur_stree();
2087 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2088 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2089 strcpy(member_name, "*$");
2090 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2091 continue;
2093 if (strncmp(sm->name, name, len) != 0)
2094 continue;
2095 if (strncmp(sm->name + len, "->", 2) != 0)
2096 continue;
2097 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2098 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2099 } END_FOR_EACH_SM(sm);
2100 } END_FOR_EACH_PTR(cb);
2102 free_string(name);
2105 static void reset_memdb(struct symbol *sym)
2107 mem_sql(NULL, NULL, "delete from caller_info;");
2108 mem_sql(NULL, NULL, "delete from return_states;");
2109 mem_sql(NULL, NULL, "delete from call_implies;");
2110 mem_sql(NULL, NULL, "delete from return_implies;");
2113 static void match_end_func_info(struct symbol *sym)
2115 if (__path_is_null())
2116 return;
2117 call_return_state_hooks(NULL);
2120 static void match_after_func(struct symbol *sym)
2122 if (!__inline_fn)
2123 reset_memdb(sym);
2126 static void init_memdb(void)
2128 char *err = NULL;
2129 int rc;
2130 const char *schema_files[] = {
2131 "db/db.schema",
2132 "db/caller_info.schema",
2133 "db/common_caller_info.schema",
2134 "db/return_states.schema",
2135 "db/function_type_size.schema",
2136 "db/type_size.schema",
2137 "db/function_type_info.schema",
2138 "db/type_info.schema",
2139 "db/call_implies.schema",
2140 "db/return_implies.schema",
2141 "db/function_ptr.schema",
2142 "db/local_values.schema",
2143 "db/function_type_value.schema",
2144 "db/type_value.schema",
2145 "db/function_type.schema",
2146 "db/data_info.schema",
2147 "db/parameter_name.schema",
2148 "db/constraints.schema",
2149 "db/constraints_required.schema",
2150 "db/fn_ptr_data_link.schema",
2151 "db/fn_data_link.schema",
2152 "db/mtag_about.schema",
2153 "db/mtag_map.schema",
2154 "db/mtag_data.schema",
2155 "db/mtag_alias.schema",
2157 static char buf[4096];
2158 int fd;
2159 int ret;
2160 int i;
2162 rc = sqlite3_open(":memory:", &mem_db);
2163 if (rc != SQLITE_OK) {
2164 sm_ierror("starting In-Memory database.");
2165 return;
2168 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2169 fd = open_schema_file(schema_files[i]);
2170 if (fd < 0)
2171 continue;
2172 ret = read(fd, buf, sizeof(buf));
2173 if (ret < 0) {
2174 sm_ierror("failed to read: %s", schema_files[i]);
2175 continue;
2177 close(fd);
2178 if (ret == sizeof(buf)) {
2179 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2180 schema_files[i], sizeof(buf));
2181 continue;
2183 buf[ret] = '\0';
2184 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2185 if (rc != SQLITE_OK) {
2186 sm_ierror("SQL error #2: %s", err);
2187 sm_ierror("%s", buf);
2192 static void init_cachedb(void)
2194 char *err = NULL;
2195 int rc;
2196 const char *schema_files[] = {
2197 "db/call_implies.schema",
2198 "db/return_implies.schema",
2199 "db/type_info.schema",
2200 "db/mtag_data.schema",
2201 "db/sink_info.schema",
2203 static char buf[4096];
2204 int fd;
2205 int ret;
2206 int i;
2208 rc = sqlite3_open(":memory:", &cache_db);
2209 if (rc != SQLITE_OK) {
2210 sm_ierror("starting In-Memory database.");
2211 return;
2214 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2215 fd = open_schema_file(schema_files[i]);
2216 if (fd < 0)
2217 continue;
2218 ret = read(fd, buf, sizeof(buf));
2219 if (ret < 0) {
2220 sm_ierror("failed to read: %s", schema_files[i]);
2221 continue;
2223 close(fd);
2224 if (ret == sizeof(buf)) {
2225 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2226 schema_files[i], sizeof(buf));
2227 continue;
2229 buf[ret] = '\0';
2230 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2231 if (rc != SQLITE_OK) {
2232 sm_ierror("SQL error #2: %s", err);
2233 sm_ierror("%s", buf);
2238 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2240 static char buf[4096];
2241 char tmp[256];
2242 char *p = buf;
2243 char *table = _table;
2244 int i;
2247 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2248 for (i = 0; i < argc; i++) {
2249 if (i)
2250 p += snprintf(p, 4096 - (p - buf), ", ");
2251 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2252 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2255 p += snprintf(p, 4096 - (p - buf), ");");
2256 if (p - buf > 4096)
2257 return 0;
2259 sm_msg("SQL: %s", buf);
2260 return 0;
2263 static void dump_cache(struct symbol_list *sym_list)
2265 if (!option_info)
2266 return;
2267 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2268 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2269 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2270 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2271 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2274 void open_smatch_db(char *db_file)
2276 int rc;
2278 if (option_no_db)
2279 return;
2281 use_states = malloc(num_checks + 1);
2282 memset(use_states, 0xff, num_checks + 1);
2284 init_memdb();
2285 init_cachedb();
2287 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2288 if (rc != SQLITE_OK) {
2289 option_no_db = 1;
2290 return;
2292 run_sql(NULL, NULL,
2293 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2294 return;
2297 static void register_common_funcs(void)
2299 struct token *token;
2300 char *func;
2301 char filename[256];
2303 if (option_project == PROJ_NONE)
2304 strcpy(filename, "common_functions");
2305 else
2306 snprintf(filename, 256, "%s.common_functions", option_project_str);
2308 token = get_tokens_file(filename);
2309 if (!token)
2310 return;
2311 if (token_type(token) != TOKEN_STREAMBEGIN)
2312 return;
2313 token = token->next;
2314 while (token_type(token) != TOKEN_STREAMEND) {
2315 if (token_type(token) != TOKEN_IDENT)
2316 return;
2317 func = alloc_string(show_ident(token->ident));
2318 add_ptr_list(&common_funcs, func);
2319 token = token->next;
2321 clear_token_alloc();
2324 static char *get_next_string(char **str)
2326 static char string[256];
2327 char *start;
2328 char *p = *str;
2329 int len;
2331 if (*p == '\0')
2332 return NULL;
2333 start = p;
2335 while (*p != '\0' && *p != ' ' && *p != '\n')
2336 p++;
2338 len = p - start;
2339 if (len > 256) {
2340 memcpy(string, start, 255);
2341 string[255] = '\0';
2342 sm_ierror("return_fix: '%s' too long", string);
2343 **str = '\0';
2344 return NULL;
2346 memcpy(string, start, len);
2347 string[len] = '\0';
2348 if (*p != '\0')
2349 p++;
2350 *str = p;
2351 return string;
2354 static void register_return_replacements(void)
2356 char *func, *orig, *new;
2357 char filename[256];
2358 char buf[4096];
2359 int fd, ret, i;
2360 char *p;
2362 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2363 fd = open_schema_file(filename);
2364 if (fd < 0)
2365 return;
2366 ret = read(fd, buf, sizeof(buf));
2367 close(fd);
2368 if (ret < 0)
2369 return;
2370 if (ret == sizeof(buf)) {
2371 sm_ierror("file too large: %s (limit %zd bytes)",
2372 filename, sizeof(buf));
2373 return;
2375 buf[ret] = '\0';
2377 p = buf;
2378 while (*p) {
2379 get_next_string(&p);
2380 replace_count++;
2382 if (replace_count == 0 || replace_count % 3 != 0) {
2383 replace_count = 0;
2384 return;
2386 replace_table = malloc(replace_count * sizeof(char *));
2388 p = buf;
2389 i = 0;
2390 while (*p) {
2391 func = alloc_string(get_next_string(&p));
2392 orig = alloc_string(get_next_string(&p));
2393 new = alloc_string(get_next_string(&p));
2395 replace_table[i++] = func;
2396 replace_table[i++] = orig;
2397 replace_table[i++] = new;
2401 void register_definition_db_callbacks(int id)
2403 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2404 add_hook(&global_variable, BASE_HOOK);
2405 add_hook(&global_variable, DECLARATION_HOOK);
2406 add_split_return_callback(match_return_info);
2407 add_split_return_callback(print_returned_struct_members);
2408 add_hook(&call_return_state_hooks, RETURN_HOOK);
2409 add_hook(&match_end_func_info, END_FUNC_HOOK);
2410 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2412 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2413 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2414 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2416 register_common_funcs();
2417 register_return_replacements();
2419 add_hook(&dump_cache, END_FILE_HOOK);
2422 void register_db_call_marker(int id)
2424 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2427 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2429 struct expression *arg;
2430 char *name = NULL;
2431 char member_name[256];
2433 *sym = NULL;
2435 if (param == -1) {
2436 const char *star = "";
2438 if (expr->type != EXPR_ASSIGNMENT)
2439 return NULL;
2440 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2441 return NULL;
2442 name = expr_to_var_sym(expr->left, sym);
2443 if (!name)
2444 return NULL;
2445 if (key[0] == '*') {
2446 star = "*";
2447 key++;
2449 if (strncmp(key, "$", 1) != 0)
2450 return name;
2451 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2452 free_string(name);
2453 return alloc_string(member_name);
2456 while (expr->type == EXPR_ASSIGNMENT)
2457 expr = strip_expr(expr->right);
2458 if (expr->type != EXPR_CALL)
2459 return NULL;
2461 arg = get_argument_from_call_expr(expr->args, param);
2462 if (!arg)
2463 return NULL;
2465 return get_variable_from_key(arg, key, sym);
2468 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2470 char buf[256];
2471 char *tmp;
2473 if (!arg)
2474 return NULL;
2476 arg = strip_expr(arg);
2478 if (strcmp(key, "$") == 0)
2479 return expr_to_var_sym(arg, sym);
2481 if (strcmp(key, "*$") == 0) {
2482 if (arg->type == EXPR_PREOP && arg->op == '&') {
2483 arg = strip_expr(arg->unop);
2484 return expr_to_var_sym(arg, sym);
2485 } else {
2486 tmp = expr_to_var_sym(arg, sym);
2487 if (!tmp)
2488 return NULL;
2489 snprintf(buf, sizeof(buf), "*%s", tmp);
2490 free_string(tmp);
2491 return alloc_string(buf);
2495 if (arg->type == EXPR_PREOP && arg->op == '&') {
2496 arg = strip_expr(arg->unop);
2497 tmp = expr_to_var_sym(arg, sym);
2498 if (!tmp)
2499 return NULL;
2500 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2501 return alloc_string(buf);
2504 tmp = expr_to_var_sym(arg, sym);
2505 if (!tmp)
2506 return NULL;
2507 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2508 free_string(tmp);
2509 return alloc_string(buf);
2512 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2514 *vsl = NULL;
2516 if (strcmp("$", key) == 0)
2517 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2518 return get_variable_from_key(arg, key, sym);
2521 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2523 int name_len;
2524 static char buf[256];
2526 name_len = strlen(param_name);
2528 if (strcmp(state_name, param_name) == 0) {
2529 return "$";
2530 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2531 strncmp(state_name, param_name, name_len) == 0) {
2532 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2533 return buf;
2534 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2535 return "*$";
2537 return NULL;
2540 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2542 if (!sym || !sym->ident)
2543 return NULL;
2545 return state_name_to_param_name(name, sym->ident->name);
2548 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2550 struct symbol *type;
2551 const char *sym_name;
2552 int name_len;
2553 static char buf[256];
2556 * mtag_name is different from param_name because mtags can be a struct
2557 * instead of a struct pointer. But we want to treat it like a pointer
2558 * because really an mtag is a pointer. Or in other words, if you pass
2559 * a struct foo then you want to talk about foo.bar but with an mtag
2560 * you want to refer to it as foo->bar.
2564 if (!sym || !sym->ident)
2565 return NULL;
2567 type = get_real_base_type(sym);
2568 if (type && type->type == SYM_BASETYPE)
2569 return "*$";
2571 sym_name = sym->ident->name;
2572 name_len = strlen(sym_name);
2574 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2575 strncmp(state_name, sym_name, name_len) == 0) {
2576 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2577 return buf;
2580 return state_name_to_param_name(state_name, sym_name);
2583 const char *get_mtag_name_expr(struct expression *expr)
2585 char *name;
2586 struct symbol *sym;
2587 const char *ret = NULL;
2589 name = expr_to_var_sym(expr, &sym);
2590 if (!name || !sym)
2591 goto free;
2593 ret = get_mtag_name_var_sym(name, sym);
2594 free:
2595 free_string(name);
2596 return ret;
2599 const char *get_param_name(struct sm_state *sm)
2601 return get_param_name_var_sym(sm->name, sm->sym);
2604 char *get_data_info_name(struct expression *expr)
2606 struct symbol *sym;
2607 char *name;
2608 char buf[256];
2609 char *ret = NULL;
2611 expr = strip_expr(expr);
2612 name = get_member_name(expr);
2613 if (name)
2614 return name;
2615 name = expr_to_var_sym(expr, &sym);
2616 if (!name || !sym)
2617 goto free;
2618 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2619 goto free;
2620 if (sym->ctype.modifiers & MOD_STATIC)
2621 snprintf(buf, sizeof(buf), "static %s", name);
2622 else
2623 snprintf(buf, sizeof(buf), "global %s", name);
2624 ret = alloc_sname(buf);
2625 free:
2626 free_string(name);
2627 return ret;