scope: fix is_outer_stmt()
[smatch.git] / smatch_db.c
blobcdc0ade71c46d46a0b583fca1e9c505ada17628c
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 char *escape_newlines(char *str)
76 char buf[1024];
77 bool found = false;
78 int i;
80 for (i = 0; i < sizeof(buf); i++) {
81 if (str[i] == '\n') {
82 found = true;
83 buf[i++] = '\\';
84 buf[i] = 'n';
85 continue;
87 buf[i] = str[i];
88 if (!str[i])
89 break;
92 if (!found)
93 return str;
95 if (buf[i] == sizeof(buf))
96 buf[i - 1] = '\0';
97 return alloc_sname(buf);
100 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
102 char *err = NULL;
103 int rc;
105 if (!db)
106 return;
108 rc = sqlite3_exec(db, sql, callback, data, &err);
109 if (rc != SQLITE_OK && !parse_error) {
110 fprintf(stderr, "SQL error #2: %s\n", err);
111 fprintf(stderr, "SQL: '%s'\n", sql);
112 parse_error = 1;
116 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
118 int i;
120 for (i = 0; i < argc; i++) {
121 if (i != 0)
122 printf(", ");
123 sm_printf("%s", argv[i]);
125 sm_printf("\n");
126 return 0;
129 void debug_sql(struct sqlite3 *db, const char *sql)
131 if (!option_debug)
132 return;
133 sm_msg("%s", sql);
134 sql_exec(db, print_sql_output, NULL, sql);
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 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
754 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
756 struct sm_state *sm;
757 char *name;
758 struct symbol *sym;
759 int len;
760 char printed_name[256];
761 int is_address = 0;
763 expr = strip_expr(expr);
764 if (!expr)
765 return;
766 if (expr->type == EXPR_PREOP && expr->op == '&') {
767 expr = strip_expr(expr->unop);
768 is_address = 1;
771 name = expr_to_var_sym(expr, &sym);
772 if (!name || !sym)
773 goto free;
775 len = strlen(name);
776 FOR_EACH_SM(stree, sm) {
777 if (sm->sym != sym)
778 continue;
779 if (strcmp(name, sm->name) == 0) {
780 if (is_address)
781 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
782 else /* these are already handled. fixme: handle them here */
783 continue;
784 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
785 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
786 } else if (strncmp(name, sm->name, len) == 0) {
787 if (isalnum(sm->name[len]))
788 continue;
789 if (is_address)
790 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
791 else
792 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
793 } else {
794 continue;
796 callback(call, param, printed_name, sm);
797 } END_FOR_EACH_SM(sm);
798 free:
799 free_string(name);
802 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
804 char **container = _container;
805 static char buf[256];
807 snprintf(buf, sizeof(buf), "%s", argv[0]);
808 *container = buf;
809 return 0;
812 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
813 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
815 struct expression *tmp;
816 char *container = NULL;
817 int offset;
818 int holder_offset;
819 char *p;
821 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
822 return;
825 * We can't use the in-mem DB because we have to parse the function
826 * first, then we know if it takes a container, then we know to pass it
827 * the container data.
830 run_sql(&param_used_callback, &container,
831 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
832 get_static_filter(call->fn->symbol), CONTAINER, param);
833 if (!container)
834 return;
836 p = strchr(container, '-');
837 if (!p)
838 return;
839 offset = atoi(p);
840 p = strchr(p, ')');
841 if (!p)
842 return;
843 p++;
845 tmp = get_assigned_expr(expr);
846 if (tmp)
847 expr = tmp;
849 if (expr->type != EXPR_PREOP || expr->op != '&')
850 return;
851 expr = strip_expr(expr->unop);
852 holder_offset = get_member_offset_from_deref(expr);
853 if (-holder_offset != offset)
854 return;
856 expr = strip_expr(expr->deref);
857 if (expr->type == EXPR_PREOP && expr->op == '*')
858 expr = strip_expr(expr->unop);
860 print_struct_members(call, expr, param, holder_offset, stree, callback);
863 static void match_call_info(struct expression *call)
865 struct member_info_callback *cb;
866 struct expression *arg;
867 struct stree *stree;
868 char *name;
869 int i;
871 name = get_fnptr_name(call->fn);
872 if (!name)
873 return;
875 FOR_EACH_PTR(member_callbacks, cb) {
876 stree = get_all_states_stree(cb->owner);
877 i = 0;
878 FOR_EACH_PTR(call->args, arg) {
879 print_struct_members(call, arg, i, -1, stree, cb->callback);
880 print_container_struct_members(call, arg, i, stree, cb->callback);
881 i++;
882 } END_FOR_EACH_PTR(arg);
883 free_stree(&stree);
884 } END_FOR_EACH_PTR(cb);
886 free_string(name);
889 static int get_param(int param, char **name, struct symbol **sym)
891 struct symbol *arg;
892 int i;
894 i = 0;
895 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
897 * this is a temporary hack to work around a bug (I think in sparse?)
898 * 2.6.37-rc1:fs/reiserfs/journal.o
899 * If there is a function definition without parameter name found
900 * after a function implementation then it causes a crash.
901 * int foo() {}
902 * int bar(char *);
904 if (arg->ident->name < (char *)100)
905 continue;
906 if (i == param) {
907 *name = arg->ident->name;
908 *sym = arg;
909 return TRUE;
911 i++;
912 } END_FOR_EACH_PTR(arg);
914 return FALSE;
917 static int function_signature_matches(const char *sig)
919 char *my_sig;
921 my_sig = function_signature();
922 if (!sig || !my_sig)
923 return 1; /* default to matching */
924 if (strcmp(my_sig, sig) == 0)
925 return 1;
926 return 0;
929 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
931 struct select_caller_info_data *data = _data;
932 int func_id;
933 long type;
934 long param;
935 char *key;
936 char *value;
937 char *name = NULL;
938 struct symbol *sym = NULL;
939 struct def_callback *def_callback;
940 struct stree *stree;
941 struct timeval cur_time;
943 data->results = 1;
945 if (argc != 5)
946 return 0;
948 gettimeofday(&cur_time, NULL);
949 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
950 return 0;
952 func_id = atoi(argv[0]);
953 errno = 0;
954 type = strtol(argv[1], NULL, 10);
955 param = strtol(argv[2], NULL, 10);
956 if (errno)
957 return 0;
958 key = argv[3];
959 value = argv[4];
961 if (data->prev_func_id == -1)
962 data->prev_func_id = func_id;
963 if (func_id != data->prev_func_id) {
964 stree = __pop_fake_cur_stree();
965 if (!data->ignore)
966 merge_stree(&data->final_states, stree);
967 free_stree(&stree);
968 __push_fake_cur_stree();
969 __unnullify_path();
970 data->prev_func_id = func_id;
971 data->ignore = 0;
974 if (data->ignore)
975 return 0;
976 if (type == INTERNAL &&
977 !function_signature_matches(value)) {
978 data->ignore = 1;
979 return 0;
982 if (param >= 0 && !get_param(param, &name, &sym))
983 return 0;
985 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
986 if (def_callback->hook_type == type)
987 def_callback->callback(name, sym, key, value);
988 } END_FOR_EACH_PTR(def_callback);
990 return 0;
993 static struct string_list *ptr_names_done;
994 static struct string_list *ptr_names;
996 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
998 insert_string(&ptr_names, alloc_string(argv[0]));
999 return 0;
1002 static char *get_next_ptr_name(void)
1004 char *ptr;
1006 FOR_EACH_PTR(ptr_names, ptr) {
1007 if (list_has_string(ptr_names_done, ptr))
1008 continue;
1009 insert_string(&ptr_names_done, ptr);
1010 return ptr;
1011 } END_FOR_EACH_PTR(ptr);
1012 return NULL;
1015 static void get_ptr_names(const char *file, const char *name)
1017 char sql_filter[1024];
1018 int before, after;
1020 if (file) {
1021 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1022 file, name);
1023 } else {
1024 snprintf(sql_filter, 1024, "function = '%s';", name);
1027 before = ptr_list_size((struct ptr_list *)ptr_names);
1029 run_sql(get_ptr_name, NULL,
1030 "select distinct ptr from function_ptr where %s",
1031 sql_filter);
1033 after = ptr_list_size((struct ptr_list *)ptr_names);
1034 if (before == after)
1035 return;
1037 while ((name = get_next_ptr_name()))
1038 get_ptr_names(NULL, name);
1041 static void match_data_from_db(struct symbol *sym)
1043 struct select_caller_info_data data = { .prev_func_id = -1 };
1044 struct sm_state *sm;
1045 struct stree *stree;
1046 struct timeval end_time;
1048 if (!sym || !sym->ident)
1049 return;
1051 gettimeofday(&data.start_time, NULL);
1053 __push_fake_cur_stree();
1054 __unnullify_path();
1056 if (!__inline_fn) {
1057 char *ptr;
1059 if (sym->ctype.modifiers & MOD_STATIC)
1060 get_ptr_names(get_base_file(), sym->ident->name);
1061 else
1062 get_ptr_names(NULL, sym->ident->name);
1064 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1065 __free_ptr_list((struct ptr_list **)&ptr_names);
1066 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1067 stree = __pop_fake_cur_stree();
1068 free_stree(&stree);
1069 return;
1072 sql_select_caller_info(&data,
1073 "call_id, type, parameter, key, value",
1074 sym);
1077 stree = __pop_fake_cur_stree();
1078 if (!data.ignore)
1079 merge_stree(&data.final_states, stree);
1080 free_stree(&stree);
1081 __push_fake_cur_stree();
1082 __unnullify_path();
1083 data.prev_func_id = -1;
1084 data.ignore = 0;
1086 FOR_EACH_PTR(ptr_names, ptr) {
1087 run_sql(caller_info_callback, &data,
1088 "select call_id, type, parameter, key, value"
1089 " from common_caller_info where function = '%s' order by call_id",
1090 ptr);
1091 } END_FOR_EACH_PTR(ptr);
1093 if (data.results) {
1094 FOR_EACH_PTR(ptr_names, ptr) {
1095 free_string(ptr);
1096 } END_FOR_EACH_PTR(ptr);
1097 goto free_ptr_names;
1100 FOR_EACH_PTR(ptr_names, ptr) {
1101 run_sql(caller_info_callback, &data,
1102 "select call_id, type, parameter, key, value"
1103 " from caller_info where function = '%s' order by call_id",
1104 ptr);
1105 free_string(ptr);
1106 } END_FOR_EACH_PTR(ptr);
1108 free_ptr_names:
1109 __free_ptr_list((struct ptr_list **)&ptr_names);
1110 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1111 } else {
1112 sql_select_caller_info(&data,
1113 "call_id, type, parameter, key, value",
1114 sym);
1117 stree = __pop_fake_cur_stree();
1118 if (!data.ignore)
1119 merge_stree(&data.final_states, stree);
1120 free_stree(&stree);
1122 gettimeofday(&end_time, NULL);
1123 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1124 FOR_EACH_SM(data.final_states, sm) {
1125 __set_sm(sm);
1126 } END_FOR_EACH_SM(sm);
1129 free_stree(&data.final_states);
1132 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1134 struct implies_info *info = _info;
1135 struct db_implies_callback *cb;
1136 struct expression *arg = NULL;
1137 int type;
1138 int param;
1140 if (argc != 5)
1141 return 0;
1143 type = atoi(argv[1]);
1144 param = atoi(argv[2]);
1146 FOR_EACH_PTR(info->cb_list, cb) {
1147 if (cb->type != type)
1148 continue;
1149 if (param != -1) {
1150 arg = get_argument_from_call_expr(info->expr->args, param);
1151 if (!arg)
1152 continue;
1154 cb->callback(info->expr, arg, argv[3], argv[4]);
1155 } END_FOR_EACH_PTR(cb);
1157 return 0;
1160 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1162 struct implies_info *info = _info;
1163 struct db_implies_callback *cb;
1164 struct expression *arg;
1165 struct symbol *sym;
1166 char *name;
1167 int type;
1168 int param;
1170 if (argc != 5)
1171 return 0;
1173 type = atoi(argv[1]);
1174 param = atoi(argv[2]);
1176 if (!get_param(param, &name, &sym))
1177 return 0;
1178 arg = symbol_expression(sym);
1179 if (!arg)
1180 return 0;
1182 FOR_EACH_PTR(info->cb_list, cb) {
1183 if (cb->type != type)
1184 continue;
1185 cb->callback(info->expr, arg, argv[3], argv[4]);
1186 } END_FOR_EACH_PTR(cb);
1188 return 0;
1191 static void match_return_implies(struct expression *expr)
1193 struct implies_info info = {
1194 .type = RETURN_IMPLIES,
1195 .cb_list = return_implies_cb_list,
1198 if (expr->fn->type != EXPR_SYMBOL ||
1199 !expr->fn->symbol)
1200 return;
1201 info.expr = expr;
1202 info.sym = expr->fn->symbol;
1203 sql_select_implies("function, type, parameter, key, value", &info,
1204 return_implies_callbacks);
1207 static void match_call_implies(struct symbol *sym)
1209 struct implies_info info = {
1210 .type = CALL_IMPLIES,
1211 .cb_list = call_implies_cb_list,
1214 if (!sym || !sym->ident)
1215 return;
1217 info.sym = sym;
1218 sql_select_implies("function, type, parameter, key, value", &info,
1219 call_implies_callbacks);
1222 static void print_initializer_list(struct expression_list *expr_list,
1223 struct symbol *struct_type)
1225 struct expression *expr;
1226 struct symbol *base_type;
1227 char struct_name[256];
1229 FOR_EACH_PTR(expr_list, expr) {
1230 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1231 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1232 continue;
1234 if (expr->type != EXPR_IDENTIFIER)
1235 continue;
1236 if (!expr->expr_ident)
1237 continue;
1238 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1239 continue;
1240 base_type = get_type(expr->ident_expression);
1241 if (!base_type || base_type->type != SYM_FN)
1242 continue;
1243 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1244 struct_type->ident->name, expr->expr_ident->name);
1245 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1246 struct_name);
1247 } END_FOR_EACH_PTR(expr);
1250 static void global_variable(struct symbol *sym)
1252 struct symbol *struct_type;
1254 if (!sym->ident)
1255 return;
1256 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1257 return;
1258 struct_type = get_base_type(sym);
1259 if (!struct_type)
1260 return;
1261 if (struct_type->type == SYM_ARRAY) {
1262 struct_type = get_base_type(struct_type);
1263 if (!struct_type)
1264 return;
1266 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1267 return;
1268 print_initializer_list(sym->initializer->expr_list, struct_type);
1271 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1273 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1276 static void call_return_state_hooks_conditional(struct expression *expr)
1278 struct returned_state_callback *cb;
1279 struct range_list *rl;
1280 char *return_ranges;
1281 int final_pass_orig = final_pass;
1283 __push_fake_cur_stree();
1285 final_pass = 0;
1286 __split_whole_condition(expr->conditional);
1287 final_pass = final_pass_orig;
1289 if (get_implied_rl(expr->cond_true, &rl))
1290 rl = cast_rl(cur_func_return_type(), rl);
1291 else
1292 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1293 return_ranges = show_rl(rl);
1294 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1296 return_id++;
1297 FOR_EACH_PTR(returned_state_callbacks, cb) {
1298 cb->callback(return_id, return_ranges, expr->cond_true);
1299 } END_FOR_EACH_PTR(cb);
1301 __push_true_states();
1302 __use_false_states();
1304 if (get_implied_rl(expr->cond_false, &rl))
1305 rl = cast_rl(cur_func_return_type(), rl);
1306 else
1307 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1308 return_ranges = show_rl(rl);
1309 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1311 return_id++;
1312 FOR_EACH_PTR(returned_state_callbacks, cb) {
1313 cb->callback(return_id, return_ranges, expr->cond_false);
1314 } END_FOR_EACH_PTR(cb);
1316 __merge_true_states();
1317 __free_fake_cur_stree();
1320 static void call_return_state_hooks_compare(struct expression *expr)
1322 struct returned_state_callback *cb;
1323 char *return_ranges;
1324 int final_pass_orig = final_pass;
1325 sval_t sval = { .type = &int_ctype };
1326 sval_t ret;
1328 if (!get_implied_value(expr, &ret))
1329 ret.value = -1;
1331 __push_fake_cur_stree();
1333 final_pass = 0;
1334 __split_whole_condition(expr);
1335 final_pass = final_pass_orig;
1337 if (ret.value != 0) {
1338 return_ranges = alloc_sname("1");
1339 sval.value = 1;
1340 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1342 return_id++;
1343 FOR_EACH_PTR(returned_state_callbacks, cb) {
1344 cb->callback(return_id, return_ranges, expr);
1345 } END_FOR_EACH_PTR(cb);
1348 __push_true_states();
1349 __use_false_states();
1351 if (ret.value != 1) {
1352 return_ranges = alloc_sname("0");
1353 sval.value = 0;
1354 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1356 return_id++;
1357 FOR_EACH_PTR(returned_state_callbacks, cb) {
1358 cb->callback(return_id, return_ranges, expr);
1359 } END_FOR_EACH_PTR(cb);
1362 __merge_true_states();
1363 __free_fake_cur_stree();
1366 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1368 struct sm_state *tmp;
1370 FOR_EACH_PTR(slist, tmp) {
1371 if (strcmp(tmp->state->name, sm->state->name) == 0)
1372 return 1;
1373 } END_FOR_EACH_PTR(tmp);
1375 return 0;
1378 static char *get_return_compare_str(struct expression *expr)
1380 char *compare_str;
1381 char *var;
1382 char buf[256];
1383 int comparison;
1384 int param;
1386 compare_str = expr_lte_to_param(expr, -1);
1387 if (compare_str)
1388 return compare_str;
1389 param = get_param_num(expr);
1390 if (param < 0)
1391 return NULL;
1393 var = expr_to_var(expr);
1394 if (!var)
1395 return NULL;
1396 snprintf(buf, sizeof(buf), "%s orig", var);
1397 comparison = get_comparison_strings(var, buf);
1398 free_string(var);
1400 if (!comparison)
1401 return NULL;
1403 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1404 return alloc_sname(buf);
1407 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1409 struct returned_state_callback *cb;
1410 struct range_list *rl;
1411 char *return_ranges;
1412 struct sm_state *tmp;
1413 int ret = 0;
1414 int nr_possible, nr_states;
1415 char *compare_str = NULL;
1416 char buf[128];
1417 struct state_list *already_handled = NULL;
1419 if (!sm || !sm->merged)
1420 return 0;
1422 if (too_many_possible(sm))
1423 return 0;
1425 /* bail if it gets too complicated */
1426 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1427 nr_states = get_db_state_count();
1428 if (nr_states * nr_possible >= 2000)
1429 return 0;
1431 FOR_EACH_PTR(sm->possible, tmp) {
1432 if (tmp->merged)
1433 continue;
1434 if (ptr_in_list(tmp, already_handled))
1435 continue;
1436 add_ptr_list(&already_handled, tmp);
1438 ret = 1;
1439 __push_fake_cur_stree();
1441 overwrite_states_using_pool(sm, tmp);
1443 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1444 return_ranges = show_rl(rl);
1445 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1446 compare_str = get_return_compare_str(expr);
1447 if (compare_str) {
1448 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1449 return_ranges = alloc_sname(buf);
1452 return_id++;
1453 FOR_EACH_PTR(returned_state_callbacks, cb) {
1454 cb->callback(return_id, return_ranges, expr);
1455 } END_FOR_EACH_PTR(cb);
1457 __free_fake_cur_stree();
1458 } END_FOR_EACH_PTR(tmp);
1460 free_slist(&already_handled);
1462 return ret;
1465 static int call_return_state_hooks_split_possible(struct expression *expr)
1467 struct sm_state *sm;
1469 if (!expr || expr_equal_to_param(expr, -1))
1470 return 0;
1472 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1473 return split_possible_helper(sm, expr);
1476 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1478 struct range_list *rl;
1479 char *return_ranges;
1480 sval_t sval;
1481 char *compare_str;
1482 char *math_str;
1483 char buf[128];
1485 *rl_p = NULL;
1487 if (!expr)
1488 return alloc_sname("");
1490 if (get_implied_value(expr, &sval)) {
1491 sval = sval_cast(cur_func_return_type(), sval);
1492 *rl_p = alloc_rl(sval, sval);
1493 return sval_to_str(sval);
1496 compare_str = expr_equal_to_param(expr, -1);
1497 math_str = get_value_in_terms_of_parameter_math(expr);
1499 if (get_implied_rl(expr, &rl)) {
1500 rl = cast_rl(cur_func_return_type(), rl);
1501 return_ranges = show_rl(rl);
1502 } else if (get_imaginary_absolute(expr, &rl)){
1503 rl = cast_rl(cur_func_return_type(), rl);
1504 return alloc_sname(show_rl(rl));
1505 } else {
1506 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1507 return_ranges = show_rl(rl);
1509 *rl_p = rl;
1511 if (compare_str) {
1512 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1513 return alloc_sname(buf);
1515 if (math_str) {
1516 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1517 return alloc_sname(buf);
1519 compare_str = get_return_compare_str(expr);
1520 if (compare_str) {
1521 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1522 return alloc_sname(buf);
1525 return return_ranges;
1528 static bool has_possible_negative(struct sm_state *sm)
1530 struct sm_state *tmp;
1532 FOR_EACH_PTR(sm->possible, tmp) {
1533 if (!estate_rl(tmp->state))
1534 continue;
1535 if (sval_is_negative(estate_min(tmp->state)) &&
1536 sval_is_negative(estate_max(tmp->state)))
1537 return true;
1538 } END_FOR_EACH_PTR(tmp);
1540 return false;
1543 static bool has_possible_zero_null(struct sm_state *sm)
1545 struct sm_state *tmp;
1546 sval_t sval;
1548 FOR_EACH_PTR(sm->possible, tmp) {
1549 if (!estate_get_single_value(tmp->state, &sval))
1550 continue;
1551 if (sval.value == 0)
1552 return true;
1553 } END_FOR_EACH_PTR(tmp);
1555 return false;
1558 static int split_positive_from_negative(struct expression *expr)
1560 struct sm_state *sm;
1561 struct returned_state_callback *cb;
1562 struct range_list *rl;
1563 const char *return_ranges;
1564 struct range_list *ret_rl;
1565 int undo;
1567 /* We're going to print the states 3 times */
1568 if (get_db_state_count() > 10000 / 3)
1569 return 0;
1571 if (!get_implied_rl(expr, &rl) || !rl)
1572 return 0;
1573 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1574 return 0;
1575 /* Forget about INT_MAX and larger */
1576 if (rl_max(rl).value <= 0)
1577 return 0;
1578 if (!sval_is_negative(rl_min(rl)))
1579 return 0;
1581 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1582 if (!sm)
1583 return 0;
1584 if (!has_possible_negative(sm))
1585 return 0;
1587 if (!assume(compare_expression(expr, '>', zero_expr())))
1588 return 0;
1590 return_id++;
1591 return_ranges = get_return_ranges_str(expr, &ret_rl);
1592 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1593 FOR_EACH_PTR(returned_state_callbacks, cb) {
1594 cb->callback(return_id, (char *)return_ranges, expr);
1595 } END_FOR_EACH_PTR(cb);
1597 end_assume();
1599 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1600 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1602 return_id++;
1603 return_ranges = get_return_ranges_str(expr, &ret_rl);
1604 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1605 FOR_EACH_PTR(returned_state_callbacks, cb) {
1606 cb->callback(return_id, (char *)return_ranges, expr);
1607 } END_FOR_EACH_PTR(cb);
1609 if (undo)
1610 end_assume();
1613 undo = assume(compare_expression(expr, '<', zero_expr()));
1615 return_id++;
1616 return_ranges = get_return_ranges_str(expr, &ret_rl);
1617 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1618 FOR_EACH_PTR(returned_state_callbacks, cb) {
1619 cb->callback(return_id, (char *)return_ranges, expr);
1620 } END_FOR_EACH_PTR(cb);
1622 if (undo)
1623 end_assume();
1625 return 1;
1628 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1630 struct returned_state_callback *cb;
1631 struct range_list *rl;
1632 struct range_list *nonnull_rl;
1633 sval_t null_sval;
1634 struct range_list *null_rl = NULL;
1635 char *return_ranges;
1636 struct sm_state *sm;
1637 struct smatch_state *state;
1638 int nr_states;
1639 int final_pass_orig = final_pass;
1641 if (!expr || expr_equal_to_param(expr, -1))
1642 return 0;
1643 if (expr->type == EXPR_CALL)
1644 return 0;
1645 if (!is_pointer(expr))
1646 return 0;
1648 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1649 if (!sm)
1650 return 0;
1651 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1652 return 0;
1653 state = sm->state;
1654 if (!estate_rl(state))
1655 return 0;
1656 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1657 return 0;
1658 if (!has_possible_zero_null(sm))
1659 return 0;
1661 nr_states = get_db_state_count();
1662 if (option_info && nr_states >= 1500)
1663 return 0;
1665 rl = estate_rl(state);
1667 __push_fake_cur_stree();
1669 final_pass = 0;
1670 __split_whole_condition(expr);
1671 final_pass = final_pass_orig;
1673 nonnull_rl = rl_filter(rl, rl_zero());
1674 return_ranges = show_rl(nonnull_rl);
1675 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1677 return_id++;
1678 FOR_EACH_PTR(returned_state_callbacks, cb) {
1679 cb->callback(return_id, return_ranges, expr);
1680 } END_FOR_EACH_PTR(cb);
1682 __push_true_states();
1683 __use_false_states();
1685 return_ranges = alloc_sname("0");
1686 null_sval = sval_type_val(rl_type(rl), 0);
1687 add_range(&null_rl, null_sval, null_sval);
1688 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1689 return_id++;
1690 FOR_EACH_PTR(returned_state_callbacks, cb) {
1691 cb->callback(return_id, return_ranges, expr);
1692 } END_FOR_EACH_PTR(cb);
1694 __merge_true_states();
1695 __free_fake_cur_stree();
1697 return 1;
1700 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1702 struct sm_state *sm;
1703 struct range_list *rl;
1704 struct range_list *nonzero_rl;
1705 sval_t zero_sval;
1706 struct range_list *zero_rl = NULL;
1707 int nr_states;
1708 struct returned_state_callback *cb;
1709 char *return_ranges;
1710 int final_pass_orig = final_pass;
1712 if (option_project != PROJ_KERNEL)
1713 return 0;
1715 nr_states = get_db_state_count();
1716 if (nr_states > 1500)
1717 return 0;
1719 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1720 if (!sm)
1721 return 0;
1722 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1723 return 0;
1725 rl = estate_rl(sm->state);
1726 if (!rl)
1727 return 0;
1729 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1730 return 0;
1731 if (rl_max(rl).value != 0)
1732 return 0;
1733 if (!has_possible_zero_null(sm))
1734 return 0;
1736 __push_fake_cur_stree();
1738 final_pass = 0;
1739 __split_whole_condition(expr);
1740 final_pass = final_pass_orig;
1742 nonzero_rl = rl_filter(rl, rl_zero());
1743 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1744 return_ranges = show_rl(nonzero_rl);
1745 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1747 return_id++;
1748 FOR_EACH_PTR(returned_state_callbacks, cb) {
1749 cb->callback(return_id, return_ranges, expr);
1750 } END_FOR_EACH_PTR(cb);
1752 __push_true_states();
1753 __use_false_states();
1755 return_ranges = alloc_sname("0");
1756 zero_sval = sval_type_val(rl_type(rl), 0);
1757 add_range(&zero_rl, zero_sval, zero_sval);
1758 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1759 return_id++;
1760 FOR_EACH_PTR(returned_state_callbacks, cb) {
1761 cb->callback(return_id, return_ranges, expr);
1762 } END_FOR_EACH_PTR(cb);
1764 __merge_true_states();
1765 __free_fake_cur_stree();
1767 return 1;
1770 static int is_boolean(struct expression *expr)
1772 struct range_list *rl;
1774 if (!get_implied_rl(expr, &rl))
1775 return 0;
1776 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1777 return 1;
1778 return 0;
1781 static int is_conditional(struct expression *expr)
1783 if (!expr)
1784 return 0;
1785 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1786 return 1;
1787 return 0;
1790 static int splitable_function_call(struct expression *expr)
1792 struct sm_state *sm;
1793 char buf[64];
1795 if (!expr || expr->type != EXPR_CALL)
1796 return 0;
1797 snprintf(buf, sizeof(buf), "return %p", expr);
1798 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1799 return split_possible_helper(sm, expr);
1802 static struct sm_state *find_bool_param(void)
1804 struct stree *start_states;
1805 struct symbol *arg;
1806 struct sm_state *sm, *tmp;
1807 sval_t sval;
1809 start_states = get_start_states();
1811 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1812 if (!arg->ident)
1813 continue;
1814 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1815 if (!sm)
1816 continue;
1817 if (rl_min(estate_rl(sm->state)).value != 0 ||
1818 rl_max(estate_rl(sm->state)).value != 1)
1819 continue;
1820 goto found;
1821 } END_FOR_EACH_PTR_REVERSE(arg);
1823 return NULL;
1825 found:
1827 * Check if it's splitable. If not, then splitting it up is likely not
1828 * useful for the callers.
1830 FOR_EACH_PTR(sm->possible, tmp) {
1831 if (is_merged(tmp))
1832 continue;
1833 if (!estate_get_single_value(tmp->state, &sval))
1834 return NULL;
1835 } END_FOR_EACH_PTR(tmp);
1837 return sm;
1840 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1842 struct returned_state_callback *cb;
1843 struct range_list *ret_rl;
1844 const char *return_ranges;
1845 struct sm_state *tmp;
1846 int ret = 0;
1847 int nr_possible, nr_states;
1848 char *compare_str = NULL;
1849 char buf[128];
1850 struct state_list *already_handled = NULL;
1852 if (!sm || !sm->merged)
1853 return 0;
1855 if (too_many_possible(sm))
1856 return 0;
1858 /* bail if it gets too complicated */
1859 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1860 nr_states = get_db_state_count();
1861 if (nr_states * nr_possible >= 2000)
1862 return 0;
1864 FOR_EACH_PTR(sm->possible, tmp) {
1865 if (tmp->merged)
1866 continue;
1867 if (ptr_in_list(tmp, already_handled))
1868 continue;
1869 add_ptr_list(&already_handled, tmp);
1871 ret = 1;
1872 __push_fake_cur_stree();
1874 overwrite_states_using_pool(sm, tmp);
1876 return_ranges = get_return_ranges_str(expr, &ret_rl);
1877 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1878 compare_str = get_return_compare_str(expr);
1879 if (compare_str) {
1880 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1881 return_ranges = alloc_sname(buf);
1884 return_id++;
1885 FOR_EACH_PTR(returned_state_callbacks, cb) {
1886 cb->callback(return_id, (char *)return_ranges, expr);
1887 } END_FOR_EACH_PTR(cb);
1889 __free_fake_cur_stree();
1890 } END_FOR_EACH_PTR(tmp);
1892 free_slist(&already_handled);
1894 return ret;
1897 static int split_by_bool_param(struct expression *expr)
1899 struct sm_state *start_sm, *sm;
1900 sval_t sval;
1902 start_sm = find_bool_param();
1903 if (!start_sm)
1904 return 0;
1905 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1906 if (!sm || estate_get_single_value(sm->state, &sval))
1907 return 0;
1908 return split_on_bool_sm(sm, expr);
1911 static int split_by_null_nonnull_param(struct expression *expr)
1913 struct symbol *arg;
1914 struct sm_state *sm;
1915 sval_t zero = {
1916 .type = &ulong_ctype,
1919 /* function must only take one pointer */
1920 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1921 return 0;
1922 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1923 if (!arg->ident)
1924 return 0;
1925 if (get_real_base_type(arg)->type != SYM_PTR)
1926 return 0;
1928 if (param_was_set_var_sym(arg->ident->name, arg))
1929 return 0;
1930 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1931 if (!sm)
1932 return 0;
1934 if (!rl_has_sval(estate_rl(sm->state), zero))
1935 return 0;
1937 return split_on_bool_sm(sm, expr);
1940 struct expression *strip_expr_statement(struct expression *expr)
1942 struct expression *orig = expr;
1943 struct statement *stmt, *last_stmt;
1945 if (!expr)
1946 return NULL;
1947 if (expr->type == EXPR_PREOP && expr->op == '(')
1948 expr = expr->unop;
1949 if (expr->type != EXPR_STATEMENT)
1950 return orig;
1951 stmt = expr->statement;
1952 if (!stmt || stmt->type != STMT_COMPOUND)
1953 return orig;
1955 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1956 if (!last_stmt || last_stmt->type == STMT_LABEL)
1957 last_stmt = last_stmt->label_statement;
1958 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1959 return orig;
1960 return strip_expr(last_stmt->expression);
1963 static void call_return_state_hooks(struct expression *expr)
1965 struct returned_state_callback *cb;
1966 struct range_list *ret_rl;
1967 const char *return_ranges;
1968 int nr_states;
1969 sval_t sval;
1971 if (__path_is_null())
1972 return;
1974 expr = strip_expr(expr);
1975 expr = strip_expr_statement(expr);
1977 if (is_impossible_path())
1978 goto vanilla;
1980 if (expr && (expr->type == EXPR_COMPARE ||
1981 !get_implied_value(expr, &sval)) &&
1982 (is_condition(expr) || is_boolean(expr))) {
1983 call_return_state_hooks_compare(expr);
1984 return;
1985 } else if (is_conditional(expr)) {
1986 call_return_state_hooks_conditional(expr);
1987 return;
1988 } else if (call_return_state_hooks_split_possible(expr)) {
1989 return;
1990 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1991 return;
1992 } else if (call_return_state_hooks_split_success_fail(expr)) {
1993 return;
1994 } else if (splitable_function_call(expr)) {
1995 return;
1996 } else if (split_positive_from_negative(expr)) {
1997 return;
1998 } else if (split_by_bool_param(expr)) {
1999 } else if (split_by_null_nonnull_param(expr)) {
2000 return;
2003 vanilla:
2004 return_ranges = get_return_ranges_str(expr, &ret_rl);
2005 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2007 return_id++;
2008 nr_states = get_db_state_count();
2009 if (nr_states >= 10000) {
2010 match_return_info(return_id, (char *)return_ranges, expr);
2011 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2012 return;
2014 FOR_EACH_PTR(returned_state_callbacks, cb) {
2015 cb->callback(return_id, (char *)return_ranges, expr);
2016 } END_FOR_EACH_PTR(cb);
2019 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2021 struct returned_member_callback *cb;
2022 struct stree *stree;
2023 struct sm_state *sm;
2024 struct symbol *type;
2025 char *name;
2026 char member_name[256];
2027 int len;
2029 type = get_type(expr);
2030 if (!type || type->type != SYM_PTR)
2031 return;
2032 name = expr_to_var(expr);
2033 if (!name)
2034 return;
2036 member_name[sizeof(member_name) - 1] = '\0';
2037 strcpy(member_name, "$");
2039 len = strlen(name);
2040 FOR_EACH_PTR(returned_member_callbacks, cb) {
2041 stree = __get_cur_stree();
2042 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2043 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2044 strcpy(member_name, "*$");
2045 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2046 continue;
2048 if (strncmp(sm->name, name, len) != 0)
2049 continue;
2050 if (strncmp(sm->name + len, "->", 2) != 0)
2051 continue;
2052 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2053 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2054 } END_FOR_EACH_SM(sm);
2055 } END_FOR_EACH_PTR(cb);
2057 free_string(name);
2060 static void reset_memdb(struct symbol *sym)
2062 mem_sql(NULL, NULL, "delete from caller_info;");
2063 mem_sql(NULL, NULL, "delete from return_states;");
2064 mem_sql(NULL, NULL, "delete from call_implies;");
2065 mem_sql(NULL, NULL, "delete from return_implies;");
2068 static void match_end_func_info(struct symbol *sym)
2070 if (__path_is_null())
2071 return;
2072 call_return_state_hooks(NULL);
2075 static void match_after_func(struct symbol *sym)
2077 if (!__inline_fn)
2078 reset_memdb(sym);
2081 static void init_memdb(void)
2083 char *err = NULL;
2084 int rc;
2085 const char *schema_files[] = {
2086 "db/db.schema",
2087 "db/caller_info.schema",
2088 "db/common_caller_info.schema",
2089 "db/return_states.schema",
2090 "db/function_type_size.schema",
2091 "db/type_size.schema",
2092 "db/function_type_info.schema",
2093 "db/type_info.schema",
2094 "db/call_implies.schema",
2095 "db/return_implies.schema",
2096 "db/function_ptr.schema",
2097 "db/local_values.schema",
2098 "db/function_type_value.schema",
2099 "db/type_value.schema",
2100 "db/function_type.schema",
2101 "db/data_info.schema",
2102 "db/parameter_name.schema",
2103 "db/constraints.schema",
2104 "db/constraints_required.schema",
2105 "db/fn_ptr_data_link.schema",
2106 "db/fn_data_link.schema",
2107 "db/mtag_about.schema",
2108 "db/mtag_map.schema",
2109 "db/mtag_data.schema",
2110 "db/mtag_alias.schema",
2112 static char buf[4096];
2113 int fd;
2114 int ret;
2115 int i;
2117 rc = sqlite3_open(":memory:", &mem_db);
2118 if (rc != SQLITE_OK) {
2119 printf("Error starting In-Memory database.");
2120 return;
2123 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2124 fd = open_schema_file(schema_files[i]);
2125 if (fd < 0) {
2126 printf("failed to open: %s\n", schema_files[i]);
2127 continue;
2129 ret = read(fd, buf, sizeof(buf));
2130 if (ret < 0) {
2131 printf("failed to read: %s\n", schema_files[i]);
2132 continue;
2134 close(fd);
2135 if (ret == sizeof(buf)) {
2136 printf("Schema file too large: %s (limit %zd bytes)",
2137 schema_files[i], sizeof(buf));
2138 continue;
2140 buf[ret] = '\0';
2141 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2142 if (rc != SQLITE_OK) {
2143 fprintf(stderr, "SQL error #2: %s\n", err);
2144 fprintf(stderr, "%s\n", buf);
2149 static void init_cachedb(void)
2151 char *err = NULL;
2152 int rc;
2153 const char *schema_files[] = {
2154 "db/call_implies.schema",
2155 "db/return_implies.schema",
2156 "db/type_info.schema",
2157 "db/mtag_data.schema",
2158 "db/sink_info.schema",
2160 static char buf[4096];
2161 int fd;
2162 int ret;
2163 int i;
2165 rc = sqlite3_open(":memory:", &cache_db);
2166 if (rc != SQLITE_OK) {
2167 printf("Error starting In-Memory database.");
2168 return;
2171 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2172 fd = open_schema_file(schema_files[i]);
2173 if (fd < 0) {
2174 printf("failed to open: %s\n", schema_files[i]);
2175 continue;
2177 ret = read(fd, buf, sizeof(buf));
2178 if (ret < 0) {
2179 printf("failed to read: %s\n", schema_files[i]);
2180 continue;
2182 close(fd);
2183 if (ret == sizeof(buf)) {
2184 printf("Schema file too large: %s (limit %zd bytes)",
2185 schema_files[i], sizeof(buf));
2186 continue;
2188 buf[ret] = '\0';
2189 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2190 if (rc != SQLITE_OK) {
2191 fprintf(stderr, "SQL error #2: %s\n", err);
2192 fprintf(stderr, "%s\n", buf);
2197 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2199 static char buf[4096];
2200 char tmp[256];
2201 char *p = buf;
2202 char *table = _table;
2203 int i;
2206 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2207 for (i = 0; i < argc; i++) {
2208 if (i)
2209 p += snprintf(p, 4096 - (p - buf), ", ");
2210 sqlite3_snprintf(sizeof(tmp), tmp, "%q", argv[i]);
2211 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2214 p += snprintf(p, 4096 - (p - buf), ");");
2215 if (p - buf > 4096)
2216 return 0;
2218 sm_msg("SQL: %s", buf);
2219 return 0;
2222 static void dump_cache(struct symbol_list *sym_list)
2224 if (!option_info)
2225 return;
2226 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2227 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2228 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2229 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2230 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2233 void open_smatch_db(char *db_file)
2235 int rc;
2237 if (option_no_db)
2238 return;
2240 use_states = malloc(num_checks + 1);
2241 memset(use_states, 0xff, num_checks + 1);
2243 init_memdb();
2244 init_cachedb();
2246 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2247 if (rc != SQLITE_OK) {
2248 option_no_db = 1;
2249 return;
2251 run_sql(NULL, NULL,
2252 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2253 return;
2256 static void register_common_funcs(void)
2258 struct token *token;
2259 char *func;
2260 char filename[256];
2262 if (option_project == PROJ_NONE)
2263 strcpy(filename, "common_functions");
2264 else
2265 snprintf(filename, 256, "%s.common_functions", option_project_str);
2267 token = get_tokens_file(filename);
2268 if (!token)
2269 return;
2270 if (token_type(token) != TOKEN_STREAMBEGIN)
2271 return;
2272 token = token->next;
2273 while (token_type(token) != TOKEN_STREAMEND) {
2274 if (token_type(token) != TOKEN_IDENT)
2275 return;
2276 func = alloc_string(show_ident(token->ident));
2277 add_ptr_list(&common_funcs, func);
2278 token = token->next;
2280 clear_token_alloc();
2283 static char *get_next_string(char **str)
2285 static char string[256];
2286 char *start;
2287 char *p = *str;
2288 int len;
2290 if (*p == '\0')
2291 return NULL;
2292 start = p;
2294 while (*p != '\0' && *p != ' ' && *p != '\n')
2295 p++;
2297 len = p - start;
2298 if (len > 256) {
2299 memcpy(string, start, 255);
2300 string[255] = '\0';
2301 printf("return_fix: '%s' too long", string);
2302 **str = '\0';
2303 return NULL;
2305 memcpy(string, start, len);
2306 string[len] = '\0';
2307 if (*p != '\0')
2308 p++;
2309 *str = p;
2310 return string;
2313 static void register_return_replacements(void)
2315 char *func, *orig, *new;
2316 char filename[256];
2317 char buf[4096];
2318 int fd, ret, i;
2319 char *p;
2321 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2322 fd = open_schema_file(filename);
2323 if (fd < 0)
2324 return;
2325 ret = read(fd, buf, sizeof(buf));
2326 close(fd);
2327 if (ret < 0)
2328 return;
2329 if (ret == sizeof(buf)) {
2330 printf("file too large: %s (limit %zd bytes)",
2331 filename, sizeof(buf));
2332 return;
2334 buf[ret] = '\0';
2336 p = buf;
2337 while (*p) {
2338 get_next_string(&p);
2339 replace_count++;
2341 if (replace_count == 0 || replace_count % 3 != 0) {
2342 replace_count = 0;
2343 return;
2345 replace_table = malloc(replace_count * sizeof(char *));
2347 p = buf;
2348 i = 0;
2349 while (*p) {
2350 func = alloc_string(get_next_string(&p));
2351 orig = alloc_string(get_next_string(&p));
2352 new = alloc_string(get_next_string(&p));
2354 replace_table[i++] = func;
2355 replace_table[i++] = orig;
2356 replace_table[i++] = new;
2360 void register_definition_db_callbacks(int id)
2362 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2363 add_hook(&global_variable, BASE_HOOK);
2364 add_hook(&global_variable, DECLARATION_HOOK);
2365 add_split_return_callback(match_return_info);
2366 add_split_return_callback(print_returned_struct_members);
2367 add_hook(&call_return_state_hooks, RETURN_HOOK);
2368 add_hook(&match_end_func_info, END_FUNC_HOOK);
2369 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2371 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2372 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2373 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2375 register_common_funcs();
2376 register_return_replacements();
2378 add_hook(&dump_cache, END_FILE_HOOK);
2381 void register_db_call_marker(int id)
2383 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2386 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2388 struct expression *arg;
2389 char *name = NULL;
2390 char member_name[256];
2392 *sym = NULL;
2394 if (param == -1) {
2395 const char *star = "";
2397 if (expr->type != EXPR_ASSIGNMENT)
2398 return NULL;
2399 name = expr_to_var_sym(expr->left, sym);
2400 if (!name)
2401 return NULL;
2402 if (key[0] == '*') {
2403 star = "*";
2404 key++;
2406 if (strncmp(key, "$", 1) != 0)
2407 return name;
2408 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2409 free_string(name);
2410 return alloc_string(member_name);
2413 while (expr->type == EXPR_ASSIGNMENT)
2414 expr = strip_expr(expr->right);
2415 if (expr->type != EXPR_CALL)
2416 return NULL;
2418 arg = get_argument_from_call_expr(expr->args, param);
2419 if (!arg)
2420 return NULL;
2422 return get_variable_from_key(arg, key, sym);
2425 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2427 char buf[256];
2428 char *tmp;
2430 if (!arg)
2431 return NULL;
2433 arg = strip_expr(arg);
2435 if (strcmp(key, "$") == 0)
2436 return expr_to_var_sym(arg, sym);
2438 if (strcmp(key, "*$") == 0) {
2439 if (arg->type == EXPR_PREOP && arg->op == '&') {
2440 arg = strip_expr(arg->unop);
2441 return expr_to_var_sym(arg, sym);
2442 } else {
2443 tmp = expr_to_var_sym(arg, sym);
2444 if (!tmp)
2445 return NULL;
2446 snprintf(buf, sizeof(buf), "*%s", tmp);
2447 free_string(tmp);
2448 return alloc_string(buf);
2452 if (arg->type == EXPR_PREOP && arg->op == '&') {
2453 arg = strip_expr(arg->unop);
2454 tmp = expr_to_var_sym(arg, sym);
2455 if (!tmp)
2456 return NULL;
2457 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2458 return alloc_string(buf);
2461 tmp = expr_to_var_sym(arg, sym);
2462 if (!tmp)
2463 return NULL;
2464 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2465 free_string(tmp);
2466 return alloc_string(buf);
2469 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2471 *vsl = NULL;
2473 if (strcmp("$", key) == 0)
2474 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2475 return get_variable_from_key(arg, key, sym);
2478 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2480 int name_len;
2481 static char buf[256];
2483 name_len = strlen(param_name);
2485 if (strcmp(state_name, param_name) == 0) {
2486 return "$";
2487 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2488 strncmp(state_name, param_name, name_len) == 0) {
2489 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2490 return buf;
2491 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2492 return "*$";
2494 return NULL;
2497 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2499 if (!sym || !sym->ident)
2500 return NULL;
2502 return state_name_to_param_name(name, sym->ident->name);
2505 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2507 struct symbol *type;
2508 const char *sym_name;
2509 int name_len;
2510 static char buf[256];
2513 * mtag_name is different from param_name because mtags can be a struct
2514 * instead of a struct pointer. But we want to treat it like a pointer
2515 * because really an mtag is a pointer. Or in other words, if you pass
2516 * a struct foo then you want to talk about foo.bar but with an mtag
2517 * you want to refer to it as foo->bar.
2521 if (!sym || !sym->ident)
2522 return NULL;
2524 type = get_real_base_type(sym);
2525 if (type && type->type == SYM_BASETYPE)
2526 return "*$";
2528 sym_name = sym->ident->name;
2529 name_len = strlen(sym_name);
2531 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2532 strncmp(state_name, sym_name, name_len) == 0) {
2533 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2534 return buf;
2537 return state_name_to_param_name(state_name, sym_name);
2540 const char *get_mtag_name_expr(struct expression *expr)
2542 char *name;
2543 struct symbol *sym;
2544 const char *ret = NULL;
2546 name = expr_to_var_sym(expr, &sym);
2547 if (!name || !sym)
2548 goto free;
2550 ret = get_mtag_name_var_sym(name, sym);
2551 free:
2552 free_string(name);
2553 return ret;
2556 const char *get_param_name(struct sm_state *sm)
2558 return get_param_name_var_sym(sm->name, sm->sym);
2561 char *get_data_info_name(struct expression *expr)
2563 struct symbol *sym;
2564 char *name;
2565 char buf[256];
2566 char *ret = NULL;
2568 expr = strip_expr(expr);
2569 name = get_member_name(expr);
2570 if (name)
2571 return name;
2572 name = expr_to_var_sym(expr, &sym);
2573 if (!name || !sym)
2574 goto free;
2575 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2576 goto free;
2577 if (sym->ctype.modifiers & MOD_STATIC)
2578 snprintf(buf, sizeof(buf), "static %s", name);
2579 else
2580 snprintf(buf, sizeof(buf), "global %s", name);
2581 ret = alloc_sname(buf);
2582 free:
2583 free_string(name);
2584 return ret;