function_hooks: fix how database comparisons are done
[smatch.git] / check_err_ptr_deref.c
blob2b1fc46955cd4713fd9fa8167bbf07027b387418
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 static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
29 if (sm->state != &checked)
30 set_state(my_id, sm->name, sm->sym, &checked);
33 static void check_is_err_ptr(struct sm_state *sm)
35 if (!sm)
36 return;
38 if (slist_has_state(sm->possible, &err_ptr)) {
39 sm_msg("error: '%s' dereferencing possible ERR_PTR()",
40 sm->name);
41 set_state(my_id, sm->name, sm->sym, &checked);
45 static void match_returns_err_ptr(const char *fn, struct expression *expr,
46 void *info)
48 set_state_expr(my_id, expr->left, &err_ptr);
52 static void match_checked(const char *fn, struct expression *call_expr,
53 struct expression *assign_expr, void *unused)
55 struct expression *arg;
57 arg = get_argument_from_call_expr(call_expr->args, 0);
58 arg = strip_expr(arg);
59 while (arg->type == EXPR_ASSIGNMENT)
60 arg = strip_expr(arg->left);
61 set_state_expr(my_id, arg, &checked);
64 static void match_err(const char *fn, struct expression *call_expr,
65 struct expression *assign_expr, void *unused)
67 struct expression *arg;
69 arg = get_argument_from_call_expr(call_expr->args, 0);
70 arg = strip_expr(arg);
71 while (arg->type == EXPR_ASSIGNMENT)
72 arg = strip_expr(arg->left);
73 set_state_expr(my_id, arg, &err_ptr);
76 static void match_dereferences(struct expression *expr)
78 struct sm_state *sm;
80 if (expr->type != EXPR_PREOP)
81 return;
82 expr = strip_expr(expr->unop);
84 sm = get_sm_state_expr(my_id, expr);
85 check_is_err_ptr(sm);
88 static void match_kfree(const char *fn, struct expression *expr, void *_arg_nr)
90 int arg_nr = PTR_INT(_arg_nr);
91 struct expression *arg;
92 struct sm_state *sm;
94 arg = get_argument_from_call_expr(expr->args, arg_nr);
96 sm = get_sm_state_expr(my_id, arg);
97 check_is_err_ptr(sm);
100 static void match_condition(struct expression *expr)
102 if (expr->type == EXPR_ASSIGNMENT) {
103 match_condition(expr->right);
104 match_condition(expr->left);
106 if (!get_state_expr(my_id, expr))
107 return;
108 /* If we know the variable is zero that means it's not an ERR_PTR */
109 set_true_false_states_expr(my_id, expr, NULL, &checked);
112 static void register_err_ptr_funcs(void)
114 struct token *token;
115 const char *func;
117 token = get_tokens_file("kernel.returns_err_ptr");
118 if (!token)
119 return;
120 if (token_type(token) != TOKEN_STREAMBEGIN)
121 return;
122 token = token->next;
123 while (token_type(token) != TOKEN_STREAMEND) {
124 if (token_type(token) != TOKEN_IDENT)
125 return;
126 func = show_ident(token->ident);
127 add_function_assign_hook(func, &match_returns_err_ptr, NULL);
128 token = token->next;
130 clear_token_alloc();
133 static void match_err_ptr(const char *fn, struct expression *expr, void *unused)
135 struct expression *arg;
136 struct sm_state *sm;
137 struct sm_state *tmp;
138 sval_t tmp_min;
139 sval_t tmp_max;
140 sval_t min = sval_type_max(&llong_ctype);
141 sval_t max = sval_type_min(&llong_ctype);
143 arg = get_argument_from_call_expr(expr->args, 0);
144 sm = get_sm_state_expr(SMATCH_EXTRA, arg);
145 if (!sm)
146 return;
147 FOR_EACH_PTR(sm->possible, tmp) {
148 tmp_min = estate_min(tmp->state);
149 if (!sval_is_a_min(tmp_min) && sval_cmp(tmp_min, min) < 0)
150 min = tmp_min;
151 tmp_max = estate_max(tmp->state);
152 if (!sval_is_a_max(tmp_max) && sval_cmp(tmp_max, max) > 0)
153 max = tmp_max;
154 } END_FOR_EACH_PTR(tmp);
155 if (sval_is_negative(min) && sval_cmp_val(min, -4095) < 0)
156 sm_msg("error: %s too low for ERR_PTR", sval_to_str(min));
157 if (sval_is_positive(max) && sval_cmp_val(max, 0) != 0)
158 sm_msg("error: passing non neg %s to ERR_PTR", sval_to_str(max));
161 void check_err_ptr_deref(int id)
163 if (option_project != PROJ_KERNEL)
164 return;
166 my_id = id;
167 return_implies_state("IS_ERR", 0, 0, &match_checked, NULL);
168 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
169 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_checked, NULL);
170 return_implies_state("IS_ERR_OR_NULL", 1, 1, &match_err, NULL);
171 return_implies_state("PTR_RET", 0, 0, &match_checked, NULL);
172 return_implies_state("PTR_RET", -4096, -1, &match_err, NULL);
173 register_err_ptr_funcs();
174 add_hook(&match_dereferences, DEREF_HOOK);
175 add_function_hook("ERR_PTR", &match_err_ptr, NULL);
176 add_hook(&match_condition, CONDITION_HOOK);
177 add_modification_hook(my_id, &ok_to_use);
178 add_function_hook("kfree", &match_kfree, INT_PTR(0));
179 add_function_hook("brelse", &match_kfree, INT_PTR(0));
180 add_function_hook("kmem_cache_free", &match_kfree, INT_PTR(1));
181 add_function_hook("vfree", &match_kfree, INT_PTR(0));