Line data Source code
1 : /* Classes for representing the state of interest at a given path of analysis.
2 : Copyright (C) 2019-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 "sbitmap.h"
24 : #include "ordered-hash-map.h"
25 : #include "selftest.h"
26 : #include "cfg.h"
27 : #include "gimple-iterator.h"
28 : #include "cgraph.h"
29 : #include "digraph.h"
30 : #include "diagnostics/event-id.h"
31 : #include "diagnostics/state-graphs.h"
32 : #include "graphviz.h"
33 :
34 : #include "text-art/tree-widget.h"
35 : #include "text-art/dump.h"
36 :
37 : #include "analyzer/analyzer-logging.h"
38 : #include "analyzer/sm.h"
39 : #include "analyzer/call-string.h"
40 : #include "analyzer/program-point.h"
41 : #include "analyzer/store.h"
42 : #include "analyzer/region-model.h"
43 : #include "analyzer/program-state.h"
44 : #include "analyzer/constraint-manager.h"
45 : #include "analyzer/pending-diagnostic.h"
46 : #include "analyzer/diagnostic-manager.h"
47 : #include "analyzer/supergraph.h"
48 : #include "analyzer/program-state.h"
49 : #include "analyzer/exploded-graph.h"
50 : #include "analyzer/state-purge.h"
51 : #include "analyzer/call-summary.h"
52 : #include "analyzer/analyzer-selftests.h"
53 : #include "analyzer/ana-state-to-diagnostic-state.h"
54 : #include "analyzer/liveness-context.h"
55 :
56 : #if ENABLE_ANALYZER
57 :
58 : namespace ana {
59 :
60 : /* class extrinsic_state. */
61 :
62 : std::unique_ptr<text_art::tree_widget>
63 0 : extrinsic_state::make_dump_widget (symbolic::node_dump_state &nds) const
64 : {
65 0 : using text_art::tree_widget;
66 0 : std::unique_ptr<tree_widget> w
67 0 : (tree_widget::from_fmt (nds, nullptr, "Extrinsic State"));
68 0 : unsigned i = 0;
69 0 : for (auto &checker : m_checkers)
70 : {
71 0 : std::unique_ptr<tree_widget> cw
72 : (tree_widget::from_fmt (nds, nullptr, "m_checkers[%i]: %qs\n",
73 0 : ++i, checker->get_name ()));
74 0 : w->add_child (std::move (cw));
75 0 : }
76 0 : return w;
77 : }
78 :
79 : /* Return a new json::object of the form
80 : {"checkers" : array of objects, one for each state_machine}. */
81 :
82 : std::unique_ptr<json::object>
83 1 : extrinsic_state::to_json () const
84 : {
85 1 : auto ext_state_obj = std::make_unique<json::object> ();
86 :
87 1 : {
88 1 : auto checkers_arr = std::make_unique<json::array> ();
89 8 : for (auto &sm : m_checkers)
90 7 : checkers_arr->append (sm->to_json ());
91 1 : ext_state_obj->set ("checkers", std::move (checkers_arr));
92 1 : }
93 :
94 1 : return ext_state_obj;
95 : }
96 :
97 : /* Get the region_model_manager for this extrinsic_state. */
98 :
99 : region_model_manager *
100 8150726 : extrinsic_state::get_model_manager () const
101 : {
102 8150726 : if (m_engine)
103 8150726 : return m_engine->get_model_manager ();
104 : else
105 : return nullptr; /* for selftests. */
106 : }
107 :
108 : symbolic::node_manager &
109 46324 : extrinsic_state::get_node_manager () const
110 : {
111 46324 : gcc_assert (m_engine);
112 46324 : return m_engine->get_node_manager ();
113 : }
114 :
115 : /* Try to find a state machine named NAME.
116 : If found, return true and write its index to *OUT.
117 : Otherwise return false. */
118 :
119 : bool
120 630152 : extrinsic_state::get_sm_idx_by_name (const char *name, unsigned *out) const
121 : {
122 2518031 : for (size_t i = 0; i < m_checkers.size (); ++i)
123 2517560 : if (0 == strcmp (name, m_checkers[i]->get_name ()))
124 : {
125 : /* Found NAME. */
126 629681 : *out = i;
127 629681 : return true;
128 : }
129 :
130 : /* NAME not found. */
131 : return false;
132 : }
133 :
134 : /* struct sm_state_map::entry_t. */
135 :
136 : int
137 72371 : sm_state_map::entry_t::cmp (const entry_t &entry_a, const entry_t &entry_b)
138 : {
139 72371 : gcc_assert (entry_a.m_state);
140 72371 : gcc_assert (entry_b.m_state);
141 72371 : if (int cmp_state = ((int)entry_a.m_state->get_id ()
142 72371 : - (int)entry_b.m_state->get_id ()))
143 : return cmp_state;
144 66012 : if (entry_a.m_origin && entry_b.m_origin)
145 0 : return svalue::cmp_ptr (entry_a.m_origin, entry_b.m_origin);
146 66012 : if (entry_a.m_origin)
147 : return 1;
148 66012 : if (entry_b.m_origin)
149 0 : return -1;
150 : return 0;
151 : }
152 :
153 : /* class sm_state_map. */
154 :
155 : /* sm_state_map's ctor. */
156 :
157 1055184 : sm_state_map::sm_state_map (const state_machine &sm)
158 1055184 : : m_sm (sm), m_map (), m_global_state (sm.get_start_state ())
159 : {
160 1055184 : }
161 :
162 : /* Clone the sm_state_map. */
163 :
164 : sm_state_map *
165 12654904 : sm_state_map::clone () const
166 : {
167 12654904 : return new sm_state_map (*this);
168 : }
169 :
170 : /* Print this sm_state_map to PP.
171 : If MODEL is non-NULL, print representative tree values where
172 : available. */
173 :
174 : void
175 0 : sm_state_map::print (const region_model *model,
176 : bool simple, bool multiline,
177 : pretty_printer *pp) const
178 : {
179 0 : bool first = true;
180 0 : if (!multiline)
181 0 : pp_string (pp, "{");
182 0 : if (m_global_state != m_sm.get_start_state ())
183 : {
184 0 : if (multiline)
185 0 : pp_string (pp, " ");
186 0 : pp_string (pp, "global: ");
187 0 : m_global_state->dump_to_pp (pp);
188 0 : if (multiline)
189 0 : pp_newline (pp);
190 : first = false;
191 : }
192 0 : auto_vec <const svalue *> keys (m_map.elements ());
193 0 : for (map_t::iterator iter = m_map.begin ();
194 0 : iter != m_map.end ();
195 0 : ++iter)
196 0 : keys.quick_push ((*iter).first);
197 0 : keys.qsort (svalue::cmp_ptr_ptr);
198 0 : unsigned i;
199 0 : const svalue *sval;
200 0 : FOR_EACH_VEC_ELT (keys, i, sval)
201 : {
202 0 : if (multiline)
203 0 : pp_string (pp, " ");
204 0 : else if (!first)
205 0 : pp_string (pp, ", ");
206 0 : first = false;
207 0 : if (!flag_dump_noaddr)
208 : {
209 0 : pp_pointer (pp, sval);
210 0 : pp_string (pp, ": ");
211 : }
212 0 : sval->dump_to_pp (pp, simple);
213 :
214 0 : entry_t e = *const_cast <map_t &> (m_map).get (sval);
215 0 : pp_string (pp, ": ");
216 0 : e.m_state->dump_to_pp (pp);
217 0 : if (model)
218 0 : if (tree rep = model->get_representative_tree (sval))
219 : {
220 0 : pp_string (pp, " (");
221 0 : dump_quoted_tree (pp, rep);
222 0 : pp_character (pp, ')');
223 : }
224 0 : if (e.m_origin)
225 : {
226 0 : pp_string (pp, " (origin: ");
227 0 : if (!flag_dump_noaddr)
228 : {
229 0 : pp_pointer (pp, e.m_origin);
230 0 : pp_string (pp, ": ");
231 : }
232 0 : e.m_origin->dump_to_pp (pp, simple);
233 0 : if (model)
234 0 : if (tree rep = model->get_representative_tree (e.m_origin))
235 : {
236 0 : pp_string (pp, " (");
237 0 : dump_quoted_tree (pp, rep);
238 0 : pp_character (pp, ')');
239 : }
240 0 : pp_string (pp, ")");
241 : }
242 0 : if (multiline)
243 0 : pp_newline (pp);
244 : }
245 0 : if (!multiline)
246 0 : pp_string (pp, "}");
247 : // TODO: m_map_ubc_to_state
248 0 : }
249 :
250 : /* Dump this object to stderr. */
251 :
252 : DEBUG_FUNCTION void
253 0 : sm_state_map::dump (bool simple) const
254 : {
255 0 : tree_dump_pretty_printer pp (stderr);
256 0 : print (nullptr, simple, true, &pp);
257 0 : pp_newline (&pp);
258 0 : }
259 :
260 : /* Return a new json::object of the form
261 : {"global" : (optional) value for global state,
262 : SVAL_DESC : value for state}. */
263 :
264 : std::unique_ptr<json::object>
265 9 : sm_state_map::to_json () const
266 : {
267 9 : auto map_obj = std::make_unique<json::object> ();
268 :
269 9 : if (m_global_state != m_sm.get_start_state ())
270 0 : map_obj->set ("global", m_global_state->to_json ());
271 9 : for (map_t::iterator iter = m_map.begin ();
272 18 : iter != m_map.end ();
273 9 : ++iter)
274 : {
275 9 : const svalue *sval = (*iter).first;
276 9 : entry_t e = (*iter).second;
277 :
278 9 : label_text sval_desc = sval->get_desc ();
279 9 : map_obj->set (sval_desc.get (), e.m_state->to_json ());
280 :
281 : /* This doesn't yet JSONify e.m_origin. */
282 9 : }
283 : // TODO: m_map_ubc_to_state
284 9 : return map_obj;
285 : }
286 :
287 : /* Make a text_art::tree_widget describing this sm_state_map,
288 : using MODEL if non-null to describe svalues. */
289 :
290 : std::unique_ptr<text_art::tree_widget>
291 70 : sm_state_map::make_dump_widget (symbolic::node_dump_state &nds,
292 : const region_model *) const
293 : {
294 70 : using text_art::styled_string;
295 70 : using text_art::tree_widget;
296 70 : std::unique_ptr<tree_widget> state_widget
297 : (tree_widget::from_fmt (nds, nullptr,
298 70 : "%qs state machine", m_sm.get_name ()));
299 :
300 70 : if (m_global_state != m_sm.get_start_state ())
301 : {
302 0 : pretty_printer the_pp;
303 0 : pretty_printer * const pp = &the_pp;
304 0 : pp_format_decoder (pp) = default_tree_printer;
305 0 : pp_string (pp, "Global State: ");
306 0 : m_global_state->dump_to_pp (pp);
307 0 : state_widget->add_child (tree_widget::make (nds, pp));
308 0 : }
309 :
310 70 : auto_vec <const svalue *> keys (m_map.elements ());
311 70 : for (map_t::iterator iter = m_map.begin ();
312 140 : iter != m_map.end ();
313 70 : ++iter)
314 70 : keys.quick_push ((*iter).first);
315 70 : keys.qsort (svalue::cmp_ptr_ptr);
316 70 : unsigned i;
317 70 : const svalue *sval;
318 210 : FOR_EACH_VEC_ELT (keys, i, sval)
319 : {
320 70 : entry_t e = *const_cast <map_t &> (m_map).get (sval);
321 :
322 70 : pretty_printer pp;
323 70 : pp_format_decoder (&pp) = default_tree_printer;
324 70 : e.m_state->dump_to_pp (&pp);
325 70 : std::unique_ptr<tree_widget> per_key_widget
326 70 : = tree_widget::make (nds, &pp);
327 140 : per_key_widget->add_child (sval->make_dump_widget (nds));
328 140 : state_widget->add_child (std::move (per_key_widget));
329 70 : }
330 70 : for (auto iter : m_map_ubc_to_state)
331 : {
332 0 : auto *ubc = iter.first;
333 0 : auto state = iter.second;
334 0 : pretty_printer pp;
335 0 : pp_format_decoder (&pp) = default_tree_printer;
336 0 : state->dump_to_pp (&pp);
337 0 : std::unique_ptr<tree_widget> per_key_widget
338 0 : = tree_widget::make (nds, &pp);
339 0 : per_key_widget->add_child (ubc->make_dump_widget (nds));
340 0 : state_widget->add_child (std::move (per_key_widget));
341 0 : }
342 :
343 70 : return state_widget;
344 70 : }
345 :
346 : DEBUG_FUNCTION void
347 0 : sm_state_map::dump () const
348 : {
349 0 : symbolic::dump_node_tree_to_stderr<sm_state_map> (*this);
350 0 : }
351 :
352 : /* Return true if no states have been set within this map
353 : (all expressions are for the start state). */
354 :
355 : bool
356 5112 : sm_state_map::is_empty_p () const
357 : {
358 5112 : return (m_map.elements () == 0
359 5029 : && m_global_state == m_sm.get_start_state ()
360 10141 : && m_map_ubc_to_state.empty ());
361 : }
362 :
363 : /* Generate a hash value for this sm_state_map. */
364 :
365 : hashval_t
366 3966998 : sm_state_map::hash () const
367 : {
368 3966998 : hashval_t result = 0;
369 :
370 : /* Accumulate the result by xoring a hash for each slot, so that the
371 : result doesn't depend on the ordering of the slots in the map. */
372 :
373 3966998 : for (map_t::iterator iter = m_map.begin ();
374 4435514 : iter != m_map.end ();
375 468516 : ++iter)
376 : {
377 468516 : inchash::hash hstate;
378 468516 : hstate.add_ptr ((*iter).first);
379 468516 : entry_t e = (*iter).second;
380 468516 : hstate.add_int (e.m_state->get_id ());
381 468516 : hstate.add_ptr (e.m_origin);
382 468516 : result ^= hstate.end ();
383 : }
384 3966998 : result ^= m_global_state->get_id ();
385 :
386 3966998 : return result;
387 : }
388 :
389 : /* Equality operator for sm_state_map. */
390 :
391 : bool
392 592899 : sm_state_map::operator== (const sm_state_map &other) const
393 : {
394 592899 : if (m_global_state != other.m_global_state)
395 : return false;
396 :
397 591883 : if (m_map.elements () != other.m_map.elements ())
398 : return false;
399 :
400 535567 : for (map_t::iterator iter = m_map.begin ();
401 612486 : iter != m_map.end ();
402 76919 : ++iter)
403 : {
404 88660 : const svalue *sval = (*iter).first;
405 88660 : entry_t e = (*iter).second;
406 88660 : entry_t *other_slot = const_cast <map_t &> (other.m_map).get (sval);
407 88660 : if (other_slot == nullptr)
408 11741 : return false;
409 165579 : if (e != *other_slot)
410 : return false;
411 : }
412 :
413 523826 : if (m_map_ubc_to_state != other.m_map_ubc_to_state)
414 : return false;
415 :
416 523648 : gcc_checking_assert (hash () == other.hash ());
417 :
418 : return true;
419 : }
420 :
421 : state_machine::state_t
422 187009 : sm_state_map::
423 : get_any_inherited_state_of_bit_vector (const symbolic::bit_vector &bv,
424 : const extrinsic_state &ext_state,
425 : region_model_manager &rmm) const
426 : {
427 188151 : switch (bv.get_kind ())
428 : {
429 0 : default:
430 0 : gcc_unreachable ();
431 :
432 : case symbolic::bit_vector::kind::concat:
433 : break;
434 : case symbolic::bit_vector::kind::conjured:
435 : break;
436 1142 : case symbolic::bit_vector::kind::cropped:
437 1142 : {
438 1142 : auto &cropped_bv
439 : = static_cast<const symbolic::cropped_bit_vector &> (bv);
440 1142 : auto &inner_bv = cropped_bv.get_uncropped_bit_vector ();
441 1142 : return get_any_inherited_state_of_bit_vector (inner_bv, ext_state, rmm);
442 : }
443 : break;
444 : case symbolic::bit_vector::kind::empty:
445 : break;
446 : case symbolic::bit_vector::kind::named_test:
447 : break;
448 : case symbolic::bit_vector::kind::poisoned:
449 : break;
450 : case symbolic::bit_vector::kind::repeated:
451 : break;
452 1052 : case symbolic::bit_vector::kind::svalue:
453 1052 : {
454 1052 : auto &from_sval
455 : = static_cast<const symbolic::bit_vector_from_svalue &> (bv);
456 1052 : auto &sval = from_sval.get_svalue ();
457 1052 : return get_state (&sval, ext_state);
458 : }
459 124015 : break;
460 124015 : case symbolic::bit_vector::kind::from_ubc:
461 124015 : {
462 124015 : auto &from_ubc
463 : = static_cast<const symbolic::bit_vector_from_ubc &> (bv);
464 124015 : auto &ubc = from_ubc.get_bit_contents ();
465 124015 : auto iter = m_map_ubc_to_state.find (&ubc);
466 124015 : if (iter != m_map_ubc_to_state.end ())
467 731 : return iter->second;
468 123284 : if (auto *conjured_bc = ubc.dyn_cast_conjured_bit_contents ())
469 : {
470 11115 : auto *id_reg = conjured_bc->get_id_region ();
471 11115 : const svalue *id_reg_ptr = rmm.get_ptr_svalue (NULL_TREE, id_reg);
472 11115 : state_machine::state_t parent_state
473 11115 : = get_state (id_reg_ptr, ext_state);
474 11115 : if (parent_state)
475 : return parent_state;
476 : }
477 112169 : if (auto *baseline_bc = ubc.dyn_cast_baseline_bit_contents ())
478 : {
479 112169 : auto &bkey = baseline_bc->get_baseline_key ();
480 112169 : const region *reg = bkey.m_cluster_reg;
481 112169 : const svalue *reg_ptr = rmm.get_ptr_svalue (NULL_TREE, reg);
482 112169 : state_machine::state_t parent_state
483 112169 : = get_state (reg_ptr, ext_state);
484 112169 : if (parent_state)
485 : return parent_state;
486 : }
487 : }
488 : break;
489 : case symbolic::bit_vector::kind::tristate:
490 : break;
491 : }
492 :
493 : return nullptr;
494 : }
495 :
496 : /* Get the state of SVAL within this object.
497 : States default to the start state. */
498 :
499 : state_machine::state_t
500 4832843 : sm_state_map::get_state (const svalue *sval,
501 : const extrinsic_state &ext_state) const
502 : {
503 4832843 : gcc_assert (sval);
504 :
505 4832843 : sval = canonicalize_svalue (sval, ext_state);
506 :
507 9665686 : if (entry_t *slot
508 4832843 : = const_cast <map_t &> (m_map).get (sval))
509 358549 : return slot->m_state;
510 :
511 : /* SVAL has no explicit sm-state.
512 : If this sm allows for state inheritance, then SVAL might have implicit
513 : sm-state inherited via a parent.
514 : For example INIT_VAL(foo.field) might inherit taintedness state from
515 : INIT_VAL(foo). */
516 4474294 : if (m_sm.inherited_state_p ())
517 1749560 : if (region_model_manager *mgr = ext_state.get_model_manager ())
518 : {
519 1749560 : if (const initial_svalue *init_sval = sval->dyn_cast_initial_svalue ())
520 : {
521 192153 : const region *reg = init_sval->get_region ();
522 : /* Try recursing upwards (up to the base region for the
523 : cluster). */
524 192153 : if (!reg->base_region_p ())
525 0 : if (const region *parent_reg = reg->get_parent_region ())
526 : {
527 0 : const svalue *parent_init_sval
528 0 : = mgr->get_or_create_initial_value (parent_reg);
529 0 : state_machine::state_t parent_state
530 0 : = get_state (parent_init_sval, ext_state);
531 0 : if (parent_state)
532 : return parent_state;
533 : }
534 : }
535 1749560 : if (auto *from_bv = sval->dyn_cast_svalue_from_bit_vector ())
536 187009 : if (auto state
537 : = get_any_inherited_state_of_bit_vector
538 187009 : (from_bv->get_bit_vector (),
539 : ext_state,
540 : *mgr))
541 : return state;
542 : }
543 :
544 8698454 : if (state_machine::state_t state
545 4349227 : = m_sm.alt_get_inherited_state (*this, sval, ext_state))
546 : return state;
547 :
548 4071426 : return m_sm.get_default_state (sval);
549 : }
550 :
551 : /* Get the "origin" svalue for any state of SVAL. */
552 :
553 : const svalue *
554 12 : sm_state_map::get_origin (const svalue *sval,
555 : const extrinsic_state &ext_state) const
556 : {
557 12 : gcc_assert (sval);
558 :
559 12 : sval = canonicalize_svalue (sval, ext_state);
560 :
561 12 : entry_t *slot
562 12 : = const_cast <map_t &> (m_map).get (sval);
563 12 : if (slot)
564 12 : return slot->m_origin;
565 : else
566 : return nullptr;
567 : }
568 :
569 : /* Set the state of SID within MODEL to STATE, recording that
570 : the state came from ORIGIN. */
571 :
572 : void
573 36574 : sm_state_map::set_state (region_model *model,
574 : const svalue *sval,
575 : state_machine::state_t state,
576 : const svalue *origin,
577 : const extrinsic_state &ext_state)
578 : {
579 36574 : if (model == nullptr)
580 : return;
581 :
582 : /* Reject attempts to set state on UNKNOWN/POISONED. */
583 36574 : if (!sval->can_have_associated_state_p ())
584 : return;
585 :
586 33278 : equiv_class &ec = model->get_constraints ()->get_equiv_class (sval);
587 33278 : if (!set_state (ec, state, origin, ext_state))
588 : return;
589 : }
590 :
591 : /* Set the state of EC to STATE, recording that the state came from
592 : ORIGIN.
593 : Return true if any states of svalue_ids within EC changed. */
594 :
595 : bool
596 33278 : sm_state_map::set_state (const equiv_class &ec,
597 : state_machine::state_t state,
598 : const svalue *origin,
599 : const extrinsic_state &ext_state)
600 : {
601 33278 : bool any_changed = false;
602 134882 : for (const svalue *sval : ec.m_vars)
603 35048 : any_changed |= impl_set_state (sval, state, origin, ext_state);
604 33278 : return any_changed;
605 : }
606 :
607 : /* Set state of SVAL to STATE, bypassing equivalence classes.
608 : Return true if the state changed. */
609 :
610 : bool
611 102604 : sm_state_map::impl_set_state (const svalue *sval,
612 : state_machine::state_t state,
613 : const svalue *origin,
614 : const extrinsic_state &ext_state)
615 : {
616 102604 : sval = canonicalize_svalue (sval, ext_state);
617 :
618 102604 : if (get_state (sval, ext_state) == state)
619 : return false;
620 :
621 86961 : gcc_assert (sval->can_have_associated_state_p ());
622 :
623 : /* Special-case state 0 as the default value. */
624 86961 : if (state == 0)
625 : {
626 800 : if (m_map.get (sval))
627 796 : m_map.remove (sval);
628 : return true;
629 : }
630 86161 : gcc_assert (sval);
631 86161 : m_map.put (sval, entry_t (state, origin));
632 :
633 : /* Update m_map_ubc_to_state. */
634 86161 : if (m_sm.inherited_state_p ())
635 20867 : if (auto *from_bv = sval->dyn_cast_svalue_from_bit_vector ())
636 274 : if (auto *from_ubc
637 274 : = from_bv->get_bit_vector ().dyn_cast_bit_vector_from_ubc ())
638 267 : m_map_ubc_to_state[&from_ubc->get_bit_contents ()] = state;
639 :
640 : return true;
641 : }
642 :
643 : /* Clear any state for SVAL from this state map. */
644 :
645 : void
646 2883 : sm_state_map::clear_any_state (const svalue *sval)
647 : {
648 2883 : m_map.remove (sval);
649 : // TODO: what about m_map_ubc_to_state?
650 2883 : }
651 :
652 : /* Clear all per-svalue state within this state map. */
653 :
654 : void
655 7839 : sm_state_map::clear_all_per_svalue_state ()
656 : {
657 7839 : m_map.empty ();
658 7839 : m_map_ubc_to_state.clear ();
659 7839 : }
660 :
661 : /* Set the "global" state within this state map to STATE. */
662 :
663 : void
664 108445 : sm_state_map::set_global_state (state_machine::state_t state)
665 : {
666 108445 : m_global_state = state;
667 108445 : }
668 :
669 : /* Get the "global" state within this state map. */
670 :
671 : state_machine::state_t
672 981236 : sm_state_map::get_global_state () const
673 : {
674 981236 : return m_global_state;
675 : }
676 :
677 : /* Purge any state for SVAL.
678 : If !SM::can_purge_p, then report the state as leaking,
679 : using CTXT. */
680 :
681 : void
682 173157 : sm_state_map::on_svalue_leak (const svalue *sval,
683 : impl_region_model_context *ctxt)
684 : {
685 173157 : if (state_machine::state_t state = get_state (sval, ctxt->m_ext_state))
686 : {
687 173157 : if (m_sm.can_purge_p (state))
688 172680 : m_map.remove (sval);
689 : else
690 477 : ctxt->on_state_leak (m_sm, sval, state);
691 : }
692 173157 : }
693 :
694 : /* Purge any state for svalues that aren't live with respect to
695 : LIVENESS_CTXT. */
696 :
697 : void
698 3047139 : sm_state_map::on_liveness_change (symbolic::liveness_context &liveness_ctxt,
699 : const extrinsic_state &ext_state,
700 : impl_region_model_context *ctxt)
701 : {
702 3047139 : symbolic::node_set<svalue> svals_to_unset;
703 3047139 : uncertainty_t *uncertainty = ctxt->get_uncertainty ();
704 :
705 3047139 : auto_vec<const svalue *> leaked_svals (m_map.elements ());
706 3447800 : for (map_t::iterator iter = m_map.begin ();
707 3447800 : iter != m_map.end ();
708 400661 : ++iter)
709 : {
710 400661 : const svalue *iter_sval = (*iter).first;
711 400661 : if (!liveness_ctxt.node_live_p (*iter_sval))
712 : {
713 20089 : svals_to_unset.insert (iter_sval);
714 20089 : entry_t e = (*iter).second;
715 20089 : if (!m_sm.can_purge_p (e.m_state))
716 781 : leaked_svals.quick_push (iter_sval);
717 : }
718 400661 : if (uncertainty)
719 356313 : if (uncertainty->unknown_sm_state_p (iter_sval))
720 321 : svals_to_unset.insert (iter_sval);
721 : }
722 :
723 3047139 : leaked_svals.qsort (svalue::cmp_ptr_ptr);
724 :
725 3047139 : unsigned i;
726 3047139 : const svalue *sval;
727 3274945 : FOR_EACH_VEC_ELT (leaked_svals, i, sval)
728 : {
729 781 : entry_t e = *m_map.get (sval);
730 781 : ctxt->on_state_leak (m_sm, sval, e.m_state);
731 : }
732 :
733 3047139 : sm_state_map old_sm_map = *this;
734 :
735 3047139 : for (auto iter = svals_to_unset.begin ();
736 3067549 : iter != svals_to_unset.end (); ++iter)
737 20410 : m_map.remove (*iter);
738 :
739 : /* For state machines like "taint" where states can be
740 : alt-inherited from other svalues, ensure that state purging doesn't
741 : make us lose sm-state.
742 :
743 : Consider e.g.:
744 :
745 : make_tainted(foo);
746 : if (foo.field > 128)
747 : return;
748 : arr[foo.field].f1 = v1;
749 :
750 : where the last line is:
751 :
752 : (A): _t1 = foo.field;
753 : (B): _t2 = _t1 * sizeof(arr[0]);
754 : (C): [arr + _t2].f1 = val;
755 :
756 : At (A), foo is 'tainted' and foo.field is 'has_ub'.
757 : After (B), foo.field's value (in _t1) is no longer directly
758 : within LIVE_SVALUES, so with state purging enabled, we would
759 : erroneously purge the "has_ub" state from the svalue.
760 :
761 : Given that _t2's value's state comes from _t1's value's state,
762 : we need to preserve that information.
763 :
764 : Hence for all svalues that have had their explicit sm-state unset,
765 : having their sm-state being unset, determine if doing so has changed
766 : their effective state, and if so, explicitly set their state.
767 :
768 : For example, in the above, unsetting the "has_ub" for _t1's value means
769 : that _t2's effective value changes from "has_ub" (from alt-inherited
770 : from _t1's value) to "tainted" (inherited from "foo"'s value).
771 :
772 : For such cases, preserve the effective state by explicitly setting the
773 : new state. In the above example, this means explicitly setting _t2's
774 : value to the value ("has_ub") it was previously alt-inheriting from _t1's
775 : value. */
776 3047139 : if (m_sm.has_alt_get_inherited_state_p ())
777 : {
778 435235 : auto_vec<const svalue *> svalues_needing_state;
779 452747 : for (auto unset_sval : svals_to_unset)
780 : {
781 17512 : const state_machine::state_t old_state
782 17512 : = old_sm_map.get_state (unset_sval, ext_state);
783 17512 : const state_machine::state_t new_state
784 17512 : = get_state (unset_sval, ext_state);
785 17512 : if (new_state != old_state)
786 17442 : svalues_needing_state.safe_push (unset_sval);
787 : }
788 471305 : for (auto sval : svalues_needing_state)
789 : {
790 17442 : const state_machine::state_t old_state
791 17442 : = old_sm_map.get_state (sval, ext_state);
792 17442 : impl_set_state (sval, old_state, nullptr, ext_state);
793 : }
794 435235 : }
795 3047139 : }
796 :
797 : /* Purge state from VAL (in response to a call to an unknown function). */
798 :
799 : void
800 198072 : sm_state_map::on_unknown_change (const symbolic::value_node &val,
801 : bool is_mutable,
802 : const extrinsic_state &ext_state)
803 : {
804 198072 : symbolic::node_set<svalue> svals_to_unset;
805 :
806 198072 : if (auto *sval = val.dyn_cast_svalue ())
807 : {
808 153202 : sval = canonicalize_svalue (sval, ext_state);
809 :
810 171649 : for (map_t::iterator iter = m_map.begin ();
811 324851 : iter != m_map.end ();
812 18447 : ++iter)
813 : {
814 18447 : const svalue *key = (*iter).first;
815 18447 : entry_t e = (*iter).second;
816 : /* We only want to purge state for some states when things
817 : are mutable. For example, in sm-malloc.cc, an on-stack ptr
818 : doesn't stop being stack-allocated when passed to an unknown fn. */
819 18447 : if (!m_sm.reset_when_passed_to_unknown_fn_p (e.m_state, is_mutable))
820 8035 : continue;
821 10412 : if (key == sval)
822 750 : svals_to_unset.add (key);
823 : /* If we have INIT_VAL(BASE_REG), then unset any INIT_VAL(REG)
824 : for REG within BASE_REG. */
825 10412 : if (const initial_svalue *init_sval = sval->dyn_cast_initial_svalue ())
826 2440 : if (const initial_svalue *init_key = key->dyn_cast_initial_svalue ())
827 : {
828 915 : const region *changed_reg = init_sval->get_region ();
829 915 : const region *changed_key = init_key->get_region ();
830 915 : if (changed_key->get_base_region () == changed_reg)
831 199 : svals_to_unset.add (key);
832 : }
833 : }
834 : }
835 44870 : else if (auto *bv = val.dyn_cast_bit_vector ())
836 : {
837 54094 : for (map_t::iterator iter = m_map.begin ();
838 98964 : iter != m_map.end ();
839 9224 : ++iter)
840 : {
841 9224 : const svalue *key = (*iter).first;
842 9224 : entry_t e = (*iter).second;
843 : /* We only want to purge state for some states when things
844 : are mutable. For example, in sm-malloc.cc, an on-stack ptr
845 : doesn't stop being stack-allocated when passed to an unknown fn. */
846 9224 : if (!m_sm.reset_when_passed_to_unknown_fn_p (e.m_state, is_mutable))
847 5013 : continue;
848 4211 : if (auto *sval_from_bv = key->dyn_cast_svalue_from_bit_vector ())
849 217 : if (&sval_from_bv->get_bit_vector () == bv)
850 33 : svals_to_unset.add (key);
851 : }
852 : }
853 : else
854 0 : gcc_unreachable ();
855 :
856 198855 : for (auto iter = svals_to_unset.begin ();
857 198855 : iter != svals_to_unset.end (); ++iter)
858 783 : impl_set_state (*iter, (state_machine::state_t)0, nullptr, ext_state);
859 198072 : }
860 :
861 : /* Purge state for things involving NODE.
862 : For use when NODE changes meaning, at the def_stmt on an SSA_NAME. */
863 :
864 : void
865 80969 : sm_state_map::purge_state_involving (const symbolic::node &node,
866 : const extrinsic_state &ext_state)
867 : {
868 80969 : symbolic::node_set<svalue> svals_to_unset;
869 :
870 90180 : for (map_t::iterator iter = m_map.begin ();
871 90180 : iter != m_map.end ();
872 9211 : ++iter)
873 : {
874 9211 : const svalue *key = (*iter).first;
875 9211 : entry_t e = (*iter).second;
876 9211 : if (!m_sm.can_purge_p (e.m_state))
877 3682 : continue;
878 5529 : if (key->involves_p (node))
879 9 : svals_to_unset.add (key);
880 : }
881 :
882 80978 : for (auto iter = svals_to_unset.begin ();
883 80978 : iter != svals_to_unset.end (); ++iter)
884 9 : impl_set_state (*iter, (state_machine::state_t)0, nullptr, ext_state);
885 80969 : }
886 :
887 : /* Comparator for imposing an order on sm_state_map instances. */
888 :
889 : int
890 355733 : sm_state_map::cmp (const sm_state_map &smap_a, const sm_state_map &smap_b)
891 : {
892 355733 : if (int cmp_count = smap_a.elements () - smap_b.elements ())
893 : return cmp_count;
894 :
895 308532 : auto_vec <const svalue *> keys_a (smap_a.elements ());
896 308532 : for (map_t::iterator iter = smap_a.begin ();
897 389387 : iter != smap_a.end ();
898 80855 : ++iter)
899 80855 : keys_a.quick_push ((*iter).first);
900 308532 : keys_a.qsort (svalue::cmp_ptr_ptr);
901 :
902 308532 : auto_vec <const svalue *> keys_b (smap_b.elements ());
903 308532 : for (map_t::iterator iter = smap_b.begin ();
904 389387 : iter != smap_b.end ();
905 80855 : ++iter)
906 80855 : keys_b.quick_push ((*iter).first);
907 308532 : keys_b.qsort (svalue::cmp_ptr_ptr);
908 :
909 308532 : unsigned i;
910 308532 : const svalue *sval_a;
911 414164 : FOR_EACH_VEC_ELT (keys_a, i, sval_a)
912 : {
913 76342 : const svalue *sval_b = keys_b[i];
914 76342 : if (int cmp_sval = svalue::cmp_ptr (sval_a, sval_b))
915 10330 : return cmp_sval;
916 72371 : const entry_t *e_a = const_cast <map_t &> (smap_a.m_map).get (sval_a);
917 72371 : const entry_t *e_b = const_cast <map_t &> (smap_b.m_map).get (sval_b);
918 72371 : if (int cmp_entry = entry_t::cmp (*e_a, *e_b))
919 : return cmp_entry;
920 : }
921 :
922 : // TODO: m_map_ubc_to_state
923 :
924 : return 0;
925 308532 : }
926 :
927 : /* Canonicalize SVAL before getting/setting it within the map.
928 : Convert all NULL pointers to (void *) to avoid state explosions
929 : involving all of the various (foo *)NULL vs (bar *)NULL. */
930 :
931 : const svalue *
932 6388056 : sm_state_map::canonicalize_svalue (const svalue *sval,
933 : const extrinsic_state &ext_state)
934 : {
935 6388056 : if (!sval)
936 : return nullptr;
937 :
938 6385618 : region_model_manager *mgr = ext_state.get_model_manager ();
939 6385618 : if (!mgr)
940 : return sval;
941 :
942 6385618 : if (sval->get_type () && POINTER_TYPE_P (sval->get_type ()))
943 : {
944 3158892 : if (tree cst = sval->maybe_get_constant ())
945 63306 : if (zerop (cst))
946 62858 : return mgr->get_or_create_constant_svalue (null_pointer_node);
947 : }
948 :
949 : /* Strip types from symbolic pointers. */
950 6322760 : if (auto *sym_ptr = sval->maybe_get_symbolic_pointer ())
951 1750498 : return mgr->get_ptr_svalue (ptr_type_node, *sym_ptr);
952 :
953 : /* Strip types from svalue_from_bit_vector. */
954 4572262 : if (auto *from_bv = sval->dyn_cast_svalue_from_bit_vector ())
955 731401 : return mgr->get_svalue_from_bit_vector (NULL_TREE,
956 731401 : from_bv->get_bit_vector ());
957 :
958 : return sval;
959 : }
960 :
961 : /* Attempt to merge this state map with OTHER, writing the result
962 : into *OUT.
963 : Return true if the merger was possible, false otherwise.
964 :
965 : Normally, only identical state maps can be merged, so that
966 : differences between state maps lead to different enodes
967 :
968 : However some state machines may support merging states to
969 : allow for discarding of less important states, and thus avoid
970 : blow-up of the exploded graph. */
971 :
972 : bool
973 541312 : sm_state_map::can_merge_with_p (const sm_state_map &other,
974 : const state_machine &sm,
975 : const extrinsic_state &ext_state,
976 : sm_state_map **out) const
977 : {
978 : /* If identical, then they merge trivially, with a copy. */
979 541312 : if (*this == other)
980 : {
981 472916 : delete *out;
982 472916 : *out = clone ();
983 472916 : return true;
984 : }
985 :
986 68396 : delete *out;
987 68396 : *out = new sm_state_map (sm);
988 :
989 : /* Otherwise, attempt to merge element by element. */
990 :
991 : /* Try to merge global state. */
992 136792 : if (state_machine::state_t merged_global_state
993 69275 : = sm.maybe_get_merged_state (get_global_state (),
994 : other.get_global_state ()))
995 67517 : (*out)->set_global_state (merged_global_state);
996 : else
997 : return false;
998 :
999 : /* Try to merge state each svalue's state (for the union
1000 : of svalues represented by each smap).
1001 : Ignore the origin information. */
1002 67517 : hash_set<const svalue *> svals;
1003 217155 : for (auto kv : *this)
1004 149638 : svals.add (kv.first);
1005 188124 : for (auto kv : other)
1006 120607 : svals.add (kv.first);
1007 116787 : for (auto sval : svals)
1008 : {
1009 109028 : state_machine::state_t this_state = get_state (sval, ext_state);
1010 109028 : state_machine::state_t other_state = other.get_state (sval, ext_state);
1011 109028 : if (state_machine::state_t merged_state
1012 109028 : = sm.maybe_get_merged_state (this_state, other_state))
1013 49270 : (*out)->impl_set_state (sval, merged_state, nullptr, ext_state);
1014 : else
1015 59758 : return false;
1016 : }
1017 :
1018 : // TODO: m_map_ubc_to_state
1019 :
1020 : /* Successfully merged all elements. */
1021 7759 : return true;
1022 67517 : }
1023 :
1024 : void
1025 11244980 : sm_state_map::validate (const extrinsic_state &ext_state) const
1026 : {
1027 12510421 : for (auto iter : m_map)
1028 : {
1029 1265441 : const svalue *key = iter.first;
1030 1265441 : const svalue *origin = iter.second.m_origin;
1031 :
1032 1265441 : gcc_assert (canonicalize_svalue (key, ext_state) == key);
1033 1265441 : if (origin)
1034 0 : gcc_assert (canonicalize_svalue (origin, ext_state) == origin);
1035 : }
1036 11244980 : }
1037 :
1038 : /* class program_state. */
1039 :
1040 : /* program_state's ctor. */
1041 :
1042 141040 : program_state::program_state (const extrinsic_state &ext_state)
1043 141040 : : m_region_model (nullptr),
1044 141040 : m_checker_states (ext_state.get_num_checkers ()),
1045 141040 : m_valid (true)
1046 : {
1047 141040 : engine *eng = ext_state.get_engine ();
1048 141040 : auto &node_mgr = eng->get_node_manager ();
1049 141040 : m_region_model = new region_model (node_mgr);
1050 141040 : const int num_states = ext_state.get_num_checkers ();
1051 1127796 : for (int i = 0; i < num_states; i++)
1052 : {
1053 986756 : sm_state_map *sm = new sm_state_map (ext_state.get_sm (i));
1054 986756 : m_checker_states.quick_push (sm);
1055 : }
1056 141040 : }
1057 :
1058 : /* Attempt to use R to replay SUMMARY into this object.
1059 : Return true if it is possible. */
1060 :
1061 : bool
1062 2898 : sm_state_map::replay_call_summary (call_summary_replay &r,
1063 : const sm_state_map &summary)
1064 : {
1065 3200 : for (auto kv : summary.m_map)
1066 : {
1067 302 : const svalue *summary_sval = kv.first;
1068 302 : const svalue *caller_sval = r.convert_svalue_from_summary (summary_sval);
1069 370 : if (!caller_sval)
1070 108 : continue;
1071 234 : caller_sval = canonicalize_svalue (caller_sval, r.get_ext_state ());
1072 234 : if (!caller_sval->can_have_associated_state_p ())
1073 40 : continue;
1074 194 : const svalue *summary_origin = kv.second.m_origin;
1075 194 : const svalue *caller_origin
1076 : = (summary_origin
1077 194 : ? r.convert_svalue_from_summary (summary_origin)
1078 : : nullptr);
1079 : // caller_origin can be nullptr.
1080 194 : m_map.put (caller_sval, entry_t (kv.second.m_state, caller_origin));
1081 : }
1082 2898 : m_global_state = summary.m_global_state;
1083 2898 : return true;
1084 : }
1085 :
1086 : /* program_state's copy ctor. */
1087 :
1088 1732366 : program_state::program_state (const program_state &other)
1089 1732366 : : m_region_model (new region_model (*other.m_region_model)),
1090 3464732 : m_checker_states (other.m_checker_states.length ()),
1091 1732366 : m_valid (true)
1092 : {
1093 1732366 : int i;
1094 1732366 : sm_state_map *smap;
1095 13847430 : FOR_EACH_VEC_ELT (other.m_checker_states, i, smap)
1096 12115064 : m_checker_states.quick_push (smap->clone ());
1097 1732366 : }
1098 :
1099 : /* program_state's assignment operator. */
1100 :
1101 : program_state&
1102 9573 : program_state::operator= (const program_state &other)
1103 : {
1104 9573 : delete m_region_model;
1105 9573 : m_region_model = new region_model (*other.m_region_model);
1106 :
1107 9573 : int i;
1108 9573 : sm_state_map *smap;
1109 76497 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1110 66924 : delete smap;
1111 9573 : m_checker_states.truncate (0);
1112 28719 : gcc_assert (m_checker_states.space (other.m_checker_states.length ()));
1113 :
1114 76497 : FOR_EACH_VEC_ELT (other.m_checker_states, i, smap)
1115 66924 : m_checker_states.quick_push (smap->clone ());
1116 :
1117 9573 : m_valid = other.m_valid;
1118 :
1119 9573 : return *this;
1120 : }
1121 :
1122 : /* Move constructor for program_state (when building with C++11). */
1123 797493 : program_state::program_state (program_state &&other)
1124 797493 : : m_region_model (other.m_region_model),
1125 1594986 : m_checker_states (other.m_checker_states.length ())
1126 : {
1127 797493 : other.m_region_model = nullptr;
1128 :
1129 797493 : int i;
1130 797493 : sm_state_map *smap;
1131 7171949 : FOR_EACH_VEC_ELT (other.m_checker_states, i, smap)
1132 5576963 : m_checker_states.quick_push (smap);
1133 797493 : other.m_checker_states.truncate (0);
1134 :
1135 797493 : m_valid = other.m_valid;
1136 797493 : }
1137 :
1138 : /* Move assignment for program_state. */
1139 :
1140 : program_state&
1141 25156 : program_state::operator= (program_state &&other)
1142 : {
1143 25156 : if (this == &other)
1144 : return *this;
1145 :
1146 25156 : delete m_region_model;
1147 25156 : m_region_model = other.m_region_model;
1148 25156 : other.m_region_model = nullptr;
1149 :
1150 25156 : int i;
1151 25156 : sm_state_map *smap;
1152 226404 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1153 176092 : delete smap;
1154 25156 : m_checker_states.truncate (0);
1155 75468 : gcc_assert (m_checker_states.space (other.m_checker_states.length ()));
1156 226404 : FOR_EACH_VEC_ELT (other.m_checker_states, i, smap)
1157 176092 : m_checker_states.quick_push (smap);
1158 25156 : other.m_checker_states.truncate (0);
1159 :
1160 25156 : m_valid = other.m_valid;
1161 :
1162 25156 : return *this;
1163 : }
1164 :
1165 : /* program_state's dtor. */
1166 :
1167 2670899 : program_state::~program_state ()
1168 : {
1169 2670899 : delete m_region_model;
1170 2670899 : }
1171 :
1172 : /* Generate a hash value for this program_state. */
1173 :
1174 : hashval_t
1175 417489 : program_state::hash () const
1176 : {
1177 417489 : hashval_t result = m_region_model->hash ();
1178 :
1179 417489 : int i;
1180 417489 : sm_state_map *smap;
1181 3754640 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1182 2919662 : result ^= smap->hash ();
1183 417489 : return result;
1184 : }
1185 :
1186 : /* Equality operator for program_state.
1187 : All parts of the program_state (region model, checker states) must
1188 : equal their counterparts in OTHER for the two program_states to be
1189 : considered equal. */
1190 :
1191 : bool
1192 65857 : program_state::operator== (const program_state &other) const
1193 : {
1194 65857 : if (!(*m_region_model == *other.m_region_model))
1195 : return false;
1196 :
1197 : int i;
1198 : sm_state_map *smap;
1199 58651 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1200 51567 : if (!(*smap == *other.m_checker_states[i]))
1201 : return false;
1202 :
1203 7084 : gcc_checking_assert (hash () == other.hash ());
1204 :
1205 : return true;
1206 : }
1207 :
1208 : /* Dump a tree-like representation of this state to stderr. */
1209 :
1210 : DEBUG_FUNCTION void
1211 0 : program_state::dump () const
1212 : {
1213 0 : symbolic::dump_node_tree_to_stderr<program_state> (*this);
1214 0 : }
1215 :
1216 : /* Dump a tree-like representation of this state to LOG. */
1217 :
1218 : void
1219 713 : program_state::dump_to_logger (logger &log, const char *label) const
1220 : {
1221 713 : symbolic::dump_node_tree_to_logger<program_state> (*this, log, label);
1222 713 : }
1223 :
1224 : /* Return a new json::object of the form
1225 : {"store" : object for store,
1226 : "constraints" : object for constraint_manager,
1227 : "curr_frame" : (optional) str for current frame,
1228 : "checkers" : { STATE_NAME : object per sm_state_map },
1229 : "valid" : true/false}. */
1230 :
1231 : std::unique_ptr<json::object>
1232 15 : program_state::to_json (const extrinsic_state &ext_state) const
1233 : {
1234 15 : auto state_obj = std::make_unique<json::object> ();
1235 :
1236 15 : state_obj->set ("store", m_region_model->get_store ()->to_json ());
1237 15 : state_obj->set ("constraints",
1238 15 : m_region_model->get_constraints ()->to_json ());
1239 15 : if (m_region_model->get_current_frame ())
1240 14 : state_obj->set ("curr_frame",
1241 14 : m_region_model->get_current_frame ()->to_json ());
1242 :
1243 : /* Provide m_checker_states as an object, using names as keys. */
1244 15 : {
1245 15 : auto checkers_obj = std::make_unique<json::object> ();
1246 :
1247 15 : int i;
1248 15 : sm_state_map *smap;
1249 135 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1250 105 : if (!smap->is_empty_p ())
1251 9 : checkers_obj->set (ext_state.get_name (i), smap->to_json ());
1252 :
1253 15 : state_obj->set ("checkers", std::move (checkers_obj));
1254 15 : }
1255 :
1256 15 : state_obj->set_bool ("valid", m_valid);
1257 :
1258 15 : return state_obj;
1259 : }
1260 :
1261 :
1262 : std::unique_ptr<text_art::tree_widget>
1263 713 : program_state::make_dump_widget (symbolic::node_dump_state &nds,
1264 : const char *label) const
1265 : {
1266 713 : if (!label)
1267 338 : label = "program_state";
1268 :
1269 713 : using text_art::tree_widget;
1270 713 : std::unique_ptr<tree_widget> state_widget
1271 713 : (tree_widget::from_fmt (nds, nullptr, "%s", label));
1272 :
1273 1426 : state_widget->add_child (m_region_model->make_dump_widget (nds));
1274 :
1275 : /* Add nodes for any sm_state_maps with state. */
1276 713 : {
1277 713 : int i;
1278 713 : sm_state_map *smap;
1279 5704 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1280 4991 : if (!smap->is_empty_p ())
1281 140 : state_widget->add_child (smap->make_dump_widget (nds, m_region_model));
1282 : }
1283 :
1284 713 : return state_widget;
1285 : }
1286 :
1287 : void
1288 0 : program_state::dump_dot (const extrinsic_state &ext_state) const
1289 : {
1290 0 : auto state_graph = make_diagnostic_state_graph (ext_state);
1291 :
1292 0 : gcc_assert (global_dc);
1293 0 : auto logical_loc_mgr = global_dc->get_logical_location_manager ();
1294 0 : gcc_assert (logical_loc_mgr);
1295 :
1296 0 : auto graph = diagnostics::state_graphs::make_dot_graph (*state_graph,
1297 0 : *logical_loc_mgr);
1298 :
1299 0 : pretty_printer pp;
1300 0 : dot::writer w (pp);
1301 0 : graph->print (w);
1302 0 : pp_flush (&pp);
1303 0 : }
1304 :
1305 : /* Update this program_state to reflect a top-level call to FUN.
1306 : The params will have initial_svalues. */
1307 :
1308 : void
1309 11666 : program_state::push_frame (const extrinsic_state &ext_state ATTRIBUTE_UNUSED,
1310 : const function &fun)
1311 : {
1312 11666 : m_region_model->push_frame (fun, nullptr, nullptr, nullptr);
1313 11666 : }
1314 :
1315 : /* Get the current function of this state. */
1316 :
1317 : const function *
1318 22 : program_state::get_current_function () const
1319 : {
1320 22 : return m_region_model->get_current_function ();
1321 : }
1322 :
1323 : /* Generate a simpler version of THIS, discarding state that's no longer
1324 : relevant at POINT.
1325 : The idea is that we're more likely to be able to consolidate
1326 : multiple (point, state) into single exploded_nodes if we discard
1327 : irrelevant state (e.g. at the end of functions). */
1328 :
1329 : program_state
1330 378165 : program_state::prune_for_point (exploded_graph &eg,
1331 : const program_point &point,
1332 : exploded_node *enode_for_diag,
1333 : uncertainty_t *uncertainty) const
1334 : {
1335 378165 : logger * const logger = eg.get_logger ();
1336 378165 : LOG_SCOPE (logger);
1337 :
1338 378165 : function *fun = point.get_function ();
1339 374506 : if (!fun)
1340 3659 : return *this;
1341 :
1342 374506 : program_state new_state (*this);
1343 :
1344 374506 : const state_purge_map *pm = eg.get_purge_map ();
1345 374506 : if (pm)
1346 : {
1347 374044 : unsigned num_ssas_purged = 0;
1348 374044 : unsigned num_decls_purged = 0;
1349 374044 : auto_vec<const decl_region *> regs;
1350 374044 : new_state.m_region_model->get_regions_for_current_frame (®s);
1351 374044 : regs.qsort (region::cmp_ptr_ptr);
1352 374044 : unsigned i;
1353 374044 : const decl_region *reg;
1354 1289914 : FOR_EACH_VEC_ELT (regs, i, reg)
1355 : {
1356 915870 : const tree node = reg->get_decl ();
1357 915870 : if (TREE_CODE (node) == SSA_NAME)
1358 : {
1359 787541 : const tree ssa_name = node;
1360 787541 : const state_purge_per_ssa_name &per_ssa
1361 787541 : = pm->get_data_for_ssa_name (node);
1362 787541 : if (!per_ssa.needed_at_supernode_p (point.get_supernode ()))
1363 : {
1364 : /* Don't purge bindings of SSA names to svalues
1365 : that have unpurgable sm-state, so that leaks are
1366 : reported at the end of the function, rather than
1367 : at the last place that such an SSA name is referred to.
1368 :
1369 : But do purge them for temporaries (when SSA_NAME_VAR is
1370 : NULL), so that we report for cases where a leak happens when
1371 : a variable is overwritten with another value, so that the leak
1372 : is reported at the point of overwrite, rather than having
1373 : temporaries keep the value reachable until the frame is
1374 : popped. */
1375 191743 : const svalue *sval
1376 191743 : = new_state.m_region_model->get_store_value (reg, nullptr);
1377 191743 : if (!new_state.can_purge_p (eg.get_ext_state (), sval)
1378 217251 : && SSA_NAME_VAR (ssa_name))
1379 : {
1380 : /* (currently only state maps can keep things
1381 : alive). */
1382 19533 : if (logger)
1383 0 : logger->log ("not purging binding for %qE"
1384 : " (used by state map)", ssa_name);
1385 19533 : continue;
1386 : }
1387 :
1388 172210 : new_state.m_region_model->purge_region (reg);
1389 172210 : num_ssas_purged++;
1390 : }
1391 : }
1392 : else
1393 : {
1394 128329 : const tree decl = node;
1395 128329 : gcc_assert (TREE_CODE (node) == VAR_DECL
1396 : || TREE_CODE (node) == PARM_DECL
1397 : || TREE_CODE (node) == RESULT_DECL);
1398 256658 : if (const state_purge_per_decl *per_decl
1399 128329 : = pm->get_any_data_for_decl (decl))
1400 102315 : if (!per_decl->needed_at_supernode_p (point.get_supernode ()))
1401 : {
1402 : /* Don't purge bindings of decls if there are svalues
1403 : that have unpurgable sm-state within the decl's cluster,
1404 : so that leaks are reported at the end of the function,
1405 : rather than at the last place that such a decl is
1406 : referred to. */
1407 1957 : if (!new_state.can_purge_base_region_p (eg.get_ext_state (),
1408 : reg))
1409 : {
1410 : /* (currently only state maps can keep things
1411 : alive). */
1412 620 : if (logger)
1413 0 : logger->log ("not purging binding for %qE"
1414 : " (value in binding used by state map)",
1415 : decl);
1416 620 : continue;
1417 : }
1418 :
1419 1337 : new_state.m_region_model->purge_region (reg);
1420 1337 : num_decls_purged++;
1421 : }
1422 : }
1423 : }
1424 :
1425 374044 : if (num_ssas_purged > 0 || num_decls_purged > 0)
1426 : {
1427 143641 : if (logger)
1428 : {
1429 49 : logger->log ("num_ssas_purged: %i", num_ssas_purged);
1430 49 : logger->log ("num_decl_purged: %i", num_decls_purged);
1431 : }
1432 143641 : impl_region_model_context ctxt (eg, enode_for_diag,
1433 : this,
1434 : &new_state,
1435 : uncertainty,
1436 143641 : nullptr);
1437 143641 : detect_leaks (*this, new_state, nullptr, eg.get_ext_state (), &ctxt);
1438 143641 : }
1439 374044 : }
1440 :
1441 374506 : new_state.m_region_model->canonicalize ();
1442 :
1443 374506 : return new_state;
1444 378165 : }
1445 :
1446 : /* Return true if there are no unpurgeable bindings within BASE_REG. */
1447 :
1448 : bool
1449 1957 : program_state::can_purge_base_region_p (const extrinsic_state &ext_state,
1450 : const region *base_reg) const
1451 : {
1452 1957 : binding_cluster *cluster
1453 1957 : = m_region_model->get_store ()->get_cluster (base_reg);
1454 1957 : if (!cluster)
1455 : return true;
1456 :
1457 1957 : symbolic::node_set<symbolic::value_node> bound_value_nodes;
1458 1957 : cluster->for_each_value ([](const symbolic::value_node &val,
1459 : void *user_data) -> iter_result
1460 : {
1461 : auto *bound_value_nodes
1462 : = static_cast <symbolic::node_set<symbolic::value_node> *> (user_data);
1463 : bound_value_nodes->add (&val);
1464 : return iter_result::keep_going;
1465 : }, &bound_value_nodes);
1466 :
1467 1957 : if (bound_value_nodes.empty ())
1468 : return true;
1469 :
1470 1907 : auto &bv_mgr
1471 1907 : = ext_state.get_node_manager ().get_bit_vector_manager ();
1472 :
1473 1907 : int i;
1474 1907 : sm_state_map *smap;
1475 12876 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1476 19717 : for (auto iter : *smap)
1477 : {
1478 1693 : const svalue *sval = iter.first;
1479 1693 : sm_state_map::entry_t e = iter.second;
1480 1693 : if (!ext_state.get_sm (i).can_purge_p (e.m_state))
1481 : {
1482 1004 : if (bound_value_nodes.contains (sval))
1483 620 : return false;
1484 :
1485 1004 : if (auto *as_bv = bv_mgr.maybe_get_bit_vector_from_svalue (*sval))
1486 : {
1487 1004 : if (bound_value_nodes.contains (as_bv))
1488 : return false;
1489 : }
1490 : }
1491 : }
1492 :
1493 : return true;
1494 1957 : }
1495 :
1496 : /* Get a representative tree to use for describing SVAL. */
1497 :
1498 : tree
1499 0 : program_state::get_representative_tree (const svalue *sval) const
1500 : {
1501 0 : gcc_assert (m_region_model);
1502 0 : return m_region_model->get_representative_tree (sval);
1503 : }
1504 :
1505 : /* Attempt to merge this state with OTHER, both at POINT.
1506 : Write the result to *OUT.
1507 : If the states were merged successfully, return true. */
1508 :
1509 : bool
1510 125685 : program_state::can_merge_with_p (const program_state &other,
1511 : const extrinsic_state &ext_state,
1512 : const program_point &point,
1513 : program_state *out) const
1514 : {
1515 125685 : gcc_assert (out);
1516 125685 : gcc_assert (m_region_model);
1517 :
1518 : /* Attempt to merge the sm-states. */
1519 : int i;
1520 : sm_state_map *smap;
1521 606360 : FOR_EACH_VEC_ELT (out->m_checker_states, i, smap)
1522 1082624 : if (!m_checker_states[i]->can_merge_with_p (*other.m_checker_states[i],
1523 : ext_state.get_sm (i),
1524 : ext_state,
1525 541312 : &out->m_checker_states[i]))
1526 : return false;
1527 :
1528 : /* Attempt to merge the region_models. */
1529 65048 : if (!m_region_model->can_merge_with_p (*other.m_region_model,
1530 : point,
1531 : out->m_region_model,
1532 : &ext_state,
1533 : this, &other))
1534 : return false;
1535 :
1536 37405 : out->m_region_model->canonicalize ();
1537 :
1538 37405 : return true;
1539 : }
1540 :
1541 : /* Assert that this object is valid. */
1542 :
1543 : void
1544 1608030 : program_state::validate (const extrinsic_state &ext_state) const
1545 : {
1546 : /* Skip this in a release build. */
1547 : #if !CHECKING_P
1548 : return;
1549 : #endif
1550 :
1551 3216060 : gcc_assert (m_checker_states.length () == ext_state.get_num_checkers ());
1552 1608030 : m_region_model->validate ();
1553 :
1554 1608030 : int i;
1555 1608030 : sm_state_map *smap;
1556 14461040 : FOR_EACH_VEC_ELT (m_checker_states, i, smap)
1557 11244980 : m_checker_states[i]->validate (ext_state);
1558 1608030 : }
1559 :
1560 : static void
1561 256 : log_set_of_values (logger *logger, const char *name,
1562 : const value_set &set)
1563 : {
1564 256 : logger->log ("%s", name);
1565 256 : logger->inc_indent ();
1566 256 : auto_vec<const symbolic::value_node *> val_vec (set.size ());
1567 256 : for (value_set::iterator iter = set.begin ();
1568 2000 : iter != set.end (); ++iter)
1569 1744 : val_vec.quick_push (*iter);
1570 256 : val_vec.qsort (svalue::cmp_ptr_ptr);
1571 256 : unsigned i;
1572 256 : const symbolic::value_node *val;
1573 2000 : FOR_EACH_VEC_ELT (val_vec, i, val)
1574 1744 : val->dump_to_logger (*logger);
1575 256 : logger->dec_indent ();
1576 256 : }
1577 :
1578 : // class symbolic::liveness_context
1579 :
1580 : namespace symbolic {
1581 :
1582 1533670 : liveness_context::liveness_context (const value_set &known_live_values,
1583 : const region_model &model)
1584 1533670 : : m_model (model)
1585 : {
1586 6157901 : for (auto iter : known_live_values)
1587 4624231 : set_known_liveness_for_node (*iter, true);
1588 1533670 : }
1589 :
1590 : bool
1591 5274523 : liveness_context::node_live_p (const symbolic::value_node &n)
1592 : {
1593 : /* Do we already know the liveness of N? */
1594 5274523 : auto iter = m_known_liveness.find (&n);
1595 5274523 : if (iter != m_known_liveness.end ())
1596 3746338 : return iter->second;
1597 :
1598 : /* Otherwise, determine if SVAL is implicitly live due to being made of
1599 : other live svalues. */
1600 1528185 : bool implicitly_live = n.implicitly_live_p (*this);
1601 1528185 : set_known_liveness_for_node (n, implicitly_live);
1602 1528185 : return implicitly_live;
1603 : }
1604 :
1605 : bool
1606 300705 : liveness_context::bits_live_p (const bit_vector_from_ubc &bv_from_ubc)
1607 : {
1608 300705 : return m_model.bits_live_p (bv_from_ubc);
1609 : }
1610 :
1611 : /* Consider any pointer to base_reg in LIVENESS_CTXT as keeping it live, so
1612 : that different (X*)ptr and (Y*)ptr are effectively the same from
1613 : a liveness perspective. */
1614 :
1615 : bool
1616 185664 : liveness_context::known_live_ptr_to_base_region_p (const region &base_reg) const
1617 : {
1618 185664 : auto iter = m_known_base_regions.find (&base_reg);
1619 185664 : if (iter != m_known_base_regions.end ())
1620 147632 : return true;
1621 :
1622 : return false;
1623 : }
1624 :
1625 : void
1626 6152416 : liveness_context::set_known_liveness_for_node (const symbolic::value_node &n,
1627 : bool is_live)
1628 : {
1629 6152416 : m_known_liveness.insert ({&n, is_live});
1630 :
1631 : /* Consider any pointer to a base region as keeping that region live, so
1632 : that different (X*)ptr and (Y*)ptr are effectively the same from
1633 : a liveness perspective. */
1634 6152416 : if (is_live)
1635 5987427 : if (auto *sval = n.dyn_cast_svalue ())
1636 3782639 : if (auto *sym_ptr = sval->maybe_get_symbolic_pointer ())
1637 530341 : m_known_base_regions.insert (&sym_ptr->get_base_region ());
1638 6152416 : }
1639 :
1640 : void
1641 0 : liveness_context::dump_to_pp (pretty_printer *pp) const
1642 : {
1643 0 : text_art::theme *theme = global_dc->get_diagram_theme ();
1644 0 : symbolic::dump_node_tree_to_pp<liveness_context>
1645 0 : (*this, theme, pp,
1646 : true /* show_ids */,
1647 : true /* elide_repeated_nodes */);
1648 0 : }
1649 :
1650 : std::unique_ptr<text_art::tree_widget>
1651 0 : liveness_context::make_dump_widget (symbolic::node_dump_state &nds) const
1652 : {
1653 0 : using text_art::tree_widget;
1654 0 : std::unique_ptr<tree_widget> widget
1655 0 : = tree_widget::from_fmt (nds, nullptr, "impl_liveness_context");
1656 0 : for (auto iter : m_known_liveness)
1657 : {
1658 0 : const symbolic::value_node *node = iter.first;
1659 0 : bool liveness = iter.second;
1660 0 : const char *prefix = liveness ? "live" : "dead";
1661 0 : widget->add_child (node->make_dump_widget (nds, prefix));
1662 : }
1663 0 : for (auto iter : m_known_base_regions)
1664 0 : widget->add_child (iter->make_dump_widget (nds, "known region"));
1665 0 : widget->add_child (m_model.make_dump_widget (nds));
1666 0 : return widget;
1667 : }
1668 :
1669 : } // namespace ana::symbolic
1670 :
1671 : /* Compare the sets of svalues reachable from each of SRC_STATE and DEST_STATE.
1672 : For all svalues that are reachable in SRC_STATE and are not live in
1673 : DEST_STATE (whether explicitly reachable in DEST_STATE, or implicitly live
1674 : based on the former set), call CTXT->on_svalue_leak for them.
1675 :
1676 : Call on_liveness_change on both the CTXT and on the DEST_STATE's
1677 : constraint_manager, purging dead svalues from sm-state and from
1678 : constraints, respectively.
1679 :
1680 : This function should be called at each fine-grained state change, not
1681 : just at exploded edges. */
1682 :
1683 : void
1684 435616 : program_state::detect_leaks (const program_state &src_state,
1685 : const program_state &dest_state,
1686 : const svalue *extra_sval,
1687 : const extrinsic_state &ext_state,
1688 : region_model_context *ctxt)
1689 : {
1690 435616 : logger *logger = ext_state.get_logger ();
1691 435616 : LOG_SCOPE (logger);
1692 435616 : const uncertainty_t *uncertainty = ctxt->get_uncertainty ();
1693 435616 : if (logger)
1694 : {
1695 128 : src_state.dump_to_logger (*logger, "src_state");
1696 128 : dest_state.dump_to_logger (*logger, "dest_state");
1697 128 : if (extra_sval)
1698 : {
1699 1 : logger->log ("extra_sval:");
1700 1 : extra_sval->dump_to_logger (*logger);
1701 : }
1702 128 : if (uncertainty)
1703 114 : uncertainty->dump_to_logger (*logger);
1704 : }
1705 :
1706 : /* Get svalues reachable from each of src_state and dest_state.
1707 : Get svalues *known* to be reachable in src_state.
1708 : Pass in uncertainty for dest_state so that we additionally get svalues that
1709 : *might* still be reachable in dst_state. */
1710 435616 : value_set known_src_svalues;
1711 435616 : src_state.m_region_model->get_reachable_svalues (&known_src_svalues,
1712 : nullptr, nullptr);
1713 435616 : value_set maybe_dest_svalues;
1714 435616 : dest_state.m_region_model->get_reachable_svalues (&maybe_dest_svalues,
1715 : extra_sval, uncertainty);
1716 :
1717 435616 : if (logger)
1718 : {
1719 128 : log_set_of_values (logger, "src_state known reachable values:",
1720 : known_src_svalues);
1721 128 : log_set_of_values (logger, "dest_state maybe reachable values:",
1722 : maybe_dest_svalues);
1723 : }
1724 :
1725 435616 : symbolic::liveness_context dest_liveness (maybe_dest_svalues,
1726 435616 : *dest_state.m_region_model);
1727 :
1728 435616 : auto_vec <const svalue *> dead_svals (known_src_svalues.size ());
1729 5082370 : for (value_set::iterator iter = known_src_svalues.begin ();
1730 5082370 : iter != known_src_svalues.end (); ++iter)
1731 4646754 : if (auto *sval = (*iter)->dyn_cast_svalue ())
1732 : {
1733 : /* For each sval reachable from SRC_STATE, determine if it is
1734 : live in DEST_STATE: either explicitly reachable, implicitly
1735 : live based on the set of explicitly reachable svalues,
1736 : or possibly reachable as recorded in uncertainty.
1737 : Record those that have ceased to be live i.e. were known
1738 : to be live, and are now not known to be even possibly-live. */
1739 2653390 : if (!dest_liveness.node_live_p (*sval))
1740 24753 : dead_svals.quick_push (sval);
1741 : }
1742 :
1743 : /* Call CTXT->on_svalue_leak on all svals in SRC_STATE that have ceased
1744 : to be live, sorting them first to ensure deterministic behavior. */
1745 435616 : dead_svals.qsort (svalue::cmp_ptr_ptr);
1746 435616 : unsigned i;
1747 435616 : const svalue *sval;
1748 460369 : FOR_EACH_VEC_ELT (dead_svals, i, sval)
1749 24753 : ctxt->on_svalue_leak (sval);
1750 :
1751 : /* Purge dead svals from sm-state. */
1752 435616 : ctxt->on_liveness_change (dest_liveness);
1753 :
1754 : /* Purge dead svals from constraints. */
1755 435616 : dest_state.m_region_model->get_constraints ()->on_liveness_change
1756 435616 : (dest_liveness);
1757 :
1758 : /* Purge dead heap-allocated regions from dynamic extents. */
1759 1223291 : for (const svalue *sval : dead_svals)
1760 24753 : if (auto *sym_ptr = sval->maybe_get_symbolic_pointer ())
1761 : {
1762 14132 : const region ® = sym_ptr->get_base_region ();
1763 14132 : if (reg.get_kind () == RK_HEAP_ALLOCATED)
1764 6945 : dest_state.m_region_model->unset_dynamic_extents (®);
1765 : }
1766 435616 : }
1767 :
1768 : /* Attempt to use R to replay SUMMARY into this object.
1769 : Return true if it is possible. */
1770 :
1771 : bool
1772 635 : program_state::replay_call_summary (call_summary_replay &r,
1773 : const program_state &summary)
1774 : {
1775 635 : if (!m_region_model->replay_call_summary (r, *summary.m_region_model))
1776 : return false;
1777 :
1778 3312 : for (unsigned sm_idx = 0; sm_idx < m_checker_states.length (); sm_idx++)
1779 : {
1780 2898 : const sm_state_map *summary_sm_map = summary.m_checker_states[sm_idx];
1781 2898 : m_checker_states[sm_idx]->replay_call_summary (r, *summary_sm_map);
1782 : }
1783 :
1784 414 : if (!summary.m_valid)
1785 0 : m_valid = false;
1786 :
1787 : return true;
1788 : }
1789 :
1790 : /* Handle calls to "__analyzer_dump_state". */
1791 :
1792 : void
1793 377 : program_state::impl_call_analyzer_dump_state (const gcall &call,
1794 : const extrinsic_state &ext_state,
1795 : region_model_context *ctxt)
1796 : {
1797 377 : call_details cd (call, m_region_model, ctxt);
1798 377 : const char *sm_name = cd.get_arg_string_literal (0);
1799 377 : if (!sm_name)
1800 : {
1801 4 : error_at (call.location, "cannot determine state machine");
1802 12 : return;
1803 : }
1804 373 : unsigned sm_idx;
1805 373 : if (!ext_state.get_sm_idx_by_name (sm_name, &sm_idx))
1806 : {
1807 4 : error_at (call.location, "unrecognized state machine %qs", sm_name);
1808 4 : return;
1809 : }
1810 369 : const sm_state_map *smap = m_checker_states[sm_idx];
1811 :
1812 369 : const svalue *sval = cd.get_arg_svalue (1);
1813 :
1814 : /* Strip off cast to int (due to variadic args). */
1815 369 : if (const svalue *cast = sval->maybe_undo_cast ())
1816 30 : sval = cast;
1817 :
1818 369 : state_machine::state_t state = smap->get_state (sval, ext_state);
1819 369 : warning_at (call.location, 0, "state: %qs", state->get_name ());
1820 : }
1821 :
1822 : #if CHECKING_P
1823 :
1824 : namespace selftest {
1825 :
1826 : /* Tests for sm_state_map. */
1827 :
1828 : static void
1829 4 : test_sm_state_map ()
1830 : {
1831 4 : tree x = build_global_decl ("x", integer_type_node);
1832 4 : tree y = build_global_decl ("y", integer_type_node);
1833 4 : tree z = build_global_decl ("z", integer_type_node);
1834 :
1835 4 : std::unique_ptr<state_machine> sm = make_malloc_state_machine (nullptr);
1836 4 : state_machine::state_t start = sm->get_start_state ();
1837 4 : std::vector<std::unique_ptr<state_machine>> checkers;
1838 4 : const state_machine &borrowed_sm = *sm.get ();
1839 4 : checkers.push_back (std::move (sm));
1840 4 : symbolic::node_manager node_mgr;
1841 4 : engine eng (node_mgr);
1842 4 : extrinsic_state ext_state (std::move (checkers), &eng);
1843 :
1844 : /* Test setting states on svalue_id instances directly. */
1845 4 : {
1846 4 : const state_machine::state test_state_42 ("test state 42", 42);
1847 4 : const state_machine::state_t TEST_STATE_42 = &test_state_42;
1848 4 : region_model model (node_mgr);
1849 4 : const svalue *x_sval = model.get_rvalue (x, nullptr);
1850 4 : const svalue *y_sval = model.get_rvalue (y, nullptr);
1851 4 : const svalue *z_sval = model.get_rvalue (z, nullptr);
1852 :
1853 4 : sm_state_map map (borrowed_sm);
1854 4 : ASSERT_TRUE (map.is_empty_p ());
1855 4 : ASSERT_EQ (map.get_state (x_sval, ext_state), start);
1856 :
1857 4 : map.impl_set_state (x_sval, TEST_STATE_42, z_sval, ext_state);
1858 4 : ASSERT_EQ (map.get_state (x_sval, ext_state), TEST_STATE_42);
1859 4 : ASSERT_EQ (map.get_origin (x_sval, ext_state), z_sval);
1860 4 : ASSERT_EQ (map.get_state (y_sval, ext_state), start);
1861 4 : ASSERT_FALSE (map.is_empty_p ());
1862 :
1863 4 : map.impl_set_state (y_sval, 0, z_sval, ext_state);
1864 4 : ASSERT_EQ (map.get_state (y_sval, ext_state), start);
1865 :
1866 4 : map.impl_set_state (x_sval, 0, z_sval, ext_state);
1867 4 : ASSERT_EQ (map.get_state (x_sval, ext_state), start);
1868 4 : ASSERT_TRUE (map.is_empty_p ());
1869 4 : }
1870 :
1871 4 : const state_machine::state test_state_5 ("test state 5", 5);
1872 4 : const state_machine::state_t TEST_STATE_5 = &test_state_5;
1873 :
1874 : /* Test setting states via equivalence classes. */
1875 4 : {
1876 4 : region_model model (node_mgr);
1877 4 : const svalue *x_sval = model.get_rvalue (x, nullptr);
1878 4 : const svalue *y_sval = model.get_rvalue (y, nullptr);
1879 4 : const svalue *z_sval = model.get_rvalue (z, nullptr);
1880 :
1881 4 : sm_state_map map (borrowed_sm);
1882 4 : ASSERT_TRUE (map.is_empty_p ());
1883 4 : ASSERT_EQ (map.get_state (x_sval, ext_state), start);
1884 4 : ASSERT_EQ (map.get_state (y_sval, ext_state), start);
1885 :
1886 4 : model.add_constraint (x, EQ_EXPR, y, nullptr);
1887 :
1888 : /* Setting x to a state should also update y, as they
1889 : are in the same equivalence class. */
1890 4 : map.set_state (&model, x_sval, TEST_STATE_5, z_sval, ext_state);
1891 4 : ASSERT_EQ (map.get_state (x_sval, ext_state), TEST_STATE_5);
1892 4 : ASSERT_EQ (map.get_state (y_sval, ext_state), TEST_STATE_5);
1893 4 : ASSERT_EQ (map.get_origin (x_sval, ext_state), z_sval);
1894 4 : ASSERT_EQ (map.get_origin (y_sval, ext_state), z_sval);
1895 4 : }
1896 :
1897 : /* Test equality and hashing. */
1898 4 : {
1899 4 : region_model model (node_mgr);
1900 4 : const svalue *y_sval = model.get_rvalue (y, nullptr);
1901 4 : const svalue *z_sval = model.get_rvalue (z, nullptr);
1902 :
1903 4 : sm_state_map map0 (borrowed_sm);
1904 4 : sm_state_map map1 (borrowed_sm);
1905 4 : sm_state_map map2 (borrowed_sm);
1906 :
1907 4 : ASSERT_EQ (map0.hash (), map1.hash ());
1908 4 : ASSERT_EQ (map0, map1);
1909 :
1910 4 : map1.impl_set_state (y_sval, TEST_STATE_5, z_sval, ext_state);
1911 4 : ASSERT_NE (map0.hash (), map1.hash ());
1912 4 : ASSERT_NE (map0, map1);
1913 :
1914 : /* Make the same change to map2. */
1915 4 : map2.impl_set_state (y_sval, TEST_STATE_5, z_sval, ext_state);
1916 4 : ASSERT_EQ (map1.hash (), map2.hash ());
1917 4 : ASSERT_EQ (map1, map2);
1918 4 : }
1919 :
1920 : /* Equality and hashing shouldn't depend on ordering. */
1921 4 : {
1922 4 : const state_machine::state test_state_2 ("test state 2", 2);
1923 4 : const state_machine::state_t TEST_STATE_2 = &test_state_2;
1924 4 : const state_machine::state test_state_3 ("test state 3", 3);
1925 4 : const state_machine::state_t TEST_STATE_3 = &test_state_3;
1926 4 : sm_state_map map0 (borrowed_sm);
1927 4 : sm_state_map map1 (borrowed_sm);
1928 4 : sm_state_map map2 (borrowed_sm);
1929 :
1930 4 : ASSERT_EQ (map0.hash (), map1.hash ());
1931 4 : ASSERT_EQ (map0, map1);
1932 :
1933 4 : region_model model (node_mgr);
1934 4 : const svalue *x_sval = model.get_rvalue (x, nullptr);
1935 4 : const svalue *y_sval = model.get_rvalue (y, nullptr);
1936 4 : const svalue *z_sval = model.get_rvalue (z, nullptr);
1937 :
1938 4 : map1.impl_set_state (x_sval, TEST_STATE_2, nullptr, ext_state);
1939 4 : map1.impl_set_state (y_sval, TEST_STATE_3, nullptr, ext_state);
1940 4 : map1.impl_set_state (z_sval, TEST_STATE_2, nullptr, ext_state);
1941 :
1942 4 : map2.impl_set_state (z_sval, TEST_STATE_2, nullptr, ext_state);
1943 4 : map2.impl_set_state (y_sval, TEST_STATE_3, nullptr, ext_state);
1944 4 : map2.impl_set_state (x_sval, TEST_STATE_2, nullptr, ext_state);
1945 :
1946 4 : ASSERT_EQ (map1.hash (), map2.hash ());
1947 4 : ASSERT_EQ (map1, map2);
1948 4 : }
1949 :
1950 : // TODO: coverage for purging
1951 4 : }
1952 :
1953 : /* Check program_state works as expected. */
1954 :
1955 : static void
1956 4 : test_program_state_1 ()
1957 : {
1958 : /* Create a program_state for a global ptr "p" that has
1959 : malloc sm-state, pointing to a region on the heap. */
1960 4 : tree p = build_global_decl ("p", ptr_type_node);
1961 :
1962 4 : std::unique_ptr<state_machine> sm = make_malloc_state_machine (nullptr);
1963 4 : const state_machine::state_t UNCHECKED_STATE
1964 4 : = sm->get_state_by_name ("unchecked");
1965 :
1966 4 : symbolic::node_manager node_mgr;
1967 4 : auto &mgr = node_mgr.get_region_model_manager ();
1968 4 : engine eng (node_mgr);
1969 4 : extrinsic_state ext_state (std::move (sm), &eng);
1970 4 : program_state s (ext_state);
1971 4 : region_model *model = s.m_region_model;
1972 4 : const svalue *size_in_bytes
1973 4 : = mgr.get_or_create_unknown_svalue (size_type_node);
1974 4 : const region *new_reg
1975 4 : = model->get_or_create_region_for_heap_alloc (size_in_bytes, nullptr);
1976 4 : const svalue *ptr_sval = mgr.get_ptr_svalue (ptr_type_node, new_reg);
1977 4 : model->set_value (model->get_lvalue (p, nullptr),
1978 : ptr_sval, nullptr);
1979 4 : sm_state_map *smap = s.m_checker_states[0];
1980 :
1981 4 : smap->impl_set_state (ptr_sval, UNCHECKED_STATE, nullptr, ext_state);
1982 4 : ASSERT_EQ (smap->get_state (ptr_sval, ext_state), UNCHECKED_STATE);
1983 4 : }
1984 :
1985 : /* Check that program_state works for string literals. */
1986 :
1987 : static void
1988 4 : test_program_state_2 ()
1989 : {
1990 : /* Create a program_state for a global ptr "p" that points to
1991 : a string constant. */
1992 4 : tree p = build_global_decl ("p", ptr_type_node);
1993 :
1994 4 : tree string_cst_ptr = build_string_literal (4, "foo");
1995 :
1996 4 : std::vector<std::unique_ptr<state_machine>> checkers;
1997 4 : symbolic::node_manager node_mgr;
1998 4 : engine eng (node_mgr);
1999 4 : extrinsic_state ext_state (std::move (checkers), &eng);
2000 :
2001 4 : program_state s (ext_state);
2002 4 : region_model *model = s.m_region_model;
2003 4 : const region *p_reg = model->get_lvalue (p, nullptr);
2004 4 : const svalue *str_sval = model->get_rvalue (string_cst_ptr, nullptr);
2005 4 : model->set_value (p_reg, str_sval, nullptr);
2006 4 : }
2007 :
2008 : /* Verify that program_states with identical sm-state can be merged,
2009 : and that the merged program_state preserves the sm-state. */
2010 :
2011 : static void
2012 4 : test_program_state_merging ()
2013 : {
2014 : /* Create a program_state for a global ptr "p" that has
2015 : malloc sm-state, pointing to a region on the heap. */
2016 4 : tree p = build_global_decl ("p", ptr_type_node);
2017 :
2018 4 : symbolic::node_manager node_mgr;
2019 4 : auto &mgr = node_mgr.get_region_model_manager ();
2020 4 : engine eng (node_mgr);
2021 4 : program_point point (program_point::origin (mgr));
2022 4 : extrinsic_state ext_state (make_malloc_state_machine (nullptr),
2023 4 : &eng);
2024 :
2025 4 : program_state s0 (ext_state);
2026 4 : uncertainty_t uncertainty;
2027 4 : impl_region_model_context ctxt (&s0, ext_state, &uncertainty);
2028 :
2029 4 : region_model *model0 = s0.m_region_model;
2030 4 : const svalue *size_in_bytes
2031 4 : = mgr.get_or_create_unknown_svalue (size_type_node);
2032 4 : const region *new_reg
2033 4 : = model0->get_or_create_region_for_heap_alloc (size_in_bytes, nullptr);
2034 4 : const svalue *ptr_sval = mgr.get_ptr_svalue (ptr_type_node, new_reg);
2035 4 : model0->set_value (model0->get_lvalue (p, &ctxt),
2036 : ptr_sval, &ctxt);
2037 4 : sm_state_map *smap = s0.m_checker_states[0];
2038 4 : const state_machine::state test_state ("test state", 0);
2039 4 : const state_machine::state_t TEST_STATE = &test_state;
2040 4 : smap->impl_set_state (ptr_sval, TEST_STATE, nullptr, ext_state);
2041 4 : ASSERT_EQ (smap->get_state (ptr_sval, ext_state), TEST_STATE);
2042 :
2043 4 : model0->canonicalize ();
2044 :
2045 : /* Verify that canonicalization preserves sm-state. */
2046 4 : ASSERT_EQ (smap->get_state (model0->get_rvalue (p, nullptr), ext_state),
2047 : TEST_STATE);
2048 :
2049 : /* Make a copy of the program_state. */
2050 4 : program_state s1 (s0);
2051 4 : ASSERT_EQ (s0, s1);
2052 :
2053 : /* We have two identical states with "p" pointing to a heap region
2054 : with the given sm-state.
2055 : They ought to be mergeable, preserving the sm-state. */
2056 4 : program_state merged (ext_state);
2057 4 : ASSERT_TRUE (s0.can_merge_with_p (s1, ext_state, point, &merged));
2058 4 : merged.validate (ext_state);
2059 :
2060 : /* Verify that the merged state has the sm-state for "p". */
2061 4 : region_model *merged_model = merged.m_region_model;
2062 4 : sm_state_map *merged_smap = merged.m_checker_states[0];
2063 4 : ASSERT_EQ (merged_smap->get_state (merged_model->get_rvalue (p, nullptr),
2064 : ext_state),
2065 : TEST_STATE);
2066 :
2067 : /* Try canonicalizing. */
2068 4 : merged.m_region_model->canonicalize ();
2069 4 : merged.validate (ext_state);
2070 :
2071 : /* Verify that the merged state still has the sm-state for "p". */
2072 4 : ASSERT_EQ (merged_smap->get_state (merged_model->get_rvalue (p, nullptr),
2073 : ext_state),
2074 : TEST_STATE);
2075 :
2076 : /* After canonicalization, we ought to have equality with the inputs. */
2077 4 : ASSERT_EQ (s0, merged);
2078 8 : }
2079 :
2080 : /* Verify that program_states with different global-state in an sm-state
2081 : can't be merged. */
2082 :
2083 : static void
2084 4 : test_program_state_merging_2 ()
2085 : {
2086 4 : symbolic::node_manager node_mgr;
2087 4 : auto &mgr = node_mgr.get_region_model_manager ();
2088 4 : engine eng (node_mgr);
2089 4 : program_point point (program_point::origin (mgr));
2090 4 : extrinsic_state ext_state (make_signal_state_machine (nullptr), &eng);
2091 :
2092 4 : const state_machine::state test_state_0 ("test state 0", 0);
2093 4 : const state_machine::state test_state_1 ("test state 1", 1);
2094 4 : const state_machine::state_t TEST_STATE_0 = &test_state_0;
2095 4 : const state_machine::state_t TEST_STATE_1 = &test_state_1;
2096 :
2097 4 : program_state s0 (ext_state);
2098 4 : {
2099 4 : sm_state_map *smap0 = s0.m_checker_states[0];
2100 4 : smap0->set_global_state (TEST_STATE_0);
2101 4 : ASSERT_EQ (smap0->get_global_state (), TEST_STATE_0);
2102 : }
2103 :
2104 4 : program_state s1 (ext_state);
2105 4 : {
2106 4 : sm_state_map *smap1 = s1.m_checker_states[0];
2107 4 : smap1->set_global_state (TEST_STATE_1);
2108 4 : ASSERT_EQ (smap1->get_global_state (), TEST_STATE_1);
2109 : }
2110 :
2111 4 : ASSERT_NE (s0, s1);
2112 :
2113 : /* They ought to not be mergeable. */
2114 4 : program_state merged (ext_state);
2115 4 : ASSERT_FALSE (s0.can_merge_with_p (s1, ext_state, point, &merged));
2116 4 : }
2117 :
2118 : /* Run all of the selftests within this file. */
2119 :
2120 : void
2121 4 : analyzer_program_state_cc_tests ()
2122 : {
2123 4 : test_sm_state_map ();
2124 4 : test_program_state_1 ();
2125 4 : test_program_state_2 ();
2126 4 : test_program_state_merging ();
2127 4 : test_program_state_merging_2 ();
2128 4 : }
2129 :
2130 : } // namespace selftest
2131 :
2132 : #endif /* CHECKING_P */
2133 :
2134 : } // namespace ana
2135 :
2136 : #endif /* #if ENABLE_ANALYZER */
|