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 void undef(struct sm_state
*sm
, struct expression
*mod_expr
)
27 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
30 static char *handle_container_of_assign(struct expression
*expr
, struct expression
**ptr
)
32 struct expression
*right
, *orig
;
37 type
= get_type(expr
->left
);
38 if (!type
|| type
->type
!= SYM_PTR
)
41 right
= strip_expr(expr
->right
);
42 if (right
->type
!= EXPR_BINOP
|| right
->op
!= '-')
45 if (!get_value(right
->right
, &sval
) ||
46 sval
.value
< 0 || sval
.value
> MTAG_OFFSET_MASK
)
49 orig
= get_assigned_expr(right
->left
);
53 snprintf(buf
, sizeof(buf
), "(%lld<~$)", sval
.value
);
55 return alloc_string(buf
);
59 static void match_assign(struct expression
*expr
)
61 struct expression
*ptr
= NULL
;
62 char *container_str
, *name
;
69 if (is_fake_call(expr
->right
) || __in_fake_parameter_assign
||
70 __in_fake_struct_assign
)
73 container_str
= handle_container_of_assign(expr
, &ptr
);
74 if (!container_str
|| !ptr
)
76 name
= expr_to_var_sym(ptr
, &sym
);
80 snprintf(buf
, sizeof(buf
), "%s %s", name
, container_str
);
81 set_state(my_id
, buf
, sym
, alloc_state_expr(expr
->left
));
85 free_string(container_str
);
88 static void return_str_hook(struct expression
*expr
, const char *ret_str
)
90 struct expression
*call
, *arg
;
96 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
99 while (call
&& call
->type
== EXPR_ASSIGNMENT
)
100 call
= strip_expr(call
->right
);
101 if (!call
|| call
->type
!= EXPR_CALL
)
104 if (!get_offset_param(ret_str
, &offset
, ¶m
))
107 arg
= get_argument_from_call_expr(call
->args
, param
);
108 arg
= strip_expr(arg
);
112 name
= expr_to_var_sym(arg
, &sym
);
116 snprintf(buf
, sizeof(buf
), "%s (%d<~$)", name
, offset
);
117 set_state(my_id
, buf
, sym
, alloc_state_expr(expr
->left
));
120 struct expression
*get_stored_container(struct expression
*expr
, int offset
)
122 struct smatch_state
*state
;
127 name
= expr_to_var_sym(expr
, &sym
);
131 snprintf(buf
, sizeof(buf
), "%s (%d<~$)", name
, offset
);
133 state
= get_state(my_id
, buf
, sym
);
139 void register_points_to_container(int id
)
143 set_dynamic_states(my_id
);
144 add_hook(&match_assign
, ASSIGNMENT_HOOK_AFTER
);
145 add_return_string_hook(return_str_hook
);
146 add_modification_hook(my_id
, &undef
);