smatch.h: introduce sm_pedantic()
[smatch.git] / smatch_param_cleared.c
blob633fc2402b1d4f50f7bc3cb08111da59fe35d360
1 /*
2 * Copyright (C) 2012 Oracle.
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
19 * This works together with smatch_clear_buffer.c. This one is only for
20 * tracking the information and smatch_clear_buffer.c changes SMATCH_EXTRA.
22 * This tracks functions like memset() which clear out a chunk of memory.
23 * It fills in a gap that smatch_param_set.c can't handle. It only handles
24 * void pointers because smatch_param_set.c should handle the rest. Oh. And
25 * also it handles arrays because Smatch sucks at handling arrays.
28 #include "scope.h"
29 #include "smatch.h"
30 #include "smatch_slist.h"
31 #include "smatch_extra.h"
33 static int my_id;
35 STATE(cleared);
36 STATE(zeroed);
38 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
40 struct expression *arg;
41 char *name;
42 struct symbol *sym;
44 while (expr->type == EXPR_ASSIGNMENT)
45 expr = strip_expr(expr->right);
46 if (expr->type != EXPR_CALL)
47 return;
49 arg = get_argument_from_call_expr(expr->args, param);
50 arg = strip_expr(arg);
51 name = get_variable_from_key(arg, key, &sym);
52 if (!name || !sym)
53 goto free;
55 if (strcmp(value, "0") == 0)
56 set_state(my_id, name, sym, &zeroed);
57 else
58 set_state(my_id, name, sym, &cleared);
59 free:
60 free_string(name);
63 static void match_memset(const char *fn, struct expression *expr, void *arg)
65 db_param_cleared(expr, PTR_INT(arg), (char *)"$", (char *)"0");
68 static void match_memcpy(const char *fn, struct expression *expr, void *arg)
70 db_param_cleared(expr, PTR_INT(arg), (char *)"$", (char *)"");
73 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
75 struct stree *stree;
76 struct sm_state *sm;
77 int param;
78 const char *param_name;
80 stree = __get_cur_stree();
82 FOR_EACH_MY_SM(my_id, stree, sm) {
83 param = get_param_num_from_sym(sm->sym);
84 if (param < 0)
85 continue;
87 param_name = get_param_name(sm);
88 if (!param_name)
89 continue;
91 if (sm->state == &zeroed) {
92 sql_insert_return_states(return_id, return_ranges,
93 PARAM_CLEARED, param, param_name, "0");
96 if (sm->state == &cleared) {
97 sql_insert_return_states(return_id, return_ranges,
98 PARAM_CLEARED, param, param_name, "");
100 } END_FOR_EACH_SM(sm);
103 bool parent_was_PARAM_CLEAR(const char *name, struct symbol *sym)
105 struct sm_state *sm;
106 char buf[80];
107 int len, i;
109 if (!name)
110 return 0;
112 len = strlen(name);
113 if (len >= sizeof(buf))
114 len = sizeof(buf) - 1;
116 for (i = len - 2; i >= 1; i--) {
117 if (name[i] != '-' && name[i] != '.')
118 continue;
120 memcpy(buf, name, i);
121 buf[i] = '\0';
122 sm = get_sm_state(my_id, buf, sym);
123 if (sm && sm->state == &cleared)
124 return true;
125 if (sm)
126 return false;
128 buf[0] = '&';
129 memcpy(buf + 1, name, i);
130 buf[i + 1] = '\0';
131 sm = get_sm_state(my_id, buf, sym);
132 if (sm && sm->state == &cleared)
133 return true;
134 if (sm)
135 return false;
138 return false;
141 bool parent_was_PARAM_CLEAR_ZERO(const char *name, struct symbol *sym)
143 struct sm_state *sm;
144 char buf[80];
145 int len, i;
147 if (!name)
148 return 0;
150 len = strlen(name);
151 if (len >= sizeof(buf))
152 len = sizeof(buf) - 1;
154 for (i = len - 2; i >= 1; i--) {
155 if (name[i] != '-' && name[i] != '.')
156 continue;
158 memcpy(buf, name, i);
159 buf[i] = '\0';
160 sm = get_sm_state(my_id, buf, sym);
161 if (sm && sm->state == &zeroed)
162 return true;
163 if (sm)
164 return false;
166 buf[0] = '&';
167 memcpy(buf + 1, name, i);
168 buf[i + 1] = '\0';
169 sm = get_sm_state(my_id, buf, sym);
170 if (sm && sm->state == &zeroed)
171 return true;
172 if (sm)
173 return false;
176 return false;
179 static void register_clears_param(void)
181 struct token *token;
182 char name[256];
183 const char *function;
184 int param;
186 if (option_project == PROJ_NONE)
187 return;
189 snprintf(name, 256, "%s.clears_argument", option_project_str);
191 token = get_tokens_file(name);
192 if (!token)
193 return;
194 if (token_type(token) != TOKEN_STREAMBEGIN)
195 return;
196 token = token->next;
197 while (token_type(token) != TOKEN_STREAMEND) {
198 if (token_type(token) != TOKEN_IDENT)
199 return;
200 function = show_ident(token->ident);
201 token = token->next;
202 if (token_type(token) != TOKEN_NUMBER)
203 return;
204 param = atoi(token->number);
205 add_function_hook(function, &match_memcpy, INT_PTR(param));
206 token = token->next;
208 clear_token_alloc();
211 #define USB_DIR_IN 0x80
212 static void match_usb_control_msg(const char *fn, struct expression *expr, void *_size_arg)
214 struct expression *inout;
215 sval_t sval;
217 inout = get_argument_from_call_expr(expr->args, 3);
219 if (get_value(inout, &sval) && !(sval.uvalue & USB_DIR_IN))
220 return;
222 db_param_cleared(expr, 6, (char *)"$", (char *)"");
225 static void match_assign(struct expression *expr)
227 struct symbol *type;
230 * If we have struct foo x, y; and we say that x = y; then it
231 * initializes the struct holes. So we record that here.
233 type = get_type(expr->left);
234 if (!type || type->type != SYM_STRUCT)
235 return;
236 set_state_expr(my_id, expr->left, &cleared);
239 static void match_array_assign(struct expression *expr)
241 struct expression *array_expr;
243 if (!is_array(expr->left))
244 return;
246 array_expr = get_array_base(expr->left);
247 set_state_expr(my_id, array_expr, &cleared);
250 void register_param_cleared(int id)
252 my_id = id;
254 add_function_hook("memset", &match_memset, INT_PTR(0));
255 add_function_hook("memzero", &match_memset, INT_PTR(0));
256 add_function_hook("__memset", &match_memset, INT_PTR(0));
257 add_function_hook("__memzero", &match_memset, INT_PTR(0));
259 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
260 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
261 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
262 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
263 add_function_hook("strcpy", &match_memcpy, INT_PTR(0));
264 add_function_hook("strncpy", &match_memcpy, INT_PTR(0));
265 add_function_hook("sprintf", &match_memcpy, INT_PTR(0));
266 add_function_hook("snprintf", &match_memcpy, INT_PTR(0));
268 add_hook(&match_assign, ASSIGNMENT_HOOK);
269 add_hook(&match_array_assign, ASSIGNMENT_HOOK);
271 register_clears_param();
273 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);
274 add_split_return_callback(&print_return_value_param);
276 if (option_project == PROJ_KERNEL) {
277 add_function_hook("usb_control_msg", &match_usb_control_msg, NULL);