1 /* Finding reachable regions and values.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "gimple-iterator.h"
30 #include "diagnostic-core.h"
35 #include "stringpool.h"
38 #include "fold-const.h"
39 #include "tree-pretty-print.h"
41 #include "analyzer/analyzer.h"
42 #include "analyzer/analyzer-logging.h"
43 #include "ordered-hash-map.h"
45 #include "analyzer/call-string.h"
46 #include "analyzer/program-point.h"
47 #include "analyzer/store.h"
48 #include "analyzer/region-model.h"
49 #include "analyzer/region-model-reachability.h"
50 #include "diagnostic.h"
51 #include "tree-diagnostic.h"
57 reachable_regions::reachable_regions (region_model
*model
)
58 : m_model (model
), m_store (model
->get_store ()),
59 m_reachable_base_regs (), m_mutable_base_regs ()
63 /* Callback called for each cluster when initializing this object. */
66 reachable_regions::init_cluster_cb (const region
*base_reg
,
67 reachable_regions
*this_ptr
)
69 this_ptr
->init_cluster (base_reg
);
72 /* Called for each cluster when initializing this object. */
74 reachable_regions::init_cluster (const region
*base_reg
)
76 /* Mark any globals as mutable (and traverse what they point to). */
77 const region
*parent
= base_reg
->get_parent_region ();
79 if (parent
->get_kind () == RK_GLOBALS
)
82 /* Mark any clusters that already escaped in previous unknown calls
83 as mutable (and traverse what they currently point to). */
84 if (m_store
->escaped_p (base_reg
))
87 if (const symbolic_region
*sym_reg
= base_reg
->dyn_cast_symbolic_region ())
89 const svalue
*ptr
= sym_reg
->get_pointer ();
90 if (ptr
->implicitly_live_p (NULL
, m_model
))
92 switch (ptr
->get_kind ())
98 /* If BASE_REG is *INIT_VAL(REG) for some other REG, see if REG is
99 unbound and untouched. If so, then add BASE_REG as a root. */
100 const initial_svalue
*init_sval
101 = as_a
<const initial_svalue
*> (ptr
);
102 const region
*init_sval_reg
= init_sval
->get_region ();
103 const region
*other_base_reg
= init_sval_reg
->get_base_region ();
104 const binding_cluster
*other_cluster
105 = m_store
->get_cluster (other_base_reg
);
106 if (other_cluster
== NULL
107 || !other_cluster
->touched_p ())
108 add (base_reg
, true);
115 /* If this cluster is due to dereferencing an unknown/conjured
116 pointer, any values written through the pointer could still
118 add (base_reg
, true);
125 /* Lazily mark the cluster containing REG as being reachable, recursively
126 adding clusters reachable from REG's cluster. */
128 reachable_regions::add (const region
*reg
, bool is_mutable
)
132 const region
*base_reg
= const_cast <region
*> (reg
->get_base_region ());
133 gcc_assert (base_reg
);
135 /* Bail out if this cluster is already in the sets at the IS_MUTABLE
136 level of mutability. */
137 if (!is_mutable
&& m_reachable_base_regs
.contains (base_reg
))
139 m_reachable_base_regs
.add (base_reg
);
143 if (m_mutable_base_regs
.contains (base_reg
))
146 m_mutable_base_regs
.add (base_reg
);
149 /* Add values within the cluster. If any are pointers, add the pointee. */
150 if (binding_cluster
*bind_cluster
= m_store
->get_cluster (base_reg
))
151 bind_cluster
->for_each_value (handle_sval_cb
, this);
153 handle_sval (m_model
->get_store_value (reg
, NULL
));
157 reachable_regions::handle_sval_cb (const svalue
*sval
,
158 reachable_regions
*this_ptr
)
160 this_ptr
->handle_sval (sval
);
163 /* Add SVAL. If it is a pointer, add the pointed-to region. */
166 reachable_regions::handle_sval (const svalue
*sval
)
168 m_reachable_svals
.add (sval
);
169 m_mutable_svals
.add (sval
);
170 if (const region_svalue
*ptr
= sval
->dyn_cast_region_svalue ())
172 const region
*pointee
= ptr
->get_pointee ();
173 /* Use const-ness of pointer type to affect mutability. */
174 bool ptr_is_mutable
= true;
176 && TREE_CODE (ptr
->get_type ()) == POINTER_TYPE
177 && TYPE_READONLY (TREE_TYPE (ptr
->get_type ())))
179 ptr_is_mutable
= false;
183 m_mutable_svals
.add (sval
);
185 add (pointee
, ptr_is_mutable
);
187 /* Treat all svalues within a compound_svalue as reachable. */
188 if (const compound_svalue
*compound_sval
189 = sval
->dyn_cast_compound_svalue ())
191 for (compound_svalue::iterator_t iter
= compound_sval
->begin ();
192 iter
!= compound_sval
->end (); ++iter
)
194 const svalue
*iter_sval
= (*iter
).second
;
195 handle_sval (iter_sval
);
198 if (const svalue
*cast
= sval
->maybe_undo_cast ())
201 /* If SVAL is the result of a reversible operation, then the operands
203 switch (sval
->get_kind ())
209 const unaryop_svalue
*unaryop_sval
= (const unaryop_svalue
*)sval
;
210 switch (unaryop_sval
->get_op ())
215 handle_sval (unaryop_sval
->get_arg ());
222 const binop_svalue
*binop_sval
= (const binop_svalue
*)sval
;
223 switch (binop_sval
->get_op ())
227 case POINTER_PLUS_EXPR
:
228 handle_sval (binop_sval
->get_arg0 ());
229 handle_sval (binop_sval
->get_arg1 ());
236 /* Add SVAL. If it is a pointer, add the pointed-to region.
237 Use PARAM_TYPE for determining mutability. */
240 reachable_regions::handle_parm (const svalue
*sval
, tree param_type
)
242 bool is_mutable
= true;
244 && TREE_CODE (param_type
) == POINTER_TYPE
245 && TYPE_READONLY (TREE_TYPE (param_type
)))
248 m_mutable_svals
.add (sval
);
250 m_reachable_svals
.add (sval
);
251 if (const region
*base_reg
= sval
->maybe_get_deref_base_region ())
252 add (base_reg
, is_mutable
);
253 /* Treat all svalues within a compound_svalue as reachable. */
254 if (const compound_svalue
*compound_sval
255 = sval
->dyn_cast_compound_svalue ())
257 for (compound_svalue::iterator_t iter
= compound_sval
->begin ();
258 iter
!= compound_sval
->end (); ++iter
)
260 const svalue
*iter_sval
= (*iter
).second
;
261 handle_sval (iter_sval
);
264 if (const svalue
*cast
= sval
->maybe_undo_cast ())
268 /* Update the store to mark the clusters that were found to be mutable
270 Notify CTXT about escaping function_decls. */
273 reachable_regions::mark_escaped_clusters (region_model_context
*ctxt
)
275 auto_vec
<const function_region
*> escaped_fn_regs
276 (m_mutable_base_regs
.elements ());
277 for (hash_set
<const region
*>::iterator iter
= m_mutable_base_regs
.begin ();
278 iter
!= m_mutable_base_regs
.end (); ++iter
)
280 const region
*base_reg
= *iter
;
281 m_store
->mark_as_escaped (base_reg
);
283 /* If we have a function that's escaped, potentially add
284 it to the worklist. */
285 if (const function_region
*fn_reg
= base_reg
->dyn_cast_function_region ())
286 escaped_fn_regs
.quick_push (fn_reg
);
290 /* Sort to ensure deterministic results. */
291 escaped_fn_regs
.qsort (region::cmp_ptr_ptr
);
293 const function_region
*fn_reg
;
294 FOR_EACH_VEC_ELT (escaped_fn_regs
, i
, fn_reg
)
295 ctxt
->on_escaped_function (fn_reg
->get_fndecl ());
299 /* Dump SET to PP, sorting it to avoid churn when comparing dumps. */
301 template <typename T
>
303 dump_set (const hash_set
<const T
*> &set
, pretty_printer
*pp
)
305 auto_vec
<const T
*> elements (set
.elements ());
306 for (typename hash_set
<const T
*>::iterator iter
= set
.begin ();
307 iter
!= set
.end (); ++iter
)
308 elements
.quick_push (*iter
);
310 elements
.qsort (T::cmp_ptr_ptr
);
314 FOR_EACH_VEC_ELT (elements
, i
, element
)
317 element
->dump_to_pp (pp
, true);
322 /* Dump a multiline representation of this object to PP. */
325 reachable_regions::dump_to_pp (pretty_printer
*pp
) const
327 pp_string (pp
, "reachable clusters: ");
329 dump_set (m_reachable_base_regs
, pp
);
331 pp_string (pp
, "mutable clusters: ");
333 dump_set (m_mutable_base_regs
, pp
);
335 pp_string (pp
, "reachable svals: ");
337 dump_set (m_reachable_svals
, pp
);
339 pp_string (pp
, "mutable svals: ");
341 dump_set (m_mutable_svals
, pp
);
344 /* Dump a multiline representation of this object to stderr. */
347 reachable_regions::dump () const
350 pp_format_decoder (&pp
) = default_tree_printer
;
351 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
352 pp
.buffer
->stream
= stderr
;
359 #endif /* #if ENABLE_ANALYZER */