math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_capped.c
bloba0d7234969eb3cce1f559ccf3866931d16d44d8c
1 /*
2 * Copyright (C) 2011 Oracle. All rights reserved.
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 trying to make a list of the variables which
20 * have capped values. Sometimes we don't know what the
21 * cap is, for example if we are comparing variables but
22 * we don't know the values of the variables. In that
23 * case we only know that our variable is capped and we
24 * sort that information here.
27 #include "smatch.h"
28 #include "smatch_slist.h"
29 #include "smatch_extra.h"
31 static int my_id;
33 STATE(capped);
34 STATE(uncapped);
36 static void set_uncapped(struct sm_state *sm, struct expression *mod_expr)
38 set_state(my_id, sm->name, sm->sym, &uncapped);
41 static struct smatch_state *unmatched_state(struct sm_state *sm)
43 struct smatch_state *state;
45 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
46 if (state && !estate_is_whole(state))
47 return &capped;
48 return &uncapped;
51 static int is_capped_macro(struct expression *expr)
53 char *name;
55 name = get_macro_name(expr->pos);
56 if (!name)
57 return 0;
59 if (strcmp(name, "min") == 0)
60 return 1;
61 if (strcmp(name, "MIN") == 0)
62 return 1;
63 if (strcmp(name, "min_t") == 0)
64 return 1;
66 return 0;
69 int is_capped(struct expression *expr)
71 sval_t dummy;
73 expr = strip_expr(expr);
74 while (expr && expr->type == EXPR_POSTOP) {
75 expr = strip_expr(expr->unop);
77 if (!expr)
78 return 0;
80 if (is_ptr_type(get_type(expr)))
81 return 0;
83 if (get_hard_max(expr, &dummy))
84 return 1;
86 if (is_capped_macro(expr))
87 return 1;
89 if (expr->type == EXPR_BINOP) {
90 struct range_list *left_rl, *right_rl;
92 if (expr->op == '&')
93 return 1;
94 if (expr->op == SPECIAL_RIGHTSHIFT)
95 return 1;
96 if (expr->op == '%')
97 return is_capped(expr->right);
98 if (!is_capped(expr->left))
99 return 0;
100 if (expr->op == '/')
101 return 1;
102 if (!is_capped(expr->right))
103 return 0;
104 if (expr->op == '*') {
105 get_absolute_rl(expr->left, &left_rl);
106 get_absolute_rl(expr->right, &right_rl);
107 if (sval_is_negative(rl_min(left_rl)) ||
108 sval_is_negative(rl_min(right_rl)))
109 return 0;
111 return 1;
113 if (get_state_expr(my_id, expr) == &capped)
114 return 1;
115 return 0;
118 int is_capped_var_sym(const char *name, struct symbol *sym)
120 if (get_state(my_id, name, sym) == &capped)
121 return 1;
122 return 0;
125 void set_param_capped_data(const char *name, struct symbol *sym, char *key, char *value)
127 char fullname[256];
129 if (strncmp(key, "$", 1))
130 return;
131 snprintf(fullname, 256, "%s%s", name, key + 1);
132 set_state(my_id, fullname, sym, &capped);
135 static void match_condition(struct expression *expr)
137 struct expression *left, *right;
138 struct smatch_state *left_true = NULL;
139 struct smatch_state *left_false = NULL;
140 struct smatch_state *right_true = NULL;
141 struct smatch_state *right_false = NULL;
142 sval_t sval;
145 if (expr->type != EXPR_COMPARE)
146 return;
148 left = strip_expr(expr->left);
149 right = strip_expr(expr->right);
151 while (left->type == EXPR_ASSIGNMENT)
152 left = strip_expr(left->left);
154 /* If we're dealing with known expressions, that's for smatch_extra.c */
155 if (get_implied_value(left, &sval) ||
156 get_implied_value(right, &sval))
157 return;
159 switch (expr->op) {
160 case '<':
161 case SPECIAL_LTE:
162 case SPECIAL_UNSIGNED_LT:
163 case SPECIAL_UNSIGNED_LTE:
164 left_true = &capped;
165 right_false = &capped;
166 break;
167 case '>':
168 case SPECIAL_GTE:
169 case SPECIAL_UNSIGNED_GT:
170 case SPECIAL_UNSIGNED_GTE:
171 left_false = &capped;
172 right_true = &capped;
173 break;
174 case SPECIAL_EQUAL:
175 left_true = &capped;
176 right_true = &capped;
177 break;
178 case SPECIAL_NOTEQUAL:
179 left_false = &capped;
180 right_false = &capped;
181 break;
183 default:
184 return;
187 set_true_false_states_expr(my_id, left, left_true, left_false);
188 set_true_false_states_expr(my_id, right, right_true, right_false);
191 static void match_assign(struct expression *expr)
193 if (is_capped(expr->right)) {
194 set_state_expr(my_id, expr->left, &capped);
195 } else {
196 if (get_state_expr(my_id, expr->left))
197 set_state_expr(my_id, expr->left, &uncapped);
201 static void match_caller_info(struct expression *expr)
203 struct expression *tmp;
204 sval_t sval;
205 int i;
207 i = -1;
208 FOR_EACH_PTR(expr->args, tmp) {
209 i++;
210 if (get_implied_value(tmp, &sval))
211 continue;
212 if (!is_capped(tmp))
213 continue;
214 sql_insert_caller_info(expr, CAPPED_DATA, i, "$", "1");
215 } END_FOR_EACH_PTR(tmp);
218 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
220 struct smatch_state *estate;
221 sval_t sval;
223 if (sm->state != &capped)
224 return;
225 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
226 if (estate_get_single_value(estate, &sval))
227 return;
228 sql_insert_caller_info(call, CAPPED_DATA, param, printed_name, "1");
231 static void print_return_implies_capped(int return_id, char *return_ranges, struct expression *expr)
233 struct smatch_state *orig, *estate;
234 struct sm_state *sm;
235 struct symbol *ret_sym;
236 const char *param_name;
237 char *return_str;
238 int param;
239 sval_t sval;
240 bool return_found = false;
242 expr = strip_expr(expr);
243 return_str = expr_to_str(expr);
244 ret_sym = expr_to_sym(expr);
246 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
247 if (sm->state != &capped)
248 continue;
250 param = get_param_num_from_sym(sm->sym);
251 if (param < 0)
252 continue;
254 estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
255 if (estate_get_single_value(estate, &sval))
256 continue;
258 orig = get_state_stree(get_start_states(), my_id, sm->name, sm->sym);
259 if (orig == &capped)
260 continue;
262 param_name = get_param_name(sm);
263 if (!param_name)
264 continue;
266 sql_insert_return_states(return_id, return_ranges, CAPPED_DATA,
267 param, param_name, "1");
268 } END_FOR_EACH_SM(sm);
270 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
271 if (!ret_sym)
272 break;
273 if (sm->state != &capped)
274 continue;
275 if (ret_sym != sm->sym)
276 continue;
278 param_name = state_name_to_param_name(sm->name, return_str);
279 if (!param_name)
280 continue;
281 if (strcmp(param_name, "$") == 0)
282 return_found = true;
283 sql_insert_return_states(return_id, return_ranges, CAPPED_DATA,
284 -1, param_name, "1");
285 } END_FOR_EACH_SM(sm);
287 if (return_found)
288 goto free_string;
290 if (option_project == PROJ_KERNEL && get_function() &&
291 strstr(get_function(), "nla_get_"))
292 sql_insert_return_states(return_id, return_ranges, CAPPED_DATA,
293 -1, "$", "1");
295 free_string:
296 free_string(return_str);
299 static void db_return_states_capped(struct expression *expr, int param, char *key, char *value)
301 char *name;
302 struct symbol *sym;
304 name = return_state_to_var_sym(expr, param, key, &sym);
305 if (!name || !sym)
306 goto free;
308 set_state(my_id, name, sym, &capped);
309 free:
310 free_string(name);
313 void register_capped(int id)
315 my_id = id;
317 add_unmatched_state_hook(my_id, &unmatched_state);
318 select_caller_info_hook(set_param_capped_data, CAPPED_DATA);
319 add_hook(&match_condition, CONDITION_HOOK);
320 add_hook(&match_assign, ASSIGNMENT_HOOK);
321 add_modification_hook(my_id, &set_uncapped);
323 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
324 add_member_info_callback(my_id, struct_member_callback);
326 add_split_return_callback(print_return_implies_capped);
327 select_return_states_hook(CAPPED_DATA, &db_return_states_capped);