Branch data Line data Source code
1 : : /* Classes for modeling the state of memory.
2 : : Copyright (C) 2019-2025 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 : : #define INCLUDE_ALGORITHM
22 : : #include "analyzer/common.h"
23 : :
24 : : #include "ordered-hash-map.h"
25 : : #include "options.h"
26 : : #include "cgraph.h"
27 : : #include "cfg.h"
28 : : #include "sbitmap.h"
29 : : #include "diagnostics/event-id.h"
30 : : #include "stor-layout.h"
31 : : #include "stringpool.h"
32 : : #include "attribs.h"
33 : : #include "tree-object-size.h"
34 : : #include "gimple-ssa.h"
35 : : #include "tree-phinodes.h"
36 : : #include "tree-ssa-operands.h"
37 : : #include "ssa-iterators.h"
38 : : #include "target.h"
39 : : #include "calls.h"
40 : : #include "is-a.h"
41 : : #include "gcc-rich-location.h"
42 : : #include "gcc-urlifier.h"
43 : : #include "diagnostics/sarif-sink.h"
44 : : #include "tree-pretty-print.h"
45 : : #include "fold-const.h"
46 : : #include "selftest-tree.h"
47 : :
48 : : #include "text-art/tree-widget.h"
49 : :
50 : : #include "analyzer/analyzer-logging.h"
51 : : #include "analyzer/supergraph.h"
52 : : #include "analyzer/call-string.h"
53 : : #include "analyzer/program-point.h"
54 : : #include "analyzer/store.h"
55 : : #include "analyzer/region-model.h"
56 : : #include "analyzer/constraint-manager.h"
57 : : #include "analyzer/sm.h"
58 : : #include "analyzer/pending-diagnostic.h"
59 : : #include "analyzer/region-model-reachability.h"
60 : : #include "analyzer/analyzer-selftests.h"
61 : : #include "analyzer/program-state.h"
62 : : #include "analyzer/call-summary.h"
63 : : #include "analyzer/checker-event.h"
64 : : #include "analyzer/checker-path.h"
65 : : #include "analyzer/feasible-graph.h"
66 : : #include "analyzer/record-layout.h"
67 : : #include "analyzer/function-set.h"
68 : :
69 : : #if ENABLE_ANALYZER
70 : :
71 : : namespace ana {
72 : :
73 : : auto_vec<pop_frame_callback> region_model::pop_frame_callbacks;
74 : :
75 : : /* Dump T to PP in language-independent form, for debugging/logging/dumping
76 : : purposes. */
77 : :
78 : : void
79 : 35936 : dump_tree (pretty_printer *pp, tree t)
80 : : {
81 : 35936 : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
82 : 35936 : }
83 : :
84 : : /* Dump T to PP in language-independent form in quotes, for
85 : : debugging/logging/dumping purposes. */
86 : :
87 : : void
88 : 1335 : dump_quoted_tree (pretty_printer *pp, tree t)
89 : : {
90 : 1335 : pp_begin_quote (pp, pp_show_color (pp));
91 : 1335 : dump_tree (pp, t);
92 : 1335 : pp_end_quote (pp, pp_show_color (pp));
93 : 1335 : }
94 : :
95 : : /* Equivalent to pp_printf (pp, "%qT", t), to avoid nesting pp_printf
96 : : calls within other pp_printf calls.
97 : :
98 : : default_tree_printer handles 'T' and some other codes by calling
99 : : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
100 : : dump_generic_node calls pp_printf in various places, leading to
101 : : garbled output.
102 : :
103 : : Ideally pp_printf could be made to be reentrant, but in the meantime
104 : : this function provides a workaround. */
105 : :
106 : : void
107 : 3858 : print_quoted_type (pretty_printer *pp, tree t)
108 : : {
109 : 3858 : if (!t)
110 : : return;
111 : 3854 : pp_begin_quote (pp, pp_show_color (pp));
112 : 3854 : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
113 : 3854 : pp_end_quote (pp, pp_show_color (pp));
114 : : }
115 : :
116 : : /* Print EXPR to PP, without quotes.
117 : : For use within svalue::maybe_print_for_user
118 : : and region::maybe_print_for_user. */
119 : :
120 : : void
121 : 38 : print_expr_for_user (pretty_printer *pp, tree expr)
122 : : {
123 : : /* Workaround for C++'s lang_hooks.decl_printable_name,
124 : : which unhelpfully (for us) prefixes the decl with its
125 : : type. */
126 : 38 : if (DECL_P (expr))
127 : 38 : dump_generic_node (pp, expr, 0, TDF_SLIM, 0);
128 : : else
129 : 0 : pp_printf (pp, "%E", expr);
130 : 38 : }
131 : :
132 : : /* class region_to_value_map. */
133 : :
134 : : /* Assignment operator for region_to_value_map. */
135 : :
136 : : region_to_value_map &
137 : 21687 : region_to_value_map::operator= (const region_to_value_map &other)
138 : : {
139 : 21687 : m_hash_map.empty ();
140 : 34669 : for (auto iter : other.m_hash_map)
141 : : {
142 : 12982 : const region *reg = iter.first;
143 : 12982 : const svalue *sval = iter.second;
144 : 12982 : m_hash_map.put (reg, sval);
145 : : }
146 : 21687 : return *this;
147 : : }
148 : :
149 : : /* Equality operator for region_to_value_map. */
150 : :
151 : : bool
152 : 431519 : region_to_value_map::operator== (const region_to_value_map &other) const
153 : : {
154 : 431519 : if (m_hash_map.elements () != other.m_hash_map.elements ())
155 : : return false;
156 : :
157 : 723430 : for (auto iter : *this)
158 : : {
159 : 147361 : const region *reg = iter.first;
160 : 147361 : const svalue *sval = iter.second;
161 : 147361 : const svalue * const *other_slot = other.get (reg);
162 : 147361 : if (other_slot == nullptr)
163 : 58 : return false;
164 : 147331 : if (sval != *other_slot)
165 : : return false;
166 : : }
167 : :
168 : 428766 : return true;
169 : : }
170 : :
171 : : /* Dump this object to PP. */
172 : :
173 : : void
174 : 416 : region_to_value_map::dump_to_pp (pretty_printer *pp, bool simple,
175 : : bool multiline) const
176 : : {
177 : 416 : auto_vec<const region *> regs;
178 : 1248 : for (iterator iter = begin (); iter != end (); ++iter)
179 : 416 : regs.safe_push ((*iter).first);
180 : 416 : regs.qsort (region::cmp_ptr_ptr);
181 : 416 : if (multiline)
182 : 416 : pp_newline (pp);
183 : : else
184 : 0 : pp_string (pp, " {");
185 : : unsigned i;
186 : : const region *reg;
187 : 832 : FOR_EACH_VEC_ELT (regs, i, reg)
188 : : {
189 : 416 : if (multiline)
190 : 416 : pp_string (pp, " ");
191 : 0 : else if (i > 0)
192 : 0 : pp_string (pp, ", ");
193 : 416 : reg->dump_to_pp (pp, simple);
194 : 416 : pp_string (pp, ": ");
195 : 416 : const svalue *sval = *get (reg);
196 : 416 : sval->dump_to_pp (pp, true);
197 : 416 : if (multiline)
198 : 416 : pp_newline (pp);
199 : : }
200 : 416 : if (!multiline)
201 : 0 : pp_string (pp, "}");
202 : 416 : }
203 : :
204 : : /* Dump this object to stderr. */
205 : :
206 : : DEBUG_FUNCTION void
207 : 0 : region_to_value_map::dump (bool simple) const
208 : : {
209 : 0 : tree_dump_pretty_printer pp (stderr);
210 : 0 : dump_to_pp (&pp, simple, true);
211 : 0 : pp_newline (&pp);
212 : 0 : }
213 : :
214 : : /* Generate a JSON value for this region_to_value_map.
215 : : This is intended for debugging the analyzer rather than
216 : : serialization. */
217 : :
218 : : std::unique_ptr<json::object>
219 : 4 : region_to_value_map::to_json () const
220 : : {
221 : 4 : auto map_obj = std::make_unique<json::object> ();
222 : :
223 : 4 : auto_vec<const region *> regs;
224 : 4 : for (iterator iter = begin (); iter != end (); ++iter)
225 : 0 : regs.safe_push ((*iter).first);
226 : 4 : regs.qsort (region::cmp_ptr_ptr);
227 : :
228 : : unsigned i;
229 : : const region *reg;
230 : 4 : FOR_EACH_VEC_ELT (regs, i, reg)
231 : : {
232 : 0 : label_text reg_desc = reg->get_desc ();
233 : 0 : const svalue *sval = *get (reg);
234 : 0 : map_obj->set (reg_desc.get (), sval->to_json ());
235 : 0 : }
236 : :
237 : 4 : return map_obj;
238 : 4 : }
239 : :
240 : : std::unique_ptr<text_art::tree_widget>
241 : 4 : region_to_value_map::
242 : : make_dump_widget (const text_art::dump_widget_info &dwi) const
243 : : {
244 : 4 : if (is_empty ())
245 : 4 : return nullptr;
246 : :
247 : 0 : std::unique_ptr<text_art::tree_widget> w
248 : 0 : (text_art::tree_widget::make (dwi, "Dynamic Extents"));
249 : :
250 : 0 : auto_vec<const region *> regs;
251 : 0 : for (iterator iter = begin (); iter != end (); ++iter)
252 : 0 : regs.safe_push ((*iter).first);
253 : 0 : regs.qsort (region::cmp_ptr_ptr);
254 : :
255 : : unsigned i;
256 : : const region *reg;
257 : 0 : FOR_EACH_VEC_ELT (regs, i, reg)
258 : : {
259 : 0 : pretty_printer the_pp;
260 : 0 : pretty_printer * const pp = &the_pp;
261 : 0 : pp_format_decoder (pp) = default_tree_printer;
262 : 0 : const bool simple = true;
263 : :
264 : 0 : reg->dump_to_pp (pp, simple);
265 : 0 : pp_string (pp, ": ");
266 : 0 : const svalue *sval = *get (reg);
267 : 0 : sval->dump_to_pp (pp, true);
268 : 0 : w->add_child (text_art::tree_widget::make (dwi, pp));
269 : 0 : }
270 : 0 : return w;
271 : 0 : }
272 : :
273 : : /* Attempt to merge THIS with OTHER, writing the result
274 : : to OUT.
275 : :
276 : : For now, write (region, value) mappings that are in common between THIS
277 : : and OTHER to OUT, effectively taking the intersection.
278 : :
279 : : Reject merger of different values. */
280 : :
281 : : bool
282 : 42128 : region_to_value_map::can_merge_with_p (const region_to_value_map &other,
283 : : region_to_value_map *out) const
284 : : {
285 : 57996 : for (auto iter : *this)
286 : : {
287 : 10775 : const region *iter_reg = iter.first;
288 : 10775 : const svalue *iter_sval = iter.second;
289 : 10775 : const svalue * const * other_slot = other.get (iter_reg);
290 : 10775 : if (other_slot)
291 : : {
292 : 10443 : if (iter_sval == *other_slot)
293 : 7602 : out->put (iter_reg, iter_sval);
294 : : else
295 : 2841 : return false;
296 : : }
297 : : }
298 : 39287 : return true;
299 : : }
300 : :
301 : : /* Purge any state involving SVAL. */
302 : :
303 : : void
304 : 28343 : region_to_value_map::purge_state_involving (const svalue *sval)
305 : : {
306 : 28343 : auto_vec<const region *> to_purge;
307 : 85971 : for (auto iter : *this)
308 : : {
309 : 28814 : const region *iter_reg = iter.first;
310 : 28814 : const svalue *iter_sval = iter.second;
311 : 28814 : if (iter_reg->involves_p (sval) || iter_sval->involves_p (sval))
312 : 22 : to_purge.safe_push (iter_reg);
313 : : }
314 : 28409 : for (auto iter : to_purge)
315 : 22 : m_hash_map.remove (iter);
316 : 28343 : }
317 : :
318 : : // struct exception_node
319 : :
320 : : bool
321 : 10825 : exception_node::operator== (const exception_node &other) const
322 : : {
323 : 10825 : return (m_exception_sval == other.m_exception_sval
324 : 10825 : && m_typeinfo_sval == other.m_typeinfo_sval
325 : 21650 : && m_destructor_sval == other.m_destructor_sval);
326 : : }
327 : :
328 : : void
329 : 6 : exception_node::dump_to_pp (pretty_printer *pp,
330 : : bool simple) const
331 : : {
332 : 6 : pp_printf (pp, "{exception: ");
333 : 6 : m_exception_sval->dump_to_pp (pp, simple);
334 : 6 : pp_string (pp, ", typeinfo: ");
335 : 6 : m_typeinfo_sval->dump_to_pp (pp, simple);
336 : 6 : pp_string (pp, ", destructor: ");
337 : 6 : m_destructor_sval->dump_to_pp (pp, simple);
338 : 6 : pp_string (pp, "}");
339 : 6 : }
340 : :
341 : : void
342 : 0 : exception_node::dump (FILE *fp, bool simple) const
343 : : {
344 : 0 : tree_dump_pretty_printer pp (fp);
345 : 0 : dump_to_pp (&pp, simple);
346 : 0 : pp_newline (&pp);
347 : 0 : }
348 : :
349 : : /* Dump a multiline representation of this model to stderr. */
350 : :
351 : : DEBUG_FUNCTION void
352 : 0 : exception_node::dump (bool simple) const
353 : : {
354 : 0 : dump (stderr, simple);
355 : 0 : }
356 : :
357 : : DEBUG_FUNCTION void
358 : 0 : exception_node::dump () const
359 : : {
360 : 0 : text_art::dump (*this);
361 : 0 : }
362 : :
363 : : std::unique_ptr<json::object>
364 : 0 : exception_node::to_json () const
365 : : {
366 : 0 : auto obj = std::make_unique<json::object> ();
367 : 0 : obj->set ("exception", m_exception_sval->to_json ());
368 : 0 : obj->set ("typeinfo", m_typeinfo_sval->to_json ());
369 : 0 : obj->set ("destructor", m_destructor_sval->to_json ());
370 : 0 : return obj;
371 : : }
372 : :
373 : : std::unique_ptr<text_art::tree_widget>
374 : 0 : exception_node::make_dump_widget (const text_art::dump_widget_info &dwi) const
375 : : {
376 : 0 : using text_art::tree_widget;
377 : 0 : std::unique_ptr<tree_widget> w
378 : 0 : (tree_widget::from_fmt (dwi, nullptr, "Exception Node"));
379 : :
380 : 0 : w->add_child (m_exception_sval->make_dump_widget (dwi, "exception"));
381 : 0 : w->add_child (m_typeinfo_sval->make_dump_widget (dwi, "typeinfo"));
382 : 0 : w->add_child (m_destructor_sval->make_dump_widget (dwi, "destructor"));
383 : :
384 : 0 : return w;
385 : : }
386 : :
387 : : tree
388 : 514 : exception_node::maybe_get_type () const
389 : : {
390 : 514 : return m_typeinfo_sval->maybe_get_type_from_typeinfo ();
391 : : }
392 : :
393 : : void
394 : 88 : exception_node::add_to_reachable_regions (reachable_regions ®s) const
395 : : {
396 : 88 : regs.handle_sval (m_exception_sval);
397 : 88 : regs.handle_sval (m_typeinfo_sval);
398 : 88 : regs.handle_sval (m_destructor_sval);
399 : 88 : }
400 : :
401 : : /* class region_model. */
402 : :
403 : : /* Ctor for region_model: construct an "empty" model. */
404 : :
405 : 288247 : region_model::region_model (region_model_manager *mgr)
406 : 288247 : : m_mgr (mgr), m_store (), m_current_frame (nullptr),
407 : 288247 : m_thrown_exceptions_stack (),
408 : 288247 : m_caught_exceptions_stack (),
409 : 288247 : m_dynamic_extents ()
410 : : {
411 : 288247 : m_constraints = new constraint_manager (mgr);
412 : 288247 : }
413 : :
414 : : /* region_model's copy ctor. */
415 : :
416 : 2993354 : region_model::region_model (const region_model &other)
417 : 2993354 : : m_mgr (other.m_mgr), m_store (other.m_store),
418 : 2993354 : m_constraints (new constraint_manager (*other.m_constraints)),
419 : 2993354 : m_current_frame (other.m_current_frame),
420 : 2993354 : m_thrown_exceptions_stack (other.m_thrown_exceptions_stack),
421 : 2993354 : m_caught_exceptions_stack (other.m_caught_exceptions_stack),
422 : 2993354 : m_dynamic_extents (other.m_dynamic_extents)
423 : : {
424 : 2993354 : }
425 : :
426 : : /* region_model's dtor. */
427 : :
428 : 3281601 : region_model::~region_model ()
429 : : {
430 : 3281601 : delete m_constraints;
431 : 3281601 : }
432 : :
433 : : /* region_model's assignment operator. */
434 : :
435 : : region_model &
436 : 21687 : region_model::operator= (const region_model &other)
437 : : {
438 : : /* m_mgr is const. */
439 : 21687 : gcc_assert (m_mgr == other.m_mgr);
440 : :
441 : 21687 : m_store = other.m_store;
442 : :
443 : 21687 : delete m_constraints;
444 : 21687 : m_constraints = new constraint_manager (*other.m_constraints);
445 : :
446 : 21687 : m_current_frame = other.m_current_frame;
447 : :
448 : 21687 : m_thrown_exceptions_stack = other.m_thrown_exceptions_stack;
449 : 21687 : m_caught_exceptions_stack = other.m_caught_exceptions_stack;
450 : :
451 : 21687 : m_dynamic_extents = other.m_dynamic_extents;
452 : :
453 : 21687 : return *this;
454 : : }
455 : :
456 : : /* Equality operator for region_model.
457 : :
458 : : Amongst other things this directly compares the stores and the constraint
459 : : managers, so for this to be meaningful both this and OTHER should
460 : : have been canonicalized. */
461 : :
462 : : bool
463 : 478868 : region_model::operator== (const region_model &other) const
464 : : {
465 : : /* We can only compare instances that use the same manager. */
466 : 478868 : gcc_assert (m_mgr == other.m_mgr);
467 : :
468 : 478868 : if (m_store != other.m_store)
469 : : return false;
470 : :
471 : 392887 : if (*m_constraints != *other.m_constraints)
472 : : return false;
473 : :
474 : 387867 : if (m_current_frame != other.m_current_frame)
475 : : return false;
476 : :
477 : 387859 : if (m_thrown_exceptions_stack != other.m_thrown_exceptions_stack)
478 : : return false;
479 : 387859 : if (m_caught_exceptions_stack != other.m_caught_exceptions_stack)
480 : : return false;
481 : :
482 : 387859 : if (m_dynamic_extents != other.m_dynamic_extents)
483 : : return false;
484 : :
485 : 387592 : gcc_checking_assert (hash () == other.hash ());
486 : :
487 : : return true;
488 : : }
489 : :
490 : : /* Generate a hash value for this region_model. */
491 : :
492 : : hashval_t
493 : 1203733 : region_model::hash () const
494 : : {
495 : 1203733 : hashval_t result = m_store.hash ();
496 : 1203733 : result ^= m_constraints->hash ();
497 : 1203733 : return result;
498 : : }
499 : :
500 : : /* Dump a representation of this model to PP, showing the
501 : : stack, the store, and any constraints.
502 : : Use SIMPLE to control how svalues and regions are printed. */
503 : :
504 : : void
505 : 1635 : region_model::dump_to_pp (pretty_printer *pp, bool simple,
506 : : bool multiline) const
507 : : {
508 : : /* Dump frame stack. */
509 : 1635 : pp_printf (pp, "stack depth: %i", get_stack_depth ());
510 : 1635 : if (multiline)
511 : 545 : pp_newline (pp);
512 : : else
513 : 1090 : pp_string (pp, " {");
514 : 3263 : for (const frame_region *iter_frame = m_current_frame; iter_frame;
515 : 1628 : iter_frame = iter_frame->get_calling_frame ())
516 : : {
517 : 1628 : if (multiline)
518 : 549 : pp_string (pp, " ");
519 : 1079 : else if (iter_frame != m_current_frame)
520 : 0 : pp_string (pp, ", ");
521 : 1628 : pp_printf (pp, "frame (index %i): ", iter_frame->get_index ());
522 : 1628 : iter_frame->dump_to_pp (pp, simple);
523 : 1628 : if (multiline)
524 : 549 : pp_newline (pp);
525 : : }
526 : 1635 : if (!multiline)
527 : 1090 : pp_string (pp, "}");
528 : :
529 : : /* Dump exception stacks. */
530 : 1635 : if (m_thrown_exceptions_stack.size () > 0)
531 : : {
532 : 6 : pp_printf (pp, "thrown exceptions: %i", (int)m_thrown_exceptions_stack.size ());
533 : 6 : if (multiline)
534 : 6 : pp_newline (pp);
535 : : else
536 : 0 : pp_string (pp, " {");
537 : 12 : for (size_t idx = 0; idx < m_thrown_exceptions_stack.size (); ++idx)
538 : : {
539 : 6 : if (multiline)
540 : 6 : pp_string (pp, " ");
541 : 0 : else if (idx > 0)
542 : 0 : pp_string (pp, ", ");
543 : 6 : pp_printf (pp, "exception (index %i): ", (int)idx);
544 : 6 : m_thrown_exceptions_stack[idx].dump_to_pp (pp, simple);
545 : 6 : if (multiline)
546 : 6 : pp_newline (pp);
547 : : }
548 : 6 : if (!multiline)
549 : 0 : pp_string (pp, "}");
550 : : }
551 : 1635 : if (m_caught_exceptions_stack.size () > 0)
552 : : {
553 : 0 : pp_printf (pp, "caught exceptions: %i", (int)m_caught_exceptions_stack.size ());
554 : 0 : if (multiline)
555 : 0 : pp_newline (pp);
556 : : else
557 : 0 : pp_string (pp, " {");
558 : 0 : for (size_t idx = 0; idx < m_caught_exceptions_stack.size (); ++idx)
559 : : {
560 : 0 : if (multiline)
561 : 0 : pp_string (pp, " ");
562 : 0 : else if (idx > 0)
563 : 0 : pp_string (pp, ", ");
564 : 0 : pp_printf (pp, "exception (index %i): ", (int)idx);
565 : 0 : m_caught_exceptions_stack[idx].dump_to_pp (pp, simple);
566 : 0 : if (multiline)
567 : 0 : pp_newline (pp);
568 : : }
569 : 0 : if (!multiline)
570 : 0 : pp_string (pp, "}");
571 : : }
572 : :
573 : : /* Dump store. */
574 : 1635 : if (!multiline)
575 : 1090 : pp_string (pp, ", {");
576 : 1635 : m_store.dump_to_pp (pp, simple, multiline,
577 : 1635 : m_mgr->get_store_manager ());
578 : 1635 : if (!multiline)
579 : 1090 : pp_string (pp, "}");
580 : :
581 : : /* Dump constraints. */
582 : 1635 : pp_string (pp, "constraint_manager:");
583 : 1635 : if (multiline)
584 : 545 : pp_newline (pp);
585 : : else
586 : 1090 : pp_string (pp, " {");
587 : 1635 : m_constraints->dump_to_pp (pp, multiline);
588 : 1635 : if (!multiline)
589 : 1090 : pp_string (pp, "}");
590 : :
591 : : /* Dump sizes of dynamic regions, if any are known. */
592 : 1635 : if (!m_dynamic_extents.is_empty ())
593 : : {
594 : 416 : pp_string (pp, "dynamic_extents:");
595 : 416 : m_dynamic_extents.dump_to_pp (pp, simple, multiline);
596 : : }
597 : 1635 : }
598 : :
599 : : /* Dump a representation of this model to FILE. */
600 : :
601 : : void
602 : 0 : region_model::dump (FILE *fp, bool simple, bool multiline) const
603 : : {
604 : 0 : tree_dump_pretty_printer pp (fp);
605 : 0 : dump_to_pp (&pp, simple, multiline);
606 : 0 : pp_newline (&pp);
607 : 0 : }
608 : :
609 : : /* Dump a multiline representation of this model to stderr. */
610 : :
611 : : DEBUG_FUNCTION void
612 : 0 : region_model::dump (bool simple) const
613 : : {
614 : 0 : dump (stderr, simple, true);
615 : 0 : }
616 : :
617 : : /* Dump a tree-like representation of this state to stderr. */
618 : :
619 : : DEBUG_FUNCTION void
620 : 0 : region_model::dump () const
621 : : {
622 : 0 : text_art::dump (*this);
623 : 0 : }
624 : :
625 : : /* Dump a multiline representation of this model to stderr. */
626 : :
627 : : DEBUG_FUNCTION void
628 : 0 : region_model::debug () const
629 : : {
630 : 0 : dump (true);
631 : 0 : }
632 : :
633 : : /* Generate a JSON value for this region_model.
634 : : This is intended for debugging the analyzer rather than
635 : : serialization. */
636 : :
637 : : std::unique_ptr<json::object>
638 : 4 : region_model::to_json () const
639 : : {
640 : 4 : auto model_obj = std::make_unique<json::object> ();
641 : 4 : model_obj->set ("store", m_store.to_json ());
642 : 4 : model_obj->set ("constraints", m_constraints->to_json ());
643 : 4 : if (m_current_frame)
644 : 4 : model_obj->set ("current_frame", m_current_frame->to_json ());
645 : :
646 : 4 : auto thrown_exceptions_arr = std::make_unique<json::array> ();
647 : 4 : for (auto &node : m_thrown_exceptions_stack)
648 : 0 : thrown_exceptions_arr->append (node.to_json ());
649 : 4 : model_obj->set ("thrown_exception_stack", std::move (thrown_exceptions_arr));
650 : :
651 : 4 : auto caught_exceptions_arr = std::make_unique<json::array> ();
652 : 4 : for (auto &node : m_caught_exceptions_stack)
653 : 0 : caught_exceptions_arr->append (node.to_json ());
654 : 4 : model_obj->set ("caught_exception_stack", std::move (caught_exceptions_arr));
655 : :
656 : 4 : model_obj->set ("dynamic_extents", m_dynamic_extents.to_json ());
657 : 8 : return model_obj;
658 : 4 : }
659 : :
660 : : std::unique_ptr<text_art::tree_widget>
661 : 4 : region_model::make_dump_widget (const text_art::dump_widget_info &dwi) const
662 : : {
663 : 4 : using text_art::tree_widget;
664 : 4 : std::unique_ptr<tree_widget> model_widget
665 : 4 : (tree_widget::from_fmt (dwi, nullptr, "Region Model"));
666 : :
667 : 4 : if (m_current_frame)
668 : : {
669 : 0 : pretty_printer the_pp;
670 : 0 : pretty_printer * const pp = &the_pp;
671 : 0 : pp_format_decoder (pp) = default_tree_printer;
672 : 0 : pp_show_color (pp) = true;
673 : 0 : const bool simple = true;
674 : :
675 : 0 : pp_string (pp, "Current Frame: ");
676 : 0 : m_current_frame->dump_to_pp (pp, simple);
677 : 0 : model_widget->add_child (tree_widget::make (dwi, pp));
678 : 0 : }
679 : :
680 : 4 : if (m_thrown_exceptions_stack.size () > 0)
681 : : {
682 : 0 : auto thrown_exceptions_widget
683 : 0 : = tree_widget::make (dwi, "Thrown Exceptions");
684 : 0 : for (auto &thrown_exception : m_thrown_exceptions_stack)
685 : 0 : thrown_exceptions_widget->add_child
686 : 0 : (thrown_exception.make_dump_widget (dwi));
687 : 0 : model_widget->add_child (std::move (thrown_exceptions_widget));
688 : 0 : }
689 : 4 : if (m_caught_exceptions_stack.size () > 0)
690 : : {
691 : 0 : auto caught_exceptions_widget
692 : 0 : = tree_widget::make (dwi, "Caught Exceptions");
693 : 0 : for (auto &caught_exception : m_caught_exceptions_stack)
694 : 0 : caught_exceptions_widget->add_child
695 : 0 : (caught_exception.make_dump_widget (dwi));
696 : 0 : model_widget->add_child (std::move (caught_exceptions_widget));
697 : 0 : }
698 : :
699 : 4 : model_widget->add_child
700 : 8 : (m_store.make_dump_widget (dwi,
701 : 4 : m_mgr->get_store_manager ()));
702 : 4 : model_widget->add_child (m_constraints->make_dump_widget (dwi));
703 : 4 : model_widget->add_child (m_dynamic_extents.make_dump_widget (dwi));
704 : 4 : return model_widget;
705 : : }
706 : :
707 : : /* Assert that this object is valid. */
708 : :
709 : : void
710 : 1659455 : region_model::validate () const
711 : : {
712 : 1659455 : m_store.validate ();
713 : 1659455 : }
714 : :
715 : : /* Canonicalize the store and constraints, to maximize the chance of
716 : : equality between region_model instances. */
717 : :
718 : : void
719 : 799406 : region_model::canonicalize ()
720 : : {
721 : 799406 : m_store.canonicalize (m_mgr->get_store_manager ());
722 : 799406 : m_constraints->canonicalize ();
723 : 799406 : }
724 : :
725 : : /* Return true if this region_model is in canonical form. */
726 : :
727 : : bool
728 : 378160 : region_model::canonicalized_p () const
729 : : {
730 : 378160 : region_model copy (*this);
731 : 378160 : copy.canonicalize ();
732 : 378160 : return *this == copy;
733 : 378160 : }
734 : :
735 : : /* See the comment for store::loop_replay_fixup. */
736 : :
737 : : void
738 : 2133 : region_model::loop_replay_fixup (const region_model *dst_state)
739 : : {
740 : 2133 : m_store.loop_replay_fixup (dst_state->get_store (), m_mgr);
741 : 2133 : }
742 : :
743 : : /* A subclass of pending_diagnostic for complaining about uses of
744 : : poisoned values. */
745 : :
746 : : class poisoned_value_diagnostic
747 : : : public pending_diagnostic_subclass<poisoned_value_diagnostic>
748 : : {
749 : : public:
750 : 2220 : poisoned_value_diagnostic (tree expr, enum poison_kind pkind,
751 : : const region *src_region,
752 : : tree check_expr)
753 : 2220 : : m_expr (expr), m_pkind (pkind),
754 : 2220 : m_src_region (src_region),
755 : 2220 : m_check_expr (check_expr)
756 : : {}
757 : :
758 : 14328 : const char *get_kind () const final override { return "poisoned_value_diagnostic"; }
759 : :
760 : 70 : bool use_of_uninit_p () const final override
761 : : {
762 : 70 : return m_pkind == poison_kind::uninit;
763 : : }
764 : :
765 : 1350 : bool operator== (const poisoned_value_diagnostic &other) const
766 : : {
767 : 1350 : return (m_expr == other.m_expr
768 : 1325 : && m_pkind == other.m_pkind
769 : 2675 : && m_src_region == other.m_src_region);
770 : : }
771 : :
772 : 1935 : int get_controlling_option () const final override
773 : : {
774 : 1935 : switch (m_pkind)
775 : : {
776 : 0 : default:
777 : 0 : gcc_unreachable ();
778 : : case poison_kind::uninit:
779 : : return OPT_Wanalyzer_use_of_uninitialized_value;
780 : : case poison_kind::freed:
781 : : case poison_kind::deleted:
782 : : return OPT_Wanalyzer_use_after_free;
783 : : case poison_kind::popped_stack:
784 : : return OPT_Wanalyzer_use_of_pointer_in_stale_stack_frame;
785 : : }
786 : : }
787 : :
788 : 1362 : bool terminate_path_p () const final override { return true; }
789 : :
790 : 573 : bool emit (diagnostic_emission_context &ctxt) final override
791 : : {
792 : 573 : switch (m_pkind)
793 : : {
794 : 0 : default:
795 : 0 : gcc_unreachable ();
796 : 546 : case poison_kind::uninit:
797 : 546 : {
798 : 546 : ctxt.add_cwe (457); /* "CWE-457: Use of Uninitialized Variable". */
799 : 546 : return ctxt.warn ("use of uninitialized value %qE",
800 : 546 : m_expr);
801 : : }
802 : 3 : break;
803 : 3 : case poison_kind::freed:
804 : 3 : {
805 : 3 : ctxt.add_cwe (416); /* "CWE-416: Use After Free". */
806 : 3 : return ctxt.warn ("use after %<free%> of %qE",
807 : 3 : m_expr);
808 : : }
809 : 9 : break;
810 : 9 : case poison_kind::deleted:
811 : 9 : {
812 : 9 : ctxt.add_cwe (416); /* "CWE-416: Use After Free". */
813 : 9 : return ctxt.warn ("use after %<delete%> of %qE",
814 : 9 : m_expr);
815 : : }
816 : 15 : break;
817 : 15 : case poison_kind::popped_stack:
818 : 15 : {
819 : : /* TODO: which CWE? */
820 : 15 : return ctxt.warn
821 : 15 : ("dereferencing pointer %qE to within stale stack frame",
822 : 15 : m_expr);
823 : : }
824 : : break;
825 : : }
826 : : }
827 : :
828 : : bool
829 : 1146 : describe_final_event (pretty_printer &pp,
830 : : const evdesc::final_event &) final override
831 : : {
832 : 1146 : switch (m_pkind)
833 : : {
834 : 0 : default:
835 : 0 : gcc_unreachable ();
836 : 1092 : case poison_kind::uninit:
837 : 1092 : {
838 : 1092 : pp_printf (&pp,
839 : : "use of uninitialized value %qE here",
840 : : m_expr);
841 : 1092 : return true;
842 : : }
843 : 6 : case poison_kind::freed:
844 : 6 : {
845 : 6 : pp_printf (&pp,
846 : : "use after %<free%> of %qE here",
847 : : m_expr);
848 : 6 : return true;
849 : : }
850 : 18 : case poison_kind::deleted:
851 : 18 : {
852 : 18 : pp_printf (&pp,
853 : : "use after %<delete%> of %qE here",
854 : : m_expr);
855 : 18 : return true;
856 : : }
857 : 30 : case poison_kind::popped_stack:
858 : 30 : {
859 : 30 : pp_printf (&pp,
860 : : "dereferencing pointer %qE to within stale stack frame",
861 : : m_expr);
862 : 30 : return true;
863 : : }
864 : : }
865 : : }
866 : :
867 : 573 : void mark_interesting_stuff (interesting_t *interest) final override
868 : : {
869 : 573 : if (m_src_region)
870 : 538 : interest->add_region_creation (m_src_region);
871 : 573 : }
872 : :
873 : : /* Attempt to suppress false positives.
874 : : Reject paths where the value of the underlying region isn't poisoned.
875 : : This can happen due to state merging when exploring the exploded graph,
876 : : where the more precise analysis during feasibility analysis finds that
877 : : the region is in fact valid.
878 : : To do this we need to get the value from the fgraph. Unfortunately
879 : : we can't simply query the state of m_src_region (from the enode),
880 : : since it might be a different region in the fnode state (e.g. with
881 : : heap-allocated regions, the numbering could be different).
882 : : Hence we access m_check_expr, if available. */
883 : :
884 : 1315 : bool check_valid_fpath_p (const feasible_node &fnode)
885 : : const final override
886 : : {
887 : 1315 : if (!m_check_expr)
888 : : return true;
889 : 1208 : const svalue *fsval = fnode.get_model ().get_rvalue (m_check_expr, nullptr);
890 : : /* Check to see if the expr is also poisoned in FNODE (and in the
891 : : same way). */
892 : 1208 : const poisoned_svalue * fspval = fsval->dyn_cast_poisoned_svalue ();
893 : 1208 : if (!fspval)
894 : : return false;
895 : 1208 : if (fspval->get_poison_kind () != m_pkind)
896 : : return false;
897 : : return true;
898 : : }
899 : :
900 : : void
901 : 8 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
902 : : const final override
903 : : {
904 : 8 : auto &props = result_obj.get_or_create_properties ();
905 : : #define PROPERTY_PREFIX "gcc/analyzer/poisoned_value_diagnostic/"
906 : 8 : props.set (PROPERTY_PREFIX "expr", tree_to_json (m_expr));
907 : 8 : props.set_string (PROPERTY_PREFIX "kind", poison_kind_to_str (m_pkind));
908 : 8 : if (m_src_region)
909 : 8 : props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ());
910 : 8 : props.set (PROPERTY_PREFIX "check_expr", tree_to_json (m_check_expr));
911 : : #undef PROPERTY_PREFIX
912 : 8 : }
913 : :
914 : : private:
915 : : tree m_expr;
916 : : enum poison_kind m_pkind;
917 : : const region *m_src_region;
918 : : tree m_check_expr;
919 : : };
920 : :
921 : : /* A subclass of pending_diagnostic for complaining about shifts
922 : : by negative counts. */
923 : :
924 : : class shift_count_negative_diagnostic
925 : : : public pending_diagnostic_subclass<shift_count_negative_diagnostic>
926 : : {
927 : : public:
928 : 16 : shift_count_negative_diagnostic (const gassign *assign, tree count_cst)
929 : 16 : : m_assign (assign), m_count_cst (count_cst)
930 : : {}
931 : :
932 : 120 : const char *get_kind () const final override
933 : : {
934 : 120 : return "shift_count_negative_diagnostic";
935 : : }
936 : :
937 : 16 : bool operator== (const shift_count_negative_diagnostic &other) const
938 : : {
939 : 16 : return (m_assign == other.m_assign
940 : 16 : && same_tree_p (m_count_cst, other.m_count_cst));
941 : : }
942 : :
943 : 24 : int get_controlling_option () const final override
944 : : {
945 : 24 : return OPT_Wanalyzer_shift_count_negative;
946 : : }
947 : :
948 : 8 : bool emit (diagnostic_emission_context &ctxt) final override
949 : : {
950 : 8 : return ctxt.warn ("shift by negative count (%qE)", m_count_cst);
951 : : }
952 : :
953 : : bool
954 : 16 : describe_final_event (pretty_printer &pp,
955 : : const evdesc::final_event &) final override
956 : : {
957 : 16 : pp_printf (&pp,
958 : : "shift by negative amount here (%qE)",
959 : : m_count_cst);
960 : 16 : return true;
961 : : }
962 : :
963 : : private:
964 : : const gassign *m_assign;
965 : : tree m_count_cst;
966 : : };
967 : :
968 : : /* A subclass of pending_diagnostic for complaining about shifts
969 : : by counts >= the width of the operand type. */
970 : :
971 : : class shift_count_overflow_diagnostic
972 : : : public pending_diagnostic_subclass<shift_count_overflow_diagnostic>
973 : : {
974 : : public:
975 : 8 : shift_count_overflow_diagnostic (const gassign *assign,
976 : : int operand_precision,
977 : : tree count_cst)
978 : 8 : : m_assign (assign), m_operand_precision (operand_precision),
979 : 8 : m_count_cst (count_cst)
980 : : {}
981 : :
982 : 36 : const char *get_kind () const final override
983 : : {
984 : 36 : return "shift_count_overflow_diagnostic";
985 : : }
986 : :
987 : 8 : bool operator== (const shift_count_overflow_diagnostic &other) const
988 : : {
989 : 8 : return (m_assign == other.m_assign
990 : 8 : && m_operand_precision == other.m_operand_precision
991 : 16 : && same_tree_p (m_count_cst, other.m_count_cst));
992 : : }
993 : :
994 : 12 : int get_controlling_option () const final override
995 : : {
996 : 12 : return OPT_Wanalyzer_shift_count_overflow;
997 : : }
998 : :
999 : 4 : bool emit (diagnostic_emission_context &ctxt) final override
1000 : : {
1001 : 4 : return ctxt.warn ("shift by count (%qE) >= precision of type (%qi)",
1002 : 4 : m_count_cst, m_operand_precision);
1003 : : }
1004 : :
1005 : : bool
1006 : 8 : describe_final_event (pretty_printer &pp,
1007 : : const evdesc::final_event &) final override
1008 : : {
1009 : 8 : pp_printf (&pp,
1010 : : "shift by count %qE here",
1011 : : m_count_cst);
1012 : 8 : return true;
1013 : : }
1014 : :
1015 : : private:
1016 : : const gassign *m_assign;
1017 : : int m_operand_precision;
1018 : : tree m_count_cst;
1019 : : };
1020 : :
1021 : : /* A subclass of pending_diagnostic for complaining about pointer
1022 : : subtractions involving unrelated buffers. */
1023 : :
1024 : : class undefined_ptrdiff_diagnostic
1025 : : : public pending_diagnostic_subclass<undefined_ptrdiff_diagnostic>
1026 : : {
1027 : : public:
1028 : : /* Region_creation_event subclass to give a custom wording when
1029 : : talking about creation of buffers for LHS and RHS of the
1030 : : subtraction. */
1031 : : class ptrdiff_region_creation_event : public region_creation_event
1032 : : {
1033 : : public:
1034 : 56 : ptrdiff_region_creation_event (const event_loc_info &loc_info,
1035 : : bool is_lhs)
1036 : 56 : : region_creation_event (loc_info),
1037 : 56 : m_is_lhs (is_lhs)
1038 : : {
1039 : : }
1040 : :
1041 : 112 : void print_desc (pretty_printer &pp) const final override
1042 : : {
1043 : 112 : if (m_is_lhs)
1044 : 56 : pp_string (&pp,
1045 : : "underlying object for left-hand side"
1046 : : " of subtraction created here");
1047 : : else
1048 : 56 : pp_string (&pp,
1049 : : "underlying object for right-hand side"
1050 : : " of subtraction created here");
1051 : 112 : }
1052 : :
1053 : : private:
1054 : : bool m_is_lhs;
1055 : : };
1056 : :
1057 : 64 : undefined_ptrdiff_diagnostic (const gassign *assign,
1058 : : const svalue *sval_a,
1059 : : const svalue *sval_b,
1060 : : const region *base_reg_a,
1061 : : const region *base_reg_b)
1062 : 64 : : m_assign (assign),
1063 : 64 : m_sval_a (sval_a),
1064 : 64 : m_sval_b (sval_b),
1065 : 64 : m_base_reg_a (base_reg_a),
1066 : 64 : m_base_reg_b (base_reg_b)
1067 : : {
1068 : 64 : gcc_assert (m_base_reg_a != m_base_reg_b);
1069 : : }
1070 : :
1071 : 380 : const char *get_kind () const final override
1072 : : {
1073 : 380 : return "undefined_ptrdiff_diagnostic";
1074 : : }
1075 : :
1076 : 56 : bool operator== (const undefined_ptrdiff_diagnostic &other) const
1077 : : {
1078 : 56 : return (m_assign == other.m_assign
1079 : 56 : && m_sval_a == other.m_sval_a
1080 : 56 : && m_sval_b == other.m_sval_b
1081 : 56 : && m_base_reg_a == other.m_base_reg_a
1082 : 112 : && m_base_reg_b == other.m_base_reg_b);
1083 : : }
1084 : :
1085 : 84 : int get_controlling_option () const final override
1086 : : {
1087 : 84 : return OPT_Wanalyzer_undefined_behavior_ptrdiff;
1088 : : }
1089 : :
1090 : 28 : bool emit (diagnostic_emission_context &ctxt) final override
1091 : : {
1092 : : /* CWE-469: Use of Pointer Subtraction to Determine Size. */
1093 : 28 : ctxt.add_cwe (469);
1094 : 28 : return ctxt.warn ("undefined behavior when subtracting pointers");
1095 : : }
1096 : :
1097 : 56 : void add_region_creation_events (const region *reg,
1098 : : tree /*capacity*/,
1099 : : const event_loc_info &loc_info,
1100 : : checker_path &emission_path) final override
1101 : : {
1102 : 56 : if (reg == m_base_reg_a)
1103 : 28 : emission_path.add_event
1104 : 28 : (std::make_unique<ptrdiff_region_creation_event> (loc_info, true));
1105 : 28 : else if (reg == m_base_reg_b)
1106 : 28 : emission_path.add_event
1107 : 28 : (std::make_unique<ptrdiff_region_creation_event> (loc_info, false));
1108 : 56 : }
1109 : :
1110 : : bool
1111 : 56 : describe_final_event (pretty_printer &pp,
1112 : : const evdesc::final_event &) final override
1113 : : {
1114 : 56 : pp_string (&pp,
1115 : : "subtraction of pointers has undefined behavior if"
1116 : : " they do not point into the same array object");
1117 : 56 : return true;
1118 : : }
1119 : :
1120 : 28 : void mark_interesting_stuff (interesting_t *interesting) final override
1121 : : {
1122 : 28 : interesting->add_region_creation (m_base_reg_a);
1123 : 28 : interesting->add_region_creation (m_base_reg_b);
1124 : 28 : }
1125 : :
1126 : : private:
1127 : : const gassign *m_assign;
1128 : : const svalue *m_sval_a;
1129 : : const svalue *m_sval_b;
1130 : : const region *m_base_reg_a;
1131 : : const region *m_base_reg_b;
1132 : : };
1133 : :
1134 : : /* Check the pointer subtraction SVAL_A - SVAL_B at ASSIGN and add
1135 : : a warning to CTXT if they're not within the same base region. */
1136 : :
1137 : : static void
1138 : 630 : check_for_invalid_ptrdiff (const gassign *assign,
1139 : : region_model_context &ctxt,
1140 : : const svalue *sval_a, const svalue *sval_b)
1141 : : {
1142 : 630 : const region *base_reg_a = sval_a->maybe_get_deref_base_region ();
1143 : 630 : if (!base_reg_a)
1144 : 566 : return;
1145 : 104 : const region *base_reg_b = sval_b->maybe_get_deref_base_region ();
1146 : 104 : if (!base_reg_b)
1147 : : return;
1148 : :
1149 : 80 : if (base_reg_a == base_reg_b)
1150 : : return;
1151 : :
1152 : 64 : if (base_reg_a->get_kind () == RK_SYMBOLIC)
1153 : : return;
1154 : 64 : if (base_reg_b->get_kind () == RK_SYMBOLIC)
1155 : : return;
1156 : :
1157 : 64 : ctxt.warn
1158 : 64 : (std::make_unique<undefined_ptrdiff_diagnostic> (assign,
1159 : : sval_a,
1160 : : sval_b,
1161 : : base_reg_a,
1162 : : base_reg_b));
1163 : : }
1164 : :
1165 : : /* If ASSIGN is a stmt that can be modelled via
1166 : : set_value (lhs_reg, SVALUE, CTXT)
1167 : : for some SVALUE, get the SVALUE.
1168 : : Otherwise return nullptr. */
1169 : :
1170 : : const svalue *
1171 : 355577 : region_model::get_gassign_result (const gassign *assign,
1172 : : region_model_context *ctxt)
1173 : : {
1174 : 355577 : tree lhs = gimple_assign_lhs (assign);
1175 : :
1176 : 355577 : if (gimple_has_volatile_ops (assign)
1177 : 355577 : && !gimple_clobber_p (assign))
1178 : : {
1179 : 116 : conjured_purge p (this, ctxt);
1180 : 116 : return m_mgr->get_or_create_conjured_svalue (TREE_TYPE (lhs),
1181 : : assign,
1182 : : get_lvalue (lhs, ctxt),
1183 : : p);
1184 : : }
1185 : :
1186 : 355461 : tree rhs1 = gimple_assign_rhs1 (assign);
1187 : 355461 : enum tree_code op = gimple_assign_rhs_code (assign);
1188 : 355461 : switch (op)
1189 : : {
1190 : : default:
1191 : : return nullptr;
1192 : :
1193 : 34091 : case POINTER_PLUS_EXPR:
1194 : 34091 : {
1195 : : /* e.g. "_1 = a_10(D) + 12;" */
1196 : 34091 : tree ptr = rhs1;
1197 : 34091 : tree offset = gimple_assign_rhs2 (assign);
1198 : :
1199 : 34091 : const svalue *ptr_sval = get_rvalue (ptr, ctxt);
1200 : 34091 : const svalue *offset_sval = get_rvalue (offset, ctxt);
1201 : : /* Quoting tree.def, "the second operand [of a POINTER_PLUS_EXPR]
1202 : : is an integer of type sizetype". */
1203 : 34091 : offset_sval = m_mgr->get_or_create_cast (size_type_node, offset_sval);
1204 : :
1205 : 34091 : const svalue *sval_binop
1206 : 34091 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1207 : : ptr_sval, offset_sval);
1208 : 34091 : return sval_binop;
1209 : : }
1210 : 720 : break;
1211 : :
1212 : 720 : case POINTER_DIFF_EXPR:
1213 : 720 : {
1214 : : /* e.g. "_1 = p_2(D) - q_3(D);". */
1215 : 720 : tree rhs2 = gimple_assign_rhs2 (assign);
1216 : 720 : const svalue *rhs1_sval = get_rvalue (rhs1, ctxt);
1217 : 720 : const svalue *rhs2_sval = get_rvalue (rhs2, ctxt);
1218 : :
1219 : : // TODO: perhaps fold to zero if they're known to be equal?
1220 : :
1221 : 720 : if (ctxt)
1222 : 630 : check_for_invalid_ptrdiff (assign, *ctxt, rhs1_sval, rhs2_sval);
1223 : :
1224 : 720 : const svalue *sval_binop
1225 : 720 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1226 : : rhs1_sval, rhs2_sval);
1227 : 720 : return sval_binop;
1228 : : }
1229 : 189484 : break;
1230 : :
1231 : : /* Assignments of the form
1232 : : set_value (lvalue (LHS), rvalue (EXPR))
1233 : : for various EXPR.
1234 : : We already have the lvalue for the LHS above, as "lhs_reg". */
1235 : 189484 : case ADDR_EXPR: /* LHS = &RHS; */
1236 : 189484 : case BIT_FIELD_REF:
1237 : 189484 : case COMPONENT_REF: /* LHS = op0.op1; */
1238 : 189484 : case MEM_REF:
1239 : 189484 : case REAL_CST:
1240 : 189484 : case COMPLEX_CST:
1241 : 189484 : case VECTOR_CST:
1242 : 189484 : case INTEGER_CST:
1243 : 189484 : case ARRAY_REF:
1244 : 189484 : case SSA_NAME: /* LHS = VAR; */
1245 : 189484 : case VAR_DECL: /* LHS = VAR; */
1246 : 189484 : case PARM_DECL:/* LHS = VAR; */
1247 : 189484 : case REALPART_EXPR:
1248 : 189484 : case IMAGPART_EXPR:
1249 : 189484 : return get_rvalue (rhs1, ctxt);
1250 : :
1251 : 49907 : case ABS_EXPR:
1252 : 49907 : case ABSU_EXPR:
1253 : 49907 : case CONJ_EXPR:
1254 : 49907 : case BIT_NOT_EXPR:
1255 : 49907 : case FIX_TRUNC_EXPR:
1256 : 49907 : case FLOAT_EXPR:
1257 : 49907 : case NEGATE_EXPR:
1258 : 49907 : case NOP_EXPR:
1259 : 49907 : case VIEW_CONVERT_EXPR:
1260 : 49907 : {
1261 : : /* Unary ops. */
1262 : 49907 : const svalue *rhs_sval = get_rvalue (rhs1, ctxt);
1263 : 49907 : const svalue *sval_unaryop
1264 : 49907 : = m_mgr->get_or_create_unaryop (TREE_TYPE (lhs), op, rhs_sval);
1265 : 49907 : return sval_unaryop;
1266 : : }
1267 : :
1268 : 14184 : case EQ_EXPR:
1269 : 14184 : case GE_EXPR:
1270 : 14184 : case LE_EXPR:
1271 : 14184 : case NE_EXPR:
1272 : 14184 : case GT_EXPR:
1273 : 14184 : case LT_EXPR:
1274 : 14184 : case UNORDERED_EXPR:
1275 : 14184 : case ORDERED_EXPR:
1276 : 14184 : {
1277 : 14184 : tree rhs2 = gimple_assign_rhs2 (assign);
1278 : :
1279 : 14184 : const svalue *rhs1_sval = get_rvalue (rhs1, ctxt);
1280 : 14184 : const svalue *rhs2_sval = get_rvalue (rhs2, ctxt);
1281 : :
1282 : 14184 : if (TREE_TYPE (lhs) == boolean_type_node)
1283 : : {
1284 : : /* Consider constraints between svalues. */
1285 : 14047 : tristate t = eval_condition (rhs1_sval, op, rhs2_sval);
1286 : 14047 : if (t.is_known ())
1287 : 7779 : return m_mgr->get_or_create_constant_svalue
1288 : 7779 : (t.is_true () ? boolean_true_node : boolean_false_node);
1289 : : }
1290 : :
1291 : : /* Otherwise, generate a symbolic binary op. */
1292 : 6405 : const svalue *sval_binop
1293 : 6405 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1294 : : rhs1_sval, rhs2_sval);
1295 : 6405 : return sval_binop;
1296 : : }
1297 : 55672 : break;
1298 : :
1299 : 55672 : case PLUS_EXPR:
1300 : 55672 : case MINUS_EXPR:
1301 : 55672 : case MULT_EXPR:
1302 : 55672 : case MULT_HIGHPART_EXPR:
1303 : 55672 : case TRUNC_DIV_EXPR:
1304 : 55672 : case CEIL_DIV_EXPR:
1305 : 55672 : case FLOOR_DIV_EXPR:
1306 : 55672 : case ROUND_DIV_EXPR:
1307 : 55672 : case TRUNC_MOD_EXPR:
1308 : 55672 : case CEIL_MOD_EXPR:
1309 : 55672 : case FLOOR_MOD_EXPR:
1310 : 55672 : case ROUND_MOD_EXPR:
1311 : 55672 : case RDIV_EXPR:
1312 : 55672 : case EXACT_DIV_EXPR:
1313 : 55672 : case LSHIFT_EXPR:
1314 : 55672 : case RSHIFT_EXPR:
1315 : 55672 : case LROTATE_EXPR:
1316 : 55672 : case RROTATE_EXPR:
1317 : 55672 : case BIT_IOR_EXPR:
1318 : 55672 : case BIT_XOR_EXPR:
1319 : 55672 : case BIT_AND_EXPR:
1320 : 55672 : case MIN_EXPR:
1321 : 55672 : case MAX_EXPR:
1322 : 55672 : case COMPLEX_EXPR:
1323 : 55672 : {
1324 : : /* Binary ops. */
1325 : 55672 : tree rhs2 = gimple_assign_rhs2 (assign);
1326 : :
1327 : 55672 : const svalue *rhs1_sval = get_rvalue (rhs1, ctxt);
1328 : 55672 : const svalue *rhs2_sval = get_rvalue (rhs2, ctxt);
1329 : :
1330 : 55672 : if (ctxt && (op == LSHIFT_EXPR || op == RSHIFT_EXPR))
1331 : : {
1332 : : /* "INT34-C. Do not shift an expression by a negative number of bits
1333 : : or by greater than or equal to the number of bits that exist in
1334 : : the operand." */
1335 : 7645 : if (const tree rhs2_cst = rhs2_sval->maybe_get_constant ())
1336 : 7419 : if (TREE_CODE (rhs2_cst) == INTEGER_CST
1337 : 7419 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1338 : : {
1339 : 7417 : if (tree_int_cst_sgn (rhs2_cst) < 0)
1340 : 16 : ctxt->warn
1341 : 32 : (std::make_unique<shift_count_negative_diagnostic>
1342 : 16 : (assign, rhs2_cst));
1343 : 7401 : else if (compare_tree_int (rhs2_cst,
1344 : 7401 : TYPE_PRECISION (TREE_TYPE (rhs1)))
1345 : : >= 0)
1346 : 8 : ctxt->warn
1347 : 16 : (std::make_unique<shift_count_overflow_diagnostic>
1348 : 8 : (assign,
1349 : 16 : int (TYPE_PRECISION (TREE_TYPE (rhs1))),
1350 : : rhs2_cst));
1351 : : }
1352 : : }
1353 : :
1354 : 55672 : const svalue *sval_binop
1355 : 55672 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1356 : : rhs1_sval, rhs2_sval);
1357 : 55672 : return sval_binop;
1358 : : }
1359 : :
1360 : : /* Vector expressions. In theory we could implement these elementwise,
1361 : : but for now, simply return unknown values. */
1362 : 0 : case VEC_DUPLICATE_EXPR:
1363 : 0 : case VEC_SERIES_EXPR:
1364 : 0 : case VEC_COND_EXPR:
1365 : 0 : case VEC_PERM_EXPR:
1366 : 0 : case VEC_WIDEN_MULT_HI_EXPR:
1367 : 0 : case VEC_WIDEN_MULT_LO_EXPR:
1368 : 0 : case VEC_WIDEN_MULT_EVEN_EXPR:
1369 : 0 : case VEC_WIDEN_MULT_ODD_EXPR:
1370 : 0 : case VEC_UNPACK_HI_EXPR:
1371 : 0 : case VEC_UNPACK_LO_EXPR:
1372 : 0 : case VEC_UNPACK_FLOAT_HI_EXPR:
1373 : 0 : case VEC_UNPACK_FLOAT_LO_EXPR:
1374 : 0 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1375 : 0 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1376 : 0 : case VEC_PACK_TRUNC_EXPR:
1377 : 0 : case VEC_PACK_SAT_EXPR:
1378 : 0 : case VEC_PACK_FIX_TRUNC_EXPR:
1379 : 0 : case VEC_PACK_FLOAT_EXPR:
1380 : 0 : case VEC_WIDEN_LSHIFT_HI_EXPR:
1381 : 0 : case VEC_WIDEN_LSHIFT_LO_EXPR:
1382 : 0 : return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (lhs));
1383 : : }
1384 : : }
1385 : :
1386 : : /* Workaround for discarding certain false positives from
1387 : : -Wanalyzer-use-of-uninitialized-value
1388 : : of the form:
1389 : : ((A OR-IF B) OR-IF C)
1390 : : and:
1391 : : ((A AND-IF B) AND-IF C)
1392 : : where evaluating B is redundant, but could involve simple accesses of
1393 : : uninitialized locals.
1394 : :
1395 : : When optimization is turned on the FE can immediately fold compound
1396 : : conditionals. Specifically, c_parser_condition parses this condition:
1397 : : ((A OR-IF B) OR-IF C)
1398 : : and calls c_fully_fold on the condition.
1399 : : Within c_fully_fold, fold_truth_andor is called, which bails when
1400 : : optimization is off, but if any optimization is turned on can convert the
1401 : : ((A OR-IF B) OR-IF C)
1402 : : into:
1403 : : ((A OR B) OR_IF C)
1404 : : for sufficiently simple B
1405 : : i.e. the inner OR-IF becomes an OR.
1406 : : At gimplification time the inner OR becomes BIT_IOR_EXPR (in gimplify_expr),
1407 : : giving this for the inner condition:
1408 : : tmp = A | B;
1409 : : if (tmp)
1410 : : thus effectively synthesizing a redundant access of B when optimization
1411 : : is turned on, when compared to:
1412 : : if (A) goto L1; else goto L4;
1413 : : L1: if (B) goto L2; else goto L4;
1414 : : L2: if (C) goto L3; else goto L4;
1415 : : for the unoptimized case.
1416 : :
1417 : : Return true if CTXT appears to be handling such a short-circuitable stmt,
1418 : : such as the def-stmt for B for the:
1419 : : tmp = A | B;
1420 : : case above, for the case where A is true and thus B would have been
1421 : : short-circuited without optimization, using MODEL for the value of A. */
1422 : :
1423 : : static bool
1424 : 1148 : within_short_circuited_stmt_p (const region_model *model,
1425 : : const gassign *assign_stmt)
1426 : : {
1427 : : /* We must have an assignment to a temporary of _Bool type. */
1428 : 1148 : tree lhs = gimple_assign_lhs (assign_stmt);
1429 : 1148 : if (TREE_TYPE (lhs) != boolean_type_node)
1430 : : return false;
1431 : 40 : if (TREE_CODE (lhs) != SSA_NAME)
1432 : : return false;
1433 : 40 : if (SSA_NAME_VAR (lhs) != NULL_TREE)
1434 : : return false;
1435 : :
1436 : : /* The temporary bool must be used exactly once: as the second arg of
1437 : : a BIT_IOR_EXPR or BIT_AND_EXPR. */
1438 : 40 : use_operand_p use_op;
1439 : 40 : gimple *use_stmt;
1440 : 40 : if (!single_imm_use (lhs, &use_op, &use_stmt))
1441 : : return false;
1442 : 1176 : const gassign *use_assign = dyn_cast <const gassign *> (use_stmt);
1443 : 40 : if (!use_assign)
1444 : : return false;
1445 : 40 : enum tree_code op = gimple_assign_rhs_code (use_assign);
1446 : 40 : if (!(op == BIT_IOR_EXPR ||op == BIT_AND_EXPR))
1447 : : return false;
1448 : 28 : if (!(gimple_assign_rhs1 (use_assign) != lhs
1449 : 28 : && gimple_assign_rhs2 (use_assign) == lhs))
1450 : : return false;
1451 : :
1452 : : /* The first arg of the bitwise stmt must have a known value in MODEL
1453 : : that implies that the value of the second arg doesn't matter, i.e.
1454 : : 1 for bitwise or, 0 for bitwise and. */
1455 : 28 : tree other_arg = gimple_assign_rhs1 (use_assign);
1456 : : /* Use a nullptr ctxt here to avoid generating warnings. */
1457 : 28 : const svalue *other_arg_sval = model->get_rvalue (other_arg, nullptr);
1458 : 28 : tree other_arg_cst = other_arg_sval->maybe_get_constant ();
1459 : 28 : if (!other_arg_cst)
1460 : : return false;
1461 : 12 : switch (op)
1462 : : {
1463 : 0 : default:
1464 : 0 : gcc_unreachable ();
1465 : 12 : case BIT_IOR_EXPR:
1466 : 12 : if (zerop (other_arg_cst))
1467 : : return false;
1468 : : break;
1469 : 0 : case BIT_AND_EXPR:
1470 : 0 : if (!zerop (other_arg_cst))
1471 : : return false;
1472 : : break;
1473 : : }
1474 : :
1475 : : /* All tests passed. We appear to be in a stmt that generates a boolean
1476 : : temporary with a value that won't matter. */
1477 : : return true;
1478 : : }
1479 : :
1480 : : /* Workaround for discarding certain false positives from
1481 : : -Wanalyzer-use-of-uninitialized-value
1482 : : seen with -ftrivial-auto-var-init=.
1483 : :
1484 : : -ftrivial-auto-var-init= will generate calls to IFN_DEFERRED_INIT.
1485 : :
1486 : : If the address of the var is taken, gimplification will give us
1487 : : something like:
1488 : :
1489 : : _1 = .DEFERRED_INIT (4, 2, &"len"[0]);
1490 : : len = _1;
1491 : :
1492 : : The result of DEFERRED_INIT will be an uninit value; we don't
1493 : : want to emit a false positive for "len = _1;"
1494 : :
1495 : : Return true if ASSIGN_STMT is such a stmt. */
1496 : :
1497 : : static bool
1498 : 1136 : due_to_ifn_deferred_init_p (const gassign *assign_stmt)
1499 : :
1500 : : {
1501 : : /* We must have an assignment to a decl from an SSA name that's the
1502 : : result of a IFN_DEFERRED_INIT call. */
1503 : 2102 : if (gimple_assign_rhs_code (assign_stmt) != SSA_NAME)
1504 : : return false;
1505 : 287 : tree lhs = gimple_assign_lhs (assign_stmt);
1506 : 287 : if (TREE_CODE (lhs) != VAR_DECL)
1507 : : return false;
1508 : 222 : tree rhs = gimple_assign_rhs1 (assign_stmt);
1509 : 222 : if (TREE_CODE (rhs) != SSA_NAME)
1510 : : return false;
1511 : 222 : const gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
1512 : 222 : const gcall *call = dyn_cast <const gcall *> (def_stmt);
1513 : 222 : if (!call)
1514 : : return false;
1515 : 222 : if (gimple_call_internal_p (call)
1516 : 222 : && gimple_call_internal_fn (call) == IFN_DEFERRED_INIT)
1517 : 210 : return true;
1518 : : return false;
1519 : : }
1520 : :
1521 : : /* Check for SVAL being poisoned, adding a warning to CTXT.
1522 : : Return SVAL, or, if a warning is added, another value, to avoid
1523 : : repeatedly complaining about the same poisoned value in followup code.
1524 : : SRC_REGION is a hint about where SVAL came from, and can be nullptr. */
1525 : :
1526 : : const svalue *
1527 : 3033182 : region_model::check_for_poison (const svalue *sval,
1528 : : tree expr,
1529 : : const region *src_region,
1530 : : region_model_context *ctxt) const
1531 : : {
1532 : 3033182 : if (!ctxt)
1533 : : return sval;
1534 : :
1535 : 1567582 : if (const poisoned_svalue *poisoned_sval = sval->dyn_cast_poisoned_svalue ())
1536 : : {
1537 : 2708 : enum poison_kind pkind = poisoned_sval->get_poison_kind ();
1538 : :
1539 : : /* Ignore uninitialized uses of empty types; there's nothing
1540 : : to initialize. */
1541 : 2708 : if (pkind == poison_kind::uninit
1542 : 2667 : && sval->get_type ()
1543 : 5265 : && is_empty_type (sval->get_type ()))
1544 : : return sval;
1545 : :
1546 : 2442 : if (pkind == poison_kind::uninit)
1547 : 2401 : if (const gimple *curr_stmt = ctxt->get_stmt ())
1548 : 1486 : if (const gassign *assign_stmt
1549 : 3368 : = dyn_cast <const gassign *> (curr_stmt))
1550 : : {
1551 : : /* Special case to avoid certain false positives. */
1552 : 1148 : if (within_short_circuited_stmt_p (this, assign_stmt))
1553 : : return sval;
1554 : :
1555 : : /* Special case to avoid false positive on
1556 : : -ftrivial-auto-var-init=. */
1557 : 1136 : if (due_to_ifn_deferred_init_p (assign_stmt))
1558 : : return sval;
1559 : : }
1560 : :
1561 : : /* If we have an SSA name for a temporary, we don't want to print
1562 : : '<unknown>'.
1563 : : Poisoned values are shared by type, and so we can't reconstruct
1564 : : the tree other than via the def stmts, using
1565 : : fixup_tree_for_diagnostic. */
1566 : 2220 : tree diag_arg = fixup_tree_for_diagnostic (expr);
1567 : 2220 : if (src_region == nullptr && pkind == poison_kind::uninit)
1568 : 2127 : src_region = get_region_for_poisoned_expr (expr);
1569 : :
1570 : : /* Can we reliably get the poisoned value from "expr"?
1571 : : This is for use by poisoned_value_diagnostic::check_valid_fpath_p.
1572 : : Unfortunately, we might not have a reliable value for EXPR.
1573 : : Hence we only query its value now, and only use it if we get the
1574 : : poisoned value back again. */
1575 : 2220 : tree check_expr = expr;
1576 : 2220 : const svalue *foo_sval = get_rvalue (expr, nullptr);
1577 : 2220 : if (foo_sval == sval)
1578 : : check_expr = expr;
1579 : : else
1580 : 110 : check_expr = nullptr;
1581 : 4440 : if (ctxt->warn
1582 : 2220 : (std::make_unique<poisoned_value_diagnostic> (diag_arg,
1583 : : pkind,
1584 : : src_region,
1585 : : check_expr)))
1586 : : {
1587 : : /* We only want to report use of a poisoned value at the first
1588 : : place it gets used; return an unknown value to avoid generating
1589 : : a chain of followup warnings. */
1590 : 1345 : sval = m_mgr->get_or_create_unknown_svalue (sval->get_type ());
1591 : : }
1592 : :
1593 : 2220 : return sval;
1594 : : }
1595 : :
1596 : : return sval;
1597 : : }
1598 : :
1599 : : /* Attempt to get a region for describing EXPR, the source of region of
1600 : : a poisoned_svalue for use in a poisoned_value_diagnostic.
1601 : : Return nullptr if there is no good region to use. */
1602 : :
1603 : : const region *
1604 : 2127 : region_model::get_region_for_poisoned_expr (tree expr) const
1605 : : {
1606 : 2127 : if (TREE_CODE (expr) == SSA_NAME)
1607 : : {
1608 : 1381 : tree decl = SSA_NAME_VAR (expr);
1609 : 1340 : if (decl && DECL_P (decl))
1610 : : expr = decl;
1611 : : else
1612 : : return nullptr;
1613 : : }
1614 : 2086 : return get_lvalue (expr, nullptr);
1615 : : }
1616 : :
1617 : : /* Update this model for the ASSIGN stmt, using CTXT to report any
1618 : : diagnostics. */
1619 : :
1620 : : void
1621 : 200373 : region_model::on_assignment (const gassign *assign, region_model_context *ctxt)
1622 : : {
1623 : 200373 : tree lhs = gimple_assign_lhs (assign);
1624 : 200373 : tree rhs1 = gimple_assign_rhs1 (assign);
1625 : :
1626 : 200373 : const region *lhs_reg = get_lvalue (lhs, ctxt);
1627 : :
1628 : : /* Any writes other than to the stack are treated
1629 : : as externally visible. */
1630 : 200373 : if (ctxt)
1631 : : {
1632 : 152405 : enum memory_space memspace = lhs_reg->get_memory_space ();
1633 : 152405 : if (memspace != MEMSPACE_STACK)
1634 : 12056 : ctxt->maybe_did_work ();
1635 : : }
1636 : :
1637 : : /* Most assignments are handled by:
1638 : : set_value (lhs_reg, SVALUE, CTXT)
1639 : : for some SVALUE. */
1640 : 200373 : if (const svalue *sval = get_gassign_result (assign, ctxt))
1641 : : {
1642 : 194261 : tree expr = get_diagnostic_tree_for_gassign (assign);
1643 : 194261 : check_for_poison (sval, expr, nullptr, ctxt);
1644 : 194261 : set_value (lhs_reg, sval, ctxt);
1645 : 194261 : return;
1646 : : }
1647 : :
1648 : 6112 : enum tree_code op = gimple_assign_rhs_code (assign);
1649 : 6112 : switch (op)
1650 : : {
1651 : 0 : default:
1652 : 0 : {
1653 : 0 : if (0)
1654 : : sorry_at (assign->location, "unhandled assignment op: %qs",
1655 : : get_tree_code_name (op));
1656 : 0 : const svalue *unknown_sval
1657 : 0 : = m_mgr->get_or_create_unknown_svalue (TREE_TYPE (lhs));
1658 : 0 : set_value (lhs_reg, unknown_sval, ctxt);
1659 : : }
1660 : 0 : break;
1661 : :
1662 : 5859 : case CONSTRUCTOR:
1663 : 5859 : {
1664 : 5859 : if (TREE_CLOBBER_P (rhs1))
1665 : : {
1666 : : /* e.g. "x ={v} {CLOBBER};" */
1667 : 5724 : clobber_region (lhs_reg);
1668 : : }
1669 : : else
1670 : : {
1671 : : /* Any CONSTRUCTOR that survives to this point is either
1672 : : just a zero-init of everything, or a vector. */
1673 : 135 : if (!CONSTRUCTOR_NO_CLEARING (rhs1))
1674 : 135 : zero_fill_region (lhs_reg, ctxt);
1675 : : unsigned ix;
1676 : : tree index;
1677 : : tree val;
1678 : 297 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), ix, index, val)
1679 : : {
1680 : 162 : gcc_assert (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE);
1681 : 162 : if (!index)
1682 : 22 : index = build_int_cst (integer_type_node, ix);
1683 : 162 : gcc_assert (TREE_CODE (index) == INTEGER_CST);
1684 : 162 : const svalue *index_sval
1685 : 162 : = m_mgr->get_or_create_constant_svalue (index);
1686 : 162 : gcc_assert (index_sval);
1687 : 162 : const region *sub_reg
1688 : 162 : = m_mgr->get_element_region (lhs_reg,
1689 : 162 : TREE_TYPE (val),
1690 : : index_sval);
1691 : 162 : const svalue *val_sval = get_rvalue (val, ctxt);
1692 : 162 : set_value (sub_reg, val_sval, ctxt);
1693 : : }
1694 : : }
1695 : : }
1696 : : break;
1697 : :
1698 : 253 : case STRING_CST:
1699 : 253 : {
1700 : : /* e.g. "struct s2 x = {{'A', 'B', 'C', 'D'}};". */
1701 : 253 : const svalue *rhs_sval = get_rvalue (rhs1, ctxt);
1702 : 387 : m_store.set_value (m_mgr->get_store_manager(), lhs_reg, rhs_sval,
1703 : 134 : ctxt ? ctxt->get_uncertainty () : nullptr);
1704 : : }
1705 : 253 : break;
1706 : : }
1707 : : }
1708 : :
1709 : : /* Handle the pre-sm-state part of STMT, modifying this object in-place.
1710 : : Write true to *OUT_UNKNOWN_SIDE_EFFECTS if the stmt has unknown
1711 : : side effects. */
1712 : :
1713 : : void
1714 : 275762 : region_model::on_stmt_pre (const gimple *stmt,
1715 : : bool *out_unknown_side_effects,
1716 : : region_model_context *ctxt)
1717 : : {
1718 : 275762 : switch (gimple_code (stmt))
1719 : : {
1720 : : case GIMPLE_COND:
1721 : : case GIMPLE_EH_DISPATCH:
1722 : : case GIMPLE_GOTO:
1723 : : case GIMPLE_LABEL:
1724 : : case GIMPLE_NOP:
1725 : : case GIMPLE_PREDICT:
1726 : : case GIMPLE_RESX:
1727 : : case GIMPLE_SWITCH:
1728 : : /* No-ops here. */
1729 : : break;
1730 : :
1731 : 200373 : case GIMPLE_ASSIGN:
1732 : 200373 : {
1733 : 200373 : const gassign *assign = as_a <const gassign *> (stmt);
1734 : 200373 : on_assignment (assign, ctxt);
1735 : : }
1736 : 200373 : break;
1737 : :
1738 : 384 : case GIMPLE_ASM:
1739 : 384 : {
1740 : 384 : const gasm *asm_stmt = as_a <const gasm *> (stmt);
1741 : 384 : on_asm_stmt (asm_stmt, ctxt);
1742 : 384 : if (ctxt)
1743 : 342 : ctxt->maybe_did_work ();
1744 : : }
1745 : : break;
1746 : :
1747 : 71620 : case GIMPLE_CALL:
1748 : 71620 : {
1749 : : /* Track whether we have a gcall to a function that's not recognized by
1750 : : anything, for which we don't have a function body, or for which we
1751 : : don't know the fndecl. */
1752 : 71620 : const gcall *call = as_a <const gcall *> (stmt);
1753 : 71620 : *out_unknown_side_effects = on_call_pre (*call, ctxt);
1754 : : }
1755 : 71620 : break;
1756 : :
1757 : 0 : case GIMPLE_RETURN:
1758 : 0 : {
1759 : 0 : const greturn *return_ = as_a <const greturn *> (stmt);
1760 : 0 : on_return (return_, ctxt);
1761 : : }
1762 : 0 : break;
1763 : :
1764 : : /* We don't expect to see any other statement kinds in the analyzer. */
1765 : 0 : case GIMPLE_DEBUG: // should have stripped these out when building the supergraph
1766 : 0 : default:
1767 : 0 : internal_error ("unexpected gimple stmt code: %qs",
1768 : 0 : gimple_code_name[gimple_code (stmt)]);
1769 : 275762 : break;
1770 : : }
1771 : 275762 : }
1772 : :
1773 : : /* Given a call CD with function attribute FORMAT_ATTR, check that the
1774 : : format arg to the call is a valid null-terminated string. */
1775 : :
1776 : : void
1777 : 1083 : region_model::check_call_format_attr (const call_details &cd,
1778 : : tree format_attr) const
1779 : : {
1780 : : /* We assume that FORMAT_ATTR has already been validated. */
1781 : :
1782 : : /* arg0 of the attribute should be kind of format strings
1783 : : that this function expects (e.g. "printf"). */
1784 : 1083 : const tree arg0_tree_list = TREE_VALUE (format_attr);
1785 : 1083 : if (!arg0_tree_list)
1786 : 0 : return;
1787 : :
1788 : : /* arg1 of the attribute should be the 1-based parameter index
1789 : : to treat as the format string. */
1790 : 1083 : const tree arg1_tree_list = TREE_CHAIN (arg0_tree_list);
1791 : 1083 : if (!arg1_tree_list)
1792 : : return;
1793 : 1083 : const tree arg1_value = TREE_VALUE (arg1_tree_list);
1794 : 1083 : if (!arg1_value)
1795 : : return;
1796 : :
1797 : 1083 : unsigned format_arg_idx = TREE_INT_CST_LOW (arg1_value) - 1;
1798 : 1083 : if (cd.num_args () <= format_arg_idx)
1799 : : return;
1800 : :
1801 : : /* Subclass of annotating_context that
1802 : : adds a note about the format attr to any saved diagnostics. */
1803 : 1083 : class annotating_ctxt : public annotating_context
1804 : : {
1805 : : public:
1806 : 1083 : annotating_ctxt (const call_details &cd,
1807 : : unsigned fmt_param_idx)
1808 : 1083 : : annotating_context (cd.get_ctxt ()),
1809 : 1083 : m_cd (cd),
1810 : 1083 : m_fmt_param_idx (fmt_param_idx)
1811 : : {
1812 : : }
1813 : 13 : void add_annotations () final override
1814 : : {
1815 : 0 : class reason_format_attr
1816 : : : public pending_note_subclass<reason_format_attr>
1817 : : {
1818 : : public:
1819 : 13 : reason_format_attr (const call_arg_details &arg_details)
1820 : 13 : : m_arg_details (arg_details)
1821 : : {
1822 : : }
1823 : :
1824 : 74 : const char *get_kind () const final override
1825 : : {
1826 : 74 : return "reason_format_attr";
1827 : : }
1828 : :
1829 : 13 : void emit () const final override
1830 : : {
1831 : 13 : inform (DECL_SOURCE_LOCATION (m_arg_details.m_called_fndecl),
1832 : : "parameter %i of %qD marked as a format string"
1833 : : " via %qs attribute",
1834 : 13 : m_arg_details.m_arg_idx + 1, m_arg_details.m_called_fndecl,
1835 : : "format");
1836 : 13 : }
1837 : :
1838 : 37 : bool operator== (const reason_format_attr &other) const
1839 : : {
1840 : 37 : return m_arg_details == other.m_arg_details;
1841 : : }
1842 : :
1843 : : private:
1844 : : call_arg_details m_arg_details;
1845 : : };
1846 : :
1847 : 13 : call_arg_details arg_details (m_cd, m_fmt_param_idx);
1848 : 13 : add_note (std::make_unique<reason_format_attr> (arg_details));
1849 : 13 : }
1850 : : private:
1851 : : const call_details &m_cd;
1852 : : unsigned m_fmt_param_idx;
1853 : : };
1854 : :
1855 : 1083 : annotating_ctxt my_ctxt (cd, format_arg_idx);
1856 : 1083 : call_details my_cd (cd, &my_ctxt);
1857 : 1083 : my_cd.check_for_null_terminated_string_arg (format_arg_idx);
1858 : : }
1859 : :
1860 : : /* Ensure that all arguments at the call described by CD are checked
1861 : : for poisoned values, by calling get_rvalue on each argument.
1862 : :
1863 : : Check that calls to functions with "format" attribute have valid
1864 : : null-terminated strings for their format argument. */
1865 : :
1866 : : void
1867 : 48754 : region_model::check_call_args (const call_details &cd) const
1868 : : {
1869 : 112589 : for (unsigned arg_idx = 0; arg_idx < cd.num_args (); arg_idx++)
1870 : 63835 : cd.get_arg_svalue (arg_idx);
1871 : :
1872 : : /* Handle attribute "format". */
1873 : 48754 : if (tree format_attr = cd.lookup_function_attribute ("format"))
1874 : 1083 : check_call_format_attr (cd, format_attr);
1875 : 48754 : }
1876 : :
1877 : : /* Update this model for an outcome of a call that returns a specific
1878 : : integer constant.
1879 : : If UNMERGEABLE, then make the result unmergeable, e.g. to prevent
1880 : : the state-merger code from merging success and failure outcomes. */
1881 : :
1882 : : void
1883 : 889 : region_model::update_for_int_cst_return (const call_details &cd,
1884 : : int retval,
1885 : : bool unmergeable)
1886 : : {
1887 : 889 : if (!cd.get_lhs_type ())
1888 : : return;
1889 : 635 : if (TREE_CODE (cd.get_lhs_type ()) != INTEGER_TYPE)
1890 : : return;
1891 : 629 : const svalue *result
1892 : 629 : = m_mgr->get_or_create_int_cst (cd.get_lhs_type (), retval);
1893 : 629 : if (unmergeable)
1894 : 629 : result = m_mgr->get_or_create_unmergeable (result);
1895 : 629 : set_value (cd.get_lhs_region (), result, cd.get_ctxt ());
1896 : : }
1897 : :
1898 : : /* Update this model for an outcome of a call that returns zero.
1899 : : If UNMERGEABLE, then make the result unmergeable, e.g. to prevent
1900 : : the state-merger code from merging success and failure outcomes. */
1901 : :
1902 : : void
1903 : 286 : region_model::update_for_zero_return (const call_details &cd,
1904 : : bool unmergeable)
1905 : : {
1906 : 286 : update_for_int_cst_return (cd, 0, unmergeable);
1907 : 286 : }
1908 : :
1909 : : /* Update this model for an outcome of a call that returns non-zero.
1910 : : Specifically, assign an svalue to the LHS, and add a constraint that
1911 : : that svalue is non-zero. */
1912 : :
1913 : : void
1914 : 133 : region_model::update_for_nonzero_return (const call_details &cd)
1915 : : {
1916 : 133 : if (!cd.get_lhs_type ())
1917 : : return;
1918 : 97 : if (TREE_CODE (cd.get_lhs_type ()) != INTEGER_TYPE)
1919 : : return;
1920 : 97 : cd.set_any_lhs_with_defaults ();
1921 : 97 : const svalue *zero
1922 : 97 : = m_mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
1923 : 97 : const svalue *result
1924 : 97 : = get_store_value (cd.get_lhs_region (), cd.get_ctxt ());
1925 : 97 : add_constraint (result, NE_EXPR, zero, cd.get_ctxt ());
1926 : : }
1927 : :
1928 : : /* Subroutine of region_model::maybe_get_copy_bounds.
1929 : : The Linux kernel commonly uses
1930 : : min_t([unsigned] long, VAR, sizeof(T));
1931 : : to set an upper bound on the size of a copy_to_user.
1932 : : Attempt to simplify such sizes by trying to get the upper bound as a
1933 : : constant.
1934 : : Return the simplified svalue if possible, or nullptr otherwise. */
1935 : :
1936 : : static const svalue *
1937 : 53 : maybe_simplify_upper_bound (const svalue *num_bytes_sval,
1938 : : region_model_manager *mgr)
1939 : : {
1940 : 53 : tree type = num_bytes_sval->get_type ();
1941 : 70 : while (const svalue *raw = num_bytes_sval->maybe_undo_cast ())
1942 : : num_bytes_sval = raw;
1943 : 53 : if (const binop_svalue *binop_sval = num_bytes_sval->dyn_cast_binop_svalue ())
1944 : 38 : if (binop_sval->get_op () == MIN_EXPR)
1945 : 8 : if (binop_sval->get_arg1 ()->get_kind () == SK_CONSTANT)
1946 : : {
1947 : 8 : return mgr->get_or_create_cast (type, binop_sval->get_arg1 ());
1948 : : /* TODO: we might want to also capture the constraint
1949 : : when recording the diagnostic, or note that we're using
1950 : : the upper bound. */
1951 : : }
1952 : : return nullptr;
1953 : : }
1954 : :
1955 : : /* Attempt to get an upper bound for the size of a copy when simulating a
1956 : : copy function.
1957 : :
1958 : : NUM_BYTES_SVAL is the symbolic value for the size of the copy.
1959 : : Use it if it's constant, otherwise try to simplify it. Failing
1960 : : that, use the size of SRC_REG if constant.
1961 : :
1962 : : Return a symbolic value for an upper limit on the number of bytes
1963 : : copied, or nullptr if no such value could be determined. */
1964 : :
1965 : : const svalue *
1966 : 144 : region_model::maybe_get_copy_bounds (const region *src_reg,
1967 : : const svalue *num_bytes_sval)
1968 : : {
1969 : 144 : if (num_bytes_sval->maybe_get_constant ())
1970 : : return num_bytes_sval;
1971 : :
1972 : 106 : if (const svalue *simplified
1973 : 53 : = maybe_simplify_upper_bound (num_bytes_sval, m_mgr))
1974 : 8 : num_bytes_sval = simplified;
1975 : :
1976 : 53 : if (num_bytes_sval->maybe_get_constant ())
1977 : : return num_bytes_sval;
1978 : :
1979 : : /* For now, try just guessing the size as the capacity of the
1980 : : base region of the src.
1981 : : This is a hack; we might get too large a value. */
1982 : 45 : const region *src_base_reg = src_reg->get_base_region ();
1983 : 45 : num_bytes_sval = get_capacity (src_base_reg);
1984 : :
1985 : 45 : if (num_bytes_sval->maybe_get_constant ())
1986 : 11 : return num_bytes_sval;
1987 : :
1988 : : /* Non-constant: give up. */
1989 : : return nullptr;
1990 : : }
1991 : :
1992 : : /* Get any known_function for FNDECL for call CD.
1993 : :
1994 : : The call must match all assumptions made by the known_function (such as
1995 : : e.g. "argument 1's type must be a pointer type").
1996 : :
1997 : : Return nullptr if no known_function is found, or it does not match the
1998 : : assumption(s). */
1999 : :
2000 : : const known_function *
2001 : 292642 : region_model::get_known_function (tree fndecl, const call_details &cd) const
2002 : : {
2003 : 292642 : known_function_manager *known_fn_mgr = m_mgr->get_known_function_manager ();
2004 : 292642 : return known_fn_mgr->get_match (fndecl, cd);
2005 : : }
2006 : :
2007 : : /* Get any known_function for IFN, or nullptr. */
2008 : :
2009 : : const known_function *
2010 : 1383 : region_model::get_known_function (enum internal_fn ifn) const
2011 : : {
2012 : 1383 : known_function_manager *known_fn_mgr = m_mgr->get_known_function_manager ();
2013 : 1383 : return known_fn_mgr->get_internal_fn (ifn);
2014 : : }
2015 : :
2016 : : /* Get any builtin_known_function for CALL and emit any warning to CTXT
2017 : : if not nullptr.
2018 : :
2019 : : The call must match all assumptions made by the known_function (such as
2020 : : e.g. "argument 1's type must be a pointer type").
2021 : :
2022 : : Return nullptr if no builtin_known_function is found, or it does
2023 : : not match the assumption(s).
2024 : :
2025 : : Internally calls get_known_function to find a known_function and cast it
2026 : : to a builtin_known_function.
2027 : :
2028 : : For instance, calloc is a C builtin, defined in gcc/builtins.def
2029 : : by the DEF_LIB_BUILTIN macro. Such builtins are recognized by the
2030 : : analyzer by their name, so that even in C++ or if the user redeclares
2031 : : them but mismatch their signature, they are still recognized as builtins.
2032 : :
2033 : : Cases when a supposed builtin is not flagged as one by the FE:
2034 : :
2035 : : The C++ FE does not recognize calloc as a builtin if it has not been
2036 : : included from a standard header, but the C FE does. Hence in C++ if
2037 : : CALL comes from a calloc and stdlib is not included,
2038 : : gcc/tree.h:fndecl_built_in_p (CALL) would be false.
2039 : :
2040 : : In C code, a __SIZE_TYPE__ calloc (__SIZE_TYPE__, __SIZE_TYPE__) user
2041 : : declaration has obviously a mismatching signature from the standard, and
2042 : : its function_decl tree won't be unified by
2043 : : gcc/c-decl.cc:match_builtin_function_types.
2044 : :
2045 : : Yet in both cases the analyzer should treat the calls as a builtin calloc
2046 : : so that extra attributes unspecified by the standard but added by GCC
2047 : : (e.g. sprintf attributes in gcc/builtins.def), useful for the detection of
2048 : : dangerous behavior, are indeed processed.
2049 : :
2050 : : Therefore for those cases when a "builtin flag" is not added by the FE,
2051 : : builtins' kf are derived from builtin_known_function, whose method
2052 : : builtin_known_function::builtin_decl returns the builtin's
2053 : : function_decl tree as defined in gcc/builtins.def, with all the extra
2054 : : attributes. */
2055 : :
2056 : : const builtin_known_function *
2057 : 161796 : region_model::get_builtin_kf (const gcall &call,
2058 : : region_model_context *ctxt /* = nullptr */) const
2059 : : {
2060 : 161796 : region_model *mut_this = const_cast <region_model *> (this);
2061 : 161796 : tree callee_fndecl = mut_this->get_fndecl_for_call (call, ctxt);
2062 : 161796 : if (! callee_fndecl)
2063 : : return nullptr;
2064 : :
2065 : 161796 : call_details cd (call, mut_this, ctxt);
2066 : 161796 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2067 : 110218 : return kf->dyn_cast_builtin_kf ();
2068 : :
2069 : : return nullptr;
2070 : : }
2071 : :
2072 : : /* Subclass of custom_edge_info for use by exploded_edges that represent
2073 : : an exception being thrown from a call we don't have the code for. */
2074 : :
2075 : : class exception_thrown_from_unrecognized_call : public custom_edge_info
2076 : : {
2077 : : public:
2078 : 5495 : exception_thrown_from_unrecognized_call (const gcall &call,
2079 : : tree fndecl)
2080 : 5495 : : m_call (call),
2081 : 5495 : m_fndecl (fndecl)
2082 : : {
2083 : : }
2084 : :
2085 : 12 : void print (pretty_printer *pp) const final override
2086 : : {
2087 : 12 : if (m_fndecl)
2088 : 12 : pp_printf (pp, "if %qD throws an exception...", m_fndecl);
2089 : : else
2090 : 0 : pp_printf (pp, "if the called function throws an exception...");
2091 : 12 : };
2092 : :
2093 : : bool
2094 : 5495 : update_model (region_model *model,
2095 : : const exploded_edge *,
2096 : : region_model_context *ctxt) const final override
2097 : : {
2098 : : /* Allocate an exception and set it as the current exception. */
2099 : 5495 : const region *exception_reg
2100 : : = model->get_or_create_region_for_heap_alloc
2101 : 5495 : (nullptr, /* We don't know the size of the region. */
2102 : : ctxt);
2103 : :
2104 : 5495 : region_model_manager *mgr = model->get_manager ();
2105 : 5495 : conjured_purge p (model, ctxt);
2106 : :
2107 : : /* The contents of the region are some conjured svalue. */
2108 : 5495 : const svalue *exception_sval
2109 : 10990 : = mgr->get_or_create_conjured_svalue (NULL_TREE,
2110 : 5495 : &m_call,
2111 : : exception_reg, p, 0);
2112 : 5495 : model->set_value (exception_reg, exception_sval, ctxt);
2113 : 5495 : const svalue *exception_ptr_sval
2114 : 5495 : = mgr->get_ptr_svalue (ptr_type_node, exception_reg);
2115 : 5495 : const svalue *tinfo_sval
2116 : 10990 : = mgr->get_or_create_conjured_svalue (ptr_type_node,
2117 : 5495 : &m_call,
2118 : : exception_reg, p, 1);
2119 : 5495 : const svalue *destructor_sval
2120 : 10990 : = mgr->get_or_create_conjured_svalue (ptr_type_node,
2121 : 5495 : &m_call,
2122 : : exception_reg, p, 2);
2123 : :
2124 : : /* Push a new exception_node on the model's thrown exception stack. */
2125 : 5495 : exception_node eh_node (exception_ptr_sval, tinfo_sval, destructor_sval);
2126 : 5495 : model->push_thrown_exception (eh_node);
2127 : :
2128 : 5495 : return true;
2129 : : }
2130 : :
2131 : : void
2132 : 22 : add_events_to_path (checker_path *emission_path,
2133 : : const exploded_edge &eedge,
2134 : : pending_diagnostic &) const final override
2135 : : {
2136 : 22 : const exploded_node *dst_node = eedge.m_dest;
2137 : 22 : const program_point &dst_point = dst_node->get_point ();
2138 : 22 : const int dst_stack_depth = dst_point.get_stack_depth ();
2139 : :
2140 : 22 : emission_path->add_event
2141 : 22 : (std::make_unique<throw_from_call_to_external_fn_event>
2142 : 22 : (event_loc_info (m_call.location,
2143 : : dst_point.get_fndecl (),
2144 : 22 : dst_stack_depth),
2145 : : dst_node,
2146 : : m_call,
2147 : 22 : m_fndecl));
2148 : 22 : }
2149 : :
2150 : : exploded_node *
2151 : 5388 : create_enode (exploded_graph &eg,
2152 : : const program_point &point,
2153 : : program_state &&state,
2154 : : exploded_node *enode_for_diag,
2155 : : region_model_context *ctxt) const final override
2156 : : {
2157 : 5388 : exploded_node *thrown_enode
2158 : 5388 : = eg.get_or_create_node (point, state, enode_for_diag,
2159 : : /* Don't add to worklist. */
2160 : : false);
2161 : 5388 : if (!thrown_enode)
2162 : : return nullptr;
2163 : :
2164 : : /* Add successor edges for thrown_enode "by hand" for the exception. */
2165 : 5292 : eg.unwind_from_exception (*thrown_enode,
2166 : 5292 : &m_call,
2167 : : ctxt);
2168 : 5292 : return thrown_enode;
2169 : : }
2170 : :
2171 : : private:
2172 : : const gcall &m_call;
2173 : : tree m_fndecl; // could be null
2174 : : };
2175 : :
2176 : : /* Get a set of functions that are assumed to not throw exceptions. */
2177 : :
2178 : : static function_set
2179 : 5362 : get_fns_assumed_not_to_throw ()
2180 : : {
2181 : : // TODO: populate this list more fully
2182 : 5362 : static const char * const fn_names[] = {
2183 : : /* This array must be kept sorted. */
2184 : :
2185 : : "fclose"
2186 : : };
2187 : 5362 : const size_t count = ARRAY_SIZE (fn_names);
2188 : 5362 : function_set fs (fn_names, count);
2189 : 5362 : return fs;
2190 : : }
2191 : :
2192 : : /* Return true if CALL could throw an exception.
2193 : : FNDECL could be NULL_TREE. */
2194 : :
2195 : : static bool
2196 : 12812 : can_throw_p (const gcall &call, tree fndecl)
2197 : : {
2198 : 12812 : if (!flag_exceptions)
2199 : : return false;
2200 : :
2201 : 6118 : if (gimple_call_nothrow_p (&call))
2202 : : return false;
2203 : :
2204 : 5502 : if (fndecl)
2205 : : {
2206 : 5362 : const function_set fs = get_fns_assumed_not_to_throw ();
2207 : 5362 : if (fs.contains_decl_p (fndecl))
2208 : 7 : return false;
2209 : : }
2210 : :
2211 : : return true;
2212 : : }
2213 : :
2214 : : /* Given CALL where we don't know what code is being called
2215 : : (by not having the body of FNDECL, or having NULL_TREE for FNDECL),
2216 : : potentially bifurcate control flow to simulate the call throwing
2217 : : an exception. */
2218 : :
2219 : : void
2220 : 17882 : region_model::check_for_throw_inside_call (const gcall &call,
2221 : : tree fndecl,
2222 : : region_model_context *ctxt)
2223 : : {
2224 : 17882 : if (!ctxt)
2225 : 12387 : return;
2226 : :
2227 : : /* Could this function throw an exception?
2228 : : If so, add an extra e-edge for that. */
2229 : 12812 : if (!can_throw_p (call, fndecl))
2230 : : return;
2231 : :
2232 : 5495 : auto throws_exception
2233 : 5495 : = std::make_unique<exception_thrown_from_unrecognized_call> (call, fndecl);
2234 : 5495 : ctxt->bifurcate (std::move (throws_exception));
2235 : 5495 : }
2236 : :
2237 : : /* A subclass of pending_diagnostic for complaining about jumps through NULL
2238 : : function pointers. */
2239 : :
2240 : : class jump_through_null : public pending_diagnostic_subclass<jump_through_null>
2241 : : {
2242 : : public:
2243 : 16 : jump_through_null (const gcall &call)
2244 : 16 : : m_call (call)
2245 : : {}
2246 : :
2247 : 152 : const char *get_kind () const final override
2248 : : {
2249 : 152 : return "jump_through_null";
2250 : : }
2251 : :
2252 : 16 : bool operator== (const jump_through_null &other) const
2253 : : {
2254 : 16 : return &m_call == &other.m_call;
2255 : : }
2256 : :
2257 : 32 : int get_controlling_option () const final override
2258 : : {
2259 : 32 : return OPT_Wanalyzer_jump_through_null;
2260 : : }
2261 : :
2262 : 16 : bool emit (diagnostic_emission_context &ctxt) final override
2263 : : {
2264 : 16 : return ctxt.warn ("jump through null pointer");
2265 : : }
2266 : :
2267 : 32 : bool describe_final_event (pretty_printer &pp,
2268 : : const evdesc::final_event &) final override
2269 : : {
2270 : 32 : pp_string (&pp, "jump through null pointer here");
2271 : 32 : return true;
2272 : : }
2273 : :
2274 : : private:
2275 : : const gcall &m_call;
2276 : : };
2277 : : /* Update this model for the CALL stmt, using CTXT to report any
2278 : : diagnostics - the first half.
2279 : :
2280 : : Updates to the region_model that should be made *before* sm-states
2281 : : are updated are done here; other updates to the region_model are done
2282 : : in region_model::on_call_post.
2283 : :
2284 : : Return true if the function call has unknown side effects (it wasn't
2285 : : recognized and we don't have a body for it, or are unable to tell which
2286 : : fndecl it is). */
2287 : :
2288 : : bool
2289 : 71620 : region_model::on_call_pre (const gcall &call, region_model_context *ctxt)
2290 : : {
2291 : 71620 : call_details cd (call, this, ctxt);
2292 : :
2293 : : /* Special-case for IFN_DEFERRED_INIT.
2294 : : We want to report uninitialized variables with -fanalyzer (treating
2295 : : -ftrivial-auto-var-init= as purely a mitigation feature).
2296 : : Handle IFN_DEFERRED_INIT by treating it as no-op: don't touch the
2297 : : lhs of the call, so that it is still uninitialized from the point of
2298 : : view of the analyzer. */
2299 : 71620 : if (gimple_call_internal_p (&call)
2300 : 71620 : && gimple_call_internal_fn (&call) == IFN_DEFERRED_INIT)
2301 : : return false; /* No side effects. */
2302 : :
2303 : : /* Get svalues for all of the arguments at the callsite, to ensure that we
2304 : : complain about any uninitialized arguments. This might lead to
2305 : : duplicates if any of the handling below also looks up the svalues,
2306 : : but the deduplication code should deal with that. */
2307 : 67396 : if (ctxt)
2308 : 48754 : check_call_args (cd);
2309 : :
2310 : 67396 : tree callee_fndecl = get_fndecl_for_call (call, ctxt);
2311 : :
2312 : 67396 : if (gimple_call_internal_p (&call))
2313 : 2766 : if (const known_function *kf
2314 : 1383 : = get_known_function (gimple_call_internal_fn (&call)))
2315 : : {
2316 : 1353 : kf->impl_call_pre (cd);
2317 : 1353 : return false; /* No further side effects. */
2318 : : }
2319 : :
2320 : 66043 : if (!callee_fndecl)
2321 : : {
2322 : : /* Check for jump through nullptr. */
2323 : 524 : if (ctxt)
2324 : 452 : if (tree fn_ptr = gimple_call_fn (&call))
2325 : : {
2326 : 426 : const svalue *fn_ptr_sval = get_rvalue (fn_ptr, ctxt);
2327 : 426 : if (fn_ptr_sval->all_zeroes_p ())
2328 : : {
2329 : 16 : ctxt->warn
2330 : 16 : (std::make_unique<jump_through_null> (call));
2331 : 16 : ctxt->terminate_path ();
2332 : 16 : return true;
2333 : : }
2334 : : }
2335 : :
2336 : 508 : check_for_throw_inside_call (call, NULL_TREE, ctxt);
2337 : 508 : cd.set_any_lhs_with_defaults ();
2338 : 508 : return true; /* Unknown side effects. */
2339 : : }
2340 : :
2341 : 65519 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2342 : : {
2343 : 46001 : kf->impl_call_pre (cd);
2344 : 46001 : return false; /* No further side effects. */
2345 : : }
2346 : :
2347 : 19518 : cd.set_any_lhs_with_defaults ();
2348 : :
2349 : 19518 : const int callee_fndecl_flags = flags_from_decl_or_type (callee_fndecl);
2350 : 19518 : if (callee_fndecl_flags & (ECF_CONST | ECF_PURE))
2351 : : return false; /* No side effects. */
2352 : :
2353 : 18174 : if (fndecl_built_in_p (callee_fndecl))
2354 : : return true; /* Unknown side effects. */
2355 : :
2356 : 17374 : if (!fndecl_has_gimple_body_p (callee_fndecl))
2357 : : {
2358 : 17374 : check_for_throw_inside_call (call, callee_fndecl, ctxt);
2359 : 17374 : return true; /* Unknown side effects. */
2360 : : }
2361 : :
2362 : : return false; /* No side effects. */
2363 : : }
2364 : :
2365 : : /* Update this model for the CALL stmt, using CTXT to report any
2366 : : diagnostics - the second half.
2367 : :
2368 : : Updates to the region_model that should be made *after* sm-states
2369 : : are updated are done here; other updates to the region_model are done
2370 : : in region_model::on_call_pre.
2371 : :
2372 : : If UNKNOWN_SIDE_EFFECTS is true, also call handle_unrecognized_call
2373 : : to purge state. */
2374 : :
2375 : : void
2376 : 71381 : region_model::on_call_post (const gcall &call,
2377 : : bool unknown_side_effects,
2378 : : region_model_context *ctxt)
2379 : : {
2380 : 71381 : if (tree callee_fndecl = get_fndecl_for_call (call, ctxt))
2381 : : {
2382 : 65327 : call_details cd (call, this, ctxt);
2383 : 65327 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2384 : : {
2385 : 45861 : kf->impl_call_post (cd);
2386 : 92061 : return;
2387 : : }
2388 : : /* Was this fndecl referenced by
2389 : : __attribute__((malloc(FOO)))? */
2390 : 19466 : if (lookup_attribute ("*dealloc", DECL_ATTRIBUTES (callee_fndecl)))
2391 : : {
2392 : 339 : impl_deallocation_call (cd);
2393 : 339 : return;
2394 : : }
2395 : : }
2396 : :
2397 : 25181 : if (unknown_side_effects)
2398 : : {
2399 : 17256 : handle_unrecognized_call (call, ctxt);
2400 : 17256 : if (ctxt)
2401 : 12307 : ctxt->maybe_did_work ();
2402 : : }
2403 : : }
2404 : :
2405 : : /* Purge state involving SVAL from this region_model, using CTXT
2406 : : (if non-NULL) to purge other state in a program_state.
2407 : :
2408 : : For example, if we're at the def-stmt of an SSA name, then we need to
2409 : : purge any state for svalues that involve that SSA name. This avoids
2410 : : false positives in loops, since a symbolic value referring to the
2411 : : SSA name will be referring to the previous value of that SSA name.
2412 : :
2413 : : For example, in:
2414 : : while ((e = hashmap_iter_next(&iter))) {
2415 : : struct oid2strbuf *e_strbuf = (struct oid2strbuf *)e;
2416 : : free (e_strbuf->value);
2417 : : }
2418 : : at the def-stmt of e_8:
2419 : : e_8 = hashmap_iter_next (&iter);
2420 : : we should purge the "freed" state of:
2421 : : INIT_VAL(CAST_REG(‘struct oid2strbuf’, (*INIT_VAL(e_8))).value)
2422 : : which is the "e_strbuf->value" value from the previous iteration,
2423 : : or we will erroneously report a double-free - the "e_8" within it
2424 : : refers to the previous value. */
2425 : :
2426 : : void
2427 : 28343 : region_model::purge_state_involving (const svalue *sval,
2428 : : region_model_context *ctxt)
2429 : : {
2430 : 28343 : if (!sval->can_have_associated_state_p ())
2431 : : return;
2432 : 28343 : m_store.purge_state_involving (sval, m_mgr);
2433 : 28343 : m_constraints->purge_state_involving (sval);
2434 : 28343 : m_dynamic_extents.purge_state_involving (sval);
2435 : 28343 : if (ctxt)
2436 : 17958 : ctxt->purge_state_involving (sval);
2437 : : }
2438 : :
2439 : : /* A pending_note subclass for adding a note about an
2440 : : __attribute__((access, ...)) to a diagnostic. */
2441 : :
2442 : : class reason_attr_access : public pending_note_subclass<reason_attr_access>
2443 : : {
2444 : : public:
2445 : 22 : reason_attr_access (tree callee_fndecl, const attr_access &access)
2446 : 22 : : m_callee_fndecl (callee_fndecl),
2447 : 22 : m_ptr_argno (access.ptrarg),
2448 : 22 : m_access_str (TREE_STRING_POINTER (access.to_external_string ()))
2449 : : {
2450 : 22 : }
2451 : :
2452 : 116 : const char *get_kind () const final override { return "reason_attr_access"; }
2453 : :
2454 : 18 : void emit () const final override
2455 : : {
2456 : 18 : auto_urlify_attributes sentinel;
2457 : 18 : inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
2458 : : "parameter %i of %qD marked with attribute %qs",
2459 : 18 : m_ptr_argno + 1, m_callee_fndecl, m_access_str);
2460 : 18 : }
2461 : :
2462 : 58 : bool operator== (const reason_attr_access &other) const
2463 : : {
2464 : 58 : return (m_callee_fndecl == other.m_callee_fndecl
2465 : 22 : && m_ptr_argno == other.m_ptr_argno
2466 : 80 : && !strcmp (m_access_str, other.m_access_str));
2467 : : }
2468 : :
2469 : : private:
2470 : : tree m_callee_fndecl;
2471 : : unsigned m_ptr_argno;
2472 : : const char *m_access_str;
2473 : : };
2474 : :
2475 : : /* Check CALL a call to external function CALLEE_FNDECL based on
2476 : : any __attribute__ ((access, ....) on the latter, complaining to
2477 : : CTXT about any issues.
2478 : :
2479 : : Currently we merely call check_region_for_write on any regions
2480 : : pointed to by arguments marked with a "write_only" or "read_write"
2481 : : attribute. */
2482 : :
2483 : : void
2484 : 1247 : region_model::check_function_attr_access (const gcall &call,
2485 : : tree callee_fndecl,
2486 : : region_model_context *ctxt,
2487 : : rdwr_map &rdwr_idx) const
2488 : : {
2489 : 1247 : gcc_assert (callee_fndecl);
2490 : 1247 : gcc_assert (ctxt);
2491 : :
2492 : 1247 : tree fntype = TREE_TYPE (callee_fndecl);
2493 : 1247 : gcc_assert (fntype);
2494 : :
2495 : 1247 : unsigned argno = 0;
2496 : :
2497 : 4751 : for (tree iter = TYPE_ARG_TYPES (fntype); iter;
2498 : 3504 : iter = TREE_CHAIN (iter), ++argno)
2499 : : {
2500 : 3504 : const attr_access* access = rdwr_idx.get (argno);
2501 : 3504 : if (!access)
2502 : 3226 : continue;
2503 : :
2504 : : /* Ignore any duplicate entry in the map for the size argument. */
2505 : 278 : if (access->ptrarg != argno)
2506 : 114 : continue;
2507 : :
2508 : 164 : if (access->mode == access_write_only
2509 : 164 : || access->mode == access_read_write)
2510 : : {
2511 : : /* Subclass of annotating_context that
2512 : : adds a note about the attr access to any saved diagnostics. */
2513 : 40 : class annotating_ctxt : public annotating_context
2514 : : {
2515 : : public:
2516 : 40 : annotating_ctxt (tree callee_fndecl,
2517 : : const attr_access &access,
2518 : : region_model_context *ctxt)
2519 : 40 : : annotating_context (ctxt),
2520 : 40 : m_callee_fndecl (callee_fndecl),
2521 : 40 : m_access (access)
2522 : : {
2523 : : }
2524 : 22 : void add_annotations () final override
2525 : : {
2526 : 22 : add_note (std::make_unique<reason_attr_access>
2527 : 22 : (m_callee_fndecl, m_access));
2528 : 22 : }
2529 : : private:
2530 : : tree m_callee_fndecl;
2531 : : const attr_access &m_access;
2532 : : };
2533 : :
2534 : : /* Use this ctxt below so that any diagnostics get the
2535 : : note added to them. */
2536 : 40 : annotating_ctxt my_ctxt (callee_fndecl, *access, ctxt);
2537 : :
2538 : 40 : tree ptr_tree = gimple_call_arg (&call, access->ptrarg);
2539 : 40 : const svalue *ptr_sval = get_rvalue (ptr_tree, &my_ctxt);
2540 : 40 : const region *reg = deref_rvalue (ptr_sval, ptr_tree, &my_ctxt);
2541 : 40 : check_region_for_write (reg, nullptr, &my_ctxt);
2542 : : /* We don't use the size arg for now. */
2543 : : }
2544 : : }
2545 : 1247 : }
2546 : :
2547 : : /* Subroutine of region_model::check_function_attr_null_terminated_string_arg,
2548 : : checking one instance of __attribute__((null_terminated_string_arg)). */
2549 : :
2550 : : void
2551 : 200 : region_model::
2552 : : check_one_function_attr_null_terminated_string_arg (const gcall &call,
2553 : : tree callee_fndecl,
2554 : : region_model_context *ctxt,
2555 : : rdwr_map &rdwr_idx,
2556 : : tree attr)
2557 : : {
2558 : 200 : gcc_assert (callee_fndecl);
2559 : 200 : gcc_assert (ctxt);
2560 : 200 : gcc_assert (attr);
2561 : :
2562 : 200 : tree arg = TREE_VALUE (attr);
2563 : 200 : if (!arg)
2564 : 76 : return;
2565 : :
2566 : : /* Convert from 1-based to 0-based index. */
2567 : 200 : unsigned int arg_idx = TREE_INT_CST_LOW (TREE_VALUE (arg)) - 1;
2568 : :
2569 : : /* If there's also an "access" attribute on the ptr param
2570 : : for reading with a size param specified, then that size
2571 : : limits the size of the possible read from the pointer. */
2572 : 200 : if (const attr_access* access = rdwr_idx.get (arg_idx))
2573 : 104 : if ((access->mode == access_read_only
2574 : 104 : || access->mode == access_read_write)
2575 : 104 : && access->sizarg != UINT_MAX)
2576 : : {
2577 : 76 : call_details cd_checked (call, this, ctxt);
2578 : 76 : const svalue *limit_sval
2579 : 76 : = cd_checked.get_arg_svalue (access->sizarg);
2580 : 76 : const svalue *ptr_sval
2581 : 76 : = cd_checked.get_arg_svalue (arg_idx);
2582 : : /* Try reading all of the bytes expressed by the size param,
2583 : : but without emitting warnings (via a null context). */
2584 : 76 : const svalue *limited_sval
2585 : 76 : = read_bytes (deref_rvalue (ptr_sval, NULL_TREE, nullptr),
2586 : : NULL_TREE,
2587 : : limit_sval,
2588 : : nullptr);
2589 : 76 : if (limited_sval->get_kind () == SK_POISONED)
2590 : : {
2591 : : /* Reading up to the truncation limit caused issues.
2592 : : Assume that the string is meant to be terminated
2593 : : before then, so perform a *checked* check for the
2594 : : terminator. */
2595 : 24 : check_for_null_terminated_string_arg (cd_checked,
2596 : : arg_idx);
2597 : : }
2598 : : else
2599 : : {
2600 : : /* Reading up to the truncation limit seems OK; repeat
2601 : : the read, but with checking enabled. */
2602 : 52 : read_bytes (deref_rvalue (ptr_sval, NULL_TREE, ctxt),
2603 : : NULL_TREE,
2604 : : limit_sval,
2605 : : ctxt);
2606 : : }
2607 : 76 : return;
2608 : : }
2609 : :
2610 : : /* Otherwise, we don't have an access-attribute limiting the read.
2611 : : Simulate a read up to the null terminator (if any). */
2612 : :
2613 : 124 : call_details cd (call, this, ctxt);
2614 : 124 : check_for_null_terminated_string_arg (cd, arg_idx);
2615 : : }
2616 : :
2617 : : /* Check CALL a call to external function CALLEE_FNDECL for any uses
2618 : : of __attribute__ ((null_terminated_string_arg)), compaining
2619 : : to CTXT about any issues.
2620 : :
2621 : : Use RDWR_IDX for tracking uses of __attribute__ ((access, ....). */
2622 : :
2623 : : void
2624 : 1247 : region_model::
2625 : : check_function_attr_null_terminated_string_arg (const gcall &call,
2626 : : tree callee_fndecl,
2627 : : region_model_context *ctxt,
2628 : : rdwr_map &rdwr_idx)
2629 : : {
2630 : 1247 : gcc_assert (callee_fndecl);
2631 : 1247 : gcc_assert (ctxt);
2632 : :
2633 : 1247 : tree fntype = TREE_TYPE (callee_fndecl);
2634 : 1247 : gcc_assert (fntype);
2635 : :
2636 : : /* A function declaration can specify multiple attribute
2637 : : null_terminated_string_arg, each with one argument. */
2638 : 1447 : for (tree attr = TYPE_ATTRIBUTES (fntype); attr; attr = TREE_CHAIN (attr))
2639 : : {
2640 : 1271 : attr = lookup_attribute ("null_terminated_string_arg", attr);
2641 : 1271 : if (!attr)
2642 : : return;
2643 : :
2644 : 200 : check_one_function_attr_null_terminated_string_arg (call, callee_fndecl,
2645 : : ctxt, rdwr_idx,
2646 : : attr);
2647 : : }
2648 : : }
2649 : :
2650 : : /* Check CALL a call to external function CALLEE_FNDECL for any
2651 : : function attributes, complaining to CTXT about any issues. */
2652 : :
2653 : : void
2654 : 11876 : region_model::check_function_attrs (const gcall &call,
2655 : : tree callee_fndecl,
2656 : : region_model_context *ctxt)
2657 : : {
2658 : 11876 : gcc_assert (callee_fndecl);
2659 : 11876 : gcc_assert (ctxt);
2660 : :
2661 : 11876 : tree fntype = TREE_TYPE (callee_fndecl);
2662 : 11876 : if (!fntype)
2663 : 10629 : return;
2664 : :
2665 : 11876 : if (!TYPE_ATTRIBUTES (fntype))
2666 : : return;
2667 : :
2668 : : /* Initialize a map of attribute access specifications for arguments
2669 : : to the function call. */
2670 : 1247 : rdwr_map rdwr_idx;
2671 : 1247 : init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype));
2672 : :
2673 : 1247 : check_function_attr_access (call, callee_fndecl, ctxt, rdwr_idx);
2674 : 1247 : check_function_attr_null_terminated_string_arg (call, callee_fndecl,
2675 : : ctxt, rdwr_idx);
2676 : 1247 : }
2677 : :
2678 : : /* Handle a call CALL to a function with unknown behavior.
2679 : :
2680 : : Traverse the regions in this model, determining what regions are
2681 : : reachable from pointer arguments to CALL and from global variables,
2682 : : recursively.
2683 : :
2684 : : Set all reachable regions to new unknown values and purge sm-state
2685 : : from their values, and from values that point to them. */
2686 : :
2687 : : void
2688 : 17256 : region_model::handle_unrecognized_call (const gcall &call,
2689 : : region_model_context *ctxt)
2690 : : {
2691 : 17256 : tree fndecl = get_fndecl_for_call (call, ctxt);
2692 : :
2693 : 17256 : if (fndecl && ctxt)
2694 : 11876 : check_function_attrs (call, fndecl, ctxt);
2695 : :
2696 : 17256 : reachable_regions reachable_regs (this);
2697 : :
2698 : : /* Determine the reachable regions and their mutability. */
2699 : 17256 : {
2700 : : /* Add globals and regions that already escaped in previous
2701 : : unknown calls. */
2702 : 17256 : m_store.for_each_cluster (reachable_regions::init_cluster_cb,
2703 : : &reachable_regs);
2704 : :
2705 : : /* Params that are pointers. */
2706 : 17256 : tree iter_param_types = NULL_TREE;
2707 : 17256 : if (fndecl)
2708 : 16753 : iter_param_types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2709 : 36455 : for (unsigned arg_idx = 0; arg_idx < gimple_call_num_args (&call);
2710 : : arg_idx++)
2711 : : {
2712 : : /* Track expected param type, where available. */
2713 : 19199 : tree param_type = NULL_TREE;
2714 : 19199 : if (iter_param_types)
2715 : : {
2716 : 17793 : param_type = TREE_VALUE (iter_param_types);
2717 : 17793 : gcc_assert (param_type);
2718 : 17793 : iter_param_types = TREE_CHAIN (iter_param_types);
2719 : : }
2720 : :
2721 : 19199 : tree parm = gimple_call_arg (&call, arg_idx);
2722 : 19199 : const svalue *parm_sval = get_rvalue (parm, ctxt);
2723 : 19199 : reachable_regs.handle_parm (parm_sval, param_type);
2724 : : }
2725 : : }
2726 : :
2727 : 17256 : uncertainty_t *uncertainty = ctxt ? ctxt->get_uncertainty () : nullptr;
2728 : :
2729 : : /* Purge sm-state for the svalues that were reachable,
2730 : : both in non-mutable and mutable form. */
2731 : 45395 : for (svalue_set::iterator iter
2732 : 17256 : = reachable_regs.begin_reachable_svals ();
2733 : 73534 : iter != reachable_regs.end_reachable_svals (); ++iter)
2734 : : {
2735 : 28139 : const svalue *sval = (*iter);
2736 : 28139 : if (ctxt)
2737 : 22249 : ctxt->on_unknown_change (sval, false);
2738 : : }
2739 : 57708 : for (svalue_set::iterator iter
2740 : 17256 : = reachable_regs.begin_mutable_svals ();
2741 : 98160 : iter != reachable_regs.end_mutable_svals (); ++iter)
2742 : : {
2743 : 40452 : const svalue *sval = (*iter);
2744 : 40452 : if (ctxt)
2745 : 32668 : ctxt->on_unknown_change (sval, true);
2746 : 40452 : if (uncertainty)
2747 : 31465 : uncertainty->on_mutable_sval_at_unknown_call (sval);
2748 : : }
2749 : :
2750 : : /* Mark any clusters that have escaped. */
2751 : 17256 : reachable_regs.mark_escaped_clusters (ctxt);
2752 : :
2753 : : /* Update bindings for all clusters that have escaped, whether above,
2754 : : or previously. */
2755 : 17256 : m_store.on_unknown_fncall (call, m_mgr->get_store_manager (),
2756 : 17256 : conjured_purge (this, ctxt));
2757 : :
2758 : : /* Purge dynamic extents from any regions that have escaped mutably:
2759 : : realloc could have been called on them. */
2760 : 42763 : for (hash_set<const region *>::iterator
2761 : 17256 : iter = reachable_regs.begin_mutable_base_regs ();
2762 : 42763 : iter != reachable_regs.end_mutable_base_regs ();
2763 : 25507 : ++iter)
2764 : : {
2765 : 25507 : const region *base_reg = (*iter);
2766 : 25507 : unset_dynamic_extents (base_reg);
2767 : : }
2768 : 17256 : }
2769 : :
2770 : : /* Traverse the regions in this model, determining what regions are
2771 : : reachable from the store and populating *OUT.
2772 : :
2773 : : If EXTRA_SVAL is non-NULL, treat it as an additional "root"
2774 : : for reachability (for handling return values from functions when
2775 : : analyzing return of the only function on the stack).
2776 : :
2777 : : If UNCERTAINTY is non-NULL, treat any svalues that were recorded
2778 : : within it as being maybe-bound as additional "roots" for reachability.
2779 : :
2780 : : Find svalues that haven't leaked. */
2781 : :
2782 : : void
2783 : 929864 : region_model::get_reachable_svalues (svalue_set *out,
2784 : : const svalue *extra_sval,
2785 : : const uncertainty_t *uncertainty)
2786 : : {
2787 : 929864 : reachable_regions reachable_regs (this);
2788 : :
2789 : : /* Add globals and regions that already escaped in previous
2790 : : unknown calls. */
2791 : 929864 : m_store.for_each_cluster (reachable_regions::init_cluster_cb,
2792 : : &reachable_regs);
2793 : :
2794 : 929864 : if (extra_sval)
2795 : 5175 : reachable_regs.handle_sval (extra_sval);
2796 : :
2797 : 929864 : if (uncertainty)
2798 : 428120 : for (uncertainty_t::iterator iter
2799 : 412170 : = uncertainty->begin_maybe_bound_svals ();
2800 : 856240 : iter != uncertainty->end_maybe_bound_svals (); ++iter)
2801 : 15950 : reachable_regs.handle_sval (*iter);
2802 : :
2803 : : /* Get regions for locals that have explicitly bound values. */
2804 : 8556933 : for (store::cluster_map_t::iterator iter = m_store.begin ();
2805 : 16184002 : iter != m_store.end (); ++iter)
2806 : : {
2807 : 7627069 : const region *base_reg = (*iter).first;
2808 : 7627069 : if (const region *parent = base_reg->get_parent_region ())
2809 : 7627069 : if (parent->get_kind () == RK_FRAME)
2810 : 4726082 : reachable_regs.add (base_reg, false);
2811 : : }
2812 : :
2813 : : /* Populate *OUT based on the values that were reachable. */
2814 : 929864 : for (svalue_set::iterator iter
2815 : 929864 : = reachable_regs.begin_reachable_svals ();
2816 : 15539506 : iter != reachable_regs.end_reachable_svals (); ++iter)
2817 : 7304821 : out->add (*iter);
2818 : 929864 : }
2819 : :
2820 : : /* Update this model for the RETURN_STMT, using CTXT to report any
2821 : : diagnostics. */
2822 : :
2823 : : void
2824 : 0 : region_model::on_return (const greturn *return_stmt, region_model_context *ctxt)
2825 : : {
2826 : 0 : tree callee = get_current_function ()->decl;
2827 : 0 : tree lhs = DECL_RESULT (callee);
2828 : 0 : tree rhs = gimple_return_retval (return_stmt);
2829 : :
2830 : 0 : if (lhs && rhs)
2831 : : {
2832 : 0 : const svalue *sval = get_rvalue (rhs, ctxt);
2833 : 0 : const region *ret_reg = get_lvalue (lhs, ctxt);
2834 : 0 : set_value (ret_reg, sval, ctxt);
2835 : : }
2836 : 0 : }
2837 : :
2838 : : /* Update this model for a call and return of setjmp/sigsetjmp at CALL within
2839 : : ENODE, using CTXT to report any diagnostics.
2840 : :
2841 : : This is for the initial direct invocation of setjmp/sigsetjmp (which returns
2842 : : 0), as opposed to any second return due to longjmp/sigsetjmp. */
2843 : :
2844 : : void
2845 : 34 : region_model::on_setjmp (const gcall &call,
2846 : : const exploded_node &enode,
2847 : : const superedge &sedge,
2848 : : region_model_context *ctxt)
2849 : : {
2850 : 34 : const svalue *buf_ptr = get_rvalue (gimple_call_arg (&call, 0), ctxt);
2851 : 34 : const region *buf_reg = deref_rvalue (buf_ptr, gimple_call_arg (&call, 0),
2852 : : ctxt);
2853 : :
2854 : : /* Create a setjmp_svalue for this call and store it in BUF_REG's
2855 : : region. */
2856 : 34 : if (buf_reg)
2857 : : {
2858 : 34 : setjmp_record r (&enode, &sedge, call);
2859 : 34 : const svalue *sval
2860 : 34 : = m_mgr->get_or_create_setjmp_svalue (r, buf_reg->get_type ());
2861 : 34 : set_value (buf_reg, sval, ctxt);
2862 : : }
2863 : :
2864 : : /* Direct calls to setjmp return 0. */
2865 : 34 : if (tree lhs = gimple_call_lhs (&call))
2866 : : {
2867 : 16 : const svalue *new_sval
2868 : 16 : = m_mgr->get_or_create_int_cst (TREE_TYPE (lhs), 0);
2869 : 16 : const region *lhs_reg = get_lvalue (lhs, ctxt);
2870 : 16 : set_value (lhs_reg, new_sval, ctxt);
2871 : : }
2872 : 34 : }
2873 : :
2874 : : /* Update this region_model for rewinding from a "longjmp" at LONGJMP_CALL
2875 : : to a "setjmp" at SETJMP_CALL where the final stack depth should be
2876 : : SETJMP_STACK_DEPTH. Pop any stack frames. Leak detection is *not*
2877 : : done, and should be done by the caller. */
2878 : :
2879 : : void
2880 : 31 : region_model::on_longjmp (const gcall &longjmp_call, const gcall &setjmp_call,
2881 : : int setjmp_stack_depth, region_model_context *ctxt)
2882 : : {
2883 : : /* Evaluate the val, using the frame of the "longjmp". */
2884 : 31 : tree fake_retval = gimple_call_arg (&longjmp_call, 1);
2885 : 31 : const svalue *fake_retval_sval = get_rvalue (fake_retval, ctxt);
2886 : :
2887 : : /* Pop any frames until we reach the stack depth of the function where
2888 : : setjmp was called. */
2889 : 31 : gcc_assert (get_stack_depth () >= setjmp_stack_depth);
2890 : 61 : while (get_stack_depth () > setjmp_stack_depth)
2891 : 30 : pop_frame (nullptr, nullptr, ctxt, nullptr, false);
2892 : :
2893 : 31 : gcc_assert (get_stack_depth () == setjmp_stack_depth);
2894 : :
2895 : : /* Assign to LHS of "setjmp" in new_state. */
2896 : 31 : if (tree lhs = gimple_call_lhs (&setjmp_call))
2897 : : {
2898 : : /* Passing 0 as the val to longjmp leads to setjmp returning 1. */
2899 : 27 : const svalue *zero_sval
2900 : 27 : = m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 0);
2901 : 27 : tristate eq_zero = eval_condition (fake_retval_sval, EQ_EXPR, zero_sval);
2902 : : /* If we have 0, use 1. */
2903 : 27 : if (eq_zero.is_true ())
2904 : : {
2905 : 2 : const svalue *one_sval
2906 : 2 : = m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 1);
2907 : 2 : fake_retval_sval = one_sval;
2908 : : }
2909 : : else
2910 : : {
2911 : : /* Otherwise note that the value is nonzero. */
2912 : 25 : m_constraints->add_constraint (fake_retval_sval, NE_EXPR, zero_sval);
2913 : : }
2914 : :
2915 : : /* Decorate the return value from setjmp as being unmergeable,
2916 : : so that we don't attempt to merge states with it as zero
2917 : : with states in which it's nonzero, leading to a clean distinction
2918 : : in the exploded_graph betweeen the first return and the second
2919 : : return. */
2920 : 27 : fake_retval_sval = m_mgr->get_or_create_unmergeable (fake_retval_sval);
2921 : :
2922 : 27 : const region *lhs_reg = get_lvalue (lhs, ctxt);
2923 : 27 : set_value (lhs_reg, fake_retval_sval, ctxt);
2924 : : }
2925 : 31 : }
2926 : :
2927 : : /* Implementation of region_model::get_lvalue; the latter adds type-checking.
2928 : :
2929 : : Get the id of the region for PV within this region_model,
2930 : : emitting any diagnostics to CTXT. */
2931 : :
2932 : : const region *
2933 : 2350291 : region_model::get_lvalue_1 (path_var pv, region_model_context *ctxt) const
2934 : : {
2935 : 2350291 : tree expr = pv.m_tree;
2936 : :
2937 : 2350291 : gcc_assert (expr);
2938 : :
2939 : 2350291 : switch (TREE_CODE (expr))
2940 : : {
2941 : 50 : default:
2942 : 50 : return m_mgr->get_region_for_unexpected_tree_code (ctxt, expr,
2943 : 50 : dump_location_t ());
2944 : :
2945 : 17310 : case ARRAY_REF:
2946 : 17310 : {
2947 : 17310 : tree array = TREE_OPERAND (expr, 0);
2948 : 17310 : tree index = TREE_OPERAND (expr, 1);
2949 : :
2950 : 17310 : const region *array_reg = get_lvalue (array, ctxt);
2951 : 17310 : const svalue *index_sval = get_rvalue (index, ctxt);
2952 : 17310 : return m_mgr->get_element_region (array_reg,
2953 : 17310 : TREE_TYPE (TREE_TYPE (array)),
2954 : 17310 : index_sval);
2955 : : }
2956 : 189 : break;
2957 : :
2958 : 189 : case BIT_FIELD_REF:
2959 : 189 : {
2960 : 189 : tree inner_expr = TREE_OPERAND (expr, 0);
2961 : 189 : const region *inner_reg = get_lvalue (inner_expr, ctxt);
2962 : 189 : tree num_bits = TREE_OPERAND (expr, 1);
2963 : 189 : tree first_bit_offset = TREE_OPERAND (expr, 2);
2964 : 189 : gcc_assert (TREE_CODE (num_bits) == INTEGER_CST);
2965 : 189 : gcc_assert (TREE_CODE (first_bit_offset) == INTEGER_CST);
2966 : 189 : bit_range bits (TREE_INT_CST_LOW (first_bit_offset),
2967 : 189 : TREE_INT_CST_LOW (num_bits));
2968 : 189 : return m_mgr->get_bit_range (inner_reg, TREE_TYPE (expr), bits);
2969 : : }
2970 : 69185 : break;
2971 : :
2972 : 69185 : case MEM_REF:
2973 : 69185 : {
2974 : 69185 : tree ptr = TREE_OPERAND (expr, 0);
2975 : 69185 : tree offset = TREE_OPERAND (expr, 1);
2976 : 69185 : const svalue *ptr_sval = get_rvalue (ptr, ctxt);
2977 : 69185 : const svalue *offset_sval = get_rvalue (offset, ctxt);
2978 : 69185 : const region *star_ptr = deref_rvalue (ptr_sval, ptr, ctxt);
2979 : 69185 : return m_mgr->get_offset_region (star_ptr,
2980 : 69185 : TREE_TYPE (expr),
2981 : 69185 : offset_sval);
2982 : : }
2983 : 873869 : break;
2984 : :
2985 : 873869 : case FUNCTION_DECL:
2986 : 873869 : return m_mgr->get_region_for_fndecl (expr);
2987 : :
2988 : 250 : case LABEL_DECL:
2989 : 250 : return m_mgr->get_region_for_label (expr);
2990 : :
2991 : 138773 : case VAR_DECL:
2992 : : /* Handle globals. */
2993 : 138773 : if (is_global_var (expr))
2994 : 54587 : return m_mgr->get_region_for_global (expr);
2995 : :
2996 : : /* Fall through. */
2997 : :
2998 : 1273014 : case SSA_NAME:
2999 : 1273014 : case PARM_DECL:
3000 : 1273014 : case RESULT_DECL:
3001 : 1273014 : {
3002 : 1273014 : gcc_assert (TREE_CODE (expr) == SSA_NAME
3003 : : || TREE_CODE (expr) == PARM_DECL
3004 : : || VAR_P (expr)
3005 : : || TREE_CODE (expr) == RESULT_DECL);
3006 : :
3007 : 1273014 : int stack_index = pv.m_stack_depth;
3008 : 1273014 : const frame_region *frame = get_frame_at_index (stack_index);
3009 : 1273014 : gcc_assert (frame);
3010 : 1273014 : return frame->get_region_for_local (m_mgr, expr, ctxt);
3011 : : }
3012 : :
3013 : 46471 : case COMPONENT_REF:
3014 : 46471 : {
3015 : : /* obj.field */
3016 : 46471 : tree obj = TREE_OPERAND (expr, 0);
3017 : 46471 : tree field = TREE_OPERAND (expr, 1);
3018 : 46471 : const region *obj_reg = get_lvalue (obj, ctxt);
3019 : 46471 : return m_mgr->get_field_region (obj_reg, field);
3020 : : }
3021 : 15366 : break;
3022 : :
3023 : 15366 : case STRING_CST:
3024 : 15366 : return m_mgr->get_region_for_string (expr);
3025 : : }
3026 : : }
3027 : :
3028 : : /* Assert that SRC_TYPE can be converted to DST_TYPE as a no-op. */
3029 : :
3030 : : static void
3031 : 5196329 : assert_compat_types (tree src_type, tree dst_type)
3032 : : {
3033 : 5196329 : if (src_type && dst_type && !VOID_TYPE_P (dst_type))
3034 : : {
3035 : : #if CHECKING_P
3036 : 5196037 : if (!(useless_type_conversion_p (src_type, dst_type)))
3037 : 0 : internal_error ("incompatible types: %qT and %qT", src_type, dst_type);
3038 : : #endif
3039 : : }
3040 : 5196329 : }
3041 : :
3042 : : /* Return true if SRC_TYPE can be converted to DST_TYPE as a no-op. */
3043 : :
3044 : : bool
3045 : 13486 : compat_types_p (tree src_type, tree dst_type)
3046 : : {
3047 : 13486 : if (src_type && dst_type && !VOID_TYPE_P (dst_type))
3048 : 13486 : if (!(useless_type_conversion_p (src_type, dst_type)))
3049 : : return false;
3050 : : return true;
3051 : : }
3052 : :
3053 : : /* Get the region for PV within this region_model,
3054 : : emitting any diagnostics to CTXT. */
3055 : :
3056 : : const region *
3057 : 2350291 : region_model::get_lvalue (path_var pv, region_model_context *ctxt) const
3058 : : {
3059 : 2350291 : if (pv.m_tree == NULL_TREE)
3060 : : return nullptr;
3061 : :
3062 : 2350291 : const region *result_reg = get_lvalue_1 (pv, ctxt);
3063 : 2350291 : assert_compat_types (result_reg->get_type (), TREE_TYPE (pv.m_tree));
3064 : 2350291 : return result_reg;
3065 : : }
3066 : :
3067 : : /* Get the region for EXPR within this region_model (assuming the most
3068 : : recent stack frame if it's a local). */
3069 : :
3070 : : const region *
3071 : 1472994 : region_model::get_lvalue (tree expr, region_model_context *ctxt) const
3072 : : {
3073 : 1472994 : return get_lvalue (path_var (expr, get_stack_depth () - 1), ctxt);
3074 : : }
3075 : :
3076 : : /* Implementation of region_model::get_rvalue; the latter adds type-checking.
3077 : :
3078 : : Get the value of PV within this region_model,
3079 : : emitting any diagnostics to CTXT. */
3080 : :
3081 : : const svalue *
3082 : 2832968 : region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt) const
3083 : : {
3084 : 2832968 : gcc_assert (pv.m_tree);
3085 : :
3086 : 2832968 : switch (TREE_CODE (pv.m_tree))
3087 : : {
3088 : 45 : default:
3089 : 45 : return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (pv.m_tree));
3090 : :
3091 : 932149 : case ADDR_EXPR:
3092 : 932149 : {
3093 : : /* "&EXPR". */
3094 : 932149 : tree expr = pv.m_tree;
3095 : 932149 : tree op0 = TREE_OPERAND (expr, 0);
3096 : 932149 : const region *expr_reg = get_lvalue (op0, ctxt);
3097 : 932149 : return m_mgr->get_ptr_svalue (TREE_TYPE (expr), expr_reg);
3098 : : }
3099 : 130 : break;
3100 : :
3101 : 130 : case BIT_FIELD_REF:
3102 : 130 : {
3103 : 130 : tree expr = pv.m_tree;
3104 : 130 : tree op0 = TREE_OPERAND (expr, 0);
3105 : 130 : const region *reg = get_lvalue (op0, ctxt);
3106 : 130 : tree num_bits = TREE_OPERAND (expr, 1);
3107 : 130 : tree first_bit_offset = TREE_OPERAND (expr, 2);
3108 : 130 : gcc_assert (TREE_CODE (num_bits) == INTEGER_CST);
3109 : 130 : gcc_assert (TREE_CODE (first_bit_offset) == INTEGER_CST);
3110 : 130 : bit_range bits (TREE_INT_CST_LOW (first_bit_offset),
3111 : 130 : TREE_INT_CST_LOW (num_bits));
3112 : 130 : return get_rvalue_for_bits (TREE_TYPE (expr), reg, bits, ctxt);
3113 : : }
3114 : :
3115 : 38293 : case VAR_DECL:
3116 : 38293 : if (DECL_HARD_REGISTER (pv.m_tree))
3117 : : {
3118 : : /* If it has a hard register, it doesn't have a memory region
3119 : : and can't be referred to as an lvalue. */
3120 : 43 : return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (pv.m_tree));
3121 : : }
3122 : : /* Fall through. */
3123 : 827228 : case PARM_DECL:
3124 : 827228 : case SSA_NAME:
3125 : 827228 : case RESULT_DECL:
3126 : 827228 : case ARRAY_REF:
3127 : 827228 : {
3128 : 827228 : const region *reg = get_lvalue (pv, ctxt);
3129 : 827228 : return get_store_value (reg, ctxt);
3130 : : }
3131 : :
3132 : 130 : case REALPART_EXPR:
3133 : 130 : case IMAGPART_EXPR:
3134 : 130 : case VIEW_CONVERT_EXPR:
3135 : 130 : {
3136 : 130 : tree expr = pv.m_tree;
3137 : 130 : tree arg = TREE_OPERAND (expr, 0);
3138 : 130 : const svalue *arg_sval = get_rvalue (arg, ctxt);
3139 : 130 : const svalue *sval_unaryop
3140 : 130 : = m_mgr->get_or_create_unaryop (TREE_TYPE (expr), TREE_CODE (expr),
3141 : : arg_sval);
3142 : 130 : return sval_unaryop;
3143 : 1020529 : };
3144 : :
3145 : 1020529 : case INTEGER_CST:
3146 : 1020529 : case REAL_CST:
3147 : 1020529 : case COMPLEX_CST:
3148 : 1020529 : case VECTOR_CST:
3149 : 1020529 : case STRING_CST:
3150 : 1020529 : case RAW_DATA_CST:
3151 : 1020529 : return m_mgr->get_or_create_constant_svalue (pv.m_tree);
3152 : :
3153 : 8 : case POINTER_PLUS_EXPR:
3154 : 8 : {
3155 : 8 : tree expr = pv.m_tree;
3156 : 8 : tree ptr = TREE_OPERAND (expr, 0);
3157 : 8 : tree offset = TREE_OPERAND (expr, 1);
3158 : 8 : const svalue *ptr_sval = get_rvalue (ptr, ctxt);
3159 : 8 : const svalue *offset_sval = get_rvalue (offset, ctxt);
3160 : 8 : const svalue *sval_binop
3161 : 8 : = m_mgr->get_or_create_binop (TREE_TYPE (expr), POINTER_PLUS_EXPR,
3162 : : ptr_sval, offset_sval);
3163 : 8 : return sval_binop;
3164 : : }
3165 : :
3166 : : /* Binary ops. */
3167 : 94 : case PLUS_EXPR:
3168 : 94 : case MULT_EXPR:
3169 : 94 : case BIT_AND_EXPR:
3170 : 94 : case BIT_IOR_EXPR:
3171 : 94 : case BIT_XOR_EXPR:
3172 : 94 : {
3173 : 94 : tree expr = pv.m_tree;
3174 : 94 : tree arg0 = TREE_OPERAND (expr, 0);
3175 : 94 : tree arg1 = TREE_OPERAND (expr, 1);
3176 : 94 : const svalue *arg0_sval = get_rvalue (arg0, ctxt);
3177 : 94 : const svalue *arg1_sval = get_rvalue (arg1, ctxt);
3178 : 94 : const svalue *sval_binop
3179 : 94 : = m_mgr->get_or_create_binop (TREE_TYPE (expr), TREE_CODE (expr),
3180 : : arg0_sval, arg1_sval);
3181 : 94 : return sval_binop;
3182 : : }
3183 : :
3184 : 50029 : case COMPONENT_REF:
3185 : 50029 : case MEM_REF:
3186 : 50029 : {
3187 : 50029 : const region *ref_reg = get_lvalue (pv, ctxt);
3188 : 50029 : return get_store_value (ref_reg, ctxt);
3189 : : }
3190 : 2583 : case OBJ_TYPE_REF:
3191 : 2583 : {
3192 : 2583 : tree expr = OBJ_TYPE_REF_EXPR (pv.m_tree);
3193 : 2583 : return get_rvalue (expr, ctxt);
3194 : : }
3195 : : }
3196 : : }
3197 : :
3198 : : /* Get the value of PV within this region_model,
3199 : : emitting any diagnostics to CTXT. */
3200 : :
3201 : : const svalue *
3202 : 2869499 : region_model::get_rvalue (path_var pv, region_model_context *ctxt) const
3203 : : {
3204 : 2869499 : if (pv.m_tree == NULL_TREE)
3205 : : return nullptr;
3206 : :
3207 : 2832968 : const svalue *result_sval = get_rvalue_1 (pv, ctxt);
3208 : :
3209 : 2832968 : assert_compat_types (result_sval->get_type (), TREE_TYPE (pv.m_tree));
3210 : :
3211 : 2832968 : result_sval = check_for_poison (result_sval, pv.m_tree, nullptr, ctxt);
3212 : :
3213 : 2832968 : return result_sval;
3214 : : }
3215 : :
3216 : : /* Get the value of EXPR within this region_model (assuming the most
3217 : : recent stack frame if it's a local). */
3218 : :
3219 : : const svalue *
3220 : 2869007 : region_model::get_rvalue (tree expr, region_model_context *ctxt) const
3221 : : {
3222 : 2869007 : return get_rvalue (path_var (expr, get_stack_depth () - 1), ctxt);
3223 : : }
3224 : :
3225 : : /* Return true if this model is on a path with "main" as the entrypoint
3226 : : (as opposed to one in which we're merely analyzing a subset of the
3227 : : path through the code). */
3228 : :
3229 : : bool
3230 : 255246 : region_model::called_from_main_p () const
3231 : : {
3232 : 255246 : if (!m_current_frame)
3233 : : return false;
3234 : : /* Determine if the oldest stack frame in this model is for "main". */
3235 : 248883 : const frame_region *frame0 = get_frame_at_index (0);
3236 : 248883 : gcc_assert (frame0);
3237 : 248883 : return id_equal (DECL_NAME (frame0->get_function ().decl), "main");
3238 : : }
3239 : :
3240 : : /* Subroutine of region_model::get_store_value for when REG is (or is within)
3241 : : a global variable that hasn't been touched since the start of this path
3242 : : (or was implicitly touched due to a call to an unknown function). */
3243 : :
3244 : : const svalue *
3245 : 264218 : region_model::get_initial_value_for_global (const region *reg) const
3246 : : {
3247 : : /* Get the decl that REG is for (or is within). */
3248 : 264218 : const decl_region *base_reg
3249 : 264218 : = reg->get_base_region ()->dyn_cast_decl_region ();
3250 : 264218 : gcc_assert (base_reg);
3251 : 264218 : tree decl = base_reg->get_decl ();
3252 : :
3253 : : /* Special-case: to avoid having to explicitly update all previously
3254 : : untracked globals when calling an unknown fn, they implicitly have
3255 : : an unknown value if an unknown call has occurred, unless this is
3256 : : static to-this-TU and hasn't escaped. Globals that have escaped
3257 : : are explicitly tracked, so we shouldn't hit this case for them. */
3258 : 264218 : if (m_store.called_unknown_fn_p ()
3259 : 81999 : && TREE_PUBLIC (decl)
3260 : 281742 : && !TREE_READONLY (decl))
3261 : 9028 : return m_mgr->get_or_create_unknown_svalue (reg->get_type ());
3262 : :
3263 : : /* If we are on a path from the entrypoint from "main" and we have a
3264 : : global decl defined in this TU that hasn't been touched yet, then
3265 : : the initial value of REG can be taken from the initialization value
3266 : : of the decl. */
3267 : 255190 : if (called_from_main_p () || TREE_READONLY (decl))
3268 : 14799 : return reg->get_initial_value_at_main (m_mgr);
3269 : :
3270 : : /* Otherwise, return INIT_VAL(REG). */
3271 : 240391 : return m_mgr->get_or_create_initial_value (reg);
3272 : : }
3273 : :
3274 : : /* Get a value for REG, looking it up in the store, or otherwise falling
3275 : : back to "initial" or "unknown" values.
3276 : : Use CTXT to report any warnings associated with reading from REG. */
3277 : :
3278 : : const svalue *
3279 : 3749737 : region_model::get_store_value (const region *reg,
3280 : : region_model_context *ctxt) const
3281 : : {
3282 : : /* Getting the value of an empty region gives an unknown_svalue. */
3283 : 3749737 : if (reg->empty_p ())
3284 : 52 : return m_mgr->get_or_create_unknown_svalue (reg->get_type ());
3285 : :
3286 : 3749685 : bool check_poisoned = true;
3287 : 3749685 : if (check_region_for_read (reg, ctxt))
3288 : 425 : check_poisoned = false;
3289 : :
3290 : : /* Special-case: handle var_decls in the constant pool. */
3291 : 3749685 : if (const decl_region *decl_reg = reg->dyn_cast_decl_region ())
3292 : 3100417 : if (const svalue *sval = decl_reg->maybe_get_constant_value (m_mgr))
3293 : : return sval;
3294 : :
3295 : 3749671 : const svalue *sval
3296 : 3749671 : = m_store.get_any_binding (m_mgr->get_store_manager (), reg);
3297 : 3749671 : if (sval)
3298 : : {
3299 : 1079641 : if (reg->get_type ())
3300 : 1077605 : sval = m_mgr->get_or_create_cast (reg->get_type (), sval);
3301 : 1079641 : return sval;
3302 : : }
3303 : :
3304 : : /* Special-case: read at a constant index within a STRING_CST. */
3305 : 2670030 : if (const offset_region *offset_reg = reg->dyn_cast_offset_region ())
3306 : 145394 : if (tree byte_offset_cst
3307 : 145394 : = offset_reg->get_byte_offset ()->maybe_get_constant ())
3308 : 8423 : if (const string_region *str_reg
3309 : 8423 : = reg->get_parent_region ()->dyn_cast_string_region ())
3310 : : {
3311 : 194 : tree string_cst = str_reg->get_string_cst ();
3312 : 388 : if (const svalue *char_sval
3313 : 194 : = m_mgr->maybe_get_char_from_string_cst (string_cst,
3314 : : byte_offset_cst))
3315 : 190 : return m_mgr->get_or_create_cast (reg->get_type (), char_sval);
3316 : : }
3317 : :
3318 : : /* Special-case: read the initial char of a STRING_CST. */
3319 : 2669840 : if (const cast_region *cast_reg = reg->dyn_cast_cast_region ())
3320 : 4876 : if (const string_region *str_reg
3321 : 2438 : = cast_reg->get_parent_region ()->dyn_cast_string_region ())
3322 : : {
3323 : 186 : tree string_cst = str_reg->get_string_cst ();
3324 : 186 : tree byte_offset_cst = integer_zero_node;
3325 : 372 : if (const svalue *char_sval
3326 : 186 : = m_mgr->maybe_get_char_from_string_cst (string_cst,
3327 : : byte_offset_cst))
3328 : 186 : return m_mgr->get_or_create_cast (reg->get_type (), char_sval);
3329 : : }
3330 : :
3331 : : /* Otherwise we implicitly have the initial value of the region
3332 : : (if the cluster had been touched, binding_cluster::get_any_binding,
3333 : : would have returned UNKNOWN, and we would already have returned
3334 : : that above). */
3335 : :
3336 : : /* Handle globals. */
3337 : 2669654 : if (reg->get_base_region ()->get_parent_region ()->get_kind ()
3338 : : == RK_GLOBALS)
3339 : 264218 : return get_initial_value_for_global (reg);
3340 : :
3341 : 2405436 : return m_mgr->get_or_create_initial_value (reg, check_poisoned);
3342 : : }
3343 : :
3344 : : /* Return false if REG does not exist, true if it may do.
3345 : : This is for detecting regions within the stack that don't exist anymore
3346 : : after frames are popped. */
3347 : :
3348 : : bool
3349 : 2409124 : region_model::region_exists_p (const region *reg) const
3350 : : {
3351 : : /* If within a stack frame, check that the stack frame is live. */
3352 : 2409124 : if (const frame_region *enclosing_frame = reg->maybe_get_frame_region ())
3353 : : {
3354 : : /* Check that the current frame is the enclosing frame, or is called
3355 : : by it. */
3356 : 2349522 : for (const frame_region *iter_frame = get_current_frame (); iter_frame;
3357 : 591967 : iter_frame = iter_frame->get_calling_frame ())
3358 : 2333301 : if (iter_frame == enclosing_frame)
3359 : : return true;
3360 : : return false;
3361 : : }
3362 : :
3363 : : return true;
3364 : : }
3365 : :
3366 : : /* Get a region for referencing PTR_SVAL, creating a region if need be, and
3367 : : potentially generating warnings via CTXT.
3368 : : PTR_SVAL must be of pointer type.
3369 : : PTR_TREE if non-NULL can be used when emitting diagnostics. */
3370 : :
3371 : : const region *
3372 : 119856 : region_model::deref_rvalue (const svalue *ptr_sval, tree ptr_tree,
3373 : : region_model_context *ctxt,
3374 : : bool add_nonnull_constraint) const
3375 : : {
3376 : 119856 : gcc_assert (ptr_sval);
3377 : 119856 : gcc_assert (POINTER_TYPE_P (ptr_sval->get_type ()));
3378 : :
3379 : : /* If we're dereferencing PTR_SVAL, assume that it is non-NULL; add this
3380 : : as a constraint. This suppresses false positives from
3381 : : -Wanalyzer-null-dereference for the case where we later have an
3382 : : if (PTR_SVAL) that would occur if we considered the false branch
3383 : : and transitioned the malloc state machine from start->null. */
3384 : 119856 : if (add_nonnull_constraint)
3385 : : {
3386 : 114482 : tree null_ptr_cst = build_int_cst (ptr_sval->get_type (), 0);
3387 : 114482 : const svalue *null_ptr
3388 : 114482 : = m_mgr->get_or_create_constant_svalue (null_ptr_cst);
3389 : 114482 : m_constraints->add_constraint (ptr_sval, NE_EXPR, null_ptr);
3390 : : }
3391 : :
3392 : 119856 : switch (ptr_sval->get_kind ())
3393 : : {
3394 : : default:
3395 : : break;
3396 : :
3397 : 45476 : case SK_REGION:
3398 : 45476 : {
3399 : 45476 : const region_svalue *region_sval
3400 : 45476 : = as_a <const region_svalue *> (ptr_sval);
3401 : 45476 : return region_sval->get_pointee ();
3402 : : }
3403 : :
3404 : 19473 : case SK_BINOP:
3405 : 19473 : {
3406 : 19473 : const binop_svalue *binop_sval
3407 : 19473 : = as_a <const binop_svalue *> (ptr_sval);
3408 : 19473 : switch (binop_sval->get_op ())
3409 : : {
3410 : 19473 : case POINTER_PLUS_EXPR:
3411 : 19473 : {
3412 : : /* If we have a symbolic value expressing pointer arithmentic,
3413 : : try to convert it to a suitable region. */
3414 : 19473 : const region *parent_region
3415 : 19473 : = deref_rvalue (binop_sval->get_arg0 (), NULL_TREE, ctxt);
3416 : 19473 : const svalue *offset = binop_sval->get_arg1 ();
3417 : 19473 : tree type= TREE_TYPE (ptr_sval->get_type ());
3418 : 19473 : return m_mgr->get_offset_region (parent_region, type, offset);
3419 : : }
3420 : : default:
3421 : : break;
3422 : : }
3423 : : }
3424 : : break;
3425 : :
3426 : 2538 : case SK_POISONED:
3427 : 2538 : {
3428 : 2538 : if (ctxt)
3429 : : {
3430 : 680 : tree ptr = get_representative_tree (ptr_sval);
3431 : : /* If we can't get a representative tree for PTR_SVAL
3432 : : (e.g. if it hasn't been bound into the store), then
3433 : : fall back on PTR_TREE, if non-NULL. */
3434 : 680 : if (!ptr)
3435 : 680 : ptr = ptr_tree;
3436 : 680 : if (ptr)
3437 : : {
3438 : 0 : const poisoned_svalue *poisoned_sval
3439 : 0 : = as_a <const poisoned_svalue *> (ptr_sval);
3440 : 0 : enum poison_kind pkind = poisoned_sval->get_poison_kind ();
3441 : 0 : ctxt->warn (std::make_unique<poisoned_value_diagnostic>
3442 : 0 : (ptr, pkind, nullptr, nullptr));
3443 : : }
3444 : : }
3445 : : }
3446 : : break;
3447 : : }
3448 : :
3449 : 54907 : return m_mgr->get_symbolic_region (ptr_sval);
3450 : : }
3451 : :
3452 : : /* Attempt to get BITS within any value of REG, as TYPE.
3453 : : In particular, extract values from compound_svalues for the case
3454 : : where there's a concrete binding at BITS.
3455 : : Return an unknown svalue if we can't handle the given case.
3456 : : Use CTXT to report any warnings associated with reading from REG. */
3457 : :
3458 : : const svalue *
3459 : 130 : region_model::get_rvalue_for_bits (tree type,
3460 : : const region *reg,
3461 : : const bit_range &bits,
3462 : : region_model_context *ctxt) const
3463 : : {
3464 : 130 : const svalue *sval = get_store_value (reg, ctxt);
3465 : 130 : return m_mgr->get_or_create_bits_within (type, bits, sval);
3466 : : }
3467 : :
3468 : : /* A subclass of pending_diagnostic for complaining about writes to
3469 : : constant regions of memory. */
3470 : :
3471 : : class write_to_const_diagnostic
3472 : : : public pending_diagnostic_subclass<write_to_const_diagnostic>
3473 : : {
3474 : : public:
3475 : 33 : write_to_const_diagnostic (const region *reg, tree decl)
3476 : 33 : : m_reg (reg), m_decl (decl)
3477 : : {}
3478 : :
3479 : 421 : const char *get_kind () const final override
3480 : : {
3481 : 421 : return "write_to_const_diagnostic";
3482 : : }
3483 : :
3484 : 33 : bool operator== (const write_to_const_diagnostic &other) const
3485 : : {
3486 : 33 : return (m_reg == other.m_reg
3487 : 33 : && m_decl == other.m_decl);
3488 : : }
3489 : :
3490 : 66 : int get_controlling_option () const final override
3491 : : {
3492 : 66 : return OPT_Wanalyzer_write_to_const;
3493 : : }
3494 : :
3495 : 33 : bool emit (diagnostic_emission_context &ctxt) final override
3496 : : {
3497 : 33 : auto_diagnostic_group d;
3498 : 33 : bool warned;
3499 : 33 : switch (m_reg->get_kind ())
3500 : : {
3501 : 20 : default:
3502 : 20 : warned = ctxt.warn ("write to %<const%> object %qE", m_decl);
3503 : 20 : break;
3504 : 9 : case RK_FUNCTION:
3505 : 9 : warned = ctxt.warn ("write to function %qE", m_decl);
3506 : 9 : break;
3507 : 4 : case RK_LABEL:
3508 : 4 : warned = ctxt.warn ("write to label %qE", m_decl);
3509 : 4 : break;
3510 : : }
3511 : 33 : if (warned)
3512 : 33 : inform (DECL_SOURCE_LOCATION (m_decl), "declared here");
3513 : 66 : return warned;
3514 : 33 : }
3515 : :
3516 : : bool
3517 : 66 : describe_final_event (pretty_printer &pp,
3518 : : const evdesc::final_event &) final override
3519 : : {
3520 : 66 : switch (m_reg->get_kind ())
3521 : : {
3522 : 40 : default:
3523 : 40 : {
3524 : 40 : pp_printf (&pp,
3525 : : "write to %<const%> object %qE here", m_decl);
3526 : 40 : return true;
3527 : : }
3528 : 18 : case RK_FUNCTION:
3529 : 18 : {
3530 : 18 : pp_printf (&pp,
3531 : : "write to function %qE here", m_decl);
3532 : 18 : return true;
3533 : : }
3534 : 8 : case RK_LABEL:
3535 : 8 : {
3536 : 8 : pp_printf (&pp,
3537 : : "write to label %qE here", m_decl);
3538 : 8 : return true;
3539 : : }
3540 : : }
3541 : : }
3542 : :
3543 : : private:
3544 : : const region *m_reg;
3545 : : tree m_decl;
3546 : : };
3547 : :
3548 : : /* A subclass of pending_diagnostic for complaining about writes to
3549 : : string literals. */
3550 : :
3551 : : class write_to_string_literal_diagnostic
3552 : : : public pending_diagnostic_subclass<write_to_string_literal_diagnostic>
3553 : : {
3554 : : public:
3555 : 51 : write_to_string_literal_diagnostic (const region *reg)
3556 : 51 : : m_reg (reg)
3557 : : {}
3558 : :
3559 : 337 : const char *get_kind () const final override
3560 : : {
3561 : 337 : return "write_to_string_literal_diagnostic";
3562 : : }
3563 : :
3564 : 47 : bool operator== (const write_to_string_literal_diagnostic &other) const
3565 : : {
3566 : 47 : return m_reg == other.m_reg;
3567 : : }
3568 : :
3569 : 94 : int get_controlling_option () const final override
3570 : : {
3571 : 94 : return OPT_Wanalyzer_write_to_string_literal;
3572 : : }
3573 : :
3574 : 43 : bool emit (diagnostic_emission_context &ctxt) final override
3575 : : {
3576 : 43 : return ctxt.warn ("write to string literal");
3577 : : /* Ideally we would show the location of the STRING_CST as well,
3578 : : but it is not available at this point. */
3579 : : }
3580 : :
3581 : : bool
3582 : 86 : describe_final_event (pretty_printer &pp,
3583 : : const evdesc::final_event &) final override
3584 : : {
3585 : 86 : pp_string (&pp, "write to string literal here");
3586 : 86 : return true;
3587 : : }
3588 : :
3589 : : private:
3590 : : const region *m_reg;
3591 : : };
3592 : :
3593 : : /* Use CTXT to warn If DEST_REG is a region that shouldn't be written to. */
3594 : :
3595 : : void
3596 : 228918 : region_model::check_for_writable_region (const region* dest_reg,
3597 : : region_model_context *ctxt) const
3598 : : {
3599 : : /* Fail gracefully if CTXT is nullptr. */
3600 : 228918 : if (!ctxt)
3601 : : return;
3602 : :
3603 : 228918 : const region *base_reg = dest_reg->get_base_region ();
3604 : 228918 : switch (base_reg->get_kind ())
3605 : : {
3606 : : default:
3607 : : break;
3608 : 9 : case RK_FUNCTION:
3609 : 9 : {
3610 : 9 : const function_region *func_reg = as_a <const function_region *> (base_reg);
3611 : 9 : tree fndecl = func_reg->get_fndecl ();
3612 : 9 : ctxt->warn
3613 : 9 : (std::make_unique<write_to_const_diagnostic>
3614 : 9 : (func_reg, fndecl));
3615 : : }
3616 : 9 : break;
3617 : 4 : case RK_LABEL:
3618 : 4 : {
3619 : 4 : const label_region *label_reg = as_a <const label_region *> (base_reg);
3620 : 4 : tree label = label_reg->get_label ();
3621 : 4 : ctxt->warn
3622 : 4 : (std::make_unique<write_to_const_diagnostic>
3623 : 4 : (label_reg, label));
3624 : : }
3625 : 4 : break;
3626 : 211414 : case RK_DECL:
3627 : 211414 : {
3628 : 211414 : const decl_region *decl_reg = as_a <const decl_region *> (base_reg);
3629 : 211414 : tree decl = decl_reg->get_decl ();
3630 : : /* Warn about writes to const globals.
3631 : : Don't warn for writes to const locals, and params in particular,
3632 : : since we would warn in push_frame when setting them up (e.g the
3633 : : "this" param is "T* const"). */
3634 : 211414 : if (TREE_READONLY (decl)
3635 : 211414 : && is_global_var (decl))
3636 : 20 : ctxt->warn
3637 : 20 : (std::make_unique<write_to_const_diagnostic> (dest_reg, decl));
3638 : : }
3639 : 211414 : break;
3640 : 51 : case RK_STRING:
3641 : 51 : ctxt->warn
3642 : 51 : (std::make_unique<write_to_string_literal_diagnostic> (dest_reg));
3643 : 51 : break;
3644 : : }
3645 : : }
3646 : :
3647 : : /* Get the capacity of REG in bytes. */
3648 : :
3649 : : const svalue *
3650 : 739778 : region_model::get_capacity (const region *reg) const
3651 : : {
3652 : 739793 : switch (reg->get_kind ())
3653 : : {
3654 : : default:
3655 : : break;
3656 : 676894 : case RK_DECL:
3657 : 676894 : {
3658 : 676894 : const decl_region *decl_reg = as_a <const decl_region *> (reg);
3659 : 676894 : tree decl = decl_reg->get_decl ();
3660 : 676894 : if (TREE_CODE (decl) == SSA_NAME)
3661 : : {
3662 : 594171 : tree type = TREE_TYPE (decl);
3663 : 594171 : tree size = TYPE_SIZE (type);
3664 : 594171 : return get_rvalue (size, nullptr);
3665 : : }
3666 : : else
3667 : : {
3668 : 82723 : tree size = decl_init_size (decl, false);
3669 : 82723 : if (size)
3670 : 82581 : return get_rvalue (size, nullptr);
3671 : : }
3672 : : }
3673 : : break;
3674 : 15 : case RK_SIZED:
3675 : : /* Look through sized regions to get at the capacity
3676 : : of the underlying regions. */
3677 : 15 : return get_capacity (reg->get_parent_region ());
3678 : 519 : case RK_STRING:
3679 : 519 : {
3680 : : /* "Capacity" here means "size". */
3681 : 519 : const string_region *string_reg = as_a <const string_region *> (reg);
3682 : 519 : tree string_cst = string_reg->get_string_cst ();
3683 : 519 : return m_mgr->get_or_create_int_cst (size_type_node,
3684 : 519 : TREE_STRING_LENGTH (string_cst));
3685 : : }
3686 : 62507 : break;
3687 : : }
3688 : :
3689 : 62507 : if (const svalue *recorded = get_dynamic_extents (reg))
3690 : : return recorded;
3691 : :
3692 : 50770 : return m_mgr->get_or_create_unknown_svalue (sizetype);
3693 : : }
3694 : :
3695 : : /* If CTXT is non-NULL, use it to warn about any problems accessing REG,
3696 : : using DIR to determine if this access is a read or write.
3697 : : Return TRUE if an OOB access was detected.
3698 : : If SVAL_HINT is non-NULL, use it as a hint in diagnostics
3699 : : about the value that would be written to REG. */
3700 : :
3701 : : bool
3702 : 4057673 : region_model::check_region_access (const region *reg,
3703 : : enum access_direction dir,
3704 : : const svalue *sval_hint,
3705 : : region_model_context *ctxt) const
3706 : : {
3707 : : /* Fail gracefully if CTXT is NULL. */
3708 : 4057673 : if (!ctxt)
3709 : : return false;
3710 : :
3711 : 731949 : bool oob_access_detected = false;
3712 : 731949 : check_region_for_taint (reg, dir, ctxt);
3713 : 731949 : if (!check_region_bounds (reg, dir, sval_hint, ctxt))
3714 : 768 : oob_access_detected = true;
3715 : :
3716 : 731949 : switch (dir)
3717 : : {
3718 : 0 : default:
3719 : 0 : gcc_unreachable ();
3720 : : case access_direction::read:
3721 : : /* Currently a no-op. */
3722 : : break;
3723 : 228918 : case access_direction::write:
3724 : 228918 : check_for_writable_region (reg, ctxt);
3725 : 228918 : break;
3726 : : }
3727 : : return oob_access_detected;
3728 : : }
3729 : :
3730 : : /* If CTXT is non-NULL, use it to warn about any problems writing to REG. */
3731 : :
3732 : : void
3733 : 307988 : region_model::check_region_for_write (const region *dest_reg,
3734 : : const svalue *sval_hint,
3735 : : region_model_context *ctxt) const
3736 : : {
3737 : 307988 : check_region_access (dest_reg, access_direction::write, sval_hint, ctxt);
3738 : 307988 : }
3739 : :
3740 : : /* If CTXT is non-NULL, use it to warn about any problems reading from REG.
3741 : : Returns TRUE if an OOB read was detected. */
3742 : :
3743 : : bool
3744 : 3749685 : region_model::check_region_for_read (const region *src_reg,
3745 : : region_model_context *ctxt) const
3746 : : {
3747 : 3749685 : return check_region_access (src_reg, access_direction::read, nullptr, ctxt);
3748 : : }
3749 : :
3750 : : /* Concrete subclass for casts of pointers that lead to trailing bytes. */
3751 : :
3752 : : class dubious_allocation_size
3753 : : : public pending_diagnostic_subclass<dubious_allocation_size>
3754 : : {
3755 : : public:
3756 : 111 : dubious_allocation_size (const region *lhs, const region *rhs,
3757 : : const svalue *capacity_sval, tree expr,
3758 : : const gimple *stmt)
3759 : 111 : : m_lhs (lhs), m_rhs (rhs),
3760 : 111 : m_capacity_sval (capacity_sval), m_expr (expr),
3761 : 111 : m_stmt (stmt),
3762 : 111 : m_has_allocation_event (false)
3763 : : {
3764 : 111 : gcc_assert (m_capacity_sval);
3765 : : }
3766 : :
3767 : 1186 : const char *get_kind () const final override
3768 : : {
3769 : 1186 : return "dubious_allocation_size";
3770 : : }
3771 : :
3772 : 111 : bool operator== (const dubious_allocation_size &other) const
3773 : : {
3774 : 111 : return (m_stmt == other.m_stmt
3775 : 111 : && pending_diagnostic::same_tree_p (m_expr, other.m_expr));
3776 : : }
3777 : :
3778 : 222 : int get_controlling_option () const final override
3779 : : {
3780 : 222 : return OPT_Wanalyzer_allocation_size;
3781 : : }
3782 : :
3783 : 111 : bool emit (diagnostic_emission_context &ctxt) final override
3784 : : {
3785 : 111 : ctxt.add_cwe (131);
3786 : :
3787 : 111 : return ctxt.warn ("allocated buffer size is not a multiple"
3788 : 111 : " of the pointee's size");
3789 : : }
3790 : :
3791 : : bool
3792 : 222 : describe_final_event (pretty_printer &pp,
3793 : : const evdesc::final_event &) final override
3794 : : {
3795 : 222 : tree pointee_type = TREE_TYPE (m_lhs->get_type ());
3796 : 222 : if (m_has_allocation_event)
3797 : : {
3798 : 214 : pp_printf (&pp,
3799 : : "assigned to %qT here;"
3800 : : " %<sizeof (%T)%> is %qE",
3801 : 214 : m_lhs->get_type (), pointee_type,
3802 : : size_in_bytes (pointee_type));
3803 : 214 : return true;
3804 : : }
3805 : : /* Fallback: Typically, we should always see an allocation_event
3806 : : before. */
3807 : 8 : if (m_expr)
3808 : : {
3809 : 8 : if (TREE_CODE (m_expr) == INTEGER_CST)
3810 : : {
3811 : 8 : pp_printf (&pp,
3812 : : "allocated %E bytes and assigned to"
3813 : : " %qT here; %<sizeof (%T)%> is %qE",
3814 : 8 : m_expr, m_lhs->get_type (), pointee_type,
3815 : : size_in_bytes (pointee_type));
3816 : 8 : return true;
3817 : : }
3818 : : else
3819 : : {
3820 : 0 : pp_printf (&pp,
3821 : : "allocated %qE bytes and assigned to"
3822 : : " %qT here; %<sizeof (%T)%> is %qE",
3823 : 0 : m_expr, m_lhs->get_type (), pointee_type,
3824 : : size_in_bytes (pointee_type));
3825 : 0 : return true;
3826 : : }
3827 : : }
3828 : :
3829 : 0 : pp_printf (&pp,
3830 : : "allocated and assigned to %qT here;"
3831 : : " %<sizeof (%T)%> is %qE",
3832 : 0 : m_lhs->get_type (), pointee_type,
3833 : : size_in_bytes (pointee_type));
3834 : 0 : return true;
3835 : : }
3836 : :
3837 : : void
3838 : 107 : add_region_creation_events (const region *,
3839 : : tree capacity,
3840 : : const event_loc_info &loc_info,
3841 : : checker_path &emission_path) final override
3842 : : {
3843 : 107 : emission_path.add_event
3844 : 107 : (std::make_unique<region_creation_event_allocation_size>
3845 : 107 : (capacity, loc_info));
3846 : :
3847 : 107 : m_has_allocation_event = true;
3848 : 107 : }
3849 : :
3850 : 111 : void mark_interesting_stuff (interesting_t *interest) final override
3851 : : {
3852 : 111 : interest->add_region_creation (m_rhs);
3853 : 111 : }
3854 : :
3855 : : void
3856 : 0 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
3857 : : const final override
3858 : : {
3859 : 0 : auto &props = result_obj.get_or_create_properties ();
3860 : : #define PROPERTY_PREFIX "gcc/analyzer/dubious_allocation_size/"
3861 : 0 : props.set (PROPERTY_PREFIX "lhs", m_lhs->to_json ());
3862 : 0 : props.set (PROPERTY_PREFIX "rhs", m_rhs->to_json ());
3863 : 0 : props.set (PROPERTY_PREFIX "capacity_sval", m_capacity_sval->to_json ());
3864 : : #undef PROPERTY_PREFIX
3865 : 0 : }
3866 : :
3867 : : private:
3868 : : const region *m_lhs;
3869 : : const region *m_rhs;
3870 : : const svalue *m_capacity_sval;
3871 : : const tree m_expr;
3872 : : const gimple *m_stmt;
3873 : : bool m_has_allocation_event;
3874 : : };
3875 : :
3876 : : /* Return true on dubious allocation sizes for constant sizes. */
3877 : :
3878 : : static bool
3879 : 1952 : capacity_compatible_with_type (tree cst, tree pointee_size_tree,
3880 : : bool is_struct)
3881 : : {
3882 : 1952 : gcc_assert (TREE_CODE (cst) == INTEGER_CST);
3883 : 1952 : gcc_assert (TREE_CODE (pointee_size_tree) == INTEGER_CST);
3884 : :
3885 : 1952 : unsigned HOST_WIDE_INT pointee_size = TREE_INT_CST_LOW (pointee_size_tree);
3886 : 1952 : unsigned HOST_WIDE_INT alloc_size = TREE_INT_CST_LOW (cst);
3887 : :
3888 : 1952 : if (is_struct)
3889 : 677 : return alloc_size == 0 || alloc_size >= pointee_size;
3890 : 1275 : return alloc_size % pointee_size == 0;
3891 : : }
3892 : :
3893 : : static bool
3894 : 422 : capacity_compatible_with_type (tree cst, tree pointee_size_tree)
3895 : : {
3896 : 0 : return capacity_compatible_with_type (cst, pointee_size_tree, false);
3897 : : }
3898 : :
3899 : : /* Checks whether SVAL could be a multiple of SIZE_CST.
3900 : :
3901 : : It works by visiting all svalues inside SVAL until it reaches
3902 : : atomic nodes. From those, it goes back up again and adds each
3903 : : node that is not a multiple of SIZE_CST to the RESULT_SET. */
3904 : :
3905 : 2300 : class size_visitor : public visitor
3906 : : {
3907 : : public:
3908 : 1150 : size_visitor (tree size_cst, const svalue *root_sval, constraint_manager *cm)
3909 : 1150 : : m_size_cst (size_cst), m_root_sval (root_sval), m_cm (cm)
3910 : : {
3911 : 1150 : m_root_sval->accept (this);
3912 : 1150 : }
3913 : :
3914 : 1150 : bool is_dubious_capacity ()
3915 : : {
3916 : 1150 : return result_set.contains (m_root_sval);
3917 : : }
3918 : :
3919 : 438 : void visit_constant_svalue (const constant_svalue *sval) final override
3920 : : {
3921 : 438 : check_constant (sval->get_constant (), sval);
3922 : 438 : }
3923 : :
3924 : 250 : void visit_unaryop_svalue (const unaryop_svalue *sval) final override
3925 : : {
3926 : 250 : if (CONVERT_EXPR_CODE_P (sval->get_op ())
3927 : 291 : && result_set.contains (sval->get_arg ()))
3928 : 105 : result_set.add (sval);
3929 : 250 : }
3930 : :
3931 : 434 : void visit_binop_svalue (const binop_svalue *sval) final override
3932 : : {
3933 : 434 : const svalue *arg0 = sval->get_arg0 ();
3934 : 434 : const svalue *arg1 = sval->get_arg1 ();
3935 : :
3936 : 434 : switch (sval->get_op ())
3937 : : {
3938 : 302 : case MULT_EXPR:
3939 : 302 : if (result_set.contains (arg0) && result_set.contains (arg1))
3940 : 24 : result_set.add (sval);
3941 : : break;
3942 : 104 : case PLUS_EXPR:
3943 : 104 : case MINUS_EXPR:
3944 : 104 : if (result_set.contains (arg0) || result_set.contains (arg1))
3945 : 28 : result_set.add (sval);
3946 : : break;
3947 : : default:
3948 : : break;
3949 : : }
3950 : 434 : }
3951 : :
3952 : 0 : void visit_unmergeable_svalue (const unmergeable_svalue *sval) final override
3953 : : {
3954 : 0 : if (result_set.contains (sval->get_arg ()))
3955 : 0 : result_set.add (sval);
3956 : 0 : }
3957 : :
3958 : 12 : void visit_widening_svalue (const widening_svalue *sval) final override
3959 : : {
3960 : 12 : const svalue *base = sval->get_base_svalue ();
3961 : 12 : const svalue *iter = sval->get_iter_svalue ();
3962 : :
3963 : 12 : if (result_set.contains (base) || result_set.contains (iter))
3964 : 8 : result_set.add (sval);
3965 : 12 : }
3966 : :
3967 : 317 : void visit_initial_svalue (const initial_svalue *sval) final override
3968 : : {
3969 : 317 : equiv_class_id id = equiv_class_id::null ();
3970 : 317 : if (m_cm->get_equiv_class_by_svalue (sval, &id))
3971 : : {
3972 : 75 : if (tree cst = id.get_obj (*m_cm).get_any_constant ())
3973 : 0 : check_constant (cst, sval);
3974 : : }
3975 : 242 : else if (!m_cm->sval_constrained_p (sval))
3976 : : {
3977 : 174 : result_set.add (sval);
3978 : : }
3979 : 317 : }
3980 : :
3981 : 30 : void visit_conjured_svalue (const conjured_svalue *sval) final override
3982 : : {
3983 : 30 : equiv_class_id id = equiv_class_id::null ();
3984 : 30 : if (m_cm->get_equiv_class_by_svalue (sval, &id))
3985 : 13 : if (tree cst = id.get_obj (*m_cm).get_any_constant ())
3986 : 8 : check_constant (cst, sval);
3987 : 30 : }
3988 : :
3989 : : private:
3990 : 446 : void check_constant (tree cst, const svalue *sval)
3991 : : {
3992 : 446 : switch (TREE_CODE (cst))
3993 : : {
3994 : : default:
3995 : : /* Assume all unhandled operands are compatible. */
3996 : : break;
3997 : 422 : case INTEGER_CST:
3998 : 422 : if (!capacity_compatible_with_type (cst, m_size_cst))
3999 : 68 : result_set.add (sval);
4000 : : break;
4001 : : }
4002 : 446 : }
4003 : :
4004 : : tree m_size_cst;
4005 : : const svalue *m_root_sval;
4006 : : constraint_manager *m_cm;
4007 : : svalue_set result_set; /* Used as a mapping of svalue*->bool. */
4008 : : };
4009 : :
4010 : : /* Return true if SIZE_CST is a power of 2, and we have
4011 : : CAPACITY_SVAL == ((X | (Y - 1) ) + 1), since it is then a multiple
4012 : : of SIZE_CST, as used by Linux kernel's round_up macro. */
4013 : :
4014 : : static bool
4015 : 1154 : is_round_up (tree size_cst,
4016 : : const svalue *capacity_sval)
4017 : : {
4018 : 1154 : if (!integer_pow2p (size_cst))
4019 : : return false;
4020 : 1154 : const binop_svalue *binop_sval = capacity_sval->dyn_cast_binop_svalue ();
4021 : 1154 : if (!binop_sval)
4022 : : return false;
4023 : 286 : if (binop_sval->get_op () != PLUS_EXPR)
4024 : : return false;
4025 : 84 : tree rhs_cst = binop_sval->get_arg1 ()->maybe_get_constant ();
4026 : 84 : if (!rhs_cst)
4027 : : return false;
4028 : 84 : if (!integer_onep (rhs_cst))
4029 : : return false;
4030 : :
4031 : : /* We have CAPACITY_SVAL == (LHS + 1) for some LHS expression. */
4032 : :
4033 : 4 : const binop_svalue *lhs_binop_sval
4034 : 4 : = binop_sval->get_arg0 ()->dyn_cast_binop_svalue ();
4035 : 4 : if (!lhs_binop_sval)
4036 : : return false;
4037 : 4 : if (lhs_binop_sval->get_op () != BIT_IOR_EXPR)
4038 : : return false;
4039 : :
4040 : 4 : tree inner_rhs_cst = lhs_binop_sval->get_arg1 ()->maybe_get_constant ();
4041 : 4 : if (!inner_rhs_cst)
4042 : : return false;
4043 : :
4044 : 4 : if (wi::to_widest (inner_rhs_cst) + 1 != wi::to_widest (size_cst))
4045 : : return false;
4046 : : return true;
4047 : : }
4048 : :
4049 : : /* Return true if CAPACITY_SVAL is known to be a multiple of SIZE_CST. */
4050 : :
4051 : : static bool
4052 : 1154 : is_multiple_p (tree size_cst,
4053 : : const svalue *capacity_sval)
4054 : : {
4055 : 1210 : if (const svalue *sval = capacity_sval->maybe_undo_cast ())
4056 : : return is_multiple_p (size_cst, sval);
4057 : :
4058 : 1154 : if (is_round_up (size_cst, capacity_sval))
4059 : : return true;
4060 : :
4061 : : return false;
4062 : : }
4063 : :
4064 : : /* Return true if we should emit a dubious_allocation_size warning
4065 : : on assigning a region of capacity CAPACITY_SVAL bytes to a pointer
4066 : : of type with size SIZE_CST, where CM expresses known constraints. */
4067 : :
4068 : : static bool
4069 : 1154 : is_dubious_capacity (tree size_cst,
4070 : : const svalue *capacity_sval,
4071 : : constraint_manager *cm)
4072 : : {
4073 : 1154 : if (is_multiple_p (size_cst, capacity_sval))
4074 : : return false;
4075 : 1150 : size_visitor v (size_cst, capacity_sval, cm);
4076 : 1150 : return v.is_dubious_capacity ();
4077 : 1150 : }
4078 : :
4079 : :
4080 : : /* Return true if a struct or union either uses the inheritance pattern,
4081 : : where the first field is a base struct, or the flexible array member
4082 : : pattern, where the last field is an array without a specified size. */
4083 : :
4084 : : static bool
4085 : 3827 : struct_or_union_with_inheritance_p (tree struc)
4086 : : {
4087 : 3827 : tree iter = TYPE_FIELDS (struc);
4088 : 3827 : if (iter == NULL_TREE)
4089 : : return false;
4090 : 3819 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (iter)))
4091 : : return true;
4092 : :
4093 : : tree last_field;
4094 : 55767 : while (iter != NULL_TREE)
4095 : : {
4096 : 52194 : last_field = iter;
4097 : 52194 : iter = DECL_CHAIN (iter);
4098 : : }
4099 : :
4100 : 3573 : if (last_field != NULL_TREE
4101 : 3573 : && TREE_CODE (TREE_TYPE (last_field)) == ARRAY_TYPE)
4102 : : return true;
4103 : :
4104 : : return false;
4105 : : }
4106 : :
4107 : : /* Return true if the lhs and rhs of an assignment have different types. */
4108 : :
4109 : : static bool
4110 : 177447 : is_any_cast_p (const gimple *stmt)
4111 : : {
4112 : 177447 : if (const gassign *assign = dyn_cast <const gassign *> (stmt))
4113 : 137735 : return gimple_assign_cast_p (assign)
4114 : 254808 : || !pending_diagnostic::same_tree_p (
4115 : 117073 : TREE_TYPE (gimple_assign_lhs (assign)),
4116 : 117073 : TREE_TYPE (gimple_assign_rhs1 (assign)));
4117 : 39712 : else if (const gcall *call = dyn_cast <const gcall *> (stmt))
4118 : : {
4119 : 39304 : tree lhs = gimple_call_lhs (call);
4120 : 68816 : return lhs != NULL_TREE && !pending_diagnostic::same_tree_p (
4121 : 29512 : TREE_TYPE (gimple_call_lhs (call)),
4122 : : gimple_call_return_type (call));
4123 : : }
4124 : :
4125 : : return false;
4126 : : }
4127 : :
4128 : : /* On pointer assignments, check whether the buffer size of
4129 : : RHS_SVAL is compatible with the type of the LHS_REG.
4130 : : Use a non-null CTXT to report allocation size warnings. */
4131 : :
4132 : : void
4133 : 306542 : region_model::check_region_size (const region *lhs_reg, const svalue *rhs_sval,
4134 : : region_model_context *ctxt) const
4135 : : {
4136 : 306542 : if (!ctxt || ctxt->get_stmt () == nullptr)
4137 : 301293 : return;
4138 : : /* Only report warnings on assignments that actually change the type. */
4139 : 177447 : if (!is_any_cast_p (ctxt->get_stmt ()))
4140 : : return;
4141 : :
4142 : 52093 : tree pointer_type = lhs_reg->get_type ();
4143 : 52093 : if (pointer_type == NULL_TREE || !POINTER_TYPE_P (pointer_type))
4144 : : return;
4145 : :
4146 : 13260 : tree pointee_type = TREE_TYPE (pointer_type);
4147 : : /* Make sure that the type on the left-hand size actually has a size. */
4148 : 13260 : if (pointee_type == NULL_TREE || VOID_TYPE_P (pointee_type)
4149 : 26111 : || TYPE_SIZE_UNIT (pointee_type) == NULL_TREE)
4150 : : return;
4151 : :
4152 : : /* Bail out early on function pointers. */
4153 : 12751 : if (TREE_CODE (pointee_type) == FUNCTION_TYPE)
4154 : : return;
4155 : :
4156 : : /* Bail out early on pointers to structs where we can
4157 : : not deduce whether the buffer size is compatible. */
4158 : 12463 : bool is_struct = RECORD_OR_UNION_TYPE_P (pointee_type);
4159 : 12463 : if (is_struct && struct_or_union_with_inheritance_p (pointee_type))
4160 : : return;
4161 : :
4162 : 12182 : tree pointee_size_tree = size_in_bytes (pointee_type);
4163 : : /* We give up if the type size is not known at compile-time or the
4164 : : type size is always compatible regardless of the buffer size. */
4165 : 12182 : if (TREE_CODE (pointee_size_tree) != INTEGER_CST
4166 : 12103 : || integer_zerop (pointee_size_tree)
4167 : 24264 : || integer_onep (pointee_size_tree))
4168 : 6933 : return;
4169 : :
4170 : 5249 : const region *rhs_reg = deref_rvalue (rhs_sval, NULL_TREE, ctxt, false);
4171 : 5249 : const svalue *capacity = get_capacity (rhs_reg);
4172 : 5249 : switch (capacity->get_kind ())
4173 : : {
4174 : 1530 : case svalue_kind::SK_CONSTANT:
4175 : 1530 : {
4176 : 1530 : const constant_svalue *cst_cap_sval
4177 : 1530 : = as_a <const constant_svalue *> (capacity);
4178 : 1530 : tree cst_cap = cst_cap_sval->get_constant ();
4179 : 1530 : if (TREE_CODE (cst_cap) == INTEGER_CST
4180 : 1530 : && !capacity_compatible_with_type (cst_cap, pointee_size_tree,
4181 : : is_struct))
4182 : 63 : ctxt->warn
4183 : 126 : (std::make_unique <dubious_allocation_size> (lhs_reg, rhs_reg,
4184 : : capacity, cst_cap,
4185 : 126 : ctxt->get_stmt ()));
4186 : : }
4187 : 1530 : break;
4188 : 3719 : default:
4189 : 3719 : {
4190 : 3719 : if (!is_struct)
4191 : : {
4192 : 1154 : if (is_dubious_capacity (pointee_size_tree,
4193 : : capacity,
4194 : 1154 : m_constraints))
4195 : : {
4196 : 48 : tree expr = get_representative_tree (capacity);
4197 : 48 : ctxt->warn
4198 : 96 : (std::make_unique <dubious_allocation_size> (lhs_reg,
4199 : : rhs_reg,
4200 : : capacity, expr,
4201 : 96 : ctxt->get_stmt ()));
4202 : : }
4203 : : }
4204 : : break;
4205 : : }
4206 : : }
4207 : : }
4208 : :
4209 : : /* Set the value of the region given by LHS_REG to the value given
4210 : : by RHS_SVAL.
4211 : : Use CTXT to report any warnings associated with writing to LHS_REG. */
4212 : :
4213 : : void
4214 : 306562 : region_model::set_value (const region *lhs_reg, const svalue *rhs_sval,
4215 : : region_model_context *ctxt)
4216 : : {
4217 : 306562 : gcc_assert (lhs_reg);
4218 : 306562 : gcc_assert (rhs_sval);
4219 : :
4220 : : /* Setting the value of an empty region is a no-op. */
4221 : 306562 : if (lhs_reg->empty_p ())
4222 : : return;
4223 : :
4224 : 306542 : check_region_size (lhs_reg, rhs_sval, ctxt);
4225 : :
4226 : 306542 : check_region_for_write (lhs_reg, rhs_sval, ctxt);
4227 : :
4228 : 534498 : m_store.set_value (m_mgr->get_store_manager(), lhs_reg, rhs_sval,
4229 : 227956 : ctxt ? ctxt->get_uncertainty () : nullptr);
4230 : : }
4231 : :
4232 : : /* Set the value of the region given by LHS to the value given by RHS. */
4233 : :
4234 : : void
4235 : 84 : region_model::set_value (tree lhs, tree rhs, region_model_context *ctxt)
4236 : : {
4237 : 84 : const region *lhs_reg = get_lvalue (lhs, ctxt);
4238 : 84 : const svalue *rhs_sval = get_rvalue (rhs, ctxt);
4239 : 84 : gcc_assert (lhs_reg);
4240 : 84 : gcc_assert (rhs_sval);
4241 : 84 : set_value (lhs_reg, rhs_sval, ctxt);
4242 : 84 : }
4243 : :
4244 : : /* Issue a note specifying that a particular function parameter is expected
4245 : : to be a valid null-terminated string. */
4246 : :
4247 : : static void
4248 : 146 : inform_about_expected_null_terminated_string_arg (const call_arg_details &ad)
4249 : : {
4250 : : // TODO: ideally we'd underline the param here
4251 : 146 : inform (DECL_SOURCE_LOCATION (ad.m_called_fndecl),
4252 : : "argument %d of %qD must be a pointer to a null-terminated string",
4253 : 146 : ad.m_arg_idx + 1, ad.m_called_fndecl);
4254 : 146 : }
4255 : :
4256 : : /* A binding of a specific svalue at a concrete byte range. */
4257 : :
4258 : : struct fragment
4259 : : {
4260 : 3547 : fragment ()
4261 : 3547 : : m_byte_range (0, 0), m_sval (nullptr)
4262 : : {
4263 : 3547 : }
4264 : :
4265 : 755 : fragment (const byte_range &bytes, const svalue *sval)
4266 : 755 : : m_byte_range (bytes), m_sval (sval)
4267 : : {
4268 : : }
4269 : :
4270 : 1664 : static int cmp_ptrs (const void *p1, const void *p2)
4271 : : {
4272 : 1664 : const fragment *f1 = (const fragment *)p1;
4273 : 1664 : const fragment *f2 = (const fragment *)p2;
4274 : 1664 : return byte_range::cmp (f1->m_byte_range, f2->m_byte_range);
4275 : : }
4276 : :
4277 : : void
4278 : 0 : dump_to_pp (pretty_printer *pp) const
4279 : : {
4280 : 0 : pp_string (pp, "fragment(");
4281 : 0 : m_byte_range.dump_to_pp (pp);
4282 : 0 : pp_string (pp, ", sval: ");
4283 : 0 : if (m_sval)
4284 : 0 : m_sval->dump_to_pp (pp, true);
4285 : : else
4286 : 0 : pp_string (pp, "nullptr");
4287 : 0 : pp_string (pp, ")");
4288 : 0 : }
4289 : :
4290 : : byte_range m_byte_range;
4291 : : const svalue *m_sval;
4292 : : };
4293 : :
4294 : : /* Determine if there is a zero terminator somewhere in the
4295 : : part of STRING_CST covered by BYTES (where BYTES is relative to the
4296 : : start of the constant).
4297 : :
4298 : : Return a tristate:
4299 : : - true if there definitely is a zero byte, writing to *OUT_BYTES_READ
4300 : : the number of bytes from that would be read, including the zero byte.
4301 : : - false if there definitely isn't a zero byte
4302 : : - unknown if we don't know. */
4303 : :
4304 : : static tristate
4305 : 284 : string_cst_has_null_terminator (tree string_cst,
4306 : : const byte_range &bytes,
4307 : : byte_offset_t *out_bytes_read)
4308 : : {
4309 : 284 : gcc_assert (bytes.m_start_byte_offset >= 0);
4310 : :
4311 : : /* If we're beyond the string_cst, reads are unsuccessful. */
4312 : 284 : if (tree cst_size = get_string_cst_size (string_cst))
4313 : 284 : if (TREE_CODE (cst_size) == INTEGER_CST)
4314 : 284 : if (bytes.m_start_byte_offset >= TREE_INT_CST_LOW (cst_size))
4315 : 0 : return tristate::unknown ();
4316 : :
4317 : : /* Assume all bytes after TREE_STRING_LENGTH are zero. This handles
4318 : : the case where an array is initialized with a string_cst that isn't
4319 : : as long as the array, where the remaining elements are
4320 : : empty-initialized and thus zeroed. */
4321 : 284 : if (bytes.m_start_byte_offset >= TREE_STRING_LENGTH (string_cst))
4322 : : {
4323 : 2 : *out_bytes_read = 1;
4324 : 2 : return tristate (true);
4325 : : }
4326 : :
4327 : : /* Look for the first 0 byte within STRING_CST
4328 : : from START_READ_OFFSET onwards. */
4329 : 282 : const byte_offset_t num_bytes_to_search
4330 : 564 : = std::min<byte_offset_t> ((TREE_STRING_LENGTH (string_cst)
4331 : 282 : - bytes.m_start_byte_offset),
4332 : 282 : bytes.m_size_in_bytes);
4333 : 282 : const char *start = (TREE_STRING_POINTER (string_cst)
4334 : 282 : + bytes.m_start_byte_offset.slow ());
4335 : 282 : if (num_bytes_to_search >= 0)
4336 : 282 : if (const void *p = memchr (start, 0, bytes.m_size_in_bytes.slow ()))
4337 : : {
4338 : 162 : *out_bytes_read = (const char *)p - start + 1;
4339 : 162 : return tristate (true);
4340 : : }
4341 : :
4342 : 120 : *out_bytes_read = bytes.m_size_in_bytes;
4343 : 120 : return tristate (false);
4344 : : }
4345 : :
4346 : : static tristate
4347 : : svalue_byte_range_has_null_terminator (const svalue *sval,
4348 : : const byte_range &bytes,
4349 : : byte_offset_t *out_bytes_read,
4350 : : logger *logger);
4351 : :
4352 : : /* Determine if there is a zero terminator somewhere in the
4353 : : part of SVAL covered by BYTES (where BYTES is relative to the svalue).
4354 : :
4355 : : Return a tristate:
4356 : : - true if there definitely is a zero byte, writing to *OUT_BYTES_READ
4357 : : the number of bytes from that would be read, including the zero byte.
4358 : : - false if there definitely isn't a zero byte
4359 : : - unknown if we don't know.
4360 : :
4361 : : Use LOGGER (if non-null) for any logging. */
4362 : :
4363 : : static tristate
4364 : 605 : svalue_byte_range_has_null_terminator_1 (const svalue *sval,
4365 : : const byte_range &bytes,
4366 : : byte_offset_t *out_bytes_read,
4367 : : logger *logger)
4368 : : {
4369 : 605 : if (bytes.m_start_byte_offset == 0
4370 : 605 : && sval->all_zeroes_p ())
4371 : : {
4372 : : /* The initial byte of an all-zeroes SVAL is a zero byte. */
4373 : 21 : *out_bytes_read = 1;
4374 : 21 : return tristate (true);
4375 : : }
4376 : :
4377 : 584 : switch (sval->get_kind ())
4378 : : {
4379 : 181 : case SK_CONSTANT:
4380 : 181 : {
4381 : 181 : tree cst
4382 : 181 : = as_a <const constant_svalue *> (sval)->get_constant ();
4383 : 181 : switch (TREE_CODE (cst))
4384 : : {
4385 : 166 : case STRING_CST:
4386 : 166 : return string_cst_has_null_terminator (cst, bytes, out_bytes_read);
4387 : 15 : case INTEGER_CST:
4388 : 15 : if (bytes.m_start_byte_offset == 0
4389 : 15 : && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (cst))))
4390 : : {
4391 : : /* Model accesses to the initial byte of a 1-byte
4392 : : INTEGER_CST. */
4393 : 13 : *out_bytes_read = 1;
4394 : 13 : if (zerop (cst))
4395 : 0 : return tristate (true);
4396 : : else
4397 : 13 : return tristate (false);
4398 : : }
4399 : : /* Treat any other access to an INTEGER_CST as unknown. */
4400 : 2 : return tristate::TS_UNKNOWN;
4401 : :
4402 : : default:
4403 : : break;
4404 : : }
4405 : : }
4406 : : break;
4407 : :
4408 : 127 : case SK_INITIAL:
4409 : 127 : {
4410 : 127 : const initial_svalue *initial_sval = (const initial_svalue *)sval;
4411 : 127 : const region *reg = initial_sval->get_region ();
4412 : 127 : if (const string_region *string_reg = reg->dyn_cast_string_region ())
4413 : : {
4414 : 118 : tree string_cst = string_reg->get_string_cst ();
4415 : 118 : return string_cst_has_null_terminator (string_cst,
4416 : : bytes,
4417 : 118 : out_bytes_read);
4418 : : }
4419 : 9 : return tristate::TS_UNKNOWN;
4420 : : }
4421 : 67 : break;
4422 : :
4423 : 67 : case SK_BITS_WITHIN:
4424 : 67 : {
4425 : 67 : const bits_within_svalue *bits_within_sval
4426 : : = (const bits_within_svalue *)sval;
4427 : 67 : byte_range bytes_within_inner (0, 0);
4428 : 67 : if (bits_within_sval->get_bits ().as_byte_range (&bytes_within_inner))
4429 : : {
4430 : : /* Consider e.g. looking for null terminator of
4431 : : bytes 2-4 of BITS_WITHIN(bytes 10-15 of inner_sval)
4432 : :
4433 : : This is equivalent to looking within bytes 12-14 of
4434 : : inner_sval. */
4435 : 67 : const byte_offset_t start_byte_relative_to_inner
4436 : 67 : = (bytes.m_start_byte_offset
4437 : 67 : + bytes_within_inner.m_start_byte_offset);
4438 : 67 : const byte_offset_t next_byte_relative_to_inner
4439 : 67 : = (bytes.get_next_byte_offset ()
4440 : 67 : + bytes_within_inner.m_start_byte_offset);
4441 : 67 : if (next_byte_relative_to_inner > start_byte_relative_to_inner)
4442 : : {
4443 : 67 : const byte_range relative_to_inner
4444 : : (start_byte_relative_to_inner,
4445 : 67 : next_byte_relative_to_inner - start_byte_relative_to_inner);
4446 : 67 : const svalue *inner_sval
4447 : 67 : = bits_within_sval->get_inner_svalue ();
4448 : 67 : return svalue_byte_range_has_null_terminator (inner_sval,
4449 : : relative_to_inner,
4450 : : out_bytes_read,
4451 : : logger);
4452 : : }
4453 : : }
4454 : : }
4455 : 0 : break;
4456 : :
4457 : : default:
4458 : : // TODO: it may be possible to handle other cases here.
4459 : : break;
4460 : : }
4461 : 209 : return tristate::TS_UNKNOWN;
4462 : : }
4463 : :
4464 : : /* Like svalue_byte_range_has_null_terminator_1, but add logging. */
4465 : :
4466 : : static tristate
4467 : 605 : svalue_byte_range_has_null_terminator (const svalue *sval,
4468 : : const byte_range &bytes,
4469 : : byte_offset_t *out_bytes_read,
4470 : : logger *logger)
4471 : : {
4472 : 605 : LOG_SCOPE (logger);
4473 : 605 : if (logger)
4474 : : {
4475 : 0 : pretty_printer *pp = logger->get_printer ();
4476 : 0 : logger->start_log_line ();
4477 : 0 : bytes.dump_to_pp (pp);
4478 : 0 : logger->log_partial (" of sval: ");
4479 : 0 : sval->dump_to_pp (pp, true);
4480 : 0 : logger->end_log_line ();
4481 : : }
4482 : 605 : tristate ts
4483 : 605 : = svalue_byte_range_has_null_terminator_1 (sval, bytes,
4484 : : out_bytes_read, logger);
4485 : 605 : if (logger)
4486 : : {
4487 : 0 : pretty_printer *pp = logger->get_printer ();
4488 : 0 : logger->start_log_line ();
4489 : 0 : pp_printf (pp, "has null terminator: %s", ts.as_string ());
4490 : 0 : if (ts.is_true ())
4491 : : {
4492 : 0 : pp_string (pp, "; bytes read: ");
4493 : 0 : pp_wide_int (pp, *out_bytes_read, SIGNED);
4494 : : }
4495 : 0 : logger->end_log_line ();
4496 : : }
4497 : 1210 : return ts;
4498 : 605 : }
4499 : :
4500 : : /* A frozen copy of a single base region's binding_cluster within a store,
4501 : : optimized for traversal of the concrete parts in byte order.
4502 : : This only captures concrete bindings, and is an implementation detail
4503 : : of region_model::scan_for_null_terminator. */
4504 : :
4505 : 3414 : class iterable_cluster
4506 : : {
4507 : : public:
4508 : 3414 : iterable_cluster (const binding_cluster *cluster)
4509 : 3414 : {
4510 : 3414 : if (!cluster)
4511 : : return;
4512 : 2709 : for (auto iter : *cluster)
4513 : : {
4514 : 949 : const binding_key *key = iter.m_key;
4515 : 949 : const svalue *sval = iter.m_sval;
4516 : :
4517 : 1898 : if (const concrete_binding *concrete_key
4518 : 949 : = key->dyn_cast_concrete_binding ())
4519 : : {
4520 : 755 : byte_range fragment_bytes (0, 0);
4521 : 755 : if (concrete_key->get_byte_range (&fragment_bytes))
4522 : 755 : m_fragments.safe_push (fragment (fragment_bytes, sval));
4523 : : }
4524 : : else
4525 : 194 : m_symbolic_bindings.safe_push (key);
4526 : : }
4527 : 1760 : m_fragments.qsort (fragment::cmp_ptrs);
4528 : : }
4529 : :
4530 : : bool
4531 : 3547 : get_fragment_for_byte (byte_offset_t byte, fragment *out_frag) const
4532 : : {
4533 : : /* TODO: binary search rather than linear. */
4534 : 3547 : unsigned iter_idx;
4535 : 3756 : for (iter_idx = 0; iter_idx < m_fragments.length (); iter_idx++)
4536 : : {
4537 : 747 : if (m_fragments[iter_idx].m_byte_range.contains_p (byte))
4538 : : {
4539 : 538 : *out_frag = m_fragments[iter_idx];
4540 : 538 : return true;
4541 : : }
4542 : : }
4543 : : return false;
4544 : : }
4545 : :
4546 : 3009 : bool has_symbolic_bindings_p () const
4547 : : {
4548 : 6018 : return !m_symbolic_bindings.is_empty ();
4549 : : }
4550 : :
4551 : 0 : void dump_to_pp (pretty_printer *pp) const
4552 : : {
4553 : 0 : pp_string (pp, "iterable_cluster (fragments: [");
4554 : 0 : for (auto const &iter : &m_fragments)
4555 : : {
4556 : 0 : if (&iter != m_fragments.begin ())
4557 : 0 : pp_string (pp, ", ");
4558 : 0 : iter.dump_to_pp (pp);
4559 : : }
4560 : 0 : pp_printf (pp, "], symbolic bindings: [");
4561 : 0 : for (auto const &iter : m_symbolic_bindings)
4562 : : {
4563 : 0 : if (&iter != m_symbolic_bindings.begin ())
4564 : 0 : pp_string (pp, ", ");
4565 : 0 : (*iter).dump_to_pp (pp, true);
4566 : : }
4567 : 0 : pp_string (pp, "])");
4568 : 0 : }
4569 : :
4570 : : private:
4571 : : auto_vec<fragment> m_fragments;
4572 : : auto_vec<const binding_key *> m_symbolic_bindings;
4573 : : };
4574 : :
4575 : : /* Simulate reading the bytes at BYTES from BASE_REG.
4576 : : Complain to CTXT about any issues with the read e.g. out-of-bounds. */
4577 : :
4578 : : const svalue *
4579 : 7537 : region_model::get_store_bytes (const region *base_reg,
4580 : : const byte_range &bytes,
4581 : : region_model_context *ctxt) const
4582 : : {
4583 : : /* Shortcut reading all of a string_region. */
4584 : 7537 : if (bytes.get_start_byte_offset () == 0)
4585 : 7348 : if (const string_region *string_reg = base_reg->dyn_cast_string_region ())
4586 : 4402 : if (bytes.m_size_in_bytes
4587 : 4402 : == TREE_STRING_LENGTH (string_reg->get_string_cst ()))
4588 : 4402 : return m_mgr->get_or_create_initial_value (base_reg);
4589 : :
4590 : 3135 : const svalue *index_sval
4591 : 3135 : = m_mgr->get_or_create_int_cst (size_type_node,
4592 : 3135 : bytes.get_start_byte_offset ());
4593 : 3135 : const region *offset_reg = m_mgr->get_offset_region (base_reg,
4594 : : NULL_TREE,
4595 : : index_sval);
4596 : 3135 : const svalue *byte_size_sval
4597 : 3135 : = m_mgr->get_or_create_int_cst (size_type_node, bytes.m_size_in_bytes);
4598 : 3135 : const region *read_reg = m_mgr->get_sized_region (offset_reg,
4599 : : NULL_TREE,
4600 : : byte_size_sval);
4601 : :
4602 : : /* Simulate reading those bytes from the store. */
4603 : 3135 : const svalue *sval = get_store_value (read_reg, ctxt);
4604 : 3135 : return sval;
4605 : : }
4606 : :
4607 : : static tree
4608 : 2689 : get_tree_for_byte_offset (tree ptr_expr, byte_offset_t byte_offset)
4609 : : {
4610 : 2689 : gcc_assert (ptr_expr);
4611 : 2689 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
4612 : 2689 : return fold_build2 (MEM_REF,
4613 : : char_type_node,
4614 : : ptr_expr, wide_int_to_tree (ptype, byte_offset));
4615 : : }
4616 : :
4617 : : /* Simulate a series of reads of REG until we find a 0 byte
4618 : : (equivalent to calling strlen).
4619 : :
4620 : : Complain to CTXT and return NULL if:
4621 : : - the buffer pointed to isn't null-terminated
4622 : : - the buffer pointed to has any uninitialized bytes before any 0-terminator
4623 : : - any of the reads aren't within the bounds of the underlying base region
4624 : :
4625 : : Otherwise, return a svalue for the number of bytes read (strlen + 1),
4626 : : and, if OUT_SVAL is non-NULL, write to *OUT_SVAL with an svalue
4627 : : representing the content of REG up to and including the terminator.
4628 : :
4629 : : Algorithm
4630 : : =========
4631 : :
4632 : : Get offset for first byte to read.
4633 : : Find the binding (if any) that contains it.
4634 : : Find the size in bits of that binding.
4635 : : Round to the nearest byte (which way???)
4636 : : Or maybe give up if we have a partial binding there.
4637 : : Get the svalue from the binding.
4638 : : Determine the strlen (if any) of that svalue.
4639 : : Does it have a 0-terminator within it?
4640 : : If so, we have a partial read up to and including that terminator
4641 : : Read those bytes from the store; add to the result in the correct place.
4642 : : Finish
4643 : : If not, we have a full read of that svalue
4644 : : Read those bytes from the store; add to the result in the correct place.
4645 : : Update read/write offsets
4646 : : Continue
4647 : : If unknown:
4648 : : Result is unknown
4649 : : Finish
4650 : : */
4651 : :
4652 : : const svalue *
4653 : 7933 : region_model::scan_for_null_terminator_1 (const region *reg,
4654 : : tree expr,
4655 : : const svalue **out_sval,
4656 : : region_model_context *ctxt) const
4657 : : {
4658 : 7933 : logger *logger = ctxt ? ctxt->get_logger () : nullptr;
4659 : 7933 : store_manager *store_mgr = m_mgr->get_store_manager ();
4660 : :
4661 : 7933 : region_offset offset = reg->get_offset (m_mgr);
4662 : 7933 : if (offset.symbolic_p ())
4663 : : {
4664 : 115 : if (out_sval)
4665 : 0 : *out_sval = get_store_value (reg, nullptr);
4666 : 115 : if (logger)
4667 : 0 : logger->log ("offset is symbolic");
4668 : 115 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4669 : : }
4670 : 7818 : byte_offset_t src_byte_offset;
4671 : 7818 : if (!offset.get_concrete_byte_offset (&src_byte_offset))
4672 : : {
4673 : 0 : if (out_sval)
4674 : 0 : *out_sval = get_store_value (reg, nullptr);
4675 : 0 : if (logger)
4676 : 0 : logger->log ("can't get concrete byte offset");
4677 : 0 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4678 : : }
4679 : 7818 : const byte_offset_t initial_src_byte_offset = src_byte_offset;
4680 : 7818 : byte_offset_t dst_byte_offset = 0;
4681 : :
4682 : 7818 : const region *base_reg = reg->get_base_region ();
4683 : :
4684 : 7818 : if (const string_region *str_reg = base_reg->dyn_cast_string_region ())
4685 : : {
4686 : 4404 : tree string_cst = str_reg->get_string_cst ();
4687 : 4404 : if (const void *p = memchr (TREE_STRING_POINTER (string_cst),
4688 : : 0,
4689 : 4404 : TREE_STRING_LENGTH (string_cst)))
4690 : : {
4691 : 4404 : size_t num_bytes_read
4692 : 4404 : = (const char *)p - TREE_STRING_POINTER (string_cst) + 1;
4693 : : /* Simulate the read. */
4694 : 4404 : byte_range bytes_to_read (0, num_bytes_read);
4695 : 4404 : const svalue *sval = get_store_bytes (reg, bytes_to_read, ctxt);
4696 : 4404 : if (out_sval)
4697 : 834 : *out_sval = sval;
4698 : 4404 : if (logger)
4699 : 0 : logger->log ("using string_cst");
4700 : 4404 : return m_mgr->get_or_create_int_cst (size_type_node,
4701 : 4404 : num_bytes_read);
4702 : : }
4703 : : }
4704 : :
4705 : 3414 : const binding_cluster *cluster = m_store.get_cluster (base_reg);
4706 : 3414 : iterable_cluster c (cluster);
4707 : 3414 : if (logger)
4708 : : {
4709 : 0 : pretty_printer *pp = logger->get_printer ();
4710 : 0 : logger->start_log_line ();
4711 : 0 : c.dump_to_pp (pp);
4712 : 0 : logger->end_log_line ();
4713 : : }
4714 : :
4715 : 3414 : binding_map result (*store_mgr);
4716 : :
4717 : 133 : while (1)
4718 : : {
4719 : 3547 : fragment f;
4720 : 3547 : if (c.get_fragment_for_byte (src_byte_offset, &f))
4721 : : {
4722 : 538 : if (logger)
4723 : : {
4724 : 0 : logger->start_log_line ();
4725 : 0 : pretty_printer *pp = logger->get_printer ();
4726 : 0 : pp_printf (pp, "src_byte_offset: ");
4727 : 0 : pp_wide_int (pp, src_byte_offset, SIGNED);
4728 : 0 : pp_string (pp, ": ");
4729 : 0 : f.dump_to_pp (pp);
4730 : 0 : logger->end_log_line ();
4731 : : }
4732 : 538 : gcc_assert (f.m_byte_range.contains_p (src_byte_offset));
4733 : : /* src_byte_offset and f.m_byte_range are both expressed relative to
4734 : : the base region.
4735 : : Convert to a byte_range relative to the svalue. */
4736 : 538 : const byte_range bytes_relative_to_svalue
4737 : 538 : (src_byte_offset - f.m_byte_range.get_start_byte_offset (),
4738 : 538 : f.m_byte_range.get_next_byte_offset () - src_byte_offset);
4739 : 538 : byte_offset_t fragment_bytes_read;
4740 : 538 : tristate is_terminated
4741 : 538 : = svalue_byte_range_has_null_terminator (f.m_sval,
4742 : : bytes_relative_to_svalue,
4743 : : &fragment_bytes_read,
4744 : : logger);
4745 : 538 : if (is_terminated.is_unknown ())
4746 : : {
4747 : 220 : if (out_sval)
4748 : 2 : *out_sval = get_store_value (reg, nullptr);
4749 : 405 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4750 : : }
4751 : :
4752 : : /* Simulate reading those bytes from the store. */
4753 : 318 : byte_range bytes_to_read (src_byte_offset, fragment_bytes_read);
4754 : 318 : const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt);
4755 : 318 : check_for_poison (sval, expr, nullptr, ctxt);
4756 : :
4757 : 318 : if (out_sval)
4758 : : {
4759 : 8 : byte_range bytes_to_write (dst_byte_offset, fragment_bytes_read);
4760 : 8 : const binding_key *key
4761 : 8 : = store_mgr->get_concrete_binding (bytes_to_write);
4762 : 8 : result.put (key, sval);
4763 : : }
4764 : :
4765 : 318 : src_byte_offset += fragment_bytes_read;
4766 : 318 : dst_byte_offset += fragment_bytes_read;
4767 : :
4768 : 318 : if (is_terminated.is_true ())
4769 : : {
4770 : 185 : if (out_sval)
4771 : 5 : *out_sval = m_mgr->get_or_create_compound_svalue (NULL_TREE,
4772 : : result);
4773 : 185 : if (logger)
4774 : 0 : logger->log ("got terminator");
4775 : 185 : return m_mgr->get_or_create_int_cst (size_type_node,
4776 : 185 : dst_byte_offset);
4777 : : }
4778 : : }
4779 : : else
4780 : : break;
4781 : : }
4782 : :
4783 : : /* No binding for this base_region, or no binding at src_byte_offset
4784 : : (or a symbolic binding). */
4785 : :
4786 : 3009 : if (c.has_symbolic_bindings_p ())
4787 : : {
4788 : 194 : if (out_sval)
4789 : 75 : *out_sval = get_store_value (reg, nullptr);
4790 : 194 : if (logger)
4791 : 0 : logger->log ("got symbolic binding");
4792 : 194 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4793 : : }
4794 : :
4795 : : /* TODO: the various special-cases seen in
4796 : : region_model::get_store_value. */
4797 : :
4798 : : /* Simulate reading from this byte, then give up. */
4799 : 2815 : byte_range bytes_to_read (src_byte_offset, 1);
4800 : 2815 : const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt);
4801 : 2815 : tree byte_expr
4802 : : = (expr
4803 : 5504 : ? get_tree_for_byte_offset (expr,
4804 : : src_byte_offset - initial_src_byte_offset)
4805 : : : NULL_TREE);
4806 : 2815 : check_for_poison (sval, byte_expr, nullptr, ctxt);
4807 : 2815 : if (base_reg->can_have_initial_svalue_p ())
4808 : : {
4809 : 2615 : if (out_sval)
4810 : 264 : *out_sval = get_store_value (reg, nullptr);
4811 : 2615 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4812 : : }
4813 : : else
4814 : : return nullptr;
4815 : 6828 : }
4816 : :
4817 : : /* Like region_model::scan_for_null_terminator_1, but add logging. */
4818 : :
4819 : : const svalue *
4820 : 7933 : region_model::scan_for_null_terminator (const region *reg,
4821 : : tree expr,
4822 : : const svalue **out_sval,
4823 : : region_model_context *ctxt) const
4824 : : {
4825 : 7933 : logger *logger = ctxt ? ctxt->get_logger () : nullptr;
4826 : 7933 : LOG_SCOPE (logger);
4827 : 7933 : if (logger)
4828 : : {
4829 : 0 : pretty_printer *pp = logger->get_printer ();
4830 : 0 : logger->start_log_line ();
4831 : 0 : logger->log_partial ("region: ");
4832 : 0 : reg->dump_to_pp (pp, true);
4833 : 0 : logger->end_log_line ();
4834 : : }
4835 : 7933 : const svalue *sval = scan_for_null_terminator_1 (reg, expr, out_sval, ctxt);
4836 : 7933 : if (logger)
4837 : : {
4838 : 0 : pretty_printer *pp = logger->get_printer ();
4839 : 0 : logger->start_log_line ();
4840 : 0 : logger->log_partial ("length result: ");
4841 : 0 : if (sval)
4842 : 0 : sval->dump_to_pp (pp, true);
4843 : : else
4844 : 0 : pp_printf (pp, "NULL");
4845 : 0 : logger->end_log_line ();
4846 : 0 : if (out_sval)
4847 : : {
4848 : 0 : logger->start_log_line ();
4849 : 0 : logger->log_partial ("content result: ");
4850 : 0 : if (*out_sval)
4851 : 0 : (*out_sval)->dump_to_pp (pp, true);
4852 : : else
4853 : 0 : pp_printf (pp, "NULL");
4854 : 0 : logger->end_log_line ();
4855 : : }
4856 : : }
4857 : 15866 : return sval;
4858 : 7933 : }
4859 : :
4860 : : /* Check that argument ARG_IDX (0-based) to the call described by CD
4861 : : is a pointer to a valid null-terminated string.
4862 : :
4863 : : Simulate scanning through the buffer, reading until we find a 0 byte
4864 : : (equivalent to calling strlen).
4865 : :
4866 : : Complain and return nullptr if:
4867 : : - the buffer pointed to isn't null-terminated
4868 : : - the buffer pointed to has any uninitalized bytes before any 0-terminator
4869 : : - any of the reads aren't within the bounds of the underlying base region
4870 : :
4871 : : Otherwise, return a svalue for strlen of the buffer (*not* including
4872 : : the null terminator).
4873 : :
4874 : : TODO: we should also complain if:
4875 : : - the pointer is NULL (or could be). */
4876 : :
4877 : : const svalue *
4878 : 206 : region_model::check_for_null_terminated_string_arg (const call_details &cd,
4879 : : unsigned arg_idx) const
4880 : : {
4881 : 206 : return check_for_null_terminated_string_arg (cd,
4882 : : arg_idx,
4883 : : false, /* include_terminator */
4884 : 206 : nullptr); // out_sval
4885 : : }
4886 : :
4887 : :
4888 : : /* Check that argument ARG_IDX (0-based) to the call described by CD
4889 : : is a pointer to a valid null-terminated string.
4890 : :
4891 : : Simulate scanning through the buffer, reading until we find a 0 byte
4892 : : (equivalent to calling strlen).
4893 : :
4894 : : Complain and return nullptr if:
4895 : : - the buffer pointed to isn't null-terminated
4896 : : - the buffer pointed to has any uninitalized bytes before any 0-terminator
4897 : : - any of the reads aren't within the bounds of the underlying base region
4898 : :
4899 : : Otherwise, return a svalue. This will be the number of bytes read
4900 : : (including the null terminator) if INCLUDE_TERMINATOR is true, or strlen
4901 : : of the buffer (not including the null terminator) if it is false.
4902 : :
4903 : : Also, when returning an svalue, if OUT_SVAL is non-nullptr, write to
4904 : : *OUT_SVAL with an svalue representing the content of the buffer up to
4905 : : and including the terminator.
4906 : :
4907 : : TODO: we should also complain if:
4908 : : - the pointer is NULL (or could be). */
4909 : :
4910 : : const svalue *
4911 : 7462 : region_model::check_for_null_terminated_string_arg (const call_details &cd,
4912 : : unsigned arg_idx,
4913 : : bool include_terminator,
4914 : : const svalue **out_sval) const
4915 : : {
4916 : 0 : class null_terminator_check_event : public custom_event
4917 : : {
4918 : : public:
4919 : 158 : null_terminator_check_event (const event_loc_info &loc_info,
4920 : : const call_arg_details &arg_details)
4921 : 158 : : custom_event (loc_info),
4922 : 158 : m_arg_details (arg_details)
4923 : : {
4924 : : }
4925 : :
4926 : 292 : void print_desc (pretty_printer &pp) const final override
4927 : : {
4928 : 292 : if (m_arg_details.m_arg_expr)
4929 : 292 : pp_printf (&pp,
4930 : : "while looking for null terminator"
4931 : : " for argument %i (%qE) of %qD...",
4932 : 292 : m_arg_details.m_arg_idx + 1,
4933 : : m_arg_details.m_arg_expr,
4934 : 292 : m_arg_details.m_called_fndecl);
4935 : : else
4936 : 0 : pp_printf (&pp,
4937 : : "while looking for null terminator"
4938 : : " for argument %i of %qD...",
4939 : 0 : m_arg_details.m_arg_idx + 1,
4940 : 0 : m_arg_details.m_called_fndecl);
4941 : 292 : }
4942 : :
4943 : : private:
4944 : : const call_arg_details m_arg_details;
4945 : : };
4946 : :
4947 : 0 : class null_terminator_check_decl_note
4948 : : : public pending_note_subclass<null_terminator_check_decl_note>
4949 : : {
4950 : : public:
4951 : 158 : null_terminator_check_decl_note (const call_arg_details &arg_details)
4952 : 158 : : m_arg_details (arg_details)
4953 : : {
4954 : : }
4955 : :
4956 : 1276 : const char *get_kind () const final override
4957 : : {
4958 : 1276 : return "null_terminator_check_decl_note";
4959 : : }
4960 : :
4961 : 146 : void emit () const final override
4962 : : {
4963 : 146 : inform_about_expected_null_terminated_string_arg (m_arg_details);
4964 : 146 : }
4965 : :
4966 : 638 : bool operator== (const null_terminator_check_decl_note &other) const
4967 : : {
4968 : 638 : return m_arg_details == other.m_arg_details;
4969 : : }
4970 : :
4971 : : private:
4972 : : const call_arg_details m_arg_details;
4973 : : };
4974 : :
4975 : : /* Subclass of decorated_region_model_context that
4976 : : adds the above event and note to any saved diagnostics. */
4977 : 7462 : class annotating_ctxt : public annotating_context
4978 : : {
4979 : : public:
4980 : 7462 : annotating_ctxt (const call_details &cd,
4981 : : unsigned arg_idx)
4982 : 7462 : : annotating_context (cd.get_ctxt ()),
4983 : 7462 : m_cd (cd),
4984 : 7462 : m_arg_idx (arg_idx)
4985 : : {
4986 : : }
4987 : 158 : void add_annotations () final override
4988 : : {
4989 : 158 : call_arg_details arg_details (m_cd, m_arg_idx);
4990 : 316 : event_loc_info loc_info (m_cd.get_location (),
4991 : 158 : m_cd.get_model ()->get_current_function ()->decl,
4992 : 316 : m_cd.get_model ()->get_stack_depth ());
4993 : :
4994 : 158 : add_event
4995 : 158 : (std::make_unique<null_terminator_check_event> (loc_info,
4996 : : arg_details));
4997 : 158 : add_note
4998 : 158 : (std::make_unique <null_terminator_check_decl_note> (arg_details));
4999 : 158 : }
5000 : : private:
5001 : : const call_details &m_cd;
5002 : : unsigned m_arg_idx;
5003 : : };
5004 : :
5005 : : /* Use this ctxt below so that any diagnostics that get added
5006 : : get annotated. */
5007 : 7462 : annotating_ctxt my_ctxt (cd, arg_idx);
5008 : :
5009 : 7462 : const svalue *arg_sval = cd.get_arg_svalue (arg_idx);
5010 : 7462 : const region *buf_reg
5011 : 7462 : = deref_rvalue (arg_sval, cd.get_arg_tree (arg_idx), &my_ctxt);
5012 : :
5013 : 14924 : if (const svalue *num_bytes_read_sval
5014 : 7462 : = scan_for_null_terminator (buf_reg,
5015 : : cd.get_arg_tree (arg_idx),
5016 : : out_sval,
5017 : : &my_ctxt))
5018 : : {
5019 : 7300 : if (include_terminator)
5020 : : return num_bytes_read_sval;
5021 : : else
5022 : : {
5023 : : /* strlen is (bytes_read - 1). */
5024 : 6120 : const svalue *one = m_mgr->get_or_create_int_cst (size_type_node, 1);
5025 : 6120 : return m_mgr->get_or_create_binop (size_type_node,
5026 : : MINUS_EXPR,
5027 : : num_bytes_read_sval,
5028 : 6120 : one);
5029 : : }
5030 : : }
5031 : : else
5032 : : return nullptr;
5033 : : }
5034 : :
5035 : : /* Remove all bindings overlapping REG within the store. */
5036 : :
5037 : : void
5038 : 5724 : region_model::clobber_region (const region *reg)
5039 : : {
5040 : 5724 : m_store.clobber_region (m_mgr->get_store_manager(), reg);
5041 : 5724 : }
5042 : :
5043 : : /* Remove any bindings for REG within the store. */
5044 : :
5045 : : void
5046 : 203370 : region_model::purge_region (const region *reg)
5047 : : {
5048 : 203370 : m_store.purge_region (m_mgr->get_store_manager(), reg);
5049 : 203370 : }
5050 : :
5051 : : /* Fill REG with SVAL.
5052 : : Use CTXT to report any warnings associated with the write
5053 : : (e.g. out-of-bounds). */
5054 : :
5055 : : void
5056 : 656 : region_model::fill_region (const region *reg,
5057 : : const svalue *sval,
5058 : : region_model_context *ctxt)
5059 : : {
5060 : 656 : check_region_for_write (reg, nullptr, ctxt);
5061 : 656 : m_store.fill_region (m_mgr->get_store_manager(), reg, sval);
5062 : 656 : }
5063 : :
5064 : : /* Zero-fill REG.
5065 : : Use CTXT to report any warnings associated with the write
5066 : : (e.g. out-of-bounds). */
5067 : :
5068 : : void
5069 : 693 : region_model::zero_fill_region (const region *reg,
5070 : : region_model_context *ctxt)
5071 : : {
5072 : 693 : check_region_for_write (reg, nullptr, ctxt);
5073 : 693 : m_store.zero_fill_region (m_mgr->get_store_manager(), reg);
5074 : 693 : }
5075 : :
5076 : : /* Copy NUM_BYTES_SVAL of SVAL to DEST_REG.
5077 : : Use CTXT to report any warnings associated with the copy
5078 : : (e.g. out-of-bounds writes). */
5079 : :
5080 : : void
5081 : 2138 : region_model::write_bytes (const region *dest_reg,
5082 : : const svalue *num_bytes_sval,
5083 : : const svalue *sval,
5084 : : region_model_context *ctxt)
5085 : : {
5086 : 2138 : const region *sized_dest_reg
5087 : 2138 : = m_mgr->get_sized_region (dest_reg, NULL_TREE, num_bytes_sval);
5088 : 2138 : set_value (sized_dest_reg, sval, ctxt);
5089 : 2138 : }
5090 : :
5091 : : /* Read NUM_BYTES_SVAL from SRC_REG.
5092 : : Use CTXT to report any warnings associated with the copy
5093 : : (e.g. out-of-bounds reads, copying of uninitialized values, etc). */
5094 : :
5095 : : const svalue *
5096 : 1086 : region_model::read_bytes (const region *src_reg,
5097 : : tree src_ptr_expr,
5098 : : const svalue *num_bytes_sval,
5099 : : region_model_context *ctxt) const
5100 : : {
5101 : 1086 : if (num_bytes_sval->get_kind () == SK_UNKNOWN)
5102 : 188 : return m_mgr->get_or_create_unknown_svalue (NULL_TREE);
5103 : 898 : const region *sized_src_reg
5104 : 898 : = m_mgr->get_sized_region (src_reg, NULL_TREE, num_bytes_sval);
5105 : 898 : const svalue *src_contents_sval = get_store_value (sized_src_reg, ctxt);
5106 : 898 : check_for_poison (src_contents_sval, src_ptr_expr,
5107 : : sized_src_reg, ctxt);
5108 : 898 : return src_contents_sval;
5109 : : }
5110 : :
5111 : : /* Copy NUM_BYTES_SVAL bytes from SRC_REG to DEST_REG.
5112 : : Use CTXT to report any warnings associated with the copy
5113 : : (e.g. out-of-bounds reads/writes, copying of uninitialized values,
5114 : : etc). */
5115 : :
5116 : : void
5117 : 500 : region_model::copy_bytes (const region *dest_reg,
5118 : : const region *src_reg,
5119 : : tree src_ptr_expr,
5120 : : const svalue *num_bytes_sval,
5121 : : region_model_context *ctxt)
5122 : : {
5123 : 500 : const svalue *data_sval
5124 : 500 : = read_bytes (src_reg, src_ptr_expr, num_bytes_sval, ctxt);
5125 : 500 : write_bytes (dest_reg, num_bytes_sval, data_sval, ctxt);
5126 : 500 : }
5127 : :
5128 : : /* Mark REG as having unknown content. */
5129 : :
5130 : : void
5131 : 255 : region_model::mark_region_as_unknown (const region *reg,
5132 : : uncertainty_t *uncertainty)
5133 : : {
5134 : 255 : svalue_set maybe_live_values;
5135 : 255 : m_store.mark_region_as_unknown (m_mgr->get_store_manager(), reg,
5136 : : uncertainty, &maybe_live_values);
5137 : 255 : m_store.on_maybe_live_values (*m_mgr->get_store_manager (),
5138 : : maybe_live_values);
5139 : 255 : }
5140 : :
5141 : : /* Determine what is known about the condition "LHS_SVAL OP RHS_SVAL" within
5142 : : this model. */
5143 : :
5144 : : tristate
5145 : 204312 : region_model::eval_condition (const svalue *lhs,
5146 : : enum tree_code op,
5147 : : const svalue *rhs) const
5148 : : {
5149 : 204312 : gcc_assert (lhs);
5150 : 204312 : gcc_assert (rhs);
5151 : :
5152 : : /* For now, make no attempt to capture constraints on floating-point
5153 : : values. */
5154 : 204312 : if ((lhs->get_type () && FLOAT_TYPE_P (lhs->get_type ()))
5155 : 348817 : || (rhs->get_type () && FLOAT_TYPE_P (rhs->get_type ())))
5156 : 72 : return tristate::unknown ();
5157 : :
5158 : : /* See what we know based on the values. */
5159 : :
5160 : : /* Unwrap any unmergeable values. */
5161 : 204240 : lhs = lhs->unwrap_any_unmergeable ();
5162 : 204240 : rhs = rhs->unwrap_any_unmergeable ();
5163 : :
5164 : 204240 : if (lhs == rhs)
5165 : : {
5166 : : /* If we have the same svalue, then we have equality
5167 : : (apart from NaN-handling).
5168 : : TODO: should this definitely be the case for poisoned values? */
5169 : : /* Poisoned and unknown values are "unknowable". */
5170 : 20799 : if (lhs->get_kind () == SK_POISONED
5171 : 20799 : || lhs->get_kind () == SK_UNKNOWN)
5172 : 9593 : return tristate::TS_UNKNOWN;
5173 : :
5174 : 11206 : switch (op)
5175 : : {
5176 : 8273 : case EQ_EXPR:
5177 : 8273 : case GE_EXPR:
5178 : 8273 : case LE_EXPR:
5179 : 8273 : return tristate::TS_TRUE;
5180 : :
5181 : 2933 : case NE_EXPR:
5182 : 2933 : case GT_EXPR:
5183 : 2933 : case LT_EXPR:
5184 : 2933 : return tristate::TS_FALSE;
5185 : :
5186 : : default:
5187 : : /* For other ops, use the logic below. */
5188 : : break;
5189 : : }
5190 : : }
5191 : :
5192 : : /* If we have a pair of region_svalues, compare them. */
5193 : 183441 : if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ())
5194 : 18332 : if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ())
5195 : : {
5196 : 309 : tristate res = region_svalue::eval_condition (lhs_ptr, op, rhs_ptr);
5197 : 309 : if (res.is_known ())
5198 : 301 : return res;
5199 : : /* Otherwise, only known through constraints. */
5200 : : }
5201 : :
5202 : 183140 : if (const constant_svalue *cst_lhs = lhs->dyn_cast_constant_svalue ())
5203 : : {
5204 : : /* If we have a pair of constants, compare them. */
5205 : 46647 : if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
5206 : 11661 : return constant_svalue::eval_condition (cst_lhs, op, cst_rhs);
5207 : : else
5208 : : {
5209 : : /* When we have one constant, put it on the RHS. */
5210 : 34986 : std::swap (lhs, rhs);
5211 : 34986 : op = swap_tree_comparison (op);
5212 : : }
5213 : : }
5214 : 171479 : gcc_assert (lhs->get_kind () != SK_CONSTANT);
5215 : :
5216 : : /* Handle comparison against zero. */
5217 : 171479 : if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
5218 : 142121 : if (zerop (cst_rhs->get_constant ()))
5219 : : {
5220 : 87532 : if (const region_svalue *ptr = lhs->dyn_cast_region_svalue ())
5221 : : {
5222 : : /* A region_svalue is a non-NULL pointer, except in certain
5223 : : special cases (see the comment for region::non_null_p). */
5224 : 17852 : const region *pointee = ptr->get_pointee ();
5225 : 17852 : if (pointee->non_null_p ())
5226 : : {
5227 : 7899 : switch (op)
5228 : : {
5229 : 0 : default:
5230 : 0 : gcc_unreachable ();
5231 : :
5232 : 206 : case EQ_EXPR:
5233 : 206 : case GE_EXPR:
5234 : 206 : case LE_EXPR:
5235 : 206 : return tristate::TS_FALSE;
5236 : :
5237 : 7693 : case NE_EXPR:
5238 : 7693 : case GT_EXPR:
5239 : 7693 : case LT_EXPR:
5240 : 7693 : return tristate::TS_TRUE;
5241 : : }
5242 : : }
5243 : : }
5244 : 69680 : else if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ())
5245 : : {
5246 : : /* Treat offsets from a non-NULL pointer as being non-NULL. This
5247 : : isn't strictly true, in that eventually ptr++ will wrap
5248 : : around and be NULL, but it won't occur in practise and thus
5249 : : can be used to suppress effectively false positives that we
5250 : : shouldn't warn for. */
5251 : 16880 : if (binop->get_op () == POINTER_PLUS_EXPR)
5252 : : {
5253 : 9909 : tristate lhs_ts = eval_condition (binop->get_arg0 (), op, rhs);
5254 : 9909 : if (lhs_ts.is_known ())
5255 : 9224 : return lhs_ts;
5256 : : }
5257 : : }
5258 : 105600 : else if (const unaryop_svalue *unaryop
5259 : 52800 : = lhs->dyn_cast_unaryop_svalue ())
5260 : : {
5261 : 2587 : if (unaryop->get_op () == NEGATE_EXPR)
5262 : : {
5263 : : /* e.g. "-X <= 0" is equivalent to X >= 0". */
5264 : 49 : tristate lhs_ts = eval_condition (unaryop->get_arg (),
5265 : : swap_tree_comparison (op),
5266 : : rhs);
5267 : 49 : if (lhs_ts.is_known ())
5268 : 48 : return lhs_ts;
5269 : : }
5270 : : }
5271 : : }
5272 : :
5273 : : /* Handle rejection of equality for comparisons of the initial values of
5274 : : "external" values (such as params) with the address of locals. */
5275 : 154308 : if (const initial_svalue *init_lhs = lhs->dyn_cast_initial_svalue ())
5276 : 37281 : if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ())
5277 : : {
5278 : 97 : tristate res = compare_initial_and_pointer (init_lhs, rhs_ptr);
5279 : 97 : if (res.is_known ())
5280 : 32 : return res;
5281 : : }
5282 : 154276 : if (const initial_svalue *init_rhs = rhs->dyn_cast_initial_svalue ())
5283 : 5291 : if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ())
5284 : : {
5285 : 162 : tristate res = compare_initial_and_pointer (init_rhs, lhs_ptr);
5286 : 162 : if (res.is_known ())
5287 : 0 : return res;
5288 : : }
5289 : :
5290 : 154276 : if (const widening_svalue *widen_lhs = lhs->dyn_cast_widening_svalue ())
5291 : 5279 : if (tree rhs_cst = rhs->maybe_get_constant ())
5292 : : {
5293 : 2862 : tristate res = widen_lhs->eval_condition_without_cm (op, rhs_cst);
5294 : 2862 : if (res.is_known ())
5295 : 61 : return res;
5296 : : }
5297 : :
5298 : : /* Handle comparisons between two svalues with more than one operand. */
5299 : 154215 : if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ())
5300 : : {
5301 : 26548 : switch (op)
5302 : : {
5303 : : default:
5304 : : break;
5305 : 3336 : case EQ_EXPR:
5306 : 3336 : {
5307 : : /* TODO: binops can be equal even if they are not structurally
5308 : : equal in case of commutative operators. */
5309 : 3336 : tristate res = structural_equality (lhs, rhs);
5310 : 3336 : if (res.is_true ())
5311 : 44 : return res;
5312 : : }
5313 : 3292 : break;
5314 : 998 : case LE_EXPR:
5315 : 998 : {
5316 : 998 : tristate res = structural_equality (lhs, rhs);
5317 : 998 : if (res.is_true ())
5318 : 0 : return res;
5319 : : }
5320 : 998 : break;
5321 : 7519 : case GE_EXPR:
5322 : 7519 : {
5323 : 7519 : tristate res = structural_equality (lhs, rhs);
5324 : 7519 : if (res.is_true ())
5325 : 39 : return res;
5326 : 7480 : res = symbolic_greater_than (binop, rhs);
5327 : 7480 : if (res.is_true ())
5328 : 36 : return res;
5329 : : }
5330 : : break;
5331 : 8931 : case GT_EXPR:
5332 : 8931 : {
5333 : 8931 : tristate res = symbolic_greater_than (binop, rhs);
5334 : 8931 : if (res.is_true ())
5335 : 154 : return res;
5336 : : }
5337 : 8777 : break;
5338 : : }
5339 : : }
5340 : :
5341 : : /* Attempt to unwrap cast if there is one, and the types match. */
5342 : 153942 : tree lhs_type = lhs->get_type ();
5343 : 153942 : tree rhs_type = rhs->get_type ();
5344 : 153942 : if (lhs_type && rhs_type)
5345 : : {
5346 : 98744 : const unaryop_svalue *lhs_un_op = dyn_cast <const unaryop_svalue *> (lhs);
5347 : 98744 : const unaryop_svalue *rhs_un_op = dyn_cast <const unaryop_svalue *> (rhs);
5348 : 3384 : if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ())
5349 : 3194 : && rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ())
5350 : 98834 : && lhs_type == rhs_type)
5351 : : {
5352 : 90 : tristate res = eval_condition (lhs_un_op->get_arg (),
5353 : : op,
5354 : : rhs_un_op->get_arg ());
5355 : 90 : if (res.is_known ())
5356 : 0 : return res;
5357 : : }
5358 : 3294 : else if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ())
5359 : 101758 : && lhs_type == rhs_type)
5360 : : {
5361 : 2595 : tristate res = eval_condition (lhs_un_op->get_arg (), op, rhs);
5362 : 2595 : if (res.is_known ())
5363 : 35 : return res;
5364 : : }
5365 : 1937 : else if (rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ())
5366 : 97996 : && lhs_type == rhs_type)
5367 : : {
5368 : 1290 : tristate res = eval_condition (lhs, op, rhs_un_op->get_arg ());
5369 : 1290 : if (res.is_known ())
5370 : 0 : return res;
5371 : : }
5372 : : }
5373 : :
5374 : : /* Otherwise, try constraints.
5375 : : Cast to const to ensure we don't change the constraint_manager as we
5376 : : do this (e.g. by creating equivalence classes). */
5377 : 153907 : const constraint_manager *constraints = m_constraints;
5378 : 153907 : return constraints->eval_condition (lhs, op, rhs);
5379 : : }
5380 : :
5381 : : /* Subroutine of region_model::eval_condition, for rejecting
5382 : : equality of INIT_VAL(PARM) with &LOCAL. */
5383 : :
5384 : : tristate
5385 : 259 : region_model::compare_initial_and_pointer (const initial_svalue *init,
5386 : : const region_svalue *ptr) const
5387 : : {
5388 : 259 : const region *pointee = ptr->get_pointee ();
5389 : :
5390 : : /* If we have a pointer to something within a stack frame, it can't be the
5391 : : initial value of a param. */
5392 : 259 : if (pointee->maybe_get_frame_region ())
5393 : 32 : if (init->initial_value_of_param_p ())
5394 : 32 : return tristate::TS_FALSE;
5395 : :
5396 : 227 : return tristate::TS_UNKNOWN;
5397 : : }
5398 : :
5399 : : /* Return true if SVAL is definitely positive. */
5400 : :
5401 : : static bool
5402 : 15440 : is_positive_svalue (const svalue *sval)
5403 : : {
5404 : 15440 : if (tree cst = sval->maybe_get_constant ())
5405 : 14898 : return !zerop (cst) && get_range_pos_neg (cst) == 1;
5406 : 542 : tree type = sval->get_type ();
5407 : 542 : if (!type)
5408 : : return false;
5409 : : /* Consider a binary operation size_t + int. The analyzer wraps the int in
5410 : : an unaryop_svalue, converting it to a size_t, but in the dynamic execution
5411 : : the result is smaller than the first operand. Thus, we have to look if
5412 : : the argument of the unaryop_svalue is also positive. */
5413 : 400 : if (const unaryop_svalue *un_op = dyn_cast <const unaryop_svalue *> (sval))
5414 : 15 : return CONVERT_EXPR_CODE_P (un_op->get_op ()) && TYPE_UNSIGNED (type)
5415 : 23 : && is_positive_svalue (un_op->get_arg ());
5416 : 385 : return TYPE_UNSIGNED (type);
5417 : : }
5418 : :
5419 : : /* Return true if A is definitely larger than B.
5420 : :
5421 : : Limitation: does not account for integer overflows and does not try to
5422 : : return false, so it can not be used negated. */
5423 : :
5424 : : tristate
5425 : 16411 : region_model::symbolic_greater_than (const binop_svalue *bin_a,
5426 : : const svalue *b) const
5427 : : {
5428 : 16411 : if (bin_a->get_op () == PLUS_EXPR || bin_a->get_op () == MULT_EXPR)
5429 : : {
5430 : : /* Eliminate the right-hand side of both svalues. */
5431 : 15472 : if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b))
5432 : 2750 : if (bin_a->get_op () == bin_b->get_op ()
5433 : 1528 : && eval_condition (bin_a->get_arg1 (),
5434 : : GT_EXPR,
5435 : 1528 : bin_b->get_arg1 ()).is_true ()
5436 : 4278 : && eval_condition (bin_a->get_arg0 (),
5437 : : GE_EXPR,
5438 : 65 : bin_b->get_arg0 ()).is_true ())
5439 : 40 : return tristate (tristate::TS_TRUE);
5440 : :
5441 : : /* Otherwise, try to remove a positive offset or factor from BIN_A. */
5442 : 15432 : if (is_positive_svalue (bin_a->get_arg1 ())
5443 : 15432 : && eval_condition (bin_a->get_arg0 (),
5444 : 14498 : GE_EXPR, b).is_true ())
5445 : 150 : return tristate (tristate::TS_TRUE);
5446 : : }
5447 : 16221 : return tristate::unknown ();
5448 : : }
5449 : :
5450 : : /* Return true if A and B are equal structurally.
5451 : :
5452 : : Structural equality means that A and B are equal if the svalues A and B have
5453 : : the same nodes at the same positions in the tree and the leafs are equal.
5454 : : Equality for conjured_svalues and initial_svalues is determined by comparing
5455 : : the pointers while constants are compared by value. That behavior is useful
5456 : : to check for binaryop_svlaues that evaluate to the same concrete value but
5457 : : might use one operand with a different type but the same constant value.
5458 : :
5459 : : For example,
5460 : : binop_svalue (mult_expr,
5461 : : initial_svalue (‘size_t’, decl_region (..., 'some_var')),
5462 : : constant_svalue (‘size_t’, 4))
5463 : : and
5464 : : binop_svalue (mult_expr,
5465 : : initial_svalue (‘size_t’, decl_region (..., 'some_var'),
5466 : : constant_svalue (‘sizetype’, 4))
5467 : : are structurally equal. A concrete C code example, where this occurs, can
5468 : : be found in test7 of out-of-bounds-5.c. */
5469 : :
5470 : : tristate
5471 : 14785 : region_model::structural_equality (const svalue *a, const svalue *b) const
5472 : : {
5473 : : /* If A and B are referentially equal, they are also structurally equal. */
5474 : 14785 : if (a == b)
5475 : 388 : return tristate (tristate::TS_TRUE);
5476 : :
5477 : 14397 : switch (a->get_kind ())
5478 : : {
5479 : 1316 : default:
5480 : 1316 : return tristate::unknown ();
5481 : : /* SK_CONJURED and SK_INITIAL are already handled
5482 : : by the referential equality above. */
5483 : 1097 : case SK_CONSTANT:
5484 : 1097 : {
5485 : 1097 : tree a_cst = a->maybe_get_constant ();
5486 : 1097 : tree b_cst = b->maybe_get_constant ();
5487 : 1097 : if (a_cst && b_cst)
5488 : 1962 : return tristate (tree_int_cst_equal (a_cst, b_cst));
5489 : : }
5490 : 95 : return tristate (tristate::TS_FALSE);
5491 : 11 : case SK_UNARYOP:
5492 : 11 : {
5493 : 11 : const unaryop_svalue *un_a = as_a <const unaryop_svalue *> (a);
5494 : 11 : if (const unaryop_svalue *un_b = dyn_cast <const unaryop_svalue *> (b))
5495 : 8 : return tristate (pending_diagnostic::same_tree_p (un_a->get_type (),
5496 : : un_b->get_type ())
5497 : 8 : && un_a->get_op () == un_b->get_op ()
5498 : : && structural_equality (un_a->get_arg (),
5499 : 16 : un_b->get_arg ()));
5500 : : }
5501 : 3 : return tristate (tristate::TS_FALSE);
5502 : 11973 : case SK_BINOP:
5503 : 11973 : {
5504 : 11973 : const binop_svalue *bin_a = as_a <const binop_svalue *> (a);
5505 : 11973 : if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b))
5506 : 2481 : return tristate (bin_a->get_op () == bin_b->get_op ()
5507 : : && structural_equality (bin_a->get_arg0 (),
5508 : 2924 : bin_b->get_arg0 ())
5509 : : && structural_equality (bin_a->get_arg1 (),
5510 : 2924 : bin_b->get_arg1 ()));
5511 : : }
5512 : 10511 : return tristate (tristate::TS_FALSE);
5513 : : }
5514 : : }
5515 : :
5516 : : /* Handle various constraints of the form:
5517 : : LHS: ((bool)INNER_LHS INNER_OP INNER_RHS))
5518 : : OP : == or !=
5519 : : RHS: zero
5520 : : and (with a cast):
5521 : : LHS: CAST([long]int, ((bool)INNER_LHS INNER_OP INNER_RHS))
5522 : : OP : == or !=
5523 : : RHS: zero
5524 : : by adding constraints for INNER_LHS INNEROP INNER_RHS.
5525 : :
5526 : : Return true if this function can fully handle the constraint; if
5527 : : so, add the implied constraint(s) and write true to *OUT if they
5528 : : are consistent with existing constraints, or write false to *OUT
5529 : : if they contradicts existing constraints.
5530 : :
5531 : : Return false for cases that this function doeesn't know how to handle.
5532 : :
5533 : : For example, if we're checking a stored conditional, we'll have
5534 : : something like:
5535 : : LHS: CAST(long int, (&HEAP_ALLOCATED_REGION(8)!=(int *)0B))
5536 : : OP : NE_EXPR
5537 : : RHS: zero
5538 : : which this function can turn into an add_constraint of:
5539 : : (&HEAP_ALLOCATED_REGION(8) != (int *)0B)
5540 : :
5541 : : Similarly, optimized && and || conditionals lead to e.g.
5542 : : if (p && q)
5543 : : becoming gimple like this:
5544 : : _1 = p_6 == 0B;
5545 : : _2 = q_8 == 0B
5546 : : _3 = _1 | _2
5547 : : On the "_3 is false" branch we can have constraints of the form:
5548 : : ((&HEAP_ALLOCATED_REGION(8)!=(int *)0B)
5549 : : | (&HEAP_ALLOCATED_REGION(10)!=(int *)0B))
5550 : : == 0
5551 : : which implies that both _1 and _2 are false,
5552 : : which this function can turn into a pair of add_constraints of
5553 : : (&HEAP_ALLOCATED_REGION(8)!=(int *)0B)
5554 : : and:
5555 : : (&HEAP_ALLOCATED_REGION(10)!=(int *)0B). */
5556 : :
5557 : : bool
5558 : 52669 : region_model::add_constraints_from_binop (const svalue *outer_lhs,
5559 : : enum tree_code outer_op,
5560 : : const svalue *outer_rhs,
5561 : : bool *out,
5562 : : region_model_context *ctxt)
5563 : : {
5564 : 54550 : while (const svalue *cast = outer_lhs->maybe_undo_cast ())
5565 : : outer_lhs = cast;
5566 : 52669 : const binop_svalue *binop_sval = outer_lhs->dyn_cast_binop_svalue ();
5567 : 52669 : if (!binop_sval)
5568 : : return false;
5569 : 7534 : if (!outer_rhs->all_zeroes_p ())
5570 : : return false;
5571 : :
5572 : 5167 : const svalue *inner_lhs = binop_sval->get_arg0 ();
5573 : 5167 : enum tree_code inner_op = binop_sval->get_op ();
5574 : 5167 : const svalue *inner_rhs = binop_sval->get_arg1 ();
5575 : :
5576 : 5167 : if (outer_op != NE_EXPR && outer_op != EQ_EXPR)
5577 : : return false;
5578 : :
5579 : : /* We have either
5580 : : - "OUTER_LHS != false" (i.e. OUTER is true), or
5581 : : - "OUTER_LHS == false" (i.e. OUTER is false). */
5582 : 4609 : bool is_true = outer_op == NE_EXPR;
5583 : :
5584 : 4609 : switch (inner_op)
5585 : : {
5586 : : default:
5587 : : return false;
5588 : :
5589 : 2536 : case EQ_EXPR:
5590 : 2536 : case NE_EXPR:
5591 : 2536 : case GE_EXPR:
5592 : 2536 : case GT_EXPR:
5593 : 2536 : case LE_EXPR:
5594 : 2536 : case LT_EXPR:
5595 : 2536 : {
5596 : : /* ...and "(inner_lhs OP inner_rhs) == 0"
5597 : : then (inner_lhs OP inner_rhs) must have the same
5598 : : logical value as LHS. */
5599 : 2536 : if (!is_true)
5600 : 1225 : inner_op = invert_tree_comparison (inner_op, false /* honor_nans */);
5601 : 2536 : *out = add_constraint (inner_lhs, inner_op, inner_rhs, ctxt);
5602 : 2536 : return true;
5603 : : }
5604 : 849 : break;
5605 : :
5606 : 849 : case BIT_AND_EXPR:
5607 : 849 : if (is_true)
5608 : : {
5609 : : /* ...and "(inner_lhs & inner_rhs) != 0"
5610 : : then both inner_lhs and inner_rhs must be true. */
5611 : 435 : const svalue *false_sval
5612 : 435 : = m_mgr->get_or_create_constant_svalue (boolean_false_node);
5613 : 435 : bool sat1 = add_constraint (inner_lhs, NE_EXPR, false_sval, ctxt);
5614 : 435 : bool sat2 = add_constraint (inner_rhs, NE_EXPR, false_sval, ctxt);
5615 : 435 : *out = sat1 && sat2;
5616 : 435 : return true;
5617 : : }
5618 : : return false;
5619 : :
5620 : 495 : case BIT_IOR_EXPR:
5621 : 495 : if (!is_true)
5622 : : {
5623 : : /* ...and "(inner_lhs | inner_rhs) == 0"
5624 : : i.e. "(inner_lhs | inner_rhs)" is false
5625 : : then both inner_lhs and inner_rhs must be false. */
5626 : 278 : const svalue *false_sval
5627 : 278 : = m_mgr->get_or_create_constant_svalue (boolean_false_node);
5628 : 278 : bool sat1 = add_constraint (inner_lhs, EQ_EXPR, false_sval, ctxt);
5629 : 278 : bool sat2 = add_constraint (inner_rhs, EQ_EXPR, false_sval, ctxt);
5630 : 278 : *out = sat1 && sat2;
5631 : 278 : return true;
5632 : : }
5633 : : return false;
5634 : : }
5635 : : }
5636 : :
5637 : : /* Attempt to add the constraint "LHS OP RHS" to this region_model.
5638 : : If it is consistent with existing constraints, add it, and return true.
5639 : : Return false if it contradicts existing constraints.
5640 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5641 : :
5642 : : bool
5643 : 69632 : region_model::add_constraint (tree lhs, enum tree_code op, tree rhs,
5644 : : region_model_context *ctxt)
5645 : : {
5646 : : /* For now, make no attempt to capture constraints on floating-point
5647 : : values. */
5648 : 69632 : if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
5649 : : return true;
5650 : :
5651 : 69262 : const svalue *lhs_sval = get_rvalue (lhs, ctxt);
5652 : 69262 : const svalue *rhs_sval = get_rvalue (rhs, ctxt);
5653 : :
5654 : 69262 : return add_constraint (lhs_sval, op, rhs_sval, ctxt);
5655 : : }
5656 : :
5657 : : static bool
5658 : 11097 : unusable_in_infinite_loop_constraint_p (const svalue *sval)
5659 : : {
5660 : 11097 : if (sval->get_kind () == SK_WIDENING)
5661 : 0 : return true;
5662 : : return false;
5663 : : }
5664 : :
5665 : : /* Attempt to add the constraint "LHS OP RHS" to this region_model.
5666 : : If it is consistent with existing constraints, add it, and return true.
5667 : : Return false if it contradicts existing constraints.
5668 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5669 : :
5670 : : bool
5671 : 77507 : region_model::add_constraint (const svalue *lhs,
5672 : : enum tree_code op,
5673 : : const svalue *rhs,
5674 : : region_model_context *ctxt)
5675 : : {
5676 : 77507 : const bool checking_for_infinite_loop
5677 : 77507 : = ctxt ? ctxt->checking_for_infinite_loop_p () : false;
5678 : :
5679 : 5665 : if (checking_for_infinite_loop)
5680 : : {
5681 : 11097 : if (unusable_in_infinite_loop_constraint_p (lhs)
5682 : 77507 : || unusable_in_infinite_loop_constraint_p (rhs))
5683 : : {
5684 : 236 : gcc_assert (ctxt);
5685 : 236 : ctxt->on_unusable_in_infinite_loop ();
5686 : 236 : return false;
5687 : : }
5688 : : }
5689 : :
5690 : 77271 : tristate t_cond = eval_condition (lhs, op, rhs);
5691 : :
5692 : : /* If we already have the condition, do nothing. */
5693 : 77271 : if (t_cond.is_true ())
5694 : : return true;
5695 : :
5696 : : /* Reject a constraint that would contradict existing knowledge, as
5697 : : unsatisfiable. */
5698 : 65223 : if (t_cond.is_false ())
5699 : : return false;
5700 : :
5701 : 56151 : if (checking_for_infinite_loop)
5702 : : {
5703 : : /* Here, we don't have a definite true/false value, so bail out
5704 : : when checking for infinite loops. */
5705 : 3482 : gcc_assert (ctxt);
5706 : 3482 : ctxt->on_unusable_in_infinite_loop ();
5707 : 3482 : return false;
5708 : : }
5709 : :
5710 : 52669 : bool out;
5711 : 52669 : if (add_constraints_from_binop (lhs, op, rhs, &out, ctxt))
5712 : 3249 : return out;
5713 : :
5714 : : /* Attempt to store the constraint. */
5715 : 49420 : if (!m_constraints->add_constraint (lhs, op, rhs))
5716 : : return false;
5717 : :
5718 : : /* Notify the context, if any. This exists so that the state machines
5719 : : in a program_state can be notified about the condition, and so can
5720 : : set sm-state for e.g. unchecked->checked, both for cfg-edges, and
5721 : : when synthesizing constraints as above. */
5722 : 49316 : if (ctxt)
5723 : 33767 : ctxt->on_condition (lhs, op, rhs);
5724 : :
5725 : : /* If we have ®ION == NULL, then drop dynamic extents for REGION (for
5726 : : the case where REGION is heap-allocated and thus could be NULL). */
5727 : 49316 : if (tree rhs_cst = rhs->maybe_get_constant ())
5728 : 40697 : if (op == EQ_EXPR && zerop (rhs_cst))
5729 : 12869 : if (const region_svalue *region_sval = lhs->dyn_cast_region_svalue ())
5730 : 1741 : unset_dynamic_extents (region_sval->get_pointee ());
5731 : :
5732 : : return true;
5733 : : }
5734 : :
5735 : : /* As above, but when returning false, if OUT is non-NULL, write a
5736 : : new rejected_constraint to *OUT. */
5737 : :
5738 : : bool
5739 : 68490 : region_model::add_constraint (tree lhs, enum tree_code op, tree rhs,
5740 : : region_model_context *ctxt,
5741 : : std::unique_ptr<rejected_constraint> *out)
5742 : : {
5743 : 68490 : bool sat = add_constraint (lhs, op, rhs, ctxt);
5744 : 68490 : if (!sat && out)
5745 : 2096 : *out = std::make_unique <rejected_op_constraint> (*this, lhs, op, rhs);
5746 : 68490 : return sat;
5747 : : }
5748 : :
5749 : : /* Determine what is known about the condition "LHS OP RHS" within
5750 : : this model.
5751 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5752 : :
5753 : : tristate
5754 : 31291 : region_model::eval_condition (tree lhs,
5755 : : enum tree_code op,
5756 : : tree rhs,
5757 : : region_model_context *ctxt) const
5758 : : {
5759 : : /* For now, make no attempt to model constraints on floating-point
5760 : : values. */
5761 : 31291 : if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
5762 : 16 : return tristate::unknown ();
5763 : :
5764 : 31275 : return eval_condition (get_rvalue (lhs, ctxt), op, get_rvalue (rhs, ctxt));
5765 : : }
5766 : :
5767 : : /* Implementation of region_model::get_representative_path_var.
5768 : : Attempt to return a path_var that represents SVAL, or return NULL_TREE.
5769 : : Use VISITED to prevent infinite mutual recursion with the overload for
5770 : : regions. */
5771 : :
5772 : : path_var
5773 : 12878 : region_model::get_representative_path_var_1 (const svalue *sval,
5774 : : svalue_set *visited,
5775 : : logger *logger) const
5776 : : {
5777 : 12878 : gcc_assert (sval);
5778 : :
5779 : : /* Prevent infinite recursion. */
5780 : 12878 : if (visited->contains (sval))
5781 : : {
5782 : 14 : if (sval->get_kind () == SK_CONSTANT)
5783 : 14 : return path_var (sval->maybe_get_constant (), 0);
5784 : : else
5785 : 0 : return path_var (NULL_TREE, 0);
5786 : : }
5787 : 12864 : visited->add (sval);
5788 : :
5789 : : /* Handle casts by recursion into get_representative_path_var. */
5790 : 12864 : if (const svalue *cast_sval = sval->maybe_undo_cast ())
5791 : : {
5792 : 402 : path_var result = get_representative_path_var (cast_sval, visited,
5793 : : logger);
5794 : 402 : tree orig_type = sval->get_type ();
5795 : : /* If necessary, wrap the result in a cast. */
5796 : 402 : if (result.m_tree && orig_type)
5797 : 332 : result.m_tree = build1 (NOP_EXPR, orig_type, result.m_tree);
5798 : 402 : return result;
5799 : : }
5800 : :
5801 : 12462 : auto_vec<path_var> pvs;
5802 : 12462 : m_store.get_representative_path_vars (this, visited, sval, logger, &pvs);
5803 : :
5804 : 12462 : if (tree cst = sval->maybe_get_constant ())
5805 : 1794 : pvs.safe_push (path_var (cst, 0));
5806 : :
5807 : : /* Handle string literals and various other pointers. */
5808 : 12462 : if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
5809 : : {
5810 : 4370 : const region *reg = ptr_sval->get_pointee ();
5811 : 4370 : if (path_var pv = get_representative_path_var (reg, visited, logger))
5812 : 30 : return path_var (build1 (ADDR_EXPR,
5813 : : sval->get_type (),
5814 : : pv.m_tree),
5815 : 30 : pv.m_stack_depth);
5816 : : }
5817 : :
5818 : : /* If we have a sub_svalue, look for ways to represent the parent. */
5819 : 12432 : if (const sub_svalue *sub_sval = sval->dyn_cast_sub_svalue ())
5820 : : {
5821 : 370 : const svalue *parent_sval = sub_sval->get_parent ();
5822 : 370 : const region *subreg = sub_sval->get_subregion ();
5823 : 740 : if (path_var parent_pv
5824 : 370 : = get_representative_path_var (parent_sval, visited, logger))
5825 : 153 : if (const field_region *field_reg = subreg->dyn_cast_field_region ())
5826 : 111 : return path_var (build3 (COMPONENT_REF,
5827 : : sval->get_type (),
5828 : : parent_pv.m_tree,
5829 : : field_reg->get_field (),
5830 : : NULL_TREE),
5831 : 111 : parent_pv.m_stack_depth);
5832 : : }
5833 : :
5834 : : /* Handle binops. */
5835 : 12321 : if (const binop_svalue *binop_sval = sval->dyn_cast_binop_svalue ())
5836 : 534 : if (path_var lhs_pv
5837 : 534 : = get_representative_path_var (binop_sval->get_arg0 (), visited,
5838 : 534 : logger))
5839 : 468 : if (path_var rhs_pv
5840 : 468 : = get_representative_path_var (binop_sval->get_arg1 (), visited,
5841 : 468 : logger))
5842 : 439 : return path_var (build2 (binop_sval->get_op (),
5843 : : sval->get_type (),
5844 : : lhs_pv.m_tree, rhs_pv.m_tree),
5845 : 439 : lhs_pv.m_stack_depth);
5846 : :
5847 : 11882 : if (pvs.length () < 1)
5848 : 2065 : return path_var (NULL_TREE, 0);
5849 : :
5850 : 9817 : pvs.qsort (readability_comparator);
5851 : 9817 : return pvs[0];
5852 : 12462 : }
5853 : :
5854 : : /* Attempt to return a path_var that represents SVAL, or return NULL_TREE.
5855 : : Use VISITED to prevent infinite mutual recursion with the overload for
5856 : : regions
5857 : :
5858 : : This function defers to get_representative_path_var_1 to do the work;
5859 : : it adds verification that get_representative_path_var_1 returned a tree
5860 : : of the correct type. */
5861 : :
5862 : : path_var
5863 : 18182 : region_model::get_representative_path_var (const svalue *sval,
5864 : : svalue_set *visited,
5865 : : logger *logger) const
5866 : : {
5867 : 18182 : if (sval == nullptr)
5868 : 5304 : return path_var (NULL_TREE, 0);
5869 : :
5870 : 12878 : LOG_SCOPE (logger);
5871 : 12878 : if (logger)
5872 : : {
5873 : 0 : logger->start_log_line ();
5874 : 0 : logger->log_partial ("sval: ");
5875 : 0 : sval->dump_to_pp (logger->get_printer (), true);
5876 : 0 : logger->end_log_line ();
5877 : : }
5878 : :
5879 : 12878 : tree orig_type = sval->get_type ();
5880 : :
5881 : 12878 : path_var result = get_representative_path_var_1 (sval, visited, logger);
5882 : :
5883 : : /* Verify that the result has the same type as SVAL, if any. */
5884 : 12878 : if (result.m_tree && orig_type)
5885 : 10655 : gcc_assert (TREE_TYPE (result.m_tree) == orig_type);
5886 : :
5887 : 12878 : if (logger)
5888 : : {
5889 : 0 : logger->start_log_line ();
5890 : 0 : logger->log_partial ("sval: ");
5891 : 0 : sval->dump_to_pp (logger->get_printer (), true);
5892 : 0 : logger->end_log_line ();
5893 : :
5894 : 0 : if (result.m_tree)
5895 : 0 : logger->log ("tree: %qE", result.m_tree);
5896 : : else
5897 : 0 : logger->log ("tree: NULL");
5898 : : }
5899 : :
5900 : 12878 : return result;
5901 : 12878 : }
5902 : :
5903 : : /* Attempt to return a tree that represents SVAL, or return NULL_TREE.
5904 : :
5905 : : Strip off any top-level cast, to avoid messages like
5906 : : double-free of '(void *)ptr'
5907 : : from analyzer diagnostics. */
5908 : :
5909 : : tree
5910 : 14378 : region_model::get_representative_tree (const svalue *sval, logger *logger) const
5911 : : {
5912 : 14378 : svalue_set visited;
5913 : 14378 : tree expr = get_representative_path_var (sval, &visited, logger).m_tree;
5914 : :
5915 : : /* Strip off any top-level cast. */
5916 : 14378 : if (expr && TREE_CODE (expr) == NOP_EXPR)
5917 : 457 : expr = TREE_OPERAND (expr, 0);
5918 : :
5919 : 14378 : return fixup_tree_for_diagnostic (expr);
5920 : 14378 : }
5921 : :
5922 : : tree
5923 : 770 : region_model::get_representative_tree (const region *reg, logger *logger) const
5924 : : {
5925 : 770 : svalue_set visited;
5926 : 770 : tree expr = get_representative_path_var (reg, &visited, logger).m_tree;
5927 : :
5928 : : /* Strip off any top-level cast. */
5929 : 770 : if (expr && TREE_CODE (expr) == NOP_EXPR)
5930 : 0 : expr = TREE_OPERAND (expr, 0);
5931 : :
5932 : 770 : return fixup_tree_for_diagnostic (expr);
5933 : 770 : }
5934 : :
5935 : : /* Implementation of region_model::get_representative_path_var.
5936 : :
5937 : : Attempt to return a path_var that represents REG, or return
5938 : : the NULL path_var.
5939 : : For example, a region for a field of a local would be a path_var
5940 : : wrapping a COMPONENT_REF.
5941 : : Use VISITED to prevent infinite mutual recursion with the overload for
5942 : : svalues. */
5943 : :
5944 : : path_var
5945 : 15488 : region_model::get_representative_path_var_1 (const region *reg,
5946 : : svalue_set *visited,
5947 : : logger *logger) const
5948 : : {
5949 : 15488 : switch (reg->get_kind ())
5950 : : {
5951 : 0 : default:
5952 : 0 : gcc_unreachable ();
5953 : :
5954 : 0 : case RK_FRAME:
5955 : 0 : case RK_GLOBALS:
5956 : 0 : case RK_CODE:
5957 : 0 : case RK_HEAP:
5958 : 0 : case RK_STACK:
5959 : 0 : case RK_THREAD_LOCAL:
5960 : 0 : case RK_ROOT:
5961 : : /* Regions that represent memory spaces are not expressible as trees. */
5962 : 0 : return path_var (NULL_TREE, 0);
5963 : :
5964 : 1 : case RK_FUNCTION:
5965 : 1 : {
5966 : 1 : const function_region *function_reg
5967 : 1 : = as_a <const function_region *> (reg);
5968 : 1 : return path_var (function_reg->get_fndecl (), 0);
5969 : : }
5970 : 1 : case RK_LABEL:
5971 : 1 : {
5972 : 1 : const label_region *label_reg = as_a <const label_region *> (reg);
5973 : 1 : return path_var (label_reg->get_label (), 0);
5974 : : }
5975 : :
5976 : 225 : case RK_SYMBOLIC:
5977 : 225 : {
5978 : 225 : const symbolic_region *symbolic_reg
5979 : 225 : = as_a <const symbolic_region *> (reg);
5980 : 225 : const svalue *pointer = symbolic_reg->get_pointer ();
5981 : 225 : path_var pointer_pv = get_representative_path_var (pointer, visited,
5982 : : logger);
5983 : 225 : if (!pointer_pv)
5984 : 16 : return path_var (NULL_TREE, 0);
5985 : 209 : tree offset = build_int_cst (pointer->get_type (), 0);
5986 : 209 : return path_var (build2 (MEM_REF,
5987 : : reg->get_type (),
5988 : : pointer_pv.m_tree,
5989 : : offset),
5990 : 209 : pointer_pv.m_stack_depth);
5991 : : }
5992 : 9717 : case RK_DECL:
5993 : 9717 : {
5994 : 9717 : const decl_region *decl_reg = as_a <const decl_region *> (reg);
5995 : 9717 : return path_var (decl_reg->get_decl (), decl_reg->get_stack_depth ());
5996 : : }
5997 : 777 : case RK_FIELD:
5998 : 777 : {
5999 : 777 : const field_region *field_reg = as_a <const field_region *> (reg);
6000 : 777 : path_var parent_pv
6001 : 777 : = get_representative_path_var (reg->get_parent_region (), visited,
6002 : : logger);
6003 : 777 : if (!parent_pv)
6004 : 37 : return path_var (NULL_TREE, 0);
6005 : 740 : return path_var (build3 (COMPONENT_REF,
6006 : : reg->get_type (),
6007 : : parent_pv.m_tree,
6008 : : field_reg->get_field (),
6009 : : NULL_TREE),
6010 : 740 : parent_pv.m_stack_depth);
6011 : : }
6012 : :
6013 : 150 : case RK_ELEMENT:
6014 : 150 : {
6015 : 150 : const element_region *element_reg
6016 : 150 : = as_a <const element_region *> (reg);
6017 : 150 : path_var parent_pv
6018 : 150 : = get_representative_path_var (reg->get_parent_region (), visited,
6019 : : logger);
6020 : 150 : if (!parent_pv)
6021 : 0 : return path_var (NULL_TREE, 0);
6022 : 150 : path_var index_pv
6023 : 150 : = get_representative_path_var (element_reg->get_index (), visited,
6024 : : logger);
6025 : 150 : if (!index_pv)
6026 : 0 : return path_var (NULL_TREE, 0);
6027 : 150 : return path_var (build4 (ARRAY_REF,
6028 : : reg->get_type (),
6029 : : parent_pv.m_tree, index_pv.m_tree,
6030 : : NULL_TREE, NULL_TREE),
6031 : 150 : parent_pv.m_stack_depth);
6032 : : }
6033 : :
6034 : 42 : case RK_OFFSET:
6035 : 42 : {
6036 : 42 : const offset_region *offset_reg
6037 : 42 : = as_a <const offset_region *> (reg);
6038 : 42 : path_var parent_pv
6039 : 42 : = get_representative_path_var (reg->get_parent_region (), visited,
6040 : : logger);
6041 : 42 : if (!parent_pv)
6042 : 0 : return path_var (NULL_TREE, 0);
6043 : 42 : path_var offset_pv
6044 : 42 : = get_representative_path_var (offset_reg->get_byte_offset (),
6045 : : visited, logger);
6046 : 42 : if (!offset_pv || TREE_CODE (offset_pv.m_tree) != INTEGER_CST)
6047 : 42 : return path_var (NULL_TREE, 0);
6048 : 0 : tree addr_parent = build1 (ADDR_EXPR,
6049 : : build_pointer_type (reg->get_type ()),
6050 : : parent_pv.m_tree);
6051 : 0 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode,
6052 : : true);
6053 : 0 : return path_var (build2 (MEM_REF, reg->get_type (), addr_parent,
6054 : : fold_convert (ptype, offset_pv.m_tree)),
6055 : 0 : parent_pv.m_stack_depth);
6056 : : }
6057 : :
6058 : 42 : case RK_SIZED:
6059 : 42 : return path_var (NULL_TREE, 0);
6060 : :
6061 : 5 : case RK_CAST:
6062 : 5 : {
6063 : 5 : path_var parent_pv
6064 : 5 : = get_representative_path_var (reg->get_parent_region (), visited,
6065 : : logger);
6066 : 5 : if (!parent_pv)
6067 : 0 : return path_var (NULL_TREE, 0);
6068 : 5 : return path_var (build1 (NOP_EXPR,
6069 : : reg->get_type (),
6070 : : parent_pv.m_tree),
6071 : 5 : parent_pv.m_stack_depth);
6072 : : }
6073 : :
6074 : 4514 : case RK_HEAP_ALLOCATED:
6075 : 4514 : case RK_ALLOCA:
6076 : : /* No good way to express heap-allocated/alloca regions as trees. */
6077 : 4514 : return path_var (NULL_TREE, 0);
6078 : :
6079 : 10 : case RK_STRING:
6080 : 10 : {
6081 : 10 : const string_region *string_reg = as_a <const string_region *> (reg);
6082 : 10 : return path_var (string_reg->get_string_cst (), 0);
6083 : : }
6084 : :
6085 : 4 : case RK_VAR_ARG:
6086 : 4 : case RK_ERRNO:
6087 : 4 : case RK_UNKNOWN:
6088 : 4 : case RK_PRIVATE:
6089 : 4 : return path_var (NULL_TREE, 0);
6090 : : }
6091 : : }
6092 : :
6093 : : /* Attempt to return a path_var that represents REG, or return
6094 : : the NULL path_var.
6095 : : For example, a region for a field of a local would be a path_var
6096 : : wrapping a COMPONENT_REF.
6097 : : Use VISITED to prevent infinite mutual recursion with the overload for
6098 : : svalues.
6099 : :
6100 : : This function defers to get_representative_path_var_1 to do the work;
6101 : : it adds verification that get_representative_path_var_1 returned a tree
6102 : : of the correct type. */
6103 : :
6104 : : path_var
6105 : 15488 : region_model::get_representative_path_var (const region *reg,
6106 : : svalue_set *visited,
6107 : : logger *logger) const
6108 : : {
6109 : 15488 : LOG_SCOPE (logger);
6110 : 15488 : if (logger)
6111 : : {
6112 : 0 : logger->start_log_line ();
6113 : 0 : logger->log_partial ("reg: ");
6114 : 0 : reg->dump_to_pp (logger->get_printer (), true);
6115 : 0 : logger->end_log_line ();
6116 : : }
6117 : :
6118 : 15488 : path_var result = get_representative_path_var_1 (reg, visited, logger);
6119 : :
6120 : : /* Verify that the result has the same type as REG, if any. */
6121 : 15488 : if (result.m_tree && reg->get_type ())
6122 : 10832 : gcc_assert (TREE_TYPE (result.m_tree) == reg->get_type ());
6123 : :
6124 : 15488 : if (logger)
6125 : : {
6126 : 0 : logger->start_log_line ();
6127 : 0 : logger->log_partial ("reg: ");
6128 : 0 : reg->dump_to_pp (logger->get_printer (), true);
6129 : 0 : logger->end_log_line ();
6130 : :
6131 : 0 : if (result.m_tree)
6132 : 0 : logger->log ("tree: %qE", result.m_tree);
6133 : : else
6134 : 0 : logger->log ("tree: NULL");
6135 : : }
6136 : :
6137 : 30976 : return result;
6138 : 15488 : }
6139 : :
6140 : : /* Push a new frame_region on to the stack region.
6141 : : Populate the frame_region with child regions for the function call's
6142 : : parameters, using values from the arguments at the callsite in the
6143 : : caller's frame. */
6144 : :
6145 : : void
6146 : 11418 : region_model::update_for_gcall (const gcall &call_stmt,
6147 : : region_model_context *ctxt,
6148 : : function *callee)
6149 : : {
6150 : : /* Build a vec of argument svalues, using the current top
6151 : : frame for resolving tree expressions. */
6152 : 11418 : auto_vec<const svalue *> arg_svals (gimple_call_num_args (&call_stmt));
6153 : :
6154 : 24085 : for (unsigned i = 0; i < gimple_call_num_args (&call_stmt); i++)
6155 : : {
6156 : 12667 : tree arg = gimple_call_arg (&call_stmt, i);
6157 : 12667 : arg_svals.quick_push (get_rvalue (arg, ctxt));
6158 : : }
6159 : :
6160 : 11418 : if(!callee)
6161 : : {
6162 : : /* Get the function * from the gcall. */
6163 : 0 : tree fn_decl = get_fndecl_for_call (call_stmt, ctxt);
6164 : 0 : callee = DECL_STRUCT_FUNCTION (fn_decl);
6165 : : }
6166 : :
6167 : 0 : gcc_assert (callee);
6168 : 11418 : push_frame (*callee, &call_stmt, &arg_svals, ctxt);
6169 : 11418 : }
6170 : :
6171 : : /* Pop the top-most frame_region from the stack, and copy the return
6172 : : region's values (if any) into the region for the lvalue of the LHS of
6173 : : the call (if any). */
6174 : :
6175 : : void
6176 : 8451 : region_model::update_for_return_gcall (const gcall &call_stmt,
6177 : : region_model_context *ctxt)
6178 : : {
6179 : : /* Get the lvalue for the result of the call, passing it to pop_frame,
6180 : : so that pop_frame can determine the region with respect to the
6181 : : *caller* frame. */
6182 : 8451 : tree lhs = gimple_call_lhs (&call_stmt);
6183 : 8451 : pop_frame (lhs, nullptr, ctxt, &call_stmt);
6184 : 8451 : }
6185 : :
6186 : : /* Attempt to use R to replay SUMMARY into this object.
6187 : : Return true if it is possible. */
6188 : :
6189 : : bool
6190 : 1629 : region_model::replay_call_summary (call_summary_replay &r,
6191 : : const region_model &summary)
6192 : : {
6193 : 1629 : gcc_assert (summary.get_stack_depth () == 1);
6194 : :
6195 : 1629 : m_store.replay_call_summary (r, summary.m_store);
6196 : :
6197 : 1629 : if (r.get_ctxt ())
6198 : 1545 : r.get_ctxt ()->maybe_did_work ();
6199 : :
6200 : 1629 : if (!m_constraints->replay_call_summary (r, *summary.m_constraints))
6201 : : return false;
6202 : :
6203 : 4578 : for (auto kv : summary.m_dynamic_extents)
6204 : : {
6205 : 1537 : const region *summary_reg = kv.first;
6206 : 1537 : const region *caller_reg = r.convert_region_from_summary (summary_reg);
6207 : 1537 : if (!caller_reg)
6208 : 2 : continue;
6209 : 1535 : const svalue *summary_sval = kv.second;
6210 : 1535 : const svalue *caller_sval = r.convert_svalue_from_summary (summary_sval);
6211 : 1535 : if (!caller_sval)
6212 : 0 : continue;
6213 : 1535 : m_dynamic_extents.put (caller_reg, caller_sval);
6214 : : }
6215 : :
6216 : 1504 : return true;
6217 : : }
6218 : :
6219 : : /* For use with push_frame when handling a top-level call within the analysis.
6220 : : PARAM has a defined but unknown initial value.
6221 : : Anything it points to has escaped, since the calling context "knows"
6222 : : the pointer, and thus calls to unknown functions could read/write into
6223 : : the region.
6224 : : If NONNULL is true, then assume that PARAM must be non-NULL. */
6225 : :
6226 : : void
6227 : 18243 : region_model::on_top_level_param (tree param,
6228 : : bool nonnull,
6229 : : region_model_context *ctxt)
6230 : : {
6231 : 18243 : if (POINTER_TYPE_P (TREE_TYPE (param)))
6232 : : {
6233 : 8705 : const region *param_reg = get_lvalue (param, ctxt);
6234 : 8705 : const svalue *init_ptr_sval
6235 : 8705 : = m_mgr->get_or_create_initial_value (param_reg);
6236 : 8705 : const region *pointee_reg = m_mgr->get_symbolic_region (init_ptr_sval);
6237 : 8705 : store_manager *store_mgr = m_mgr->get_store_manager ();
6238 : 8705 : m_store.mark_as_escaped (*store_mgr, pointee_reg);
6239 : 8705 : if (nonnull)
6240 : : {
6241 : 455 : const svalue *null_ptr_sval
6242 : 455 : = m_mgr->get_or_create_null_ptr (TREE_TYPE (param));
6243 : 455 : add_constraint (init_ptr_sval, NE_EXPR, null_ptr_sval, ctxt);
6244 : : }
6245 : : }
6246 : 18243 : }
6247 : :
6248 : : /* Update this region_model to reflect pushing a frame onto the stack
6249 : : for a call to FUN.
6250 : :
6251 : : If CALL_STMT is non-NULL, this is for the interprocedural case where
6252 : : we already have an execution path into the caller. It can be NULL for
6253 : : top-level entrypoints into the analysis, or in selftests.
6254 : :
6255 : : If ARG_SVALS is non-NULL, use it to populate the parameters
6256 : : in the new frame.
6257 : : Otherwise, the params have their initial_svalues.
6258 : :
6259 : : Return the frame_region for the new frame. */
6260 : :
6261 : : const region *
6262 : 30984 : region_model::push_frame (const function &fun,
6263 : : const gcall *call_stmt,
6264 : : const vec<const svalue *> *arg_svals,
6265 : : region_model_context *ctxt)
6266 : : {
6267 : 30984 : tree fndecl = fun.decl;
6268 : 30984 : if (arg_svals)
6269 : : {
6270 : : /* If the result of the callee is DECL_BY_REFERENCE, then
6271 : : we'll need to store a reference to the caller's lhs of
6272 : : CALL_STMT within callee's result.
6273 : : If so, determine the region of CALL_STMT's lhs within
6274 : : the caller's frame before updating m_current_frame. */
6275 : 11418 : const region *caller_return_by_reference_reg = nullptr;
6276 : 11418 : if (tree result = DECL_RESULT (fndecl))
6277 : 11418 : if (DECL_BY_REFERENCE (result))
6278 : : {
6279 : 36 : gcc_assert (call_stmt);
6280 : 36 : tree lhs = gimple_call_lhs (call_stmt);
6281 : 36 : gcc_assert (lhs);
6282 : 36 : caller_return_by_reference_reg = get_lvalue (lhs, ctxt);
6283 : : }
6284 : :
6285 : : /* Update m_current_frame. */
6286 : 11418 : m_current_frame = m_mgr->get_frame_region (m_current_frame, fun);
6287 : :
6288 : : /* Arguments supplied from a caller frame. */
6289 : 11418 : unsigned idx = 0;
6290 : 23670 : for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
6291 : 12252 : iter_parm = DECL_CHAIN (iter_parm), ++idx)
6292 : : {
6293 : : /* If there's a mismatching declaration, the call stmt might
6294 : : not have enough args. Handle this case by leaving the
6295 : : rest of the params as uninitialized. */
6296 : 12255 : if (idx >= arg_svals->length ())
6297 : : break;
6298 : 12252 : tree parm_lval = iter_parm;
6299 : 12252 : if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm))
6300 : 11251 : parm_lval = parm_default_ssa;
6301 : 12252 : const region *parm_reg = get_lvalue (parm_lval, ctxt);
6302 : 12252 : const svalue *arg_sval = (*arg_svals)[idx];
6303 : 12252 : set_value (parm_reg, arg_sval, ctxt);
6304 : : }
6305 : :
6306 : : /* Handle any variadic args. */
6307 : : unsigned va_arg_idx = 0;
6308 : 11833 : for (; idx < arg_svals->length (); idx++, va_arg_idx++)
6309 : : {
6310 : 415 : const svalue *arg_sval = (*arg_svals)[idx];
6311 : 415 : const region *var_arg_reg
6312 : 415 : = m_mgr->get_var_arg_region (m_current_frame,
6313 : : va_arg_idx);
6314 : 415 : set_value (var_arg_reg, arg_sval, ctxt);
6315 : : }
6316 : :
6317 : : /* If the result of the callee is DECL_BY_REFERENCE, then above
6318 : : we should have determined the region within the
6319 : : caller's frame that the callee will be writing back to.
6320 : : Use this now to initialize the reference in callee's frame. */
6321 : 11418 : if (tree result = DECL_RESULT (fndecl))
6322 : 11418 : if (DECL_BY_REFERENCE (result))
6323 : : {
6324 : : /* Get reference to the caller lhs. */
6325 : 36 : gcc_assert (caller_return_by_reference_reg);
6326 : 36 : const svalue *ref_sval
6327 : 36 : = m_mgr->get_ptr_svalue (TREE_TYPE (result),
6328 : : caller_return_by_reference_reg);
6329 : :
6330 : : /* Get region for default val of DECL_RESULT within the
6331 : : callee. */
6332 : 36 : tree result_default_ssa = get_ssa_default_def (fun, result);
6333 : 36 : gcc_assert (result_default_ssa);
6334 : 36 : const region *callee_result_reg
6335 : 36 : = get_lvalue (result_default_ssa, ctxt);
6336 : :
6337 : : /* Set the callee's reference to refer to the caller's lhs. */
6338 : 36 : set_value (callee_result_reg, ref_sval, ctxt);
6339 : : }
6340 : : }
6341 : : else
6342 : : {
6343 : : /* Otherwise we have a top-level call within the analysis. The params
6344 : : have defined but unknown initial values.
6345 : : Anything they point to has escaped. */
6346 : :
6347 : : /* Update m_current_frame. */
6348 : 19566 : m_current_frame = m_mgr->get_frame_region (m_current_frame, fun);
6349 : :
6350 : : /* Handle "__attribute__((nonnull))". */
6351 : 19566 : tree fntype = TREE_TYPE (fndecl);
6352 : 19566 : bitmap nonnull_args = get_nonnull_args (fntype);
6353 : :
6354 : 19566 : unsigned parm_idx = 0;
6355 : 37809 : for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
6356 : 18243 : iter_parm = DECL_CHAIN (iter_parm))
6357 : : {
6358 : 18243 : bool non_null = (nonnull_args
6359 : 18243 : ? (bitmap_empty_p (nonnull_args)
6360 : 514 : || bitmap_bit_p (nonnull_args, parm_idx))
6361 : 18243 : : false);
6362 : 18243 : if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm))
6363 : 15365 : on_top_level_param (parm_default_ssa, non_null, ctxt);
6364 : : else
6365 : 2878 : on_top_level_param (iter_parm, non_null, ctxt);
6366 : 18243 : parm_idx++;
6367 : : }
6368 : :
6369 : 19566 : BITMAP_FREE (nonnull_args);
6370 : : }
6371 : :
6372 : 30984 : return m_current_frame;
6373 : : }
6374 : :
6375 : : /* Get the function of the top-most frame in this region_model's stack.
6376 : : There must be such a frame. */
6377 : :
6378 : : const function *
6379 : 183 : region_model::get_current_function () const
6380 : : {
6381 : 183 : const frame_region *frame = get_current_frame ();
6382 : 183 : gcc_assert (frame);
6383 : 183 : return &frame->get_function ();
6384 : : }
6385 : :
6386 : : /* Custom region_model_context for the assignment to the result
6387 : : at a call statement when popping a frame (PR analyzer/106203). */
6388 : :
6389 : : class caller_context : public region_model_context_decorator
6390 : : {
6391 : : public:
6392 : 4269 : caller_context (region_model_context *inner,
6393 : : const gcall *call_stmt,
6394 : : const frame_region &caller_frame)
6395 : 4269 : : region_model_context_decorator (inner),
6396 : 4269 : m_call_stmt (call_stmt),
6397 : 4269 : m_caller_frame (caller_frame)
6398 : : {}
6399 : :
6400 : : pending_location
6401 : 9 : get_pending_location_for_diag () const override
6402 : : {
6403 : 9 : pending_location ploc
6404 : 9 : = region_model_context_decorator::get_pending_location_for_diag ();
6405 : :
6406 : 9 : ploc.m_event_loc_info
6407 : 9 : = event_loc_info (m_call_stmt->location,
6408 : 9 : m_caller_frame.get_fndecl (),
6409 : 9 : m_caller_frame.get_stack_depth ());
6410 : :
6411 : 9 : return ploc;
6412 : : }
6413 : :
6414 : 8547 : const gimple *get_stmt () const override
6415 : : {
6416 : 8547 : return m_call_stmt;
6417 : : };
6418 : :
6419 : : private:
6420 : : const gcall *m_call_stmt;
6421 : : const frame_region &m_caller_frame;
6422 : : };
6423 : :
6424 : :
6425 : : /* Pop the topmost frame_region from this region_model's stack;
6426 : :
6427 : : If RESULT_LVALUE is non-null, copy any return value from the frame
6428 : : into the corresponding region (evaluated with respect to the *caller*
6429 : : frame, rather than the called frame).
6430 : : If OUT_RESULT is non-null, copy any return value from the frame
6431 : : into *OUT_RESULT.
6432 : :
6433 : : If non-null, use CALL_STMT as the location when complaining about
6434 : : assignment of the return value to RESULT_LVALUE.
6435 : :
6436 : : If EVAL_RETURN_SVALUE is false, then don't evaluate the return value.
6437 : : This is for use when unwinding frames e.g. due to longjmp, to suppress
6438 : : erroneously reporting uninitialized return values.
6439 : :
6440 : : Purge the frame region and all its descendent regions.
6441 : : Convert any pointers that point into such regions into
6442 : : poison_kind::popped_stack svalues. */
6443 : :
6444 : : void
6445 : 26143 : region_model::pop_frame (tree result_lvalue,
6446 : : const svalue **out_result,
6447 : : region_model_context *ctxt,
6448 : : const gcall *call_stmt,
6449 : : bool eval_return_svalue)
6450 : : {
6451 : 26143 : gcc_assert (m_current_frame);
6452 : :
6453 : 26143 : const region_model pre_popped_model = *this;
6454 : 26143 : const frame_region *frame_reg = m_current_frame;
6455 : :
6456 : : /* Notify state machines. */
6457 : 26143 : if (ctxt)
6458 : 23920 : ctxt->on_pop_frame (frame_reg);
6459 : :
6460 : : /* Evaluate the result, within the callee frame. */
6461 : 26143 : tree fndecl = m_current_frame->get_function ().decl;
6462 : 26143 : tree result = DECL_RESULT (fndecl);
6463 : 26143 : const svalue *retval = nullptr;
6464 : 26143 : if (result
6465 : 26135 : && TREE_TYPE (result) != void_type_node
6466 : 38130 : && eval_return_svalue)
6467 : : {
6468 : 9774 : retval = get_rvalue (result, ctxt);
6469 : 9774 : if (out_result)
6470 : 5175 : *out_result = retval;
6471 : : }
6472 : :
6473 : : /* Pop the frame. */
6474 : 26143 : m_current_frame = m_current_frame->get_calling_frame ();
6475 : :
6476 : 26143 : if (result_lvalue
6477 : 26143 : && retval
6478 : : /* Don't write back for DECL_BY_REFERENCE; the writes
6479 : : should have happened within the callee already. */
6480 : 26143 : && !DECL_BY_REFERENCE (result))
6481 : : {
6482 : 4269 : gcc_assert (eval_return_svalue);
6483 : :
6484 : : /* Compute result_dst_reg using RESULT_LVALUE *after* popping
6485 : : the frame, but before poisoning pointers into the old frame. */
6486 : 4269 : const region *result_dst_reg = get_lvalue (result_lvalue, ctxt);
6487 : :
6488 : : /* Assign retval to result_dst_reg, using caller_context
6489 : : to set the call_stmt and the popped_frame for any diagnostics
6490 : : due to the assignment. */
6491 : 4269 : gcc_assert (m_current_frame);
6492 : 4269 : caller_context caller_ctxt (ctxt, call_stmt, *m_current_frame);
6493 : 4269 : set_value (result_dst_reg, retval, call_stmt ? &caller_ctxt : ctxt);
6494 : : }
6495 : :
6496 : 26143 : unbind_region_and_descendents (frame_reg,poison_kind::popped_stack);
6497 : 26143 : notify_on_pop_frame (this, &pre_popped_model, retval, ctxt);
6498 : 26143 : }
6499 : :
6500 : : /* Get the number of frames in this region_model's stack. */
6501 : :
6502 : : int
6503 : 5109368 : region_model::get_stack_depth () const
6504 : : {
6505 : 5109368 : const frame_region *frame = get_current_frame ();
6506 : 5109368 : if (frame)
6507 : 5092653 : return frame->get_stack_depth ();
6508 : : else
6509 : : return 0;
6510 : : }
6511 : :
6512 : : /* Get the frame_region with the given index within the stack.
6513 : : The frame_region must exist. */
6514 : :
6515 : : const frame_region *
6516 : 1521933 : region_model::get_frame_at_index (int index) const
6517 : : {
6518 : 1521933 : const frame_region *frame = get_current_frame ();
6519 : 1521933 : gcc_assert (frame);
6520 : 1521933 : gcc_assert (index >= 0);
6521 : 1521933 : gcc_assert (index <= frame->get_index ());
6522 : 1809740 : while (index != frame->get_index ())
6523 : : {
6524 : 287807 : frame = frame->get_calling_frame ();
6525 : 287807 : gcc_assert (frame);
6526 : : }
6527 : 1521933 : return frame;
6528 : : }
6529 : :
6530 : : /* Unbind svalues for any regions in REG and below.
6531 : : Find any pointers to such regions; convert them to
6532 : : poisoned values of kind PKIND.
6533 : : Also purge any dynamic extents. */
6534 : :
6535 : : void
6536 : 36681 : region_model::unbind_region_and_descendents (const region *reg,
6537 : : enum poison_kind pkind)
6538 : : {
6539 : : /* Gather a set of base regions to be unbound. */
6540 : 36681 : hash_set<const region *> base_regs;
6541 : 211746 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6542 : 386811 : iter != m_store.end (); ++iter)
6543 : : {
6544 : 175065 : const region *iter_base_reg = (*iter).first;
6545 : 175065 : if (iter_base_reg->descendent_of_p (reg))
6546 : 41448 : base_regs.add (iter_base_reg);
6547 : : }
6548 : 78129 : for (hash_set<const region *>::iterator iter = base_regs.begin ();
6549 : 119577 : iter != base_regs.end (); ++iter)
6550 : 41448 : m_store.purge_cluster (*iter);
6551 : :
6552 : : /* Find any pointers to REG or its descendents; convert to poisoned. */
6553 : 36681 : poison_any_pointers_to_descendents (reg, pkind);
6554 : :
6555 : : /* Purge dynamic extents of any base regions in REG and below
6556 : : (e.g. VLAs and alloca stack regions). */
6557 : 111642 : for (auto iter : m_dynamic_extents)
6558 : : {
6559 : 19140 : const region *iter_reg = iter.first;
6560 : 19140 : if (iter_reg->descendent_of_p (reg))
6561 : 5958 : unset_dynamic_extents (iter_reg);
6562 : : }
6563 : 36681 : }
6564 : :
6565 : : /* Find any pointers to REG or its descendents; convert them to
6566 : : poisoned values of kind PKIND. */
6567 : :
6568 : : void
6569 : 36681 : region_model::poison_any_pointers_to_descendents (const region *reg,
6570 : : enum poison_kind pkind)
6571 : : {
6572 : 303915 : for (const auto &cluster_iter : m_store)
6573 : : {
6574 : 133617 : binding_cluster *cluster = cluster_iter.second;
6575 : 133617 : for (auto iter = cluster->begin ();
6576 : 270091 : iter != cluster->end ();
6577 : 136474 : ++iter)
6578 : : {
6579 : 136474 : auto bp = *iter;
6580 : 136474 : const svalue *sval = bp.m_sval;
6581 : 136474 : if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
6582 : : {
6583 : 31911 : const region *ptr_dst = ptr_sval->get_pointee ();
6584 : : /* Poison ptrs to descendents of REG, but not to REG itself,
6585 : : otherwise double-free detection doesn't work (since sm-state
6586 : : for "free" is stored on the original ptr svalue). */
6587 : 31911 : if (ptr_dst->descendent_of_p (reg)
6588 : 31911 : && ptr_dst != reg)
6589 : : {
6590 : 133 : const svalue *new_sval
6591 : 133 : = m_mgr->get_or_create_poisoned_svalue (pkind,
6592 : : sval->get_type ());
6593 : 133 : cluster->get_map ().overwrite (iter, new_sval);
6594 : : }
6595 : : }
6596 : : }
6597 : : }
6598 : 36681 : }
6599 : :
6600 : : /* Attempt to merge THIS with OTHER_MODEL, writing the result
6601 : : to OUT_MODEL. Use POINT to distinguish values created as a
6602 : : result of merging. */
6603 : :
6604 : : bool
6605 : 117904 : region_model::can_merge_with_p (const region_model &other_model,
6606 : : const program_point &point,
6607 : : region_model *out_model,
6608 : : const extrinsic_state *ext_state,
6609 : : const program_state *state_a,
6610 : : const program_state *state_b) const
6611 : : {
6612 : 117904 : gcc_assert (out_model);
6613 : 117904 : gcc_assert (m_mgr == other_model.m_mgr);
6614 : 117904 : gcc_assert (m_mgr == out_model->m_mgr);
6615 : :
6616 : 117904 : if (m_current_frame != other_model.m_current_frame)
6617 : : return false;
6618 : 117904 : out_model->m_current_frame = m_current_frame;
6619 : :
6620 : 117904 : model_merger m (this, &other_model, point, out_model,
6621 : 117904 : ext_state, state_a, state_b);
6622 : :
6623 : 117904 : if (!store::can_merge_p (&m_store, &other_model.m_store,
6624 : 117904 : &out_model->m_store, m_mgr->get_store_manager (),
6625 : : &m))
6626 : : return false;
6627 : :
6628 : 42128 : if (!m_dynamic_extents.can_merge_with_p (other_model.m_dynamic_extents,
6629 : : &out_model->m_dynamic_extents))
6630 : : return false;
6631 : :
6632 : : /* Merge constraints. */
6633 : 39287 : constraint_manager::merge (*m_constraints,
6634 : 39287 : *other_model.m_constraints,
6635 : : out_model->m_constraints);
6636 : :
6637 : 40023 : for (auto iter : m.m_svals_changing_meaning)
6638 : 736 : out_model->m_constraints->purge_state_involving (iter);
6639 : :
6640 : 39287 : if (m_thrown_exceptions_stack != other_model.m_thrown_exceptions_stack)
6641 : : return false;
6642 : 39258 : out_model->m_thrown_exceptions_stack = m_thrown_exceptions_stack;
6643 : :
6644 : 39258 : if (m_caught_exceptions_stack != other_model.m_caught_exceptions_stack)
6645 : : return false;
6646 : 39258 : out_model->m_caught_exceptions_stack = m_caught_exceptions_stack;
6647 : :
6648 : 39258 : return true;
6649 : 117904 : }
6650 : :
6651 : : /* Attempt to get the fndecl used at CALL, if known, or NULL_TREE
6652 : : otherwise. */
6653 : :
6654 : : tree
6655 : 923320 : region_model::get_fndecl_for_call (const gcall &call,
6656 : : region_model_context *ctxt)
6657 : : {
6658 : 923320 : tree fn_ptr = gimple_call_fn (&call);
6659 : 923320 : if (fn_ptr == NULL_TREE)
6660 : : return NULL_TREE;
6661 : 881500 : const svalue *fn_ptr_sval = get_rvalue (fn_ptr, ctxt);
6662 : 1763000 : if (const region_svalue *fn_ptr_ptr
6663 : 881500 : = fn_ptr_sval->dyn_cast_region_svalue ())
6664 : : {
6665 : 874823 : const region *reg = fn_ptr_ptr->get_pointee ();
6666 : 874823 : if (const function_region *fn_reg = reg->dyn_cast_function_region ())
6667 : : {
6668 : 874767 : tree fn_decl = fn_reg->get_fndecl ();
6669 : 874767 : cgraph_node *node = cgraph_node::get (fn_decl);
6670 : 874767 : if (!node)
6671 : : return NULL_TREE;
6672 : 874767 : const cgraph_node *ultimate_node = node->ultimate_alias_target ();
6673 : 874767 : if (ultimate_node)
6674 : 874767 : return ultimate_node->decl;
6675 : : }
6676 : : }
6677 : :
6678 : : return NULL_TREE;
6679 : : }
6680 : :
6681 : : /* Would be much simpler to use a lambda here, if it were supported. */
6682 : :
6683 : : struct append_regions_cb_data
6684 : : {
6685 : : const region_model *model;
6686 : : auto_vec<const decl_region *> *out;
6687 : : };
6688 : :
6689 : : /* Populate *OUT with all decl_regions in the current
6690 : : frame that have clusters within the store. */
6691 : :
6692 : : void
6693 : 381462 : region_model::
6694 : : get_regions_for_current_frame (auto_vec<const decl_region *> *out) const
6695 : : {
6696 : 381462 : append_regions_cb_data data;
6697 : 381462 : data.model = this;
6698 : 381462 : data.out = out;
6699 : 381462 : m_store.for_each_cluster (append_regions_cb, &data);
6700 : 381462 : }
6701 : :
6702 : : /* Implementation detail of get_regions_for_current_frame. */
6703 : :
6704 : : void
6705 : 2820882 : region_model::append_regions_cb (const region *base_reg,
6706 : : append_regions_cb_data *cb_data)
6707 : : {
6708 : 2820882 : if (base_reg->get_parent_region () != cb_data->model->m_current_frame)
6709 : : return;
6710 : 1460822 : if (const decl_region *decl_reg = base_reg->dyn_cast_decl_region ())
6711 : 1442504 : cb_data->out->safe_push (decl_reg);
6712 : : }
6713 : :
6714 : :
6715 : : /* Abstract class for diagnostics related to the use of
6716 : : floating-point arithmetic where precision is needed. */
6717 : :
6718 : 25 : class imprecise_floating_point_arithmetic : public pending_diagnostic
6719 : : {
6720 : : public:
6721 : 50 : int get_controlling_option () const final override
6722 : : {
6723 : 50 : return OPT_Wanalyzer_imprecise_fp_arithmetic;
6724 : : }
6725 : : };
6726 : :
6727 : : /* Concrete diagnostic to complain about uses of floating-point arithmetic
6728 : : in the size argument of malloc etc. */
6729 : :
6730 : : class float_as_size_arg : public imprecise_floating_point_arithmetic
6731 : : {
6732 : : public:
6733 : 25 : float_as_size_arg (tree arg) : m_arg (arg)
6734 : : {}
6735 : :
6736 : 305 : const char *get_kind () const final override
6737 : : {
6738 : 305 : return "float_as_size_arg_diagnostic";
6739 : : }
6740 : :
6741 : 25 : bool subclass_equal_p (const pending_diagnostic &other) const final override
6742 : : {
6743 : 25 : return same_tree_p (m_arg, ((const float_as_size_arg &) other).m_arg);
6744 : : }
6745 : :
6746 : 25 : bool emit (diagnostic_emission_context &ctxt) final override
6747 : : {
6748 : 25 : bool warned = ctxt.warn ("use of floating-point arithmetic here might"
6749 : : " yield unexpected results");
6750 : 25 : if (warned)
6751 : 25 : inform (ctxt.get_location (),
6752 : : "only use operands of an integer type"
6753 : : " inside the size argument");
6754 : 25 : return warned;
6755 : : }
6756 : :
6757 : : bool
6758 : 50 : describe_final_event (pretty_printer &pp,
6759 : : const evdesc::final_event &) final override
6760 : : {
6761 : 50 : if (m_arg)
6762 : 50 : pp_printf (&pp,
6763 : : "operand %qE is of type %qT",
6764 : 50 : m_arg, TREE_TYPE (m_arg));
6765 : : else
6766 : 0 : pp_printf (&pp,
6767 : : "at least one operand of the size argument is"
6768 : : " of a floating-point type");
6769 : 50 : return true;
6770 : : }
6771 : :
6772 : : private:
6773 : : tree m_arg;
6774 : : };
6775 : :
6776 : : /* Visitor to find uses of floating-point variables/constants in an svalue. */
6777 : :
6778 : : class contains_floating_point_visitor : public visitor
6779 : : {
6780 : : public:
6781 : 7818 : contains_floating_point_visitor (const svalue *root_sval) : m_result (nullptr)
6782 : : {
6783 : 7818 : root_sval->accept (this);
6784 : : }
6785 : :
6786 : 7818 : const svalue *get_svalue_to_report ()
6787 : : {
6788 : 7818 : return m_result;
6789 : : }
6790 : :
6791 : 7525 : void visit_constant_svalue (const constant_svalue *sval) final override
6792 : : {
6793 : : /* At the point the analyzer runs, constant integer operands in a floating
6794 : : point expression are already implictly converted to floating-points.
6795 : : Thus, we do prefer to report non-constants such that the diagnostic
6796 : : always reports a floating-point operand. */
6797 : 7525 : tree type = sval->get_type ();
6798 : 7525 : if (type && FLOAT_TYPE_P (type) && !m_result)
6799 : 9 : m_result = sval;
6800 : 7525 : }
6801 : :
6802 : 445 : void visit_conjured_svalue (const conjured_svalue *sval) final override
6803 : : {
6804 : 445 : tree type = sval->get_type ();
6805 : 445 : if (type && FLOAT_TYPE_P (type))
6806 : 0 : m_result = sval;
6807 : 445 : }
6808 : :
6809 : 974 : void visit_initial_svalue (const initial_svalue *sval) final override
6810 : : {
6811 : 974 : tree type = sval->get_type ();
6812 : 974 : if (type && FLOAT_TYPE_P (type))
6813 : 16 : m_result = sval;
6814 : 974 : }
6815 : :
6816 : : private:
6817 : : /* Non-null if at least one floating-point operand was found. */
6818 : : const svalue *m_result;
6819 : : };
6820 : :
6821 : : /* May complain about uses of floating-point operands in SIZE_IN_BYTES. */
6822 : :
6823 : : void
6824 : 7818 : region_model::check_dynamic_size_for_floats (const svalue *size_in_bytes,
6825 : : region_model_context *ctxt) const
6826 : : {
6827 : 7818 : gcc_assert (ctxt);
6828 : :
6829 : 7818 : contains_floating_point_visitor v (size_in_bytes);
6830 : 7818 : if (const svalue *float_sval = v.get_svalue_to_report ())
6831 : : {
6832 : 25 : tree diag_arg = get_representative_tree (float_sval);
6833 : 25 : ctxt->warn (std::make_unique<float_as_size_arg> (diag_arg));
6834 : : }
6835 : 7818 : }
6836 : :
6837 : : /* Return a region describing a heap-allocated block of memory.
6838 : : Use CTXT to complain about tainted sizes.
6839 : :
6840 : : Reuse an existing heap_allocated_region if it's not being referenced by
6841 : : this region_model; otherwise create a new one.
6842 : :
6843 : : Optionally (update_state_machine) transitions the pointer pointing to the
6844 : : heap_allocated_region from start to assumed non-null. */
6845 : :
6846 : : const region *
6847 : 17707 : region_model::get_or_create_region_for_heap_alloc (const svalue *size_in_bytes,
6848 : : region_model_context *ctxt,
6849 : : bool update_state_machine,
6850 : : const call_details *cd)
6851 : : {
6852 : : /* Determine which regions are referenced in this region_model, so that
6853 : : we can reuse an existing heap_allocated_region if it's not in use on
6854 : : this path. */
6855 : 17707 : auto_bitmap base_regs_in_use;
6856 : 17707 : get_referenced_base_regions (base_regs_in_use);
6857 : :
6858 : : /* Don't reuse regions that are marked as TOUCHED. */
6859 : 111119 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6860 : 204531 : iter != m_store.end (); ++iter)
6861 : 93412 : if ((*iter).second->touched_p ())
6862 : : {
6863 : 14167 : const region *base_reg = (*iter).first;
6864 : 14167 : bitmap_set_bit (base_regs_in_use, base_reg->get_id ());
6865 : : }
6866 : :
6867 : 17707 : const region *reg
6868 : 17707 : = m_mgr->get_or_create_region_for_heap_alloc (base_regs_in_use);
6869 : 17707 : if (size_in_bytes)
6870 : 12186 : if (compat_types_p (size_in_bytes->get_type (), size_type_node))
6871 : 12186 : set_dynamic_extents (reg, size_in_bytes, ctxt);
6872 : :
6873 : 17707 : if (update_state_machine && cd)
6874 : : {
6875 : 0 : const svalue *ptr_sval
6876 : 0 : = m_mgr->get_ptr_svalue (cd->get_lhs_type (), reg);
6877 : 0 : transition_ptr_sval_non_null (ctxt, ptr_sval);
6878 : : }
6879 : :
6880 : 17707 : return reg;
6881 : 17707 : }
6882 : :
6883 : : /* Populate OUT_IDS with the set of IDs of those base regions which are
6884 : : reachable in this region_model. */
6885 : :
6886 : : void
6887 : 19883 : region_model::get_referenced_base_regions (auto_bitmap &out_ids) const
6888 : : {
6889 : 19883 : reachable_regions reachable_regs (const_cast<region_model *> (this));
6890 : 19883 : m_store.for_each_cluster (reachable_regions::init_cluster_cb,
6891 : : &reachable_regs);
6892 : : /* Get regions for locals that have explicitly bound values. */
6893 : 150657 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6894 : 281431 : iter != m_store.end (); ++iter)
6895 : : {
6896 : 130774 : const region *base_reg = (*iter).first;
6897 : 130774 : if (const region *parent = base_reg->get_parent_region ())
6898 : 130774 : if (parent->get_kind () == RK_FRAME)
6899 : 77970 : reachable_regs.add (base_reg, false);
6900 : : }
6901 : :
6902 : 19887 : for (auto &eh_node : m_thrown_exceptions_stack)
6903 : 4 : eh_node.add_to_reachable_regions (reachable_regs);
6904 : 19967 : for (auto &eh_node : m_caught_exceptions_stack)
6905 : 84 : eh_node.add_to_reachable_regions (reachable_regs);
6906 : :
6907 : :
6908 : 19883 : bitmap_clear (out_ids);
6909 : 154120 : for (auto iter_reg : reachable_regs)
6910 : 134237 : bitmap_set_bit (out_ids, iter_reg->get_id ());
6911 : 19883 : }
6912 : :
6913 : : /* Return a new region describing a block of memory allocated within the
6914 : : current frame.
6915 : : Use CTXT to complain about tainted sizes. */
6916 : :
6917 : : const region *
6918 : 425 : region_model::create_region_for_alloca (const svalue *size_in_bytes,
6919 : : region_model_context *ctxt)
6920 : : {
6921 : 425 : const region *reg = m_mgr->create_region_for_alloca (m_current_frame);
6922 : 425 : if (compat_types_p (size_in_bytes->get_type (), size_type_node))
6923 : 424 : set_dynamic_extents (reg, size_in_bytes, ctxt);
6924 : 425 : return reg;
6925 : : }
6926 : :
6927 : : /* Record that the size of REG is SIZE_IN_BYTES.
6928 : : Use CTXT to complain about tainted sizes. */
6929 : :
6930 : : void
6931 : 13070 : region_model::set_dynamic_extents (const region *reg,
6932 : : const svalue *size_in_bytes,
6933 : : region_model_context *ctxt)
6934 : : {
6935 : 13070 : assert_compat_types (size_in_bytes->get_type (), size_type_node);
6936 : 13070 : if (ctxt)
6937 : : {
6938 : 7818 : check_dynamic_size_for_taint (reg->get_memory_space (), size_in_bytes,
6939 : : ctxt);
6940 : 7818 : check_dynamic_size_for_floats (size_in_bytes, ctxt);
6941 : : }
6942 : 13070 : m_dynamic_extents.put (reg, size_in_bytes);
6943 : 13070 : }
6944 : :
6945 : : /* Get the recording of REG in bytes, or nullptr if no dynamic size was
6946 : : recorded. */
6947 : :
6948 : : const svalue *
6949 : 63968 : region_model::get_dynamic_extents (const region *reg) const
6950 : : {
6951 : 63968 : if (const svalue * const *slot = m_dynamic_extents.get (reg))
6952 : 12405 : return *slot;
6953 : : return nullptr;
6954 : : }
6955 : :
6956 : : /* Unset any recorded dynamic size of REG. */
6957 : :
6958 : : void
6959 : 50333 : region_model::unset_dynamic_extents (const region *reg)
6960 : : {
6961 : 50333 : m_dynamic_extents.remove (reg);
6962 : 50333 : }
6963 : :
6964 : : /* A subclass of pending_diagnostic for complaining about uninitialized data
6965 : : being copied across a trust boundary to an untrusted output
6966 : : (e.g. copy_to_user infoleaks in the Linux kernel). */
6967 : :
6968 : : class exposure_through_uninit_copy
6969 : : : public pending_diagnostic_subclass<exposure_through_uninit_copy>
6970 : : {
6971 : : public:
6972 : 25 : exposure_through_uninit_copy (const region *src_region,
6973 : : const region *dest_region,
6974 : : const svalue *copied_sval)
6975 : 25 : : m_src_region (src_region),
6976 : 25 : m_dest_region (dest_region),
6977 : 25 : m_copied_sval (copied_sval)
6978 : : {
6979 : 25 : gcc_assert (m_copied_sval->get_kind () == SK_POISONED
6980 : : || m_copied_sval->get_kind () == SK_COMPOUND);
6981 : 25 : }
6982 : :
6983 : 294 : const char *get_kind () const final override
6984 : : {
6985 : 294 : return "exposure_through_uninit_copy";
6986 : : }
6987 : :
6988 : 25 : bool operator== (const exposure_through_uninit_copy &other) const
6989 : : {
6990 : 25 : return (m_src_region == other.m_src_region
6991 : 25 : && m_dest_region == other.m_dest_region
6992 : 50 : && m_copied_sval == other.m_copied_sval);
6993 : : }
6994 : :
6995 : 50 : int get_controlling_option () const final override
6996 : : {
6997 : 50 : return OPT_Wanalyzer_exposure_through_uninit_copy;
6998 : : }
6999 : :
7000 : 25 : bool emit (diagnostic_emission_context &ctxt) final override
7001 : : {
7002 : : /* CWE-200: Exposure of Sensitive Information to an Unauthorized Actor. */
7003 : 25 : ctxt.add_cwe (200);
7004 : 50 : enum memory_space mem_space = get_src_memory_space ();
7005 : 25 : bool warned;
7006 : 25 : switch (mem_space)
7007 : : {
7008 : 0 : default:
7009 : 0 : warned = ctxt.warn ("potential exposure of sensitive information"
7010 : : " by copying uninitialized data"
7011 : : " across trust boundary");
7012 : 0 : break;
7013 : 25 : case MEMSPACE_STACK:
7014 : 25 : warned = ctxt.warn ("potential exposure of sensitive information"
7015 : : " by copying uninitialized data from stack"
7016 : : " across trust boundary");
7017 : 25 : break;
7018 : 0 : case MEMSPACE_HEAP:
7019 : 0 : warned = ctxt.warn ("potential exposure of sensitive information"
7020 : : " by copying uninitialized data from heap"
7021 : : " across trust boundary");
7022 : 0 : break;
7023 : : }
7024 : 25 : if (warned)
7025 : : {
7026 : 25 : const location_t loc = ctxt.get_location ();
7027 : 25 : inform_number_of_uninit_bits (loc);
7028 : 25 : complain_about_uninit_ranges (loc);
7029 : :
7030 : 25 : if (mem_space == MEMSPACE_STACK)
7031 : 25 : maybe_emit_fixit_hint ();
7032 : : }
7033 : 25 : return warned;
7034 : : }
7035 : :
7036 : : bool
7037 : 50 : describe_final_event (pretty_printer &pp,
7038 : : const evdesc::final_event &) final override
7039 : : {
7040 : 100 : enum memory_space mem_space = get_src_memory_space ();
7041 : 50 : switch (mem_space)
7042 : : {
7043 : 0 : default:
7044 : 0 : pp_string (&pp, "uninitialized data copied here");
7045 : 0 : return true;
7046 : :
7047 : 50 : case MEMSPACE_STACK:
7048 : 50 : pp_string (&pp, "uninitialized data copied from stack here");
7049 : 50 : return true;
7050 : :
7051 : 0 : case MEMSPACE_HEAP:
7052 : 0 : pp_string (&pp, "uninitialized data copied from heap here");
7053 : 0 : return true;
7054 : : }
7055 : : }
7056 : :
7057 : 25 : void mark_interesting_stuff (interesting_t *interest) final override
7058 : : {
7059 : 25 : if (m_src_region)
7060 : 25 : interest->add_region_creation (m_src_region);
7061 : 25 : }
7062 : :
7063 : : void
7064 : 0 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
7065 : : const final override
7066 : : {
7067 : 0 : auto &props = result_obj.get_or_create_properties ();
7068 : : #define PROPERTY_PREFIX "gcc/-Wanalyzer-exposure-through-uninit-copy/"
7069 : 0 : props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ());
7070 : 0 : props.set (PROPERTY_PREFIX "dest_region", m_dest_region->to_json ());
7071 : 0 : props.set (PROPERTY_PREFIX "copied_sval", m_copied_sval->to_json ());
7072 : : #undef PROPERTY_PREFIX
7073 : 0 : }
7074 : :
7075 : : private:
7076 : 75 : enum memory_space get_src_memory_space () const
7077 : : {
7078 : 75 : return m_src_region ? m_src_region->get_memory_space () : MEMSPACE_UNKNOWN;
7079 : : }
7080 : :
7081 : 25 : bit_size_t calc_num_uninit_bits () const
7082 : : {
7083 : 25 : switch (m_copied_sval->get_kind ())
7084 : : {
7085 : 0 : default:
7086 : 0 : gcc_unreachable ();
7087 : 4 : break;
7088 : 4 : case SK_POISONED:
7089 : 4 : {
7090 : 4 : const poisoned_svalue *poisoned_sval
7091 : 4 : = as_a <const poisoned_svalue *> (m_copied_sval);
7092 : 4 : gcc_assert (poisoned_sval->get_poison_kind () == poison_kind::uninit);
7093 : :
7094 : : /* Give up if don't have type information. */
7095 : 4 : if (m_copied_sval->get_type () == NULL_TREE)
7096 : 0 : return 0;
7097 : :
7098 : 4 : bit_size_t size_in_bits;
7099 : 4 : if (int_size_in_bits (m_copied_sval->get_type (), &size_in_bits))
7100 : 4 : return size_in_bits;
7101 : :
7102 : : /* Give up if we can't get the size of the type. */
7103 : 0 : return 0;
7104 : : }
7105 : 21 : break;
7106 : 21 : case SK_COMPOUND:
7107 : 21 : {
7108 : 21 : const compound_svalue *compound_sval
7109 : 21 : = as_a <const compound_svalue *> (m_copied_sval);
7110 : 21 : bit_size_t result = 0;
7111 : : /* Find keys for uninit svals. */
7112 : 82 : for (auto iter : *compound_sval)
7113 : : {
7114 : 61 : const svalue *sval = iter.m_sval;
7115 : 122 : if (const poisoned_svalue *psval
7116 : 61 : = sval->dyn_cast_poisoned_svalue ())
7117 : 24 : if (psval->get_poison_kind () == poison_kind::uninit)
7118 : : {
7119 : 24 : const binding_key *key = iter.m_key;
7120 : 24 : const concrete_binding *ckey
7121 : 24 : = key->dyn_cast_concrete_binding ();
7122 : 24 : gcc_assert (ckey);
7123 : 24 : result += ckey->get_size_in_bits ();
7124 : : }
7125 : : }
7126 : 21 : return result;
7127 : : }
7128 : : }
7129 : : }
7130 : :
7131 : 25 : void inform_number_of_uninit_bits (location_t loc) const
7132 : : {
7133 : 25 : bit_size_t num_uninit_bits = calc_num_uninit_bits ();
7134 : 25 : if (num_uninit_bits <= 0)
7135 : 0 : return;
7136 : 25 : if (num_uninit_bits % BITS_PER_UNIT == 0)
7137 : : {
7138 : : /* Express in bytes. */
7139 : 25 : byte_size_t num_uninit_bytes = num_uninit_bits / BITS_PER_UNIT;
7140 : 25 : if (num_uninit_bytes == 1)
7141 : 3 : inform (loc, "1 byte is uninitialized");
7142 : : else
7143 : 22 : inform (loc,
7144 : : "%wu bytes are uninitialized", num_uninit_bytes.to_uhwi ());
7145 : : }
7146 : : else
7147 : : {
7148 : : /* Express in bits. */
7149 : 0 : if (num_uninit_bits == 1)
7150 : 0 : inform (loc, "1 bit is uninitialized");
7151 : : else
7152 : 0 : inform (loc,
7153 : : "%wu bits are uninitialized", num_uninit_bits.to_uhwi ());
7154 : : }
7155 : : }
7156 : :
7157 : 25 : void complain_about_uninit_ranges (location_t loc) const
7158 : : {
7159 : 50 : if (const compound_svalue *compound_sval
7160 : 25 : = m_copied_sval->dyn_cast_compound_svalue ())
7161 : : {
7162 : : /* Find keys for uninit svals. */
7163 : 21 : auto_vec<const concrete_binding *> uninit_keys;
7164 : 82 : for (auto iter : *compound_sval)
7165 : : {
7166 : 61 : const svalue *sval = iter.m_sval;
7167 : 122 : if (const poisoned_svalue *psval
7168 : 61 : = sval->dyn_cast_poisoned_svalue ())
7169 : 24 : if (psval->get_poison_kind () == poison_kind::uninit)
7170 : : {
7171 : 24 : const binding_key *key = iter.m_key;
7172 : 24 : const concrete_binding *ckey
7173 : 24 : = key->dyn_cast_concrete_binding ();
7174 : 24 : gcc_assert (ckey);
7175 : 24 : uninit_keys.safe_push (ckey);
7176 : : }
7177 : : }
7178 : : /* Complain about them in sorted order. */
7179 : 21 : uninit_keys.qsort (concrete_binding::cmp_ptr_ptr);
7180 : :
7181 : 21 : std::unique_ptr<record_layout> layout;
7182 : :
7183 : 21 : tree type = m_copied_sval->get_type ();
7184 : 21 : if (type && TREE_CODE (type) == RECORD_TYPE)
7185 : : {
7186 : 17 : layout = std::make_unique<record_layout> (type);
7187 : :
7188 : 17 : if (0)
7189 : : layout->dump ();
7190 : : }
7191 : :
7192 : : unsigned i;
7193 : : const concrete_binding *ckey;
7194 : 45 : FOR_EACH_VEC_ELT (uninit_keys, i, ckey)
7195 : : {
7196 : 24 : bit_offset_t start_bit = ckey->get_start_bit_offset ();
7197 : 24 : bit_offset_t next_bit = ckey->get_next_bit_offset ();
7198 : 24 : complain_about_uninit_range (loc, start_bit, next_bit,
7199 : 24 : layout.get ());
7200 : : }
7201 : 21 : }
7202 : 25 : }
7203 : :
7204 : 24 : void complain_about_uninit_range (location_t loc,
7205 : : bit_offset_t start_bit,
7206 : : bit_offset_t next_bit,
7207 : : const record_layout *layout) const
7208 : : {
7209 : 24 : if (layout)
7210 : : {
7211 : 75 : while (start_bit < next_bit)
7212 : : {
7213 : 165 : if (const record_layout::item *item
7214 : 55 : = layout->get_item_at (start_bit))
7215 : : {
7216 : 55 : gcc_assert (start_bit >= item->get_start_bit_offset ());
7217 : 55 : gcc_assert (start_bit < item->get_next_bit_offset ());
7218 : 55 : if (item->get_start_bit_offset () == start_bit
7219 : 108 : && item->get_next_bit_offset () <= next_bit)
7220 : 53 : complain_about_fully_uninit_item (*item);
7221 : : else
7222 : 2 : complain_about_partially_uninit_item (*item);
7223 : 55 : start_bit = item->get_next_bit_offset ();
7224 : 55 : continue;
7225 : : }
7226 : : else
7227 : : break;
7228 : : }
7229 : : }
7230 : :
7231 : 24 : if (start_bit >= next_bit)
7232 : : return;
7233 : :
7234 : 4 : if (start_bit % 8 == 0 && next_bit % 8 == 0)
7235 : : {
7236 : : /* Express in bytes. */
7237 : 4 : byte_offset_t start_byte = start_bit / 8;
7238 : 4 : byte_offset_t last_byte = (next_bit / 8) - 1;
7239 : 4 : if (last_byte == start_byte)
7240 : 0 : inform (loc,
7241 : : "byte %wu is uninitialized",
7242 : : start_byte.to_uhwi ());
7243 : : else
7244 : 4 : inform (loc,
7245 : : "bytes %wu - %wu are uninitialized",
7246 : : start_byte.to_uhwi (),
7247 : : last_byte.to_uhwi ());
7248 : : }
7249 : : else
7250 : : {
7251 : : /* Express in bits. */
7252 : 0 : bit_offset_t last_bit = next_bit - 1;
7253 : 0 : if (last_bit == start_bit)
7254 : 0 : inform (loc,
7255 : : "bit %wu is uninitialized",
7256 : : start_bit.to_uhwi ());
7257 : : else
7258 : 0 : inform (loc,
7259 : : "bits %wu - %wu are uninitialized",
7260 : : start_bit.to_uhwi (),
7261 : : last_bit.to_uhwi ());
7262 : : }
7263 : : }
7264 : :
7265 : : static void
7266 : 53 : complain_about_fully_uninit_item (const record_layout::item &item)
7267 : : {
7268 : 53 : const_tree field = item.m_field;
7269 : 53 : bit_size_t num_bits = item.m_bit_range.m_size_in_bits;
7270 : 53 : if (item.m_is_padding)
7271 : : {
7272 : 11 : if (num_bits % 8 == 0)
7273 : : {
7274 : : /* Express in bytes. */
7275 : 9 : byte_size_t num_bytes = num_bits / BITS_PER_UNIT;
7276 : 9 : if (num_bytes == 1)
7277 : 2 : inform (DECL_SOURCE_LOCATION (field),
7278 : : "padding after field %qD is uninitialized (1 byte)",
7279 : : field);
7280 : : else
7281 : 7 : inform (DECL_SOURCE_LOCATION (field),
7282 : : "padding after field %qD is uninitialized (%wu bytes)",
7283 : : field, num_bytes.to_uhwi ());
7284 : : }
7285 : : else
7286 : : {
7287 : : /* Express in bits. */
7288 : 2 : if (num_bits == 1)
7289 : 0 : inform (DECL_SOURCE_LOCATION (field),
7290 : : "padding after field %qD is uninitialized (1 bit)",
7291 : : field);
7292 : : else
7293 : 2 : inform (DECL_SOURCE_LOCATION (field),
7294 : : "padding after field %qD is uninitialized (%wu bits)",
7295 : : field, num_bits.to_uhwi ());
7296 : : }
7297 : : }
7298 : : else
7299 : : {
7300 : 42 : if (num_bits % 8 == 0)
7301 : : {
7302 : : /* Express in bytes. */
7303 : 32 : byte_size_t num_bytes = num_bits / BITS_PER_UNIT;
7304 : 32 : if (num_bytes == 1)
7305 : 1 : inform (DECL_SOURCE_LOCATION (field),
7306 : : "field %qD is uninitialized (1 byte)", field);
7307 : : else
7308 : 31 : inform (DECL_SOURCE_LOCATION (field),
7309 : : "field %qD is uninitialized (%wu bytes)",
7310 : : field, num_bytes.to_uhwi ());
7311 : : }
7312 : : else
7313 : : {
7314 : : /* Express in bits. */
7315 : 10 : if (num_bits == 1)
7316 : 9 : inform (DECL_SOURCE_LOCATION (field),
7317 : : "field %qD is uninitialized (1 bit)", field);
7318 : : else
7319 : 1 : inform (DECL_SOURCE_LOCATION (field),
7320 : : "field %qD is uninitialized (%wu bits)",
7321 : : field, num_bits.to_uhwi ());
7322 : : }
7323 : : }
7324 : 53 : }
7325 : :
7326 : : static void
7327 : 2 : complain_about_partially_uninit_item (const record_layout::item &item)
7328 : : {
7329 : 2 : const_tree field = item.m_field;
7330 : 2 : if (item.m_is_padding)
7331 : 0 : inform (DECL_SOURCE_LOCATION (field),
7332 : : "padding after field %qD is partially uninitialized",
7333 : : field);
7334 : : else
7335 : 2 : inform (DECL_SOURCE_LOCATION (field),
7336 : : "field %qD is partially uninitialized",
7337 : : field);
7338 : : /* TODO: ideally we'd describe what parts are uninitialized. */
7339 : 2 : }
7340 : :
7341 : 25 : void maybe_emit_fixit_hint () const
7342 : : {
7343 : 25 : if (tree decl = m_src_region->maybe_get_decl ())
7344 : : {
7345 : 25 : gcc_rich_location hint_richloc (DECL_SOURCE_LOCATION (decl));
7346 : 25 : hint_richloc.add_fixit_insert_after (" = {0}");
7347 : 25 : inform (&hint_richloc,
7348 : : "suggest forcing zero-initialization by"
7349 : : " providing a %<{0}%> initializer");
7350 : 25 : }
7351 : 25 : }
7352 : :
7353 : : private:
7354 : : const region *m_src_region;
7355 : : const region *m_dest_region;
7356 : : const svalue *m_copied_sval;
7357 : : };
7358 : :
7359 : : /* Return true if any part of SVAL is uninitialized. */
7360 : :
7361 : : static bool
7362 : 80 : contains_uninit_p (const svalue *sval)
7363 : : {
7364 : 80 : switch (sval->get_kind ())
7365 : : {
7366 : : default:
7367 : : return false;
7368 : 4 : case SK_POISONED:
7369 : 4 : {
7370 : 4 : const poisoned_svalue *psval
7371 : 4 : = as_a <const poisoned_svalue *> (sval);
7372 : 4 : return psval->get_poison_kind () == poison_kind::uninit;
7373 : : }
7374 : 43 : case SK_COMPOUND:
7375 : 43 : {
7376 : 43 : const compound_svalue *compound_sval
7377 : 43 : = as_a <const compound_svalue *> (sval);
7378 : :
7379 : 141 : for (auto iter : *compound_sval)
7380 : : {
7381 : 119 : const svalue *sval = iter.m_sval;
7382 : 238 : if (const poisoned_svalue *psval
7383 : 119 : = sval->dyn_cast_poisoned_svalue ())
7384 : 21 : if (psval->get_poison_kind () == poison_kind::uninit)
7385 : 21 : return true;
7386 : : }
7387 : :
7388 : 22 : return false;
7389 : : }
7390 : : }
7391 : : }
7392 : :
7393 : : /* Function for use by plugins when simulating writing data through a
7394 : : pointer to an "untrusted" region DST_REG (and thus crossing a security
7395 : : boundary), such as copying data to user space in an OS kernel.
7396 : :
7397 : : Check that COPIED_SVAL is fully initialized. If not, complain about
7398 : : an infoleak to CTXT.
7399 : :
7400 : : SRC_REG can be nullptr; if non-NULL it is used as a hint in the diagnostic
7401 : : as to where COPIED_SVAL came from. */
7402 : :
7403 : : void
7404 : 80 : region_model::maybe_complain_about_infoleak (const region *dst_reg,
7405 : : const svalue *copied_sval,
7406 : : const region *src_reg,
7407 : : region_model_context *ctxt)
7408 : : {
7409 : : /* Check for exposure. */
7410 : 80 : if (contains_uninit_p (copied_sval))
7411 : 25 : ctxt->warn
7412 : 25 : (std::make_unique<exposure_through_uninit_copy> (src_reg,
7413 : : dst_reg,
7414 : : copied_sval));
7415 : 80 : }
7416 : :
7417 : : /* Set errno to a positive symbolic int, as if some error has occurred. */
7418 : :
7419 : : void
7420 : 453 : region_model::set_errno (const call_details &cd)
7421 : : {
7422 : 453 : const region *errno_reg = m_mgr->get_errno_region ();
7423 : 453 : conjured_purge p (this, cd.get_ctxt ());
7424 : 453 : const svalue *new_errno_sval
7425 : 453 : = m_mgr->get_or_create_conjured_svalue (integer_type_node,
7426 : 453 : &cd.get_call_stmt (),
7427 : : errno_reg, p);
7428 : 453 : const svalue *zero
7429 : 453 : = m_mgr->get_or_create_int_cst (integer_type_node, 0);
7430 : 453 : add_constraint (new_errno_sval, GT_EXPR, zero, cd.get_ctxt ());
7431 : 453 : set_value (errno_reg, new_errno_sval, cd.get_ctxt ());
7432 : 453 : }
7433 : :
7434 : : // class region_model_context
7435 : :
7436 : : bool
7437 : 3912 : region_model_context::
7438 : : warn (std::unique_ptr<pending_diagnostic> d,
7439 : : std::unique_ptr<pending_location::fixer_for_epath> ploc_fixer)
7440 : : {
7441 : 3912 : pending_location ploc (get_pending_location_for_diag ());
7442 : 3912 : ploc.m_fixer_for_epath = std::move (ploc_fixer);
7443 : 3912 : return warn_at (std::move (d), std::move (ploc));
7444 : 3912 : }
7445 : :
7446 : : /* class noop_region_model_context : public region_model_context. */
7447 : :
7448 : : void
7449 : 0 : noop_region_model_context::add_note (std::unique_ptr<pending_note>)
7450 : : {
7451 : 0 : }
7452 : :
7453 : : void
7454 : 0 : noop_region_model_context::add_event (std::unique_ptr<checker_event>)
7455 : : {
7456 : 0 : }
7457 : :
7458 : : void
7459 : 78 : noop_region_model_context::bifurcate (std::unique_ptr<custom_edge_info>)
7460 : : {
7461 : 78 : }
7462 : :
7463 : : void
7464 : 0 : noop_region_model_context::terminate_path ()
7465 : : {
7466 : 0 : }
7467 : :
7468 : : /* class region_model_context_decorator : public region_model_context. */
7469 : :
7470 : : void
7471 : 166 : region_model_context_decorator::add_event (std::unique_ptr<checker_event> event)
7472 : : {
7473 : 166 : if (m_inner)
7474 : 166 : m_inner->add_event (std::move (event));
7475 : 166 : }
7476 : :
7477 : : /* struct model_merger. */
7478 : :
7479 : : /* Dump a multiline representation of this merger to PP. */
7480 : :
7481 : : void
7482 : 0 : model_merger::dump_to_pp (pretty_printer *pp, bool simple) const
7483 : : {
7484 : 0 : pp_string (pp, "model A:");
7485 : 0 : pp_newline (pp);
7486 : 0 : m_model_a->dump_to_pp (pp, simple, true);
7487 : 0 : pp_newline (pp);
7488 : :
7489 : 0 : pp_string (pp, "model B:");
7490 : 0 : pp_newline (pp);
7491 : 0 : m_model_b->dump_to_pp (pp, simple, true);
7492 : 0 : pp_newline (pp);
7493 : :
7494 : 0 : pp_string (pp, "merged model:");
7495 : 0 : pp_newline (pp);
7496 : 0 : m_merged_model->dump_to_pp (pp, simple, true);
7497 : 0 : pp_newline (pp);
7498 : 0 : }
7499 : :
7500 : : /* Dump a multiline representation of this merger to FILE. */
7501 : :
7502 : : void
7503 : 0 : model_merger::dump (FILE *fp, bool simple) const
7504 : : {
7505 : 0 : tree_dump_pretty_printer pp (fp);
7506 : 0 : dump_to_pp (&pp, simple);
7507 : 0 : }
7508 : :
7509 : : /* Dump a multiline representation of this merger to stderr. */
7510 : :
7511 : : DEBUG_FUNCTION void
7512 : 0 : model_merger::dump (bool simple) const
7513 : : {
7514 : 0 : dump (stderr, simple);
7515 : 0 : }
7516 : :
7517 : : /* Return true if it's OK to merge SVAL with other svalues. */
7518 : :
7519 : : bool
7520 : 267090 : model_merger::mergeable_svalue_p (const svalue *sval) const
7521 : : {
7522 : 267090 : if (m_ext_state)
7523 : : {
7524 : : /* Reject merging svalues that have non-purgable sm-state,
7525 : : to avoid falsely reporting memory leaks by merging them
7526 : : with something else. For example, given a local var "p",
7527 : : reject the merger of a:
7528 : : store_a mapping "p" to a malloc-ed ptr
7529 : : with:
7530 : : store_b mapping "p" to a NULL ptr. */
7531 : 267042 : if (m_state_a)
7532 : 267042 : if (!m_state_a->can_purge_p (*m_ext_state, sval))
7533 : : return false;
7534 : 265144 : if (m_state_b)
7535 : 265144 : if (!m_state_b->can_purge_p (*m_ext_state, sval))
7536 : : return false;
7537 : : }
7538 : : return true;
7539 : : }
7540 : :
7541 : : /* Mark WIDENING_SVAL as changing meaning during the merge. */
7542 : :
7543 : : void
7544 : 894 : model_merger::on_widening_reuse (const widening_svalue *widening_sval)
7545 : : {
7546 : 894 : m_svals_changing_meaning.add (widening_sval);
7547 : 894 : }
7548 : :
7549 : : } // namespace ana
7550 : :
7551 : : /* Dump RMODEL fully to stderr (i.e. without summarization). */
7552 : :
7553 : : DEBUG_FUNCTION void
7554 : 0 : debug (const region_model &rmodel)
7555 : : {
7556 : 0 : rmodel.dump (false);
7557 : 0 : }
7558 : :
7559 : : /* class rejected_op_constraint : public rejected_constraint. */
7560 : :
7561 : : void
7562 : 4 : rejected_op_constraint::dump_to_pp (pretty_printer *pp) const
7563 : : {
7564 : 4 : region_model m (m_model);
7565 : 4 : const svalue *lhs_sval = m.get_rvalue (m_lhs, nullptr);
7566 : 4 : const svalue *rhs_sval = m.get_rvalue (m_rhs, nullptr);
7567 : 4 : lhs_sval->dump_to_pp (pp, true);
7568 : 4 : pp_printf (pp, " %s ", op_symbol_code (m_op));
7569 : 4 : rhs_sval->dump_to_pp (pp, true);
7570 : 4 : }
7571 : :
7572 : : /* class rejected_default_case : public rejected_constraint. */
7573 : :
7574 : : void
7575 : 0 : rejected_default_case::dump_to_pp (pretty_printer *pp) const
7576 : : {
7577 : 0 : pp_string (pp, "implicit default for enum");
7578 : 0 : }
7579 : :
7580 : : /* class rejected_ranges_constraint : public rejected_constraint. */
7581 : :
7582 : : void
7583 : 0 : rejected_ranges_constraint::dump_to_pp (pretty_printer *pp) const
7584 : : {
7585 : 0 : region_model m (m_model);
7586 : 0 : const svalue *sval = m.get_rvalue (m_expr, nullptr);
7587 : 0 : sval->dump_to_pp (pp, true);
7588 : 0 : pp_string (pp, " in ");
7589 : 0 : m_ranges->dump_to_pp (pp, true);
7590 : 0 : }
7591 : :
7592 : : /* class engine. */
7593 : :
7594 : : /* engine's ctor. */
7595 : :
7596 : 3338 : engine::engine (region_model_manager &mgr,
7597 : : const supergraph *sg,
7598 : 3338 : logger *logger)
7599 : 3338 : : m_mgr (mgr),
7600 : 3338 : m_sg (sg)
7601 : : {
7602 : 3338 : }
7603 : :
7604 : : /* Dump the managed objects by class to LOGGER, and the per-class totals. */
7605 : :
7606 : : void
7607 : 2 : engine::log_stats (logger *logger) const
7608 : : {
7609 : 2 : m_mgr.log_stats (logger, true);
7610 : 2 : }
7611 : :
7612 : : namespace ana {
7613 : :
7614 : : #if CHECKING_P
7615 : :
7616 : : namespace selftest {
7617 : :
7618 : : /* Build a constant tree of the given type from STR. */
7619 : :
7620 : : static tree
7621 : 64 : build_real_cst_from_string (tree type, const char *str)
7622 : : {
7623 : 64 : REAL_VALUE_TYPE real;
7624 : 64 : real_from_string (&real, str);
7625 : 64 : return build_real (type, real);
7626 : : }
7627 : :
7628 : : /* Append various "interesting" constants to OUT (e.g. NaN). */
7629 : :
7630 : : static void
7631 : 8 : append_interesting_constants (auto_vec<tree> *out)
7632 : : {
7633 : 8 : out->safe_push (integer_zero_node);
7634 : 8 : out->safe_push (build_int_cst (integer_type_node, 42));
7635 : 8 : out->safe_push (build_int_cst (unsigned_type_node, 0));
7636 : 8 : out->safe_push (build_int_cst (unsigned_type_node, 42));
7637 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "QNaN"));
7638 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-QNaN"));
7639 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "SNaN"));
7640 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-SNaN"));
7641 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "0.0"));
7642 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-0.0"));
7643 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "Inf"));
7644 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-Inf"));
7645 : 8 : }
7646 : :
7647 : : /* Verify that tree_cmp is a well-behaved comparator for qsort, even
7648 : : if the underlying constants aren't comparable. */
7649 : :
7650 : : static void
7651 : 4 : test_tree_cmp_on_constants ()
7652 : : {
7653 : 4 : auto_vec<tree> csts;
7654 : 4 : append_interesting_constants (&csts);
7655 : :
7656 : : /* Try sorting every triple. */
7657 : 4 : const unsigned num = csts.length ();
7658 : 52 : for (unsigned i = 0; i < num; i++)
7659 : 624 : for (unsigned j = 0; j < num; j++)
7660 : 7488 : for (unsigned k = 0; k < num; k++)
7661 : : {
7662 : 6912 : auto_vec<tree> v (3);
7663 : 6912 : v.quick_push (csts[i]);
7664 : 6912 : v.quick_push (csts[j]);
7665 : 6912 : v.quick_push (csts[k]);
7666 : 6912 : v.qsort (tree_cmp);
7667 : 6912 : }
7668 : 4 : }
7669 : :
7670 : : /* Implementation detail of the ASSERT_CONDITION_* macros. */
7671 : :
7672 : : void
7673 : 8 : assert_condition (const location &loc,
7674 : : region_model &model,
7675 : : const svalue *lhs, tree_code op, const svalue *rhs,
7676 : : tristate expected)
7677 : : {
7678 : 8 : tristate actual = model.eval_condition (lhs, op, rhs);
7679 : 8 : ASSERT_EQ_AT (loc, actual, expected);
7680 : 8 : }
7681 : :
7682 : : /* Implementation detail of the ASSERT_CONDITION_* macros. */
7683 : :
7684 : : void
7685 : 3084 : assert_condition (const location &loc,
7686 : : region_model &model,
7687 : : tree lhs, tree_code op, tree rhs,
7688 : : tristate expected)
7689 : : {
7690 : 3084 : tristate actual = model.eval_condition (lhs, op, rhs, nullptr);
7691 : 3084 : ASSERT_EQ_AT (loc, actual, expected);
7692 : 3084 : }
7693 : :
7694 : : /* Implementation detail of ASSERT_DUMP_TREE_EQ. */
7695 : :
7696 : : static void
7697 : 20 : assert_dump_tree_eq (const location &loc, tree t, const char *expected)
7698 : : {
7699 : 20 : auto_fix_quotes sentinel;
7700 : 20 : pretty_printer pp;
7701 : 20 : pp_format_decoder (&pp) = default_tree_printer;
7702 : 20 : dump_tree (&pp, t);
7703 : 20 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
7704 : 20 : }
7705 : :
7706 : : /* Assert that dump_tree (T) is EXPECTED. */
7707 : :
7708 : : #define ASSERT_DUMP_TREE_EQ(T, EXPECTED) \
7709 : : SELFTEST_BEGIN_STMT \
7710 : : assert_dump_tree_eq ((SELFTEST_LOCATION), (T), (EXPECTED)); \
7711 : : SELFTEST_END_STMT
7712 : :
7713 : : /* Implementation detail of ASSERT_DUMP_EQ. */
7714 : :
7715 : : static void
7716 : 8 : assert_dump_eq (const location &loc,
7717 : : const region_model &model,
7718 : : bool summarize,
7719 : : const char *expected)
7720 : : {
7721 : 8 : auto_fix_quotes sentinel;
7722 : 8 : pretty_printer pp;
7723 : 8 : pp_format_decoder (&pp) = default_tree_printer;
7724 : :
7725 : 8 : model.dump_to_pp (&pp, summarize, true);
7726 : 8 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
7727 : 8 : }
7728 : :
7729 : : /* Assert that MODEL.dump_to_pp (SUMMARIZE) is EXPECTED. */
7730 : :
7731 : : #define ASSERT_DUMP_EQ(MODEL, SUMMARIZE, EXPECTED) \
7732 : : SELFTEST_BEGIN_STMT \
7733 : : assert_dump_eq ((SELFTEST_LOCATION), (MODEL), (SUMMARIZE), (EXPECTED)); \
7734 : : SELFTEST_END_STMT
7735 : :
7736 : : /* Smoketest for region_model::dump_to_pp. */
7737 : :
7738 : : static void
7739 : 4 : test_dump ()
7740 : : {
7741 : 4 : region_model_manager mgr;
7742 : 4 : region_model model (&mgr);
7743 : :
7744 : 4 : ASSERT_DUMP_EQ (model, false,
7745 : : "stack depth: 0\n"
7746 : : "m_called_unknown_fn: FALSE\n"
7747 : : "constraint_manager:\n"
7748 : : " equiv classes:\n"
7749 : : " constraints:\n");
7750 : 4 : ASSERT_DUMP_EQ (model, true,
7751 : : "stack depth: 0\n"
7752 : : "m_called_unknown_fn: FALSE\n"
7753 : : "constraint_manager:\n"
7754 : : " equiv classes:\n"
7755 : : " constraints:\n");
7756 : :
7757 : 4 : text_art::ascii_theme theme;
7758 : 4 : pretty_printer pp;
7759 : 4 : dump_to_pp (model, &theme, &pp);
7760 : 4 : ASSERT_STREQ ("Region Model\n"
7761 : : "`- Store\n"
7762 : : " `- m_called_unknown_fn: false\n",
7763 : : pp_formatted_text (&pp));
7764 : 4 : }
7765 : :
7766 : : /* Helper function for selftests. Create a struct or union type named NAME,
7767 : : with the fields given by the FIELD_DECLS in FIELDS.
7768 : : If IS_STRUCT is true create a RECORD_TYPE (aka a struct), otherwise
7769 : : create a UNION_TYPE. */
7770 : :
7771 : : static tree
7772 : 16 : make_test_compound_type (const char *name, bool is_struct,
7773 : : const auto_vec<tree> *fields)
7774 : : {
7775 : 16 : tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE);
7776 : 16 : TYPE_NAME (t) = get_identifier (name);
7777 : 16 : TYPE_SIZE (t) = 0;
7778 : :
7779 : 16 : tree fieldlist = NULL_TREE;
7780 : 16 : int i;
7781 : 16 : tree field;
7782 : 48 : FOR_EACH_VEC_ELT (*fields, i, field)
7783 : : {
7784 : 32 : gcc_assert (TREE_CODE (field) == FIELD_DECL);
7785 : 32 : DECL_CONTEXT (field) = t;
7786 : 32 : fieldlist = chainon (field, fieldlist);
7787 : : }
7788 : 16 : fieldlist = nreverse (fieldlist);
7789 : 16 : TYPE_FIELDS (t) = fieldlist;
7790 : :
7791 : 16 : layout_type (t);
7792 : 16 : return t;
7793 : : }
7794 : :
7795 : : /* Selftest fixture for creating the type "struct coord {int x; int y; };". */
7796 : :
7797 : : struct coord_test
7798 : : {
7799 : 16 : coord_test ()
7800 : 16 : {
7801 : 16 : auto_vec<tree> fields;
7802 : 16 : m_x_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
7803 : : get_identifier ("x"), integer_type_node);
7804 : 16 : fields.safe_push (m_x_field);
7805 : 16 : m_y_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
7806 : : get_identifier ("y"), integer_type_node);
7807 : 16 : fields.safe_push (m_y_field);
7808 : 16 : m_coord_type = make_test_compound_type ("coord", true, &fields);
7809 : 16 : }
7810 : :
7811 : : tree m_x_field;
7812 : : tree m_y_field;
7813 : : tree m_coord_type;
7814 : : };
7815 : :
7816 : : /* Verify usage of a struct. */
7817 : :
7818 : : static void
7819 : 4 : test_struct ()
7820 : : {
7821 : 4 : coord_test ct;
7822 : :
7823 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
7824 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
7825 : : c, ct.m_x_field, NULL_TREE);
7826 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
7827 : : c, ct.m_y_field, NULL_TREE);
7828 : :
7829 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
7830 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
7831 : :
7832 : 4 : region_model_manager mgr;
7833 : 4 : region_model model (&mgr);
7834 : : /* Set fields in order y, then x. */
7835 : 4 : model.set_value (c_y, int_m3, nullptr);
7836 : 4 : model.set_value (c_x, int_17, nullptr);
7837 : :
7838 : : /* Verify get_offset for "c.x". */
7839 : 4 : {
7840 : 4 : const region *c_x_reg = model.get_lvalue (c_x, nullptr);
7841 : 4 : region_offset offset = c_x_reg->get_offset (&mgr);
7842 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, nullptr));
7843 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
7844 : : }
7845 : :
7846 : : /* Verify get_offset for "c.y". */
7847 : 4 : {
7848 : 4 : const region *c_y_reg = model.get_lvalue (c_y, nullptr);
7849 : 4 : region_offset offset = c_y_reg->get_offset (&mgr);
7850 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, nullptr));
7851 : 4 : ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE);
7852 : : }
7853 : :
7854 : : /* Check iteration order of binding_cluster (and thus of binding_map). */
7855 : 4 : {
7856 : 4 : std::vector<binding_map::binding_pair> vec;
7857 : 4 : auto cluster
7858 : 4 : = model.get_store ()->get_cluster (model.get_lvalue (c, nullptr));
7859 : 12 : for (auto iter : *cluster)
7860 : 8 : vec.push_back (iter);
7861 : 4 : ASSERT_EQ (vec.size (), 2);
7862 : : /* we should get them back in ascending order in memory (x then y). */
7863 : : /* x */
7864 : 4 : ASSERT_EQ (vec[0].m_key->dyn_cast_concrete_binding ()->get_bit_range (),
7865 : : bit_range (0, INT_TYPE_SIZE));
7866 : 4 : ASSERT_TRUE (tree_int_cst_equal(vec[0].m_sval->maybe_get_constant (),
7867 : : int_17));
7868 : : /* y */
7869 : 4 : ASSERT_EQ (vec[1].m_key->dyn_cast_concrete_binding ()->get_bit_range (),
7870 : : bit_range (INT_TYPE_SIZE, INT_TYPE_SIZE));
7871 : 4 : ASSERT_TRUE (tree_int_cst_equal(vec[1].m_sval->maybe_get_constant (),
7872 : : int_m3));
7873 : 4 : }
7874 : 4 : }
7875 : :
7876 : : /* Verify usage of an array element. */
7877 : :
7878 : : static void
7879 : 4 : test_array_1 ()
7880 : : {
7881 : 4 : tree tlen = size_int (10);
7882 : 4 : tree arr_type = build_array_type (char_type_node, build_index_type (tlen));
7883 : :
7884 : 4 : tree a = build_global_decl ("a", arr_type);
7885 : :
7886 : 4 : region_model_manager mgr;
7887 : 4 : region_model model (&mgr);
7888 : 4 : tree int_0 = integer_zero_node;
7889 : 4 : tree a_0 = build4 (ARRAY_REF, char_type_node,
7890 : : a, int_0, NULL_TREE, NULL_TREE);
7891 : 4 : tree char_A = build_int_cst (char_type_node, 'A');
7892 : 4 : model.set_value (a_0, char_A, nullptr);
7893 : 4 : }
7894 : :
7895 : : /* Verify that region_model::get_representative_tree works as expected. */
7896 : :
7897 : : static void
7898 : 4 : test_get_representative_tree ()
7899 : : {
7900 : 4 : region_model_manager mgr;
7901 : :
7902 : : /* STRING_CST. */
7903 : 4 : {
7904 : 4 : tree string_cst = build_string (4, "foo");
7905 : 4 : region_model m (&mgr);
7906 : 4 : const svalue *str_sval = m.get_rvalue (string_cst, nullptr);
7907 : 4 : tree rep = m.get_representative_tree (str_sval);
7908 : 4 : ASSERT_EQ (rep, string_cst);
7909 : 4 : }
7910 : :
7911 : : /* String literal. */
7912 : 4 : {
7913 : 4 : tree string_cst_ptr = build_string_literal (4, "foo");
7914 : 4 : region_model m (&mgr);
7915 : 4 : const svalue *str_sval = m.get_rvalue (string_cst_ptr, nullptr);
7916 : 4 : tree rep = m.get_representative_tree (str_sval);
7917 : 4 : ASSERT_DUMP_TREE_EQ (rep, "&\"foo\"[0]");
7918 : 4 : }
7919 : :
7920 : : /* Value of an element within an array. */
7921 : 4 : {
7922 : 4 : tree tlen = size_int (10);
7923 : 4 : tree arr_type = build_array_type (char_type_node, build_index_type (tlen));
7924 : 4 : tree a = build_global_decl ("a", arr_type);
7925 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
7926 : 4 : char_type_node, "test value");
7927 : :
7928 : : /* Value of a[3]. */
7929 : 4 : {
7930 : 4 : test_region_model_context ctxt;
7931 : 4 : region_model model (&mgr);
7932 : 4 : tree int_3 = build_int_cst (integer_type_node, 3);
7933 : 4 : tree a_3 = build4 (ARRAY_REF, char_type_node,
7934 : : a, int_3, NULL_TREE, NULL_TREE);
7935 : 4 : const region *a_3_reg = model.get_lvalue (a_3, &ctxt);
7936 : 4 : model.set_value (a_3_reg, &test_sval, &ctxt);
7937 : 4 : tree rep = model.get_representative_tree (&test_sval);
7938 : 4 : ASSERT_DUMP_TREE_EQ (rep, "a[3]");
7939 : 4 : }
7940 : :
7941 : : /* Value of a[0]. */
7942 : 4 : {
7943 : 4 : test_region_model_context ctxt;
7944 : 4 : region_model model (&mgr);
7945 : 4 : tree idx = integer_zero_node;
7946 : 4 : tree a_0 = build4 (ARRAY_REF, char_type_node,
7947 : : a, idx, NULL_TREE, NULL_TREE);
7948 : 4 : const region *a_0_reg = model.get_lvalue (a_0, &ctxt);
7949 : 4 : model.set_value (a_0_reg, &test_sval, &ctxt);
7950 : 4 : tree rep = model.get_representative_tree (&test_sval);
7951 : 4 : ASSERT_DUMP_TREE_EQ (rep, "a[0]");
7952 : 4 : }
7953 : 4 : }
7954 : :
7955 : : /* Value of a field within a struct. */
7956 : 4 : {
7957 : 4 : coord_test ct;
7958 : :
7959 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
7960 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
7961 : : c, ct.m_x_field, NULL_TREE);
7962 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
7963 : : c, ct.m_y_field, NULL_TREE);
7964 : :
7965 : 4 : test_region_model_context ctxt;
7966 : :
7967 : : /* Value of initial field. */
7968 : 4 : {
7969 : 4 : region_model m (&mgr);
7970 : 4 : const region *c_x_reg = m.get_lvalue (c_x, &ctxt);
7971 : 4 : placeholder_svalue test_sval_x (mgr.alloc_symbol_id (),
7972 : 4 : integer_type_node, "test x val");
7973 : 4 : m.set_value (c_x_reg, &test_sval_x, &ctxt);
7974 : 4 : tree rep = m.get_representative_tree (&test_sval_x);
7975 : 4 : ASSERT_DUMP_TREE_EQ (rep, "c.x");
7976 : 4 : }
7977 : :
7978 : : /* Value of non-initial field. */
7979 : 4 : {
7980 : 4 : region_model m (&mgr);
7981 : 4 : const region *c_y_reg = m.get_lvalue (c_y, &ctxt);
7982 : 4 : placeholder_svalue test_sval_y (mgr.alloc_symbol_id (),
7983 : 4 : integer_type_node, "test y val");
7984 : 4 : m.set_value (c_y_reg, &test_sval_y, &ctxt);
7985 : 4 : tree rep = m.get_representative_tree (&test_sval_y);
7986 : 4 : ASSERT_DUMP_TREE_EQ (rep, "c.y");
7987 : 4 : }
7988 : 4 : }
7989 : 4 : }
7990 : :
7991 : : /* Verify that calling region_model::get_rvalue repeatedly on the same
7992 : : tree constant retrieves the same svalue *. */
7993 : :
7994 : : static void
7995 : 4 : test_unique_constants ()
7996 : : {
7997 : 4 : tree int_0 = integer_zero_node;
7998 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
7999 : :
8000 : 4 : test_region_model_context ctxt;
8001 : 4 : region_model_manager mgr;
8002 : 4 : region_model model (&mgr);
8003 : 4 : ASSERT_EQ (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_0, &ctxt));
8004 : 4 : ASSERT_EQ (model.get_rvalue (int_42, &ctxt),
8005 : : model.get_rvalue (int_42, &ctxt));
8006 : 4 : ASSERT_NE (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_42, &ctxt));
8007 : 4 : ASSERT_EQ (ctxt.get_num_diagnostics (), 0);
8008 : :
8009 : : /* A "(const int)42" will be a different tree from "(int)42)"... */
8010 : 4 : tree const_int_type_node
8011 : 4 : = build_qualified_type (integer_type_node, TYPE_QUAL_CONST);
8012 : 4 : tree const_int_42 = build_int_cst (const_int_type_node, 42);
8013 : 4 : ASSERT_NE (int_42, const_int_42);
8014 : : /* It should have a different const_svalue. */
8015 : 4 : const svalue *int_42_sval = model.get_rvalue (int_42, &ctxt);
8016 : 4 : const svalue *const_int_42_sval = model.get_rvalue (const_int_42, &ctxt);
8017 : 4 : ASSERT_NE (int_42_sval, const_int_42_sval);
8018 : : /* But they should compare as equal. */
8019 : 4 : ASSERT_CONDITION_TRUE (model, int_42_sval, EQ_EXPR, const_int_42_sval);
8020 : 4 : ASSERT_CONDITION_FALSE (model, int_42_sval, NE_EXPR, const_int_42_sval);
8021 : 4 : }
8022 : :
8023 : : /* Verify that each type gets its own singleton unknown_svalue within a
8024 : : region_model_manager, and that NULL_TREE gets its own singleton. */
8025 : :
8026 : : static void
8027 : 4 : test_unique_unknowns ()
8028 : : {
8029 : 4 : region_model_manager mgr;
8030 : 4 : const svalue *unknown_int
8031 : 4 : = mgr.get_or_create_unknown_svalue (integer_type_node);
8032 : : /* Repeated calls with the same type should get the same "unknown"
8033 : : svalue. */
8034 : 4 : const svalue *unknown_int_2
8035 : 4 : = mgr.get_or_create_unknown_svalue (integer_type_node);
8036 : 4 : ASSERT_EQ (unknown_int, unknown_int_2);
8037 : :
8038 : : /* Different types (or the NULL type) should have different
8039 : : unknown_svalues. */
8040 : 4 : const svalue *unknown_NULL_type = mgr.get_or_create_unknown_svalue (nullptr);
8041 : 4 : ASSERT_NE (unknown_NULL_type, unknown_int);
8042 : :
8043 : : /* Repeated calls with NULL for the type should get the same "unknown"
8044 : : svalue. */
8045 : 4 : const svalue *unknown_NULL_type_2 = mgr.get_or_create_unknown_svalue (nullptr);
8046 : 4 : ASSERT_EQ (unknown_NULL_type, unknown_NULL_type_2);
8047 : 4 : }
8048 : :
8049 : : /* Verify that initial_svalue are handled as expected. */
8050 : :
8051 : : static void
8052 : 4 : test_initial_svalue_folding ()
8053 : : {
8054 : 4 : region_model_manager mgr;
8055 : 4 : tree x = build_global_decl ("x", integer_type_node);
8056 : 4 : tree y = build_global_decl ("y", integer_type_node);
8057 : :
8058 : 4 : test_region_model_context ctxt;
8059 : 4 : region_model model (&mgr);
8060 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8061 : 4 : const svalue *y_init = model.get_rvalue (y, &ctxt);
8062 : 4 : ASSERT_NE (x_init, y_init);
8063 : 4 : const region *x_reg = model.get_lvalue (x, &ctxt);
8064 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg));
8065 : :
8066 : 4 : }
8067 : :
8068 : : /* Verify that unary ops are folded as expected. */
8069 : :
8070 : : static void
8071 : 4 : test_unaryop_svalue_folding ()
8072 : : {
8073 : 4 : region_model_manager mgr;
8074 : 4 : tree x = build_global_decl ("x", integer_type_node);
8075 : 4 : tree y = build_global_decl ("y", integer_type_node);
8076 : :
8077 : 4 : test_region_model_context ctxt;
8078 : 4 : region_model model (&mgr);
8079 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8080 : 4 : const svalue *y_init = model.get_rvalue (y, &ctxt);
8081 : 4 : const region *x_reg = model.get_lvalue (x, &ctxt);
8082 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg));
8083 : :
8084 : : /* "(int)x" -> "x". */
8085 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_cast (integer_type_node, x_init));
8086 : :
8087 : : /* "(void *)x" -> something other than "x". */
8088 : 4 : ASSERT_NE (x_init, mgr.get_or_create_cast (ptr_type_node, x_init));
8089 : :
8090 : : /* "!(x == y)" -> "x != y". */
8091 : 4 : ASSERT_EQ (mgr.get_or_create_unaryop
8092 : : (boolean_type_node, TRUTH_NOT_EXPR,
8093 : : mgr.get_or_create_binop (boolean_type_node, EQ_EXPR,
8094 : : x_init, y_init)),
8095 : : mgr.get_or_create_binop (boolean_type_node, NE_EXPR,
8096 : : x_init, y_init));
8097 : : /* "!(x > y)" -> "x <= y". */
8098 : 4 : ASSERT_EQ (mgr.get_or_create_unaryop
8099 : : (boolean_type_node, TRUTH_NOT_EXPR,
8100 : : mgr.get_or_create_binop (boolean_type_node, GT_EXPR,
8101 : : x_init, y_init)),
8102 : : mgr.get_or_create_binop (boolean_type_node, LE_EXPR,
8103 : : x_init, y_init));
8104 : 4 : }
8105 : :
8106 : : /* Verify that binops on constant svalues are folded. */
8107 : :
8108 : : static void
8109 : 4 : test_binop_svalue_folding ()
8110 : : {
8111 : : #define NUM_CSTS 10
8112 : 4 : tree cst_int[NUM_CSTS];
8113 : 4 : region_model_manager mgr;
8114 : 4 : const svalue *cst_sval[NUM_CSTS];
8115 : 44 : for (int i = 0; i < NUM_CSTS; i++)
8116 : : {
8117 : 40 : cst_int[i] = build_int_cst (integer_type_node, i);
8118 : 40 : cst_sval[i] = mgr.get_or_create_constant_svalue (cst_int[i]);
8119 : 40 : ASSERT_EQ (cst_sval[i]->get_kind (), SK_CONSTANT);
8120 : 40 : ASSERT_EQ (cst_sval[i]->maybe_get_constant (), cst_int[i]);
8121 : : }
8122 : :
8123 : 44 : for (int i = 0; i < NUM_CSTS; i++)
8124 : 440 : for (int j = 0; j < NUM_CSTS; j++)
8125 : : {
8126 : 400 : if (i != j)
8127 : 360 : ASSERT_NE (cst_sval[i], cst_sval[j]);
8128 : 400 : if (i + j < NUM_CSTS)
8129 : : {
8130 : 220 : const svalue *sum
8131 : 220 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8132 : : cst_sval[i], cst_sval[j]);
8133 : 220 : ASSERT_EQ (sum, cst_sval[i + j]);
8134 : : }
8135 : 400 : if (i - j >= 0)
8136 : : {
8137 : 220 : const svalue *difference
8138 : 220 : = mgr.get_or_create_binop (integer_type_node, MINUS_EXPR,
8139 : : cst_sval[i], cst_sval[j]);
8140 : 220 : ASSERT_EQ (difference, cst_sval[i - j]);
8141 : : }
8142 : 400 : if (i * j < NUM_CSTS)
8143 : : {
8144 : 168 : const svalue *product
8145 : 168 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8146 : : cst_sval[i], cst_sval[j]);
8147 : 168 : ASSERT_EQ (product, cst_sval[i * j]);
8148 : : }
8149 : 400 : const svalue *eq = mgr.get_or_create_binop (integer_type_node, EQ_EXPR,
8150 : : cst_sval[i], cst_sval[j]);
8151 : 400 : ASSERT_EQ (eq, i == j ? cst_sval[1] : cst_sval [0]);
8152 : 400 : const svalue *neq = mgr.get_or_create_binop (integer_type_node, NE_EXPR,
8153 : : cst_sval[i], cst_sval[j]);
8154 : 400 : ASSERT_EQ (neq, i != j ? cst_sval[1] : cst_sval [0]);
8155 : : // etc
8156 : : }
8157 : :
8158 : 4 : tree x = build_global_decl ("x", integer_type_node);
8159 : :
8160 : 4 : test_region_model_context ctxt;
8161 : 4 : region_model model (&mgr);
8162 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8163 : :
8164 : : /* PLUS_EXPR folding. */
8165 : 4 : const svalue *x_init_plus_zero
8166 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8167 : : x_init, cst_sval[0]);
8168 : 4 : ASSERT_EQ (x_init_plus_zero, x_init);
8169 : 4 : const svalue *zero_plus_x_init
8170 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8171 : : cst_sval[0], x_init);
8172 : 4 : ASSERT_EQ (zero_plus_x_init, x_init);
8173 : :
8174 : : /* MULT_EXPR folding. */
8175 : 4 : const svalue *x_init_times_zero
8176 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8177 : : x_init, cst_sval[0]);
8178 : 4 : ASSERT_EQ (x_init_times_zero, cst_sval[0]);
8179 : 4 : const svalue *zero_times_x_init
8180 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8181 : : cst_sval[0], x_init);
8182 : 4 : ASSERT_EQ (zero_times_x_init, cst_sval[0]);
8183 : :
8184 : 4 : const svalue *x_init_times_one
8185 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8186 : : x_init, cst_sval[1]);
8187 : 4 : ASSERT_EQ (x_init_times_one, x_init);
8188 : 4 : const svalue *one_times_x_init
8189 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8190 : : cst_sval[1], x_init);
8191 : 4 : ASSERT_EQ (one_times_x_init, x_init);
8192 : :
8193 : : // etc
8194 : : // TODO: do we want to use the match-and-simplify DSL for this?
8195 : :
8196 : : /* Verify that binops put any constants on the RHS. */
8197 : 4 : const svalue *four_times_x_init
8198 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8199 : : cst_sval[4], x_init);
8200 : 4 : const svalue *x_init_times_four
8201 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8202 : : x_init, cst_sval[4]);
8203 : 4 : ASSERT_EQ (four_times_x_init, x_init_times_four);
8204 : 4 : const binop_svalue *binop = four_times_x_init->dyn_cast_binop_svalue ();
8205 : 4 : ASSERT_EQ (binop->get_op (), MULT_EXPR);
8206 : 4 : ASSERT_EQ (binop->get_arg0 (), x_init);
8207 : 4 : ASSERT_EQ (binop->get_arg1 (), cst_sval[4]);
8208 : :
8209 : : /* Verify that ((x + 1) + 1) == (x + 2). */
8210 : 4 : const svalue *x_init_plus_one
8211 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8212 : : x_init, cst_sval[1]);
8213 : 4 : const svalue *x_init_plus_two
8214 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8215 : : x_init, cst_sval[2]);
8216 : 4 : const svalue *x_init_plus_one_plus_one
8217 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8218 : : x_init_plus_one, cst_sval[1]);
8219 : 4 : ASSERT_EQ (x_init_plus_one_plus_one, x_init_plus_two);
8220 : :
8221 : : /* Verify various binops on booleans. */
8222 : 4 : {
8223 : 4 : const svalue *sval_true = mgr.get_or_create_int_cst (boolean_type_node, 1);
8224 : 4 : const svalue *sval_false = mgr.get_or_create_int_cst (boolean_type_node, 0);
8225 : 4 : const svalue *sval_unknown
8226 : 4 : = mgr.get_or_create_unknown_svalue (boolean_type_node);
8227 : 4 : const placeholder_svalue sval_placeholder (mgr.alloc_symbol_id (),
8228 : 4 : boolean_type_node, "v");
8229 : 12 : for (auto op : {BIT_IOR_EXPR, TRUTH_OR_EXPR})
8230 : : {
8231 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8232 : : sval_true, sval_unknown),
8233 : : sval_true);
8234 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8235 : : sval_false, sval_unknown),
8236 : : sval_unknown);
8237 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8238 : : sval_false, &sval_placeholder),
8239 : : &sval_placeholder);
8240 : : }
8241 : 12 : for (auto op : {BIT_AND_EXPR, TRUTH_AND_EXPR})
8242 : : {
8243 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8244 : : sval_false, sval_unknown),
8245 : : sval_false);
8246 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8247 : : sval_true, sval_unknown),
8248 : : sval_unknown);
8249 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8250 : : sval_true, &sval_placeholder),
8251 : : &sval_placeholder);
8252 : : }
8253 : 4 : }
8254 : 4 : }
8255 : :
8256 : : /* Verify that sub_svalues are folded as expected. */
8257 : :
8258 : : static void
8259 : 4 : test_sub_svalue_folding ()
8260 : : {
8261 : 4 : coord_test ct;
8262 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
8263 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8264 : : c, ct.m_x_field, NULL_TREE);
8265 : :
8266 : 4 : region_model_manager mgr;
8267 : 4 : region_model model (&mgr);
8268 : 4 : test_region_model_context ctxt;
8269 : 4 : const region *c_x_reg = model.get_lvalue (c_x, &ctxt);
8270 : :
8271 : : /* Verify that sub_svalue of "unknown" simply
8272 : : yields an unknown. */
8273 : :
8274 : 4 : const svalue *unknown = mgr.get_or_create_unknown_svalue (ct.m_coord_type);
8275 : 4 : const svalue *sub = mgr.get_or_create_sub_svalue (TREE_TYPE (ct.m_x_field),
8276 : : unknown, c_x_reg);
8277 : 4 : ASSERT_EQ (sub->get_kind (), SK_UNKNOWN);
8278 : 4 : ASSERT_EQ (sub->get_type (), TREE_TYPE (ct.m_x_field));
8279 : 4 : }
8280 : :
8281 : : /* Get BIT within VAL as a symbolic value within MGR. */
8282 : :
8283 : : static const svalue *
8284 : 256 : get_bit (region_model_manager *mgr,
8285 : : bit_offset_t bit,
8286 : : unsigned HOST_WIDE_INT val)
8287 : : {
8288 : 256 : const svalue *inner_svalue
8289 : 256 : = mgr->get_or_create_int_cst (unsigned_type_node, val);
8290 : 256 : return mgr->get_or_create_bits_within (boolean_type_node,
8291 : 256 : bit_range (bit, 1),
8292 : 256 : inner_svalue);
8293 : : }
8294 : :
8295 : : /* Verify that bits_within_svalues are folded as expected. */
8296 : :
8297 : : static void
8298 : 4 : test_bits_within_svalue_folding ()
8299 : : {
8300 : 4 : region_model_manager mgr;
8301 : :
8302 : 4 : const svalue *zero = mgr.get_or_create_int_cst (boolean_type_node, 0);
8303 : 4 : const svalue *one = mgr.get_or_create_int_cst (boolean_type_node, 1);
8304 : :
8305 : 4 : {
8306 : 4 : const unsigned val = 0x0000;
8307 : 68 : for (unsigned bit = 0; bit < 16; bit++)
8308 : 64 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8309 : : }
8310 : :
8311 : 4 : {
8312 : 4 : const unsigned val = 0x0001;
8313 : 4 : ASSERT_EQ (get_bit (&mgr, 0, val), one);
8314 : 64 : for (unsigned bit = 1; bit < 16; bit++)
8315 : 60 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8316 : : }
8317 : :
8318 : 4 : {
8319 : 4 : const unsigned val = 0x8000;
8320 : 64 : for (unsigned bit = 0; bit < 15; bit++)
8321 : 60 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8322 : 4 : ASSERT_EQ (get_bit (&mgr, 15, val), one);
8323 : : }
8324 : :
8325 : 4 : {
8326 : 4 : const unsigned val = 0xFFFF;
8327 : 68 : for (unsigned bit = 0; bit < 16; bit++)
8328 : 64 : ASSERT_EQ (get_bit (&mgr, bit, val), one);
8329 : : }
8330 : 4 : }
8331 : :
8332 : : /* Test that region::descendent_of_p works as expected. */
8333 : :
8334 : : static void
8335 : 4 : test_descendent_of_p ()
8336 : : {
8337 : 4 : region_model_manager mgr;
8338 : 4 : const region *stack = mgr.get_stack_region ();
8339 : 4 : const region *heap = mgr.get_heap_region ();
8340 : 4 : const region *code = mgr.get_code_region ();
8341 : 4 : const region *globals = mgr.get_globals_region ();
8342 : :
8343 : : /* descendent_of_p should return true when used on the region itself. */
8344 : 4 : ASSERT_TRUE (stack->descendent_of_p (stack));
8345 : 4 : ASSERT_FALSE (stack->descendent_of_p (heap));
8346 : 4 : ASSERT_FALSE (stack->descendent_of_p (code));
8347 : 4 : ASSERT_FALSE (stack->descendent_of_p (globals));
8348 : :
8349 : 4 : tree x = build_global_decl ("x", integer_type_node);
8350 : 4 : const region *x_reg = mgr.get_region_for_global (x);
8351 : 4 : ASSERT_TRUE (x_reg->descendent_of_p (globals));
8352 : :
8353 : : /* A cast_region should be a descendent of the original region. */
8354 : 4 : const region *cast_reg = mgr.get_cast_region (x_reg, ptr_type_node);
8355 : 4 : ASSERT_TRUE (cast_reg->descendent_of_p (x_reg));
8356 : 4 : }
8357 : :
8358 : : /* Verify that bit_range_region works as expected. */
8359 : :
8360 : : static void
8361 : 4 : test_bit_range_regions ()
8362 : : {
8363 : 4 : tree x = build_global_decl ("x", integer_type_node);
8364 : 4 : region_model_manager mgr;
8365 : 4 : const region *x_reg = mgr.get_region_for_global (x);
8366 : 4 : const region *byte0
8367 : 4 : = mgr.get_bit_range (x_reg, char_type_node, bit_range (0, 8));
8368 : 4 : const region *byte1
8369 : 4 : = mgr.get_bit_range (x_reg, char_type_node, bit_range (8, 8));
8370 : 4 : ASSERT_TRUE (byte0->descendent_of_p (x_reg));
8371 : 4 : ASSERT_TRUE (byte1->descendent_of_p (x_reg));
8372 : 4 : ASSERT_NE (byte0, byte1);
8373 : 4 : }
8374 : :
8375 : : /* Verify that simple assignments work as expected. */
8376 : :
8377 : : static void
8378 : 4 : test_assignment ()
8379 : : {
8380 : 4 : tree int_0 = integer_zero_node;
8381 : 4 : tree x = build_global_decl ("x", integer_type_node);
8382 : 4 : tree y = build_global_decl ("y", integer_type_node);
8383 : :
8384 : : /* "x == 0", then use of y, then "y = 0;". */
8385 : 4 : region_model_manager mgr;
8386 : 4 : region_model model (&mgr);
8387 : 4 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
8388 : 4 : ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, int_0);
8389 : 4 : model.set_value (model.get_lvalue (y, nullptr),
8390 : : model.get_rvalue (int_0, nullptr),
8391 : : nullptr);
8392 : 4 : ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, int_0);
8393 : 4 : ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, x);
8394 : 4 : }
8395 : :
8396 : : /* Verify that compound assignments work as expected. */
8397 : :
8398 : : static void
8399 : 4 : test_compound_assignment ()
8400 : : {
8401 : 4 : coord_test ct;
8402 : :
8403 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
8404 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8405 : : c, ct.m_x_field, NULL_TREE);
8406 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
8407 : : c, ct.m_y_field, NULL_TREE);
8408 : 4 : tree d = build_global_decl ("d", ct.m_coord_type);
8409 : 4 : tree d_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8410 : : d, ct.m_x_field, NULL_TREE);
8411 : 4 : tree d_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
8412 : : d, ct.m_y_field, NULL_TREE);
8413 : :
8414 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
8415 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
8416 : :
8417 : 4 : region_model_manager mgr;
8418 : 4 : region_model model (&mgr);
8419 : 4 : model.set_value (c_x, int_17, nullptr);
8420 : 4 : model.set_value (c_y, int_m3, nullptr);
8421 : :
8422 : : /* Copy c to d. */
8423 : 4 : const svalue *sval = model.get_rvalue (c, nullptr);
8424 : 4 : model.set_value (model.get_lvalue (d, nullptr), sval, nullptr);
8425 : :
8426 : : /* Check that the fields have the same svalues. */
8427 : 4 : ASSERT_EQ (model.get_rvalue (c_x, nullptr), model.get_rvalue (d_x, nullptr));
8428 : 4 : ASSERT_EQ (model.get_rvalue (c_y, nullptr), model.get_rvalue (d_y, nullptr));
8429 : 4 : }
8430 : :
8431 : : /* Verify the details of pushing and popping stack frames. */
8432 : :
8433 : : static void
8434 : 4 : test_stack_frames ()
8435 : : {
8436 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8437 : 4 : tree int_10 = build_int_cst (integer_type_node, 10);
8438 : 4 : tree int_5 = build_int_cst (integer_type_node, 5);
8439 : 4 : tree int_0 = integer_zero_node;
8440 : :
8441 : 4 : auto_vec <tree> param_types;
8442 : 4 : tree parent_fndecl = make_fndecl (integer_type_node,
8443 : : "parent_fn",
8444 : : param_types);
8445 : 4 : allocate_struct_function (parent_fndecl, true);
8446 : :
8447 : 4 : tree child_fndecl = make_fndecl (integer_type_node,
8448 : : "child_fn",
8449 : : param_types);
8450 : 4 : allocate_struct_function (child_fndecl, true);
8451 : :
8452 : : /* "a" and "b" in the parent frame. */
8453 : 4 : tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8454 : : get_identifier ("a"),
8455 : : integer_type_node);
8456 : 4 : DECL_CONTEXT (a) = parent_fndecl;
8457 : 4 : tree b = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8458 : : get_identifier ("b"),
8459 : : integer_type_node);
8460 : 4 : DECL_CONTEXT (b) = parent_fndecl;
8461 : : /* "x" and "y" in a child frame. */
8462 : 4 : tree x = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8463 : : get_identifier ("x"),
8464 : : integer_type_node);
8465 : 4 : DECL_CONTEXT (x) = child_fndecl;
8466 : 4 : tree y = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8467 : : get_identifier ("y"),
8468 : : integer_type_node);
8469 : 4 : DECL_CONTEXT (y) = child_fndecl;
8470 : :
8471 : : /* "p" global. */
8472 : 4 : tree p = build_global_decl ("p", ptr_type_node);
8473 : :
8474 : : /* "q" global. */
8475 : 4 : tree q = build_global_decl ("q", ptr_type_node);
8476 : :
8477 : 4 : region_model_manager mgr;
8478 : 4 : test_region_model_context ctxt;
8479 : 4 : region_model model (&mgr);
8480 : :
8481 : : /* Push stack frame for "parent_fn". */
8482 : 4 : const region *parent_frame_reg
8483 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (parent_fndecl),
8484 : : nullptr, nullptr, &ctxt);
8485 : 4 : ASSERT_EQ (model.get_current_frame (), parent_frame_reg);
8486 : 4 : ASSERT_TRUE (model.region_exists_p (parent_frame_reg));
8487 : 4 : const region *a_in_parent_reg = model.get_lvalue (a, &ctxt);
8488 : 4 : model.set_value (a_in_parent_reg,
8489 : : model.get_rvalue (int_42, &ctxt),
8490 : : &ctxt);
8491 : 4 : ASSERT_EQ (a_in_parent_reg->maybe_get_frame_region (), parent_frame_reg);
8492 : :
8493 : 4 : model.add_constraint (b, LT_EXPR, int_10, &ctxt);
8494 : 4 : ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
8495 : : tristate (tristate::TS_TRUE));
8496 : :
8497 : : /* Push stack frame for "child_fn". */
8498 : 4 : const region *child_frame_reg
8499 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (child_fndecl),
8500 : : nullptr, nullptr, &ctxt);
8501 : 4 : ASSERT_EQ (model.get_current_frame (), child_frame_reg);
8502 : 4 : ASSERT_TRUE (model.region_exists_p (child_frame_reg));
8503 : 4 : const region *x_in_child_reg = model.get_lvalue (x, &ctxt);
8504 : 4 : model.set_value (x_in_child_reg,
8505 : : model.get_rvalue (int_0, &ctxt),
8506 : : &ctxt);
8507 : 4 : ASSERT_EQ (x_in_child_reg->maybe_get_frame_region (), child_frame_reg);
8508 : :
8509 : 4 : model.add_constraint (y, NE_EXPR, int_5, &ctxt);
8510 : 4 : ASSERT_EQ (model.eval_condition (y, NE_EXPR, int_5, &ctxt),
8511 : : tristate (tristate::TS_TRUE));
8512 : :
8513 : : /* Point a global pointer at a local in the child frame: p = &x. */
8514 : 4 : const region *p_in_globals_reg = model.get_lvalue (p, &ctxt);
8515 : 4 : model.set_value (p_in_globals_reg,
8516 : : mgr.get_ptr_svalue (ptr_type_node, x_in_child_reg),
8517 : : &ctxt);
8518 : 4 : ASSERT_EQ (p_in_globals_reg->maybe_get_frame_region (), nullptr);
8519 : :
8520 : : /* Point another global pointer at p: q = &p. */
8521 : 4 : const region *q_in_globals_reg = model.get_lvalue (q, &ctxt);
8522 : 4 : model.set_value (q_in_globals_reg,
8523 : : mgr.get_ptr_svalue (ptr_type_node, p_in_globals_reg),
8524 : : &ctxt);
8525 : :
8526 : : /* Test region::descendent_of_p. */
8527 : 4 : ASSERT_TRUE (child_frame_reg->descendent_of_p (child_frame_reg));
8528 : 4 : ASSERT_TRUE (x_in_child_reg->descendent_of_p (child_frame_reg));
8529 : 4 : ASSERT_FALSE (a_in_parent_reg->descendent_of_p (child_frame_reg));
8530 : :
8531 : : /* Pop the "child_fn" frame from the stack. */
8532 : 4 : model.pop_frame (nullptr, nullptr, &ctxt, nullptr);
8533 : 4 : ASSERT_FALSE (model.region_exists_p (child_frame_reg));
8534 : 4 : ASSERT_TRUE (model.region_exists_p (parent_frame_reg));
8535 : :
8536 : : /* Verify that p (which was pointing at the local "x" in the popped
8537 : : frame) has been poisoned. */
8538 : 4 : const svalue *new_p_sval = model.get_rvalue (p, nullptr);
8539 : 4 : ASSERT_EQ (new_p_sval->get_kind (), SK_POISONED);
8540 : 4 : ASSERT_EQ (new_p_sval->dyn_cast_poisoned_svalue ()->get_poison_kind (),
8541 : : poison_kind::popped_stack);
8542 : :
8543 : : /* Verify that q still points to p, in spite of the region
8544 : : renumbering. */
8545 : 4 : const svalue *new_q_sval = model.get_rvalue (q, &ctxt);
8546 : 4 : ASSERT_EQ (new_q_sval->get_kind (), SK_REGION);
8547 : 4 : ASSERT_EQ (new_q_sval->maybe_get_region (),
8548 : : model.get_lvalue (p, &ctxt));
8549 : :
8550 : : /* Verify that top of stack has been updated. */
8551 : 4 : ASSERT_EQ (model.get_current_frame (), parent_frame_reg);
8552 : :
8553 : : /* Verify locals in parent frame. */
8554 : : /* Verify "a" still has its value. */
8555 : 4 : const svalue *new_a_sval = model.get_rvalue (a, &ctxt);
8556 : 4 : ASSERT_EQ (new_a_sval->get_kind (), SK_CONSTANT);
8557 : 4 : ASSERT_EQ (new_a_sval->dyn_cast_constant_svalue ()->get_constant (),
8558 : : int_42);
8559 : : /* Verify "b" still has its constraint. */
8560 : 4 : ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
8561 : : tristate (tristate::TS_TRUE));
8562 : 4 : }
8563 : :
8564 : : /* Verify that get_representative_path_var works as expected, that
8565 : : we can map from regions to parms and back within a recursive call
8566 : : stack. */
8567 : :
8568 : : static void
8569 : 4 : test_get_representative_path_var ()
8570 : : {
8571 : 4 : auto_vec <tree> param_types;
8572 : 4 : tree fndecl = make_fndecl (integer_type_node,
8573 : : "factorial",
8574 : : param_types);
8575 : 4 : allocate_struct_function (fndecl, true);
8576 : :
8577 : : /* Parm "n". */
8578 : 4 : tree n = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8579 : : get_identifier ("n"),
8580 : : integer_type_node);
8581 : 4 : DECL_CONTEXT (n) = fndecl;
8582 : :
8583 : 4 : region_model_manager mgr;
8584 : 4 : test_region_model_context ctxt;
8585 : 4 : region_model model (&mgr);
8586 : :
8587 : : /* Push 5 stack frames for "factorial", each with a param */
8588 : 4 : auto_vec<const region *> parm_regs;
8589 : 4 : auto_vec<const svalue *> parm_svals;
8590 : 24 : for (int depth = 0; depth < 5; depth++)
8591 : : {
8592 : 20 : const region *frame_n_reg
8593 : 20 : = model.push_frame (*DECL_STRUCT_FUNCTION (fndecl),
8594 : : nullptr, nullptr, &ctxt);
8595 : 20 : const region *parm_n_reg = model.get_lvalue (path_var (n, depth), &ctxt);
8596 : 20 : parm_regs.safe_push (parm_n_reg);
8597 : :
8598 : 20 : ASSERT_EQ (parm_n_reg->get_parent_region (), frame_n_reg);
8599 : 20 : const svalue *sval_n = mgr.get_or_create_initial_value (parm_n_reg);
8600 : 20 : parm_svals.safe_push (sval_n);
8601 : : }
8602 : :
8603 : : /* Verify that we can recognize that the regions are the parms,
8604 : : at every depth. */
8605 : 24 : for (int depth = 0; depth < 5; depth++)
8606 : : {
8607 : 20 : {
8608 : 20 : svalue_set visited;
8609 : 40 : ASSERT_EQ (model.get_representative_path_var (parm_regs[depth],
8610 : : &visited,
8611 : : nullptr),
8612 : : path_var (n, depth + 1));
8613 : 20 : }
8614 : : /* ...and that we can lookup lvalues for locals for all frames,
8615 : : not just the top. */
8616 : 20 : ASSERT_EQ (model.get_lvalue (path_var (n, depth), nullptr),
8617 : : parm_regs[depth]);
8618 : : /* ...and that we can locate the svalues. */
8619 : 20 : {
8620 : 20 : svalue_set visited;
8621 : 40 : ASSERT_EQ (model.get_representative_path_var (parm_svals[depth],
8622 : : &visited,
8623 : : nullptr),
8624 : : path_var (n, depth + 1));
8625 : 20 : }
8626 : : }
8627 : 4 : }
8628 : :
8629 : : /* Ensure that region_model::operator== works as expected. */
8630 : :
8631 : : static void
8632 : 4 : test_equality_1 ()
8633 : : {
8634 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8635 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
8636 : :
8637 : : /* Verify that "empty" region_model instances are equal to each other. */
8638 : 4 : region_model_manager mgr;
8639 : 4 : region_model model0 (&mgr);
8640 : 4 : region_model model1 (&mgr);
8641 : 4 : ASSERT_EQ (model0, model1);
8642 : :
8643 : : /* Verify that setting state in model1 makes the models non-equal. */
8644 : 4 : tree x = build_global_decl ("x", integer_type_node);
8645 : 4 : model0.set_value (x, int_42, nullptr);
8646 : 4 : ASSERT_EQ (model0.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8647 : 4 : ASSERT_NE (model0, model1);
8648 : :
8649 : : /* Verify the copy-ctor. */
8650 : 4 : region_model model2 (model0);
8651 : 4 : ASSERT_EQ (model0, model2);
8652 : 4 : ASSERT_EQ (model2.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8653 : 4 : ASSERT_NE (model1, model2);
8654 : :
8655 : : /* Verify that models obtained from copy-ctor are independently editable
8656 : : w/o affecting the original model. */
8657 : 4 : model2.set_value (x, int_17, nullptr);
8658 : 4 : ASSERT_NE (model0, model2);
8659 : 4 : ASSERT_EQ (model2.get_rvalue (x, nullptr)->maybe_get_constant (), int_17);
8660 : 4 : ASSERT_EQ (model0.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8661 : 4 : }
8662 : :
8663 : : /* Verify that region models for
8664 : : x = 42; y = 113;
8665 : : and
8666 : : y = 113; x = 42;
8667 : : are equal. */
8668 : :
8669 : : static void
8670 : 4 : test_canonicalization_2 ()
8671 : : {
8672 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8673 : 4 : tree int_113 = build_int_cst (integer_type_node, 113);
8674 : 4 : tree x = build_global_decl ("x", integer_type_node);
8675 : 4 : tree y = build_global_decl ("y", integer_type_node);
8676 : :
8677 : 4 : region_model_manager mgr;
8678 : 4 : region_model model0 (&mgr);
8679 : 4 : model0.set_value (model0.get_lvalue (x, nullptr),
8680 : : model0.get_rvalue (int_42, nullptr),
8681 : : nullptr);
8682 : 4 : model0.set_value (model0.get_lvalue (y, nullptr),
8683 : : model0.get_rvalue (int_113, nullptr),
8684 : : nullptr);
8685 : :
8686 : 4 : region_model model1 (&mgr);
8687 : 4 : model1.set_value (model1.get_lvalue (y, nullptr),
8688 : : model1.get_rvalue (int_113, nullptr),
8689 : : nullptr);
8690 : 4 : model1.set_value (model1.get_lvalue (x, nullptr),
8691 : : model1.get_rvalue (int_42, nullptr),
8692 : : nullptr);
8693 : :
8694 : 4 : ASSERT_EQ (model0, model1);
8695 : 4 : }
8696 : :
8697 : : /* Verify that constraints for
8698 : : x > 3 && y > 42
8699 : : and
8700 : : y > 42 && x > 3
8701 : : are equal after canonicalization. */
8702 : :
8703 : : static void
8704 : 4 : test_canonicalization_3 ()
8705 : : {
8706 : 4 : tree int_3 = build_int_cst (integer_type_node, 3);
8707 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8708 : 4 : tree x = build_global_decl ("x", integer_type_node);
8709 : 4 : tree y = build_global_decl ("y", integer_type_node);
8710 : :
8711 : 4 : region_model_manager mgr;
8712 : 4 : region_model model0 (&mgr);
8713 : 4 : model0.add_constraint (x, GT_EXPR, int_3, nullptr);
8714 : 4 : model0.add_constraint (y, GT_EXPR, int_42, nullptr);
8715 : :
8716 : 4 : region_model model1 (&mgr);
8717 : 4 : model1.add_constraint (y, GT_EXPR, int_42, nullptr);
8718 : 4 : model1.add_constraint (x, GT_EXPR, int_3, nullptr);
8719 : :
8720 : 4 : model0.canonicalize ();
8721 : 4 : model1.canonicalize ();
8722 : 4 : ASSERT_EQ (model0, model1);
8723 : 4 : }
8724 : :
8725 : : /* Verify that we can canonicalize a model containing NaN and other real
8726 : : constants. */
8727 : :
8728 : : static void
8729 : 4 : test_canonicalization_4 ()
8730 : : {
8731 : 4 : auto_vec<tree> csts;
8732 : 4 : append_interesting_constants (&csts);
8733 : :
8734 : 4 : region_model_manager mgr;
8735 : 4 : region_model model (&mgr);
8736 : :
8737 : 60 : for (tree cst : csts)
8738 : 48 : model.get_rvalue (cst, nullptr);
8739 : :
8740 : 4 : model.canonicalize ();
8741 : 4 : }
8742 : :
8743 : : /* Assert that if we have two region_model instances
8744 : : with values VAL_A and VAL_B for EXPR that they are
8745 : : mergable. Write the merged model to *OUT_MERGED_MODEL,
8746 : : and the merged svalue ptr to *OUT_MERGED_SVALUE.
8747 : : If VAL_A or VAL_B are nullptr_TREE, don't populate EXPR
8748 : : for that region_model. */
8749 : :
8750 : : static void
8751 : 20 : assert_region_models_merge (tree expr, tree val_a, tree val_b,
8752 : : region_model *out_merged_model,
8753 : : const svalue **out_merged_svalue)
8754 : : {
8755 : 20 : region_model_manager *mgr = out_merged_model->get_manager ();
8756 : 20 : program_point point (program_point::origin (*mgr));
8757 : 20 : test_region_model_context ctxt;
8758 : 20 : region_model model0 (mgr);
8759 : 20 : region_model model1 (mgr);
8760 : 20 : if (val_a)
8761 : 16 : model0.set_value (model0.get_lvalue (expr, &ctxt),
8762 : : model0.get_rvalue (val_a, &ctxt),
8763 : : &ctxt);
8764 : 20 : if (val_b)
8765 : 16 : model1.set_value (model1.get_lvalue (expr, &ctxt),
8766 : : model1.get_rvalue (val_b, &ctxt),
8767 : : &ctxt);
8768 : :
8769 : : /* They should be mergeable. */
8770 : 20 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, out_merged_model));
8771 : 20 : *out_merged_svalue = out_merged_model->get_rvalue (expr, &ctxt);
8772 : 20 : }
8773 : :
8774 : : /* Verify that we can merge region_model instances. */
8775 : :
8776 : : static void
8777 : 4 : test_state_merging ()
8778 : : {
8779 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8780 : 4 : tree int_113 = build_int_cst (integer_type_node, 113);
8781 : 4 : tree x = build_global_decl ("x", integer_type_node);
8782 : 4 : tree y = build_global_decl ("y", integer_type_node);
8783 : 4 : tree z = build_global_decl ("z", integer_type_node);
8784 : 4 : tree p = build_global_decl ("p", ptr_type_node);
8785 : :
8786 : 4 : tree addr_of_y = build1 (ADDR_EXPR, ptr_type_node, y);
8787 : 4 : tree addr_of_z = build1 (ADDR_EXPR, ptr_type_node, z);
8788 : :
8789 : 4 : auto_vec <tree> param_types;
8790 : 4 : tree test_fndecl = make_fndecl (integer_type_node, "test_fn", param_types);
8791 : 4 : allocate_struct_function (test_fndecl, true);
8792 : :
8793 : : /* Param "a". */
8794 : 4 : tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8795 : : get_identifier ("a"),
8796 : : integer_type_node);
8797 : 4 : DECL_CONTEXT (a) = test_fndecl;
8798 : 4 : tree addr_of_a = build1 (ADDR_EXPR, ptr_type_node, a);
8799 : :
8800 : : /* Param "q", a pointer. */
8801 : 4 : tree q = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8802 : : get_identifier ("q"),
8803 : : ptr_type_node);
8804 : 4 : DECL_CONTEXT (q) = test_fndecl;
8805 : :
8806 : 4 : region_model_manager mgr;
8807 : 4 : program_point point (program_point::origin (mgr));
8808 : :
8809 : 4 : {
8810 : 4 : region_model model0 (&mgr);
8811 : 4 : region_model model1 (&mgr);
8812 : 4 : region_model merged (&mgr);
8813 : : /* Verify empty models can be merged. */
8814 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8815 : 4 : ASSERT_EQ (model0, merged);
8816 : 4 : }
8817 : :
8818 : : /* Verify that we can merge two contradictory constraints on the
8819 : : value for a global. */
8820 : : /* TODO: verify that the merged model doesn't have a value for
8821 : : the global */
8822 : 4 : {
8823 : 4 : region_model model0 (&mgr);
8824 : 4 : region_model model1 (&mgr);
8825 : 4 : region_model merged (&mgr);
8826 : 4 : test_region_model_context ctxt;
8827 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
8828 : 4 : model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
8829 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8830 : 4 : ASSERT_NE (model0, merged);
8831 : 4 : ASSERT_NE (model1, merged);
8832 : 4 : }
8833 : :
8834 : : /* Verify handling of a PARM_DECL. */
8835 : 4 : {
8836 : 4 : test_region_model_context ctxt;
8837 : 4 : region_model model0 (&mgr);
8838 : 4 : region_model model1 (&mgr);
8839 : 4 : ASSERT_EQ (model0.get_stack_depth (), 0);
8840 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8841 : : nullptr, nullptr, &ctxt);
8842 : 4 : ASSERT_EQ (model0.get_stack_depth (), 1);
8843 : 4 : model1.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8844 : : nullptr, nullptr, &ctxt);
8845 : :
8846 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
8847 : 4 : integer_type_node, "test sval");
8848 : 4 : model0.set_value (model0.get_lvalue (a, &ctxt), &test_sval, &ctxt);
8849 : 4 : model1.set_value (model1.get_lvalue (a, &ctxt), &test_sval, &ctxt);
8850 : 4 : ASSERT_EQ (model0, model1);
8851 : :
8852 : : /* They should be mergeable, and the result should be the same. */
8853 : 4 : region_model merged (&mgr);
8854 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8855 : 4 : ASSERT_EQ (model0, merged);
8856 : : /* In particular, "a" should have the placeholder value. */
8857 : 4 : ASSERT_EQ (merged.get_rvalue (a, &ctxt), &test_sval);
8858 : 4 : }
8859 : :
8860 : : /* Verify handling of a global. */
8861 : 4 : {
8862 : 4 : test_region_model_context ctxt;
8863 : 4 : region_model model0 (&mgr);
8864 : 4 : region_model model1 (&mgr);
8865 : :
8866 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
8867 : 4 : integer_type_node, "test sval");
8868 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt), &test_sval, &ctxt);
8869 : 4 : model1.set_value (model1.get_lvalue (x, &ctxt), &test_sval, &ctxt);
8870 : 4 : ASSERT_EQ (model0, model1);
8871 : :
8872 : : /* They should be mergeable, and the result should be the same. */
8873 : 4 : region_model merged (&mgr);
8874 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8875 : 4 : ASSERT_EQ (model0, merged);
8876 : : /* In particular, "x" should have the placeholder value. */
8877 : 4 : ASSERT_EQ (merged.get_rvalue (x, &ctxt), &test_sval);
8878 : 4 : }
8879 : :
8880 : : /* Use global-handling to verify various combinations of values. */
8881 : :
8882 : : /* Two equal constant values. */
8883 : 4 : {
8884 : 4 : region_model merged (&mgr);
8885 : 4 : const svalue *merged_x_sval;
8886 : 4 : assert_region_models_merge (x, int_42, int_42, &merged, &merged_x_sval);
8887 : :
8888 : : /* In particular, there should be a constant value for "x". */
8889 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_CONSTANT);
8890 : 4 : ASSERT_EQ (merged_x_sval->dyn_cast_constant_svalue ()->get_constant (),
8891 : : int_42);
8892 : 4 : }
8893 : :
8894 : : /* Two non-equal constant values. */
8895 : 4 : {
8896 : 4 : region_model merged (&mgr);
8897 : 4 : const svalue *merged_x_sval;
8898 : 4 : assert_region_models_merge (x, int_42, int_113, &merged, &merged_x_sval);
8899 : :
8900 : : /* In particular, there should be a "widening" value for "x". */
8901 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_WIDENING);
8902 : 4 : }
8903 : :
8904 : : /* Initial and constant. */
8905 : 4 : {
8906 : 4 : region_model merged (&mgr);
8907 : 4 : const svalue *merged_x_sval;
8908 : 4 : assert_region_models_merge (x, NULL_TREE, int_113, &merged, &merged_x_sval);
8909 : :
8910 : : /* In particular, there should be an unknown value for "x". */
8911 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
8912 : 4 : }
8913 : :
8914 : : /* Constant and initial. */
8915 : 4 : {
8916 : 4 : region_model merged (&mgr);
8917 : 4 : const svalue *merged_x_sval;
8918 : 4 : assert_region_models_merge (x, int_42, NULL_TREE, &merged, &merged_x_sval);
8919 : :
8920 : : /* In particular, there should be an unknown value for "x". */
8921 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
8922 : 4 : }
8923 : :
8924 : : /* Unknown and constant. */
8925 : : // TODO
8926 : :
8927 : : /* Pointers: NULL and NULL. */
8928 : : // TODO
8929 : :
8930 : : /* Pointers: NULL and non-NULL. */
8931 : : // TODO
8932 : :
8933 : : /* Pointers: non-NULL and non-NULL: ptr to a local. */
8934 : 4 : {
8935 : 4 : region_model model0 (&mgr);
8936 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8937 : : nullptr, nullptr, nullptr);
8938 : 4 : model0.set_value (model0.get_lvalue (p, nullptr),
8939 : : model0.get_rvalue (addr_of_a, nullptr), nullptr);
8940 : :
8941 : 4 : region_model model1 (model0);
8942 : 4 : ASSERT_EQ (model0, model1);
8943 : :
8944 : : /* They should be mergeable, and the result should be the same. */
8945 : 4 : region_model merged (&mgr);
8946 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8947 : 4 : ASSERT_EQ (model0, merged);
8948 : 4 : }
8949 : :
8950 : : /* Pointers: non-NULL and non-NULL: ptr to a global. */
8951 : 4 : {
8952 : 4 : region_model merged (&mgr);
8953 : : /* p == &y in both input models. */
8954 : 4 : const svalue *merged_p_sval;
8955 : 4 : assert_region_models_merge (p, addr_of_y, addr_of_y, &merged,
8956 : : &merged_p_sval);
8957 : :
8958 : : /* We should get p == &y in the merged model. */
8959 : 4 : ASSERT_EQ (merged_p_sval->get_kind (), SK_REGION);
8960 : 4 : const region_svalue *merged_p_ptr
8961 : 4 : = merged_p_sval->dyn_cast_region_svalue ();
8962 : 4 : const region *merged_p_star_reg = merged_p_ptr->get_pointee ();
8963 : 4 : ASSERT_EQ (merged_p_star_reg, merged.get_lvalue (y, nullptr));
8964 : 4 : }
8965 : :
8966 : : /* Pointers: non-NULL ptrs to different globals should not merge;
8967 : : see e.g. gcc.dg/analyzer/torture/uninit-pr108725.c */
8968 : 4 : {
8969 : 4 : region_model merged_model (&mgr);
8970 : 4 : program_point point (program_point::origin (mgr));
8971 : 4 : test_region_model_context ctxt;
8972 : : /* x == &y vs x == &z in the input models; these are actually casts
8973 : : of the ptrs to "int". */
8974 : 4 : region_model model0 (&mgr);
8975 : 4 : region_model model1 (&mgr);
8976 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt),
8977 : : model0.get_rvalue (addr_of_y, &ctxt),
8978 : : &ctxt);
8979 : 4 : model1.set_value (model1.get_lvalue (x, &ctxt),
8980 : : model1.get_rvalue (addr_of_z, &ctxt),
8981 : : &ctxt);
8982 : : /* They should not be mergeable. */
8983 : 4 : ASSERT_FALSE (model0.can_merge_with_p (model1, point, &merged_model));
8984 : 4 : }
8985 : :
8986 : : /* Pointers: non-NULL and non-NULL: ptr to a heap region. */
8987 : 4 : {
8988 : 4 : test_region_model_context ctxt;
8989 : 4 : region_model model0 (&mgr);
8990 : 4 : tree size = build_int_cst (size_type_node, 1024);
8991 : 4 : const svalue *size_sval = mgr.get_or_create_constant_svalue (size);
8992 : 4 : const region *new_reg
8993 : 4 : = model0.get_or_create_region_for_heap_alloc (size_sval, &ctxt);
8994 : 4 : const svalue *ptr_sval = mgr.get_ptr_svalue (ptr_type_node, new_reg);
8995 : 4 : model0.set_value (model0.get_lvalue (p, &ctxt),
8996 : : ptr_sval, &ctxt);
8997 : :
8998 : 4 : region_model model1 (model0);
8999 : :
9000 : 4 : ASSERT_EQ (model0, model1);
9001 : :
9002 : 4 : region_model merged (&mgr);
9003 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9004 : :
9005 : : /* The merged model ought to be identical. */
9006 : 4 : ASSERT_EQ (model0, merged);
9007 : 4 : }
9008 : :
9009 : : /* Two regions sharing the same placeholder svalue should continue sharing
9010 : : it after self-merger. */
9011 : 4 : {
9012 : 4 : test_region_model_context ctxt;
9013 : 4 : region_model model0 (&mgr);
9014 : 4 : placeholder_svalue placeholder_sval (mgr.alloc_symbol_id (),
9015 : 4 : integer_type_node, "test");
9016 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt),
9017 : : &placeholder_sval, &ctxt);
9018 : 4 : model0.set_value (model0.get_lvalue (y, &ctxt), &placeholder_sval, &ctxt);
9019 : 4 : region_model model1 (model0);
9020 : :
9021 : : /* They should be mergeable, and the result should be the same. */
9022 : 4 : region_model merged (&mgr);
9023 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9024 : 4 : ASSERT_EQ (model0, merged);
9025 : :
9026 : : /* In particular, we should have x == y. */
9027 : 4 : ASSERT_EQ (merged.eval_condition (x, EQ_EXPR, y, &ctxt),
9028 : : tristate (tristate::TS_TRUE));
9029 : 4 : }
9030 : :
9031 : 4 : {
9032 : 4 : region_model model0 (&mgr);
9033 : 4 : region_model model1 (&mgr);
9034 : 4 : test_region_model_context ctxt;
9035 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
9036 : 4 : model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
9037 : 4 : region_model merged (&mgr);
9038 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9039 : 4 : }
9040 : :
9041 : 4 : {
9042 : 4 : region_model model0 (&mgr);
9043 : 4 : region_model model1 (&mgr);
9044 : 4 : test_region_model_context ctxt;
9045 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
9046 : 4 : model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
9047 : 4 : model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
9048 : 4 : region_model merged (&mgr);
9049 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9050 : 4 : }
9051 : :
9052 : : // TODO: what can't we merge? need at least one such test
9053 : :
9054 : : /* TODO: various things
9055 : : - heap regions
9056 : : - value merging:
9057 : : - every combination, but in particular
9058 : : - pairs of regions
9059 : : */
9060 : :
9061 : : /* Views. */
9062 : 4 : {
9063 : 4 : test_region_model_context ctxt;
9064 : 4 : region_model model0 (&mgr);
9065 : :
9066 : 4 : const region *x_reg = model0.get_lvalue (x, &ctxt);
9067 : 4 : const region *x_as_ptr = mgr.get_cast_region (x_reg, ptr_type_node);
9068 : 4 : model0.set_value (x_as_ptr, model0.get_rvalue (addr_of_y, &ctxt), &ctxt);
9069 : :
9070 : 4 : region_model model1 (model0);
9071 : 4 : ASSERT_EQ (model1, model0);
9072 : :
9073 : : /* They should be mergeable, and the result should be the same. */
9074 : 4 : region_model merged (&mgr);
9075 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9076 : 4 : }
9077 : :
9078 : : /* Verify that we can merge a model in which a local in an older stack
9079 : : frame points to a local in a more recent stack frame. */
9080 : 4 : {
9081 : 4 : region_model model0 (&mgr);
9082 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9083 : : nullptr, nullptr, nullptr);
9084 : 4 : const region *q_in_first_frame = model0.get_lvalue (q, nullptr);
9085 : :
9086 : : /* Push a second frame. */
9087 : 4 : const region *reg_2nd_frame
9088 : 4 : = model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9089 : : nullptr, nullptr, nullptr);
9090 : :
9091 : : /* Have a pointer in the older frame point to a local in the
9092 : : more recent frame. */
9093 : 4 : const svalue *sval_ptr = model0.get_rvalue (addr_of_a, nullptr);
9094 : 4 : model0.set_value (q_in_first_frame, sval_ptr, nullptr);
9095 : :
9096 : : /* Verify that it's pointing at the newer frame. */
9097 : 4 : const region *reg_pointee = sval_ptr->maybe_get_region ();
9098 : 4 : ASSERT_EQ (reg_pointee->get_parent_region (), reg_2nd_frame);
9099 : :
9100 : 4 : model0.canonicalize ();
9101 : :
9102 : 4 : region_model model1 (model0);
9103 : 4 : ASSERT_EQ (model0, model1);
9104 : :
9105 : : /* They should be mergeable, and the result should be the same
9106 : : (after canonicalization, at least). */
9107 : 4 : region_model merged (&mgr);
9108 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9109 : 4 : merged.canonicalize ();
9110 : 4 : ASSERT_EQ (model0, merged);
9111 : 4 : }
9112 : :
9113 : : /* Verify that we can merge a model in which a local points to a global. */
9114 : 4 : {
9115 : 4 : region_model model0 (&mgr);
9116 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9117 : : nullptr, nullptr, nullptr);
9118 : 4 : model0.set_value (model0.get_lvalue (q, nullptr),
9119 : : model0.get_rvalue (addr_of_y, nullptr), nullptr);
9120 : :
9121 : 4 : region_model model1 (model0);
9122 : 4 : ASSERT_EQ (model0, model1);
9123 : :
9124 : : /* They should be mergeable, and the result should be the same
9125 : : (after canonicalization, at least). */
9126 : 4 : region_model merged (&mgr);
9127 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9128 : 4 : ASSERT_EQ (model0, merged);
9129 : 4 : }
9130 : 4 : }
9131 : :
9132 : : /* Verify that constraints are correctly merged when merging region_model
9133 : : instances. */
9134 : :
9135 : : static void
9136 : 4 : test_constraint_merging ()
9137 : : {
9138 : 4 : tree int_0 = integer_zero_node;
9139 : 4 : tree int_5 = build_int_cst (integer_type_node, 5);
9140 : 4 : tree x = build_global_decl ("x", integer_type_node);
9141 : 4 : tree y = build_global_decl ("y", integer_type_node);
9142 : 4 : tree z = build_global_decl ("z", integer_type_node);
9143 : 4 : tree n = build_global_decl ("n", integer_type_node);
9144 : :
9145 : 4 : region_model_manager mgr;
9146 : 4 : test_region_model_context ctxt;
9147 : :
9148 : : /* model0: 0 <= (x == y) < n. */
9149 : 4 : region_model model0 (&mgr);
9150 : 4 : model0.add_constraint (x, EQ_EXPR, y, &ctxt);
9151 : 4 : model0.add_constraint (x, GE_EXPR, int_0, nullptr);
9152 : 4 : model0.add_constraint (x, LT_EXPR, n, nullptr);
9153 : :
9154 : : /* model1: z != 5 && (0 <= x < n). */
9155 : 4 : region_model model1 (&mgr);
9156 : 4 : model1.add_constraint (z, NE_EXPR, int_5, nullptr);
9157 : 4 : model1.add_constraint (x, GE_EXPR, int_0, nullptr);
9158 : 4 : model1.add_constraint (x, LT_EXPR, n, nullptr);
9159 : :
9160 : : /* They should be mergeable; the merged constraints should
9161 : : be: (0 <= x < n). */
9162 : 4 : program_point point (program_point::origin (mgr));
9163 : 4 : region_model merged (&mgr);
9164 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9165 : :
9166 : 4 : ASSERT_EQ (merged.eval_condition (x, GE_EXPR, int_0, &ctxt),
9167 : : tristate (tristate::TS_TRUE));
9168 : 4 : ASSERT_EQ (merged.eval_condition (x, LT_EXPR, n, &ctxt),
9169 : : tristate (tristate::TS_TRUE));
9170 : :
9171 : 4 : ASSERT_EQ (merged.eval_condition (z, NE_EXPR, int_5, &ctxt),
9172 : : tristate (tristate::TS_UNKNOWN));
9173 : 4 : ASSERT_EQ (merged.eval_condition (x, LT_EXPR, y, &ctxt),
9174 : : tristate (tristate::TS_UNKNOWN));
9175 : 4 : }
9176 : :
9177 : : /* Verify that widening_svalue::eval_condition_without_cm works as
9178 : : expected. */
9179 : :
9180 : : static void
9181 : 4 : test_widening_constraints ()
9182 : : {
9183 : 4 : region_model_manager mgr;
9184 : 4 : const supernode *snode = nullptr;
9185 : 4 : tree int_0 = integer_zero_node;
9186 : 4 : tree int_m1 = build_int_cst (integer_type_node, -1);
9187 : 4 : tree int_1 = integer_one_node;
9188 : 4 : tree int_256 = build_int_cst (integer_type_node, 256);
9189 : 4 : test_region_model_context ctxt;
9190 : 4 : const svalue *int_0_sval = mgr.get_or_create_constant_svalue (int_0);
9191 : 4 : const svalue *int_1_sval = mgr.get_or_create_constant_svalue (int_1);
9192 : 4 : const svalue *w_zero_then_one_sval
9193 : 4 : = mgr.get_or_create_widening_svalue (integer_type_node, snode,
9194 : : int_0_sval, int_1_sval);
9195 : 4 : const widening_svalue *w_zero_then_one
9196 : 4 : = w_zero_then_one_sval->dyn_cast_widening_svalue ();
9197 : 4 : ASSERT_EQ (w_zero_then_one->get_direction (),
9198 : : widening_svalue::DIR_ASCENDING);
9199 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_m1),
9200 : : tristate::TS_FALSE);
9201 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_0),
9202 : : tristate::TS_FALSE);
9203 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_1),
9204 : : tristate::TS_UNKNOWN);
9205 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_256),
9206 : : tristate::TS_UNKNOWN);
9207 : :
9208 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_m1),
9209 : : tristate::TS_FALSE);
9210 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_0),
9211 : : tristate::TS_UNKNOWN);
9212 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_1),
9213 : : tristate::TS_UNKNOWN);
9214 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_256),
9215 : : tristate::TS_UNKNOWN);
9216 : :
9217 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_m1),
9218 : : tristate::TS_TRUE);
9219 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_0),
9220 : : tristate::TS_UNKNOWN);
9221 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_1),
9222 : : tristate::TS_UNKNOWN);
9223 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_256),
9224 : : tristate::TS_UNKNOWN);
9225 : :
9226 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_m1),
9227 : : tristate::TS_TRUE);
9228 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_0),
9229 : : tristate::TS_TRUE);
9230 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_1),
9231 : : tristate::TS_UNKNOWN);
9232 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_256),
9233 : : tristate::TS_UNKNOWN);
9234 : :
9235 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_m1),
9236 : : tristate::TS_FALSE);
9237 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_0),
9238 : : tristate::TS_UNKNOWN);
9239 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_1),
9240 : : tristate::TS_UNKNOWN);
9241 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_256),
9242 : : tristate::TS_UNKNOWN);
9243 : :
9244 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_m1),
9245 : : tristate::TS_TRUE);
9246 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_0),
9247 : : tristate::TS_UNKNOWN);
9248 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_1),
9249 : : tristate::TS_UNKNOWN);
9250 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_256),
9251 : : tristate::TS_UNKNOWN);
9252 : 4 : }
9253 : :
9254 : : /* Verify merging constraints for states simulating successive iterations
9255 : : of a loop.
9256 : : Simulate:
9257 : : for (i = 0; i < 256; i++)
9258 : : [...body...]
9259 : : i.e. this gimple:.
9260 : : i_15 = 0;
9261 : : goto <bb 4>;
9262 : :
9263 : : <bb 4> :
9264 : : i_11 = PHI <i_15(2), i_23(3)>
9265 : : if (i_11 <= 255)
9266 : : goto <bb 3>;
9267 : : else
9268 : : goto [AFTER LOOP]
9269 : :
9270 : : <bb 3> :
9271 : : [LOOP BODY]
9272 : : i_23 = i_11 + 1;
9273 : :
9274 : : and thus these ops (and resultant states):
9275 : : i_11 = PHI()
9276 : : {i_11: 0}
9277 : : add_constraint (i_11 <= 255) [for the true edge]
9278 : : {i_11: 0} [constraint was a no-op]
9279 : : i_23 = i_11 + 1;
9280 : : {i_22: 1}
9281 : : i_11 = PHI()
9282 : : {i_11: WIDENED (at phi, 0, 1)}
9283 : : add_constraint (i_11 <= 255) [for the true edge]
9284 : : {i_11: WIDENED (at phi, 0, 1); WIDENED <= 255}
9285 : : i_23 = i_11 + 1;
9286 : : {i_23: (WIDENED (at phi, 0, 1) + 1); WIDENED <= 255}
9287 : : i_11 = PHI(); merge with state at phi above
9288 : : {i_11: WIDENED (at phi, 0, 1); WIDENED <= 256}
9289 : : [changing meaning of "WIDENED" here]
9290 : : if (i_11 <= 255)
9291 : : T: {i_11: WIDENED (at phi, 0, 1); WIDENED <= 255}; cache hit
9292 : : F: {i_11: 256}
9293 : : */
9294 : :
9295 : : static void
9296 : 4 : test_iteration_1 ()
9297 : : {
9298 : 4 : region_model_manager mgr;
9299 : 4 : program_point point (program_point::origin (mgr));
9300 : :
9301 : 4 : tree int_0 = integer_zero_node;
9302 : 4 : tree int_1 = integer_one_node;
9303 : 4 : tree int_256 = build_int_cst (integer_type_node, 256);
9304 : 4 : tree i = build_global_decl ("i", integer_type_node);
9305 : :
9306 : 4 : test_region_model_context ctxt;
9307 : :
9308 : : /* model0: i: 0. */
9309 : 4 : region_model model0 (&mgr);
9310 : 4 : model0.set_value (i, int_0, &ctxt);
9311 : :
9312 : : /* model1: i: 1. */
9313 : 4 : region_model model1 (&mgr);
9314 : 4 : model1.set_value (i, int_1, &ctxt);
9315 : :
9316 : : /* Should merge "i" to a widened value. */
9317 : 4 : region_model model2 (&mgr);
9318 : 4 : ASSERT_TRUE (model1.can_merge_with_p (model0, point, &model2));
9319 : 4 : const svalue *merged_i = model2.get_rvalue (i, &ctxt);
9320 : 4 : ASSERT_EQ (merged_i->get_kind (), SK_WIDENING);
9321 : 4 : const widening_svalue *w = merged_i->dyn_cast_widening_svalue ();
9322 : 4 : ASSERT_EQ (w->get_direction (), widening_svalue::DIR_ASCENDING);
9323 : :
9324 : : /* Add constraint: i < 256 */
9325 : 4 : model2.add_constraint (i, LT_EXPR, int_256, &ctxt);
9326 : 4 : ASSERT_EQ (model2.eval_condition (i, LT_EXPR, int_256, &ctxt),
9327 : : tristate (tristate::TS_TRUE));
9328 : 4 : ASSERT_EQ (model2.eval_condition (i, GE_EXPR, int_0, &ctxt),
9329 : : tristate (tristate::TS_TRUE));
9330 : :
9331 : : /* Try merging with the initial state. */
9332 : 4 : region_model model3 (&mgr);
9333 : 4 : ASSERT_TRUE (model2.can_merge_with_p (model0, point, &model3));
9334 : : /* Merging the merged value with the initial value should be idempotent,
9335 : : so that the analysis converges. */
9336 : 4 : ASSERT_EQ (model3.get_rvalue (i, &ctxt), merged_i);
9337 : : /* Merger of 0 and a widening value with constraint < CST
9338 : : should retain the constraint, even though it was implicit
9339 : : for the 0 case. */
9340 : 4 : ASSERT_EQ (model3.eval_condition (i, LT_EXPR, int_256, &ctxt),
9341 : : tristate (tristate::TS_TRUE));
9342 : : /* ...and we should have equality: the analysis should have converged. */
9343 : 4 : ASSERT_EQ (model3, model2);
9344 : :
9345 : : /* "i_23 = i_11 + 1;" */
9346 : 4 : region_model model4 (model3);
9347 : 4 : ASSERT_EQ (model4, model2);
9348 : 4 : model4.set_value (i, build2 (PLUS_EXPR, integer_type_node, i, int_1), &ctxt);
9349 : 4 : const svalue *plus_one = model4.get_rvalue (i, &ctxt);
9350 : 4 : ASSERT_EQ (plus_one->get_kind (), SK_BINOP);
9351 : :
9352 : : /* Try merging with the "i: 1" state. */
9353 : 4 : region_model model5 (&mgr);
9354 : 4 : ASSERT_TRUE (model4.can_merge_with_p (model1, point, &model5));
9355 : 4 : ASSERT_EQ (model5.get_rvalue (i, &ctxt), plus_one);
9356 : 4 : ASSERT_EQ (model5, model4);
9357 : :
9358 : : /* "i_11 = PHI();" merge with state at phi above.
9359 : : For i, we should have a merger of WIDENING with WIDENING + 1,
9360 : : and this should be WIDENING again. */
9361 : 4 : region_model model6 (&mgr);
9362 : 4 : ASSERT_TRUE (model5.can_merge_with_p (model2, point, &model6));
9363 : 4 : const svalue *merged_widening = model6.get_rvalue (i, &ctxt);
9364 : 4 : ASSERT_EQ (merged_widening->get_kind (), SK_WIDENING);
9365 : 4 : }
9366 : :
9367 : : /* Verify that if we mark a pointer to a malloc-ed region as non-NULL,
9368 : : all cast pointers to that region are also known to be non-NULL. */
9369 : :
9370 : : static void
9371 : 4 : test_malloc_constraints ()
9372 : : {
9373 : 4 : region_model_manager mgr;
9374 : 4 : region_model model (&mgr);
9375 : 4 : tree p = build_global_decl ("p", ptr_type_node);
9376 : 4 : tree char_star = build_pointer_type (char_type_node);
9377 : 4 : tree q = build_global_decl ("q", char_star);
9378 : 4 : tree null_ptr = build_int_cst (ptr_type_node, 0);
9379 : :
9380 : 4 : const svalue *size_in_bytes
9381 : 4 : = mgr.get_or_create_unknown_svalue (size_type_node);
9382 : 4 : const region *reg
9383 : 4 : = model.get_or_create_region_for_heap_alloc (size_in_bytes, nullptr);
9384 : 4 : const svalue *sval = mgr.get_ptr_svalue (ptr_type_node, reg);
9385 : 4 : model.set_value (model.get_lvalue (p, nullptr), sval, nullptr);
9386 : 4 : model.set_value (q, p, nullptr);
9387 : :
9388 : 4 : ASSERT_CONDITION_UNKNOWN (model, p, NE_EXPR, null_ptr);
9389 : 4 : ASSERT_CONDITION_UNKNOWN (model, p, EQ_EXPR, null_ptr);
9390 : 4 : ASSERT_CONDITION_UNKNOWN (model, q, NE_EXPR, null_ptr);
9391 : 4 : ASSERT_CONDITION_UNKNOWN (model, q, EQ_EXPR, null_ptr);
9392 : :
9393 : 4 : model.add_constraint (p, NE_EXPR, null_ptr, nullptr);
9394 : :
9395 : 4 : ASSERT_CONDITION_TRUE (model, p, NE_EXPR, null_ptr);
9396 : 4 : ASSERT_CONDITION_FALSE (model, p, EQ_EXPR, null_ptr);
9397 : 4 : ASSERT_CONDITION_TRUE (model, q, NE_EXPR, null_ptr);
9398 : 4 : ASSERT_CONDITION_FALSE (model, q, EQ_EXPR, null_ptr);
9399 : 4 : }
9400 : :
9401 : : /* Smoketest of getting and setting the value of a variable. */
9402 : :
9403 : : static void
9404 : 4 : test_var ()
9405 : : {
9406 : : /* "int i;" */
9407 : 4 : tree i = build_global_decl ("i", integer_type_node);
9408 : :
9409 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9410 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
9411 : :
9412 : 4 : region_model_manager mgr;
9413 : 4 : region_model model (&mgr);
9414 : :
9415 : 4 : const region *i_reg = model.get_lvalue (i, nullptr);
9416 : 4 : ASSERT_EQ (i_reg->get_kind (), RK_DECL);
9417 : :
9418 : : /* Reading "i" should give a symbolic "initial value". */
9419 : 4 : const svalue *sval_init = model.get_rvalue (i, nullptr);
9420 : 4 : ASSERT_EQ (sval_init->get_kind (), SK_INITIAL);
9421 : 4 : ASSERT_EQ (sval_init->dyn_cast_initial_svalue ()->get_region (), i_reg);
9422 : : /* ..and doing it again should give the same "initial value". */
9423 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr), sval_init);
9424 : :
9425 : : /* "i = 17;". */
9426 : 4 : model.set_value (i, int_17, nullptr);
9427 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr),
9428 : : model.get_rvalue (int_17, nullptr));
9429 : :
9430 : : /* "i = -3;". */
9431 : 4 : model.set_value (i, int_m3, nullptr);
9432 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr),
9433 : : model.get_rvalue (int_m3, nullptr));
9434 : :
9435 : : /* Verify get_offset for "i". */
9436 : 4 : {
9437 : 4 : region_offset offset = i_reg->get_offset (&mgr);
9438 : 4 : ASSERT_EQ (offset.get_base_region (), i_reg);
9439 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
9440 : : }
9441 : 4 : }
9442 : :
9443 : : static void
9444 : 4 : test_array_2 ()
9445 : : {
9446 : : /* "int arr[10];" */
9447 : 4 : tree tlen = size_int (10);
9448 : 4 : tree arr_type
9449 : 4 : = build_array_type (integer_type_node, build_index_type (tlen));
9450 : 4 : tree arr = build_global_decl ("arr", arr_type);
9451 : :
9452 : : /* "int i;" */
9453 : 4 : tree i = build_global_decl ("i", integer_type_node);
9454 : :
9455 : 4 : tree int_0 = integer_zero_node;
9456 : 4 : tree int_1 = integer_one_node;
9457 : :
9458 : 4 : tree arr_0 = build4 (ARRAY_REF, integer_type_node,
9459 : : arr, int_0, NULL_TREE, NULL_TREE);
9460 : 4 : tree arr_1 = build4 (ARRAY_REF, integer_type_node,
9461 : : arr, int_1, NULL_TREE, NULL_TREE);
9462 : 4 : tree arr_i = build4 (ARRAY_REF, integer_type_node,
9463 : : arr, i, NULL_TREE, NULL_TREE);
9464 : :
9465 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9466 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
9467 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
9468 : :
9469 : 4 : region_model_manager mgr;
9470 : 4 : region_model model (&mgr);
9471 : : /* "arr[0] = 17;". */
9472 : 4 : model.set_value (arr_0, int_17, nullptr);
9473 : : /* "arr[1] = -3;". */
9474 : 4 : model.set_value (arr_1, int_m3, nullptr);
9475 : :
9476 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr),
9477 : : model.get_rvalue (int_17, nullptr));
9478 : 4 : ASSERT_EQ (model.get_rvalue (arr_1, nullptr),
9479 : : model.get_rvalue (int_m3, nullptr));
9480 : :
9481 : : /* Overwrite a pre-existing binding: "arr[1] = 42;". */
9482 : 4 : model.set_value (arr_1, int_42, nullptr);
9483 : 4 : ASSERT_EQ (model.get_rvalue (arr_1, nullptr),
9484 : : model.get_rvalue (int_42, nullptr));
9485 : :
9486 : : /* Verify get_offset for "arr[0]". */
9487 : 4 : {
9488 : 4 : const region *arr_0_reg = model.get_lvalue (arr_0, nullptr);
9489 : 4 : region_offset offset = arr_0_reg->get_offset (&mgr);
9490 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9491 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
9492 : : }
9493 : :
9494 : : /* Verify get_offset for "arr[1]". */
9495 : 4 : {
9496 : 4 : const region *arr_1_reg = model.get_lvalue (arr_1, nullptr);
9497 : 4 : region_offset offset = arr_1_reg->get_offset (&mgr);
9498 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9499 : 4 : ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE);
9500 : : }
9501 : :
9502 : : /* Verify get_offset for "arr[i]". */
9503 : 4 : {
9504 : 4 : const region *arr_i_reg = model.get_lvalue (arr_i, nullptr);
9505 : 4 : region_offset offset = arr_i_reg->get_offset (&mgr);
9506 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9507 : 4 : const svalue *offset_sval = offset.get_symbolic_byte_offset ();
9508 : 4 : if (const svalue *cast = offset_sval->maybe_undo_cast ())
9509 : 4 : offset_sval = cast;
9510 : 4 : ASSERT_EQ (offset_sval->get_kind (), SK_BINOP);
9511 : : }
9512 : :
9513 : : /* "arr[i] = i;" - this should remove the earlier bindings. */
9514 : 4 : model.set_value (arr_i, i, nullptr);
9515 : 4 : ASSERT_EQ (model.get_rvalue (arr_i, nullptr), model.get_rvalue (i, nullptr));
9516 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr)->get_kind (), SK_UNKNOWN);
9517 : :
9518 : : /* "arr[0] = 17;" - this should remove the arr[i] binding. */
9519 : 4 : model.set_value (arr_0, int_17, nullptr);
9520 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr),
9521 : : model.get_rvalue (int_17, nullptr));
9522 : 4 : ASSERT_EQ (model.get_rvalue (arr_i, nullptr)->get_kind (), SK_UNKNOWN);
9523 : 4 : }
9524 : :
9525 : : /* Smoketest of dereferencing a pointer via MEM_REF. */
9526 : :
9527 : : static void
9528 : 4 : test_mem_ref ()
9529 : : {
9530 : : /*
9531 : : x = 17;
9532 : : p = &x;
9533 : : *p;
9534 : : */
9535 : 4 : tree x = build_global_decl ("x", integer_type_node);
9536 : 4 : tree int_star = build_pointer_type (integer_type_node);
9537 : 4 : tree p = build_global_decl ("p", int_star);
9538 : :
9539 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9540 : 4 : tree addr_of_x = build1 (ADDR_EXPR, int_star, x);
9541 : 4 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
9542 : 4 : tree offset_0 = build_int_cst (ptype, 0);
9543 : 4 : tree star_p = build2 (MEM_REF, integer_type_node, p, offset_0);
9544 : :
9545 : 4 : region_model_manager mgr;
9546 : 4 : region_model model (&mgr);
9547 : :
9548 : : /* "x = 17;". */
9549 : 4 : model.set_value (x, int_17, nullptr);
9550 : :
9551 : : /* "p = &x;". */
9552 : 4 : model.set_value (p, addr_of_x, nullptr);
9553 : :
9554 : 4 : const svalue *sval = model.get_rvalue (star_p, nullptr);
9555 : 4 : ASSERT_EQ (sval->maybe_get_constant (), int_17);
9556 : 4 : }
9557 : :
9558 : : /* Test for a POINTER_PLUS_EXPR followed by a MEM_REF.
9559 : : Analogous to this code:
9560 : : void test_6 (int a[10])
9561 : : {
9562 : : __analyzer_eval (a[3] == 42); [should be UNKNOWN]
9563 : : a[3] = 42;
9564 : : __analyzer_eval (a[3] == 42); [should be TRUE]
9565 : : }
9566 : : from data-model-1.c, which looks like this at the gimple level:
9567 : : # __analyzer_eval (a[3] == 42); [should be UNKNOWN]
9568 : : int *_1 = a_10(D) + 12; # POINTER_PLUS_EXPR
9569 : : int _2 = *_1; # MEM_REF
9570 : : _Bool _3 = _2 == 42;
9571 : : int _4 = (int) _3;
9572 : : __analyzer_eval (_4);
9573 : :
9574 : : # a[3] = 42;
9575 : : int *_5 = a_10(D) + 12; # POINTER_PLUS_EXPR
9576 : : *_5 = 42; # MEM_REF
9577 : :
9578 : : # __analyzer_eval (a[3] == 42); [should be TRUE]
9579 : : int *_6 = a_10(D) + 12; # POINTER_PLUS_EXPR
9580 : : int _7 = *_6; # MEM_REF
9581 : : _Bool _8 = _7 == 42;
9582 : : int _9 = (int) _8;
9583 : : __analyzer_eval (_9); */
9584 : :
9585 : : static void
9586 : 4 : test_POINTER_PLUS_EXPR_then_MEM_REF ()
9587 : : {
9588 : 4 : tree int_star = build_pointer_type (integer_type_node);
9589 : 4 : tree a = build_global_decl ("a", int_star);
9590 : 4 : tree offset_12 = build_int_cst (size_type_node, 12);
9591 : 4 : tree pointer_plus_expr = build2 (POINTER_PLUS_EXPR, int_star, a, offset_12);
9592 : 4 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
9593 : 4 : tree offset_0 = build_int_cst (ptype, 0);
9594 : 4 : tree mem_ref = build2 (MEM_REF, integer_type_node,
9595 : : pointer_plus_expr, offset_0);
9596 : 4 : region_model_manager mgr;
9597 : 4 : region_model m (&mgr);
9598 : :
9599 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
9600 : 4 : m.set_value (mem_ref, int_42, nullptr);
9601 : 4 : ASSERT_EQ (m.get_rvalue (mem_ref, nullptr)->maybe_get_constant (), int_42);
9602 : 4 : }
9603 : :
9604 : : /* Verify that malloc works. */
9605 : :
9606 : : static void
9607 : 4 : test_malloc ()
9608 : : {
9609 : 4 : tree int_star = build_pointer_type (integer_type_node);
9610 : 4 : tree p = build_global_decl ("p", int_star);
9611 : 4 : tree n = build_global_decl ("n", integer_type_node);
9612 : 4 : tree n_times_4 = build2 (MULT_EXPR, size_type_node,
9613 : : n, build_int_cst (size_type_node, 4));
9614 : :
9615 : 4 : region_model_manager mgr;
9616 : 4 : test_region_model_context ctxt;
9617 : 4 : region_model model (&mgr);
9618 : :
9619 : : /* "p = malloc (n * 4);". */
9620 : 4 : const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt);
9621 : 4 : const region *reg
9622 : 4 : = model.get_or_create_region_for_heap_alloc (size_sval, &ctxt);
9623 : 4 : const svalue *ptr = mgr.get_ptr_svalue (int_star, reg);
9624 : 4 : model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt);
9625 : 4 : ASSERT_EQ (model.get_capacity (reg), size_sval);
9626 : 4 : }
9627 : :
9628 : : /* Verify that alloca works. */
9629 : :
9630 : : static void
9631 : 4 : test_alloca ()
9632 : : {
9633 : 4 : auto_vec <tree> param_types;
9634 : 4 : tree fndecl = make_fndecl (integer_type_node,
9635 : : "test_fn",
9636 : : param_types);
9637 : 4 : allocate_struct_function (fndecl, true);
9638 : :
9639 : :
9640 : 4 : tree int_star = build_pointer_type (integer_type_node);
9641 : 4 : tree p = build_global_decl ("p", int_star);
9642 : 4 : tree n = build_global_decl ("n", integer_type_node);
9643 : 4 : tree n_times_4 = build2 (MULT_EXPR, size_type_node,
9644 : : n, build_int_cst (size_type_node, 4));
9645 : :
9646 : 4 : region_model_manager mgr;
9647 : 4 : test_region_model_context ctxt;
9648 : 4 : region_model model (&mgr);
9649 : :
9650 : : /* Push stack frame. */
9651 : 4 : const region *frame_reg
9652 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (fndecl),
9653 : : nullptr, nullptr, &ctxt);
9654 : : /* "p = alloca (n * 4);". */
9655 : 4 : const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt);
9656 : 4 : const region *reg = model.create_region_for_alloca (size_sval, &ctxt);
9657 : 4 : ASSERT_EQ (reg->get_parent_region (), frame_reg);
9658 : 4 : const svalue *ptr = mgr.get_ptr_svalue (int_star, reg);
9659 : 4 : model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt);
9660 : 4 : ASSERT_EQ (model.get_capacity (reg), size_sval);
9661 : :
9662 : : /* Verify that the pointers to the alloca region are replaced by
9663 : : poisoned values when the frame is popped. */
9664 : 4 : model.pop_frame (nullptr, nullptr, &ctxt, nullptr);
9665 : 4 : ASSERT_EQ (model.get_rvalue (p, nullptr)->get_kind (), SK_POISONED);
9666 : 4 : }
9667 : :
9668 : : /* Verify that svalue::involves_p works. */
9669 : :
9670 : : static void
9671 : 4 : test_involves_p ()
9672 : : {
9673 : 4 : region_model_manager mgr;
9674 : 4 : tree int_star = build_pointer_type (integer_type_node);
9675 : 4 : tree p = build_global_decl ("p", int_star);
9676 : 4 : tree q = build_global_decl ("q", int_star);
9677 : :
9678 : 4 : test_region_model_context ctxt;
9679 : 4 : region_model model (&mgr);
9680 : 4 : const svalue *p_init = model.get_rvalue (p, &ctxt);
9681 : 4 : const svalue *q_init = model.get_rvalue (q, &ctxt);
9682 : :
9683 : 4 : ASSERT_TRUE (p_init->involves_p (p_init));
9684 : 4 : ASSERT_FALSE (p_init->involves_p (q_init));
9685 : :
9686 : 4 : const region *star_p_reg = mgr.get_symbolic_region (p_init);
9687 : 4 : const region *star_q_reg = mgr.get_symbolic_region (q_init);
9688 : :
9689 : 4 : const svalue *init_star_p = mgr.get_or_create_initial_value (star_p_reg);
9690 : 4 : const svalue *init_star_q = mgr.get_or_create_initial_value (star_q_reg);
9691 : :
9692 : 4 : ASSERT_TRUE (init_star_p->involves_p (p_init));
9693 : 4 : ASSERT_FALSE (p_init->involves_p (init_star_p));
9694 : 4 : ASSERT_FALSE (init_star_p->involves_p (q_init));
9695 : 4 : ASSERT_TRUE (init_star_q->involves_p (q_init));
9696 : 4 : ASSERT_FALSE (init_star_q->involves_p (p_init));
9697 : 4 : }
9698 : :
9699 : : /* Run all of the selftests within this file. */
9700 : :
9701 : : void
9702 : 4 : analyzer_region_model_cc_tests ()
9703 : : {
9704 : 4 : test_tree_cmp_on_constants ();
9705 : 4 : test_dump ();
9706 : 4 : test_struct ();
9707 : 4 : test_array_1 ();
9708 : 4 : test_get_representative_tree ();
9709 : 4 : test_unique_constants ();
9710 : 4 : test_unique_unknowns ();
9711 : 4 : test_initial_svalue_folding ();
9712 : 4 : test_unaryop_svalue_folding ();
9713 : 4 : test_binop_svalue_folding ();
9714 : 4 : test_sub_svalue_folding ();
9715 : 4 : test_bits_within_svalue_folding ();
9716 : 4 : test_descendent_of_p ();
9717 : 4 : test_bit_range_regions ();
9718 : 4 : test_assignment ();
9719 : 4 : test_compound_assignment ();
9720 : 4 : test_stack_frames ();
9721 : 4 : test_get_representative_path_var ();
9722 : 4 : test_equality_1 ();
9723 : 4 : test_canonicalization_2 ();
9724 : 4 : test_canonicalization_3 ();
9725 : 4 : test_canonicalization_4 ();
9726 : 4 : test_state_merging ();
9727 : 4 : test_constraint_merging ();
9728 : 4 : test_widening_constraints ();
9729 : 4 : test_iteration_1 ();
9730 : 4 : test_malloc_constraints ();
9731 : 4 : test_var ();
9732 : 4 : test_array_2 ();
9733 : 4 : test_mem_ref ();
9734 : 4 : test_POINTER_PLUS_EXPR_then_MEM_REF ();
9735 : 4 : test_malloc ();
9736 : 4 : test_alloca ();
9737 : 4 : test_involves_p ();
9738 : 4 : }
9739 : :
9740 : : } // namespace selftest
9741 : :
9742 : : #endif /* CHECKING_P */
9743 : :
9744 : : } // namespace ana
9745 : :
9746 : : #endif /* #if ENABLE_ANALYZER */
|