kernel: fix type bug handle IS_ERR() and friends
[smatch.git] / check_kernel.c
blob443c7d654c9401395a140dff988495dd31a6df26
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 * This is kernel specific stuff for smatch_extra.
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_extra.h"
26 static sval_t err_ptr_min = {
27 .type = &ptr_ctype,
28 .value = -4095,
30 static sval_t err_ptr_max = {
31 .type = &ptr_ctype,
32 .value = -1,
34 static sval_t null_ptr = {
35 .type = &ptr_ctype,
36 .value = 0,
39 static int implied_err_cast_return(struct expression *call, void *unused, struct range_list **rl)
41 struct expression *arg;
43 arg = get_argument_from_call_expr(call->args, 0);
44 if (!get_implied_rl(arg, rl)) {
45 *rl = alloc_rl(err_ptr_min, err_ptr_max);
46 *rl = cast_rl(get_type(arg), *rl);
48 return 1;
51 static void hack_ERR_PTR(struct symbol *sym)
53 struct symbol *arg;
54 struct smatch_state *estate;
55 struct range_list *after;
56 sval_t low_error = {
57 .type = &long_ctype,
58 .value = -4095,
60 sval_t minus_one = {
61 .type = &long_ctype,
62 .value = -1,
64 sval_t zero = {
65 .type = &long_ctype,
66 .value = 0,
69 if (!sym || !sym->ident)
70 return;
71 if (strcmp(sym->ident->name, "ERR_PTR") != 0)
72 return;
74 arg = first_ptr_list((struct ptr_list *)sym->ctype.base_type->arguments);
75 if (!arg || !arg->ident)
76 return;
78 estate = get_state(SMATCH_EXTRA, arg->ident->name, arg);
79 if (!estate) {
80 after = alloc_rl(low_error, minus_one);
81 } else {
82 after = rl_intersection(estate_rl(estate), alloc_rl(low_error, zero));
83 if (rl_equiv(estate_rl(estate), after))
84 return;
86 set_state(SMATCH_EXTRA, arg->ident->name, arg, alloc_estate_rl(after));
89 static void match_param_valid_ptr(const char *fn, struct expression *call_expr,
90 struct expression *assign_expr, void *_param)
92 int param = PTR_INT(_param);
93 struct expression *arg;
94 struct smatch_state *pre_state;
95 struct smatch_state *end_state;
96 struct range_list *rl;
98 arg = get_argument_from_call_expr(call_expr->args, param);
99 pre_state = get_state_expr(SMATCH_EXTRA, arg);
100 if (estate_rl(pre_state)) {
101 rl = estate_rl(pre_state);
102 rl = remove_range(rl, null_ptr, null_ptr);
103 rl = remove_range(rl, err_ptr_min, err_ptr_max);
104 } else {
105 rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
107 end_state = alloc_estate_rl(rl);
108 set_extra_expr_nomod(arg, end_state);
111 static void match_param_err_or_null(const char *fn, struct expression *call_expr,
112 struct expression *assign_expr, void *_param)
114 int param = PTR_INT(_param);
115 struct expression *arg;
116 struct range_list *rl;
117 struct smatch_state *pre_state;
118 struct smatch_state *end_state;
120 arg = get_argument_from_call_expr(call_expr->args, param);
121 pre_state = get_state_expr(SMATCH_EXTRA, arg);
122 call_results_to_rl(call_expr, &ptr_ctype, "0,(-4095)-(-1)", &rl);
123 rl = rl_intersection(estate_rl(pre_state), rl);
124 rl = cast_rl(get_type(arg), rl);
125 end_state = alloc_estate_rl(rl);
126 set_extra_expr_nomod(arg, end_state);
129 static void match_not_err(const char *fn, struct expression *call_expr,
130 struct expression *assign_expr, void *unused)
132 struct expression *arg;
133 struct smatch_state *pre_state;
134 struct range_list *rl;
136 arg = get_argument_from_call_expr(call_expr->args, 0);
137 pre_state = get_state_expr(SMATCH_EXTRA, arg);
138 if (estate_rl(pre_state))
139 rl = rl_intersection(estate_rl(pre_state), valid_ptr_rl);
140 else
141 rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
142 rl = cast_rl(get_type(arg), rl);
143 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
146 static void match_err(const char *fn, struct expression *call_expr,
147 struct expression *assign_expr, void *unused)
149 struct expression *arg;
150 struct smatch_state *pre_state;
151 struct range_list *rl;
153 arg = get_argument_from_call_expr(call_expr->args, 0);
154 pre_state = get_state_expr(SMATCH_EXTRA, arg);
155 rl = estate_rl(pre_state);
156 if (!rl)
157 rl = alloc_rl(err_ptr_min, err_ptr_max);
158 rl = rl_intersection(rl, alloc_rl(err_ptr_min, err_ptr_max));
159 rl = cast_rl(get_type(arg), rl);
160 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
163 static void match_container_of_macro(const char *fn, struct expression *expr, void *unused)
165 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
168 static void match_container_of(struct expression *expr)
170 struct expression *right = expr->right;
171 char *macro;
174 * The problem here is that sometimes the container_of() macro is itself
175 * inside a macro and get_macro() only returns the name of the outside
176 * macro.
180 * This actually an expression statement assignment but smatch_flow
181 * pre-mangles it for us so we only get the last chunk:
182 * sk = (typeof(sk))((char *)__mptr - offsetof(...))
185 macro = get_macro_name(right->pos);
186 if (!macro)
187 return;
188 if (right->type != EXPR_CAST)
189 return;
190 right = strip_expr(right);
191 if (right->type != EXPR_BINOP || right->op != '-' ||
192 right->left->type != EXPR_CAST)
193 return;
194 right = strip_expr(right->left);
195 if (right->type != EXPR_SYMBOL)
196 return;
197 if (!right->symbol->ident ||
198 strcmp(right->symbol->ident->name, "__mptr") != 0)
199 return;
200 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
203 static int match_next_bit(struct expression *call, void *unused, struct range_list **rl)
205 struct expression *start_arg;
206 struct expression *size_arg;
207 struct symbol *type;
208 sval_t min, max, tmp;
210 size_arg = get_argument_from_call_expr(call->args, 1);
211 /* btw. there isn't a start_arg for find_first_bit() */
212 start_arg = get_argument_from_call_expr(call->args, 2);
214 type = get_type(call);
215 min = sval_type_val(type, 0);
216 max = sval_type_val(type, sizeof(long long) * 8);
218 if (get_implied_max(size_arg, &tmp) && tmp.uvalue < max.value)
219 max = tmp;
220 if (start_arg && get_implied_min(start_arg, &tmp) && !sval_is_negative(tmp))
221 min = tmp;
222 if (sval_cmp(min, max) > 0)
223 max = min;
224 min = sval_cast(type, min);
225 max = sval_cast(type, max);
226 *rl = alloc_rl(min, max);
227 return 1;
230 static int match_fls(struct expression *call, void *unused, struct range_list **rl)
232 struct expression *arg;
233 struct range_list *arg_rl;
234 sval_t zero = {};
235 sval_t start = {
236 .type = &int_ctype,
237 .value = 0,
239 sval_t end = {
240 .type = &int_ctype,
241 .value = 32,
243 sval_t sval;
245 arg = get_argument_from_call_expr(call->args, 0);
246 if (!get_implied_rl(arg, &arg_rl))
247 return 0;
248 if (rl_to_sval(arg_rl, &sval)) {
249 int i;
251 for (i = 63; i >= 0; i--) {
252 if (sval.uvalue & 1ULL << i)
253 break;
255 sval.value = i + 1;
256 *rl = alloc_rl(sval, sval);
257 return 1;
259 zero.type = rl_type(arg_rl);
260 if (!rl_has_sval(arg_rl, zero))
261 start.value = 1;
262 *rl = alloc_rl(start, end);
263 return 1;
268 static void find_module_init_exit(struct symbol_list *sym_list)
270 struct symbol *sym;
271 struct symbol *fn;
272 struct statement *stmt;
273 char *name;
274 int init;
275 int count;
278 * This is more complicated because Sparse ignores the "alias"
279 * attribute. I search backwards because module_init() is normally at
280 * the end of the file.
282 count = 0;
283 FOR_EACH_PTR_REVERSE(sym_list, sym) {
284 if (sym->type != SYM_NODE)
285 continue;
286 if (!(sym->ctype.modifiers & MOD_STATIC))
287 continue;
288 fn = get_base_type(sym);
289 if (!fn)
290 continue;
291 if (fn->type != SYM_FN)
292 continue;
293 if (!sym->ident)
294 continue;
295 if (!fn->inline_stmt)
296 continue;
297 if (strcmp(sym->ident->name, "__inittest") == 0)
298 init = 1;
299 else if (strcmp(sym->ident->name, "__exittest") == 0)
300 init = 0;
301 else
302 continue;
304 count++;
306 stmt = first_ptr_list((struct ptr_list *)fn->inline_stmt->stmts);
307 if (!stmt || stmt->type != STMT_RETURN)
308 continue;
309 name = expr_to_var(stmt->ret_value);
310 if (!name)
311 continue;
312 if (init)
313 sql_insert_function_ptr(name, "(struct module)->init");
314 else
315 sql_insert_function_ptr(name, "(struct module)->exit");
316 free_string(name);
317 if (count >= 2)
318 return;
319 } END_FOR_EACH_PTR_REVERSE(sym);
322 static void match_end_file(struct symbol_list *sym_list)
324 struct symbol *sym;
326 /* find the last static symbol in the file */
327 FOR_EACH_PTR_REVERSE(sym_list, sym) {
328 if (!(sym->ctype.modifiers & MOD_STATIC))
329 continue;
330 if (!sym->scope)
331 continue;
332 find_module_init_exit(sym->scope->symbols);
333 return;
334 } END_FOR_EACH_PTR_REVERSE(sym);
337 static struct expression *get_val_expr(struct expression *expr)
339 struct symbol *sym, *val;
341 if (expr->type != EXPR_DEREF)
342 return NULL;
343 expr = expr->deref;
344 if (expr->type != EXPR_SYMBOL)
345 return NULL;
346 if (strcmp(expr->symbol_name->name, "__u") != 0)
347 return NULL;
348 sym = get_base_type(expr->symbol);
349 val = first_ptr_list((struct ptr_list *)sym->symbol_list);
350 if (!val || strcmp(val->ident->name, "__val") != 0)
351 return NULL;
352 return member_expression(expr, '.', val->ident);
355 static void match__write_once_size(const char *fn, struct expression *call,
356 void *unused)
358 struct expression *dest, *data, *assign;
359 struct range_list *rl;
361 dest = get_argument_from_call_expr(call->args, 0);
362 if (dest->type != EXPR_PREOP || dest->op != '&')
363 return;
364 dest = strip_expr(dest->unop);
366 data = get_argument_from_call_expr(call->args, 1);
367 data = get_val_expr(data);
368 if (!data)
369 return;
370 get_absolute_rl(data, &rl);
371 assign = assign_expression(dest, '=', data);
373 __in_fake_assign++;
374 __split_expr(assign);
375 __in_fake_assign--;
378 static void match__read_once_size(const char *fn, struct expression *call,
379 void *unused)
381 struct expression *dest, *data, *assign;
382 struct symbol *type, *val_sym;
385 * We want to change:
386 * __read_once_size_nocheck(&(x), __u.__c, sizeof(x));
387 * into a fake assignment:
388 * __u.val = x;
392 data = get_argument_from_call_expr(call->args, 0);
393 if (data->type != EXPR_PREOP || data->op != '&')
394 return;
395 data = strip_parens(data->unop);
397 dest = get_argument_from_call_expr(call->args, 1);
398 if (dest->type != EXPR_DEREF || dest->op != '.')
399 return;
400 if (!dest->member || strcmp(dest->member->name, "__c") != 0)
401 return;
402 dest = dest->deref;
403 type = get_type(dest);
404 if (!type)
405 return;
406 val_sym = first_ptr_list((struct ptr_list *)type->symbol_list);
407 dest = member_expression(dest, '.', val_sym->ident);
409 assign = assign_expression(dest, '=', data);
410 __in_fake_assign++;
411 __split_expr(assign);
412 __in_fake_assign--;
415 void check_kernel(int id)
417 if (option_project != PROJ_KERNEL)
418 return;
420 add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
421 add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
422 add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
423 add_hook(hack_ERR_PTR, AFTER_DEF_HOOK);
424 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_valid_ptr, (void *)0);
425 return_implies_state("IS_ERR_OR_NULL", 1, 1, &match_param_err_or_null, (void *)0);
426 return_implies_state("IS_ERR", 0, 0, &match_not_err, NULL);
427 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
428 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_valid_ptr, (void *)0);
430 add_macro_assign_hook_extra("container_of", &match_container_of_macro, NULL);
431 add_hook(match_container_of, ASSIGNMENT_HOOK);
433 add_implied_return_hook("find_next_bit", &match_next_bit, NULL);
434 add_implied_return_hook("find_next_zero_bit", &match_next_bit, NULL);
435 add_implied_return_hook("find_first_bit", &match_next_bit, NULL);
436 add_implied_return_hook("find_first_zero_bit", &match_next_bit, NULL);
438 add_implied_return_hook("fls", &match_fls, NULL);
439 add_implied_return_hook("fls64", &match_fls, NULL);
441 add_function_hook("__ftrace_bad_type", &__match_nullify_path_hook, NULL);
442 add_function_hook("__write_once_size", &match__write_once_size, NULL);
444 add_function_hook("__read_once_size", &match__read_once_size, NULL);
445 add_function_hook("__read_once_size_nocheck", &match__read_once_size, NULL);
447 if (option_info)
448 add_hook(match_end_file, END_FILE_HOOK);