smatch_kernel_host_data: enable additional debug
[smatch.git] / smatch_param_limit.c
blobef21ac70fdb623520f44894c9cf5c68ddacd9b8a
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 smatch_state *unmatched_state(struct sm_state *sm)
54 struct smatch_state *state;
56 if (!param_was_set_var_sym(sm->name, sm->sym)) {
57 state = __get_state(SMATCH_EXTRA, sm->name, sm->sym);
58 if (state)
59 return state;
61 return alloc_estate_whole(estate_type(sm->state));
64 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
66 struct smatch_state *state;
68 state = get_state(my_id, name, sym);
69 if (state)
70 return state;
72 state = get_state(SMATCH_EXTRA, name, sym);
73 if (state)
74 return state;
75 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
78 static struct range_list *generify_mtag_range(struct smatch_state *state)
80 struct range_list *rl;
81 struct data_range *drange;
83 if (!estate_type(state) || estate_type(state)->type != SYM_PTR)
84 return estate_rl(state);
87 * The problem is that we get too specific on our param limits when we
88 * know exactly what pointers are passed to a function. It gets to the
89 * point where we say "pointer x will succeed, but everything else will
90 * fail." And then we introduce a new caller which passes a different
91 * pointer and it's like, "Sorry bro, that's not possible."
94 rl = estate_rl(state);
95 FOR_EACH_PTR(rl, drange) {
96 if (drange->min.value != drange->max.value)
97 continue;
98 if (drange->min.value == 0)
99 continue;
100 if (is_err_ptr(drange->min))
101 continue;
102 return rl_union(valid_ptr_rl, rl);
103 } END_FOR_EACH_PTR(drange);
105 return estate_rl(state);
108 static bool sm_was_set(struct sm_state *sm)
110 struct relation *rel;
112 if (!estate_related(sm->state))
113 return param_was_set_var_sym(sm->name, sm->sym);
115 FOR_EACH_PTR(estate_related(sm->state), rel) {
116 if (param_was_set_var_sym(sm->name, sm->sym))
117 return true;
118 } END_FOR_EACH_PTR(rel);
119 return false;
122 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
124 struct smatch_state *state, *old;
125 struct sm_state *tmp;
126 struct range_list *rl;
127 const char *param_name;
128 int param;
130 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), tmp) {
131 if (tmp->name[0] == '&')
132 continue;
133 param = get_param_num_from_sym(tmp->sym);
134 if (param < 0)
135 continue;
137 param_name = get_param_name(tmp);
138 if (!param_name)
139 continue;
141 state = __get_state(my_id, tmp->name, tmp->sym);
142 if (!state) {
143 if (sm_was_set(tmp))
144 continue;
145 state = tmp->state;
148 if (estate_is_whole(state) || estate_is_empty(state))
149 continue;
150 old = get_state_stree(get_start_states(), SMATCH_EXTRA, tmp->name, tmp->sym);
151 if (old && rl_equiv(estate_rl(old), estate_rl(state)))
152 continue;
154 if (is_ignored_kernel_data(param_name))
155 continue;
157 rl = generify_mtag_range(state);
158 sql_insert_return_states(return_id, return_ranges, PARAM_LIMIT,
159 param, param_name, show_rl(rl));
160 } END_FOR_EACH_SM(tmp);
163 static void extra_mod_hook(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
165 struct smatch_state *orig;
166 struct symbol *param_sym;
167 char *param_name;
169 if (expr && expr->smatch_flags & Fake)
170 return;
172 param_name = get_param_var_sym_var_sym(name, sym, NULL, &param_sym);
173 if (!param_name || !param_sym)
174 goto free;
175 if (get_param_num_from_sym(param_sym) < 0)
176 goto free;
178 /* already saved */
179 if (get_state(my_id, param_name, param_sym))
180 goto free;
182 orig = get_state(SMATCH_EXTRA, param_name, param_sym);
183 if (!orig)
184 orig = alloc_estate_whole(estate_type(state));
186 set_state(my_id, param_name, param_sym, orig);
187 free:
188 free_string(param_name);
191 void register_param_limit(int id)
193 my_id = id;
195 db_ignore_states(my_id);
196 set_dynamic_states(my_id);
198 add_extra_mod_hook(&extra_mod_hook);
199 add_unmatched_state_hook(my_id, &unmatched_state);
200 add_merge_hook(my_id, &merge_estates);
202 add_split_return_callback(&print_return_value_param);