db: introduce get_mtag_name_expr()
[smatch.git] / smatch_struct_assignment.c
blob7c8ce24497bfbc35806533c86c7af44fa3e9c770
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 right = deref_expression(right);
151 assign = assign_expression(left, right);
152 split_fake_expr(assign);
153 return;
155 if (!type || type->type != SYM_BASETYPE)
156 return;
157 right = strip_expr(right);
158 if (!right)
159 right = unknown_value_expression(left);
160 assign = assign_expression(left, right);
161 split_fake_expr(assign);
164 static void set_inner_struct_members(int mode, struct expression *faked, struct expression *left, struct expression *right, struct symbol *member)
166 struct expression *left_member;
167 struct expression *right_member = NULL; /* silence GCC */
168 struct expression *assign;
169 struct symbol *base = get_real_base_type(member);
170 struct symbol *tmp;
172 if (member->ident) {
173 left = member_expression(left, '.', member->ident);
174 if (mode != COPY_MEMSET && right)
175 right = member_expression(right, '.', member->ident);
178 FOR_EACH_PTR(base->symbol_list, tmp) {
179 struct symbol *type;
181 type = get_real_base_type(tmp);
182 if (!type)
183 continue;
185 if (type->type == SYM_ARRAY)
186 continue;
187 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
188 set_inner_struct_members(mode, faked, left, right, tmp);
189 continue;
191 if (!tmp->ident)
192 continue;
194 left_member = member_expression(left, '.', tmp->ident);
196 switch (mode) {
197 case COPY_NORMAL:
198 case COPY_MEMCPY:
199 if (right)
200 right_member = member_expression(right, '.', tmp->ident);
201 else
202 right_member = unknown_value_expression(left_member);
203 break;
204 case COPY_MEMSET:
205 right_member = right;
206 break;
209 assign = assign_expression(left_member, right_member);
210 split_fake_expr(assign);
211 } END_FOR_EACH_PTR(tmp);
214 static void __struct_members_copy(int mode, struct expression *faked,
215 struct expression *left,
216 struct expression *right)
218 struct symbol *struct_type, *tmp, *type;
219 struct expression *left_member;
220 struct expression *right_member;
221 struct expression *assign;
222 int op = '.';
225 if (__in_fake_assign)
226 return;
227 faked_expression = faked;
229 left = strip_expr(left);
230 right = strip_expr(right);
232 struct_type = get_struct_type(left);
233 if (!struct_type) {
235 * This is not a struct assignment obviously. But this is where
236 * memcpy() is handled so it feels like a good place to add this
237 * code.
239 handle_non_struct_assignments(left, right);
240 goto done;
243 if (is_pointer(left)) {
244 left = deref_expression(left);
245 op = '*';
247 if (mode != COPY_MEMSET)
248 right = get_right_base_expr(struct_type, right);
250 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
251 type = get_real_base_type(tmp);
252 if (!type)
253 continue;
254 if (type->type == SYM_ARRAY)
255 continue;
257 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
258 set_inner_struct_members(mode, faked, left, right, tmp);
259 continue;
262 if (!tmp->ident)
263 continue;
265 left_member = member_expression(left, op, tmp->ident);
266 right_member = NULL;
268 switch (mode) {
269 case COPY_NORMAL:
270 case COPY_MEMCPY:
271 if (right)
272 right_member = member_expression(right, op, tmp->ident);
273 else
274 right_member = unknown_value_expression(left_member);
275 break;
276 case COPY_MEMSET:
277 right_member = right;
278 break;
280 if (!right_member) {
281 sm_msg("internal. No right member");
282 continue;
284 assign = assign_expression(left_member, right_member);
285 split_fake_expr(assign);
286 } END_FOR_EACH_PTR(tmp);
288 done:
289 faked_expression = NULL;
292 static int returns_zeroed_mem(struct expression *expr)
294 char *fn;
296 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
297 return 0;
298 fn = expr_to_var(expr->fn);
299 if (!fn)
300 return 0;
301 if (strcmp(fn, "kcalloc") == 0)
302 return 1;
303 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
304 return 1;
305 return 0;
308 static int copy_containter_states(struct expression *left, struct expression *right, int offset)
310 char *left_name = NULL, *right_name = NULL;
311 struct symbol *left_sym, *right_sym;
312 struct sm_state *sm, *new_sm;
313 int ret = 0;
314 int len;
315 char buf[64];
316 char new_name[128];
318 right_name = expr_to_var_sym(right, &right_sym);
319 if (!right_name || !right_sym)
320 goto free;
321 left_name = expr_to_var_sym(left, &left_sym);
322 if (!left_name || !left_sym)
323 goto free;
325 len = snprintf(buf, sizeof(buf), "%s(-%d)", right_name, offset);
326 if (len >= sizeof(buf))
327 goto free;
329 FOR_EACH_SM(__get_cur_stree(), sm) {
330 if (sm->sym != right_sym)
331 continue;
332 if (strncmp(sm->name, buf, len) != 0)
333 continue;
334 snprintf(new_name, sizeof(new_name), "%s%s", left_name, sm->name + len);
335 new_sm = clone_sm(sm);
336 new_sm->name = alloc_sname(new_name);
337 new_sm->sym = left_sym;
338 __set_sm(new_sm);
339 ret = 1;
340 } END_FOR_EACH_SM(sm);
341 free:
342 free_string(left_name);
343 free_string(right_name);
344 return ret;
347 static int handle_param_offsets(struct expression *expr)
349 struct expression *right;
350 sval_t sval;
352 right = strip_expr(expr->right);
354 if (right->type != EXPR_BINOP || right->op != '-')
355 return 0;
357 if (!get_value(right->right, &sval))
358 return 0;
360 right = get_assigned_expr(right->left);
361 if (!right)
362 return 0;
363 return copy_containter_states(expr->left, right, sval.value);
366 static void returns_container_of(struct expression *expr, int param, char *key, char *value)
368 struct expression *call, *arg;
369 int offset;
371 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
372 return;
373 call = strip_expr(expr->right);
374 if (call->type != EXPR_CALL)
375 return;
376 if (param != -1)
377 return;
378 param = atoi(key);
379 offset = atoi(value);
381 arg = get_argument_from_call_expr(call->args, param);
382 if (!arg)
383 return;
385 copy_containter_states(expr->left, arg, -offset);
388 void __fake_struct_member_assignments(struct expression *expr)
390 struct symbol *left_type;
392 if (expr->op != '=')
393 return;
395 if (is_zero(expr->right))
396 return;
398 left_type = get_type(expr->left);
399 if (left_type &&
400 left_type->type != SYM_PTR &&
401 left_type->type != SYM_STRUCT &&
402 left_type != &ulong_ctype)
403 return;
405 if (handle_param_offsets(expr))
406 return;
408 if (returns_zeroed_mem(expr->right))
409 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
410 else
411 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
414 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
416 struct expression *buf;
417 struct expression *val;
419 buf = get_argument_from_call_expr(expr->args, 0);
420 val = get_argument_from_call_expr(expr->args, 1);
422 buf = strip_expr(buf);
423 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
426 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
428 struct expression *dest;
429 struct expression *src;
431 dest = get_argument_from_call_expr(expr->args, 0);
432 src = get_argument_from_call_expr(expr->args, 1);
434 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
437 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
439 struct expression *dest;
441 dest = get_argument_from_call_expr(expr->args, 0);
442 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
445 static void match_sscanf(const char *fn, struct expression *expr, void *unused)
447 struct expression *arg;
448 int i;
450 i = -1;
451 FOR_EACH_PTR(expr->args, arg) {
452 if (++i < 2)
453 continue;
454 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
455 } END_FOR_EACH_PTR(arg);
458 static void unop_expr(struct expression *expr)
460 if (expr->op != SPECIAL_INCREMENT &&
461 expr->op != SPECIAL_DECREMENT)
462 return;
464 if (!is_pointer(expr))
465 return;
466 __struct_members_copy(COPY_MEMCPY, expr, expr->unop, NULL);
469 static void register_clears_param(void)
471 struct token *token;
472 char name[256];
473 const char *function;
474 int param;
476 if (option_project == PROJ_NONE)
477 return;
479 snprintf(name, 256, "%s.clears_argument", option_project_str);
481 token = get_tokens_file(name);
482 if (!token)
483 return;
484 if (token_type(token) != TOKEN_STREAMBEGIN)
485 return;
486 token = token->next;
487 while (token_type(token) != TOKEN_STREAMEND) {
488 if (token_type(token) != TOKEN_IDENT)
489 return;
490 function = show_ident(token->ident);
491 token = token->next;
492 if (token_type(token) != TOKEN_NUMBER)
493 return;
494 param = atoi(token->number);
495 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
496 token = token->next;
498 clear_token_alloc();
501 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
503 struct expression *arg;
505 while (expr->type == EXPR_ASSIGNMENT)
506 expr = strip_expr(expr->right);
507 if (expr->type != EXPR_CALL)
508 return;
511 * FIXME: __struct_members_copy() requires an expression but
512 * get_variable_from_key() returns a name/sym pair so that doesn't
513 * work here.
515 if (strcmp(key, "$") != 0)
516 return;
518 arg = get_argument_from_call_expr(expr->args, param);
519 if (!arg)
520 return;
522 if (strcmp(value, "0") == 0)
523 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
524 else
525 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
528 void register_struct_assignment(int id)
530 add_function_hook("memset", &match_memset, NULL);
531 add_function_hook("__memset", &match_memset, NULL);
533 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
534 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
535 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
536 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
538 add_function_hook("sscanf", &match_sscanf, NULL);
540 add_hook(&unop_expr, OP_HOOK);
541 register_clears_param();
542 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);
544 select_return_states_hook(CONTAINER, &returns_container_of);