whitespace: add a newline
[smatch.git] / check_kernel.c
blobbe518e631507dbbfe113db5caf78e577eea2bc67
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_range_list(arg, rl))
23 *rl = alloc_range_list(-4095, -1);
24 return 1;
27 static int implied_copy_return(struct expression *call, void *unused, struct range_list **rl)
29 struct expression *arg;
30 long long max;
32 arg = get_argument_from_call_expr(call->args, 2);
33 if (!get_absolute_max(arg, &max))
34 max = whole_range.max;
35 *rl = alloc_range_list(0, max);
36 return 1;
39 static void match_param_nonnull(const char *fn, struct expression *call_expr,
40 struct expression *assign_expr, void *_param)
42 int param = PTR_INT(_param);
43 struct expression *arg;
44 struct smatch_state *pre_state;
45 struct smatch_state *true_state;
47 arg = get_argument_from_call_expr(call_expr->args, param);
48 pre_state = get_state_expr(SMATCH_EXTRA, arg);
49 true_state = add_filter(pre_state, 0);
50 set_extra_expr_nomod(arg, true_state);
53 static void match_not_err(const char *fn, struct expression *call_expr,
54 struct expression *assign_expr, void *unused)
56 struct expression *arg;
57 struct smatch_state *pre_state;
58 struct smatch_state *new_state;
60 arg = get_argument_from_call_expr(call_expr->args, 0);
61 pre_state = get_state_expr(SMATCH_EXTRA, arg);
62 new_state = filter_range(pre_state, whole_range.min, -1);
63 set_extra_expr_nomod(arg, new_state);
66 static void match_err(const char *fn, struct expression *call_expr,
67 struct expression *assign_expr, void *unused)
69 struct expression *arg;
70 struct smatch_state *pre_state;
71 struct smatch_state *new_state;
73 arg = get_argument_from_call_expr(call_expr->args, 0);
74 pre_state = get_state_expr(SMATCH_EXTRA, arg);
75 new_state = filter_range(pre_state, whole_range.min, -4096);
76 new_state = filter_range(new_state, 0, whole_range.max);
77 set_extra_expr_nomod(arg, new_state);
80 static void match_container_of(const char *fn, struct expression *expr, void *unused)
82 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min, valid_ptr_max));
85 void check_kernel(int id)
87 if (option_project != PROJ_KERNEL)
88 return;
90 add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
91 add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
92 add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
93 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_nonnull, (void *)0);
94 return_implies_state("IS_ERR", 0, 0, &match_not_err, NULL);
95 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
96 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_nonnull, (void *)0);
97 add_macro_assign_hook_extra("container_of", &match_container_of, NULL);
99 add_implied_return_hook("copy_to_user", &implied_copy_return, NULL);
100 add_implied_return_hook("__copy_to_user", &implied_copy_return, NULL);
101 add_implied_return_hook("copy_from_user", &implied_copy_return, NULL);
102 add_implied_return_hook("__copy_fom_user", &implied_copy_return, NULL);
103 add_implied_return_hook("clear_user", &implied_copy_return, NULL);