user_data: update to stree
[smatch.git] / smatch_param_limit.c
bloba28aa0eea96c9a82b6c05293915a7b54c2a627eb
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 for functions like this:
21 * int foo(int a)
22 * {
23 * if (a >= 0 && a < 10) {
24 * a = 42;
25 * return 1;
26 * }
27 * return 0;
28 * }
30 * If we pass in 5, it returns 1.
32 * It's a bit complicated because we can't just consider the final value, we
33 * have to always consider the passed in value.
37 #include "scope.h"
38 #include "smatch.h"
39 #include "smatch_extra.h"
40 #include "smatch_slist.h"
42 static int my_id;
44 STATE(original);
46 static struct state_list *start_states;
47 static struct state_list_stack *saved_stack;
49 static void save_start_states(struct statement *stmt)
51 start_states = get_all_states(SMATCH_EXTRA);
54 static void match_end_func(void)
56 free_slist(&start_states);
59 static struct smatch_state *unmatched_state(struct sm_state *sm)
61 return &original;
64 static struct smatch_state *filter_my_sm(struct sm_state *sm)
66 struct range_list *ret = NULL;
67 struct sm_state *tmp;
68 struct smatch_state *estate;
70 FOR_EACH_PTR(sm->possible, tmp) {
71 if (tmp->state == &merged)
72 continue;
73 if (tmp->state == &original) {
74 estate = get_state_slist(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
75 if (!estate) {
76 // sm_msg("debug: no value found in pool %p", tmp->pool);
77 continue;
79 } else {
80 estate = tmp->state;
82 ret = rl_union(ret, estate_rl(estate));
83 } END_FOR_EACH_PTR(tmp);
85 return alloc_estate_rl(ret);
88 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
90 struct sm_state *sm;
91 struct smatch_state *state;
93 sm = get_sm_state(my_id, name, sym);
94 if (sm)
95 return filter_my_sm(sm);
97 state = get_state(SMATCH_EXTRA, name, sym);
98 if (state)
99 return state;
100 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
103 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
105 struct state_list *extra_slist;
106 struct sm_state *tmp;
107 struct sm_state *my_sm;
108 struct smatch_state *state;
109 int param;
111 extra_slist = get_all_states(SMATCH_EXTRA);
113 FOR_EACH_PTR(extra_slist, tmp) {
114 if (!tmp->sym || !tmp->sym->ident || strcmp(tmp->name, tmp->sym->ident->name) != 0)
115 continue;
117 param = get_param_num_from_sym(tmp->sym);
118 if (param < 0)
119 continue;
121 my_sm = get_sm_state(my_id, tmp->name, tmp->sym);
122 if (!my_sm) {
123 struct smatch_state *old;
125 if (estate_is_whole(tmp->state))
126 continue;
127 old = get_state_slist(start_states, SMATCH_EXTRA, tmp->name, tmp->sym);
128 if (old && estates_equiv(old, tmp->state))
129 continue;
130 sql_insert_return_states(return_id, return_ranges,
131 LIMITED_VALUE, param, "$$",
132 tmp->state->name);
133 continue;
136 state = filter_my_sm(my_sm);
137 if (!state)
138 continue;
139 /* This represents an impossible state. I screwd up. Bail. */
140 if (!estate_rl(state))
141 continue;
142 if (estate_is_whole(state))
143 continue;
144 sql_insert_return_states(return_id, return_ranges,
145 LIMITED_VALUE, param, "$$",
146 state->name);
147 } END_FOR_EACH_PTR(tmp);
149 free_slist(&extra_slist);
152 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
154 struct smatch_state *orig_vals;
155 int param;
157 param = get_param_num_from_sym(sym);
158 if (param < 0)
159 return;
161 /* we are only saving params for now */
162 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
163 return;
165 orig_vals = get_orig_estate(name, sym);
166 set_state(my_id, name, sym, orig_vals);
169 static void match_save_states(struct expression *expr)
171 push_slist(&saved_stack, start_states);
172 start_states = NULL;
175 static void match_restore_states(struct expression *expr)
177 start_states = pop_slist(&saved_stack);
180 void register_param_limit(int id)
182 my_id = id;
184 add_hook(&save_start_states, AFTER_DEF_HOOK);
185 add_extra_mod_hook(&extra_mod_hook);
186 add_unmatched_state_hook(my_id, &unmatched_state);
187 add_split_return_callback(&print_return_value_param);
188 add_hook(&match_end_func, END_FUNC_HOOK);
189 add_hook(&match_save_states, INLINE_FN_START);
190 add_hook(&match_restore_states, INLINE_FN_END);