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;
25 * That's equivalent to saying:
30 * Turning an assignment like that into a bunch of small fake assignments is
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:
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.
60 #include "smatch_slist.h"
61 #include "smatch_extra.h"
69 static struct symbol
*get_struct_type(struct expression
*expr
)
73 type
= get_type(expr
);
76 if (type
->type
== SYM_PTR
)
77 type
= get_real_base_type(type
);
78 if (type
&& type
->type
== SYM_STRUCT
)
83 static struct expression
*get_right_base_expr(struct symbol
*left_type
, struct expression
*right
)
85 struct symbol
*struct_type
;
90 struct_type
= get_struct_type(right
);
93 if (struct_type
!= left_type
)
96 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
97 right
= strip_expr(right
->unop
);
99 if (right
->type
== EXPR_CALL
)
102 if (is_pointer(right
))
103 right
= deref_expression(right
);
108 static struct expression
*remove_addr(struct expression
*expr
)
112 expr
= strip_expr(expr
);
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
)
122 return deref_expression(expr
);
125 static struct expression
*faked_expression
;
126 struct expression
*get_faked_expression(void)
128 if (!__in_fake_assign
)
130 return faked_expression
;
133 static void split_fake_expr(struct expression
*expr
)
140 static void handle_non_struct_assignments(struct expression
*left
, struct expression
*right
)
143 struct expression
*assign
;
145 type
= get_type(left
);
146 if (!type
|| type
->type
!= SYM_BASETYPE
)
148 right
= strip_expr(right
);
149 if (right
&& right
->type
== EXPR_PREOP
&& right
->op
== '&')
150 right
= remove_addr(right
);
152 right
= unknown_value_expression(left
);
153 assign
= assign_expression(left
, right
);
154 split_fake_expr(assign
);
157 static void set_inner_struct_members(int mode
, struct expression
*faked
, struct expression
*left
, struct expression
*right
, struct symbol
*member
)
159 struct expression
*left_member
;
160 struct expression
*right_member
= NULL
; /* silence GCC */
161 struct expression
*assign
;
162 struct symbol
*base
= get_real_base_type(member
);
166 left
= member_expression(left
, '.', member
->ident
);
167 if (mode
!= COPY_MEMSET
&& right
)
168 right
= member_expression(right
, '.', member
->ident
);
171 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
174 type
= get_real_base_type(tmp
);
178 if (type
->type
== SYM_ARRAY
)
180 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
181 set_inner_struct_members(mode
, faked
, left
, right
, tmp
);
187 left_member
= member_expression(left
, '.', tmp
->ident
);
193 right_member
= member_expression(right
, '.', tmp
->ident
);
195 right_member
= unknown_value_expression(left_member
);
198 right_member
= right
;
202 assign
= assign_expression(left_member
, right_member
);
203 split_fake_expr(assign
);
204 } END_FOR_EACH_PTR(tmp
);
207 static void __struct_members_copy(int mode
, struct expression
*faked
,
208 struct expression
*left
,
209 struct expression
*right
)
211 struct symbol
*struct_type
, *tmp
, *type
;
212 struct expression
*left_member
;
213 struct expression
*right_member
;
214 struct expression
*assign
;
218 if (__in_fake_assign
)
220 faked_expression
= faked
;
222 left
= strip_expr(left
);
223 right
= strip_expr(right
);
225 struct_type
= get_struct_type(left
);
228 * This is not a struct assignment obviously. But this is where
229 * memcpy() is handled so it feels like a good place to add this
232 handle_non_struct_assignments(left
, right
);
236 if (is_pointer(left
)) {
237 left
= deref_expression(left
);
240 if (mode
!= COPY_MEMSET
)
241 right
= get_right_base_expr(struct_type
, right
);
243 FOR_EACH_PTR(struct_type
->symbol_list
, tmp
) {
244 type
= get_real_base_type(tmp
);
247 if (type
->type
== SYM_ARRAY
)
250 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
251 set_inner_struct_members(mode
, faked
, left
, right
, tmp
);
258 left_member
= member_expression(left
, op
, tmp
->ident
);
265 right_member
= member_expression(right
, op
, tmp
->ident
);
267 right_member
= unknown_value_expression(left_member
);
270 right_member
= right
;
274 sm_msg("internal. No right member");
277 assign
= assign_expression(left_member
, right_member
);
278 split_fake_expr(assign
);
279 } END_FOR_EACH_PTR(tmp
);
282 faked_expression
= NULL
;
285 static int returns_zeroed_mem(struct expression
*expr
)
289 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
)
291 fn
= expr_to_var(expr
->fn
);
294 if (strcmp(fn
, "kcalloc") == 0)
296 if (option_project
== PROJ_KERNEL
&& strstr(fn
, "zalloc"))
301 static int copy_containter_states(struct expression
*left
, struct expression
*right
, int offset
)
303 char *left_name
= NULL
, *right_name
= NULL
;
304 struct symbol
*left_sym
, *right_sym
;
305 struct sm_state
*sm
, *new_sm
;
311 right_name
= expr_to_var_sym(right
, &right_sym
);
312 if (!right_name
|| !right_sym
)
314 left_name
= expr_to_var_sym(left
, &left_sym
);
315 if (!left_name
|| !left_sym
)
318 len
= snprintf(buf
, sizeof(buf
), "%s(-%d)", right_name
, offset
);
319 if (len
>= sizeof(buf
))
322 FOR_EACH_SM(__get_cur_stree(), sm
) {
323 if (sm
->sym
!= right_sym
)
325 if (strncmp(sm
->name
, buf
, len
) != 0)
327 snprintf(new_name
, sizeof(new_name
), "%s%s", left_name
, sm
->name
+ len
);
328 new_sm
= clone_sm(sm
);
329 new_sm
->name
= alloc_sname(new_name
);
330 new_sm
->sym
= left_sym
;
333 } END_FOR_EACH_SM(sm
);
335 free_string(left_name
);
336 free_string(right_name
);
340 static int handle_param_offsets(struct expression
*expr
)
342 struct expression
*right
;
345 right
= strip_expr(expr
->right
);
347 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
350 if (!get_value(right
->right
, &sval
))
353 right
= get_assigned_expr(right
->left
);
356 return copy_containter_states(expr
->left
, right
, sval
.value
);
359 static void returns_container_of(struct expression
*expr
, int param
, char *key
, char *value
)
361 struct expression
*call
, *arg
;
364 if (expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
366 call
= strip_expr(expr
->right
);
367 if (call
->type
!= EXPR_CALL
)
372 offset
= atoi(value
);
374 arg
= get_argument_from_call_expr(call
->args
, param
);
378 copy_containter_states(expr
->left
, arg
, -offset
);
381 void __fake_struct_member_assignments(struct expression
*expr
)
383 struct symbol
*struct_type
;
384 struct symbol
*left_type
;
389 if (is_zero(expr
->right
))
392 left_type
= get_type(expr
->left
);
394 left_type
->type
!= SYM_PTR
&&
395 left_type
->type
!= SYM_STRUCT
&&
396 left_type
!= &ulong_ctype
)
399 struct_type
= get_struct_type(expr
->left
);
403 if (handle_param_offsets(expr
))
406 if (returns_zeroed_mem(expr
->right
))
407 __struct_members_copy(COPY_MEMSET
, expr
, expr
->left
, zero_expr());
409 __struct_members_copy(COPY_NORMAL
, expr
, expr
->left
, expr
->right
);
412 static void match_memset(const char *fn
, struct expression
*expr
, void *_size_arg
)
414 struct expression
*buf
;
415 struct expression
*val
;
417 buf
= get_argument_from_call_expr(expr
->args
, 0);
418 val
= get_argument_from_call_expr(expr
->args
, 1);
420 buf
= strip_expr(buf
);
421 __struct_members_copy(COPY_MEMSET
, expr
, remove_addr(buf
), val
);
424 static void match_memcpy(const char *fn
, struct expression
*expr
, void *_arg
)
426 struct expression
*dest
;
427 struct expression
*src
;
429 dest
= get_argument_from_call_expr(expr
->args
, 0);
430 src
= get_argument_from_call_expr(expr
->args
, 1);
432 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(dest
), remove_addr(src
));
435 static void match_memcpy_unknown(const char *fn
, struct expression
*expr
, void *_arg
)
437 struct expression
*dest
;
439 dest
= get_argument_from_call_expr(expr
->args
, 0);
440 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(dest
), NULL
);
443 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
445 struct expression
*arg
;
449 FOR_EACH_PTR(expr
->args
, arg
) {
452 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(arg
), NULL
);
453 } END_FOR_EACH_PTR(arg
);
456 static void unop_expr(struct expression
*expr
)
458 if (expr
->op
!= SPECIAL_INCREMENT
&&
459 expr
->op
!= SPECIAL_DECREMENT
)
462 if (!is_pointer(expr
))
464 __struct_members_copy(COPY_MEMCPY
, expr
, expr
->unop
, NULL
);
467 static void register_clears_param(void)
471 const char *function
;
474 if (option_project
== PROJ_NONE
)
477 snprintf(name
, 256, "%s.clears_argument", option_project_str
);
479 token
= get_tokens_file(name
);
482 if (token_type(token
) != TOKEN_STREAMBEGIN
)
485 while (token_type(token
) != TOKEN_STREAMEND
) {
486 if (token_type(token
) != TOKEN_IDENT
)
488 function
= show_ident(token
->ident
);
490 if (token_type(token
) != TOKEN_NUMBER
)
492 param
= atoi(token
->number
);
493 add_function_hook(function
, &match_memcpy_unknown
, INT_PTR(param
));
499 static void db_param_cleared(struct expression
*expr
, int param
, char *key
, char *value
)
501 struct expression
*arg
;
503 while (expr
->type
== EXPR_ASSIGNMENT
)
504 expr
= strip_expr(expr
->right
);
505 if (expr
->type
!= EXPR_CALL
)
509 * FIXME: __struct_members_copy() requires an expression but
510 * get_variable_from_key() returns a name/sym pair so that doesn't
513 if (strcmp(key
, "$") != 0)
516 arg
= get_argument_from_call_expr(expr
->args
, param
);
520 if (strcmp(value
, "0") == 0)
521 __struct_members_copy(COPY_MEMSET
, expr
, remove_addr(arg
), zero_expr());
523 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(arg
), NULL
);
526 void register_struct_assignment(int id
)
528 add_function_hook("memset", &match_memset
, NULL
);
529 add_function_hook("__memset", &match_memset
, NULL
);
531 add_function_hook("memcpy", &match_memcpy
, INT_PTR(0));
532 add_function_hook("memmove", &match_memcpy
, INT_PTR(0));
533 add_function_hook("__memcpy", &match_memcpy
, INT_PTR(0));
534 add_function_hook("__memmove", &match_memcpy
, INT_PTR(0));
536 add_function_hook("sscanf", &match_sscanf
, NULL
);
538 add_hook(&unop_expr
, OP_HOOK
);
539 register_clears_param();
540 select_return_states_hook(PARAM_CLEARED
, &db_param_cleared
);
542 select_return_states_hook(CONTAINER
, &returns_container_of
);