unwind: add release_and_free_resource() and release_resource()
[smatch.git] / check_unwind.c
bloba833f70ca1454fd58cbb4b4ba1fc9428cb8cf30a
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;
25 static int info_id;
27 STATE(alloc);
28 STATE(release);
29 STATE(param_released);
30 STATE(ignore);
32 static unsigned long fn_has_alloc;
34 struct ref_func_info {
35 const char *name;
36 int type;
37 int param;
38 const char *key;
39 const sval_t *implies_start, *implies_end;
40 func_hook *call_back;
43 static struct ref_func_info func_table[] = {
44 { "clk_prepare", ALLOC, 0, "$", &int_zero, &int_zero },
45 { "clk_prepare_enable", ALLOC, 0, "$", &int_zero, &int_zero },
46 { "clk_disable_unprepare", RELEASE, 0, "$" },
47 { "clk_unprepare", RELEASE, 0, "$" },
49 { "alloc_etherdev_mqs", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
50 { "free_netdev", RELEASE, 0, "$" },
53 * FIXME: A common pattern in release functions like amd76xrom_cleanup()
54 * is to do:
56 * if (window->rsrc.parent)
57 * release_resource(&window->rsrc);
59 * Which is slightly tricky to know how to merge the states so let's
60 * hold off checking request_resource() for now.
62 * { "request_resource", ALLOC, 1, "$", &int_zero, &int_zero },
63 * { "release_resource", RELEASE, 0, "$" },
67 { "pci_request_regions", ALLOC, 0, "$", &int_zero, &int_zero },
68 { "pci_release_regions", RELEASE, 0, "$" },
70 { "request_free_mem_region", ALLOC, -1, "$->start", &valid_ptr_min_sval, &valid_ptr_max_sval },
71 { "__request_region", ALLOC, 1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
72 { "release_and_free_resource", RELEASE, 0, "$->start" },
73 { "release_resource", RELEASE, 0, "$->start" },
74 { "__release_region", RELEASE, 1, "$" },
76 { "ioremap", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
77 { "of_iomap", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
78 { "ioremap_encrypted", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
79 { "iounmap", RELEASE, 0, "$" },
81 { "request_threaded_irq", ALLOC, 0, "$", &int_zero, &int_zero },
82 { "request_irq", ALLOC, 0, "$", &int_zero, &int_zero },
83 { "free_irq", RELEASE, 0, "$" },
84 { "pci_request_irq", ALLOC, 1, "$", &int_zero, &int_zero },
85 { "pci_free_irq", RELEASE, 1, "$" },
87 { "register_netdev", ALLOC, 0, "$", &int_zero, &int_zero },
88 { "unregister_netdev", RELEASE, 0, "$" },
90 { "misc_register", ALLOC, 0, "$", &int_zero, &int_zero },
91 { "misc_deregister", RELEASE, 0, "$" },
93 { "ieee80211_alloc_hw", ALLOC, -1, "$", &valid_ptr_min_sval, &valid_ptr_max_sval },
94 { "ieee80211_free_hw", RELEASE, 0, "$" },
97 static struct smatch_state *unmatched_state(struct sm_state *sm)
99 struct smatch_state *state;
101 if (sm->state != &param_released)
102 return &undefined;
104 if (is_impossible_path())
105 return &param_released;
107 state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
108 if (!state)
109 return &undefined;
110 if (!estate_rl(state) || is_err_or_null(estate_rl(state)))
111 return &param_released;
112 if (parent_is_err_or_null_var_sym(sm->name, sm->sym))
113 return &param_released;
115 return &undefined;
118 static bool is_param_var_sym(const char *name, struct symbol *sym)
120 const char *key;
122 return get_param_key_from_var_sym(name, sym, NULL, &key) >= 0;
125 static void mark_matches_as_undefined(const char *key)
127 struct sm_state *sm;
128 int start_pos, state_len, key_len;
129 char *p;
131 while ((p = strchr(key, '-'))) {
132 if (p[1] != '>')
133 return;
134 key = p + 2;
136 key_len = strlen(key);
138 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
139 state_len = strlen(sm->name);
140 if (state_len < key_len)
141 continue;
142 if (!slist_has_state(sm->possible, &alloc))
143 continue;
145 start_pos = state_len - key_len;
146 if ((start_pos == 0 || !isalnum(sm->name[start_pos - 1])) &&
147 strcmp(sm->name + start_pos, key) == 0)
148 update_ssa_state(my_id, sm->name, sm->sym, &undefined);
150 } END_FOR_EACH_SM(sm);
153 static bool is_alloc_primitive(struct expression *expr)
155 int i;
157 while (expr->type == EXPR_ASSIGNMENT)
158 expr = strip_expr(expr->right);
159 if (expr->type != EXPR_CALL)
160 return false;
162 if (expr->fn->type != EXPR_SYMBOL)
163 return false;
165 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
166 if (sym_name_is(func_table[i].name, expr->fn))
167 return true;
170 return false;
173 static void return_param_alloc(struct expression *expr, const char *name, struct symbol *sym, void *data)
175 fn_has_alloc = true;
176 set_ssa_state(my_id, name, sym, &alloc);
179 static void return_param_release(struct expression *expr, const char *name, struct symbol *sym, void *data)
181 struct sm_state *start_sm;
183 /* The !data means this comes from the DB (not hard coded). */
184 if (!data && is_alloc_primitive(expr))
185 return;
187 start_sm = get_ssa_sm_state(my_id, name, sym);
188 if (start_sm) {
189 update_ssa_state(my_id, start_sm->name, start_sm->sym, &release);
190 } else {
191 if (fn_has_alloc) {
192 mark_matches_as_undefined(name);
193 return;
195 if (is_param_var_sym(name, sym))
196 set_state(info_id, name, sym, &param_released);
200 static void ignore_path(const char *fn, struct expression *expr, void *data)
202 set_state(my_id, "path", NULL, &ignore);
205 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
207 struct sm_state *sm;
208 const char *param_name;
209 int param;
211 if (is_impossible_path())
212 return;
214 FOR_EACH_MY_SM(info_id, __get_cur_stree(), sm) {
215 if (sm->state != &param_released)
216 continue;
217 param = get_param_key_from_sm(sm, expr, &param_name);
218 if (param < 0)
219 continue;
220 sql_insert_return_states(return_id, return_ranges, RELEASE,
221 param, param_name, "");
222 } END_FOR_EACH_SM(sm);
225 enum {
226 UNKNOWN, FAIL, SUCCESS, NUM_BUCKETS
229 static int success_fail_positive(struct range_list *rl)
231 if (!rl)
232 return UNKNOWN;
234 if (sval_is_negative(rl_min(rl)) && sval_is_negative(rl_max(rl)))
235 return FAIL;
237 if (rl_min(rl).value == 0)
238 return SUCCESS;
240 return UNKNOWN;
243 static void check_balance(const char *name, struct symbol *sym)
245 struct range_list *inc_lines = NULL;
246 int inc_buckets[NUM_BUCKETS] = {};
247 struct stree *stree, *orig_stree;
248 struct smatch_state *state;
249 struct sm_state *return_sm;
250 struct sm_state *sm;
251 sval_t line = sval_type_val(&int_ctype, 0);
252 int bucket;
254 FOR_EACH_PTR(get_all_return_strees(), stree) {
255 orig_stree = __swap_cur_stree(stree);
257 if (is_impossible_path())
258 goto swap_stree;
259 if (db_incomplete())
260 goto swap_stree;
261 if (get_state(my_id, "path", NULL) == &ignore)
262 goto swap_stree;
264 return_sm = get_sm_state(RETURN_ID, "return_ranges", NULL);
265 if (!return_sm)
266 goto swap_stree;
267 line.value = return_sm->line;
269 sm = get_sm_state(my_id, name, sym);
270 if (!sm)
271 goto swap_stree;
273 state = sm->state;
274 if (state == &param_released)
275 state = &release;
277 if (state != &alloc &&
278 state != &release)
279 goto swap_stree;
281 bucket = success_fail_positive(estate_rl(return_sm->state));
282 if (bucket != FAIL)
283 goto swap_stree;
285 if (state == &alloc) {
286 add_range(&inc_lines, line, line);
287 inc_buckets[bucket] = true;
289 swap_stree:
290 __swap_cur_stree(orig_stree);
291 } END_FOR_EACH_PTR(stree);
293 if (inc_buckets[FAIL])
294 goto complain;
296 return;
298 complain:
299 sm_warning("'%s' not released on lines: %s.", ssa_name(name), show_rl(inc_lines));
302 static void match_check_balanced(struct symbol *sym)
304 struct sm_state *sm;
306 FOR_EACH_MY_SM(my_id, get_all_return_states(), sm) {
307 if (sm->sym == NULL && strcmp(sm->name, "path") == 0)
308 continue;
309 check_balance(sm->name, sm->sym);
310 } END_FOR_EACH_SM(sm);
313 void check_unwind(int id)
315 struct ref_func_info *info;
316 int i;
318 my_id = id;
320 if (option_project != PROJ_KERNEL)
321 return;
323 for (i = 0; i < ARRAY_SIZE(func_table); i++) {
324 info = &func_table[i];
326 if (info->call_back) {
327 add_function_hook(info->name, info->call_back, info);
328 } else if (info->implies_start && info->type == ALLOC) {
329 return_implies_param_key_exact(info->name,
330 *info->implies_start,
331 *info->implies_end,
332 &return_param_alloc,
333 info->param, info->key, info);
334 } else if (info->implies_start) {
335 return_implies_param_key(info->name,
336 *info->implies_start,
337 *info->implies_end,
338 &return_param_release,
339 info->param, info->key, info);
340 } else {
341 add_function_param_key_hook(info->name,
342 (info->type == ALLOC) ? &return_param_alloc : &return_param_release,
343 info->param, info->key, info);
347 add_function_hook("devm_add_action_or_reset", &ignore_path, NULL);
348 add_function_hook("drmm_add_action", &ignore_path, NULL);
349 add_function_hook("__drmm_add_action", &ignore_path, NULL);
351 add_function_data(&fn_has_alloc);
353 add_split_return_callback(match_return_info);
354 select_return_param_key(RELEASE, &return_param_release);
355 add_hook(&match_check_balanced, END_FUNC_HOOK);
358 void check_unwind_info(int id)
360 info_id = id;
361 add_unmatched_state_hook(info_id, &unmatched_state);