debug: segfault using the --trace parameter
[smatch.git] / smatch_param_limit.c
blobf819c8170527f6ed10bb10313a6740215406d085
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 STATE(original);
54 static struct stree *start_states;
55 static struct stree_stack *saved_stack;
57 static void save_start_states(struct statement *stmt)
59 start_states = get_all_states_stree(SMATCH_EXTRA);
62 static void free_start_states(void)
64 free_stree(&start_states);
67 static struct smatch_state *unmatched_state(struct sm_state *sm)
69 return &original;
72 static struct smatch_state *filter_my_sm(struct sm_state *sm)
74 struct range_list *ret = NULL;
75 struct sm_state *tmp;
76 struct smatch_state *estate;
78 FOR_EACH_PTR(sm->possible, tmp) {
79 if (tmp->state == &merged)
80 continue;
81 if (tmp->state == &original) {
82 estate = get_state_stree(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
83 if (!estate) {
84 // sm_msg("debug: no value found in pool %p", tmp->pool);
85 continue;
87 } else {
88 estate = tmp->state;
90 ret = rl_union(ret, estate_rl(estate));
91 } END_FOR_EACH_PTR(tmp);
93 return alloc_estate_rl(ret);
96 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
98 struct sm_state *sm;
99 struct smatch_state *state;
101 sm = get_sm_state(my_id, name, sym);
102 if (sm)
103 return filter_my_sm(sm);
105 state = get_state(SMATCH_EXTRA, name, sym);
106 if (state)
107 return state;
108 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
111 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
113 struct stree *stree;
114 struct sm_state *tmp;
115 struct sm_state *my_sm;
116 struct smatch_state *orig;
117 struct smatch_state *state;
118 int param;
119 char *compare;
120 const char *compare_str;
121 char buf[256];
123 stree = __get_cur_stree();
125 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
126 if (!tmp->sym || !tmp->sym->ident || strcmp(tmp->name, tmp->sym->ident->name) != 0)
127 continue;
129 param = get_param_num_from_sym(tmp->sym);
130 if (param < 0)
131 continue;
133 compare = expr_param_comparison(symbol_expression(tmp->sym), param);
134 if (!compare)
135 compare = expr_equal_to_param(symbol_expression(tmp->sym), param);
136 if (!compare)
137 compare = expr_lte_to_param(symbol_expression(tmp->sym), param);
138 compare_str = compare;
139 if (!compare_str)
140 compare_str = "";
142 my_sm = get_sm_state(my_id, tmp->name, tmp->sym);
143 if (!my_sm) {
145 if (estate_is_whole(tmp->state) && !compare)
146 continue;
147 orig = get_state_stree(start_states, SMATCH_EXTRA, tmp->name, tmp->sym);
148 if (orig && estates_equiv(orig, tmp->state) && !compare)
149 continue;
151 snprintf(buf, sizeof(buf), "%s%s", tmp->state->name, compare_str);
152 sql_insert_return_states(return_id, return_ranges,
153 LIMITED_VALUE, param, "$", buf);
154 continue;
157 state = filter_my_sm(my_sm);
158 if (!state)
159 continue;
160 /* This represents an impossible state. I screwd up. Bail. */
161 if (!estate_rl(state))
162 continue;
163 if (estate_is_whole(state) && !compare)
164 continue;
166 orig = get_state_stree(start_states, SMATCH_EXTRA, tmp->name, tmp->sym);
167 if (orig && estates_equiv(orig, state) && !compare)
168 continue;
170 snprintf(buf, sizeof(buf), "%s%s", state->name, compare_str);
171 sql_insert_return_states(return_id, return_ranges,
172 LIMITED_VALUE, param, "$", buf);
173 } END_FOR_EACH_SM(tmp);
176 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
178 struct smatch_state *orig_vals;
179 int param;
181 param = get_param_num_from_sym(sym);
182 if (param < 0)
183 return;
185 /* we are only saving params for now */
186 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
187 return;
189 orig_vals = get_orig_estate(name, sym);
190 set_state(my_id, name, sym, orig_vals);
193 static void match_save_states(struct expression *expr)
195 push_stree(&saved_stack, start_states);
196 start_states = NULL;
199 static void match_restore_states(struct expression *expr)
201 free_stree(&start_states);
202 start_states = pop_stree(&saved_stack);
205 void register_param_limit(int id)
207 my_id = id;
209 add_hook(&save_start_states, AFTER_DEF_HOOK);
210 add_hook(&free_start_states, END_FUNC_HOOK);
212 add_extra_mod_hook(&extra_mod_hook);
213 add_unmatched_state_hook(my_id, &unmatched_state);
215 add_hook(&match_save_states, INLINE_FN_START);
216 add_hook(&match_restore_states, INLINE_FN_END);
218 add_split_return_callback(&print_return_value_param);