2 * sparse/check_deference.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_slist.h"
15 * TODO: The return_null list of functions should be determined automatically
17 static const char *return_null
[] = {
26 ALLOCATOR(func_n_param
, "func parameters");
27 DECLARE_PTR_LIST(param_list
, struct func_n_param
);
29 static struct param_list
*funcs
;
30 static struct param_list
*do_not_call
;
31 static struct param_list
*calls
;
38 STATE(assumed_nonnull
);
43 static struct symbol
*func_sym
;
46 * merge_func() is to handle assumed_nonnull.
48 * Assumed_nonnull is a funny state. It's almost the same as
49 * no state. Maybe it would be better to delete it completely...
51 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
52 struct smatch_state
*s1
,
53 struct smatch_state
*s2
)
55 if (s1
== &ignore
|| s2
== &ignore
)
61 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
63 if (sm
->state
== &assumed_nonnull
)
64 return &assumed_nonnull
;
68 static int is_maybe_null_no_arg(const char *name
, struct symbol
*sym
)
70 struct state_list
*slist
;
74 slist
= get_possible_states(my_id
, name
, sym
);
75 FOR_EACH_PTR(slist
, tmp
) {
76 if (tmp
->state
== &ignore
)
78 if (tmp
->state
== &isnull
) {
79 sm_debug("is_maybe_null_no_arg() says %s &isnull\n", name
);
82 if (tmp
->state
== &undefined
) {
83 sm_debug("is_maybe_null_no_arg() says %s &undefined\n", name
);
86 } END_FOR_EACH_PTR(tmp
);
91 static int is_maybe_null(const char *name
, struct symbol
*sym
)
93 struct state_list
*slist
;
97 slist
= get_possible_states(my_id
, name
, sym
);
98 FOR_EACH_PTR(slist
, tmp
) {
99 if (tmp
->state
== &ignore
)
101 if (tmp
->state
== &isnull
) {
102 sm_debug("is_maybe_null() says %s &isnull\n", name
);
105 if (tmp
->state
== &undefined
) {
106 sm_debug("is_maybe_null() says %s &undefined\n", name
);
109 if (tmp
->state
== &arg_null
) {
110 sm_debug("is_maybe_null() says %s &arg_null\n", name
);
113 } END_FOR_EACH_PTR(tmp
);
117 static int is_maybe_null_arg(char *name
, struct symbol
*sym
)
119 struct state_list
*slist
;
120 struct sm_state
*tmp
;
123 slist
= get_possible_states(my_id
, name
, sym
);
124 FOR_EACH_PTR(slist
, tmp
) {
125 if (tmp
->state
!= &argument
&& tmp
->state
!= &arg_null
&&
126 tmp
->state
!= &arg_nonnull
&& tmp
->state
!= &merged
&&
127 tmp
->state
!= &assumed_nonnull
)
129 if (tmp
->state
== &argument
|| tmp
->state
== &arg_null
)
131 } END_FOR_EACH_PTR(tmp
);
133 /* We really only care about arguments if they are null */
137 static struct func_n_param
*alloc_func_n_param(struct symbol
*func
, int param
,
140 struct func_n_param
*tmp
= __alloc_func_n_param(0);
148 static int get_arg_num(struct symbol
*sym
)
153 FOR_EACH_PTR(func_sym
->ctype
.base_type
->arguments
, arg
) {
158 } END_FOR_EACH_PTR(arg
);
162 static void add_do_not_call(struct symbol
*sym
, int line
)
164 struct func_n_param
*tmp
;
165 int num
= get_arg_num(sym
);
167 FOR_EACH_PTR(do_not_call
, tmp
) {
168 if (tmp
->func
== func_sym
&& tmp
->param
== num
)
170 } END_FOR_EACH_PTR(tmp
);
171 tmp
= alloc_func_n_param(func_sym
, num
, line
);
172 add_ptr_list(&do_not_call
, tmp
);
175 static void add_param(struct param_list
**list
, struct symbol
*func
, int param
,
178 struct func_n_param
*tmp
;
180 tmp
= alloc_func_n_param(func
, param
, line
);
181 add_ptr_list(list
, tmp
);
184 static void match_function_def(struct symbol
*sym
)
189 add_param(&funcs
, sym
, 0, 0);
190 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
194 set_state(my_id
, arg
->ident
->name
, arg
, &argument
);
195 } END_FOR_EACH_PTR(arg
);
198 static void match_function_call_after(struct expression
*expr
)
200 struct expression
*tmp
;
203 struct symbol
*func
= NULL
;
206 if (expr
->fn
->type
== EXPR_SYMBOL
)
207 func
= expr
->fn
->symbol
;
210 FOR_EACH_PTR(expr
->args
, tmp
) {
211 tmp
= strip_expr(tmp
);
212 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
213 set_state_expr(my_id
, tmp
->unop
, &assumed_nonnull
);
215 name
= get_variable_from_expr(tmp
, &sym
);
216 if (final_pass
&& name
&& is_maybe_null_no_arg(name
, sym
))
217 add_param(&calls
, func
, i
, get_lineno());
221 } END_FOR_EACH_PTR(tmp
);
224 static void match_assign_returns_null(const char *fn
, struct expression
*expr
,
227 set_state_expr(my_id
, expr
->left
, &undefined
);
230 static void match_assign(struct expression
*expr
)
232 if (is_zero(expr
->right
)) {
233 set_state_expr(my_id
, expr
->left
, &isnull
);
236 /* hack alert. we set the state to here but it might get
237 set later to &undefined in match_function_assign().
238 By default we assume it's assigned something nonnull here. */
239 set_state_expr(my_id
, expr
->left
, &assumed_nonnull
);
243 * set_new_true_false_paths() is used in the following conditions
244 * if (a) { ... if (a) { ... } }
245 * The problem is that after the second blog a is set to undefined
246 * even though the second condition is meaning less. (The second test
247 * could be a macro for example).
249 * Also, stuff passed to the condition hook is processed behind the scenes
250 * a bit. Instead of a condition like (foo == 0) which is true when foo is
251 * null, we get just (foo) and the true and false states are adjusted later.
252 * Basically the important thing to remember, is that for us, true is always
253 * non null and false is always null.
255 static void set_new_true_false_paths(struct expression
*expr
, int recently_assigned
)
257 struct smatch_state
*tmp
;
262 name
= get_variable_from_expr(expr
, &sym
);
266 tmp
= get_state_expr(my_id
, expr
);
267 sm_debug("set_new_stuff called at for %s on line %d value='%s'\n",
268 name
, get_lineno(), show_state(tmp
));
270 if (tmp
== &argument
) {
271 set_true_false_states_expr(my_id
, expr
, &arg_nonnull
, &arg_null
);
275 if (tmp
== &ignore
) {
276 set_true_false_states_expr(my_id
, expr
, &ignore
, &ignore
);
280 if (!tmp
|| recently_assigned
|| is_maybe_null(name
, sym
)) {
281 set_true_false_states_expr(my_id
, expr
, &nonnull
, &isnull
);
289 static void match_condition(struct expression
*expr
)
291 expr
= strip_expr(expr
);
296 set_new_true_false_paths(expr
, 0);
298 case EXPR_ASSIGNMENT
:
300 * There is a kernel macro that does
301 * for ( ... ; ... || x = NULL ; ) ...
303 if (is_zero(expr
->right
)) {
304 set_true_false_states_expr(my_id
, expr
->left
, NULL
, &isnull
);
307 set_new_true_false_paths(expr
->left
, 1);
312 static void match_declarations(struct symbol
*sym
)
316 if ((get_base_type(sym
))->type
== SYM_ARRAY
) {
320 name
= sym
->ident
->name
;
322 if (!sym
->initializer
) {
323 set_state(my_id
, name
, sym
, &undefined
);
324 scoped_state(my_id
, name
, sym
);
328 static void match_preop_deref(struct expression
*expr
)
331 struct symbol
*sym
= NULL
;
333 deref
= get_variable_from_expr(expr
->unop
, &sym
);
337 if (is_maybe_null_arg(deref
, sym
)) {
338 add_do_not_call(sym
, get_lineno());
339 set_state(my_id
, deref
, sym
, &assumed_nonnull
);
340 } else if (is_maybe_null(deref
, sym
)) {
341 sm_msg("error: dereferencing undefined: '%s'", deref
);
342 set_state(my_id
, deref
, sym
, &ignore
);
347 static void match_dereferences(struct expression
*expr
)
350 struct symbol
*sym
= NULL
;
352 if (expr
->type
== EXPR_PREOP
) {
353 match_preop_deref(expr
);
357 if (expr
->deref
->op
!= '*')
360 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
364 if (is_maybe_null_arg(deref
, sym
)) {
365 add_do_not_call(sym
, get_lineno());
366 set_state(my_id
, deref
, sym
, &assumed_nonnull
);
367 } else if (is_maybe_null(deref
, sym
)) {
368 sm_msg("error: dereferencing undefined: '%s'", deref
);
369 set_state(my_id
, deref
, sym
, &ignore
);
374 static void end_file_processing(void)
376 struct func_n_param
*param1
, *param2
;
378 // if there is an error print it out...
379 FOR_EACH_PTR(calls
, param1
) {
380 FOR_EACH_PTR(do_not_call
, param2
) {
381 if (param1
->func
== param2
->func
&&
382 param1
->param
== param2
->param
)
383 sm_printf("%s +%d error: cross_func deref %s %d\n",
384 get_filename(), param1
->line
,
385 param1
->func
->ident
->name
,
387 } END_FOR_EACH_PTR(param2
);
388 } END_FOR_EACH_PTR(param1
);
393 // if a function is not static print it out...
394 FOR_EACH_PTR(do_not_call
, param1
) {
395 if (!(param1
->func
->ctype
.modifiers
& MOD_STATIC
))
396 sm_printf("%s +%d info: unchecked param %s %d\n",
397 get_filename(), param1
->line
,
398 param1
->func
->ident
->name
, param1
->param
);
399 } END_FOR_EACH_PTR(param1
);
401 // if someone calls a non-static function print that..
402 FOR_EACH_PTR(calls
, param1
) {
405 FOR_EACH_PTR(funcs
, param2
) {
406 if (param1
->func
== param2
->func
)
408 } END_FOR_EACH_PTR(param2
);
410 sm_printf("%s +%d info: undefined param %s %d\n",
411 get_filename(), param1
->line
,
412 param1
->func
->ident
->name
, param1
->param
);
413 } END_FOR_EACH_PTR(param1
);
416 static void register_allocation_funcs(void)
421 token
= get_tokens_file("kernel.allocation_funcs");
424 if (token_type(token
) != TOKEN_STREAMBEGIN
)
427 while (token_type(token
) != TOKEN_STREAMEND
) {
428 if (token_type(token
) != TOKEN_IDENT
)
430 func
= show_ident(token
->ident
);
431 add_function_assign_hook(func
, &match_assign_returns_null
,
438 void check_null_deref(int id
)
443 set_default_state(my_id
, &assumed_nonnull
);
444 add_merge_hook(my_id
, &merge_func
);
445 add_unmatched_state_hook(my_id
, &unmatched_state
);
446 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
447 add_hook(&match_function_call_after
, FUNCTION_CALL_HOOK
);
448 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
449 add_hook(&match_condition
, CONDITION_HOOK
);
450 add_hook(&match_dereferences
, DEREF_HOOK
);
451 add_hook(&match_declarations
, DECLARATION_HOOK
);
452 add_hook(&end_file_processing
, END_FILE_HOOK
);
454 for(i
= 0; i
< ARRAY_SIZE(return_null
); i
++) {
455 add_function_assign_hook(return_null
[i
],
456 &match_assign_returns_null
, NULL
);
458 register_allocation_funcs();