buf_size: pull bytes_to_elements() in its own function
[smatch.git] / check_kernel.c
blob8b6d45f823685881f0c4415d2b91ae42a5ab4de6
1 /*
2 * sparse/check_kernel.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is kernel specific stuff for smatch_extra.
14 #include "smatch.h"
15 #include "smatch_extra.h"
17 static void match_err_cast(const char *fn, struct expression *expr, void *unused)
19 struct expression *arg;
20 struct expression *right;
21 struct range_list *rl;
23 right = strip_expr(expr->right);
24 arg = get_argument_from_call_expr(right->args, 0);
26 if (get_implied_range_list(arg, &rl))
27 set_extra_expr_mod(expr->left, alloc_estate_range_list(rl));
28 else
29 set_extra_expr_mod(expr->left, alloc_estate_range(-4095, -1));
32 static void match_param_nonnull(const char *fn, struct expression *call_expr,
33 struct expression *assign_expr, void *_param)
35 int param = PTR_INT(_param);
36 struct expression *arg;
37 struct smatch_state *pre_state;
38 struct smatch_state *true_state;
40 arg = get_argument_from_call_expr(call_expr->args, param);
41 pre_state = get_state_expr(SMATCH_EXTRA, arg);
42 true_state = add_filter(pre_state, 0);
43 set_extra_expr_nomod(arg, true_state);
46 static void match_container_of(const char *fn, struct expression *expr, void *unused)
48 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min, valid_ptr_max));
51 void check_kernel(int id)
53 if (option_project != PROJ_KERNEL)
54 return;
56 add_function_assign_hook("ERR_PTR", &match_err_cast, NULL);
57 add_function_assign_hook("ERR_CAST", &match_err_cast, NULL);
58 add_function_assign_hook("PTR_ERR", &match_err_cast, NULL);
59 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_nonnull, (void *)0);
60 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_nonnull, (void *)0);
61 add_macro_assign_hook("container_of", &match_container_of, NULL);