db/fixup_kernel.sh: commit all my stuff
[smatch.git] / smatch_struct_assignment.c
blobf9efcaec2a7024203e1c79f7307c8d76b387690a
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 struct symbol *type;
112 expr = strip_expr(expr);
113 if (!expr)
114 return NULL;
116 if (expr->type == EXPR_PREOP && expr->op == '&')
117 return strip_expr(expr->unop);
118 type = get_type(expr);
119 if (!type || type->type != SYM_PTR)
120 return expr;
122 return deref_expression(expr);
125 static struct expression *faked_expression;
126 struct expression *get_faked_expression(void)
128 if (!__in_fake_assign)
129 return NULL;
130 return faked_expression;
133 static void split_fake_expr(struct expression *expr)
135 __in_fake_assign++;
136 __in_fake_struct_assign++;
137 __split_expr(expr);
138 __in_fake_struct_assign--;
139 __in_fake_assign--;
142 static void handle_non_struct_assignments(struct expression *left, struct expression *right)
144 struct symbol *type;
145 struct expression *assign;
147 type = get_type(left);
148 if (!type)
149 return;
150 if (type->type == SYM_PTR) {
151 left = deref_expression(left);
152 if (right)
153 right = deref_expression(right);
154 else
155 right = unknown_value_expression(left);
156 assign = assign_expression(left, '=', right);
157 split_fake_expr(assign);
158 return;
160 if (type->type != SYM_BASETYPE)
161 return;
162 right = strip_expr(right);
163 if (!right)
164 right = unknown_value_expression(left);
165 assign = assign_expression(left, '=', right);
166 split_fake_expr(assign);
169 static void set_inner_struct_members(int mode, struct expression *faked, struct expression *left, struct expression *right, struct symbol *member)
171 struct expression *left_member;
172 struct expression *right_member = NULL; /* silence GCC */
173 struct expression *assign;
174 struct symbol *base = get_real_base_type(member);
175 struct symbol *tmp;
177 if (member->ident) {
178 left = member_expression(left, '.', member->ident);
179 if (mode != COPY_MEMSET && right)
180 right = member_expression(right, '.', member->ident);
183 FOR_EACH_PTR(base->symbol_list, tmp) {
184 struct symbol *type;
186 type = get_real_base_type(tmp);
187 if (!type)
188 continue;
190 if (type->type == SYM_ARRAY)
191 continue;
192 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
193 set_inner_struct_members(mode, faked, left, right, tmp);
194 continue;
196 if (!tmp->ident)
197 continue;
199 left_member = member_expression(left, '.', tmp->ident);
201 switch (mode) {
202 case COPY_NORMAL:
203 case COPY_MEMCPY:
204 if (right)
205 right_member = member_expression(right, '.', tmp->ident);
206 else
207 right_member = unknown_value_expression(left_member);
208 break;
209 case COPY_MEMSET:
210 right_member = right;
211 break;
214 assign = assign_expression(left_member, '=', right_member);
215 split_fake_expr(assign);
216 } END_FOR_EACH_PTR(tmp);
219 static void __struct_members_copy(int mode, struct expression *faked,
220 struct expression *left,
221 struct expression *right)
223 struct symbol *struct_type, *tmp, *type;
224 struct expression *left_member;
225 struct expression *right_member;
226 struct expression *assign;
227 int op = '.';
230 if (__in_fake_assign)
231 return;
232 faked_expression = faked;
234 left = strip_expr(left);
235 right = strip_expr(right);
237 struct_type = get_struct_type(left);
238 if (!struct_type) {
240 * This is not a struct assignment obviously. But this is where
241 * memcpy() is handled so it feels like a good place to add this
242 * code.
244 handle_non_struct_assignments(left, right);
245 goto done;
248 if (is_pointer(left)) {
249 left = deref_expression(left);
250 op = '*';
252 if (mode != COPY_MEMSET)
253 right = get_right_base_expr(struct_type, right);
255 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
256 type = get_real_base_type(tmp);
257 if (!type)
258 continue;
259 if (type->type == SYM_ARRAY)
260 continue;
262 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
263 set_inner_struct_members(mode, faked, left, right, tmp);
264 continue;
267 if (!tmp->ident)
268 continue;
270 left_member = member_expression(left, op, tmp->ident);
271 right_member = NULL;
273 switch (mode) {
274 case COPY_NORMAL:
275 case COPY_MEMCPY:
276 if (right)
277 right_member = member_expression(right, op, tmp->ident);
278 else
279 right_member = unknown_value_expression(left_member);
280 break;
281 case COPY_MEMSET:
282 right_member = right;
283 break;
285 if (!right_member) {
286 sm_msg("internal. No right member");
287 continue;
289 assign = assign_expression(left_member, '=', right_member);
290 split_fake_expr(assign);
291 } END_FOR_EACH_PTR(tmp);
293 done:
294 faked_expression = NULL;
297 static int returns_zeroed_mem(struct expression *expr)
299 char *fn;
301 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
302 return 0;
303 fn = expr_to_var(expr->fn);
304 if (!fn)
305 return 0;
306 if (strcmp(fn, "kcalloc") == 0)
307 return 1;
308 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
309 return 1;
310 return 0;
313 static int copy_containter_states(struct expression *left, struct expression *right, int offset)
315 char *left_name = NULL, *right_name = NULL;
316 struct symbol *left_sym, *right_sym;
317 struct sm_state *sm, *new_sm;
318 int ret = 0;
319 int len;
320 char buf[64];
321 char new_name[128];
323 right_name = expr_to_var_sym(right, &right_sym);
324 if (!right_name || !right_sym)
325 goto free;
326 left_name = expr_to_var_sym(left, &left_sym);
327 if (!left_name || !left_sym)
328 goto free;
330 len = snprintf(buf, sizeof(buf), "%s(-%d)", right_name, offset);
331 if (len >= sizeof(buf))
332 goto free;
334 FOR_EACH_SM(__get_cur_stree(), sm) {
335 if (sm->sym != right_sym)
336 continue;
337 if (strncmp(sm->name, buf, len) != 0)
338 continue;
339 snprintf(new_name, sizeof(new_name), "%s%s", left_name, sm->name + len);
340 new_sm = clone_sm(sm);
341 new_sm->name = alloc_sname(new_name);
342 new_sm->sym = left_sym;
343 __set_sm(new_sm);
344 ret = 1;
345 } END_FOR_EACH_SM(sm);
346 free:
347 free_string(left_name);
348 free_string(right_name);
349 return ret;
352 static int handle_param_offsets(struct expression *expr)
354 struct expression *right;
355 sval_t sval;
357 right = strip_expr(expr->right);
359 if (right->type != EXPR_BINOP || right->op != '-')
360 return 0;
362 if (!get_value(right->right, &sval))
363 return 0;
365 right = get_assigned_expr(right->left);
366 if (!right)
367 return 0;
368 return copy_containter_states(expr->left, right, sval.value);
371 static void returns_container_of(struct expression *expr, int param, char *key, char *value)
373 struct expression *call, *arg;
374 int offset;
376 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
377 return;
378 call = strip_expr(expr->right);
379 if (call->type != EXPR_CALL)
380 return;
381 if (param != -1)
382 return;
383 param = atoi(key);
384 offset = atoi(value);
386 arg = get_argument_from_call_expr(call->args, param);
387 if (!arg)
388 return;
390 copy_containter_states(expr->left, arg, -offset);
393 void __fake_struct_member_assignments(struct expression *expr)
395 struct symbol *left_type;
397 if (expr->op != '=')
398 return;
400 if (is_zero(expr->right))
401 return;
403 left_type = get_type(expr->left);
404 if (!left_type ||
405 (left_type->type != SYM_PTR &&
406 left_type->type != SYM_STRUCT))
407 return;
409 if (handle_param_offsets(expr))
410 return;
412 if (returns_zeroed_mem(expr->right))
413 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
414 else
415 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
418 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
420 struct expression *buf;
421 struct expression *val;
423 buf = get_argument_from_call_expr(expr->args, 0);
424 val = get_argument_from_call_expr(expr->args, 1);
426 buf = strip_expr(buf);
427 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
430 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
432 struct expression *dest;
433 struct expression *src;
435 dest = get_argument_from_call_expr(expr->args, 0);
436 src = get_argument_from_call_expr(expr->args, 1);
438 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
441 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
443 struct expression *dest;
445 dest = get_argument_from_call_expr(expr->args, 0);
446 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
449 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
451 struct expression *arg;
452 int i;
454 i = -1;
455 FOR_EACH_PTR(expr->args, arg) {
456 if (++i < 2)
457 continue;
458 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
459 } END_FOR_EACH_PTR(arg);
462 static void unop_expr(struct expression *expr)
464 if (expr->op != SPECIAL_INCREMENT &&
465 expr->op != SPECIAL_DECREMENT)
466 return;
468 if (!is_pointer(expr))
469 return;
470 __struct_members_copy(COPY_MEMCPY, expr, expr->unop, NULL);
473 static void register_clears_param(void)
475 struct token *token;
476 char name[256];
477 const char *function;
478 int param;
480 if (option_project == PROJ_NONE)
481 return;
483 snprintf(name, 256, "%s.clears_argument", option_project_str);
485 token = get_tokens_file(name);
486 if (!token)
487 return;
488 if (token_type(token) != TOKEN_STREAMBEGIN)
489 return;
490 token = token->next;
491 while (token_type(token) != TOKEN_STREAMEND) {
492 if (token_type(token) != TOKEN_IDENT)
493 return;
494 function = show_ident(token->ident);
495 token = token->next;
496 if (token_type(token) != TOKEN_NUMBER)
497 return;
498 param = atoi(token->number);
499 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
500 token = token->next;
502 clear_token_alloc();
505 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
507 struct expression *arg;
509 while (expr->type == EXPR_ASSIGNMENT)
510 expr = strip_expr(expr->right);
511 if (expr->type != EXPR_CALL)
512 return;
515 * FIXME: __struct_members_copy() requires an expression but
516 * get_variable_from_key() returns a name/sym pair so that doesn't
517 * work here.
519 if (strcmp(key, "$") != 0)
520 return;
522 arg = get_argument_from_call_expr(expr->args, param);
523 if (!arg)
524 return;
526 if (strcmp(value, "0") == 0)
527 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
528 else
529 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
532 void register_struct_assignment(int id)
534 add_function_hook("memset", &match_memset, NULL);
535 add_function_hook("__memset", &match_memset, NULL);
537 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
538 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
539 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
540 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
542 add_function_hook("sscanf", &match_sscanf, NULL);
544 add_hook(&unop_expr, OP_HOOK);
545 register_clears_param();
546 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);
548 select_return_states_hook(CONTAINER, &returns_container_of);