implied_not_equal: handle literals
[smatch.git] / check_unwind.c
blob615617c1e3e9da2b7e6b7ca3880a8044321a133b
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 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;
34 if (arg_no == -1)
35 arg_expr = assign_expr->left;
36 else
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;
47 if (arg_no == -1)
48 arg_expr = assign_expr->left;
49 else
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))
61 return;
62 set_state_expr(my_id, arg_expr, &unallocated);
65 static int func_returns_int()
67 struct symbol *type;
69 type = get_base_type(cur_func_sym);
70 if (!type || type->type != SYM_FN)
71 return 0;
72 type = get_base_type(type);
73 if (type->ctype.base_type == &int_type) {
74 return 1;
76 return 0;
79 static void match_return(struct expression *ret_value)
81 struct state_list *slist;
82 struct sm_state *tmp;
84 if (!func_returns_int())
85 return;
86 if (!implied_not_equal(ret_value, 0))
87 return;
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);
94 free_slist(&slist);
97 void check_unwind(int id)
99 if (option_project != PROJ_KERNEL)
100 return;
101 my_id = id;
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,
120 INT_PTR(-1));
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);