comparison: pass the ignore field to range_comparison_to_param_helper()
[smatch.git] / check_unwind.c
blobe332e1283d1d6d8a4c284f498745ada75bb2d45e
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
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 a kernel check to make sure we unwind everything on
20 * on errors.
24 #include "smatch.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 #define EBUSY 16
29 #define MAX_ERRNO 4095
31 static int my_id;
33 STATE(allocated);
34 STATE(unallocated);
36 /* state of unwind function */
37 STATE(called);
39 static int was_passed_as_param(struct expression *expr)
41 char *name;
42 struct symbol *sym;
43 struct symbol *arg;
45 name = expr_to_var_sym(expr, &sym);
46 if (!name)
47 return 0;
48 free_string(name);
50 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
51 if (arg == sym)
52 return 1;
53 } END_FOR_EACH_PTR(arg);
54 return 0;
57 static void print_unwind_functions(const char *fn, struct expression *expr, void *_arg_no)
59 struct expression *arg_expr;
60 int arg_no = PTR_INT(_arg_no);
61 static struct symbol *last_printed = NULL;
63 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
64 if (!was_passed_as_param(arg_expr))
65 return;
66 if (last_printed == cur_func_sym)
67 return;
68 last_printed = cur_func_sym;
69 sm_msg("info: is unwind function");
72 static void request_granted(const char *fn, struct expression *call_expr,
73 struct expression *assign_expr, void *_arg_no)
75 struct expression *arg_expr;
76 int arg_no = PTR_INT(_arg_no);
78 if (arg_no == -1)
79 arg_expr = assign_expr->left;
80 else
81 arg_expr = get_argument_from_call_expr(call_expr->args, arg_no);
82 set_state_expr(my_id, arg_expr, &allocated);
85 static void request_denied(const char *fn, struct expression *call_expr,
86 struct expression *assign_expr, void *_arg_no)
88 struct expression *arg_expr;
89 int arg_no = PTR_INT(_arg_no);
91 if (arg_no == -1)
92 arg_expr = assign_expr->left;
93 else
94 arg_expr = get_argument_from_call_expr(call_expr->args, arg_no);
95 set_state_expr(my_id, arg_expr, &unallocated);
98 static void match_release(const char *fn, struct expression *expr, void *_arg_no)
100 struct expression *arg_expr;
101 int arg_no = PTR_INT(_arg_no);
103 arg_expr = get_argument_from_call_expr(expr->args, arg_no);
104 if (get_state_expr(my_id, arg_expr))
105 set_state_expr(my_id, arg_expr, &unallocated);
106 set_equiv_state_expr(my_id, arg_expr, &unallocated);
109 static void match_unwind_function(const char *fn, struct expression *expr, void *unused)
111 set_state(my_id, "unwind_function", NULL, &called);
114 static int func_returns_int(void)
116 struct symbol *type;
118 type = get_base_type(cur_func_sym);
119 if (!type || type->type != SYM_FN)
120 return 0;
121 type = get_base_type(type);
122 if (type->ctype.base_type == &int_type) {
123 return 1;
125 return 0;
128 static void match_return(struct expression *ret_value)
130 struct stree *stree;
131 struct sm_state *tmp;
132 sval_t sval;
134 if (!func_returns_int())
135 return;
136 if (get_value(ret_value, &sval) && sval_cmp_val(sval, 0) >= 0)
137 return;
138 if (!implied_not_equal(ret_value, 0))
139 return;
140 if (get_state(my_id, "unwind_function", NULL) == &called)
141 return;
143 stree = __get_cur_stree();
144 FOR_EACH_MY_SM(my_id, stree, tmp) {
145 if (slist_has_state(tmp->possible, &allocated))
146 sm_msg("warn: '%s' was not released on error", tmp->name);
147 } END_FOR_EACH_SM(tmp);
150 static void register_unwind_functions(void)
152 struct token *token;
153 const char *func;
155 token = get_tokens_file("kernel.unwind_functions");
156 if (!token)
157 return;
158 if (token_type(token) != TOKEN_STREAMBEGIN)
159 return;
160 token = token->next;
161 while (token_type(token) != TOKEN_STREAMEND) {
162 if (token_type(token) != TOKEN_IDENT)
163 return;
164 func = show_ident(token->ident);
165 add_function_hook(func, &match_unwind_function, NULL);
166 token = token->next;
168 clear_token_alloc();
171 static void release_function_indicator(const char *name)
173 if (!option_info)
174 return;
175 add_function_hook(name, &print_unwind_functions, INT_PTR(0));
178 void check_unwind(int id)
180 if (option_project != PROJ_KERNEL || !option_spammy)
181 return;
182 my_id = id;
184 register_unwind_functions();
186 return_implies_state("request_resource", 0, 0, &request_granted, INT_PTR(1));
187 return_implies_state("request_resource", -EBUSY, -EBUSY, &request_denied, INT_PTR(1));
188 add_function_hook("release_resource", &match_release, INT_PTR(0));
189 release_function_indicator("release_resource");
191 return_implies_state("__request_region", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(1));
192 return_implies_state("__request_region", 0, 0, &request_denied, INT_PTR(1));
193 add_function_hook("__release_region", &match_release, INT_PTR(1));
194 release_function_indicator("__release_region");
196 return_implies_state("ioremap", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(-1));
197 return_implies_state("ioremap", 0, 0, &request_denied, INT_PTR(-1));
198 add_function_hook("iounmap", &match_release, INT_PTR(0));
200 return_implies_state("pci_iomap", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(-1));
201 return_implies_state("pci_iomap", 0, 0, &request_denied, INT_PTR(-1));
202 add_function_hook("pci_iounmap", &match_release, INT_PTR(1));
203 release_function_indicator("pci_iounmap");
205 return_implies_state("__create_workqueue_key", valid_ptr_min, valid_ptr_max, &request_granted,
206 INT_PTR(-1));
207 return_implies_state("__create_workqueue_key", 0, 0, &request_denied, INT_PTR(-1));
208 add_function_hook("destroy_workqueue", &match_release, INT_PTR(0));
210 return_implies_state("request_irq", 0, 0, &request_granted, INT_PTR(0));
211 return_implies_state("request_irq", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
212 add_function_hook("free_irq", &match_release, INT_PTR(0));
213 release_function_indicator("free_irq");
215 return_implies_state("register_netdev", 0, 0, &request_granted, INT_PTR(0));
216 return_implies_state("register_netdev", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
217 add_function_hook("unregister_netdev", &match_release, INT_PTR(0));
218 release_function_indicator("unregister_netdev");
220 return_implies_state("misc_register", 0, 0, &request_granted, INT_PTR(0));
221 return_implies_state("misc_register", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
222 add_function_hook("misc_deregister", &match_release, INT_PTR(0));
223 release_function_indicator("misc_deregister");
226 add_hook(&match_return, RETURN_HOOK);