extra, db: don't use PARAM_VALUE for return states
[smatch.git] / check_err_ptr_deref.c
blob3c79509a7e3319cd8b91eba940bf56b9556d7463
1 /*
2 * Copyright (C) 2009 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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static int my_id;
24 STATE(err_ptr);
25 STATE(checked);
27 sval_t err_ptr_min = {
28 .type = &int_ctype,
29 .value = -4095,
32 sval_t err_ptr_max = {
33 .type = &int_ctype,
34 .value = -1,
37 struct range_list *err_ptr_rl;
39 static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
41 if (sm->state != &checked)
42 set_state(my_id, sm->name, sm->sym, &checked);
45 static void check_is_err_ptr(struct expression *expr)
47 struct sm_state *sm;
48 struct range_list *rl;
50 sm = get_sm_state_expr(my_id, expr);
51 if (!sm)
52 return;
54 if (!slist_has_state(sm->possible, &err_ptr))
55 return;
57 get_absolute_rl(expr, &rl);
58 if (!possibly_true_rl(rl, SPECIAL_EQUAL, err_ptr_rl))
59 return;
61 sm_msg("error: '%s' dereferencing possible ERR_PTR()", sm->name);
62 set_state(my_id, sm->name, sm->sym, &checked);
65 static void match_returns_err_ptr(const char *fn, struct expression *expr,
66 void *info)
68 set_state_expr(my_id, expr->left, &err_ptr);
72 static void match_checked(const char *fn, struct expression *call_expr,
73 struct expression *assign_expr, void *unused)
75 struct expression *arg;
77 arg = get_argument_from_call_expr(call_expr->args, 0);
78 arg = strip_expr(arg);
79 while (arg->type == EXPR_ASSIGNMENT)
80 arg = strip_expr(arg->left);
81 set_state_expr(my_id, arg, &checked);
84 static void match_err(const char *fn, struct expression *call_expr,
85 struct expression *assign_expr, void *unused)
87 struct expression *arg;
89 arg = get_argument_from_call_expr(call_expr->args, 0);
90 arg = strip_expr(arg);
91 while (arg->type == EXPR_ASSIGNMENT)
92 arg = strip_expr(arg->left);
93 set_state_expr(my_id, arg, &err_ptr);
96 static void match_dereferences(struct expression *expr)
98 if (expr->type != EXPR_PREOP)
99 return;
100 check_is_err_ptr(expr->unop);
103 static void match_kfree(const char *fn, struct expression *expr, void *_arg_nr)
105 int arg_nr = PTR_INT(_arg_nr);
106 struct expression *arg;
108 arg = get_argument_from_call_expr(expr->args, arg_nr);
109 check_is_err_ptr(arg);
112 static void match_condition(struct expression *expr)
114 if (expr->type == EXPR_ASSIGNMENT) {
115 match_condition(expr->right);
116 match_condition(expr->left);
118 if (!get_state_expr(my_id, expr))
119 return;
120 /* If we know the variable is zero that means it's not an ERR_PTR */
121 set_true_false_states_expr(my_id, expr, NULL, &checked);
124 static void register_err_ptr_funcs(void)
126 struct token *token;
127 const char *func;
129 token = get_tokens_file("kernel.returns_err_ptr");
130 if (!token)
131 return;
132 if (token_type(token) != TOKEN_STREAMBEGIN)
133 return;
134 token = token->next;
135 while (token_type(token) != TOKEN_STREAMEND) {
136 if (token_type(token) != TOKEN_IDENT)
137 return;
138 func = show_ident(token->ident);
139 add_function_assign_hook(func, &match_returns_err_ptr, NULL);
140 token = token->next;
142 clear_token_alloc();
145 static void match_err_ptr(const char *fn, struct expression *expr, void *unused)
147 struct expression *arg;
148 struct sm_state *sm;
149 struct sm_state *tmp;
150 sval_t tmp_min;
151 sval_t tmp_max;
152 sval_t min = sval_type_max(&llong_ctype);
153 sval_t max = sval_type_min(&llong_ctype);
155 arg = get_argument_from_call_expr(expr->args, 0);
156 sm = get_sm_state_expr(SMATCH_EXTRA, arg);
157 if (!sm)
158 return;
159 FOR_EACH_PTR(sm->possible, tmp) {
160 tmp_min = estate_min(tmp->state);
161 if (!sval_is_a_min(tmp_min) && sval_cmp(tmp_min, min) < 0)
162 min = tmp_min;
163 tmp_max = estate_max(tmp->state);
164 if (!sval_is_a_max(tmp_max) && sval_cmp(tmp_max, max) > 0)
165 max = tmp_max;
166 } END_FOR_EACH_PTR(tmp);
167 if (sval_is_negative(min) && sval_cmp_val(min, -4095) < 0)
168 sm_msg("error: %s too low for ERR_PTR", sval_to_str(min));
169 if (sval_is_positive(max) && sval_cmp_val(max, 0) != 0)
170 sm_msg("error: passing non negative %s to ERR_PTR", sval_to_str(max));
173 void check_err_ptr_deref(int id)
175 if (option_project != PROJ_KERNEL)
176 return;
178 my_id = id;
179 return_implies_state("IS_ERR", 0, 0, &match_checked, NULL);
180 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
181 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_checked, NULL);
182 return_implies_state("IS_ERR_OR_NULL", 1, 1, &match_err, NULL);
183 return_implies_state("PTR_RET", 0, 0, &match_checked, NULL);
184 return_implies_state("PTR_RET", -4096, -1, &match_err, NULL);
185 register_err_ptr_funcs();
186 add_hook(&match_dereferences, DEREF_HOOK);
187 add_function_hook("ERR_PTR", &match_err_ptr, NULL);
188 add_hook(&match_condition, CONDITION_HOOK);
189 add_modification_hook(my_id, &ok_to_use);
190 add_function_hook("kfree", &match_kfree, INT_PTR(0));
191 add_function_hook("brelse", &match_kfree, INT_PTR(0));
192 add_function_hook("kmem_cache_free", &match_kfree, INT_PTR(1));
193 add_function_hook("vfree", &match_kfree, INT_PTR(0));
195 err_ptr_rl = clone_rl_permanent(alloc_rl(err_ptr_min, err_ptr_max));