fix: spaces in macro definition on the command line
[smatch.git] / smatch_db.c
blob4f66938330e3e3e9ed98f79f8af90128796c0771
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(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 str;
97 if (j == sizeof(buf))
98 buf[j - 1] = '\0';
99 return (alloc_sname(buf));
102 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
104 char *err = NULL;
105 int rc;
107 if (!db)
108 return;
110 rc = sqlite3_exec(db, sql, callback, data, &err);
111 if (rc != SQLITE_OK && !parse_error) {
112 fprintf(stderr, "SQL error #2: %s\n", err);
113 fprintf(stderr, "SQL: '%s'\n", sql);
114 parse_error = 1;
118 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
120 int i;
122 for (i = 0; i < argc; i++) {
123 if (i != 0)
124 printf(", ");
125 sm_printf("%s", argv[i]);
127 sm_printf("\n");
128 return 0;
131 void debug_sql(struct sqlite3 *db, const char *sql)
133 if (!option_debug)
134 return;
135 sm_msg("%s", sql);
136 sql_exec(db, print_sql_output, NULL, sql);
139 static int replace_count;
140 static char **replace_table;
141 static const char *replace_return_ranges(const char *return_ranges)
143 int i;
145 if (!get_function()) {
146 /* I have no idea why EXPORT_SYMBOL() is here */
147 return return_ranges;
149 for (i = 0; i < replace_count; i += 3) {
150 if (strcmp(replace_table[i + 0], get_function()) == 0) {
151 if (strcmp(replace_table[i + 1], return_ranges) == 0)
152 return replace_table[i + 2];
155 return return_ranges;
159 static char *use_states;
160 static int get_db_state_count(void)
162 struct sm_state *sm;
163 int count = 0;
165 FOR_EACH_SM(__get_cur_stree(), sm) {
166 if (sm->owner == USHRT_MAX)
167 continue;
168 if (use_states[sm->owner])
169 count++;
170 } END_FOR_EACH_SM(sm);
171 return count;
174 void db_ignore_states(int id)
176 use_states[id] = 0;
179 void sql_insert_return_states(int return_id, const char *return_ranges,
180 int type, int param, const char *key, const char *value)
182 if (key && strlen(key) >= 80)
183 return;
184 return_ranges = replace_return_ranges(return_ranges);
185 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
186 get_base_file(), get_function(), (unsigned long)__inline_fn,
187 return_id, return_ranges, fn_static(), type, param, key, value);
190 static struct string_list *common_funcs;
191 static int is_common_function(const char *fn)
193 char *tmp;
195 if (!fn)
196 return 0;
198 if (strncmp(fn, "__builtin_", 10) == 0)
199 return 1;
201 FOR_EACH_PTR(common_funcs, tmp) {
202 if (strcmp(tmp, fn) == 0)
203 return 1;
204 } END_FOR_EACH_PTR(tmp);
206 return 0;
209 static char *function_signature(void)
211 return type_to_str(get_real_base_type(cur_func_sym));
214 void sql_insert_caller_info(struct expression *call, int type,
215 int param, const char *key, const char *value)
217 FILE *tmp_fd = sm_outfd;
218 char *fn;
220 if (!option_info && !__inline_call)
221 return;
223 if (key && strlen(key) >= 80)
224 return;
226 fn = get_fnptr_name(call->fn);
227 if (!fn)
228 return;
230 if (__inline_call) {
231 mem_sql(NULL, NULL,
232 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
233 get_base_file(), get_function(), fn, (unsigned long)call,
234 is_static(call->fn), type, param, key, value);
237 if (!option_info)
238 return;
240 if (strncmp(fn, "__builtin_", 10) == 0)
241 return;
242 if (type != INTERNAL && is_common_function(fn))
243 return;
245 sm_outfd = caller_info_fd;
246 sm_msg("SQL_caller_info: insert into caller_info values ("
247 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
248 get_base_file(), get_function(), fn, is_static(call->fn),
249 type, param, key, value);
250 sm_outfd = tmp_fd;
252 free_string(fn);
255 void sql_insert_function_ptr(const char *fn, const char *struct_name)
257 sql_insert(function_ptr, "'%s', '%s', '%s', 0", get_base_file(), fn,
258 struct_name);
261 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
263 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
264 get_base_file(), get_function(), (unsigned long)__inline_fn,
265 fn_static(), type, param, key, value);
268 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
270 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
271 get_base_file(), get_function(), (unsigned long)__inline_fn,
272 fn_static(), type, param, key, value);
275 void sql_insert_function_type_size(const char *member, const char *ranges)
277 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
280 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
282 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
285 void sql_insert_type_info(int type, const char *member, const char *value)
287 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
290 void sql_insert_local_values(const char *name, const char *value)
292 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
295 void sql_insert_function_type_value(const char *type, const char *value)
297 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
300 void sql_insert_function_type(int param, const char *value)
302 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
303 get_base_file(), get_function(), fn_static(), param, value);
306 void sql_insert_parameter_name(int param, const char *value)
308 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
309 get_base_file(), get_function(), fn_static(), param, value);
312 void sql_insert_data_info(struct expression *data, int type, const char *value)
314 char *data_name;
316 data_name = get_data_info_name(data);
317 if (!data_name)
318 return;
319 sql_insert(data_info, "'%s', '%s', %d, '%s'",
320 is_static(data) ? get_base_file() : "extern",
321 data_name, type, value);
324 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
326 sql_insert(data_info, "'%s', '%s', %d, '%s'",
327 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
328 var, type, value);
331 void sql_save_constraint(const char *con)
333 if (!option_info)
334 return;
336 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", con);
339 void sql_save_constraint_required(const char *data, int op, const char *limit)
341 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
344 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
346 if (!option_info)
347 return;
349 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
350 "select constraints_required.data, constraints_required.op, '%s' from "
351 "constraints_required where bound = '%s';", new_limit, old_limit);
354 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
356 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
359 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
361 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
362 return;
364 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
365 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
366 fn->symbol->ident->name,
367 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
368 type, param, key, value);
371 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
373 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
374 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
377 void sql_insert_mtag_data(mtag_t tag, const char *var, int offset, int type, const char *value)
379 sql_insert(mtag_data, "%lld, '%s', %d, %d, '%s'", tag, var, offset, type, value);
382 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
384 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
387 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
389 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
392 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
394 mtag_t *saved_tag = _tag;
395 mtag_t new_tag;
397 new_tag = strtoll(argv[0], NULL, 10);
399 if (!*saved_tag)
400 *saved_tag = new_tag;
401 else if (*saved_tag != new_tag)
402 *saved_tag = -1ULL;
404 return 0;
407 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
409 mtag_t tmp = 0;
411 run_sql(save_mtag, &tmp,
412 "select container from mtag_map where tag = %lld and offset = %d;",
413 tag, offset);
415 if (tmp == 0 || tmp == -1ULL)
416 return 0;
417 *container = tmp;
418 return 1;
421 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
423 mtag_t tmp = 0;
425 run_sql(save_mtag, &tmp,
426 "select tag from mtag_map where container = %lld and offset = %d;",
427 container, offset);
429 if (tmp == 0 || tmp == -1ULL)
430 return 0;
431 *tag = tmp;
432 return 1;
435 char *get_static_filter(struct symbol *sym)
437 static char sql_filter[1024];
439 /* This can only happen on buggy code. Return invalid SQL. */
440 if (!sym) {
441 sql_filter[0] = '\0';
442 return sql_filter;
445 if (sym->ctype.modifiers & MOD_STATIC) {
446 snprintf(sql_filter, sizeof(sql_filter),
447 "file = '%s' and function = '%s' and static = '1'",
448 get_base_file(), sym->ident->name);
449 } else {
450 snprintf(sql_filter, sizeof(sql_filter),
451 "function = '%s' and static = '0'", sym->ident->name);
454 return sql_filter;
457 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
459 int *row_count = _row_count;
461 *row_count = 0;
462 if (argc != 1)
463 return 0;
464 *row_count = atoi(argv[0]);
465 return 0;
468 static void mark_call_params_untracked(struct expression *call)
470 struct expression *arg;
471 int i = 0;
473 FOR_EACH_PTR(call->args, arg) {
474 mark_untracked(call, i++, "$", NULL);
475 } END_FOR_EACH_PTR(arg);
478 static void sql_select_return_states_pointer(const char *cols,
479 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
481 char *ptr;
482 int return_count = 0;
484 ptr = get_fnptr_name(call->fn);
485 if (!ptr)
486 return;
488 run_sql(get_row_count, &return_count,
489 "select count(*) from return_states join function_ptr "
490 "where return_states.function == function_ptr.function and "
491 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
492 /* The magic number 100 is just from testing on the kernel. */
493 if (return_count > 100) {
494 mark_call_params_untracked(call);
495 return;
498 run_sql(callback, info,
499 "select %s from return_states join function_ptr where "
500 "return_states.function == function_ptr.function and ptr = '%s' "
501 "and searchable = 1 "
502 "order by function_ptr.file, return_states.file, return_id, type;",
503 cols, ptr);
506 static int is_local_symbol(struct expression *expr)
508 if (expr->type != EXPR_SYMBOL)
509 return 0;
510 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
511 return 0;
512 return 1;
515 void sql_select_return_states(const char *cols, struct expression *call,
516 int (*callback)(void*, int, char**, char**), void *info)
518 int row_count = 0;
520 if (is_fake_call(call))
521 return;
523 if (call->fn->type != EXPR_SYMBOL || !call->fn->symbol || is_local_symbol(call->fn)) {
524 sql_select_return_states_pointer(cols, call, callback, info);
525 return;
528 if (inlinable(call->fn)) {
529 mem_sql(callback, info,
530 "select %s from return_states where call_id = '%lu' order by return_id, type;",
531 cols, (unsigned long)call);
532 return;
535 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
536 get_static_filter(call->fn->symbol));
537 if (row_count > 3000)
538 return;
540 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
541 cols, get_static_filter(call->fn->symbol));
544 #define CALL_IMPLIES 0
545 #define RETURN_IMPLIES 1
547 struct implies_info {
548 int type;
549 struct db_implies_cb_list *cb_list;
550 struct expression *expr;
551 struct symbol *sym;
554 void sql_select_implies(const char *cols, struct implies_info *info,
555 int (*callback)(void*, int, char**, char**))
557 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
558 mem_sql(callback, info,
559 "select %s from return_implies where call_id = '%lu';",
560 cols, (unsigned long)info->expr);
561 return;
564 run_sql(callback, info, "select %s from %s_implies where %s;",
565 cols,
566 info->type == CALL_IMPLIES ? "call" : "return",
567 get_static_filter(info->sym));
570 struct select_caller_info_data {
571 struct stree *final_states;
572 struct timeval start_time;
573 int prev_func_id;
574 int ignore;
575 int results;
578 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
580 static void sql_select_caller_info(struct select_caller_info_data *data,
581 const char *cols, struct symbol *sym)
583 if (__inline_fn) {
584 mem_sql(caller_info_callback, data,
585 "select %s from caller_info where call_id = %lu;",
586 cols, (unsigned long)__inline_fn);
587 return;
590 if (sym->ident->name && is_common_function(sym->ident->name))
591 return;
592 run_sql(caller_info_callback, data,
593 "select %s from common_caller_info where %s order by call_id;",
594 cols, get_static_filter(sym));
595 if (data->results)
596 return;
598 run_sql(caller_info_callback, data,
599 "select %s from caller_info where %s order by call_id;",
600 cols, get_static_filter(sym));
603 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
605 struct def_callback *def_callback = __alloc_def_callback(0);
607 def_callback->hook_type = type;
608 def_callback->callback = callback;
609 add_ptr_list(&select_caller_info_callbacks, def_callback);
613 * These call backs are used when the --info option is turned on to print struct
614 * member information. For example foo->bar could have a state in
615 * smatch_extra.c and also check_user.c.
617 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
619 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
621 member_callback->owner = owner;
622 member_callback->callback = callback;
623 add_ptr_list(&member_callbacks, member_callback);
626 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
628 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
630 callback->callback = fn;
631 add_ptr_list(&returned_state_callbacks, callback);
634 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))
636 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
638 member_callback->owner = owner;
639 member_callback->callback = callback;
640 add_ptr_list(&returned_member_callbacks, member_callback);
643 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
645 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
647 cb->type = type;
648 cb->callback = callback;
649 add_ptr_list(&call_implies_cb_list, cb);
652 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
654 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
656 cb->type = type;
657 cb->callback = callback;
658 add_ptr_list(&return_implies_cb_list, cb);
661 struct return_info {
662 struct expression *static_returns_call;
663 struct symbol *return_type;
664 struct range_list *return_range_list;
667 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
669 struct return_info *ret_info = _ret_info;
670 struct range_list *rl;
671 struct expression *call_expr = ret_info->static_returns_call;
673 if (argc != 1)
674 return 0;
675 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
676 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
677 return 0;
680 struct range_list *db_return_vals(struct expression *expr)
682 struct return_info ret_info = {};
683 char buf[64];
684 struct sm_state *sm;
686 if (is_fake_call(expr))
687 return NULL;
689 snprintf(buf, sizeof(buf), "return %p", expr);
690 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
691 if (sm)
692 return clone_rl(estate_rl(sm->state));
693 ret_info.static_returns_call = expr;
694 ret_info.return_type = get_type(expr);
695 if (!ret_info.return_type)
696 return NULL;
698 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
699 return NULL;
701 ret_info.return_range_list = NULL;
702 if (inlinable(expr->fn)) {
703 mem_sql(db_return_callback, &ret_info,
704 "select distinct return from return_states where call_id = '%lu';",
705 (unsigned long)expr);
706 } else {
707 run_sql(db_return_callback, &ret_info,
708 "select distinct return from return_states where %s;",
709 get_static_filter(expr->fn->symbol));
711 return ret_info.return_range_list;
714 struct range_list *db_return_vals_from_str(const char *fn_name)
716 struct return_info ret_info;
718 ret_info.static_returns_call = NULL;
719 ret_info.return_type = &llong_ctype;
720 ret_info.return_range_list = NULL;
722 run_sql(db_return_callback, &ret_info,
723 "select distinct return from return_states where function = '%s';",
724 fn_name);
725 return ret_info.return_range_list;
728 static void match_call_marker(struct expression *expr)
730 struct symbol *type;
732 type = get_type(expr->fn);
733 if (type && type->type == SYM_PTR)
734 type = get_real_base_type(type);
737 * we just want to record something in the database so that if we have
738 * two calls like: frob(4); frob(some_unkown); then on the receiving
739 * side we know that sometimes frob is called with unknown parameters.
742 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
745 static char *show_offset(int offset)
747 static char buf[64];
749 buf[0] = '\0';
750 if (offset != -1)
751 snprintf(buf, sizeof(buf), "(-%d)", offset);
752 return buf;
755 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
756 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
758 struct sm_state *sm;
759 char *name;
760 struct symbol *sym;
761 int len;
762 char printed_name[256];
763 int is_address = 0;
765 expr = strip_expr(expr);
766 if (!expr)
767 return;
768 if (expr->type == EXPR_PREOP && expr->op == '&') {
769 expr = strip_expr(expr->unop);
770 is_address = 1;
773 name = expr_to_var_sym(expr, &sym);
774 if (!name || !sym)
775 goto free;
777 len = strlen(name);
778 FOR_EACH_SM(stree, sm) {
779 if (sm->sym != sym)
780 continue;
781 if (strcmp(name, sm->name) == 0) {
782 if (is_address)
783 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
784 else /* these are already handled. fixme: handle them here */
785 continue;
786 } else if (sm->name[0] == '*' && strcmp(name, sm->name + 1) == 0) {
787 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
788 } else if (strncmp(name, sm->name, len) == 0) {
789 if (isalnum(sm->name[len]))
790 continue;
791 if (is_address)
792 snprintf(printed_name, sizeof(printed_name), "$%s->%s", show_offset(offset), sm->name + len + 1);
793 else
794 snprintf(printed_name, sizeof(printed_name), "$%s%s", show_offset(offset), sm->name + len);
795 } else {
796 continue;
798 callback(call, param, printed_name, sm);
799 } END_FOR_EACH_SM(sm);
800 free:
801 free_string(name);
804 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
806 char **container = _container;
807 static char buf[256];
809 snprintf(buf, sizeof(buf), "%s", argv[0]);
810 *container = buf;
811 return 0;
814 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
815 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
817 struct expression *tmp;
818 char *container = NULL;
819 int offset;
820 int holder_offset;
821 char *p;
823 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
824 return;
827 * We can't use the in-mem DB because we have to parse the function
828 * first, then we know if it takes a container, then we know to pass it
829 * the container data.
832 run_sql(&param_used_callback, &container,
833 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
834 get_static_filter(call->fn->symbol), CONTAINER, param);
835 if (!container)
836 return;
838 p = strchr(container, '-');
839 if (!p)
840 return;
841 offset = atoi(p);
842 p = strchr(p, ')');
843 if (!p)
844 return;
845 p++;
847 tmp = get_assigned_expr(expr);
848 if (tmp)
849 expr = tmp;
851 if (expr->type != EXPR_PREOP || expr->op != '&')
852 return;
853 expr = strip_expr(expr->unop);
854 holder_offset = get_member_offset_from_deref(expr);
855 if (-holder_offset != offset)
856 return;
858 expr = strip_expr(expr->deref);
859 if (expr->type == EXPR_PREOP && expr->op == '*')
860 expr = strip_expr(expr->unop);
862 print_struct_members(call, expr, param, holder_offset, stree, callback);
865 static void match_call_info(struct expression *call)
867 struct member_info_callback *cb;
868 struct expression *arg;
869 struct stree *stree;
870 char *name;
871 int i;
873 name = get_fnptr_name(call->fn);
874 if (!name)
875 return;
877 FOR_EACH_PTR(member_callbacks, cb) {
878 stree = get_all_states_stree(cb->owner);
879 i = 0;
880 FOR_EACH_PTR(call->args, arg) {
881 print_struct_members(call, arg, i, -1, stree, cb->callback);
882 print_container_struct_members(call, arg, i, stree, cb->callback);
883 i++;
884 } END_FOR_EACH_PTR(arg);
885 free_stree(&stree);
886 } END_FOR_EACH_PTR(cb);
888 free_string(name);
891 static int get_param(int param, char **name, struct symbol **sym)
893 struct symbol *arg;
894 int i;
896 i = 0;
897 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
899 * this is a temporary hack to work around a bug (I think in sparse?)
900 * 2.6.37-rc1:fs/reiserfs/journal.o
901 * If there is a function definition without parameter name found
902 * after a function implementation then it causes a crash.
903 * int foo() {}
904 * int bar(char *);
906 if (arg->ident->name < (char *)100)
907 continue;
908 if (i == param) {
909 *name = arg->ident->name;
910 *sym = arg;
911 return TRUE;
913 i++;
914 } END_FOR_EACH_PTR(arg);
916 return FALSE;
919 static int function_signature_matches(const char *sig)
921 char *my_sig;
923 my_sig = function_signature();
924 if (!sig || !my_sig)
925 return 1; /* default to matching */
926 if (strcmp(my_sig, sig) == 0)
927 return 1;
928 return 0;
931 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
933 struct select_caller_info_data *data = _data;
934 int func_id;
935 long type;
936 long param;
937 char *key;
938 char *value;
939 char *name = NULL;
940 struct symbol *sym = NULL;
941 struct def_callback *def_callback;
942 struct stree *stree;
943 struct timeval cur_time;
945 data->results = 1;
947 if (argc != 5)
948 return 0;
950 gettimeofday(&cur_time, NULL);
951 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
952 return 0;
954 func_id = atoi(argv[0]);
955 errno = 0;
956 type = strtol(argv[1], NULL, 10);
957 param = strtol(argv[2], NULL, 10);
958 if (errno)
959 return 0;
960 key = argv[3];
961 value = argv[4];
963 if (data->prev_func_id == -1)
964 data->prev_func_id = func_id;
965 if (func_id != data->prev_func_id) {
966 stree = __pop_fake_cur_stree();
967 if (!data->ignore)
968 merge_stree(&data->final_states, stree);
969 free_stree(&stree);
970 __push_fake_cur_stree();
971 __unnullify_path();
972 data->prev_func_id = func_id;
973 data->ignore = 0;
976 if (data->ignore)
977 return 0;
978 if (type == INTERNAL &&
979 !function_signature_matches(value)) {
980 data->ignore = 1;
981 return 0;
984 if (param >= 0 && !get_param(param, &name, &sym))
985 return 0;
987 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
988 if (def_callback->hook_type == type)
989 def_callback->callback(name, sym, key, value);
990 } END_FOR_EACH_PTR(def_callback);
992 return 0;
995 static struct string_list *ptr_names_done;
996 static struct string_list *ptr_names;
998 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1000 insert_string(&ptr_names, alloc_string(argv[0]));
1001 return 0;
1004 static char *get_next_ptr_name(void)
1006 char *ptr;
1008 FOR_EACH_PTR(ptr_names, ptr) {
1009 if (list_has_string(ptr_names_done, ptr))
1010 continue;
1011 insert_string(&ptr_names_done, ptr);
1012 return ptr;
1013 } END_FOR_EACH_PTR(ptr);
1014 return NULL;
1017 static void get_ptr_names(const char *file, const char *name)
1019 char sql_filter[1024];
1020 int before, after;
1022 if (file) {
1023 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1024 file, name);
1025 } else {
1026 snprintf(sql_filter, 1024, "function = '%s';", name);
1029 before = ptr_list_size((struct ptr_list *)ptr_names);
1031 run_sql(get_ptr_name, NULL,
1032 "select distinct ptr from function_ptr where %s",
1033 sql_filter);
1035 after = ptr_list_size((struct ptr_list *)ptr_names);
1036 if (before == after)
1037 return;
1039 while ((name = get_next_ptr_name()))
1040 get_ptr_names(NULL, name);
1043 static void match_data_from_db(struct symbol *sym)
1045 struct select_caller_info_data data = { .prev_func_id = -1 };
1046 struct sm_state *sm;
1047 struct stree *stree;
1048 struct timeval end_time;
1050 if (!sym || !sym->ident)
1051 return;
1053 gettimeofday(&data.start_time, NULL);
1055 __push_fake_cur_stree();
1056 __unnullify_path();
1058 if (!__inline_fn) {
1059 char *ptr;
1061 if (sym->ctype.modifiers & MOD_STATIC)
1062 get_ptr_names(get_base_file(), sym->ident->name);
1063 else
1064 get_ptr_names(NULL, sym->ident->name);
1066 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1067 __free_ptr_list((struct ptr_list **)&ptr_names);
1068 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1069 stree = __pop_fake_cur_stree();
1070 free_stree(&stree);
1071 return;
1074 sql_select_caller_info(&data,
1075 "call_id, type, parameter, key, value",
1076 sym);
1079 stree = __pop_fake_cur_stree();
1080 if (!data.ignore)
1081 merge_stree(&data.final_states, stree);
1082 free_stree(&stree);
1083 __push_fake_cur_stree();
1084 __unnullify_path();
1085 data.prev_func_id = -1;
1086 data.ignore = 0;
1088 FOR_EACH_PTR(ptr_names, ptr) {
1089 run_sql(caller_info_callback, &data,
1090 "select call_id, type, parameter, key, value"
1091 " from common_caller_info where function = '%s' order by call_id",
1092 ptr);
1093 } END_FOR_EACH_PTR(ptr);
1095 if (data.results) {
1096 FOR_EACH_PTR(ptr_names, ptr) {
1097 free_string(ptr);
1098 } END_FOR_EACH_PTR(ptr);
1099 goto free_ptr_names;
1102 FOR_EACH_PTR(ptr_names, ptr) {
1103 run_sql(caller_info_callback, &data,
1104 "select call_id, type, parameter, key, value"
1105 " from caller_info where function = '%s' order by call_id",
1106 ptr);
1107 free_string(ptr);
1108 } END_FOR_EACH_PTR(ptr);
1110 free_ptr_names:
1111 __free_ptr_list((struct ptr_list **)&ptr_names);
1112 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1113 } else {
1114 sql_select_caller_info(&data,
1115 "call_id, type, parameter, key, value",
1116 sym);
1119 stree = __pop_fake_cur_stree();
1120 if (!data.ignore)
1121 merge_stree(&data.final_states, stree);
1122 free_stree(&stree);
1124 gettimeofday(&end_time, NULL);
1125 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1126 FOR_EACH_SM(data.final_states, sm) {
1127 __set_sm(sm);
1128 } END_FOR_EACH_SM(sm);
1131 free_stree(&data.final_states);
1134 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1136 struct implies_info *info = _info;
1137 struct db_implies_callback *cb;
1138 struct expression *arg = NULL;
1139 int type;
1140 int param;
1142 if (argc != 5)
1143 return 0;
1145 type = atoi(argv[1]);
1146 param = atoi(argv[2]);
1148 FOR_EACH_PTR(info->cb_list, cb) {
1149 if (cb->type != type)
1150 continue;
1151 if (param != -1) {
1152 arg = get_argument_from_call_expr(info->expr->args, param);
1153 if (!arg)
1154 continue;
1156 cb->callback(info->expr, arg, argv[3], argv[4]);
1157 } END_FOR_EACH_PTR(cb);
1159 return 0;
1162 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1164 struct implies_info *info = _info;
1165 struct db_implies_callback *cb;
1166 struct expression *arg;
1167 struct symbol *sym;
1168 char *name;
1169 int type;
1170 int param;
1172 if (argc != 5)
1173 return 0;
1175 type = atoi(argv[1]);
1176 param = atoi(argv[2]);
1178 if (!get_param(param, &name, &sym))
1179 return 0;
1180 arg = symbol_expression(sym);
1181 if (!arg)
1182 return 0;
1184 FOR_EACH_PTR(info->cb_list, cb) {
1185 if (cb->type != type)
1186 continue;
1187 cb->callback(info->expr, arg, argv[3], argv[4]);
1188 } END_FOR_EACH_PTR(cb);
1190 return 0;
1193 static void match_return_implies(struct expression *expr)
1195 struct implies_info info = {
1196 .type = RETURN_IMPLIES,
1197 .cb_list = return_implies_cb_list,
1200 if (expr->fn->type != EXPR_SYMBOL ||
1201 !expr->fn->symbol)
1202 return;
1203 info.expr = expr;
1204 info.sym = expr->fn->symbol;
1205 sql_select_implies("function, type, parameter, key, value", &info,
1206 return_implies_callbacks);
1209 static void match_call_implies(struct symbol *sym)
1211 struct implies_info info = {
1212 .type = CALL_IMPLIES,
1213 .cb_list = call_implies_cb_list,
1216 if (!sym || !sym->ident)
1217 return;
1219 info.sym = sym;
1220 sql_select_implies("function, type, parameter, key, value", &info,
1221 call_implies_callbacks);
1224 static void print_initializer_list(struct expression_list *expr_list,
1225 struct symbol *struct_type)
1227 struct expression *expr;
1228 struct symbol *base_type;
1229 char struct_name[256];
1231 FOR_EACH_PTR(expr_list, expr) {
1232 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
1233 print_initializer_list(expr->idx_expression->expr_list, struct_type);
1234 continue;
1236 if (expr->type != EXPR_IDENTIFIER)
1237 continue;
1238 if (!expr->expr_ident)
1239 continue;
1240 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
1241 continue;
1242 base_type = get_type(expr->ident_expression);
1243 if (!base_type || base_type->type != SYM_FN)
1244 continue;
1245 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
1246 struct_type->ident->name, expr->expr_ident->name);
1247 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
1248 struct_name);
1249 } END_FOR_EACH_PTR(expr);
1252 static void global_variable(struct symbol *sym)
1254 struct symbol *struct_type;
1256 if (!sym->ident)
1257 return;
1258 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
1259 return;
1260 struct_type = get_base_type(sym);
1261 if (!struct_type)
1262 return;
1263 if (struct_type->type == SYM_ARRAY) {
1264 struct_type = get_base_type(struct_type);
1265 if (!struct_type)
1266 return;
1268 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
1269 return;
1270 print_initializer_list(sym->initializer->expr_list, struct_type);
1273 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1275 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1278 static void call_return_state_hooks_conditional(struct expression *expr)
1280 struct returned_state_callback *cb;
1281 struct range_list *rl;
1282 char *return_ranges;
1283 int final_pass_orig = final_pass;
1285 __push_fake_cur_stree();
1287 final_pass = 0;
1288 __split_whole_condition(expr->conditional);
1289 final_pass = final_pass_orig;
1291 if (get_implied_rl(expr->cond_true, &rl))
1292 rl = cast_rl(cur_func_return_type(), rl);
1293 else
1294 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_true)));
1295 return_ranges = show_rl(rl);
1296 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1298 return_id++;
1299 FOR_EACH_PTR(returned_state_callbacks, cb) {
1300 cb->callback(return_id, return_ranges, expr->cond_true);
1301 } END_FOR_EACH_PTR(cb);
1303 __push_true_states();
1304 __use_false_states();
1306 if (get_implied_rl(expr->cond_false, &rl))
1307 rl = cast_rl(cur_func_return_type(), rl);
1308 else
1309 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr->cond_false)));
1310 return_ranges = show_rl(rl);
1311 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1313 return_id++;
1314 FOR_EACH_PTR(returned_state_callbacks, cb) {
1315 cb->callback(return_id, return_ranges, expr->cond_false);
1316 } END_FOR_EACH_PTR(cb);
1318 __merge_true_states();
1319 __free_fake_cur_stree();
1322 static void call_return_state_hooks_compare(struct expression *expr)
1324 struct returned_state_callback *cb;
1325 char *return_ranges;
1326 int final_pass_orig = final_pass;
1327 sval_t sval = { .type = &int_ctype };
1328 sval_t ret;
1330 if (!get_implied_value(expr, &ret))
1331 ret.value = -1;
1333 __push_fake_cur_stree();
1335 final_pass = 0;
1336 __split_whole_condition(expr);
1337 final_pass = final_pass_orig;
1339 if (ret.value != 0) {
1340 return_ranges = alloc_sname("1");
1341 sval.value = 1;
1342 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1344 return_id++;
1345 FOR_EACH_PTR(returned_state_callbacks, cb) {
1346 cb->callback(return_id, return_ranges, expr);
1347 } END_FOR_EACH_PTR(cb);
1350 __push_true_states();
1351 __use_false_states();
1353 if (ret.value != 1) {
1354 return_ranges = alloc_sname("0");
1355 sval.value = 0;
1356 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1358 return_id++;
1359 FOR_EACH_PTR(returned_state_callbacks, cb) {
1360 cb->callback(return_id, return_ranges, expr);
1361 } END_FOR_EACH_PTR(cb);
1364 __merge_true_states();
1365 __free_fake_cur_stree();
1368 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1370 struct sm_state *tmp;
1372 FOR_EACH_PTR(slist, tmp) {
1373 if (strcmp(tmp->state->name, sm->state->name) == 0)
1374 return 1;
1375 } END_FOR_EACH_PTR(tmp);
1377 return 0;
1380 static char *get_return_compare_str(struct expression *expr)
1382 char *compare_str;
1383 char *var;
1384 char buf[256];
1385 int comparison;
1386 int param;
1388 compare_str = expr_lte_to_param(expr, -1);
1389 if (compare_str)
1390 return compare_str;
1391 param = get_param_num(expr);
1392 if (param < 0)
1393 return NULL;
1395 var = expr_to_var(expr);
1396 if (!var)
1397 return NULL;
1398 snprintf(buf, sizeof(buf), "%s orig", var);
1399 comparison = get_comparison_strings(var, buf);
1400 free_string(var);
1402 if (!comparison)
1403 return NULL;
1405 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1406 return alloc_sname(buf);
1409 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1411 struct returned_state_callback *cb;
1412 struct range_list *rl;
1413 char *return_ranges;
1414 struct sm_state *tmp;
1415 int ret = 0;
1416 int nr_possible, nr_states;
1417 char *compare_str = NULL;
1418 char buf[128];
1419 struct state_list *already_handled = NULL;
1421 if (!sm || !sm->merged)
1422 return 0;
1424 if (too_many_possible(sm))
1425 return 0;
1427 /* bail if it gets too complicated */
1428 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1429 nr_states = get_db_state_count();
1430 if (nr_states * nr_possible >= 2000)
1431 return 0;
1433 FOR_EACH_PTR(sm->possible, tmp) {
1434 if (tmp->merged)
1435 continue;
1436 if (ptr_in_list(tmp, already_handled))
1437 continue;
1438 add_ptr_list(&already_handled, tmp);
1440 ret = 1;
1441 __push_fake_cur_stree();
1443 overwrite_states_using_pool(sm, tmp);
1445 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1446 return_ranges = show_rl(rl);
1447 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1448 compare_str = get_return_compare_str(expr);
1449 if (compare_str) {
1450 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1451 return_ranges = alloc_sname(buf);
1454 return_id++;
1455 FOR_EACH_PTR(returned_state_callbacks, cb) {
1456 cb->callback(return_id, return_ranges, expr);
1457 } END_FOR_EACH_PTR(cb);
1459 __free_fake_cur_stree();
1460 } END_FOR_EACH_PTR(tmp);
1462 free_slist(&already_handled);
1464 return ret;
1467 static int call_return_state_hooks_split_possible(struct expression *expr)
1469 struct sm_state *sm;
1471 if (!expr || expr_equal_to_param(expr, -1))
1472 return 0;
1474 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1475 return split_possible_helper(sm, expr);
1478 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1480 struct range_list *rl;
1481 char *return_ranges;
1482 sval_t sval;
1483 char *compare_str;
1484 char *math_str;
1485 char buf[128];
1487 *rl_p = NULL;
1489 if (!expr)
1490 return alloc_sname("");
1492 if (get_implied_value(expr, &sval)) {
1493 sval = sval_cast(cur_func_return_type(), sval);
1494 *rl_p = alloc_rl(sval, sval);
1495 return sval_to_str(sval);
1498 compare_str = expr_equal_to_param(expr, -1);
1499 math_str = get_value_in_terms_of_parameter_math(expr);
1501 if (get_implied_rl(expr, &rl)) {
1502 rl = cast_rl(cur_func_return_type(), rl);
1503 return_ranges = show_rl(rl);
1504 } else if (get_imaginary_absolute(expr, &rl)){
1505 rl = cast_rl(cur_func_return_type(), rl);
1506 return alloc_sname(show_rl(rl));
1507 } else {
1508 rl = cast_rl(cur_func_return_type(), alloc_whole_rl(get_type(expr)));
1509 return_ranges = show_rl(rl);
1511 *rl_p = rl;
1513 if (compare_str) {
1514 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1515 return alloc_sname(buf);
1517 if (math_str) {
1518 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1519 return alloc_sname(buf);
1521 compare_str = get_return_compare_str(expr);
1522 if (compare_str) {
1523 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1524 return alloc_sname(buf);
1527 return return_ranges;
1530 static bool has_possible_negative(struct sm_state *sm)
1532 struct sm_state *tmp;
1534 FOR_EACH_PTR(sm->possible, tmp) {
1535 if (!estate_rl(tmp->state))
1536 continue;
1537 if (sval_is_negative(estate_min(tmp->state)) &&
1538 sval_is_negative(estate_max(tmp->state)))
1539 return true;
1540 } END_FOR_EACH_PTR(tmp);
1542 return false;
1545 static bool has_possible_zero_null(struct sm_state *sm)
1547 struct sm_state *tmp;
1548 sval_t sval;
1550 FOR_EACH_PTR(sm->possible, tmp) {
1551 if (!estate_get_single_value(tmp->state, &sval))
1552 continue;
1553 if (sval.value == 0)
1554 return true;
1555 } END_FOR_EACH_PTR(tmp);
1557 return false;
1560 static int split_positive_from_negative(struct expression *expr)
1562 struct sm_state *sm;
1563 struct returned_state_callback *cb;
1564 struct range_list *rl;
1565 const char *return_ranges;
1566 struct range_list *ret_rl;
1567 int undo;
1569 /* We're going to print the states 3 times */
1570 if (get_db_state_count() > 10000 / 3)
1571 return 0;
1573 if (!get_implied_rl(expr, &rl) || !rl)
1574 return 0;
1575 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1576 return 0;
1577 /* Forget about INT_MAX and larger */
1578 if (rl_max(rl).value <= 0)
1579 return 0;
1580 if (!sval_is_negative(rl_min(rl)))
1581 return 0;
1583 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1584 if (!sm)
1585 return 0;
1586 if (!has_possible_negative(sm))
1587 return 0;
1589 if (!assume(compare_expression(expr, '>', zero_expr())))
1590 return 0;
1592 return_id++;
1593 return_ranges = get_return_ranges_str(expr, &ret_rl);
1594 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1595 FOR_EACH_PTR(returned_state_callbacks, cb) {
1596 cb->callback(return_id, (char *)return_ranges, expr);
1597 } END_FOR_EACH_PTR(cb);
1599 end_assume();
1601 if (rl_has_sval(rl, sval_type_val(rl_type(rl), 0))) {
1602 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1604 return_id++;
1605 return_ranges = get_return_ranges_str(expr, &ret_rl);
1606 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1607 FOR_EACH_PTR(returned_state_callbacks, cb) {
1608 cb->callback(return_id, (char *)return_ranges, expr);
1609 } END_FOR_EACH_PTR(cb);
1611 if (undo)
1612 end_assume();
1615 undo = assume(compare_expression(expr, '<', zero_expr()));
1617 return_id++;
1618 return_ranges = get_return_ranges_str(expr, &ret_rl);
1619 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1620 FOR_EACH_PTR(returned_state_callbacks, cb) {
1621 cb->callback(return_id, (char *)return_ranges, expr);
1622 } END_FOR_EACH_PTR(cb);
1624 if (undo)
1625 end_assume();
1627 return 1;
1630 static int call_return_state_hooks_split_null_non_null(struct expression *expr)
1632 struct returned_state_callback *cb;
1633 struct range_list *rl;
1634 struct range_list *nonnull_rl;
1635 sval_t null_sval;
1636 struct range_list *null_rl = NULL;
1637 char *return_ranges;
1638 struct sm_state *sm;
1639 struct smatch_state *state;
1640 int nr_states;
1641 int final_pass_orig = final_pass;
1643 if (!expr || expr_equal_to_param(expr, -1))
1644 return 0;
1645 if (expr->type == EXPR_CALL)
1646 return 0;
1647 if (!is_pointer(expr))
1648 return 0;
1650 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1651 if (!sm)
1652 return 0;
1653 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1654 return 0;
1655 state = sm->state;
1656 if (!estate_rl(state))
1657 return 0;
1658 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1659 return 0;
1660 if (!has_possible_zero_null(sm))
1661 return 0;
1663 nr_states = get_db_state_count();
1664 if (option_info && nr_states >= 1500)
1665 return 0;
1667 rl = estate_rl(state);
1669 __push_fake_cur_stree();
1671 final_pass = 0;
1672 __split_whole_condition(expr);
1673 final_pass = final_pass_orig;
1675 nonnull_rl = rl_filter(rl, rl_zero());
1676 return_ranges = show_rl(nonnull_rl);
1677 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1679 return_id++;
1680 FOR_EACH_PTR(returned_state_callbacks, cb) {
1681 cb->callback(return_id, return_ranges, expr);
1682 } END_FOR_EACH_PTR(cb);
1684 __push_true_states();
1685 __use_false_states();
1687 return_ranges = alloc_sname("0");
1688 null_sval = sval_type_val(rl_type(rl), 0);
1689 add_range(&null_rl, null_sval, null_sval);
1690 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1691 return_id++;
1692 FOR_EACH_PTR(returned_state_callbacks, cb) {
1693 cb->callback(return_id, return_ranges, expr);
1694 } END_FOR_EACH_PTR(cb);
1696 __merge_true_states();
1697 __free_fake_cur_stree();
1699 return 1;
1702 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1704 struct sm_state *sm;
1705 struct range_list *rl;
1706 struct range_list *nonzero_rl;
1707 sval_t zero_sval;
1708 struct range_list *zero_rl = NULL;
1709 int nr_states;
1710 struct returned_state_callback *cb;
1711 char *return_ranges;
1712 int final_pass_orig = final_pass;
1714 if (option_project != PROJ_KERNEL)
1715 return 0;
1717 nr_states = get_db_state_count();
1718 if (nr_states > 1500)
1719 return 0;
1721 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1722 if (!sm)
1723 return 0;
1724 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1725 return 0;
1727 rl = estate_rl(sm->state);
1728 if (!rl)
1729 return 0;
1731 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1732 return 0;
1733 if (rl_max(rl).value != 0)
1734 return 0;
1735 if (!has_possible_zero_null(sm))
1736 return 0;
1738 __push_fake_cur_stree();
1740 final_pass = 0;
1741 __split_whole_condition(expr);
1742 final_pass = final_pass_orig;
1744 nonzero_rl = rl_filter(rl, rl_zero());
1745 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1746 return_ranges = show_rl(nonzero_rl);
1747 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1749 return_id++;
1750 FOR_EACH_PTR(returned_state_callbacks, cb) {
1751 cb->callback(return_id, return_ranges, expr);
1752 } END_FOR_EACH_PTR(cb);
1754 __push_true_states();
1755 __use_false_states();
1757 return_ranges = alloc_sname("0");
1758 zero_sval = sval_type_val(rl_type(rl), 0);
1759 add_range(&zero_rl, zero_sval, zero_sval);
1760 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1761 return_id++;
1762 FOR_EACH_PTR(returned_state_callbacks, cb) {
1763 cb->callback(return_id, return_ranges, expr);
1764 } END_FOR_EACH_PTR(cb);
1766 __merge_true_states();
1767 __free_fake_cur_stree();
1769 return 1;
1772 static int is_boolean(struct expression *expr)
1774 struct range_list *rl;
1776 if (!get_implied_rl(expr, &rl))
1777 return 0;
1778 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1779 return 1;
1780 return 0;
1783 static int is_conditional(struct expression *expr)
1785 if (!expr)
1786 return 0;
1787 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1788 return 1;
1789 return 0;
1792 static int splitable_function_call(struct expression *expr)
1794 struct sm_state *sm;
1795 char buf[64];
1797 if (!expr || expr->type != EXPR_CALL)
1798 return 0;
1799 snprintf(buf, sizeof(buf), "return %p", expr);
1800 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1801 return split_possible_helper(sm, expr);
1804 static struct sm_state *find_bool_param(void)
1806 struct stree *start_states;
1807 struct symbol *arg;
1808 struct sm_state *sm, *tmp;
1809 sval_t sval;
1811 start_states = get_start_states();
1813 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1814 if (!arg->ident)
1815 continue;
1816 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1817 if (!sm)
1818 continue;
1819 if (rl_min(estate_rl(sm->state)).value != 0 ||
1820 rl_max(estate_rl(sm->state)).value != 1)
1821 continue;
1822 goto found;
1823 } END_FOR_EACH_PTR_REVERSE(arg);
1825 return NULL;
1827 found:
1829 * Check if it's splitable. If not, then splitting it up is likely not
1830 * useful for the callers.
1832 FOR_EACH_PTR(sm->possible, tmp) {
1833 if (is_merged(tmp))
1834 continue;
1835 if (!estate_get_single_value(tmp->state, &sval))
1836 return NULL;
1837 } END_FOR_EACH_PTR(tmp);
1839 return sm;
1842 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1844 struct returned_state_callback *cb;
1845 struct range_list *ret_rl;
1846 const char *return_ranges;
1847 struct sm_state *tmp;
1848 int ret = 0;
1849 int nr_possible, nr_states;
1850 char *compare_str = NULL;
1851 char buf[128];
1852 struct state_list *already_handled = NULL;
1854 if (!sm || !sm->merged)
1855 return 0;
1857 if (too_many_possible(sm))
1858 return 0;
1860 /* bail if it gets too complicated */
1861 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1862 nr_states = get_db_state_count();
1863 if (nr_states * nr_possible >= 2000)
1864 return 0;
1866 FOR_EACH_PTR(sm->possible, tmp) {
1867 if (tmp->merged)
1868 continue;
1869 if (ptr_in_list(tmp, already_handled))
1870 continue;
1871 add_ptr_list(&already_handled, tmp);
1873 ret = 1;
1874 __push_fake_cur_stree();
1876 overwrite_states_using_pool(sm, tmp);
1878 return_ranges = get_return_ranges_str(expr, &ret_rl);
1879 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1880 compare_str = get_return_compare_str(expr);
1881 if (compare_str) {
1882 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1883 return_ranges = alloc_sname(buf);
1886 return_id++;
1887 FOR_EACH_PTR(returned_state_callbacks, cb) {
1888 cb->callback(return_id, (char *)return_ranges, expr);
1889 } END_FOR_EACH_PTR(cb);
1891 __free_fake_cur_stree();
1892 } END_FOR_EACH_PTR(tmp);
1894 free_slist(&already_handled);
1896 return ret;
1899 static int split_by_bool_param(struct expression *expr)
1901 struct sm_state *start_sm, *sm;
1902 sval_t sval;
1904 start_sm = find_bool_param();
1905 if (!start_sm)
1906 return 0;
1907 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1908 if (!sm || estate_get_single_value(sm->state, &sval))
1909 return 0;
1910 return split_on_bool_sm(sm, expr);
1913 static int split_by_null_nonnull_param(struct expression *expr)
1915 struct symbol *arg;
1916 struct sm_state *sm;
1917 sval_t zero = {
1918 .type = &ulong_ctype,
1921 /* function must only take one pointer */
1922 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1923 return 0;
1924 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1925 if (!arg->ident)
1926 return 0;
1927 if (get_real_base_type(arg)->type != SYM_PTR)
1928 return 0;
1930 if (param_was_set_var_sym(arg->ident->name, arg))
1931 return 0;
1932 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1933 if (!sm)
1934 return 0;
1936 if (!rl_has_sval(estate_rl(sm->state), zero))
1937 return 0;
1939 return split_on_bool_sm(sm, expr);
1942 struct expression *strip_expr_statement(struct expression *expr)
1944 struct expression *orig = expr;
1945 struct statement *stmt, *last_stmt;
1947 if (!expr)
1948 return NULL;
1949 if (expr->type == EXPR_PREOP && expr->op == '(')
1950 expr = expr->unop;
1951 if (expr->type != EXPR_STATEMENT)
1952 return orig;
1953 stmt = expr->statement;
1954 if (!stmt || stmt->type != STMT_COMPOUND)
1955 return orig;
1957 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1958 if (!last_stmt || last_stmt->type == STMT_LABEL)
1959 last_stmt = last_stmt->label_statement;
1960 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
1961 return orig;
1962 return strip_expr(last_stmt->expression);
1965 static void call_return_state_hooks(struct expression *expr)
1967 struct returned_state_callback *cb;
1968 struct range_list *ret_rl;
1969 const char *return_ranges;
1970 int nr_states;
1971 sval_t sval;
1973 if (__path_is_null())
1974 return;
1976 expr = strip_expr(expr);
1977 expr = strip_expr_statement(expr);
1979 if (is_impossible_path())
1980 goto vanilla;
1982 if (expr && (expr->type == EXPR_COMPARE ||
1983 !get_implied_value(expr, &sval)) &&
1984 (is_condition(expr) || is_boolean(expr))) {
1985 call_return_state_hooks_compare(expr);
1986 return;
1987 } else if (is_conditional(expr)) {
1988 call_return_state_hooks_conditional(expr);
1989 return;
1990 } else if (call_return_state_hooks_split_possible(expr)) {
1991 return;
1992 } else if (call_return_state_hooks_split_null_non_null(expr)) {
1993 return;
1994 } else if (call_return_state_hooks_split_success_fail(expr)) {
1995 return;
1996 } else if (splitable_function_call(expr)) {
1997 return;
1998 } else if (split_positive_from_negative(expr)) {
1999 return;
2000 } else if (split_by_bool_param(expr)) {
2001 } else if (split_by_null_nonnull_param(expr)) {
2002 return;
2005 vanilla:
2006 return_ranges = get_return_ranges_str(expr, &ret_rl);
2007 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2009 return_id++;
2010 nr_states = get_db_state_count();
2011 if (nr_states >= 10000) {
2012 match_return_info(return_id, (char *)return_ranges, expr);
2013 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2014 return;
2016 FOR_EACH_PTR(returned_state_callbacks, cb) {
2017 cb->callback(return_id, (char *)return_ranges, expr);
2018 } END_FOR_EACH_PTR(cb);
2021 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2023 struct returned_member_callback *cb;
2024 struct stree *stree;
2025 struct sm_state *sm;
2026 struct symbol *type;
2027 char *name;
2028 char member_name[256];
2029 int len;
2031 type = get_type(expr);
2032 if (!type || type->type != SYM_PTR)
2033 return;
2034 name = expr_to_var(expr);
2035 if (!name)
2036 return;
2038 member_name[sizeof(member_name) - 1] = '\0';
2039 strcpy(member_name, "$");
2041 len = strlen(name);
2042 FOR_EACH_PTR(returned_member_callbacks, cb) {
2043 stree = __get_cur_stree();
2044 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2045 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2046 strcpy(member_name, "*$");
2047 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2048 continue;
2050 if (strncmp(sm->name, name, len) != 0)
2051 continue;
2052 if (strncmp(sm->name + len, "->", 2) != 0)
2053 continue;
2054 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2055 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2056 } END_FOR_EACH_SM(sm);
2057 } END_FOR_EACH_PTR(cb);
2059 free_string(name);
2062 static void reset_memdb(struct symbol *sym)
2064 mem_sql(NULL, NULL, "delete from caller_info;");
2065 mem_sql(NULL, NULL, "delete from return_states;");
2066 mem_sql(NULL, NULL, "delete from call_implies;");
2067 mem_sql(NULL, NULL, "delete from return_implies;");
2070 static void match_end_func_info(struct symbol *sym)
2072 if (__path_is_null())
2073 return;
2074 call_return_state_hooks(NULL);
2077 static void match_after_func(struct symbol *sym)
2079 if (!__inline_fn)
2080 reset_memdb(sym);
2083 static void init_memdb(void)
2085 char *err = NULL;
2086 int rc;
2087 const char *schema_files[] = {
2088 "db/db.schema",
2089 "db/caller_info.schema",
2090 "db/common_caller_info.schema",
2091 "db/return_states.schema",
2092 "db/function_type_size.schema",
2093 "db/type_size.schema",
2094 "db/function_type_info.schema",
2095 "db/type_info.schema",
2096 "db/call_implies.schema",
2097 "db/return_implies.schema",
2098 "db/function_ptr.schema",
2099 "db/local_values.schema",
2100 "db/function_type_value.schema",
2101 "db/type_value.schema",
2102 "db/function_type.schema",
2103 "db/data_info.schema",
2104 "db/parameter_name.schema",
2105 "db/constraints.schema",
2106 "db/constraints_required.schema",
2107 "db/fn_ptr_data_link.schema",
2108 "db/fn_data_link.schema",
2109 "db/mtag_about.schema",
2110 "db/mtag_map.schema",
2111 "db/mtag_data.schema",
2112 "db/mtag_alias.schema",
2114 static char buf[4096];
2115 int fd;
2116 int ret;
2117 int i;
2119 rc = sqlite3_open(":memory:", &mem_db);
2120 if (rc != SQLITE_OK) {
2121 printf("Error starting In-Memory database.");
2122 return;
2125 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2126 fd = open_schema_file(schema_files[i]);
2127 if (fd < 0)
2128 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 continue;
2175 ret = read(fd, buf, sizeof(buf));
2176 if (ret < 0) {
2177 printf("failed to read: %s\n", schema_files[i]);
2178 continue;
2180 close(fd);
2181 if (ret == sizeof(buf)) {
2182 printf("Schema file too large: %s (limit %zd bytes)",
2183 schema_files[i], sizeof(buf));
2184 continue;
2186 buf[ret] = '\0';
2187 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2188 if (rc != SQLITE_OK) {
2189 fprintf(stderr, "SQL error #2: %s\n", err);
2190 fprintf(stderr, "%s\n", buf);
2195 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2197 static char buf[4096];
2198 char tmp[256];
2199 char *p = buf;
2200 char *table = _table;
2201 int i;
2204 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2205 for (i = 0; i < argc; i++) {
2206 if (i)
2207 p += snprintf(p, 4096 - (p - buf), ", ");
2208 sqlite3_snprintf(sizeof(tmp), tmp, "%q", argv[i]);
2209 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2212 p += snprintf(p, 4096 - (p - buf), ");");
2213 if (p - buf > 4096)
2214 return 0;
2216 sm_msg("SQL: %s", buf);
2217 return 0;
2220 static void dump_cache(struct symbol_list *sym_list)
2222 if (!option_info)
2223 return;
2224 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2225 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2226 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2227 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2228 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2231 void open_smatch_db(char *db_file)
2233 int rc;
2235 if (option_no_db)
2236 return;
2238 use_states = malloc(num_checks + 1);
2239 memset(use_states, 0xff, num_checks + 1);
2241 init_memdb();
2242 init_cachedb();
2244 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2245 if (rc != SQLITE_OK) {
2246 option_no_db = 1;
2247 return;
2249 run_sql(NULL, NULL,
2250 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2251 return;
2254 static void register_common_funcs(void)
2256 struct token *token;
2257 char *func;
2258 char filename[256];
2260 if (option_project == PROJ_NONE)
2261 strcpy(filename, "common_functions");
2262 else
2263 snprintf(filename, 256, "%s.common_functions", option_project_str);
2265 token = get_tokens_file(filename);
2266 if (!token)
2267 return;
2268 if (token_type(token) != TOKEN_STREAMBEGIN)
2269 return;
2270 token = token->next;
2271 while (token_type(token) != TOKEN_STREAMEND) {
2272 if (token_type(token) != TOKEN_IDENT)
2273 return;
2274 func = alloc_string(show_ident(token->ident));
2275 add_ptr_list(&common_funcs, func);
2276 token = token->next;
2278 clear_token_alloc();
2281 static char *get_next_string(char **str)
2283 static char string[256];
2284 char *start;
2285 char *p = *str;
2286 int len;
2288 if (*p == '\0')
2289 return NULL;
2290 start = p;
2292 while (*p != '\0' && *p != ' ' && *p != '\n')
2293 p++;
2295 len = p - start;
2296 if (len > 256) {
2297 memcpy(string, start, 255);
2298 string[255] = '\0';
2299 printf("return_fix: '%s' too long", string);
2300 **str = '\0';
2301 return NULL;
2303 memcpy(string, start, len);
2304 string[len] = '\0';
2305 if (*p != '\0')
2306 p++;
2307 *str = p;
2308 return string;
2311 static void register_return_replacements(void)
2313 char *func, *orig, *new;
2314 char filename[256];
2315 char buf[4096];
2316 int fd, ret, i;
2317 char *p;
2319 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2320 fd = open_schema_file(filename);
2321 if (fd < 0)
2322 return;
2323 ret = read(fd, buf, sizeof(buf));
2324 close(fd);
2325 if (ret < 0)
2326 return;
2327 if (ret == sizeof(buf)) {
2328 printf("file too large: %s (limit %zd bytes)",
2329 filename, sizeof(buf));
2330 return;
2332 buf[ret] = '\0';
2334 p = buf;
2335 while (*p) {
2336 get_next_string(&p);
2337 replace_count++;
2339 if (replace_count == 0 || replace_count % 3 != 0) {
2340 replace_count = 0;
2341 return;
2343 replace_table = malloc(replace_count * sizeof(char *));
2345 p = buf;
2346 i = 0;
2347 while (*p) {
2348 func = alloc_string(get_next_string(&p));
2349 orig = alloc_string(get_next_string(&p));
2350 new = alloc_string(get_next_string(&p));
2352 replace_table[i++] = func;
2353 replace_table[i++] = orig;
2354 replace_table[i++] = new;
2358 void register_definition_db_callbacks(int id)
2360 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2361 add_hook(&global_variable, BASE_HOOK);
2362 add_hook(&global_variable, DECLARATION_HOOK);
2363 add_split_return_callback(match_return_info);
2364 add_split_return_callback(print_returned_struct_members);
2365 add_hook(&call_return_state_hooks, RETURN_HOOK);
2366 add_hook(&match_end_func_info, END_FUNC_HOOK);
2367 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2369 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2370 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2371 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2373 register_common_funcs();
2374 register_return_replacements();
2376 add_hook(&dump_cache, END_FILE_HOOK);
2379 void register_db_call_marker(int id)
2381 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2384 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2386 struct expression *arg;
2387 char *name = NULL;
2388 char member_name[256];
2390 *sym = NULL;
2392 if (param == -1) {
2393 const char *star = "";
2395 if (expr->type != EXPR_ASSIGNMENT)
2396 return NULL;
2397 name = expr_to_var_sym(expr->left, sym);
2398 if (!name)
2399 return NULL;
2400 if (key[0] == '*') {
2401 star = "*";
2402 key++;
2404 if (strncmp(key, "$", 1) != 0)
2405 return name;
2406 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2407 free_string(name);
2408 return alloc_string(member_name);
2411 while (expr->type == EXPR_ASSIGNMENT)
2412 expr = strip_expr(expr->right);
2413 if (expr->type != EXPR_CALL)
2414 return NULL;
2416 arg = get_argument_from_call_expr(expr->args, param);
2417 if (!arg)
2418 return NULL;
2420 return get_variable_from_key(arg, key, sym);
2423 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2425 char buf[256];
2426 char *tmp;
2428 if (!arg)
2429 return NULL;
2431 arg = strip_expr(arg);
2433 if (strcmp(key, "$") == 0)
2434 return expr_to_var_sym(arg, sym);
2436 if (strcmp(key, "*$") == 0) {
2437 if (arg->type == EXPR_PREOP && arg->op == '&') {
2438 arg = strip_expr(arg->unop);
2439 return expr_to_var_sym(arg, sym);
2440 } else {
2441 tmp = expr_to_var_sym(arg, sym);
2442 if (!tmp)
2443 return NULL;
2444 snprintf(buf, sizeof(buf), "*%s", tmp);
2445 free_string(tmp);
2446 return alloc_string(buf);
2450 if (arg->type == EXPR_PREOP && arg->op == '&') {
2451 arg = strip_expr(arg->unop);
2452 tmp = expr_to_var_sym(arg, sym);
2453 if (!tmp)
2454 return NULL;
2455 snprintf(buf, sizeof(buf), "%s.%s", tmp, key + 3);
2456 return alloc_string(buf);
2459 tmp = expr_to_var_sym(arg, sym);
2460 if (!tmp)
2461 return NULL;
2462 snprintf(buf, sizeof(buf), "%s%s", tmp, key + 1);
2463 free_string(tmp);
2464 return alloc_string(buf);
2467 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2469 *vsl = NULL;
2471 if (strcmp("$", key) == 0)
2472 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2473 return get_variable_from_key(arg, key, sym);
2476 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2478 int name_len;
2479 static char buf[256];
2481 name_len = strlen(param_name);
2483 if (strcmp(state_name, param_name) == 0) {
2484 return "$";
2485 } else if (state_name[name_len] == '-' && /* check for '-' from "->" */
2486 strncmp(state_name, param_name, name_len) == 0) {
2487 snprintf(buf, sizeof(buf), "$%s", state_name + name_len);
2488 return buf;
2489 } else if (state_name[0] == '*' && strcmp(state_name + 1, param_name) == 0) {
2490 return "*$";
2492 return NULL;
2495 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2497 if (!sym || !sym->ident)
2498 return NULL;
2500 return state_name_to_param_name(name, sym->ident->name);
2503 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2505 struct symbol *type;
2506 const char *sym_name;
2507 int name_len;
2508 static char buf[256];
2511 * mtag_name is different from param_name because mtags can be a struct
2512 * instead of a struct pointer. But we want to treat it like a pointer
2513 * because really an mtag is a pointer. Or in other words, if you pass
2514 * a struct foo then you want to talk about foo.bar but with an mtag
2515 * you want to refer to it as foo->bar.
2519 if (!sym || !sym->ident)
2520 return NULL;
2522 type = get_real_base_type(sym);
2523 if (type && type->type == SYM_BASETYPE)
2524 return "*$";
2526 sym_name = sym->ident->name;
2527 name_len = strlen(sym_name);
2529 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2530 strncmp(state_name, sym_name, name_len) == 0) {
2531 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2532 return buf;
2535 return state_name_to_param_name(state_name, sym_name);
2538 const char *get_mtag_name_expr(struct expression *expr)
2540 char *name;
2541 struct symbol *sym;
2542 const char *ret = NULL;
2544 name = expr_to_var_sym(expr, &sym);
2545 if (!name || !sym)
2546 goto free;
2548 ret = get_mtag_name_var_sym(name, sym);
2549 free:
2550 free_string(name);
2551 return ret;
2554 const char *get_param_name(struct sm_state *sm)
2556 return get_param_name_var_sym(sm->name, sm->sym);
2559 char *get_data_info_name(struct expression *expr)
2561 struct symbol *sym;
2562 char *name;
2563 char buf[256];
2564 char *ret = NULL;
2566 expr = strip_expr(expr);
2567 name = get_member_name(expr);
2568 if (name)
2569 return name;
2570 name = expr_to_var_sym(expr, &sym);
2571 if (!name || !sym)
2572 goto free;
2573 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2574 goto free;
2575 if (sym->ctype.modifiers & MOD_STATIC)
2576 snprintf(buf, sizeof(buf), "static %s", name);
2577 else
2578 snprintf(buf, sizeof(buf), "global %s", name);
2579 ret = alloc_sname(buf);
2580 free:
2581 free_string(name);
2582 return ret;