helper: improve get_member_name() for anonymous structs
[smatch.git] / smatch_db.c
blob1311e406d7d3041d8460c0734b7e6e69e8abd2c9
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 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;
762 struct symbol *type;
764 expr = strip_expr(expr);
765 if (!expr)
766 return;
767 if (expr->type == EXPR_PREOP && expr->op == '&') {
768 expr = strip_expr(expr->unop);
769 is_address = 1;
772 type = get_type(expr);
773 if (type && type_bits(type) < type_bits(&ulong_ctype))
774 return;
776 name = expr_to_var_sym(expr, &sym);
777 if (!name || !sym)
778 goto free;
780 len = strlen(name);
781 FOR_EACH_SM(stree, sm) {
782 if (sm->sym != sym)
783 continue;
784 if (strcmp(name, sm->name) == 0) {
785 if (is_address)
786 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
787 else /* these are already handled. fixme: handle them here */
788 continue;
789 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
790 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
791 } else if (strncmp(name, sm->name, len) == 0) {
792 if (isalnum(sm->name[len]))
793 continue;
794 if (is_address)
795 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
796 else
797 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
798 } else {
799 continue;
801 callback(call, param, printed_name, sm);
802 } END_FOR_EACH_SM(sm);
803 free:
804 free_string(name);
807 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
809 char **container = _container;
810 static char buf[256];
812 snprintf(buf, sizeof(buf), "%s", argv[0]);
813 *container = buf;
814 return 0;
817 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
818 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
820 struct expression *tmp;
821 char *container = NULL;
822 int offset;
823 int holder_offset;
824 char *p;
826 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
827 return;
830 * We can't use the in-mem DB because we have to parse the function
831 * first, then we know if it takes a container, then we know to pass it
832 * the container data.
835 run_sql(&param_used_callback, &container,
836 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
837 get_static_filter(call->fn->symbol), CONTAINER, param);
838 if (!container)
839 return;
841 p = strchr(container, '-');
842 if (!p)
843 return;
844 offset = atoi(p);
845 p = strchr(p, ')');
846 if (!p)
847 return;
848 p++;
850 tmp = get_assigned_expr(expr);
851 if (tmp)
852 expr = tmp;
854 if (expr->type != EXPR_PREOP || expr->op != '&')
855 return;
856 expr = strip_expr(expr->unop);
857 holder_offset = get_member_offset_from_deref(expr);
858 if (-holder_offset != offset)
859 return;
861 expr = strip_expr(expr->deref);
862 if (expr->type == EXPR_PREOP && expr->op == '*')
863 expr = strip_expr(expr->unop);
865 print_struct_members(call, expr, param, holder_offset, stree, callback);
868 static void match_call_info(struct expression *call)
870 struct member_info_callback *cb;
871 struct expression *arg;
872 struct stree *stree;
873 char *name;
874 int i;
876 name = get_fnptr_name(call->fn);
877 if (!name)
878 return;
880 FOR_EACH_PTR(member_callbacks, cb) {
881 stree = get_all_states_stree(cb->owner);
882 i = 0;
883 FOR_EACH_PTR(call->args, arg) {
884 print_struct_members(call, arg, i, -1, stree, cb->callback);
885 print_container_struct_members(call, arg, i, stree, cb->callback);
886 i++;
887 } END_FOR_EACH_PTR(arg);
888 free_stree(&stree);
889 } END_FOR_EACH_PTR(cb);
891 free_string(name);
894 static int get_param(int param, char **name, struct symbol **sym)
896 struct symbol *arg;
897 int i;
899 i = 0;
900 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
902 * this is a temporary hack to work around a bug (I think in sparse?)
903 * 2.6.37-rc1:fs/reiserfs/journal.o
904 * If there is a function definition without parameter name found
905 * after a function implementation then it causes a crash.
906 * int foo() {}
907 * int bar(char *);
909 if (arg->ident->name < (char *)100)
910 continue;
911 if (i == param) {
912 *name = arg->ident->name;
913 *sym = arg;
914 return TRUE;
916 i++;
917 } END_FOR_EACH_PTR(arg);
919 return FALSE;
922 static int function_signature_matches(const char *sig)
924 char *my_sig;
926 my_sig = function_signature();
927 if (!sig || !my_sig)
928 return 1; /* default to matching */
929 if (strcmp(my_sig, sig) == 0)
930 return 1;
931 return 0;
934 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
936 struct select_caller_info_data *data = _data;
937 int func_id;
938 long type;
939 long param;
940 char *key;
941 char *value;
942 char *name = NULL;
943 struct symbol *sym = NULL;
944 struct def_callback *def_callback;
945 struct stree *stree;
946 struct timeval cur_time;
948 data->results = 1;
950 if (argc != 5)
951 return 0;
953 gettimeofday(&cur_time, NULL);
954 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
955 return 0;
957 func_id = atoi(argv[0]);
958 errno = 0;
959 type = strtol(argv[1], NULL, 10);
960 param = strtol(argv[2], NULL, 10);
961 if (errno)
962 return 0;
963 key = argv[3];
964 value = argv[4];
966 if (data->prev_func_id == -1)
967 data->prev_func_id = func_id;
968 if (func_id != data->prev_func_id) {
969 stree = __pop_fake_cur_stree();
970 if (!data->ignore)
971 merge_stree(&data->final_states, stree);
972 free_stree(&stree);
973 __push_fake_cur_stree();
974 __unnullify_path();
975 data->prev_func_id = func_id;
976 data->ignore = 0;
979 if (data->ignore)
980 return 0;
981 if (type == INTERNAL &&
982 !function_signature_matches(value)) {
983 data->ignore = 1;
984 return 0;
987 if (param >= 0 && !get_param(param, &name, &sym))
988 return 0;
990 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
991 if (def_callback->hook_type == type)
992 def_callback->callback(name, sym, key, value);
993 } END_FOR_EACH_PTR(def_callback);
995 return 0;
998 static struct string_list *ptr_names_done;
999 static struct string_list *ptr_names;
1001 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1003 insert_string(&ptr_names, alloc_string(argv[0]));
1004 return 0;
1007 static char *get_next_ptr_name(void)
1009 char *ptr;
1011 FOR_EACH_PTR(ptr_names, ptr) {
1012 if (list_has_string(ptr_names_done, ptr))
1013 continue;
1014 insert_string(&ptr_names_done, ptr);
1015 return ptr;
1016 } END_FOR_EACH_PTR(ptr);
1017 return NULL;
1020 static void get_ptr_names(const char *file, const char *name)
1022 char sql_filter[1024];
1023 int before, after;
1025 if (file) {
1026 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1027 file, name);
1028 } else {
1029 snprintf(sql_filter, 1024, "function = '%s';", name);
1032 before = ptr_list_size((struct ptr_list *)ptr_names);
1034 run_sql(get_ptr_name, NULL,
1035 "select distinct ptr from function_ptr where %s",
1036 sql_filter);
1038 after = ptr_list_size((struct ptr_list *)ptr_names);
1039 if (before == after)
1040 return;
1042 while ((name = get_next_ptr_name()))
1043 get_ptr_names(NULL, name);
1046 static void match_data_from_db(struct symbol *sym)
1048 struct select_caller_info_data data = { .prev_func_id = -1 };
1049 struct sm_state *sm;
1050 struct stree *stree;
1051 struct timeval end_time;
1053 if (!sym || !sym->ident)
1054 return;
1056 gettimeofday(&data.start_time, NULL);
1058 __push_fake_cur_stree();
1059 __unnullify_path();
1061 if (!__inline_fn) {
1062 char *ptr;
1064 if (sym->ctype.modifiers & MOD_STATIC)
1065 get_ptr_names(get_base_file(), sym->ident->name);
1066 else
1067 get_ptr_names(NULL, sym->ident->name);
1069 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1070 __free_ptr_list((struct ptr_list **)&ptr_names);
1071 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1072 stree = __pop_fake_cur_stree();
1073 free_stree(&stree);
1074 return;
1077 sql_select_caller_info(&data,
1078 "call_id, type, parameter, key, value",
1079 sym);
1082 stree = __pop_fake_cur_stree();
1083 if (!data.ignore)
1084 merge_stree(&data.final_states, stree);
1085 free_stree(&stree);
1086 __push_fake_cur_stree();
1087 __unnullify_path();
1088 data.prev_func_id = -1;
1089 data.ignore = 0;
1091 FOR_EACH_PTR(ptr_names, ptr) {
1092 run_sql(caller_info_callback, &data,
1093 "select call_id, type, parameter, key, value"
1094 " from common_caller_info where function = '%s' order by call_id",
1095 ptr);
1096 } END_FOR_EACH_PTR(ptr);
1098 if (data.results) {
1099 FOR_EACH_PTR(ptr_names, ptr) {
1100 free_string(ptr);
1101 } END_FOR_EACH_PTR(ptr);
1102 goto free_ptr_names;
1105 FOR_EACH_PTR(ptr_names, ptr) {
1106 run_sql(caller_info_callback, &data,
1107 "select call_id, type, parameter, key, value"
1108 " from caller_info where function = '%s' order by call_id",
1109 ptr);
1110 free_string(ptr);
1111 } END_FOR_EACH_PTR(ptr);
1113 free_ptr_names:
1114 __free_ptr_list((struct ptr_list **)&ptr_names);
1115 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1116 } else {
1117 sql_select_caller_info(&data,
1118 "call_id, type, parameter, key, value",
1119 sym);
1122 stree = __pop_fake_cur_stree();
1123 if (!data.ignore)
1124 merge_stree(&data.final_states, stree);
1125 free_stree(&stree);
1127 gettimeofday(&end_time, NULL);
1128 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1129 FOR_EACH_SM(data.final_states, sm) {
1130 __set_sm(sm);
1131 } END_FOR_EACH_SM(sm);
1134 free_stree(&data.final_states);
1137 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1139 struct implies_info *info = _info;
1140 struct db_implies_callback *cb;
1141 struct expression *arg = NULL;
1142 int type;
1143 int param;
1145 if (argc != 5)
1146 return 0;
1148 type = atoi(argv[1]);
1149 param = atoi(argv[2]);
1151 FOR_EACH_PTR(info->cb_list, cb) {
1152 if (cb->type != type)
1153 continue;
1154 if (param != -1) {
1155 arg = get_argument_from_call_expr(info->expr->args, param);
1156 if (!arg)
1157 continue;
1159 cb->callback(info->expr, arg, argv[3], argv[4]);
1160 } END_FOR_EACH_PTR(cb);
1162 return 0;
1165 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1167 struct implies_info *info = _info;
1168 struct db_implies_callback *cb;
1169 struct expression *arg;
1170 struct symbol *sym;
1171 char *name;
1172 int type;
1173 int param;
1175 if (argc != 5)
1176 return 0;
1178 type = atoi(argv[1]);
1179 param = atoi(argv[2]);
1181 if (!get_param(param, &name, &sym))
1182 return 0;
1183 arg = symbol_expression(sym);
1184 if (!arg)
1185 return 0;
1187 FOR_EACH_PTR(info->cb_list, cb) {
1188 if (cb->type != type)
1189 continue;
1190 cb->callback(info->expr, arg, argv[3], argv[4]);
1191 } END_FOR_EACH_PTR(cb);
1193 return 0;
1196 static void match_return_implies(struct expression *expr)
1198 struct implies_info info = {
1199 .type = RETURN_IMPLIES,
1200 .cb_list = return_implies_cb_list,
1203 if (expr->fn->type != EXPR_SYMBOL ||
1204 !expr->fn->symbol)
1205 return;
1206 info.expr = expr;
1207 info.sym = expr->fn->symbol;
1208 sql_select_implies("function, type, parameter, key, value", &info,
1209 return_implies_callbacks);
1212 static void match_call_implies(struct symbol *sym)
1214 struct implies_info info = {
1215 .type = CALL_IMPLIES,
1216 .cb_list = call_implies_cb_list,
1219 if (!sym || !sym->ident)
1220 return;
1222 info.sym = sym;
1223 sql_select_implies("function, type, parameter, key, value", &info,
1224 call_implies_callbacks);
1227 static void print_initializer_list(struct expression_list *expr_list,
1228 struct symbol *struct_type)
1230 struct expression *expr;
1231 struct symbol *base_type;
1232 char struct_name[256];
1234 FOR_EACH_PTR(expr_list, expr) {
1235 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1236 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1237 continue;
1239 if (expr->type != EXPR_IDENTIFIER)
1240 continue;
1241 if (!expr->expr_ident)
1242 continue;
1243 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1244 continue;
1245 base_type = get_type(expr->ident_expression);
1246 if (!base_type || base_type->type != SYM_FN)
1247 continue;
1248 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1249 struct_type->ident->name, expr->expr_ident->name);
1250 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1251 struct_name);
1252 } END_FOR_EACH_PTR(expr);
1255 static void global_variable(struct symbol *sym)
1257 struct symbol *struct_type;
1259 if (!sym->ident)
1260 return;
1261 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1262 return;
1263 struct_type = get_base_type(sym);
1264 if (!struct_type)
1265 return;
1266 if (struct_type->type == SYM_ARRAY) {
1267 struct_type = get_base_type(struct_type);
1268 if (!struct_type)
1269 return;
1271 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1272 return;
1273 print_initializer_list(sym->initializer->expr_list, struct_type);
1276 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1278 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1281 static void call_return_state_hooks_conditional(struct expression *expr)
1283 struct returned_state_callback *cb;
1284 struct range_list *rl;
1285 char *return_ranges;
1286 int final_pass_orig = final_pass;
1288 __push_fake_cur_stree();
1290 final_pass = 0;
1291 __split_whole_condition(expr->conditional);
1292 final_pass = final_pass_orig;
1294 if (get_implied_rl(expr->cond_true, &rl))
1295 rl = cast_rl(cur_func_return_type(), rl);
1296 else
1297 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1298 return_ranges = show_rl(rl);
1299 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1301 return_id++;
1302 FOR_EACH_PTR(returned_state_callbacks, cb) {
1303 cb->callback(return_id, return_ranges, expr->cond_true);
1304 } END_FOR_EACH_PTR(cb);
1306 __push_true_states();
1307 __use_false_states();
1309 if (get_implied_rl(expr->cond_false, &rl))
1310 rl = cast_rl(cur_func_return_type(), rl);
1311 else
1312 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1313 return_ranges = show_rl(rl);
1314 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1316 return_id++;
1317 FOR_EACH_PTR(returned_state_callbacks, cb) {
1318 cb->callback(return_id, return_ranges, expr->cond_false);
1319 } END_FOR_EACH_PTR(cb);
1321 __merge_true_states();
1322 __free_fake_cur_stree();
1325 static void call_return_state_hooks_compare(struct expression *expr)
1327 struct returned_state_callback *cb;
1328 char *return_ranges;
1329 int final_pass_orig = final_pass;
1330 sval_t sval = { .type = &int_ctype };
1331 sval_t ret;
1333 if (!get_implied_value(expr, &ret))
1334 ret.value = -1;
1336 __push_fake_cur_stree();
1338 final_pass = 0;
1339 __split_whole_condition(expr);
1340 final_pass = final_pass_orig;
1342 if (ret.value != 0) {
1343 return_ranges = alloc_sname("1");
1344 sval.value = 1;
1345 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1347 return_id++;
1348 FOR_EACH_PTR(returned_state_callbacks, cb) {
1349 cb->callback(return_id, return_ranges, expr);
1350 } END_FOR_EACH_PTR(cb);
1353 __push_true_states();
1354 __use_false_states();
1356 if (ret.value != 1) {
1357 return_ranges = alloc_sname("0");
1358 sval.value = 0;
1359 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1361 return_id++;
1362 FOR_EACH_PTR(returned_state_callbacks, cb) {
1363 cb->callback(return_id, return_ranges, expr);
1364 } END_FOR_EACH_PTR(cb);
1367 __merge_true_states();
1368 __free_fake_cur_stree();
1371 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1373 struct sm_state *tmp;
1375 FOR_EACH_PTR(slist, tmp) {
1376 if (strcmp(tmp->state->name, sm->state->name) == 0)
1377 return 1;
1378 } END_FOR_EACH_PTR(tmp);
1380 return 0;
1383 static char *get_return_compare_str(struct expression *expr)
1385 char *compare_str;
1386 char *var;
1387 char buf[256];
1388 int comparison;
1389 int param;
1391 compare_str = expr_lte_to_param(expr, -1);
1392 if (compare_str)
1393 return compare_str;
1394 param = get_param_num(expr);
1395 if (param < 0)
1396 return NULL;
1398 var = expr_to_var(expr);
1399 if (!var)
1400 return NULL;
1401 snprintf(buf, sizeof(buf), "%s orig", var);
1402 comparison = get_comparison_strings(var, buf);
1403 free_string(var);
1405 if (!comparison)
1406 return NULL;
1408 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1409 return alloc_sname(buf);
1412 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1414 struct returned_state_callback *cb;
1415 struct range_list *rl;
1416 char *return_ranges;
1417 struct sm_state *tmp;
1418 int ret = 0;
1419 int nr_possible, nr_states;
1420 char *compare_str = NULL;
1421 char buf[128];
1422 struct state_list *already_handled = NULL;
1424 if (!sm || !sm->merged)
1425 return 0;
1427 if (too_many_possible(sm))
1428 return 0;
1430 /* bail if it gets too complicated */
1431 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1432 nr_states = get_db_state_count();
1433 if (nr_states * nr_possible >= 2000)
1434 return 0;
1436 FOR_EACH_PTR(sm->possible, tmp) {
1437 if (tmp->merged)
1438 continue;
1439 if (ptr_in_list(tmp, already_handled))
1440 continue;
1441 add_ptr_list(&already_handled, tmp);
1443 ret = 1;
1444 __push_fake_cur_stree();
1446 overwrite_states_using_pool(sm, tmp);
1448 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1449 return_ranges = show_rl(rl);
1450 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1451 compare_str = get_return_compare_str(expr);
1452 if (compare_str) {
1453 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1454 return_ranges = alloc_sname(buf);
1457 return_id++;
1458 FOR_EACH_PTR(returned_state_callbacks, cb) {
1459 cb->callback(return_id, return_ranges, expr);
1460 } END_FOR_EACH_PTR(cb);
1462 __free_fake_cur_stree();
1463 } END_FOR_EACH_PTR(tmp);
1465 free_slist(&already_handled);
1467 return ret;
1470 static int call_return_state_hooks_split_possible(struct expression *expr)
1472 struct sm_state *sm;
1474 if (!expr || expr_equal_to_param(expr, -1))
1475 return 0;
1477 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1478 return split_possible_helper(sm, expr);
1481 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1483 struct range_list *rl;
1484 char *return_ranges;
1485 sval_t sval;
1486 char *compare_str;
1487 char *math_str;
1488 char buf[128];
1490 *rl_p = NULL;
1492 if (!expr)
1493 return alloc_sname("");
1495 if (get_implied_value(expr, &sval)) {
1496 sval = sval_cast(cur_func_return_type(), sval);
1497 *rl_p = alloc_rl(sval, sval);
1498 return sval_to_str(sval);
1501 compare_str = expr_equal_to_param(expr, -1);
1502 math_str = get_value_in_terms_of_parameter_math(expr);
1504 if (get_implied_rl(expr, &rl)) {
1505 rl = cast_rl(cur_func_return_type(), rl);
1506 return_ranges = show_rl(rl);
1507 } else if (get_imaginary_absolute(expr, &rl)){
1508 rl = cast_rl(cur_func_return_type(), rl);
1509 return alloc_sname(show_rl(rl));
1510 } else {
1511 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1512 return_ranges = show_rl(rl);
1514 *rl_p = rl;
1516 if (compare_str) {
1517 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1518 return alloc_sname(buf);
1520 if (math_str) {
1521 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1522 return alloc_sname(buf);
1524 compare_str = get_return_compare_str(expr);
1525 if (compare_str) {
1526 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1527 return alloc_sname(buf);
1530 return return_ranges;
1533 static bool has_possible_negative(struct sm_state *sm)
1535 struct sm_state *tmp;
1537 FOR_EACH_PTR(sm->possible, tmp) {
1538 if (!estate_rl(tmp->state))
1539 continue;
1540 if (sval_is_negative(estate_min(tmp->state)) &&
1541 sval_is_negative(estate_max(tmp->state)))
1542 return true;
1543 } END_FOR_EACH_PTR(tmp);
1545 return false;
1548 static bool has_possible_zero_null(struct sm_state *sm)
1550 struct sm_state *tmp;
1551 sval_t sval;
1553 FOR_EACH_PTR(sm->possible, tmp) {
1554 if (!estate_get_single_value(tmp->state, &sval))
1555 continue;
1556 if (sval.value == 0)
1557 return true;
1558 } END_FOR_EACH_PTR(tmp);
1560 return false;
1563 static int split_positive_from_negative(struct expression *expr)
1565 struct sm_state *sm;
1566 struct returned_state_callback *cb;
1567 struct range_list *rl;
1568 const char *return_ranges;
1569 struct range_list *ret_rl;
1570 int undo;
1571 bool has_zero;
1573 /* We're going to print the states 3 times */
1574 if (get_db_state_count() > 10000 / 3)
1575 return 0;
1577 if (!get_implied_rl(expr, &rl) || !rl)
1578 return 0;
1579 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1580 return 0;
1581 /* Forget about INT_MAX and larger */
1582 if (rl_max(rl).value <= 0)
1583 return 0;
1584 if (!sval_is_negative(rl_min(rl)))
1585 return 0;
1587 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1588 if (!sm)
1589 return 0;
1590 if (!has_possible_negative(sm))
1591 return 0;
1592 has_zero = has_possible_zero_null(sm);
1594 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1595 return 0;
1597 return_id++;
1598 return_ranges = get_return_ranges_str(expr, &ret_rl);
1599 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1600 FOR_EACH_PTR(returned_state_callbacks, cb) {
1601 cb->callback(return_id, (char *)return_ranges, expr);
1602 } END_FOR_EACH_PTR(cb);
1604 end_assume();
1606 if (has_zero) {
1607 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1609 return_id++;
1610 return_ranges = get_return_ranges_str(expr, &ret_rl);
1611 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1612 FOR_EACH_PTR(returned_state_callbacks, cb) {
1613 cb->callback(return_id, (char *)return_ranges, expr);
1614 } END_FOR_EACH_PTR(cb);
1616 if (undo)
1617 end_assume();
1620 undo = assume(compare_expression(expr, '<', zero_expr()));
1622 return_id++;
1623 return_ranges = get_return_ranges_str(expr, &ret_rl);
1624 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1625 FOR_EACH_PTR(returned_state_callbacks, cb) {
1626 cb->callback(return_id, (char *)return_ranges, expr);
1627 } END_FOR_EACH_PTR(cb);
1629 if (undo)
1630 end_assume();
1632 return 1;
1635 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1637 struct returned_state_callback *cb;
1638 struct range_list *rl;
1639 struct range_list *nonnull_rl;
1640 sval_t null_sval;
1641 struct range_list *null_rl = NULL;
1642 char *return_ranges;
1643 struct sm_state *sm;
1644 struct smatch_state *state;
1645 int nr_states;
1646 int final_pass_orig = final_pass;
1648 if (!expr || expr_equal_to_param(expr, -1))
1649 return 0;
1650 if (expr->type == EXPR_CALL)
1651 return 0;
1652 if (!is_pointer(expr))
1653 return 0;
1655 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1656 if (!sm)
1657 return 0;
1658 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1659 return 0;
1660 state = sm->state;
1661 if (!estate_rl(state))
1662 return 0;
1663 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1664 return 0;
1665 if (!has_possible_zero_null(sm))
1666 return 0;
1668 nr_states = get_db_state_count();
1669 if (option_info && nr_states >= 1500)
1670 return 0;
1672 rl = estate_rl(state);
1674 __push_fake_cur_stree();
1676 final_pass = 0;
1677 __split_whole_condition(expr);
1678 final_pass = final_pass_orig;
1680 nonnull_rl = rl_filter(rl, rl_zero());
1681 return_ranges = show_rl(nonnull_rl);
1682 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1684 return_id++;
1685 FOR_EACH_PTR(returned_state_callbacks, cb) {
1686 cb->callback(return_id, return_ranges, expr);
1687 } END_FOR_EACH_PTR(cb);
1689 __push_true_states();
1690 __use_false_states();
1692 return_ranges = alloc_sname("0");
1693 null_sval = sval_type_val(rl_type(rl), 0);
1694 add_range(&null_rl, null_sval, null_sval);
1695 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1696 return_id++;
1697 FOR_EACH_PTR(returned_state_callbacks, cb) {
1698 cb->callback(return_id, return_ranges, expr);
1699 } END_FOR_EACH_PTR(cb);
1701 __merge_true_states();
1702 __free_fake_cur_stree();
1704 return 1;
1707 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1709 struct sm_state *sm;
1710 struct range_list *rl;
1711 struct range_list *nonzero_rl;
1712 sval_t zero_sval;
1713 struct range_list *zero_rl = NULL;
1714 int nr_states;
1715 struct returned_state_callback *cb;
1716 char *return_ranges;
1717 int final_pass_orig = final_pass;
1719 if (option_project != PROJ_KERNEL)
1720 return 0;
1722 nr_states = get_db_state_count();
1723 if (nr_states > 1500)
1724 return 0;
1726 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1727 if (!sm)
1728 return 0;
1729 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1730 return 0;
1732 rl = estate_rl(sm->state);
1733 if (!rl)
1734 return 0;
1736 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1737 return 0;
1738 if (rl_max(rl).value != 0)
1739 return 0;
1740 if (!has_possible_zero_null(sm))
1741 return 0;
1743 __push_fake_cur_stree();
1745 final_pass = 0;
1746 __split_whole_condition(expr);
1747 final_pass = final_pass_orig;
1749 nonzero_rl = rl_filter(rl, rl_zero());
1750 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1751 return_ranges = show_rl(nonzero_rl);
1752 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1754 return_id++;
1755 FOR_EACH_PTR(returned_state_callbacks, cb) {
1756 cb->callback(return_id, return_ranges, expr);
1757 } END_FOR_EACH_PTR(cb);
1759 __push_true_states();
1760 __use_false_states();
1762 return_ranges = alloc_sname("0");
1763 zero_sval = sval_type_val(rl_type(rl), 0);
1764 add_range(&zero_rl, zero_sval, zero_sval);
1765 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1766 return_id++;
1767 FOR_EACH_PTR(returned_state_callbacks, cb) {
1768 cb->callback(return_id, return_ranges, expr);
1769 } END_FOR_EACH_PTR(cb);
1771 __merge_true_states();
1772 __free_fake_cur_stree();
1774 return 1;
1777 static int is_boolean(struct expression *expr)
1779 struct range_list *rl;
1781 if (!get_implied_rl(expr, &rl))
1782 return 0;
1783 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1784 return 1;
1785 return 0;
1788 static int is_conditional(struct expression *expr)
1790 if (!expr)
1791 return 0;
1792 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1793 return 1;
1794 return 0;
1797 static int splitable_function_call(struct expression *expr)
1799 struct sm_state *sm;
1800 char buf[64];
1802 if (!expr || expr->type != EXPR_CALL)
1803 return 0;
1804 snprintf(buf, sizeof(buf), "return %p", expr);
1805 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1806 return split_possible_helper(sm, expr);
1809 static struct sm_state *find_bool_param(void)
1811 struct stree *start_states;
1812 struct symbol *arg;
1813 struct sm_state *sm, *tmp;
1814 sval_t sval;
1816 start_states = get_start_states();
1818 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1819 if (!arg->ident)
1820 continue;
1821 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1822 if (!sm)
1823 continue;
1824 if (rl_min(estate_rl(sm->state)).value != 0 ||
1825 rl_max(estate_rl(sm->state)).value != 1)
1826 continue;
1827 goto found;
1828 } END_FOR_EACH_PTR_REVERSE(arg);
1830 return NULL;
1832 found:
1834 * Check if it's splitable. If not, then splitting it up is likely not
1835 * useful for the callers.
1837 FOR_EACH_PTR(sm->possible, tmp) {
1838 if (is_merged(tmp))
1839 continue;
1840 if (!estate_get_single_value(tmp->state, &sval))
1841 return NULL;
1842 } END_FOR_EACH_PTR(tmp);
1844 return sm;
1847 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1849 struct returned_state_callback *cb;
1850 struct range_list *ret_rl;
1851 const char *return_ranges;
1852 struct sm_state *tmp;
1853 int ret = 0;
1854 int nr_possible, nr_states;
1855 char *compare_str = NULL;
1856 char buf[128];
1857 struct state_list *already_handled = NULL;
1859 if (!sm || !sm->merged)
1860 return 0;
1862 if (too_many_possible(sm))
1863 return 0;
1865 /* bail if it gets too complicated */
1866 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1867 nr_states = get_db_state_count();
1868 if (nr_states * nr_possible >= 2000)
1869 return 0;
1871 FOR_EACH_PTR(sm->possible, tmp) {
1872 if (tmp->merged)
1873 continue;
1874 if (ptr_in_list(tmp, already_handled))
1875 continue;
1876 add_ptr_list(&already_handled, tmp);
1878 ret = 1;
1879 __push_fake_cur_stree();
1881 overwrite_states_using_pool(sm, tmp);
1883 return_ranges = get_return_ranges_str(expr, &ret_rl);
1884 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1885 compare_str = get_return_compare_str(expr);
1886 if (compare_str) {
1887 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1888 return_ranges = alloc_sname(buf);
1891 return_id++;
1892 FOR_EACH_PTR(returned_state_callbacks, cb) {
1893 cb->callback(return_id, (char *)return_ranges, expr);
1894 } END_FOR_EACH_PTR(cb);
1896 __free_fake_cur_stree();
1897 } END_FOR_EACH_PTR(tmp);
1899 free_slist(&already_handled);
1901 return ret;
1904 static int split_by_bool_param(struct expression *expr)
1906 struct sm_state *start_sm, *sm;
1907 sval_t sval;
1909 start_sm = find_bool_param();
1910 if (!start_sm)
1911 return 0;
1912 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1913 if (!sm || estate_get_single_value(sm->state, &sval))
1914 return 0;
1915 return split_on_bool_sm(sm, expr);
1918 static int split_by_null_nonnull_param(struct expression *expr)
1920 struct symbol *arg;
1921 struct sm_state *sm;
1922 sval_t zero = {
1923 .type = &ulong_ctype,
1926 /* function must only take one pointer */
1927 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1928 return 0;
1929 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1930 if (!arg->ident)
1931 return 0;
1932 if (get_real_base_type(arg)->type != SYM_PTR)
1933 return 0;
1935 if (param_was_set_var_sym(arg->ident->name, arg))
1936 return 0;
1937 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1938 if (!sm)
1939 return 0;
1941 if (!rl_has_sval(estate_rl(sm->state), zero))
1942 return 0;
1944 return split_on_bool_sm(sm, expr);
1947 struct expression *strip_expr_statement(struct expression *expr)
1949 struct expression *orig = expr;
1950 struct statement *stmt, *last_stmt;
1952 if (!expr)
1953 return NULL;
1954 if (expr->type == EXPR_PREOP && expr->op == '(')
1955 expr = expr->unop;
1956 if (expr->type != EXPR_STATEMENT)
1957 return orig;
1958 stmt = expr->statement;
1959 if (!stmt || stmt->type != STMT_COMPOUND)
1960 return orig;
1962 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1963 if (!last_stmt || last_stmt->type == STMT_LABEL)
1964 last_stmt = last_stmt->label_statement;
1965 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1966 return orig;
1967 return strip_expr(last_stmt->expression);
1970 static void call_return_state_hooks(struct expression *expr)
1972 struct returned_state_callback *cb;
1973 struct range_list *ret_rl;
1974 const char *return_ranges;
1975 int nr_states;
1976 sval_t sval;
1978 if (__path_is_null())
1979 return;
1981 expr = strip_expr(expr);
1982 expr = strip_expr_statement(expr);
1984 if (is_impossible_path())
1985 goto vanilla;
1987 if (expr && (expr->type == EXPR_COMPARE ||
1988 !get_implied_value(expr, &sval)) &&
1989 (is_condition(expr) || is_boolean(expr))) {
1990 call_return_state_hooks_compare(expr);
1991 return;
1992 } else if (is_conditional(expr)) {
1993 call_return_state_hooks_conditional(expr);
1994 return;
1995 } else if (call_return_state_hooks_split_possible(expr)) {
1996 return;
1997 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1998 return;
1999 } else if (call_return_state_hooks_split_success_fail(expr)) {
2000 return;
2001 } else if (splitable_function_call(expr)) {
2002 return;
2003 } else if (split_positive_from_negative(expr)) {
2004 return;
2005 } else if (split_by_bool_param(expr)) {
2006 } else if (split_by_null_nonnull_param(expr)) {
2007 return;
2010 vanilla:
2011 return_ranges = get_return_ranges_str(expr, &ret_rl);
2012 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2014 return_id++;
2015 nr_states = get_db_state_count();
2016 if (nr_states >= 10000) {
2017 match_return_info(return_id, (char *)return_ranges, expr);
2018 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2019 return;
2021 FOR_EACH_PTR(returned_state_callbacks, cb) {
2022 cb->callback(return_id, (char *)return_ranges, expr);
2023 } END_FOR_EACH_PTR(cb);
2026 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2028 struct returned_member_callback *cb;
2029 struct stree *stree;
2030 struct sm_state *sm;
2031 struct symbol *type;
2032 char *name;
2033 char member_name[256];
2034 int len;
2036 type = get_type(expr);
2037 if (!type || type->type != SYM_PTR)
2038 return;
2039 name = expr_to_var(expr);
2040 if (!name)
2041 return;
2043 member_name[sizeof(member_name) - 1] = '\0';
2044 strcpy(member_name, "$");
2046 len = strlen(name);
2047 FOR_EACH_PTR(returned_member_callbacks, cb) {
2048 stree = __get_cur_stree();
2049 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2050 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2051 strcpy(member_name, "*$");
2052 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2053 continue;
2055 if (strncmp(sm->name, name, len) != 0)
2056 continue;
2057 if (strncmp(sm->name + len, "->", 2) != 0)
2058 continue;
2059 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2060 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2061 } END_FOR_EACH_SM(sm);
2062 } END_FOR_EACH_PTR(cb);
2064 free_string(name);
2067 static void reset_memdb(struct symbol *sym)
2069 mem_sql(NULL, NULL, "delete from caller_info;");
2070 mem_sql(NULL, NULL, "delete from return_states;");
2071 mem_sql(NULL, NULL, "delete from call_implies;");
2072 mem_sql(NULL, NULL, "delete from return_implies;");
2075 static void match_end_func_info(struct symbol *sym)
2077 if (__path_is_null())
2078 return;
2079 call_return_state_hooks(NULL);
2082 static void match_after_func(struct symbol *sym)
2084 if (!__inline_fn)
2085 reset_memdb(sym);
2088 static void init_memdb(void)
2090 char *err = NULL;
2091 int rc;
2092 const char *schema_files[] = {
2093 "db/db.schema",
2094 "db/caller_info.schema",
2095 "db/common_caller_info.schema",
2096 "db/return_states.schema",
2097 "db/function_type_size.schema",
2098 "db/type_size.schema",
2099 "db/function_type_info.schema",
2100 "db/type_info.schema",
2101 "db/call_implies.schema",
2102 "db/return_implies.schema",
2103 "db/function_ptr.schema",
2104 "db/local_values.schema",
2105 "db/function_type_value.schema",
2106 "db/type_value.schema",
2107 "db/function_type.schema",
2108 "db/data_info.schema",
2109 "db/parameter_name.schema",
2110 "db/constraints.schema",
2111 "db/constraints_required.schema",
2112 "db/fn_ptr_data_link.schema",
2113 "db/fn_data_link.schema",
2114 "db/mtag_about.schema",
2115 "db/mtag_map.schema",
2116 "db/mtag_data.schema",
2117 "db/mtag_alias.schema",
2119 static char buf[4096];
2120 int fd;
2121 int ret;
2122 int i;
2124 rc = sqlite3_open(":memory:", &mem_db);
2125 if (rc != SQLITE_OK) {
2126 sm_ierror("starting In-Memory database.");
2127 return;
2130 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2131 fd = open_schema_file(schema_files[i]);
2132 if (fd < 0)
2133 continue;
2134 ret = read(fd, buf, sizeof(buf));
2135 if (ret < 0) {
2136 sm_ierror("failed to read: %s", schema_files[i]);
2137 continue;
2139 close(fd);
2140 if (ret == sizeof(buf)) {
2141 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2142 schema_files[i], sizeof(buf));
2143 continue;
2145 buf[ret] = '\0';
2146 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2147 if (rc != SQLITE_OK) {
2148 sm_ierror("SQL error #2: %s", err);
2149 sm_ierror("%s", buf);
2154 static void init_cachedb(void)
2156 char *err = NULL;
2157 int rc;
2158 const char *schema_files[] = {
2159 "db/call_implies.schema",
2160 "db/return_implies.schema",
2161 "db/type_info.schema",
2162 "db/mtag_data.schema",
2163 "db/sink_info.schema",
2165 static char buf[4096];
2166 int fd;
2167 int ret;
2168 int i;
2170 rc = sqlite3_open(":memory:", &cache_db);
2171 if (rc != SQLITE_OK) {
2172 sm_ierror("starting In-Memory database.");
2173 return;
2176 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2177 fd = open_schema_file(schema_files[i]);
2178 if (fd < 0)
2179 continue;
2180 ret = read(fd, buf, sizeof(buf));
2181 if (ret < 0) {
2182 sm_ierror("failed to read: %s", schema_files[i]);
2183 continue;
2185 close(fd);
2186 if (ret == sizeof(buf)) {
2187 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2188 schema_files[i], sizeof(buf));
2189 continue;
2191 buf[ret] = '\0';
2192 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2193 if (rc != SQLITE_OK) {
2194 sm_ierror("SQL error #2: %s", err);
2195 sm_ierror("%s", buf);
2200 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2202 static char buf[4096];
2203 char tmp[256];
2204 char *p = buf;
2205 char *table = _table;
2206 int i;
2209 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2210 for (i = 0; i < argc; i++) {
2211 if (i)
2212 p += snprintf(p, 4096 - (p - buf), ", ");
2213 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2214 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2217 p += snprintf(p, 4096 - (p - buf), ");");
2218 if (p - buf > 4096)
2219 return 0;
2221 sm_msg("SQL: %s", buf);
2222 return 0;
2225 static void dump_cache(struct symbol_list *sym_list)
2227 if (!option_info)
2228 return;
2229 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2230 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2231 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2232 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2233 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2236 void open_smatch_db(char *db_file)
2238 int rc;
2240 if (option_no_db)
2241 return;
2243 use_states = malloc(num_checks + 1);
2244 memset(use_states, 0xff, num_checks + 1);
2246 init_memdb();
2247 init_cachedb();
2249 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2250 if (rc != SQLITE_OK) {
2251 option_no_db = 1;
2252 return;
2254 run_sql(NULL, NULL,
2255 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2256 return;
2259 static void register_common_funcs(void)
2261 struct token *token;
2262 char *func;
2263 char filename[256];
2265 if (option_project == PROJ_NONE)
2266 strcpy(filename, "common_functions");
2267 else
2268 snprintf(filename, 256, "%s.common_functions", option_project_str);
2270 token = get_tokens_file(filename);
2271 if (!token)
2272 return;
2273 if (token_type(token) != TOKEN_STREAMBEGIN)
2274 return;
2275 token = token->next;
2276 while (token_type(token) != TOKEN_STREAMEND) {
2277 if (token_type(token) != TOKEN_IDENT)
2278 return;
2279 func = alloc_string(show_ident(token->ident));
2280 add_ptr_list(&common_funcs, func);
2281 token = token->next;
2283 clear_token_alloc();
2286 static char *get_next_string(char **str)
2288 static char string[256];
2289 char *start;
2290 char *p = *str;
2291 int len;
2293 if (*p == '\0')
2294 return NULL;
2295 start = p;
2297 while (*p != '\0' && *p != ' ' && *p != '\n')
2298 p++;
2300 len = p - start;
2301 if (len > 256) {
2302 memcpy(string, start, 255);
2303 string[255] = '\0';
2304 sm_ierror("return_fix: '%s' too long", string);
2305 **str = '\0';
2306 return NULL;
2308 memcpy(string, start, len);
2309 string[len] = '\0';
2310 if (*p != '\0')
2311 p++;
2312 *str = p;
2313 return string;
2316 static void register_return_replacements(void)
2318 char *func, *orig, *new;
2319 char filename[256];
2320 char buf[4096];
2321 int fd, ret, i;
2322 char *p;
2324 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2325 fd = open_schema_file(filename);
2326 if (fd < 0)
2327 return;
2328 ret = read(fd, buf, sizeof(buf));
2329 close(fd);
2330 if (ret < 0)
2331 return;
2332 if (ret == sizeof(buf)) {
2333 sm_ierror("file too large: %s (limit %zd bytes)",
2334 filename, sizeof(buf));
2335 return;
2337 buf[ret] = '\0';
2339 p = buf;
2340 while (*p) {
2341 get_next_string(&p);
2342 replace_count++;
2344 if (replace_count == 0 || replace_count % 3 != 0) {
2345 replace_count = 0;
2346 return;
2348 replace_table = malloc(replace_count * sizeof(char *));
2350 p = buf;
2351 i = 0;
2352 while (*p) {
2353 func = alloc_string(get_next_string(&p));
2354 orig = alloc_string(get_next_string(&p));
2355 new = alloc_string(get_next_string(&p));
2357 replace_table[i++] = func;
2358 replace_table[i++] = orig;
2359 replace_table[i++] = new;
2363 void register_definition_db_callbacks(int id)
2365 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2366 add_hook(&global_variable, BASE_HOOK);
2367 add_hook(&global_variable, DECLARATION_HOOK);
2368 add_split_return_callback(match_return_info);
2369 add_split_return_callback(print_returned_struct_members);
2370 add_hook(&call_return_state_hooks, RETURN_HOOK);
2371 add_hook(&match_end_func_info, END_FUNC_HOOK);
2372 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2374 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2375 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2376 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2378 register_common_funcs();
2379 register_return_replacements();
2381 add_hook(&dump_cache, END_FILE_HOOK);
2384 void register_db_call_marker(int id)
2386 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2389 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2391 struct expression *arg;
2392 char *name = NULL;
2393 char member_name[256];
2395 *sym = NULL;
2397 if (param == -1) {
2398 const char *star = "";
2400 if (expr->type != EXPR_ASSIGNMENT)
2401 return NULL;
2402 name = expr_to_var_sym(expr->left, sym);
2403 if (!name)
2404 return NULL;
2405 if (key[0] == '*') {
2406 star = "*";
2407 key++;
2409 if (strncmp(key, "$", 1) != 0)
2410 return name;
2411 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2412 free_string(name);
2413 return alloc_string(member_name);
2416 while (expr->type == EXPR_ASSIGNMENT)
2417 expr = strip_expr(expr->right);
2418 if (expr->type != EXPR_CALL)
2419 return NULL;
2421 arg = get_argument_from_call_expr(expr->args, param);
2422 if (!arg)
2423 return NULL;
2425 return get_variable_from_key(arg, key, sym);
2428 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2430 char buf[256];
2431 char *tmp;
2433 if (!arg)
2434 return NULL;
2436 arg = strip_expr(arg);
2438 if (strcmp(key, "$") == 0)
2439 return expr_to_var_sym(arg, sym);
2441 if (strcmp(key, "*$") == 0) {
2442 if (arg->type == EXPR_PREOP && arg->op == '&') {
2443 arg = strip_expr(arg->unop);
2444 return expr_to_var_sym(arg, sym);
2445 } else {
2446 tmp = expr_to_var_sym(arg, sym);
2447 if (!tmp)
2448 return NULL;
2449 snprintf(buf, sizeof(buf), "*%s", tmp);
2450 free_string(tmp);
2451 return alloc_string(buf);
2455 if (arg->type == EXPR_PREOP && arg->op == '&') {
2456 arg = strip_expr(arg->unop);
2457 tmp = expr_to_var_sym(arg, sym);
2458 if (!tmp)
2459 return NULL;
2460 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2461 return alloc_string(buf);
2464 tmp = expr_to_var_sym(arg, sym);
2465 if (!tmp)
2466 return NULL;
2467 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2468 free_string(tmp);
2469 return alloc_string(buf);
2472 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2474 *vsl = NULL;
2476 if (strcmp("$", key) == 0)
2477 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2478 return get_variable_from_key(arg, key, sym);
2481 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2483 int name_len;
2484 static char buf[256];
2486 name_len = strlen(param_name);
2488 if (strcmp(state_name, param_name) == 0) {
2489 return "$";
2490 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2491 strncmp(state_name, param_name, name_len) == 0) {
2492 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2493 return buf;
2494 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2495 return "*$";
2497 return NULL;
2500 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2502 if (!sym || !sym->ident)
2503 return NULL;
2505 return state_name_to_param_name(name, sym->ident->name);
2508 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2510 struct symbol *type;
2511 const char *sym_name;
2512 int name_len;
2513 static char buf[256];
2516 * mtag_name is different from param_name because mtags can be a struct
2517 * instead of a struct pointer. But we want to treat it like a pointer
2518 * because really an mtag is a pointer. Or in other words, if you pass
2519 * a struct foo then you want to talk about foo.bar but with an mtag
2520 * you want to refer to it as foo->bar.
2524 if (!sym || !sym->ident)
2525 return NULL;
2527 type = get_real_base_type(sym);
2528 if (type && type->type == SYM_BASETYPE)
2529 return "*$";
2531 sym_name = sym->ident->name;
2532 name_len = strlen(sym_name);
2534 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2535 strncmp(state_name, sym_name, name_len) == 0) {
2536 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2537 return buf;
2540 return state_name_to_param_name(state_name, sym_name);
2543 const char *get_mtag_name_expr(struct expression *expr)
2545 char *name;
2546 struct symbol *sym;
2547 const char *ret = NULL;
2549 name = expr_to_var_sym(expr, &sym);
2550 if (!name || !sym)
2551 goto free;
2553 ret = get_mtag_name_var_sym(name, sym);
2554 free:
2555 free_string(name);
2556 return ret;
2559 const char *get_param_name(struct sm_state *sm)
2561 return get_param_name_var_sym(sm->name, sm->sym);
2564 char *get_data_info_name(struct expression *expr)
2566 struct symbol *sym;
2567 char *name;
2568 char buf[256];
2569 char *ret = NULL;
2571 expr = strip_expr(expr);
2572 name = get_member_name(expr);
2573 if (name)
2574 return name;
2575 name = expr_to_var_sym(expr, &sym);
2576 if (!name || !sym)
2577 goto free;
2578 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2579 goto free;
2580 if (sym->ctype.modifiers & MOD_STATIC)
2581 snprintf(buf, sizeof(buf), "static %s", name);
2582 else
2583 snprintf(buf, sizeof(buf), "global %s", name);
2584 ret = alloc_sname(buf);
2585 free:
2586 free_string(name);
2587 return ret;