untracked_param: add a call back for when we lose track of a parameter
[smatch.git] / check_deref.c
blob3e4787bc8f87d2b1a5342ce9f8d955841fb6e1dd
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 sm = get_sm_state_expr(my_id, expr);
71 if (!sm)
72 return;
73 if (is_ignored(my_id, sm->name, sm->sym))
74 return;
75 if (implied_not_equal(expr, 0))
76 return;
78 FOR_EACH_PTR(sm->possible, tmp) {
79 if (tmp->state == &merged)
80 continue;
81 if (tmp->state == &ok)
82 continue;
83 add_ignore(my_id, sm->name, sm->sym);
84 if (tmp->state == &null) {
85 if (option_spammy)
86 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
87 return;
89 if (tmp->state == &uninitialized) {
90 if (option_spammy)
91 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
92 return;
94 sm_msg("error: potential null dereference '%s'. (%s returns null)",
95 tmp->name, tmp->state->name);
96 return;
97 } END_FOR_EACH_PTR(tmp);
100 static void check_dereference_name_sym(char *name, struct symbol *sym)
102 struct sm_state *sm;
103 struct sm_state *tmp;
105 sm = get_sm_state(my_id, name, sym);
106 if (!sm)
107 return;
108 if (is_ignored(my_id, sm->name, sm->sym))
109 return;
110 if (implied_not_equal_name_sym(name, sym, 0))
111 return;
113 FOR_EACH_PTR(sm->possible, tmp) {
114 if (tmp->state == &merged)
115 continue;
116 if (tmp->state == &ok)
117 continue;
118 add_ignore(my_id, sm->name, sm->sym);
119 if (tmp->state == &null) {
120 if (option_spammy)
121 sm_msg("error: potential NULL dereference '%s'.", tmp->name);
122 return;
124 if (tmp->state == &uninitialized) {
125 if (option_spammy)
126 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp->name);
127 return;
129 sm_msg("error: potential null dereference '%s'. (%s returns null)",
130 tmp->name, tmp->state->name);
131 return;
132 } END_FOR_EACH_PTR(tmp);
135 static void match_dereferences(struct expression *expr)
137 if (expr->type != EXPR_PREOP)
138 return;
139 check_dereference(expr->unop);
142 static void match_pointer_as_array(struct expression *expr)
144 if (!is_array(expr))
145 return;
146 check_dereference(expr->unop->left);
149 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
151 struct symbol *sym;
152 char *name;
154 name = get_variable_from_key(arg, key, &sym);
155 if (!name || !sym)
156 goto free;
158 check_dereference_name_sym(name, sym);
159 free:
160 free_string(name);
163 static void match_declarations(struct symbol *sym)
165 const char *name;
167 if ((get_base_type(sym))->type == SYM_ARRAY)
168 return;
170 if (!sym->ident)
171 return;
172 name = sym->ident->name;
173 if (!sym->initializer) {
174 set_state(my_id, name, sym, &uninitialized);
175 scoped_state(my_id, name, sym);
179 static void match_assign(struct expression *expr)
181 struct statement *stmt;
183 if (!is_zero(expr->right))
184 return;
186 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
187 if (stmt->type == STMT_DECLARATION)
188 return;
189 break;
190 } END_FOR_EACH_PTR_REVERSE(stmt);
192 set_state_expr(my_id, expr->left, &null);
195 static void match_condition(struct expression *expr)
197 if (expr->type == EXPR_ASSIGNMENT) {
198 match_condition(expr->right);
199 match_condition(expr->left);
201 if (!get_state_expr(my_id, expr))
202 return;
203 set_true_false_states_expr(my_id, expr, &ok, NULL);
206 static int called_with_no_fail(struct expression *call, int param)
208 struct expression *arg;
209 sval_t sval;
211 if (param == -1)
212 return 0;
213 call = strip_expr(call);
214 if (call->type != EXPR_CALL)
215 return 0;
216 arg = get_argument_from_call_expr(call->args, param);
217 if (get_value(arg, &sval) && (sval.uvalue & __GFP_NOFAIL))
218 return 1;
219 return 0;
222 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
224 struct smatch_state *state;
225 int gfp_param = PTR_INT(_gfp);
227 if (called_with_no_fail(expr->right, gfp_param))
228 return;
229 state = alloc_my_state(fn);
230 set_state_expr(my_id, expr->left, state);
233 static void register_allocation_funcs(void)
235 struct token *token;
236 const char *func;
237 int arg;
239 token = get_tokens_file("kernel.allocation_funcs_gfp");
240 if (!token)
241 return;
242 if (token_type(token) != TOKEN_STREAMBEGIN)
243 return;
244 token = token->next;
245 while (token_type(token) != TOKEN_STREAMEND) {
246 if (token_type(token) != TOKEN_IDENT)
247 return;
248 func = show_ident(token->ident);
249 token = token->next;
250 if (token_type(token) == TOKEN_IDENT)
251 arg = -1;
252 else if (token_type(token) == TOKEN_NUMBER)
253 arg = atoi(token->number);
254 else
255 return;
256 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
257 token = token->next;
259 clear_token_alloc();
262 void check_deref(int id)
264 my_id = id;
266 add_unmatched_state_hook(my_id, &unmatched_state);
267 add_modification_hook(my_id, &is_ok);
268 add_hook(&match_dereferences, DEREF_HOOK);
269 add_hook(&match_pointer_as_array, OP_HOOK);
270 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
271 add_hook(&match_condition, CONDITION_HOOK);
272 add_hook(&match_declarations, DECLARATION_HOOK);
273 add_hook(&match_assign, ASSIGNMENT_HOOK);
274 if (option_project == PROJ_KERNEL)
275 register_allocation_funcs();