function_hooks: fix locking false positives when you have no db
[smatch.git] / check_deref.c
blobaf8aca86c3f8a4681ba6a562678824e87a0b493c
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * There was a previous null dereference test but it was too confusing and
20 * difficult to debug. This test is much simpler in its goals and scope.
22 * This test only complains about:
23 * 1) dereferencing uninitialized variables
24 * 2) dereferencing variables which were assigned as null.
25 * 3) dereferencing variables which were assigned a function the returns
26 * null.
28 * If we dereference something then we complain if any of those three
29 * are possible.
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
39 #define __GFP_NOFAIL 0x800
41 STATE(null);
42 STATE(ok);
43 STATE(uninitialized);
45 static struct smatch_state *alloc_my_state(const char *name)
47 struct smatch_state *state;
49 state = __alloc_smatch_state(0);
50 state->name = name;
51 return state;
54 static struct smatch_state *unmatched_state(struct sm_state *sm)
56 return &ok;
59 static void is_ok(struct sm_state *sm, struct expression *mod_expr)
61 set_state(my_id, sm->name, sm->sym, &ok);
64 static void check_dereference(struct expression *expr)
66 struct sm_state *sm;
67 struct sm_state *tmp;
69 expr = strip_expr(expr);
70 if (is_static(expr))
71 return;
72 sm = get_sm_state_expr(my_id, expr);
73 if (!sm)
74 return;
75 if (is_ignored(my_id, sm->name, sm->sym))
76 return;
77 if (implied_not_equal(expr, 0))
78 return;
79 if (is_impossible_path())
80 return;
82 FOR_EACH_PTR(sm->possible, tmp) {
83 if (tmp->state == &merged)
84 continue;
85 if (tmp->state == &ok)
86 continue;
87 add_ignore(my_id, sm->name, sm->sym);
88 if (tmp->state == &null) {
89 if (option_spammy)
90 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
91 return;
93 if (tmp->state == &uninitialized) {
94 if (option_spammy)
95 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
96 return;
98 sm_msg("error: potential null dereference '%s'. (%s returns null)",
99 tmp->name, tmp->state->name);
100 return;
101 } END_FOR_EACH_PTR(tmp);
104 static void check_dereference_name_sym(char *name, struct symbol *sym)
106 struct sm_state *sm;
107 struct sm_state *tmp;
109 sm = get_sm_state(my_id, name, sym);
110 if (!sm)
111 return;
112 if (is_ignored(my_id, sm->name, sm->sym))
113 return;
114 if (implied_not_equal_name_sym(name, sym, 0))
115 return;
116 if (is_impossible_path())
117 return;
119 FOR_EACH_PTR(sm->possible, tmp) {
120 if (tmp->state == &merged)
121 continue;
122 if (tmp->state == &ok)
123 continue;
124 add_ignore(my_id, sm->name, sm->sym);
125 if (tmp->state == &null) {
126 if (option_spammy)
127 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
128 return;
130 if (tmp->state == &uninitialized) {
131 if (option_spammy)
132 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
133 return;
135 sm_msg("error: potential null dereference '%s'. (%s returns null)",
136 tmp->name, tmp->state->name);
137 return;
138 } END_FOR_EACH_PTR(tmp);
141 static void match_dereferences(struct expression *expr)
143 if (expr->type != EXPR_PREOP)
144 return;
145 check_dereference(expr->unop);
148 static void match_pointer_as_array(struct expression *expr)
150 if (!is_array(expr))
151 return;
152 check_dereference(get_array_base(expr));
155 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
157 struct symbol *sym;
158 char *name;
160 name = get_variable_from_key(arg, key, &sym);
161 if (!name || !sym)
162 goto free;
164 check_dereference_name_sym(name, sym);
165 free:
166 free_string(name);
169 static void match_declarations(struct symbol *sym)
171 const char *name;
173 if ((get_base_type(sym))->type == SYM_ARRAY)
174 return;
176 if (!sym->ident)
177 return;
178 name = sym->ident->name;
179 if (!sym->initializer) {
180 set_state(my_id, name, sym, &uninitialized);
181 scoped_state(my_id, name, sym);
185 static void match_assign(struct expression *expr)
187 struct statement *stmt;
189 if (!is_zero(expr->right))
190 return;
192 if (__in_fake_assign)
193 return;
195 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
196 if (stmt->type == STMT_DECLARATION)
197 return;
198 break;
199 } END_FOR_EACH_PTR_REVERSE(stmt);
201 set_state_expr(my_id, expr->left, &null);
204 static void match_condition(struct expression *expr)
206 if (expr->type == EXPR_ASSIGNMENT) {
207 match_condition(expr->right);
208 match_condition(expr->left);
210 if (!get_state_expr(my_id, expr))
211 return;
212 set_true_false_states_expr(my_id, expr, &ok, NULL);
215 static int called_with_no_fail(struct expression *call, int param)
217 struct expression *arg;
218 sval_t sval;
220 if (param == -1)
221 return 0;
222 call = strip_expr(call);
223 if (call->type != EXPR_CALL)
224 return 0;
225 arg = get_argument_from_call_expr(call->args, param);
226 if (get_value(arg, &sval) && (sval.uvalue & __GFP_NOFAIL))
227 return 1;
228 return 0;
231 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
233 struct smatch_state *state;
234 int gfp_param = PTR_INT(_gfp);
236 if (called_with_no_fail(expr->right, gfp_param))
237 return;
238 state = alloc_my_state(fn);
239 set_state_expr(my_id, expr->left, state);
242 static void register_allocation_funcs(void)
244 struct token *token;
245 const char *func;
246 int arg;
248 token = get_tokens_file("kernel.allocation_funcs_gfp");
249 if (!token)
250 return;
251 if (token_type(token) != TOKEN_STREAMBEGIN)
252 return;
253 token = token->next;
254 while (token_type(token) != TOKEN_STREAMEND) {
255 if (token_type(token) != TOKEN_IDENT)
256 return;
257 func = show_ident(token->ident);
258 token = token->next;
259 if (token_type(token) == TOKEN_IDENT)
260 arg = -1;
261 else if (token_type(token) == TOKEN_NUMBER)
262 arg = atoi(token->number);
263 else
264 return;
265 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
266 token = token->next;
268 clear_token_alloc();
271 void check_deref(int id)
273 my_id = id;
275 add_unmatched_state_hook(my_id, &unmatched_state);
276 add_modification_hook(my_id, &is_ok);
277 add_hook(&match_dereferences, DEREF_HOOK);
278 add_hook(&match_pointer_as_array, OP_HOOK);
279 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
280 add_hook(&match_condition, CONDITION_HOOK);
281 add_hook(&match_declarations, DECLARATION_HOOK);
282 add_hook(&match_assign, ASSIGNMENT_HOOK);
283 if (option_project == PROJ_KERNEL)
284 register_allocation_funcs();