Line data Source code
1 : /* Symbolic vectors of bits e.g. "(buf_a[0-10) + buf_b[0-len) + (0b * 8))"
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 "fold-const.h"
25 : #include "tm.h"
26 :
27 : #include "diagnostics/markup.h"
28 : #include "selftest.h"
29 :
30 : #include "analyzer/symbolic-bit-vectors.h"
31 : #include "analyzer/region-model.h"
32 : #include "analyzer/liveness-context.h"
33 : #include "analyzer/analyzer-selftests.h"
34 :
35 : #if ENABLE_ANALYZER
36 :
37 : namespace ana {
38 :
39 : // TODO: move this
40 : /* class conjured_purge. */
41 :
42 : /* Purge state relating to BV. */
43 :
44 : void
45 11664 : conjured_purge::purge (const symbolic::conjured_bit_vector &bv) const
46 : {
47 11664 : m_model->purge_state_involving (bv, m_ctxt);
48 11664 : }
49 :
50 : /* Purge state relating to BV. */
51 :
52 : void
53 13239 : conjured_purge::purge (const symbolic::conjured_bit_contents &bc) const
54 : {
55 13239 : m_model->purge_state_involving (bc, m_ctxt);
56 13239 : }
57 :
58 : namespace symbolic {
59 :
60 : // class bit_vector : public symbolic::node
61 :
62 1280668 : bit_vector::bit_vector (bit_vector_manager &bv_mgr,
63 : complexity c,
64 : const symbolic_bit_size &size_in_bits)
65 1280668 : : value_node (c, bv_mgr.get_node_manager ().alloc_node_id ()),
66 1280668 : m_bv_mgr (bv_mgr),
67 1280668 : m_size_in_bits (size_in_bits)
68 : {
69 1280668 : }
70 :
71 : void
72 3042 : bit_vector::print_dump_widget_label (pretty_printer *pp) const
73 : {
74 3042 : start_any_dump_color (pp);
75 :
76 3042 : print_prefix_for_dump_widget_label (pp);
77 :
78 3042 : pp_string (pp, "[size: ");
79 :
80 3042 : end_any_dump_color (pp);
81 3042 : m_size_in_bits.start_any_dump_color (pp);
82 :
83 3042 : m_size_in_bits.dump_inline_to_pp (pp, units::role::size);
84 :
85 3042 : m_size_in_bits.end_any_dump_color (pp);
86 3042 : start_any_dump_color (pp);
87 :
88 3042 : pp_string (pp, "]");
89 :
90 3042 : print_any_suffix_for_dump_widget_label (pp);
91 :
92 3042 : end_any_dump_color (pp);
93 3042 : }
94 :
95 : bool
96 0 : bit_vector::maybe_print_for_user (pretty_printer *,
97 : const region_model &,
98 : const symbolic::node *) const
99 : {
100 0 : return false;
101 : }
102 :
103 : const symbolic_byte_size *
104 106 : bit_vector::maybe_get_size_in_bytes () const
105 : {
106 106 : return maybe_bytes_from_bits (m_size_in_bits);
107 : }
108 :
109 : bool
110 879008 : bit_vector::maybe_get_concrete_size_in_bits (concrete_bit_size *out) const
111 : {
112 879008 : return concrete_from_symbolic (m_size_in_bits, out);
113 : }
114 :
115 : bool
116 753406 : bit_vector::maybe_get_concrete_size_in_bytes (concrete_byte_size *out) const
117 : {
118 753406 : concrete_bit_size conc_bit_size;
119 753406 : if (!maybe_get_concrete_size_in_bits (&conc_bit_size))
120 : return false;
121 742250 : return maybe_bytes_from_bits (conc_bit_size, out);
122 753406 : }
123 :
124 : /* Base implementation of get_tristate_at_offset: crop to one bit, and get
125 : that bit as a tristate. */
126 :
127 : tristate
128 832 : bit_vector::get_tristate_at_offset (const symbolic_bit_offset &bit) const
129 : {
130 832 : auto &bit_mgr = m_bv_mgr.get_node_manager ().get_scalars ().get_bits ();
131 832 : auto &bv_for_bit = get_cropped_bits
132 832 : (symbolic_bit_range (bit,
133 832 : bit_mgr.get_concrete (1_bit)));
134 832 : gcc_assert (bv_for_bit.get_size ().is_one_p ());
135 832 : auto *ts_bv = bv_for_bit.dyn_cast_tristate_bit_vector ();
136 832 : if (!ts_bv)
137 24 : return tristate::unknown ();
138 808 : return ts_bv->get_bit_value ();
139 : }
140 :
141 : class hexdumper
142 : {
143 : public:
144 56 : hexdumper (text_art::style_manager &sm,
145 : const hexdump_options &hexdump_opts)
146 56 : : m_sm (sm),
147 56 : m_hexdump_opts (hexdump_opts),
148 56 : m_num_columns (1),
149 56 : m_x_binary (-1),
150 56 : m_x_hex (-1),
151 56 : m_x_ascii (-1)
152 : {
153 56 : if (hexdump_opts.m_show_binary)
154 36 : m_x_binary = m_num_columns++;
155 56 : if (hexdump_opts.m_show_hex)
156 52 : m_x_hex = m_num_columns++;
157 56 : if (hexdump_opts.m_show_ascii)
158 36 : m_x_ascii = m_num_columns++;
159 56 : }
160 :
161 : text_art::table
162 56 : make_table (const bit_vector &bv,
163 : concrete_bit_size conc_bit_size)
164 : {
165 56 : using namespace text_art;
166 :
167 56 : auto bits_per_line = bits_from_bytes (m_hexdump_opts.m_bytes_per_line);
168 :
169 56 : table t (table::size_t (m_num_columns, 0));
170 :
171 56 : if (m_hexdump_opts.m_show_headings)
172 : {
173 4 : int y_headings = t.add_row ();
174 4 : if (m_x_binary >= 0)
175 4 : t.set_cell (table::coord_t (m_x_binary, y_headings),
176 8 : styled_string (m_sm, "Binary"));
177 4 : if (m_x_hex >= 0)
178 4 : t.set_cell (table::coord_t (m_x_hex, y_headings),
179 8 : styled_string (m_sm, "Hex"));
180 4 : if (m_x_ascii >= 0)
181 4 : t.set_cell (table::coord_t (m_x_ascii, y_headings),
182 8 : styled_string (m_sm, "ASCII"));
183 : }
184 :
185 56 : for (concrete_bit_offset start_of_this_line = 0_bits;
186 148 : start_of_this_line < conc_bit_size;
187 92 : start_of_this_line += bits_per_line)
188 : {
189 92 : auto start_of_next_line = start_of_this_line + bits_per_line;
190 92 : auto size_of_this_line = (start_of_next_line < conc_bit_size
191 92 : ? bits_per_line
192 92 : : conc_bit_size - start_of_this_line);
193 184 : add_hexdump_line (t,
194 : bv,
195 92 : concrete_bit_range (start_of_this_line,
196 276 : size_of_this_line));
197 92 : }
198 56 : return t;
199 56 : }
200 :
201 : private:
202 : void
203 92 : add_hexdump_line (text_art::table &t,
204 : const bit_vector &bv,
205 : concrete_bit_range bits_on_line)
206 : {
207 92 : using namespace text_art;
208 :
209 184 : gcc_assert (bits_on_line.get_start_offset () >= 0_bits);
210 : //gcc_assert (bits_on_line.get_last_offset () < bv.get_size ());
211 :
212 92 : auto row_y = t.add_row ();
213 :
214 92 : {
215 92 : pretty_printer pp;
216 92 : bits_on_line.dump_to_pp_pretty (&pp);
217 92 : t.set_cell (table::coord_t (0, row_y),
218 184 : styled_string (m_sm, pp_formatted_text (&pp)),
219 : x_align::RIGHT);
220 92 : }
221 :
222 : /* Try to print the individual bits. */
223 92 : if (m_x_binary >= 0)
224 : {
225 60 : pretty_printer pp;
226 60 : int count = 0;
227 60 : for (concrete_bit_offset i = bits_on_line.get_start_offset ();
228 1872 : i < bits_on_line.get_next_offset ();
229 1812 : i += 1_bit, ++count)
230 : {
231 1812 : tristate ts = bv.get_tristate_at_offset (i);
232 1812 : pp_character (&pp, get_dump_char_for_tristate_bit (ts));
233 :
234 : // Add a space every 8 bits:
235 3624 : if (count % 8 == 7
236 2036 : && i < bits_on_line.get_last_offset ())
237 168 : pp_space (&pp);
238 60 : }
239 60 : t.set_cell (table::coord_t (m_x_binary, row_y),
240 120 : styled_string (m_sm, pp_formatted_text (&pp)),
241 : x_align::LEFT);
242 60 : }
243 :
244 : /* Try to print individual bytes. */
245 92 : if (m_x_hex >= 0)
246 : {
247 88 : pretty_printer pp;
248 88 : bool first = true;
249 496 : for (concrete_bit_offset i = bits_on_line.get_start_offset ();
250 496 : i < bits_on_line.get_next_offset ();
251 408 : i += 8_bits)
252 : {
253 408 : if (i + 7_bits < bits_on_line.get_next_offset ())
254 : {
255 408 : auto &byte
256 1224 : = bv.get_cropped_bits (concrete_bit_range (i, 8_bits));
257 408 : uint8_t byte_val;
258 408 : if (first)
259 : first = false;
260 : else
261 320 : pp_space (&pp);
262 408 : if (byte.maybe_as_uint8 (&byte_val))
263 376 : print_byte_as_hex (&pp, byte_val);
264 : else
265 32 : pp_string (&pp, "??");
266 : }
267 88 : }
268 88 : t.set_cell (table::coord_t (m_x_hex, row_y),
269 176 : styled_string (m_sm, pp_formatted_text (&pp)),
270 : x_align::LEFT);
271 88 : }
272 :
273 : // Try to print ASCII
274 92 : if (m_x_ascii >= 0)
275 : {
276 64 : pretty_printer pp;
277 376 : for (concrete_bit_offset i = bits_on_line.get_start_offset ();
278 376 : i < bits_on_line.get_next_offset ();
279 312 : i += 8_bits)
280 : {
281 312 : if (i + 7_bits < bits_on_line.get_next_offset ())
282 : {
283 312 : auto &byte
284 936 : = bv.get_cropped_bits (concrete_bit_range (i, 8_bits));
285 312 : pp_character (&pp, get_dump_char_for_byte (byte));
286 : }
287 64 : }
288 64 : t.set_cell (table::coord_t (m_x_ascii, row_y),
289 128 : styled_string (m_sm, pp_formatted_text (&pp)),
290 : x_align::LEFT);
291 64 : }
292 92 : }
293 :
294 : static char
295 1812 : get_dump_char_for_tristate_bit (tristate t)
296 : {
297 1812 : switch (t.get_value ())
298 : {
299 0 : default:
300 0 : gcc_unreachable ();
301 : case tristate::TS_UNKNOWN:
302 : return '?';
303 : case tristate::TS_TRUE:
304 : return '1';
305 : case tristate::TS_FALSE:
306 : return '0';
307 : }
308 : }
309 :
310 : static void
311 376 : print_byte_as_hex (pretty_printer *pp, uint8_t byte)
312 : {
313 376 : const char *const hexchars = "0123456789abcdef";
314 376 : pp_character (pp, hexchars[byte / 16]);
315 376 : pp_character (pp, hexchars[byte % 16]);
316 376 : }
317 :
318 : static char
319 312 : get_dump_char_for_byte (const bit_vector &bv)
320 : {
321 312 : uint8_t byte_val;
322 312 : if (!bv.maybe_as_uint8 (&byte_val))
323 : return '?';
324 296 : gcc_assert (byte_val >= 0 && byte_val <= 255);
325 296 : if (ISPRINT (byte_val))
326 244 : return byte_val;
327 : else
328 : return '.';
329 : }
330 :
331 : text_art::style_manager &m_sm;
332 :
333 : const hexdump_options &m_hexdump_opts;
334 : int m_num_columns;
335 : int m_x_binary;
336 : int m_x_hex;
337 : int m_x_ascii;
338 : };
339 :
340 : static std::unique_ptr<text_art::canvas>
341 56 : make_hexdump_canvas (const bit_vector &bv,
342 : const hexdump_options &hexdump_opts)
343 : {
344 56 : concrete_bit_size conc_bit_size;
345 56 : if (!concrete_from_symbolic (bv.get_size (), &conc_bit_size))
346 0 : return nullptr;
347 :
348 56 : text_art::style_manager sm;
349 56 : text_art::ascii_theme theme;
350 56 : hexdumper h (sm, hexdump_opts);
351 56 : auto t = h.make_table (bv, conc_bit_size);
352 56 : return std::make_unique<text_art::canvas> (t.to_canvas (theme, sm));
353 56 : }
354 :
355 : void
356 56 : bit_vector::hexdump_to_pp (pretty_printer *pp,
357 : const hexdump_options &hexdump_opts) const
358 : {
359 56 : auto c = make_hexdump_canvas (*this, hexdump_opts);
360 56 : if (!c)
361 0 : return;
362 56 : c->print_to_pp (pp);
363 56 : }
364 :
365 : void
366 0 : bit_vector::hexdump (const hexdump_options &hexdump_opts) const
367 : {
368 0 : tree_dump_pretty_printer pp (stderr);
369 0 : hexdump_to_pp (&pp, hexdump_opts);
370 0 : }
371 :
372 : void
373 0 : bit_vector::hexdump () const
374 : {
375 0 : hexdump_options hexdump_opts;
376 0 : hexdump_opts.m_show_headings = true;
377 0 : hexdump_opts.m_show_binary = false;
378 0 : hexdump_opts.m_show_hex = true;
379 0 : hexdump_opts.m_show_ascii = true;
380 0 : hexdump (hexdump_opts);
381 0 : }
382 :
383 : bool
384 284 : bit_vector::conjured_p () const
385 : {
386 284 : return false;
387 : }
388 :
389 : tristate
390 844196 : bit_vector::get_tristate_at_offset (const concrete_bit_offset &bit) const
391 : {
392 844196 : auto &bit_mgr = m_bv_mgr.get_node_manager ().get_scalars ().get_bits ();
393 844196 : auto &sym_bit_offset = bit_mgr.get_concrete (bit);
394 844196 : return get_tristate_at_offset (sym_bit_offset);
395 : }
396 :
397 : const bit_vector &
398 19148 : bit_vector::get_cropped_bits (const symbolic_bit_range &bits_to_use) const
399 : {
400 19148 : return m_bv_mgr.get_cropped_bits (*this, bits_to_use);
401 : }
402 :
403 : const bit_vector &
404 2185579 : bit_vector::get_cropped_bits (const concrete_bit_range &bits_to_use) const
405 : {
406 2185579 : auto &bit_mgr = m_bv_mgr.get_bit_manager ();
407 2185579 : return m_bv_mgr.get_cropped_bits (*this,
408 2185579 : symbolic_from_concrete (bits_to_use,
409 2185579 : bit_mgr));
410 : }
411 :
412 : const bit_vector &
413 0 : bit_vector::get_cropped_bytes (const symbolic_byte_range &bytes_to_use) const
414 : {
415 0 : return m_bv_mgr.get_cropped_bits (*this, bits_from_bytes (bytes_to_use));
416 : }
417 :
418 : const bit_vector &
419 140 : bit_vector::get_cropped_bytes (const concrete_byte_range &bytes_to_use) const
420 : {
421 140 : auto &bit_mgr = m_bv_mgr.get_bit_manager ();
422 140 : return m_bv_mgr.get_cropped_bits
423 140 : (*this,
424 280 : symbolic_from_concrete (bits_from_bytes (bytes_to_use),
425 140 : bit_mgr));
426 : }
427 :
428 : /* Get the first SIZE bits of this bit_vector. */
429 :
430 : const bit_vector &
431 929 : bit_vector::get_prefix (const symbolic_bit_size &size) const
432 : {
433 929 : auto &bit_mgr = m_bv_mgr.get_bit_manager ();
434 929 : return m_bv_mgr.get_cropped_bits
435 929 : (*this,
436 929 : symbolic_bit_range (bit_mgr.get_concrete (0_bits),
437 929 : size));
438 : }
439 :
440 : const bit_vector &
441 921 : bit_vector::get_prefix (const concrete_bit_size &size) const
442 : {
443 921 : return get_prefix (symbolic_from_concrete (size,
444 921 : m_bv_mgr.get_bit_manager ()));
445 : }
446 :
447 : /* Get the last SIZE bits of this bit_vector. */
448 :
449 : const bit_vector &
450 1987 : bit_vector::get_suffix (const symbolic_bit_size &size) const
451 : {
452 1987 : return m_bv_mgr.get_cropped_bits (*this,
453 1987 : symbolic_bit_range (get_size () - size,
454 1987 : size));
455 : }
456 :
457 : const bit_vector &
458 1987 : bit_vector::get_suffix (const concrete_bit_size &size) const
459 : {
460 1987 : return get_suffix (symbolic_from_concrete (size,
461 1987 : m_bv_mgr.get_bit_manager ()));
462 : }
463 :
464 : const bit_vector &
465 0 : bit_vector::get_byte_at_offset (const symbolic_byte_offset &byte_offset) const
466 : {
467 0 : auto &byte_mgr = m_bv_mgr.get_node_manager ().get_scalars ().get_bytes ();
468 0 : return m_bv_mgr.get_cropped_bytes
469 0 : (*this,
470 0 : symbolic_byte_range (byte_offset,
471 0 : byte_mgr.get_concrete (1_byte)));
472 : }
473 :
474 : const bit_vector &
475 478505 : bit_vector::get_byte_at_offset (const concrete_byte_offset &byte_offset) const
476 : {
477 478505 : auto &byte_mgr = m_bv_mgr.get_node_manager ().get_scalars ().get_bytes ();
478 478505 : return m_bv_mgr.get_cropped_bytes
479 478505 : (*this,
480 957010 : symbolic_byte_range (byte_mgr.get_concrete (byte_offset),
481 478505 : byte_mgr.get_concrete (1_byte)));
482 : }
483 :
484 : const bit_vector *
485 77654 : bit_vector::maybe_fold_crop (const symbolic_bit_range &) const
486 : {
487 77654 : return nullptr;
488 : }
489 :
490 : /* If this is an 8-bit constant value, return true and write it to *OUT.
491 : Otherwise return false. */
492 :
493 : bool
494 480001 : bit_vector::maybe_as_uint8 (uint8_t *out) const
495 : {
496 480001 : concrete_byte_size byte_size;
497 480001 : if (!maybe_get_concrete_size_in_bytes (&byte_size))
498 : return false;
499 480001 : if (byte_size != 1_byte)
500 : return false;
501 :
502 480001 : if (auto *ts = dyn_cast_tristate_bit_vector ())
503 249894 : switch (ts->get_bit_value ().get_value ())
504 : {
505 0 : default:
506 0 : gcc_unreachable ();
507 : case tristate::TS_UNKNOWN:
508 : return false;
509 :
510 84 : case tristate::TS_TRUE:
511 84 : *out = 0xff;
512 84 : return true;
513 :
514 249778 : case tristate::TS_FALSE:
515 249778 : *out = 0x00;
516 249778 : return true;
517 : }
518 :
519 230107 : if (auto *sval_bv = dyn_cast_bit_vector_from_svalue ())
520 : {
521 2425 : auto &sval = sval_bv->get_svalue ();
522 2425 : if (tree type = sval.get_type ())
523 2425 : if (INTEGRAL_TYPE_P (type)
524 2425 : && TYPE_PRECISION (type) == 8)
525 2425 : if (tree cst = sval.maybe_get_constant ())
526 : {
527 2379 : *out = TREE_INT_CST_LOW (cst);
528 2379 : return true;
529 : }
530 : }
531 :
532 : return false;
533 480001 : }
534 :
535 : const bit_vector *
536 74005 : bit_vector::can_merge_p (const bit_vector &bv_a,
537 : const bit_vector &bv_b,
538 : tree type,
539 : model_merger *merger)
540 : {
541 74005 : if (&bv_a == &bv_b)
542 : return &bv_a;
543 :
544 65165 : auto &bv_mgr = bv_a.m_bv_mgr;
545 :
546 : /* Reject attempts to merge poisoned bit vectors with other bit vectors
547 : (either non-poisoned, or other kinds of poison), so that e.g.
548 : we identify paths in which a variable is conditionally uninitialized. */
549 65165 : if (bv_a.get_kind () == kind::poisoned
550 65165 : || bv_b.get_kind () == kind::poisoned)
551 : return nullptr;
552 :
553 64844 : if (!merger->mergeable_bit_vector_p (bv_a))
554 : return nullptr;
555 64064 : if (!merger->mergeable_bit_vector_p (bv_b))
556 : return nullptr;
557 :
558 : /* If we don't have a type, but both bvs are built from svalues with
559 : the same underlying type, try to use that type. */
560 63113 : if (!type)
561 3899 : if (auto *from_sval_a = bv_a.dyn_cast_bit_vector_from_svalue ())
562 933 : if (auto *from_sval_b = bv_b.dyn_cast_bit_vector_from_svalue ())
563 : {
564 672 : tree type_a = from_sval_a->get_svalue ().get_type ();
565 672 : tree type_b = from_sval_b->get_svalue ().get_type ();
566 672 : if (type_a == type_b)
567 3899 : type = type_a;
568 : }
569 :
570 3899 : if (type)
571 : {
572 59878 : auto &rmm = bv_mgr.get_node_manager ().get_svalue_manager ();
573 59878 : const svalue *sval_a = rmm.get_svalue_from_bit_vector (type, bv_a);
574 59878 : const svalue *sval_b = rmm.get_svalue_from_bit_vector (type, bv_b);
575 59878 : if (const svalue *merged_sval = sval_a->can_merge_p (sval_b, &rmm,
576 : merger))
577 46003 : return &bv_mgr.get_bit_vector_from_svalue (*merged_sval);
578 : else
579 : /* svalues not mergeable; don't merge the bit vectors. */
580 : return nullptr;
581 : }
582 :
583 3235 : return &bv_mgr.get_unknown_bits (bv_a.get_size ());
584 : }
585 :
586 : std::unique_ptr<json::object>
587 184 : bit_vector::make_json_obj (const char *type_name) const
588 : {
589 184 : auto obj = value_node::make_json_obj (type_name);
590 184 : obj->set ("size_in_bits", m_size_in_bits.to_json ());
591 184 : return obj;
592 : }
593 :
594 :
595 : static bool
596 25499 : maybe_set_irange_bitmask_from_concrete_tristate_vector (const concrete_tristate_vector &ts_vec,
597 : irange_bitmask *out)
598 : {
599 25499 : unsigned HOST_WIDE_INT value = 0;
600 25499 : unsigned HOST_WIDE_INT mask = -1;
601 859447 : for (size_t i = 0; i < ts_vec.size (); ++i)
602 : {
603 833948 : tristate ts = ts_vec[i];
604 833948 : if (ts.is_known ())
605 : {
606 : /* Clear the mask for known bits. */
607 357 : mask &= ~(1L << i);
608 :
609 : /* Set the value. */
610 357 : if (ts.is_true ())
611 107 : value |= (1L << i);
612 : }
613 : }
614 50998 : *out = irange_bitmask (wi::uhwi (value, ts_vec.size ()),
615 50998 : wi::uhwi (mask, ts_vec.size ()));
616 25499 : return true;
617 : }
618 :
619 : bool
620 25511 : bit_vector::maybe_get_irange_bitmask (irange_bitmask *out) const
621 : {
622 25511 : concrete_bit_size conc_size;
623 25511 : if (!maybe_get_concrete_size_in_bits (&conc_size))
624 : return false;
625 :
626 25511 : if (conc_size > 64_bits)
627 : return false;
628 :
629 25495 : out->set_unknown (conc_size.as_unsigned_long ());
630 25495 : concrete_tristate_vector ts_vec;
631 25495 : ts_vec.reserve (conc_size.as_unsigned_long ());
632 884918 : for (concrete_bit_offset i = 0_bits; i < conc_size; ++i)
633 : {
634 : /* TODO: this is a lot of vfuncs and sym to conc,
635 : and O(n^2) e.g. on concat_bit_vector
636 : We might want a bulk query interface that
637 : returns a concrete_tristate_vector. */
638 833928 : tristate ts = get_tristate_at_offset (i);
639 833928 : ts_vec.push_back (ts);
640 25495 : }
641 25495 : return maybe_set_irange_bitmask_from_concrete_tristate_vector (ts_vec,
642 : out);
643 25495 : }
644 :
645 : /* Return true if this bit vector is the initial value of a region,
646 : or derived directly from such a value (e.g. by casting or cropping). */
647 :
648 : bool
649 8967 : bit_vector::derived_from_initial_value_p () const
650 : {
651 9043 : switch (get_kind ())
652 : {
653 : default:
654 : return false;
655 :
656 76 : case bit_vector::kind::cropped:
657 76 : {
658 76 : auto &cropped_bv = static_cast <const cropped_bit_vector &> (*this);
659 76 : return cropped_bv
660 76 : .get_uncropped_bit_vector ()
661 76 : .derived_from_initial_value_p ();
662 : }
663 :
664 76 : case bit_vector::kind::svalue:
665 76 : {
666 76 : auto &from_sval = static_cast <const bit_vector_from_svalue &> (*this);
667 76 : return from_sval.get_svalue ().derived_from_initial_value_p ();
668 : }
669 :
670 7496 : case bit_vector::kind::from_ubc:
671 7496 : {
672 : /* Baseline contents are the initial contents of a region. */
673 7496 : auto &from_ubc = static_cast <const bit_vector_from_ubc &> (*this);
674 7496 : return (from_ubc.get_bit_contents ().dyn_cast_baseline_bit_contents ()
675 7496 : != nullptr);
676 : }
677 : }
678 : }
679 :
680 : // class concat_bit_vector : public bit_vector
681 :
682 : bool
683 216304 : concat_bit_vector::key::operator< (const key &other) const
684 : {
685 216304 : if (m_bit_vectors.size () != other.m_bit_vectors.size ())
686 4097 : return m_bit_vectors.size () < other.m_bit_vectors.size ();
687 360654 : for (size_t i = 0; i < m_bit_vectors.size (); ++i)
688 295038 : if (m_bit_vectors[i] != other.m_bit_vectors[i])
689 146591 : return m_bit_vectors[i]->get_id () < other.m_bit_vectors[i]->get_id ();
690 : return false;
691 : }
692 :
693 : complexity
694 4146 : concat_bit_vector::key::get_complexity () const
695 : {
696 4146 : unsigned num_child_nodes = 0;
697 4146 : unsigned max_child_depth = 0;
698 14128 : for (auto iter : m_bit_vectors)
699 : {
700 9982 : const complexity &child_c = iter->get_complexity ();
701 9982 : num_child_nodes += child_c.m_num_nodes;
702 9982 : max_child_depth = std::max (max_child_depth, child_c.m_max_depth);
703 : }
704 4146 : return complexity (num_child_nodes + 1, max_child_depth + 1);
705 : }
706 :
707 : const symbolic_bit_size &
708 4146 : concat_bit_vector::key::
709 : get_size (scalar_manager<concrete_bit_size> &bit_mgr) const
710 : {
711 4146 : const symbolic_bit_size *sum = &bit_mgr.get_concrete (0_bits);
712 14128 : for (auto iter : m_bit_vectors)
713 9982 : sum = &(*sum + iter->get_size ());
714 4146 : return *sum;
715 : }
716 :
717 : tristate
718 10224 : concat_bit_vector::get_tristate_at_offset (const symbolic_bit_offset &bit) const
719 : {
720 10224 : auto *remaining_offset = &bit;
721 17075 : for (auto child : m_bit_vectors)
722 : {
723 : /* Is the bit of interest within child?
724 : | remaining_offset < child->get_size ()
725 : | => (remaining_offset - child->get_size ()) < 0 */
726 17075 : auto &diff = *remaining_offset - child->get_size ();
727 17075 : if (auto *conc_diff = diff.dyn_cast_concrete_scalar ())
728 : {
729 17075 : if (conc_diff->get_concrete () < 0_bits)
730 10224 : return child->get_tristate_at_offset (*remaining_offset);
731 : else
732 : {
733 : /* Skip this child.
734 : | remaining_offset -= child_get_size
735 : | ==> remaining_offset = remaining_offset - child_get_size
736 : | ==> remaining_offset = diff */
737 6851 : remaining_offset = &diff;
738 6851 : continue;
739 : }
740 : }
741 : else
742 : {
743 : /* We don't know whether we're in child or not. */
744 0 : return tristate::unknown ();
745 : }
746 : }
747 0 : return tristate::unknown ();
748 : }
749 :
750 4146 : concat_bit_vector::concat_bit_vector (bit_vector_manager &bv_mgr,
751 : const key &k)
752 : : bit_vector (bv_mgr,
753 : k.get_complexity (),
754 4146 : k.get_size (bv_mgr.get_node_manager ()
755 4146 : .get_scalars ().get_bits ())),
756 4146 : m_bit_vectors (std::move (k.m_bit_vectors))
757 : {
758 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
759 4146 : gcc_assert (!get_size ().is_zero_p ());
760 :
761 : /* After folding, we shouldn't have empty or single-element
762 : concatentations. */
763 4146 : gcc_assert (m_bit_vectors.size () >= 2);
764 :
765 : if (CHECKING_P)
766 9982 : for (auto iter = m_bit_vectors.begin ();
767 14128 : iter != m_bit_vectors.end ();
768 9982 : ++iter)
769 : {
770 : /* No empty children. */
771 9982 : gcc_assert (!(*iter)->get_size ().is_zero_p ());
772 :
773 : /* No nesting. */
774 9982 : gcc_assert (!(*iter)->dyn_cast_concat_bit_vector ());
775 :
776 : /* Check that we folded away consecutive children. */
777 9982 : auto next = iter;
778 9982 : ++next;
779 9982 : if (next != m_bit_vectors.end ())
780 : {
781 : /* Must not have the same bit_vector appearing
782 : consecutively as a child. */
783 5836 : gcc_assert (*iter != *next);
784 :
785 : /* Must not have consecutive children that are the
786 : uniform_bit_vector with the same underlying bit. */
787 5836 : if (auto *iter_poisoned = (*iter)->dyn_cast_poisoned_bit_vector ())
788 106 : if (auto *next_poisoned = (*next)->dyn_cast_poisoned_bit_vector ())
789 4 : gcc_assert (iter_poisoned->get_poison_kind ()
790 : != next_poisoned->get_poison_kind ());
791 5836 : if (auto *iter_tristate = (*iter)->dyn_cast_tristate_bit_vector ())
792 594 : if (auto *next_tristate = (*next)->dyn_cast_tristate_bit_vector ())
793 105 : gcc_assert (iter_tristate->get_bit_value ()
794 : != next_tristate->get_bit_value ());
795 :
796 : /* Must not have consecutive repetitions of the same bit vector,
797 : or a bit vector next to repetitions of that bit vector. */
798 5836 : auto *iter_rep = (*iter)->dyn_cast_repeated_bit_vector ();
799 5836 : auto *next_rep = (*next)->dyn_cast_repeated_bit_vector ();
800 5836 : if (iter_rep)
801 : {
802 186 : if (next_rep)
803 45 : gcc_assert (&iter_rep->get_inner_bit_vector ()
804 : != &next_rep->get_inner_bit_vector ());
805 : else
806 141 : gcc_assert (&iter_rep->get_inner_bit_vector () != (*next));
807 : }
808 5650 : else if (next_rep)
809 100 : gcc_assert ((*iter) != &next_rep->get_inner_bit_vector ());
810 :
811 : /* Must not have consecutive bit_vector_from_ubc taken from the
812 : same UBC at consecutive positions. */
813 5836 : if (auto *iter_from_ubc = (*iter)->dyn_cast_bit_vector_from_ubc ())
814 207 : if (auto *next_from_ubc = (*next)->dyn_cast_bit_vector_from_ubc ())
815 16 : if (&iter_from_ubc->get_bit_contents ()
816 16 : == &next_from_ubc->get_bit_contents ())
817 4 : gcc_assert
818 : (&iter_from_ubc->get_bit_range ().get_next_offset ()
819 : != &next_from_ubc->get_bit_range ().get_start_offset ());
820 : }
821 : }
822 4146 : }
823 :
824 : void
825 14 : concat_bit_vector::
826 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
827 : {
828 14 : pp_string (pp, "concat_bit_vector");
829 14 : }
830 :
831 : void
832 14 : concat_bit_vector::add_dump_widget_children (text_art::tree_widget &w,
833 : node_dump_state &nds) const
834 : {
835 42 : for (auto iter : m_bit_vectors)
836 56 : w.add_child (iter->make_dump_widget (nds, nullptr));
837 14 : }
838 :
839 : void
840 3983 : concat_bit_vector::accept_node_visitor (node_visitor &v) const
841 : {
842 3983 : if (!v.visit_bit_vector (*this))
843 : return;
844 13444 : for (auto iter : m_bit_vectors)
845 9461 : iter->accept_node_visitor (v);
846 : }
847 :
848 : std::unique_ptr<json::object>
849 8 : concat_bit_vector::to_json () const
850 : {
851 8 : auto obj = bit_vector::make_json_obj ("concat_bit_vector");
852 8 : auto arr = std::make_unique<json::array> ();
853 32 : for (auto iter : m_bit_vectors)
854 24 : arr->append (iter->to_json ());
855 8 : obj->set ("bit_vectors", std::move (arr));
856 16 : return obj;
857 8 : }
858 :
859 : std::unique_ptr<diagnostics::markup::node>
860 0 : concat_bit_vector::make_markup_node (markup_context &) const
861 : {
862 0 : return diagnostics::markup::make_text_node ("concat_bit_vector");
863 : }
864 :
865 : iter_result
866 0 : concat_bit_vector::for_each_svalue (iter_result (*cb) (const svalue &sval,
867 : void *user_data),
868 : void *user_data) const
869 : {
870 0 : for (auto iter : m_bit_vectors)
871 0 : if (iter->for_each_svalue (cb, user_data) == iter_result::stop)
872 0 : return iter_result::stop;
873 : return iter_result::keep_going;
874 : }
875 :
876 : bool
877 77 : concat_bit_vector::implicitly_live_p (liveness_context &liveness_ctxt) const
878 : {
879 145 : for (auto iter : m_bit_vectors)
880 145 : if (!liveness_ctxt.node_live_p (*iter))
881 77 : return false;
882 : return true;
883 : }
884 :
885 : /* crop of concat e.g.:
886 : concat(X1, ... Xn)[a, b] --> concat(X1[...], ..., Xn[...])
887 : */
888 : const bit_vector *
889 3157 : concat_bit_vector::
890 : maybe_fold_crop (const symbolic_bit_range &sym_inner_bits_to_use) const
891 : {
892 3157 : concrete_bit_range conc_inner_bits_to_use;
893 3157 : if (!concrete_from_symbolic (sym_inner_bits_to_use, &conc_inner_bits_to_use))
894 : return nullptr;
895 :
896 3157 : std::vector<const bit_vector *> vec_of_cropped;
897 3157 : concrete_bit_size leading_bits_to_ignore
898 3157 : = conc_inner_bits_to_use.get_start_offset ();
899 3157 : concrete_bit_size remaining_bits_to_add
900 3157 : = conc_inner_bits_to_use.get_size ();
901 11478 : for (auto iter : *this)
902 : {
903 11478 : auto &iter_size = iter->get_size ();
904 11478 : concrete_bit_size conc_iter_size;
905 11478 : if (!concrete_from_symbolic (iter_size, &conc_iter_size))
906 0 : return nullptr;
907 :
908 11478 : concrete_bit_size bits_to_use
909 11478 : = std::min (conc_iter_size - leading_bits_to_ignore,
910 11478 : remaining_bits_to_add);
911 11478 : if (bits_to_use > 0_bits)
912 : {
913 3332 : auto &cropped_iter = iter->get_cropped_bits
914 9996 : (concrete_bit_range (leading_bits_to_ignore,
915 9996 : bits_to_use));
916 3332 : vec_of_cropped.push_back (&cropped_iter);
917 3332 : leading_bits_to_ignore = 0_bits;
918 3332 : remaining_bits_to_add -= bits_to_use;
919 : }
920 : else
921 8146 : leading_bits_to_ignore -= conc_iter_size;
922 :
923 11478 : if (remaining_bits_to_add <= 0_bits)
924 : break;
925 11478 : }
926 :
927 3157 : return &m_bv_mgr.get_concat_bits (std::move (vec_of_cropped));
928 3157 : }
929 :
930 : // class conjured_bit_vector : public bit_vector
931 :
932 : bool
933 351118 : conjured_bit_vector::key::operator< (const key &other) const
934 : {
935 351118 : if (int c = m_stmt->uid - other.m_stmt->uid)
936 279507 : return c < 0;
937 71611 : if (int c = node::cmp (*m_id_reg, *other.m_id_reg))
938 28879 : return c < 0;
939 42732 : return m_idx < other.m_idx;
940 : }
941 :
942 : complexity
943 14662 : conjured_bit_vector::key::get_complexity () const
944 : {
945 14662 : return complexity (m_id_reg);
946 : }
947 :
948 : const symbolic_bit_size &
949 14662 : conjured_bit_vector::key::get_size (scalar_manager<concrete_bit_size> &bit_mgr) const
950 : {
951 14662 : auto size = m_id_reg->maybe_get_bit_size (bit_mgr);
952 14662 : if (!size)
953 8894 : size = &bit_mgr.get_unknown ();
954 14662 : return *size;
955 : }
956 :
957 : tristate
958 546500 : conjured_bit_vector::get_tristate_at_offset (const symbolic_bit_offset &) const
959 : {
960 546500 : return tristate::unknown ();
961 : }
962 :
963 14662 : conjured_bit_vector::conjured_bit_vector (bit_vector_manager &bv_mgr,
964 : const key &k)
965 : : bit_vector (bv_mgr,
966 : k.get_complexity (),
967 14662 : k.get_size (bv_mgr.get_node_manager ()
968 14662 : .get_scalars ().get_bits ())),
969 14662 : m_stmt (k.m_stmt),
970 14662 : m_id_reg (k.m_id_reg),
971 14662 : m_idx (k.m_idx)
972 : {
973 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
974 14662 : gcc_assert (!get_size ().is_zero_p ());
975 14662 : }
976 :
977 : void
978 0 : conjured_bit_vector::
979 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
980 : {
981 0 : pp_printf (pp, "conjured_bit_vector (");
982 0 : pp_gimple_stmt_1 (pp, m_stmt, 0, (dump_flags_t)0);
983 0 : if (m_idx != 0)
984 0 : pp_printf (pp, ", %i", m_idx);
985 0 : pp_character (pp, ')');
986 0 : }
987 :
988 : void
989 0 : conjured_bit_vector::add_dump_widget_children (text_art::tree_widget &w,
990 : node_dump_state &nds) const
991 : {
992 0 : w.add_child (m_id_reg->make_dump_widget (nds, "id_reg"));
993 0 : }
994 :
995 : void
996 1922629 : conjured_bit_vector::accept_node_visitor (node_visitor &v) const
997 : {
998 1922629 : if (!v.visit_bit_vector (*this))
999 : return;
1000 1922629 : m_id_reg->accept_node_visitor (v);
1001 : }
1002 :
1003 : std::unique_ptr<json::object>
1004 0 : conjured_bit_vector::to_json () const
1005 : {
1006 0 : auto obj = bit_vector::make_json_obj ("conjured_bit_vector");
1007 :
1008 0 : {
1009 0 : pretty_printer pp;
1010 0 : pp_format_decoder (&pp) = default_tree_printer;
1011 0 : pp_gimple_stmt_1 (&pp, m_stmt, 0, (dump_flags_t)0);
1012 0 : obj->set_string ("stmt", pp_formatted_text (&pp));
1013 0 : }
1014 0 : obj->set ("id_reg", m_id_reg->to_json ());
1015 0 : obj->set_integer ("id", m_idx);
1016 :
1017 0 : return obj;
1018 : }
1019 :
1020 : std::unique_ptr<diagnostics::markup::node>
1021 12 : conjured_bit_vector::make_markup_node (markup_context &markup_ctxt) const
1022 : {
1023 12 : if (markup_ctxt.m_user_facing)
1024 12 : return m_id_reg->make_markup_node (markup_ctxt);
1025 :
1026 0 : auto markup = std::make_unique<diagnostics::markup::compound_node> ();
1027 0 : markup->add_child ("conjured_bit_vector(");
1028 0 : markup->add_child (")");
1029 0 : return markup;
1030 0 : }
1031 :
1032 : iter_result
1033 597 : conjured_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1034 : void *user_data),
1035 : void *) const
1036 : {
1037 597 : return iter_result::keep_going;
1038 : }
1039 :
1040 : // class cropped_bit_vector : public bit_vector
1041 :
1042 : bool
1043 441230 : cropped_bit_vector::key::operator< (const key &other) const
1044 : {
1045 441230 : if (int c = node::cmp (*m_uncropped_bv,
1046 441230 : *other.m_uncropped_bv))
1047 197096 : return c < 0;
1048 413616 : return symbolic_bit_range::cmp (m_bits_to_use, other.m_bits_to_use) < 0;
1049 : }
1050 :
1051 : tristate
1052 10708 : cropped_bit_vector::
1053 : get_tristate_at_offset (const symbolic_bit_offset &bit) const
1054 : {
1055 10708 : return m_uncropped_bv.get_tristate_at_offset
1056 10708 : (m_crop_start_bit_offset + bit);
1057 : }
1058 :
1059 7749 : cropped_bit_vector::cropped_bit_vector (bit_vector_manager &bv_mgr,
1060 : const key &k)
1061 : : bit_vector (bv_mgr,
1062 7749 : complexity (*k.m_uncropped_bv),
1063 : k.get_size ()),
1064 7749 : m_uncropped_bv (*k.m_uncropped_bv),
1065 7749 : m_crop_start_bit_offset (k.m_bits_to_use.get_start_offset ())
1066 : {
1067 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1068 7749 : gcc_assert (!get_size ().is_zero_p ());
1069 :
1070 : /* No nesting. */
1071 7749 : gcc_assert (!m_uncropped_bv.dyn_cast_cropped_bit_vector ());
1072 :
1073 : /* Cropping of a uniform_bit_vector should have been folded
1074 : before now. */
1075 7749 : gcc_assert (!m_uncropped_bv.dyn_cast_uniform_bit_vector ());
1076 7749 : }
1077 :
1078 : void
1079 7 : cropped_bit_vector::
1080 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1081 : {
1082 7 : pp_string (pp, "cropped_bit_vector");
1083 7 : }
1084 :
1085 : void
1086 7 : cropped_bit_vector::
1087 : print_any_suffix_for_dump_widget_label (pretty_printer *pp) const
1088 : {
1089 7 : concrete_bit_range conc_bits;
1090 7 : if (concrete_from_symbolic (get_uncropped_bits_to_use (), &conc_bits))
1091 : {
1092 7 : pp_string (pp, ": ");
1093 7 : end_any_dump_color (pp);
1094 7 : m_size_in_bits.start_any_dump_color (pp);
1095 7 : conc_bits.dump_to_pp_pretty (pp);
1096 : }
1097 7 : }
1098 :
1099 : void
1100 7 : cropped_bit_vector::add_dump_widget_children (text_art::tree_widget &w,
1101 : node_dump_state &nds) const
1102 : {
1103 14 : w.add_child (m_uncropped_bv.make_dump_widget (nds, "uncropped"));
1104 7 : concrete_bit_range conc_bits;
1105 7 : if (!concrete_from_symbolic (get_uncropped_bits_to_use (), &conc_bits))
1106 0 : w.add_child (get_uncropped_bits_to_use ().make_dump_widget (nds,
1107 : "bits to use"));
1108 7 : }
1109 :
1110 : void
1111 53130 : cropped_bit_vector::accept_node_visitor (node_visitor &v) const
1112 : {
1113 53130 : if (!v.visit_bit_vector (*this))
1114 : return;
1115 273 : m_uncropped_bv.accept_node_visitor (v);
1116 : }
1117 :
1118 : std::unique_ptr<json::object>
1119 4 : cropped_bit_vector::to_json () const
1120 : {
1121 4 : auto obj = bit_vector::make_json_obj ("cropped_bit_vector");
1122 4 : obj->set ("crop_start_bit_offset", m_crop_start_bit_offset.to_json ());
1123 4 : obj->set ("uncropped_bv", m_uncropped_bv.to_json ());
1124 4 : return obj;
1125 : }
1126 :
1127 : std::unique_ptr<diagnostics::markup::node>
1128 0 : cropped_bit_vector::make_markup_node (markup_context &markup_ctxt) const
1129 : {
1130 0 : auto result = std::make_unique<diagnostics::markup::compound_node> ();
1131 0 : result->add_child ("cropped_bit_vector (start_bit_offset=");
1132 0 : result->add_child (m_crop_start_bit_offset.make_markup_node (markup_ctxt));
1133 0 : result->add_child (", uncropped_bv=");
1134 0 : result->add_child (m_uncropped_bv.make_markup_node (markup_ctxt));
1135 0 : result->add_child (")");
1136 0 : return result;
1137 0 : }
1138 :
1139 : iter_result
1140 43 : cropped_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1141 : void *user_data),
1142 : void *) const
1143 : {
1144 43 : return iter_result::keep_going;
1145 : }
1146 :
1147 : bool
1148 754 : cropped_bit_vector::implicitly_live_p (liveness_context &liveness_ctxt) const
1149 : {
1150 754 : return m_uncropped_bv.implicitly_live_p (liveness_ctxt);
1151 : }
1152 :
1153 : /* Crop of crop: A[m..n][p..q] --> A[m + n .. m + n + sizeof(p..q)]. */
1154 :
1155 : const bit_vector *
1156 2295 : cropped_bit_vector::
1157 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1158 : {
1159 2295 : symbolic_bit_range bit_range_relative_to_grandchild
1160 2295 : ((get_uncropped_bits_to_use ().get_start_offset ()
1161 : + bits_to_use.get_start_offset ()),
1162 2295 : bits_to_use.get_size ());
1163 2295 : return &m_bv_mgr.get_cropped_bits (get_uncropped_bit_vector (),
1164 2295 : bit_range_relative_to_grandchild);
1165 : }
1166 :
1167 : // class named_test_bit_vector : public bit_vector
1168 :
1169 : #if CHECKING_P
1170 :
1171 : tristate
1172 0 : named_test_bit_vector::
1173 : get_tristate_at_offset (const symbolic_bit_offset &) const
1174 : {
1175 0 : return tristate::unknown ();
1176 : }
1177 :
1178 128 : named_test_bit_vector::
1179 : named_test_bit_vector (bit_vector_manager &bv_mgr,
1180 : const symbolic_bit_size &size_in_bits,
1181 : std::string label)
1182 128 : : bit_vector (bv_mgr, complexity (1, 1), size_in_bits),
1183 128 : m_label (std::move (label))
1184 : {
1185 128 : }
1186 :
1187 : void
1188 8 : named_test_bit_vector::
1189 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1190 : {
1191 8 : pp_string (pp, "named_test_bit_vector");
1192 8 : }
1193 :
1194 : void
1195 8 : named_test_bit_vector::
1196 : print_any_suffix_for_dump_widget_label (pretty_printer *pp) const
1197 : {
1198 8 : pp_printf (pp, ": %qs", m_label.c_str ());
1199 8 : }
1200 :
1201 : void
1202 8 : named_test_bit_vector::add_dump_widget_children (text_art::tree_widget &,
1203 : node_dump_state &) const
1204 : {
1205 : // no-op
1206 8 : }
1207 :
1208 : void
1209 0 : named_test_bit_vector::accept_node_visitor (node_visitor &v) const
1210 : {
1211 0 : v.visit_bit_vector (*this);
1212 0 : }
1213 :
1214 : std::unique_ptr<json::object>
1215 28 : named_test_bit_vector::to_json () const
1216 : {
1217 28 : auto obj = bit_vector::make_json_obj ("named_test_bit_vector");
1218 56 : obj->set_string ("label", m_label);
1219 28 : return obj;
1220 : }
1221 :
1222 : std::unique_ptr<diagnostics::markup::node>
1223 0 : named_test_bit_vector::make_markup_node (markup_context &) const
1224 : {
1225 0 : return diagnostics::markup::make_text_node (m_label);
1226 : }
1227 :
1228 : iter_result
1229 0 : named_test_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1230 : void *user_data),
1231 : void *) const
1232 : {
1233 0 : return iter_result::keep_going;
1234 : }
1235 :
1236 : #endif /* #if CHECKING_P */
1237 :
1238 : // class empty_bit_vector : public bit_vector
1239 :
1240 : tristate
1241 0 : empty_bit_vector::get_tristate_at_offset (const symbolic_bit_offset &) const
1242 : {
1243 : /* This should never be called; there are no bits to query. */
1244 0 : gcc_unreachable ();
1245 : }
1246 :
1247 4495 : empty_bit_vector::empty_bit_vector (bit_vector_manager &bv_mgr)
1248 : : uniform_bit_vector (bv_mgr,
1249 4495 : complexity (1, 1),
1250 4495 : (bv_mgr.get_node_manager ()
1251 4495 : .get_scalars ().get_bits ().get_concrete (0_bits)))
1252 : {
1253 4495 : }
1254 :
1255 : void
1256 0 : empty_bit_vector::
1257 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1258 : {
1259 0 : pp_string (pp, "empty_bit_vector");
1260 0 : }
1261 :
1262 : void
1263 0 : empty_bit_vector::add_dump_widget_children (text_art::tree_widget &,
1264 : node_dump_state &) const
1265 : {
1266 : // No-op
1267 0 : }
1268 :
1269 : void
1270 42 : empty_bit_vector::accept_node_visitor (node_visitor &v) const
1271 : {
1272 42 : v.visit_bit_vector (*this);
1273 42 : }
1274 :
1275 : std::unique_ptr<json::object>
1276 4 : empty_bit_vector::to_json () const
1277 : {
1278 4 : return bit_vector::make_json_obj ("empty_bit_vector");
1279 : }
1280 :
1281 : std::unique_ptr<diagnostics::markup::node>
1282 0 : empty_bit_vector::make_markup_node (markup_context &) const
1283 : {
1284 0 : return diagnostics::markup::make_text_node ("empty_bit_vector");
1285 : }
1286 :
1287 : iter_result
1288 0 : empty_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1289 : void *user_data),
1290 : void *) const
1291 : {
1292 0 : return iter_result::keep_going;
1293 : }
1294 :
1295 : // class poisoned_bit_vector : public bit_vector
1296 :
1297 : bool
1298 6462589 : poisoned_bit_vector::key::operator< (const key &other) const
1299 : {
1300 6462589 : if (int c = node::cmp (m_size_in_bits, other.m_size_in_bits))
1301 3137865 : return c < 0;
1302 3324724 : return (int)m_poison_kind < (int)other.m_poison_kind;
1303 : }
1304 :
1305 : tristate
1306 22679 : poisoned_bit_vector::get_tristate_at_offset (const symbolic_bit_offset &) const
1307 : {
1308 22679 : return tristate::unknown ();
1309 : }
1310 :
1311 12611 : poisoned_bit_vector::poisoned_bit_vector (bit_vector_manager &bv_mgr,
1312 : const key &k)
1313 : : uniform_bit_vector (bv_mgr,
1314 12611 : complexity (1, 1),
1315 : k.m_size_in_bits),
1316 12611 : m_poison_kind (k.m_poison_kind)
1317 : {
1318 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1319 12611 : gcc_assert (!get_size ().is_zero_p ());
1320 12611 : }
1321 :
1322 : void
1323 20 : poisoned_bit_vector::
1324 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1325 : {
1326 20 : pp_string (pp, "poisoned_bit_vector");
1327 20 : }
1328 :
1329 : void
1330 20 : poisoned_bit_vector::
1331 : print_any_suffix_for_dump_widget_label (pretty_printer *pp) const
1332 : {
1333 20 : pp_printf (pp, ": %s", poison_kind_to_str (m_poison_kind));
1334 20 : }
1335 :
1336 : void
1337 20 : poisoned_bit_vector::add_dump_widget_children (text_art::tree_widget &,
1338 : node_dump_state &) const
1339 : {
1340 : // no-op
1341 20 : }
1342 :
1343 : void
1344 20193 : poisoned_bit_vector::accept_node_visitor (node_visitor &v) const
1345 : {
1346 20193 : v.visit_bit_vector (*this);
1347 20193 : }
1348 :
1349 : std::unique_ptr<json::object>
1350 8 : poisoned_bit_vector::to_json () const
1351 : {
1352 8 : auto obj = bit_vector::make_json_obj ("poisoned_bit_vector");
1353 8 : obj->set_string ("poison_kind", poison_kind_to_str (m_poison_kind));
1354 8 : return obj;
1355 : }
1356 :
1357 : std::unique_ptr<diagnostics::markup::node>
1358 0 : poisoned_bit_vector::make_markup_node (markup_context &) const
1359 : {
1360 0 : return diagnostics::markup::make_text_node ("poisoned_bit_vector");
1361 : }
1362 :
1363 : iter_result
1364 18 : poisoned_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1365 : void *user_data),
1366 : void *) const
1367 : {
1368 18 : return iter_result::keep_going;
1369 : }
1370 :
1371 : /* poisoned(x)[m..n] --> poisoned(x, sizeof(m..n)). */
1372 :
1373 : const bit_vector *
1374 52989 : poisoned_bit_vector::
1375 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1376 : {
1377 52989 : return &m_bv_mgr.get_poisoned_bits (bits_to_use.get_size (),
1378 52989 : get_poison_kind ());
1379 : }
1380 :
1381 : // class repeated_bit_vector : public bit_vector
1382 :
1383 : bool
1384 2058 : repeated_bit_vector::key::operator< (const key &other) const
1385 : {
1386 2058 : if (m_inner_bv != other.m_inner_bv)
1387 859 : return m_inner_bv->get_id () < other.m_inner_bv->get_id ();
1388 1199 : return m_repeat_count < other.m_repeat_count;
1389 : }
1390 :
1391 294 : repeated_bit_vector::repeated_bit_vector (bit_vector_manager &bv_mgr,
1392 : const key &k)
1393 : : bit_vector (bv_mgr,
1394 294 : complexity (*k.m_inner_bv),
1395 : k.get_size ()),
1396 294 : m_inner_bv (*k.m_inner_bv),
1397 588 : m_repeat_count (*k.m_repeat_count)
1398 : {
1399 294 : gcc_assert (!m_repeat_count.is_zero_p ());
1400 294 : gcc_assert (!m_repeat_count.is_one_p ());
1401 :
1402 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1403 294 : gcc_assert (!get_size ().is_zero_p ());
1404 :
1405 : /* No nesting. */
1406 294 : gcc_assert (!m_inner_bv.dyn_cast_repeated_bit_vector ());
1407 :
1408 : /* Repetition of a uniform_bit_vector should have been folded
1409 : before now. */
1410 294 : gcc_assert (!m_inner_bv.dyn_cast_uniform_bit_vector ());
1411 294 : }
1412 :
1413 : void
1414 0 : repeated_bit_vector::
1415 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1416 : {
1417 0 : pp_string (pp, "repeated_bit_vector");
1418 0 : }
1419 :
1420 : void
1421 0 : repeated_bit_vector::add_dump_widget_children (text_art::tree_widget &w,
1422 : node_dump_state &nds) const
1423 : {
1424 0 : w.add_child (m_inner_bv.make_dump_widget (nds, "m_inner_bv"));
1425 0 : w.add_child (m_repeat_count.make_dump_widget (nds, "m_repeat_count"));
1426 0 : }
1427 :
1428 : void
1429 118759 : repeated_bit_vector::accept_node_visitor (node_visitor &v) const
1430 : {
1431 118759 : if (!v.visit_bit_vector (*this))
1432 : return;
1433 118759 : m_inner_bv.accept_node_visitor (v);
1434 : }
1435 :
1436 : std::unique_ptr<json::object>
1437 4 : repeated_bit_vector::to_json () const
1438 : {
1439 4 : auto obj = bit_vector::make_json_obj ("repeated_bit_vector");
1440 4 : obj->set ("inner_bv", m_inner_bv.to_json ());
1441 4 : obj->set ("repeat_count", m_repeat_count.to_json ());
1442 4 : return obj;
1443 : }
1444 :
1445 : std::unique_ptr<diagnostics::markup::node>
1446 0 : repeated_bit_vector::make_markup_node (markup_context &) const
1447 : {
1448 0 : return diagnostics::markup::make_text_node ("repeated_bit_vector");
1449 : }
1450 :
1451 : iter_result
1452 525 : repeated_bit_vector::for_each_svalue (iter_result (*cb) (const svalue &sval,
1453 : void *user_data),
1454 : void *user_data) const
1455 : {
1456 525 : return m_inner_bv.for_each_svalue (cb, user_data);
1457 : }
1458 :
1459 : const bit_vector *
1460 2734 : repeated_bit_vector::
1461 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1462 : {
1463 2734 : concrete_bit_range conc_bits_to_use;
1464 2734 : if (!concrete_from_symbolic (bits_to_use, &conc_bits_to_use))
1465 : return nullptr;
1466 :
1467 2734 : concrete_bit_size conc_bit_size;
1468 2734 : if (!concrete_from_symbolic (get_size (), &conc_bit_size))
1469 : return nullptr;
1470 :
1471 2734 : concrete_bit_size conc_inner_bit_size;
1472 2734 : if (!concrete_from_symbolic (m_inner_bv.get_size (),
1473 : &conc_inner_bit_size))
1474 : return nullptr;
1475 :
1476 2734 : concrete_bit_size remaining_bits_to_add = conc_bits_to_use.get_size ();
1477 :
1478 2734 : std::vector<const bit_vector *> vec_of_cropped;
1479 :
1480 : /* Do we have a leading fragment from one of the iterations? */
1481 2734 : concrete_bit_offset bit_offset_within_repeat
1482 2734 : = conc_bits_to_use.get_start_offset ().floor_mod (conc_inner_bit_size);
1483 2734 : if (bit_offset_within_repeat > 0_bits)
1484 : {
1485 796 : concrete_bit_size leading_size
1486 796 : = std::min (conc_inner_bit_size - bit_offset_within_repeat,
1487 796 : remaining_bits_to_add);
1488 796 : auto &leading_suffix = m_inner_bv.get_cropped_bits
1489 1592 : (concrete_bit_range (bit_offset_within_repeat,
1490 2388 : leading_size));
1491 796 : vec_of_cropped.push_back (&leading_suffix);
1492 796 : remaining_bits_to_add -= leading_size;
1493 796 : }
1494 :
1495 : /* Do we have any full iterations of the inner bv? */
1496 2734 : integer num_full_repeats
1497 2734 : = remaining_bits_to_add.floor_div (conc_inner_bit_size);
1498 2734 : if (num_full_repeats > 0)
1499 : {
1500 1803 : auto &full_repeats = m_bv_mgr.get_repeated_bits (m_inner_bv,
1501 : num_full_repeats);
1502 1803 : vec_of_cropped.push_back (&full_repeats);
1503 1803 : remaining_bits_to_add -= (conc_inner_bit_size * num_full_repeats);
1504 : }
1505 :
1506 : /* Do we have a trailing suffix from one of the iterations? */
1507 2734 : if (remaining_bits_to_add > 0_bits)
1508 : {
1509 147 : gcc_assert (remaining_bits_to_add < conc_inner_bit_size);
1510 147 : auto &trailing_prefix
1511 147 : = m_inner_bv.get_prefix (remaining_bits_to_add);
1512 147 : vec_of_cropped.push_back (&trailing_prefix);
1513 : }
1514 :
1515 2734 : return &m_bv_mgr.get_concat_bits (std::move (vec_of_cropped));
1516 8202 : }
1517 :
1518 : // class bit_vector_from_svalue : public bit_vector
1519 :
1520 : bool
1521 32249950 : bit_vector_from_svalue::key::operator< (const key &other) const
1522 : {
1523 32249950 : if (m_sval != other.m_sval)
1524 31656176 : return m_sval->get_id () < other.m_sval->get_id ();
1525 : return false;
1526 : }
1527 :
1528 : const symbolic_bit_size &
1529 1202186 : bit_vector_from_svalue::key::
1530 : get_size (scalar_manager<concrete_bit_size> &bit_mgr) const
1531 : {
1532 1202186 : concrete_bit_size conc_size;
1533 1202186 : bool has_size = m_sval->has_concrete_size_p (&conc_size,
1534 : bit_mgr);
1535 1202186 : gcc_assert (has_size);
1536 1202186 : return bit_mgr.get_concrete (conc_size);
1537 1202186 : }
1538 :
1539 : tristate
1540 18316 : bit_vector_from_svalue::
1541 : get_tristate_at_offset (const symbolic_bit_offset &bit) const
1542 : {
1543 18316 : auto &bit_mgr = bit.get_mgr ();
1544 18316 : auto &cropped_bit
1545 18316 : = get_cropped_bits (symbolic_bit_range (bit, bit_mgr.get_concrete (1_bit)));
1546 18316 : if (auto *tristate_bit = cropped_bit.dyn_cast_tristate_bit_vector ())
1547 8896 : return tristate_bit->get_bit_value ();
1548 :
1549 9420 : return tristate::unknown ();
1550 : }
1551 :
1552 1202186 : bit_vector_from_svalue::bit_vector_from_svalue (bit_vector_manager &bv_mgr,
1553 : const key &k)
1554 : : bit_vector (bv_mgr,
1555 1202186 : complexity (*k.m_sval),
1556 1202186 : k.get_size (bv_mgr.get_node_manager ()
1557 1202186 : .get_scalars ().get_bits ())),
1558 2404372 : m_sval (*k.m_sval)
1559 : {
1560 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1561 1202186 : gcc_assert (!get_size ().is_zero_p ());
1562 :
1563 : // No nesting
1564 1202186 : gcc_assert (!m_sval.dyn_cast_svalue_from_bit_vector ());
1565 1202186 : }
1566 :
1567 : void
1568 1755 : bit_vector_from_svalue::
1569 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1570 : {
1571 1755 : pp_string (pp, "bit_vector_from_svalue");
1572 1755 : }
1573 :
1574 : void
1575 1755 : bit_vector_from_svalue::
1576 : add_dump_widget_children (text_art::tree_widget &w,
1577 : node_dump_state &nds) const
1578 : {
1579 3510 : w.add_child (m_sval.make_dump_widget (nds, "m_sval"));
1580 1755 : }
1581 :
1582 : bool
1583 0 : bit_vector_from_svalue::
1584 : maybe_print_for_user (pretty_printer *pp,
1585 : const region_model &model,
1586 : const symbolic::node *) const
1587 : {
1588 0 : return m_sval.maybe_print_for_user (pp, model, nullptr);
1589 : }
1590 :
1591 : void
1592 5222107 : bit_vector_from_svalue::accept_node_visitor (node_visitor &v) const
1593 : {
1594 5222107 : if (!v.visit_bit_vector (*this))
1595 : return;
1596 5222107 : m_sval.accept_node_visitor (v);
1597 : }
1598 :
1599 : std::unique_ptr<json::object>
1600 88 : bit_vector_from_svalue::to_json () const
1601 : {
1602 88 : auto obj = bit_vector::make_json_obj ("bit_vector_from_svalue");
1603 88 : obj->set ("sval", m_sval.to_json ());
1604 88 : return obj;
1605 : }
1606 :
1607 : std::unique_ptr<diagnostics::markup::node>
1608 0 : bit_vector_from_svalue::make_markup_node (markup_context &markup_ctxt) const
1609 : {
1610 0 : return m_sval.make_markup_node (markup_ctxt);
1611 : }
1612 :
1613 : iter_result
1614 6116 : bit_vector_from_svalue::for_each_svalue (iter_result (*cb) (const svalue &sval,
1615 : void *user_data),
1616 : void *user_data) const
1617 : {
1618 6116 : return m_sval.for_each_svalue (cb, user_data);
1619 : }
1620 :
1621 : const bit_vector *
1622 27039 : bit_vector_from_svalue::
1623 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1624 : {
1625 27039 : concrete_bit_range conc_bits_to_use;
1626 27039 : if (!concrete_from_symbolic (bits_to_use, &conc_bits_to_use))
1627 : return nullptr;
1628 :
1629 27039 : if (auto *folded = maybe_fold_crop_from_cst (conc_bits_to_use))
1630 : return folded;
1631 :
1632 13379 : if (auto *folded = maybe_fold_crop_from_bitmask (conc_bits_to_use))
1633 : return folded;
1634 :
1635 : return nullptr;
1636 27039 : }
1637 :
1638 : const bit_vector *
1639 27039 : bit_vector_from_svalue::
1640 : maybe_fold_crop_from_cst (const concrete_bit_range &inner_bits_to_use) const
1641 : {
1642 : /* Attempt to extract bytes from constants. */
1643 27039 : tree tree_cst = m_sval.maybe_get_constant ();
1644 27039 : if (!tree_cst)
1645 14221 : if (auto *init_sval = m_sval.dyn_cast_initial_svalue ())
1646 13383 : if (auto *string_reg
1647 13383 : = init_sval->get_region ()->dyn_cast_string_region ())
1648 1192 : tree_cst = string_reg->get_string_cst ();
1649 14221 : if (!tree_cst)
1650 : return nullptr;
1651 :
1652 14010 : concrete_byte_range conc_bytes_to_use;
1653 14010 : concrete_bit_offset bit_offset;
1654 14010 : get_smallest_enclosing_byte_range (inner_bits_to_use,
1655 : &conc_bytes_to_use,
1656 : &bit_offset);
1657 14010 : gcc_assert (bit_offset < 8_bits);
1658 28020 : if (auto p = m_bv_mgr.maybe_fold_bytes_within_constant (tree_cst,
1659 : conc_bytes_to_use))
1660 : {
1661 13660 : auto effective_size = inner_bits_to_use.get_size ();
1662 13660 : if (p == this)
1663 : {
1664 : /* We're looking at bits within one of the m_cached_bytes
1665 : instances; we need to avoid infinite recursion. */
1666 9291 : gcc_assert (TREE_TYPE (tree_cst) == unsigned_char_type_node);
1667 9291 : gcc_assert (effective_size < 8_bits);
1668 9291 : int byte_val = TREE_INT_CST_LOW (tree_cst);
1669 9291 : gcc_assert (this == &m_bv_mgr.get_uint8 (byte_val));
1670 9291 : std::vector<const symbolic::bit_vector *> raw_bits;
1671 9291 : int idx_after_bits
1672 9291 : = bit_offset.as_long () + effective_size.as_long ();
1673 9291 : for (int bit_idx = bit_offset.as_long ();
1674 18582 : bit_idx < idx_after_bits; ++bit_idx)
1675 : {
1676 : /* Add in LSB through MSB order. */
1677 9291 : bool bit_val = byte_val & (1 << bit_idx);
1678 9291 : raw_bits.push_back
1679 14091 : (&m_bv_mgr.get_tristate_bits (1_bit, tristate (bit_val)));
1680 : }
1681 9291 : return &m_bv_mgr.get_concat_bits (std::move (raw_bits));
1682 9291 : }
1683 : else
1684 4369 : return &p->get_cropped_bits
1685 13107 : (concrete_bit_range (bit_offset, effective_size));
1686 13660 : }
1687 :
1688 : return nullptr;
1689 28020 : }
1690 :
1691 : const bit_vector *
1692 13379 : bit_vector_from_svalue::
1693 : maybe_fold_crop_from_bitmask (const concrete_bit_range &inner_bits_to_use) const
1694 : {
1695 13379 : if (inner_bits_to_use.get_next_offset () > 64_bits)
1696 : return nullptr;
1697 :
1698 9440 : value_range vr;
1699 9440 : if (!m_sval.maybe_get_value_range (vr))
1700 : return nullptr;
1701 :
1702 885 : irange_bitmask bitmask = vr.get_bitmask ();
1703 :
1704 : /* The unknown bits are those for which the mask is one.
1705 : Bail out if we have any unknown bits in the bit_range of interest. */
1706 885 : unsigned HOST_WIDE_INT mask_for_bits
1707 : = wi::extract_uhwi
1708 885 : (bitmask.mask (),
1709 1955 : inner_bits_to_use.get_start_offset ().as_unsigned_long (),
1710 885 : inner_bits_to_use.get_size ().as_unsigned_long ());
1711 885 : if (mask_for_bits != 0)
1712 : return nullptr;
1713 :
1714 : // We have known bits; get their value.
1715 456 : unsigned HOST_WIDE_INT known_value
1716 : = wi::extract_uhwi
1717 456 : (bitmask.value (),
1718 928 : inner_bits_to_use.get_start_offset ().as_unsigned_long (),
1719 456 : inner_bits_to_use.get_size ().as_unsigned_long ());
1720 456 : if (inner_bits_to_use.get_size ().is_one_p ())
1721 720 : return &m_bv_mgr.get_tristate_bits (1_bit, known_value != 0);
1722 32 : else if (inner_bits_to_use.get_size () == 8_bits)
1723 32 : return &m_bv_mgr.get_uint8 (known_value);
1724 : else
1725 : return nullptr;
1726 9440 : }
1727 :
1728 : bool
1729 726 : bit_vector_from_svalue::
1730 : implicitly_live_p (symbolic::liveness_context &liveness_ctxt) const
1731 : {
1732 726 : return liveness_ctxt.node_live_p (m_sval);
1733 : }
1734 :
1735 : // class bit_vector_from_ubc : public bit_vector
1736 :
1737 : tristate
1738 255220 : bit_vector_from_ubc::
1739 : get_tristate_at_offset (const symbolic_bit_offset &) const
1740 : {
1741 255220 : return tristate::unknown ();
1742 : }
1743 :
1744 14661 : bit_vector_from_ubc::
1745 : bit_vector_from_ubc (const unfoldable_bit_contents &bc,
1746 : symbolic_bit_range bit_range)
1747 14661 : : bit_vector (bc.get_node_mgr ().get_bit_vector_manager (),
1748 : complexity::from_pair
1749 : (bc.get_complexity (),
1750 29322 : bit_range.get_children_complexity ()),
1751 : // FIXME: what about symbolic?
1752 14661 : bit_range.get_size ()),
1753 14661 : m_unfoldable_bit_contents (bc),
1754 14661 : m_bit_range (bit_range)
1755 : {
1756 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1757 14661 : gcc_assert (!get_size ().is_zero_p ());
1758 14661 : }
1759 :
1760 : void
1761 875 : bit_vector_from_ubc::
1762 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1763 : {
1764 875 : pp_string (pp, "bit_vector_from_ubc");
1765 875 : }
1766 :
1767 : void
1768 846 : bit_vector_from_ubc::
1769 : add_dump_widget_children (text_art::tree_widget &w,
1770 : node_dump_state &nds) const
1771 : {
1772 846 : w.add_child
1773 1692 : (m_unfoldable_bit_contents.make_dump_widget (nds, "bit_contents"));
1774 1692 : w.add_child (m_bit_range.make_dump_widget (nds, "range"));
1775 846 : }
1776 :
1777 : void
1778 14824387 : bit_vector_from_ubc::accept_node_visitor (node_visitor &v) const
1779 : {
1780 14824387 : if (!v.visit_bit_vector (*this))
1781 : return;
1782 14824387 : m_unfoldable_bit_contents.accept_node_visitor (v);
1783 14824387 : m_bit_range.accept_node_visitor (v);
1784 : }
1785 :
1786 : bool
1787 125172 : bit_vector_from_ubc::conjured_p () const
1788 : {
1789 125172 : return m_unfoldable_bit_contents.conjured_p ();
1790 : }
1791 :
1792 : std::unique_ptr<json::object>
1793 0 : bit_vector_from_ubc::to_json () const
1794 : {
1795 0 : auto obj
1796 0 : = bit_vector::make_json_obj ("bit_vector_from_ubc");
1797 0 : obj->set ("bit_contents", m_unfoldable_bit_contents.to_json ());
1798 0 : obj->set ("bit_range", m_bit_range.to_json ());
1799 0 : return obj;
1800 : }
1801 :
1802 : std::unique_ptr<diagnostics::markup::node>
1803 0 : bit_vector_from_ubc::make_markup_node (markup_context &) const
1804 : {
1805 0 : return diagnostics::markup::make_text_node ("bit_vector_from_ubc");
1806 : }
1807 :
1808 : iter_result
1809 13066 : bit_vector_from_ubc::for_each_svalue (iter_result (*) (const svalue &sval,
1810 : void *user_data),
1811 : void *) const
1812 : {
1813 13066 : return iter_result::keep_going;
1814 : }
1815 :
1816 : bool
1817 300705 : bit_vector_from_ubc::
1818 : implicitly_live_p (symbolic::liveness_context &liveness_ctxt) const
1819 : {
1820 300705 : return liveness_ctxt.bits_live_p (*this);
1821 : }
1822 :
1823 : /* crop(bit_vector_from_ubc (UBC, RANGE), SUBRANGE)
1824 : --> bit_vector_from_ubc (UBC, SUBRANGE OF RANGE). */
1825 :
1826 : const bit_vector *
1827 82512 : bit_vector_from_ubc::
1828 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1829 : {
1830 82512 : auto &result_size = bits_to_use.get_size ();
1831 82512 : symbolic_bit_range subrange
1832 82512 : (get_bit_range ().get_start_offset ()
1833 : + bits_to_use.get_start_offset (),
1834 82512 : result_size);
1835 82512 : return &get_bit_contents ().get_bits (subrange);
1836 : }
1837 :
1838 : // class tristate_bit_vector : public bit_vector
1839 :
1840 : bool
1841 2508148 : tristate_bit_vector::key::operator< (const key &other) const
1842 : {
1843 2508148 : if (int c = node::cmp (m_size_in_bits, other.m_size_in_bits))
1844 688110 : return c < 0;
1845 1820038 : return (static_cast<int> (m_bit_value.get_value ())
1846 1820038 : < static_cast<int> (other.m_bit_value.get_value ()));
1847 : }
1848 :
1849 : tristate
1850 653 : tristate_bit_vector::get_tristate_at_offset (const symbolic_bit_offset &) const
1851 : {
1852 653 : return m_bit_value;
1853 : }
1854 :
1855 19736 : tristate_bit_vector::tristate_bit_vector (bit_vector_manager &bv_mgr,
1856 : const key &k)
1857 19736 : : uniform_bit_vector (bv_mgr, complexity (1, 1), k.m_size_in_bits),
1858 19736 : m_bit_value (k.m_bit_value)
1859 : {
1860 : /* The only empty bit_vector should be the singleton empty_bit_vector. */
1861 19736 : gcc_assert (!get_size ().is_zero_p ());
1862 19736 : }
1863 :
1864 : void
1865 363 : tristate_bit_vector::
1866 : print_prefix_for_dump_widget_label (pretty_printer *pp) const
1867 : {
1868 363 : pp_string (pp, "tristate_bit_vector");
1869 363 : }
1870 :
1871 : void
1872 363 : tristate_bit_vector::
1873 : print_any_suffix_for_dump_widget_label (pretty_printer *pp) const
1874 : {
1875 363 : pp_string (pp, ": ");
1876 363 : if (m_size_in_bits.is_one_p ())
1877 12 : switch (m_bit_value.get_value ())
1878 : {
1879 0 : default:
1880 0 : gcc_unreachable ();
1881 4 : case tristate::TS_UNKNOWN:
1882 4 : pp_string (pp, "unknown bit");
1883 4 : break;
1884 4 : case tristate::TS_TRUE:
1885 4 : pp_string (pp, "true bit");
1886 4 : break;
1887 4 : case tristate::TS_FALSE:
1888 4 : pp_string (pp, "false bit");
1889 4 : break;
1890 : }
1891 : else
1892 351 : switch (m_bit_value.get_value ())
1893 : {
1894 0 : default:
1895 0 : gcc_unreachable ();
1896 152 : case tristate::TS_UNKNOWN:
1897 152 : pp_string (pp, "unknown bits");
1898 152 : break;
1899 16 : case tristate::TS_TRUE:
1900 16 : pp_string (pp, "true bits");
1901 16 : break;
1902 183 : case tristate::TS_FALSE:
1903 183 : pp_string (pp, "false bits");
1904 183 : break;
1905 : }
1906 363 : }
1907 :
1908 : void
1909 363 : tristate_bit_vector::add_dump_widget_children (text_art::tree_widget &,
1910 : node_dump_state &) const
1911 : {
1912 : // no-op
1913 363 : }
1914 :
1915 : void
1916 1860557 : tristate_bit_vector::accept_node_visitor (node_visitor &v) const
1917 : {
1918 1860557 : v.visit_bit_vector (*this);
1919 1860557 : }
1920 :
1921 : std::unique_ptr<json::object>
1922 40 : tristate_bit_vector::to_json () const
1923 : {
1924 40 : auto obj = bit_vector::make_json_obj ("tristate_bit_vector");
1925 40 : obj->set_string ("bit_value", m_bit_value.as_string ());
1926 40 : return obj;
1927 : }
1928 :
1929 : std::unique_ptr<diagnostics::markup::node>
1930 0 : tristate_bit_vector::make_markup_node (markup_context &) const
1931 : {
1932 0 : return diagnostics::markup::make_text_node ("tristate_bit_vector");
1933 : }
1934 :
1935 : iter_result
1936 1852 : tristate_bit_vector::for_each_svalue (iter_result (*) (const svalue &sval,
1937 : void *user_data),
1938 : void *) const
1939 : {
1940 1852 : return iter_result::keep_going;
1941 : }
1942 :
1943 : /* tristate(x)[m..n] --> tristate(x, sizeof(m..n)). */
1944 :
1945 : const bit_vector *
1946 252539 : tristate_bit_vector::
1947 : maybe_fold_crop (const symbolic_bit_range &bits_to_use) const
1948 : {
1949 252539 : return &m_bv_mgr.get_tristate_bits (bits_to_use.get_size (),
1950 252539 : get_bit_value ());
1951 : }
1952 :
1953 : /* symbolic::bit_vector consolidation. */
1954 :
1955 4495 : bit_vector_manager::bit_vector_manager (node_manager &node_mgr)
1956 4495 : : m_node_mgr (node_mgr),
1957 4495 : m_empty_bv (*this)
1958 : {
1959 : // Populate m_cached_bytes
1960 4495 : memset (m_cached_bytes, 0, sizeof (m_cached_bytes));
1961 4495 : m_cached_bytes[0x00] = &get_all_zeros (1_byte);
1962 1146225 : for (int i = 1; i < 255; ++i)
1963 : {
1964 1141730 : auto &rmm = node_mgr.get_region_model_manager ();
1965 1141730 : auto &sval = *rmm.get_or_create_int_cst (unsigned_char_type_node, i);
1966 1141730 : m_cached_bytes[i] = &get_bit_vector_from_svalue (sval);
1967 : }
1968 4495 : m_cached_bytes[0xff] = &get_all_ones (1_byte);
1969 4495 : }
1970 :
1971 :
1972 : const bit_vector &
1973 630015 : bit_vector_manager::get_tristate_bits (const symbolic_bit_size &num_bits,
1974 : tristate bit_val)
1975 : {
1976 630015 : if (num_bits.is_zero_p ())
1977 4 : return m_empty_bv;
1978 :
1979 630011 : const symbolic::tristate_bit_vector::key k {num_bits, bit_val};
1980 630011 : auto iter = m_tristate_map.find (k);
1981 630011 : if (iter != m_tristate_map.end ())
1982 610275 : return *(iter->second);
1983 19736 : auto bv_up = std::make_unique<symbolic::tristate_bit_vector> (*this, k);
1984 19736 : auto &result = *bv_up.get ();
1985 19736 : m_tristate_map.emplace (k, std::move (bv_up));
1986 19736 : return result;
1987 19736 : }
1988 :
1989 : const bit_vector &
1990 272514 : bit_vector_manager::get_tristate_bits (const concrete_bit_size &num_bits,
1991 : tristate bit_val)
1992 : {
1993 272514 : return get_tristate_bits (symbolic_from_concrete (num_bits,
1994 : get_bit_manager ()),
1995 272514 : bit_val);
1996 : }
1997 :
1998 : // Unknown bits of unknown length
1999 : const bit_vector &
2000 326 : bit_vector_manager::get_unknown_bits ()
2001 : {
2002 326 : const symbolic_bit_size &unknown_size = get_bit_manager ().get_unknown ();
2003 326 : return get_tristate_bits (unknown_size, tristate::unknown ());
2004 : }
2005 :
2006 : static void
2007 2312454 : append_canonically (std::vector<const bit_vector *> &canonical_bvs,
2008 : const bit_vector &bv_to_add,
2009 : bit_vector_manager &bv_mgr)
2010 : {
2011 2312454 : if (bv_to_add.get_size ().is_zero_p ())
2012 : return;
2013 :
2014 : /* Flatten adding concat bit_vectors. */
2015 2312438 : if (auto concat_bv = bv_to_add.dyn_cast_concat_bit_vector ())
2016 : {
2017 184 : for (auto iter : *concat_bv)
2018 128 : append_canonically (canonical_bvs, *iter, bv_mgr);
2019 : return;
2020 : }
2021 :
2022 2312382 : if (!canonical_bvs.empty ())
2023 : {
2024 : /* Check for merging opportunities. */
2025 41985 : auto &back = canonical_bvs.back ();
2026 41985 : gcc_assert (back);
2027 :
2028 : /* A + A --> A * 2 */
2029 41985 : if (&bv_to_add == back)
2030 : {
2031 832 : back = &bv_mgr.get_repeated_bits (bv_to_add, 2);
2032 832 : return;
2033 : }
2034 :
2035 : /* (A * n) + A --> A * (n + 1). */
2036 41153 : if (auto repeated_bv = back->dyn_cast_repeated_bit_vector ())
2037 793 : if (&repeated_bv->get_inner_bit_vector () == &bv_to_add)
2038 : {
2039 17 : auto &unitless_mgr
2040 17 : = bv_mgr.get_node_manager ().get_scalars ().get_unitless ();
2041 34 : back = &bv_mgr.get_repeated_bits
2042 17 : (repeated_bv->get_inner_bit_vector (),
2043 17 : (repeated_bv->get_repeat_count ()
2044 17 : + unitless_mgr.get_concrete (1)));
2045 17 : return;
2046 : }
2047 :
2048 : /* A + (A * n) --> A * (n + 1). */
2049 41136 : if (auto repeated_bv = bv_to_add.dyn_cast_repeated_bit_vector ())
2050 708 : if (back == &repeated_bv->get_inner_bit_vector ())
2051 : {
2052 4 : auto &unitless_mgr
2053 4 : = bv_mgr.get_node_manager ().get_scalars ().get_unitless ();
2054 8 : back = &bv_mgr.get_repeated_bits
2055 4 : (repeated_bv->get_inner_bit_vector (),
2056 4 : (repeated_bv->get_repeat_count ()
2057 4 : + unitless_mgr.get_concrete (1)));
2058 4 : return;
2059 : }
2060 :
2061 : /* (A * m) + (A * n) --> A * (m + n). */
2062 41132 : if (auto repeated_lhs = back->dyn_cast_repeated_bit_vector ())
2063 776 : if (auto repeated_rhs = bv_to_add.dyn_cast_repeated_bit_vector ())
2064 166 : if (&repeated_lhs->get_inner_bit_vector ()
2065 166 : == &repeated_rhs->get_inner_bit_vector ())
2066 : {
2067 4 : back = &bv_mgr.get_repeated_bits
2068 4 : (repeated_lhs->get_inner_bit_vector (),
2069 4 : (repeated_lhs->get_repeat_count ()
2070 4 : + repeated_rhs->get_repeat_count ()));
2071 4 : return;
2072 : }
2073 :
2074 : /* concat(A[p, q], A[q, r]) --> A[p, r]. */
2075 41128 : if (auto cropped_lhs = back->dyn_cast_cropped_bit_vector ())
2076 359 : if (auto cropped_rhs = bv_to_add.dyn_cast_cropped_bit_vector ())
2077 52 : if ((&cropped_lhs->get_uncropped_bit_vector ()
2078 52 : == &cropped_rhs->get_uncropped_bit_vector ())
2079 52 : && (&cropped_lhs->get_uncropped_bits_to_use ().get_next_offset ()
2080 17 : == &cropped_rhs->get_uncropped_bits_to_use ().get_start_offset ()))
2081 : {
2082 9 : symbolic_bit_range concat_bit_range
2083 9 : (cropped_lhs->get_uncropped_bits_to_use ().get_start_offset (),
2084 9 : cropped_lhs->get_size () + cropped_rhs->get_size ());
2085 9 : back = &bv_mgr.get_cropped_bits (cropped_lhs->get_uncropped_bit_vector (),
2086 : concat_bit_range);
2087 9 : return;
2088 : }
2089 :
2090 : /* poisoned(m, X) + poisoned(n, X) --> poisoned(m + n, X)
2091 : for same kind of poison. */
2092 41119 : if (auto poisoned_lhs = back->dyn_cast_poisoned_bit_vector ())
2093 421 : if (auto poisoned_rhs = bv_to_add.dyn_cast_poisoned_bit_vector ())
2094 47 : if (poisoned_lhs->get_poison_kind () == poisoned_rhs->get_poison_kind ())
2095 : {
2096 86 : back = &bv_mgr.get_poisoned_bits
2097 43 : ((poisoned_lhs->get_size ()
2098 43 : + poisoned_rhs->get_size ()),
2099 : poisoned_lhs->get_poison_kind ());
2100 43 : return;
2101 : }
2102 :
2103 : /* tristate(m, X) + tristate(n, X) --> tristate(m + n, X). */
2104 41076 : if (auto tristate_lhs = back->dyn_cast_tristate_bit_vector ())
2105 4334 : if (auto tristate_rhs = bv_to_add.dyn_cast_tristate_bit_vector ())
2106 1692 : if (tristate_lhs->get_bit_value () == tristate_rhs->get_bit_value ())
2107 : {
2108 1339 : back = &bv_mgr.get_tristate_bits
2109 1339 : ((tristate_lhs->get_size ()
2110 1339 : + tristate_rhs->get_size ()),
2111 : tristate_lhs->get_bit_value ());
2112 1339 : return;
2113 : }
2114 :
2115 : /* bits_from_ubc (BC, R1) + bits_from_ubc (BC, R2)
2116 : --> bits_from_ubc (BC, R1 + R2)
2117 : if it's the same BC and the ranges touch. */
2118 39737 : if (auto from_ubc_lhs = back->dyn_cast_bit_vector_from_ubc ())
2119 459 : if (auto from_ubc_rhs = bv_to_add.dyn_cast_bit_vector_from_ubc ())
2120 27 : if (&from_ubc_lhs->get_bit_contents ()
2121 27 : == &from_ubc_rhs->get_bit_contents ())
2122 15 : if (&from_ubc_lhs->get_bit_range ().get_next_offset ()
2123 15 : == &from_ubc_rhs->get_bit_range ().get_start_offset ())
2124 : {
2125 11 : symbolic_bit_range combined_bit_range
2126 11 : (from_ubc_lhs->get_bit_range ().get_start_offset (),
2127 11 : (from_ubc_lhs->get_bit_range ().get_size ()
2128 11 : + from_ubc_rhs->get_bit_range ().get_size ()));
2129 11 : auto &ubc = from_ubc_lhs->get_bit_contents ();
2130 11 : back = &ubc.get_bits (combined_bit_range);
2131 11 : return;
2132 : }
2133 : }
2134 :
2135 2310123 : canonical_bvs.push_back (&bv_to_add);
2136 : }
2137 :
2138 : /* Simplify BIT_VECTORS into a canonical form. */
2139 :
2140 : std::vector<const bit_vector *>
2141 2270401 : bit_vector_manager::
2142 : canonicalize_concat_bits (const std::vector<const bit_vector *> &bvs)
2143 : {
2144 2270401 : std::vector<const bit_vector *> result;
2145 4582727 : for (auto iter : bvs)
2146 2312326 : append_canonically (result, *iter, *this);
2147 2270401 : return result;
2148 : }
2149 :
2150 : const bit_vector *
2151 2270397 : bit_vector_manager::
2152 : maybe_fold_concat_bits (const std::vector<const bit_vector *> &canonical_bvs)
2153 : {
2154 : /* Unwrap single element: concat(X) --> X. */
2155 2270397 : if (canonical_bvs.size () == 1)
2156 2233443 : return canonical_bvs[0];
2157 :
2158 : return nullptr;
2159 : }
2160 :
2161 : const bit_vector &
2162 2270404 : bit_vector_manager::
2163 : get_concat_bits (std::vector<const bit_vector *> bvs)
2164 : {
2165 2270404 : if (bvs.size () == 0)
2166 3 : return m_empty_bv;
2167 :
2168 : /* Simplify into a canonical form. */
2169 2270401 : auto canonical_bvs = canonicalize_concat_bits (bvs);
2170 2270401 : if (canonical_bvs.size () == 0)
2171 4 : return m_empty_bv;
2172 :
2173 2270397 : if (auto *folded = maybe_fold_concat_bits (canonical_bvs))
2174 : return *folded;
2175 :
2176 36954 : const symbolic::concat_bit_vector::key k (std::move (canonical_bvs));
2177 36954 : auto iter = m_concat_map.find (k);
2178 36954 : if (iter != m_concat_map.end ())
2179 32808 : return *(iter->second);
2180 :
2181 4146 : auto bv_up = std::make_unique<symbolic::concat_bit_vector> (*this, k);
2182 4146 : auto &result = *bv_up.get ();
2183 4146 : m_concat_map.emplace (k, std::move (bv_up));
2184 4146 : return result;
2185 2311501 : }
2186 :
2187 : const bit_vector &
2188 26326 : bit_vector_manager::get_conjured_bits (const gimple *stmt,
2189 : const region *id_reg,
2190 : const conjured_purge &p,
2191 : unsigned idx)
2192 : {
2193 26326 : const symbolic::conjured_bit_vector::key k (stmt, id_reg, idx);
2194 26326 : auto iter = m_conjured_map.find (k);
2195 26326 : if (iter != m_conjured_map.end ())
2196 : {
2197 11664 : auto &bv = iter->second;
2198 : /* We're reusing an existing conjured_bit_vector, perhaps from a different
2199 : state within this analysis, or perhaps from an earlier state on this
2200 : execution path. For the latter, purge any state involving the "new"
2201 : bit_vector from the current program_state. */
2202 11664 : p.purge (*bv);
2203 11664 : return *bv;
2204 : }
2205 14662 : auto bv_up = std::make_unique<symbolic::conjured_bit_vector> (*this, k);
2206 14662 : auto &result = *bv_up.get ();
2207 14662 : m_conjured_map.emplace (k, std::move (bv_up));
2208 14662 : return result;
2209 14662 : }
2210 :
2211 : const bit_vector *
2212 1363519 : bit_vector_manager::maybe_fold_bits_for_constant (tree tree_cst)
2213 : {
2214 1363519 : gcc_assert (tree_cst);
2215 :
2216 1363519 : switch (TREE_CODE (tree_cst))
2217 : {
2218 : default:
2219 : break;
2220 :
2221 1362239 : case INTEGER_CST:
2222 1362239 : {
2223 1362239 : bit_size_t size;
2224 1362239 : bool got_size = int_size_in_bits (TREE_TYPE (tree_cst), &size);
2225 1362239 : gcc_assert (got_size);
2226 :
2227 1362239 : integer_with_unit<units::bit> num_bits = convert_from_bit_size_t (size);
2228 :
2229 : /* Fold all-zeroes and all-ones. */
2230 1362239 : if (zerop (tree_cst))
2231 116346 : return &get_all_zeros (num_bits);
2232 1245893 : if (integer_all_onesp (tree_cst))
2233 10630 : return &get_all_ones (num_bits);
2234 :
2235 : /* Fold bytes. */
2236 1235263 : if (num_bits == 8_bits)
2237 : {
2238 1142118 : uint8_t val = TREE_INT_CST_LOW (tree_cst);
2239 1142118 : if (auto *b = m_cached_bytes[val])
2240 : return b;
2241 : }
2242 131682 : }
2243 1230557 : break;
2244 :
2245 42 : case RAW_DATA_CST:
2246 42 : if (RAW_DATA_LENGTH (tree_cst) == 0)
2247 4 : return &m_empty_bv;
2248 : break;
2249 : }
2250 : return nullptr;
2251 : }
2252 :
2253 : const bit_vector &
2254 120 : bit_vector_manager::get_bits_for_constant (tree tree_cst)
2255 : {
2256 120 : if (auto *folded = maybe_fold_bits_for_constant (tree_cst))
2257 : return *folded;
2258 :
2259 48 : const svalue *sval
2260 48 : = get_svalue_manager ().get_or_create_constant_svalue (tree_cst);
2261 48 : return get_bit_vector_from_svalue (*sval);
2262 : }
2263 :
2264 : const bit_vector *
2265 14010 : bit_vector_manager::
2266 : maybe_fold_bytes_within_constant (tree tree_cst,
2267 : concrete_byte_range conc_bytes)
2268 : {
2269 14010 : gcc_assert (tree_cst);
2270 14010 : gcc_assert (CONSTANT_CLASS_P (tree_cst));
2271 28020 : gcc_assert (conc_bytes.get_start_offset () >= 0_bytes);
2272 :
2273 14010 : switch (TREE_CODE (tree_cst))
2274 : {
2275 : default:
2276 : return nullptr;
2277 :
2278 : /* Special-case these. */
2279 1853 : case STRING_CST:
2280 3706 : return maybe_fold_bytes_within_string_cst (tree_cst, conc_bytes);
2281 194 : case RAW_DATA_CST:
2282 388 : return maybe_fold_bytes_within_raw_data_cst (tree_cst, conc_bytes);
2283 :
2284 : /* Handle the constants that native_encode_expr can. */
2285 11963 : case INTEGER_CST:
2286 11963 : case REAL_CST:
2287 11963 : case FIXED_CST:
2288 11963 : case COMPLEX_CST:
2289 11963 : case VECTOR_CST:
2290 11963 : case CONSTRUCTOR:
2291 11963 : {
2292 : /* Use native_encode_expr and extract the pertinent byte(s). */
2293 11963 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
2294 11963 : int size = native_encode_expr (tree_cst, buf, sizeof buf, 0);
2295 11963 : if (size == 0)
2296 : return nullptr;
2297 :
2298 23926 : gcc_assert (conc_bytes.get_last_offset () < concrete_byte_size (size));
2299 :
2300 11963 : std::vector<const bit_vector *> vec_of_bytes;
2301 11963 : const concrete_byte_offset next_offset = conc_bytes.get_next_offset ();
2302 11963 : for (concrete_byte_offset conc_offset = conc_bytes.get_start_offset ();
2303 24130 : conc_offset < next_offset;
2304 12167 : conc_offset += 1_byte)
2305 12167 : vec_of_bytes.push_back
2306 12167 : (&get_uint8 (buf[conc_offset.as_unsigned_long ()]));
2307 :
2308 11963 : return &get_concat_bits (std::move (vec_of_bytes));
2309 11963 : }
2310 : }
2311 : }
2312 :
2313 : const bit_vector *
2314 1853 : bit_vector_manager::
2315 : maybe_fold_bytes_within_string_cst (tree string_cst,
2316 : concrete_byte_range conc_bytes)
2317 : {
2318 1853 : gcc_assert (TREE_CODE (string_cst) == STRING_CST);
2319 :
2320 : /* Adapted from fold_read_from_constant_string. */
2321 1853 : scalar_int_mode char_mode;
2322 1853 : if (!is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string_cst))),
2323 : &char_mode))
2324 : return nullptr;
2325 3706 : if (GET_MODE_SIZE (char_mode) != 1)
2326 : return nullptr;
2327 :
2328 3706 : gcc_assert (conc_bytes.get_start_offset () >= 0_bytes);
2329 3706 : if (conc_bytes.get_start_offset ()
2330 3706 : < integer_with_unit<units::byte> (TREE_STRING_LENGTH (string_cst)))
2331 : {
2332 : /* We're within the area defined by TREE_STRING_POINTER. */
2333 1846 : if (conc_bytes.get_size () == 1_byte)
2334 1496 : return &get_uint8
2335 2992 : (TREE_STRING_POINTER (string_cst)
2336 3487 : [conc_bytes.get_start_offset ().get_integer ().as_unsigned_long ()]);
2337 : else
2338 : return nullptr;
2339 : }
2340 : else
2341 : {
2342 : /* We're in the padding area of trailing zeroes. */
2343 7 : return &get_all_zeros (conc_bytes.get_size ());
2344 : }
2345 : }
2346 :
2347 : const bit_vector *
2348 194 : bit_vector_manager::
2349 : maybe_fold_bytes_within_raw_data_cst (tree raw_data_cst,
2350 : concrete_byte_range conc_bytes)
2351 : {
2352 194 : gcc_assert (TREE_CODE (raw_data_cst) == RAW_DATA_CST);
2353 :
2354 194 : if (conc_bytes.get_size () != 1_byte)
2355 : return nullptr;
2356 :
2357 388 : gcc_assert (conc_bytes.get_start_offset () >= 0_bytes);
2358 388 : gcc_assert
2359 : (conc_bytes.get_start_offset ()
2360 : < integer_with_unit<units::byte> (RAW_DATA_LENGTH (raw_data_cst)));
2361 :
2362 194 : size_t idx
2363 230 : = conc_bytes.get_start_offset ().get_integer ().as_unsigned_long ();
2364 194 : return &get_uint8 (RAW_DATA_UCHAR_ELT (raw_data_cst, idx));
2365 : }
2366 :
2367 : const bit_vector &
2368 22318 : bit_vector_manager::
2369 : make_baseline_bit_vector (baseline_key k)
2370 : {
2371 22318 : gcc_assert (k.m_cluster_reg->can_have_initial_svalue_p ());
2372 :
2373 22318 : auto &rmm = m_node_mgr.get_svalue_manager ();
2374 22318 : const svalue *sval = nullptr;
2375 22318 : switch (k.m_baseline_state)
2376 : {
2377 0 : default:
2378 0 : gcc_unreachable ();
2379 :
2380 819 : case baseline_state::main:
2381 819 : sval = k.m_cluster_reg->get_initial_value_at_main (&rmm);
2382 819 : break;
2383 :
2384 21499 : case baseline_state::after_main:
2385 21499 : sval = rmm.get_or_create_initial_value (k.m_cluster_reg);
2386 21499 : break;
2387 : }
2388 :
2389 22318 : gcc_assert (sval);
2390 22318 : return get_bit_vector_from_svalue (*sval);
2391 : }
2392 :
2393 : const bit_vector &
2394 22318 : bit_vector_manager::
2395 : get_baseline_bit_vector (const cluster_region &cluster_reg,
2396 : baseline_state bs)
2397 : {
2398 22318 : baseline_key k {&cluster_reg, bs};
2399 22318 : auto iter = m_baseline_map.find (k);
2400 22318 : if (iter != m_baseline_map.end ())
2401 0 : return iter->second;
2402 :
2403 22318 : auto &bc = make_baseline_bit_vector (k);
2404 22318 : m_baseline_map.insert ({k, bc});
2405 22318 : return bc;
2406 : }
2407 :
2408 : const bit_vector *
2409 2688594 : bit_vector_manager::
2410 : maybe_fold_cropped_bits (const bit_vector &inner_bv,
2411 : const symbolic_bit_range &inner_bits_to_use)
2412 : {
2413 2688594 : if (inner_bits_to_use.get_size ().is_zero_p ())
2414 4 : return &m_empty_bv;
2415 :
2416 : // x[0..size(x)] --> x. */
2417 2688590 : if (inner_bits_to_use.get_start_offset ().is_zero_p ()
2418 2688590 : && &inner_bits_to_use.get_size () == &inner_bv.get_size ())
2419 : return &inner_bv;
2420 :
2421 500919 : if (auto folded = inner_bv.maybe_fold_crop (inner_bits_to_use))
2422 : return folded;
2423 :
2424 : return nullptr;
2425 : }
2426 :
2427 : const bit_vector &
2428 2688594 : bit_vector_manager::
2429 : get_cropped_bits (const bit_vector &inner_bv,
2430 : const symbolic_bit_range &inner_bits_to_use)
2431 : {
2432 2688594 : if (auto *folded = maybe_fold_cropped_bits (inner_bv, inner_bits_to_use))
2433 : return *folded;
2434 :
2435 90577 : const symbolic::cropped_bit_vector::key k {&inner_bv,
2436 90577 : inner_bits_to_use};
2437 90577 : auto iter = m_cropped_map.find (k);
2438 90577 : if (iter != m_cropped_map.end ())
2439 82828 : return *(iter->second);
2440 7749 : auto bv_up = std::make_unique<cropped_bit_vector> (*this, k);
2441 7749 : auto &result = *bv_up.get ();
2442 7749 : m_cropped_map.emplace (k, std::move (bv_up));
2443 7749 : return result;
2444 7749 : }
2445 :
2446 : const bit_vector &
2447 478505 : bit_vector_manager::
2448 : get_cropped_bytes (const bit_vector &inner_bv,
2449 : const symbolic_byte_range &inner_bytes_to_use)
2450 : {
2451 478505 : return get_cropped_bits (inner_bv,
2452 478505 : bits_from_bytes (inner_bytes_to_use));
2453 : }
2454 :
2455 : const bit_vector *
2456 3437 : bit_vector_manager::
2457 : maybe_fold_repeated_bits (const bit_vector &inner_bv,
2458 : const symbolic_integer &repeat_count)
2459 : {
2460 3437 : if (repeat_count.is_zero_p ())
2461 4 : return &m_empty_bv;
2462 :
2463 3433 : if (repeat_count.is_one_p ())
2464 : return &inner_bv;
2465 :
2466 1658 : if (auto poisoned_bv = inner_bv.dyn_cast_poisoned_bit_vector ())
2467 42 : return &get_poisoned_bits
2468 42 : (inner_bv.get_size () * repeat_count,
2469 42 : poisoned_bv->get_poison_kind ());
2470 :
2471 1616 : if (auto tristate_bv = inner_bv.dyn_cast_tristate_bit_vector ())
2472 965 : return &get_tristate_bits
2473 965 : (inner_bv.get_size () * repeat_count,
2474 965 : tristate_bv->get_bit_value ());
2475 :
2476 651 : if (auto repeated_bv = inner_bv.dyn_cast_repeated_bit_vector ())
2477 11 : return &get_repeated_bits (repeated_bv->get_inner_bit_vector (),
2478 11 : (repeated_bv->get_repeat_count ()
2479 11 : * repeat_count));
2480 :
2481 : return nullptr;
2482 : }
2483 :
2484 : const bit_vector &
2485 3437 : bit_vector_manager::
2486 : get_repeated_bits (const bit_vector &inner_bv,
2487 : const symbolic_integer &repeat_count)
2488 : {
2489 3437 : if (auto *folded = maybe_fold_repeated_bits (inner_bv, repeat_count))
2490 : return *folded;
2491 :
2492 640 : const symbolic::repeated_bit_vector::key k {&inner_bv, &repeat_count};
2493 640 : auto iter = m_repeated_map.find (k);
2494 640 : if (iter != m_repeated_map.end ())
2495 346 : return *(iter->second);
2496 294 : auto bv_up = std::make_unique<repeated_bit_vector> (*this, k);
2497 294 : auto &result = *bv_up.get ();
2498 294 : m_repeated_map.emplace (k, std::move (bv_up));
2499 294 : return result;
2500 294 : }
2501 :
2502 : const bit_vector &
2503 2710 : bit_vector_manager::
2504 : get_repeated_bits (const bit_vector &inner_bv,
2505 : const concrete_integer &repeat_count)
2506 : {
2507 2710 : auto &integer_mgr = get_node_manager ().get_scalars ().get_unitless ();
2508 2710 : return get_repeated_bits (inner_bv,
2509 2710 : integer_mgr.get_concrete (repeat_count));
2510 : }
2511 :
2512 : /* Return true if TYPE_A and TYPE_B are known to be the same size. */
2513 :
2514 : static bool
2515 14975 : types_are_same_size_p (const_tree type_a, const_tree type_b)
2516 : {
2517 14975 : concrete_bit_size size_a;
2518 14975 : if (!get_type_size (type_a, &size_a))
2519 : return false;
2520 14975 : concrete_bit_size size_b;
2521 14975 : if (!get_type_size (type_b, &size_b))
2522 : return false;
2523 14975 : return size_a == size_b;
2524 14975 : }
2525 :
2526 : const bit_vector *
2527 1844839 : bit_vector_manager::maybe_fold_bits_for_svalue (const svalue &sval)
2528 : {
2529 1844839 : switch (sval.get_kind ())
2530 : {
2531 : default:
2532 : break;
2533 :
2534 1363399 : case SK_CONSTANT:
2535 1363399 : {
2536 1363399 : tree tree_cst = sval.maybe_get_constant ();
2537 1363399 : if (auto *folded = maybe_fold_bits_for_constant (tree_cst))
2538 131636 : return folded;
2539 :
2540 : /* Fold any 1-byte constants we missed. */
2541 1231785 : auto &bit_mgr = get_node_manager ().get_scalars ().get_bits ();
2542 1231785 : concrete_bit_size conc_bit_size;
2543 1231785 : if (sval.has_concrete_size_p (&conc_bit_size, bit_mgr)
2544 3695355 : && conc_bit_size == 8_bits)
2545 : {
2546 1137434 : unsigned char buf[1];
2547 1137434 : int size = native_encode_expr (tree_cst, buf, sizeof buf, 0);
2548 1137434 : gcc_assert (size == 1);
2549 1137434 : unsigned char val = buf[0];
2550 1137434 : if (auto *b = m_cached_bytes[val])
2551 22 : return b;
2552 : }
2553 22 : }
2554 1231763 : break;
2555 :
2556 : /* TODO: re-enabling this will require a rewrite of parts of
2557 : access-diagram.cc which expect string regions, rather than
2558 : constant_svalue of string_cst. */
2559 : #if 0
2560 : case SK_INITIAL:
2561 : {
2562 : auto &init_sval = *as_a <const initial_svalue *> (&sval);
2563 : if (auto *string_reg
2564 : = init_sval.get_region ()->dyn_cast_string_region ())
2565 : return &get_bits_for_constant (string_reg->get_string_cst ());
2566 : }
2567 : break;
2568 : #endif
2569 :
2570 72400 : case SK_BIT_VECTOR:
2571 72400 : {
2572 72400 : auto &bit_vector_sval = *as_a<const svalue_from_bit_vector *> (&sval);
2573 72400 : return &bit_vector_sval.get_bit_vector ();
2574 : }
2575 122448 : break;
2576 :
2577 122448 : case SK_UNKNOWN:
2578 122448 : {
2579 122448 : concrete_bit_size size;
2580 122448 : if (sval.has_concrete_size_p (&size, get_bit_manager ()))
2581 122448 : return &get_unknown_bits (size);
2582 : else
2583 0 : return &get_unknown_bits ();
2584 122448 : }
2585 21605 : break;
2586 :
2587 21605 : case SK_UNARYOP:
2588 21605 : {
2589 21605 : auto &unaryop_sval = *as_a<const unaryop_svalue *> (&sval);
2590 21605 : if (CONVERT_EXPR_CODE_P (unaryop_sval.get_op ()))
2591 : {
2592 : /* If we have a type conversion for which no instructions
2593 : are needed, they are presumably bit-for-bit identical,
2594 : or for which the sizes are identical.
2595 : Fold away the conversion, so e.g.
2596 : e.g. BIT_VECTOR_FOR (UNARY_OP(signed, NOP_EXPR, SVAL(unsigned)))
2597 : folds to BIT_VECTOR_FOR (SVAL(unsigned)). */
2598 20602 : if (sval.get_type () == nullptr
2599 20602 : || unaryop_sval.get_arg ()->get_type () == nullptr
2600 20594 : || tree_nop_conversion_p (sval.get_type (),
2601 20594 : unaryop_sval.get_arg ()->get_type ())
2602 35577 : || types_are_same_size_p (sval.get_type (),
2603 14975 : unaryop_sval.get_arg ()->get_type ()))
2604 5640 : return &get_bit_vector_from_svalue (*unaryop_sval.get_arg ());
2605 : }
2606 : }
2607 : break;
2608 : }
2609 :
2610 : return nullptr;
2611 : }
2612 :
2613 : const bit_vector &
2614 1823295 : bit_vector_manager::get_bit_vector_from_svalue (const svalue &sval)
2615 : {
2616 1823295 : if (auto *folded = maybe_fold_bits_for_svalue (sval))
2617 : return *folded;
2618 :
2619 : /* We need to be able to determine the size of SVAL. */
2620 1498072 : concrete_bit_size conc_bit_size;
2621 1498072 : gcc_assert (sval.has_concrete_size_p (&conc_bit_size,
2622 : get_bit_manager ()));
2623 :
2624 1498072 : if (conc_bit_size == 0_bits)
2625 0 : return get_empty ();
2626 :
2627 1498072 : const symbolic::bit_vector_from_svalue::key k {&sval};
2628 1498072 : auto iter = m_svalue_to_bv_map.find (k);
2629 1498072 : if (iter != m_svalue_to_bv_map.end ())
2630 295886 : return *(iter->second);
2631 1202186 : auto bv_up = std::make_unique<symbolic::bit_vector_from_svalue> (*this, k);
2632 1202186 : auto &result = *bv_up.get ();
2633 1202186 : m_svalue_to_bv_map.emplace (k, std::move (bv_up));
2634 1202186 : return result;
2635 2700258 : }
2636 :
2637 : const bit_vector *
2638 1004 : bit_vector_manager::maybe_get_bit_vector_from_svalue (const svalue &sval)
2639 : {
2640 1004 : if (auto *folded = maybe_fold_bits_for_svalue (sval))
2641 : return folded;
2642 :
2643 1001 : const symbolic::bit_vector_from_svalue::key k {&sval};
2644 1001 : auto iter = m_svalue_to_bv_map.find (k);
2645 1001 : if (iter != m_svalue_to_bv_map.end ())
2646 1001 : return iter->second.get ();
2647 : return nullptr;
2648 : }
2649 :
2650 : const bit_vector &
2651 1673835 : bit_vector_manager::get_poisoned_bits (const symbolic_bit_size &num_bits,
2652 : enum poison_kind kind)
2653 : {
2654 1673835 : if (num_bits.is_zero_p ())
2655 23 : return m_empty_bv;
2656 :
2657 1673812 : poisoned_bit_vector::key k {num_bits, kind};
2658 :
2659 1673812 : auto iter = m_poisoned_map.find (k);
2660 1673812 : if (iter != m_poisoned_map.end ())
2661 1661201 : return *(iter->second);
2662 :
2663 12611 : auto bv_up = std::make_unique<poisoned_bit_vector> (*this, k);
2664 12611 : auto &result = *bv_up.get ();
2665 12611 : m_poisoned_map.emplace (k, std::move (bv_up));
2666 12611 : return result;
2667 12611 : }
2668 :
2669 : const bit_vector &
2670 56 : bit_vector_manager::get_poisoned_bits (const concrete_bit_size &num_bits,
2671 : enum poison_kind kind)
2672 : {
2673 56 : return get_poisoned_bits (symbolic_from_concrete (num_bits,
2674 : get_bit_manager ()),
2675 56 : kind);
2676 : }
2677 :
2678 : #if CHECKING_P
2679 :
2680 : const bit_vector &
2681 128 : bit_vector_manager::
2682 : get_named_test_bit_vector (const symbolic_bit_size &size_in_bits,
2683 : std::string label)
2684 : {
2685 128 : auto bv_up = std::make_unique<named_test_bit_vector> (*this,
2686 : size_in_bits,
2687 128 : std::move (label));
2688 128 : auto &result = *bv_up.get ();
2689 128 : m_named_test_bvs.push_back (std::move (bv_up));
2690 256 : return result;
2691 128 : }
2692 :
2693 : const bit_vector &
2694 12 : bit_vector_manager::
2695 : get_named_test_bit_vector (const symbolic_byte_size &size_in_bytes,
2696 : std::string label)
2697 : {
2698 12 : auto &size_in_bits = bits_from_bytes (size_in_bytes);
2699 12 : return get_named_test_bit_vector (size_in_bits, std::move (label));
2700 : }
2701 :
2702 : const bit_vector &
2703 116 : bit_vector_manager::
2704 : get_named_test_bit_vector (const concrete_bit_size &conc_size_in_bits,
2705 : std::string label)
2706 : {
2707 116 : auto &bit_mgr = get_node_manager ().get_scalars ().get_bits ();
2708 116 : auto &sym_size_in_bits = symbolic_from_concrete (conc_size_in_bits, bit_mgr);
2709 116 : return get_named_test_bit_vector (sym_size_in_bits, std::move (label));
2710 : }
2711 :
2712 : const bit_vector &
2713 100 : bit_vector_manager::
2714 : get_named_test_bit_vector (const concrete_byte_size &conc_size_in_bytes,
2715 : std::string label)
2716 : {
2717 100 : concrete_bit_size conc_size_in_bits = bits_from_bytes (conc_size_in_bytes);
2718 100 : return get_named_test_bit_vector (conc_size_in_bits, std::move (label));
2719 100 : }
2720 :
2721 : #endif /* #if CHECKING_P */
2722 :
2723 : } // namespace ana::symbolic
2724 :
2725 : #if CHECKING_P
2726 :
2727 : namespace selftest {
2728 :
2729 : static void
2730 4 : test_maybe_set_irange_bitmask_from_concrete_tristate_vector ()
2731 : {
2732 : /* A combination of ones, zeros, and unknown, expressed in order
2733 : LSB through MSB. */
2734 4 : concrete_tristate_vector ts_vec
2735 : ({tristate (true),
2736 : tristate (false),
2737 4 : tristate::unknown (),
2738 : tristate (true),
2739 4 : tristate (false)});
2740 :
2741 4 : irange_bitmask irange_bmask;
2742 4 : bool ok = symbolic::maybe_set_irange_bitmask_from_concrete_tristate_vector
2743 4 : (ts_vec, &irange_bmask);
2744 4 : ASSERT_TRUE (ok);
2745 4 : irange_bmask.verify_mask ();
2746 :
2747 : /* We expect:
2748 : index: [..., 4, 3, 2, 1, 0]
2749 : VALUE: [..., 0, 1, 0, 0, 1]
2750 : MASK: [..., 0, 0, 1, 0, 0]
2751 : | ^ ^ ^ ^ ^
2752 : | | | | | bit 0: true
2753 : | | | | bit 1: false
2754 : | | | bit 2: unknown
2755 : | | bit 3: true
2756 : | bit 4: false. */
2757 4 : ASSERT_EQ (irange_bmask.value (), wi::uhwi (0b01001, 5));
2758 4 : }
2759 :
2760 : static void
2761 4 : test_tristate ()
2762 : {
2763 4 : symbolic::node_manager node_mgr;
2764 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
2765 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
2766 :
2767 4 : auto &false_bit = mgr.get_all_zeros (1_bit);
2768 4 : ASSERT_SAME_SYMBOLIC_NODE (false_bit.get_size (),
2769 : bit_mgr.get_concrete (1_bit));
2770 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2771 : (false_bit,
2772 : "tristate_bit_vector[size: 1 bit]: false bit\n");
2773 4 : false_bit.to_json ();
2774 4 : ASSERT_EQ (false_bit.get_tristate_at_offset (0_bits), tristate (false));
2775 :
2776 4 : auto &true_bit = mgr.get_tristate_bits (1_bit, tristate (true));
2777 4 : ASSERT_SAME_SYMBOLIC_NODE (true_bit.get_size (),
2778 : bit_mgr.get_concrete (1_bit));
2779 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2780 : (true_bit,
2781 : "tristate_bit_vector[size: 1 bit]: true bit\n");
2782 4 : true_bit.to_json ();
2783 4 : ASSERT_EQ (true_bit.get_tristate_at_offset (0_bits), tristate (true));
2784 :
2785 4 : auto &unknown_bit = mgr.get_unknown_bits (1_bit);
2786 4 : ASSERT_SAME_SYMBOLIC_NODE (unknown_bit.get_size (),
2787 : bit_mgr.get_concrete (1_bit));
2788 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2789 : (unknown_bit,
2790 : "tristate_bit_vector[size: 1 bit]: unknown bit\n");
2791 4 : unknown_bit.to_json ();
2792 4 : ASSERT_EQ (unknown_bit.get_tristate_at_offset (0_bits), tristate::unknown ());
2793 :
2794 4 : auto &three_zero_bits = mgr.get_all_zeros (3_bits);
2795 4 : ASSERT_SAME_SYMBOLIC_NODE (three_zero_bits.get_size (),
2796 : bit_mgr.get_concrete (3_bits));
2797 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2798 : (three_zero_bits,
2799 : "tristate_bit_vector[size: 3 bits]: false bits\n");
2800 :
2801 4 : auto &byte_00 = mgr.get_all_zeros (1_byte);
2802 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2803 : (byte_00,
2804 : "tristate_bit_vector[size: 1 byte]: false bits\n");
2805 :
2806 4 : auto &byte_ff = mgr.get_tristate_bits (8_bits, tristate (true));
2807 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2808 : (byte_ff,
2809 : "tristate_bit_vector[size: 1 byte]: true bits\n");
2810 :
2811 4 : auto &unknown_32_bits = mgr.get_unknown_bits (32_bits);
2812 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2813 : (unknown_32_bits,
2814 : "tristate_bit_vector[size: 4 bytes]: unknown bits\n");
2815 4 : ASSERT_EQ (unknown_32_bits.get_tristate_at_offset (31_bits),
2816 : tristate::unknown ());
2817 :
2818 4 : auto &x = bit_mgr.get_named_test_scalar ("X");
2819 4 : auto &sym_sized_zeros = mgr.get_all_zeros (x);
2820 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2821 : (sym_sized_zeros,
2822 : "tristate_bit_vector[size: X bits]: false bits\n");
2823 4 : ASSERT_SAME_SYMBOLIC_NODE (sym_sized_zeros.get_size (),
2824 : x);
2825 4 : sym_sized_zeros.to_json ();
2826 4 : ASSERT_EQ (sym_sized_zeros.get_tristate_at_offset (0_bits), tristate (false));
2827 4 : ASSERT_EQ (sym_sized_zeros.get_tristate_at_offset (x), tristate (false));
2828 :
2829 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_all_zeros (0_bits),
2830 : mgr.get_empty ());
2831 4 : mgr.get_empty ().to_json ();
2832 :
2833 4 : {
2834 4 : pretty_printer pp;
2835 4 : hexdump_options hexdump_opts;
2836 4 : hexdump_opts.m_show_headings = true;
2837 4 : hexdump_opts.m_show_binary = true;
2838 4 : unknown_32_bits.hexdump_to_pp (&pp, hexdump_opts);
2839 4 : ASSERT_STREQ
2840 : (pp_formatted_text (&pp),
2841 : (" +-----------------------------------+-----------+-----+\n"
2842 : " | Binary | Hex |ASCII|\n"
2843 : "+------------+-----------------------------------+-----------+-----+\n"
2844 : "|[bytes 0..4)|???????? ???????? ???????? ????????|?? ?? ?? ??|???? |\n"
2845 : "+------------+-----------------------------------+-----------+-----+\n"));
2846 4 : }
2847 4 : {
2848 4 : pretty_printer pp;
2849 4 : hexdump_options hexdump_opts;
2850 4 : hexdump_opts.m_show_headings = false;
2851 4 : hexdump_opts.m_show_binary = true;
2852 4 : mgr.get_all_zeros (32_bits).hexdump_to_pp (&pp, hexdump_opts);
2853 4 : ASSERT_STREQ
2854 : (pp_formatted_text (&pp),
2855 : ("+------------+-----------------------------------+-----------+----+\n"
2856 : "|[bytes 0..4)|00000000 00000000 00000000 00000000|00 00 00 00|....|\n"
2857 : "+------------+-----------------------------------+-----------+----+\n"));
2858 4 : }
2859 4 : {
2860 4 : pretty_printer pp;
2861 4 : hexdump_options hexdump_opts;
2862 4 : hexdump_opts.m_show_headings = false;
2863 4 : hexdump_opts.m_show_binary = true;
2864 4 : mgr.get_all_ones (32_bits).hexdump_to_pp (&pp, hexdump_opts);
2865 4 : ASSERT_STREQ
2866 : (pp_formatted_text (&pp),
2867 : ("+------------+-----------------------------------+-----------+----+\n"
2868 : "|[bytes 0..4)|11111111 11111111 11111111 11111111|ff ff ff ff|....|\n"
2869 : "+------------+-----------------------------------+-----------+----+\n"));
2870 4 : }
2871 4 : }
2872 :
2873 : static void
2874 4 : test_integer_constants ()
2875 : {
2876 4 : symbolic::node_manager node_mgr;
2877 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
2878 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
2879 :
2880 : // Verify that we fold all zeroes and all ones
2881 :
2882 4 : auto &char_0
2883 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 0));
2884 4 : ASSERT_SAME_SYMBOLIC_NODE (char_0.get_size (),
2885 : bit_mgr.get_concrete (8_bits));
2886 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2887 : (char_0,
2888 : "tristate_bit_vector[size: 1 byte]: false bits\n");
2889 4 : char_0.to_json ();
2890 :
2891 4 : auto &char_0xff
2892 4 : = mgr.get_bits_for_constant (build_int_cst (unsigned_char_type_node,
2893 : 0xff));
2894 4 : ASSERT_SAME_SYMBOLIC_NODE (char_0xff.get_size (),
2895 : bit_mgr.get_concrete (8_bits));
2896 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2897 : (char_0xff,
2898 : "tristate_bit_vector[size: 1 byte]: true bits\n");
2899 4 : char_0xff.to_json ();
2900 :
2901 : // Verify that we handle constants that we can't simplify
2902 :
2903 4 : auto &char_A
2904 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node,
2905 : 'A'));
2906 4 : ASSERT_SAME_SYMBOLIC_NODE (char_A.get_size (),
2907 : bit_mgr.get_concrete (8_bits));
2908 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2909 : (char_A,
2910 : ("bit_vector_from_svalue[size: 1 byte]\n"
2911 : "`- m_sval: 'unsigned char': constant_svalue ('65')\n"));
2912 :
2913 4 : auto &int_42
2914 4 : = mgr.get_bits_for_constant (build_int_cst (uint32_type_node, 42));
2915 4 : ASSERT_SAME_SYMBOLIC_NODE (int_42.get_size (),
2916 : bit_mgr.get_concrete (32_bits));
2917 4 : int_42.to_json ();
2918 :
2919 : // -1 is simplifiable to all ones...
2920 4 : auto &char_minus_one
2921 4 : = mgr.get_bits_for_constant (build_int_cst (signed_char_type_node, -1));
2922 4 : ASSERT_SAME_SYMBOLIC_NODE (char_minus_one.get_size (),
2923 : bit_mgr.get_concrete (8_bits));
2924 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2925 : (char_minus_one,
2926 : "tristate_bit_vector[size: 1 byte]: true bits\n");
2927 :
2928 : // ...but -2 isn't
2929 4 : auto &char_minus_two
2930 4 : = mgr.get_bits_for_constant (build_int_cst (signed_char_type_node,
2931 : -2));
2932 4 : ASSERT_SAME_SYMBOLIC_NODE (char_minus_two.get_size (),
2933 : bit_mgr.get_concrete (8_bits));
2934 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
2935 : (char_minus_two,
2936 : ("bit_vector_from_svalue[size: 1 byte]\n"
2937 : "`- m_sval: 'unsigned char': constant_svalue ('254')\n"));
2938 4 : ASSERT_SAME_SYMBOLIC_NODE
2939 : (char_minus_two,
2940 : mgr.get_bits_for_constant (build_int_cst (unsigned_char_type_node,
2941 : 254)));
2942 4 : }
2943 :
2944 : static void
2945 4 : test_bytes ()
2946 : {
2947 4 : symbolic::node_manager node_mgr;
2948 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
2949 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
2950 :
2951 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_uint8 (0),
2952 : mgr.get_all_zeros (1_byte));
2953 1020 : for (int i = 0x01; i < 0xff; ++i)
2954 : {
2955 1016 : auto &b = mgr.get_uint8 (i);
2956 1016 : ASSERT_SAME_SYMBOLIC_NODE (b.get_size (),
2957 : bit_mgr.get_concrete (8_bits));
2958 : }
2959 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_uint8 (0xff),
2960 : mgr.get_all_ones (1_byte));
2961 :
2962 1024 : for (int i = 0; i < 0xff; ++i)
2963 : {
2964 1020 : auto &bv = mgr.get_uint8 (i);
2965 1020 : uint8_t val;
2966 1020 : bool out = bv.maybe_as_uint8 (&val);
2967 1020 : ASSERT_TRUE (out);
2968 1020 : ASSERT_EQ (val, i);
2969 9180 : for (int bit_idx = 0; bit_idx < 8; ++bit_idx)
2970 12256 : ASSERT_EQ (bv.get_tristate_at_offset (concrete_bit_offset (bit_idx)),
2971 : tristate (i & (1 << bit_idx)));
2972 : }
2973 4 : }
2974 :
2975 : static void
2976 4 : test_string_constants ()
2977 : {
2978 4 : tree string_cst_ptr = make_string_constant ("abc");
2979 :
2980 4 : symbolic::node_manager node_mgr;
2981 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
2982 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
2983 :
2984 4 : auto &abc0 = bv_mgr.get_bits_for_constant (string_cst_ptr);
2985 4 : ASSERT_SAME_SYMBOLIC_NODE (abc0.get_size (),
2986 : bit_mgr.get_concrete (bits_from_bytes (4_bytes)));
2987 4 : abc0.to_json ();
2988 :
2989 4 : {
2990 4 : pretty_printer pp;
2991 4 : abc0.hexdump_to_pp (&pp, hexdump_options ());
2992 4 : ASSERT_STREQ
2993 : (pp_formatted_text (&pp),
2994 : ("+------------+-----------+----+\n"
2995 : "|[bytes 0..4)|61 62 63 00|abc.|\n"
2996 : "+------------+-----------+----+\n"));
2997 4 : }
2998 :
2999 : /* Verify accessing chars within a STRING_CST. */
3000 4 : {
3001 4 : auto &a = abc0.get_byte_at_offset (0_bytes);
3002 4 : ASSERT_SAME_SYMBOLIC_NODE (a, bv_mgr.get_uint8 ('a'));
3003 :
3004 4 : auto &b = abc0.get_byte_at_offset (1_byte);
3005 4 : ASSERT_SAME_SYMBOLIC_NODE (b, bv_mgr.get_uint8 ('b'));
3006 :
3007 4 : auto &c = abc0.get_byte_at_offset (2_bytes);
3008 4 : ASSERT_SAME_SYMBOLIC_NODE (c, bv_mgr.get_uint8 ('c'));
3009 :
3010 4 : auto &term = abc0.get_byte_at_offset (3_bytes);
3011 4 : ASSERT_SAME_SYMBOLIC_NODE (term, bv_mgr.get_uint8 (0));
3012 : }
3013 :
3014 24 : for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
3015 16 : ASSERT_EQ (abc0.scan_for_first_zero_byte (i),
3016 4 : string_scan::found_nul (3_bytes));
3017 :
3018 : /* Example of concatenating known string literals. */
3019 4 : {
3020 4 : const char *hello = "hello";
3021 4 : const char *world = " world!";
3022 4 : tree t_hello = make_string_constant (hello);
3023 4 : tree t_world = make_string_constant (world);
3024 4 : auto &bv_hello = bv_mgr.get_bits_for_constant (t_hello);
3025 4 : auto &bv_world = bv_mgr.get_bits_for_constant (t_world);
3026 4 : auto &concat_bv = bv_mgr.get_concat_bits
3027 12 : ({&bv_hello.get_prefix (concrete_byte_size (strlen (hello))),
3028 8 : &bv_world.get_prefix (concrete_byte_size (strlen (world))),
3029 4 : &bv_mgr.get_uint8 (0)});
3030 4 : pretty_printer pp;
3031 4 : hexdump_options hexdump_opts;
3032 4 : hexdump_opts.m_bytes_per_line = 8_bytes;
3033 4 : concat_bv.hexdump_to_pp (&pp, hexdump_opts);
3034 4 : ASSERT_STREQ
3035 : (pp_formatted_text (&pp),
3036 : "+-------------+-----------------------+--------+\n"
3037 : "| [bytes 0..8)|68 65 6c 6c 6f 20 77 6f|hello wo|\n"
3038 : "+-------------+-----------------------+--------+\n"
3039 : "|[bytes 8..13)|72 6c 64 21 00 |rld!. |\n"
3040 : "+-------------+-----------------------+--------+\n");
3041 60 : for (concrete_byte_offset i = 0_bytes; i < 13_bytes; ++i)
3042 52 : ASSERT_EQ (concat_bv.scan_for_first_zero_byte (i),
3043 4 : string_scan::found_nul (12_bytes));
3044 4 : }
3045 4 : }
3046 :
3047 : static tree
3048 12 : make_raw_data_constant (const char *buf, size_t size)
3049 : {
3050 12 : tree node = make_node (RAW_DATA_CST);
3051 12 : TREE_TYPE (node) = integer_type_node;
3052 12 : RAW_DATA_LENGTH (node) = size;
3053 12 : RAW_DATA_POINTER (node) = buf;
3054 12 : return node;
3055 : }
3056 :
3057 : static void
3058 4 : test_raw_data_constant_1 ()
3059 : {
3060 4 : tree raw_data_cst_ptr = make_raw_data_constant ("abc", 3);
3061 :
3062 4 : symbolic::node_manager node_mgr;
3063 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3064 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3065 :
3066 4 : auto &abc = mgr.get_bits_for_constant (raw_data_cst_ptr);
3067 4 : ASSERT_SAME_SYMBOLIC_NODE (abc.get_size (),
3068 : bit_mgr.get_concrete (bits_from_bytes (3_bytes)));
3069 4 : abc.to_json ();
3070 :
3071 4 : {
3072 4 : pretty_printer pp;
3073 4 : abc.hexdump_to_pp (&pp, hexdump_options ());
3074 4 : ASSERT_STREQ
3075 : (pp_formatted_text (&pp),
3076 : ("+------------+--------+---+\n"
3077 : "|[bytes 0..3)|61 62 63|abc|\n"
3078 : "+------------+--------+---+\n"));
3079 4 : }
3080 :
3081 : /* Verify accessing chars within a RAW_DATA_CST. */
3082 4 : {
3083 4 : auto &a = abc.get_byte_at_offset (0_bytes);
3084 4 : ASSERT_SAME_SYMBOLIC_NODE (a, mgr.get_uint8 ('a'));
3085 :
3086 4 : auto &b = abc.get_byte_at_offset (1_byte);
3087 4 : ASSERT_SAME_SYMBOLIC_NODE (b, mgr.get_uint8 ('b'));
3088 :
3089 4 : auto &c = abc.get_byte_at_offset (2_bytes);
3090 4 : ASSERT_SAME_SYMBOLIC_NODE (c, mgr.get_uint8 ('c'));
3091 : }
3092 4 : }
3093 :
3094 : /* Test of a raw data cst with multiple embedded zero bytes. */
3095 :
3096 : static void
3097 4 : test_raw_data_constant_2 ()
3098 : {
3099 4 : tree raw_data_cst_ptr = make_raw_data_constant ("a\0b\0c\0", 6);
3100 :
3101 4 : symbolic::node_manager node_mgr;
3102 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3103 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3104 :
3105 4 : auto &a0b0c0 = mgr.get_bits_for_constant (raw_data_cst_ptr);
3106 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_size (),
3107 : bit_mgr.get_concrete (bits_from_bytes (6_bytes)));
3108 4 : a0b0c0.to_json ();
3109 :
3110 4 : {
3111 4 : pretty_printer pp;
3112 4 : a0b0c0.hexdump_to_pp (&pp, hexdump_options ());
3113 4 : ASSERT_STREQ
3114 : (pp_formatted_text (&pp),
3115 : ("+------------+-----------+----+\n"
3116 : "|[bytes 0..4)|61 00 62 00|a.b.|\n"
3117 : "+------------+-----------+----+\n"
3118 : "|[bytes 4..6)|63 00 |c. |\n"
3119 : "+------------+-----------+----+\n"));
3120 4 : }
3121 :
3122 : /* Verify accessing chars within a RAW_DATA_CST. */
3123 4 : {
3124 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (0_bytes),
3125 : mgr.get_uint8 ('a'));
3126 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (1_byte),
3127 : mgr.get_uint8 (0));
3128 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (2_bytes),
3129 : mgr.get_uint8 ('b'));
3130 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (3_bytes),
3131 : mgr.get_uint8 (0));
3132 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (4_bytes),
3133 : mgr.get_uint8 ('c'));
3134 4 : ASSERT_SAME_SYMBOLIC_NODE (a0b0c0.get_byte_at_offset (5_bytes),
3135 : mgr.get_uint8 (0));
3136 : }
3137 :
3138 : /* String scans should locate the NUL bytes. */
3139 4 : {
3140 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (0_bytes),
3141 : string_scan::found_nul (1_byte));
3142 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (1_byte),
3143 : string_scan::found_nul (1_byte));
3144 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (2_bytes),
3145 : string_scan::found_nul (3_bytes));
3146 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (3_bytes),
3147 : string_scan::found_nul (3_bytes));
3148 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (4_bytes),
3149 : string_scan::found_nul (5_bytes));
3150 4 : ASSERT_EQ (a0b0c0.scan_for_first_zero_byte (5_bytes),
3151 : string_scan::found_nul (5_bytes));
3152 : }
3153 4 : }
3154 :
3155 : /* Test of an empty raw data cst. */
3156 :
3157 : static void
3158 4 : test_raw_data_constant_empty ()
3159 : {
3160 4 : tree raw_data_cst_ptr = make_raw_data_constant ("", 0);
3161 :
3162 4 : symbolic::node_manager node_mgr;
3163 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3164 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3165 :
3166 4 : auto &bv = mgr.get_bits_for_constant (raw_data_cst_ptr);
3167 4 : ASSERT_SAME_SYMBOLIC_NODE (bv.get_size (),
3168 : bit_mgr.get_concrete (bits_from_bytes (0_bytes)));
3169 4 : ASSERT_SAME_SYMBOLIC_NODE (bv,
3170 : node_mgr.get_bit_vector_manager ().get_empty ());
3171 4 : }
3172 :
3173 : static void
3174 4 : test_concat ()
3175 : {
3176 4 : symbolic::node_manager node_mgr;
3177 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3178 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3179 :
3180 4 : auto &char_a
3181 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'a'));
3182 4 : auto &char_b
3183 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'b'));
3184 4 : auto &char_x
3185 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'x'));
3186 4 : auto &char_y
3187 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'y'));
3188 :
3189 4 : auto &ab = mgr.get_concat_bits ({&char_a, &char_b});
3190 4 : ASSERT_SAME_SYMBOLIC_NODE (ab.get_size (),
3191 : bit_mgr.get_concrete (bits_from_bytes (2_bytes)));
3192 4 : ab.to_json ();
3193 :
3194 4 : auto &xy
3195 4 : = mgr.get_concat_bits ({&char_x, &char_y});
3196 4 : ASSERT_SAME_SYMBOLIC_NODE (xy.get_size (),
3197 : bit_mgr.get_concrete (bits_from_bytes (2_bytes)));
3198 16 : for (concrete_byte_offset i = 0_bytes; i < 2_bytes; ++i)
3199 8 : ASSERT_EQ (xy.scan_for_first_zero_byte (i),
3200 4 : string_scan::beyond_domain (2_bytes));
3201 :
3202 4 : auto &single_element
3203 4 : = mgr.get_concat_bits ({&char_a});
3204 4 : ASSERT_SAME_SYMBOLIC_NODE (single_element, char_a);
3205 :
3206 : /* Verity concat (empty, empty) --> empty. */
3207 4 : ASSERT_SAME_SYMBOLIC_NODE
3208 : (mgr.get_concat_bits ({&mgr.get_empty (), &mgr.get_empty ()}),
3209 : mgr.get_empty ());
3210 :
3211 : /* Verity concat (a, empty) --> a. */
3212 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_concat_bits ({&char_a, &mgr.get_empty ()}),
3213 : char_a);
3214 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_concat_bits ({&mgr.get_empty (), &char_a}),
3215 : char_a);
3216 :
3217 : /* Verify concat(a, a) --> a * 2. */
3218 4 : auto &aa
3219 4 : = mgr.get_concat_bits ({&char_a, &char_a});
3220 4 : ASSERT_SAME_SYMBOLIC_NODE (aa.get_size (),
3221 : bit_mgr.get_concrete (16_bits));
3222 4 : ASSERT_SAME_SYMBOLIC_NODE
3223 : (aa,
3224 : mgr.get_repeated_bits (char_a, 2));
3225 :
3226 : /* Verify concat(a, a, a) --> a * 3. */
3227 4 : auto &aaa
3228 : = mgr.get_concat_bits
3229 4 : ({&char_a, &char_a, &char_a});
3230 4 : ASSERT_SAME_SYMBOLIC_NODE (aaa.get_size (),
3231 : bit_mgr.get_concrete (24_bits));
3232 4 : ASSERT_SAME_SYMBOLIC_NODE
3233 : (aaa,
3234 : mgr.get_repeated_bits (char_a, 3));
3235 :
3236 : /* Verify concat(a * 2, a) --> a * 3. */
3237 4 : {
3238 4 : auto &bits = mgr.get_concat_bits ({&aa, &char_a});
3239 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, aaa);
3240 : }
3241 :
3242 : /* Verify concat(a, a * 2) --> a * 3. */
3243 4 : {
3244 4 : auto &bits = mgr.get_concat_bits ({&char_a, &aa});
3245 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, aaa);
3246 : }
3247 :
3248 : /* Verify concat(a * 2, a * 2) --> a * 4. */
3249 4 : {
3250 4 : auto &bits = mgr.get_concat_bits ({&aa, &aa});
3251 4 : ASSERT_SAME_SYMBOLIC_NODE
3252 : (bits,
3253 : mgr.get_repeated_bits (char_a, 4));
3254 : }
3255 :
3256 : /* Verify concat(a * 2, a, a * 2) --> a * 5. */
3257 4 : {
3258 4 : auto &bits = mgr.get_concat_bits ({&aa, &char_a, &aa});
3259 4 : ASSERT_SAME_SYMBOLIC_NODE
3260 : (bits,
3261 : mgr.get_repeated_bits (char_a, 5));
3262 : }
3263 :
3264 : /* Verify concat(a, a, b, b) --> concat(a * 2, b * 2). */
3265 4 : {
3266 4 : auto &bits
3267 : = mgr.get_concat_bits
3268 4 : ({&char_a, &char_a, &char_b, &char_b});
3269 4 : ASSERT_SAME_SYMBOLIC_NODE
3270 : (bits,
3271 : mgr.get_concat_bits
3272 : ({&mgr.get_repeated_bits (char_a, 2),
3273 : &mgr.get_repeated_bits (char_b, 2)}));
3274 : }
3275 :
3276 : /* Verify concat of poisoneds. */
3277 4 : {
3278 : /* Verify concat(poisoned(m), poisoned(n) --> poisoned(m + n)
3279 : for same kind of poison. */
3280 4 : {
3281 4 : auto &bits = mgr.get_concat_bits
3282 8 : ({&mgr.get_poisoned_bits (2_bits, poison_kind::uninit),
3283 8 : &mgr.get_poisoned_bits (3_bits, poison_kind::uninit)});
3284 4 : ASSERT_SAME_SYMBOLIC_NODE
3285 : (bits,
3286 : mgr.get_poisoned_bits (5_bits, poison_kind::uninit));
3287 : }
3288 :
3289 : // Verify that we don't fold the concat of non-equal kinds of poison
3290 4 : {
3291 4 : auto &bits = mgr.get_concat_bits
3292 8 : ({&mgr.get_poisoned_bits (2_bits, poison_kind::uninit),
3293 8 : &mgr.get_poisoned_bits (3_bits, poison_kind::freed)});
3294 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3295 : (bits,
3296 : ("concat_bit_vector[size: 5 bits]\n"
3297 : "+- poisoned_bit_vector[size: 2 bits]: uninit\n"
3298 : "`- poisoned_bit_vector[size: 3 bits]: freed\n"));
3299 : }
3300 : }
3301 :
3302 : /* Verify concat of tristates. */
3303 4 : {
3304 : /* Verify concat(tristate(m), tristate(n) --> tristate(m + n)
3305 : for same tristate. */
3306 4 : {
3307 4 : auto &bits
3308 8 : = mgr.get_concat_bits ({&mgr.get_all_zeros (2_bits),
3309 8 : &mgr.get_all_zeros (3_bits)});
3310 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, mgr.get_all_zeros (5_bits));
3311 : }
3312 :
3313 : // Verify that we don't fold the concat of non-equal tristates
3314 4 : {
3315 4 : auto &bits
3316 : = mgr.get_concat_bits
3317 4 : ({
3318 4 : &mgr.get_all_zeros (2_bits),
3319 8 : &mgr.get_all_ones (3_bits)
3320 : });
3321 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3322 : (bits,
3323 : ("concat_bit_vector[size: 5 bits]\n"
3324 : "+- tristate_bit_vector[size: 2 bits]: false bits\n"
3325 : "`- tristate_bit_vector[size: 3 bits]: true bits\n"));
3326 4 : ASSERT_EQ (bits.get_tristate_at_offset (0_bits), tristate (false));
3327 4 : ASSERT_EQ (bits.get_tristate_at_offset (1_bit), tristate (false));
3328 4 : ASSERT_EQ (bits.get_tristate_at_offset (2_bits), tristate (true));
3329 4 : ASSERT_EQ (bits.get_tristate_at_offset (3_bits), tristate (true));
3330 4 : ASSERT_EQ (bits.get_tristate_at_offset (4_bits), tristate (true));
3331 :
3332 4 : {
3333 4 : pretty_printer pp;
3334 4 : hexdump_options hexdump_opts;
3335 4 : hexdump_opts.m_show_binary = true;
3336 4 : hexdump_opts.m_show_hex = false;
3337 4 : hexdump_opts.m_show_ascii = false;
3338 4 : bits.hexdump_to_pp (&pp, hexdump_opts);
3339 4 : ASSERT_STREQ
3340 : (pp_formatted_text (&pp),
3341 : "+-----------+-----+\n"
3342 : "|[bits 0..5)|00111|\n"
3343 : "+-----------+-----+\n");
3344 4 : }
3345 : }
3346 : }
3347 :
3348 : /* Verify concat of concat --> concat. */
3349 : /*
3350 : e.g.
3351 : concat(concat(A, B), concat(X, Y)) --> concat(A, B, X, Y);
3352 : */
3353 4 : auto &abxy
3354 4 : = mgr.get_concat_bits ({&ab, &xy});
3355 4 : ASSERT_SAME_SYMBOLIC_NODE (abxy.get_size (),
3356 : bit_mgr.get_concrete (32_bits));
3357 4 : ASSERT_SAME_SYMBOLIC_NODE
3358 : (abxy,
3359 : mgr.get_concat_bits ({&char_a, &char_b, &char_x, &char_y}));
3360 4 : abxy.to_json ();
3361 24 : for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
3362 16 : ASSERT_EQ (abxy.scan_for_first_zero_byte (i),
3363 4 : string_scan::beyond_domain (4_bytes));
3364 :
3365 : /* Verify concat of concat.
3366 : This doesn't repeat right now; it instead unwraps everything. */
3367 4 : auto &abab
3368 4 : = mgr.get_concat_bits ({&ab, &ab});
3369 4 : ASSERT_SAME_SYMBOLIC_NODE (abab.get_size (),
3370 : bit_mgr.get_concrete (32_bits));
3371 4 : ASSERT_SAME_SYMBOLIC_NODE
3372 : (abab,
3373 : mgr.get_concat_bits ({&char_a, &char_b, &char_a, &char_b}));
3374 24 : for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
3375 16 : ASSERT_EQ (abab.scan_for_first_zero_byte (i),
3376 4 : string_scan::beyond_domain (4_bytes));
3377 4 : }
3378 :
3379 : static void
3380 4 : test_cropped ()
3381 : {
3382 4 : symbolic::node_manager node_mgr;
3383 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
3384 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3385 :
3386 4 : auto &a = bv_mgr.get_named_test_bit_vector (10_bytes, "A");
3387 4 : auto &b = bv_mgr.get_named_test_bit_vector (10_bytes, "B");
3388 :
3389 4 : a.to_json ();
3390 :
3391 : /* Verify the "can't fold" case, and dumping. */
3392 4 : {
3393 12 : auto &bits = a.get_cropped_bytes (concrete_byte_range (5_bytes, 2_bytes));
3394 4 : ASSERT_SAME_SYMBOLIC_NODE (bits.get_size (),
3395 : bit_mgr.get_concrete (16_bits));
3396 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3397 : (bits,
3398 : ("cropped_bit_vector[size: 2 bytes]: [bytes 5..7)\n"
3399 : "`- uncropped: named_test_bit_vector[size: 10 bytes]: 'A'\n"));
3400 4 : bits.to_json ();
3401 16 : for (concrete_byte_offset i = 0_bytes; i < 2_bytes; ++i)
3402 8 : ASSERT_EQ (bits.scan_for_first_zero_byte (i),
3403 4 : string_scan::cannot_scan (i));
3404 20 : for (concrete_byte_offset i = 2_bytes; i < 5_bytes; ++i)
3405 12 : ASSERT_EQ (bits.scan_for_first_zero_byte (i),
3406 4 : string_scan::beyond_domain (i));
3407 :
3408 : // Verify that consolidation works
3409 12 : ASSERT_SAME_SYMBOLIC_NODE
3410 : (a.get_cropped_bytes (concrete_byte_range (5_bytes, 2_bytes)),
3411 : bits);
3412 :
3413 : // Verify that we don't erroneously consolidate...
3414 : // ...different inner bit_vector:
3415 12 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3416 : (b.get_cropped_bytes (concrete_byte_range (2_bytes, 2_bytes)),
3417 : bits);
3418 : // ...different start offset:
3419 12 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3420 : (a.get_cropped_bytes (concrete_byte_range (6_bytes, 2_bytes)),
3421 : bits);
3422 : // ...different size:
3423 12 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3424 : (a.get_cropped_bytes (concrete_byte_range (5_bytes, 1_byte)),
3425 : bits);
3426 : }
3427 :
3428 : /* Verify that A[0..size(A)] --> A. */
3429 4 : {
3430 12 : auto &bits = a.get_cropped_bytes (concrete_byte_range (0_bytes, 10_bytes));
3431 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, a);
3432 : }
3433 :
3434 : /* Verify that A[(empty)] --> empty. */
3435 4 : {
3436 12 : auto &bits = a.get_cropped_bytes (concrete_byte_range (0_bytes, 0_bytes));
3437 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, bv_mgr.get_empty ());
3438 : }
3439 :
3440 : /* Verify that concat(A[p, q], A[q, r]) --> A[p, r]. */
3441 4 : {
3442 12 : auto &a35 = a.get_cropped_bytes (concrete_byte_range (3_bytes, 3_bytes));
3443 4 : ASSERT_SAME_SYMBOLIC_NODE
3444 : (a35.get_size (),
3445 : bit_mgr.get_concrete (bits_from_bytes (3_bytes)));
3446 12 : auto &a67 = a.get_cropped_bytes (concrete_byte_range (6_bytes, 2_bytes));
3447 4 : ASSERT_SAME_SYMBOLIC_NODE
3448 : (a67.get_size (),
3449 : bit_mgr.get_concrete (bits_from_bytes (2_bytes)));
3450 4 : auto &a35a67 = bv_mgr.get_concat_bits ({&a35, &a67});
3451 4 : ASSERT_SAME_SYMBOLIC_NODE
3452 : (a35a67.get_size (),
3453 : bit_mgr.get_concrete (bits_from_bytes (5_bytes)));
3454 12 : ASSERT_SAME_SYMBOLIC_NODE
3455 : (a35a67,
3456 : a.get_cropped_bytes (concrete_byte_range (3_bytes, 5_bytes)));
3457 : }
3458 :
3459 : /* Verify that concat(A[0, n], A[n, size(A)]) --> A. */
3460 4 : {
3461 12 : auto &a06 = a.get_cropped_bytes (concrete_byte_range (0_bytes, 7_bytes));
3462 12 : auto &a79 = a.get_cropped_bytes (concrete_byte_range (7_bytes, 3_bytes));
3463 4 : auto &a06a79 = bv_mgr.get_concat_bits ({&a06, &a79});
3464 4 : ASSERT_SAME_SYMBOLIC_NODE (a06a79, a);
3465 : }
3466 :
3467 : /* Verify crop of crop: e.g. A[5..10][2..3] --> A[7..8]. */
3468 4 : {
3469 12 : auto &a59 = a.get_cropped_bytes (concrete_byte_range (5_bytes, 5_bytes));
3470 4 : auto &a59_23
3471 12 : = a59.get_cropped_bytes (concrete_byte_range (2_bytes, 2_bytes));
3472 12 : ASSERT_SAME_SYMBOLIC_NODE
3473 : (a59_23,
3474 : a.get_cropped_bytes (concrete_byte_range (7_bytes, 2_bytes)));
3475 : }
3476 :
3477 : // Verify poisoned(x)[m..n] --> poisoned(x, sizeof(m..n)).
3478 4 : {
3479 4 : auto &bits
3480 8 : = (bv_mgr.get_poisoned_bits (bits_from_bytes (10_bytes),
3481 : poison_kind::uninit)
3482 12 : .get_cropped_bytes (concrete_byte_range (5_bytes, 2_bytes)));
3483 4 : ASSERT_SAME_SYMBOLIC_NODE
3484 : (bits.get_size (),
3485 : bit_mgr.get_concrete (bits_from_bytes (2_bytes)));
3486 4 : ASSERT_SAME_SYMBOLIC_NODE
3487 : (bits,
3488 : bv_mgr.get_poisoned_bits (16_bits, poison_kind::uninit));
3489 : }
3490 :
3491 : // Verify tristate(x)[m..n] --> tristate(x, sizeof(m..n)).
3492 4 : {
3493 4 : auto &bits
3494 4 : = (bv_mgr.get_all_zeros (10_bytes)
3495 12 : .get_cropped_bytes (concrete_byte_range (5_bytes, 2_bytes)));
3496 4 : ASSERT_SAME_SYMBOLIC_NODE
3497 : (bits.get_size (),
3498 : bit_mgr.get_concrete (bits_from_bytes (2_bytes)));
3499 4 : ASSERT_SAME_SYMBOLIC_NODE (bits, bv_mgr.get_all_zeros (16_bits));
3500 : }
3501 :
3502 : /* crop of concat e.g.:
3503 : concat(X1, ... Xn)[a, b] --> concat(X1[...], ..., Xn[...])
3504 : */
3505 4 : {
3506 4 : auto &x = bv_mgr.get_named_test_bit_vector (1_byte, "X");
3507 4 : auto &y = bv_mgr.get_named_test_bit_vector (1_byte, "Y");
3508 4 : auto &z = bv_mgr.get_named_test_bit_vector (1_byte, "Z");
3509 4 : auto &xyz = bv_mgr.get_concat_bits ({&x, &y, &z});
3510 12 : ASSERT_SAME_SYMBOLIC_NODE
3511 : (xyz.get_cropped_bytes (concrete_byte_range (0_bytes, 1_byte)),
3512 : x);
3513 12 : ASSERT_SAME_SYMBOLIC_NODE
3514 : (xyz.get_cropped_bytes (concrete_byte_range (1_byte, 1_byte)),
3515 : y);
3516 12 : ASSERT_SAME_SYMBOLIC_NODE
3517 : (xyz.get_cropped_bytes (concrete_byte_range (2_bytes, 1_byte)),
3518 : z);
3519 12 : ASSERT_SAME_SYMBOLIC_NODE
3520 : (xyz.get_cropped_bytes (concrete_byte_range (0_bytes, 2_bytes)),
3521 : bv_mgr.get_concat_bits ({&x, &y}));
3522 12 : ASSERT_SAME_SYMBOLIC_NODE
3523 : (xyz.get_cropped_bits (concrete_bit_range (3_bits, 9_bits)),
3524 : bv_mgr.get_concat_bits
3525 : ({&x.get_suffix (5_bits), &y.get_prefix (4_bits)}));
3526 : }
3527 :
3528 4 : {
3529 : /* 20 bytes: 5 repeats of a 4 byte vector:
3530 : If x is [abcd] then we have bytes 0-19:
3531 : . 1111111111
3532 : . 01234567890123456789
3533 : . abcdabcdabcdabcdabcd */
3534 4 : auto &x = bv_mgr.get_named_test_bit_vector (4_bytes, "X");
3535 4 : auto &x_times_5 = bv_mgr.get_repeated_bits (x, 5);
3536 12 : ASSERT_SAME_SYMBOLIC_NODE
3537 : (x_times_5.get_cropped_bytes (concrete_byte_range (0_bytes, 20_bytes)),
3538 : x_times_5);
3539 :
3540 : /* Getting individual bytes. */
3541 88 : for (concrete_byte_offset i = 0_bytes; i < 20_bytes; ++i)
3542 80 : ASSERT_SAME_SYMBOLIC_NODE
3543 : (x_times_5.get_byte_at_offset (i),
3544 4 : x.get_byte_at_offset (i.floor_mod (4_bytes)));
3545 :
3546 : // Cropping on an exact boundary should give us X again:
3547 12 : ASSERT_SAME_SYMBOLIC_NODE
3548 : (x_times_5.get_cropped_bytes (concrete_byte_range (0_bytes, 4_bytes)),
3549 : x);
3550 12 : ASSERT_SAME_SYMBOLIC_NODE
3551 : (x_times_5.get_cropped_bytes (concrete_byte_range (4_bytes, 4_bytes)),
3552 : x);
3553 :
3554 : /* Cropping at awkward boundaries. */
3555 : /* Bytes [3,5). */
3556 12 : ASSERT_SAME_SYMBOLIC_NODE
3557 : (x_times_5.get_cropped_bytes (concrete_byte_range (3_bytes, 2_bytes)),
3558 : bv_mgr.get_concat_bits
3559 : ({&x.get_suffix (1_byte),
3560 : &x.get_prefix (1_byte)}));
3561 :
3562 : /* Bytes [3,19):
3563 : . 1111111111
3564 : . 01234567890123456789
3565 : . abcdabcdabcdabcdabcd
3566 : . [PQQQQQQQQQQQQRRR]
3567 : should give us:
3568 : - P: the last byte of X
3569 : - Q: 3 repeats of X
3570 : - R: the first 3 bytes of X. */
3571 28 : ASSERT_SAME_SYMBOLIC_NODE
3572 : (x_times_5.get_cropped_bytes (concrete_byte_range (3_bytes, 16_bytes)),
3573 : bv_mgr.get_concat_bits
3574 : ({&x.get_cropped_bytes (concrete_byte_range (3_bytes, 1_byte)),
3575 : &bv_mgr.get_repeated_bits (x, 3),
3576 : &x.get_cropped_bytes (concrete_byte_range (0_bytes, 3_bytes))}));
3577 : }
3578 4 : }
3579 :
3580 : static void
3581 4 : test_named_test_bit_vector ()
3582 : {
3583 4 : symbolic::node_manager node_mgr;
3584 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
3585 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3586 :
3587 4 : auto &x = bv_mgr.get_named_test_bit_vector (1024_bytes, "X");
3588 4 : ASSERT_SAME_SYMBOLIC_NODE
3589 : (x.get_size (),
3590 : bit_mgr.get_concrete (bits_from_bytes (1024_bytes)));
3591 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3592 : (x,
3593 : "named_test_bit_vector[size: 1024 bytes]: 'X'\n");
3594 4 : x.to_json ();
3595 4 : }
3596 :
3597 : static void
3598 4 : test_poisoned ()
3599 : {
3600 4 : symbolic::node_manager node_mgr;
3601 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3602 4 : auto &bit_mgr = node_mgr.get_scalars ().get_bits ();
3603 :
3604 4 : auto &uninit_bit = mgr.get_poisoned_bits (1_bit, poison_kind::uninit);
3605 :
3606 4 : ASSERT_SAME_SYMBOLIC_NODE (uninit_bit.get_size (),
3607 : bit_mgr.get_concrete (1_bit));
3608 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ (uninit_bit,
3609 : "poisoned_bit_vector[size: 1 bit]: uninit\n");
3610 :
3611 4 : ASSERT_SAME_SYMBOLIC_NODE
3612 : (mgr.get_poisoned_bits (0_bits, poison_kind::uninit),
3613 : mgr.get_empty ());
3614 :
3615 4 : uninit_bit.to_json ();
3616 :
3617 4 : auto &freed_bit = mgr.get_poisoned_bits (1_bit, poison_kind::freed);
3618 4 : ASSERT_SAME_SYMBOLIC_NODE (freed_bit.get_size (),
3619 : bit_mgr.get_concrete (1_bit));
3620 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ (freed_bit,
3621 : "poisoned_bit_vector[size: 1 bit]: freed\n");
3622 :
3623 4 : freed_bit.to_json ();
3624 :
3625 4 : auto &three_uninit_bits
3626 4 : = mgr.get_poisoned_bits (3_bits, poison_kind::uninit);
3627 4 : ASSERT_SAME_SYMBOLIC_NODE (three_uninit_bits.get_size (),
3628 : bit_mgr.get_concrete (3_bits));
3629 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ (three_uninit_bits,
3630 : "poisoned_bit_vector[size: 3 bits]: uninit\n");
3631 4 : }
3632 :
3633 : static void
3634 4 : test_repeated ()
3635 : {
3636 4 : symbolic::node_manager node_mgr;
3637 4 : auto &mgr = node_mgr.get_bit_vector_manager ();
3638 :
3639 4 : auto &byte_00 = mgr.get_all_zeros (1_byte);
3640 4 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3641 : (byte_00,
3642 : "tristate_bit_vector[size: 1 byte]: false bits\n");
3643 :
3644 : /* Verify that repeats of tristate_bit_vector are folded to
3645 : tristate_bit_vector. */
3646 4 : auto &kilobyte_of_zeros = mgr.get_repeated_bits (byte_00, 1024);
3647 4 : ASSERT_SAME_SYMBOLIC_NODE (kilobyte_of_zeros, mgr.get_all_zeros (1024_bytes));
3648 :
3649 4 : kilobyte_of_zeros.to_json ();
3650 :
3651 : // Verify that 0 repeats is the empty bit vector:
3652 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_repeated_bits (byte_00, 0),
3653 : mgr.get_empty ());
3654 :
3655 : // Verify that we fold repeats of repeats into repeats:
3656 4 : auto &char_A
3657 4 : = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'A'));
3658 4 : auto &ten_As = mgr.get_repeated_bits (char_A, 10);
3659 4 : auto &two_times_ten_As = mgr.get_repeated_bits (ten_As, 2);
3660 4 : ASSERT_SAME_SYMBOLIC_NODE
3661 : (two_times_ten_As,
3662 : mgr.get_repeated_bits (char_A, 20));
3663 :
3664 4 : {
3665 4 : pretty_printer pp;
3666 4 : hexdump_options hexdump_opts;
3667 4 : hexdump_opts.m_bytes_per_line = 10_bytes;
3668 4 : two_times_ten_As.hexdump_to_pp (&pp, hexdump_opts);
3669 4 : ASSERT_STREQ
3670 : (pp_formatted_text (&pp),
3671 : "+--------------+-----------------------------+----------+\n"
3672 : "| [bytes 0..10)|41 41 41 41 41 41 41 41 41 41|AAAAAAAAAA|\n"
3673 : "+--------------+-----------------------------+----------+\n"
3674 : "|[bytes 10..20)|41 41 41 41 41 41 41 41 41 41|AAAAAAAAAA|\n"
3675 : "+--------------+-----------------------------+----------+\n");
3676 4 : }
3677 4 : {
3678 4 : pretty_printer pp;
3679 4 : hexdump_options hexdump_opts;
3680 4 : hexdump_opts.m_show_binary = true;
3681 4 : two_times_ten_As.hexdump_to_pp (&pp, hexdump_opts);
3682 4 : ASSERT_STREQ
3683 : (pp_formatted_text (&pp),
3684 : ("+--------------+-----------------------------------+-----------+----+\n"
3685 : "| [bytes 0..4)|10000010 10000010 10000010 10000010|41 41 41 41|AAAA|\n"
3686 : "+--------------+-----------------------------------+-----------+----+\n"
3687 : "| [bytes 4..8)|10000010 10000010 10000010 10000010|41 41 41 41|AAAA|\n"
3688 : "+--------------+-----------------------------------+-----------+----+\n"
3689 : "| [bytes 8..12)|10000010 10000010 10000010 10000010|41 41 41 41|AAAA|\n"
3690 : "+--------------+-----------------------------------+-----------+----+\n"
3691 : "|[bytes 12..16)|10000010 10000010 10000010 10000010|41 41 41 41|AAAA|\n"
3692 : "+--------------+-----------------------------------+-----------+----+\n"
3693 : "|[bytes 16..20)|10000010 10000010 10000010 10000010|41 41 41 41|AAAA|\n"
3694 : "+--------------+-----------------------------------+-----------+----+\n"));
3695 4 : }
3696 :
3697 : // Verify that repeat(A, 1) --> A.
3698 4 : ASSERT_SAME_SYMBOLIC_NODE (mgr.get_repeated_bits (char_A, 1), char_A);
3699 4 : }
3700 :
3701 : static void
3702 4 : test_bit_vector_from_svalue ()
3703 : {
3704 4 : symbolic::node_manager node_mgr;
3705 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
3706 4 : auto &rmm = node_mgr.get_svalue_manager ();
3707 :
3708 4 : auto *unknown_char_sval
3709 4 : = rmm.get_or_create_unknown_svalue (unsigned_char_type_node);
3710 4 : ASSERT_SAME_SYMBOLIC_NODE
3711 : (bv_mgr.get_bit_vector_from_svalue (*unknown_char_sval),
3712 : bv_mgr.get_unknown_bits (8_bits));
3713 :
3714 4 : unknown_char_sval->to_json ();
3715 :
3716 : /* Verify that we remove redundant casts
3717 : e.g. BIT_VECTOR_FOR (UNARY_OP(signed, NOP_EXPR, SVAL(unsigned)))
3718 : folds to BIT_VECTOR_FOR (SVAL(unsigned)). */
3719 4 : {
3720 4 : placeholder_svalue sval (node_mgr.alloc_node_id (),
3721 4 : integer_type_node, "x");
3722 4 : auto *cast_sval = rmm.get_or_create_cast (unsigned_type_node, &sval);
3723 4 : ASSERT_DIFFERENT_SYMBOLIC_NODE (sval, *cast_sval);
3724 4 : ASSERT_SAME_SYMBOLIC_NODE (bv_mgr.get_bit_vector_from_svalue (sval),
3725 : bv_mgr.get_bit_vector_from_svalue (*cast_sval));
3726 4 : }
3727 :
3728 : /* Make a bit_vector from bitwise "and" and "or" against a concrete mask to
3729 : verify what we "know" about individual bits and bytes (using the
3730 : irange_bitmask). */
3731 4 : {
3732 4 : placeholder_svalue y (node_mgr.alloc_node_id (),
3733 4 : uint32_type_node, "y");
3734 4 : auto *mask = rmm.get_or_create_int_cst (uint32_type_node, 0xff00ff00);
3735 :
3736 4 : hexdump_options hexdump_opts;
3737 4 : hexdump_opts.m_show_binary = true;
3738 4 : hexdump_opts.m_show_ascii = false;
3739 :
3740 4 : {
3741 4 : auto *bitwise_and = rmm.get_or_create_binop (uint32_type_node,
3742 : BIT_AND_EXPR,
3743 : &y,
3744 : mask);
3745 4 : auto &bv = bv_mgr.get_bit_vector_from_svalue (*bitwise_and);
3746 :
3747 : // Check what we know about individual bits in the bit_vector
3748 40 : for (auto i = 0_bits; i < 8_bits; ++i)
3749 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate (false));
3750 40 : for (auto i = 8_bits; i < 16_bits; ++i)
3751 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate::unknown ());
3752 40 : for (auto i = 16_bits; i < 24_bits; ++i)
3753 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate (false));
3754 40 : for (auto i = 24_bits; i < 32_bits; ++i)
3755 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate::unknown ());
3756 :
3757 : // Check what we know about individual bytes in the bit_vector
3758 4 : ASSERT_SAME_SYMBOLIC_NODE (bv.get_byte_at_offset (0_bytes),
3759 : bv_mgr.get_uint8 (0));
3760 4 : ASSERT_SAME_SYMBOLIC_NODE (bv.get_byte_at_offset (2_bytes),
3761 : bv_mgr.get_uint8 (0));
3762 :
3763 4 : pretty_printer pp;
3764 4 : bv.hexdump_to_pp (&pp, hexdump_opts);
3765 4 : ASSERT_STREQ
3766 : (pp_formatted_text (&pp),
3767 : "+------------+-----------------------------------+-----------+\n"
3768 : "|[bytes 0..4)|00000000 ???????? 00000000 ????????|00 ?? 00 ??|\n"
3769 : "+------------+-----------------------------------+-----------+\n");
3770 4 : }
3771 4 : {
3772 4 : auto *bitwise_or = rmm.get_or_create_binop (uint32_type_node,
3773 : BIT_IOR_EXPR,
3774 : &y,
3775 : mask);
3776 4 : auto &bv = bv_mgr.get_bit_vector_from_svalue (*bitwise_or);
3777 :
3778 : // Check what we know about individual bits in the bit_vector
3779 40 : for (auto i = 0_bits; i < 8_bits; ++i)
3780 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate::unknown ());
3781 40 : for (auto i = 8_bits; i < 16_bits; ++i)
3782 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate (true));
3783 40 : for (auto i = 16_bits; i < 24_bits; ++i)
3784 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate::unknown ());
3785 40 : for (auto i = 24_bits; i < 32_bits; ++i)
3786 32 : ASSERT_EQ (bv.get_tristate_at_offset (i), tristate (true));
3787 :
3788 : // Check what we know about individual bytes in the bit_vector
3789 4 : ASSERT_SAME_SYMBOLIC_NODE (bv.get_byte_at_offset (1_byte),
3790 : bv_mgr.get_uint8 (0xff));
3791 4 : ASSERT_SAME_SYMBOLIC_NODE (bv.get_byte_at_offset (3_bytes),
3792 : bv_mgr.get_uint8 (0xff));
3793 :
3794 4 : pretty_printer pp;
3795 4 : bv.hexdump_to_pp (&pp, hexdump_opts);
3796 4 : ASSERT_STREQ
3797 : (pp_formatted_text (&pp),
3798 : "+------------+-----------------------------------+-----------+\n"
3799 : "|[bytes 0..4)|???????? 11111111 ???????? 11111111|?? ff ?? ff|\n"
3800 : "+------------+-----------------------------------+-----------+\n");
3801 4 : }
3802 4 : }
3803 4 : }
3804 :
3805 : /* Consider this buggy C code (writing to a fixed-size buffer with no
3806 : size checks on inputs):
3807 : | char buf[1024];
3808 : | len_a = strlen (str_a);
3809 : | len_b = strlen (str_b);
3810 : | memcpy (buf, str_a, len_a);
3811 : | memcpy (buf + len_a, str_b, len_b);
3812 : | buf[len_a + len_b] = '\0';
3813 : */
3814 : static void
3815 4 : test_example_strcat ()
3816 : {
3817 4 : symbolic::node_manager node_mgr;
3818 4 : auto &bv_mgr = node_mgr.get_bit_vector_manager ();
3819 4 : auto &byte_mgr = node_mgr.get_scalars ().get_bytes ();
3820 :
3821 4 : auto &strlen_a = byte_mgr.get_named_test_scalar ("strlen(a)");
3822 4 : auto &strlen_b = byte_mgr.get_named_test_scalar ("strlen(b)");
3823 4 : auto &a = bv_mgr.get_named_test_bit_vector (strlen_a + 1_byte, "a");
3824 4 : auto &b = bv_mgr.get_named_test_bit_vector (strlen_b + 1_byte, "b");
3825 :
3826 4 : auto &bv_for_buf = bv_mgr.get_concat_bits
3827 12 : ({&a.get_prefix (strlen_a),
3828 4 : &b.get_prefix (strlen_b),
3829 4 : &bv_mgr.get_uint8 (0)});
3830 :
3831 4 : ASSERT_SAME_SYMBOLIC_NODE
3832 : (bv_for_buf.get_size (),
3833 : bits_from_bytes (strlen_a + strlen_b + 1_byte));
3834 4 : ASSERT_SAME_SYMBOLIC_NODE
3835 : (*maybe_bytes_from_bits (bv_for_buf.get_size ()),
3836 : strlen_a + strlen_b + 1_byte);
3837 4 : }
3838 :
3839 : static void
3840 4 : test_from_unfoldable_bit_contents ()
3841 : {
3842 4 : symbolic::node_manager mgr;
3843 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3844 4 : auto &bc_mgr = mgr.get_bit_contents_manager ();
3845 :
3846 4 : auto &ubc_a = bc_mgr.get_named_test_bit_contents ("A");
3847 4 : auto &ubc_b = bc_mgr.get_named_test_bit_contents ("B");
3848 :
3849 12 : ASSERT_SYMBOLIC_NODE_DUMP_EQ
3850 : (ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3851 : ("bit_vector_from_ubc[size: 10 bytes]\n"
3852 : "+- bit_contents: named_test_bit_contents: A\n"
3853 : "`- range: symbolic range\n"
3854 : " +- start offset: concrete_scalar<bits>: 0\n"
3855 : " `- size: concrete_scalar<bits>: 80\n"));
3856 :
3857 20 : ASSERT_SAME_SYMBOLIC_NODE
3858 : (ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3859 : ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)));
3860 :
3861 20 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3862 : (ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3863 : ubc_a.get_bytes (concrete_byte_range (0_bytes, 9_bytes)));
3864 :
3865 20 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3866 : (ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)),
3867 : ubc_b.get_bytes (concrete_byte_range (0_bytes, 10_bytes)));
3868 :
3869 : /* Concat of touching ranges from same bit contents should
3870 : be folded to a single range from that bit contents. */
3871 28 : ASSERT_SAME_SYMBOLIC_NODE
3872 : (bv_mgr.get_concat_bits
3873 : ({&ubc_a.get_bytes (concrete_byte_range (0_bytes, 4_bytes)),
3874 : &ubc_a.get_bytes (concrete_byte_range (4_bytes, 6_bytes))}),
3875 : ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)));
3876 : /* ...but not for those that don't touch: */
3877 28 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3878 : (bv_mgr.get_concat_bits
3879 : ({&ubc_a.get_bytes (concrete_byte_range (0_bytes, 4_bytes)),
3880 : &ubc_a.get_bytes (concrete_byte_range (5_bytes, 5_bytes))}),
3881 : ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)));
3882 : /* ...but not for those from different bit contents: */
3883 28 : ASSERT_DIFFERENT_SYMBOLIC_NODE
3884 : (bv_mgr.get_concat_bits
3885 : ({&ubc_a.get_bytes (concrete_byte_range (0_bytes, 4_bytes)),
3886 : &ubc_b.get_bytes (concrete_byte_range (4_bytes, 6_bytes))}),
3887 : ubc_a.get_bytes (concrete_byte_range (0_bytes, 10_bytes)));
3888 :
3889 : /* Cropping a range from a ubc should give the appropriate
3890 : subrange from the ubc. */
3891 4 : auto &a_bytes_10_14
3892 12 : = ubc_a.get_bytes (concrete_byte_range (10_bytes, 5_bytes));
3893 12 : ASSERT_SAME_SYMBOLIC_NODE
3894 : (a_bytes_10_14.get_cropped_bytes (concrete_byte_range (0_bytes, 5_bytes)),
3895 : a_bytes_10_14);
3896 20 : ASSERT_SAME_SYMBOLIC_NODE
3897 : (a_bytes_10_14.get_cropped_bytes (concrete_byte_range (0_bytes, 1_byte)),
3898 : ubc_a.get_bytes (concrete_byte_range (10_bytes, 1_byte)));
3899 20 : ASSERT_SAME_SYMBOLIC_NODE
3900 : (a_bytes_10_14.get_cropped_bytes (concrete_byte_range (4_bytes, 1_byte)),
3901 : ubc_a.get_bytes (concrete_byte_range (14_bytes, 1_byte)));
3902 4 : }
3903 :
3904 : /* Test type-punning uint32_t to bytes, using the target's endianness. */
3905 :
3906 : static void
3907 4 : test_uint32_t_to_bytes ()
3908 : {
3909 4 : symbolic::node_manager mgr;
3910 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3911 4 : auto &rmm = mgr.get_region_model_manager ();
3912 :
3913 4 : auto &uint32_0x12345678
3914 4 : = *rmm.get_or_create_int_cst (uint32_type_node, 0x12345678);
3915 4 : auto &bv_0x12345678 = bv_mgr.get_bit_vector_from_svalue (uint32_0x12345678);
3916 :
3917 4 : auto &uint8_0x12
3918 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x12);
3919 4 : auto &bv_0x12 = bv_mgr.get_bit_vector_from_svalue (uint8_0x12);
3920 :
3921 4 : auto &uint8_0x34
3922 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x34);
3923 4 : auto &bv_0x34 = bv_mgr.get_bit_vector_from_svalue (uint8_0x34);
3924 :
3925 4 : auto &uint8_0x56
3926 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x56);
3927 4 : auto &bv_0x56 = bv_mgr.get_bit_vector_from_svalue (uint8_0x56);
3928 :
3929 4 : auto &uint8_0x78
3930 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x78);
3931 4 : auto &bv_0x78 = bv_mgr.get_bit_vector_from_svalue (uint8_0x78);
3932 :
3933 4 : auto &bv_byte_0 = bv_0x12345678.get_byte_at_offset (0_bytes);
3934 4 : auto &bv_byte_1 = bv_0x12345678.get_byte_at_offset (1_byte);
3935 4 : auto &bv_byte_2 = bv_0x12345678.get_byte_at_offset (2_bytes);
3936 4 : auto &bv_byte_3 = bv_0x12345678.get_byte_at_offset (3_bytes);
3937 :
3938 4 : if (BYTES_BIG_ENDIAN)
3939 : {
3940 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_0, bv_0x12);
3941 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_1, bv_0x34);
3942 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_2, bv_0x56);
3943 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_3, bv_0x78);
3944 : }
3945 : else
3946 : {
3947 : /* Little-endian. */
3948 4 : if (!WORDS_BIG_ENDIAN)
3949 : {
3950 4 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_0, bv_0x78);
3951 4 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_1, bv_0x56);
3952 4 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_2, bv_0x34);
3953 4 : ASSERT_SAME_SYMBOLIC_NODE (bv_byte_3, bv_0x12);
3954 : }
3955 : }
3956 4 : }
3957 :
3958 : /* Test type-punning bytes to uint32_t, using the target's endianness. */
3959 :
3960 : static void
3961 4 : test_bytes_to_uint32_t ()
3962 : {
3963 4 : symbolic::node_manager mgr;
3964 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
3965 4 : auto &rmm = mgr.get_region_model_manager ();
3966 :
3967 4 : auto &uint8_0x12
3968 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x12);
3969 4 : auto &bv_0x12 = bv_mgr.get_bit_vector_from_svalue (uint8_0x12);
3970 :
3971 4 : auto &uint8_0x34
3972 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x34);
3973 4 : auto &bv_0x34 = bv_mgr.get_bit_vector_from_svalue (uint8_0x34);
3974 :
3975 4 : auto &uint8_0x56
3976 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x56);
3977 4 : auto &bv_0x56 = bv_mgr.get_bit_vector_from_svalue (uint8_0x56);
3978 :
3979 4 : auto &uint8_0x78
3980 4 : = *rmm.get_or_create_int_cst (unsigned_char_type_node, 0x78);
3981 4 : auto &bv_0x78 = bv_mgr.get_bit_vector_from_svalue (uint8_0x78);
3982 :
3983 4 : auto &bytes
3984 4 : = bv_mgr.get_concat_bits ({&bv_0x12, &bv_0x34, &bv_0x56, &bv_0x78});
3985 :
3986 4 : const svalue *sval = rmm.get_svalue_from_bit_vector (uint32_type_node, bytes);
3987 :
3988 4 : const uint32_t expected_uint32 = (BYTES_BIG_ENDIAN
3989 : ? 0x12345678
3990 : : (WORDS_BIG_ENDIAN
3991 : ? 0x34127856
3992 : : 0x78563412));
3993 4 : ASSERT_SAME_SYMBOLIC_NODE
3994 : (*sval,
3995 : *rmm.get_or_create_int_cst (uint32_type_node, expected_uint32));
3996 4 : }
3997 :
3998 : static void
3999 4 : test_every_byte_value ()
4000 : {
4001 4 : symbolic::node_manager mgr;
4002 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
4003 :
4004 4 : std::vector<const symbolic::bit_vector *> vec_of_every_byte_value;
4005 1028 : for (int i = 0; i < 256; ++i)
4006 1024 : vec_of_every_byte_value.push_back (&bv_mgr.get_uint8 (i));
4007 4 : auto &every_byte_value_bv
4008 4 : = bv_mgr.get_concat_bits (std::move (vec_of_every_byte_value));
4009 :
4010 4 : {
4011 4 : hexdump_options hexdump_opts;
4012 4 : hexdump_opts.m_show_binary = true;
4013 4 : hexdump_opts.m_show_hex = true;
4014 4 : hexdump_opts.m_show_ascii = false;
4015 :
4016 : /* Hexdump of first 8 bytes. */
4017 4 : {
4018 4 : pretty_printer pp;
4019 4 : every_byte_value_bv
4020 12 : .get_cropped_bytes (concrete_byte_range (0_bytes, 8_bytes))
4021 4 : .hexdump_to_pp (&pp, hexdump_opts);
4022 4 : ASSERT_STREQ
4023 : (pp_formatted_text (&pp),
4024 : ("+------------+-----------------------------------+-----------+\n"
4025 : "|[bytes 0..4)|00000000 10000000 01000000 11000000|00 01 02 03|\n"
4026 : "+------------+-----------------------------------+-----------+\n"
4027 : "|[bytes 4..8)|00100000 10100000 01100000 11100000|04 05 06 07|\n"
4028 : "+------------+-----------------------------------+-----------+\n"));
4029 4 : }
4030 :
4031 : /* Hexdump of last 8 bytes. */
4032 4 : {
4033 4 : pretty_printer pp;
4034 4 : every_byte_value_bv
4035 12 : .get_cropped_bytes (concrete_byte_range (248_bytes, 8_bytes))
4036 4 : .hexdump_to_pp (&pp, hexdump_opts);
4037 4 : ASSERT_STREQ
4038 : (pp_formatted_text (&pp),
4039 : ("+------------+-----------------------------------+-----------+\n"
4040 : "|[bytes 0..4)|00011111 10011111 01011111 11011111|f8 f9 fa fb|\n"
4041 : "+------------+-----------------------------------+-----------+\n"
4042 : "|[bytes 4..8)|00111111 10111111 01111111 11111111|fc fd fe ff|\n"
4043 : "+------------+-----------------------------------+-----------+\n"));
4044 4 : }
4045 4 : }
4046 4 : }
4047 :
4048 : /* Run all of the selftests within this file. */
4049 :
4050 : void
4051 4 : analyzer_symbolic_bit_vectors_cc_tests ()
4052 : {
4053 4 : test_maybe_set_irange_bitmask_from_concrete_tristate_vector ();
4054 4 : test_tristate ();
4055 4 : test_integer_constants ();
4056 4 : test_bytes ();
4057 4 : test_string_constants ();
4058 4 : test_raw_data_constant_1 ();
4059 4 : test_raw_data_constant_2 ();
4060 4 : test_raw_data_constant_empty ();
4061 4 : test_concat ();
4062 4 : test_cropped ();
4063 4 : test_named_test_bit_vector ();
4064 4 : test_poisoned ();
4065 4 : test_repeated ();
4066 4 : test_bit_vector_from_svalue ();
4067 4 : test_from_unfoldable_bit_contents ();
4068 4 : test_example_strcat ();
4069 4 : test_uint32_t_to_bytes ();
4070 4 : test_bytes_to_uint32_t ();
4071 4 : test_every_byte_value ();
4072 4 : }
4073 :
4074 : } // namespace selftest
4075 :
4076 : #endif /* CHECKING_P */
4077 :
4078 : } // namespace ana
4079 :
4080 : #endif /* #if ENABLE_ANALYZER */
|