2 * smatch/check_unwind.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * This is a kernel check to make sure we unwind everything on
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
21 #define MAX_ERRNO 4095
28 /* state of unwind function */
31 static int was_passed_as_param(struct expression
*expr
)
37 name
= get_variable_from_expr(expr
, &sym
);
42 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
45 } END_FOR_EACH_PTR(arg
);
49 static void print_unwind_functions(const char *fn
, struct expression
*expr
, void *_arg_no
)
51 struct expression
*arg_expr
;
52 int arg_no
= PTR_INT(_arg_no
);
53 static struct symbol
*last_printed
= NULL
;
55 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
56 if (!was_passed_as_param(arg_expr
))
58 if (last_printed
== cur_func_sym
)
60 last_printed
= cur_func_sym
;
61 sm_msg("info: is unwind function");
64 static void request_granted(const char *fn
, struct expression
*call_expr
,
65 struct expression
*assign_expr
, void *_arg_no
)
67 struct expression
*arg_expr
;
68 int arg_no
= PTR_INT(_arg_no
);
71 arg_expr
= assign_expr
->left
;
73 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
74 set_state_expr(my_id
, arg_expr
, &allocated
);
77 static void request_denied(const char *fn
, struct expression
*call_expr
,
78 struct expression
*assign_expr
, void *_arg_no
)
80 struct expression
*arg_expr
;
81 int arg_no
= PTR_INT(_arg_no
);
84 arg_expr
= assign_expr
->left
;
86 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
87 set_state_expr(my_id
, arg_expr
, &unallocated
);
90 static void match_release(const char *fn
, struct expression
*expr
, void *_arg_no
)
92 struct expression
*arg_expr
;
93 int arg_no
= PTR_INT(_arg_no
);
95 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
96 if (get_state_expr(my_id
, arg_expr
))
97 set_state_expr(my_id
, arg_expr
, &unallocated
);
98 set_equiv_state_expr(my_id
, arg_expr
, &unallocated
);
101 static void match_unwind_function(const char *fn
, struct expression
*expr
, void *unused
)
103 set_state(my_id
, "unwind_function", NULL
, &called
);
106 static int func_returns_int()
110 type
= get_base_type(cur_func_sym
);
111 if (!type
|| type
->type
!= SYM_FN
)
113 type
= get_base_type(type
);
114 if (type
->ctype
.base_type
== &int_type
) {
120 static void match_return(struct expression
*ret_value
)
122 struct state_list
*slist
;
123 struct sm_state
*tmp
;
126 if (!func_returns_int())
128 if (get_value(ret_value
, &val
) && val
>= 0)
130 if (!implied_not_equal(ret_value
, 0))
132 if (get_state(my_id
, "unwind_function", NULL
) == &called
)
135 slist
= get_all_states(my_id
);
136 FOR_EACH_PTR(slist
, tmp
) {
137 if (slist_has_state(tmp
->possible
, &allocated
))
138 sm_msg("warn: '%s' was not released on error", tmp
->name
);
139 } END_FOR_EACH_PTR(tmp
);
143 static void register_unwind_functions(void)
148 token
= get_tokens_file("kernel.unwind_functions");
151 if (token_type(token
) != TOKEN_STREAMBEGIN
)
154 while (token_type(token
) != TOKEN_STREAMEND
) {
155 if (token_type(token
) != TOKEN_IDENT
)
157 func
= show_ident(token
->ident
);
158 add_function_hook(func
, &match_unwind_function
, NULL
);
164 static void release_function_indicator(const char *name
)
168 add_function_hook(name
, &print_unwind_functions
, INT_PTR(0));
171 void check_unwind(int id
)
173 if (option_project
!= PROJ_KERNEL
)
177 register_unwind_functions();
179 return_implies_state("request_resource", 0, 0, &request_granted
, INT_PTR(1));
180 return_implies_state("request_resource", -EBUSY
, -EBUSY
, &request_denied
, INT_PTR(1));
181 add_function_hook("release_resource", &match_release
, INT_PTR(0));
182 release_function_indicator("release_resource");
184 return_implies_state("__request_region", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(1));
185 return_implies_state("__request_region", 0, 0, &request_denied
, INT_PTR(1));
186 add_function_hook("__release_region", &match_release
, INT_PTR(1));
187 release_function_indicator("__release_region");
189 return_implies_state("ioremap", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(-1));
190 return_implies_state("ioremap", 0, 0, &request_denied
, INT_PTR(-1));
191 add_function_hook("iounmap", &match_release
, INT_PTR(0));
193 return_implies_state("pci_iomap", valid_ptr_min
, valid_ptr_max
, &request_granted
, INT_PTR(-1));
194 return_implies_state("pci_iomap", 0, 0, &request_denied
, INT_PTR(-1));
195 add_function_hook("pci_iounmap", &match_release
, INT_PTR(1));
196 release_function_indicator("pci_iounmap");
198 return_implies_state("__create_workqueue_key", valid_ptr_min
, valid_ptr_max
, &request_granted
,
200 return_implies_state("__create_workqueue_key", 0, 0, &request_denied
, INT_PTR(-1));
201 add_function_hook("destroy_workqueue", &match_release
, INT_PTR(0));
203 return_implies_state("request_irq", 0, 0, &request_granted
, INT_PTR(0));
204 return_implies_state("request_irq", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
205 add_function_hook("free_irq", &match_release
, INT_PTR(0));
206 release_function_indicator("free_irq");
208 return_implies_state("register_netdev", 0, 0, &request_granted
, INT_PTR(0));
209 return_implies_state("register_netdev", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
210 add_function_hook("unregister_netdev", &match_release
, INT_PTR(0));
211 release_function_indicator("unregister_netdev");
213 return_implies_state("misc_register", 0, 0, &request_granted
, INT_PTR(0));
214 return_implies_state("misc_register", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
215 add_function_hook("misc_deregister", &match_release
, INT_PTR(0));
216 release_function_indicator("misc_deregister");
219 add_hook(&match_return
, RETURN_HOOK
);