2 * Copyright (C) 2022 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_slist.h"
21 #include "smatch_extra.h"
25 static char *handle_container_of_assign(struct expression
*expr
, struct expression
**ptr
)
27 struct expression
*right
, *orig
;
32 type
= get_type(expr
->left
);
33 if (!type
|| type
->type
!= SYM_PTR
)
36 right
= strip_expr(expr
->right
);
37 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
40 if (!get_value(right
->right
, &sval
) ||
41 sval
.value
< 0 || sval
.value
> MTAG_OFFSET_MASK
)
44 orig
= get_assigned_expr(right
->left
);
48 snprintf(buf
, sizeof(buf
), "(%lld<~$)", sval
.value
);
50 return alloc_string(buf
);
54 static void match_assign(struct expression
*expr
)
56 struct expression
*ptr
= NULL
;
57 char *container_str
, *name
;
64 if (is_fake_call(expr
->right
) || __in_fake_parameter_assign
||
65 __in_fake_struct_assign
)
68 container_str
= handle_container_of_assign(expr
, &ptr
);
69 if (!container_str
|| !ptr
)
71 name
= expr_to_var_sym(ptr
, &sym
);
75 snprintf(buf
, sizeof(buf
), "%s %s", name
, container_str
);
76 set_state(my_id
, buf
, sym
, alloc_state_expr(expr
->left
));
80 free_string(container_str
);
83 static void return_str_hook(struct expression
*expr
, const char *ret_str
)
85 struct expression
*call
, *arg
;
91 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
94 while (call
&& call
->type
== EXPR_ASSIGNMENT
)
95 call
= strip_expr(call
->right
);
96 if (!call
|| call
->type
!= EXPR_CALL
)
99 if (!get_offset_param(ret_str
, &offset
, ¶m
))
102 arg
= get_argument_from_call_expr(call
->args
, param
);
103 arg
= strip_expr(arg
);
107 name
= expr_to_var_sym(arg
, &sym
);
111 snprintf(buf
, sizeof(buf
), "%s (%d<~$)", name
, offset
);
112 set_state(my_id
, buf
, sym
, alloc_state_expr(expr
->left
));
115 struct expression
*get_stored_container(struct expression
*expr
, int offset
)
117 struct smatch_state
*state
;
122 name
= expr_to_var_sym(expr
, &sym
);
126 snprintf(buf
, sizeof(buf
), "%s (%d<~$)", name
, offset
);
128 state
= get_state(my_id
, buf
, sym
);
134 void register_points_to_container(int id
)
138 set_dynamic_states(my_id
);
139 add_hook(&match_assign
, ASSIGNMENT_HOOK_AFTER
);
140 add_return_string_hook(return_str_hook
);
141 add_modification_hook(my_id
, &set_undefined
);