1 /* Symbolic offsets and ranges.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
22 #define INCLUDE_MEMORY
23 #define INCLUDE_VECTOR
25 #include "coretypes.h"
27 #include "diagnostic-core.h"
28 #include "gimple-pretty-print.h"
30 #include "basic-block.h"
32 #include "gimple-iterator.h"
33 #include "diagnostic-core.h"
38 #include "stringpool.h"
41 #include "fold-const.h"
42 #include "tree-pretty-print.h"
44 #include "analyzer/analyzer.h"
45 #include "analyzer/analyzer-logging.h"
46 #include "ordered-hash-map.h"
48 #include "analyzer/supergraph.h"
50 #include "analyzer/call-string.h"
51 #include "analyzer/program-point.h"
52 #include "analyzer/store.h"
53 #include "analyzer/region-model.h"
54 #include "analyzer/constraint-manager.h"
55 #include "analyzer/analyzer-selftests.h"
56 #include "analyzer/ranges.h"
62 /* class symbolic_byte_offset. */
64 symbolic_byte_offset::symbolic_byte_offset (int i
, region_model_manager
&mgr
)
65 : m_num_bytes_sval (mgr
.get_or_create_int_cst (size_type_node
, i
))
69 symbolic_byte_offset::symbolic_byte_offset (const svalue
*num_bytes_sval
)
70 : m_num_bytes_sval (num_bytes_sval
)
74 symbolic_byte_offset::symbolic_byte_offset (region_offset offset
,
75 region_model_manager
&mgr
)
77 if (offset
.concrete_p ())
79 bit_offset_t num_bits
= offset
.get_bit_offset ();
80 gcc_assert (num_bits
% BITS_PER_UNIT
== 0);
81 byte_offset_t num_bytes
= num_bits
/ BITS_PER_UNIT
;
82 m_num_bytes_sval
= mgr
.get_or_create_int_cst (size_type_node
, num_bytes
);
85 m_num_bytes_sval
= offset
.get_symbolic_byte_offset ();
89 symbolic_byte_offset::dump_to_pp (pretty_printer
*pp
, bool simple
) const
91 pp_string (pp
, "byte ");
92 m_num_bytes_sval
->dump_to_pp (pp
, simple
);
96 symbolic_byte_offset::dump (bool simple
) const
99 pp_format_decoder (&pp
) = default_tree_printer
;
100 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
101 pp
.set_output_stream (stderr
);
102 dump_to_pp (&pp
, simple
);
108 symbolic_byte_offset::to_json () const
110 return m_num_bytes_sval
->to_json ();
114 symbolic_byte_offset::maybe_get_constant () const
116 return m_num_bytes_sval
->maybe_get_constant ();
119 /* class symbolic_byte_range. */
121 symbolic_byte_range::symbolic_byte_range (region_offset start
,
122 const svalue
*num_bytes
,
123 region_model_manager
&mgr
)
124 : m_start (start
, mgr
),
130 symbolic_byte_range::dump_to_pp (pretty_printer
*pp
,
132 region_model_manager
&mgr
) const
136 pp_string (pp
, "empty");
140 if (tree size_cst
= m_size
.maybe_get_constant ())
141 if (integer_onep (size_cst
))
143 pp_string (pp
, "byte ");
144 m_start
.get_svalue ()->dump_to_pp (pp
, simple
);
148 pp_string (pp
, "bytes ");
149 m_start
.get_svalue ()->dump_to_pp (pp
, simple
);
150 pp_string (pp
, " to ");
151 get_last_byte_offset (mgr
).get_svalue ()->dump_to_pp (pp
, simple
);
155 symbolic_byte_range::dump (bool simple
, region_model_manager
&mgr
) const
158 pp_format_decoder (&pp
) = default_tree_printer
;
159 pp_show_color (&pp
) = pp_show_color (global_dc
->printer
);
160 pp
.set_output_stream (stderr
);
161 dump_to_pp (&pp
, simple
, mgr
);
167 symbolic_byte_range::to_json () const
169 json::object
*obj
= new json::object ();
170 obj
->set ("start", m_start
.to_json ());
171 obj
->set ("size", m_size
.to_json ());
176 symbolic_byte_range::empty_p () const
178 tree cst
= m_size
.maybe_get_constant ();
185 symbolic_byte_range::get_last_byte_offset (region_model_manager
&mgr
) const
187 gcc_assert (!empty_p ());
188 const symbolic_byte_offset
one (1, mgr
);
189 return symbolic_byte_offset
190 (mgr
.get_or_create_binop (size_type_node
,
192 get_next_byte_offset (mgr
).get_svalue (),
197 symbolic_byte_range::get_next_byte_offset (region_model_manager
&mgr
) const
199 return symbolic_byte_offset (mgr
.get_or_create_binop (size_type_node
,
201 m_start
.get_svalue (),
202 m_size
.get_svalue ()));
205 /* Attempt to determine if THIS range intersects OTHER,
206 using constraints from MODEL. */
209 symbolic_byte_range::intersection (const symbolic_byte_range
&other
,
210 const region_model
&model
) const
212 /* If either is empty, then there is no intersection. */
214 return tristate::TS_FALSE
;
215 if (other
.empty_p ())
216 return tristate::TS_FALSE
;
218 /* For brevity, consider THIS to be "range A", and OTHER to be "range B". */
220 region_model_manager
*mgr
= model
.get_manager ();
222 const svalue
*first_sval_a
= m_start
.get_svalue ();
223 const svalue
*first_sval_b
= other
.m_start
.get_svalue ();
224 const svalue
*last_sval_a
= get_last_byte_offset (*mgr
).get_svalue ();
225 const svalue
*last_sval_b
= other
.get_last_byte_offset (*mgr
).get_svalue ();
227 if (m_size
.get_svalue ()->get_kind () == SK_UNKNOWN
228 || other
.m_size
.get_svalue ()->get_kind () == SK_UNKNOWN
)
230 if (first_sval_a
== first_sval_b
)
231 return tristate::TS_TRUE
;
233 return tristate::TS_UNKNOWN
;
236 if (first_sval_a
== first_sval_b
)
237 return tristate::TS_TRUE
;
239 /* Is B fully before A? */
240 tristate b_fully_before_a
= model
.eval_condition (last_sval_b
,
243 /* Is B fully after A? */
244 tristate b_fully_after_a
= model
.eval_condition (first_sval_b
,
248 if (b_fully_before_a
.is_true ()
249 || b_fully_after_a
.is_true ())
250 return tristate::TS_FALSE
;
252 if (b_fully_before_a
.is_unknown ()
253 || b_fully_after_a
.is_unknown ())
254 return tristate::TS_UNKNOWN
;
256 return tristate::TS_TRUE
;
263 static void test_intersects (void)
265 region_model_manager mgr
;
266 region_model
m (&mgr
);
268 /* Test various concrete ranges. */
269 symbolic_byte_offset
zero (0, mgr
);
270 symbolic_byte_offset
one (1, mgr
);
271 symbolic_byte_offset
five (5, mgr
);
272 symbolic_byte_offset
nine (9, mgr
);
273 symbolic_byte_offset
ten (10, mgr
);
275 symbolic_byte_range
r0_9 (zero
, ten
);
276 symbolic_byte_range
r0 (zero
, one
);
277 symbolic_byte_range
r5_9 (five
, five
);
278 symbolic_byte_range
r9 (nine
, one
);
279 symbolic_byte_range
r10 (ten
, one
);
280 symbolic_byte_range
r10_19 (ten
, ten
);
282 ASSERT_EQ (r0_9
.get_start_byte_offset (), zero
);
283 ASSERT_EQ (r0_9
.get_size_in_bytes (), ten
);
284 ASSERT_EQ (r0_9
.get_next_byte_offset (mgr
), ten
);
285 ASSERT_EQ (r0_9
.get_last_byte_offset (mgr
), nine
);
287 symbolic_byte_range
concrete_empty (zero
, zero
);
288 ASSERT_TRUE (concrete_empty
.empty_p ());
290 ASSERT_EQ (r0_9
.intersection (r0
, m
), tristate::TS_TRUE
);
291 ASSERT_EQ (r0
.intersection (r0_9
, m
), tristate::TS_TRUE
);
292 ASSERT_EQ (r0_9
.intersection (r9
, m
), tristate::TS_TRUE
);
293 ASSERT_EQ (r9
.intersection (r0_9
, m
), tristate::TS_TRUE
);
294 ASSERT_EQ (r0_9
.intersection (r10
, m
), tristate::TS_FALSE
);
295 ASSERT_EQ (r10
.intersection (r0_9
, m
), tristate::TS_FALSE
);
296 ASSERT_EQ (concrete_empty
.intersection (r0_9
, m
), tristate::TS_FALSE
);
297 ASSERT_EQ (r0_9
.intersection (concrete_empty
, m
), tristate::TS_FALSE
);
299 ASSERT_EQ (r5_9
.intersection (r0
, m
), tristate::TS_FALSE
);
300 ASSERT_EQ (r0
.intersection (r5_9
, m
), tristate::TS_FALSE
);
301 ASSERT_EQ (r9
.intersection (r5_9
, m
), tristate::TS_TRUE
);
302 ASSERT_EQ (r10
.intersection (r5_9
, m
), tristate::TS_FALSE
);
304 /* Test various symbolic ranges. */
305 tree x
= build_global_decl ("x", size_type_node
);
306 const svalue
*x_init_sval
= m
.get_rvalue (x
, nullptr);
307 tree y
= build_global_decl ("y", size_type_node
);
308 const svalue
*y_init_sval
= m
.get_rvalue (y
, nullptr);
310 symbolic_byte_range
r0_x_minus_1 (zero
, x_init_sval
);
311 symbolic_byte_range
rx (x_init_sval
, one
);
312 symbolic_byte_range
r0_y_minus_1 (zero
, y_init_sval
);
313 symbolic_byte_range
ry (y_init_sval
, one
);
314 symbolic_byte_range
rx_x_plus_y_minus_1 (x_init_sval
, y_init_sval
);
316 symbolic_byte_range
symbolic_empty (x_init_sval
, zero
);
317 ASSERT_TRUE (symbolic_empty
.empty_p ());
319 ASSERT_EQ (rx_x_plus_y_minus_1
.get_start_byte_offset (), x_init_sval
);
320 ASSERT_EQ (rx_x_plus_y_minus_1
.get_size_in_bytes (), y_init_sval
);
322 (rx_x_plus_y_minus_1
.get_next_byte_offset (mgr
).get_svalue ()->get_kind (),
325 (rx_x_plus_y_minus_1
.get_last_byte_offset (mgr
).get_svalue ()->get_kind (),
328 ASSERT_EQ (rx
.intersection (ry
, m
), tristate::TS_UNKNOWN
);
329 ASSERT_EQ (rx
.intersection (concrete_empty
, m
), tristate::TS_FALSE
);
330 ASSERT_EQ (concrete_empty
.intersection (rx
, m
), tristate::TS_FALSE
);
331 ASSERT_EQ (rx
.intersection (symbolic_empty
, m
), tristate::TS_FALSE
);
332 ASSERT_EQ (symbolic_empty
.intersection (rx
, m
), tristate::TS_FALSE
);
333 ASSERT_EQ (r0_x_minus_1
.intersection (r0
, m
), tristate::TS_TRUE
);
335 ASSERT_EQ (r0_x_minus_1
.intersection (rx
, m
), tristate::TS_FALSE
);
336 /* Fails (with UNKNOWN): b_fully_after_a is UNKNOWN, when it could
337 be TRUE: last of A is (x - 1), but it's not necessarily true that
338 X > (x - 1), for the case where x is (unsigned)0. */
340 ASSERT_EQ (r0_x_minus_1
.intersection (r0_y_minus_1
, m
), tristate::TS_TRUE
);
344 /* Run all of the selftests within this file. */
347 analyzer_ranges_cc_tests ()
352 } // namespace selftest
354 #endif /* CHECKING_P */
358 #endif /* #if ENABLE_ANALYZER */