locking: ignore unreachable unlocks
[smatch.git] / check_deref.c
blob78905b21009f9cd51aad28e8ec284980fcd653ac
1 /*
2 * sparse/check_deref.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * There was a previous null dereference test but it was too confusing and
12 * difficult to debug. This test is much simpler in its goals and scope.
14 * This test only complains about:
15 * 1) dereferencing uninitialized variables
16 * 2) dereferencing variables which were assigned as null.
17 * 3) dereferencing variables which were assigned a function the returns
18 * null.
20 * If we dereference something then we complain if any of those three
21 * are possible.
25 #include "smatch.h"
26 #include "smatch_slist.h"
27 #include "smatch_extra.h"
29 static int my_id;
31 #define __GFP_NOFAIL 0x800
33 STATE(null);
34 STATE(ok);
35 STATE(uninitialized);
37 static struct smatch_state *alloc_my_state(const char *name)
39 struct smatch_state *state;
41 state = malloc(sizeof(*state));
42 state->name = name;
43 return state;
46 static struct smatch_state *unmatched_state(struct sm_state *sm)
48 return &ok;
51 static void is_ok(const char *name, struct symbol *sym, struct expression *expr, void *unused)
53 set_state(my_id, name, sym, &ok);
56 static void check_dereference(struct expression *expr)
58 struct sm_state *sm;
59 struct sm_state *tmp;
61 expr = strip_expr(expr);
62 sm = get_sm_state_expr(my_id, expr);
63 if (!sm)
64 return;
65 if (is_ignored(my_id, sm->name, sm->sym))
66 return;
67 if (implied_not_equal(expr, 0))
68 return;
70 FOR_EACH_PTR(sm->possible, tmp) {
71 if (tmp->state == &merged)
72 continue;
73 if (tmp->state == &ok)
74 continue;
75 add_ignore(my_id, sm->name, sm->sym);
76 if (tmp->state == &null) {
77 sm_msg("error: potential null derefence '%s'.", tmp->name);
78 return;
80 if (tmp->state == &uninitialized) {
81 sm_msg("error: potentially derefencing uninitialized '%s'.", tmp->name);
82 return;
84 sm_msg("error: potential null dereference '%s'. (%s returns null)",
85 tmp->name, tmp->state->name);
86 return;
87 } END_FOR_EACH_PTR(tmp);
90 static void match_dereferences(struct expression *expr)
92 if (expr->type != EXPR_PREOP)
93 return;
94 check_dereference(expr->unop);
97 static void match_pointer_as_array(struct expression *expr)
99 if (!is_array(expr))
100 return;
101 check_dereference(expr->unop->left);
104 static void match_declarations(struct symbol *sym)
106 const char *name;
108 if ((get_base_type(sym))->type == SYM_ARRAY)
109 return;
111 name = sym->ident->name;
112 if (!sym->initializer) {
113 set_state(my_id, name, sym, &uninitialized);
114 scoped_state(my_id, name, sym);
118 static void match_assign(struct expression *expr)
120 if (is_zero(expr->right)) {
121 set_state_expr(my_id, expr->left, &null);
122 return;
126 static void match_condition(struct expression *expr)
128 if (expr->type == EXPR_ASSIGNMENT) {
129 match_condition(expr->right);
130 match_condition(expr->left);
132 if (!get_state_expr(my_id, expr))
133 return;
134 set_true_false_states_expr(my_id, expr, &ok, NULL);
137 static int called_with_no_fail(struct expression *call, int param)
139 struct expression *arg;
140 long long val;
142 if (param == -1)
143 return 0;
144 call = strip_expr(call);
145 if (call->type != EXPR_CALL)
146 return 0;
147 arg = get_argument_from_call_expr(call->args, param);
148 if (get_value(arg, &val) && (val & __GFP_NOFAIL))
149 return 1;
150 return 0;
153 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
155 struct smatch_state *state;
156 int gfp_param = PTR_INT(_gfp);
158 if (called_with_no_fail(expr->right, gfp_param))
159 return;
160 state = alloc_my_state(fn);
161 set_state_expr(my_id, expr->left, state);
164 static void register_allocation_funcs(void)
166 struct token *token;
167 const char *func;
168 int arg;
170 token = get_tokens_file("kernel.allocation_funcs_gfp");
171 if (!token)
172 return;
173 if (token_type(token) != TOKEN_STREAMBEGIN)
174 return;
175 token = token->next;
176 while (token_type(token) != TOKEN_STREAMEND) {
177 if (token_type(token) != TOKEN_IDENT)
178 return;
179 func = show_ident(token->ident);
180 token = token->next;
181 if (token_type(token) == TOKEN_IDENT)
182 arg = -1;
183 else if (token_type(token) == TOKEN_NUMBER)
184 arg = atoi(token->number);
185 else
186 return;
187 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
188 token = token->next;
190 clear_token_alloc();
193 void check_deref(int id)
195 my_id = id;
197 add_unmatched_state_hook(my_id, &unmatched_state);
198 set_default_modification_hook(my_id, &is_ok);
199 add_hook(&match_dereferences, DEREF_HOOK);
200 add_hook(&match_pointer_as_array, OP_HOOK);
201 add_hook(&match_condition, CONDITION_HOOK);
202 add_hook(&match_declarations, DECLARATION_HOOK);
203 add_hook(&match_assign, ASSIGNMENT_HOOK);
204 if (option_project == PROJ_KERNEL)
205 register_allocation_funcs();