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
28 * If we dereference something then we complain if any of those three
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
39 #define __GFP_NOFAIL 0x800
45 static struct smatch_state
*alloc_my_state(const char *name
)
47 struct smatch_state
*state
;
49 state
= __alloc_smatch_state(0);
54 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
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
)
69 expr
= strip_expr(expr
);
70 sm
= get_sm_state_expr(my_id
, expr
);
73 if (is_ignored(my_id
, sm
->name
, sm
->sym
))
75 if (implied_not_equal(expr
, 0))
78 FOR_EACH_PTR(sm
->possible
, tmp
) {
79 if (tmp
->state
== &merged
)
81 if (tmp
->state
== &ok
)
83 add_ignore(my_id
, sm
->name
, sm
->sym
);
84 if (tmp
->state
== &null
) {
86 sm_msg("error: potential NULL dereference '%s'.", tmp
->name
);
89 if (tmp
->state
== &uninitialized
) {
91 sm_msg("error: potentially dereferencing uninitialized '%s'.", tmp
->name
);
94 sm_msg("error: potential null dereference '%s'. (%s returns null)",
95 tmp
->name
, tmp
->state
->name
);
97 } END_FOR_EACH_PTR(tmp
);
100 static void match_dereferences(struct expression
*expr
)
102 if (expr
->type
!= EXPR_PREOP
)
104 check_dereference(expr
->unop
);
107 static void match_pointer_as_array(struct expression
*expr
)
111 check_dereference(expr
->unop
->left
);
114 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
116 check_dereference(arg
);
119 static void match_declarations(struct symbol
*sym
)
123 if ((get_base_type(sym
))->type
== SYM_ARRAY
)
128 name
= sym
->ident
->name
;
129 if (!sym
->initializer
) {
130 set_state(my_id
, name
, sym
, &uninitialized
);
131 scoped_state(my_id
, name
, sym
);
135 static void match_assign(struct expression
*expr
)
137 struct statement
*stmt
;
139 if (!is_zero(expr
->right
))
142 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
143 if (stmt
->type
== STMT_DECLARATION
)
146 } END_FOR_EACH_PTR_REVERSE(stmt
);
148 set_state_expr(my_id
, expr
->left
, &null
);
151 static void match_condition(struct expression
*expr
)
153 if (expr
->type
== EXPR_ASSIGNMENT
) {
154 match_condition(expr
->right
);
155 match_condition(expr
->left
);
157 if (!get_state_expr(my_id
, expr
))
159 set_true_false_states_expr(my_id
, expr
, &ok
, NULL
);
162 static int called_with_no_fail(struct expression
*call
, int param
)
164 struct expression
*arg
;
169 call
= strip_expr(call
);
170 if (call
->type
!= EXPR_CALL
)
172 arg
= get_argument_from_call_expr(call
->args
, param
);
173 if (get_value(arg
, &sval
) && (sval
.uvalue
& __GFP_NOFAIL
))
178 static void match_assign_returns_null(const char *fn
, struct expression
*expr
, void *_gfp
)
180 struct smatch_state
*state
;
181 int gfp_param
= PTR_INT(_gfp
);
183 if (called_with_no_fail(expr
->right
, gfp_param
))
185 state
= alloc_my_state(fn
);
186 set_state_expr(my_id
, expr
->left
, state
);
189 static void register_allocation_funcs(void)
195 token
= get_tokens_file("kernel.allocation_funcs_gfp");
198 if (token_type(token
) != TOKEN_STREAMBEGIN
)
201 while (token_type(token
) != TOKEN_STREAMEND
) {
202 if (token_type(token
) != TOKEN_IDENT
)
204 func
= show_ident(token
->ident
);
206 if (token_type(token
) == TOKEN_IDENT
)
208 else if (token_type(token
) == TOKEN_NUMBER
)
209 arg
= atoi(token
->number
);
212 add_function_assign_hook(func
, &match_assign_returns_null
, INT_PTR(arg
));
218 void check_deref(int id
)
222 add_unmatched_state_hook(my_id
, &unmatched_state
);
223 add_modification_hook(my_id
, &is_ok
);
224 add_hook(&match_dereferences
, DEREF_HOOK
);
225 add_hook(&match_pointer_as_array
, OP_HOOK
);
226 select_call_implies_hook(DEREFERENCE
, &set_param_dereferenced
);
227 add_hook(&match_condition
, CONDITION_HOOK
);
228 add_hook(&match_declarations
, DECLARATION_HOOK
);
229 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
230 if (option_project
== PROJ_KERNEL
)
231 register_allocation_funcs();