avl: change some void pointers to struct sm_state pointers
[smatch.git] / check_unwind.c
blobf4e0461205da62c9169cf60110bfd4e36e123992
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()
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 state_list *slist;
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 slist = get_all_states(my_id);
144 FOR_EACH_PTR(slist, 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_PTR(tmp);
148 free_slist(&slist);
151 static void register_unwind_functions(void)
153 struct token *token;
154 const char *func;
156 token = get_tokens_file("kernel.unwind_functions");
157 if (!token)
158 return;
159 if (token_type(token) != TOKEN_STREAMBEGIN)
160 return;
161 token = token->next;
162 while (token_type(token) != TOKEN_STREAMEND) {
163 if (token_type(token) != TOKEN_IDENT)
164 return;
165 func = show_ident(token->ident);
166 add_function_hook(func, &match_unwind_function, NULL);
167 token = token->next;
169 clear_token_alloc();
172 static void release_function_indicator(const char *name)
174 if (!option_info)
175 return;
176 add_function_hook(name, &print_unwind_functions, INT_PTR(0));
179 void check_unwind(int id)
181 if (option_project != PROJ_KERNEL || !option_spammy)
182 return;
183 my_id = id;
185 register_unwind_functions();
187 return_implies_state("request_resource", 0, 0, &request_granted, INT_PTR(1));
188 return_implies_state("request_resource", -EBUSY, -EBUSY, &request_denied, INT_PTR(1));
189 add_function_hook("release_resource", &match_release, INT_PTR(0));
190 release_function_indicator("release_resource");
192 return_implies_state("__request_region", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(1));
193 return_implies_state("__request_region", 0, 0, &request_denied, INT_PTR(1));
194 add_function_hook("__release_region", &match_release, INT_PTR(1));
195 release_function_indicator("__release_region");
197 return_implies_state("ioremap", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(-1));
198 return_implies_state("ioremap", 0, 0, &request_denied, INT_PTR(-1));
199 add_function_hook("iounmap", &match_release, INT_PTR(0));
201 return_implies_state("pci_iomap", valid_ptr_min, valid_ptr_max, &request_granted, INT_PTR(-1));
202 return_implies_state("pci_iomap", 0, 0, &request_denied, INT_PTR(-1));
203 add_function_hook("pci_iounmap", &match_release, INT_PTR(1));
204 release_function_indicator("pci_iounmap");
206 return_implies_state("__create_workqueue_key", valid_ptr_min, valid_ptr_max, &request_granted,
207 INT_PTR(-1));
208 return_implies_state("__create_workqueue_key", 0, 0, &request_denied, INT_PTR(-1));
209 add_function_hook("destroy_workqueue", &match_release, INT_PTR(0));
211 return_implies_state("request_irq", 0, 0, &request_granted, INT_PTR(0));
212 return_implies_state("request_irq", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
213 add_function_hook("free_irq", &match_release, INT_PTR(0));
214 release_function_indicator("free_irq");
216 return_implies_state("register_netdev", 0, 0, &request_granted, INT_PTR(0));
217 return_implies_state("register_netdev", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
218 add_function_hook("unregister_netdev", &match_release, INT_PTR(0));
219 release_function_indicator("unregister_netdev");
221 return_implies_state("misc_register", 0, 0, &request_granted, INT_PTR(0));
222 return_implies_state("misc_register", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
223 add_function_hook("misc_deregister", &match_release, INT_PTR(0));
224 release_function_indicator("misc_deregister");
227 add_hook(&match_return, RETURN_HOOK);