extra: fix function comparisons
[smatch.git] / smatch_db.c
bloba1d725faa21abd926b39b4c323534598b499ebb9
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 if (expr->type == EXPR_PREOP && expr->op == '&') {
795 expr = strip_expr(expr->unop);
796 is_address = 1;
799 type = get_type(expr);
800 if (type && type_bits(type) < type_bits(&ulong_ctype))
801 return;
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 (list_has_string(ptr_names_done, ptr))
1042 continue;
1043 insert_string(&ptr_names_done, ptr);
1044 return ptr;
1045 } END_FOR_EACH_PTR(ptr);
1046 return NULL;
1049 static void get_ptr_names(const char *file, const char *name)
1051 char sql_filter[1024];
1052 int before, after;
1054 if (file) {
1055 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1056 file, name);
1057 } else {
1058 snprintf(sql_filter, 1024, "function = '%s';", name);
1061 before = ptr_list_size((struct ptr_list *)ptr_names);
1063 run_sql(get_ptr_name, NULL,
1064 "select distinct ptr from function_ptr where %s",
1065 sql_filter);
1067 after = ptr_list_size((struct ptr_list *)ptr_names);
1068 if (before == after)
1069 return;
1071 while ((name = get_next_ptr_name()))
1072 get_ptr_names(NULL, name);
1075 static void match_data_from_db(struct symbol *sym)
1077 struct select_caller_info_data data = { .prev_func_id = -1 };
1078 struct sm_state *sm;
1079 struct stree *stree;
1080 struct timeval end_time;
1082 if (!sym || !sym->ident)
1083 return;
1085 gettimeofday(&data.start_time, NULL);
1087 __push_fake_cur_stree();
1088 __unnullify_path();
1090 if (!__inline_fn) {
1091 char *ptr;
1093 if (sym->ctype.modifiers & MOD_STATIC)
1094 get_ptr_names(get_base_file(), sym->ident->name);
1095 else
1096 get_ptr_names(NULL, sym->ident->name);
1098 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1099 __free_ptr_list((struct ptr_list **)&ptr_names);
1100 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1101 stree = __pop_fake_cur_stree();
1102 free_stree(&stree);
1103 return;
1106 sql_select_caller_info(&data,
1107 "call_id, type, parameter, key, value",
1108 sym);
1111 stree = __pop_fake_cur_stree();
1112 if (!data.ignore)
1113 merge_stree(&data.final_states, stree);
1114 free_stree(&stree);
1115 __push_fake_cur_stree();
1116 __unnullify_path();
1117 data.prev_func_id = -1;
1118 data.ignore = 0;
1120 FOR_EACH_PTR(ptr_names, ptr) {
1121 run_sql(caller_info_callback, &data,
1122 "select call_id, type, parameter, key, value"
1123 " from common_caller_info where function = '%s' order by call_id",
1124 ptr);
1125 } END_FOR_EACH_PTR(ptr);
1127 if (data.results) {
1128 FOR_EACH_PTR(ptr_names, ptr) {
1129 free_string(ptr);
1130 } END_FOR_EACH_PTR(ptr);
1131 goto free_ptr_names;
1134 FOR_EACH_PTR(ptr_names, ptr) {
1135 run_sql(caller_info_callback, &data,
1136 "select call_id, type, parameter, key, value"
1137 " from caller_info where function = '%s' order by call_id",
1138 ptr);
1139 free_string(ptr);
1140 } END_FOR_EACH_PTR(ptr);
1142 free_ptr_names:
1143 __free_ptr_list((struct ptr_list **)&ptr_names);
1144 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1145 } else {
1146 sql_select_caller_info(&data,
1147 "call_id, type, parameter, key, value",
1148 sym);
1151 stree = __pop_fake_cur_stree();
1152 if (!data.ignore)
1153 merge_stree(&data.final_states, stree);
1154 free_stree(&stree);
1156 gettimeofday(&end_time, NULL);
1157 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1158 FOR_EACH_SM(data.final_states, sm) {
1159 __set_sm(sm);
1160 } END_FOR_EACH_SM(sm);
1163 free_stree(&data.final_states);
1166 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1168 struct implies_info *info = _info;
1169 struct db_implies_callback *cb;
1170 struct expression *arg = NULL;
1171 int type;
1172 int param;
1174 if (argc != 5)
1175 return 0;
1177 type = atoi(argv[1]);
1178 param = atoi(argv[2]);
1180 FOR_EACH_PTR(info->cb_list, cb) {
1181 if (cb->type != type)
1182 continue;
1183 if (param != -1) {
1184 arg = get_argument_from_call_expr(info->expr->args, param);
1185 if (!arg)
1186 continue;
1188 cb->callback(info->expr, arg, argv[3], argv[4]);
1189 } END_FOR_EACH_PTR(cb);
1191 return 0;
1194 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1196 struct implies_info *info = _info;
1197 struct db_implies_callback *cb;
1198 struct expression *arg;
1199 struct symbol *sym;
1200 char *name;
1201 int type;
1202 int param;
1204 if (argc != 5)
1205 return 0;
1207 type = atoi(argv[1]);
1208 param = atoi(argv[2]);
1210 if (!get_param(param, &name, &sym))
1211 return 0;
1212 arg = symbol_expression(sym);
1213 if (!arg)
1214 return 0;
1216 FOR_EACH_PTR(info->cb_list, cb) {
1217 if (cb->type != type)
1218 continue;
1219 cb->callback(info->expr, arg, argv[3], argv[4]);
1220 } END_FOR_EACH_PTR(cb);
1222 return 0;
1225 static void match_return_implies(struct expression *expr)
1227 struct implies_info info = {
1228 .type = RETURN_IMPLIES,
1229 .cb_list = return_implies_cb_list,
1232 if (expr->fn->type != EXPR_SYMBOL ||
1233 !expr->fn->symbol)
1234 return;
1235 info.expr = expr;
1236 info.sym = expr->fn->symbol;
1237 sql_select_implies("function, type, parameter, key, value", &info,
1238 return_implies_callbacks);
1241 static void match_call_implies(struct symbol *sym)
1243 struct implies_info info = {
1244 .type = CALL_IMPLIES,
1245 .cb_list = call_implies_cb_list,
1248 if (!sym || !sym->ident)
1249 return;
1251 info.sym = sym;
1252 sql_select_implies("function, type, parameter, key, value", &info,
1253 call_implies_callbacks);
1256 static void print_initializer_list(struct expression_list *expr_list,
1257 struct symbol *struct_type)
1259 struct expression *expr;
1260 struct symbol *base_type;
1261 char struct_name[256];
1263 FOR_EACH_PTR(expr_list, expr) {
1264 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1265 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1266 continue;
1268 if (expr->type != EXPR_IDENTIFIER)
1269 continue;
1270 if (!expr->expr_ident)
1271 continue;
1272 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1273 continue;
1274 base_type = get_type(expr->ident_expression);
1275 if (!base_type || base_type->type != SYM_FN)
1276 continue;
1277 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1278 struct_type->ident->name, expr->expr_ident->name);
1279 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1280 struct_name);
1281 } END_FOR_EACH_PTR(expr);
1284 static void global_variable(struct symbol *sym)
1286 struct symbol *struct_type;
1288 if (!sym->ident)
1289 return;
1290 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1291 return;
1292 struct_type = get_base_type(sym);
1293 if (!struct_type)
1294 return;
1295 if (struct_type->type == SYM_ARRAY) {
1296 struct_type = get_base_type(struct_type);
1297 if (!struct_type)
1298 return;
1300 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1301 return;
1302 print_initializer_list(sym->initializer->expr_list, struct_type);
1305 static char *get_return_compare_str(struct expression *expr)
1307 char *compare_str;
1308 char *var;
1309 char buf[256];
1310 int comparison;
1311 int param;
1313 compare_str = expr_lte_to_param(expr, -1);
1314 if (compare_str)
1315 return compare_str;
1316 param = get_param_num(expr);
1317 if (param < 0)
1318 return NULL;
1320 var = expr_to_var(expr);
1321 if (!var)
1322 return NULL;
1323 snprintf(buf, sizeof(buf), "%s orig", var);
1324 comparison = get_comparison_strings(var, buf);
1325 free_string(var);
1327 if (!comparison)
1328 return NULL;
1330 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1331 return alloc_sname(buf);
1334 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1336 struct range_list *rl;
1337 char *return_ranges;
1338 sval_t sval;
1339 char *compare_str;
1340 char *math_str;
1341 char buf[128];
1343 *rl_p = NULL;
1345 if (!expr)
1346 return alloc_sname("");
1348 if (get_implied_value(expr, &sval)) {
1349 sval = sval_cast(cur_func_return_type(), sval);
1350 *rl_p = alloc_rl(sval, sval);
1351 return sval_to_str_or_err_ptr(sval);
1354 compare_str = expr_equal_to_param(expr, -1);
1355 math_str = get_value_in_terms_of_parameter_math(expr);
1357 if (get_implied_rl(expr, &rl)) {
1358 rl = cast_rl(cur_func_return_type(), rl);
1359 return_ranges = show_rl(rl);
1360 } else if (get_imaginary_absolute(expr, &rl)){
1361 rl = cast_rl(cur_func_return_type(), rl);
1362 return alloc_sname(show_rl(rl));
1363 } else {
1364 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1365 return_ranges = show_rl(rl);
1367 *rl_p = rl;
1369 if (compare_str) {
1370 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1371 return alloc_sname(buf);
1373 if (math_str) {
1374 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1375 return alloc_sname(buf);
1377 compare_str = get_return_compare_str(expr);
1378 if (compare_str) {
1379 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1380 return alloc_sname(buf);
1383 return return_ranges;
1386 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1388 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1391 static void call_return_state_hooks_conditional(struct expression *expr)
1393 struct returned_state_callback *cb;
1394 struct range_list *rl;
1395 const char *return_ranges;
1396 int final_pass_orig = final_pass;
1398 __push_fake_cur_stree();
1400 final_pass = 0;
1401 __split_whole_condition(expr->conditional);
1402 final_pass = final_pass_orig;
1404 return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &rl);
1406 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1408 return_id++;
1409 FOR_EACH_PTR(returned_state_callbacks, cb) {
1410 cb->callback(return_id, (char *)return_ranges, expr->cond_true);
1411 } END_FOR_EACH_PTR(cb);
1413 __push_true_states();
1414 __use_false_states();
1416 return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1417 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1419 return_id++;
1420 FOR_EACH_PTR(returned_state_callbacks, cb) {
1421 cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1422 } END_FOR_EACH_PTR(cb);
1424 __merge_true_states();
1425 __free_fake_cur_stree();
1428 static void call_return_state_hooks_compare(struct expression *expr)
1430 struct returned_state_callback *cb;
1431 char *return_ranges;
1432 int final_pass_orig = final_pass;
1433 sval_t sval = { .type = &int_ctype };
1434 sval_t ret;
1436 if (!get_implied_value(expr, &ret))
1437 ret.value = -1;
1439 __push_fake_cur_stree();
1441 final_pass = 0;
1442 __split_whole_condition(expr);
1443 final_pass = final_pass_orig;
1445 if (ret.value != 0) {
1446 return_ranges = alloc_sname("1");
1447 sval.value = 1;
1448 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1450 return_id++;
1451 FOR_EACH_PTR(returned_state_callbacks, cb) {
1452 cb->callback(return_id, return_ranges, expr);
1453 } END_FOR_EACH_PTR(cb);
1456 __push_true_states();
1457 __use_false_states();
1459 if (ret.value != 1) {
1460 return_ranges = alloc_sname("0");
1461 sval.value = 0;
1462 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1464 return_id++;
1465 FOR_EACH_PTR(returned_state_callbacks, cb) {
1466 cb->callback(return_id, return_ranges, expr);
1467 } END_FOR_EACH_PTR(cb);
1470 __merge_true_states();
1471 __free_fake_cur_stree();
1474 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1476 struct sm_state *tmp;
1478 FOR_EACH_PTR(slist, tmp) {
1479 if (strcmp(tmp->state->name, sm->state->name) == 0)
1480 return 1;
1481 } END_FOR_EACH_PTR(tmp);
1483 return 0;
1486 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1488 struct returned_state_callback *cb;
1489 struct range_list *rl;
1490 char *return_ranges;
1491 struct sm_state *tmp;
1492 int ret = 0;
1493 int nr_possible, nr_states;
1494 char *compare_str = NULL;
1495 char buf[128];
1496 struct state_list *already_handled = NULL;
1498 if (!sm || !sm->merged)
1499 return 0;
1501 if (too_many_possible(sm))
1502 return 0;
1504 /* bail if it gets too complicated */
1505 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1506 nr_states = get_db_state_count();
1507 if (nr_states * nr_possible >= 2000)
1508 return 0;
1510 FOR_EACH_PTR(sm->possible, tmp) {
1511 if (tmp->merged)
1512 continue;
1513 if (ptr_in_list(tmp, already_handled))
1514 continue;
1515 add_ptr_list(&already_handled, tmp);
1517 ret = 1;
1518 __push_fake_cur_stree();
1520 overwrite_states_using_pool(sm, tmp);
1522 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1523 return_ranges = show_rl(rl);
1524 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1525 compare_str = get_return_compare_str(expr);
1526 if (compare_str) {
1527 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1528 return_ranges = alloc_sname(buf);
1531 return_id++;
1532 FOR_EACH_PTR(returned_state_callbacks, cb) {
1533 cb->callback(return_id, return_ranges, expr);
1534 } END_FOR_EACH_PTR(cb);
1536 __free_fake_cur_stree();
1537 } END_FOR_EACH_PTR(tmp);
1539 free_slist(&already_handled);
1541 return ret;
1544 static int call_return_state_hooks_split_possible(struct expression *expr)
1546 struct sm_state *sm;
1548 if (!expr || expr_equal_to_param(expr, -1))
1549 return 0;
1551 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1552 return split_possible_helper(sm, expr);
1555 static bool has_possible_negative(struct sm_state *sm)
1557 struct sm_state *tmp;
1559 FOR_EACH_PTR(sm->possible, tmp) {
1560 if (!estate_rl(tmp->state))
1561 continue;
1562 if (sval_is_negative(estate_min(tmp->state)) &&
1563 sval_is_negative(estate_max(tmp->state)))
1564 return true;
1565 } END_FOR_EACH_PTR(tmp);
1567 return false;
1570 static bool has_possible_zero_null(struct sm_state *sm)
1572 struct sm_state *tmp;
1573 sval_t sval;
1575 FOR_EACH_PTR(sm->possible, tmp) {
1576 if (!estate_get_single_value(tmp->state, &sval))
1577 continue;
1578 if (sval.value == 0)
1579 return true;
1580 } END_FOR_EACH_PTR(tmp);
1582 return false;
1585 static int split_positive_from_negative(struct expression *expr)
1587 struct sm_state *sm;
1588 struct returned_state_callback *cb;
1589 struct range_list *rl;
1590 const char *return_ranges;
1591 struct range_list *ret_rl;
1592 int undo;
1593 bool has_zero;
1595 /* We're going to print the states 3 times */
1596 if (get_db_state_count() > 10000 / 3)
1597 return 0;
1599 if (!get_implied_rl(expr, &rl) || !rl)
1600 return 0;
1601 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1602 return 0;
1603 /* Forget about INT_MAX and larger */
1604 if (rl_max(rl).value <= 0)
1605 return 0;
1606 if (!sval_is_negative(rl_min(rl)))
1607 return 0;
1609 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1610 if (!sm)
1611 return 0;
1612 if (!has_possible_negative(sm))
1613 return 0;
1614 has_zero = has_possible_zero_null(sm);
1616 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1617 return 0;
1619 return_id++;
1620 return_ranges = get_return_ranges_str(expr, &ret_rl);
1621 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1622 FOR_EACH_PTR(returned_state_callbacks, cb) {
1623 cb->callback(return_id, (char *)return_ranges, expr);
1624 } END_FOR_EACH_PTR(cb);
1626 end_assume();
1628 if (has_zero) {
1629 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1631 return_id++;
1632 return_ranges = get_return_ranges_str(expr, &ret_rl);
1633 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1634 FOR_EACH_PTR(returned_state_callbacks, cb) {
1635 cb->callback(return_id, (char *)return_ranges, expr);
1636 } END_FOR_EACH_PTR(cb);
1638 if (undo)
1639 end_assume();
1642 undo = assume(compare_expression(expr, '<', zero_expr()));
1644 return_id++;
1645 return_ranges = get_return_ranges_str(expr, &ret_rl);
1646 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1647 FOR_EACH_PTR(returned_state_callbacks, cb) {
1648 cb->callback(return_id, (char *)return_ranges, expr);
1649 } END_FOR_EACH_PTR(cb);
1651 if (undo)
1652 end_assume();
1654 return 1;
1657 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1659 struct returned_state_callback *cb;
1660 struct range_list *rl;
1661 struct range_list *nonnull_rl;
1662 sval_t null_sval;
1663 struct range_list *null_rl = NULL;
1664 char *return_ranges;
1665 struct sm_state *sm;
1666 struct smatch_state *state;
1667 int nr_states;
1668 int final_pass_orig = final_pass;
1670 if (!expr || expr_equal_to_param(expr, -1))
1671 return 0;
1672 if (expr->type == EXPR_CALL)
1673 return 0;
1674 if (!is_pointer(expr))
1675 return 0;
1677 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1678 if (!sm)
1679 return 0;
1680 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1681 return 0;
1682 state = sm->state;
1683 if (!estate_rl(state))
1684 return 0;
1685 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1686 return 0;
1687 if (!has_possible_zero_null(sm))
1688 return 0;
1690 nr_states = get_db_state_count();
1691 if (option_info && nr_states >= 1500)
1692 return 0;
1694 rl = estate_rl(state);
1696 __push_fake_cur_stree();
1698 final_pass = 0;
1699 __split_whole_condition(expr);
1700 final_pass = final_pass_orig;
1702 nonnull_rl = rl_filter(rl, rl_zero());
1703 return_ranges = show_rl(nonnull_rl);
1704 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1706 return_id++;
1707 FOR_EACH_PTR(returned_state_callbacks, cb) {
1708 cb->callback(return_id, return_ranges, expr);
1709 } END_FOR_EACH_PTR(cb);
1711 __push_true_states();
1712 __use_false_states();
1714 return_ranges = alloc_sname("0");
1715 null_sval = sval_type_val(rl_type(rl), 0);
1716 add_range(&null_rl, null_sval, null_sval);
1717 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1718 return_id++;
1719 FOR_EACH_PTR(returned_state_callbacks, cb) {
1720 cb->callback(return_id, return_ranges, expr);
1721 } END_FOR_EACH_PTR(cb);
1723 __merge_true_states();
1724 __free_fake_cur_stree();
1726 return 1;
1729 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1731 struct sm_state *sm;
1732 struct range_list *rl;
1733 struct range_list *nonzero_rl;
1734 sval_t zero_sval;
1735 struct range_list *zero_rl = NULL;
1736 int nr_states;
1737 struct returned_state_callback *cb;
1738 char *return_ranges;
1739 int final_pass_orig = final_pass;
1741 if (option_project != PROJ_KERNEL)
1742 return 0;
1744 nr_states = get_db_state_count();
1745 if (nr_states > 1500)
1746 return 0;
1748 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1749 if (!sm)
1750 return 0;
1751 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1752 return 0;
1754 rl = estate_rl(sm->state);
1755 if (!rl)
1756 return 0;
1758 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1759 return 0;
1760 if (rl_max(rl).value != 0)
1761 return 0;
1762 if (!has_possible_zero_null(sm))
1763 return 0;
1765 __push_fake_cur_stree();
1767 final_pass = 0;
1768 __split_whole_condition(expr);
1769 final_pass = final_pass_orig;
1771 nonzero_rl = rl_filter(rl, rl_zero());
1772 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1773 return_ranges = show_rl(nonzero_rl);
1774 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1776 return_id++;
1777 FOR_EACH_PTR(returned_state_callbacks, cb) {
1778 cb->callback(return_id, return_ranges, expr);
1779 } END_FOR_EACH_PTR(cb);
1781 __push_true_states();
1782 __use_false_states();
1784 return_ranges = alloc_sname("0");
1785 zero_sval = sval_type_val(rl_type(rl), 0);
1786 add_range(&zero_rl, zero_sval, zero_sval);
1787 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1788 return_id++;
1789 FOR_EACH_PTR(returned_state_callbacks, cb) {
1790 cb->callback(return_id, return_ranges, expr);
1791 } END_FOR_EACH_PTR(cb);
1793 __merge_true_states();
1794 __free_fake_cur_stree();
1796 return 1;
1799 static int is_boolean(struct expression *expr)
1801 struct range_list *rl;
1803 if (!get_implied_rl(expr, &rl))
1804 return 0;
1805 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1806 return 1;
1807 return 0;
1810 static int is_conditional(struct expression *expr)
1812 if (!expr)
1813 return 0;
1814 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1815 return 1;
1816 return 0;
1819 static int splitable_function_call(struct expression *expr)
1821 struct sm_state *sm;
1822 char buf[64];
1824 if (!expr || expr->type != EXPR_CALL)
1825 return 0;
1826 snprintf(buf, sizeof(buf), "return %p", expr);
1827 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1828 return split_possible_helper(sm, expr);
1831 static struct sm_state *find_bool_param(void)
1833 struct stree *start_states;
1834 struct symbol *arg;
1835 struct sm_state *sm, *tmp;
1836 sval_t sval;
1838 start_states = get_start_states();
1840 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1841 if (!arg->ident)
1842 continue;
1843 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1844 if (!sm)
1845 continue;
1846 if (rl_min(estate_rl(sm->state)).value != 0 ||
1847 rl_max(estate_rl(sm->state)).value != 1)
1848 continue;
1849 goto found;
1850 } END_FOR_EACH_PTR_REVERSE(arg);
1852 return NULL;
1854 found:
1856 * Check if it's splitable. If not, then splitting it up is likely not
1857 * useful for the callers.
1859 FOR_EACH_PTR(sm->possible, tmp) {
1860 if (is_merged(tmp))
1861 continue;
1862 if (!estate_get_single_value(tmp->state, &sval))
1863 return NULL;
1864 } END_FOR_EACH_PTR(tmp);
1866 return sm;
1869 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1871 struct returned_state_callback *cb;
1872 struct range_list *ret_rl;
1873 const char *return_ranges;
1874 struct sm_state *tmp;
1875 int ret = 0;
1876 int nr_possible, nr_states;
1877 char *compare_str = NULL;
1878 char buf[128];
1879 struct state_list *already_handled = NULL;
1881 if (!sm || !sm->merged)
1882 return 0;
1884 if (too_many_possible(sm))
1885 return 0;
1887 /* bail if it gets too complicated */
1888 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1889 nr_states = get_db_state_count();
1890 if (nr_states * nr_possible >= 2000)
1891 return 0;
1893 FOR_EACH_PTR(sm->possible, tmp) {
1894 if (tmp->merged)
1895 continue;
1896 if (ptr_in_list(tmp, already_handled))
1897 continue;
1898 add_ptr_list(&already_handled, tmp);
1900 ret = 1;
1901 __push_fake_cur_stree();
1903 overwrite_states_using_pool(sm, tmp);
1905 return_ranges = get_return_ranges_str(expr, &ret_rl);
1906 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1907 compare_str = get_return_compare_str(expr);
1908 if (compare_str) {
1909 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1910 return_ranges = alloc_sname(buf);
1913 return_id++;
1914 FOR_EACH_PTR(returned_state_callbacks, cb) {
1915 cb->callback(return_id, (char *)return_ranges, expr);
1916 } END_FOR_EACH_PTR(cb);
1918 __free_fake_cur_stree();
1919 } END_FOR_EACH_PTR(tmp);
1921 free_slist(&already_handled);
1923 return ret;
1926 static int split_by_bool_param(struct expression *expr)
1928 struct sm_state *start_sm, *sm;
1929 sval_t sval;
1931 start_sm = find_bool_param();
1932 if (!start_sm)
1933 return 0;
1934 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1935 if (!sm || estate_get_single_value(sm->state, &sval))
1936 return 0;
1937 return split_on_bool_sm(sm, expr);
1940 static int split_by_null_nonnull_param(struct expression *expr)
1942 struct symbol *arg;
1943 struct sm_state *sm;
1944 sval_t zero = {
1945 .type = &ulong_ctype,
1948 /* function must only take one pointer */
1949 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1950 return 0;
1951 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1952 if (!arg->ident)
1953 return 0;
1954 if (get_real_base_type(arg)->type != SYM_PTR)
1955 return 0;
1957 if (param_was_set_var_sym(arg->ident->name, arg))
1958 return 0;
1959 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1960 if (!sm)
1961 return 0;
1963 if (!rl_has_sval(estate_rl(sm->state), zero))
1964 return 0;
1966 return split_on_bool_sm(sm, expr);
1969 struct expression *strip_expr_statement(struct expression *expr)
1971 struct expression *orig = expr;
1972 struct statement *stmt, *last_stmt;
1974 if (!expr)
1975 return NULL;
1976 if (expr->type == EXPR_PREOP && expr->op == '(')
1977 expr = expr->unop;
1978 if (expr->type != EXPR_STATEMENT)
1979 return orig;
1980 stmt = expr->statement;
1981 if (!stmt || stmt->type != STMT_COMPOUND)
1982 return orig;
1984 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1985 if (!last_stmt || last_stmt->type == STMT_LABEL)
1986 last_stmt = last_stmt->label_statement;
1987 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1988 return orig;
1989 return strip_expr(last_stmt->expression);
1992 static void call_return_state_hooks(struct expression *expr)
1994 struct returned_state_callback *cb;
1995 struct range_list *ret_rl;
1996 const char *return_ranges;
1997 int nr_states;
1998 sval_t sval;
2000 if (__path_is_null())
2001 return;
2003 expr = strip_expr(expr);
2004 expr = strip_expr_statement(expr);
2006 if (is_impossible_path())
2007 goto vanilla;
2009 if (expr && (expr->type == EXPR_COMPARE ||
2010 !get_implied_value(expr, &sval)) &&
2011 (is_condition(expr) || is_boolean(expr))) {
2012 call_return_state_hooks_compare(expr);
2013 return;
2014 } else if (is_conditional(expr)) {
2015 call_return_state_hooks_conditional(expr);
2016 return;
2017 } else if (call_return_state_hooks_split_possible(expr)) {
2018 return;
2019 } else if (call_return_state_hooks_split_null_non_null(expr)) {
2020 return;
2021 } else if (call_return_state_hooks_split_success_fail(expr)) {
2022 return;
2023 } else if (splitable_function_call(expr)) {
2024 return;
2025 } else if (split_positive_from_negative(expr)) {
2026 return;
2027 } else if (split_by_bool_param(expr)) {
2028 } else if (split_by_null_nonnull_param(expr)) {
2029 return;
2032 vanilla:
2033 return_ranges = get_return_ranges_str(expr, &ret_rl);
2034 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2036 return_id++;
2037 nr_states = get_db_state_count();
2038 if (nr_states >= 10000) {
2039 match_return_info(return_id, (char *)return_ranges, expr);
2040 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2041 return;
2043 FOR_EACH_PTR(returned_state_callbacks, cb) {
2044 cb->callback(return_id, (char *)return_ranges, expr);
2045 } END_FOR_EACH_PTR(cb);
2048 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2050 struct returned_member_callback *cb;
2051 struct stree *stree;
2052 struct sm_state *sm;
2053 struct symbol *type;
2054 char *name;
2055 char member_name[256];
2056 int len;
2058 type = get_type(expr);
2059 if (!type || type->type != SYM_PTR)
2060 return;
2061 name = expr_to_var(expr);
2062 if (!name)
2063 return;
2065 member_name[sizeof(member_name) - 1] = '\0';
2066 strcpy(member_name, "$");
2068 len = strlen(name);
2069 FOR_EACH_PTR(returned_member_callbacks, cb) {
2070 stree = __get_cur_stree();
2071 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2072 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2073 strcpy(member_name, "*$");
2074 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2075 continue;
2077 if (strncmp(sm->name, name, len) != 0)
2078 continue;
2079 if (strncmp(sm->name + len, "->", 2) != 0)
2080 continue;
2081 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2082 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2083 } END_FOR_EACH_SM(sm);
2084 } END_FOR_EACH_PTR(cb);
2086 free_string(name);
2089 static void reset_memdb(struct symbol *sym)
2091 mem_sql(NULL, NULL, "delete from caller_info;");
2092 mem_sql(NULL, NULL, "delete from return_states;");
2093 mem_sql(NULL, NULL, "delete from call_implies;");
2094 mem_sql(NULL, NULL, "delete from return_implies;");
2097 static void match_end_func_info(struct symbol *sym)
2099 if (__path_is_null())
2100 return;
2101 call_return_state_hooks(NULL);
2104 static void match_after_func(struct symbol *sym)
2106 if (!__inline_fn)
2107 reset_memdb(sym);
2110 static void init_memdb(void)
2112 char *err = NULL;
2113 int rc;
2114 const char *schema_files[] = {
2115 "db/db.schema",
2116 "db/caller_info.schema",
2117 "db/common_caller_info.schema",
2118 "db/return_states.schema",
2119 "db/function_type_size.schema",
2120 "db/type_size.schema",
2121 "db/function_type_info.schema",
2122 "db/type_info.schema",
2123 "db/call_implies.schema",
2124 "db/return_implies.schema",
2125 "db/function_ptr.schema",
2126 "db/local_values.schema",
2127 "db/function_type_value.schema",
2128 "db/type_value.schema",
2129 "db/function_type.schema",
2130 "db/data_info.schema",
2131 "db/parameter_name.schema",
2132 "db/constraints.schema",
2133 "db/constraints_required.schema",
2134 "db/fn_ptr_data_link.schema",
2135 "db/fn_data_link.schema",
2136 "db/mtag_about.schema",
2137 "db/mtag_map.schema",
2138 "db/mtag_data.schema",
2139 "db/mtag_alias.schema",
2141 static char buf[4096];
2142 int fd;
2143 int ret;
2144 int i;
2146 rc = sqlite3_open(":memory:", &mem_db);
2147 if (rc != SQLITE_OK) {
2148 sm_ierror("starting In-Memory database.");
2149 return;
2152 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2153 fd = open_schema_file(schema_files[i]);
2154 if (fd < 0)
2155 continue;
2156 ret = read(fd, buf, sizeof(buf));
2157 if (ret < 0) {
2158 sm_ierror("failed to read: %s", schema_files[i]);
2159 continue;
2161 close(fd);
2162 if (ret == sizeof(buf)) {
2163 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2164 schema_files[i], sizeof(buf));
2165 continue;
2167 buf[ret] = '\0';
2168 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2169 if (rc != SQLITE_OK) {
2170 sm_ierror("SQL error #2: %s", err);
2171 sm_ierror("%s", buf);
2176 static void init_cachedb(void)
2178 char *err = NULL;
2179 int rc;
2180 const char *schema_files[] = {
2181 "db/call_implies.schema",
2182 "db/return_implies.schema",
2183 "db/type_info.schema",
2184 "db/mtag_data.schema",
2185 "db/sink_info.schema",
2187 static char buf[4096];
2188 int fd;
2189 int ret;
2190 int i;
2192 rc = sqlite3_open(":memory:", &cache_db);
2193 if (rc != SQLITE_OK) {
2194 sm_ierror("starting In-Memory database.");
2195 return;
2198 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2199 fd = open_schema_file(schema_files[i]);
2200 if (fd < 0)
2201 continue;
2202 ret = read(fd, buf, sizeof(buf));
2203 if (ret < 0) {
2204 sm_ierror("failed to read: %s", schema_files[i]);
2205 continue;
2207 close(fd);
2208 if (ret == sizeof(buf)) {
2209 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2210 schema_files[i], sizeof(buf));
2211 continue;
2213 buf[ret] = '\0';
2214 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2215 if (rc != SQLITE_OK) {
2216 sm_ierror("SQL error #2: %s", err);
2217 sm_ierror("%s", buf);
2222 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2224 static char buf[4096];
2225 char tmp[256];
2226 char *p = buf;
2227 char *table = _table;
2228 int i;
2231 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2232 for (i = 0; i < argc; i++) {
2233 if (i)
2234 p += snprintf(p, 4096 - (p - buf), ", ");
2235 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2236 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2239 p += snprintf(p, 4096 - (p - buf), ");");
2240 if (p - buf > 4096)
2241 return 0;
2243 sm_msg("SQL: %s", buf);
2244 return 0;
2247 static void dump_cache(struct symbol_list *sym_list)
2249 if (!option_info)
2250 return;
2251 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2252 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2253 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2254 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2255 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2258 void open_smatch_db(char *db_file)
2260 int rc;
2262 if (option_no_db)
2263 return;
2265 use_states = malloc(num_checks + 1);
2266 memset(use_states, 0xff, num_checks + 1);
2268 init_memdb();
2269 init_cachedb();
2271 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2272 if (rc != SQLITE_OK) {
2273 option_no_db = 1;
2274 return;
2276 run_sql(NULL, NULL,
2277 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2278 return;
2281 static void register_common_funcs(void)
2283 struct token *token;
2284 char *func;
2285 char filename[256];
2287 if (option_project == PROJ_NONE)
2288 strcpy(filename, "common_functions");
2289 else
2290 snprintf(filename, 256, "%s.common_functions", option_project_str);
2292 token = get_tokens_file(filename);
2293 if (!token)
2294 return;
2295 if (token_type(token) != TOKEN_STREAMBEGIN)
2296 return;
2297 token = token->next;
2298 while (token_type(token) != TOKEN_STREAMEND) {
2299 if (token_type(token) != TOKEN_IDENT)
2300 return;
2301 func = alloc_string(show_ident(token->ident));
2302 add_ptr_list(&common_funcs, func);
2303 token = token->next;
2305 clear_token_alloc();
2308 static char *get_next_string(char **str)
2310 static char string[256];
2311 char *start;
2312 char *p = *str;
2313 int len;
2315 if (*p == '\0')
2316 return NULL;
2317 start = p;
2319 while (*p != '\0' && *p != ' ' && *p != '\n')
2320 p++;
2322 len = p - start;
2323 if (len > 256) {
2324 memcpy(string, start, 255);
2325 string[255] = '\0';
2326 sm_ierror("return_fix: '%s' too long", string);
2327 **str = '\0';
2328 return NULL;
2330 memcpy(string, start, len);
2331 string[len] = '\0';
2332 if (*p != '\0')
2333 p++;
2334 *str = p;
2335 return string;
2338 static void register_return_replacements(void)
2340 char *func, *orig, *new;
2341 char filename[256];
2342 char buf[4096];
2343 int fd, ret, i;
2344 char *p;
2346 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2347 fd = open_schema_file(filename);
2348 if (fd < 0)
2349 return;
2350 ret = read(fd, buf, sizeof(buf));
2351 close(fd);
2352 if (ret < 0)
2353 return;
2354 if (ret == sizeof(buf)) {
2355 sm_ierror("file too large: %s (limit %zd bytes)",
2356 filename, sizeof(buf));
2357 return;
2359 buf[ret] = '\0';
2361 p = buf;
2362 while (*p) {
2363 get_next_string(&p);
2364 replace_count++;
2366 if (replace_count == 0 || replace_count % 3 != 0) {
2367 replace_count = 0;
2368 return;
2370 replace_table = malloc(replace_count * sizeof(char *));
2372 p = buf;
2373 i = 0;
2374 while (*p) {
2375 func = alloc_string(get_next_string(&p));
2376 orig = alloc_string(get_next_string(&p));
2377 new = alloc_string(get_next_string(&p));
2379 replace_table[i++] = func;
2380 replace_table[i++] = orig;
2381 replace_table[i++] = new;
2385 void register_definition_db_callbacks(int id)
2387 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2388 add_hook(&global_variable, BASE_HOOK);
2389 add_hook(&global_variable, DECLARATION_HOOK);
2390 add_split_return_callback(match_return_info);
2391 add_split_return_callback(print_returned_struct_members);
2392 add_hook(&call_return_state_hooks, RETURN_HOOK);
2393 add_hook(&match_end_func_info, END_FUNC_HOOK);
2394 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2396 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2397 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2398 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2400 register_common_funcs();
2401 register_return_replacements();
2403 add_hook(&dump_cache, END_FILE_HOOK);
2406 void register_db_call_marker(int id)
2408 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2411 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2413 struct expression *arg;
2414 char *name = NULL;
2415 char member_name[256];
2417 *sym = NULL;
2419 if (param == -1) {
2420 const char *star = "";
2422 if (expr->type != EXPR_ASSIGNMENT)
2423 return NULL;
2424 name = expr_to_var_sym(expr->left, sym);
2425 if (!name)
2426 return NULL;
2427 if (key[0] == '*') {
2428 star = "*";
2429 key++;
2431 if (strncmp(key, "$", 1) != 0)
2432 return name;
2433 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2434 free_string(name);
2435 return alloc_string(member_name);
2438 while (expr->type == EXPR_ASSIGNMENT)
2439 expr = strip_expr(expr->right);
2440 if (expr->type != EXPR_CALL)
2441 return NULL;
2443 arg = get_argument_from_call_expr(expr->args, param);
2444 if (!arg)
2445 return NULL;
2447 return get_variable_from_key(arg, key, sym);
2450 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2452 char buf[256];
2453 char *tmp;
2455 if (!arg)
2456 return NULL;
2458 arg = strip_expr(arg);
2460 if (strcmp(key, "$") == 0)
2461 return expr_to_var_sym(arg, sym);
2463 if (strcmp(key, "*$") == 0) {
2464 if (arg->type == EXPR_PREOP && arg->op == '&') {
2465 arg = strip_expr(arg->unop);
2466 return expr_to_var_sym(arg, sym);
2467 } else {
2468 tmp = expr_to_var_sym(arg, sym);
2469 if (!tmp)
2470 return NULL;
2471 snprintf(buf, sizeof(buf), "*%s", tmp);
2472 free_string(tmp);
2473 return alloc_string(buf);
2477 if (arg->type == EXPR_PREOP && arg->op == '&') {
2478 arg = strip_expr(arg->unop);
2479 tmp = expr_to_var_sym(arg, sym);
2480 if (!tmp)
2481 return NULL;
2482 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2483 return alloc_string(buf);
2486 tmp = expr_to_var_sym(arg, sym);
2487 if (!tmp)
2488 return NULL;
2489 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2490 free_string(tmp);
2491 return alloc_string(buf);
2494 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2496 *vsl = NULL;
2498 if (strcmp("$", key) == 0)
2499 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2500 return get_variable_from_key(arg, key, sym);
2503 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2505 int name_len;
2506 static char buf[256];
2508 name_len = strlen(param_name);
2510 if (strcmp(state_name, param_name) == 0) {
2511 return "$";
2512 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2513 strncmp(state_name, param_name, name_len) == 0) {
2514 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2515 return buf;
2516 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2517 return "*$";
2519 return NULL;
2522 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2524 if (!sym || !sym->ident)
2525 return NULL;
2527 return state_name_to_param_name(name, sym->ident->name);
2530 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2532 struct symbol *type;
2533 const char *sym_name;
2534 int name_len;
2535 static char buf[256];
2538 * mtag_name is different from param_name because mtags can be a struct
2539 * instead of a struct pointer. But we want to treat it like a pointer
2540 * because really an mtag is a pointer. Or in other words, if you pass
2541 * a struct foo then you want to talk about foo.bar but with an mtag
2542 * you want to refer to it as foo->bar.
2546 if (!sym || !sym->ident)
2547 return NULL;
2549 type = get_real_base_type(sym);
2550 if (type && type->type == SYM_BASETYPE)
2551 return "*$";
2553 sym_name = sym->ident->name;
2554 name_len = strlen(sym_name);
2556 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2557 strncmp(state_name, sym_name, name_len) == 0) {
2558 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2559 return buf;
2562 return state_name_to_param_name(state_name, sym_name);
2565 const char *get_mtag_name_expr(struct expression *expr)
2567 char *name;
2568 struct symbol *sym;
2569 const char *ret = NULL;
2571 name = expr_to_var_sym(expr, &sym);
2572 if (!name || !sym)
2573 goto free;
2575 ret = get_mtag_name_var_sym(name, sym);
2576 free:
2577 free_string(name);
2578 return ret;
2581 const char *get_param_name(struct sm_state *sm)
2583 return get_param_name_var_sym(sm->name, sm->sym);
2586 char *get_data_info_name(struct expression *expr)
2588 struct symbol *sym;
2589 char *name;
2590 char buf[256];
2591 char *ret = NULL;
2593 expr = strip_expr(expr);
2594 name = get_member_name(expr);
2595 if (name)
2596 return name;
2597 name = expr_to_var_sym(expr, &sym);
2598 if (!name || !sym)
2599 goto free;
2600 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2601 goto free;
2602 if (sym->ctype.modifiers & MOD_STATIC)
2603 snprintf(buf, sizeof(buf), "static %s", name);
2604 else
2605 snprintf(buf, sizeof(buf), "global %s", name);
2606 ret = alloc_sname(buf);
2607 free:
2608 free_string(name);
2609 return ret;