2 * Copyright (C) 2020 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
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
25 static void undef(struct sm_state
*sm
, struct expression
*mod_expr
)
27 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
30 char *swap_names(const char *orig
, const char *remove
, const char *add
)
35 bool is_star
= false; /* fixme: this should be star_cnt */
50 while(orig
[offset
] == '*' || orig
[offset
] == '&' || orig
[offset
] == '(')
54 if (len
+ offset
> strlen(orig
))
56 if (orig
[offset
+ len
] == '\0')
58 else if (orig
[offset
+ len
] != '-')
60 if (strncmp(orig
+ offset
, remove
, len
) != 0)
63 if (!is_star
&& is_end
)
66 ret
= snprintf(buf
, sizeof(buf
), "%.*s%s%s%s", offset
, orig
,
68 is_end
? "" : (is_addr
? "." : "->"),
69 is_end
? "" : orig
+ offset
+ 2 + len
);
70 if (ret
>= sizeof(buf
))
72 return alloc_string(buf
);
75 static char *swap_with_param(const char *name
, struct symbol
*sym
, struct symbol
**sym_p
)
77 struct smatch_state
*state
;
78 struct var_sym
*var_sym
;
82 * Say you know that "foo = bar;" and you have a state "foo->baz" then
83 * we can just substitute "bar" for "foo" giving "bar->baz".
86 if (!sym
|| !sym
->ident
)
89 state
= get_state(my_id
, sym
->ident
->name
, sym
);
90 if (!state
|| !state
->data
)
92 var_sym
= state
->data
;
94 ret
= swap_names(name
, sym
->ident
->name
, var_sym
->var
);
98 *sym_p
= var_sym
->sym
;
102 struct expression
*map_container_of_to_simpler_expr_key(struct expression
*expr
, const char *orig_key
, char **new_key
)
104 struct expression
*container
;
106 char *p
= (char *)orig_key
;
112 bool no_member
= false;
114 expr
= strip_expr(expr
);
115 if (expr
->type
!= EXPR_DEREF
&&
116 (expr
->type
!= EXPR_PREOP
&& expr
->op
== '&'))
120 if (*p
== '(' && isdigit(*(p
+ 1))) {
122 offset
= strtoul(p
+ 1, &p
, 10);
123 if (!p
|| strncmp(p
, "<~$", 3) != 0)
128 param
= strtoul(p
+ 1, &p
, 10);
134 if (strcmp(p
, ")") == 0) {
139 if (strncmp(p
, ")->", 3) != 0)
146 if (!no_member
&& *p
== '\0')
149 if (offset
== get_member_offset_from_deref(expr
)) {
150 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
151 expr
= strip_expr(expr
->unop
);
152 if (expr
->type
!= EXPR_DEREF
)
154 expr
= strip_expr(expr
->deref
);
155 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
157 container
= expr
->unop
;
160 container
= expr
->deref
;
162 container
= get_stored_container(expr
, offset
);
169 *new_key
= alloc_sname("$");
173 ret
= snprintf(buf
, sizeof(buf
), "%.*s$%s%s", (int)(start
- orig_key
), orig_key
, arrow
? "->" : ".", p
);
174 if (ret
>= sizeof(buf
))
176 *new_key
= alloc_sname(buf
);
181 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
)
186 bool address
= false;
188 bool add_dot
= false;
191 // FIXME: this function has been marked for being made static
192 // Use get_name_sym_from_param_key().
200 arg
= strip_expr(arg
);
202 if (strcmp(key
, "$") == 0)
203 return expr_to_var_sym(arg
, sym
);
205 if (strcmp(key
, "*$") == 0) {
206 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
207 arg
= strip_expr(arg
->unop
);
208 return expr_to_var_sym(arg
, sym
);
210 tmp
= expr_to_var_sym(arg
, sym
);
213 ret
= snprintf(buf
, sizeof(buf
), "*%s", tmp
);
215 if (ret
>= sizeof(buf
))
217 return alloc_string(buf
);
221 if (strncmp(key
, "(*$)", 4) == 0) {
222 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
223 arg
= strip_expr(arg
->unop
);
224 snprintf(buf
, sizeof(buf
), "$%s", key
+ 4);
225 return get_variable_from_key(arg
, buf
, sym
);
227 tmp
= expr_to_var_sym(arg
, sym
);
230 ret
= snprintf(buf
, sizeof(buf
), "(*%s)%s", tmp
, key
+ 4);
232 if (ret
>= sizeof(buf
))
234 return alloc_string(buf
);
238 if (strstr(key
, "<~$")) {
239 struct expression
*expr
;
240 char *new_key
= NULL
;
242 expr
= map_container_of_to_simpler_expr_key(arg
, key
, &new_key
);
247 *sym
= expr_to_sym(expr
);
252 while (key
[0] == '*') {
263 * FIXME: This is a hack.
264 * We should be able to parse expressions like (*$)->foo and *$->foo.
266 type
= get_type(arg
);
267 if (is_struct_ptr(type
))
270 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&' && star_cnt
&& !add_dot
) {
271 arg
= strip_expr(arg
->unop
);
275 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
276 arg
= strip_expr(arg
->unop
);
277 tmp
= expr_to_var_sym(arg
, sym
);
280 ret
= snprintf(buf
, sizeof(buf
), "%s%.*s%s.%s",
281 address
? "&" : "", star_cnt
, "**********",
283 if (ret
>= sizeof(buf
))
285 return alloc_string(buf
);
288 tmp
= expr_to_var_sym(arg
, sym
);
291 ret
= snprintf(buf
, sizeof(buf
), "%s%.*s%s%s",
292 address
? "&" : "", star_cnt
, "**********", tmp
, key
+ 1);
294 if (ret
>= sizeof(buf
))
296 return alloc_string(buf
);
299 bool split_param_key(const char *value
, int *param
, char *key
, int len
)
304 l
= snprintf(key
, len
, "%s", value
);
309 while (*p
&& *p
!= '$')
316 if (*param
< 0 || *param
> 99)
325 memmove(p
- skip
, p
, l
- (p
- key
) + 1);
330 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
)
332 struct smatch_state
*state
;
333 struct expression
*arg
;
339 while (expr
->type
== EXPR_ASSIGNMENT
)
341 if (expr
->type
!= EXPR_CALL
)
344 if (!split_param_key(data
, ¶m
, buf
, sizeof(buf
)))
347 if (strcmp(buf
, "$") == 0) {
348 arg
= get_argument_from_call_expr(expr
->args
, param
);
351 return get_implied_rl(arg
, rl
);
354 name
= get_name_sym_from_param_key(expr
, param
, buf
, &sym
);
358 state
= get_state(SMATCH_EXTRA
, name
, sym
);
359 if (!estate_rl(state
))
361 *rl
= estate_rl(state
);
365 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
)
369 if (strcmp("$", key
) == 0)
370 return expr_to_chunk_sym_vsl(arg
, sym
, vsl
);
371 return get_variable_from_key(arg
, key
, sym
);
374 static char *state_name_to_param_name(const char *state_name
, const char *param_name
)
376 bool address
= false;
383 * Normally what happens is that we map "*foo->bar" to "*param->bar"
384 * but with container_of() there is no notation for that in C and it's
385 * just a Smatch invention. So in that case, the state name is the
388 if (strstr(state_name
, "<~$"))
389 return (char *)state_name
;
391 name_len
= strlen(param_name
);
393 while (state_name
[0] == '*') {
398 if (state_name
[0] == '&') {
403 /* ten out of ten stars! */
407 if (strncmp(state_name
, "(*", 2) == 0 &&
408 strncmp(state_name
+ 2, param_name
, name_len
) == 0 &&
409 state_name
[name_len
+ 2] == ')') {
410 ret
= snprintf(buf
, sizeof(buf
), "%s%.*s(*$)%s",
412 star_cnt
, "**********",
413 state_name
+ name_len
+ 3);
414 if (ret
>= sizeof(buf
))
416 return alloc_sname(buf
);
419 if (strcmp(state_name
, param_name
) == 0) {
420 snprintf(buf
, sizeof(buf
), "%s%.*s$",
422 star_cnt
, "**********");
423 return alloc_sname(buf
);
426 /* check for '-' from "->" */
427 if (strncmp(state_name
, param_name
, name_len
) == 0 &&
428 state_name
[name_len
] == '-') {
429 ret
= snprintf(buf
, sizeof(buf
), "%s%.*s$%s",
431 star_cnt
, "**********",
432 state_name
+ name_len
);
433 if (ret
>= sizeof(buf
))
435 return alloc_sname(buf
);
440 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
)
442 if (!sym
|| !sym
->ident
)
445 return state_name_to_param_name(name
, sym
->ident
->name
);
448 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
)
451 const char *sym_name
;
453 static char buf
[256];
456 * mtag_name is different from param_name because mtags can be a struct
457 * instead of a struct pointer. But we want to treat it like a pointer
458 * because really an mtag is a pointer. Or in other words, if you pass
459 * a struct foo then you want to talk about foo.bar but with an mtag
460 * you want to refer to it as foo->bar.
464 if (!sym
|| !sym
->ident
)
467 type
= get_real_base_type(sym
);
468 if (type
&& type
->type
== SYM_BASETYPE
)
471 sym_name
= sym
->ident
->name
;
472 name_len
= strlen(sym_name
);
474 if (state_name
[name_len
] == '.' && /* check for '-' from "->" */
475 strncmp(state_name
, sym_name
, name_len
) == 0) {
476 snprintf(buf
, sizeof(buf
), "$->%s", state_name
+ name_len
+ 1);
480 return state_name_to_param_name(state_name
, sym_name
);
483 const char *get_mtag_name_expr(struct expression
*expr
)
487 const char *ret
= NULL
;
489 name
= expr_to_var_sym(expr
, &sym
);
493 ret
= get_mtag_name_var_sym(name
, sym
);
499 char *get_param_name(struct sm_state
*sm
)
501 return get_param_name_var_sym(sm
->name
, sm
->sym
);
504 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
)
506 struct smatch_state
*state
;
507 struct var_sym
*var_sym
;
512 // FIXME was modified...
514 param
= get_param_num_from_sym(sym
);
517 return alloc_string(name
);
520 state
= get_state(my_id
, name
, sym
);
521 if (state
&& state
->data
) {
522 var_sym
= state
->data
;
526 *sym_p
= var_sym
->sym
;
527 return alloc_string(var_sym
->var
);
530 /* One would think that handling container_of() should be done here
531 * but it it's quite tricky because we only have a name and a sym
532 * and none of the assignments have been handled yet, either here or
533 * in smatch_assignments.c. On the other hand handling container_of()
534 * in the assignment hook has the advantage that it saves resources and
535 * it should work fine because of the fake assignments which we do.
538 return swap_with_param(name
, sym
, sym_p
);
541 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
)
544 const char *ret
= NULL
;
547 name
= expr_to_var_sym(expr
, &sym
);
551 ret
= get_param_var_sym_var_sym(name
, sym
, NULL
, sym_p
);
554 return alloc_string(ret
);
557 int get_return_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
558 struct expression
*ret_expr
,
561 const char *param_name
;
562 struct symbol
*ret_sym
;
568 ret_str
= expr_to_str_sym(ret_expr
, &ret_sym
);
569 if (ret_str
&& ret_sym
== sym
) {
570 param_name
= state_name_to_param_name(name
, ret_str
);
572 free_string(ret_str
);
578 free_string(ret_str
);
583 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
584 struct expression
*ret_expr
,
587 const char *param_name
;
589 struct symbol
*other_sym
;
595 /* straight forward param match */
596 param
= get_param_num_from_sym(sym
);
598 param_name
= get_param_name_var_sym(name
, sym
);
606 param
= get_return_param_key_from_var_sym(name
, sym
, ret_expr
, key
);
610 other_name
= get_param_var_sym_var_sym(name
, sym
, ret_expr
, &other_sym
);
611 if (!other_name
|| !other_sym
)
613 param
= get_param_num_from_sym(other_sym
);
615 sm_msg("internal: '%s' parameter not found", other_name
);
619 param_name
= get_param_name_var_sym(other_name
, other_sym
);
628 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
631 return get_param_key_from_var_sym(sm
->name
, sm
->sym
, ret_expr
, key
);
634 int get_param_key_from_expr(struct expression
*expr
, struct expression
*ret_expr
,
642 name
= expr_to_var_sym(expr
, &sym
);
646 ret
= get_param_key_from_var_sym(name
, sym
, ret_expr
, key
);
652 const char *get_param_key_swap_dollar(struct expression
*expr
)
659 sm
= get_sm_state_expr(my_id
, expr
);
660 if (!sm
|| slist_has_state(sm
->possible
, &undefined
))
663 param
= get_param_key_from_expr(expr
, NULL
, &key
);
667 p
= strchr(key
, '$');
671 snprintf(buf
, sizeof(buf
), "%.*s%d%s", (int)(p
- key
+ 1), key
, param
, p
+ 1);
672 return alloc_sname(buf
);
675 int map_to_param(const char *name
, struct symbol
*sym
)
677 return get_param_key_from_var_sym(name
, sym
, NULL
, NULL
);
680 int get_param_num_from_sym(struct symbol
*sym
)
686 return UNKNOWN_SCOPE
;
688 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
) {
689 if (sym
->ctype
.modifiers
& MOD_STATIC
)
696 sm_msg("warn: internal. problem with scope: %s",
697 sym
->ident
? sym
->ident
->name
: "<anon var>");
704 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
708 } END_FOR_EACH_PTR(tmp
);
712 int get_param_num(struct expression
*expr
)
718 return UNKNOWN_SCOPE
;
719 name
= expr_to_var_sym(expr
, &sym
);
722 return UNKNOWN_SCOPE
;
723 return get_param_num_from_sym(sym
);
726 struct symbol
*get_param_sym_from_num(int num
)
735 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, sym
) {
738 } END_FOR_EACH_PTR(sym
);
742 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
)
744 struct expression
*call
, *arg
;
751 sm_msg("internal: null call_expr. param=%d key='%s'", param
, key
);
756 while (call
->type
== EXPR_ASSIGNMENT
)
757 call
= strip_expr(call
->right
);
759 if (call
->type
!= EXPR_CALL
)
763 expr
->type
== EXPR_ASSIGNMENT
&&
765 name
= get_variable_from_key(expr
->left
, key
, sym
);
766 if (!name
|| (sym
&& !*sym
))
768 } else if (param
>= 0) {
769 arg
= get_argument_from_call_expr(call
->args
, param
);
773 name
= get_variable_from_key(arg
, key
, sym
);
774 if (!name
|| (sym
&& !*sym
))
777 name
= alloc_string(key
);
786 static char *handle_container_of_assign(struct expression
*expr
, struct symbol
**sym
)
788 struct expression
*right
, *orig
;
794 type
= get_type(expr
->left
);
795 if (!type
|| type
->type
!= SYM_PTR
)
798 right
= strip_expr(expr
->right
);
799 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
802 if (!get_value(right
->right
, &sval
) ||
803 sval
.value
< 0 || sval
.value
> MTAG_OFFSET_MASK
)
806 orig
= get_assigned_expr(right
->left
);
809 if (orig
->type
!= EXPR_SYMBOL
)
811 param
= get_param_num_from_sym(orig
->symbol
);
815 snprintf(buf
, sizeof(buf
), "(%lld<~$%d)", sval
.value
, param
);
817 return alloc_string(buf
);
820 const char *get_container_of_str(struct expression
*expr
)
822 struct smatch_state
*state
;
824 state
= get_state_expr(my_id
, expr
);
827 if (!strstr(state
->name
, "<~$"))
832 static void match_assign(struct expression
*expr
)
834 struct symbol
*param_sym
;
840 /* __in_fake_parameter_assign is included deliberately */
841 if (is_fake_call(expr
->right
) ||
842 __in_fake_struct_assign
)
845 param_name
= get_param_name_sym(expr
->right
, ¶m_sym
);
846 if (param_name
&& param_sym
)
849 param_name
= handle_container_of_assign(expr
, ¶m_sym
);
850 if (param_name
&& param_sym
)
856 set_state_expr(my_id
, expr
->left
, alloc_var_sym_state(param_name
, param_sym
));
858 free_string(param_name
);
861 bool get_offset_param(const char *ret_str
, int *offset
, int *param
)
867 p
= strstr(ret_str
, "[(");
872 p
= strstr(p
, "<~$");
882 static void return_str_hook(struct expression
*expr
, const char *ret_str
)
884 struct expression
*call
, *arg
;
889 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
892 while (call
&& call
->type
== EXPR_ASSIGNMENT
)
893 call
= strip_expr(call
->right
);
894 if (!call
|| call
->type
!= EXPR_CALL
)
897 if (!get_offset_param(ret_str
, &offset
, ¶m
))
900 arg
= get_argument_from_call_expr(call
->args
, param
);
901 arg
= strip_expr(arg
);
905 /* fixme this could be better */
906 if (arg
->type
!= EXPR_SYMBOL
)
910 param
= get_param_num(arg
);
914 snprintf(buf
, sizeof(buf
), "(%d<~$%d)", offset
, param
);
915 set_state_expr(my_id
, expr
->left
, alloc_var_sym_state(buf
, sym
));
918 void register_param_key(int id
)
922 set_dynamic_states(my_id
);
923 add_hook(&match_assign
, ASSIGNMENT_HOOK_AFTER
);
924 add_return_string_hook(return_str_hook
);
925 add_modification_hook(my_id
, &undef
);