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
);
81 if (type
->type
== SYM_STRUCT
)
83 if (type
->type
== SYM_UNION
)
88 static struct expression
*get_right_base_expr(struct symbol
*left_type
, struct expression
*right
)
90 struct symbol
*struct_type
;
95 struct_type
= get_struct_type(right
);
98 if (struct_type
!= left_type
)
101 if (right
->type
== EXPR_PREOP
&& right
->op
== '&')
102 right
= strip_expr(right
->unop
);
104 if (right
->type
== EXPR_CALL
)
107 if (is_pointer(right
))
108 right
= deref_expression(right
);
113 static struct expression
*remove_addr(struct expression
*expr
)
117 expr
= strip_expr(expr
);
121 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
122 return strip_expr(expr
->unop
);
123 type
= get_type(expr
);
126 if (type
->type
!= SYM_PTR
&& type
->type
!= SYM_ARRAY
)
129 return deref_expression(expr
);
132 static struct expression
*faked_expression
;
133 struct expression
*get_faked_expression(void)
135 if (!__in_fake_assign
)
137 return faked_expression
;
140 static void split_fake_expr(struct expression
*expr
)
143 __in_fake_struct_assign
++;
145 __in_fake_struct_assign
--;
149 static void handle_non_struct_assignments(struct expression
*left
, struct expression
*right
)
152 struct expression
*assign
;
154 type
= get_type(left
);
157 if (type
->type
== SYM_PTR
) {
158 left
= deref_expression(left
);
160 right
= deref_expression(right
);
162 right
= unknown_value_expression(left
);
163 assign
= assign_expression(left
, '=', right
);
164 split_fake_expr(assign
);
167 if (type
->type
!= SYM_BASETYPE
)
169 right
= strip_expr(right
);
171 right
= unknown_value_expression(left
);
172 assign
= assign_expression(left
, '=', right
);
173 split_fake_expr(assign
);
176 static void set_inner_struct_members(int mode
, struct expression
*faked
, struct expression
*left
, struct expression
*right
, struct symbol
*member
)
178 struct expression
*left_member
;
179 struct expression
*right_member
= NULL
; /* silence GCC */
180 struct expression
*assign
;
181 struct symbol
*base
= get_real_base_type(member
);
185 left
= member_expression(left
, '.', member
->ident
);
186 if (mode
!= COPY_MEMSET
&& right
)
187 right
= member_expression(right
, '.', member
->ident
);
190 FOR_EACH_PTR(base
->symbol_list
, tmp
) {
193 type
= get_real_base_type(tmp
);
197 if (type
->type
== SYM_ARRAY
)
199 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
200 set_inner_struct_members(mode
, faked
, left
, right
, tmp
);
206 left_member
= member_expression(left
, '.', tmp
->ident
);
212 right_member
= member_expression(right
, '.', tmp
->ident
);
214 right_member
= unknown_value_expression(left_member
);
217 right_member
= right
;
221 assign
= assign_expression(left_member
, '=', right_member
);
222 split_fake_expr(assign
);
223 } END_FOR_EACH_PTR(tmp
);
226 static void __struct_members_copy(int mode
, struct expression
*faked
,
227 struct expression
*left
,
228 struct expression
*right
)
230 struct symbol
*struct_type
, *tmp
, *type
;
231 struct expression
*left_member
;
232 struct expression
*right_member
;
233 struct expression
*assign
;
237 if (__in_fake_assign
)
239 faked_expression
= faked
;
241 left
= strip_expr(left
);
242 right
= strip_expr(right
);
244 struct_type
= get_struct_type(left
);
247 * This is not a struct assignment obviously. But this is where
248 * memcpy() is handled so it feels like a good place to add this
251 handle_non_struct_assignments(left
, right
);
255 if (is_pointer(left
)) {
256 left
= deref_expression(left
);
259 if (mode
!= COPY_MEMSET
)
260 right
= get_right_base_expr(struct_type
, right
);
262 FOR_EACH_PTR(struct_type
->symbol_list
, tmp
) {
263 type
= get_real_base_type(tmp
);
266 if (type
->type
== SYM_ARRAY
)
269 if (type
->type
== SYM_UNION
|| type
->type
== SYM_STRUCT
) {
270 set_inner_struct_members(mode
, faked
, left
, right
, tmp
);
277 left_member
= member_expression(left
, op
, tmp
->ident
);
284 right_member
= member_expression(right
, op
, tmp
->ident
);
286 right_member
= unknown_value_expression(left_member
);
289 right_member
= right
;
293 sm_perror("No right member");
296 assign
= assign_expression(left_member
, '=', right_member
);
297 split_fake_expr(assign
);
298 } END_FOR_EACH_PTR(tmp
);
301 faked_expression
= NULL
;
304 static int returns_zeroed_mem(struct expression
*expr
)
308 if (expr
->type
!= EXPR_CALL
|| expr
->fn
->type
!= EXPR_SYMBOL
)
310 fn
= expr_to_var(expr
->fn
);
313 if (strcmp(fn
, "kcalloc") == 0)
315 if (option_project
== PROJ_KERNEL
&& strstr(fn
, "zalloc"))
320 static int copy_containter_states(struct expression
*left
, struct expression
*right
, int offset
)
322 char *left_name
= NULL
, *right_name
= NULL
;
323 struct symbol
*left_sym
, *right_sym
;
324 struct sm_state
*sm
, *new_sm
;
330 right_name
= expr_to_var_sym(right
, &right_sym
);
331 if (!right_name
|| !right_sym
)
333 left_name
= expr_to_var_sym(left
, &left_sym
);
334 if (!left_name
|| !left_sym
)
337 len
= snprintf(buf
, sizeof(buf
), "%s(-%d)", right_name
, offset
);
338 if (len
>= sizeof(buf
))
341 FOR_EACH_SM(__get_cur_stree(), sm
) {
342 if (sm
->sym
!= right_sym
)
344 if (strncmp(sm
->name
, buf
, len
) != 0)
346 snprintf(new_name
, sizeof(new_name
), "%s%s", left_name
, sm
->name
+ len
);
347 new_sm
= clone_sm(sm
);
348 new_sm
->name
= alloc_sname(new_name
);
349 new_sm
->sym
= left_sym
;
352 } END_FOR_EACH_SM(sm
);
354 free_string(left_name
);
355 free_string(right_name
);
359 static int handle_param_offsets(struct expression
*expr
)
361 struct expression
*right
;
364 right
= strip_expr(expr
->right
);
366 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
369 if (!get_value(right
->right
, &sval
))
372 right
= get_assigned_expr(right
->left
);
375 return copy_containter_states(expr
->left
, right
, sval
.value
);
378 static void returns_container_of(struct expression
*expr
, int param
, char *key
, char *value
)
380 struct expression
*call
, *arg
;
383 if (expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
385 call
= strip_expr(expr
->right
);
386 if (call
->type
!= EXPR_CALL
)
391 offset
= atoi(value
);
393 arg
= get_argument_from_call_expr(call
->args
, param
);
397 copy_containter_states(expr
->left
, arg
, -offset
);
400 void __fake_struct_member_assignments(struct expression
*expr
)
402 struct symbol
*left_type
;
407 if (is_zero(expr
->right
))
410 left_type
= get_type(expr
->left
);
412 (left_type
->type
!= SYM_PTR
&&
413 left_type
->type
!= SYM_STRUCT
))
416 if (handle_param_offsets(expr
))
419 if (returns_zeroed_mem(expr
->right
))
420 __struct_members_copy(COPY_MEMSET
, expr
, expr
->left
, zero_expr());
422 __struct_members_copy(COPY_NORMAL
, expr
, expr
->left
, expr
->right
);
425 static void match_memset(const char *fn
, struct expression
*expr
, void *_size_arg
)
427 struct expression
*buf
;
428 struct expression
*val
;
430 buf
= get_argument_from_call_expr(expr
->args
, 0);
431 val
= get_argument_from_call_expr(expr
->args
, 1);
433 buf
= strip_expr(buf
);
434 __struct_members_copy(COPY_MEMSET
, expr
, remove_addr(buf
), val
);
437 static void match_memcpy(const char *fn
, struct expression
*expr
, void *_arg
)
439 struct expression
*dest
;
440 struct expression
*src
;
442 dest
= get_argument_from_call_expr(expr
->args
, 0);
443 src
= get_argument_from_call_expr(expr
->args
, 1);
445 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(dest
), remove_addr(src
));
448 static void match_memcpy_unknown(const char *fn
, struct expression
*expr
, void *_arg
)
450 struct expression
*dest
;
452 dest
= get_argument_from_call_expr(expr
->args
, 0);
453 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(dest
), NULL
);
456 static void match_sscanf(const char *fn
, struct expression
*expr
, void *unused
)
458 struct expression
*arg
;
462 FOR_EACH_PTR(expr
->args
, arg
) {
465 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(arg
), NULL
);
466 } END_FOR_EACH_PTR(arg
);
469 static void unop_expr(struct expression
*expr
)
471 if (expr
->op
!= SPECIAL_INCREMENT
&&
472 expr
->op
!= SPECIAL_DECREMENT
)
475 if (!is_pointer(expr
))
477 faked_expression
= expr
;
478 __struct_members_copy(COPY_MEMCPY
, expr
, expr
->unop
, NULL
);
479 faked_expression
= NULL
;
482 static void register_clears_param(void)
486 const char *function
;
489 if (option_project
== PROJ_NONE
)
492 snprintf(name
, 256, "%s.clears_argument", option_project_str
);
494 token
= get_tokens_file(name
);
497 if (token_type(token
) != TOKEN_STREAMBEGIN
)
500 while (token_type(token
) != TOKEN_STREAMEND
) {
501 if (token_type(token
) != TOKEN_IDENT
)
503 function
= show_ident(token
->ident
);
505 if (token_type(token
) != TOKEN_NUMBER
)
507 param
= atoi(token
->number
);
508 add_function_hook(function
, &match_memcpy_unknown
, INT_PTR(param
));
514 static void db_param_cleared(struct expression
*expr
, int param
, char *key
, char *value
)
516 struct expression
*arg
;
518 while (expr
->type
== EXPR_ASSIGNMENT
)
519 expr
= strip_expr(expr
->right
);
520 if (expr
->type
!= EXPR_CALL
)
524 * FIXME: __struct_members_copy() requires an expression but
525 * get_variable_from_key() returns a name/sym pair so that doesn't
528 if (strcmp(key
, "$") != 0)
531 arg
= get_argument_from_call_expr(expr
->args
, param
);
535 if (strcmp(value
, "0") == 0)
536 __struct_members_copy(COPY_MEMSET
, expr
, remove_addr(arg
), zero_expr());
538 __struct_members_copy(COPY_MEMCPY
, expr
, remove_addr(arg
), NULL
);
541 void register_struct_assignment(int id
)
543 add_function_hook("memset", &match_memset
, NULL
);
544 add_function_hook("__memset", &match_memset
, NULL
);
546 add_function_hook("memcpy", &match_memcpy
, INT_PTR(0));
547 add_function_hook("memmove", &match_memcpy
, INT_PTR(0));
548 add_function_hook("__memcpy", &match_memcpy
, INT_PTR(0));
549 add_function_hook("__memmove", &match_memcpy
, INT_PTR(0));
551 add_function_hook("sscanf", &match_sscanf
, NULL
);
553 add_hook(&unop_expr
, OP_HOOK
);
554 register_clears_param();
555 select_return_states_hook(PARAM_CLEARED
, &db_param_cleared
);
557 select_return_states_hook(CONTAINER
, &returns_container_of
);