extra: shuffle smatch_extra.h around some more
[smatch.git] / check_kernel.c
bloba24db0d333a2e15558ddce612e5308ec1b091e55
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 int implied_err_cast_return(struct expression *call, void *unused, struct range_list **rl)
19 struct expression *arg;
21 arg = get_argument_from_call_expr(call->args, 0);
22 if (!get_implied_rl(arg, rl))
23 *rl = alloc_rl(ll_to_sval(-4095), ll_to_sval(-1));
24 return 1;
27 static int implied_copy_return(struct expression *call, void *unused, struct range_list **rl)
29 struct expression *arg;
30 sval_t max;
32 arg = get_argument_from_call_expr(call->args, 2);
33 get_absolute_max(arg, &max);
34 *rl = alloc_rl(ll_to_sval(0), max);
35 return 1;
38 static void match_param_nonnull(const char *fn, struct expression *call_expr,
39 struct expression *assign_expr, void *_param)
41 int param = PTR_INT(_param);
42 struct expression *arg;
43 struct smatch_state *pre_state;
44 struct smatch_state *true_state;
46 arg = get_argument_from_call_expr(call_expr->args, param);
47 pre_state = get_state_expr(SMATCH_EXTRA, arg);
48 true_state = estate_filter_sval(pre_state, ll_to_sval(0));
49 set_extra_expr_nomod(arg, true_state);
52 static void match_not_err(const char *fn, struct expression *call_expr,
53 struct expression *assign_expr, void *unused)
55 struct expression *arg;
56 struct smatch_state *pre_state;
57 struct smatch_state *new_state;
59 arg = get_argument_from_call_expr(call_expr->args, 0);
60 pre_state = get_state_expr(SMATCH_EXTRA, arg);
61 new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-1));
62 set_extra_expr_nomod(arg, new_state);
65 static void match_err(const char *fn, struct expression *call_expr,
66 struct expression *assign_expr, void *unused)
68 struct expression *arg;
69 struct smatch_state *pre_state;
70 struct smatch_state *new_state;
72 arg = get_argument_from_call_expr(call_expr->args, 0);
73 pre_state = get_state_expr(SMATCH_EXTRA, arg);
74 new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-4096));
75 new_state = estate_filter_range(new_state, ll_to_sval(0), sval_type_max(&long_ctype));
76 set_extra_expr_nomod(arg, new_state);
79 static void match_container_of(const char *fn, struct expression *expr, void *unused)
81 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
84 void check_kernel(int id)
86 if (option_project != PROJ_KERNEL)
87 return;
89 add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
90 add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
91 add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
92 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_nonnull, (void *)0);
93 return_implies_state("IS_ERR", 0, 0, &match_not_err, NULL);
94 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
95 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_nonnull, (void *)0);
96 add_macro_assign_hook_extra("container_of", &match_container_of, NULL);
98 add_implied_return_hook("copy_to_user", &implied_copy_return, NULL);
99 add_implied_return_hook("__copy_to_user", &implied_copy_return, NULL);
100 add_implied_return_hook("copy_from_user", &implied_copy_return, NULL);
101 add_implied_return_hook("__copy_fom_user", &implied_copy_return, NULL);
102 add_implied_return_hook("clear_user", &implied_copy_return, NULL);