Line data Source code
1 : /* Bounds-checking of reads and writes to memory regions.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3, or (at your option)
9 : any later version.
10 :
11 : GCC is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "analyzer/common.h"
21 :
22 : #include "intl.h"
23 : #include "diagnostics/diagram.h"
24 : #include "diagnostics/sarif-sink.h"
25 :
26 : #include "analyzer/analyzer-logging.h"
27 : #include "analyzer/region-model.h"
28 : #include "analyzer/checker-event.h"
29 : #include "analyzer/checker-path.h"
30 : #include "analyzer/access-diagram.h"
31 :
32 : #if ENABLE_ANALYZER
33 :
34 : namespace ana {
35 :
36 : /* Abstract base class for all out-of-bounds warnings. */
37 :
38 : class out_of_bounds : public pending_diagnostic
39 : {
40 : public:
41 : class oob_region_creation_event_capacity : public region_creation_event_capacity
42 : {
43 : public:
44 506 : oob_region_creation_event_capacity (tree byte_capacity,
45 : const event_loc_info &loc_info,
46 : out_of_bounds &oob)
47 : : region_creation_event_capacity (byte_capacity,
48 : loc_info),
49 506 : m_oob (oob)
50 : {
51 : }
52 506 : void prepare_for_emission (checker_path *path,
53 : pending_diagnostic *pd,
54 : diagnostics::paths::event_id_t emission_id) override
55 : {
56 506 : region_creation_event_capacity::prepare_for_emission (path,
57 : pd,
58 : emission_id);
59 506 : m_oob.m_region_creation_event_id = emission_id;
60 506 : }
61 : private:
62 : out_of_bounds &m_oob;
63 : };
64 :
65 875 : out_of_bounds (const region_model &model,
66 : const symbolic::memory_range &access_range,
67 : tree diag_arg,
68 : const svalue *sval_hint)
69 875 : : m_model (model),
70 875 : m_access_range (access_range),
71 875 : m_diag_arg (diag_arg),
72 714 : m_sval_hint (sval_hint)
73 : {}
74 :
75 824 : bool subclass_equal_p (const pending_diagnostic &base_other) const override
76 : {
77 824 : const out_of_bounds &other
78 : (static_cast <const out_of_bounds &>(base_other));
79 824 : return (m_access_range == other.m_access_range
80 824 : && pending_diagnostic::same_tree_p (m_diag_arg, other.m_diag_arg));
81 : }
82 :
83 1489 : int get_controlling_option () const final override
84 : {
85 1489 : return OPT_Wanalyzer_out_of_bounds;
86 : }
87 :
88 1274 : void mark_interesting_stuff (interesting_t *interest) final override
89 : {
90 1274 : interest->add_region_creation (&m_access_range.get_base_region ());
91 1274 : }
92 :
93 234 : void add_region_creation_events (const region *,
94 : tree byte_capacity,
95 : const event_loc_info &loc_info,
96 : checker_path &emission_path) override
97 : {
98 : /* The memory space is described in the diagnostic message itself,
99 : so we don't need an event for that. */
100 234 : if (byte_capacity)
101 144 : emission_path.add_event
102 144 : (std::make_unique<oob_region_creation_event_capacity> (byte_capacity,
103 : loc_info,
104 : *this));
105 234 : }
106 :
107 : void
108 4 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
109 : const override
110 : {
111 4 : auto &props = result_obj.get_or_create_properties ();
112 : #define PROPERTY_PREFIX "gcc/analyzer/out_of_bounds/"
113 8 : props.set_string (PROPERTY_PREFIX "dir",
114 4 : get_dir () == access_direction::read ? "read" : "write");
115 4 : props.set (PROPERTY_PREFIX "model", m_model.to_json ());
116 4 : props.set (PROPERTY_PREFIX "access_range", m_access_range.to_json ());
117 4 : props.set (PROPERTY_PREFIX "diag_arg", tree_to_json (m_diag_arg));
118 4 : if (m_sval_hint)
119 4 : props.set (PROPERTY_PREFIX "sval_hint", m_sval_hint->to_json ());
120 8 : props.set (PROPERTY_PREFIX "region_creation_event_id",
121 4 : diagnostic_event_id_to_json (m_region_creation_event_id));
122 : #undef PROPERTY_PREFIX
123 4 : }
124 :
125 : virtual enum access_direction get_dir () const = 0;
126 :
127 : protected:
128 : memory_space
129 637 : get_memory_space () const
130 : {
131 637 : return m_access_range.get_base_region ().get_memory_space ();
132 : }
133 :
134 : void
135 637 : maybe_show_notes (diagnostic_emission_context &ctxt) const
136 : {
137 637 : maybe_describe_array_bounds (ctxt.get_location ());
138 637 : maybe_show_diagram (ctxt.get_logger ());
139 637 : }
140 :
141 : /* Potentially add a note about valid ways to index this array, such
142 : as (given "int arr[10];"):
143 : note: valid subscripts for 'arr' are '[0]' to '[9]'
144 : We print the '[' and ']' characters so as to express the valid
145 : subscripts using C syntax, rather than just as byte ranges,
146 : which hopefully is more clear to the user. */
147 : void
148 637 : maybe_describe_array_bounds (location_t loc) const
149 : {
150 637 : if (!m_diag_arg)
151 : return;
152 455 : tree t = TREE_TYPE (m_diag_arg);
153 455 : if (!t)
154 : return;
155 455 : if (TREE_CODE (t) != ARRAY_TYPE)
156 : return;
157 364 : tree domain = TYPE_DOMAIN (t);
158 364 : if (!domain)
159 : return;
160 363 : tree max_idx = TYPE_MAX_VALUE (domain);
161 363 : if (!max_idx)
162 : return;
163 361 : tree min_idx = TYPE_MIN_VALUE (domain);
164 361 : inform (loc,
165 : "valid subscripts for %qE are %<[%E]%> to %<[%E]%>",
166 : m_diag_arg, min_idx, max_idx);
167 : }
168 :
169 : void
170 637 : maybe_show_diagram (logger *logger) const
171 : {
172 637 : access_operation op (m_model, get_dir (), m_access_range, m_sval_hint);
173 :
174 : /* Don't attempt to make a diagram if there's no valid way of
175 : accessing the base region (e.g. a 0-element array). */
176 637 : if (op.get_valid_bits ().get_size ().is_zero_p ())
177 4 : return;
178 :
179 633 : if (const text_art::theme *theme = global_dc->get_diagram_theme ())
180 : {
181 72 : text_art::style_manager sm;
182 72 : text_art::canvas canvas (make_access_diagram (op, sm, *theme, logger));
183 72 : if (canvas.get_size ().w == 0 && canvas.get_size ().h == 0)
184 : {
185 : /* In lieu of exceptions, return a zero-sized diagram if there's
186 : a problem. Give up if that's happened. */
187 0 : return;
188 : }
189 72 : diagnostics::diagram diagram
190 : (canvas,
191 : /* Alt text. */
192 72 : _("Diagram visualizing the predicted out-of-bounds access"));
193 72 : global_dc->emit_diagram (diagram);
194 72 : }
195 : }
196 :
197 : text_art::canvas
198 72 : make_access_diagram (const access_operation &op,
199 : text_art::style_manager &sm,
200 : const text_art::theme &theme,
201 : logger *logger) const
202 : {
203 72 : access_diagram d (op, m_region_creation_event_id, sm, theme, logger);
204 72 : return d.to_canvas (sm);
205 72 : }
206 :
207 : region_model m_model;
208 : symbolic::memory_range m_access_range;
209 : tree m_diag_arg;
210 : const svalue *m_sval_hint;
211 : diagnostics::paths::event_id_t m_region_creation_event_id;
212 : };
213 :
214 : /* Abstract base class for all out-of-bounds warnings where the
215 : out-of-bounds range is concrete. */
216 :
217 : class concrete_out_of_bounds : public out_of_bounds
218 : {
219 : public:
220 714 : concrete_out_of_bounds (const region_model &model,
221 : const symbolic::memory_range &access_range,
222 : tree diag_arg,
223 : concrete_bit_range out_of_bounds_bits,
224 : const svalue *sval_hint)
225 : : out_of_bounds (model, access_range, diag_arg, sval_hint),
226 714 : m_out_of_bounds_bits (out_of_bounds_bits)
227 714 : {}
228 :
229 667 : bool subclass_equal_p (const pending_diagnostic &base_other) const override
230 : {
231 667 : const concrete_out_of_bounds &other
232 : (static_cast <const concrete_out_of_bounds &>(base_other));
233 667 : return (out_of_bounds::subclass_equal_p (other)
234 667 : && m_out_of_bounds_bits == other.m_out_of_bounds_bits);
235 : }
236 :
237 4 : void maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
238 : const override
239 : {
240 4 : out_of_bounds::maybe_add_sarif_properties (result_obj);
241 4 : auto &props = result_obj.get_or_create_properties ();
242 : #define PROPERTY_PREFIX "gcc/analyzer/concrete_out_of_bounds/"
243 8 : props.set (PROPERTY_PREFIX "out_of_bounds_bits",
244 4 : m_out_of_bounds_bits.to_json ());
245 : #undef PROPERTY_PREFIX
246 4 : }
247 :
248 986 : bool get_out_of_bounds_bytes (concrete_byte_range *out) const
249 : {
250 986 : return maybe_bytes_from_bits (m_out_of_bounds_bits, out);
251 : }
252 :
253 : protected:
254 : concrete_bit_range m_out_of_bounds_bits;
255 : };
256 :
257 : /* Abstract subclass to complain about concrete out-of-bounds
258 : past the end of the buffer. */
259 :
260 : class concrete_past_the_end : public concrete_out_of_bounds
261 : {
262 : public:
263 505 : concrete_past_the_end (const region_model &model,
264 : const symbolic::memory_range &access_range,tree diag_arg,
265 : concrete_bit_range bad_range,
266 : concrete_bit_size conc_bit_capacity,
267 : const svalue *sval_hint)
268 : : concrete_out_of_bounds (model, access_range, diag_arg, bad_range, sval_hint),
269 1010 : m_conc_bit_capacity (conc_bit_capacity),
270 1010 : m_conc_byte_capacity (nullptr)
271 : {
272 505 : concrete_byte_size bytes;
273 505 : if (maybe_bytes_from_bits (m_conc_bit_capacity, &bytes))
274 505 : m_conc_byte_capacity
275 505 : = std::make_unique<concrete_byte_size> (bytes);
276 505 : }
277 :
278 : bool
279 479 : subclass_equal_p (const pending_diagnostic &base_other) const final override
280 : {
281 479 : const concrete_past_the_end &other
282 : (static_cast <const concrete_past_the_end &>(base_other));
283 479 : return (concrete_out_of_bounds::subclass_equal_p (other)
284 479 : && m_conc_bit_capacity == other.m_conc_bit_capacity);
285 : }
286 :
287 362 : void add_region_creation_events (const region *,
288 : tree,
289 : const event_loc_info &loc_info,
290 : checker_path &emission_path) final override
291 : {
292 362 : if (m_conc_byte_capacity)
293 : {
294 362 : tree byte_bound
295 362 : = make_integer_cst_from_integer (size_type_node,
296 : m_conc_byte_capacity->get_integer (),
297 362 : true);
298 362 : emission_path.add_event
299 362 : (std::make_unique<oob_region_creation_event_capacity> (byte_bound,
300 : loc_info,
301 : *this));
302 : }
303 362 : }
304 :
305 4 : void maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
306 : const final override
307 : {
308 4 : concrete_out_of_bounds::maybe_add_sarif_properties (result_obj);
309 4 : auto &props = result_obj.get_or_create_properties ();
310 : #define PROPERTY_PREFIX "gcc/analyzer/concrete_past_the_end/"
311 8 : props.set (PROPERTY_PREFIX "conc_bit_capacity",
312 4 : m_conc_bit_capacity.to_json ());
313 4 : if (m_conc_byte_capacity)
314 8 : props.set (PROPERTY_PREFIX "conc_byte_capacity",
315 4 : m_conc_byte_capacity->to_json ());
316 : #undef PROPERTY_PREFIX
317 4 : }
318 :
319 : protected:
320 : concrete_bit_size m_conc_bit_capacity;
321 : std::unique_ptr<concrete_byte_size> m_conc_byte_capacity;
322 : };
323 :
324 : /* Concrete subclass to complain about buffer overflows. */
325 :
326 : class concrete_buffer_overflow : public concrete_past_the_end
327 : {
328 : public:
329 212 : concrete_buffer_overflow (const region_model &model,
330 : const symbolic::memory_range &access_range,
331 : tree diag_arg,
332 : concrete_bit_range bad_range,
333 : concrete_bit_size conc_bit_capacity,
334 : const svalue *sval_hint)
335 : : concrete_past_the_end (model, access_range, diag_arg, bad_range,
336 424 : conc_bit_capacity, sval_hint)
337 212 : {}
338 :
339 2426 : const char *get_kind () const final override
340 : {
341 2426 : return "concrete_buffer_overflow";
342 : }
343 :
344 176 : bool emit (diagnostic_emission_context &ctxt) final override
345 : {
346 176 : bool warned;
347 176 : switch (get_memory_space ())
348 : {
349 70 : default:
350 70 : ctxt.add_cwe (787);
351 70 : warned = ctxt.warn ("buffer overflow");
352 70 : break;
353 73 : case memory_space::stack:
354 73 : ctxt.add_cwe (121);
355 73 : warned = ctxt.warn ("stack-based buffer overflow");
356 73 : break;
357 33 : case memory_space::heap:
358 33 : ctxt.add_cwe (122);
359 33 : warned = ctxt.warn ("heap-based buffer overflow");
360 33 : break;
361 : }
362 :
363 176 : if (warned)
364 : {
365 176 : auto num_bad_bits = m_out_of_bounds_bits.get_size ();
366 176 : concrete_byte_size num_bad_bytes;
367 176 : if (maybe_bytes_from_bits (num_bad_bits, &num_bad_bytes))
368 : {
369 172 : pp_element_integer e_num_bad_bytes (num_bad_bytes.get_integer ());
370 172 : if (m_diag_arg)
371 280 : inform_n (ctxt.get_location (),
372 140 : (size_t)num_bad_bytes.as_unsigned_long (),
373 : "write of %e byte to beyond the end of %qE",
374 : "write of %e bytes to beyond the end of %qE",
375 : &e_num_bad_bytes,
376 : m_diag_arg);
377 : else
378 64 : inform_n (ctxt.get_location (),
379 32 : (size_t)num_bad_bytes.as_unsigned_long (),
380 : "write of %e byte to beyond the end of the region",
381 : "write of %e bytes to beyond the end of the region",
382 : &e_num_bad_bytes);
383 172 : }
384 : else
385 : {
386 4 : pp_element_integer e_num_bad_bits (num_bad_bits.get_integer ());
387 4 : if (m_diag_arg)
388 0 : inform_n (ctxt.get_location (),
389 0 : (size_t)num_bad_bits.as_unsigned_long (),
390 : "write of %e bit to beyond the end of %qE",
391 : "write of %e bits to beyond the end of %qE",
392 : &e_num_bad_bits,
393 : m_diag_arg);
394 : else
395 8 : inform_n (ctxt.get_location (),
396 4 : (size_t)num_bad_bits.as_unsigned_long (),
397 : "write of %e bit to beyond the end of the region",
398 : "write of %e bits to beyond the end of the region",
399 : &e_num_bad_bits);
400 4 : }
401 :
402 176 : maybe_show_notes (ctxt);
403 176 : }
404 :
405 176 : return warned;
406 : }
407 :
408 : bool
409 352 : describe_final_event (pretty_printer &pp,
410 : const evdesc::final_event &) final override
411 : {
412 352 : if (m_conc_byte_capacity)
413 : {
414 352 : concrete_byte_range out_of_bounds_bytes;
415 352 : if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
416 : {
417 344 : describe_final_event_as_bytes (pp, out_of_bounds_bytes);
418 344 : return true;
419 : }
420 352 : }
421 8 : describe_final_event_as_bits (pp);
422 8 : return true;
423 : }
424 :
425 : void
426 344 : describe_final_event_as_bytes (pretty_printer &pp,
427 : const concrete_byte_range &out_of_bounds_bytes)
428 : {
429 344 : gcc_assert (m_conc_byte_capacity);
430 :
431 344 : auto start = out_of_bounds_bytes.get_start_offset ();
432 344 : auto last = out_of_bounds_bytes.get_last_offset ();
433 :
434 344 : pp_element_integer e_start (start.get_integer ());
435 344 : pp_element_integer e_byte_capacity (m_conc_byte_capacity->get_integer ());
436 344 : if (start == last)
437 : {
438 84 : if (m_diag_arg)
439 56 : pp_printf (&pp,
440 : "out-of-bounds write at byte %e but %qE"
441 : " ends at byte %e", &e_start, m_diag_arg,
442 : &e_byte_capacity);
443 : else
444 28 : pp_printf (&pp,
445 : "out-of-bounds write at byte %e but region"
446 : " ends at byte %e", &e_start,
447 : &e_byte_capacity);
448 : }
449 : else
450 : {
451 260 : pp_element_integer e_last (last.get_integer ());
452 260 : if (m_diag_arg)
453 224 : pp_printf (&pp,
454 : "out-of-bounds write from byte %e till"
455 : " byte %e but %qE ends at byte %e",
456 : &e_start, &e_last, m_diag_arg,
457 : &e_byte_capacity);
458 : else
459 36 : pp_printf (&pp,
460 : "out-of-bounds write from byte %e till"
461 : " byte %e but region ends at byte %e",
462 : &e_start, &e_last, &e_byte_capacity);
463 260 : }
464 344 : }
465 :
466 : void
467 8 : describe_final_event_as_bits (pretty_printer &pp)
468 : {
469 8 : auto start = m_out_of_bounds_bits.get_start_offset ();
470 8 : auto last = m_out_of_bounds_bits.get_last_offset ();
471 :
472 8 : pp_element_integer e_start (start.get_integer ());
473 8 : pp_element_integer e_bit_capacity (m_conc_bit_capacity.get_integer ());
474 8 : if (start == last)
475 : {
476 0 : if (m_diag_arg)
477 0 : pp_printf (&pp,
478 : "out-of-bounds write at bit %e but %qE"
479 : " ends at bit %e", &e_start, m_diag_arg,
480 : &e_bit_capacity);
481 : else
482 0 : pp_printf (&pp,
483 : "out-of-bounds write at bit %e but region"
484 : " ends at bit %e", &e_start,
485 : &e_bit_capacity);
486 : }
487 : else
488 : {
489 8 : pp_element_integer e_last (last.get_integer ());
490 8 : if (m_diag_arg)
491 0 : pp_printf (&pp,
492 : "out-of-bounds write from bit %e till"
493 : " bit %e but %qE ends at bit %e",
494 : &e_start, &e_last, m_diag_arg,
495 : &e_bit_capacity);
496 : else
497 8 : pp_printf (&pp,
498 : "out-of-bounds write from bit %e till"
499 : " bit %e but region ends at bit %e",
500 : &e_start, &e_last, &e_bit_capacity);
501 8 : }
502 8 : }
503 :
504 : enum access_direction
505 180 : get_dir () const final override { return access_direction::write; }
506 : };
507 :
508 : /* Concrete subclass to complain about buffer over-reads. */
509 :
510 : class concrete_buffer_over_read : public concrete_past_the_end
511 : {
512 : public:
513 293 : concrete_buffer_over_read (const region_model &model,
514 : const symbolic::memory_range &access_range,
515 : tree diag_arg,
516 : concrete_bit_range bad_range,
517 : concrete_bit_size conc_bit_capacity)
518 : : concrete_past_the_end (model, access_range, diag_arg,
519 586 : bad_range, conc_bit_capacity, nullptr)
520 293 : {}
521 :
522 2493 : const char *get_kind () const final override
523 : {
524 2493 : return "concrete_buffer_over_read";
525 : }
526 :
527 189 : bool emit (diagnostic_emission_context &ctxt) final override
528 : {
529 189 : bool warned;
530 189 : ctxt.add_cwe (126);
531 189 : switch (get_memory_space ())
532 : {
533 60 : default:
534 60 : warned = ctxt.warn ("buffer over-read");
535 60 : break;
536 112 : case memory_space::stack:
537 112 : warned = ctxt.warn ("stack-based buffer over-read");
538 112 : break;
539 17 : case memory_space::heap:
540 17 : warned = ctxt.warn ("heap-based buffer over-read");
541 17 : break;
542 : }
543 :
544 189 : if (warned)
545 : {
546 189 : auto num_bad_bits = m_out_of_bounds_bits.get_size ();
547 189 : concrete_byte_size num_bad_bytes;
548 189 : if (maybe_bytes_from_bits (num_bad_bits, &num_bad_bytes))
549 : {
550 185 : pp_element_integer e_num_bad_bytes (num_bad_bytes.get_integer ());
551 185 : if (m_diag_arg)
552 344 : inform_n (ctxt.get_location (),
553 172 : (size_t)num_bad_bytes.as_unsigned_long (),
554 : "read of %e byte from after the end of %qE",
555 : "read of %e bytes from after the end of %qE",
556 : &e_num_bad_bytes,
557 : m_diag_arg);
558 : else
559 26 : inform_n (ctxt.get_location (),
560 13 : (size_t)num_bad_bytes.as_unsigned_long (),
561 : "read of %e byte from after the end of the region",
562 : "read of %e bytes from after the end of the region",
563 : &e_num_bad_bytes);
564 185 : }
565 : else
566 : {
567 4 : pp_element_integer e_num_bad_bits (num_bad_bits.get_integer ());
568 4 : if (m_diag_arg)
569 0 : inform_n (ctxt.get_location (),
570 0 : (size_t)num_bad_bits.as_unsigned_long (),
571 : "read of %e bit from after the end of %qE",
572 : "read of %e bits from after the end of %qE",
573 : &e_num_bad_bits,
574 : m_diag_arg);
575 : else
576 8 : inform_n (ctxt.get_location (),
577 4 : (size_t)num_bad_bits.as_unsigned_long (),
578 : "read of %e bit from after the end of the region",
579 : "read of %e bits from after the end of the region",
580 : &e_num_bad_bits);
581 4 : }
582 :
583 189 : maybe_show_notes (ctxt);
584 189 : }
585 :
586 189 : return warned;
587 : }
588 :
589 : bool
590 378 : describe_final_event (pretty_printer &pp,
591 : const evdesc::final_event &) final override
592 : {
593 378 : if (m_conc_byte_capacity)
594 : {
595 378 : concrete_byte_range out_of_bounds_bytes;
596 378 : if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
597 : {
598 370 : describe_final_event_as_bytes (pp, out_of_bounds_bytes);
599 370 : return true;
600 : }
601 378 : }
602 8 : describe_final_event_as_bits (pp);
603 8 : return true;
604 : }
605 :
606 : void
607 370 : describe_final_event_as_bytes (pretty_printer &pp,
608 : const concrete_byte_range &out_of_bounds_bytes)
609 : {
610 370 : gcc_assert (m_conc_byte_capacity);
611 :
612 370 : auto start = out_of_bounds_bytes.get_start_offset ();
613 370 : auto last = out_of_bounds_bytes.get_last_offset ();
614 :
615 370 : pp_element_integer e_start (start.get_integer ());
616 370 : pp_element_integer e_byte_capacity (m_conc_byte_capacity->get_integer ());
617 :
618 370 : if (start == last)
619 : {
620 234 : if (m_diag_arg)
621 218 : pp_printf (&pp,
622 : "out-of-bounds read at byte %e but %qE"
623 : " ends at byte %e", &e_start, m_diag_arg,
624 : &e_byte_capacity);
625 : else
626 16 : pp_printf (&pp,
627 : "out-of-bounds read at byte %e but region"
628 : " ends at byte %e", &e_start,
629 : &e_byte_capacity);
630 : }
631 : else
632 : {
633 136 : pp_element_integer e_last (last.get_integer ());
634 136 : if (m_diag_arg)
635 126 : pp_printf (&pp,
636 : "out-of-bounds read from byte %e till"
637 : " byte %e but %qE ends at byte %e",
638 : &e_start, &e_last, m_diag_arg,
639 : &e_byte_capacity);
640 : else
641 10 : pp_printf (&pp,
642 : "out-of-bounds read from byte %e till"
643 : " byte %e but region ends at byte %e",
644 : &e_start, &e_last, &e_byte_capacity);
645 136 : }
646 370 : }
647 :
648 : void
649 8 : describe_final_event_as_bits (pretty_printer &pp)
650 : {
651 8 : auto start = m_out_of_bounds_bits.get_start_offset ();
652 8 : auto last = m_out_of_bounds_bits.get_last_offset ();
653 :
654 8 : pp_element_integer e_start (start.get_integer ());
655 8 : pp_element_integer e_bit_capacity (m_conc_bit_capacity.get_integer ());
656 :
657 8 : if (start == last)
658 : {
659 0 : if (m_diag_arg)
660 0 : pp_printf (&pp,
661 : "out-of-bounds read at bit %e but %qE"
662 : " ends at bit %e", &e_start, m_diag_arg,
663 : &e_bit_capacity);
664 : else
665 0 : pp_printf (&pp,
666 : "out-of-bounds read at bit %e but region"
667 : " ends at bit %e", &e_start,
668 : &e_bit_capacity);
669 : }
670 : else
671 : {
672 8 : pp_element_integer e_last (last.get_integer ());
673 8 : if (m_diag_arg)
674 0 : pp_printf (&pp,
675 : "out-of-bounds read from bit %e till"
676 : " bit %e but %qE ends at bit %e",
677 : &e_start, &e_last, m_diag_arg,
678 : &e_bit_capacity);
679 : else
680 8 : pp_printf (&pp,
681 : "out-of-bounds read from bit %e till"
682 : " bit %e but region ends at bit %e",
683 : &e_start, &e_last, &e_bit_capacity);
684 8 : }
685 8 : }
686 :
687 : enum access_direction
688 189 : get_dir () const final override { return access_direction::read; }
689 : };
690 :
691 : /* Concrete subclass to complain about buffer underwrites. */
692 :
693 : class concrete_buffer_underwrite : public concrete_out_of_bounds
694 : {
695 : public:
696 66 : concrete_buffer_underwrite (const region_model &model,
697 : const symbolic::memory_range &access_range,
698 : tree diag_arg,
699 : concrete_bit_range bad_range,
700 : const svalue *sval_hint)
701 : : concrete_out_of_bounds (model, access_range, diag_arg,
702 66 : bad_range, sval_hint)
703 66 : {}
704 :
705 658 : const char *get_kind () const final override
706 : {
707 658 : return "concrete_buffer_underwrite";
708 : }
709 :
710 62 : bool emit (diagnostic_emission_context &ctxt) final override
711 : {
712 62 : bool warned;
713 62 : ctxt.add_cwe (124);
714 62 : switch (get_memory_space ())
715 : {
716 40 : default:
717 40 : warned = ctxt.warn ("buffer underwrite");
718 40 : break;
719 14 : case memory_space::stack:
720 14 : warned = ctxt.warn ("stack-based buffer underwrite");
721 14 : break;
722 8 : case memory_space::heap:
723 8 : warned = ctxt.warn ("heap-based buffer underwrite");
724 8 : break;
725 : }
726 62 : if (warned)
727 62 : maybe_show_notes (ctxt);
728 62 : return warned;
729 : }
730 :
731 : bool
732 124 : describe_final_event (pretty_printer &pp,
733 : const evdesc::final_event &) final override
734 : {
735 124 : concrete_byte_range out_of_bounds_bytes;
736 124 : if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
737 116 : describe_final_event_as_bytes (pp, out_of_bounds_bytes);
738 : else
739 8 : describe_final_event_as_bits (pp);
740 124 : return true;
741 124 : }
742 :
743 : void
744 116 : describe_final_event_as_bytes (pretty_printer &pp,
745 : const concrete_byte_range &out_of_bounds_bytes)
746 : {
747 116 : auto start = out_of_bounds_bytes.get_start_offset ();
748 116 : auto last = out_of_bounds_bytes.get_last_offset ();
749 :
750 116 : pp_element_integer e_start (start.get_integer ());
751 :
752 116 : if (start == last)
753 : {
754 24 : if (m_diag_arg)
755 24 : pp_printf (&pp,
756 : "out-of-bounds write at byte %e but %qE"
757 : " starts at byte 0",
758 : &e_start, m_diag_arg);
759 : else
760 0 : pp_printf (&pp,
761 : "out-of-bounds write at byte %e but region"
762 : " starts at byte 0", &e_start);
763 : }
764 : else
765 : {
766 92 : pp_element_integer e_last (last.get_integer ());
767 92 : if (m_diag_arg)
768 82 : pp_printf (&pp,
769 : "out-of-bounds write from byte %e till"
770 : " byte %e but %qE starts at byte 0",
771 : &e_start, &e_last, m_diag_arg);
772 : else
773 10 : pp_printf (&pp,
774 : "out-of-bounds write from byte %e till"
775 : " byte %e but region starts at byte 0",
776 : &e_start, &e_last);
777 92 : }
778 116 : }
779 :
780 : void
781 8 : describe_final_event_as_bits (pretty_printer &pp)
782 : {
783 8 : auto start = m_out_of_bounds_bits.get_start_offset ();
784 8 : auto last = m_out_of_bounds_bits.get_last_offset ();
785 :
786 8 : pp_element_integer e_start (start.get_integer ());
787 8 : if (start == last)
788 : {
789 0 : if (m_diag_arg)
790 0 : pp_printf (&pp,
791 : "out-of-bounds write at bit %e but %qE"
792 : " starts at bit 0",
793 : &e_start, m_diag_arg);
794 : else
795 0 : pp_printf (&pp,
796 : "out-of-bounds write at bit %e but region"
797 : " starts at bit 0", &e_start);
798 : }
799 : else
800 : {
801 8 : pp_element_integer e_last (last.get_integer ());
802 8 : if (m_diag_arg)
803 0 : pp_printf (&pp,
804 : "out-of-bounds write from bit %e till"
805 : " bit %e but %qE starts at bit 0",
806 : &e_start, &e_last, m_diag_arg);
807 : else
808 8 : pp_printf (&pp,
809 : "out-of-bounds write from bit %e till"
810 : " bit %e but region starts at bit 0",
811 : &e_start, &e_last);
812 8 : }
813 8 : }
814 :
815 : enum access_direction
816 62 : get_dir () const final override { return access_direction::write; }
817 : };
818 :
819 : /* Concrete subclass to complain about buffer under-reads. */
820 :
821 : class concrete_buffer_under_read : public concrete_out_of_bounds
822 : {
823 : public:
824 143 : concrete_buffer_under_read (const region_model &model,
825 : const symbolic::memory_range &access_range,
826 : tree diag_arg,
827 : concrete_bit_range bad_range)
828 143 : : concrete_out_of_bounds (model, access_range, diag_arg, bad_range, nullptr)
829 143 : {}
830 :
831 1063 : const char *get_kind () const final override
832 : {
833 1063 : return "concrete_buffer_under_read";
834 : }
835 :
836 66 : bool emit (diagnostic_emission_context &ctxt) final override
837 : {
838 66 : bool warned;
839 66 : ctxt.add_cwe (127);
840 66 : switch (get_memory_space ())
841 : {
842 44 : default:
843 44 : warned = ctxt.warn ("buffer under-read");
844 44 : break;
845 14 : case memory_space::stack:
846 14 : warned = ctxt.warn ("stack-based buffer under-read");
847 14 : break;
848 8 : case memory_space::heap:
849 8 : warned = ctxt.warn ("heap-based buffer under-read");
850 8 : break;
851 : }
852 66 : if (warned)
853 66 : maybe_show_notes (ctxt);
854 66 : return warned;
855 : }
856 :
857 : bool
858 132 : describe_final_event (pretty_printer &pp,
859 : const evdesc::final_event &) final override
860 : {
861 132 : concrete_byte_range out_of_bounds_bytes;
862 132 : if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
863 124 : describe_final_event_as_bytes (pp, out_of_bounds_bytes);
864 : else
865 8 : describe_final_event_as_bits (pp);
866 132 : return true;
867 132 : }
868 :
869 : void
870 124 : describe_final_event_as_bytes (pretty_printer &pp,
871 : const concrete_byte_range &out_of_bounds_bytes)
872 : {
873 124 : auto start = out_of_bounds_bytes.get_start_offset ();
874 124 : auto last = out_of_bounds_bytes.get_last_offset ();
875 :
876 124 : pp_element_integer e_start (start.get_integer ());
877 124 : if (start == last)
878 : {
879 34 : if (m_diag_arg)
880 34 : pp_printf (&pp,
881 : "out-of-bounds read at byte %e but %qE"
882 : " starts at byte 0",
883 : &e_start, m_diag_arg);
884 : else
885 0 : pp_printf (&pp,
886 : "out-of-bounds read at byte %e but region"
887 : " starts at byte 0",
888 : &e_start);
889 : }
890 : else
891 : {
892 90 : pp_element_integer e_last (last.get_integer ());
893 90 : if (m_diag_arg)
894 82 : pp_printf (&pp,
895 : "out-of-bounds read from byte %e till"
896 : " byte %e but %qE starts at byte 0",
897 : &e_start, &e_last, m_diag_arg);
898 : else
899 8 : pp_printf (&pp,
900 : "out-of-bounds read from byte %e till"
901 : " byte %e but region starts at byte 0",
902 : &e_start, &e_last);
903 90 : }
904 124 : }
905 :
906 : void
907 8 : describe_final_event_as_bits (pretty_printer &pp)
908 : {
909 8 : auto start = m_out_of_bounds_bits.get_start_offset ();
910 8 : auto last = m_out_of_bounds_bits.get_last_offset ();
911 :
912 8 : pp_element_integer e_start (start.get_integer ());
913 :
914 8 : if (start == last)
915 : {
916 0 : if (m_diag_arg)
917 0 : pp_printf (&pp,
918 : "out-of-bounds read at bit %e but %qE"
919 : " starts at bit 0",
920 : &e_start, m_diag_arg);
921 : else
922 0 : pp_printf (&pp,
923 : "out-of-bounds read at bit %e but region"
924 : " starts at bit 0", &e_start);
925 : }
926 : else
927 : {
928 8 : pp_element_integer e_last (last.get_integer ());
929 8 : if (m_diag_arg)
930 0 : pp_printf (&pp,
931 : "out-of-bounds read from bit %e till"
932 : " bit %e but %qE starts at bit 0",
933 : &e_start, &e_last, m_diag_arg);
934 : else
935 8 : pp_printf (&pp,
936 : "out-of-bounds read from bit %e till"
937 : " bit %e but region starts at bit 0",
938 : &e_start, &e_last);
939 8 : }
940 8 : }
941 :
942 : enum access_direction
943 66 : get_dir () const final override { return access_direction::read; }
944 : };
945 :
946 : /* Abstract class to complain about out-of-bounds read/writes where
947 : the values are symbolic. */
948 :
949 : class symbolic_past_the_end : public out_of_bounds
950 : {
951 : public:
952 161 : symbolic_past_the_end (const region_model &model,
953 : const symbolic::memory_range &access_range,
954 : tree diag_arg,
955 : tree offset,
956 : tree num_bytes,
957 : tree capacity,
958 : const svalue *sval_hint)
959 : : out_of_bounds (model, access_range, diag_arg, sval_hint),
960 161 : m_offset (offset),
961 161 : m_num_bytes (num_bytes),
962 322 : m_capacity (capacity)
963 : {}
964 :
965 : bool
966 157 : subclass_equal_p (const pending_diagnostic &base_other) const final override
967 : {
968 157 : const symbolic_past_the_end &other
969 : (static_cast <const symbolic_past_the_end &>(base_other));
970 157 : return (out_of_bounds::subclass_equal_p (other)
971 157 : && pending_diagnostic::same_tree_p (m_offset, other.m_offset)
972 157 : && pending_diagnostic::same_tree_p (m_num_bytes, other.m_num_bytes)
973 314 : && pending_diagnostic::same_tree_p (m_capacity, other.m_capacity));
974 : }
975 :
976 : void
977 0 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
978 : const final override
979 : {
980 0 : out_of_bounds::maybe_add_sarif_properties (result_obj);
981 0 : auto &props = result_obj.get_or_create_properties ();
982 : #define PROPERTY_PREFIX "gcc/analyzer/symbolic_past_the_end/"
983 0 : props.set (PROPERTY_PREFIX "offset", tree_to_json (m_offset));
984 0 : props.set (PROPERTY_PREFIX "num_bytes", tree_to_json (m_num_bytes));
985 0 : props.set (PROPERTY_PREFIX "capacity", tree_to_json (m_capacity));
986 : #undef PROPERTY_PREFIX
987 0 : }
988 :
989 : protected:
990 : tree m_offset;
991 : tree m_num_bytes;
992 : tree m_capacity;
993 : };
994 :
995 : /* Concrete subclass to complain about overflows with symbolic values. */
996 :
997 : class symbolic_buffer_overflow : public symbolic_past_the_end
998 : {
999 : public:
1000 120 : symbolic_buffer_overflow (const region_model &model,
1001 : const symbolic::memory_range &access_range,
1002 : tree diag_arg,
1003 : tree offset,
1004 : tree num_bytes,
1005 : tree capacity,
1006 : const svalue *sval_hint)
1007 : : symbolic_past_the_end (model, access_range, diag_arg,
1008 : offset, num_bytes,
1009 : capacity,
1010 120 : sval_hint)
1011 : {
1012 : }
1013 :
1014 1905 : const char *get_kind () const final override
1015 : {
1016 1905 : return "symbolic_buffer_overflow";
1017 : }
1018 :
1019 119 : bool emit (diagnostic_emission_context &ctxt) final override
1020 : {
1021 119 : bool warned;
1022 119 : switch (get_memory_space ())
1023 : {
1024 40 : default:
1025 40 : ctxt.add_cwe (787);
1026 40 : warned = ctxt.warn ("buffer overflow");
1027 40 : break;
1028 34 : case memory_space::stack:
1029 34 : ctxt.add_cwe (121);
1030 34 : warned = ctxt.warn ("stack-based buffer overflow");
1031 34 : break;
1032 45 : case memory_space::heap:
1033 45 : ctxt.add_cwe (122);
1034 45 : warned = ctxt.warn ("heap-based buffer overflow");
1035 45 : break;
1036 : }
1037 119 : if (warned)
1038 119 : maybe_show_notes (ctxt);
1039 119 : return warned;
1040 : }
1041 :
1042 : bool
1043 238 : describe_final_event (pretty_printer &pp,
1044 : const evdesc::final_event &) final override
1045 : {
1046 238 : if (m_offset)
1047 : {
1048 : /* Known offset. */
1049 204 : if (m_num_bytes)
1050 : {
1051 : /* Known offset, known size. */
1052 198 : if (TREE_CODE (m_num_bytes) == INTEGER_CST)
1053 : {
1054 : /* Known offset, known constant size. */
1055 164 : if (pending_diagnostic::same_tree_p (m_num_bytes,
1056 : integer_one_node))
1057 : {
1058 : /* Singular m_num_bytes. */
1059 120 : if (m_diag_arg)
1060 32 : pp_printf (&pp,
1061 : "write of %E byte at offset %qE exceeds %qE",
1062 : m_num_bytes, m_offset, m_diag_arg);
1063 : else
1064 88 : pp_printf (&pp,
1065 : "write of %E byte at offset %qE exceeds"
1066 : " the buffer",
1067 : m_num_bytes, m_offset);
1068 : }
1069 : else
1070 : {
1071 : /* Plural m_num_bytes. */
1072 44 : if (m_diag_arg)
1073 8 : pp_printf (&pp,
1074 : "write of %E bytes at offset %qE exceeds %qE",
1075 : m_num_bytes, m_offset, m_diag_arg);
1076 : else
1077 36 : pp_printf (&pp,
1078 : "write of %E bytes at offset %qE exceeds"
1079 : " the buffer",
1080 : m_num_bytes, m_offset);
1081 : }
1082 : }
1083 : else
1084 : {
1085 : /* Known offset, known symbolic size. */
1086 34 : if (m_diag_arg)
1087 8 : pp_printf (&pp,
1088 : "write of %qE bytes at offset %qE exceeds %qE",
1089 : m_num_bytes, m_offset, m_diag_arg);
1090 : else
1091 26 : pp_printf (&pp,
1092 : "write of %qE bytes at offset %qE exceeds"
1093 : " the buffer",
1094 : m_num_bytes, m_offset);
1095 : }
1096 : }
1097 : else
1098 : {
1099 : /* Known offset, unknown size. */
1100 6 : if (m_diag_arg)
1101 0 : pp_printf (&pp,
1102 : "write at offset %qE exceeds %qE",
1103 : m_offset, m_diag_arg);
1104 : else
1105 6 : pp_printf (&pp,
1106 : "write at offset %qE exceeds the buffer",
1107 : m_offset);
1108 : }
1109 : }
1110 : else
1111 : {
1112 : /* Unknown offset. */
1113 34 : if (m_diag_arg)
1114 0 : pp_printf (&pp,
1115 : "out-of-bounds write on %qE",
1116 : m_diag_arg);
1117 : else
1118 34 : pp_printf (&pp, "out-of-bounds write");
1119 : }
1120 238 : return true;
1121 : }
1122 :
1123 : enum access_direction
1124 119 : get_dir () const final override { return access_direction::write; }
1125 : };
1126 :
1127 : /* Concrete subclass to complain about over-reads with symbolic values. */
1128 :
1129 : class symbolic_buffer_over_read : public symbolic_past_the_end
1130 : {
1131 : public:
1132 41 : symbolic_buffer_over_read (const region_model &model,
1133 : const symbolic::memory_range &access_range,
1134 : tree diag_arg,
1135 : tree offset,
1136 : tree num_bytes,
1137 : tree capacity)
1138 : : symbolic_past_the_end (model, access_range, diag_arg,
1139 : offset, num_bytes,
1140 : capacity,
1141 41 : nullptr)
1142 : {
1143 : }
1144 :
1145 587 : const char *get_kind () const final override
1146 : {
1147 587 : return "symbolic_buffer_over_read";
1148 : }
1149 :
1150 25 : bool emit (diagnostic_emission_context &ctxt) final override
1151 : {
1152 25 : ctxt.add_cwe (126);
1153 25 : bool warned;
1154 25 : switch (get_memory_space ())
1155 : {
1156 4 : default:
1157 4 : ctxt.add_cwe (787);
1158 4 : warned = ctxt.warn ("buffer over-read");
1159 4 : break;
1160 21 : case memory_space::stack:
1161 21 : ctxt.add_cwe (121);
1162 21 : warned = ctxt.warn ("stack-based buffer over-read");
1163 21 : break;
1164 0 : case memory_space::heap:
1165 0 : ctxt.add_cwe (122);
1166 0 : warned = ctxt.warn ("heap-based buffer over-read");
1167 0 : break;
1168 : }
1169 25 : if (warned)
1170 25 : maybe_show_notes (ctxt);
1171 25 : return warned;
1172 : }
1173 :
1174 : bool
1175 50 : describe_final_event (pretty_printer &pp,
1176 : const evdesc::final_event &) final override
1177 : {
1178 50 : if (m_offset)
1179 : {
1180 : /* Known offset. */
1181 50 : if (m_num_bytes)
1182 : {
1183 : /* Known offset, known size. */
1184 50 : if (TREE_CODE (m_num_bytes) == INTEGER_CST)
1185 : {
1186 : /* Known offset, known constant size. */
1187 24 : if (pending_diagnostic::same_tree_p (m_num_bytes,
1188 : integer_one_node))
1189 : {
1190 : /* Singular m_num_bytes. */
1191 0 : if (m_diag_arg)
1192 0 : pp_printf (&pp,
1193 : "read of %E byte at offset %qE exceeds %qE",
1194 : m_num_bytes, m_offset, m_diag_arg);
1195 : else
1196 0 : pp_printf (&pp,
1197 : "read of %E byte at offset %qE exceeds"
1198 : " the buffer",
1199 : m_num_bytes, m_offset);
1200 : }
1201 : else
1202 : {
1203 : /* Plural m_num_bytes. */
1204 24 : if (m_diag_arg)
1205 8 : pp_printf (&pp,
1206 : "read of %E bytes at offset %qE exceeds %qE",
1207 : m_num_bytes, m_offset, m_diag_arg);
1208 : else
1209 16 : pp_printf (&pp,
1210 : "read of %E bytes at offset %qE exceeds"
1211 : " the buffer",
1212 : m_num_bytes, m_offset);
1213 : }
1214 : }
1215 : else
1216 : {
1217 : /* Known offset, known symbolic size. */
1218 26 : if (m_diag_arg)
1219 8 : pp_printf (&pp,
1220 : "read of %qE bytes at offset %qE exceeds %qE",
1221 : m_num_bytes, m_offset, m_diag_arg);
1222 : else
1223 18 : pp_printf (&pp,
1224 : "read of %qE bytes at offset %qE exceeds"
1225 : " the buffer",
1226 : m_num_bytes, m_offset);
1227 : }
1228 : }
1229 : else
1230 : {
1231 : /* Known offset, unknown size. */
1232 0 : if (m_diag_arg)
1233 0 : pp_printf (&pp,
1234 : "read at offset %qE exceeds %qE",
1235 : m_offset, m_diag_arg);
1236 : else
1237 0 : pp_printf (&pp,
1238 : "read at offset %qE exceeds the buffer",
1239 : m_offset);
1240 : }
1241 : }
1242 : else
1243 : {
1244 : /* Unknown offset. */
1245 0 : if (m_diag_arg)
1246 0 : pp_printf (&pp,
1247 : "out-of-bounds read on %qE",
1248 : m_diag_arg);
1249 : else
1250 0 : pp_printf (&pp,
1251 : "out-of-bounds read");
1252 : }
1253 50 : return true;
1254 : }
1255 :
1256 : enum access_direction
1257 25 : get_dir () const final override { return access_direction::read; }
1258 : };
1259 :
1260 : /* Check whether an access is past the end of the BASE_REG.
1261 : Return TRUE if the access was valid, FALSE otherwise. */
1262 :
1263 : bool
1264 39121 : region_model::check_symbolic_bounds (const symbolic::memory_range &attempted_range,
1265 : const svalue *capacity,
1266 : enum access_direction dir,
1267 : const svalue *sval_hint,
1268 : region_model_context *ctxt) const
1269 : {
1270 39121 : gcc_assert (ctxt);
1271 :
1272 39121 : auto &next_bit = attempted_range.get_bit_range ().get_next_offset ();
1273 39121 : auto *next_byte_as_scalar = maybe_bytes_from_bits (next_bit);
1274 39121 : if (!next_byte_as_scalar)
1275 : return true;
1276 :
1277 36475 : auto &start_bit = attempted_range.get_bit_range ().get_start_offset ();
1278 36475 : auto *start_byte_as_scalar = maybe_bytes_from_bits (start_bit);
1279 36475 : if (!start_byte_as_scalar)
1280 : return true;
1281 :
1282 36475 : auto &attempted_size_in_bits = attempted_range.get_bit_range ().get_size ();
1283 36475 : auto *attempted_size_in_bytes
1284 36475 : = maybe_bytes_from_bits (attempted_size_in_bits);
1285 36475 : if (!attempted_size_in_bytes)
1286 : return true;
1287 :
1288 36475 : const svalue *next_byte
1289 36475 : = m_mgr->get_svalue_from_scalar (size_type_node, *next_byte_as_scalar);
1290 :
1291 36475 : if (eval_condition (next_byte, GT_EXPR, capacity).is_true ())
1292 : {
1293 161 : auto &base_reg = attempted_range.get_base_region ();
1294 161 : tree diag_arg = get_representative_tree (&base_reg);
1295 161 : const svalue *sym_byte_offset
1296 161 : = m_mgr->get_svalue_from_scalar (size_type_node, *start_byte_as_scalar);
1297 161 : tree offset_tree = get_representative_tree (sym_byte_offset);
1298 161 : const svalue *num_bytes_sval
1299 161 : = m_mgr->get_svalue_from_scalar (size_type_node,
1300 : *attempted_size_in_bytes);
1301 161 : tree num_bytes_tree = get_representative_tree (num_bytes_sval);
1302 161 : tree capacity_tree = get_representative_tree (capacity);
1303 161 : switch (dir)
1304 : {
1305 0 : default:
1306 0 : gcc_unreachable ();
1307 41 : break;
1308 41 : case access_direction::read:
1309 41 : gcc_assert (sval_hint == nullptr);
1310 41 : ctxt->warn
1311 41 : (std::make_unique<symbolic_buffer_over_read> (*this,
1312 : attempted_range,
1313 : diag_arg,
1314 : offset_tree,
1315 : num_bytes_tree,
1316 : capacity_tree));
1317 41 : return false;
1318 120 : break;
1319 120 : case access_direction::write:
1320 120 : ctxt->warn
1321 120 : (std::make_unique<symbolic_buffer_overflow> (*this,
1322 : attempted_range,
1323 : diag_arg,
1324 : offset_tree,
1325 : num_bytes_tree,
1326 : capacity_tree,
1327 : sval_hint));
1328 120 : return false;
1329 161 : break;
1330 : }
1331 : }
1332 :
1333 : return true;
1334 : }
1335 :
1336 : /* Complain to CTXT if an access of ATTEMPTED_RANGE is outside the bounds
1337 : of the underlying region. SVAL_HINT can be non-null for writes and, if
1338 : so, may be used to make the diagnostic more readable.
1339 : Return true if the access was valid, false otherwise. */
1340 :
1341 : bool
1342 663776 : region_model::
1343 : check_memory_range_bounds (const symbolic::memory_range &attempted_range,
1344 : enum access_direction dir,
1345 : const svalue *sval_hint,
1346 : region_model_context *ctxt) const
1347 : {
1348 663776 : gcc_assert (ctxt);
1349 :
1350 : /* Bail out if 0 bits are accessed. */
1351 663776 : if (attempted_range.get_bit_range ().get_size ().is_zero_p ())
1352 : return true;
1353 :
1354 663740 : const region &base_reg = attempted_range.get_base_region ();
1355 :
1356 : /* Get the capacity of the buffer (in bytes). */
1357 663740 : auto &byte_capacity = get_capacity_as_scalar (&base_reg);
1358 :
1359 663740 : integer_with_unit<units::byte> conc_byte_capacity;
1360 663740 : bool capacity_is_concrete
1361 663740 : = concrete_from_symbolic (byte_capacity, &conc_byte_capacity);
1362 :
1363 : /* If any of the base region, the offset, or the number of bytes accessed
1364 : are symbolic, we have to reason about symbolic values. */
1365 :
1366 663740 : concrete_bit_range conc_attempted_range;
1367 663740 : bool attempted_range_is_concrete
1368 663740 : = concrete_from_symbolic (attempted_range.get_bit_range (),
1369 : &conc_attempted_range);
1370 663740 : if (base_reg.symbolic_p () || !attempted_range_is_concrete)
1371 : {
1372 39121 : auto *byte_capacity_as_sval = get_capacity (&base_reg);
1373 39121 : gcc_assert (byte_capacity_as_sval);
1374 39121 : return check_symbolic_bounds (attempted_range,
1375 : byte_capacity_as_sval,
1376 39121 : dir, sval_hint, ctxt);
1377 : }
1378 :
1379 : /* Otherwise continue to check with concrete values. */
1380 624619 : concrete_bit_range bits_outside;
1381 624619 : bool oob_safe = true;
1382 : /* If conc_attempted_range has a subset < 0, we do have an underwrite. */
1383 624619 : if (conc_attempted_range.falls_short_of_p (0_bits, &bits_outside))
1384 : {
1385 209 : tree diag_arg = get_representative_tree (&base_reg);
1386 209 : switch (dir)
1387 : {
1388 0 : default:
1389 0 : gcc_unreachable ();
1390 143 : break;
1391 143 : case access_direction::read:
1392 143 : gcc_assert (sval_hint == nullptr);
1393 143 : ctxt->warn
1394 143 : (std::make_unique<concrete_buffer_under_read> (*this,
1395 : attempted_range,
1396 : diag_arg,
1397 : bits_outside));
1398 143 : oob_safe = false;
1399 143 : break;
1400 66 : case access_direction::write:
1401 66 : ctxt->warn
1402 66 : (std::make_unique<concrete_buffer_underwrite> (*this,
1403 : attempted_range,
1404 : diag_arg,
1405 : bits_outside,
1406 : sval_hint));
1407 66 : oob_safe = false;
1408 66 : break;
1409 : }
1410 : }
1411 :
1412 : /* For accesses past the end, we do need a concrete capacity. No need to
1413 : do a symbolic check here because the inequality check does not reason
1414 : whether constants are greater than symbolic values. */
1415 624619 : if (!capacity_is_concrete)
1416 : return oob_safe;
1417 :
1418 618925 : auto conc_bit_capacity = bits_from_bytes (conc_byte_capacity);
1419 618925 : if (conc_attempted_range.exceeds_p (conc_bit_capacity, &bits_outside))
1420 : {
1421 505 : tree diag_arg = get_representative_tree (&base_reg);
1422 :
1423 505 : switch (dir)
1424 : {
1425 0 : default:
1426 0 : gcc_unreachable ();
1427 293 : break;
1428 293 : case access_direction::read:
1429 293 : gcc_assert (sval_hint == nullptr);
1430 293 : ctxt->warn
1431 293 : (std::make_unique<concrete_buffer_over_read> (*this,
1432 : attempted_range,
1433 : diag_arg,
1434 : bits_outside,
1435 : conc_bit_capacity));
1436 293 : oob_safe = false;
1437 293 : break;
1438 212 : case access_direction::write:
1439 212 : ctxt->warn
1440 212 : (std::make_unique<concrete_buffer_overflow> (*this,
1441 : attempted_range,
1442 : diag_arg,
1443 : bits_outside,
1444 : conc_bit_capacity,
1445 : sval_hint));
1446 212 : oob_safe = false;
1447 212 : break;
1448 : }
1449 : }
1450 618925 : return oob_safe;
1451 1907284 : }
1452 :
1453 : /* May complain when the access on REG is out-of-bounds.
1454 : Return TRUE if the access was valid, FALSE otherwise. */
1455 :
1456 : bool
1457 0 : region_model::check_region_bounds (const region *reg,
1458 : enum access_direction dir,
1459 : const svalue *sval_hint,
1460 : region_model_context *ctxt) const
1461 : {
1462 0 : gcc_assert (ctxt);
1463 :
1464 0 : return check_memory_range_bounds (reg->get_memory_range (*m_mgr),
1465 : dir,
1466 : sval_hint,
1467 0 : ctxt);
1468 : }
1469 :
1470 : } // namespace ana
1471 :
1472 : #endif /* #if ENABLE_ANALYZER */
|