implied: pass sm_states instead of pools
[smatch.git] / smatch_struct_assignment.c
blob1578aa2297f71a0350c9f7c073e4afa148d24a0c
1 /*
2 * Copyright (C) 2014 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 file started out by saying that if you have:
21 * struct foo one, two;
22 * ...
23 * one = two;
25 * That's equivalent to saying:
27 * one.x = two.x;
28 * one.y = two.y;
30 * Turning an assignment like that into a bunch of small fake assignments is
31 * really useful.
33 * The call to memcpy(&one, &two, sizeof(foo)); is the same as "one = two;" so
34 * we can re-use the code. And we may as well use it for memset() too.
35 * Assigning pointers is almost the same:
37 * p1 = p2;
39 * Is the same as:
41 * p1->x = p2->x;
42 * p1->y = p2->y;
44 * The problem is that you can go a bit crazy with pointers to pointers.
46 * p1->x->y->z->one->two->three = p2->x->y->z->one->two->three;
48 * I don't have a proper solution for this problem right now. I just copy one
49 * level and don't nest. It should handle limitted nesting but intelligently.
51 * The other thing is that you end up with a lot of garbage assignments where
52 * we record "x could be anything. x->y could be anything. x->y->z->a->b->c
53 * could *also* be anything!". There should be a better way to filter this
54 * useless information.
58 #include "scope.h"
59 #include "smatch.h"
60 #include "smatch_slist.h"
61 #include "smatch_extra.h"
63 enum {
64 COPY_NORMAL,
65 COPY_MEMCPY,
66 COPY_MEMSET,
69 static struct symbol *get_struct_type(struct expression *expr)
71 struct symbol *type;
73 type = get_type(expr);
74 if (!type)
75 return NULL;
76 if (type->type == SYM_PTR)
77 type = get_real_base_type(type);
78 if (type && type->type == SYM_STRUCT)
79 return type;
80 return NULL;
83 static struct expression *get_right_base_expr(struct symbol *left_type, struct expression *right)
85 struct symbol *struct_type;
87 if (!right)
88 return NULL;
90 struct_type = get_struct_type(right);
91 if (!struct_type)
92 return NULL;
93 if (struct_type != left_type)
94 return NULL;
96 if (right->type == EXPR_PREOP && right->op == '&')
97 right = strip_expr(right->unop);
99 if (right->type == EXPR_CALL)
100 return NULL;
102 if (is_pointer(right))
103 right = deref_expression(right);
105 return right;
108 static struct expression *remove_addr(struct expression *expr)
110 expr = strip_expr(expr);
112 if (expr->type == EXPR_PREOP && expr->op == '&')
113 return strip_expr(expr->unop);
114 return expr;
117 static struct expression *faked_expression;
118 struct expression *get_faked_expression(void)
120 if (!__in_fake_assign)
121 return NULL;
122 return faked_expression;
125 static void set_inner_struct_members(int mode, struct expression *faked, struct expression *left, struct expression *right, struct symbol *member)
127 struct expression *left_member;
128 struct expression *right_member = NULL; /* silence GCC */
129 struct expression *assign;
130 struct symbol *base = get_real_base_type(member);
131 struct symbol *tmp;
133 if (member->ident) {
134 left = member_expression(left, '.', member->ident);
135 if (mode != COPY_MEMSET && right)
136 right = member_expression(right, '.', member->ident);
139 FOR_EACH_PTR(base->symbol_list, tmp) {
140 struct symbol *type;
142 type = get_real_base_type(tmp);
143 if (!type)
144 continue;
146 if (type->type == SYM_ARRAY)
147 continue;
148 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
149 set_inner_struct_members(mode, faked, left, right, tmp);
150 continue;
152 if (!tmp->ident)
153 continue;
155 left_member = member_expression(left, '.', tmp->ident);
157 switch (mode) {
158 case COPY_NORMAL:
159 case COPY_MEMCPY:
160 if (right)
161 right_member = member_expression(right, '.', tmp->ident);
162 else
163 right_member = unknown_value_expression(left_member);
164 break;
165 case COPY_MEMSET:
166 right_member = right;
167 break;
170 assign = assign_expression(left_member, right_member);
171 __in_fake_assign++;
172 __split_expr(assign);
173 __in_fake_assign--;
174 } END_FOR_EACH_PTR(tmp);
179 static void __struct_members_copy(int mode, struct expression *faked,
180 struct expression *left,
181 struct expression *right)
183 struct symbol *struct_type, *tmp, *type;
184 struct expression *left_member;
185 struct expression *right_member;
186 struct expression *assign;
187 int op = '.';
190 if (__in_fake_assign)
191 return;
192 faked_expression = faked;
194 left = strip_expr(left);
195 right = strip_expr(right);
197 struct_type = get_struct_type(left);
198 if (!struct_type) {
200 * This is not a struct assignment obviously. But this is where
201 * memcpy() is handled so it feels like a good place to add this
202 * code.
205 type = get_type(left);
206 if (!type || type->type != SYM_BASETYPE)
207 goto done;
209 right = strip_expr(right);
210 if (right && right->type == EXPR_PREOP && right->op == '&')
211 right = remove_addr(right);
212 else
213 right = unknown_value_expression(left);
214 assign = assign_expression(left, right);
215 __in_fake_assign++;
216 __split_expr(assign);
217 __in_fake_assign--;
218 goto done;
221 if (is_pointer(left)) {
222 left = deref_expression(left);
223 op = '*';
225 if (mode != COPY_MEMSET)
226 right = get_right_base_expr(struct_type, right);
228 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
229 type = get_real_base_type(tmp);
230 if (!type)
231 continue;
232 if (type->type == SYM_ARRAY)
233 continue;
235 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
236 set_inner_struct_members(mode, faked, left, right, tmp);
237 continue;
240 if (!tmp->ident)
241 continue;
243 left_member = member_expression(left, op, tmp->ident);
244 right_member = NULL;
246 switch (mode) {
247 case COPY_NORMAL:
248 case COPY_MEMCPY:
249 if (right)
250 right_member = member_expression(right, op, tmp->ident);
251 else
252 right_member = unknown_value_expression(left_member);
253 break;
254 case COPY_MEMSET:
255 right_member = right;
256 break;
258 if (!right_member) {
259 sm_msg("internal. No right member");
260 continue;
262 assign = assign_expression(left_member, right_member);
263 __in_fake_assign++;
264 __split_expr(assign);
265 __in_fake_assign--;
266 } END_FOR_EACH_PTR(tmp);
268 done:
269 faked_expression = NULL;
272 static int returns_zeroed_mem(struct expression *expr)
274 char *fn;
276 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
277 return 0;
278 fn = expr_to_var(expr->fn);
279 if (!fn)
280 return 0;
281 if (strcmp(fn, "kcalloc") == 0)
282 return 1;
283 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
284 return 1;
285 return 0;
288 void __fake_struct_member_assignments(struct expression *expr)
290 struct symbol *struct_type;
292 if (is_zero(expr->right))
293 return;
295 struct_type = get_struct_type(expr->left);
296 if (!struct_type)
297 return;
299 if (returns_zeroed_mem(expr->right))
300 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
301 else
302 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
305 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
307 struct expression *buf;
308 struct expression *val;
310 buf = get_argument_from_call_expr(expr->args, 0);
311 val = get_argument_from_call_expr(expr->args, 1);
313 buf = strip_expr(buf);
314 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
317 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
319 struct expression *dest;
320 struct expression *src;
322 dest = get_argument_from_call_expr(expr->args, 0);
323 src = get_argument_from_call_expr(expr->args, 1);
325 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
328 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
330 struct expression *dest;
332 dest = get_argument_from_call_expr(expr->args, 0);
333 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
336 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
338 struct expression *arg;
339 int i;
341 i = -1;
342 FOR_EACH_PTR(expr->args, arg) {
343 if (++i < 2)
344 continue;
345 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
346 } END_FOR_EACH_PTR(arg);
349 static void unop_expr(struct expression *expr)
351 if (expr->op != SPECIAL_INCREMENT &&
352 expr->op != SPECIAL_DECREMENT)
353 return;
355 if (!is_pointer(expr))
356 return;
357 __struct_members_copy(COPY_MEMCPY, expr, expr->unop, NULL);
360 static void register_clears_param(void)
362 struct token *token;
363 char name[256];
364 const char *function;
365 int param;
367 if (option_project == PROJ_NONE)
368 return;
370 snprintf(name, 256, "%s.clears_argument", option_project_str);
372 token = get_tokens_file(name);
373 if (!token)
374 return;
375 if (token_type(token) != TOKEN_STREAMBEGIN)
376 return;
377 token = token->next;
378 while (token_type(token) != TOKEN_STREAMEND) {
379 if (token_type(token) != TOKEN_IDENT)
380 return;
381 function = show_ident(token->ident);
382 token = token->next;
383 if (token_type(token) != TOKEN_NUMBER)
384 return;
385 param = atoi(token->number);
386 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
387 token = token->next;
389 clear_token_alloc();
392 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
394 struct expression *arg;
396 while (expr->type == EXPR_ASSIGNMENT)
397 expr = strip_expr(expr->right);
398 if (expr->type != EXPR_CALL)
399 return;
402 * FIXME: __struct_members_copy() requires an expression but
403 * get_variable_from_key() returns a name/sym pair so that doesn't
404 * work here.
406 if (strcmp(key, "$") != 0)
407 return;
409 arg = get_argument_from_call_expr(expr->args, param);
410 if (!arg)
411 return;
413 if (strcmp(value, "0") == 0)
414 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
415 else
416 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
419 void register_struct_assignment(int id)
421 add_function_hook("memset", &match_memset, NULL);
422 add_function_hook("__memset", &match_memset, NULL);
424 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
425 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
426 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
427 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
429 add_function_hook("sscanf", &match_sscanf, NULL);
431 add_hook(&unop_expr, OP_HOOK);
432 register_clears_param();
433 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);