Line data Source code
1 : /* Finding reachable regions and values.
2 : Copyright (C) 2020-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "analyzer/common.h"
22 :
23 : #include "ordered-hash-map.h"
24 : #include "diagnostic.h"
25 : #include "tree-diagnostic.h"
26 :
27 : #include "analyzer/analyzer-logging.h"
28 : #include "analyzer/call-string.h"
29 : #include "analyzer/program-point.h"
30 : #include "analyzer/store.h"
31 : #include "analyzer/region-model.h"
32 : #include "analyzer/region-model-reachability.h"
33 : #include "analyzer/liveness-context.h"
34 :
35 : #if ENABLE_ANALYZER
36 :
37 : namespace ana {
38 :
39 : static bool
40 99987068 : recurse_into_children_p (const symbolic::value_node &val)
41 : {
42 : /* Don't recurse into children for cropped bit vectors:
43 : the values aren't complete, so aren't live. */
44 99987068 : if (auto *bv = val.dyn_cast_bit_vector ())
45 23545903 : if (bv->get_kind () == symbolic::bit_vector::kind::cropped)
46 52857 : return false;
47 : return true;
48 : }
49 :
50 908244 : reachable_regions::reachable_regions (region_model *model)
51 908244 : : m_model (model), m_store (model->get_store ()),
52 908244 : m_reachable_base_regs (), m_mutable_base_regs ()
53 : {
54 908244 : }
55 :
56 : /* Callback called for each cluster when initializing this object. */
57 :
58 : void
59 5184646 : reachable_regions::init_cluster_cb (const cluster_region &cluster_reg,
60 : reachable_regions *this_ptr)
61 : {
62 5184646 : this_ptr->init_cluster (cluster_reg);
63 5184646 : }
64 :
65 : /* Called for each cluster when initializing this object. */
66 : void
67 5184646 : reachable_regions::init_cluster (const cluster_region &cluster_reg)
68 : {
69 : /* Mark any globals as mutable (and traverse what they point to). */
70 5184646 : if (cluster_reg.get_memory_space () == memory_space::globals)
71 404116 : add (&cluster_reg, true);
72 :
73 : /* Mark any clusters that already escaped in previous unknown calls
74 : as mutable (and traverse what they currently point to). */
75 5184646 : if (m_store->escaped_p (cluster_reg))
76 1189089 : add (&cluster_reg, true);
77 :
78 5184646 : if (const symbolic_region *sym_reg = cluster_reg.dyn_cast_symbolic_region ())
79 : {
80 1098054 : const svalue *ptr = sym_reg->get_pointer ();
81 1098054 : symbolic::liveness_context liveness_ctxt (value_set (), *m_model);
82 1098054 : if (ptr->implicitly_live_p (liveness_ctxt))
83 903384 : add (&cluster_reg, true);
84 1098054 : switch (ptr->get_kind ())
85 : {
86 : default:
87 : break;
88 894613 : case SK_INITIAL:
89 894613 : {
90 : /* If CLUSTER_REG is *INIT_VAL(REG) for some other REG, see if REG is
91 : unbound and untouched. If so, then add CLUSTER_REG as a root. */
92 894613 : const initial_svalue *init_sval
93 894613 : = as_a <const initial_svalue *> (ptr);
94 894613 : const region *init_sval_reg = init_sval->get_region ();
95 894613 : const region *other_cluster_reg = init_sval_reg->get_cluster_region ();
96 894613 : const binding_cluster *other_cluster
97 894613 : = m_store->get_cluster (other_cluster_reg);
98 894613 : if (other_cluster == nullptr
99 894613 : || !other_cluster->touched_p ())
100 879520 : add (&cluster_reg, true);
101 : }
102 : break;
103 :
104 0 : case SK_UNKNOWN:
105 0 : {
106 : /* If this cluster is due to dereferencing an unknown pointer,
107 : any values written through the pointer could still
108 : be live. */
109 0 : add (&cluster_reg, true);
110 : }
111 0 : break;
112 : }
113 1098054 : }
114 5184646 : }
115 :
116 : /* Lazily mark the cluster containing REG as being reachable, recursively
117 : adding clusters reachable from REG's cluster. */
118 : void
119 8955056 : reachable_regions::add (const region *reg, bool is_mutable)
120 : {
121 8955056 : gcc_assert (reg);
122 :
123 8955056 : const cluster_region *base_reg
124 8955056 : = const_cast <cluster_region *> (reg->get_base_region ());
125 8955056 : gcc_assert (base_reg);
126 :
127 : /* Bail out if this cluster is already in the sets at the IS_MUTABLE
128 : level of mutability. */
129 8955056 : if (!is_mutable && m_reachable_base_regs.contains (base_reg))
130 119836 : return;
131 8835220 : m_reachable_base_regs.add (base_reg);
132 :
133 8835220 : if (is_mutable)
134 : {
135 5697935 : if (m_mutable_base_regs.contains (base_reg))
136 : return;
137 : else
138 2120881 : m_mutable_base_regs.add (base_reg);
139 : }
140 :
141 : /* Add values within the cluster. If any are pointers, add the pointee. */
142 5258166 : if (binding_cluster *bind_cluster = m_store->get_cluster (base_reg))
143 : {
144 4998950 : bind_cluster->for_each_value
145 4998950 : ([] (const symbolic::value_node &val,
146 : void *user_data) -> iter_result
147 : {
148 : ((reachable_regions *)user_data)->handle_value (val);
149 : return iter_result::keep_going;
150 : },
151 : this);
152 : }
153 : }
154 :
155 : void
156 0 : reachable_regions::handle_sval_cb (const svalue *sval,
157 : reachable_regions *this_ptr)
158 : {
159 0 : this_ptr->handle_value (*sval);
160 0 : }
161 :
162 : /* Add VAL. If it is a pointer, add the pointed-to region. */
163 :
164 : void
165 23258229 : reachable_regions::handle_value (const symbolic::value_node &val)
166 : {
167 123245297 : auto cb = [] (const symbolic::value_node &val, void *user_data) -> bool
168 : {
169 99987068 : reachable_regions *rr = (reachable_regions *)user_data;
170 99987068 : if (!rr->m_reachable_svals.contains (&val))
171 : {
172 9383144 : rr->m_reachable_svals.insert (&val);
173 9383144 : rr->handle_value (val);
174 : }
175 :
176 99987068 : return recurse_into_children_p (val);
177 : };
178 :
179 23258229 : symbolic::value_node_visitor v {cb, this};
180 23258229 : val.accept_node_visitor (v);
181 :
182 23258229 : m_mutable_svals.insert (&val);
183 :
184 23258229 : if (auto sval = val.dyn_cast_svalue ())
185 : {
186 13285403 : if (auto *bit_vec_sval = sval->dyn_cast_svalue_from_bit_vector ())
187 1316223 : handle_value (bit_vec_sval->get_bit_vector ());
188 :
189 13285403 : if (auto *ptr = sval->maybe_get_symbolic_pointer ())
190 : {
191 : /* Use const-ness of pointer type to affect mutability. */
192 2337593 : bool ptr_is_mutable = true;
193 2337593 : if (sval->get_type ()
194 2337593 : && TREE_CODE (sval->get_type ()) == POINTER_TYPE
195 4671768 : && TYPE_READONLY (TREE_TYPE (sval->get_type ())))
196 : {
197 : ptr_is_mutable = false;
198 : }
199 : else
200 : {
201 2314993 : m_mutable_svals.insert (sval);
202 : }
203 2337593 : add (&ptr->get_base_region (), ptr_is_mutable);
204 : }
205 13285403 : if (const svalue *cast = sval->maybe_undo_cast ())
206 1971636 : handle_value (*cast);
207 :
208 : /* If SVAL is the result of a reversible operation, then the operands
209 : are reachable. */
210 13285403 : switch (sval->get_kind ())
211 : {
212 : default:
213 : break;
214 2038285 : case SK_UNARYOP:
215 2038285 : {
216 2038285 : const unaryop_svalue *unaryop_sval = (const unaryop_svalue *)sval;
217 2038285 : switch (unaryop_sval->get_op ())
218 : {
219 : default:
220 : break;
221 56868 : case NEGATE_EXPR:
222 56868 : handle_value (*unaryop_sval->get_arg ());
223 56868 : break;
224 : }
225 : }
226 : break;
227 3222570 : case SK_BINOP:
228 3222570 : {
229 3222570 : const binop_svalue *binop_sval = (const binop_svalue *)sval;
230 3222570 : switch (binop_sval->get_op ())
231 : {
232 : default:
233 : break;
234 251745 : case POINTER_PLUS_EXPR:
235 251745 : handle_value (*binop_sval->get_arg0 ());
236 251745 : handle_value (*binop_sval->get_arg1 ());
237 251745 : break;
238 : }
239 : }
240 : }
241 : }
242 :
243 23258229 : if (auto *bv = val.dyn_cast_bit_vector ())
244 9972826 : if (auto *from_svalue = bv->dyn_cast_bit_vector_from_svalue ())
245 4946311 : handle_value (from_svalue->get_svalue ());
246 23258229 : }
247 :
248 : /* Add SVAL. If it is a pointer, add the pointed-to region.
249 : Use PARAM_TYPE for determining mutability. */
250 :
251 : void
252 19487 : reachable_regions::handle_parm (const svalue *sval, tree param_type)
253 : {
254 19487 : bool is_mutable = true;
255 19487 : if (param_type
256 17983 : && TREE_CODE (param_type) == POINTER_TYPE
257 31956 : && TYPE_READONLY (TREE_TYPE (param_type)))
258 3161 : is_mutable = false;
259 19487 : if (is_mutable)
260 16326 : m_mutable_svals.insert (sval);
261 : else
262 3161 : m_reachable_svals.insert (sval);
263 19487 : if (const region *base_reg = sval->maybe_get_deref_base_region ())
264 8495 : add (base_reg, is_mutable);
265 19487 : if (auto *sval_from_bv = sval->dyn_cast_svalue_from_bit_vector ())
266 : {
267 2094 : auto &bv = sval_from_bv->get_bit_vector ();
268 2094 : if (auto *concat_bv = bv.dyn_cast_concat_bit_vector ())
269 233 : for (auto child_bv : *concat_bv)
270 202 : if (auto *bv_from_sval = child_bv->dyn_cast_bit_vector_from_svalue ())
271 186 : handle_value (bv_from_sval->get_svalue ());
272 : }
273 19487 : if (const svalue *cast = sval->maybe_undo_cast ())
274 309 : handle_value (*cast);
275 19487 : }
276 :
277 : /* Update the store to mark the clusters that were found to be mutable
278 : as having escaped.
279 : Notify CTXT about escaping function_decls. */
280 :
281 : void
282 17576 : reachable_regions::mark_escaped_clusters (region_model_context *ctxt)
283 : {
284 17576 : auto_vec<const function_region *> escaped_fn_regs
285 17576 : (m_mutable_base_regs.elements ());
286 41122 : for (hash_set<const region *>::iterator iter = m_mutable_base_regs.begin ();
287 41122 : iter != m_mutable_base_regs.end (); ++iter)
288 : {
289 23546 : const region *base_reg = *iter;
290 23546 : m_store->mark_as_escaped (base_reg,
291 23546 : m_model->get_baseline_state ());
292 :
293 : /* If we have a function that's escaped, potentially add
294 : it to the worklist. */
295 23546 : if (const function_region *fn_reg = base_reg->dyn_cast_function_region ())
296 615 : escaped_fn_regs.quick_push (fn_reg);
297 : }
298 17576 : if (ctxt)
299 : {
300 : /* Sort to ensure deterministic results. */
301 12157 : escaped_fn_regs.qsort (region::cmp_ptr_ptr);
302 12157 : unsigned i;
303 12157 : const function_region *fn_reg;
304 27798 : FOR_EACH_VEC_ELT (escaped_fn_regs, i, fn_reg)
305 309 : ctxt->on_escaped_function (fn_reg->get_fndecl ());
306 : }
307 17576 : }
308 :
309 : /* Dump SET to PP, sorting it to avoid churn when comparing dumps. */
310 :
311 : template <typename T>
312 : static void
313 0 : dump_set (const hash_set<const T *> &set, pretty_printer *pp)
314 : {
315 0 : auto_vec<const T *> elements (set.elements ());
316 0 : for (typename hash_set<const T *>::iterator iter = set.begin ();
317 0 : iter != set.end (); ++iter)
318 0 : elements.quick_push (*iter);
319 :
320 0 : elements.qsort (T::cmp_ptr_ptr);
321 :
322 : unsigned i;
323 : const T *element;
324 0 : FOR_EACH_VEC_ELT (elements, i, element)
325 : {
326 0 : pp_string (pp, " ");
327 0 : element->dump_to_pp (pp, true);
328 0 : pp_newline (pp);
329 : }
330 0 : }
331 :
332 : /* Dump a multiline representation of this object to PP. */
333 :
334 : void
335 0 : reachable_regions::dump_to_pp (pretty_printer *pp) const
336 : {
337 0 : pp_string (pp, "reachable clusters: ");
338 0 : pp_newline (pp);
339 0 : dump_set (m_reachable_base_regs, pp);
340 :
341 0 : pp_string (pp, "mutable clusters: ");
342 0 : pp_newline (pp);
343 0 : dump_set (m_mutable_base_regs, pp);
344 :
345 0 : pp_string (pp, "reachable svals: ");
346 0 : pp_newline (pp);
347 0 : m_reachable_svals.dump_to_pp (pp);
348 :
349 0 : pp_string (pp, "mutable svals: ");
350 0 : pp_newline (pp);
351 0 : m_mutable_svals.dump_to_pp (pp);
352 0 : }
353 :
354 : /* Dump a multiline representation of this object to stderr. */
355 :
356 : DEBUG_FUNCTION void
357 0 : reachable_regions::dump () const
358 : {
359 0 : tree_dump_pretty_printer pp (stderr);
360 0 : dump_to_pp (&pp);
361 0 : }
362 :
363 : } // namespace ana
364 :
365 : #endif /* #if ENABLE_ANALYZER */
|