Line data Source code
1 : /* A (base_region, symbolic bit range) pair.
2 : Copyright (C) 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 "selftest.h"
24 :
25 : #include "analyzer/symbolic-memory-ranges.h"
26 : #include "analyzer/region-model-manager.h"
27 :
28 : #if ENABLE_ANALYZER
29 :
30 : namespace ana {
31 : namespace symbolic {
32 :
33 : // class memory_range
34 :
35 4587 : memory_range::memory_range (pointer sym_ptr,
36 : const symbolic_byte_size &num_bytes)
37 4587 : : m_base_reg (sym_ptr.get_base_region ()),
38 4587 : m_bit_range (bits_from_bytes (symbolic_byte_range (sym_ptr.get_byte_offset (),
39 4587 : num_bytes)))
40 : {
41 4587 : }
42 :
43 : DEBUG_FUNCTION void
44 0 : memory_range::dump () const
45 : {
46 0 : symbolic::dump_node_tree_to_stderr<memory_range> (*this);
47 0 : }
48 :
49 : std::unique_ptr<text_art::tree_widget>
50 0 : memory_range::make_dump_widget (symbolic::node_dump_state &nds) const
51 : {
52 0 : using text_art::tree_widget;
53 0 : std::unique_ptr<tree_widget> widget
54 0 : (tree_widget::from_fmt (nds, nullptr, "Memory Range"));
55 0 : widget->add_child (m_base_reg.make_dump_widget (nds, "base region"));
56 0 : widget->add_child (m_bit_range.make_dump_widget (nds, "bit range"));
57 0 : return widget;
58 : }
59 :
60 : std::unique_ptr<json::object>
61 4 : memory_range::to_json () const
62 : {
63 4 : auto obj = std::make_unique<json::object> ();
64 4 : obj->set ("base_reg", m_base_reg.to_json ());
65 4 : obj->set ("bit_range", m_bit_range.to_json ());
66 4 : return obj;
67 : }
68 :
69 : } // namespace ana::symbolic
70 :
71 : #if CHECKING_P
72 :
73 : namespace selftest {
74 :
75 : /* seen in test_29c at
76 : q = &p[7];
77 : __analyzer_eval (q->x == 107024);
78 :
79 : (gdb) call memory_range.dump()
80 : Memory Range
81 : ├─ base region: (635): ‘struct coord’: symbolic_region: %<*%>
82 : │ ├─ m_sval_ptr: (630): ‘struct coord *’: pointer_svalue:
83 : │ │ ╰─ Pointer
84 : │ │ ├─ base region: (583): alloca_region
85 : │ │ │ ╰─ parent: (520): frame_region(‘test_29c’, index: 0, depth: 1)
86 : │ │ │ ╰─ parent: (1): stack region
87 : │ │ │ ╰─ parent: (0): root region
88 : │ │ ╰─ byte offset: (629): scalar<bytes>: 112
89 : │ ╰─ parent: (0): root region
90 : ╰─ bit range: symbolic range
91 : ├─ start offset: (527): scalar<bits>: 0
92 : ╰─ size: (528): scalar<bits>: 64
93 :
94 : Presumably we should fold this to:
95 :
96 : Memory Range
97 : ├─ base region: (583): alloca_region
98 : │ ╰─ parent: (520): frame_region(‘test_29c’, index: 0, depth: 1)
99 : │ ╰─ parent: (1): stack region
100 : │ ╰─ parent: (0): root region
101 : ╰─ bit range: symbolic range
102 : ├─ start offset: (527): scalar<bits>: 112 * 8
103 : ╰─ size: (528): scalar<bits>: 64
104 :
105 : i.e.
106 : memory_range (SYMBOLIC(POINTER(BASE_REG, OFFSET), (START, SIZE)))
107 : should fold to:
108 : memory_range (BASE_REG, (START + OFFSET, SIZE))
109 : */
110 :
111 : static void
112 4 : test_offset_from_pointer ()
113 : {
114 4 : symbolic::node_manager mgr;
115 4 : auto &bv_mgr = mgr.get_bit_vector_manager ();
116 4 : auto &rmm = mgr.get_region_model_manager ();
117 :
118 :
119 : // coord_test ct;
120 : // FIXME
121 4 : }
122 :
123 : /* Run all of the selftests within this file. */
124 :
125 : void
126 4 : analyzer_symbolic_memory_ranges_cc_tests ()
127 : {
128 4 : test_offset_from_pointer ();
129 4 : }
130 :
131 : } // namespace selftest
132 :
133 : #endif /* CHECKING_P */
134 :
135 : } // namespace ana
136 :
137 : #endif /* #if ENABLE_ANALYZER */
|