8ef2e1708bed88b87e8a69075e13e9c3918715b4
[smatch.git] / smatch_struct_assignment.c
blob8ef2e1708bed88b87e8a69075e13e9c3918715b4
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 static struct symbol *get_struct_type(struct expression *expr)
65 struct symbol *type;
67 type = get_type(expr);
68 if (!type)
69 return NULL;
70 if (type->type == SYM_PTR)
71 type = get_real_base_type(type);
72 if (type && type->type == SYM_STRUCT)
73 return type;
74 return NULL;
77 static struct expression *get_matching_member_expr(struct symbol *left_type, struct expression *right, struct symbol *left_member)
79 struct symbol *struct_type;
80 int op = '.';
82 if (!left_member->ident)
83 return NULL;
85 struct_type = get_struct_type(right);
86 if (!struct_type)
87 return NULL;
88 if (struct_type != left_type)
89 return NULL;
91 if (right->type == EXPR_PREOP && right->op == '&')
92 right = strip_expr(right->unop);
94 if (is_pointer(right)) {
95 right = deref_expression(right);
96 op = '*';
99 return member_expression(right, op, left_member->ident);
102 static struct expression *remove_addr(struct expression *expr)
104 expr = strip_expr(expr);
106 if (expr->type == EXPR_PREOP && expr->op == '&')
107 return strip_expr(expr->unop);
108 return expr;
111 void __struct_members_copy(int mode, struct expression *left, struct expression *right)
113 struct symbol *struct_type, *tmp, *type;
114 struct expression *left_member;
115 struct expression *right_member = NULL;
116 struct expression *assign;
117 int op = '.';
120 if (__in_fake_assign)
121 return;
123 left = strip_expr(left);
124 right = strip_expr(right);
126 struct_type = get_struct_type(left);
127 if (!struct_type) {
129 * This is not a struct assignment obviously. But this is where
130 * memcpy() is handled so it feels like a good place to add this
131 * code.
134 type = get_type(left);
135 if (!type || type->type != SYM_BASETYPE)
136 return;
138 right = strip_expr(right);
139 if (right && right->type == EXPR_PREOP && right->op == '&')
140 right = remove_addr(right);
141 else
142 right = unknown_value_expression(left);
143 assign = assign_expression(left, right);
144 __in_fake_assign++;
145 __split_expr(assign);
146 __in_fake_assign--;
147 return;
150 if (is_pointer(left)) {
151 left = deref_expression(left);
152 op = '*';
155 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
156 type = get_real_base_type(tmp);
157 if (type && type->type == SYM_ARRAY)
158 continue;
160 left_member = member_expression(left, op, tmp->ident);
162 switch (mode) {
163 case COPY_NORMAL:
164 case COPY_MEMCPY:
165 right_member = get_matching_member_expr(struct_type, right, tmp);
166 break;
167 case COPY_MEMSET:
168 right_member = right;
169 break;
171 if (!right_member)
172 right_member = unknown_value_expression(left_member);
173 assign = assign_expression(left_member, right_member);
174 __in_fake_assign++;
175 __split_expr(assign);
176 __in_fake_assign--;
177 } END_FOR_EACH_PTR(tmp);
180 void __fake_struct_member_assignments(struct expression *expr)
182 struct symbol *struct_type;
184 if (is_zero(expr->right))
185 return;
187 struct_type = get_struct_type(expr->left);
188 if (!struct_type)
189 return;
191 __struct_members_copy(COPY_NORMAL, expr->left, expr->right);
194 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
196 struct expression *buf;
197 struct expression *val;
199 buf = get_argument_from_call_expr(expr->args, 0);
200 val = get_argument_from_call_expr(expr->args, 1);
202 buf = strip_expr(buf);
203 __struct_members_copy(COPY_MEMSET, remove_addr(buf), val);
206 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
208 struct expression *dest;
209 struct expression *src;
211 dest = get_argument_from_call_expr(expr->args, 0);
212 src = get_argument_from_call_expr(expr->args, 1);
214 __struct_members_copy(COPY_MEMCPY, remove_addr(dest), remove_addr(src));
217 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
219 struct expression *dest;
221 dest = get_argument_from_call_expr(expr->args, 0);
222 __struct_members_copy(COPY_MEMCPY, remove_addr(dest), NULL);
225 static void register_clears_param(void)
227 struct token *token;
228 char name[256];
229 const char *function;
230 int param;
232 if (option_project == PROJ_NONE)
233 return;
235 snprintf(name, 256, "%s.clears_argument", option_project_str);
237 token = get_tokens_file(name);
238 if (!token)
239 return;
240 if (token_type(token) != TOKEN_STREAMBEGIN)
241 return;
242 token = token->next;
243 while (token_type(token) != TOKEN_STREAMEND) {
244 if (token_type(token) != TOKEN_IDENT)
245 return;
246 function = show_ident(token->ident);
247 token = token->next;
248 if (token_type(token) != TOKEN_NUMBER)
249 return;
250 param = atoi(token->number);
251 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
252 token = token->next;
254 clear_token_alloc();
257 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
259 struct expression *arg;
261 while (expr->type == EXPR_ASSIGNMENT)
262 expr = strip_expr(expr->right);
263 if (expr->type != EXPR_CALL)
264 return;
266 arg = get_argument_from_call_expr(expr->args, param);
267 if (!arg)
268 return;
270 if (strcmp(value, "0") == 0)
271 __struct_members_copy(COPY_MEMSET, remove_addr(arg), zero_expr());
272 else
273 __struct_members_copy(COPY_MEMCPY, remove_addr(arg), NULL);
277 void register_struct_assignment(int id)
279 add_function_hook("memset", &match_memset, NULL);
281 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
282 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
284 register_clears_param();
285 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);