testing_index_after_use: silence some false positives
[smatch.git] / smatch_db.c
blob738f30fa1da8fadf4102bb6dccb1ed56a2d97f2e
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_is_param(struct expression *expr)
1307 char *var;
1308 char buf[256];
1309 int comparison;
1310 int param;
1312 param = get_param_num(expr);
1313 if (param < 0)
1314 return NULL;
1316 var = expr_to_var(expr);
1317 if (!var)
1318 return NULL;
1319 snprintf(buf, sizeof(buf), "%s orig", var);
1320 comparison = get_comparison_strings(var, buf);
1321 free_string(var);
1323 if (!comparison)
1324 return NULL;
1326 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1327 return alloc_sname(buf);
1330 static char *get_return_compare_str(struct expression *expr)
1332 char *compare_str;
1334 compare_str = get_return_compare_is_param(expr);
1335 if (compare_str)
1336 return compare_str;
1338 compare_str = expr_lte_to_param(expr, -1);
1339 if (compare_str)
1340 return compare_str;
1342 return expr_param_comparison(expr, -1);
1345 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1347 struct range_list *rl;
1348 char *return_ranges;
1349 sval_t sval;
1350 char *compare_str;
1351 char *math_str;
1352 char buf[128];
1354 *rl_p = NULL;
1356 if (!expr)
1357 return alloc_sname("");
1359 if (get_implied_value(expr, &sval)) {
1360 sval = sval_cast(cur_func_return_type(), sval);
1361 *rl_p = alloc_rl(sval, sval);
1362 return sval_to_str_or_err_ptr(sval);
1365 compare_str = expr_equal_to_param(expr, -1);
1366 math_str = get_value_in_terms_of_parameter_math(expr);
1368 if (get_implied_rl(expr, &rl)) {
1369 rl = cast_rl(cur_func_return_type(), rl);
1370 return_ranges = show_rl(rl);
1371 } else if (get_imaginary_absolute(expr, &rl)){
1372 rl = cast_rl(cur_func_return_type(), rl);
1373 return alloc_sname(show_rl(rl));
1374 } else {
1375 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1376 return_ranges = show_rl(rl);
1378 *rl_p = rl;
1380 if (compare_str) {
1381 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1382 return alloc_sname(buf);
1384 if (math_str) {
1385 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1386 return alloc_sname(buf);
1388 compare_str = get_return_compare_str(expr);
1389 if (compare_str) {
1390 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1391 return alloc_sname(buf);
1394 return return_ranges;
1397 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1399 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1402 static void call_return_state_hooks_conditional(struct expression *expr)
1404 struct returned_state_callback *cb;
1405 struct range_list *rl;
1406 const char *return_ranges;
1407 int final_pass_orig = final_pass;
1409 __push_fake_cur_stree();
1411 final_pass = 0;
1412 __split_whole_condition(expr->conditional);
1413 final_pass = final_pass_orig;
1415 return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &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_true);
1422 } END_FOR_EACH_PTR(cb);
1424 __push_true_states();
1425 __use_false_states();
1427 return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1428 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1430 return_id++;
1431 FOR_EACH_PTR(returned_state_callbacks, cb) {
1432 cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1433 } END_FOR_EACH_PTR(cb);
1435 __merge_true_states();
1436 __free_fake_cur_stree();
1439 static void call_return_state_hooks_compare(struct expression *expr)
1441 struct returned_state_callback *cb;
1442 char *return_ranges;
1443 int final_pass_orig = final_pass;
1444 sval_t sval = { .type = &int_ctype };
1445 sval_t ret;
1447 if (!get_implied_value(expr, &ret))
1448 ret.value = -1;
1450 __push_fake_cur_stree();
1452 final_pass = 0;
1453 __split_whole_condition(expr);
1454 final_pass = final_pass_orig;
1456 if (ret.value != 0) {
1457 return_ranges = alloc_sname("1");
1458 sval.value = 1;
1459 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1461 return_id++;
1462 FOR_EACH_PTR(returned_state_callbacks, cb) {
1463 cb->callback(return_id, return_ranges, expr);
1464 } END_FOR_EACH_PTR(cb);
1467 __push_true_states();
1468 __use_false_states();
1470 if (ret.value != 1) {
1471 return_ranges = alloc_sname("0");
1472 sval.value = 0;
1473 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1475 return_id++;
1476 FOR_EACH_PTR(returned_state_callbacks, cb) {
1477 cb->callback(return_id, return_ranges, expr);
1478 } END_FOR_EACH_PTR(cb);
1481 __merge_true_states();
1482 __free_fake_cur_stree();
1485 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1487 struct sm_state *tmp;
1489 FOR_EACH_PTR(slist, tmp) {
1490 if (strcmp(tmp->state->name, sm->state->name) == 0)
1491 return 1;
1492 } END_FOR_EACH_PTR(tmp);
1494 return 0;
1497 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1499 struct returned_state_callback *cb;
1500 struct range_list *rl;
1501 char *return_ranges;
1502 struct sm_state *tmp;
1503 int ret = 0;
1504 int nr_possible, nr_states;
1505 char *compare_str = NULL;
1506 char buf[128];
1507 struct state_list *already_handled = NULL;
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 = ptr_list_size((struct ptr_list *)sm->possible);
1517 nr_states = get_db_state_count();
1518 if (nr_states * nr_possible >= 2000)
1519 return 0;
1521 FOR_EACH_PTR(sm->possible, tmp) {
1522 if (tmp->merged)
1523 continue;
1524 if (ptr_in_list(tmp, already_handled))
1525 continue;
1526 add_ptr_list(&already_handled, tmp);
1528 ret = 1;
1529 __push_fake_cur_stree();
1531 overwrite_states_using_pool(sm, tmp);
1533 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1534 return_ranges = show_rl(rl);
1535 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1536 compare_str = get_return_compare_str(expr);
1537 if (compare_str) {
1538 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1539 return_ranges = alloc_sname(buf);
1542 return_id++;
1543 FOR_EACH_PTR(returned_state_callbacks, cb) {
1544 cb->callback(return_id, return_ranges, expr);
1545 } END_FOR_EACH_PTR(cb);
1547 __free_fake_cur_stree();
1548 } END_FOR_EACH_PTR(tmp);
1550 free_slist(&already_handled);
1552 return ret;
1555 static int call_return_state_hooks_split_possible(struct expression *expr)
1557 struct sm_state *sm;
1559 if (!expr || expr_equal_to_param(expr, -1))
1560 return 0;
1562 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1563 return split_possible_helper(sm, expr);
1566 static bool has_possible_negative(struct sm_state *sm)
1568 struct sm_state *tmp;
1570 FOR_EACH_PTR(sm->possible, tmp) {
1571 if (!estate_rl(tmp->state))
1572 continue;
1573 if (sval_is_negative(estate_min(tmp->state)) &&
1574 sval_is_negative(estate_max(tmp->state)))
1575 return true;
1576 } END_FOR_EACH_PTR(tmp);
1578 return false;
1581 static bool has_possible_zero_null(struct sm_state *sm)
1583 struct sm_state *tmp;
1584 sval_t sval;
1586 FOR_EACH_PTR(sm->possible, tmp) {
1587 if (!estate_get_single_value(tmp->state, &sval))
1588 continue;
1589 if (sval.value == 0)
1590 return true;
1591 } END_FOR_EACH_PTR(tmp);
1593 return false;
1596 static int split_positive_from_negative(struct expression *expr)
1598 struct sm_state *sm;
1599 struct returned_state_callback *cb;
1600 struct range_list *rl;
1601 const char *return_ranges;
1602 struct range_list *ret_rl;
1603 int undo;
1604 bool has_zero;
1606 /* We're going to print the states 3 times */
1607 if (get_db_state_count() > 10000 / 3)
1608 return 0;
1610 if (!get_implied_rl(expr, &rl) || !rl)
1611 return 0;
1612 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1613 return 0;
1614 /* Forget about INT_MAX and larger */
1615 if (rl_max(rl).value <= 0)
1616 return 0;
1617 if (!sval_is_negative(rl_min(rl)))
1618 return 0;
1620 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1621 if (!sm)
1622 return 0;
1623 if (!has_possible_negative(sm))
1624 return 0;
1625 has_zero = has_possible_zero_null(sm);
1627 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1628 return 0;
1630 return_id++;
1631 return_ranges = get_return_ranges_str(expr, &ret_rl);
1632 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1633 FOR_EACH_PTR(returned_state_callbacks, cb) {
1634 cb->callback(return_id, (char *)return_ranges, expr);
1635 } END_FOR_EACH_PTR(cb);
1637 end_assume();
1639 if (has_zero) {
1640 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1642 return_id++;
1643 return_ranges = get_return_ranges_str(expr, &ret_rl);
1644 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1645 FOR_EACH_PTR(returned_state_callbacks, cb) {
1646 cb->callback(return_id, (char *)return_ranges, expr);
1647 } END_FOR_EACH_PTR(cb);
1649 if (undo)
1650 end_assume();
1653 undo = assume(compare_expression(expr, '<', zero_expr()));
1655 return_id++;
1656 return_ranges = get_return_ranges_str(expr, &ret_rl);
1657 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1658 FOR_EACH_PTR(returned_state_callbacks, cb) {
1659 cb->callback(return_id, (char *)return_ranges, expr);
1660 } END_FOR_EACH_PTR(cb);
1662 if (undo)
1663 end_assume();
1665 return 1;
1668 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1670 struct returned_state_callback *cb;
1671 struct range_list *rl;
1672 struct range_list *nonnull_rl;
1673 sval_t null_sval;
1674 struct range_list *null_rl = NULL;
1675 char *return_ranges;
1676 struct sm_state *sm;
1677 struct smatch_state *state;
1678 int nr_states;
1679 int final_pass_orig = final_pass;
1681 if (!expr || expr_equal_to_param(expr, -1))
1682 return 0;
1683 if (expr->type == EXPR_CALL)
1684 return 0;
1685 if (!is_pointer(expr))
1686 return 0;
1688 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1689 if (!sm)
1690 return 0;
1691 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1692 return 0;
1693 state = sm->state;
1694 if (!estate_rl(state))
1695 return 0;
1696 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1697 return 0;
1698 if (!has_possible_zero_null(sm))
1699 return 0;
1701 nr_states = get_db_state_count();
1702 if (option_info && nr_states >= 1500)
1703 return 0;
1705 rl = estate_rl(state);
1707 __push_fake_cur_stree();
1709 final_pass = 0;
1710 __split_whole_condition(expr);
1711 final_pass = final_pass_orig;
1713 nonnull_rl = rl_filter(rl, rl_zero());
1714 return_ranges = show_rl(nonnull_rl);
1715 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1717 return_id++;
1718 FOR_EACH_PTR(returned_state_callbacks, cb) {
1719 cb->callback(return_id, return_ranges, expr);
1720 } END_FOR_EACH_PTR(cb);
1722 __push_true_states();
1723 __use_false_states();
1725 return_ranges = alloc_sname("0");
1726 null_sval = sval_type_val(rl_type(rl), 0);
1727 add_range(&null_rl, null_sval, null_sval);
1728 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1729 return_id++;
1730 FOR_EACH_PTR(returned_state_callbacks, cb) {
1731 cb->callback(return_id, return_ranges, expr);
1732 } END_FOR_EACH_PTR(cb);
1734 __merge_true_states();
1735 __free_fake_cur_stree();
1737 return 1;
1740 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1742 struct sm_state *sm;
1743 struct range_list *rl;
1744 struct range_list *nonzero_rl;
1745 sval_t zero_sval;
1746 struct range_list *zero_rl = NULL;
1747 int nr_states;
1748 struct returned_state_callback *cb;
1749 char *return_ranges;
1750 int final_pass_orig = final_pass;
1752 if (option_project != PROJ_KERNEL)
1753 return 0;
1755 nr_states = get_db_state_count();
1756 if (nr_states > 1500)
1757 return 0;
1759 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1760 if (!sm)
1761 return 0;
1762 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1763 return 0;
1765 rl = estate_rl(sm->state);
1766 if (!rl)
1767 return 0;
1769 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1770 return 0;
1771 if (rl_max(rl).value != 0)
1772 return 0;
1773 if (!has_possible_zero_null(sm))
1774 return 0;
1776 __push_fake_cur_stree();
1778 final_pass = 0;
1779 __split_whole_condition(expr);
1780 final_pass = final_pass_orig;
1782 nonzero_rl = rl_filter(rl, rl_zero());
1783 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1784 return_ranges = show_rl(nonzero_rl);
1785 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1787 return_id++;
1788 FOR_EACH_PTR(returned_state_callbacks, cb) {
1789 cb->callback(return_id, return_ranges, expr);
1790 } END_FOR_EACH_PTR(cb);
1792 __push_true_states();
1793 __use_false_states();
1795 return_ranges = alloc_sname("0");
1796 zero_sval = sval_type_val(rl_type(rl), 0);
1797 add_range(&zero_rl, zero_sval, zero_sval);
1798 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1799 return_id++;
1800 FOR_EACH_PTR(returned_state_callbacks, cb) {
1801 cb->callback(return_id, return_ranges, expr);
1802 } END_FOR_EACH_PTR(cb);
1804 __merge_true_states();
1805 __free_fake_cur_stree();
1807 return 1;
1810 static int is_boolean(struct expression *expr)
1812 struct range_list *rl;
1814 if (!get_implied_rl(expr, &rl))
1815 return 0;
1816 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1817 return 1;
1818 return 0;
1821 static int is_conditional(struct expression *expr)
1823 if (!expr)
1824 return 0;
1825 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1826 return 1;
1827 return 0;
1830 static int splitable_function_call(struct expression *expr)
1832 struct sm_state *sm;
1833 char buf[64];
1835 if (!expr || expr->type != EXPR_CALL)
1836 return 0;
1837 snprintf(buf, sizeof(buf), "return %p", expr);
1838 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1839 return split_possible_helper(sm, expr);
1842 static struct sm_state *find_bool_param(void)
1844 struct stree *start_states;
1845 struct symbol *arg;
1846 struct sm_state *sm, *tmp;
1847 sval_t sval;
1849 start_states = get_start_states();
1851 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1852 if (!arg->ident)
1853 continue;
1854 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1855 if (!sm)
1856 continue;
1857 if (rl_min(estate_rl(sm->state)).value != 0 ||
1858 rl_max(estate_rl(sm->state)).value != 1)
1859 continue;
1860 goto found;
1861 } END_FOR_EACH_PTR_REVERSE(arg);
1863 return NULL;
1865 found:
1867 * Check if it's splitable. If not, then splitting it up is likely not
1868 * useful for the callers.
1870 FOR_EACH_PTR(sm->possible, tmp) {
1871 if (is_merged(tmp))
1872 continue;
1873 if (!estate_get_single_value(tmp->state, &sval))
1874 return NULL;
1875 } END_FOR_EACH_PTR(tmp);
1877 return sm;
1880 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1882 struct returned_state_callback *cb;
1883 struct range_list *ret_rl;
1884 const char *return_ranges;
1885 struct sm_state *tmp;
1886 int ret = 0;
1887 int nr_possible, nr_states;
1888 char *compare_str = NULL;
1889 char buf[128];
1890 struct state_list *already_handled = NULL;
1892 if (!sm || !sm->merged)
1893 return 0;
1895 if (too_many_possible(sm))
1896 return 0;
1898 /* bail if it gets too complicated */
1899 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1900 nr_states = get_db_state_count();
1901 if (nr_states * nr_possible >= 2000)
1902 return 0;
1904 FOR_EACH_PTR(sm->possible, tmp) {
1905 if (tmp->merged)
1906 continue;
1907 if (ptr_in_list(tmp, already_handled))
1908 continue;
1909 add_ptr_list(&already_handled, tmp);
1911 ret = 1;
1912 __push_fake_cur_stree();
1914 overwrite_states_using_pool(sm, tmp);
1916 return_ranges = get_return_ranges_str(expr, &ret_rl);
1917 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1918 compare_str = get_return_compare_str(expr);
1919 if (compare_str) {
1920 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1921 return_ranges = alloc_sname(buf);
1924 return_id++;
1925 FOR_EACH_PTR(returned_state_callbacks, cb) {
1926 cb->callback(return_id, (char *)return_ranges, expr);
1927 } END_FOR_EACH_PTR(cb);
1929 __free_fake_cur_stree();
1930 } END_FOR_EACH_PTR(tmp);
1932 free_slist(&already_handled);
1934 return ret;
1937 static int split_by_bool_param(struct expression *expr)
1939 struct sm_state *start_sm, *sm;
1940 sval_t sval;
1942 start_sm = find_bool_param();
1943 if (!start_sm)
1944 return 0;
1945 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1946 if (!sm || estate_get_single_value(sm->state, &sval))
1947 return 0;
1948 return split_on_bool_sm(sm, expr);
1951 static int split_by_null_nonnull_param(struct expression *expr)
1953 struct symbol *arg;
1954 struct sm_state *sm;
1955 sval_t zero = {
1956 .type = &ulong_ctype,
1959 /* function must only take one pointer */
1960 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1961 return 0;
1962 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1963 if (!arg->ident)
1964 return 0;
1965 if (get_real_base_type(arg)->type != SYM_PTR)
1966 return 0;
1968 if (param_was_set_var_sym(arg->ident->name, arg))
1969 return 0;
1970 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1971 if (!sm)
1972 return 0;
1974 if (!rl_has_sval(estate_rl(sm->state), zero))
1975 return 0;
1977 return split_on_bool_sm(sm, expr);
1980 struct expression *strip_expr_statement(struct expression *expr)
1982 struct expression *orig = expr;
1983 struct statement *stmt, *last_stmt;
1985 if (!expr)
1986 return NULL;
1987 if (expr->type == EXPR_PREOP && expr->op == '(')
1988 expr = expr->unop;
1989 if (expr->type != EXPR_STATEMENT)
1990 return orig;
1991 stmt = expr->statement;
1992 if (!stmt || stmt->type != STMT_COMPOUND)
1993 return orig;
1995 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1996 if (!last_stmt || last_stmt->type == STMT_LABEL)
1997 last_stmt = last_stmt->label_statement;
1998 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1999 return orig;
2000 return strip_expr(last_stmt->expression);
2003 static void call_return_state_hooks(struct expression *expr)
2005 struct returned_state_callback *cb;
2006 struct range_list *ret_rl;
2007 const char *return_ranges;
2008 int nr_states;
2009 sval_t sval;
2011 if (__path_is_null())
2012 return;
2014 expr = strip_expr(expr);
2015 expr = strip_expr_statement(expr);
2017 if (is_impossible_path())
2018 goto vanilla;
2020 if (expr && (expr->type == EXPR_COMPARE ||
2021 !get_implied_value(expr, &sval)) &&
2022 (is_condition(expr) || is_boolean(expr))) {
2023 call_return_state_hooks_compare(expr);
2024 return;
2025 } else if (is_conditional(expr)) {
2026 call_return_state_hooks_conditional(expr);
2027 return;
2028 } else if (call_return_state_hooks_split_possible(expr)) {
2029 return;
2030 } else if (call_return_state_hooks_split_null_non_null(expr)) {
2031 return;
2032 } else if (call_return_state_hooks_split_success_fail(expr)) {
2033 return;
2034 } else if (splitable_function_call(expr)) {
2035 return;
2036 } else if (split_positive_from_negative(expr)) {
2037 return;
2038 } else if (split_by_bool_param(expr)) {
2039 } else if (split_by_null_nonnull_param(expr)) {
2040 return;
2043 vanilla:
2044 return_ranges = get_return_ranges_str(expr, &ret_rl);
2045 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2047 return_id++;
2048 nr_states = get_db_state_count();
2049 if (nr_states >= 10000) {
2050 match_return_info(return_id, (char *)return_ranges, expr);
2051 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2052 return;
2054 FOR_EACH_PTR(returned_state_callbacks, cb) {
2055 cb->callback(return_id, (char *)return_ranges, expr);
2056 } END_FOR_EACH_PTR(cb);
2059 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2061 struct returned_member_callback *cb;
2062 struct stree *stree;
2063 struct sm_state *sm;
2064 struct symbol *type;
2065 char *name;
2066 char member_name[256];
2067 int len;
2069 type = get_type(expr);
2070 if (!type || type->type != SYM_PTR)
2071 return;
2072 name = expr_to_var(expr);
2073 if (!name)
2074 return;
2076 member_name[sizeof(member_name) - 1] = '\0';
2077 strcpy(member_name, "$");
2079 len = strlen(name);
2080 FOR_EACH_PTR(returned_member_callbacks, cb) {
2081 stree = __get_cur_stree();
2082 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2083 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2084 strcpy(member_name, "*$");
2085 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2086 continue;
2088 if (strncmp(sm->name, name, len) != 0)
2089 continue;
2090 if (strncmp(sm->name + len, "->", 2) != 0)
2091 continue;
2092 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2093 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2094 } END_FOR_EACH_SM(sm);
2095 } END_FOR_EACH_PTR(cb);
2097 free_string(name);
2100 static void reset_memdb(struct symbol *sym)
2102 mem_sql(NULL, NULL, "delete from caller_info;");
2103 mem_sql(NULL, NULL, "delete from return_states;");
2104 mem_sql(NULL, NULL, "delete from call_implies;");
2105 mem_sql(NULL, NULL, "delete from return_implies;");
2108 static void match_end_func_info(struct symbol *sym)
2110 if (__path_is_null())
2111 return;
2112 call_return_state_hooks(NULL);
2115 static void match_after_func(struct symbol *sym)
2117 if (!__inline_fn)
2118 reset_memdb(sym);
2121 static void init_memdb(void)
2123 char *err = NULL;
2124 int rc;
2125 const char *schema_files[] = {
2126 "db/db.schema",
2127 "db/caller_info.schema",
2128 "db/common_caller_info.schema",
2129 "db/return_states.schema",
2130 "db/function_type_size.schema",
2131 "db/type_size.schema",
2132 "db/function_type_info.schema",
2133 "db/type_info.schema",
2134 "db/call_implies.schema",
2135 "db/return_implies.schema",
2136 "db/function_ptr.schema",
2137 "db/local_values.schema",
2138 "db/function_type_value.schema",
2139 "db/type_value.schema",
2140 "db/function_type.schema",
2141 "db/data_info.schema",
2142 "db/parameter_name.schema",
2143 "db/constraints.schema",
2144 "db/constraints_required.schema",
2145 "db/fn_ptr_data_link.schema",
2146 "db/fn_data_link.schema",
2147 "db/mtag_about.schema",
2148 "db/mtag_map.schema",
2149 "db/mtag_data.schema",
2150 "db/mtag_alias.schema",
2152 static char buf[4096];
2153 int fd;
2154 int ret;
2155 int i;
2157 rc = sqlite3_open(":memory:", &mem_db);
2158 if (rc != SQLITE_OK) {
2159 sm_ierror("starting In-Memory database.");
2160 return;
2163 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2164 fd = open_schema_file(schema_files[i]);
2165 if (fd < 0)
2166 continue;
2167 ret = read(fd, buf, sizeof(buf));
2168 if (ret < 0) {
2169 sm_ierror("failed to read: %s", schema_files[i]);
2170 continue;
2172 close(fd);
2173 if (ret == sizeof(buf)) {
2174 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2175 schema_files[i], sizeof(buf));
2176 continue;
2178 buf[ret] = '\0';
2179 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2180 if (rc != SQLITE_OK) {
2181 sm_ierror("SQL error #2: %s", err);
2182 sm_ierror("%s", buf);
2187 static void init_cachedb(void)
2189 char *err = NULL;
2190 int rc;
2191 const char *schema_files[] = {
2192 "db/call_implies.schema",
2193 "db/return_implies.schema",
2194 "db/type_info.schema",
2195 "db/mtag_data.schema",
2196 "db/sink_info.schema",
2198 static char buf[4096];
2199 int fd;
2200 int ret;
2201 int i;
2203 rc = sqlite3_open(":memory:", &cache_db);
2204 if (rc != SQLITE_OK) {
2205 sm_ierror("starting In-Memory database.");
2206 return;
2209 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2210 fd = open_schema_file(schema_files[i]);
2211 if (fd < 0)
2212 continue;
2213 ret = read(fd, buf, sizeof(buf));
2214 if (ret < 0) {
2215 sm_ierror("failed to read: %s", schema_files[i]);
2216 continue;
2218 close(fd);
2219 if (ret == sizeof(buf)) {
2220 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2221 schema_files[i], sizeof(buf));
2222 continue;
2224 buf[ret] = '\0';
2225 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2226 if (rc != SQLITE_OK) {
2227 sm_ierror("SQL error #2: %s", err);
2228 sm_ierror("%s", buf);
2233 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2235 static char buf[4096];
2236 char tmp[256];
2237 char *p = buf;
2238 char *table = _table;
2239 int i;
2242 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2243 for (i = 0; i < argc; i++) {
2244 if (i)
2245 p += snprintf(p, 4096 - (p - buf), ", ");
2246 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2247 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2250 p += snprintf(p, 4096 - (p - buf), ");");
2251 if (p - buf > 4096)
2252 return 0;
2254 sm_msg("SQL: %s", buf);
2255 return 0;
2258 static void dump_cache(struct symbol_list *sym_list)
2260 if (!option_info)
2261 return;
2262 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2263 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2264 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2265 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2266 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2269 void open_smatch_db(char *db_file)
2271 int rc;
2273 if (option_no_db)
2274 return;
2276 use_states = malloc(num_checks + 1);
2277 memset(use_states, 0xff, num_checks + 1);
2279 init_memdb();
2280 init_cachedb();
2282 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2283 if (rc != SQLITE_OK) {
2284 option_no_db = 1;
2285 return;
2287 run_sql(NULL, NULL,
2288 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2289 return;
2292 static void register_common_funcs(void)
2294 struct token *token;
2295 char *func;
2296 char filename[256];
2298 if (option_project == PROJ_NONE)
2299 strcpy(filename, "common_functions");
2300 else
2301 snprintf(filename, 256, "%s.common_functions", option_project_str);
2303 token = get_tokens_file(filename);
2304 if (!token)
2305 return;
2306 if (token_type(token) != TOKEN_STREAMBEGIN)
2307 return;
2308 token = token->next;
2309 while (token_type(token) != TOKEN_STREAMEND) {
2310 if (token_type(token) != TOKEN_IDENT)
2311 return;
2312 func = alloc_string(show_ident(token->ident));
2313 add_ptr_list(&common_funcs, func);
2314 token = token->next;
2316 clear_token_alloc();
2319 static char *get_next_string(char **str)
2321 static char string[256];
2322 char *start;
2323 char *p = *str;
2324 int len;
2326 if (*p == '\0')
2327 return NULL;
2328 start = p;
2330 while (*p != '\0' && *p != ' ' && *p != '\n')
2331 p++;
2333 len = p - start;
2334 if (len > 256) {
2335 memcpy(string, start, 255);
2336 string[255] = '\0';
2337 sm_ierror("return_fix: '%s' too long", string);
2338 **str = '\0';
2339 return NULL;
2341 memcpy(string, start, len);
2342 string[len] = '\0';
2343 if (*p != '\0')
2344 p++;
2345 *str = p;
2346 return string;
2349 static void register_return_replacements(void)
2351 char *func, *orig, *new;
2352 char filename[256];
2353 char buf[4096];
2354 int fd, ret, i;
2355 char *p;
2357 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2358 fd = open_schema_file(filename);
2359 if (fd < 0)
2360 return;
2361 ret = read(fd, buf, sizeof(buf));
2362 close(fd);
2363 if (ret < 0)
2364 return;
2365 if (ret == sizeof(buf)) {
2366 sm_ierror("file too large: %s (limit %zd bytes)",
2367 filename, sizeof(buf));
2368 return;
2370 buf[ret] = '\0';
2372 p = buf;
2373 while (*p) {
2374 get_next_string(&p);
2375 replace_count++;
2377 if (replace_count == 0 || replace_count % 3 != 0) {
2378 replace_count = 0;
2379 return;
2381 replace_table = malloc(replace_count * sizeof(char *));
2383 p = buf;
2384 i = 0;
2385 while (*p) {
2386 func = alloc_string(get_next_string(&p));
2387 orig = alloc_string(get_next_string(&p));
2388 new = alloc_string(get_next_string(&p));
2390 replace_table[i++] = func;
2391 replace_table[i++] = orig;
2392 replace_table[i++] = new;
2396 void register_definition_db_callbacks(int id)
2398 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2399 add_hook(&global_variable, BASE_HOOK);
2400 add_hook(&global_variable, DECLARATION_HOOK);
2401 add_split_return_callback(match_return_info);
2402 add_split_return_callback(print_returned_struct_members);
2403 add_hook(&call_return_state_hooks, RETURN_HOOK);
2404 add_hook(&match_end_func_info, END_FUNC_HOOK);
2405 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2407 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2408 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2409 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2411 register_common_funcs();
2412 register_return_replacements();
2414 add_hook(&dump_cache, END_FILE_HOOK);
2417 void register_db_call_marker(int id)
2419 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2422 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2424 struct expression *arg;
2425 char *name = NULL;
2426 char member_name[256];
2428 *sym = NULL;
2430 if (param == -1) {
2431 const char *star = "";
2433 if (expr->type != EXPR_ASSIGNMENT)
2434 return NULL;
2435 name = expr_to_var_sym(expr->left, sym);
2436 if (!name)
2437 return NULL;
2438 if (key[0] == '*') {
2439 star = "*";
2440 key++;
2442 if (strncmp(key, "$", 1) != 0)
2443 return name;
2444 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2445 free_string(name);
2446 return alloc_string(member_name);
2449 while (expr->type == EXPR_ASSIGNMENT)
2450 expr = strip_expr(expr->right);
2451 if (expr->type != EXPR_CALL)
2452 return NULL;
2454 arg = get_argument_from_call_expr(expr->args, param);
2455 if (!arg)
2456 return NULL;
2458 return get_variable_from_key(arg, key, sym);
2461 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2463 char buf[256];
2464 char *tmp;
2466 if (!arg)
2467 return NULL;
2469 arg = strip_expr(arg);
2471 if (strcmp(key, "$") == 0)
2472 return expr_to_var_sym(arg, sym);
2474 if (strcmp(key, "*$") == 0) {
2475 if (arg->type == EXPR_PREOP && arg->op == '&') {
2476 arg = strip_expr(arg->unop);
2477 return expr_to_var_sym(arg, sym);
2478 } else {
2479 tmp = expr_to_var_sym(arg, sym);
2480 if (!tmp)
2481 return NULL;
2482 snprintf(buf, sizeof(buf), "*%s", tmp);
2483 free_string(tmp);
2484 return alloc_string(buf);
2488 if (arg->type == EXPR_PREOP && arg->op == '&') {
2489 arg = strip_expr(arg->unop);
2490 tmp = expr_to_var_sym(arg, sym);
2491 if (!tmp)
2492 return NULL;
2493 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2494 return alloc_string(buf);
2497 tmp = expr_to_var_sym(arg, sym);
2498 if (!tmp)
2499 return NULL;
2500 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2501 free_string(tmp);
2502 return alloc_string(buf);
2505 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2507 *vsl = NULL;
2509 if (strcmp("$", key) == 0)
2510 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2511 return get_variable_from_key(arg, key, sym);
2514 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2516 int name_len;
2517 static char buf[256];
2519 name_len = strlen(param_name);
2521 if (strcmp(state_name, param_name) == 0) {
2522 return "$";
2523 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2524 strncmp(state_name, param_name, name_len) == 0) {
2525 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2526 return buf;
2527 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2528 return "*$";
2530 return NULL;
2533 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2535 if (!sym || !sym->ident)
2536 return NULL;
2538 return state_name_to_param_name(name, sym->ident->name);
2541 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2543 struct symbol *type;
2544 const char *sym_name;
2545 int name_len;
2546 static char buf[256];
2549 * mtag_name is different from param_name because mtags can be a struct
2550 * instead of a struct pointer. But we want to treat it like a pointer
2551 * because really an mtag is a pointer. Or in other words, if you pass
2552 * a struct foo then you want to talk about foo.bar but with an mtag
2553 * you want to refer to it as foo->bar.
2557 if (!sym || !sym->ident)
2558 return NULL;
2560 type = get_real_base_type(sym);
2561 if (type && type->type == SYM_BASETYPE)
2562 return "*$";
2564 sym_name = sym->ident->name;
2565 name_len = strlen(sym_name);
2567 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2568 strncmp(state_name, sym_name, name_len) == 0) {
2569 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2570 return buf;
2573 return state_name_to_param_name(state_name, sym_name);
2576 const char *get_mtag_name_expr(struct expression *expr)
2578 char *name;
2579 struct symbol *sym;
2580 const char *ret = NULL;
2582 name = expr_to_var_sym(expr, &sym);
2583 if (!name || !sym)
2584 goto free;
2586 ret = get_mtag_name_var_sym(name, sym);
2587 free:
2588 free_string(name);
2589 return ret;
2592 const char *get_param_name(struct sm_state *sm)
2594 return get_param_name_var_sym(sm->name, sm->sym);
2597 char *get_data_info_name(struct expression *expr)
2599 struct symbol *sym;
2600 char *name;
2601 char buf[256];
2602 char *ret = NULL;
2604 expr = strip_expr(expr);
2605 name = get_member_name(expr);
2606 if (name)
2607 return name;
2608 name = expr_to_var_sym(expr, &sym);
2609 if (!name || !sym)
2610 goto free;
2611 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2612 goto free;
2613 if (sym->ctype.modifiers & MOD_STATIC)
2614 snprintf(buf, sizeof(buf), "static %s", name);
2615 else
2616 snprintf(buf, sizeof(buf), "global %s", name);
2617 ret = alloc_sname(buf);
2618 free:
2619 free_string(name);
2620 return ret;