core: make fake cur_slists stackable
[smatch.git] / check_deref.c
blobae0ff6c84d77743bbbbd7a6d16e41c7ced0ab1fd
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"
28 static int my_id;
30 #define __GFP_NOFAIL 0x800
32 STATE(null);
33 STATE(ok);
34 STATE(uninitialized);
36 static struct smatch_state *alloc_my_state(const char *name)
38 struct smatch_state *state;
40 state = malloc(sizeof(*state));
41 state->name = name;
42 return state;
45 static struct smatch_state *unmatched_state(struct sm_state *sm)
47 return &ok;
50 static void is_ok(const char *name, struct symbol *sym, struct expression *expr, void *unused)
52 set_state(my_id, name, sym, &ok);
55 static void check_dereference(struct expression *expr)
57 struct sm_state *sm;
58 struct sm_state *tmp;
60 expr = strip_expr(expr);
61 sm = get_sm_state_expr(my_id, expr);
62 if (!sm)
63 return;
64 if (is_ignored(my_id, sm->name, sm->sym))
65 return;
67 FOR_EACH_PTR(sm->possible, tmp) {
68 if (tmp->state == &merged)
69 continue;
70 if (tmp->state == &ok)
71 continue;
72 add_ignore(my_id, sm->name, sm->sym);
73 if (tmp->state == &null) {
74 sm_msg("error: potential null derefence '%s'.", tmp->name);
75 return;
77 if (tmp->state == &uninitialized) {
78 sm_msg("error: potentially derefencing uninitialized '%s'.", tmp->name);
79 return;
81 sm_msg("error: potential null dereference '%s'. (%s returns null)",
82 tmp->name, tmp->state->name);
83 return;
84 } END_FOR_EACH_PTR(tmp);
87 static void match_dereferences(struct expression *expr)
89 if (expr->type != EXPR_PREOP)
90 return;
91 check_dereference(expr->unop);
94 static void match_pointer_as_array(struct expression *expr)
96 if (!is_array(expr))
97 return;
98 check_dereference(expr->unop->left);
101 static void match_declarations(struct symbol *sym)
103 const char *name;
105 if ((get_base_type(sym))->type == SYM_ARRAY)
106 return;
108 name = sym->ident->name;
109 if (!sym->initializer) {
110 set_state(my_id, name, sym, &uninitialized);
111 scoped_state(my_id, name, sym);
115 static void match_assign(struct expression *expr)
117 if (is_zero(expr->right)) {
118 set_state_expr(my_id, expr->left, &null);
119 return;
123 static void match_condition(struct expression *expr)
125 if (expr->type == EXPR_ASSIGNMENT) {
126 match_condition(expr->right);
127 match_condition(expr->left);
129 if (!get_state_expr(my_id, expr))
130 return;
131 set_true_false_states_expr(my_id, expr, &ok, NULL);
134 static int called_with_no_fail(struct expression *call, int param)
136 struct expression *arg;
137 long long val;
139 if (param == -1)
140 return 0;
141 call = strip_expr(call);
142 if (call->type != EXPR_CALL)
143 return 0;
144 arg = get_argument_from_call_expr(call->args, param);
145 if (get_value(arg, &val) && (val & __GFP_NOFAIL))
146 return 1;
147 return 0;
150 static void match_assign_returns_null(const char *fn, struct expression *expr, void *_gfp)
152 struct smatch_state *state;
153 int gfp_param = (int)_gfp;
155 if (called_with_no_fail(expr->right, gfp_param))
156 return;
157 state = alloc_my_state(fn);
158 set_state_expr(my_id, expr->left, state);
161 static void register_allocation_funcs(void)
163 struct token *token;
164 const char *func;
165 int arg;
167 token = get_tokens_file("kernel.allocation_funcs_gfp");
168 if (!token)
169 return;
170 if (token_type(token) != TOKEN_STREAMBEGIN)
171 return;
172 token = token->next;
173 while (token_type(token) != TOKEN_STREAMEND) {
174 if (token_type(token) != TOKEN_IDENT)
175 return;
176 func = show_ident(token->ident);
177 token = token->next;
178 if (token_type(token) == TOKEN_IDENT)
179 arg = -1;
180 else if (token_type(token) == TOKEN_NUMBER)
181 arg = atoi(token->number);
182 else
183 return;
184 add_function_assign_hook(func, &match_assign_returns_null, INT_PTR(arg));
185 token = token->next;
187 clear_token_alloc();
190 void check_deref(int id)
192 my_id = id;
194 add_unmatched_state_hook(my_id, &unmatched_state);
195 set_default_modification_hook(my_id, &is_ok);
196 add_hook(&match_dereferences, DEREF_HOOK);
197 add_hook(&match_pointer_as_array, OP_HOOK);
198 add_hook(&match_condition, CONDITION_HOOK);
199 add_hook(&match_declarations, DECLARATION_HOOK);
200 add_hook(&match_assign, ASSIGNMENT_HOOK);
201 if (option_project == PROJ_KERNEL)
202 register_allocation_funcs();