helper: don't strip casting in get_array_offset()
[smatch.git] / smatch_param_limit.c
blobd4088e84edfb2ad7f945ee191927c3171f82111e
1 /*
2 * Copyright (C) 2012 Oracle.
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
19 * This is almost the same as smatch_param_filter.c. The difference is that
20 * this only deals with values passed on the stack and param filter only deals
21 * with values changed so that the caller sees the new value. It other words
22 * the key for these should always be "$" and the key for param_filter should
23 * never be "$". Also smatch_param_set() should never use "$" as the key.
24 * Param set should work together with param_filter to determine the value that
25 * the caller sees at the end.
27 * This is for functions like this:
29 * int foo(int a)
30 * {
31 * if (a >= 0 && a < 10) {
32 * a = 42;
33 * return 1;
34 * }
35 * return 0;
36 * }
38 * If we pass in 5, it returns 1.
40 * It's a bit complicated because we can't just consider the final value, we
41 * have to always consider the passed in value.
45 #include "scope.h"
46 #include "smatch.h"
47 #include "smatch_extra.h"
48 #include "smatch_slist.h"
50 static int my_id;
52 static struct stree *start_states;
53 static struct stree_stack *saved_stack;
55 static void save_start_states(struct statement *stmt)
57 struct smatch_state *state;
58 struct symbol *tmp;
60 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
61 if (!tmp->ident)
62 continue;
63 state = get_state(SMATCH_EXTRA, tmp->ident->name, tmp);
64 if (!state)
65 state = alloc_estate_whole(get_real_base_type(tmp));
66 set_state_stree(&start_states, SMATCH_EXTRA, tmp->ident->name, tmp, state);
67 } END_FOR_EACH_PTR(tmp);
70 static void free_start_states(void)
72 free_stree(&start_states);
75 static struct smatch_state *unmatched_state(struct sm_state *sm)
77 struct smatch_state *state;
79 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
80 if (state)
81 return state;
82 return alloc_estate_whole(get_real_base_type(sm->sym));
85 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
87 struct smatch_state *state;
89 state = get_state(my_id, name, sym);
90 if (state)
91 return state;
93 state = get_state(SMATCH_EXTRA, name, sym);
94 if (state)
95 return state;
96 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
99 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
101 struct smatch_state *start_state, *state;
102 struct symbol *tmp;
103 int param;
105 param = -1;
106 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
107 param++;
108 if (!tmp->ident)
109 continue;
110 state = get_state(my_id, tmp->ident->name, tmp);
111 if (state)
112 goto print;
113 state = get_state(SMATCH_EXTRA, tmp->ident->name, tmp);
114 if (state)
115 goto print;
116 continue;
117 print:
118 if (estate_is_whole(state))
119 continue;
121 start_state = get_state_stree(start_states, SMATCH_EXTRA, tmp->ident->name, tmp);
122 if (estates_equiv(state, start_state))
123 continue;
124 // sm_msg("return_range %s limited '%s' from %s to %s", return_ranges, tmp->ident->name, start_state->name, state->name);
125 sql_insert_return_states(return_id, return_ranges,
126 PARAM_LIMIT, param, "$", state->name);
127 } END_FOR_EACH_PTR(tmp);
130 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
132 struct smatch_state *orig_vals;
133 int param;
135 param = get_param_num_from_sym(sym);
136 if (param < 0)
137 return;
139 /* we only save on-stack params */
140 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
141 return;
143 orig_vals = get_orig_estate(name, sym);
144 set_state(my_id, name, sym, orig_vals);
147 static void match_save_states(struct expression *expr)
149 push_stree(&saved_stack, start_states);
150 start_states = NULL;
153 static void match_restore_states(struct expression *expr)
155 free_stree(&start_states);
156 start_states = pop_stree(&saved_stack);
159 void register_param_limit(int id)
161 my_id = id;
163 add_hook(&save_start_states, AFTER_DEF_HOOK);
164 add_hook(&free_start_states, END_FUNC_HOOK);
166 add_extra_mod_hook(&extra_mod_hook);
167 add_unmatched_state_hook(my_id, &unmatched_state);
168 add_merge_hook(my_id, &merge_estates);
170 add_hook(&match_save_states, INLINE_FN_START);
171 add_hook(&match_restore_states, INLINE_FN_END);
173 add_split_return_callback(&print_return_value_param);