unwind: register unwind functions that unwind everything
[smatch.git] / check_unwind.c
blob619a41b33e5dde0bcfed360720c2bc271d3b999c
1 /*
2 * smatch/check_unwind.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is a kernel check to make sure we unwind everything on
12 * on errors.
16 #include "smatch.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 #define EBUSY 16
21 #define MAX_ERRNO 4095
23 static int my_id;
25 STATE(allocated);
26 STATE(unallocated);
28 /* state of unwind function */
29 STATE(called);
31 static int was_passed_as_param(struct expression *expr)
33 char *name;
34 struct symbol *sym;
35 struct symbol *arg;
37 name = get_variable_from_expr(expr, &sym);
38 if (!name)
39 return 0;
40 free_string(name);
42 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
43 if (arg == sym)
44 return 1;
45 } END_FOR_EACH_PTR(arg);
46 return 0;
49 static void print_unwind_functions(const char *fn, struct expression *expr, void *_arg_no)
51 struct expression *arg_expr;
52 int arg_no = (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))
57 return;
58 if (last_printed == cur_func_sym)
59 return;
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 = (int)_arg_no;
70 if (arg_no == -1)
71 arg_expr = assign_expr->left;
72 else
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 = (int)_arg_no;
83 if (arg_no == -1)
84 arg_expr = assign_expr->left;
85 else
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 = (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()
108 struct symbol *type;
110 type = get_base_type(cur_func_sym);
111 if (!type || type->type != SYM_FN)
112 return 0;
113 type = get_base_type(type);
114 if (type->ctype.base_type == &int_type) {
115 return 1;
117 return 0;
120 static void match_return(struct expression *ret_value)
122 struct state_list *slist;
123 struct sm_state *tmp;
125 if (!func_returns_int())
126 return;
127 if (!implied_not_equal(ret_value, 0))
128 return;
129 if (get_state(my_id, "unwind_function", NULL) == &called)
130 return;
132 slist = get_all_states(my_id);
133 FOR_EACH_PTR(slist, tmp) {
134 if (slist_has_state(tmp->possible, &allocated))
135 sm_msg("warn: '%s' was not released on error", tmp->name);
136 } END_FOR_EACH_PTR(tmp);
137 free_slist(&slist);
140 static void register_unwind_functions(void)
142 struct token *token;
143 const char *func;
145 token = get_tokens_file("kernel.unwind_functions");
146 if (!token)
147 return;
148 if (token_type(token) != TOKEN_STREAMBEGIN)
149 return;
150 token = token->next;
151 while (token_type(token) != TOKEN_STREAMEND) {
152 if (token_type(token) != TOKEN_IDENT)
153 return;
154 func = show_ident(token->ident);
155 add_function_hook(func, &match_unwind_function, NULL);
156 token = token->next;
158 clear_token_alloc();
161 void check_unwind(int id)
163 if (option_project != PROJ_KERNEL)
164 return;
165 my_id = id;
167 register_unwind_functions();
169 return_implies_state("request_resource", 0, 0, &request_granted, INT_PTR(1));
170 return_implies_state("request_resource", -EBUSY, -EBUSY, &request_denied, INT_PTR(1));
171 add_function_hook("release_resource", &match_release, INT_PTR(0));
173 return_implies_state("__request_region", 1, POINTER_MAX, &request_granted, INT_PTR(1));
174 return_implies_state("__request_region", 0, 0, &request_denied, INT_PTR(1));
175 add_function_hook("__release_region", &match_release, INT_PTR(1));
177 return_implies_state("ioremap", 1, POINTER_MAX, &request_granted, INT_PTR(-1));
178 return_implies_state("ioremap", 0, 0, &request_denied, INT_PTR(-1));
179 add_function_hook("iounmap", &match_release, INT_PTR(0));
181 return_implies_state("pci_iomap", 1, POINTER_MAX, &request_granted, INT_PTR(-1));
182 return_implies_state("pci_iomap", 0, 0, &request_denied, INT_PTR(-1));
183 add_function_hook("pci_iounmap", &match_release, INT_PTR(1));
185 return_implies_state("__create_workqueue_key", 1, POINTER_MAX, &request_granted,
186 INT_PTR(-1));
187 return_implies_state("__create_workqueue_key", 0, 0, &request_denied, INT_PTR(-1));
188 add_function_hook("destroy_workqueue", &match_release, INT_PTR(0));
190 return_implies_state("request_irq", 0, 0, &request_granted, INT_PTR(0));
191 return_implies_state("request_irq", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
192 add_function_hook("free_irq", &match_release, INT_PTR(0));
194 return_implies_state("register_netdev", 0, 0, &request_granted, INT_PTR(0));
195 return_implies_state("register_netdev", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
196 add_function_hook("unregister_netdev", &match_release, INT_PTR(0));
198 return_implies_state("misc_register", 0, 0, &request_granted, INT_PTR(0));
199 return_implies_state("misc_register", -MAX_ERRNO, -1, &request_denied, INT_PTR(0));
200 add_function_hook("misc_deregister", &match_release, INT_PTR(0));
202 add_hook(&match_return, RETURN_HOOK);
204 if (option_info)
205 add_function_hook("free_irq", &print_unwind_functions, INT_PTR(0));