free: improve cross function tracking
[smatch.git] / smatch_struct_assignment.c
blobd742ef64ff53e20276e238e2dd053f5f820d5b32
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_matching_member_expr(struct symbol *left_type, struct expression *right, struct symbol *left_member)
85 struct symbol *struct_type;
86 int op = '.';
88 if (!left_member->ident)
89 return NULL;
91 struct_type = get_struct_type(right);
92 if (!struct_type)
93 return NULL;
94 if (struct_type != left_type)
95 return NULL;
97 if (right->type == EXPR_PREOP && right->op == '&')
98 right = strip_expr(right->unop);
100 if (is_pointer(right)) {
101 right = deref_expression(right);
102 op = '*';
105 return member_expression(right, op, left_member->ident);
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 __struct_members_copy(int mode, struct expression *faked,
126 struct expression *left,
127 struct expression *right)
129 struct symbol *struct_type, *tmp, *type;
130 struct expression *left_member;
131 struct expression *right_member = NULL;
132 struct expression *assign;
133 int op = '.';
136 if (__in_fake_assign)
137 return;
138 faked_expression = faked;
140 left = strip_expr(left);
141 right = strip_expr(right);
143 struct_type = get_struct_type(left);
144 if (!struct_type) {
146 * This is not a struct assignment obviously. But this is where
147 * memcpy() is handled so it feels like a good place to add this
148 * code.
151 type = get_type(left);
152 if (!type || type->type != SYM_BASETYPE)
153 goto done;
155 right = strip_expr(right);
156 if (right && right->type == EXPR_PREOP && right->op == '&')
157 right = remove_addr(right);
158 else
159 right = unknown_value_expression(left);
160 assign = assign_expression(left, right);
161 __in_fake_assign++;
162 __split_expr(assign);
163 __in_fake_assign--;
164 goto done;
167 if (is_pointer(left)) {
168 left = deref_expression(left);
169 op = '*';
172 FOR_EACH_PTR(struct_type->symbol_list, tmp) {
173 type = get_real_base_type(tmp);
174 if (type && type->type == SYM_ARRAY)
175 continue;
177 left_member = member_expression(left, op, tmp->ident);
179 switch (mode) {
180 case COPY_NORMAL:
181 case COPY_MEMCPY:
182 right_member = get_matching_member_expr(struct_type, right, tmp);
183 break;
184 case COPY_MEMSET:
185 right_member = right;
186 break;
188 if (!right_member)
189 right_member = unknown_value_expression(left_member);
190 assign = assign_expression(left_member, right_member);
191 __in_fake_assign++;
192 __split_expr(assign);
193 __in_fake_assign--;
194 } END_FOR_EACH_PTR(tmp);
196 done:
197 faked_expression = NULL;
200 static int returns_zeroed_mem(struct expression *expr)
202 char *fn;
204 if (expr->type != EXPR_CALL || expr->fn->type != EXPR_SYMBOL)
205 return 0;
206 fn = expr_to_var(expr->fn);
207 if (!fn)
208 return 0;
209 if (strcmp(fn, "kcalloc") == 0)
210 return 1;
211 if (option_project == PROJ_KERNEL && strstr(fn, "zalloc"))
212 return 1;
213 return 0;
216 void __fake_struct_member_assignments(struct expression *expr)
218 struct symbol *struct_type;
220 if (is_zero(expr->right))
221 return;
223 struct_type = get_struct_type(expr->left);
224 if (!struct_type)
225 return;
227 if (returns_zeroed_mem(expr->right))
228 __struct_members_copy(COPY_MEMSET, expr, expr->left, zero_expr());
229 else
230 __struct_members_copy(COPY_NORMAL, expr, expr->left, expr->right);
233 static void match_memset(const char *fn, struct expression *expr, void *_size_arg)
235 struct expression *buf;
236 struct expression *val;
238 buf = get_argument_from_call_expr(expr->args, 0);
239 val = get_argument_from_call_expr(expr->args, 1);
241 buf = strip_expr(buf);
242 __struct_members_copy(COPY_MEMSET, expr, remove_addr(buf), val);
245 static void match_memcpy(const char *fn, struct expression *expr, void *_arg)
247 struct expression *dest;
248 struct expression *src;
250 dest = get_argument_from_call_expr(expr->args, 0);
251 src = get_argument_from_call_expr(expr->args, 1);
253 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), remove_addr(src));
256 static void match_memcpy_unknown(const char *fn, struct expression *expr, void *_arg)
258 struct expression *dest;
260 dest = get_argument_from_call_expr(expr->args, 0);
261 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(dest), NULL);
264 static void register_clears_param(void)
266 struct token *token;
267 char name[256];
268 const char *function;
269 int param;
271 if (option_project == PROJ_NONE)
272 return;
274 snprintf(name, 256, "%s.clears_argument", option_project_str);
276 token = get_tokens_file(name);
277 if (!token)
278 return;
279 if (token_type(token) != TOKEN_STREAMBEGIN)
280 return;
281 token = token->next;
282 while (token_type(token) != TOKEN_STREAMEND) {
283 if (token_type(token) != TOKEN_IDENT)
284 return;
285 function = show_ident(token->ident);
286 token = token->next;
287 if (token_type(token) != TOKEN_NUMBER)
288 return;
289 param = atoi(token->number);
290 add_function_hook(function, &match_memcpy_unknown, INT_PTR(param));
291 token = token->next;
293 clear_token_alloc();
296 static void db_param_cleared(struct expression *expr, int param, char *key, char *value)
298 struct expression *arg;
300 while (expr->type == EXPR_ASSIGNMENT)
301 expr = strip_expr(expr->right);
302 if (expr->type != EXPR_CALL)
303 return;
305 arg = get_argument_from_call_expr(expr->args, param);
306 if (!arg)
307 return;
309 if (strcmp(value, "0") == 0)
310 __struct_members_copy(COPY_MEMSET, expr, remove_addr(arg), zero_expr());
311 else
312 __struct_members_copy(COPY_MEMCPY, expr, remove_addr(arg), NULL);
316 void register_struct_assignment(int id)
318 add_function_hook("memset", &match_memset, NULL);
320 add_function_hook("memcpy", &match_memcpy, INT_PTR(0));
321 add_function_hook("memmove", &match_memcpy, INT_PTR(0));
323 register_clears_param();
324 select_return_states_hook(PARAM_CLEARED, &db_param_cleared);