rosenberg: fix a crashing bug
[smatch.git] / smatch_struct_assignment.c
blob620d7f9c9c73901a23a338f9ed3dadad57cdde65
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 __split_expr(expr);
137 __in_fake_assign--;
140 static void handle_non_struct_assignments(struct expression *left, struct expression *right)
142 struct symbol *type;
143 struct expression *assign;
145 type = get_type(left);
146 if (!type)
147 return;
148 if (type->type == SYM_PTR) {
149 left = deref_expression(left);
150 if (right)
151 right = deref_expression(right);
152 else
153 right = unknown_value_expression(left);
154 assign = assign_expression(left, right);
155 split_fake_expr(assign);
156 return;
158 if (type->type != SYM_BASETYPE)
159 return;
160 right = strip_expr(right);
161 if (!right)
162 right = unknown_value_expression(left);
163 assign = assign_expression(left, right);
164 split_fake_expr(assign);
167 static void set_inner_struct_members(int mode, struct expression *faked, struct expression *left, struct expression *right, struct symbol *member)
169 struct expression *left_member;
170 struct expression *right_member = NULL; /* silence GCC */
171 struct expression *assign;
172 struct symbol *base = get_real_base_type(member);
173 struct symbol *tmp;
175 if (member->ident) {
176 left = member_expression(left, '.', member->ident);
177 if (mode != COPY_MEMSET && right)
178 right = member_expression(right, '.', member->ident);
181 FOR_EACH_PTR(base->symbol_list, tmp) {
182 struct symbol *type;
184 type = get_real_base_type(tmp);
185 if (!type)
186 continue;
188 if (type->type == SYM_ARRAY)
189 continue;
190 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
191 set_inner_struct_members(mode, faked, left, right, tmp);
192 continue;
194 if (!tmp->ident)
195 continue;
197 left_member = member_expression(left, '.', tmp->ident);
199 switch (mode) {
200 case COPY_NORMAL:
201 case COPY_MEMCPY:
202 if (right)
203 right_member = member_expression(right, '.', tmp->ident);
204 else
205 right_member = unknown_value_expression(left_member);
206 break;
207 case COPY_MEMSET:
208 right_member = right;
209 break;
212 assign = assign_expression(left_member, right_member);
213 split_fake_expr(assign);
214 } END_FOR_EACH_PTR(tmp);
217 static void __struct_members_copy(int mode, struct expression *faked,
218 struct expression *left,
219 struct expression *right)
221 struct symbol *struct_type, *tmp, *type;
222 struct expression *left_member;
223 struct expression *right_member;
224 struct expression *assign;
225 int op = '.';
228 if (__in_fake_assign)
229 return;
230 faked_expression = faked;
232 left = strip_expr(left);
233 right = strip_expr(right);
235 struct_type = get_struct_type(left);
236 if (!struct_type) {
238 * This is not a struct assignment obviously. But this is where
239 * memcpy() is handled so it feels like a good place to add this
240 * code.
242 handle_non_struct_assignments(left, right);
243 goto done;
246 if (is_pointer(left)) {
247 left = deref_expression(left);
248 op = '*';
250 if (mode != COPY_MEMSET)
251 right = get_right_base_expr(struct_type, right);
253 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
254 type = get_real_base_type(tmp);
255 if (!type)
256 continue;
257 if (type->type == SYM_ARRAY)
258 continue;
260 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
261 set_inner_struct_members(mode, faked, left, right, tmp);
262 continue;
265 if (!tmp->ident)
266 continue;
268 left_member = member_expression(left, op, tmp->ident);
269 right_member = NULL;
271 switch (mode) {
272 case COPY_NORMAL:
273 case COPY_MEMCPY:
274 if (right)
275 right_member = member_expression(right, op, tmp->ident);
276 else
277 right_member = unknown_value_expression(left_member);
278 break;
279 case COPY_MEMSET:
280 right_member = right;
281 break;
283 if (!right_member) {
284 sm_msg("internal. No right member");
285 continue;
287 assign = assign_expression(left_member, right_member);
288 split_fake_expr(assign);
289 } END_FOR_EACH_PTR(tmp);
291 done:
292 faked_expression = NULL;
295 static int returns_zeroed_mem(struct expression *expr)
297 char *fn;
299 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
300 return 0;
301 fn = expr_to_var(expr->fn);
302 if (!fn)
303 return 0;
304 if (strcmp(fn, "kcalloc") == 0)
305 return 1;
306 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
307 return 1;
308 return 0;
311 static int copy_containter_states(struct expression *left, struct expression *right, int offset)
313 char *left_name = NULL, *right_name = NULL;
314 struct symbol *left_sym, *right_sym;
315 struct sm_state *sm, *new_sm;
316 int ret = 0;
317 int len;
318 char buf[64];
319 char new_name[128];
321 right_name = expr_to_var_sym(right, &right_sym);
322 if (!right_name || !right_sym)
323 goto free;
324 left_name = expr_to_var_sym(left, &left_sym);
325 if (!left_name || !left_sym)
326 goto free;
328 len = snprintf(buf, sizeof(buf), "%s(-%d)", right_name, offset);
329 if (len >= sizeof(buf))
330 goto free;
332 FOR_EACH_SM(__get_cur_stree(), sm) {
333 if (sm->sym != right_sym)
334 continue;
335 if (strncmp(sm->name, buf, len) != 0)
336 continue;
337 snprintf(new_name, sizeof(new_name), "%s%s", left_name, sm->name + len);
338 new_sm = clone_sm(sm);
339 new_sm->name = alloc_sname(new_name);
340 new_sm->sym = left_sym;
341 __set_sm(new_sm);
342 ret = 1;
343 } END_FOR_EACH_SM(sm);
344 free:
345 free_string(left_name);
346 free_string(right_name);
347 return ret;
350 static int handle_param_offsets(struct expression *expr)
352 struct expression *right;
353 sval_t sval;
355 right = strip_expr(expr->right);
357 if (right->type != EXPR_BINOP || right->op != '-')
358 return 0;
360 if (!get_value(right->right, &sval))
361 return 0;
363 right = get_assigned_expr(right->left);
364 if (!right)
365 return 0;
366 return copy_containter_states(expr->left, right, sval.value);
369 static void returns_container_of(struct expression *expr, int param, char *key, char *value)
371 struct expression *call, *arg;
372 int offset;
374 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
375 return;
376 call = strip_expr(expr->right);
377 if (call->type != EXPR_CALL)
378 return;
379 if (param != -1)
380 return;
381 param = atoi(key);
382 offset = atoi(value);
384 arg = get_argument_from_call_expr(call->args, param);
385 if (!arg)
386 return;
388 copy_containter_states(expr->left, arg, -offset);
391 void __fake_struct_member_assignments(struct expression *expr)
393 struct symbol *left_type;
395 if (expr->op != '=')
396 return;
398 if (is_zero(expr->right))
399 return;
401 left_type = get_type(expr->left);
402 if (!left_type ||
403 (left_type->type != SYM_PTR &&
404 left_type->type != SYM_STRUCT))
405 return;
407 if (handle_param_offsets(expr))
408 return;
410 if (returns_zeroed_mem(expr->right))
411 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
412 else
413 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
416 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
418 struct expression *buf;
419 struct expression *val;
421 buf = get_argument_from_call_expr(expr->args, 0);
422 val = get_argument_from_call_expr(expr->args, 1);
424 buf = strip_expr(buf);
425 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
428 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
430 struct expression *dest;
431 struct expression *src;
433 dest = get_argument_from_call_expr(expr->args, 0);
434 src = get_argument_from_call_expr(expr->args, 1);
436 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
439 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
441 struct expression *dest;
443 dest = get_argument_from_call_expr(expr->args, 0);
444 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
447 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
449 struct expression *arg;
450 int i;
452 i = -1;
453 FOR_EACH_PTR(expr->args, arg) {
454 if (++i < 2)
455 continue;
456 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
457 } END_FOR_EACH_PTR(arg);
460 static void unop_expr(struct expression *expr)
462 if (expr->op != SPECIAL_INCREMENT &&
463 expr->op != SPECIAL_DECREMENT)
464 return;
466 if (!is_pointer(expr))
467 return;
468 __struct_members_copy(COPY_MEMCPY, expr, expr->unop, NULL);
471 static void register_clears_param(void)
473 struct token *token;
474 char name[256];
475 const char *function;
476 int param;
478 if (option_project == PROJ_NONE)
479 return;
481 snprintf(name, 256, "%s.clears_argument", option_project_str);
483 token = get_tokens_file(name);
484 if (!token)
485 return;
486 if (token_type(token) != TOKEN_STREAMBEGIN)
487 return;
488 token = token->next;
489 while (token_type(token) != TOKEN_STREAMEND) {
490 if (token_type(token) != TOKEN_IDENT)
491 return;
492 function = show_ident(token->ident);
493 token = token->next;
494 if (token_type(token) != TOKEN_NUMBER)
495 return;
496 param = atoi(token->number);
497 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
498 token = token->next;
500 clear_token_alloc();
503 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
505 struct expression *arg;
507 while (expr->type == EXPR_ASSIGNMENT)
508 expr = strip_expr(expr->right);
509 if (expr->type != EXPR_CALL)
510 return;
513 * FIXME: __struct_members_copy() requires an expression but
514 * get_variable_from_key() returns a name/sym pair so that doesn't
515 * work here.
517 if (strcmp(key, "$") != 0)
518 return;
520 arg = get_argument_from_call_expr(expr->args, param);
521 if (!arg)
522 return;
524 if (strcmp(value, "0") == 0)
525 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
526 else
527 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
530 void register_struct_assignment(int id)
532 add_function_hook("memset", &match_memset, NULL);
533 add_function_hook("__memset", &match_memset, NULL);
535 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
536 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
537 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
538 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
540 add_function_hook("sscanf", &match_sscanf, NULL);
542 add_hook(&unop_expr, OP_HOOK);
543 register_clears_param();
544 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);
546 select_return_states_hook(CONTAINER, &returns_container_of);