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
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
29 #define MAX_ERRNO 4095
36 /* state of unwind function */
39 static int was_passed_as_param(struct expression
*expr
)
45 name
= expr_to_var_sym(expr
, &sym
);
50 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
53 } END_FOR_EACH_PTR(arg
);
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
))
66 if (last_printed
== cur_func_sym
)
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
);
81 arg_expr
= assign_expr
->left
;
83 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
85 set_state_expr(my_id
, arg_expr
, &allocated
);
88 static void request_denied(const char *fn
, struct expression
*call_expr
,
89 struct expression
*assign_expr
, void *_arg_no
)
91 struct expression
*arg_expr
;
92 int arg_no
= PTR_INT(_arg_no
);
97 arg_expr
= assign_expr
->left
;
99 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
101 set_state_expr(my_id
, arg_expr
, &unallocated
);
104 static void match_release(const char *fn
, struct expression
*expr
, void *_arg_no
)
106 struct expression
*arg_expr
;
107 int arg_no
= PTR_INT(_arg_no
);
109 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
110 if (get_state_expr(my_id
, arg_expr
))
111 set_state_expr(my_id
, arg_expr
, &unallocated
);
112 set_equiv_state_expr(my_id
, arg_expr
, &unallocated
);
115 static void match_unwind_function(const char *fn
, struct expression
*expr
, void *unused
)
117 set_state(my_id
, "unwind_function", NULL
, &called
);
120 static int func_returns_int(void)
124 type
= get_base_type(cur_func_sym
);
125 if (!type
|| type
->type
!= SYM_FN
)
127 type
= get_base_type(type
);
128 if (type
->ctype
.base_type
== &int_type
) {
134 static void match_return(struct expression
*ret_value
)
137 struct sm_state
*tmp
;
140 if (!func_returns_int())
142 if (get_value(ret_value
, &sval
) && sval_cmp_val(sval
, 0) >= 0)
144 if (!implied_not_equal(ret_value
, 0))
146 if (get_state(my_id
, "unwind_function", NULL
) == &called
)
149 stree
= __get_cur_stree();
150 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
151 if (slist_has_state(tmp
->possible
, &allocated
))
152 sm_msg("warn: '%s' was not released on error", tmp
->name
);
153 } END_FOR_EACH_SM(tmp
);
156 static void register_unwind_functions(void)
161 token
= get_tokens_file("kernel.unwind_functions");
164 if (token_type(token
) != TOKEN_STREAMBEGIN
)
167 while (token_type(token
) != TOKEN_STREAMEND
) {
168 if (token_type(token
) != TOKEN_IDENT
)
170 func
= show_ident(token
->ident
);
171 add_function_hook(func
, &match_unwind_function
, NULL
);
177 static void release_function_indicator(const char *name
)
181 add_function_hook(name
, &print_unwind_functions
, INT_PTR(0));
184 void check_unwind(int id
)
186 if (option_project
!= PROJ_KERNEL
|| !option_spammy
)
190 register_unwind_functions();
192 return_implies_state("request_resource", 0, 0, &request_granted
, INT_PTR(1));
193 return_implies_state("request_resource", -EBUSY
, -EBUSY
, &request_denied
, INT_PTR(1));
194 add_function_hook("release_resource", &match_release
, INT_PTR(0));
195 release_function_indicator("release_resource");
197 return_implies_state("__request_region", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(1));
198 return_implies_state("__request_region", 0, 0, &request_denied
, INT_PTR(1));
199 add_function_hook("__release_region", &match_release
, INT_PTR(1));
200 release_function_indicator("__release_region");
202 return_implies_state("ioremap", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(-1));
203 return_implies_state("ioremap", 0, 0, &request_denied
, INT_PTR(-1));
204 add_function_hook("iounmap", &match_release
, INT_PTR(0));
206 return_implies_state("pci_iomap", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(-1));
207 return_implies_state("pci_iomap", 0, 0, &request_denied
, INT_PTR(-1));
208 add_function_hook("pci_iounmap", &match_release
, INT_PTR(1));
209 release_function_indicator("pci_iounmap");
211 return_implies_state("__create_workqueue_key", valid_ptr_min
, valid_ptr_max
, &request_granted
,
213 return_implies_state("__create_workqueue_key", 0, 0, &request_denied
, INT_PTR(-1));
214 add_function_hook("destroy_workqueue", &match_release
, INT_PTR(0));
216 return_implies_state("request_irq", 0, 0, &request_granted
, INT_PTR(0));
217 return_implies_state("request_irq", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
218 add_function_hook("free_irq", &match_release
, INT_PTR(0));
219 release_function_indicator("free_irq");
221 return_implies_state("register_netdev", 0, 0, &request_granted
, INT_PTR(0));
222 return_implies_state("register_netdev", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
223 add_function_hook("unregister_netdev", &match_release
, INT_PTR(0));
224 release_function_indicator("unregister_netdev");
226 return_implies_state("misc_register", 0, 0, &request_granted
, INT_PTR(0));
227 return_implies_state("misc_register", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
228 add_function_hook("misc_deregister", &match_release
, INT_PTR(0));
229 release_function_indicator("misc_deregister");
232 add_hook(&match_return
, RETURN_HOOK
);