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 static void request_granted(const char *fn
, struct expression
*call_expr
,
29 struct expression
*assign_expr
, void *_arg_no
)
31 struct expression
*arg_expr
;
32 int arg_no
= (int)_arg_no
;
35 arg_expr
= assign_expr
->left
;
37 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
38 set_state_expr(my_id
, arg_expr
, &allocated
);
41 static void request_denied(const char *fn
, struct expression
*call_expr
,
42 struct expression
*assign_expr
, void *_arg_no
)
44 struct expression
*arg_expr
;
45 int arg_no
= (int)_arg_no
;
48 arg_expr
= assign_expr
->left
;
50 arg_expr
= get_argument_from_call_expr(call_expr
->args
, arg_no
);
51 set_state_expr(my_id
, arg_expr
, &unallocated
);
54 static void match_release(const char *fn
, struct expression
*expr
, void *_arg_no
)
56 struct expression
*arg_expr
;
57 int arg_no
= (int)_arg_no
;
59 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
60 if (!get_state_expr(my_id
, arg_expr
))
62 set_state_expr(my_id
, arg_expr
, &unallocated
);
65 static int func_returns_int()
69 type
= get_base_type(cur_func_sym
);
70 if (!type
|| type
->type
!= SYM_FN
)
72 type
= get_base_type(type
);
73 if (type
->ctype
.base_type
== &int_type
) {
79 static void match_return(struct expression
*ret_value
)
81 struct state_list
*slist
;
84 if (!func_returns_int())
86 if (!implied_not_equal(ret_value
, 0))
89 slist
= get_all_states(my_id
);
90 FOR_EACH_PTR(slist
, tmp
) {
91 if (slist_has_state(tmp
->possible
, &allocated
))
92 sm_msg("warn: '%s' was not released on error", tmp
->name
);
93 } END_FOR_EACH_PTR(tmp
);
97 void check_unwind(int id
)
99 if (option_project
!= PROJ_KERNEL
)
103 return_implies_state("request_resource", 0, 0, &request_granted
, INT_PTR(1));
104 return_implies_state("request_resource", -EBUSY
, -EBUSY
, &request_denied
, INT_PTR(1));
105 add_function_hook("release_resource", &match_release
, INT_PTR(0));
107 return_implies_state("__request_region", 1, POINTER_MAX
, &request_granted
, INT_PTR(1));
108 return_implies_state("__request_region", 0, 0, &request_denied
, INT_PTR(1));
109 add_function_hook("__release_region", &match_release
, INT_PTR(1));
111 return_implies_state("ioremap", 1, POINTER_MAX
, &request_granted
, INT_PTR(-1));
112 return_implies_state("ioremap", 0, 0, &request_denied
, INT_PTR(-1));
113 add_function_hook("iounmap", &match_release
, INT_PTR(0));
115 return_implies_state("pci_iomap", 1, POINTER_MAX
, &request_granted
, INT_PTR(-1));
116 return_implies_state("pci_iomap", 0, 0, &request_denied
, INT_PTR(-1));
117 add_function_hook("pci_iounmap", &match_release
, INT_PTR(1));
119 return_implies_state("__create_workqueue_key", 1, POINTER_MAX
, &request_granted
,
121 return_implies_state("__create_workqueue_key", 0, 0, &request_denied
, INT_PTR(-1));
122 add_function_hook("destroy_workqueue", &match_release
, INT_PTR(0));
124 return_implies_state("request_irq", 0, 0, &request_granted
, INT_PTR(0));
125 return_implies_state("request_irq", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
126 add_function_hook("free_irq", &match_release
, INT_PTR(0));
128 return_implies_state("register_netdev", 0, 0, &request_granted
, INT_PTR(0));
129 return_implies_state("register_netdev", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
130 add_function_hook("unregister_netdev", &match_release
, INT_PTR(0));
132 return_implies_state("misc_register", 0, 0, &request_granted
, INT_PTR(0));
133 return_implies_state("misc_register", -MAX_ERRNO
, -1, &request_denied
, INT_PTR(0));
134 add_function_hook("misc_deregister", &match_release
, INT_PTR(0));
136 add_hook(&match_return
, RETURN_HOOK
);