kernel: preserve the sm state when handling IS_ERR()
[smatch.git] / check_kernel.c
blob95d2e28b2dc6bbe53f6f82109357981c26b19f4f
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);
47 *rl = cast_rl(get_type(call), *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 *pre, *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 if (pre_state)
123 pre = estate_rl(pre_state);
124 else
125 pre = alloc_whole_rl(&ptr_ctype);
126 call_results_to_rl(call_expr, &ptr_ctype, "0,(-4095)-(-1)", &rl);
127 rl = rl_intersection(pre, rl);
128 rl = cast_rl(get_type(arg), rl);
129 end_state = alloc_estate_rl(rl);
130 set_extra_expr_nomod(arg, end_state);
133 static void match_not_err(const char *fn, struct expression *call_expr,
134 struct expression *assign_expr, void *unused)
136 struct expression *arg;
137 struct smatch_state *pre_state;
138 struct range_list *rl;
140 arg = get_argument_from_call_expr(call_expr->args, 0);
141 pre_state = get_state_expr(SMATCH_EXTRA, arg);
142 if (pre_state)
143 return;
144 rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
145 rl = cast_rl(get_type(arg), rl);
146 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
149 static void match_err(const char *fn, struct expression *call_expr,
150 struct expression *assign_expr, void *unused)
152 struct expression *arg;
153 struct smatch_state *pre_state;
154 struct range_list *rl;
156 arg = get_argument_from_call_expr(call_expr->args, 0);
157 pre_state = get_state_expr(SMATCH_EXTRA, arg);
158 rl = estate_rl(pre_state);
159 if (!rl)
160 rl = alloc_rl(err_ptr_min, err_ptr_max);
161 rl = rl_intersection(rl, alloc_rl(err_ptr_min, err_ptr_max));
162 rl = cast_rl(get_type(arg), rl);
163 if (pre_state && rl) {
165 * Ideally this would all be handled by smatch_implied.c
166 * but it doesn't work very well for impossible paths.
169 return;
171 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
174 static void match_container_of_macro(const char *fn, struct expression *expr, void *unused)
176 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
179 static void match_container_of(struct expression *expr)
181 struct expression *right = expr->right;
182 char *macro;
185 * The problem here is that sometimes the container_of() macro is itself
186 * inside a macro and get_macro() only returns the name of the outside
187 * macro.
191 * This actually an expression statement assignment but smatch_flow
192 * pre-mangles it for us so we only get the last chunk:
193 * sk = (typeof(sk))((char *)__mptr - offsetof(...))
196 macro = get_macro_name(right->pos);
197 if (!macro)
198 return;
199 if (right->type != EXPR_CAST)
200 return;
201 right = strip_expr(right);
202 if (right->type != EXPR_BINOP || right->op != '-' ||
203 right->left->type != EXPR_CAST)
204 return;
205 right = strip_expr(right->left);
206 if (right->type != EXPR_SYMBOL)
207 return;
208 if (!right->symbol->ident ||
209 strcmp(right->symbol->ident->name, "__mptr") != 0)
210 return;
211 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
214 static int match_next_bit(struct expression *call, void *unused, struct range_list **rl)
216 struct expression *start_arg;
217 struct expression *size_arg;
218 struct symbol *type;
219 sval_t min, max, tmp;
221 size_arg = get_argument_from_call_expr(call->args, 1);
222 /* btw. there isn't a start_arg for find_first_bit() */
223 start_arg = get_argument_from_call_expr(call->args, 2);
225 type = get_type(call);
226 min = sval_type_val(type, 0);
227 max = sval_type_val(type, sizeof(long long) * 8);
229 if (get_implied_max(size_arg, &tmp) && tmp.uvalue < max.value)
230 max = tmp;
231 if (start_arg && get_implied_min(start_arg, &tmp) && !sval_is_negative(tmp))
232 min = tmp;
233 if (sval_cmp(min, max) > 0)
234 max = min;
235 min = sval_cast(type, min);
236 max = sval_cast(type, max);
237 *rl = alloc_rl(min, max);
238 return 1;
241 static int match_fls(struct expression *call, void *unused, struct range_list **rl)
243 struct expression *arg;
244 struct range_list *arg_rl;
245 sval_t zero = {};
246 sval_t start = {
247 .type = &int_ctype,
248 .value = 0,
250 sval_t end = {
251 .type = &int_ctype,
252 .value = 32,
254 sval_t sval;
256 arg = get_argument_from_call_expr(call->args, 0);
257 if (!get_implied_rl(arg, &arg_rl))
258 return 0;
259 if (rl_to_sval(arg_rl, &sval)) {
260 int i;
262 for (i = 63; i >= 0; i--) {
263 if (sval.uvalue & 1ULL << i)
264 break;
266 sval.value = i + 1;
267 *rl = alloc_rl(sval, sval);
268 return 1;
270 zero.type = rl_type(arg_rl);
271 if (!rl_has_sval(arg_rl, zero))
272 start.value = 1;
273 *rl = alloc_rl(start, end);
274 return 1;
277 static void find_module_init_exit(struct symbol_list *sym_list)
279 struct symbol *sym;
280 struct symbol *fn;
281 struct statement *stmt;
282 char *name;
283 int init;
284 int count;
287 * This is more complicated because Sparse ignores the "alias"
288 * attribute. I search backwards because module_init() is normally at
289 * the end of the file.
291 count = 0;
292 FOR_EACH_PTR_REVERSE(sym_list, sym) {
293 if (sym->type != SYM_NODE)
294 continue;
295 if (!(sym->ctype.modifiers & MOD_STATIC))
296 continue;
297 fn = get_base_type(sym);
298 if (!fn)
299 continue;
300 if (fn->type != SYM_FN)
301 continue;
302 if (!sym->ident)
303 continue;
304 if (!fn->inline_stmt)
305 continue;
306 if (strcmp(sym->ident->name, "__inittest") == 0)
307 init = 1;
308 else if (strcmp(sym->ident->name, "__exittest") == 0)
309 init = 0;
310 else
311 continue;
313 count++;
315 stmt = first_ptr_list((struct ptr_list *)fn->inline_stmt->stmts);
316 if (!stmt || stmt->type != STMT_RETURN)
317 continue;
318 name = expr_to_var(stmt->ret_value);
319 if (!name)
320 continue;
321 if (init)
322 sql_insert_function_ptr(name, "(struct module)->init");
323 else
324 sql_insert_function_ptr(name, "(struct module)->exit");
325 free_string(name);
326 if (count >= 2)
327 return;
328 } END_FOR_EACH_PTR_REVERSE(sym);
331 static void match_end_file(struct symbol_list *sym_list)
333 struct symbol *sym;
335 /* find the last static symbol in the file */
336 FOR_EACH_PTR_REVERSE(sym_list, sym) {
337 if (!(sym->ctype.modifiers & MOD_STATIC))
338 continue;
339 if (!sym->scope)
340 continue;
341 find_module_init_exit(sym->scope->symbols);
342 return;
343 } END_FOR_EACH_PTR_REVERSE(sym);
346 static struct expression *get_val_expr(struct expression *expr)
348 struct symbol *sym, *val;
350 if (expr->type != EXPR_DEREF)
351 return NULL;
352 expr = expr->deref;
353 if (expr->type != EXPR_SYMBOL)
354 return NULL;
355 if (strcmp(expr->symbol_name->name, "__u") != 0)
356 return NULL;
357 sym = get_base_type(expr->symbol);
358 val = first_ptr_list((struct ptr_list *)sym->symbol_list);
359 if (!val || strcmp(val->ident->name, "__val") != 0)
360 return NULL;
361 return member_expression(expr, '.', val->ident);
364 static void match__write_once_size(const char *fn, struct expression *call,
365 void *unused)
367 struct expression *dest, *data, *assign;
368 struct range_list *rl;
370 dest = get_argument_from_call_expr(call->args, 0);
371 if (dest->type != EXPR_PREOP || dest->op != '&')
372 return;
373 dest = strip_expr(dest->unop);
375 data = get_argument_from_call_expr(call->args, 1);
376 data = get_val_expr(data);
377 if (!data)
378 return;
379 get_absolute_rl(data, &rl);
380 assign = assign_expression(dest, '=', data);
382 __in_fake_assign++;
383 __split_expr(assign);
384 __in_fake_assign--;
387 static void match__read_once_size(const char *fn, struct expression *call,
388 void *unused)
390 struct expression *dest, *data, *assign;
391 struct symbol *type, *val_sym;
394 * We want to change:
395 * __read_once_size_nocheck(&(x), __u.__c, sizeof(x));
396 * into a fake assignment:
397 * __u.val = x;
401 data = get_argument_from_call_expr(call->args, 0);
402 if (data->type != EXPR_PREOP || data->op != '&')
403 return;
404 data = strip_parens(data->unop);
406 dest = get_argument_from_call_expr(call->args, 1);
407 if (dest->type != EXPR_DEREF || dest->op != '.')
408 return;
409 if (!dest->member || strcmp(dest->member->name, "__c") != 0)
410 return;
411 dest = dest->deref;
412 type = get_type(dest);
413 if (!type)
414 return;
415 val_sym = first_ptr_list((struct ptr_list *)type->symbol_list);
416 dest = member_expression(dest, '.', val_sym->ident);
418 assign = assign_expression(dest, '=', data);
419 __in_fake_assign++;
420 __split_expr(assign);
421 __in_fake_assign--;
424 static void match_closure_call(const char *name, struct expression *call,
425 void *unused)
427 struct expression *cl, *fn, *fake_call;
428 struct expression_list *args = NULL;
430 cl = get_argument_from_call_expr(call->args, 0);
431 fn = get_argument_from_call_expr(call->args, 1);
432 if (!fn || !cl)
433 return;
435 add_ptr_list(&args, cl);
436 fake_call = call_expression(fn, args);
437 __split_expr(fake_call);
440 bool is_ignored_kernel_data(const char *name)
442 if (option_project != PROJ_KERNEL)
443 return false;
446 * On the file I was looking at lockdep was 25% of the DB.
448 if (strstr(name, ".dep_map."))
449 return true;
450 if (strstr(name, ".lockdep_map."))
451 return true;
452 return false;
455 void check_kernel(int id)
457 if (option_project != PROJ_KERNEL)
458 return;
460 err_ptr_min = sval_cast(&ptr_ctype, err_ptr_min);
461 err_ptr_max = sval_cast(&ptr_ctype, err_ptr_max);
463 add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
464 add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
465 add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
466 add_hook(hack_ERR_PTR, AFTER_DEF_HOOK);
467 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_valid_ptr, (void *)0);
468 return_implies_state("IS_ERR_OR_NULL", 1, 1, &match_param_err_or_null, (void *)0);
469 return_implies_state("IS_ERR", 0, 0, &match_not_err, NULL);
470 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
471 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_valid_ptr, (void *)0);
473 add_macro_assign_hook_extra("container_of", &match_container_of_macro, NULL);
474 add_hook(match_container_of, ASSIGNMENT_HOOK);
476 add_implied_return_hook("find_next_bit", &match_next_bit, NULL);
477 add_implied_return_hook("find_next_zero_bit", &match_next_bit, NULL);
478 add_implied_return_hook("find_first_bit", &match_next_bit, NULL);
479 add_implied_return_hook("find_first_zero_bit", &match_next_bit, NULL);
481 add_implied_return_hook("fls", &match_fls, NULL);
482 add_implied_return_hook("fls64", &match_fls, NULL);
484 add_function_hook("__ftrace_bad_type", &__match_nullify_path_hook, NULL);
485 add_function_hook("__write_once_size", &match__write_once_size, NULL);
487 add_function_hook("__read_once_size", &match__read_once_size, NULL);
488 add_function_hook("__read_once_size_nocheck", &match__read_once_size, NULL);
490 add_function_hook("closure_call", &match_closure_call, NULL);
492 if (option_info)
493 add_hook(match_end_file, END_FILE_HOOK);