implied: preserve history in overwrite_states_using_pool()
[smatch.git] / check_unwind.c
blob9f3675b421ddb8c7e499c957ca433179ffda82a1
1 /*
2 * Copyright (C) 2020 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
18 #include <ctype.h>
20 #include "smatch.h"
21 #include "smatch_extra.h"
22 #include "smatch_slist.h"
24 static int my_id;
26 STATE(alloc);
27 STATE(release);
28 STATE(param_released);
29 STATE(ignore);
31 static unsigned long fn_has_alloc;
33 struct ref_func_info {
34 const char *name;
35 int type;
36 int param;
37 const char *key;
38 const sval_t *implies_start, *implies_end;
39 func_hook *call_back;
42 static struct ref_func_info func_table[] = {
43 { "clk_prepare_enable", ALLOC, 0, "$", &int_zero, &int_zero },
44 { "clk_disable_unprepare", RELEASE, 0, "$" },
46 { "alloc_etherdev_mqs", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
47 { "free_netdev", RELEASE, 0, "$" },
50 * FIXME: A common pattern in release functions like amd76xrom_cleanup()
51 * is to do:
53 * if (window->rsrc.parent)
54 * release_resource(&window->rsrc);
56 * Which is slightly tricky to know how to merge the states so let's
57 * hold off checking request_resource() for now.
59 * { "request_resource", ALLOC, 1, "$", &int_zero, &int_zero },
60 * { "release_resource", RELEASE, 0, "$" },
64 { "pci_request_regions", ALLOC, 0, "$", &int_zero, &int_zero },
65 { "pci_release_regions", RELEASE, 0, "$" },
67 { "request_free_mem_region", ALLOC, -1, "$->start", &valid_ptr_min_sval, &valid_ptr_max_sval },
68 { "__request_region", ALLOC, 1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
69 { "__release_region", RELEASE, 1, "$" },
71 { "ioremap", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
72 { "of_iomap", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
73 { "ioremap_encrypted", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
74 { "iounmap", RELEASE, 0, "$" },
76 { "request_threaded_irq", ALLOC, 0, "$", &int_zero, &int_zero },
77 { "request_irq", ALLOC, 0, "$", &int_zero, &int_zero },
78 { "free_irq", RELEASE, 0, "$" },
79 { "pci_request_irq", ALLOC, 1, "$", &int_zero, &int_zero },
80 { "pci_free_irq", RELEASE, 1, "$" },
82 { "register_netdev", ALLOC, 0, "$", &int_zero, &int_zero },
83 { "unregister_netdev", RELEASE, 0, "$" },
85 { "misc_register", ALLOC, 0, "$", &int_zero, &int_zero },
86 { "misc_deregister", RELEASE, 0, "$" },
88 { "ieee80211_alloc_hw", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
89 { "ieee80211_free_hw", RELEASE, 0, "$" },
92 struct smatch_state *unmatched_state(struct sm_state *sm)
94 struct smatch_state *state;
96 if (sm->state != &param_released)
97 return &undefined;
99 if (is_impossible_path())
100 return &param_released;
102 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
103 if (!state)
104 return &undefined;
105 if (!estate_rl(state) || is_err_or_null(estate_rl(state)))
106 return &param_released;
107 if (parent_is_err_or_null_var_sym(sm->name, sm->sym))
108 return &param_released;
110 return &undefined;
113 static struct sm_state *get_start_sm(const char *name, struct symbol *sym)
115 struct sm_state *sm;
117 sm = get_sm_state(my_id, name, sym);
118 if (sm)
119 return sm;
121 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
122 if (get_comparison_strings(name, sm->name) == SPECIAL_EQUAL)
123 return sm;
124 } END_FOR_EACH_SM(sm);
126 return NULL;
129 static bool is_param_var_sym(const char *name, struct symbol *sym)
131 const char *key;
133 return get_param_key_from_var_sym(name, sym, NULL, &key) >= 0;
136 static void mark_matches_as_undefined(const char *key)
138 struct sm_state *sm;
139 int start_pos, state_len, key_len;
140 char *p;
142 while ((p = strchr(key, '-'))) {
143 if (p[1] != '>')
144 return;
145 key = p + 2;
147 key_len = strlen(key);
149 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
150 state_len = strlen(sm->name);
151 if (state_len < key_len)
152 continue;
153 if (!slist_has_state(sm->possible, &alloc))
154 continue;
156 start_pos = state_len - key_len;
157 if ((start_pos == 0 || !isalnum(sm->name[start_pos - 1])) &&
158 strcmp(sm->name + start_pos, key) == 0)
159 set_state(my_id, sm->name, sm->sym, &undefined);
161 } END_FOR_EACH_SM(sm);
164 static bool is_alloc_primitive(struct expression *expr)
166 int i;
168 while (expr->type == EXPR_ASSIGNMENT)
169 expr = strip_expr(expr->right);
170 if (expr->type != EXPR_CALL)
171 return false;
173 if (expr->fn->type != EXPR_SYMBOL)
174 return false;
176 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
177 if (sym_name_is(func_table[i].name, expr->fn))
178 return true;
181 return false;
184 static void return_param_alloc(struct expression *expr, const char *name, struct symbol *sym, void *data)
186 fn_has_alloc = true;
187 set_state(my_id, name, sym, &alloc);
190 static void return_param_release(struct expression *expr, const char *name, struct symbol *sym, void *data)
192 struct sm_state *start_sm;
194 /* The !data means this comes from the DB (not hard coded). */
195 if (!data && is_alloc_primitive(expr))
196 return;
198 start_sm = get_start_sm(name, sym);
199 if (!start_sm) {
200 if (fn_has_alloc) {
201 mark_matches_as_undefined(name);
202 return;
204 if (is_param_var_sym(name, sym))
205 set_state(my_id, name, sym, &param_released);
206 return;
209 if (start_sm)
210 set_state(my_id, start_sm->name, start_sm->sym, &release);
211 else
212 set_state(my_id, name, sym, &release);
215 static void ignore_path(const char *fn, struct expression *expr, void *data)
217 set_state(my_id, "path", NULL, &ignore);
220 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
222 struct sm_state *sm;
223 const char *param_name;
224 int param;
226 if (is_impossible_path())
227 return;
229 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
230 if (sm->state != &param_released)
231 continue;
232 param = get_param_key_from_sm(sm, expr, &param_name);
233 if (param < 0)
234 continue;
235 sql_insert_return_states(return_id, return_ranges, RELEASE,
236 param, param_name, "");
237 } END_FOR_EACH_SM(sm);
240 enum {
241 UNKNOWN, FAIL, SUCCESS, NUM_BUCKETS
244 static int success_fail_positive(struct range_list *rl)
246 if (!rl)
247 return UNKNOWN;
249 if (sval_is_negative(rl_min(rl)) && sval_is_negative(rl_max(rl)))
250 return FAIL;
252 if (rl_min(rl).value == 0)
253 return SUCCESS;
255 return UNKNOWN;
258 static void check_balance(const char *name, struct symbol *sym)
260 struct range_list *inc_lines = NULL;
261 int inc_buckets[NUM_BUCKETS] = {};
262 struct stree *stree, *orig_stree;
263 struct smatch_state *state;
264 struct sm_state *return_sm;
265 struct sm_state *sm;
266 sval_t line = sval_type_val(&int_ctype, 0);
267 int bucket;
269 FOR_EACH_PTR(get_all_return_strees(), stree) {
270 orig_stree = __swap_cur_stree(stree);
272 if (is_impossible_path())
273 goto swap_stree;
274 if (db_incomplete())
275 goto swap_stree;
276 if (get_state(my_id, "path", NULL) == &ignore)
277 goto swap_stree;
279 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
280 if (!return_sm)
281 goto swap_stree;
282 line.value = return_sm->line;
284 sm = get_sm_state(my_id, name, sym);
285 if (!sm)
286 goto swap_stree;
288 state = sm->state;
289 if (state == &param_released)
290 state = &release;
292 if (state != &alloc &&
293 state != &release)
294 goto swap_stree;
296 bucket = success_fail_positive(estate_rl(return_sm->state));
297 if (bucket != FAIL)
298 goto swap_stree;
300 if (state == &alloc) {
301 add_range(&inc_lines, line, line);
302 inc_buckets[bucket] = true;
304 swap_stree:
305 __swap_cur_stree(orig_stree);
306 } END_FOR_EACH_PTR(stree);
308 if (inc_buckets[FAIL])
309 goto complain;
311 return;
313 complain:
314 sm_warning("'%s' not released on lines: %s.", name, show_rl(inc_lines));
317 static void match_check_balanced(struct symbol *sym)
319 struct sm_state *sm;
321 FOR_EACH_MY_SM(my_id, get_all_return_states(), sm) {
322 if (sm->sym == NULL && strcmp(sm->name, "path") == 0)
323 continue;
324 check_balance(sm->name, sm->sym);
325 } END_FOR_EACH_SM(sm);
328 void check_unwind(int id)
330 struct ref_func_info *info;
331 int i;
333 my_id = id;
335 if (option_project != PROJ_KERNEL)
336 return;
338 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
339 info = &func_table[i];
341 if (info->call_back) {
342 add_function_hook(info->name, info->call_back, info);
343 } else if (info->implies_start && info->type == ALLOC) {
344 return_implies_param_key_exact(info->name,
345 *info->implies_start,
346 *info->implies_end,
347 &return_param_alloc,
348 info->param, info->key, info);
349 } else if (info->implies_start) {
350 return_implies_param_key(info->name,
351 *info->implies_start,
352 *info->implies_end,
353 &return_param_release,
354 info->param, info->key, info);
355 } else {
356 add_function_param_key_hook(info->name,
357 (info->type == ALLOC) ? &return_param_alloc : &return_param_release,
358 info->param, info->key, info);
362 add_function_hook("devm_add_action_or_reset", &ignore_path, NULL);
364 add_unmatched_state_hook(my_id, &unmatched_state);
365 add_function_data(&fn_has_alloc);
367 add_split_return_callback(match_return_info);
368 select_return_param_key(RELEASE, &return_param_release);
369 add_hook(&match_check_balanced, END_FUNC_HOOK);