Line data Source code
1 : /* Symbolic maps of bits e.g. "bytes [0-4): <bitvec>, bytes[16-20): <bitvec>"
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 "gimple-pretty-print.h"
24 : #include "tm.h"
25 :
26 : #include "diagnostics/markup.h"
27 :
28 : #include "selftest.h"
29 :
30 : #include "analyzer/symbolic-bit-contents.h"
31 : #include "analyzer/region-model.h"
32 : #include "analyzer/type.h"
33 : #include "analyzer/record-layout.h"
34 :
35 : #if ENABLE_ANALYZER
36 :
37 : namespace ana {
38 : namespace symbolic {
39 :
40 : bool
41 18467734 : baseline_key::operator< (const baseline_key &other) const
42 : {
43 18467734 : if (int c = node::cmp (*m_cluster_reg, *other.m_cluster_reg))
44 14528830 : return c < 0;
45 3938904 : return m_baseline_state < other.m_baseline_state;
46 : }
47 :
48 : // class bit_contents : public symbolic::node
49 :
50 : const bit_vector &
51 764 : bit_contents::get_bytes (const symbolic_byte_range &byte_range) const
52 : {
53 764 : return get_bits (bits_from_bytes (byte_range));
54 : }
55 :
56 : const bit_vector &
57 1776685 : bit_contents::get_bits (const concrete_bit_range &bit_range) const
58 : {
59 1776685 : return get_bits (symbolic_bit_range (bit_range,
60 1776685 : m_node_mgr.get_scalars ().get_bits ()));
61 : }
62 :
63 : const bit_vector &
64 764 : bit_contents::get_bytes (const concrete_byte_range &byte_range) const
65 : {
66 764 : return get_bytes
67 764 : (symbolic_byte_range (byte_range,
68 764 : m_node_mgr.get_scalars ().get_bytes ()));
69 : }
70 :
71 : const bit_contents &
72 306396 : bit_contents::set_bits (const symbolic_bit_range &bit_range,
73 : const bit_vector &bv,
74 : uncertainty_t *uncertainty) const
75 : {
76 306396 : auto &mgr = get_bit_contents_manager ();
77 306396 : return mgr.set_bits (*this, bit_range, bv, uncertainty);
78 : }
79 :
80 : const bit_contents &
81 683 : bit_contents::set_bits (const concrete_bit_range &conc_bit_range,
82 : const bit_vector &bv,
83 : uncertainty_t *uncertainty) const
84 : {
85 683 : return set_bits
86 683 : (symbolic_bit_range (conc_bit_range,
87 683 : m_node_mgr.get_scalars ().get_bits ()),
88 : bv,
89 683 : uncertainty);
90 : }
91 :
92 : const bit_contents &
93 284 : bit_contents::set_bytes (const symbolic_byte_range &byte_range,
94 : const bit_vector &bv,
95 : uncertainty_t *uncertainty) const
96 : {
97 284 : return set_bits (bits_from_bytes (byte_range), bv, uncertainty);
98 : }
99 :
100 : const bit_contents &
101 240 : bit_contents::set_bytes (const concrete_byte_range &byte_range,
102 : const bit_vector &bv,
103 : uncertainty_t *uncertainty) const
104 : {
105 240 : return set_bytes
106 240 : (symbolic_byte_range (byte_range,
107 240 : m_node_mgr.get_scalars ().get_bytes ()),
108 : bv,
109 240 : uncertainty);
110 : }
111 :
112 : const bit_contents &
113 76 : bit_contents::set_byte (integer_with_unit<units::byte> byte_offset,
114 : uint8_t value,
115 : uncertainty_t *uncertainty) const
116 : {
117 76 : auto &bv_mgr = get_bit_vector_manager ();
118 228 : return set_bytes (concrete_byte_range (byte_offset, 1_byte),
119 : bv_mgr.get_uint8 (value),
120 76 : uncertainty);
121 : }
122 :
123 : const bit_contents *
124 440947 : bit_contents::can_merge_p (const bit_contents &bit_contents_a,
125 : const bit_contents &bit_contents_b,
126 : tree type,
127 : model_merger *merger)
128 : {
129 440947 : if (&bit_contents_a == &bit_contents_b)
130 : return &bit_contents_a;
131 :
132 112684 : auto &bit_contents_mgr = bit_contents_a.get_bit_contents_manager ();
133 :
134 : /* Attempt mergers of concrete_bit_overlay, and with their baselines. */
135 112684 : auto *conc_bit_overlay_a = bit_contents_a.dyn_cast_concrete_bit_overlay ();
136 112684 : auto *conc_bit_overlay_b = bit_contents_b.dyn_cast_concrete_bit_overlay ();
137 112684 : if (conc_bit_overlay_a)
138 : {
139 73876 : if (conc_bit_overlay_b)
140 : {
141 68558 : if (&conc_bit_overlay_a->get_baseline ()
142 68558 : == &conc_bit_overlay_b->get_baseline ())
143 62393 : return concrete_bit_overlay::can_merge_p
144 62393 : (bit_contents_a,
145 : bit_contents_b,
146 : conc_bit_overlay_a->get_baseline (),
147 : conc_bit_overlay_a,
148 : conc_bit_overlay_b,
149 : type,
150 62393 : merger);
151 : }
152 : else
153 : {
154 5318 : if (&conc_bit_overlay_a->get_baseline () == &bit_contents_b)
155 2387 : return concrete_bit_overlay::can_merge_p
156 2387 : (bit_contents_a,
157 : bit_contents_b,
158 : bit_contents_b,
159 : conc_bit_overlay_a,
160 : nullptr,
161 : type,
162 2387 : merger);
163 : }
164 : }
165 : else
166 : {
167 38808 : if (conc_bit_overlay_b)
168 : {
169 8454 : if (&conc_bit_overlay_b->get_baseline () == &bit_contents_a)
170 1595 : return concrete_bit_overlay::can_merge_p
171 1595 : (bit_contents_a,
172 : bit_contents_b,
173 : bit_contents_a,
174 : nullptr,
175 : conc_bit_overlay_b,
176 : type,
177 1595 : merger);
178 : }
179 : else
180 : {
181 : /* Neither are concrete bit overlays. */
182 : }
183 : }
184 :
185 : /* Allow mergers of e.g. conjured_bit_vector. */
186 46309 : value_set maybe_live_in_a = bit_contents_a.get_maybe_live_values ();
187 46309 : value_set maybe_live_in_b = bit_contents_b.get_maybe_live_values ();
188 46309 : value_set maybe_live_in_either = maybe_live_in_a.union_ (maybe_live_in_b);
189 46309 : return &bit_contents_mgr.get_untracked (std::move (maybe_live_in_either));
190 46309 : }
191 :
192 : value_set
193 93499 : bit_contents::get_maybe_live_values () const
194 : {
195 93499 : value_set maybe_live;
196 93499 : this->for_each_value ([] (const symbolic::value_node &val,
197 : void *user_data) -> iter_result {
198 : value_set *maybe_live = static_cast<value_set *> (user_data);
199 : maybe_live->add (&val);
200 : return iter_result::keep_going;
201 : }, &maybe_live);
202 93499 : return maybe_live;
203 : }
204 :
205 : // class uniform_bit_contents : public bit_contents
206 :
207 : const bit_contents *
208 12 : uniform_bit_contents::maybe_fold_offset (const symbolic_bit_offset &) const
209 : {
210 : /* Ignore the offset and fold to this bit_contents. */
211 12 : return this;
212 : }
213 :
214 : iter_result
215 3386920 : uniform_bit_contents::
216 : for_each_value (iter_result (*) (const symbolic::value_node &val,
217 : void *user_data),
218 : void *) const
219 : {
220 : // No-op
221 3386920 : return iter_result::keep_going;
222 : }
223 :
224 : // class uninit_contents : public uniform_bit_contents
225 :
226 4495 : uninit_contents::uninit_contents (node_manager &node_mgr)
227 4495 : : uniform_bit_contents (complexity (1, 1), node_mgr)
228 : {
229 4495 : }
230 :
231 : const bit_vector &
232 1619533 : uninit_contents::get_bits (const symbolic_bit_range &bit_range) const
233 : {
234 1619533 : return get_bit_vector_manager ().get_uninitialized_bits
235 1619533 : (bit_range.get_size ());
236 : }
237 :
238 : void
239 1375 : uninit_contents::print_dump_widget_label (pretty_printer *pp) const
240 : {
241 1375 : pp_string (pp, "uninitialized contents");
242 1375 : }
243 :
244 : void
245 514 : uninit_contents::add_dump_widget_children (text_art::tree_widget &,
246 : node_dump_state &) const
247 : {
248 : // no-op
249 514 : }
250 :
251 : void
252 17867 : uninit_contents::accept_node_visitor (node_visitor &v) const
253 : {
254 17867 : v.visit_bit_contents (*this);
255 17867 : }
256 :
257 : std::unique_ptr<json::object>
258 4 : uninit_contents::to_json () const
259 : {
260 4 : return make_json_obj ("uninit_contents");
261 : }
262 :
263 : std::unique_ptr<diagnostics::markup::node>
264 0 : uninit_contents::make_markup_node (markup_context &) const
265 : {
266 0 : return diagnostics::markup::make_text_node ("uninit_contents");
267 : }
268 :
269 : string_scan
270 397 : uninit_contents::
271 : scan_for_first_zero_byte (string_scan::byte_offset_type start_byte_offset) const
272 : {
273 397 : return string_scan::found_poison (start_byte_offset, poison_kind::uninit);
274 : }
275 :
276 : void
277 30 : uninit_contents::
278 : get_representative_path_vars (const region_model *,
279 : value_set *,
280 : const cluster_region &,
281 : const svalue *,
282 : tree,
283 : logger *,
284 : auto_vec<path_var> *) const
285 : {
286 : // no-op
287 30 : }
288 :
289 : const bit_contents &
290 552 : uninit_contents::
291 : poison_any_pointers_to_descendents (const region *,
292 : enum poison_kind) const
293 : {
294 : // no-op
295 552 : return *this;
296 : }
297 :
298 : const bit_contents &
299 1422 : uninit_contents::purge_state_involving (const symbolic::node &) const
300 : {
301 : // no-op
302 1422 : return *this;
303 : }
304 :
305 : // class untracked_bit_contents : public uniform_bit_contents
306 :
307 : bool
308 482065 : untracked_bit_contents::key::operator< (const key &other) const
309 : {
310 482065 : return m_maybe_live < other.m_maybe_live;
311 : }
312 :
313 : complexity
314 3471 : untracked_bit_contents::key::get_complexity () const
315 : {
316 3471 : complexity child_complexities (0, 0);
317 :
318 10382 : for (auto iter : m_maybe_live)
319 6911 : child_complexities += iter->get_complexity ();
320 :
321 : /* One extra node for the node itself; one more level of depth. */
322 3471 : return complexity (child_complexities.m_num_nodes + 1,
323 3471 : child_complexities.m_max_depth + 1);
324 : }
325 :
326 3471 : untracked_bit_contents::untracked_bit_contents (node_manager &node_mgr,
327 : const key &k)
328 : : uniform_bit_contents (k.get_complexity (), node_mgr),
329 3471 : m_maybe_live (k.m_maybe_live)
330 : {
331 3471 : if (CHECKING_P)
332 10382 : for (auto iter : m_maybe_live)
333 6911 : gcc_assert (track_value_liveness_p (*iter));
334 3471 : }
335 :
336 : const bit_vector &
337 86144 : untracked_bit_contents::get_bits (const symbolic_bit_range &bit_range) const
338 : {
339 86144 : return get_bit_vector_manager ().get_unknown_bits (bit_range.get_size ());
340 : }
341 :
342 : void
343 160 : untracked_bit_contents::print_dump_widget_label (pretty_printer *pp) const
344 : {
345 160 : pp_string (pp, "untracked_bit_contents");
346 160 : }
347 :
348 : void
349 160 : untracked_bit_contents::add_dump_widget_children (text_art::tree_widget &w,
350 : node_dump_state &nds) const
351 : {
352 160 : if (!m_maybe_live.empty ())
353 : {
354 5 : using tree_widget = text_art::tree_widget;
355 5 : std::unique_ptr<tree_widget> maybe_live_w
356 5 : (tree_widget::from_fmt (nds, nullptr, "maybe live values"));
357 15 : for (auto iter : m_maybe_live)
358 20 : maybe_live_w->add_child (iter->make_dump_widget (nds));
359 10 : w.add_child (std::move (maybe_live_w));
360 5 : }
361 160 : }
362 :
363 : void
364 19913 : untracked_bit_contents::accept_node_visitor (node_visitor &v) const
365 : {
366 19913 : if (v.visit_bit_contents (*this))
367 45355 : for (auto iter : m_maybe_live)
368 25442 : iter->accept_node_visitor (v);
369 19913 : }
370 :
371 : std::unique_ptr<json::object>
372 0 : untracked_bit_contents::to_json () const
373 : {
374 0 : return make_json_obj ("untracked_bit_contents");
375 : }
376 :
377 : std::unique_ptr<diagnostics::markup::node>
378 0 : untracked_bit_contents::make_markup_node (markup_context &) const
379 : {
380 0 : return diagnostics::markup::make_text_node ("untracked_bit_contents");
381 : }
382 :
383 : string_scan
384 285 : untracked_bit_contents::
385 : scan_for_first_zero_byte (string_scan::byte_offset_type start_byte_offset) const
386 : {
387 285 : return string_scan::cannot_scan (start_byte_offset);
388 : }
389 :
390 : void
391 1046 : untracked_bit_contents::
392 : get_representative_path_vars (const region_model *,
393 : value_set *,
394 : const cluster_region &,
395 : const svalue *,
396 : tree,
397 : logger *,
398 : auto_vec<path_var> *) const
399 : {
400 : // no-op
401 1046 : }
402 :
403 : const bit_contents &
404 12205 : untracked_bit_contents::
405 : poison_any_pointers_to_descendents (const region *,
406 : enum poison_kind) const
407 : {
408 : // no-op
409 12205 : return *this;
410 : }
411 :
412 : const bit_contents &
413 18009 : untracked_bit_contents::purge_state_involving (const symbolic::node &n) const
414 : {
415 18009 : value_set survivors;
416 43698 : for (auto iter : m_maybe_live)
417 25689 : if (!iter->involves_p (n))
418 25620 : survivors.add (iter);
419 :
420 18009 : return get_bit_contents_manager ().get_untracked (std::move (survivors));
421 18009 : }
422 :
423 : bool
424 523399 : untracked_bit_contents::bits_live_p (const bit_vector_from_ubc &) const
425 : {
426 523399 : return false;
427 : }
428 :
429 : iter_result
430 883792 : untracked_bit_contents::
431 : for_each_value (iter_result (*cb) (const symbolic::value_node &val,
432 : void *user_data),
433 : void *user_data) const
434 : {
435 1839449 : for (auto iter : m_maybe_live)
436 955657 : if (cb (*iter, user_data) == iter_result::stop)
437 883792 : return iter_result::stop;
438 :
439 : return iter_result::keep_going;
440 : }
441 :
442 : bool
443 195899 : untracked_bit_contents::track_value_liveness_p (const symbolic::value_node &val)
444 : {
445 195899 : if (auto *sval = val.dyn_cast_svalue ())
446 108073 : return track_svalue_liveness_p (*sval);
447 87826 : else if (auto *bv = val.dyn_cast_bit_vector ())
448 87826 : return track_bit_vector_liveness_p (*bv);
449 0 : gcc_unreachable ();
450 : }
451 :
452 : bool
453 151073 : untracked_bit_contents::track_svalue_liveness_p (const svalue &sval)
454 : {
455 151073 : switch (sval.get_kind ())
456 : {
457 0 : default:
458 0 : gcc_unreachable ();
459 :
460 : case SK_CONSTANT:
461 : case SK_UNKNOWN:
462 : case SK_POISONED:
463 : return false;
464 :
465 116605 : case SK_POINTER:
466 116605 : case SK_SETJMP:
467 116605 : case SK_INITIAL:
468 116605 : case SK_UNARYOP:
469 116605 : case SK_BINOP:
470 116605 : case SK_UNMERGEABLE:
471 116605 : case SK_PLACEHOLDER:
472 116605 : case SK_WIDENING:
473 116605 : case SK_ASM_OUTPUT:
474 116605 : case SK_CONST_FN_RESULT:
475 116605 : return true;
476 :
477 21741 : case SK_BIT_VECTOR:
478 21741 : {
479 21741 : auto &from_bv = static_cast<const svalue_from_bit_vector &> (sval);
480 21741 : return track_bit_vector_liveness_p (from_bv.get_bit_vector ());
481 : }
482 : break;
483 : }
484 :
485 : gcc_unreachable ();
486 : }
487 :
488 : bool
489 109567 : untracked_bit_contents::
490 : track_bit_vector_liveness_p (const symbolic::bit_vector &bv)
491 : {
492 110704 : switch (bv.get_kind ())
493 : {
494 0 : default:
495 0 : gcc_unreachable ();
496 :
497 : case bit_vector::kind::empty:
498 : case bit_vector::kind::poisoned:
499 : case bit_vector::kind::tristate:
500 : return false;
501 :
502 : case bit_vector::kind::conjured:
503 : case bit_vector::kind::from_ubc:
504 : case bit_vector::kind::named_test:
505 : return true;
506 :
507 0 : case bit_vector::kind::concat:
508 0 : {
509 0 : auto &concat_bv = static_cast<const concat_bit_vector &> (bv);
510 0 : for (auto iter : concat_bv)
511 0 : if (track_bit_vector_liveness_p (*iter))
512 54230 : return true;
513 : return false;
514 : }
515 268 : break;
516 :
517 268 : case bit_vector::kind::cropped:
518 268 : {
519 268 : auto &cropped_bv = static_cast<const cropped_bit_vector &> (bv);
520 268 : return track_bit_vector_liveness_p
521 268 : (cropped_bv.get_uncropped_bit_vector ());
522 : }
523 869 : break;
524 :
525 869 : case bit_vector::kind::repeated:
526 869 : {
527 869 : auto &repeated_bv = static_cast<const repeated_bit_vector &> (bv);
528 869 : return track_bit_vector_liveness_p
529 869 : (repeated_bv.get_inner_bit_vector ());
530 : }
531 43000 : break;
532 :
533 43000 : case bit_vector::kind::svalue:
534 43000 : {
535 43000 : auto &from_sval = static_cast<const bit_vector_from_svalue &> (bv);
536 43000 : return track_svalue_liveness_p (from_sval.get_svalue ());
537 : }
538 : break;
539 : }
540 :
541 : gcc_unreachable ();
542 : }
543 :
544 : // class zero_contents : public uniform_bit_contents
545 :
546 4495 : zero_contents::zero_contents (node_manager &node_mgr)
547 4495 : : uniform_bit_contents (complexity (1, 1), node_mgr)
548 : {
549 4495 : }
550 :
551 : const bit_vector &
552 3133 : zero_contents::get_bits (const symbolic_bit_range &bit_range) const
553 : {
554 3133 : return get_bit_vector_manager ().get_all_zeros (bit_range.get_size ());
555 : }
556 :
557 : void
558 0 : zero_contents::print_dump_widget_label (pretty_printer *pp) const
559 : {
560 0 : pp_string (pp, "zero contents");
561 0 : }
562 :
563 : void
564 0 : zero_contents::add_dump_widget_children (text_art::tree_widget &,
565 : node_dump_state &) const
566 : {
567 : // no-op
568 0 : }
569 :
570 : void
571 9 : zero_contents::accept_node_visitor (node_visitor &v) const
572 : {
573 9 : v.visit_bit_contents (*this);
574 9 : }
575 :
576 : std::unique_ptr<json::object>
577 4 : zero_contents::to_json () const
578 : {
579 4 : return make_json_obj ("zero_contents");
580 : }
581 :
582 : std::unique_ptr<diagnostics::markup::node>
583 0 : zero_contents::make_markup_node (markup_context &) const
584 : {
585 0 : return diagnostics::markup::make_text_node ("zero_contents");
586 : }
587 :
588 : string_scan
589 88 : zero_contents::
590 : scan_for_first_zero_byte (string_scan::byte_offset_type start_byte_offset) const
591 : {
592 88 : return string_scan::found_nul (start_byte_offset);
593 : }
594 :
595 : void
596 0 : zero_contents::
597 : get_representative_path_vars (const region_model *,
598 : value_set *,
599 : const cluster_region &,
600 : const svalue *,
601 : tree,
602 : logger *,
603 : auto_vec<path_var> *) const
604 : {
605 : // no-op
606 0 : }
607 :
608 : const bit_contents &
609 14 : zero_contents::
610 : poison_any_pointers_to_descendents (const region *,
611 : enum poison_kind) const
612 : {
613 : // no-op
614 14 : return *this;
615 : }
616 :
617 : const bit_contents &
618 1 : zero_contents::purge_state_involving (const symbolic::node &) const
619 : {
620 : // no-op
621 1 : return *this;
622 : }
623 :
624 : // class unfoldable_bit_contents : public bit_contents
625 :
626 : const symbolic::bit_vector &
627 348608 : unfoldable_bit_contents::get_bits (const symbolic_bit_range &bit_range) const
628 : {
629 348608 : auto &bv_mgr = get_bit_vector_manager ();
630 348608 : if (bit_range.get_size ().is_zero_p ())
631 30 : return bv_mgr.get_empty ();
632 :
633 348578 : auto iter = m_range_to_bv_map.find (bit_range);
634 348578 : if (iter != m_range_to_bv_map.end ())
635 333917 : return *(iter->second);
636 :
637 14661 : auto bv_up
638 14661 : = std::make_unique<symbolic::bit_vector_from_ubc> (*this, bit_range);
639 14661 : auto &result = *bv_up;
640 14661 : m_range_to_bv_map.emplace (bit_range, std::move (bv_up));
641 14661 : return result;
642 14661 : }
643 :
644 : string_scan
645 2615 : unfoldable_bit_contents::
646 : scan_for_first_zero_byte (string_scan::byte_offset_type start_byte_offset) const
647 : {
648 2615 : return string_scan::cannot_scan (start_byte_offset);
649 : }
650 :
651 : bool
652 114570 : unfoldable_bit_contents::
653 : bits_live_p (const symbolic::bit_vector_from_ubc &bv_from_ubc) const
654 : {
655 114570 : return &bv_from_ubc.get_bit_contents () == this;
656 : }
657 :
658 : /* Identify when SVAL is a cropping from this unfoldable_bit_contents,
659 : reporting it to OUT_PVS as the appropriate subregion of CLUSTER_REG. */
660 :
661 : void
662 4740 : unfoldable_bit_contents::
663 : get_representative_path_vars (const region_model *model,
664 : value_set *visited,
665 : const cluster_region &cluster_reg,
666 : const svalue *sval,
667 : tree desired_type,
668 : logger *logger,
669 : auto_vec<path_var> *out_pvs) const
670 : {
671 4740 : auto *from_bv = sval->dyn_cast_svalue_from_bit_vector ();
672 4740 : if (!from_bv)
673 4581 : return;
674 :
675 2168 : auto &bv = from_bv->get_bit_vector ();
676 2168 : auto *from_bc = bv.dyn_cast_bit_vector_from_ubc ();
677 2168 : if (!from_bc)
678 : return;
679 :
680 375 : if (&from_bc->get_bit_contents () != this)
681 : return;
682 :
683 218 : symbolic::memory_range mem_range (cluster_reg, from_bc->get_bit_range ());
684 218 : auto *subreg = model->maybe_get_region_for_mem_range (mem_range,
685 : desired_type,
686 : logger);
687 218 : if (!subreg)
688 : return;
689 :
690 218 : auto *cast_reg
691 218 : = model->get_manager ()->get_cast_region (subreg, desired_type);
692 218 : path_var pv = model->get_representative_path_var (cast_reg, visited, logger);
693 218 : if (!pv)
694 : return;
695 :
696 159 : out_pvs->safe_push (pv);
697 : }
698 :
699 : // class baseline_bit_contents : public unfoldable_bit_contents
700 :
701 8038 : baseline_bit_contents::
702 : baseline_bit_contents (node_manager &node_mgr, const baseline_key &bk)
703 8038 : : symbolic::unfoldable_bit_contents (complexity (bk.m_cluster_reg), node_mgr),
704 8038 : m_baseline_key (bk)
705 : {
706 8038 : gcc_assert (!m_baseline_key.region_has_meaningful_size_p (node_mgr));
707 8038 : }
708 :
709 : void
710 242 : baseline_bit_contents::print_dump_widget_label (pretty_printer *pp) const
711 : {
712 242 : pp_printf (pp, "baseline_bit_contents");
713 242 : }
714 :
715 : void
716 205 : baseline_bit_contents::add_dump_widget_children (text_art::tree_widget &w,
717 : node_dump_state &nds) const
718 : {
719 410 : w.add_child (m_baseline_key.m_cluster_reg->make_dump_widget (nds, "cluster_reg"));
720 205 : }
721 :
722 : void
723 3985657 : baseline_bit_contents::accept_node_visitor (node_visitor &v) const
724 : {
725 3985657 : if (v.visit_bit_contents (*this))
726 3985657 : m_baseline_key.m_cluster_reg->accept_node_visitor (v);
727 3985657 : }
728 :
729 : std::unique_ptr<json::object>
730 14 : baseline_bit_contents::to_json () const
731 : {
732 14 : auto obj = make_json_obj ("baseline_bit_contents");
733 14 : obj->set ("baseline_key", m_baseline_key.to_json ());
734 14 : return obj;
735 : }
736 :
737 : std::unique_ptr<diagnostics::markup::node>
738 0 : baseline_bit_contents::make_markup_node (markup_context &) const
739 : {
740 0 : return diagnostics::markup::make_text_node ("baseline_bit_contents");
741 : }
742 :
743 : const bit_contents &
744 8036 : baseline_bit_contents::
745 : poison_any_pointers_to_descendents (const region *reg,
746 : enum poison_kind) const
747 : {
748 8036 : if (reg == m_baseline_key.m_cluster_reg)
749 : {
750 : // TODO
751 : }
752 8036 : return *this;
753 : }
754 :
755 : const bit_contents &
756 7763 : baseline_bit_contents::purge_state_involving (const symbolic::node &) const
757 : {
758 7763 : return *this;
759 : }
760 :
761 : iter_result
762 515976 : baseline_bit_contents::
763 : for_each_value (iter_result (*) (const symbolic::value_node &val,
764 : void *user_data),
765 : void *) const
766 : {
767 : // No-op
768 515976 : return iter_result::keep_going;
769 : }
770 :
771 : // class conjured_bit_contents : public unfoldable_bit_contents
772 :
773 : bool
774 273979 : conjured_bit_contents::key::operator< (const key &other) const
775 : {
776 273979 : if (int c = m_stmt->uid - other.m_stmt->uid)
777 188073 : return c < 0;
778 85906 : if (int c = node::cmp (*m_id_reg, *other.m_id_reg))
779 59428 : return c < 0;
780 26478 : return m_idx < other.m_idx;
781 : }
782 :
783 : complexity
784 12264 : conjured_bit_contents::key::get_complexity () const
785 : {
786 12264 : return complexity (m_id_reg);
787 : }
788 :
789 12264 : conjured_bit_contents::
790 : conjured_bit_contents (node_manager &node_mgr, const key &k)
791 : : symbolic::unfoldable_bit_contents (k.get_complexity (), node_mgr),
792 12264 : m_stmt (k.m_stmt),
793 12264 : m_id_reg (k.m_id_reg),
794 12264 : m_idx (k.m_idx)
795 : {
796 12264 : }
797 :
798 : void
799 930 : conjured_bit_contents::print_dump_widget_label (pretty_printer *pp) const
800 : {
801 930 : pp_printf (pp, "conjured_bit_contents (");
802 930 : pp_gimple_stmt_1 (pp, m_stmt, 0, (dump_flags_t)0);
803 930 : if (m_idx != 0)
804 0 : pp_printf (pp, ", %i", m_idx);
805 930 : pp_character (pp, ')');
806 930 : }
807 :
808 : void
809 663 : conjured_bit_contents::add_dump_widget_children (text_art::tree_widget &w,
810 : node_dump_state &nds) const
811 : {
812 1326 : w.add_child (m_id_reg->make_dump_widget (nds, "id_reg"));
813 663 : }
814 :
815 : void
816 10842423 : conjured_bit_contents::accept_node_visitor (node_visitor &v) const
817 : {
818 10842423 : if (v.visit_bit_contents (*this))
819 10842423 : m_id_reg->accept_node_visitor (v);
820 10842423 : }
821 :
822 : std::unique_ptr<json::object>
823 0 : conjured_bit_contents::to_json () const
824 : {
825 0 : auto obj = make_json_obj ("conjured_bit_contents");
826 :
827 0 : {
828 0 : pretty_printer pp;
829 0 : pp_format_decoder (&pp) = default_tree_printer;
830 0 : pp_gimple_stmt_1 (&pp, m_stmt, 0, (dump_flags_t)0);
831 0 : obj->set_string ("stmt", pp_formatted_text (&pp));
832 0 : }
833 0 : obj->set ("id_reg", m_id_reg->to_json ());
834 0 : obj->set_integer ("id", m_idx);
835 :
836 0 : return obj;
837 : }
838 :
839 : std::unique_ptr<diagnostics::markup::node>
840 0 : conjured_bit_contents::make_markup_node (markup_context &) const
841 : {
842 0 : return diagnostics::markup::make_text_node ("conjured_bit_contents");
843 : }
844 :
845 : const bit_contents &
846 11426 : conjured_bit_contents::
847 : poison_any_pointers_to_descendents (const region *reg,
848 : enum poison_kind) const
849 : {
850 11426 : if (reg == m_id_reg)
851 : {
852 : // TODO
853 : }
854 11426 : return *this;
855 : }
856 :
857 : const bit_contents &
858 46631 : conjured_bit_contents::purge_state_involving (const symbolic::node &) const
859 : {
860 46631 : return *this;
861 : }
862 :
863 : iter_result
864 354083 : conjured_bit_contents::
865 : for_each_value (iter_result (*) (const symbolic::value_node &val,
866 : void *user_data),
867 : void *) const
868 : {
869 : // No-op
870 354083 : return iter_result::keep_going;
871 : }
872 :
873 : #if CHECKING_P
874 :
875 : // class named_test_bit_contents : public unfoldable_bit_contents
876 :
877 64 : named_test_bit_contents::named_test_bit_contents (node_manager &node_mgr,
878 : std::string name)
879 64 : : unfoldable_bit_contents (complexity (1, 1), node_mgr),
880 64 : m_name (std::move (name))
881 : {
882 64 : }
883 :
884 : void
885 4 : named_test_bit_contents::print_dump_widget_label (pretty_printer *pp) const
886 : {
887 4 : pp_printf (pp, "named_test_bit_contents: %s", m_name.c_str ());
888 4 : }
889 :
890 : void
891 4 : named_test_bit_contents::
892 : add_dump_widget_children (text_art::tree_widget &,
893 : node_dump_state &) const
894 : {
895 : // no-op
896 4 : }
897 :
898 : void
899 0 : named_test_bit_contents::accept_node_visitor (node_visitor &v) const
900 : {
901 0 : v.visit_bit_contents (*this);
902 0 : }
903 :
904 : std::unique_ptr<json::object>
905 12 : named_test_bit_contents::to_json () const
906 : {
907 12 : auto obj = make_json_obj ("named_test_bit_contents");
908 24 : obj->set_string ("name", m_name);
909 12 : return obj;
910 : }
911 :
912 : std::unique_ptr<diagnostics::markup::node>
913 0 : named_test_bit_contents::make_markup_node (markup_context &) const
914 : {
915 0 : return diagnostics::markup::make_text_node (m_name);
916 : }
917 :
918 : const bit_contents &
919 0 : named_test_bit_contents::
920 : poison_any_pointers_to_descendents (const region *,
921 : enum poison_kind) const
922 : {
923 : // no-op
924 0 : return *this;
925 : }
926 :
927 : const bit_contents &
928 0 : named_test_bit_contents::purge_state_involving (const symbolic::node &) const
929 :
930 : {
931 : // no-op
932 0 : return *this;
933 : }
934 :
935 : iter_result
936 4 : named_test_bit_contents::
937 : for_each_value (iter_result (*) (const symbolic::value_node &val,
938 : void *user_data),
939 : void *) const
940 : {
941 : // No-op
942 4 : return iter_result::keep_going;
943 : }
944 :
945 : #endif /* #if CHECKING_P */
946 :
947 : // class concrete_bit_overlay : public bit_overlay
948 :
949 : bool
950 5802585 : concrete_bit_overlay::key::
951 : operator< (const concrete_bit_overlay::key &other) const
952 : {
953 5802585 : if (int c = node::cmp (*m_baseline, *other.m_baseline))
954 276380 : return c < 0;
955 5526205 : if (m_concrete_range_map.size () != other.m_concrete_range_map.size ())
956 246086 : return m_concrete_range_map.size () < other.m_concrete_range_map.size ();
957 :
958 5280119 : auto iter_a = m_concrete_range_map.begin ();
959 5280119 : auto iter_b = other.m_concrete_range_map.begin ();
960 :
961 6659832 : while (iter_a != m_concrete_range_map.end ())
962 : {
963 5449484 : if (int c = node::cmp (iter_a->second, iter_b->second))
964 4066946 : return c < 0;
965 1382538 : if (int c = concrete_bit_range::cmp (iter_a->first, iter_b->first))
966 2825 : return c < 0;
967 1379713 : ++iter_a;
968 1379713 : ++iter_b;
969 : }
970 :
971 : return false;
972 : }
973 :
974 : complexity
975 78491 : concrete_bit_overlay::key::get_complexity () const
976 : {
977 78491 : complexity child_complexities = m_baseline->get_complexity ();
978 170485 : for (auto iter : m_concrete_range_map)
979 183988 : child_complexities += iter.second.get_complexity ();
980 78491 : return complexity::from_child_complexities (child_complexities);
981 : }
982 :
983 : /* Canonicalize by removing bindings where the bit_vector matches the
984 : baseline's bit_vector. */
985 :
986 : void
987 1370747 : concrete_bit_overlay::key::remove_redundant_bindings ()
988 : {
989 1370747 : for (auto iter = m_concrete_range_map.begin ();
990 2869048 : iter != m_concrete_range_map.end ();
991 : )
992 : {
993 1498301 : auto &conc_bit_range = iter->first;
994 1498301 : auto &new_bv = iter->second;
995 1498301 : auto &existing_bv = m_baseline->get_bits (conc_bit_range);
996 1498301 : if (&existing_bv == &new_bv)
997 4165 : iter = m_concrete_range_map.erase (iter);
998 : else
999 1494136 : ++iter;
1000 : }
1001 1370747 : }
1002 :
1003 78491 : concrete_bit_overlay::concrete_bit_overlay (const key &k)
1004 78491 : : bit_overlay (k.get_complexity (), *k.m_baseline),
1005 78491 : m_concrete_range_map (k.m_concrete_range_map)
1006 : {
1007 78491 : if (CHECKING_P)
1008 : {
1009 78491 : gcc_assert (!m_concrete_range_map.empty ());
1010 :
1011 : /* Avoid nesting of overlays. */
1012 78491 : gcc_assert
1013 : (get_baseline ().get_kind () != bit_contents::kind::concrete_overlay
1014 : && get_baseline ().get_kind () != bit_contents::kind::symbolic_overlay);
1015 :
1016 78491 : for (auto iter = m_concrete_range_map.begin ();
1017 170485 : iter != m_concrete_range_map.end ();
1018 91994 : ++iter)
1019 : {
1020 91994 : auto &conc_bit_range = iter->first;
1021 91994 : auto &overlay_bv = iter->second;
1022 :
1023 91994 : auto next = iter;
1024 91994 : ++next;
1025 :
1026 : // Check for ordering and non-overlapping bit ranges
1027 91994 : if (next != m_concrete_range_map.end ())
1028 : {
1029 13503 : auto &next_conc_bit_range = next->first;
1030 27006 : gcc_assert (conc_bit_range.get_start_offset ()
1031 : < next_conc_bit_range.get_start_offset ());
1032 27006 : gcc_assert (conc_bit_range.get_last_offset ()
1033 : < next_conc_bit_range.get_start_offset ());
1034 : }
1035 :
1036 : /* Must not have redundant bindings. */
1037 91994 : auto &baseline_bv = get_baseline ().get_bits (conc_bit_range);
1038 91994 : gcc_assert (&overlay_bv != &baseline_bv);
1039 :
1040 : /* The size of each bit_vector must be concrete and match that of
1041 : the bit range it is bound to. */
1042 91994 : concrete_bit_size conc_bv_size;
1043 91994 : bool got_conc_bv_size
1044 91994 : = overlay_bv.maybe_get_concrete_size_in_bits (&conc_bv_size);
1045 91994 : gcc_assert (got_conc_bv_size);
1046 91994 : gcc_assert (conc_bv_size == conc_bit_range.get_size ());
1047 :
1048 91994 : if (auto *concat_bv = overlay_bv.dyn_cast_concat_bit_vector ())
1049 : {
1050 : /* Check for no leading concrete-sized child elements. */
1051 0 : auto first_child_bv = concat_bv->begin ();
1052 0 : gcc_assert
1053 : (!(*first_child_bv)->get_size ().dyn_cast_concrete_scalar ());
1054 : }
1055 91994 : }
1056 : }
1057 78491 : }
1058 :
1059 : const bit_vector &
1060 2208283 : concrete_bit_overlay::get_bits (const symbolic_bit_range &sym_bit_range) const
1061 : {
1062 2208283 : concrete_bit_range conc_bit_range;
1063 2208283 : if (concrete_from_symbolic (sym_bit_range, &conc_bit_range))
1064 2205683 : return get_concrete_bits (conc_bit_range);
1065 :
1066 : // TODO: should we be returning a bit_vector_from_ubc here?
1067 2600 : auto &bv_mgr = get_bit_vector_manager ();
1068 2600 : return bv_mgr.get_unknown_bits (sym_bit_range.get_size ()); // FIXME
1069 2208283 : }
1070 :
1071 : void
1072 1516 : concrete_bit_overlay::print_dump_widget_label (pretty_printer *pp) const
1073 : {
1074 1516 : pp_string (pp, "concrete bit overlay");
1075 1516 : }
1076 :
1077 : void
1078 1471 : concrete_bit_overlay::add_dump_widget_children (text_art::tree_widget &w,
1079 : node_dump_state &nds) const
1080 : {
1081 1471 : using tree_widget = text_art::tree_widget;
1082 1471 : std::unique_ptr<tree_widget> ranges_widget
1083 1471 : (tree_widget::from_fmt (nds, nullptr, "concrete ranges"));
1084 2942 : for (auto iter : m_concrete_range_map)
1085 : {
1086 1471 : auto &bit_range = iter.first;
1087 1471 : auto &bv = iter.second;
1088 1471 : pretty_printer pp;
1089 1471 : bit_range.dump_to_pp_pretty (&pp);
1090 2942 : ranges_widget->add_child (bv.make_dump_widget (nds,
1091 : pp_formatted_text (&pp)));
1092 2942 : }
1093 2942 : w.add_child (std::move (ranges_widget));
1094 2942 : w.add_child (get_baseline ().make_dump_widget (nds, "baseline"));
1095 1471 : }
1096 :
1097 : void
1098 17501 : concrete_bit_overlay::accept_node_visitor (node_visitor &v) const
1099 : {
1100 17501 : if (v.visit_bit_contents (*this))
1101 : {
1102 17501 : get_baseline ().accept_node_visitor (v);
1103 36233 : for (auto iter : m_concrete_range_map)
1104 : {
1105 18732 : auto &bv = iter.second;
1106 18732 : bv.accept_node_visitor (v);
1107 18732 : }
1108 : }
1109 17501 : }
1110 :
1111 : std::unique_ptr<json::object>
1112 20 : concrete_bit_overlay::to_json () const
1113 : {
1114 20 : auto obj = make_json_obj ("concrete_bit_overlay");
1115 20 : auto arr = std::make_unique<json::array> ();
1116 96 : for (auto iter : m_concrete_range_map)
1117 : {
1118 76 : auto &bit_range = iter.first;
1119 76 : auto &bv = iter.second;
1120 76 : auto element = std::make_unique<json::object> ();
1121 76 : element->set ("range", bit_range.to_json ());
1122 76 : element->set ("bv", bv.to_json ());
1123 76 : arr->append (std::move (element));
1124 152 : }
1125 20 : obj->set ("concrete_range_map", std::move (arr));
1126 20 : obj->set ("baseline", get_baseline ().to_json ());
1127 40 : return obj;
1128 20 : }
1129 :
1130 : std::unique_ptr<diagnostics::markup::node>
1131 0 : concrete_bit_overlay::make_markup_node (markup_context &) const
1132 : {
1133 0 : return diagnostics::markup::make_text_node ("concrete_bit_overlay");
1134 : }
1135 :
1136 : string_scan
1137 3782 : concrete_bit_overlay::
1138 : scan_for_first_zero_byte (string_scan::byte_offset_type start_scan_offset) const
1139 : {
1140 5319 : for (auto iter : m_concrete_range_map)
1141 : {
1142 5084 : auto &bit_range = iter.first;
1143 5084 : auto &bv = iter.second;
1144 :
1145 5084 : concrete_byte_range byte_range;
1146 10168 : if (!maybe_bytes_from_bits (bit_range, &byte_range))
1147 0 : return string_scan::cannot_scan (start_scan_offset);
1148 :
1149 5084 : if (start_scan_offset < byte_range.get_start_offset ())
1150 : {
1151 : /* We're looking before the range. See it we have non-zero
1152 : bytes leading to the range. */
1153 234 : string_scan scan
1154 234 : = get_baseline ().scan_for_first_zero_byte (start_scan_offset);
1155 234 : switch (scan.m_state)
1156 : {
1157 234 : default:
1158 234 : return scan;
1159 0 : case string_scan::state::beyond_domain:
1160 : /* We have non-zero bytes leading to the range. */
1161 0 : start_scan_offset = byte_range.get_start_offset ();
1162 0 : break;
1163 : }
1164 234 : }
1165 :
1166 4850 : if (start_scan_offset < byte_range.get_next_offset ())
1167 : {
1168 : /* Look within this bv. */
1169 3763 : auto bv_result = bv.scan_for_first_zero_byte
1170 3763 : (start_scan_offset
1171 3763 : - byte_range.get_start_offset ());
1172 3763 : if (bv_result.m_state == string_scan::state::beyond_domain)
1173 : {
1174 : /* Try to carry on to the next range (or gap). */
1175 450 : start_scan_offset = byte_range.get_next_offset ();
1176 450 : continue;
1177 : }
1178 : else
1179 3313 : return string_scan (bv_result.m_byte_offset
1180 6626 : + byte_range.get_start_offset (),
1181 6626 : bv_result.m_state);
1182 3763 : }
1183 : else
1184 : {
1185 : /* We're scannning after this overlay; ignore it. */
1186 1087 : continue;
1187 : }
1188 10168 : }
1189 :
1190 : // Beyond the end of the overlay ranges; look at underlying bit contents:
1191 235 : return get_baseline ().scan_for_first_zero_byte (start_scan_offset);
1192 : }
1193 :
1194 : static bool
1195 26341 : bit_vector_matches_sval_p (const symbolic::bit_vector &bv,
1196 : const svalue &sval)
1197 : {
1198 26341 : if (auto *bvs = bv.dyn_cast_bit_vector_from_svalue ())
1199 : {
1200 16803 : if (&bvs->get_svalue () == &sval)
1201 : return true;
1202 26254 : if (auto *sym_ptr_from_bv
1203 13127 : = bvs->get_svalue ().maybe_get_symbolic_pointer ())
1204 9024 : if (auto *sym_ptr_from_sval = sval.maybe_get_symbolic_pointer ())
1205 5696 : if (*sym_ptr_from_bv == *sym_ptr_from_sval)
1206 : return true;
1207 : }
1208 20540 : auto &bv_mgr = bv.get_bit_vector_manager ();
1209 20540 : if (auto *bv_from_sval = bv_mgr.maybe_fold_bits_for_svalue (sval))
1210 6898 : if (&bv == bv_from_sval)
1211 2421 : return true;
1212 : return false;
1213 : }
1214 :
1215 : void
1216 22460 : concrete_bit_overlay::
1217 : get_representative_path_vars (const region_model *model,
1218 : value_set *visited,
1219 : const cluster_region &cluster_reg,
1220 : const svalue *sval,
1221 : tree desired_type,
1222 : logger *logger,
1223 : auto_vec<path_var> *out_pvs) const
1224 : {
1225 : /* Find if SVAL is present anywhere in m_concrete_range_map. */
1226 45796 : for (auto iter : m_concrete_range_map)
1227 : {
1228 23336 : auto &bit_range = iter.first;
1229 23336 : auto &bv = iter.second;
1230 :
1231 23336 : if (bit_vector_matches_sval_p (bv, *sval))
1232 : {
1233 : /* Found SVAL at bit_range relative to CLUSTER_REG.
1234 : Try to convert that to a subregion, and from there
1235 : to a path_var. */
1236 8148 : auto_vec<const region *> sub_regions;
1237 8148 : cluster_reg.get_subregions_for_bit_range (model->get_manager (),
1238 : bit_range,
1239 : desired_type,
1240 : &sub_regions);
1241 31560 : for (auto reg : sub_regions)
1242 15608 : if (path_var pv
1243 7804 : = model->get_representative_path_var (reg,
1244 : visited,
1245 7804 : logger))
1246 7804 : append_pathvar_with_type (pv, desired_type, out_pvs);
1247 8148 : }
1248 23336 : }
1249 22460 : }
1250 :
1251 : /* Helper subroutine for poison_any_pointers_to_descendents implementations.
1252 : Return true for descendents of BAD_ANCESTOR_REG, but not to zero-offset
1253 : pointers (otherwise double-free detection doesn't work, since sm-state
1254 : for "free" is stored on the original ptr svalue). */
1255 :
1256 : static bool
1257 35865 : should_poison_sym_ptr_p (const symbolic::pointer &sym_ptr,
1258 : const region &bad_ancestor_reg)
1259 : {
1260 35865 : const region &cluster_reg = sym_ptr.get_cluster_region ();
1261 35865 : if (!cluster_reg.descendent_of_p (&bad_ancestor_reg))
1262 : return false;
1263 12960 : if (&cluster_reg == &bad_ancestor_reg
1264 12960 : && sym_ptr.get_byte_offset ().is_zero_p ())
1265 12384 : return false;
1266 : return true;
1267 : }
1268 :
1269 : const bit_contents &
1270 90259 : concrete_bit_overlay::
1271 : poison_any_pointers_to_descendents (const region *reg,
1272 : enum poison_kind pkind) const
1273 : {
1274 90259 : concrete_bit_overlay::key k {&get_baseline (), {}};
1275 :
1276 186728 : for (auto iter : m_concrete_range_map)
1277 : {
1278 96469 : auto &bit_range = iter.first;
1279 96469 : const bit_vector *bv = &iter.second;
1280 :
1281 96469 : if (auto *bvs = bv->dyn_cast_bit_vector_from_svalue ())
1282 : {
1283 68225 : if (auto *sym_ptr = bvs->get_svalue ().maybe_get_symbolic_pointer ())
1284 35798 : if (should_poison_sym_ptr_p (*sym_ptr, *reg))
1285 : {
1286 576 : auto &bv_mgr = get_bit_vector_manager ();
1287 576 : bv = &bv_mgr.get_poisoned_bits (bv->get_size (), pkind);
1288 : }
1289 : }
1290 192938 : k.m_concrete_range_map.insert ({bit_range, *bv});
1291 96469 : }
1292 :
1293 90259 : return get_bit_contents_manager ().get_concrete_bit_overlay (std::move (k),
1294 90259 : nullptr);
1295 90259 : }
1296 :
1297 : const bit_contents &
1298 246381 : concrete_bit_overlay::purge_state_involving (const symbolic::node &node) const
1299 : {
1300 246381 : concrete_bit_overlay::key k {&get_baseline (), {}};
1301 :
1302 503125 : for (auto iter : m_concrete_range_map)
1303 : {
1304 256744 : auto &bit_range = iter.first;
1305 256744 : const bit_vector *bv = &iter.second;
1306 :
1307 256744 : if (bv->involves_p (node))
1308 : {
1309 2401 : auto &bv_mgr = get_bit_vector_manager ();
1310 2401 : bv = &bv_mgr.get_unknown_bits (bv->get_size ());
1311 : }
1312 513488 : k.m_concrete_range_map.insert ({bit_range, *bv});
1313 256744 : }
1314 :
1315 246381 : return get_bit_contents_manager ().get_concrete_bit_overlay (std::move (k),
1316 246381 : nullptr);
1317 246381 : }
1318 :
1319 : bool
1320 592815 : concrete_bit_overlay::bits_live_p (const symbolic::bit_vector_from_ubc &) const
1321 : {
1322 592815 : return false;
1323 : }
1324 :
1325 : iter_result
1326 3473773 : concrete_bit_overlay::
1327 : for_each_value (iter_result (*cb) (const symbolic::value_node &val,
1328 : void *user_data),
1329 : void *user_data) const
1330 : {
1331 3473773 : if (get_baseline ().for_each_value (cb, user_data) == iter_result::stop)
1332 : return iter_result::stop;
1333 7662320 : for (auto iter : m_concrete_range_map)
1334 : {
1335 4188547 : const bit_vector *bv = &iter.second;
1336 4188547 : if (cb (*bv, user_data) == iter_result::stop)
1337 0 : return iter_result::stop;
1338 4188547 : }
1339 3473773 : return iter_result::keep_going;
1340 : }
1341 :
1342 : // Implementation detail of concrete_bit_overlay::get_concrete_bits
1343 :
1344 2205683 : class pending_bit_vector
1345 : {
1346 : public:
1347 2205683 : pending_bit_vector (bit_vector_manager &bv_mgr,
1348 : const bit_contents &baseline)
1349 2205683 : : m_bv_mgr (bv_mgr),
1350 2205683 : m_baseline (baseline),
1351 2205683 : m_accum_size (0_bits)
1352 : {}
1353 :
1354 : void
1355 2546408 : on_concrete_mapping (const concrete_bit_range &key_range,
1356 : const bit_vector &bv_value,
1357 : const concrete_bit_range &crop_range)
1358 : {
1359 2546408 : concrete_bit_range overlap;
1360 2546408 : if (key_range.intersects_p (crop_range, &overlap))
1361 : {
1362 2176222 : integer_with_unit<units::bit> start_offset_within_full_bv
1363 2176222 : = overlap.get_start_offset () - key_range.get_start_offset ();
1364 2176222 : concrete_bit_range bits_to_use (start_offset_within_full_bv,
1365 4352444 : overlap.get_size ());
1366 :
1367 : /* Add any necessary leading padding. */
1368 2176222 : integer_with_unit<units::bit> size_up_to_start_of_binding
1369 2176222 : = key_range.get_start_offset () - crop_range.get_start_offset ();
1370 2176222 : append_any_gap
1371 6528666 : (concrete_bit_range (crop_range.get_start_offset () + m_accum_size,
1372 4352444 : size_up_to_start_of_binding - m_accum_size));
1373 :
1374 2176222 : auto &cropped_bv = bv_value.get_cropped_bits (bits_to_use);
1375 2176222 : append_bit_vector (cropped_bv);
1376 4352444 : }
1377 2546408 : }
1378 :
1379 : void
1380 4381905 : append_any_gap (concrete_bit_range gap_range)
1381 : {
1382 4381905 : if (gap_range.get_size () > 0_bits)
1383 32180 : append_bit_vector (m_baseline.get_bits (gap_range));
1384 4381905 : }
1385 :
1386 : void
1387 2205683 : add_any_trailing_gap (const concrete_bit_range &bit_range)
1388 : {
1389 2205683 : append_any_gap
1390 6617049 : (concrete_bit_range (bit_range.get_start_offset () + m_accum_size,
1391 6617049 : bit_range.get_size () - m_accum_size));
1392 2205683 : }
1393 :
1394 : const bit_vector &
1395 2205683 : to_bit_vector ()
1396 : {
1397 2205683 : return m_bv_mgr.get_concat_bits (std::move (m_bit_vectors));
1398 : }
1399 :
1400 : private:
1401 : void
1402 2208402 : append_bit_vector (const symbolic::bit_vector &bv)
1403 : {
1404 2208402 : concrete_bit_size conc_size;
1405 2208402 : if (!concrete_from_symbolic (bv.get_size (), &conc_size))
1406 0 : gcc_unreachable ();
1407 2208402 : m_bit_vectors.push_back (&bv);
1408 2208402 : m_accum_size += conc_size;
1409 2208402 : }
1410 :
1411 : bit_vector_manager &m_bv_mgr;
1412 : const bit_contents &m_baseline;
1413 : std::vector<const symbolic::bit_vector *> m_bit_vectors;
1414 : concrete_bit_size m_accum_size;
1415 : };
1416 :
1417 : const bit_vector &
1418 2205683 : concrete_bit_overlay::
1419 : get_concrete_bits (const concrete_bit_range &bit_range) const
1420 : {
1421 2205683 : pending_bit_vector pending (get_bit_vector_manager (), get_baseline ());
1422 4752091 : for (auto iter : m_concrete_range_map)
1423 5092816 : pending.on_concrete_mapping (iter.first, iter.second, bit_range);
1424 2205683 : pending.add_any_trailing_gap (bit_range);
1425 :
1426 2205683 : auto &result = pending.to_bit_vector ();
1427 2205683 : gcc_assert (result.get_size ().dyn_cast_concrete_scalar ()->get_concrete ()
1428 : == bit_range.get_size ());
1429 2205683 : return result;
1430 2205683 : }
1431 :
1432 : /* Return true iff CONC_BIT_OFFSET is bound in one of this
1433 : concrete_bit_overlay's ranges. */
1434 :
1435 : bool
1436 150030 : concrete_bit_overlay::
1437 : bit_offset_is_bound_p (concrete_bit_offset conc_bit_offset) const
1438 : {
1439 : /* The concrete range comparator sorts by offset, then by size,
1440 : so we use a zero-sized range. */
1441 150030 : auto iter = m_concrete_range_map.lower_bound
1442 450090 : (concrete_bit_range (conc_bit_offset, 0_bits));
1443 150030 : if (iter != m_concrete_range_map.end ()
1444 298142 : && iter->first.contains_p (conc_bit_offset))
1445 : return true;
1446 5126 : if (iter == m_concrete_range_map.begin ())
1447 : return false;
1448 4841 : --iter;
1449 4841 : return iter->first.contains_p (conc_bit_offset);
1450 : }
1451 :
1452 : /* Populate a map for mapping BV to NEW_BIT_RANGE.
1453 : Canonicalize concat_bit_vector into multiple ranges where
1454 : possible. */
1455 :
1456 : std::map<concrete_bit_range, const bit_vector &>
1457 362633 : concrete_bit_overlay::
1458 : canonicalize_new_bit_vec (const concrete_bit_range &new_bit_range,
1459 : const bit_vector &bv)
1460 : {
1461 362633 : if (auto *concat_bv = bv.dyn_cast_concat_bit_vector ())
1462 : {
1463 : /* Canonicalize by splitting out leading concrete-sized elements
1464 : from concat_bv as separate bindings. */
1465 530 : std::map<concrete_bit_range, const bit_vector &> split_map;
1466 530 : concrete_bit_offset curr_start_offset = new_bit_range.get_start_offset ();
1467 1897 : for (auto iter : *concat_bv)
1468 : {
1469 1367 : concrete_bit_size conc_element_size;
1470 1367 : if (iter->maybe_get_concrete_size_in_bits (&conc_element_size))
1471 : {
1472 1367 : concrete_bit_range element_bit_range (curr_start_offset,
1473 2734 : conc_element_size);
1474 1367 : split_map.insert ({element_bit_range, *iter});
1475 1367 : curr_start_offset += conc_element_size;
1476 1367 : }
1477 : else
1478 : {
1479 : /* We have a symbolically-sized element; do as a single range. */
1480 0 : return {{new_bit_range, bv}};
1481 : }
1482 1367 : }
1483 530 : return split_map;
1484 530 : }
1485 :
1486 : /* Do BV as a single range. */
1487 724206 : return {{new_bit_range, bv}};
1488 : }
1489 :
1490 : /* Generate a map from MAP where any adjacent bindings have been
1491 : consolidated, combining the ranges and concatenating the bit_vectors. */
1492 :
1493 : std::map<concrete_bit_range, const bit_vector &>
1494 44720 : concrete_bit_overlay::
1495 : consolidate_adjacent (std::map<concrete_bit_range, const bit_vector &> map,
1496 : bit_vector_manager &bv_mgr)
1497 : {
1498 44720 : std::vector<std::pair<concrete_bit_range, const bit_vector &>> result;
1499 129136 : for (auto iter : map)
1500 : {
1501 84416 : if (!result.empty ())
1502 : {
1503 39696 : const concrete_bit_range &prev_range = result.back ().first;
1504 39696 : const concrete_bit_range &iter_range = iter.first;
1505 39696 : if (prev_range.get_next_offset () == iter_range.get_start_offset ())
1506 : {
1507 : /* They touch. Try concatenating the vectors, to see if
1508 : the results simplifies. */
1509 37336 : const bit_vector &prev_bv = result.back ().second;
1510 37336 : const bit_vector &iter_bv = iter.second;
1511 37336 : auto &concat
1512 37336 : = bv_mgr.get_concat_bits ({&prev_bv, &iter_bv});
1513 37336 : if (!concat.dyn_cast_concat_bit_vector ())
1514 : {
1515 : /* We were able to consolidate adjacent vectors.
1516 : Overwrite with the consolidated range/bit_vector,
1517 : rather than inserting a new element. */
1518 2047 : concrete_bit_range combined_range
1519 4094 : (prev_range.get_start_offset (),
1520 6141 : prev_range.get_size () + iter_range.get_size ());
1521 2047 : result.pop_back ();
1522 2047 : result.push_back ({combined_range, concat});
1523 2047 : continue;
1524 2047 : }
1525 : }
1526 : }
1527 164738 : result.push_back (iter);
1528 84416 : }
1529 44720 : return std::map<concrete_bit_range, const bit_vector &> (result.begin (),
1530 44720 : result.end ());
1531 44720 : }
1532 :
1533 : /* Consider:
1534 : | +------------------------------------+
1535 : | | old binding |
1536 : | +------------------------------------+
1537 : |
1538 : | +----------------------+
1539 : | | new binding |
1540 : | +----------------------+
1541 :
1542 : This function makes a new overlay by adding the new
1543 : binding, then "cutting a hole" out of any old bindings:
1544 : | +------+......................+------+
1545 : | |prefix| hole for new binding |suffix|
1546 : | +------+......................+------+
1547 : and adding them where they are not overwritten. */
1548 :
1549 : const bit_contents &
1550 44720 : concrete_bit_overlay::set_concrete_bits (const concrete_bit_range &new_bit_range,
1551 : const bit_vector &bv) const
1552 : {
1553 44720 : std::map<concrete_bit_range, const bit_vector &> new_concrete_range_map
1554 44720 : = canonicalize_new_bit_vec (new_bit_range, bv);
1555 :
1556 120274 : for (auto iter : m_concrete_range_map)
1557 : {
1558 75554 : auto &original_bit_range = iter.first;
1559 75554 : concrete_bit_range overlap;
1560 75554 : if (!new_bit_range.intersects_p (original_bit_range, &overlap))
1561 : {
1562 : // No overlap
1563 36817 : new_concrete_range_map.insert (iter);
1564 36817 : continue;
1565 : }
1566 :
1567 : /* Otherwise we have overlap. There are various cases that can occur:
1568 :
1569 : If NEW_BIT_RANGE is fully within original_bit_range:
1570 : | +------------------------------------+
1571 : | | original_bit_range |
1572 : | +------------------------------------+
1573 : | +----------------------+
1574 : | | new_bit_range |
1575 : | +----------------------+
1576 : then cut the old binding into two cropped pieces:
1577 : | +------+......................+------+
1578 : | |prefix| hole for new binding |suffix|
1579 : | +------+......................+------+
1580 :
1581 : If original_bit_range is fully within NEW_BIT_RANGE:
1582 : | +----------------------+
1583 : | | original_bit_range |
1584 : | +----------------------+
1585 : | +------------------------------------+
1586 : | | new_bit_range |
1587 : | +------------------------------------+
1588 : then the old binding is fully discarded
1589 :
1590 : Otherwise, we have either this case:
1591 : | +------------------+
1592 : | |original_bit_range|
1593 : | +------------------+
1594 : | +----------------------+
1595 : | | new_bit_range |
1596 : | +----------------------+
1597 : | +------+
1598 : | |prefix|
1599 : | +------+
1600 : or this case:
1601 : | +------------------+
1602 : | |original_bit_range|
1603 : | +------------------+
1604 : | +----------------------+
1605 : | | new_bit_range |
1606 : | +----------------------+
1607 : | +------+
1608 : | |suffix|
1609 : | +------+
1610 :
1611 : Handle these four cases by adding any prefix
1612 : and adding any suffix. */
1613 :
1614 : /* Add any prefix. */
1615 77474 : auto prefix_size = (overlap.get_start_offset ()
1616 38737 : - original_bit_range.get_start_offset ());
1617 38737 : if (prefix_size > 0_bits)
1618 : {
1619 734 : concrete_bit_range prefix_range
1620 1468 : (original_bit_range.get_start_offset (),
1621 1468 : prefix_size);
1622 734 : auto &prefix_bv = iter.second.get_prefix (prefix_size);
1623 734 : for (auto iter_prefix : canonicalize_new_bit_vec (prefix_range,
1624 1468 : prefix_bv))
1625 2202 : new_concrete_range_map.insert (iter_prefix);
1626 734 : }
1627 :
1628 : /* Add any suffix. */
1629 77474 : auto suffix_size = (original_bit_range.get_next_offset ()
1630 38737 : - overlap.get_next_offset ());
1631 38737 : if (suffix_size > 0_bits)
1632 : {
1633 1955 : concrete_bit_range suffix_range
1634 3910 : (overlap.get_next_offset (),
1635 3910 : suffix_size);
1636 1955 : auto &suffix_bv = iter.second.get_suffix (suffix_size);
1637 1963 : for (auto iter_suffix : canonicalize_new_bit_vec (suffix_range,
1638 3918 : suffix_bv))
1639 5881 : new_concrete_range_map.insert (iter_suffix);
1640 1955 : }
1641 189845 : }
1642 :
1643 : // TODO: is there any other canonicalization we want to do here?
1644 89440 : new_concrete_range_map = consolidate_adjacent (new_concrete_range_map,
1645 44720 : get_bit_vector_manager ());
1646 :
1647 44720 : concrete_bit_overlay::key k {&get_baseline (),
1648 44720 : std::move (new_concrete_range_map)};
1649 44720 : return get_bit_contents_manager ().get_concrete_bit_overlay (std::move (k),
1650 44720 : nullptr);
1651 44720 : }
1652 :
1653 : /* Attempt to extract an svalue (with type), a special-case for handling
1654 : interprocedural type-checking of variadic args. */
1655 :
1656 : const svalue *
1657 557 : concrete_bit_overlay::maybe_get_var_arg_svalue () const
1658 : {
1659 : /* Look for an initial binding to a bit_vector. */
1660 557 : auto iter = m_concrete_range_map.begin ();
1661 557 : if (iter == m_concrete_range_map.end ())
1662 : return nullptr;
1663 557 : auto &bv = iter->second;
1664 557 : auto *bv_from_sval = bv.dyn_cast_bit_vector_from_svalue ();
1665 557 : if (!bv_from_sval)
1666 : return nullptr;
1667 557 : return &bv_from_sval->get_svalue ();
1668 : }
1669 :
1670 : static tree
1671 82675 : maybe_get_type_for_range (tree type, const concrete_bit_range &bit_range)
1672 : {
1673 82675 : if (!type)
1674 : return NULL_TREE;
1675 :
1676 76793 : if (bit_range.get_start_offset () == 0_bits)
1677 : {
1678 68340 : bit_size_t type_size;
1679 68340 : if (int_size_in_bits (type, &type_size))
1680 68147 : if (bit_range.get_size () == convert_from_bit_size_t (type_size))
1681 : /* We have the full size of TYPE, use it. */
1682 66024 : return type;
1683 : }
1684 :
1685 10769 : if (TREE_CODE (type) == RECORD_TYPE)
1686 : {
1687 : // TODO: maybe cache this?
1688 5575 : record_layout layout (type);
1689 :
1690 5575 : if (auto *item = layout.get_item_at (bit_range.get_start_offset ()))
1691 : {
1692 5570 : const_tree field = item->m_field;
1693 5570 : concrete_bit_range range_relative_to_field
1694 5570 : (bit_range.get_start_offset ()
1695 16710 : - item->get_start_offset (),
1696 11140 : bit_range.get_size ());
1697 5570 : return maybe_get_type_for_range (TREE_TYPE (field),
1698 : range_relative_to_field);
1699 5570 : }
1700 5575 : }
1701 :
1702 : return NULL_TREE;
1703 : }
1704 :
1705 : /* Attempt to merge BIT_CONTENTS_A and BIT_CONTENTS_B.
1706 : At least one is a concrete_bit_overlay with SHARED_BASELINE,
1707 : otherwise they *are* SHARED_BASELINE.
1708 : CONC_BIT_OVERLAY_{A,B} are the result of dynamic casting each of
1709 : BIT_CONTENTS_{A,B} to concrete_bit_overlay. */
1710 :
1711 : const bit_contents *
1712 66375 : concrete_bit_overlay::
1713 : can_merge_p (const bit_contents &bit_contents_a,
1714 : const bit_contents &bit_contents_b,
1715 : const bit_contents &shared_baseline,
1716 : const concrete_bit_overlay *conc_bit_overlay_a,
1717 : const concrete_bit_overlay *conc_bit_overlay_b,
1718 : tree type,
1719 : model_merger *merger)
1720 : {
1721 : /* Locate all transitions. */
1722 66375 : std::set<concrete_bit_offset> bit_offsets;
1723 66375 : if (conc_bit_overlay_a)
1724 138640 : for (auto iter : conc_bit_overlay_a->m_concrete_range_map)
1725 : {
1726 73860 : bit_offsets.insert (iter.first.get_start_offset ());
1727 73860 : bit_offsets.insert (iter.first.get_next_offset ());
1728 73860 : }
1729 66375 : if (conc_bit_overlay_b)
1730 136012 : for (auto iter : conc_bit_overlay_b->m_concrete_range_map)
1731 : {
1732 72024 : bit_offsets.insert (iter.first.get_start_offset ());
1733 72024 : bit_offsets.insert (iter.first.get_next_offset ());
1734 72024 : }
1735 :
1736 : /* Convert into a vector of ranges, expressing the ranges
1737 : between elements in the set of transition offsets. */
1738 66375 : concrete_bit_offset prev_offset;
1739 66375 : std::vector<concrete_bit_range> bit_ranges;
1740 66375 : for (auto iter = bit_offsets.begin ();
1741 210694 : iter != bit_offsets.end ();
1742 144319 : ++iter)
1743 : {
1744 144319 : if (iter != bit_offsets.begin ())
1745 : {
1746 77944 : auto &next_offset = *iter;
1747 77944 : auto size = next_offset - prev_offset;
1748 233832 : bit_ranges.push_back (concrete_bit_range (prev_offset, size));
1749 77944 : }
1750 144319 : prev_offset = *iter;
1751 : }
1752 :
1753 : /* Now try to merge the bit vectors at each range. */
1754 66375 : concrete_bit_overlay::key k {&shared_baseline, {}};
1755 125529 : for (auto bit_range : bit_ranges)
1756 : {
1757 77105 : tree type_for_range = maybe_get_type_for_range (type, bit_range);
1758 :
1759 : /* Subroutine: return true if the start offset of BIT_RANGE is bound
1760 : within CONC_BIT_OVERLAY. */
1761 231315 : auto range_is_bound_p = []
1762 : (const concrete_bit_range &bit_range,
1763 : const concrete_bit_overlay *conc_bit_overlay) -> bool
1764 : {
1765 154210 : if (!conc_bit_overlay)
1766 : return false;
1767 149834 : return conc_bit_overlay->bit_offset_is_bound_p
1768 149834 : (bit_range.get_start_offset ());
1769 : };
1770 77105 : bool bound_in_a = range_is_bound_p (bit_range, conc_bit_overlay_a);
1771 77105 : bool bound_in_b = range_is_bound_p (bit_range, conc_bit_overlay_b);
1772 :
1773 77105 : const bit_vector *merged_bv = nullptr;
1774 :
1775 77105 : auto &bv_a = bit_contents_a.get_bits (bit_range);
1776 77105 : auto &bv_b = bit_contents_b.get_bits (bit_range);
1777 77105 : if (bound_in_a && bound_in_b)
1778 : {
1779 : /* Bound in both. */
1780 69799 : merged_bv = bit_vector::can_merge_p (bv_a,
1781 : bv_b,
1782 : type_for_range,
1783 : merger);
1784 : }
1785 7306 : else if (bound_in_a || bound_in_b)
1786 : {
1787 : /* We only have a binding for iter in one of the concrete_range_maps,
1788 : i.e. an attempt to merge bound and not-bound.
1789 : We might be dealing with conditional initialization, or of an
1790 : initialization of a growing region of a buffer in a loop. */
1791 6230 : if ((bound_in_a
1792 6230 : && !conc_bit_overlay_b
1793 2679 : && bv_b.dyn_cast_poisoned_bit_vector ())
1794 6230 : || (bound_in_b
1795 5047 : && !conc_bit_overlay_a
1796 1667 : && bv_a.dyn_cast_poisoned_bit_vector ()))
1797 : {
1798 : /* One is bound; the other is fully unbound and is uninitialized
1799 : over the range of interest.
1800 : Assume this is conditional initialization, and block the
1801 : merger. */
1802 : return nullptr;
1803 : }
1804 :
1805 : /* Otherwise, merge this bit range as "UNKNOWN" (assuming the other
1806 : bv is mergeable). */
1807 4206 : auto &bv_mgr = bit_contents_a.get_bit_vector_manager ();
1808 4206 : auto &unknown_bv
1809 4206 : = bv_mgr.get_unknown_bits (bit_range.get_size ());
1810 4206 : auto &bound_bv = bound_in_a ? bv_a : bv_b;
1811 4206 : if (bit_vector::can_merge_p (unknown_bv,
1812 : bound_bv,
1813 : type_for_range,
1814 : merger))
1815 : merged_bv = &unknown_bv;
1816 : }
1817 : else
1818 : {
1819 : /* We have a "hole" where neither has a binding. */
1820 1076 : continue;
1821 : }
1822 :
1823 73136 : if (merged_bv)
1824 116156 : for (auto iter : canonicalize_new_bit_vec (bit_range, *merged_bv))
1825 174234 : k.m_concrete_range_map.insert (iter);
1826 : else
1827 : return nullptr;
1828 77105 : }
1829 :
1830 48424 : auto &bit_contents_mgr = shared_baseline.get_bit_contents_manager ();
1831 48424 : return &bit_contents_mgr.get_concrete_bit_overlay (std::move (k),
1832 : nullptr);
1833 66375 : }
1834 :
1835 : // class symbolic_bit_overlay : public bit_overlay
1836 :
1837 : bool
1838 186135 : symbolic_bit_overlay::key::
1839 : operator< (const symbolic_bit_overlay::key &other) const
1840 : {
1841 186135 : if (int c = node::cmp (*m_baseline, *other.m_baseline))
1842 10908 : return c < 0;
1843 175227 : if (m_symbolic_range_map.size () != other.m_symbolic_range_map.size ())
1844 0 : return m_symbolic_range_map.size () < other.m_symbolic_range_map.size ();
1845 :
1846 175227 : auto iter_a = m_symbolic_range_map.begin ();
1847 175227 : auto iter_b = other.m_symbolic_range_map.begin ();
1848 :
1849 207099 : while (iter_a != m_symbolic_range_map.end ())
1850 : {
1851 175227 : if (int c = node::cmp (iter_a->second, iter_b->second))
1852 141901 : return c < 0;
1853 33326 : if (int c = symbolic_bit_range::cmp (iter_a->first, iter_b->first))
1854 1454 : return c < 0;
1855 31872 : ++iter_a;
1856 31872 : ++iter_b;
1857 : }
1858 :
1859 : return false;
1860 : }
1861 :
1862 : complexity
1863 2446 : symbolic_bit_overlay::key::get_complexity () const
1864 : {
1865 2446 : complexity child_complexities = m_baseline->get_complexity ();
1866 4892 : for (auto iter : m_symbolic_range_map)
1867 : {
1868 2446 : child_complexities += iter.first.get_complexity ();
1869 2446 : child_complexities += iter.second.get_complexity ();
1870 : }
1871 2446 : return complexity::from_child_complexities (child_complexities);
1872 : }
1873 :
1874 : /* Canonicalize by removing bindings where the bit_vector matches the
1875 : baseline's bit_vector. */
1876 :
1877 : void
1878 37607 : symbolic_bit_overlay::key::remove_redundant_bindings ()
1879 : {
1880 37607 : for (auto iter = m_symbolic_range_map.begin ();
1881 75214 : iter != m_symbolic_range_map.end ();
1882 : )
1883 : {
1884 37607 : auto &sym_bit_range = iter->first;
1885 37607 : auto &new_bv = iter->second;
1886 37607 : auto &existing_bv = m_baseline->get_bits (sym_bit_range);
1887 37607 : if (&existing_bv == &new_bv)
1888 636 : iter = m_symbolic_range_map.erase (iter);
1889 : else
1890 36971 : ++iter;
1891 : }
1892 37607 : }
1893 :
1894 2446 : symbolic_bit_overlay::symbolic_bit_overlay (const key &k)
1895 2446 : : bit_overlay (k.get_complexity (), *k.m_baseline),
1896 2446 : m_symbolic_range_map (k.m_symbolic_range_map)
1897 : {
1898 2446 : if (CHECKING_P)
1899 : {
1900 2446 : gcc_assert (!m_symbolic_range_map.empty ());
1901 2446 : gcc_assert (m_symbolic_range_map.size () == 1);
1902 :
1903 : /* Avoid nesting of overlays. */
1904 2446 : gcc_assert
1905 : (get_baseline ().get_kind () != bit_contents::kind::concrete_overlay
1906 : && get_baseline ().get_kind () != bit_contents::kind::symbolic_overlay);
1907 :
1908 4892 : for (auto iter : m_symbolic_range_map)
1909 : {
1910 2446 : auto &bit_range = iter.first;
1911 2446 : auto &overlay_bv = iter.second;
1912 :
1913 : /* Start offsets are not allowed to be "unknown". */
1914 2446 : gcc_assert (!bit_range.get_start_offset ().is_unknown_p ());
1915 :
1916 : // TODO(design question): do we want to allow unknown sizes?
1917 : #if 0
1918 : gcc_assert (!bit_range.get_size ().is_unknown_p ());
1919 : #endif
1920 :
1921 : // Bit ranges in the map are not allowed to be fully concrete
1922 2446 : concrete_bit_range conc_bit_range;
1923 2446 : bool got_conc_bit_range
1924 2446 : = concrete_from_symbolic (bit_range, &conc_bit_range);
1925 2446 : gcc_assert (!got_conc_bit_range);
1926 :
1927 : /* Must not have redundant bindings. */
1928 2446 : auto &baseline_bv = get_baseline ().get_bits (bit_range);
1929 2446 : gcc_assert (&overlay_bv != &baseline_bv);
1930 :
1931 : /* The size of each bit_vector must match that of the bit
1932 : range it is bound to. */
1933 2446 : gcc_assert (&overlay_bv.get_size ()
1934 : == &bit_range.get_size ());
1935 2446 : }
1936 : }
1937 2446 : }
1938 :
1939 : const bit_vector &
1940 2999 : symbolic_bit_overlay::get_bits (const symbolic_bit_range &sym_bit_range) const
1941 : {
1942 : /* TODO: we currently ignore the baseline
1943 : should we be clobbering the baseline when we create this? */
1944 :
1945 2999 : auto iter = m_symbolic_range_map.find (sym_bit_range);
1946 2999 : if (iter != m_symbolic_range_map.end ())
1947 928 : return iter->second;
1948 :
1949 : /* TODO: check for crops, etc
1950 : if no intersection, return the baseline. */
1951 :
1952 2071 : auto &bv_mgr = get_bit_vector_manager ();
1953 2071 : return bv_mgr.get_unknown_bits (sym_bit_range.get_size ());
1954 : }
1955 :
1956 : void
1957 11 : symbolic_bit_overlay::print_dump_widget_label (pretty_printer *pp) const
1958 : {
1959 11 : pp_string (pp, "symbolic bit overlay");
1960 11 : }
1961 :
1962 : void
1963 11 : symbolic_bit_overlay::add_dump_widget_children (text_art::tree_widget &w,
1964 : node_dump_state &nds) const
1965 : {
1966 11 : using tree_widget = text_art::tree_widget;
1967 11 : std::unique_ptr<tree_widget> ranges_widget
1968 11 : (tree_widget::from_fmt (nds, nullptr, "symbolic ranges"));
1969 22 : for (auto iter : m_symbolic_range_map)
1970 : {
1971 11 : auto &bit_range = iter.first;
1972 11 : auto &bv = iter.second;
1973 22 : ranges_widget->add_child (bit_range.make_dump_widget (nds));
1974 22 : ranges_widget->add_child (bv.make_dump_widget (nds));
1975 : }
1976 22 : w.add_child (std::move (ranges_widget));
1977 22 : w.add_child (get_baseline ().make_dump_widget (nds, "baseline"));
1978 11 : }
1979 :
1980 : void
1981 1034 : symbolic_bit_overlay::accept_node_visitor (node_visitor &v) const
1982 : {
1983 1034 : if (v.visit_bit_contents (*this))
1984 : {
1985 1034 : get_baseline ().accept_node_visitor (v);
1986 2068 : for (auto iter : m_symbolic_range_map)
1987 : {
1988 1034 : auto &bit_range = iter.first;
1989 1034 : auto &bv = iter.second;
1990 1034 : bit_range.accept_node_visitor (v);
1991 1034 : bv.accept_node_visitor (v);
1992 : }
1993 : }
1994 1034 : }
1995 :
1996 : std::unique_ptr<json::object>
1997 0 : symbolic_bit_overlay::to_json () const
1998 : {
1999 0 : auto obj = make_json_obj ("symbolic_bit_overlay");
2000 0 : auto arr = std::make_unique<json::array> ();
2001 0 : for (auto iter : m_symbolic_range_map)
2002 : {
2003 0 : auto &bit_range = iter.first;
2004 0 : auto &bv = iter.second;
2005 0 : auto element = std::make_unique<json::object> ();
2006 0 : element->set ("range", bit_range.to_json ());
2007 0 : element->set ("bv", bv.to_json ());
2008 0 : arr->append (std::move (element));
2009 0 : }
2010 0 : obj->set ("concrete_range_map", std::move (arr));
2011 0 : obj->set ("baseline", get_baseline ().to_json ());
2012 0 : return obj;
2013 0 : }
2014 :
2015 : std::unique_ptr<diagnostics::markup::node>
2016 0 : symbolic_bit_overlay::make_markup_node (markup_context &) const
2017 : {
2018 0 : return diagnostics::markup::make_text_node ("symbolic_bit_overlay");
2019 : }
2020 :
2021 : string_scan
2022 96 : symbolic_bit_overlay::
2023 : scan_for_first_zero_byte (string_scan::byte_offset_type start_scan_offset) const
2024 : {
2025 182 : for (auto iter : m_symbolic_range_map)
2026 : {
2027 96 : auto &bit_range = iter.first;
2028 96 : auto &bv = iter.second;
2029 :
2030 96 : symbolic_byte_range byte_range;
2031 96 : if (!maybe_bytes_from_bits (bit_range, &byte_range))
2032 7 : return string_scan::cannot_scan (start_scan_offset);
2033 :
2034 89 : auto *concrete_start_offset
2035 89 : = byte_range.get_start_offset ().dyn_cast_concrete_scalar ();
2036 89 : if (!concrete_start_offset)
2037 3 : return string_scan::cannot_scan (start_scan_offset);
2038 86 : concrete_byte_offset start_of_range_byte_offset
2039 86 : = concrete_start_offset->get_concrete ();
2040 :
2041 86 : if (start_scan_offset < start_of_range_byte_offset)
2042 : {
2043 : /* TODO: look at underlying bit contents. */
2044 0 : return string_scan::cannot_scan (start_scan_offset);
2045 : }
2046 86 : else if (start_scan_offset == start_of_range_byte_offset)
2047 : {
2048 : /* Look within this bv. */
2049 86 : auto bv_result = bv.scan_for_first_zero_byte (0_bytes);
2050 86 : if (bv_result.m_state == string_scan::state::beyond_domain)
2051 : {
2052 : /* Try to carry on to the next range (or gap). */
2053 0 : auto *concrete_next_offset
2054 0 : = byte_range.get_next_offset ().dyn_cast_concrete_scalar ();
2055 0 : if (concrete_next_offset)
2056 0 : start_scan_offset = concrete_next_offset->get_concrete ();
2057 : else
2058 0 : return string_scan::cannot_scan (start_scan_offset);
2059 0 : continue;
2060 0 : }
2061 86 : }
2062 : else
2063 : /* We might be scannning within or after this overlay; give up. */
2064 0 : return string_scan::cannot_scan (start_scan_offset);
2065 86 : }
2066 :
2067 : // Beyond the end of the overlay ranges; look at underlying bit contents:
2068 86 : return get_baseline ().scan_for_first_zero_byte (start_scan_offset);
2069 : }
2070 :
2071 : void
2072 3005 : symbolic_bit_overlay::
2073 : get_representative_path_vars (const region_model *model,
2074 : value_set *visited,
2075 : const cluster_region &cluster_reg,
2076 : const svalue *sval,
2077 : tree desired_type,
2078 : logger *logger,
2079 : auto_vec<path_var> *out_pvs) const
2080 : {
2081 : /* Find if SVAL is present anywhere in m_symbolic_range_map. */
2082 6010 : for (auto iter : m_symbolic_range_map)
2083 : {
2084 3005 : auto &bit_range = iter.first;
2085 3005 : auto &bv = iter.second;
2086 :
2087 3005 : if (bit_vector_matches_sval_p (bv, *sval))
2088 : {
2089 : /* Found SVAL at bit_range relative to CLUSTER_REG.
2090 : Try to convert that to a subregion, and from there
2091 : to a path_var. */
2092 74 : symbolic::memory_range mem_range (cluster_reg,
2093 74 : bit_range);
2094 148 : if (auto *reg = model->maybe_get_region_for_mem_range
2095 74 : (mem_range,
2096 : desired_type,
2097 : logger))
2098 148 : if (path_var pv
2099 74 : = model->get_representative_path_var (reg,
2100 : visited,
2101 74 : logger))
2102 70 : append_pathvar_with_type (pv, desired_type, out_pvs);
2103 : }
2104 : }
2105 3005 : }
2106 :
2107 : const bit_contents &
2108 3428 : symbolic_bit_overlay::
2109 : poison_any_pointers_to_descendents (const region *reg,
2110 : enum poison_kind pkind) const
2111 : {
2112 3428 : symbolic_bit_overlay::key k {&get_baseline (), {}};
2113 :
2114 6856 : for (auto iter : m_symbolic_range_map)
2115 : {
2116 3428 : auto &bit_range = iter.first;
2117 3428 : const bit_vector *bv = &iter.second;
2118 :
2119 3428 : if (auto *bvs = bv->dyn_cast_bit_vector_from_svalue ())
2120 : {
2121 89 : if (auto *sym_ptr = bvs->get_svalue ().maybe_get_symbolic_pointer ())
2122 67 : if (should_poison_sym_ptr_p (*sym_ptr, *reg))
2123 : {
2124 0 : auto &bv_mgr = get_bit_vector_manager ();
2125 0 : bv = &bv_mgr.get_poisoned_bits (bv->get_size (), pkind);
2126 : }
2127 : }
2128 3428 : k.m_symbolic_range_map.insert ({bit_range, *bv});
2129 : }
2130 :
2131 3428 : return get_bit_contents_manager ().get_symbolic_bit_overlay (std::move (k),
2132 3428 : nullptr);
2133 3428 : }
2134 :
2135 : const bit_contents &
2136 11233 : symbolic_bit_overlay::purge_state_involving (const symbolic::node &node) const
2137 : {
2138 11233 : symbolic_bit_overlay::key k {&get_baseline (), {}};
2139 :
2140 22466 : for (auto iter : m_symbolic_range_map)
2141 : {
2142 11233 : auto &bit_range = iter.first;
2143 : // FIXME: also need to purge bit_range
2144 11233 : const bit_vector *bv = &iter.second;
2145 :
2146 11233 : if (bv->involves_p (node))
2147 : {
2148 3 : auto &bv_mgr = get_bit_vector_manager ();
2149 3 : bv = &bv_mgr.get_unknown_bits (bv->get_size ());
2150 : }
2151 11233 : k.m_symbolic_range_map.insert ({bit_range, *bv});
2152 : }
2153 :
2154 11233 : return get_bit_contents_manager ().get_symbolic_bit_overlay (std::move (k),
2155 11233 : nullptr);
2156 11233 : }
2157 :
2158 : bool
2159 16347 : symbolic_bit_overlay::bits_live_p (const symbolic::bit_vector_from_ubc &) const
2160 : {
2161 16347 : return false;
2162 : }
2163 :
2164 : iter_result
2165 97820 : symbolic_bit_overlay::
2166 : for_each_value (iter_result (*cb) (const symbolic::value_node &val,
2167 : void *user_data),
2168 : void *user_data) const
2169 : {
2170 97820 : if (get_baseline ().for_each_value (cb, user_data) == iter_result::stop)
2171 : return iter_result::stop;
2172 195640 : for (auto iter : m_symbolic_range_map)
2173 : {
2174 97820 : const bit_vector *bv = &iter.second;
2175 97820 : if (cb (*bv, user_data) == iter_result::stop)
2176 97820 : return iter_result::stop;
2177 : }
2178 : return iter_result::keep_going;
2179 : }
2180 :
2181 : // class offset_bit_contents : public bit_contents
2182 :
2183 : bool
2184 28 : offset_bit_contents::key::operator< (const key &other) const
2185 : {
2186 28 : if (int c = node::cmp (*m_inner_bc, *other.m_inner_bc))
2187 0 : return c < 0;
2188 28 : return node::cmp (*m_bit_offset, *other.m_bit_offset) < 0;
2189 : }
2190 :
2191 : complexity
2192 9 : offset_bit_contents::key::get_complexity () const
2193 : {
2194 9 : return complexity::from_pair (m_inner_bc->get_complexity (),
2195 9 : m_bit_offset->get_complexity ());
2196 : }
2197 :
2198 9 : offset_bit_contents::offset_bit_contents (node_manager &node_mgr,
2199 : const key &k)
2200 : : bit_contents (k.get_complexity (), node_mgr),
2201 9 : m_inner_bc (*k.m_inner_bc),
2202 9 : m_bit_offset (*k.m_bit_offset)
2203 : {
2204 9 : gcc_assert (!m_bit_offset.is_zero_p ());
2205 9 : gcc_assert (!m_inner_bc.dyn_cast_uniform_bit_contents ());
2206 :
2207 : /* No nesting. */
2208 9 : gcc_assert (!m_inner_bc.dyn_cast_offset_bit_contents ());
2209 9 : }
2210 :
2211 : const bit_vector &
2212 7 : offset_bit_contents::get_bits (const symbolic_bit_range &bit_range) const
2213 : {
2214 7 : symbolic_bit_range range_in_inner (bit_range.get_start_offset ()
2215 7 : + m_bit_offset,
2216 7 : bit_range.get_size ());
2217 7 : return m_inner_bc.get_bits (range_in_inner);
2218 : }
2219 :
2220 : void
2221 0 : offset_bit_contents::print_dump_widget_label (pretty_printer *pp) const
2222 : {
2223 0 : pp_string (pp, "offset_bit_contents");
2224 0 : }
2225 :
2226 : void
2227 0 : offset_bit_contents::add_dump_widget_children (text_art::tree_widget &w,
2228 : node_dump_state &nds) const
2229 : {
2230 0 : w.add_child (m_inner_bc.make_dump_widget (nds, "inner_bc"));
2231 0 : w.add_child (m_bit_offset.make_dump_widget (nds, "bit_offset"));
2232 0 : }
2233 :
2234 : void
2235 0 : offset_bit_contents::accept_node_visitor (node_visitor &v) const
2236 : {
2237 0 : if (v.visit_bit_contents (*this))
2238 : {
2239 0 : m_inner_bc.accept_node_visitor (v);
2240 0 : m_bit_offset.accept_node_visitor (v);
2241 : }
2242 0 : }
2243 :
2244 : std::unique_ptr<json::object>
2245 0 : offset_bit_contents::to_json () const
2246 : {
2247 0 : auto obj = make_json_obj ("offset_bit_contents");
2248 :
2249 0 : obj->set ("inner_bc", m_inner_bc.to_json ());
2250 0 : obj->set ("bit_offset", m_bit_offset.to_json ());
2251 :
2252 0 : return obj;
2253 : }
2254 :
2255 : std::unique_ptr<diagnostics::markup::node>
2256 0 : offset_bit_contents::make_markup_node (markup_context &) const
2257 : {
2258 0 : return diagnostics::markup::make_text_node ("offset_bit_contents");
2259 : }
2260 :
2261 : string_scan
2262 0 : offset_bit_contents::
2263 : scan_for_first_zero_byte (string_scan::byte_offset_type start_byte_offset) const
2264 : {
2265 0 : concrete_bit_offset conc_bit_offset;
2266 0 : if (concrete_from_symbolic (m_bit_offset, &conc_bit_offset))
2267 : {
2268 0 : concrete_byte_offset conc_byte_offset;
2269 0 : if (maybe_bytes_from_bits (conc_bit_offset, &conc_byte_offset))
2270 : {
2271 0 : string_scan inner
2272 0 : = m_inner_bc.scan_for_first_zero_byte (start_byte_offset
2273 0 : + conc_byte_offset);
2274 0 : inner.m_byte_offset -= conc_byte_offset;
2275 0 : return inner;
2276 : }
2277 0 : }
2278 :
2279 0 : return string_scan::cannot_scan (start_byte_offset);
2280 0 : }
2281 :
2282 : void
2283 0 : offset_bit_contents::
2284 : get_representative_path_vars (const region_model *,
2285 : value_set *,
2286 : const cluster_region &,
2287 : const svalue *,
2288 : tree,
2289 : logger *,
2290 : auto_vec<path_var> *) const
2291 : {
2292 : // no-op
2293 0 : }
2294 :
2295 : const bit_contents &
2296 0 : offset_bit_contents::
2297 : poison_any_pointers_to_descendents (const region *reg,
2298 : enum poison_kind pkind) const
2299 : {
2300 0 : auto &poisoned_inner
2301 0 : = m_inner_bc.poison_any_pointers_to_descendents (reg, pkind);
2302 0 : return get_bit_contents_manager ().get_offset_bit_contents (poisoned_inner,
2303 0 : m_bit_offset);
2304 : }
2305 :
2306 : const bit_contents &
2307 0 : offset_bit_contents::purge_state_involving (const symbolic::node &n) const
2308 : {
2309 0 : auto &purged_inner = m_inner_bc.purge_state_involving (n);
2310 0 : return get_bit_contents_manager ().get_offset_bit_contents (purged_inner,
2311 0 : m_bit_offset);
2312 : }
2313 :
2314 : bool
2315 0 : offset_bit_contents::
2316 : bits_live_p (const symbolic::bit_vector_from_ubc &bv_from_ubc) const
2317 : {
2318 0 : return m_inner_bc.bits_live_p (bv_from_ubc);
2319 : }
2320 :
2321 : iter_result
2322 0 : offset_bit_contents::
2323 : for_each_value (iter_result (*cb) (const symbolic::value_node &val,
2324 : void *user_data),
2325 : void *user_data) const
2326 : {
2327 0 : return m_inner_bc.for_each_value (cb, user_data);
2328 : }
2329 :
2330 : /* offset(offset(bc, x), y) --> offset(bc, x + y). */
2331 :
2332 : const bit_contents *
2333 5 : offset_bit_contents::
2334 : maybe_fold_offset (const symbolic_bit_offset &bit_offset) const
2335 : {
2336 5 : return &(get_bit_contents_manager ()
2337 5 : .get_offset_bit_contents (m_inner_bc,
2338 5 : m_bit_offset + bit_offset));
2339 : }
2340 :
2341 : // class bit_contents_manager
2342 :
2343 4495 : bit_contents_manager::bit_contents_manager (node_manager &node_mgr)
2344 4495 : : m_node_mgr (node_mgr),
2345 4495 : m_uninit_contents (node_mgr),
2346 4495 : m_zero_contents (node_mgr)
2347 : {
2348 4495 : }
2349 :
2350 : const bit_contents &
2351 127284 : bit_contents_manager::get_untracked (value_set maybe_live)
2352 : {
2353 127284 : value_set canonical_vs;
2354 316272 : for (auto iter : maybe_live)
2355 188988 : if (untracked_bit_contents::track_value_liveness_p (*iter))
2356 163924 : canonical_vs.add (iter);
2357 :
2358 127284 : untracked_bit_contents::key k (std::move (canonical_vs));
2359 :
2360 127284 : auto iter = m_untracked_bit_contents.find (k);
2361 127284 : if (iter != m_untracked_bit_contents.end ())
2362 123813 : return *(iter->second);
2363 :
2364 3471 : auto p = std::make_unique<untracked_bit_contents> (m_node_mgr, k);
2365 3471 : auto &result = *p;
2366 3471 : m_untracked_bit_contents.emplace (k, std::move (p));
2367 3471 : return result;
2368 130755 : }
2369 :
2370 : const bit_contents &
2371 306396 : bit_contents_manager::set_bits (const bit_contents &original,
2372 : const symbolic_bit_range &sym_bit_range,
2373 : const bit_vector &bv,
2374 : uncertainty_t *uncertainty)
2375 : {
2376 306396 : concrete_bit_range conc_bit_range;
2377 306396 : if (concrete_from_symbolic (sym_bit_range, &conc_bit_range))
2378 301866 : return set_bits (original, conc_bit_range, bv, uncertainty);
2379 :
2380 4530 : std::map<symbolic_bit_range, const bit_vector &> overlays;
2381 4530 : overlays.insert ({sym_bit_range, bv});
2382 4530 : symbolic_bit_overlay::key k {&original, std::move (overlays)};
2383 4530 : return get_symbolic_bit_overlay (std::move (k), uncertainty);
2384 4530 : }
2385 :
2386 : const bit_contents &
2387 301866 : bit_contents_manager::set_bits (const bit_contents &original,
2388 : const concrete_bit_range &conc_bit_range,
2389 : const bit_vector &bv,
2390 : uncertainty_t *uncertainty)
2391 : {
2392 301866 : if (auto *conc_overlay = original.dyn_cast_concrete_bit_overlay ())
2393 44720 : return conc_overlay->set_concrete_bits (conc_bit_range, bv);
2394 :
2395 257146 : std::map<concrete_bit_range, const bit_vector &> overlays
2396 257146 : = concrete_bit_overlay::canonicalize_new_bit_vec (conc_bit_range, bv);
2397 257146 : concrete_bit_overlay::key k {&original, std::move (overlays)};
2398 257146 : return get_concrete_bit_overlay (std::move (k), uncertainty);
2399 257146 : }
2400 :
2401 : const bit_contents &
2402 687082 : bit_contents_manager::
2403 : get_concrete_bit_overlay (concrete_bit_overlay::key k,
2404 : uncertainty_t *uncertainty)
2405 : {
2406 687082 : gcc_assert (k.m_baseline);
2407 :
2408 687082 : k.remove_redundant_bindings ();
2409 :
2410 687082 : if (k.m_concrete_range_map.size () == 0)
2411 3417 : return *k.m_baseline;
2412 :
2413 : /* Avoid nesting of overlays.
2414 : Any attempt to make a concrete_bit_overlay on top of another
2415 : concrete_bit_overlay should have been flattened by now. */
2416 683665 : gcc_assert (k.m_baseline->get_kind () != bit_contents::kind::concrete_overlay);
2417 683665 : if (k.m_baseline->get_kind () == bit_contents::kind::symbolic_overlay)
2418 : {
2419 72 : if (uncertainty)
2420 42 : uncertainty->add_bit_contents (*k.m_baseline);
2421 72 : value_set maybe_live = k.m_baseline->get_maybe_live_values ();
2422 72 : k.m_baseline = &get_untracked (std::move (maybe_live));
2423 72 : }
2424 :
2425 683665 : k.remove_redundant_bindings ();
2426 683665 : if (k.m_concrete_range_map.size () == 0)
2427 0 : return *k.m_baseline;
2428 :
2429 683665 : auto iter = m_concrete_bit_overlays.find (k);
2430 683665 : if (iter != m_concrete_bit_overlays.end ())
2431 605174 : return *(iter->second);
2432 :
2433 78491 : auto p = std::make_unique<concrete_bit_overlay> (k);
2434 78491 : auto &result = *p;
2435 78491 : m_concrete_bit_overlays.emplace (k, std::move (p));
2436 78491 : return result;
2437 78491 : }
2438 :
2439 : const bit_contents &
2440 19225 : bit_contents_manager::
2441 : get_symbolic_bit_overlay (symbolic_bit_overlay::key k,
2442 : uncertainty_t *uncertainty)
2443 : {
2444 19225 : gcc_assert (k.m_baseline);
2445 :
2446 19225 : k.remove_redundant_bindings ();
2447 :
2448 19225 : if (k.m_symbolic_range_map.size () == 0)
2449 636 : return *k.m_baseline;
2450 :
2451 36971 : for (auto &iter : k.m_symbolic_range_map)
2452 : {
2453 18589 : auto &bit_range = iter.first;
2454 : /* If any of the bit ranges start at an unknown offset,
2455 : fall back to the whole bit contents being unknown. */
2456 18589 : if (bit_range.get_start_offset ().is_unknown_p ())
2457 : {
2458 207 : if (uncertainty)
2459 193 : uncertainty->add_bit_contents (*k.m_baseline);
2460 :
2461 207 : value_set maybe_live = k.m_baseline->get_maybe_live_values ();
2462 414 : for (auto &iter2 : k.m_symbolic_range_map)
2463 : {
2464 207 : auto &bv = iter2.second;
2465 207 : if (uncertainty)
2466 193 : uncertainty->on_maybe_bound_value (bv);
2467 207 : maybe_live.add (&bv);
2468 : }
2469 207 : return get_untracked (std::move (maybe_live));
2470 207 : }
2471 : }
2472 :
2473 : /* Avoid nesting of overlays. */
2474 18382 : if (k.m_baseline->get_kind () == bit_contents::kind::concrete_overlay
2475 18382 : || k.m_baseline->get_kind () == bit_contents::kind::symbolic_overlay)
2476 : {
2477 602 : if (uncertainty)
2478 440 : uncertainty->add_bit_contents (*k.m_baseline);
2479 602 : value_set maybe_live = k.m_baseline->get_maybe_live_values ();
2480 602 : k.m_baseline = &get_untracked (std::move (maybe_live));
2481 602 : }
2482 :
2483 18382 : k.remove_redundant_bindings ();
2484 :
2485 18382 : if (k.m_symbolic_range_map.size () == 0)
2486 0 : return *k.m_baseline;
2487 :
2488 : /* Any attempt to create more than one symbolic key ought to have been
2489 : folded into an "untracked" baseline by now. */
2490 18382 : gcc_assert (k.m_symbolic_range_map.size () == 1);
2491 :
2492 18382 : auto iter = m_symbolic_bit_overlays.find (k);
2493 18382 : if (iter != m_symbolic_bit_overlays.end ())
2494 15936 : return *(iter->second);
2495 :
2496 2446 : auto p = std::make_unique<symbolic_bit_overlay> (k);
2497 2446 : auto &result = *p;
2498 2446 : m_symbolic_bit_overlays.emplace (k, std::move (p));
2499 2446 : return result;
2500 2446 : }
2501 :
2502 : /* Create a bit_contents that is uninitialized everywhere, apart from
2503 : bytes [0-sizeof(TREE_CST)), which are bound to TREE_CST's representation
2504 : on the target. */
2505 :
2506 : const bit_contents &
2507 0 : bit_contents_manager::make_bit_contents_for_constant (tree tree_cst)
2508 : {
2509 0 : auto &bv_mgr = m_node_mgr.get_bit_vector_manager ();
2510 0 : auto &bit_mgr = m_node_mgr.get_scalars ().get_bits ();
2511 0 : auto &bv = bv_mgr.get_bits_for_constant (tree_cst);
2512 :
2513 0 : return (get_uninitialized ()
2514 0 : .set_bits (symbolic_bit_range (bit_mgr.get_concrete (0_bits),
2515 0 : bv.get_size ()),
2516 : bv,
2517 0 : nullptr));
2518 : }
2519 :
2520 : const bit_contents &
2521 0 : bit_contents_manager::get_bit_contents_for_constant (tree tree_cst)
2522 : {
2523 0 : auto iter = m_bit_contents_for_constants.find (tree_cst);
2524 0 : if (iter != m_bit_contents_for_constants.end ())
2525 0 : return iter->second;
2526 :
2527 0 : auto &bc = make_bit_contents_for_constant (tree_cst);
2528 0 : m_bit_contents_for_constants.insert ({tree_cst, bc});
2529 0 : return bc;
2530 : }
2531 :
2532 : bool
2533 38394 : baseline_key::
2534 : region_has_meaningful_size_p (symbolic::node_manager &node_mgr) const
2535 : {
2536 38394 : if (m_cluster_reg->get_kind () == RK_SYMBOLIC)
2537 : {
2538 : /* We don't know the size of the underlying region:
2539 : "T *ptr" could be just one T, but could be an array
2540 : of T, or within an array of T. */
2541 : return false;
2542 : }
2543 :
2544 22402 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
2545 22402 : if (m_cluster_reg->maybe_get_bit_size (bit_mgr))
2546 22318 : return true;
2547 :
2548 : return false;
2549 : }
2550 :
2551 : std::unique_ptr<json::object>
2552 14 : baseline_key::to_json () const
2553 : {
2554 14 : auto obj = std::make_unique<json::object> ();
2555 14 : obj->set ("cluster_reg", m_cluster_reg->to_json ());
2556 14 : obj->set_string ("baseline_state", baseline_state_to_str (m_baseline_state));
2557 14 : return obj;
2558 : }
2559 :
2560 : const bit_contents &
2561 131365 : bit_contents_manager::make_baseline_contents (baseline_key k)
2562 : {
2563 131365 : if (!k.m_cluster_reg->can_have_initial_svalue_p ())
2564 100810 : return get_uninitialized ();
2565 :
2566 30555 : auto &rmm = m_node_mgr.get_svalue_manager ();
2567 30555 : switch (k.m_baseline_state)
2568 : {
2569 0 : default:
2570 0 : gcc_unreachable ();
2571 :
2572 1219 : case baseline_state::main:
2573 1219 : {
2574 1219 : if (auto *decl_reg = k.m_cluster_reg->dyn_cast_decl_region ())
2575 : {
2576 : /* Attempt to get the initializer value for cluster_reg as a
2577 : bit_contents. */
2578 793 : if (auto *bc = decl_reg->maybe_get_bit_contents_for_initializer (&rmm))
2579 : return *bc;
2580 : }
2581 : }
2582 : break;
2583 :
2584 : case baseline_state::after_main:
2585 : break;
2586 : }
2587 :
2588 30356 : if (k.region_has_meaningful_size_p (m_node_mgr))
2589 : {
2590 : /* Get or create a concrete_bit_overlay, with a baseline bit_vector
2591 : at [0, size), and uninitialized everywhere else. */
2592 22318 : auto &bv_mgr = m_node_mgr.get_bit_vector_manager ();
2593 22318 : auto &bit_mgr = m_node_mgr.get_scalars ().get_bits ();
2594 22318 : auto &baseline_bv
2595 22318 : = bv_mgr.get_baseline_bit_vector (*k.m_cluster_reg,
2596 : k.m_baseline_state);
2597 22318 : return (get_uninitialized ()
2598 22318 : .set_bits (symbolic_bit_range (bit_mgr.get_concrete (0_bits),
2599 44636 : baseline_bv.get_size ()),
2600 : baseline_bv,
2601 : nullptr));
2602 : }
2603 : else
2604 : {
2605 : /* Make a baseline_bit_contents. */
2606 8038 : auto bc
2607 8038 : = std::make_unique<baseline_bit_contents> (m_node_mgr, k);
2608 8038 : auto &result = *bc;
2609 8038 : m_unfoldable_bit_contents.push_back (std::move (bc));
2610 8038 : return result;
2611 8038 : }
2612 : }
2613 :
2614 : const bit_contents &
2615 2100225 : bit_contents_manager::
2616 : get_baseline_contents (const cluster_region &cluster_reg,
2617 : baseline_state bs)
2618 : {
2619 : /* Read-only var decls don't change,
2620 : so always use baseline_state::main for them. */
2621 2100225 : if (tree decl = cluster_reg.maybe_get_decl ())
2622 1745970 : if (VAR_P (decl) && TREE_READONLY (decl))
2623 : bs = baseline_state::main;
2624 :
2625 2100225 : baseline_key k {&cluster_reg, bs};
2626 2100225 : auto iter = m_baseline_contents.find (k);
2627 2100225 : if (iter != m_baseline_contents.end ())
2628 1968860 : return iter->second;
2629 :
2630 131365 : auto &bc = make_baseline_contents (k);
2631 131365 : m_baseline_contents.insert ({k, bc});
2632 131365 : return bc;
2633 : }
2634 :
2635 : const bit_contents &
2636 25503 : bit_contents_manager::
2637 : get_conjured_bit_contents (const region &base_reg,
2638 : const gimple *stmt,
2639 : const conjured_purge &p,
2640 : unsigned idx)
2641 : {
2642 25503 : const symbolic::conjured_bit_contents::key k (stmt, &base_reg, idx);
2643 25503 : auto iter = m_conjured_map.find (k);
2644 25503 : if (iter != m_conjured_map.end ())
2645 : {
2646 13239 : auto *bc = iter->second.get ();
2647 : /* We're reusing an existing conjured_bit_contents, perhaps from a
2648 : different state within this analysis, or perhaps from an earlier state
2649 : on this execution path. For the latter, purge any state involving the
2650 : "new" bit_contents from the current program_state. */
2651 13239 : p.purge (*bc);
2652 13239 : return *bc;
2653 : }
2654 12264 : auto bc = std::make_unique<symbolic::conjured_bit_contents> (m_node_mgr, k);
2655 12264 : auto &result = *bc.get ();
2656 12264 : m_conjured_map.emplace (k, std::move (bc));
2657 12264 : return result;
2658 12264 : }
2659 :
2660 : #if CHECKING_P
2661 : const bit_contents &
2662 60 : bit_contents_manager::
2663 : get_named_test_bit_contents (std::string name)
2664 : {
2665 60 : auto obj_up = std::make_unique<named_test_bit_contents> (m_node_mgr,
2666 60 : std::move (name));
2667 60 : auto &result = *obj_up.get ();
2668 60 : m_unfoldable_bit_contents.push_back (std::move (obj_up));
2669 120 : return result;
2670 60 : }
2671 : #endif
2672 :
2673 : const bit_contents &
2674 373 : bit_contents_manager::
2675 : get_offset_bit_contents (const bit_contents &inner_bc,
2676 : const symbolic_bit_offset &bit_offset)
2677 : {
2678 : /* An offset of zero can always be ignored. */
2679 373 : if (bit_offset.is_zero_p ())
2680 : return inner_bc;
2681 :
2682 30 : if (auto *folded = inner_bc.maybe_fold_offset (bit_offset))
2683 : return *folded;
2684 :
2685 13 : const symbolic::offset_bit_contents::key k {&inner_bc, &bit_offset};
2686 13 : auto iter = m_offset_map.find (k);
2687 13 : if (iter != m_offset_map.end ())
2688 4 : return *(iter->second);
2689 :
2690 9 : auto bc = std::make_unique<symbolic::offset_bit_contents> (m_node_mgr, k);
2691 9 : auto &result = *bc.get ();
2692 9 : m_offset_map.emplace (k, std::move (bc));
2693 9 : return result;
2694 9 : }
2695 :
2696 : } // namespace ana::symbolic
2697 :
2698 : #if CHECKING_P
2699 :
2700 : namespace selftest {
2701 :
2702 : static void
2703 4 : test_uninit ()
2704 : {
2705 4 : symbolic::node_manager mgr;
2706 4 : auto &bit_cont_mgr = mgr.get_bit_contents_manager ();
2707 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
2708 4 : auto &uninit_bc = bit_cont_mgr.get_uninitialized ();
2709 12 : ASSERT_SAME_SYMBOLIC_NODE
2710 : (uninit_bc.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
2711 : bv_mgr.get_uninitialized_bytes (10_bytes));
2712 4 : }
2713 :
2714 : static void
2715 4 : test_untracked ()
2716 : {
2717 4 : symbolic::node_manager mgr;
2718 4 : auto &bit_cont_mgr = mgr.get_bit_contents_manager ();
2719 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
2720 4 : auto &untracked_bc = bit_cont_mgr.get_untracked ({});
2721 12 : ASSERT_SAME_SYMBOLIC_NODE
2722 : (untracked_bc.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
2723 : bv_mgr.get_unknown_bytes (10_bytes));
2724 :
2725 4 : ASSERT_SAME_SYMBOLIC_NODE
2726 : (untracked_bc,
2727 : bit_cont_mgr.get_untracked ({}));
2728 :
2729 4 : auto &custom_byte_a = bv_mgr.get_named_test_bit_vector (8_bits,
2730 4 : "custom_byte_a");
2731 4 : auto &custom_byte_b = bv_mgr.get_named_test_bit_vector (8_bits,
2732 4 : "custom_byte_b");
2733 :
2734 4 : value_set maybe_live_a;
2735 4 : maybe_live_a.add (&custom_byte_a);
2736 :
2737 4 : value_set maybe_live_b;
2738 4 : maybe_live_b.add (&custom_byte_b);
2739 :
2740 4 : ASSERT_NE (maybe_live_a, maybe_live_b);
2741 :
2742 4 : value_set maybe_live_ab;
2743 4 : maybe_live_ab.add (&custom_byte_a);
2744 4 : maybe_live_ab.add (&custom_byte_b);
2745 :
2746 4 : value_set maybe_live_ba;
2747 4 : maybe_live_ba.add (&custom_byte_b);
2748 4 : maybe_live_ba.add (&custom_byte_a);
2749 :
2750 : // Insertion order shouldn't matter
2751 4 : ASSERT_EQ (maybe_live_ab, maybe_live_ba);
2752 :
2753 4 : ASSERT_DIFFERENT_SYMBOLIC_NODE
2754 : (untracked_bc,
2755 : bit_cont_mgr.get_untracked (maybe_live_a));
2756 :
2757 4 : ASSERT_SAME_SYMBOLIC_NODE
2758 : (bit_cont_mgr.get_untracked (maybe_live_a),
2759 : bit_cont_mgr.get_untracked (maybe_live_a));
2760 :
2761 4 : ASSERT_DIFFERENT_SYMBOLIC_NODE
2762 : (bit_cont_mgr.get_untracked (maybe_live_a),
2763 : bit_cont_mgr.get_untracked (maybe_live_b));
2764 :
2765 4 : ASSERT_SAME_SYMBOLIC_NODE
2766 : (bit_cont_mgr.get_untracked (maybe_live_ab),
2767 : bit_cont_mgr.get_untracked (maybe_live_ba));
2768 4 : }
2769 :
2770 : static void
2771 4 : test_all_zeros ()
2772 : {
2773 4 : symbolic::node_manager mgr;
2774 4 : auto &bit_cont_mgr = mgr.get_bit_contents_manager ();
2775 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
2776 4 : auto &all_zeros_bc = bit_cont_mgr.get_all_zeros ();
2777 12 : ASSERT_SAME_SYMBOLIC_NODE
2778 : (all_zeros_bc.get_bytes (concrete_byte_range (0_bytes, 1024_bytes)),
2779 : bv_mgr.get_all_zeros (1024_bytes));
2780 4 : }
2781 :
2782 : /* Simulation of "char buf[10];" */
2783 :
2784 : static void
2785 4 : test_static_sized_char_array ()
2786 : {
2787 4 : symbolic::node_manager mgr;
2788 4 : auto &bit_cont_mgr = mgr.get_bit_contents_manager ();
2789 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
2790 :
2791 4 : const symbolic::bit_contents *zeroed_buf = &bit_cont_mgr.get_all_zeros ();
2792 4 : const symbolic::bit_contents *uninit_buf = &bit_cont_mgr.get_uninitialized ();
2793 :
2794 4 : {
2795 12 : auto &bv = zeroed_buf->get_bytes (concrete_byte_range (0_bytes, 10_bytes));
2796 4 : ASSERT_SAME_SYMBOLIC_NODE (bv,
2797 : bv_mgr.get_all_zeros (10_bytes));
2798 : }
2799 :
2800 4 : {
2801 12 : auto &bv = uninit_buf->get_bytes (concrete_byte_range (0_bytes, 10_bytes));
2802 4 : ASSERT_SAME_SYMBOLIC_NODE (bv,
2803 : bv_mgr.get_uninitialized_bytes (10_bytes));
2804 : }
2805 :
2806 12 : auto set_abc0 = [] (const auto *buf)
2807 : {
2808 8 : buf = &buf->set_byte (0_bytes, 'a', nullptr);
2809 8 : buf = &buf->set_byte (1_byte, 'b', nullptr);
2810 8 : buf = &buf->set_byte (2_bytes, 'c', nullptr);
2811 8 : buf = &buf->set_byte (3_bytes, '\0', nullptr);
2812 8 : return buf;
2813 : };
2814 :
2815 4 : zeroed_buf = set_abc0 (zeroed_buf);
2816 4 : uninit_buf = set_abc0 (uninit_buf);
2817 :
2818 4 : zeroed_buf->to_json ();
2819 4 : uninit_buf->to_json ();
2820 :
2821 4 : {
2822 12 : auto &bv = zeroed_buf->get_bytes (concrete_byte_range (0_bytes, 10_bytes));
2823 4 : ASSERT_SAME_SYMBOLIC_NODE
2824 : (bv,
2825 : bv_mgr.get_concat_bits
2826 : ({&bv_mgr.get_uint8 ('a'),
2827 : &bv_mgr.get_uint8 ('b'),
2828 : &bv_mgr.get_uint8 ('c'),
2829 : &bv_mgr.get_all_zeros (7_bytes)}));
2830 : }
2831 4 : {
2832 12 : auto &bv = uninit_buf->get_bytes (concrete_byte_range (0_bytes, 10_bytes));
2833 4 : ASSERT_SAME_SYMBOLIC_NODE
2834 : (bv,
2835 : bv_mgr.get_concat_bits
2836 : ({&bv_mgr.get_uint8 ('a'),
2837 : &bv_mgr.get_uint8 ('b'),
2838 : &bv_mgr.get_uint8 ('c'),
2839 : &bv_mgr.get_uint8 (0),
2840 : &bv_mgr.get_uninitialized_bytes (6_bytes)}));
2841 : }
2842 :
2843 : /* Verify scanning for zero bytes. */
2844 4 : {
2845 : /* For byte offsets [0, 3), both should find the NUL at offset 3. */
2846 24 : for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
2847 : {
2848 16 : ASSERT_EQ (zeroed_buf->scan_for_first_zero_byte (i),
2849 : string_scan::found_nul (3_bytes));
2850 16 : ASSERT_EQ (uninit_buf->scan_for_first_zero_byte (i),
2851 : string_scan::found_nul (3_bytes));
2852 4 : }
2853 :
2854 : /* Verify byte offsets outside that range (both negative and positive).
2855 : For zeroed_buf, we should find NUL at every offset.
2856 : For uninit_buf, we should report a problem at every offset. */
2857 4 : {
2858 : /* Negative byte offsets. */
2859 44 : for (concrete_byte_offset i = -10_bytes; i < 0_bytes; ++i)
2860 : {
2861 40 : ASSERT_EQ (zeroed_buf->scan_for_first_zero_byte (i),
2862 : string_scan::found_nul (i));
2863 40 : ASSERT_EQ (uninit_buf->scan_for_first_zero_byte (i),
2864 : string_scan::found_poison (i, poison_kind::uninit));
2865 4 : }
2866 32 : for (concrete_byte_offset i = 4_bytes; i < 10_bytes; ++i)
2867 : {
2868 24 : ASSERT_EQ (zeroed_buf->scan_for_first_zero_byte (i),
2869 : string_scan::found_nul (i));
2870 24 : ASSERT_EQ (uninit_buf->scan_for_first_zero_byte (i),
2871 : string_scan::found_poison (i, poison_kind::uninit));
2872 4 : }
2873 : }
2874 : }
2875 4 : }
2876 :
2877 : /* Construct a non-trivial bit_contents, then sample it as a bit_vector
2878 : at various ranges, including negative offsets. */
2879 :
2880 : static void
2881 4 : test_reading_nontrivial_bit_map ()
2882 : {
2883 4 : symbolic::node_manager mgr;
2884 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
2885 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
2886 :
2887 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
2888 :
2889 : // Check reading from the baseline contents:
2890 20 : ASSERT_SAME_SYMBOLIC_NODE
2891 : (unbound.get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
2892 : unbound.get_bytes (concrete_byte_range (0_bytes, 100_bytes)));
2893 :
2894 : /* Set up this binding:
2895 : | [-4]: 'x'
2896 : | [-3]: 'y'
2897 : | [-2]: 'z'
2898 : | [-1]: unbound
2899 : |[0 - 10): zero-filled (10 bytes)
2900 : | [10-16): unbound
2901 : | [16-32): 0xff-filled (16 bytes)
2902 : | [32): unbound
2903 : | [33]: 'a'
2904 : | [34): unbound
2905 : | [35]: 'b'
2906 : | [36): unbound
2907 : | [37]: 'c'
2908 : | [38]: 'c'
2909 : */
2910 4 : const symbolic::bit_contents *bmap = &unbound;
2911 4 : bmap = &bmap->set_byte (-4_bytes, 'x', nullptr);
2912 4 : bmap = &bmap->set_byte (-3_bytes, 'y', nullptr);
2913 4 : bmap = &bmap->set_byte (-2_bytes, 'z', nullptr);
2914 16 : bmap = &bmap->set_bytes (concrete_byte_range (0_bytes, 10_bytes),
2915 4 : bv_mgr.get_all_zeros (10_bytes),
2916 : nullptr);
2917 16 : bmap = &bmap->set_bytes (concrete_byte_range(16_bytes, 16_bytes),
2918 4 : bv_mgr.get_all_ones (16_bytes),
2919 : nullptr);
2920 4 : bmap = &bmap->set_byte (33_bytes, 'a', nullptr);
2921 4 : bmap = &bmap->set_byte (35_bytes, 'b', nullptr);
2922 4 : bmap = &bmap->set_byte (37_bytes, 'c', nullptr);
2923 4 : bmap = &bmap->set_byte (38_bytes, 'c', nullptr);
2924 :
2925 : // We should have a single concrete overlay over "unbound"
2926 4 : auto *conc_bit_overlay = bmap->dyn_cast_concrete_bit_overlay ();
2927 4 : ASSERT_NE (conc_bit_overlay, nullptr);
2928 4 : ASSERT_SAME_SYMBOLIC_NODE (conc_bit_overlay->get_baseline (),
2929 : unbound);
2930 :
2931 4 : bmap->to_json ();
2932 :
2933 : // Test individual bytes
2934 4 : {
2935 : /* Subroutines to determine if the initial/final bit of the byte at
2936 : the given offset is bound in conc_bit_overlay. */
2937 4 : auto initial_bit_offset_is_bound_p
2938 188 : = [conc_bit_overlay] (concrete_byte_offset byte_offset) -> bool
2939 : {
2940 188 : auto initial_bit_offset = bits_from_bytes (byte_offset);
2941 188 : return conc_bit_overlay->bit_offset_is_bound_p (initial_bit_offset);
2942 192 : };
2943 4 : auto final_bit_offset_is_bound_p
2944 8 : = [conc_bit_overlay] (concrete_byte_offset byte_offset) -> bool
2945 : {
2946 8 : auto initial_bit_offset = bits_from_bytes (byte_offset);
2947 8 : return conc_bit_overlay->bit_offset_is_bound_p
2948 8 : (initial_bit_offset + 7_bits);
2949 12 : };
2950 :
2951 : // A long way before the first binding:
2952 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (-100_bytes));
2953 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-100_bytes),
2954 : unbound.get_byte (-100_bytes));
2955 :
2956 : // Immediately before the first binding:
2957 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (-5_bytes));
2958 4 : ASSERT_FALSE (final_bit_offset_is_bound_p (-5_bytes));
2959 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-5_bytes),
2960 : unbound.get_byte (-5_bytes));
2961 :
2962 : // The xyz bindings:
2963 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (-4_bytes));
2964 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-4_bytes),
2965 : bv_mgr.get_uint8 ('x'));
2966 :
2967 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (-3_bytes));
2968 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-3_bytes),
2969 : bv_mgr.get_uint8 ('y'));
2970 :
2971 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (-2_bytes));
2972 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-2_bytes),
2973 : bv_mgr.get_uint8 ('z'));
2974 :
2975 : // Immediately after the xyz bindings:
2976 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (-1_byte));
2977 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-1_byte),
2978 : unbound.get_byte (-1_byte));
2979 :
2980 48 : for (auto i = 0_bytes; i < 10_bytes; ++i)
2981 : {
2982 40 : ASSERT_TRUE (initial_bit_offset_is_bound_p (i));
2983 40 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (i),
2984 : bv_mgr.get_uint8 (0));
2985 4 : }
2986 32 : for (auto i = 10_bytes; i < 16_bytes; ++i)
2987 : {
2988 24 : ASSERT_FALSE (initial_bit_offset_is_bound_p (i));
2989 24 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (i),
2990 : unbound.get_byte (i));
2991 4 : }
2992 72 : for (auto i = 16_bytes; i < 32_bytes; ++i)
2993 : {
2994 64 : ASSERT_TRUE (initial_bit_offset_is_bound_p (i));
2995 64 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (i),
2996 : bv_mgr.get_uint8 (0xff));
2997 4 : }
2998 :
2999 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (32_bytes));
3000 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (32_bytes),
3001 : unbound.get_byte (32_bytes));
3002 :
3003 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (33_bytes));
3004 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (33_bytes),
3005 : bv_mgr.get_uint8 ('a'));
3006 :
3007 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (34_bytes));
3008 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (34_bytes),
3009 : unbound.get_byte (34_bytes));
3010 :
3011 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (35_bytes));
3012 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (35_bytes),
3013 : bv_mgr.get_uint8 ('b'));
3014 :
3015 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (36_bytes));
3016 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (36_bytes),
3017 : unbound.get_byte (36_bytes));
3018 :
3019 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (37_bytes));
3020 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (37_bytes),
3021 : bv_mgr.get_uint8 ('c'));
3022 :
3023 4 : ASSERT_TRUE (initial_bit_offset_is_bound_p (38_bytes));
3024 4 : ASSERT_TRUE (final_bit_offset_is_bound_p (38_bytes));
3025 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (38_bytes),
3026 : bv_mgr.get_uint8 ('c'));
3027 :
3028 : // Immediately after the last binding:
3029 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (39_bytes));
3030 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (39_bytes),
3031 : unbound.get_byte (39_bytes));
3032 :
3033 : // A long way after the last binding:
3034 4 : ASSERT_FALSE (initial_bit_offset_is_bound_p (100_bytes));
3035 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (100_bytes),
3036 : unbound.get_byte (100_bytes));
3037 : }
3038 :
3039 : // Test ranges of bytes
3040 4 : {
3041 : /* Before the first binding. */
3042 20 : ASSERT_SAME_SYMBOLIC_NODE
3043 : (bmap->get_bytes (concrete_byte_range (-100_bytes, 10_bytes)),
3044 : unbound.get_bytes (concrete_byte_range (-100_bytes, 10_bytes)));
3045 :
3046 : /* Partial read of the zero bytes. */
3047 12 : ASSERT_SAME_SYMBOLIC_NODE
3048 : (bmap->get_bytes (concrete_byte_range (5_bytes, 2_bytes)),
3049 : bv_mgr.get_all_zeros (2_bytes));
3050 :
3051 : /* Test that the two neighboring 'c' chars are consolidated
3052 : when read as a bit vector. */
3053 12 : ASSERT_SAME_SYMBOLIC_NODE
3054 : (bmap->get_bytes (concrete_byte_range (37_bytes, 2_bytes)),
3055 : bv_mgr.get_repeated_bits (bv_mgr.get_uint8 ('c'), 2));
3056 :
3057 : /* After the last binding. */
3058 20 : ASSERT_SAME_SYMBOLIC_NODE
3059 : (bmap->get_bytes (concrete_byte_range (100_bytes, 10_bytes)),
3060 : unbound.get_bytes (concrete_byte_range (100_bytes, 10_bytes)));
3061 : }
3062 :
3063 : /* Verify scanning for NUL bytes. */
3064 4 : {
3065 : // A long way before the first binding:
3066 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (-100_bytes),
3067 : string_scan::cannot_scan (-100_bytes));
3068 :
3069 : // Immediately before the first binding:
3070 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (-5_bytes),
3071 : string_scan::cannot_scan (-5_bytes));
3072 :
3073 : // Scanning the xyz bindings should fail, at the "unbound" successor.
3074 16 : for (auto i = -4_bytes; i < -1_byte; ++i)
3075 12 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3076 4 : string_scan::cannot_scan (-1_byte));
3077 :
3078 : // Immediately after the xyz bindings:
3079 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (-1_byte),
3080 : string_scan::cannot_scan (-1_byte));
3081 :
3082 48 : for (auto i = 0_bytes; i < 10_bytes; ++i)
3083 40 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3084 4 : string_scan::found_nul (i));
3085 32 : for (auto i = 10_bytes; i < 16_bytes; ++i)
3086 24 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3087 4 : string_scan::cannot_scan (i));
3088 :
3089 : /* Scanning after that point should hit an unbound byte
3090 : and thus be unable to scan there. */
3091 :
3092 : // Scans that should hit the unbound byte 32:
3093 72 : for (auto i = 16_bytes; i < 32_bytes; ++i)
3094 64 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3095 4 : string_scan::cannot_scan (32_bytes));
3096 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (32_bytes),
3097 : string_scan::cannot_scan (32_bytes));
3098 :
3099 : // Scans that should hit the unbound byte 34:
3100 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (33_bytes),
3101 : string_scan::cannot_scan (34_bytes));
3102 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (34_bytes),
3103 : string_scan::cannot_scan (34_bytes));
3104 :
3105 : // Scans that should hit the unbound byte 36:
3106 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (35_bytes),
3107 : string_scan::cannot_scan (36_bytes));
3108 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (36_bytes),
3109 : string_scan::cannot_scan (36_bytes));
3110 :
3111 : // Scans that should hit the unbound bytes 39 onwards:
3112 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (37_bytes),
3113 : string_scan::cannot_scan (39_bytes));
3114 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (38_bytes),
3115 : string_scan::cannot_scan (39_bytes));
3116 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (39_bytes),
3117 : string_scan::cannot_scan (39_bytes));
3118 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (40_bytes),
3119 : string_scan::cannot_scan (40_bytes));
3120 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (100_bytes),
3121 : string_scan::cannot_scan (100_bytes));
3122 : }
3123 4 : }
3124 :
3125 : /* Test of two successive updates to a bitmap which don't overlap,
3126 : with a gap. */
3127 :
3128 : static void
3129 4 : test_update_no_overlap_with_gap ()
3130 : {
3131 4 : symbolic::node_manager mgr;
3132 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3133 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3134 :
3135 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3136 :
3137 4 : const symbolic::bit_contents *bmap = &unbound;
3138 :
3139 : /* Fill [20, 35) with a 15-byte custom bit vector. */
3140 4 : auto &first = bv_mgr.get_named_test_bit_vector (15_bytes, "first");
3141 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 15_bytes),
3142 : first, nullptr);
3143 12 : ASSERT_SAME_SYMBOLIC_NODE
3144 : (bmap->get_bytes (concrete_byte_range (20_bytes, 15_bytes)),
3145 : first);
3146 28 : ASSERT_SAME_SYMBOLIC_NODE
3147 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3148 : bv_mgr.get_concat_bits
3149 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3150 : &first,
3151 : &unbound.get_bytes (concrete_byte_range (35_bytes, 65_bytes))}));
3152 :
3153 : /* Fill [40, 90) with a second 50-byte custom vector.
3154 : They don't overlap, so this should give us:
3155 : [20, 35) : first
3156 : [40, 90) : second. */
3157 4 : auto &second = bv_mgr.get_named_test_bit_vector (50_bytes, "second");
3158 12 : bmap = &bmap->set_bytes (concrete_byte_range (40_bytes, 50_bytes),
3159 : second,
3160 : nullptr);
3161 4 : bmap->to_json ();
3162 :
3163 12 : ASSERT_SAME_SYMBOLIC_NODE
3164 : (bmap->get_bytes (concrete_byte_range (20_bytes, 15_bytes)),
3165 : first);
3166 12 : ASSERT_SAME_SYMBOLIC_NODE
3167 : (bmap->get_bytes (concrete_byte_range (40_bytes, 50_bytes)),
3168 : second);
3169 36 : ASSERT_SAME_SYMBOLIC_NODE
3170 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3171 : bv_mgr.get_concat_bits
3172 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3173 : &first,
3174 : &unbound.get_bytes (concrete_byte_range (35_bytes, 5_bytes)),
3175 : &second,
3176 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3177 4 : }
3178 :
3179 : /* Test of two successive updates to a bitmap which don't overlap,
3180 : but do touch. */
3181 :
3182 : static void
3183 4 : test_update_no_overlap_touching ()
3184 : {
3185 4 : symbolic::node_manager mgr;
3186 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3187 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3188 :
3189 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3190 :
3191 4 : const symbolic::bit_contents *bmap = &unbound;
3192 :
3193 : /* Starts as unbound. */
3194 20 : ASSERT_SAME_SYMBOLIC_NODE
3195 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3196 : unbound.get_bytes (concrete_byte_range (0_bytes, 100_bytes)));
3197 :
3198 : /* Fill [20, 35) with a 15-byte custom bit vector. */
3199 4 : auto &first = bv_mgr.get_named_test_bit_vector (15_bytes, "first");
3200 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 15_bytes),
3201 : first,
3202 : nullptr);
3203 12 : ASSERT_SAME_SYMBOLIC_NODE
3204 : (bmap->get_bytes (concrete_byte_range (20_bytes, 15_bytes)),
3205 : first);
3206 28 : ASSERT_SAME_SYMBOLIC_NODE
3207 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3208 : bv_mgr.get_concat_bits
3209 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3210 : &first,
3211 : &unbound.get_bytes (concrete_byte_range (35_bytes, 65_bytes))}));
3212 :
3213 : /* Fill [35, 75) with with a second custom vector (40 bytes).
3214 : They don't overlap, so this should give us:
3215 : [20, 35) : first
3216 : [35, 75) : second. */
3217 4 : auto &second = bv_mgr.get_named_test_bit_vector (40_bytes, "second");
3218 12 : bmap = &bmap->set_bytes (concrete_byte_range (35_bytes, 40_bytes),
3219 : second,
3220 : nullptr);
3221 4 : bmap->to_json ();
3222 12 : ASSERT_SAME_SYMBOLIC_NODE
3223 : (bmap->get_bytes (concrete_byte_range (20_bytes, 15_bytes)),
3224 : first);
3225 12 : ASSERT_SAME_SYMBOLIC_NODE
3226 : (bmap->get_bytes (concrete_byte_range (35_bytes, 40_bytes)),
3227 : second);
3228 28 : ASSERT_SAME_SYMBOLIC_NODE
3229 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3230 : bv_mgr.get_concat_bits
3231 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3232 : &first,
3233 : &second,
3234 : &unbound.get_bytes (concrete_byte_range (75_bytes, 25_bytes))}));
3235 4 : }
3236 :
3237 : /* Consider:
3238 : | +------------------------------------+
3239 : | | old binding |
3240 : | +------------------------------------+
3241 : then writing to:
3242 : | +----------------------+
3243 : | | new binding |
3244 : | +----------------------+
3245 :
3246 : Verify that we "punch a hole" and fill it with the new binding:
3247 : | +------+----------------------+------+
3248 : | |prefix| new binding |suffix|
3249 : | +------+----------------------+------+
3250 : */
3251 :
3252 : static void
3253 4 : test_update_punching_a_hole ()
3254 : {
3255 4 : symbolic::node_manager mgr;
3256 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3257 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3258 :
3259 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3260 :
3261 4 : const symbolic::bit_contents *bmap = &unbound;
3262 :
3263 : /* Fill [20, 90) with a 70-byte custom bit vector. */
3264 4 : auto &first = bv_mgr.get_named_test_bit_vector (70_bytes, "first");
3265 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 70_bytes),
3266 : first,
3267 : nullptr);
3268 12 : ASSERT_SAME_SYMBOLIC_NODE
3269 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3270 : first);
3271 28 : ASSERT_SAME_SYMBOLIC_NODE
3272 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3273 : bv_mgr.get_concat_bits
3274 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3275 : &first,
3276 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3277 :
3278 : /* Fill [25, 75) with a second bit vector (50 byte), punching a hole
3279 : in "first"
3280 : This should give us:
3281 : [20, 25) : first 5 bytes of first
3282 : [25, 75) : "second"
3283 : [75, 90) : last 15 bytes of first. */
3284 4 : auto &second = bv_mgr.get_named_test_bit_vector (50_bytes, "second");
3285 12 : bmap = &bmap->set_bytes (concrete_byte_range (25_bytes, 50_bytes),
3286 : second,
3287 : nullptr);
3288 : // Test what's now in the range that "first" was stored to:
3289 12 : ASSERT_SAME_SYMBOLIC_NODE
3290 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3291 : bv_mgr.get_concat_bits
3292 : ({&first.get_prefix (5_bytes),
3293 : &second,
3294 : &first.get_suffix (15_bytes)}));
3295 : // Test what's in the range that "second" was stored to:
3296 12 : ASSERT_SAME_SYMBOLIC_NODE
3297 : (bmap->get_bytes (concrete_byte_range (25_bytes, 50_bytes)),
3298 : second);
3299 : // Test everything:
3300 28 : ASSERT_SAME_SYMBOLIC_NODE
3301 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3302 : bv_mgr.get_concat_bits
3303 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3304 : &first.get_prefix (5_bytes),
3305 : &second,
3306 : &first.get_suffix (15_bytes),
3307 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3308 4 : }
3309 :
3310 : /* Verify the effect of a pair of overwrites to the same range. */
3311 :
3312 : static void
3313 4 : test_update_full_overwrite ()
3314 : {
3315 4 : symbolic::node_manager mgr;
3316 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3317 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3318 :
3319 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3320 :
3321 4 : const symbolic::bit_contents *bmap = &unbound;
3322 :
3323 : /* Fill [20, 90) with a 70-byte custom bit vector. */
3324 4 : auto &first = bv_mgr.get_named_test_bit_vector (70_bytes, "first");
3325 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 70_bytes),
3326 : first,
3327 : nullptr);
3328 12 : ASSERT_SAME_SYMBOLIC_NODE
3329 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3330 : first);
3331 28 : ASSERT_SAME_SYMBOLIC_NODE
3332 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3333 : bv_mgr.get_concat_bits
3334 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3335 : &first,
3336 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3337 :
3338 : /* Overwrite it with a different 70-byte custom bit vector. */
3339 4 : auto &second = bv_mgr.get_named_test_bit_vector (70_bytes, "second");
3340 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 70_bytes),
3341 : second,
3342 : nullptr);
3343 12 : ASSERT_SAME_SYMBOLIC_NODE
3344 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3345 : second);
3346 28 : ASSERT_SAME_SYMBOLIC_NODE
3347 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3348 : bv_mgr.get_concat_bits
3349 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3350 : &second,
3351 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3352 4 : }
3353 :
3354 : /* Verify the correct handling of this pair of writes:
3355 : | +-----------------------+
3356 : | |first |
3357 : | +-----------------------+
3358 : | +----------------------+
3359 : | | second |
3360 : | +----------------------+
3361 :
3362 : We should end up with:
3363 : | +------+----------------------+
3364 : | |prefix| second |
3365 : | +------+----------------------+
3366 : */
3367 :
3368 : static void
3369 4 : test_update_overlap_with_prefix ()
3370 : {
3371 4 : symbolic::node_manager mgr;
3372 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3373 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3374 :
3375 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3376 :
3377 4 : const symbolic::bit_contents *bmap = &unbound;
3378 :
3379 : /* Fill [20, 90) with a 70-byte custom bit vector. */
3380 4 : auto &first = bv_mgr.get_named_test_bit_vector (70_bytes, "first");
3381 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 70_bytes),
3382 : first,
3383 : nullptr);
3384 12 : ASSERT_SAME_SYMBOLIC_NODE
3385 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3386 : first);
3387 28 : ASSERT_SAME_SYMBOLIC_NODE
3388 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3389 : bv_mgr.get_concat_bits
3390 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3391 : &first,
3392 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3393 :
3394 : /* Fill [30, 95) with a second bvec.
3395 : This should give us:
3396 : [20, 30) : first 10 bytes of first
3397 : [30, 95): second. */
3398 4 : auto &second = bv_mgr.get_named_test_bit_vector (65_bytes, "second");
3399 12 : bmap = &bmap->set_bytes (concrete_byte_range (30_bytes, 65_bytes),
3400 : second,
3401 : nullptr);
3402 : // Test what's now in the range that "first" was stored to:
3403 12 : ASSERT_SAME_SYMBOLIC_NODE
3404 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3405 : bv_mgr.get_concat_bits
3406 : ({&first.get_prefix (10_bytes),
3407 : &second.get_prefix (60_bytes)}));
3408 : // Test what's in the range that "second" was stored to:
3409 12 : ASSERT_SAME_SYMBOLIC_NODE
3410 : (bmap->get_bytes (concrete_byte_range (30_bytes, 65_bytes)),
3411 : second);
3412 : // Test everything:
3413 28 : ASSERT_SAME_SYMBOLIC_NODE
3414 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3415 : bv_mgr.get_concat_bits
3416 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3417 : &first.get_prefix (10_bytes),
3418 : &second,
3419 : &unbound.get_bytes (concrete_byte_range (95_bytes, 5_bytes))}));
3420 4 : }
3421 :
3422 : /* Verify the correct handling of this pair of writes:
3423 : | +-----------------------+
3424 : | |first |
3425 : | +-----------------------+
3426 : | +----------------------+
3427 : | | second |
3428 : | +----------------------+
3429 :
3430 : We should end up with:
3431 : | +----------------------+------+
3432 : | | second |suffix|
3433 : | +----------------------+------+
3434 : */
3435 :
3436 : static void
3437 4 : test_update_overlap_with_suffix ()
3438 : {
3439 4 : symbolic::node_manager mgr;
3440 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3441 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3442 :
3443 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3444 :
3445 4 : const symbolic::bit_contents *bmap = &unbound;
3446 :
3447 : /* Fill [20, 90) with a 70-byte custom bit vector. */
3448 4 : auto &first = bv_mgr.get_named_test_bit_vector (70_bytes, "first");
3449 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 70_bytes),
3450 : first,
3451 : nullptr);
3452 12 : ASSERT_SAME_SYMBOLIC_NODE
3453 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3454 : first);
3455 28 : ASSERT_SAME_SYMBOLIC_NODE
3456 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3457 : bv_mgr.get_concat_bits
3458 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3459 : &first,
3460 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3461 :
3462 : /* Fill [5, 70) with a second bvec.
3463 : This should give us:
3464 : [5, 70) : second.
3465 : [70, 90): last 20 bytes of first. */
3466 4 : auto &second = bv_mgr.get_named_test_bit_vector (65_bytes, "second");
3467 12 : bmap = &bmap->set_bytes (concrete_byte_range (5_bytes, 65_bytes),
3468 : second,
3469 : nullptr);
3470 :
3471 : // Test what's now in the range that "first" was stored to:
3472 12 : ASSERT_SAME_SYMBOLIC_NODE
3473 : (bmap->get_bytes (concrete_byte_range (20_bytes, 70_bytes)),
3474 : bv_mgr.get_concat_bits
3475 : ({&second.get_suffix (50_bytes),
3476 : &first.get_suffix (20_bytes)}));
3477 : // Test what's in the range that "second" was stored to:
3478 12 : ASSERT_SAME_SYMBOLIC_NODE
3479 : (bmap->get_bytes (concrete_byte_range (5_bytes, 65_bytes)),
3480 : second);
3481 : // Test everything:
3482 28 : ASSERT_SAME_SYMBOLIC_NODE
3483 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3484 : bv_mgr.get_concat_bits
3485 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 5_bytes)),
3486 : &second,
3487 : &first.get_suffix (20_bytes),
3488 : &unbound.get_bytes (concrete_byte_range (90_bytes, 10_bytes))}));
3489 4 : }
3490 :
3491 : /* Verify the correct handling of this bit_map:
3492 : | +-------+ +-------+ +-------+ +-------+ +-------+
3493 : | | A | | B | | C | | D | | E |
3494 : | +-------+ +-------+ +-------+ +-------+ +-------+
3495 : when overwritten with:
3496 : | +----------------------+
3497 : | | F |
3498 : | +----------------------+
3499 :
3500 : We should end up with:
3501 : | +-------+ +---+----------------------+----+ +-------+
3502 : | | A | | B'| F | D' | | E |
3503 : | +-------+ +---+----------------------+----+ +-------+
3504 : */
3505 :
3506 : static void
3507 4 : test_update_multiple_overlaps ()
3508 : {
3509 4 : symbolic::node_manager mgr;
3510 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3511 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3512 :
3513 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3514 :
3515 4 : const symbolic::bit_contents *bmap = &unbound;
3516 :
3517 : /* Populate it with:
3518 : [10,15): A
3519 : [20,30): B
3520 : [40,60): C
3521 : [70,80): D
3522 : [90,95): E. */
3523 4 : auto &a = bv_mgr.get_named_test_bit_vector (5_bytes, "A");
3524 12 : bmap = &bmap->set_bytes (concrete_byte_range (10_bytes, 5_bytes),
3525 : a, nullptr);
3526 4 : auto &b = bv_mgr.get_named_test_bit_vector (10_bytes, "B");
3527 12 : bmap = &bmap->set_bytes (concrete_byte_range (20_bytes, 10_bytes),
3528 : b, nullptr);
3529 4 : auto &c = bv_mgr.get_named_test_bit_vector (20_bytes, "C");
3530 12 : bmap = &bmap->set_bytes (concrete_byte_range (40_bytes, 20_bytes),
3531 : c, nullptr);
3532 4 : auto &d = bv_mgr.get_named_test_bit_vector (10_bytes, "D");
3533 12 : bmap = &bmap->set_bytes (concrete_byte_range (70_bytes, 10_bytes),
3534 : d, nullptr);
3535 4 : auto &e = bv_mgr.get_named_test_bit_vector (5_bytes, "E");
3536 12 : bmap = &bmap->set_bytes (concrete_byte_range (90_bytes, 5_bytes),
3537 : e, nullptr);
3538 :
3539 60 : ASSERT_SAME_SYMBOLIC_NODE
3540 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3541 : bv_mgr.get_concat_bits
3542 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3543 : &a,
3544 : &unbound.get_bytes (concrete_byte_range (15_bytes, 5_bytes)),
3545 : &b,
3546 : &unbound.get_bytes (concrete_byte_range (30_bytes, 10_bytes)),
3547 : &c,
3548 : &unbound.get_bytes (concrete_byte_range (60_bytes, 10_bytes)),
3549 : &d,
3550 : &unbound.get_bytes (concrete_byte_range (80_bytes, 10_bytes)),
3551 : &e,
3552 : &unbound.get_bytes (concrete_byte_range (95_bytes, 5_bytes))}));
3553 :
3554 : /* Overwrite [25, 75) with F. */
3555 4 : auto &f = bv_mgr.get_named_test_bit_vector (50_bytes, "F");
3556 12 : bmap = &bmap->set_bytes (concrete_byte_range (25_bytes, 50_bytes),
3557 : f, nullptr);
3558 :
3559 44 : ASSERT_SAME_SYMBOLIC_NODE
3560 : (bmap->get_bytes (concrete_byte_range (0_bytes, 100_bytes)),
3561 : bv_mgr.get_concat_bits
3562 : ({&unbound.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3563 : &a,
3564 : &unbound.get_bytes (concrete_byte_range (15_bytes, 5_bytes)),
3565 : &b.get_prefix (5_bytes),
3566 : &f,
3567 : &d.get_suffix (5_bytes),
3568 : &unbound.get_bytes (concrete_byte_range (80_bytes, 10_bytes)),
3569 : &e,
3570 : &unbound.get_bytes (concrete_byte_range (95_bytes, 5_bytes))}));
3571 4 : }
3572 :
3573 : static void
3574 4 : test_uint32 ()
3575 : {
3576 4 : symbolic::node_manager mgr;
3577 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3578 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3579 4 : auto &rmm = mgr.get_region_model_manager ();
3580 :
3581 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3582 :
3583 4 : const symbolic::bit_contents *bmap = &unbound;
3584 :
3585 16 : bmap = &bmap->set_bytes (concrete_byte_range (0_bytes, 4_bytes),
3586 4 : bv_mgr.get_all_zeros (32_bits),
3587 : nullptr);
3588 :
3589 4 : auto &zero_byte = bv_mgr.get_uint8 (0);
3590 :
3591 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (-1_byte),
3592 : unbound.get_byte (-1_byte));
3593 :
3594 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (0_bytes),
3595 : zero_byte);
3596 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (1_byte),
3597 : zero_byte);
3598 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (2_bytes),
3599 : zero_byte);
3600 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (3_bytes),
3601 : zero_byte);
3602 :
3603 4 : ASSERT_SAME_SYMBOLIC_NODE (bmap->get_byte (4_bytes),
3604 : unbound.get_byte (4_bytes));
3605 :
3606 : // Read the zero, with a gap at both start and end:
3607 28 : ASSERT_SAME_SYMBOLIC_NODE
3608 : (bmap->get_bytes (concrete_byte_range (-2_bytes, 8_bytes)),
3609 : bv_mgr.get_concat_bits
3610 : ({&unbound.get_bytes (concrete_byte_range (-2_bytes, 2_bytes)),
3611 : &bv_mgr.get_all_zeros (4_bytes),
3612 : &unbound.get_bytes (concrete_byte_range (4_bytes, 2_bytes))}));
3613 :
3614 : // Read the zero bytes as an uint32_t svalue.
3615 4 : {
3616 12 : auto &bvec = bmap->get_bytes (concrete_byte_range (0_bytes, 4_bytes));
3617 4 : auto &sval = *rmm.get_svalue_from_bit_vector (uint32_type_node, bvec);
3618 4 : ASSERT_SAME_SYMBOLIC_NODE
3619 : (sval,
3620 : *rmm.get_or_create_int_cst (uint32_type_node, 0));
3621 : }
3622 :
3623 : // Store (uint32_t)42, and try reading it back.
3624 4 : {
3625 4 : auto &int_42 = *rmm.get_or_create_int_cst (uint32_type_node, 42);
3626 12 : bmap = &bmap->set_bytes (concrete_byte_range (0_bytes, 4_bytes),
3627 : bv_mgr.get_bit_vector_from_svalue (int_42),
3628 : nullptr);
3629 :
3630 12 : auto &bvec = bmap->get_bytes (concrete_byte_range (0_bytes, 4_bytes));
3631 4 : auto &sval = *rmm.get_svalue_from_bit_vector (uint32_type_node, bvec);
3632 4 : ASSERT_SAME_SYMBOLIC_NODE (sval, int_42);
3633 : }
3634 4 : }
3635 :
3636 : static void
3637 48 : assert_byte_eq (const ::selftest::location &loc,
3638 : const symbolic::bit_contents *bmap,
3639 : concrete_byte_offset byte_offset,
3640 : uint8_t expected_value,
3641 : symbolic::node_manager &mgr)
3642 : {
3643 48 : auto &rmm = mgr.get_region_model_manager ();
3644 48 : auto &actual_bvec = bmap->get_byte (byte_offset);
3645 48 : auto &actual_sval
3646 48 : = *rmm.get_svalue_from_bit_vector (unsigned_char_type_node, actual_bvec);
3647 48 : auto &expected_sval
3648 48 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, expected_value);
3649 48 : ASSERT_SAME_SYMBOLIC_NODE_AT (loc, actual_sval, expected_sval);
3650 48 : }
3651 :
3652 : #define ASSERT_BYTE_EQ(BMAP, BYTE_OFFSET, EXPECTED_VALUE, NODE_MGR) \
3653 : SELFTEST_BEGIN_STMT \
3654 : assert_byte_eq (SELFTEST_LOCATION, (BMAP), (BYTE_OFFSET), \
3655 : (EXPECTED_VALUE), (NODE_MGR)); \
3656 : SELFTEST_END_STMT
3657 :
3658 : /* Consider these 12 bytes:
3659 :
3660 : struct foo {
3661 : uint32_t x;
3662 : uint32_t y;
3663 : char str[]
3664 : } f = {0x00000042, 0x12345678, "abc"};
3665 :
3666 : where we thus have:
3667 :
3668 : uint32_t x : 0x00000042
3669 : uint32_t y : 0x12345678
3670 : char str[] : "abc\0"
3671 :
3672 : +------------------------------+--------------------------------------------+
3673 : | Field: | Bytes: |
3674 : | +--------+-----------------+-----------------+
3675 : | | | Big-endian | Little-endian |
3676 : +------+----------+------------+--------+-----+-----------|-----+-----------+
3677 : | name | type | value | offset | hex | binary | hex | binary |
3678 : +------+----------+------------+--------+-----+-----------|-----+-----------+
3679 : | x | uint32_t | 0x00000042 | [0] | 00 | 0000 0000 | 42 | 0100 0010 |
3680 : | | | | [1] | 00 | 0000 0000 | 00 | 0000 0000 |
3681 : | | | | [2] | 00 | 0000 0000 | 00 | 0000 0000 |
3682 : | | | | [3] | 42 | 0100 0010 | 00 | 0000 0000 |
3683 : +------+----------+------------+--------+-----+-----------|-----+-----------+
3684 : | y | uint32_t | 0x12345678 | [4] | 12 | 0001 0010 | 78 | 0111 1000 |
3685 : | | | | [5] | 34 | 0011 0100 | 56 | 0101 0110 |
3686 : | | | | [6] | 56 | 0101 0110 | 34 | 0011 0100 |
3687 : | | | | [7] | 78 | 0111 1000 | 12 | 0001 0010 |
3688 : +------+----------+------------+--------+-----+-----------+-----+-----------+
3689 : | str | char[] | "abc\0" | [8] | 61 | 0110 0001 | 61 | 0110 0001 |
3690 : | | | | [9] | 62 | 0110 0010 | 62 | 0110 0010 |
3691 : | | | | [10] | 63 | 0110 0011 | 63 | 0110 0011 |
3692 : | | | | [11] | 00 | 0000 0000 | 00 | 0000 0000 |
3693 : +------+----------+------------+--------+-----+-----------+-----+-----------+
3694 :
3695 : Verify that we can store/retrieve these values, and then try type-punning
3696 : it all to bytes as per the target's endianness. */
3697 :
3698 : static void
3699 4 : test_endianness ()
3700 : {
3701 4 : symbolic::node_manager mgr;
3702 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3703 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3704 4 : auto &rmm = mgr.get_region_model_manager ();
3705 :
3706 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3707 :
3708 4 : const symbolic::bit_contents *bmap = &unbound;
3709 :
3710 : // Populate bmap as above.
3711 4 : auto &int_0x42 = *rmm.get_or_create_int_cst (uint32_type_node, 0x42);
3712 4 : bmap = &bmap->set_bytes
3713 12 : (concrete_byte_range (0_bytes, 4_bytes),
3714 : bv_mgr.get_bit_vector_from_svalue (int_0x42),
3715 : nullptr);
3716 4 : auto &int_0x12345678
3717 4 : = *rmm.get_or_create_int_cst (uint32_type_node, 0x12345678);
3718 4 : bmap = &bmap->set_bytes
3719 12 : (concrete_byte_range (4_bytes, 4_bytes),
3720 : bv_mgr.get_bit_vector_from_svalue (int_0x12345678),
3721 : nullptr);
3722 4 : bmap = &bmap->set_byte (8_bytes, 'a', nullptr);
3723 4 : bmap = &bmap->set_byte (9_bytes, 'b', nullptr);
3724 4 : bmap = &bmap->set_byte (10_bytes, 'c', nullptr);
3725 4 : bmap = &bmap->set_byte (11_bytes, 0, nullptr);
3726 :
3727 : // Try reading the values back, using the types we used when setting them
3728 4 : {
3729 4 : {
3730 12 : auto &bvec = bmap->get_bytes (concrete_byte_range (0_bytes, 4_bytes));
3731 4 : auto &sval = *rmm.get_svalue_from_bit_vector (uint32_type_node, bvec);
3732 4 : ASSERT_SAME_SYMBOLIC_NODE (sval, int_0x42);
3733 : }
3734 4 : {
3735 12 : auto &bvec = bmap->get_bytes (concrete_byte_range (4_bytes, 4_bytes));
3736 4 : auto &sval = *rmm.get_svalue_from_bit_vector (uint32_type_node, bvec);
3737 4 : ASSERT_SAME_SYMBOLIC_NODE (sval, int_0x12345678);
3738 : }
3739 4 : ASSERT_BYTE_EQ (bmap, 8_bytes, 'a', mgr);
3740 4 : ASSERT_BYTE_EQ (bmap, 9_bytes, 'b', mgr);
3741 4 : ASSERT_BYTE_EQ (bmap, 10_bytes, 'c', mgr);
3742 4 : ASSERT_BYTE_EQ (bmap, 11_bytes, 0, mgr);
3743 : }
3744 :
3745 : // Try type-punning the uint32_t fields to bytes
3746 4 : {
3747 4 : if (BYTES_BIG_ENDIAN)
3748 : {
3749 : /* Big-endian. */
3750 : ASSERT_BYTE_EQ (bmap, 0_bytes, 0x00, mgr);
3751 : ASSERT_BYTE_EQ (bmap, 1_byte, 0x00, mgr);
3752 : ASSERT_BYTE_EQ (bmap, 2_bytes, 0x00, mgr);
3753 : ASSERT_BYTE_EQ (bmap, 3_bytes, 0x42, mgr);
3754 :
3755 : ASSERT_BYTE_EQ (bmap, 4_bytes, 0x12, mgr);
3756 : ASSERT_BYTE_EQ (bmap, 5_bytes, 0x34, mgr);
3757 : ASSERT_BYTE_EQ (bmap, 6_bytes, 0x56, mgr);
3758 : ASSERT_BYTE_EQ (bmap, 7_bytes, 0x78, mgr);
3759 : }
3760 : else
3761 : {
3762 : /* Little-endian. */
3763 4 : if (!WORDS_BIG_ENDIAN)
3764 : {
3765 4 : ASSERT_BYTE_EQ (bmap, 0_bytes, 0x42, mgr);
3766 4 : ASSERT_BYTE_EQ (bmap, 1_byte, 0x00, mgr);
3767 4 : ASSERT_BYTE_EQ (bmap, 2_bytes, 0x00, mgr);
3768 4 : ASSERT_BYTE_EQ (bmap, 3_bytes, 0x00, mgr);
3769 :
3770 4 : ASSERT_BYTE_EQ (bmap, 4_bytes, 0x78, mgr);
3771 4 : ASSERT_BYTE_EQ (bmap, 5_bytes, 0x56, mgr);
3772 4 : ASSERT_BYTE_EQ (bmap, 6_bytes, 0x34, mgr);
3773 4 : ASSERT_BYTE_EQ (bmap, 7_bytes, 0x12, mgr);
3774 : }
3775 : }
3776 : }
3777 :
3778 : // Try scanning for zero bytes
3779 4 : if (BYTES_BIG_ENDIAN)
3780 : {
3781 : /* Big-endian. */
3782 : /* First 3 bytes are all zero. */
3783 : for (auto i = 0_bytes; i < 3_bytes; ++i)
3784 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3785 : string_scan::found_nul (i));
3786 : /* Then non-zero until byte 11. */
3787 : for (auto i = 4_bytes; i < 11_bytes; ++i)
3788 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3789 : string_scan::found_nul (11_bytes));
3790 : }
3791 : else
3792 : {
3793 : /* Little-endian. */
3794 4 : if (!WORDS_BIG_ENDIAN)
3795 : {
3796 : /* First byte is non-zero (42). */
3797 4 : ASSERT_EQ (bmap->scan_for_first_zero_byte (0_bytes),
3798 : string_scan::found_nul (1_byte));
3799 : /* Then 3 zero bytes. */
3800 20 : for (auto i = 1_byte; i < 4_bytes; ++i)
3801 12 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3802 4 : string_scan::found_nul (i));
3803 : /* Then non-zero until byte 11. */
3804 36 : for (auto i = 4_bytes; i < 11_bytes; ++i)
3805 28 : ASSERT_EQ (bmap->scan_for_first_zero_byte (i),
3806 4 : string_scan::found_nul (11_bytes));
3807 : }
3808 : }
3809 4 : }
3810 :
3811 : /* Consider:
3812 : | struct foo {
3813 : | uint32_t x;
3814 : | uint32_t y;
3815 : | } f;
3816 : | f.x = 10;
3817 : | f.y = 42;
3818 : Verify that we canonicalize this as two adjacent bindings of
3819 : bit_vectors, rather than one binding to a concat bit_vector. */
3820 :
3821 : static void
3822 4 : test_canonicalize_concat ()
3823 : {
3824 4 : symbolic::node_manager mgr;
3825 4 : auto &rmm = mgr.get_region_model_manager ();
3826 4 : auto &int_10 = *rmm.get_or_create_int_cst (uint32_type_node, 10);
3827 4 : auto &int_42 = *rmm.get_or_create_int_cst (uint32_type_node, 42);
3828 :
3829 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3830 4 : auto &bv_10 = bv_mgr.get_bit_vector_from_svalue (int_10);
3831 4 : auto &bv_42 = bv_mgr.get_bit_vector_from_svalue (int_42);
3832 :
3833 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3834 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3835 :
3836 : /* Approach A: create it as two 4-byte bindings. */
3837 4 : const symbolic::bit_contents *bmap_a = &unbound;
3838 12 : bmap_a = &bmap_a->set_bytes (concrete_byte_range (0_bytes, 4_bytes),
3839 : bv_10, nullptr);
3840 12 : bmap_a = &bmap_a->set_bytes (concrete_byte_range(4_bytes, 4_bytes),
3841 : bv_42, nullptr);
3842 :
3843 : /* Approach B: create it as one 8-byte binding. */
3844 4 : const symbolic::bit_contents *bmap_b = &unbound;
3845 4 : auto &concat_bv
3846 4 : = bv_mgr.get_concat_bits ({&bv_10, &bv_42});
3847 12 : bmap_b = &bmap_b->set_bytes (concrete_byte_range (0_bytes, 8_bytes),
3848 : concat_bv, nullptr);
3849 :
3850 : /* Verify that they are canonicalized to the same symbol. */
3851 4 : ASSERT_SAME_SYMBOLIC_NODE (*bmap_a, *bmap_b);
3852 :
3853 : /* Verify that internally we used approach A. */
3854 4 : ASSERT_EQ (bmap_a->dyn_cast_concrete_bit_overlay ()->size (), 2);
3855 4 : }
3856 :
3857 : /* Consider a map of (10 bytes of zeros) then (5 bytes of zeros)
3858 : as opposed to a map of (15 bytes of zeros). */
3859 :
3860 : static void
3861 4 : test_consolidation ()
3862 : {
3863 4 : symbolic::node_manager mgr;
3864 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3865 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3866 :
3867 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
3868 :
3869 : /* Approach A: create it as two adjacent bindings. */
3870 4 : const symbolic::bit_contents *bmap_a = &unbound;
3871 16 : bmap_a = &bmap_a->set_bytes (concrete_byte_range (0_bytes, 10_bytes),
3872 4 : bv_mgr.get_all_zeros (10_bytes),
3873 : nullptr);
3874 16 : bmap_a = &bmap_a->set_bytes (concrete_byte_range(10_bytes, 5_bytes),
3875 4 : bv_mgr.get_all_zeros (5_bytes),
3876 : nullptr);
3877 :
3878 : /* Approach B: create it as one binding. */
3879 4 : const symbolic::bit_contents *bmap_b = &unbound;
3880 16 : bmap_b = &bmap_b->set_bytes (concrete_byte_range (0_bytes, 15_bytes),
3881 4 : bv_mgr.get_all_zeros (15_bytes),
3882 : nullptr);
3883 :
3884 : /* Verify that they are canonicalized to the same symbolic node. */
3885 4 : ASSERT_SAME_SYMBOLIC_NODE (*bmap_a, *bmap_b);
3886 :
3887 : /* Verify that internally we used approach B. */
3888 4 : ASSERT_EQ (bmap_a->dyn_cast_concrete_bit_overlay ()->size (), 1);
3889 4 : }
3890 :
3891 : /* Verify that we canonicalize redundant bindings of bit ranges
3892 : when building concrete and symbolic overlays. */
3893 :
3894 : static void
3895 4 : test_redundant_bindings ()
3896 : {
3897 4 : symbolic::node_manager mgr;
3898 4 : auto &bit_contents_mgr = mgr.get_bit_contents_manager ();
3899 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3900 4 : auto &byte_mgr = mgr.get_scalars ().get_bytes ();
3901 :
3902 4 : auto &zero_everywhere = bit_contents_mgr.get_all_zeros ();
3903 4 : auto &untracked_bc = bit_contents_mgr.get_untracked ({});
3904 :
3905 4 : const symbolic_byte_size &one_byte
3906 4 : = symbolic_from_concrete (1_byte, byte_mgr);
3907 :
3908 4 : auto &zero_byte = bv_mgr.get_uint8 (0);
3909 4 : auto &custom_byte_a = bv_mgr.get_named_test_bit_vector (8_bits,
3910 4 : "custom_byte_a");
3911 4 : auto &custom_byte_b = bv_mgr.get_named_test_bit_vector (8_bits,
3912 4 : "custom_byte_b");
3913 4 : auto &unknown_byte = bv_mgr.get_unknown_bytes (1_byte);
3914 :
3915 4 : auto &kilobyte_of_zeros
3916 12 : = untracked_bc.set_bytes (concrete_byte_range (0_bytes, 1024_bytes),
3917 4 : bv_mgr.get_all_zeros (1024_bytes),
3918 : nullptr);
3919 :
3920 4 : auto &offset_x = byte_mgr.get_named_test_scalar ("x");
3921 4 : auto &offset_y = byte_mgr.get_named_test_scalar ("y");
3922 4 : auto one_byte_at_offset_x = symbolic_byte_range (offset_x, one_byte);
3923 4 : auto one_byte_at_offset_y = symbolic_byte_range (offset_y, one_byte);
3924 :
3925 : /* Concrete bit ranges. */
3926 12 : ASSERT_SAME_SYMBOLIC_NODE
3927 : (untracked_bc.set_bytes (concrete_byte_range (0_bytes, 1_byte),
3928 : unknown_byte,
3929 : nullptr),
3930 : untracked_bc);
3931 12 : ASSERT_SAME_SYMBOLIC_NODE
3932 : (zero_everywhere.set_bytes (concrete_byte_range (0_bytes, 1_byte),
3933 : zero_byte,
3934 : nullptr),
3935 : zero_everywhere);
3936 :
3937 : /* 1024 zero bytes; redundantly writing a zero into it at offset 16 ought to
3938 : canonicalized to the same representation. */
3939 12 : ASSERT_SAME_SYMBOLIC_NODE
3940 : (kilobyte_of_zeros.set_bytes (concrete_byte_range (16_bytes, 1_byte),
3941 : zero_byte,
3942 : nullptr),
3943 : kilobyte_of_zeros);
3944 : /* Writing a redundant value matching the baseline should split
3945 : the binding. */
3946 28 : ASSERT_SAME_SYMBOLIC_NODE
3947 : (kilobyte_of_zeros.set_bytes (concrete_byte_range (512_bytes, 1_byte),
3948 : unknown_byte,
3949 : nullptr),
3950 : untracked_bc
3951 : .set_bytes (concrete_byte_range (0_bytes, 512_bytes),
3952 : bv_mgr.get_all_zeros (512_bytes),
3953 : nullptr)
3954 : .set_bytes (concrete_byte_range (513_bytes, 511_bytes),
3955 : bv_mgr.get_all_zeros (511_bytes),
3956 : nullptr));
3957 : /* Writes to outside the bound range that match the baseline also ought
3958 : to be canonicalized away. */
3959 12 : ASSERT_SAME_SYMBOLIC_NODE
3960 : (kilobyte_of_zeros.set_bytes (concrete_byte_range (4096_bytes, 1_byte),
3961 : unknown_byte,
3962 : nullptr),
3963 : kilobyte_of_zeros);
3964 :
3965 : /* Symbolic start offsets. */
3966 :
3967 : /* Unknown start offsets. */
3968 4 : auto one_byte_at_unknown_offset
3969 4 : = symbolic_byte_range (byte_mgr.get_unknown (), one_byte);
3970 4 : ASSERT_SAME_SYMBOLIC_NODE
3971 : (untracked_bc.set_bytes (one_byte_at_unknown_offset,
3972 : unknown_byte,
3973 : nullptr),
3974 : untracked_bc);
3975 4 : ASSERT_SAME_SYMBOLIC_NODE
3976 : (zero_everywhere.set_bytes (one_byte_at_unknown_offset,
3977 : zero_byte,
3978 : nullptr),
3979 : zero_everywhere);
3980 :
3981 : /* Symbolic write on top of symbolic binding on top of "untracked". */
3982 4 : {
3983 4 : auto &zero_at_x = untracked_bc.set_bytes (one_byte_at_offset_x,
3984 : zero_byte,
3985 : nullptr);
3986 4 : auto &result = zero_at_x.set_bytes (one_byte_at_offset_y, custom_byte_a,
3987 : nullptr);
3988 : /* We should have the last bound bit_vec, on top of
3989 : "untracked-with-A-maybe-live". */
3990 4 : value_set maybe_live;
3991 4 : maybe_live.add (&zero_byte);
3992 4 : ASSERT_SAME_SYMBOLIC_NODE
3993 : (result,
3994 : bit_contents_mgr.get_untracked (maybe_live)
3995 : .set_bytes (one_byte_at_offset_y,
3996 : custom_byte_a,
3997 : nullptr));
3998 4 : }
3999 :
4000 : /* Symbolic write on top of symbolic binding on top of "zero". */
4001 4 : {
4002 4 : auto &custom_at_x = zero_everywhere.set_bytes (one_byte_at_offset_x,
4003 : custom_byte_a,
4004 : nullptr);
4005 4 : auto &result = custom_at_x.set_bytes (one_byte_at_offset_y, custom_byte_b,
4006 : nullptr);
4007 : /* We should have the last bound bit_vec, on top of
4008 : "untracked, with custom_byte_a maybe-live". */
4009 4 : value_set maybe_live;
4010 4 : maybe_live.add (&custom_byte_a);
4011 4 : ASSERT_SAME_SYMBOLIC_NODE
4012 : (result,
4013 : bit_contents_mgr.get_untracked (maybe_live)
4014 : .set_bytes (one_byte_at_offset_y,
4015 : custom_byte_b,
4016 : nullptr));
4017 4 : }
4018 :
4019 : /* Concrete write on top of symbolic binding. */
4020 4 : {
4021 4 : auto &custom_at_x = zero_everywhere.set_bytes (one_byte_at_offset_x,
4022 : custom_byte_a,
4023 : nullptr);
4024 12 : auto &result = custom_at_x.set_bytes (concrete_byte_range (0_bytes, 1_byte),
4025 : custom_byte_b,
4026 : nullptr);
4027 : /* We should have the last bound bit_vec, on top of
4028 : "untracked, with custom_byte_a maybe-live". */
4029 4 : value_set maybe_live;
4030 4 : maybe_live.add (&custom_byte_a);
4031 12 : ASSERT_SAME_SYMBOLIC_NODE
4032 : (result,
4033 : bit_contents_mgr.get_untracked (maybe_live)
4034 : .set_bytes (concrete_byte_range (0_bytes, 1_byte),
4035 : custom_byte_b,
4036 : nullptr));
4037 4 : }
4038 4 : }
4039 :
4040 : static void
4041 4 : test_offset_bit_contents ()
4042 : {
4043 4 : symbolic::node_manager node_mgr;
4044 4 : auto &bit_contents_mgr = node_mgr.get_bit_contents_manager ();
4045 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
4046 :
4047 4 : symbolic::named_test_bit_contents placeholder_bc
4048 4 : (node_mgr, "placeholder_bc");
4049 :
4050 4 : auto &x_bits = bit_mgr.get_named_test_scalar ("x");
4051 4 : auto &y_bits = bit_mgr.get_named_test_scalar ("y");
4052 :
4053 : /* Verify that we fold away zero offsets. */
4054 4 : {
4055 4 : auto &zero_offset = bit_mgr.get_concrete (0_bits);
4056 4 : ASSERT_SAME_SYMBOLIC_NODE
4057 : (placeholder_bc,
4058 : bit_contents_mgr.get_offset_bit_contents
4059 : (placeholder_bc,
4060 : zero_offset));
4061 : }
4062 :
4063 : /* Verify that we fold away offsets of the various uniform_bit_contents
4064 : subclasses. */
4065 4 : {
4066 4 : ASSERT_SAME_SYMBOLIC_NODE
4067 : (bit_contents_mgr.get_untracked ({}),
4068 : bit_contents_mgr.get_offset_bit_contents
4069 : (bit_contents_mgr.get_untracked ({}),
4070 : x_bits));
4071 4 : ASSERT_SAME_SYMBOLIC_NODE
4072 : (bit_contents_mgr.get_uninitialized (),
4073 : bit_contents_mgr.get_offset_bit_contents
4074 : (bit_contents_mgr.get_uninitialized (),
4075 : x_bits));
4076 4 : ASSERT_SAME_SYMBOLIC_NODE
4077 : (bit_contents_mgr.get_all_zeros (),
4078 : bit_contents_mgr.get_offset_bit_contents
4079 : (bit_contents_mgr.get_all_zeros (),
4080 : x_bits));
4081 : }
4082 :
4083 : /* Verify the not-folded case. */
4084 4 : auto &offset_bc = bit_contents_mgr.get_offset_bit_contents (placeholder_bc,
4085 : x_bits);
4086 4 : {
4087 4 : auto *as_offset_bc = offset_bc.dyn_cast_offset_bit_contents ();
4088 4 : ASSERT_NE (as_offset_bc, nullptr);
4089 4 : ASSERT_SAME_SYMBOLIC_NODE (as_offset_bc->get_inner_bit_contents (),
4090 : placeholder_bc);
4091 4 : ASSERT_SAME_SYMBOLIC_NODE (as_offset_bc->get_bit_offset (),
4092 : x_bits);
4093 :
4094 4 : ASSERT_SAME_SYMBOLIC_NODE
4095 : (offset_bc.get_byte (10_bytes),
4096 : placeholder_bc.get_bits
4097 : (symbolic_bit_range (bit_mgr.get_concrete (bits_from_bytes (10_bytes))
4098 : + x_bits,
4099 : bit_mgr.get_concrete (8_bits))));
4100 : }
4101 :
4102 : /* Verify that offset-of-offset folds to sum of offsets. */
4103 4 : ASSERT_SAME_SYMBOLIC_NODE
4104 : (bit_contents_mgr.get_offset_bit_contents (offset_bc,
4105 : y_bits),
4106 : bit_contents_mgr.get_offset_bit_contents (placeholder_bc,
4107 : x_bits + y_bits));
4108 4 : }
4109 :
4110 : /* Consider:
4111 : | struct str {
4112 : | uint32_t m_len;
4113 : | char m_buf[];
4114 : | } *s;
4115 : |
4116 : | s->m_len = n;
4117 : | memcpy (s->m_buf, src, n);
4118 : | s->m_buf[n] = '\0'; */
4119 :
4120 : static void
4121 4 : test_string_with_trailing_array ()
4122 : {
4123 4 : symbolic::node_manager node_mgr;
4124 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
4125 4 : auto &bc_mgr = node_mgr.get_bit_contents_manager ();
4126 4 : auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
4127 :
4128 4 : auto &unbound = bc_mgr.get_named_test_bit_contents ("unbound");
4129 4 : const symbolic::bit_contents *bmap = &unbound;
4130 :
4131 4 : placeholder_svalue n_sval
4132 4 : (node_mgr.alloc_node_id (), uint32_type_node, "n");
4133 4 : auto &n_bytes = byte_mgr.get_scalar_from_svalue (n_sval);
4134 4 : auto &src_bv = bv_mgr.get_named_test_bit_vector (n_bytes, "src");
4135 :
4136 : /* "s->m_len = n;" */
4137 12 : bmap = &bmap->set_bytes (concrete_byte_range (0_bytes, 4_bytes),
4138 : bv_mgr.get_bit_vector_from_svalue (n_sval),
4139 : nullptr);
4140 :
4141 : /* "memcpy (s->m_buf, src, n);" */
4142 4 : bmap = &bmap->set_bytes (symbolic_byte_range (byte_mgr.get_concrete (4_bytes),
4143 4 : n_bytes),
4144 : src_bv,
4145 : nullptr);
4146 :
4147 : /* "s->m_buf[n] = '\0';" */
4148 12 : bmap = &bmap->set_bytes (symbolic_byte_range (byte_mgr.get_concrete (4_bytes)
4149 : + n_bytes,
4150 12 : byte_mgr.get_concrete (1_byte)),
4151 : bv_mgr.get_uint8 (0),
4152 : nullptr);
4153 4 : }
4154 :
4155 :
4156 : /* Run all of the selftests within this file. */
4157 :
4158 : void
4159 4 : analyzer_symbolic_bit_contents_cc_tests ()
4160 : {
4161 4 : test_uninit ();
4162 4 : test_untracked ();
4163 4 : test_all_zeros ();
4164 : // rule of 5 ?
4165 4 : test_static_sized_char_array ();
4166 4 : test_reading_nontrivial_bit_map ();
4167 4 : test_update_no_overlap_with_gap ();
4168 4 : test_update_no_overlap_touching ();
4169 4 : test_update_punching_a_hole ();
4170 4 : test_update_full_overwrite ();
4171 4 : test_update_overlap_with_prefix ();
4172 4 : test_update_overlap_with_suffix ();
4173 4 : test_update_multiple_overlaps ();
4174 4 : test_uint32 ();
4175 4 : test_endianness ();
4176 4 : test_canonicalize_concat ();
4177 4 : test_consolidation ();
4178 4 : test_redundant_bindings ();
4179 4 : test_offset_bit_contents ();
4180 4 : test_string_with_trailing_array ();
4181 4 : }
4182 :
4183 : } // namespace selftest
4184 :
4185 : #endif /* CHECKING_P */
4186 :
4187 : } // namespace ana
4188 :
4189 : #endif /* #if ENABLE_ANALYZER */
|