Documentation: make me less confused
[smatch.git] / smatch_struct_assignment.c
blob54be27b13c5d4cb629d095f4af9f5e8de57c7f9f
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, *right_member, *assign;
128 struct symbol *base = get_real_base_type(member);
129 struct symbol *tmp;
131 if (member->ident) {
132 left = member_expression(left, '.', member->ident);
133 if (mode != COPY_MEMSET && right)
134 right = member_expression(right, '.', member->ident);
137 FOR_EACH_PTR(base->symbol_list, tmp) {
138 struct symbol *type;
140 type = get_real_base_type(tmp);
141 if (!type)
142 continue;
144 if (type->type == SYM_ARRAY)
145 continue;
146 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
147 set_inner_struct_members(mode, faked, left, right, tmp);
148 continue;
150 if (!tmp->ident)
151 continue;
153 left_member = member_expression(left, '.', tmp->ident);
155 switch (mode) {
156 case COPY_NORMAL:
157 case COPY_MEMCPY:
158 if (right)
159 right_member = member_expression(right, '.', tmp->ident);
160 else
161 right_member = unknown_value_expression(left_member);
162 break;
163 case COPY_MEMSET:
164 right_member = right;
165 break;
168 assign = assign_expression(left_member, right_member);
169 __in_fake_assign++;
170 __split_expr(assign);
171 __in_fake_assign--;
172 } END_FOR_EACH_PTR(tmp);
177 static void __struct_members_copy(int mode, struct expression *faked,
178 struct expression *left,
179 struct expression *right)
181 struct symbol *struct_type, *tmp, *type;
182 struct expression *left_member;
183 struct expression *right_member;
184 struct expression *assign;
185 int op = '.';
188 if (__in_fake_assign)
189 return;
190 faked_expression = faked;
192 left = strip_expr(left);
193 right = strip_expr(right);
195 struct_type = get_struct_type(left);
196 if (!struct_type) {
198 * This is not a struct assignment obviously. But this is where
199 * memcpy() is handled so it feels like a good place to add this
200 * code.
203 type = get_type(left);
204 if (!type || type->type != SYM_BASETYPE)
205 goto done;
207 right = strip_expr(right);
208 if (right && right->type == EXPR_PREOP && right->op == '&')
209 right = remove_addr(right);
210 else
211 right = unknown_value_expression(left);
212 assign = assign_expression(left, right);
213 __in_fake_assign++;
214 __split_expr(assign);
215 __in_fake_assign--;
216 goto done;
219 if (is_pointer(left)) {
220 left = deref_expression(left);
221 op = '*';
223 if (mode != COPY_MEMSET)
224 right = get_right_base_expr(struct_type, right);
226 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
227 type = get_real_base_type(tmp);
228 if (!type)
229 continue;
230 if (type->type == SYM_ARRAY)
231 continue;
233 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
234 set_inner_struct_members(mode, faked, left, right, tmp);
235 continue;
238 if (!tmp->ident)
239 continue;
241 left_member = member_expression(left, op, tmp->ident);
242 right_member = NULL;
244 switch (mode) {
245 case COPY_NORMAL:
246 case COPY_MEMCPY:
247 if (right)
248 right_member = member_expression(right, op, tmp->ident);
249 else
250 right_member = unknown_value_expression(left_member);
251 break;
252 case COPY_MEMSET:
253 right_member = right;
254 break;
256 if (!right_member) {
257 sm_msg("internal. No right member");
258 continue;
260 assign = assign_expression(left_member, right_member);
261 __in_fake_assign++;
262 __split_expr(assign);
263 __in_fake_assign--;
264 } END_FOR_EACH_PTR(tmp);
266 done:
267 faked_expression = NULL;
270 static int returns_zeroed_mem(struct expression *expr)
272 char *fn;
274 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
275 return 0;
276 fn = expr_to_var(expr->fn);
277 if (!fn)
278 return 0;
279 if (strcmp(fn, "kcalloc") == 0)
280 return 1;
281 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
282 return 1;
283 return 0;
286 void __fake_struct_member_assignments(struct expression *expr)
288 struct symbol *struct_type;
290 if (is_zero(expr->right))
291 return;
293 struct_type = get_struct_type(expr->left);
294 if (!struct_type)
295 return;
297 if (returns_zeroed_mem(expr->right))
298 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
299 else
300 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
303 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
305 struct expression *buf;
306 struct expression *val;
308 buf = get_argument_from_call_expr(expr->args, 0);
309 val = get_argument_from_call_expr(expr->args, 1);
311 buf = strip_expr(buf);
312 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
315 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
317 struct expression *dest;
318 struct expression *src;
320 dest = get_argument_from_call_expr(expr->args, 0);
321 src = get_argument_from_call_expr(expr->args, 1);
323 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
326 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
328 struct expression *dest;
330 dest = get_argument_from_call_expr(expr->args, 0);
331 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
334 static void register_clears_param(void)
336 struct token *token;
337 char name[256];
338 const char *function;
339 int param;
341 if (option_project == PROJ_NONE)
342 return;
344 snprintf(name, 256, "%s.clears_argument", option_project_str);
346 token = get_tokens_file(name);
347 if (!token)
348 return;
349 if (token_type(token) != TOKEN_STREAMBEGIN)
350 return;
351 token = token->next;
352 while (token_type(token) != TOKEN_STREAMEND) {
353 if (token_type(token) != TOKEN_IDENT)
354 return;
355 function = show_ident(token->ident);
356 token = token->next;
357 if (token_type(token) != TOKEN_NUMBER)
358 return;
359 param = atoi(token->number);
360 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
361 token = token->next;
363 clear_token_alloc();
366 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
368 struct expression *arg;
370 while (expr->type == EXPR_ASSIGNMENT)
371 expr = strip_expr(expr->right);
372 if (expr->type != EXPR_CALL)
373 return;
376 * FIXME: __struct_members_copy() requires an expression but
377 * get_variable_from_key() returns a name/sym pair so that doesn't
378 * work here.
380 if (strcmp(key, "$") != 0)
381 return;
383 arg = get_argument_from_call_expr(expr->args, param);
384 if (!arg)
385 return;
387 if (strcmp(value, "0") == 0)
388 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
389 else
390 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
393 void register_struct_assignment(int id)
395 add_function_hook("memset", &match_memset, NULL);
396 add_function_hook("__memset", &match_memset, NULL);
398 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
399 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
400 add_function_hook("__memcpy", &match_memcpy, INT_PTR(0));
401 add_function_hook("__memmove", &match_memcpy, INT_PTR(0));
403 register_clears_param();
404 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);