Line data Source code
1 : /* Typesafe symbolic ranges of values (e.g. "bytes m-n").
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 : #ifndef GCC_ANALYZER_SYMBOLIC_RANGES_H
22 : #define GCC_ANALYZER_SYMBOLIC_RANGES_H
23 :
24 : #include "analyzer/symbolic-scalars.h"
25 : #include "analyzer/concrete-ranges.h"
26 :
27 : namespace ana {
28 : namespace symbolic {
29 :
30 : template<typename ConcreteValueType>
31 : class range
32 : {
33 : public:
34 1908 : range ()
35 1908 : : m_start_offset (nullptr),
36 1908 : m_size (nullptr)
37 : {
38 : /* Placeholder default ctor for the few places where we need
39 : to conditionally initialize these. */
40 : }
41 :
42 458710 : range (const scalar<ConcreteValueType> &start_offset,
43 : const scalar<ConcreteValueType> &size)
44 615976 : : m_start_offset (&start_offset),
45 615893 : m_size (&size)
46 : {
47 : }
48 :
49 1741368 : range (const concrete::range<ConcreteValueType> &conc_range,
50 : scalar_manager<ConcreteValueType> &mgr)
51 1741368 : : m_start_offset (&mgr.get_concrete (conc_range.get_start_offset ())),
52 1741368 : m_size (&mgr.get_concrete (conc_range.get_size ()))
53 : {
54 1741368 : }
55 :
56 : static range
57 8 : from_endpoints (const scalar<ConcreteValueType> &start_offset,
58 : const scalar<ConcreteValueType> &next_offset)
59 : {
60 8 : return range (start_offset, next_offset - start_offset);
61 : }
62 :
63 : complexity
64 14543 : get_children_complexity () const
65 : {
66 14543 : return (m_start_offset->get_complexity ()
67 14543 : + m_size->get_complexity ());
68 : }
69 :
70 : const scalar<ConcreteValueType> &
71 3278988 : get_start_offset () const { return *m_start_offset; };
72 :
73 : const scalar<ConcreteValueType> &
74 8032975 : get_size () const { return *m_size; }
75 :
76 : const scalar<ConcreteValueType> &
77 168 : get_last_offset () const
78 : {
79 168 : ConcreteValueType concrete_one (1);
80 168 : auto &symbolic_one = m_start_offset->get_mgr ().get_concrete (concrete_one);
81 168 : return (get_next_offset () - symbolic_one);
82 168 : }
83 :
84 : const scalar<ConcreteValueType> &
85 41461 : get_next_offset () const { return *m_start_offset + *m_size; }
86 :
87 : std::unique_ptr<text_art::tree_widget>
88 : make_dump_widget (node_dump_state &nds,
89 : const char *prefix = nullptr) const;
90 :
91 : void dump () const;
92 :
93 : void log (const char *label, logger &log) const;
94 :
95 : bool
96 4707 : operator== (const range<ConcreteValueType> &other) const
97 : {
98 4703 : return (m_start_offset == other.m_start_offset
99 4707 : && m_size == other.m_size);
100 : }
101 :
102 : static int
103 1312001 : cmp (const range<ConcreteValueType> &lhs,
104 : const range<ConcreteValueType> &rhs)
105 : {
106 261907 : if (int c = node::cmp (*lhs.m_start_offset, *rhs.m_start_offset))
107 : return c;
108 1093278 : return node::cmp (*lhs.m_size, *rhs.m_size);
109 : }
110 :
111 :
112 : bool
113 1050094 : operator< (const range<ConcreteValueType> &other) const
114 : {
115 1050094 : return cmp (*this, other) < 0;
116 : }
117 :
118 : complexity
119 2424 : get_complexity () const
120 : {
121 2424 : return (m_start_offset->get_complexity ()
122 2424 : + m_size->get_complexity ());
123 : }
124 :
125 : void
126 : accept_node_visitor (node_visitor &v) const;
127 :
128 : std::unique_ptr<json::object>
129 : to_json () const;
130 :
131 : void
132 : dump_inline_to_pp (pretty_printer *pp) const;
133 :
134 : tristate
135 : intersects (const range<ConcreteValueType> &other,
136 : const region_model &model) const;
137 :
138 : bool
139 : access_range_contains_p (const range<ConcreteValueType> &other) const;
140 :
141 : bool
142 288 : empty_p () const
143 : {
144 288 : return m_size->is_zero_p ();
145 : }
146 :
147 : private:
148 : const scalar<ConcreteValueType> *m_start_offset;
149 : const scalar<ConcreteValueType> *m_size;
150 : };
151 :
152 : template <typename ConcreteValueType>
153 : inline bool
154 3840486 : concrete_from_symbolic (const symbolic::range<ConcreteValueType> &in,
155 : concrete::range<ConcreteValueType> *out)
156 : {
157 3840486 : if (auto *concrete_start_offset
158 3840486 : = in.get_start_offset ().dyn_cast_concrete_scalar ())
159 3814257 : if (auto *concrete_size = in.get_size ().dyn_cast_concrete_scalar ())
160 : {
161 11420778 : *out = concrete::range<ConcreteValueType>
162 : (concrete_start_offset->get_concrete (),
163 : concrete_size->get_concrete ());
164 3806926 : return true;
165 : }
166 : return false;
167 : }
168 :
169 : template <typename ConcreteValueType>
170 : inline symbolic::range<ConcreteValueType>
171 2164704 : symbolic_from_concrete (const concrete::range<ConcreteValueType> &conc_range,
172 : scalar_manager<concrete_bit_size> &bit_mgr)
173 : {
174 2164704 : return symbolic::range<ConcreteValueType>
175 4329408 : (symbolic_from_concrete (conc_range.get_start_offset (), bit_mgr),
176 4329408 : symbolic_from_concrete (conc_range.get_size (), bit_mgr));
177 : }
178 :
179 : } // namespace ana::symbolic
180 :
181 : symbolic_bit_range
182 : bits_from_bytes (const symbolic_byte_range &byte_range);
183 :
184 : bool
185 : maybe_bytes_from_bits (const symbolic_bit_range &bit_range,
186 : symbolic_byte_range *out);
187 :
188 : } // namespace ana
189 :
190 : #endif /* GCC_ANALYZER_SYMBOLIC_RANGES_H */
|