Branch data Line data Source code
1 : : /* Classes for modeling the state of memory.
2 : : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #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 : 35960 : dump_tree (pretty_printer *pp, tree t)
80 : : {
81 : 35960 : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
82 : 35960 : }
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 : 431681 : region_to_value_map::operator== (const region_to_value_map &other) const
153 : : {
154 : 431681 : if (m_hash_map.elements () != other.m_hash_map.elements ())
155 : : return false;
156 : :
157 : 723605 : for (auto iter : *this)
158 : : {
159 : 147376 : const region *reg = iter.first;
160 : 147376 : const svalue *sval = iter.second;
161 : 147376 : const svalue * const *other_slot = other.get (reg);
162 : 147376 : if (other_slot == nullptr)
163 : 58 : return false;
164 : 147346 : if (sval != *other_slot)
165 : : return false;
166 : : }
167 : :
168 : 428911 : 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 : 42132 : region_to_value_map::can_merge_with_p (const region_to_value_map &other,
283 : : region_to_value_map *out) const
284 : : {
285 : 58046 : for (auto iter : *this)
286 : : {
287 : 10798 : const region *iter_reg = iter.first;
288 : 10798 : const svalue *iter_sval = iter.second;
289 : 10798 : const svalue * const * other_slot = other.get (iter_reg);
290 : 10798 : if (other_slot)
291 : : {
292 : 10466 : if (iter_sval == *other_slot)
293 : 7625 : out->put (iter_reg, iter_sval);
294 : : else
295 : 2841 : return false;
296 : : }
297 : : }
298 : 39291 : return true;
299 : : }
300 : :
301 : : /* Purge any state involving SVAL. */
302 : :
303 : : void
304 : 28359 : region_to_value_map::purge_state_involving (const svalue *sval)
305 : : {
306 : 28359 : auto_vec<const region *> to_purge;
307 : 86071 : for (auto iter : *this)
308 : : {
309 : 28856 : const region *iter_reg = iter.first;
310 : 28856 : const svalue *iter_sval = iter.second;
311 : 28856 : if (iter_reg->involves_p (sval) || iter_sval->involves_p (sval))
312 : 22 : to_purge.safe_push (iter_reg);
313 : : }
314 : 28425 : for (auto iter : to_purge)
315 : 22 : m_hash_map.remove (iter);
316 : 28359 : }
317 : :
318 : : // struct exception_node
319 : :
320 : : bool
321 : 10831 : exception_node::operator== (const exception_node &other) const
322 : : {
323 : 10831 : return (m_exception_sval == other.m_exception_sval
324 : 10831 : && m_typeinfo_sval == other.m_typeinfo_sval
325 : 21662 : && 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 : 288217 : region_model::region_model (region_model_manager *mgr)
406 : 288217 : : m_mgr (mgr), m_store (), m_current_frame (nullptr),
407 : 288217 : m_thrown_exceptions_stack (),
408 : 288217 : m_caught_exceptions_stack (),
409 : 288217 : m_dynamic_extents ()
410 : : {
411 : 288217 : m_constraints = new constraint_manager (mgr);
412 : 288217 : }
413 : :
414 : : /* region_model's copy ctor. */
415 : :
416 : 2994001 : region_model::region_model (const region_model &other)
417 : 2994001 : : m_mgr (other.m_mgr), m_store (other.m_store),
418 : 2994001 : m_constraints (new constraint_manager (*other.m_constraints)),
419 : 2994001 : m_current_frame (other.m_current_frame),
420 : 2994001 : m_thrown_exceptions_stack (other.m_thrown_exceptions_stack),
421 : 2994001 : m_caught_exceptions_stack (other.m_caught_exceptions_stack),
422 : 2994001 : m_dynamic_extents (other.m_dynamic_extents)
423 : : {
424 : 2994001 : }
425 : :
426 : : /* region_model's dtor. */
427 : :
428 : 3282218 : region_model::~region_model ()
429 : : {
430 : 3282218 : delete m_constraints;
431 : 3282218 : }
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 : 478820 : region_model::operator== (const region_model &other) const
464 : : {
465 : : /* We can only compare instances that use the same manager. */
466 : 478820 : gcc_assert (m_mgr == other.m_mgr);
467 : :
468 : 478820 : if (m_store != other.m_store)
469 : : return false;
470 : :
471 : 393000 : if (*m_constraints != *other.m_constraints)
472 : : return false;
473 : :
474 : 388029 : if (m_current_frame != other.m_current_frame)
475 : : return false;
476 : :
477 : 388021 : if (m_thrown_exceptions_stack != other.m_thrown_exceptions_stack)
478 : : return false;
479 : 388021 : if (m_caught_exceptions_stack != other.m_caught_exceptions_stack)
480 : : return false;
481 : :
482 : 388021 : if (m_dynamic_extents != other.m_dynamic_extents)
483 : : return false;
484 : :
485 : 387737 : 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 : 1204110 : region_model::hash () const
494 : : {
495 : 1204110 : hashval_t result = m_store.hash ();
496 : 1204110 : result ^= m_constraints->hash ();
497 : 1204110 : 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 : 1659827 : region_model::validate () const
711 : : {
712 : 1659827 : m_store.validate ();
713 : 1659827 : }
714 : :
715 : : /* Canonicalize the store and constraints, to maximize the chance of
716 : : equality between region_model instances. */
717 : :
718 : : void
719 : 799605 : region_model::canonicalize ()
720 : : {
721 : 799605 : m_store.canonicalize (m_mgr->get_store_manager ());
722 : 799605 : m_constraints->canonicalize ();
723 : 799605 : }
724 : :
725 : : /* Return true if this region_model is in canonical form. */
726 : :
727 : : bool
728 : 378247 : region_model::canonicalized_p () const
729 : : {
730 : 378247 : region_model copy (*this);
731 : 378247 : copy.canonicalize ();
732 : 378247 : return *this == copy;
733 : 378247 : }
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 : 355679 : region_model::get_gassign_result (const gassign *assign,
1172 : : region_model_context *ctxt)
1173 : : {
1174 : 355679 : tree lhs = gimple_assign_lhs (assign);
1175 : :
1176 : 355679 : if (gimple_has_volatile_ops (assign)
1177 : 355679 : && !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 : 355563 : tree rhs1 = gimple_assign_rhs1 (assign);
1187 : 355563 : enum tree_code op = gimple_assign_rhs_code (assign);
1188 : 355563 : switch (op)
1189 : : {
1190 : : default:
1191 : : return nullptr;
1192 : :
1193 : 34107 : case POINTER_PLUS_EXPR:
1194 : 34107 : {
1195 : : /* e.g. "_1 = a_10(D) + 12;" */
1196 : 34107 : tree ptr = rhs1;
1197 : 34107 : tree offset = gimple_assign_rhs2 (assign);
1198 : :
1199 : 34107 : const svalue *ptr_sval = get_rvalue (ptr, ctxt);
1200 : 34107 : 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 : 34107 : offset_sval = m_mgr->get_or_create_cast (size_type_node, offset_sval);
1204 : :
1205 : 34107 : const svalue *sval_binop
1206 : 34107 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1207 : : ptr_sval, offset_sval);
1208 : 34107 : 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 : 189490 : 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 : 189490 : case ADDR_EXPR: /* LHS = &RHS; */
1236 : 189490 : case BIT_FIELD_REF:
1237 : 189490 : case COMPONENT_REF: /* LHS = op0.op1; */
1238 : 189490 : case MEM_REF:
1239 : 189490 : case REAL_CST:
1240 : 189490 : case COMPLEX_CST:
1241 : 189490 : case VECTOR_CST:
1242 : 189490 : case INTEGER_CST:
1243 : 189490 : case ARRAY_REF:
1244 : 189490 : case SSA_NAME: /* LHS = VAR; */
1245 : 189490 : case VAR_DECL: /* LHS = VAR; */
1246 : 189490 : case PARM_DECL:/* LHS = VAR; */
1247 : 189490 : case REALPART_EXPR:
1248 : 189490 : case IMAGPART_EXPR:
1249 : 189490 : return get_rvalue (rhs1, ctxt);
1250 : :
1251 : 49955 : case ABS_EXPR:
1252 : 49955 : case ABSU_EXPR:
1253 : 49955 : case CONJ_EXPR:
1254 : 49955 : case BIT_NOT_EXPR:
1255 : 49955 : case FIX_TRUNC_EXPR:
1256 : 49955 : case FLOAT_EXPR:
1257 : 49955 : case NEGATE_EXPR:
1258 : 49955 : case NOP_EXPR:
1259 : 49955 : case VIEW_CONVERT_EXPR:
1260 : 49955 : {
1261 : : /* Unary ops. */
1262 : 49955 : const svalue *rhs_sval = get_rvalue (rhs1, ctxt);
1263 : 49955 : const svalue *sval_unaryop
1264 : 49955 : = m_mgr->get_or_create_unaryop (TREE_TYPE (lhs), op, rhs_sval);
1265 : 49955 : return sval_unaryop;
1266 : : }
1267 : :
1268 : 14192 : case EQ_EXPR:
1269 : 14192 : case GE_EXPR:
1270 : 14192 : case LE_EXPR:
1271 : 14192 : case NE_EXPR:
1272 : 14192 : case GT_EXPR:
1273 : 14192 : case LT_EXPR:
1274 : 14192 : case UNORDERED_EXPR:
1275 : 14192 : case ORDERED_EXPR:
1276 : 14192 : {
1277 : 14192 : tree rhs2 = gimple_assign_rhs2 (assign);
1278 : :
1279 : 14192 : const svalue *rhs1_sval = get_rvalue (rhs1, ctxt);
1280 : 14192 : const svalue *rhs2_sval = get_rvalue (rhs2, ctxt);
1281 : :
1282 : 14192 : if (TREE_TYPE (lhs) == boolean_type_node)
1283 : : {
1284 : : /* Consider constraints between svalues. */
1285 : 14055 : tristate t = eval_condition (rhs1_sval, op, rhs2_sval);
1286 : 14055 : if (t.is_known ())
1287 : 7787 : return m_mgr->get_or_create_constant_svalue
1288 : 7787 : (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 : 55696 : break;
1298 : :
1299 : 55696 : case PLUS_EXPR:
1300 : 55696 : case MINUS_EXPR:
1301 : 55696 : case MULT_EXPR:
1302 : 55696 : case MULT_HIGHPART_EXPR:
1303 : 55696 : case TRUNC_DIV_EXPR:
1304 : 55696 : case CEIL_DIV_EXPR:
1305 : 55696 : case FLOOR_DIV_EXPR:
1306 : 55696 : case ROUND_DIV_EXPR:
1307 : 55696 : case TRUNC_MOD_EXPR:
1308 : 55696 : case CEIL_MOD_EXPR:
1309 : 55696 : case FLOOR_MOD_EXPR:
1310 : 55696 : case ROUND_MOD_EXPR:
1311 : 55696 : case RDIV_EXPR:
1312 : 55696 : case EXACT_DIV_EXPR:
1313 : 55696 : case LSHIFT_EXPR:
1314 : 55696 : case RSHIFT_EXPR:
1315 : 55696 : case LROTATE_EXPR:
1316 : 55696 : case RROTATE_EXPR:
1317 : 55696 : case BIT_IOR_EXPR:
1318 : 55696 : case BIT_XOR_EXPR:
1319 : 55696 : case BIT_AND_EXPR:
1320 : 55696 : case MIN_EXPR:
1321 : 55696 : case MAX_EXPR:
1322 : 55696 : case COMPLEX_EXPR:
1323 : 55696 : {
1324 : : /* Binary ops. */
1325 : 55696 : tree rhs2 = gimple_assign_rhs2 (assign);
1326 : :
1327 : 55696 : const svalue *rhs1_sval = get_rvalue (rhs1, ctxt);
1328 : 55696 : const svalue *rhs2_sval = get_rvalue (rhs2, ctxt);
1329 : :
1330 : 55696 : 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 : 55696 : const svalue *sval_binop
1355 : 55696 : = m_mgr->get_or_create_binop (TREE_TYPE (lhs), op,
1356 : : rhs1_sval, rhs2_sval);
1357 : 55696 : 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 : 3034130 : region_model::check_for_poison (const svalue *sval,
1528 : : tree expr,
1529 : : const region *src_region,
1530 : : region_model_context *ctxt) const
1531 : : {
1532 : 3034130 : if (!ctxt)
1533 : : return sval;
1534 : :
1535 : 1568109 : 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 : 200424 : region_model::on_assignment (const gassign *assign, region_model_context *ctxt)
1622 : : {
1623 : 200424 : tree lhs = gimple_assign_lhs (assign);
1624 : 200424 : tree rhs1 = gimple_assign_rhs1 (assign);
1625 : :
1626 : 200424 : const region *lhs_reg = get_lvalue (lhs, ctxt);
1627 : :
1628 : : /* Any writes other than to the stack are treated
1629 : : as externally visible. */
1630 : 200424 : if (ctxt)
1631 : : {
1632 : 152456 : enum memory_space memspace = lhs_reg->get_memory_space ();
1633 : 152456 : 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 : 200424 : if (const svalue *sval = get_gassign_result (assign, ctxt))
1641 : : {
1642 : 194312 : tree expr = get_diagnostic_tree_for_gassign (assign);
1643 : 194312 : check_for_poison (sval, expr, nullptr, ctxt);
1644 : 194312 : set_value (lhs_reg, sval, ctxt);
1645 : 194312 : 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 : 275825 : region_model::on_stmt_pre (const gimple *stmt,
1715 : : bool *out_unknown_side_effects,
1716 : : region_model_context *ctxt)
1717 : : {
1718 : 275825 : 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 : 200424 : case GIMPLE_ASSIGN:
1732 : 200424 : {
1733 : 200424 : const gassign *assign = as_a <const gassign *> (stmt);
1734 : 200424 : on_assignment (assign, ctxt);
1735 : : }
1736 : 200424 : 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 : 71632 : case GIMPLE_CALL:
1748 : 71632 : {
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 : 71632 : const gcall *call = as_a <const gcall *> (stmt);
1753 : 71632 : *out_unknown_side_effects = on_call_pre (*call, ctxt);
1754 : : }
1755 : 71632 : 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 : 275825 : break;
1770 : : }
1771 : 275825 : }
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 : 48766 : region_model::check_call_args (const call_details &cd) const
1868 : : {
1869 : 112613 : for (unsigned arg_idx = 0; arg_idx < cd.num_args (); arg_idx++)
1870 : 63847 : cd.get_arg_svalue (arg_idx);
1871 : :
1872 : : /* Handle attribute "format". */
1873 : 48766 : if (tree format_attr = cd.lookup_function_attribute ("format"))
1874 : 1083 : check_call_format_attr (cd, format_attr);
1875 : 48766 : }
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 : 292714 : region_model::get_known_function (tree fndecl, const call_details &cd) const
2002 : : {
2003 : 292714 : known_function_manager *known_fn_mgr = m_mgr->get_known_function_manager ();
2004 : 292714 : 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 : 161844 : region_model::get_builtin_kf (const gcall &call,
2058 : : region_model_context *ctxt /* = nullptr */) const
2059 : : {
2060 : 161844 : region_model *mut_this = const_cast <region_model *> (this);
2061 : 161844 : tree callee_fndecl = mut_this->get_fndecl_for_call (call, ctxt);
2062 : 161844 : if (! callee_fndecl)
2063 : : return nullptr;
2064 : :
2065 : 161844 : call_details cd (call, mut_this, ctxt);
2066 : 161844 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2067 : 110266 : 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 : 71632 : region_model::on_call_pre (const gcall &call, region_model_context *ctxt)
2290 : : {
2291 : 71632 : 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 : 71632 : if (gimple_call_internal_p (&call)
2300 : 71632 : && 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 : 67408 : if (ctxt)
2308 : 48766 : check_call_args (cd);
2309 : :
2310 : 67408 : tree callee_fndecl = get_fndecl_for_call (call, ctxt);
2311 : :
2312 : 67408 : 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 : 66055 : 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 : 65531 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2342 : : {
2343 : 46013 : kf->impl_call_pre (cd);
2344 : 46013 : 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 : 71393 : region_model::on_call_post (const gcall &call,
2377 : : bool unknown_side_effects,
2378 : : region_model_context *ctxt)
2379 : : {
2380 : 71393 : if (tree callee_fndecl = get_fndecl_for_call (call, ctxt))
2381 : : {
2382 : 65339 : call_details cd (call, this, ctxt);
2383 : 65339 : if (const known_function *kf = get_known_function (callee_fndecl, cd))
2384 : : {
2385 : 45873 : kf->impl_call_post (cd);
2386 : 92085 : 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 : 28359 : region_model::purge_state_involving (const svalue *sval,
2428 : : region_model_context *ctxt)
2429 : : {
2430 : 28359 : if (!sval->can_have_associated_state_p ())
2431 : : return;
2432 : 28359 : m_store.purge_state_involving (sval, m_mgr);
2433 : 28359 : m_constraints->purge_state_involving (sval);
2434 : 28359 : m_dynamic_extents.purge_state_involving (sval);
2435 : 28359 : if (ctxt)
2436 : 17974 : 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 : 930200 : region_model::get_reachable_svalues (svalue_set *out,
2784 : : const svalue *extra_sval,
2785 : : const uncertainty_t *uncertainty)
2786 : : {
2787 : 930200 : reachable_regions reachable_regs (this);
2788 : :
2789 : : /* Add globals and regions that already escaped in previous
2790 : : unknown calls. */
2791 : 930200 : m_store.for_each_cluster (reachable_regions::init_cluster_cb,
2792 : : &reachable_regs);
2793 : :
2794 : 930200 : if (extra_sval)
2795 : 5187 : reachable_regs.handle_sval (extra_sval);
2796 : :
2797 : 930200 : if (uncertainty)
2798 : 428264 : for (uncertainty_t::iterator iter
2799 : 412314 : = uncertainty->begin_maybe_bound_svals ();
2800 : 856528 : 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 : 8557907 : for (store::cluster_map_t::iterator iter = m_store.begin ();
2805 : 16185614 : iter != m_store.end (); ++iter)
2806 : : {
2807 : 7627707 : const region *base_reg = (*iter).first;
2808 : 7627707 : if (const region *parent = base_reg->get_parent_region ())
2809 : 7627707 : if (parent->get_kind () == RK_FRAME)
2810 : 4726656 : reachable_regs.add (base_reg, false);
2811 : : }
2812 : :
2813 : : /* Populate *OUT based on the values that were reachable. */
2814 : 930200 : for (svalue_set::iterator iter
2815 : 930200 : = reachable_regs.begin_reachable_svals ();
2816 : 15539866 : iter != reachable_regs.end_reachable_svals (); ++iter)
2817 : 7304833 : out->add (*iter);
2818 : 930200 : }
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 : 2350960 : region_model::get_lvalue_1 (path_var pv, region_model_context *ctxt) const
2934 : : {
2935 : 2350960 : tree expr = pv.m_tree;
2936 : :
2937 : 2350960 : gcc_assert (expr);
2938 : :
2939 : 2350960 : 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 : 874121 : break;
2984 : :
2985 : 874121 : case FUNCTION_DECL:
2986 : 874121 : 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 : 1273427 : case SSA_NAME:
2999 : 1273427 : case PARM_DECL:
3000 : 1273427 : case RESULT_DECL:
3001 : 1273427 : {
3002 : 1273427 : 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 : 1273427 : int stack_index = pv.m_stack_depth;
3008 : 1273427 : const frame_region *frame = get_frame_at_index (stack_index);
3009 : 1273427 : gcc_assert (frame);
3010 : 1273427 : 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 : 15370 : break;
3022 : :
3023 : 15370 : case STRING_CST:
3024 : 15370 : 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 : 5197889 : assert_compat_types (tree src_type, tree dst_type)
3032 : : {
3033 : 5197889 : if (src_type && dst_type && !VOID_TYPE_P (dst_type))
3034 : : {
3035 : : #if CHECKING_P
3036 : 5197597 : 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 : 5197889 : }
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 : 2350960 : region_model::get_lvalue (path_var pv, region_model_context *ctxt) const
3058 : : {
3059 : 2350960 : if (pv.m_tree == NULL_TREE)
3060 : : return nullptr;
3061 : :
3062 : 2350960 : const region *result_reg = get_lvalue_1 (pv, ctxt);
3063 : 2350960 : assert_compat_types (result_reg->get_type (), TREE_TYPE (pv.m_tree));
3064 : 2350960 : 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 : 1473413 : region_model::get_lvalue (tree expr, region_model_context *ctxt) const
3072 : : {
3073 : 1473413 : 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 : 2833859 : region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt) const
3083 : : {
3084 : 2833859 : gcc_assert (pv.m_tree);
3085 : :
3086 : 2833859 : 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 : 932405 : case ADDR_EXPR:
3092 : 932405 : {
3093 : : /* "&EXPR". */
3094 : 932405 : tree expr = pv.m_tree;
3095 : 932405 : tree op0 = TREE_OPERAND (expr, 0);
3096 : 932405 : const region *expr_reg = get_lvalue (op0, ctxt);
3097 : 932405 : 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 : 827478 : case PARM_DECL:
3124 : 827478 : case SSA_NAME:
3125 : 827478 : case RESULT_DECL:
3126 : 827478 : case ARRAY_REF:
3127 : 827478 : {
3128 : 827478 : const region *reg = get_lvalue (pv, ctxt);
3129 : 827478 : 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 : 1020914 : };
3144 : :
3145 : 1020914 : case INTEGER_CST:
3146 : 1020914 : case REAL_CST:
3147 : 1020914 : case COMPLEX_CST:
3148 : 1020914 : case VECTOR_CST:
3149 : 1020914 : case STRING_CST:
3150 : 1020914 : case RAW_DATA_CST:
3151 : 1020914 : 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 : 2870394 : region_model::get_rvalue (path_var pv, region_model_context *ctxt) const
3203 : : {
3204 : 2870394 : if (pv.m_tree == NULL_TREE)
3205 : : return nullptr;
3206 : :
3207 : 2833859 : const svalue *result_sval = get_rvalue_1 (pv, ctxt);
3208 : :
3209 : 2833859 : assert_compat_types (result_sval->get_type (), TREE_TYPE (pv.m_tree));
3210 : :
3211 : 2833859 : result_sval = check_for_poison (result_sval, pv.m_tree, nullptr, ctxt);
3212 : :
3213 : 2833859 : 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 : 2869902 : region_model::get_rvalue (tree expr, region_model_context *ctxt) const
3221 : : {
3222 : 2869902 : 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 : 255245 : region_model::called_from_main_p () const
3231 : : {
3232 : 255245 : if (!m_current_frame)
3233 : : return false;
3234 : : /* Determine if the oldest stack frame in this model is for "main". */
3235 : 248882 : const frame_region *frame0 = get_frame_at_index (0);
3236 : 248882 : gcc_assert (frame0);
3237 : 248882 : 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 : 264217 : region_model::get_initial_value_for_global (const region *reg) const
3246 : : {
3247 : : /* Get the decl that REG is for (or is within). */
3248 : 264217 : const decl_region *base_reg
3249 : 264217 : = reg->get_base_region ()->dyn_cast_decl_region ();
3250 : 264217 : gcc_assert (base_reg);
3251 : 264217 : 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 : 264217 : if (m_store.called_unknown_fn_p ()
3259 : 81996 : && TREE_PUBLIC (decl)
3260 : 281741 : && !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 : 255189 : 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 : 240390 : 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 : 3750436 : 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 : 3750436 : if (reg->empty_p ())
3284 : 52 : return m_mgr->get_or_create_unknown_svalue (reg->get_type ());
3285 : :
3286 : 3750384 : bool check_poisoned = true;
3287 : 3750384 : if (check_region_for_read (reg, ctxt))
3288 : 425 : check_poisoned = false;
3289 : :
3290 : : /* Special-case: handle var_decls in the constant pool. */
3291 : 3750384 : if (const decl_region *decl_reg = reg->dyn_cast_decl_region ())
3292 : 3100930 : if (const svalue *sval = decl_reg->maybe_get_constant_value (m_mgr))
3293 : : return sval;
3294 : :
3295 : 3750370 : const svalue *sval
3296 : 3750370 : = m_store.get_any_binding (m_mgr->get_store_manager (), reg);
3297 : 3750370 : if (sval)
3298 : : {
3299 : 1080049 : if (reg->get_type ())
3300 : 1077999 : sval = m_mgr->get_or_create_cast (reg->get_type (), sval);
3301 : 1080049 : return sval;
3302 : : }
3303 : :
3304 : : /* Special-case: read at a constant index within a STRING_CST. */
3305 : 2670321 : 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 : 2670131 : 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 : 2669945 : if (reg->get_base_region ()->get_parent_region ()->get_kind ()
3338 : : == RK_GLOBALS)
3339 : 264217 : return get_initial_value_for_global (reg);
3340 : :
3341 : 2405728 : 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 : 2409354 : region_model::region_exists_p (const region *reg) const
3350 : : {
3351 : : /* If within a stack frame, check that the stack frame is live. */
3352 : 2409354 : 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 : 2349772 : for (const frame_region *iter_frame = get_current_frame (); iter_frame;
3357 : 591990 : iter_frame = iter_frame->get_calling_frame ())
3358 : 2333539 : 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 : 119872 : region_model::deref_rvalue (const svalue *ptr_sval, tree ptr_tree,
3373 : : region_model_context *ctxt,
3374 : : bool add_nonnull_constraint) const
3375 : : {
3376 : 119872 : gcc_assert (ptr_sval);
3377 : 119872 : 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 : 119872 : if (add_nonnull_constraint)
3385 : : {
3386 : 114498 : tree null_ptr_cst = build_int_cst (ptr_sval->get_type (), 0);
3387 : 114498 : const svalue *null_ptr
3388 : 114498 : = m_mgr->get_or_create_constant_svalue (null_ptr_cst);
3389 : 114498 : m_constraints->add_constraint (ptr_sval, NE_EXPR, null_ptr);
3390 : : }
3391 : :
3392 : 119872 : switch (ptr_sval->get_kind ())
3393 : : {
3394 : : default:
3395 : : break;
3396 : :
3397 : 45480 : case SK_REGION:
3398 : 45480 : {
3399 : 45480 : const region_svalue *region_sval
3400 : 45480 : = as_a <const region_svalue *> (ptr_sval);
3401 : 45480 : return region_sval->get_pointee ();
3402 : : }
3403 : :
3404 : 19481 : case SK_BINOP:
3405 : 19481 : {
3406 : 19481 : const binop_svalue *binop_sval
3407 : 19481 : = as_a <const binop_svalue *> (ptr_sval);
3408 : 19481 : switch (binop_sval->get_op ())
3409 : : {
3410 : 19481 : case POINTER_PLUS_EXPR:
3411 : 19481 : {
3412 : : /* If we have a symbolic value expressing pointer arithmentic,
3413 : : try to convert it to a suitable region. */
3414 : 19481 : const region *parent_region
3415 : 19481 : = deref_rvalue (binop_sval->get_arg0 (), NULL_TREE, ctxt);
3416 : 19481 : const svalue *offset = binop_sval->get_arg1 ();
3417 : 19481 : tree type= TREE_TYPE (ptr_sval->get_type ());
3418 : 19481 : 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 : 54911 : 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 : 229021 : 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 : 229021 : if (!ctxt)
3601 : : return;
3602 : :
3603 : 229021 : const region *base_reg = dest_reg->get_base_region ();
3604 : 229021 : 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 : 211517 : case RK_DECL:
3627 : 211517 : {
3628 : 211517 : const decl_region *decl_reg = as_a <const decl_region *> (base_reg);
3629 : 211517 : 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 : 211517 : if (TREE_READONLY (decl)
3635 : 211517 : && is_global_var (decl))
3636 : 20 : ctxt->warn
3637 : 20 : (std::make_unique<write_to_const_diagnostic> (dest_reg, decl));
3638 : : }
3639 : 211517 : 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 : 740107 : region_model::get_capacity (const region *reg) const
3651 : : {
3652 : 740122 : switch (reg->get_kind ())
3653 : : {
3654 : : default:
3655 : : break;
3656 : 677215 : case RK_DECL:
3657 : 677215 : {
3658 : 677215 : const decl_region *decl_reg = as_a <const decl_region *> (reg);
3659 : 677215 : tree decl = decl_reg->get_decl ();
3660 : 677215 : if (TREE_CODE (decl) == SSA_NAME)
3661 : : {
3662 : 594452 : tree type = TREE_TYPE (decl);
3663 : 594452 : tree size = TYPE_SIZE (type);
3664 : 594452 : return get_rvalue (size, nullptr);
3665 : : }
3666 : : else
3667 : : {
3668 : 82763 : tree size = decl_init_size (decl, false);
3669 : 82763 : if (size)
3670 : 82621 : 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 : 523 : case RK_STRING:
3679 : 523 : {
3680 : : /* "Capacity" here means "size". */
3681 : 523 : const string_region *string_reg = as_a <const string_region *> (reg);
3682 : 523 : tree string_cst = string_reg->get_string_cst ();
3683 : 523 : return m_mgr->get_or_create_int_cst (size_type_node,
3684 : 523 : TREE_STRING_LENGTH (string_cst));
3685 : : }
3686 : 62511 : break;
3687 : : }
3688 : :
3689 : 62511 : if (const svalue *recorded = get_dynamic_extents (reg))
3690 : : return recorded;
3691 : :
3692 : 50774 : 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 : 4058475 : 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 : 4058475 : if (!ctxt)
3709 : : return false;
3710 : :
3711 : 732278 : bool oob_access_detected = false;
3712 : 732278 : check_region_for_taint (reg, dir, ctxt);
3713 : 732278 : if (!check_region_bounds (reg, dir, sval_hint, ctxt))
3714 : 768 : oob_access_detected = true;
3715 : :
3716 : 732278 : switch (dir)
3717 : : {
3718 : 0 : default:
3719 : 0 : gcc_unreachable ();
3720 : : case access_direction::read:
3721 : : /* Currently a no-op. */
3722 : : break;
3723 : 229021 : case access_direction::write:
3724 : 229021 : check_for_writable_region (reg, ctxt);
3725 : 229021 : 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 : 308091 : region_model::check_region_for_write (const region *dest_reg,
3734 : : const svalue *sval_hint,
3735 : : region_model_context *ctxt) const
3736 : : {
3737 : 308091 : check_region_access (dest_reg, access_direction::write, sval_hint, ctxt);
3738 : 308091 : }
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 : 3750384 : region_model::check_region_for_read (const region *src_reg,
3745 : : region_model_context *ctxt) const
3746 : : {
3747 : 3750384 : 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 : 177530 : is_any_cast_p (const gimple *stmt)
4111 : : {
4112 : 177530 : if (const gassign *assign = dyn_cast <const gassign *> (stmt))
4113 : 137786 : return gimple_assign_cast_p (assign)
4114 : 254898 : || !pending_diagnostic::same_tree_p (
4115 : 117112 : TREE_TYPE (gimple_assign_lhs (assign)),
4116 : 117112 : TREE_TYPE (gimple_assign_rhs1 (assign)));
4117 : 39744 : else if (const gcall *call = dyn_cast <const gcall *> (stmt))
4118 : : {
4119 : 39336 : tree lhs = gimple_call_lhs (call);
4120 : 68872 : return lhs != NULL_TREE && !pending_diagnostic::same_tree_p (
4121 : 29536 : 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 : 306645 : region_model::check_region_size (const region *lhs_reg, const svalue *rhs_sval,
4134 : : region_model_context *ctxt) const
4135 : : {
4136 : 306645 : if (!ctxt || ctxt->get_stmt () == nullptr)
4137 : 301396 : return;
4138 : : /* Only report warnings on assignments that actually change the type. */
4139 : 177530 : if (!is_any_cast_p (ctxt->get_stmt ()))
4140 : : return;
4141 : :
4142 : 52109 : tree pointer_type = lhs_reg->get_type ();
4143 : 52109 : 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 : 306665 : region_model::set_value (const region *lhs_reg, const svalue *rhs_sval,
4215 : : region_model_context *ctxt)
4216 : : {
4217 : 306665 : gcc_assert (lhs_reg);
4218 : 306665 : gcc_assert (rhs_sval);
4219 : :
4220 : : /* Setting the value of an empty region is a no-op. */
4221 : 306665 : if (lhs_reg->empty_p ())
4222 : : return;
4223 : :
4224 : 306645 : check_region_size (lhs_reg, rhs_sval, ctxt);
4225 : :
4226 : 306645 : check_region_for_write (lhs_reg, rhs_sval, ctxt);
4227 : :
4228 : 534704 : m_store.set_value (m_mgr->get_store_manager(), lhs_reg, rhs_sval,
4229 : 228059 : 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 : 3553 : fragment ()
4261 : 3553 : : m_byte_range (0, 0), m_sval (nullptr)
4262 : : {
4263 : 3553 : }
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 : 3420 : class iterable_cluster
4506 : : {
4507 : : public:
4508 : 3420 : iterable_cluster (const binding_cluster *cluster)
4509 : 3420 : {
4510 : 3420 : if (!cluster)
4511 : : return;
4512 : 2713 : 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 : 1764 : m_fragments.qsort (fragment::cmp_ptrs);
4528 : : }
4529 : :
4530 : : bool
4531 : 3553 : get_fragment_for_byte (byte_offset_t byte, fragment *out_frag) const
4532 : : {
4533 : : /* TODO: binary search rather than linear. */
4534 : 3553 : unsigned iter_idx;
4535 : 3762 : 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 : 3015 : bool has_symbolic_bindings_p () const
4547 : : {
4548 : 6030 : 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 : 7545 : 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 : 7545 : if (bytes.get_start_byte_offset () == 0)
4585 : 7350 : 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 : 3143 : const svalue *index_sval
4591 : 3143 : = m_mgr->get_or_create_int_cst (size_type_node,
4592 : 3143 : bytes.get_start_byte_offset ());
4593 : 3143 : const region *offset_reg = m_mgr->get_offset_region (base_reg,
4594 : : NULL_TREE,
4595 : : index_sval);
4596 : 3143 : const svalue *byte_size_sval
4597 : 3143 : = m_mgr->get_or_create_int_cst (size_type_node, bytes.m_size_in_bytes);
4598 : 3143 : 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 : 3143 : const svalue *sval = get_store_value (read_reg, ctxt);
4604 : 3143 : return sval;
4605 : : }
4606 : :
4607 : : static tree
4608 : 2695 : get_tree_for_byte_offset (tree ptr_expr, byte_offset_t byte_offset)
4609 : : {
4610 : 2695 : gcc_assert (ptr_expr);
4611 : 2695 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
4612 : 2695 : 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 : 7941 : 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 : 7941 : logger *logger = ctxt ? ctxt->get_logger () : nullptr;
4659 : 7941 : store_manager *store_mgr = m_mgr->get_store_manager ();
4660 : :
4661 : 7941 : region_offset offset = reg->get_offset (m_mgr);
4662 : 7941 : 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 : 7826 : byte_offset_t src_byte_offset;
4671 : 7826 : 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 : 7826 : const byte_offset_t initial_src_byte_offset = src_byte_offset;
4680 : 7826 : byte_offset_t dst_byte_offset = 0;
4681 : :
4682 : 7826 : const region *base_reg = reg->get_base_region ();
4683 : :
4684 : 7826 : if (const string_region *str_reg = base_reg->dyn_cast_string_region ())
4685 : : {
4686 : 4408 : tree string_cst = str_reg->get_string_cst ();
4687 : 4410 : if (src_byte_offset >= 0
4688 : 4407 : && src_byte_offset < TREE_STRING_LENGTH (string_cst)
4689 : 8814 : && wi::fits_shwi_p (src_byte_offset))
4690 : : {
4691 : 4406 : HOST_WIDE_INT str_byte_offset = src_byte_offset.to_shwi ();
4692 : 4406 : const char *effective_start
4693 : 4406 : = TREE_STRING_POINTER (string_cst) + str_byte_offset;
4694 : 4406 : size_t effective_len
4695 : 4406 : = TREE_STRING_LENGTH (string_cst) - str_byte_offset;
4696 : 4406 : if (const void *p = memchr (effective_start, 0, effective_len))
4697 : : {
4698 : 4406 : size_t num_bytes_read
4699 : 4406 : = (const char *)p - effective_start + 1;
4700 : : /* Simulate the read. */
4701 : 4406 : byte_range bytes_to_read (0, num_bytes_read);
4702 : 4406 : const svalue *sval = get_store_bytes (reg, bytes_to_read, ctxt);
4703 : 4406 : if (out_sval)
4704 : 834 : *out_sval = sval;
4705 : 4406 : if (logger)
4706 : 0 : logger->log ("using string_cst");
4707 : 4406 : return m_mgr->get_or_create_int_cst (size_type_node,
4708 : 4406 : num_bytes_read);
4709 : : }
4710 : : }
4711 : : }
4712 : 3420 : const binding_cluster *cluster = m_store.get_cluster (base_reg);
4713 : 3420 : iterable_cluster c (cluster);
4714 : 3420 : if (logger)
4715 : : {
4716 : 0 : pretty_printer *pp = logger->get_printer ();
4717 : 0 : logger->start_log_line ();
4718 : 0 : c.dump_to_pp (pp);
4719 : 0 : logger->end_log_line ();
4720 : : }
4721 : :
4722 : 3420 : binding_map result (*store_mgr);
4723 : :
4724 : 133 : while (1)
4725 : : {
4726 : 3553 : fragment f;
4727 : 3553 : if (c.get_fragment_for_byte (src_byte_offset, &f))
4728 : : {
4729 : 538 : if (logger)
4730 : : {
4731 : 0 : logger->start_log_line ();
4732 : 0 : pretty_printer *pp = logger->get_printer ();
4733 : 0 : pp_printf (pp, "src_byte_offset: ");
4734 : 0 : pp_wide_int (pp, src_byte_offset, SIGNED);
4735 : 0 : pp_string (pp, ": ");
4736 : 0 : f.dump_to_pp (pp);
4737 : 0 : logger->end_log_line ();
4738 : : }
4739 : 538 : gcc_assert (f.m_byte_range.contains_p (src_byte_offset));
4740 : : /* src_byte_offset and f.m_byte_range are both expressed relative to
4741 : : the base region.
4742 : : Convert to a byte_range relative to the svalue. */
4743 : 538 : const byte_range bytes_relative_to_svalue
4744 : 538 : (src_byte_offset - f.m_byte_range.get_start_byte_offset (),
4745 : 538 : f.m_byte_range.get_next_byte_offset () - src_byte_offset);
4746 : 538 : byte_offset_t fragment_bytes_read;
4747 : 538 : tristate is_terminated
4748 : 538 : = svalue_byte_range_has_null_terminator (f.m_sval,
4749 : : bytes_relative_to_svalue,
4750 : : &fragment_bytes_read,
4751 : : logger);
4752 : 538 : if (is_terminated.is_unknown ())
4753 : : {
4754 : 220 : if (out_sval)
4755 : 2 : *out_sval = get_store_value (reg, nullptr);
4756 : 405 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4757 : : }
4758 : :
4759 : : /* Simulate reading those bytes from the store. */
4760 : 318 : byte_range bytes_to_read (src_byte_offset, fragment_bytes_read);
4761 : 318 : const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt);
4762 : 318 : check_for_poison (sval, expr, nullptr, ctxt);
4763 : :
4764 : 318 : if (out_sval)
4765 : : {
4766 : 8 : byte_range bytes_to_write (dst_byte_offset, fragment_bytes_read);
4767 : 8 : const binding_key *key
4768 : 8 : = store_mgr->get_concrete_binding (bytes_to_write);
4769 : 8 : result.put (key, sval);
4770 : : }
4771 : :
4772 : 318 : src_byte_offset += fragment_bytes_read;
4773 : 318 : dst_byte_offset += fragment_bytes_read;
4774 : :
4775 : 318 : if (is_terminated.is_true ())
4776 : : {
4777 : 185 : if (out_sval)
4778 : 5 : *out_sval = m_mgr->get_or_create_compound_svalue (NULL_TREE,
4779 : : result);
4780 : 185 : if (logger)
4781 : 0 : logger->log ("got terminator");
4782 : 185 : return m_mgr->get_or_create_int_cst (size_type_node,
4783 : 185 : dst_byte_offset);
4784 : : }
4785 : : }
4786 : : else
4787 : : break;
4788 : : }
4789 : :
4790 : : /* No binding for this base_region, or no binding at src_byte_offset
4791 : : (or a symbolic binding). */
4792 : :
4793 : 3015 : if (c.has_symbolic_bindings_p ())
4794 : : {
4795 : 194 : if (out_sval)
4796 : 75 : *out_sval = get_store_value (reg, nullptr);
4797 : 194 : if (logger)
4798 : 0 : logger->log ("got symbolic binding");
4799 : 194 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4800 : : }
4801 : :
4802 : : /* TODO: the various special-cases seen in
4803 : : region_model::get_store_value. */
4804 : :
4805 : : /* Simulate reading from this byte, then give up. */
4806 : 2821 : byte_range bytes_to_read (src_byte_offset, 1);
4807 : 2821 : const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt);
4808 : 2821 : tree byte_expr
4809 : : = (expr
4810 : 5516 : ? get_tree_for_byte_offset (expr,
4811 : : src_byte_offset - initial_src_byte_offset)
4812 : : : NULL_TREE);
4813 : 2821 : check_for_poison (sval, byte_expr, nullptr, ctxt);
4814 : 2821 : if (base_reg->can_have_initial_svalue_p ())
4815 : : {
4816 : 2621 : if (out_sval)
4817 : 264 : *out_sval = get_store_value (reg, nullptr);
4818 : 2621 : return m_mgr->get_or_create_unknown_svalue (size_type_node);
4819 : : }
4820 : : else
4821 : : return nullptr;
4822 : 6840 : }
4823 : :
4824 : : /* Like region_model::scan_for_null_terminator_1, but add logging. */
4825 : :
4826 : : const svalue *
4827 : 7941 : region_model::scan_for_null_terminator (const region *reg,
4828 : : tree expr,
4829 : : const svalue **out_sval,
4830 : : region_model_context *ctxt) const
4831 : : {
4832 : 7941 : logger *logger = ctxt ? ctxt->get_logger () : nullptr;
4833 : 7941 : LOG_SCOPE (logger);
4834 : 7941 : if (logger)
4835 : : {
4836 : 0 : pretty_printer *pp = logger->get_printer ();
4837 : 0 : logger->start_log_line ();
4838 : 0 : logger->log_partial ("region: ");
4839 : 0 : reg->dump_to_pp (pp, true);
4840 : 0 : logger->end_log_line ();
4841 : : }
4842 : 7941 : const svalue *sval = scan_for_null_terminator_1 (reg, expr, out_sval, ctxt);
4843 : 7941 : if (logger)
4844 : : {
4845 : 0 : pretty_printer *pp = logger->get_printer ();
4846 : 0 : logger->start_log_line ();
4847 : 0 : logger->log_partial ("length result: ");
4848 : 0 : if (sval)
4849 : 0 : sval->dump_to_pp (pp, true);
4850 : : else
4851 : 0 : pp_printf (pp, "NULL");
4852 : 0 : logger->end_log_line ();
4853 : 0 : if (out_sval)
4854 : : {
4855 : 0 : logger->start_log_line ();
4856 : 0 : logger->log_partial ("content result: ");
4857 : 0 : if (*out_sval)
4858 : 0 : (*out_sval)->dump_to_pp (pp, true);
4859 : : else
4860 : 0 : pp_printf (pp, "NULL");
4861 : 0 : logger->end_log_line ();
4862 : : }
4863 : : }
4864 : 15882 : return sval;
4865 : 7941 : }
4866 : :
4867 : : /* Check that argument ARG_IDX (0-based) to the call described by CD
4868 : : is a pointer to a valid null-terminated string.
4869 : :
4870 : : Simulate scanning through the buffer, reading until we find a 0 byte
4871 : : (equivalent to calling strlen).
4872 : :
4873 : : Complain and return nullptr if:
4874 : : - the buffer pointed to isn't null-terminated
4875 : : - the buffer pointed to has any uninitalized bytes before any 0-terminator
4876 : : - any of the reads aren't within the bounds of the underlying base region
4877 : :
4878 : : Otherwise, return a svalue for strlen of the buffer (*not* including
4879 : : the null terminator).
4880 : :
4881 : : TODO: we should also complain if:
4882 : : - the pointer is NULL (or could be). */
4883 : :
4884 : : const svalue *
4885 : 206 : region_model::check_for_null_terminated_string_arg (const call_details &cd,
4886 : : unsigned arg_idx) const
4887 : : {
4888 : 206 : return check_for_null_terminated_string_arg (cd,
4889 : : arg_idx,
4890 : : false, /* include_terminator */
4891 : 206 : nullptr); // out_sval
4892 : : }
4893 : :
4894 : :
4895 : : /* Check that argument ARG_IDX (0-based) to the call described by CD
4896 : : is a pointer to a valid null-terminated string.
4897 : :
4898 : : Simulate scanning through the buffer, reading until we find a 0 byte
4899 : : (equivalent to calling strlen).
4900 : :
4901 : : Complain and return nullptr if:
4902 : : - the buffer pointed to isn't null-terminated
4903 : : - the buffer pointed to has any uninitalized bytes before any 0-terminator
4904 : : - any of the reads aren't within the bounds of the underlying base region
4905 : :
4906 : : Otherwise, return a svalue. This will be the number of bytes read
4907 : : (including the null terminator) if INCLUDE_TERMINATOR is true, or strlen
4908 : : of the buffer (not including the null terminator) if it is false.
4909 : :
4910 : : Also, when returning an svalue, if OUT_SVAL is non-nullptr, write to
4911 : : *OUT_SVAL with an svalue representing the content of the buffer up to
4912 : : and including the terminator.
4913 : :
4914 : : TODO: we should also complain if:
4915 : : - the pointer is NULL (or could be). */
4916 : :
4917 : : const svalue *
4918 : 7470 : region_model::check_for_null_terminated_string_arg (const call_details &cd,
4919 : : unsigned arg_idx,
4920 : : bool include_terminator,
4921 : : const svalue **out_sval) const
4922 : : {
4923 : 0 : class null_terminator_check_event : public custom_event
4924 : : {
4925 : : public:
4926 : 158 : null_terminator_check_event (const event_loc_info &loc_info,
4927 : : const call_arg_details &arg_details)
4928 : 158 : : custom_event (loc_info),
4929 : 158 : m_arg_details (arg_details)
4930 : : {
4931 : : }
4932 : :
4933 : 292 : void print_desc (pretty_printer &pp) const final override
4934 : : {
4935 : 292 : if (m_arg_details.m_arg_expr)
4936 : 292 : pp_printf (&pp,
4937 : : "while looking for null terminator"
4938 : : " for argument %i (%qE) of %qD...",
4939 : 292 : m_arg_details.m_arg_idx + 1,
4940 : : m_arg_details.m_arg_expr,
4941 : 292 : m_arg_details.m_called_fndecl);
4942 : : else
4943 : 0 : pp_printf (&pp,
4944 : : "while looking for null terminator"
4945 : : " for argument %i of %qD...",
4946 : 0 : m_arg_details.m_arg_idx + 1,
4947 : 0 : m_arg_details.m_called_fndecl);
4948 : 292 : }
4949 : :
4950 : : private:
4951 : : const call_arg_details m_arg_details;
4952 : : };
4953 : :
4954 : 0 : class null_terminator_check_decl_note
4955 : : : public pending_note_subclass<null_terminator_check_decl_note>
4956 : : {
4957 : : public:
4958 : 158 : null_terminator_check_decl_note (const call_arg_details &arg_details)
4959 : 158 : : m_arg_details (arg_details)
4960 : : {
4961 : : }
4962 : :
4963 : 1276 : const char *get_kind () const final override
4964 : : {
4965 : 1276 : return "null_terminator_check_decl_note";
4966 : : }
4967 : :
4968 : 146 : void emit () const final override
4969 : : {
4970 : 146 : inform_about_expected_null_terminated_string_arg (m_arg_details);
4971 : 146 : }
4972 : :
4973 : 638 : bool operator== (const null_terminator_check_decl_note &other) const
4974 : : {
4975 : 638 : return m_arg_details == other.m_arg_details;
4976 : : }
4977 : :
4978 : : private:
4979 : : const call_arg_details m_arg_details;
4980 : : };
4981 : :
4982 : : /* Subclass of decorated_region_model_context that
4983 : : adds the above event and note to any saved diagnostics. */
4984 : 7470 : class annotating_ctxt : public annotating_context
4985 : : {
4986 : : public:
4987 : 7470 : annotating_ctxt (const call_details &cd,
4988 : : unsigned arg_idx)
4989 : 7470 : : annotating_context (cd.get_ctxt ()),
4990 : 7470 : m_cd (cd),
4991 : 7470 : m_arg_idx (arg_idx)
4992 : : {
4993 : : }
4994 : 158 : void add_annotations () final override
4995 : : {
4996 : 158 : call_arg_details arg_details (m_cd, m_arg_idx);
4997 : 316 : event_loc_info loc_info (m_cd.get_location (),
4998 : 158 : m_cd.get_model ()->get_current_function ()->decl,
4999 : 316 : m_cd.get_model ()->get_stack_depth ());
5000 : :
5001 : 158 : add_event
5002 : 158 : (std::make_unique<null_terminator_check_event> (loc_info,
5003 : : arg_details));
5004 : 158 : add_note
5005 : 158 : (std::make_unique <null_terminator_check_decl_note> (arg_details));
5006 : 158 : }
5007 : : private:
5008 : : const call_details &m_cd;
5009 : : unsigned m_arg_idx;
5010 : : };
5011 : :
5012 : : /* Use this ctxt below so that any diagnostics that get added
5013 : : get annotated. */
5014 : 7470 : annotating_ctxt my_ctxt (cd, arg_idx);
5015 : :
5016 : 7470 : const svalue *arg_sval = cd.get_arg_svalue (arg_idx);
5017 : 7470 : const region *buf_reg
5018 : 7470 : = deref_rvalue (arg_sval, cd.get_arg_tree (arg_idx), &my_ctxt);
5019 : :
5020 : 14940 : if (const svalue *num_bytes_read_sval
5021 : 7470 : = scan_for_null_terminator (buf_reg,
5022 : : cd.get_arg_tree (arg_idx),
5023 : : out_sval,
5024 : : &my_ctxt))
5025 : : {
5026 : 7308 : if (include_terminator)
5027 : : return num_bytes_read_sval;
5028 : : else
5029 : : {
5030 : : /* strlen is (bytes_read - 1). */
5031 : 6128 : const svalue *one = m_mgr->get_or_create_int_cst (size_type_node, 1);
5032 : 6128 : return m_mgr->get_or_create_binop (size_type_node,
5033 : : MINUS_EXPR,
5034 : : num_bytes_read_sval,
5035 : 6128 : one);
5036 : : }
5037 : : }
5038 : : else
5039 : : return nullptr;
5040 : : }
5041 : :
5042 : : /* Remove all bindings overlapping REG within the store. */
5043 : :
5044 : : void
5045 : 5724 : region_model::clobber_region (const region *reg)
5046 : : {
5047 : 5724 : m_store.clobber_region (m_mgr->get_store_manager(), reg);
5048 : 5724 : }
5049 : :
5050 : : /* Remove any bindings for REG within the store. */
5051 : :
5052 : : void
5053 : 203457 : region_model::purge_region (const region *reg)
5054 : : {
5055 : 203457 : m_store.purge_region (m_mgr->get_store_manager(), reg);
5056 : 203457 : }
5057 : :
5058 : : /* Fill REG with SVAL.
5059 : : Use CTXT to report any warnings associated with the write
5060 : : (e.g. out-of-bounds). */
5061 : :
5062 : : void
5063 : 656 : region_model::fill_region (const region *reg,
5064 : : const svalue *sval,
5065 : : region_model_context *ctxt)
5066 : : {
5067 : 656 : check_region_for_write (reg, nullptr, ctxt);
5068 : 656 : m_store.fill_region (m_mgr->get_store_manager(), reg, sval);
5069 : 656 : }
5070 : :
5071 : : /* Zero-fill REG.
5072 : : Use CTXT to report any warnings associated with the write
5073 : : (e.g. out-of-bounds). */
5074 : :
5075 : : void
5076 : 693 : region_model::zero_fill_region (const region *reg,
5077 : : region_model_context *ctxt)
5078 : : {
5079 : 693 : check_region_for_write (reg, nullptr, ctxt);
5080 : 693 : m_store.zero_fill_region (m_mgr->get_store_manager(), reg);
5081 : 693 : }
5082 : :
5083 : : /* Copy NUM_BYTES_SVAL of SVAL to DEST_REG.
5084 : : Use CTXT to report any warnings associated with the copy
5085 : : (e.g. out-of-bounds writes). */
5086 : :
5087 : : void
5088 : 2138 : region_model::write_bytes (const region *dest_reg,
5089 : : const svalue *num_bytes_sval,
5090 : : const svalue *sval,
5091 : : region_model_context *ctxt)
5092 : : {
5093 : 2138 : const region *sized_dest_reg
5094 : 2138 : = m_mgr->get_sized_region (dest_reg, NULL_TREE, num_bytes_sval);
5095 : 2138 : set_value (sized_dest_reg, sval, ctxt);
5096 : 2138 : }
5097 : :
5098 : : /* Read NUM_BYTES_SVAL from SRC_REG.
5099 : : Use CTXT to report any warnings associated with the copy
5100 : : (e.g. out-of-bounds reads, copying of uninitialized values, etc). */
5101 : :
5102 : : const svalue *
5103 : 1086 : region_model::read_bytes (const region *src_reg,
5104 : : tree src_ptr_expr,
5105 : : const svalue *num_bytes_sval,
5106 : : region_model_context *ctxt) const
5107 : : {
5108 : 1086 : if (num_bytes_sval->get_kind () == SK_UNKNOWN)
5109 : 188 : return m_mgr->get_or_create_unknown_svalue (NULL_TREE);
5110 : 898 : const region *sized_src_reg
5111 : 898 : = m_mgr->get_sized_region (src_reg, NULL_TREE, num_bytes_sval);
5112 : 898 : const svalue *src_contents_sval = get_store_value (sized_src_reg, ctxt);
5113 : 898 : check_for_poison (src_contents_sval, src_ptr_expr,
5114 : : sized_src_reg, ctxt);
5115 : 898 : return src_contents_sval;
5116 : : }
5117 : :
5118 : : /* Copy NUM_BYTES_SVAL bytes from SRC_REG to DEST_REG.
5119 : : Use CTXT to report any warnings associated with the copy
5120 : : (e.g. out-of-bounds reads/writes, copying of uninitialized values,
5121 : : etc). */
5122 : :
5123 : : void
5124 : 500 : region_model::copy_bytes (const region *dest_reg,
5125 : : const region *src_reg,
5126 : : tree src_ptr_expr,
5127 : : const svalue *num_bytes_sval,
5128 : : region_model_context *ctxt)
5129 : : {
5130 : 500 : const svalue *data_sval
5131 : 500 : = read_bytes (src_reg, src_ptr_expr, num_bytes_sval, ctxt);
5132 : 500 : write_bytes (dest_reg, num_bytes_sval, data_sval, ctxt);
5133 : 500 : }
5134 : :
5135 : : /* Mark REG as having unknown content. */
5136 : :
5137 : : void
5138 : 255 : region_model::mark_region_as_unknown (const region *reg,
5139 : : uncertainty_t *uncertainty)
5140 : : {
5141 : 255 : svalue_set maybe_live_values;
5142 : 255 : m_store.mark_region_as_unknown (m_mgr->get_store_manager(), reg,
5143 : : uncertainty, &maybe_live_values);
5144 : 255 : m_store.on_maybe_live_values (*m_mgr->get_store_manager (),
5145 : : maybe_live_values);
5146 : 255 : }
5147 : :
5148 : : /* Determine what is known about the condition "LHS_SVAL OP RHS_SVAL" within
5149 : : this model. */
5150 : :
5151 : : tristate
5152 : 204359 : region_model::eval_condition (const svalue *lhs,
5153 : : enum tree_code op,
5154 : : const svalue *rhs) const
5155 : : {
5156 : 204359 : gcc_assert (lhs);
5157 : 204359 : gcc_assert (rhs);
5158 : :
5159 : : /* For now, make no attempt to capture constraints on floating-point
5160 : : values. */
5161 : 204359 : if ((lhs->get_type () && FLOAT_TYPE_P (lhs->get_type ()))
5162 : 348915 : || (rhs->get_type () && FLOAT_TYPE_P (rhs->get_type ())))
5163 : 72 : return tristate::unknown ();
5164 : :
5165 : : /* See what we know based on the values. */
5166 : :
5167 : : /* Unwrap any unmergeable values. */
5168 : 204287 : lhs = lhs->unwrap_any_unmergeable ();
5169 : 204287 : rhs = rhs->unwrap_any_unmergeable ();
5170 : :
5171 : 204287 : if (lhs == rhs)
5172 : : {
5173 : : /* If we have the same svalue, then we have equality
5174 : : (apart from NaN-handling).
5175 : : TODO: should this definitely be the case for poisoned values? */
5176 : : /* Poisoned and unknown values are "unknowable". */
5177 : 20815 : if (lhs->get_kind () == SK_POISONED
5178 : 20815 : || lhs->get_kind () == SK_UNKNOWN)
5179 : 9593 : return tristate::TS_UNKNOWN;
5180 : :
5181 : 11222 : switch (op)
5182 : : {
5183 : 8285 : case EQ_EXPR:
5184 : 8285 : case GE_EXPR:
5185 : 8285 : case LE_EXPR:
5186 : 8285 : return tristate::TS_TRUE;
5187 : :
5188 : 2937 : case NE_EXPR:
5189 : 2937 : case GT_EXPR:
5190 : 2937 : case LT_EXPR:
5191 : 2937 : return tristate::TS_FALSE;
5192 : :
5193 : : default:
5194 : : /* For other ops, use the logic below. */
5195 : : break;
5196 : : }
5197 : : }
5198 : :
5199 : : /* If we have a pair of region_svalues, compare them. */
5200 : 183472 : if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ())
5201 : 18336 : if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ())
5202 : : {
5203 : 309 : tristate res = region_svalue::eval_condition (lhs_ptr, op, rhs_ptr);
5204 : 309 : if (res.is_known ())
5205 : 301 : return res;
5206 : : /* Otherwise, only known through constraints. */
5207 : : }
5208 : :
5209 : 183171 : if (const constant_svalue *cst_lhs = lhs->dyn_cast_constant_svalue ())
5210 : : {
5211 : : /* If we have a pair of constants, compare them. */
5212 : 46661 : if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
5213 : 11665 : return constant_svalue::eval_condition (cst_lhs, op, cst_rhs);
5214 : : else
5215 : : {
5216 : : /* When we have one constant, put it on the RHS. */
5217 : 34996 : std::swap (lhs, rhs);
5218 : 34996 : op = swap_tree_comparison (op);
5219 : : }
5220 : : }
5221 : 171506 : gcc_assert (lhs->get_kind () != SK_CONSTANT);
5222 : :
5223 : : /* Handle comparison against zero. */
5224 : 171506 : if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ())
5225 : 142155 : if (zerop (cst_rhs->get_constant ()))
5226 : : {
5227 : 87562 : if (const region_svalue *ptr = lhs->dyn_cast_region_svalue ())
5228 : : {
5229 : : /* A region_svalue is a non-NULL pointer, except in certain
5230 : : special cases (see the comment for region::non_null_p). */
5231 : 17856 : const region *pointee = ptr->get_pointee ();
5232 : 17856 : if (pointee->non_null_p ())
5233 : : {
5234 : 7903 : switch (op)
5235 : : {
5236 : 0 : default:
5237 : 0 : gcc_unreachable ();
5238 : :
5239 : 206 : case EQ_EXPR:
5240 : 206 : case GE_EXPR:
5241 : 206 : case LE_EXPR:
5242 : 206 : return tristate::TS_FALSE;
5243 : :
5244 : 7697 : case NE_EXPR:
5245 : 7697 : case GT_EXPR:
5246 : 7697 : case LT_EXPR:
5247 : 7697 : return tristate::TS_TRUE;
5248 : : }
5249 : : }
5250 : : }
5251 : 69706 : else if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ())
5252 : : {
5253 : : /* Treat offsets from a non-NULL pointer as being non-NULL. This
5254 : : isn't strictly true, in that eventually ptr++ will wrap
5255 : : around and be NULL, but it won't occur in practise and thus
5256 : : can be used to suppress effectively false positives that we
5257 : : shouldn't warn for. */
5258 : 16896 : if (binop->get_op () == POINTER_PLUS_EXPR)
5259 : : {
5260 : 9917 : tristate lhs_ts = eval_condition (binop->get_arg0 (), op, rhs);
5261 : 9917 : if (lhs_ts.is_known ())
5262 : 9228 : return lhs_ts;
5263 : : }
5264 : : }
5265 : 105620 : else if (const unaryop_svalue *unaryop
5266 : 52810 : = lhs->dyn_cast_unaryop_svalue ())
5267 : : {
5268 : 2587 : if (unaryop->get_op () == NEGATE_EXPR)
5269 : : {
5270 : : /* e.g. "-X <= 0" is equivalent to X >= 0". */
5271 : 49 : tristate lhs_ts = eval_condition (unaryop->get_arg (),
5272 : : swap_tree_comparison (op),
5273 : : rhs);
5274 : 49 : if (lhs_ts.is_known ())
5275 : 48 : return lhs_ts;
5276 : : }
5277 : : }
5278 : : }
5279 : :
5280 : : /* Handle rejection of equality for comparisons of the initial values of
5281 : : "external" values (such as params) with the address of locals. */
5282 : 154327 : if (const initial_svalue *init_lhs = lhs->dyn_cast_initial_svalue ())
5283 : 37289 : if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ())
5284 : : {
5285 : 97 : tristate res = compare_initial_and_pointer (init_lhs, rhs_ptr);
5286 : 97 : if (res.is_known ())
5287 : 32 : return res;
5288 : : }
5289 : 154295 : if (const initial_svalue *init_rhs = rhs->dyn_cast_initial_svalue ())
5290 : 5291 : if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ())
5291 : : {
5292 : 162 : tristate res = compare_initial_and_pointer (init_rhs, lhs_ptr);
5293 : 162 : if (res.is_known ())
5294 : 0 : return res;
5295 : : }
5296 : :
5297 : 154295 : if (const widening_svalue *widen_lhs = lhs->dyn_cast_widening_svalue ())
5298 : 5279 : if (tree rhs_cst = rhs->maybe_get_constant ())
5299 : : {
5300 : 2862 : tristate res = widen_lhs->eval_condition_without_cm (op, rhs_cst);
5301 : 2862 : if (res.is_known ())
5302 : 61 : return res;
5303 : : }
5304 : :
5305 : : /* Handle comparisons between two svalues with more than one operand. */
5306 : 154234 : if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ())
5307 : : {
5308 : 26539 : switch (op)
5309 : : {
5310 : : default:
5311 : : break;
5312 : 3340 : case EQ_EXPR:
5313 : 3340 : {
5314 : : /* TODO: binops can be equal even if they are not structurally
5315 : : equal in case of commutative operators. */
5316 : 3340 : tristate res = structural_equality (lhs, rhs);
5317 : 3340 : if (res.is_true ())
5318 : 44 : return res;
5319 : : }
5320 : 3296 : break;
5321 : 998 : case LE_EXPR:
5322 : 998 : {
5323 : 998 : tristate res = structural_equality (lhs, rhs);
5324 : 998 : if (res.is_true ())
5325 : 0 : return res;
5326 : : }
5327 : 998 : break;
5328 : 7507 : case GE_EXPR:
5329 : 7507 : {
5330 : 7507 : tristate res = structural_equality (lhs, rhs);
5331 : 7507 : if (res.is_true ())
5332 : 39 : return res;
5333 : 7468 : res = symbolic_greater_than (binop, rhs);
5334 : 7468 : if (res.is_true ())
5335 : 36 : return res;
5336 : : }
5337 : : break;
5338 : 8927 : case GT_EXPR:
5339 : 8927 : {
5340 : 8927 : tristate res = symbolic_greater_than (binop, rhs);
5341 : 8927 : if (res.is_true ())
5342 : 154 : return res;
5343 : : }
5344 : 8773 : break;
5345 : : }
5346 : : }
5347 : :
5348 : : /* Attempt to unwrap cast if there is one, and the types match. */
5349 : 153961 : tree lhs_type = lhs->get_type ();
5350 : 153961 : tree rhs_type = rhs->get_type ();
5351 : 153961 : if (lhs_type && rhs_type)
5352 : : {
5353 : 98763 : const unaryop_svalue *lhs_un_op = dyn_cast <const unaryop_svalue *> (lhs);
5354 : 98763 : const unaryop_svalue *rhs_un_op = dyn_cast <const unaryop_svalue *> (rhs);
5355 : 3387 : if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ())
5356 : 3197 : && rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ())
5357 : 98853 : && lhs_type == rhs_type)
5358 : : {
5359 : 90 : tristate res = eval_condition (lhs_un_op->get_arg (),
5360 : : op,
5361 : : rhs_un_op->get_arg ());
5362 : 90 : if (res.is_known ())
5363 : 0 : return res;
5364 : : }
5365 : 3297 : else if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ())
5366 : 101780 : && lhs_type == rhs_type)
5367 : : {
5368 : 2598 : tristate res = eval_condition (lhs_un_op->get_arg (), op, rhs);
5369 : 2598 : if (res.is_known ())
5370 : 35 : return res;
5371 : : }
5372 : 1937 : else if (rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ())
5373 : 98012 : && lhs_type == rhs_type)
5374 : : {
5375 : 1290 : tristate res = eval_condition (lhs, op, rhs_un_op->get_arg ());
5376 : 1290 : if (res.is_known ())
5377 : 0 : return res;
5378 : : }
5379 : : }
5380 : :
5381 : : /* Otherwise, try constraints.
5382 : : Cast to const to ensure we don't change the constraint_manager as we
5383 : : do this (e.g. by creating equivalence classes). */
5384 : 153926 : const constraint_manager *constraints = m_constraints;
5385 : 153926 : return constraints->eval_condition (lhs, op, rhs);
5386 : : }
5387 : :
5388 : : /* Subroutine of region_model::eval_condition, for rejecting
5389 : : equality of INIT_VAL(PARM) with &LOCAL. */
5390 : :
5391 : : tristate
5392 : 259 : region_model::compare_initial_and_pointer (const initial_svalue *init,
5393 : : const region_svalue *ptr) const
5394 : : {
5395 : 259 : const region *pointee = ptr->get_pointee ();
5396 : :
5397 : : /* If we have a pointer to something within a stack frame, it can't be the
5398 : : initial value of a param. */
5399 : 259 : if (pointee->maybe_get_frame_region ())
5400 : 32 : if (init->initial_value_of_param_p ())
5401 : 32 : return tristate::TS_FALSE;
5402 : :
5403 : 227 : return tristate::TS_UNKNOWN;
5404 : : }
5405 : :
5406 : : /* Return true if SVAL is definitely positive. */
5407 : :
5408 : : static bool
5409 : 15424 : is_positive_svalue (const svalue *sval)
5410 : : {
5411 : 15424 : if (tree cst = sval->maybe_get_constant ())
5412 : 14898 : return !zerop (cst) && get_range_pos_neg (cst) == 1;
5413 : 526 : tree type = sval->get_type ();
5414 : 526 : if (!type)
5415 : : return false;
5416 : : /* Consider a binary operation size_t + int. The analyzer wraps the int in
5417 : : an unaryop_svalue, converting it to a size_t, but in the dynamic execution
5418 : : the result is smaller than the first operand. Thus, we have to look if
5419 : : the argument of the unaryop_svalue is also positive. */
5420 : 392 : if (const unaryop_svalue *un_op = dyn_cast <const unaryop_svalue *> (sval))
5421 : 12 : return CONVERT_EXPR_CODE_P (un_op->get_op ()) && TYPE_UNSIGNED (type)
5422 : 20 : && is_positive_svalue (un_op->get_arg ());
5423 : 380 : return TYPE_UNSIGNED (type);
5424 : : }
5425 : :
5426 : : /* Return true if A is definitely larger than B.
5427 : :
5428 : : Limitation: does not account for integer overflows and does not try to
5429 : : return false, so it can not be used negated. */
5430 : :
5431 : : tristate
5432 : 16395 : region_model::symbolic_greater_than (const binop_svalue *bin_a,
5433 : : const svalue *b) const
5434 : : {
5435 : 16395 : if (bin_a->get_op () == PLUS_EXPR || bin_a->get_op () == MULT_EXPR)
5436 : : {
5437 : : /* Eliminate the right-hand side of both svalues. */
5438 : 15456 : if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b))
5439 : 2750 : if (bin_a->get_op () == bin_b->get_op ()
5440 : 1528 : && eval_condition (bin_a->get_arg1 (),
5441 : : GT_EXPR,
5442 : 1528 : bin_b->get_arg1 ()).is_true ()
5443 : 4278 : && eval_condition (bin_a->get_arg0 (),
5444 : : GE_EXPR,
5445 : 65 : bin_b->get_arg0 ()).is_true ())
5446 : 40 : return tristate (tristate::TS_TRUE);
5447 : :
5448 : : /* Otherwise, try to remove a positive offset or factor from BIN_A. */
5449 : 15416 : if (is_positive_svalue (bin_a->get_arg1 ())
5450 : 15416 : && eval_condition (bin_a->get_arg0 (),
5451 : 14494 : GE_EXPR, b).is_true ())
5452 : 150 : return tristate (tristate::TS_TRUE);
5453 : : }
5454 : 16205 : return tristate::unknown ();
5455 : : }
5456 : :
5457 : : /* Return true if A and B are equal structurally.
5458 : :
5459 : : Structural equality means that A and B are equal if the svalues A and B have
5460 : : the same nodes at the same positions in the tree and the leafs are equal.
5461 : : Equality for conjured_svalues and initial_svalues is determined by comparing
5462 : : the pointers while constants are compared by value. That behavior is useful
5463 : : to check for binaryop_svlaues that evaluate to the same concrete value but
5464 : : might use one operand with a different type but the same constant value.
5465 : :
5466 : : For example,
5467 : : binop_svalue (mult_expr,
5468 : : initial_svalue (‘size_t’, decl_region (..., 'some_var')),
5469 : : constant_svalue (‘size_t’, 4))
5470 : : and
5471 : : binop_svalue (mult_expr,
5472 : : initial_svalue (‘size_t’, decl_region (..., 'some_var'),
5473 : : constant_svalue (‘sizetype’, 4))
5474 : : are structurally equal. A concrete C code example, where this occurs, can
5475 : : be found in test7 of out-of-bounds-5.c. */
5476 : :
5477 : : tristate
5478 : 14777 : region_model::structural_equality (const svalue *a, const svalue *b) const
5479 : : {
5480 : : /* If A and B are referentially equal, they are also structurally equal. */
5481 : 14777 : if (a == b)
5482 : 388 : return tristate (tristate::TS_TRUE);
5483 : :
5484 : 14389 : switch (a->get_kind ())
5485 : : {
5486 : 1316 : default:
5487 : 1316 : return tristate::unknown ();
5488 : : /* SK_CONJURED and SK_INITIAL are already handled
5489 : : by the referential equality above. */
5490 : 1097 : case SK_CONSTANT:
5491 : 1097 : {
5492 : 1097 : tree a_cst = a->maybe_get_constant ();
5493 : 1097 : tree b_cst = b->maybe_get_constant ();
5494 : 1097 : if (a_cst && b_cst)
5495 : 1962 : return tristate (tree_int_cst_equal (a_cst, b_cst));
5496 : : }
5497 : 95 : return tristate (tristate::TS_FALSE);
5498 : 11 : case SK_UNARYOP:
5499 : 11 : {
5500 : 11 : const unaryop_svalue *un_a = as_a <const unaryop_svalue *> (a);
5501 : 11 : if (const unaryop_svalue *un_b = dyn_cast <const unaryop_svalue *> (b))
5502 : 8 : return tristate (pending_diagnostic::same_tree_p (un_a->get_type (),
5503 : : un_b->get_type ())
5504 : 8 : && un_a->get_op () == un_b->get_op ()
5505 : : && structural_equality (un_a->get_arg (),
5506 : 16 : un_b->get_arg ()));
5507 : : }
5508 : 3 : return tristate (tristate::TS_FALSE);
5509 : 11965 : case SK_BINOP:
5510 : 11965 : {
5511 : 11965 : const binop_svalue *bin_a = as_a <const binop_svalue *> (a);
5512 : 11965 : if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b))
5513 : 2481 : return tristate (bin_a->get_op () == bin_b->get_op ()
5514 : : && structural_equality (bin_a->get_arg0 (),
5515 : 2924 : bin_b->get_arg0 ())
5516 : : && structural_equality (bin_a->get_arg1 (),
5517 : 2924 : bin_b->get_arg1 ()));
5518 : : }
5519 : 10503 : return tristate (tristate::TS_FALSE);
5520 : : }
5521 : : }
5522 : :
5523 : : /* Handle various constraints of the form:
5524 : : LHS: ((bool)INNER_LHS INNER_OP INNER_RHS))
5525 : : OP : == or !=
5526 : : RHS: zero
5527 : : and (with a cast):
5528 : : LHS: CAST([long]int, ((bool)INNER_LHS INNER_OP INNER_RHS))
5529 : : OP : == or !=
5530 : : RHS: zero
5531 : : by adding constraints for INNER_LHS INNEROP INNER_RHS.
5532 : :
5533 : : Return true if this function can fully handle the constraint; if
5534 : : so, add the implied constraint(s) and write true to *OUT if they
5535 : : are consistent with existing constraints, or write false to *OUT
5536 : : if they contradicts existing constraints.
5537 : :
5538 : : Return false for cases that this function doeesn't know how to handle.
5539 : :
5540 : : For example, if we're checking a stored conditional, we'll have
5541 : : something like:
5542 : : LHS: CAST(long int, (&HEAP_ALLOCATED_REGION(8)!=(int *)0B))
5543 : : OP : NE_EXPR
5544 : : RHS: zero
5545 : : which this function can turn into an add_constraint of:
5546 : : (&HEAP_ALLOCATED_REGION(8) != (int *)0B)
5547 : :
5548 : : Similarly, optimized && and || conditionals lead to e.g.
5549 : : if (p && q)
5550 : : becoming gimple like this:
5551 : : _1 = p_6 == 0B;
5552 : : _2 = q_8 == 0B
5553 : : _3 = _1 | _2
5554 : : On the "_3 is false" branch we can have constraints of the form:
5555 : : ((&HEAP_ALLOCATED_REGION(8)!=(int *)0B)
5556 : : | (&HEAP_ALLOCATED_REGION(10)!=(int *)0B))
5557 : : == 0
5558 : : which implies that both _1 and _2 are false,
5559 : : which this function can turn into a pair of add_constraints of
5560 : : (&HEAP_ALLOCATED_REGION(8)!=(int *)0B)
5561 : : and:
5562 : : (&HEAP_ALLOCATED_REGION(10)!=(int *)0B). */
5563 : :
5564 : : bool
5565 : 52677 : region_model::add_constraints_from_binop (const svalue *outer_lhs,
5566 : : enum tree_code outer_op,
5567 : : const svalue *outer_rhs,
5568 : : bool *out,
5569 : : region_model_context *ctxt)
5570 : : {
5571 : 54560 : while (const svalue *cast = outer_lhs->maybe_undo_cast ())
5572 : : outer_lhs = cast;
5573 : 52677 : const binop_svalue *binop_sval = outer_lhs->dyn_cast_binop_svalue ();
5574 : 52677 : if (!binop_sval)
5575 : : return false;
5576 : 7534 : if (!outer_rhs->all_zeroes_p ())
5577 : : return false;
5578 : :
5579 : 5175 : const svalue *inner_lhs = binop_sval->get_arg0 ();
5580 : 5175 : enum tree_code inner_op = binop_sval->get_op ();
5581 : 5175 : const svalue *inner_rhs = binop_sval->get_arg1 ();
5582 : :
5583 : 5175 : if (outer_op != NE_EXPR && outer_op != EQ_EXPR)
5584 : : return false;
5585 : :
5586 : : /* We have either
5587 : : - "OUTER_LHS != false" (i.e. OUTER is true), or
5588 : : - "OUTER_LHS == false" (i.e. OUTER is false). */
5589 : 4617 : bool is_true = outer_op == NE_EXPR;
5590 : :
5591 : 4617 : switch (inner_op)
5592 : : {
5593 : : default:
5594 : : return false;
5595 : :
5596 : 2536 : case EQ_EXPR:
5597 : 2536 : case NE_EXPR:
5598 : 2536 : case GE_EXPR:
5599 : 2536 : case GT_EXPR:
5600 : 2536 : case LE_EXPR:
5601 : 2536 : case LT_EXPR:
5602 : 2536 : {
5603 : : /* ...and "(inner_lhs OP inner_rhs) == 0"
5604 : : then (inner_lhs OP inner_rhs) must have the same
5605 : : logical value as LHS. */
5606 : 2536 : if (!is_true)
5607 : 1225 : inner_op = invert_tree_comparison (inner_op, false /* honor_nans */);
5608 : 2536 : *out = add_constraint (inner_lhs, inner_op, inner_rhs, ctxt);
5609 : 2536 : return true;
5610 : : }
5611 : 849 : break;
5612 : :
5613 : 849 : case BIT_AND_EXPR:
5614 : 849 : if (is_true)
5615 : : {
5616 : : /* ...and "(inner_lhs & inner_rhs) != 0"
5617 : : then both inner_lhs and inner_rhs must be true. */
5618 : 435 : const svalue *false_sval
5619 : 435 : = m_mgr->get_or_create_constant_svalue (boolean_false_node);
5620 : 435 : bool sat1 = add_constraint (inner_lhs, NE_EXPR, false_sval, ctxt);
5621 : 435 : bool sat2 = add_constraint (inner_rhs, NE_EXPR, false_sval, ctxt);
5622 : 435 : *out = sat1 && sat2;
5623 : 435 : return true;
5624 : : }
5625 : : return false;
5626 : :
5627 : 495 : case BIT_IOR_EXPR:
5628 : 495 : if (!is_true)
5629 : : {
5630 : : /* ...and "(inner_lhs | inner_rhs) == 0"
5631 : : i.e. "(inner_lhs | inner_rhs)" is false
5632 : : then both inner_lhs and inner_rhs must be false. */
5633 : 278 : const svalue *false_sval
5634 : 278 : = m_mgr->get_or_create_constant_svalue (boolean_false_node);
5635 : 278 : bool sat1 = add_constraint (inner_lhs, EQ_EXPR, false_sval, ctxt);
5636 : 278 : bool sat2 = add_constraint (inner_rhs, EQ_EXPR, false_sval, ctxt);
5637 : 278 : *out = sat1 && sat2;
5638 : 278 : return true;
5639 : : }
5640 : : return false;
5641 : : }
5642 : : }
5643 : :
5644 : : /* Attempt to add the constraint "LHS OP RHS" to this region_model.
5645 : : If it is consistent with existing constraints, add it, and return true.
5646 : : Return false if it contradicts existing constraints.
5647 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5648 : :
5649 : : bool
5650 : 69648 : region_model::add_constraint (tree lhs, enum tree_code op, tree rhs,
5651 : : region_model_context *ctxt)
5652 : : {
5653 : : /* For now, make no attempt to capture constraints on floating-point
5654 : : values. */
5655 : 69648 : if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
5656 : : return true;
5657 : :
5658 : 69278 : const svalue *lhs_sval = get_rvalue (lhs, ctxt);
5659 : 69278 : const svalue *rhs_sval = get_rvalue (rhs, ctxt);
5660 : :
5661 : 69278 : return add_constraint (lhs_sval, op, rhs_sval, ctxt);
5662 : : }
5663 : :
5664 : : static bool
5665 : 11097 : unusable_in_infinite_loop_constraint_p (const svalue *sval)
5666 : : {
5667 : 11097 : if (sval->get_kind () == SK_WIDENING)
5668 : 0 : return true;
5669 : : return false;
5670 : : }
5671 : :
5672 : : /* Attempt to add the constraint "LHS OP RHS" to this region_model.
5673 : : If it is consistent with existing constraints, add it, and return true.
5674 : : Return false if it contradicts existing constraints.
5675 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5676 : :
5677 : : bool
5678 : 77523 : region_model::add_constraint (const svalue *lhs,
5679 : : enum tree_code op,
5680 : : const svalue *rhs,
5681 : : region_model_context *ctxt)
5682 : : {
5683 : 77523 : const bool checking_for_infinite_loop
5684 : 77523 : = ctxt ? ctxt->checking_for_infinite_loop_p () : false;
5685 : :
5686 : 5665 : if (checking_for_infinite_loop)
5687 : : {
5688 : 11097 : if (unusable_in_infinite_loop_constraint_p (lhs)
5689 : 77523 : || unusable_in_infinite_loop_constraint_p (rhs))
5690 : : {
5691 : 236 : gcc_assert (ctxt);
5692 : 236 : ctxt->on_unusable_in_infinite_loop ();
5693 : 236 : return false;
5694 : : }
5695 : : }
5696 : :
5697 : 77287 : tristate t_cond = eval_condition (lhs, op, rhs);
5698 : :
5699 : : /* If we already have the condition, do nothing. */
5700 : 77287 : if (t_cond.is_true ())
5701 : : return true;
5702 : :
5703 : : /* Reject a constraint that would contradict existing knowledge, as
5704 : : unsatisfiable. */
5705 : 65235 : if (t_cond.is_false ())
5706 : : return false;
5707 : :
5708 : 56159 : if (checking_for_infinite_loop)
5709 : : {
5710 : : /* Here, we don't have a definite true/false value, so bail out
5711 : : when checking for infinite loops. */
5712 : 3482 : gcc_assert (ctxt);
5713 : 3482 : ctxt->on_unusable_in_infinite_loop ();
5714 : 3482 : return false;
5715 : : }
5716 : :
5717 : 52677 : bool out;
5718 : 52677 : if (add_constraints_from_binop (lhs, op, rhs, &out, ctxt))
5719 : 3249 : return out;
5720 : :
5721 : : /* Attempt to store the constraint. */
5722 : 49428 : if (!m_constraints->add_constraint (lhs, op, rhs))
5723 : : return false;
5724 : :
5725 : : /* Notify the context, if any. This exists so that the state machines
5726 : : in a program_state can be notified about the condition, and so can
5727 : : set sm-state for e.g. unchecked->checked, both for cfg-edges, and
5728 : : when synthesizing constraints as above. */
5729 : 49324 : if (ctxt)
5730 : 33775 : ctxt->on_condition (lhs, op, rhs);
5731 : :
5732 : : /* If we have ®ION == NULL, then drop dynamic extents for REGION (for
5733 : : the case where REGION is heap-allocated and thus could be NULL). */
5734 : 49324 : if (tree rhs_cst = rhs->maybe_get_constant ())
5735 : 40705 : if (op == EQ_EXPR && zerop (rhs_cst))
5736 : 12873 : if (const region_svalue *region_sval = lhs->dyn_cast_region_svalue ())
5737 : 1741 : unset_dynamic_extents (region_sval->get_pointee ());
5738 : :
5739 : : return true;
5740 : : }
5741 : :
5742 : : /* As above, but when returning false, if OUT is non-NULL, write a
5743 : : new rejected_constraint to *OUT. */
5744 : :
5745 : : bool
5746 : 68506 : region_model::add_constraint (tree lhs, enum tree_code op, tree rhs,
5747 : : region_model_context *ctxt,
5748 : : std::unique_ptr<rejected_constraint> *out)
5749 : : {
5750 : 68506 : bool sat = add_constraint (lhs, op, rhs, ctxt);
5751 : 68506 : if (!sat && out)
5752 : 2096 : *out = std::make_unique <rejected_op_constraint> (*this, lhs, op, rhs);
5753 : 68506 : return sat;
5754 : : }
5755 : :
5756 : : /* Determine what is known about the condition "LHS OP RHS" within
5757 : : this model.
5758 : : Use CTXT for reporting any diagnostics associated with the accesses. */
5759 : :
5760 : : tristate
5761 : 31303 : region_model::eval_condition (tree lhs,
5762 : : enum tree_code op,
5763 : : tree rhs,
5764 : : region_model_context *ctxt) const
5765 : : {
5766 : : /* For now, make no attempt to model constraints on floating-point
5767 : : values. */
5768 : 31303 : if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
5769 : 16 : return tristate::unknown ();
5770 : :
5771 : 31287 : return eval_condition (get_rvalue (lhs, ctxt), op, get_rvalue (rhs, ctxt));
5772 : : }
5773 : :
5774 : : /* Implementation of region_model::get_representative_path_var.
5775 : : Attempt to return a path_var that represents SVAL, or return NULL_TREE.
5776 : : Use VISITED to prevent infinite mutual recursion with the overload for
5777 : : regions. */
5778 : :
5779 : : path_var
5780 : 12878 : region_model::get_representative_path_var_1 (const svalue *sval,
5781 : : svalue_set *visited,
5782 : : logger *logger) const
5783 : : {
5784 : 12878 : gcc_assert (sval);
5785 : :
5786 : : /* Prevent infinite recursion. */
5787 : 12878 : if (visited->contains (sval))
5788 : : {
5789 : 14 : if (sval->get_kind () == SK_CONSTANT)
5790 : 14 : return path_var (sval->maybe_get_constant (), 0);
5791 : : else
5792 : 0 : return path_var (NULL_TREE, 0);
5793 : : }
5794 : 12864 : visited->add (sval);
5795 : :
5796 : : /* Handle casts by recursion into get_representative_path_var. */
5797 : 12864 : if (const svalue *cast_sval = sval->maybe_undo_cast ())
5798 : : {
5799 : 402 : path_var result = get_representative_path_var (cast_sval, visited,
5800 : : logger);
5801 : 402 : tree orig_type = sval->get_type ();
5802 : : /* If necessary, wrap the result in a cast. */
5803 : 402 : if (result.m_tree && orig_type)
5804 : 332 : result.m_tree = build1 (NOP_EXPR, orig_type, result.m_tree);
5805 : 402 : return result;
5806 : : }
5807 : :
5808 : 12462 : auto_vec<path_var> pvs;
5809 : 12462 : m_store.get_representative_path_vars (this, visited, sval, logger, &pvs);
5810 : :
5811 : 12462 : if (tree cst = sval->maybe_get_constant ())
5812 : 1794 : pvs.safe_push (path_var (cst, 0));
5813 : :
5814 : : /* Handle string literals and various other pointers. */
5815 : 12462 : if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
5816 : : {
5817 : 4370 : const region *reg = ptr_sval->get_pointee ();
5818 : 4370 : if (path_var pv = get_representative_path_var (reg, visited, logger))
5819 : 30 : return path_var (build1 (ADDR_EXPR,
5820 : : sval->get_type (),
5821 : : pv.m_tree),
5822 : 30 : pv.m_stack_depth);
5823 : : }
5824 : :
5825 : : /* If we have a sub_svalue, look for ways to represent the parent. */
5826 : 12432 : if (const sub_svalue *sub_sval = sval->dyn_cast_sub_svalue ())
5827 : : {
5828 : 370 : const svalue *parent_sval = sub_sval->get_parent ();
5829 : 370 : const region *subreg = sub_sval->get_subregion ();
5830 : 740 : if (path_var parent_pv
5831 : 370 : = get_representative_path_var (parent_sval, visited, logger))
5832 : 153 : if (const field_region *field_reg = subreg->dyn_cast_field_region ())
5833 : 111 : return path_var (build3 (COMPONENT_REF,
5834 : : sval->get_type (),
5835 : : parent_pv.m_tree,
5836 : : field_reg->get_field (),
5837 : : NULL_TREE),
5838 : 111 : parent_pv.m_stack_depth);
5839 : : }
5840 : :
5841 : : /* Handle binops. */
5842 : 12321 : if (const binop_svalue *binop_sval = sval->dyn_cast_binop_svalue ())
5843 : 534 : if (path_var lhs_pv
5844 : 534 : = get_representative_path_var (binop_sval->get_arg0 (), visited,
5845 : 534 : logger))
5846 : 468 : if (path_var rhs_pv
5847 : 468 : = get_representative_path_var (binop_sval->get_arg1 (), visited,
5848 : 468 : logger))
5849 : 439 : return path_var (build2 (binop_sval->get_op (),
5850 : : sval->get_type (),
5851 : : lhs_pv.m_tree, rhs_pv.m_tree),
5852 : 439 : lhs_pv.m_stack_depth);
5853 : :
5854 : 11882 : if (pvs.length () < 1)
5855 : 2065 : return path_var (NULL_TREE, 0);
5856 : :
5857 : 9817 : pvs.qsort (readability_comparator);
5858 : 9817 : return pvs[0];
5859 : 12462 : }
5860 : :
5861 : : /* Attempt to return a path_var that represents SVAL, or return NULL_TREE.
5862 : : Use VISITED to prevent infinite mutual recursion with the overload for
5863 : : regions
5864 : :
5865 : : This function defers to get_representative_path_var_1 to do the work;
5866 : : it adds verification that get_representative_path_var_1 returned a tree
5867 : : of the correct type. */
5868 : :
5869 : : path_var
5870 : 18182 : region_model::get_representative_path_var (const svalue *sval,
5871 : : svalue_set *visited,
5872 : : logger *logger) const
5873 : : {
5874 : 18182 : if (sval == nullptr)
5875 : 5304 : return path_var (NULL_TREE, 0);
5876 : :
5877 : 12878 : LOG_SCOPE (logger);
5878 : 12878 : if (logger)
5879 : : {
5880 : 0 : logger->start_log_line ();
5881 : 0 : logger->log_partial ("sval: ");
5882 : 0 : sval->dump_to_pp (logger->get_printer (), true);
5883 : 0 : logger->end_log_line ();
5884 : : }
5885 : :
5886 : 12878 : tree orig_type = sval->get_type ();
5887 : :
5888 : 12878 : path_var result = get_representative_path_var_1 (sval, visited, logger);
5889 : :
5890 : : /* Verify that the result has the same type as SVAL, if any. */
5891 : 12878 : if (result.m_tree && orig_type)
5892 : 10655 : gcc_assert (TREE_TYPE (result.m_tree) == orig_type);
5893 : :
5894 : 12878 : if (logger)
5895 : : {
5896 : 0 : logger->start_log_line ();
5897 : 0 : logger->log_partial ("sval: ");
5898 : 0 : sval->dump_to_pp (logger->get_printer (), true);
5899 : 0 : logger->end_log_line ();
5900 : :
5901 : 0 : if (result.m_tree)
5902 : 0 : logger->log ("tree: %qE", result.m_tree);
5903 : : else
5904 : 0 : logger->log ("tree: NULL");
5905 : : }
5906 : :
5907 : 12878 : return result;
5908 : 12878 : }
5909 : :
5910 : : /* Attempt to return a tree that represents SVAL, or return NULL_TREE.
5911 : :
5912 : : Strip off any top-level cast, to avoid messages like
5913 : : double-free of '(void *)ptr'
5914 : : from analyzer diagnostics. */
5915 : :
5916 : : tree
5917 : 14378 : region_model::get_representative_tree (const svalue *sval, logger *logger) const
5918 : : {
5919 : 14378 : svalue_set visited;
5920 : 14378 : tree expr = get_representative_path_var (sval, &visited, logger).m_tree;
5921 : :
5922 : : /* Strip off any top-level cast. */
5923 : 14378 : if (expr && TREE_CODE (expr) == NOP_EXPR)
5924 : 457 : expr = TREE_OPERAND (expr, 0);
5925 : :
5926 : 14378 : return fixup_tree_for_diagnostic (expr);
5927 : 14378 : }
5928 : :
5929 : : tree
5930 : 770 : region_model::get_representative_tree (const region *reg, logger *logger) const
5931 : : {
5932 : 770 : svalue_set visited;
5933 : 770 : tree expr = get_representative_path_var (reg, &visited, logger).m_tree;
5934 : :
5935 : : /* Strip off any top-level cast. */
5936 : 770 : if (expr && TREE_CODE (expr) == NOP_EXPR)
5937 : 0 : expr = TREE_OPERAND (expr, 0);
5938 : :
5939 : 770 : return fixup_tree_for_diagnostic (expr);
5940 : 770 : }
5941 : :
5942 : : /* Implementation of region_model::get_representative_path_var.
5943 : :
5944 : : Attempt to return a path_var that represents REG, or return
5945 : : the NULL path_var.
5946 : : For example, a region for a field of a local would be a path_var
5947 : : wrapping a COMPONENT_REF.
5948 : : Use VISITED to prevent infinite mutual recursion with the overload for
5949 : : svalues. */
5950 : :
5951 : : path_var
5952 : 15488 : region_model::get_representative_path_var_1 (const region *reg,
5953 : : svalue_set *visited,
5954 : : logger *logger) const
5955 : : {
5956 : 15488 : switch (reg->get_kind ())
5957 : : {
5958 : 0 : default:
5959 : 0 : gcc_unreachable ();
5960 : :
5961 : 0 : case RK_FRAME:
5962 : 0 : case RK_GLOBALS:
5963 : 0 : case RK_CODE:
5964 : 0 : case RK_HEAP:
5965 : 0 : case RK_STACK:
5966 : 0 : case RK_THREAD_LOCAL:
5967 : 0 : case RK_ROOT:
5968 : : /* Regions that represent memory spaces are not expressible as trees. */
5969 : 0 : return path_var (NULL_TREE, 0);
5970 : :
5971 : 1 : case RK_FUNCTION:
5972 : 1 : {
5973 : 1 : const function_region *function_reg
5974 : 1 : = as_a <const function_region *> (reg);
5975 : 1 : return path_var (function_reg->get_fndecl (), 0);
5976 : : }
5977 : 1 : case RK_LABEL:
5978 : 1 : {
5979 : 1 : const label_region *label_reg = as_a <const label_region *> (reg);
5980 : 1 : return path_var (label_reg->get_label (), 0);
5981 : : }
5982 : :
5983 : 225 : case RK_SYMBOLIC:
5984 : 225 : {
5985 : 225 : const symbolic_region *symbolic_reg
5986 : 225 : = as_a <const symbolic_region *> (reg);
5987 : 225 : const svalue *pointer = symbolic_reg->get_pointer ();
5988 : 225 : path_var pointer_pv = get_representative_path_var (pointer, visited,
5989 : : logger);
5990 : 225 : if (!pointer_pv)
5991 : 16 : return path_var (NULL_TREE, 0);
5992 : 209 : tree offset = build_int_cst (pointer->get_type (), 0);
5993 : 209 : return path_var (build2 (MEM_REF,
5994 : : reg->get_type (),
5995 : : pointer_pv.m_tree,
5996 : : offset),
5997 : 209 : pointer_pv.m_stack_depth);
5998 : : }
5999 : 9717 : case RK_DECL:
6000 : 9717 : {
6001 : 9717 : const decl_region *decl_reg = as_a <const decl_region *> (reg);
6002 : 9717 : return path_var (decl_reg->get_decl (), decl_reg->get_stack_depth ());
6003 : : }
6004 : 777 : case RK_FIELD:
6005 : 777 : {
6006 : 777 : const field_region *field_reg = as_a <const field_region *> (reg);
6007 : 777 : path_var parent_pv
6008 : 777 : = get_representative_path_var (reg->get_parent_region (), visited,
6009 : : logger);
6010 : 777 : if (!parent_pv)
6011 : 37 : return path_var (NULL_TREE, 0);
6012 : 740 : return path_var (build3 (COMPONENT_REF,
6013 : : reg->get_type (),
6014 : : parent_pv.m_tree,
6015 : : field_reg->get_field (),
6016 : : NULL_TREE),
6017 : 740 : parent_pv.m_stack_depth);
6018 : : }
6019 : :
6020 : 150 : case RK_ELEMENT:
6021 : 150 : {
6022 : 150 : const element_region *element_reg
6023 : 150 : = as_a <const element_region *> (reg);
6024 : 150 : path_var parent_pv
6025 : 150 : = get_representative_path_var (reg->get_parent_region (), visited,
6026 : : logger);
6027 : 150 : if (!parent_pv)
6028 : 0 : return path_var (NULL_TREE, 0);
6029 : 150 : path_var index_pv
6030 : 150 : = get_representative_path_var (element_reg->get_index (), visited,
6031 : : logger);
6032 : 150 : if (!index_pv)
6033 : 0 : return path_var (NULL_TREE, 0);
6034 : 150 : return path_var (build4 (ARRAY_REF,
6035 : : reg->get_type (),
6036 : : parent_pv.m_tree, index_pv.m_tree,
6037 : : NULL_TREE, NULL_TREE),
6038 : 150 : parent_pv.m_stack_depth);
6039 : : }
6040 : :
6041 : 42 : case RK_OFFSET:
6042 : 42 : {
6043 : 42 : const offset_region *offset_reg
6044 : 42 : = as_a <const offset_region *> (reg);
6045 : 42 : path_var parent_pv
6046 : 42 : = get_representative_path_var (reg->get_parent_region (), visited,
6047 : : logger);
6048 : 42 : if (!parent_pv)
6049 : 0 : return path_var (NULL_TREE, 0);
6050 : 42 : path_var offset_pv
6051 : 42 : = get_representative_path_var (offset_reg->get_byte_offset (),
6052 : : visited, logger);
6053 : 42 : if (!offset_pv || TREE_CODE (offset_pv.m_tree) != INTEGER_CST)
6054 : 42 : return path_var (NULL_TREE, 0);
6055 : 0 : tree addr_parent = build1 (ADDR_EXPR,
6056 : : build_pointer_type (reg->get_type ()),
6057 : : parent_pv.m_tree);
6058 : 0 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode,
6059 : : true);
6060 : 0 : return path_var (build2 (MEM_REF, reg->get_type (), addr_parent,
6061 : : fold_convert (ptype, offset_pv.m_tree)),
6062 : 0 : parent_pv.m_stack_depth);
6063 : : }
6064 : :
6065 : 42 : case RK_SIZED:
6066 : 42 : return path_var (NULL_TREE, 0);
6067 : :
6068 : 5 : case RK_CAST:
6069 : 5 : {
6070 : 5 : path_var parent_pv
6071 : 5 : = get_representative_path_var (reg->get_parent_region (), visited,
6072 : : logger);
6073 : 5 : if (!parent_pv)
6074 : 0 : return path_var (NULL_TREE, 0);
6075 : 5 : return path_var (build1 (NOP_EXPR,
6076 : : reg->get_type (),
6077 : : parent_pv.m_tree),
6078 : 5 : parent_pv.m_stack_depth);
6079 : : }
6080 : :
6081 : 4514 : case RK_HEAP_ALLOCATED:
6082 : 4514 : case RK_ALLOCA:
6083 : : /* No good way to express heap-allocated/alloca regions as trees. */
6084 : 4514 : return path_var (NULL_TREE, 0);
6085 : :
6086 : 10 : case RK_STRING:
6087 : 10 : {
6088 : 10 : const string_region *string_reg = as_a <const string_region *> (reg);
6089 : 10 : return path_var (string_reg->get_string_cst (), 0);
6090 : : }
6091 : :
6092 : 4 : case RK_VAR_ARG:
6093 : 4 : case RK_ERRNO:
6094 : 4 : case RK_UNKNOWN:
6095 : 4 : case RK_PRIVATE:
6096 : 4 : return path_var (NULL_TREE, 0);
6097 : : }
6098 : : }
6099 : :
6100 : : /* Attempt to return a path_var that represents REG, or return
6101 : : the NULL path_var.
6102 : : For example, a region for a field of a local would be a path_var
6103 : : wrapping a COMPONENT_REF.
6104 : : Use VISITED to prevent infinite mutual recursion with the overload for
6105 : : svalues.
6106 : :
6107 : : This function defers to get_representative_path_var_1 to do the work;
6108 : : it adds verification that get_representative_path_var_1 returned a tree
6109 : : of the correct type. */
6110 : :
6111 : : path_var
6112 : 15488 : region_model::get_representative_path_var (const region *reg,
6113 : : svalue_set *visited,
6114 : : logger *logger) const
6115 : : {
6116 : 15488 : LOG_SCOPE (logger);
6117 : 15488 : if (logger)
6118 : : {
6119 : 0 : logger->start_log_line ();
6120 : 0 : logger->log_partial ("reg: ");
6121 : 0 : reg->dump_to_pp (logger->get_printer (), true);
6122 : 0 : logger->end_log_line ();
6123 : : }
6124 : :
6125 : 15488 : path_var result = get_representative_path_var_1 (reg, visited, logger);
6126 : :
6127 : : /* Verify that the result has the same type as REG, if any. */
6128 : 15488 : if (result.m_tree && reg->get_type ())
6129 : 10832 : gcc_assert (TREE_TYPE (result.m_tree) == reg->get_type ());
6130 : :
6131 : 15488 : if (logger)
6132 : : {
6133 : 0 : logger->start_log_line ();
6134 : 0 : logger->log_partial ("reg: ");
6135 : 0 : reg->dump_to_pp (logger->get_printer (), true);
6136 : 0 : logger->end_log_line ();
6137 : :
6138 : 0 : if (result.m_tree)
6139 : 0 : logger->log ("tree: %qE", result.m_tree);
6140 : : else
6141 : 0 : logger->log ("tree: NULL");
6142 : : }
6143 : :
6144 : 30976 : return result;
6145 : 15488 : }
6146 : :
6147 : : /* Push a new frame_region on to the stack region.
6148 : : Populate the frame_region with child regions for the function call's
6149 : : parameters, using values from the arguments at the callsite in the
6150 : : caller's frame. */
6151 : :
6152 : : void
6153 : 11430 : region_model::update_for_gcall (const gcall &call_stmt,
6154 : : region_model_context *ctxt,
6155 : : function *callee)
6156 : : {
6157 : : /* Build a vec of argument svalues, using the current top
6158 : : frame for resolving tree expressions. */
6159 : 11430 : auto_vec<const svalue *> arg_svals (gimple_call_num_args (&call_stmt));
6160 : :
6161 : 24113 : for (unsigned i = 0; i < gimple_call_num_args (&call_stmt); i++)
6162 : : {
6163 : 12683 : tree arg = gimple_call_arg (&call_stmt, i);
6164 : 12683 : arg_svals.quick_push (get_rvalue (arg, ctxt));
6165 : : }
6166 : :
6167 : 11430 : if(!callee)
6168 : : {
6169 : : /* Get the function * from the gcall. */
6170 : 0 : tree fn_decl = get_fndecl_for_call (call_stmt, ctxt);
6171 : 0 : callee = DECL_STRUCT_FUNCTION (fn_decl);
6172 : : }
6173 : :
6174 : 0 : gcc_assert (callee);
6175 : 11430 : push_frame (*callee, &call_stmt, &arg_svals, ctxt);
6176 : 11430 : }
6177 : :
6178 : : /* Pop the top-most frame_region from the stack, and copy the return
6179 : : region's values (if any) into the region for the lvalue of the LHS of
6180 : : the call (if any). */
6181 : :
6182 : : void
6183 : 8463 : region_model::update_for_return_gcall (const gcall &call_stmt,
6184 : : region_model_context *ctxt)
6185 : : {
6186 : : /* Get the lvalue for the result of the call, passing it to pop_frame,
6187 : : so that pop_frame can determine the region with respect to the
6188 : : *caller* frame. */
6189 : 8463 : tree lhs = gimple_call_lhs (&call_stmt);
6190 : 8463 : pop_frame (lhs, nullptr, ctxt, &call_stmt);
6191 : 8463 : }
6192 : :
6193 : : /* Attempt to use R to replay SUMMARY into this object.
6194 : : Return true if it is possible. */
6195 : :
6196 : : bool
6197 : 1629 : region_model::replay_call_summary (call_summary_replay &r,
6198 : : const region_model &summary)
6199 : : {
6200 : 1629 : gcc_assert (summary.get_stack_depth () == 1);
6201 : :
6202 : 1629 : m_store.replay_call_summary (r, summary.m_store);
6203 : :
6204 : 1629 : if (r.get_ctxt ())
6205 : 1545 : r.get_ctxt ()->maybe_did_work ();
6206 : :
6207 : 1629 : if (!m_constraints->replay_call_summary (r, *summary.m_constraints))
6208 : : return false;
6209 : :
6210 : 4578 : for (auto kv : summary.m_dynamic_extents)
6211 : : {
6212 : 1537 : const region *summary_reg = kv.first;
6213 : 1537 : const region *caller_reg = r.convert_region_from_summary (summary_reg);
6214 : 1537 : if (!caller_reg)
6215 : 2 : continue;
6216 : 1535 : const svalue *summary_sval = kv.second;
6217 : 1535 : const svalue *caller_sval = r.convert_svalue_from_summary (summary_sval);
6218 : 1535 : if (!caller_sval)
6219 : 0 : continue;
6220 : 1535 : m_dynamic_extents.put (caller_reg, caller_sval);
6221 : : }
6222 : :
6223 : 1504 : return true;
6224 : : }
6225 : :
6226 : : /* For use with push_frame when handling a top-level call within the analysis.
6227 : : PARAM has a defined but unknown initial value.
6228 : : Anything it points to has escaped, since the calling context "knows"
6229 : : the pointer, and thus calls to unknown functions could read/write into
6230 : : the region.
6231 : : If NONNULL is true, then assume that PARAM must be non-NULL. */
6232 : :
6233 : : void
6234 : 18263 : region_model::on_top_level_param (tree param,
6235 : : bool nonnull,
6236 : : region_model_context *ctxt)
6237 : : {
6238 : 18263 : if (POINTER_TYPE_P (TREE_TYPE (param)))
6239 : : {
6240 : 8709 : const region *param_reg = get_lvalue (param, ctxt);
6241 : 8709 : const svalue *init_ptr_sval
6242 : 8709 : = m_mgr->get_or_create_initial_value (param_reg);
6243 : 8709 : const region *pointee_reg = m_mgr->get_symbolic_region (init_ptr_sval);
6244 : 8709 : store_manager *store_mgr = m_mgr->get_store_manager ();
6245 : 8709 : m_store.mark_as_escaped (*store_mgr, pointee_reg);
6246 : 8709 : if (nonnull)
6247 : : {
6248 : 455 : const svalue *null_ptr_sval
6249 : 455 : = m_mgr->get_or_create_null_ptr (TREE_TYPE (param));
6250 : 455 : add_constraint (init_ptr_sval, NE_EXPR, null_ptr_sval, ctxt);
6251 : : }
6252 : : }
6253 : 18263 : }
6254 : :
6255 : : /* Update this region_model to reflect pushing a frame onto the stack
6256 : : for a call to FUN.
6257 : :
6258 : : If CALL_STMT is non-NULL, this is for the interprocedural case where
6259 : : we already have an execution path into the caller. It can be NULL for
6260 : : top-level entrypoints into the analysis, or in selftests.
6261 : :
6262 : : If ARG_SVALS is non-NULL, use it to populate the parameters
6263 : : in the new frame.
6264 : : Otherwise, the params have their initial_svalues.
6265 : :
6266 : : Return the frame_region for the new frame. */
6267 : :
6268 : : const region *
6269 : 30983 : region_model::push_frame (const function &fun,
6270 : : const gcall *call_stmt,
6271 : : const vec<const svalue *> *arg_svals,
6272 : : region_model_context *ctxt)
6273 : : {
6274 : 30983 : tree fndecl = fun.decl;
6275 : 30983 : if (arg_svals)
6276 : : {
6277 : : /* If the result of the callee is DECL_BY_REFERENCE, then
6278 : : we'll need to store a reference to the caller's lhs of
6279 : : CALL_STMT within callee's result.
6280 : : If so, determine the region of CALL_STMT's lhs within
6281 : : the caller's frame before updating m_current_frame. */
6282 : 11430 : const region *caller_return_by_reference_reg = nullptr;
6283 : 11430 : if (tree result = DECL_RESULT (fndecl))
6284 : 11430 : if (DECL_BY_REFERENCE (result))
6285 : : {
6286 : 36 : gcc_assert (call_stmt);
6287 : 36 : tree lhs = gimple_call_lhs (call_stmt);
6288 : 36 : gcc_assert (lhs);
6289 : 36 : caller_return_by_reference_reg = get_lvalue (lhs, ctxt);
6290 : : }
6291 : :
6292 : : /* Update m_current_frame. */
6293 : 11430 : m_current_frame = m_mgr->get_frame_region (m_current_frame, fun);
6294 : :
6295 : : /* Arguments supplied from a caller frame. */
6296 : 11430 : unsigned idx = 0;
6297 : 23698 : for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
6298 : 12268 : iter_parm = DECL_CHAIN (iter_parm), ++idx)
6299 : : {
6300 : : /* If there's a mismatching declaration, the call stmt might
6301 : : not have enough args. Handle this case by leaving the
6302 : : rest of the params as uninitialized. */
6303 : 12271 : if (idx >= arg_svals->length ())
6304 : : break;
6305 : 12268 : tree parm_lval = iter_parm;
6306 : 12268 : if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm))
6307 : 11267 : parm_lval = parm_default_ssa;
6308 : 12268 : const region *parm_reg = get_lvalue (parm_lval, ctxt);
6309 : 12268 : const svalue *arg_sval = (*arg_svals)[idx];
6310 : 12268 : set_value (parm_reg, arg_sval, ctxt);
6311 : : }
6312 : :
6313 : : /* Handle any variadic args. */
6314 : : unsigned va_arg_idx = 0;
6315 : 11845 : for (; idx < arg_svals->length (); idx++, va_arg_idx++)
6316 : : {
6317 : 415 : const svalue *arg_sval = (*arg_svals)[idx];
6318 : 415 : const region *var_arg_reg
6319 : 415 : = m_mgr->get_var_arg_region (m_current_frame,
6320 : : va_arg_idx);
6321 : 415 : set_value (var_arg_reg, arg_sval, ctxt);
6322 : : }
6323 : :
6324 : : /* If the result of the callee is DECL_BY_REFERENCE, then above
6325 : : we should have determined the region within the
6326 : : caller's frame that the callee will be writing back to.
6327 : : Use this now to initialize the reference in callee's frame. */
6328 : 11430 : if (tree result = DECL_RESULT (fndecl))
6329 : 11430 : if (DECL_BY_REFERENCE (result))
6330 : : {
6331 : : /* Get reference to the caller lhs. */
6332 : 36 : gcc_assert (caller_return_by_reference_reg);
6333 : 36 : const svalue *ref_sval
6334 : 36 : = m_mgr->get_ptr_svalue (TREE_TYPE (result),
6335 : : caller_return_by_reference_reg);
6336 : :
6337 : : /* Get region for default val of DECL_RESULT within the
6338 : : callee. */
6339 : 36 : tree result_default_ssa = get_ssa_default_def (fun, result);
6340 : 36 : gcc_assert (result_default_ssa);
6341 : 36 : const region *callee_result_reg
6342 : 36 : = get_lvalue (result_default_ssa, ctxt);
6343 : :
6344 : : /* Set the callee's reference to refer to the caller's lhs. */
6345 : 36 : set_value (callee_result_reg, ref_sval, ctxt);
6346 : : }
6347 : : }
6348 : : else
6349 : : {
6350 : : /* Otherwise we have a top-level call within the analysis. The params
6351 : : have defined but unknown initial values.
6352 : : Anything they point to has escaped. */
6353 : :
6354 : : /* Update m_current_frame. */
6355 : 19553 : m_current_frame = m_mgr->get_frame_region (m_current_frame, fun);
6356 : :
6357 : : /* Handle "__attribute__((nonnull))". */
6358 : 19553 : tree fntype = TREE_TYPE (fndecl);
6359 : 19553 : bitmap nonnull_args = get_nonnull_args (fntype);
6360 : :
6361 : 19553 : unsigned parm_idx = 0;
6362 : 37816 : for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm;
6363 : 18263 : iter_parm = DECL_CHAIN (iter_parm))
6364 : : {
6365 : 18263 : bool non_null = (nonnull_args
6366 : 18263 : ? (bitmap_empty_p (nonnull_args)
6367 : 514 : || bitmap_bit_p (nonnull_args, parm_idx))
6368 : 18263 : : false);
6369 : 18263 : if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm))
6370 : 15385 : on_top_level_param (parm_default_ssa, non_null, ctxt);
6371 : : else
6372 : 2878 : on_top_level_param (iter_parm, non_null, ctxt);
6373 : 18263 : parm_idx++;
6374 : : }
6375 : :
6376 : 19553 : BITMAP_FREE (nonnull_args);
6377 : : }
6378 : :
6379 : 30983 : return m_current_frame;
6380 : : }
6381 : :
6382 : : /* Get the function of the top-most frame in this region_model's stack.
6383 : : There must be such a frame. */
6384 : :
6385 : : const function *
6386 : 183 : region_model::get_current_function () const
6387 : : {
6388 : 183 : const frame_region *frame = get_current_frame ();
6389 : 183 : gcc_assert (frame);
6390 : 183 : return &frame->get_function ();
6391 : : }
6392 : :
6393 : : /* Custom region_model_context for the assignment to the result
6394 : : at a call statement when popping a frame (PR analyzer/106203). */
6395 : :
6396 : : class caller_context : public region_model_context_decorator
6397 : : {
6398 : : public:
6399 : 4277 : caller_context (region_model_context *inner,
6400 : : const gcall *call_stmt,
6401 : : const frame_region &caller_frame)
6402 : 4277 : : region_model_context_decorator (inner),
6403 : 4277 : m_call_stmt (call_stmt),
6404 : 4277 : m_caller_frame (caller_frame)
6405 : : {}
6406 : :
6407 : : pending_location
6408 : 9 : get_pending_location_for_diag () const override
6409 : : {
6410 : 9 : pending_location ploc
6411 : 9 : = region_model_context_decorator::get_pending_location_for_diag ();
6412 : :
6413 : 9 : ploc.m_event_loc_info
6414 : 9 : = event_loc_info (m_call_stmt->location,
6415 : 9 : m_caller_frame.get_fndecl (),
6416 : 9 : m_caller_frame.get_stack_depth ());
6417 : :
6418 : 9 : return ploc;
6419 : : }
6420 : :
6421 : 8563 : const gimple *get_stmt () const override
6422 : : {
6423 : 8563 : return m_call_stmt;
6424 : : };
6425 : :
6426 : : private:
6427 : : const gcall *m_call_stmt;
6428 : : const frame_region &m_caller_frame;
6429 : : };
6430 : :
6431 : :
6432 : : /* Pop the topmost frame_region from this region_model's stack;
6433 : :
6434 : : If RESULT_LVALUE is non-null, copy any return value from the frame
6435 : : into the corresponding region (evaluated with respect to the *caller*
6436 : : frame, rather than the called frame).
6437 : : If OUT_RESULT is non-null, copy any return value from the frame
6438 : : into *OUT_RESULT.
6439 : :
6440 : : If non-null, use CALL_STMT as the location when complaining about
6441 : : assignment of the return value to RESULT_LVALUE.
6442 : :
6443 : : If EVAL_RETURN_SVALUE is false, then don't evaluate the return value.
6444 : : This is for use when unwinding frames e.g. due to longjmp, to suppress
6445 : : erroneously reporting uninitialized return values.
6446 : :
6447 : : Purge the frame region and all its descendent regions.
6448 : : Convert any pointers that point into such regions into
6449 : : poison_kind::popped_stack svalues. */
6450 : :
6451 : : void
6452 : 26142 : region_model::pop_frame (tree result_lvalue,
6453 : : const svalue **out_result,
6454 : : region_model_context *ctxt,
6455 : : const gcall *call_stmt,
6456 : : bool eval_return_svalue)
6457 : : {
6458 : 26142 : gcc_assert (m_current_frame);
6459 : :
6460 : 26142 : const region_model pre_popped_model = *this;
6461 : 26142 : const frame_region *frame_reg = m_current_frame;
6462 : :
6463 : : /* Notify state machines. */
6464 : 26142 : if (ctxt)
6465 : 23919 : ctxt->on_pop_frame (frame_reg);
6466 : :
6467 : : /* Evaluate the result, within the callee frame. */
6468 : 26142 : tree fndecl = m_current_frame->get_function ().decl;
6469 : 26142 : tree result = DECL_RESULT (fndecl);
6470 : 26142 : const svalue *retval = nullptr;
6471 : 26142 : if (result
6472 : 26134 : && TREE_TYPE (result) != void_type_node
6473 : 38149 : && eval_return_svalue)
6474 : : {
6475 : 9794 : retval = get_rvalue (result, ctxt);
6476 : 9794 : if (out_result)
6477 : 5187 : *out_result = retval;
6478 : : }
6479 : :
6480 : : /* Pop the frame. */
6481 : 26142 : m_current_frame = m_current_frame->get_calling_frame ();
6482 : :
6483 : 26142 : if (result_lvalue
6484 : 26142 : && retval
6485 : : /* Don't write back for DECL_BY_REFERENCE; the writes
6486 : : should have happened within the callee already. */
6487 : 26142 : && !DECL_BY_REFERENCE (result))
6488 : : {
6489 : 4277 : gcc_assert (eval_return_svalue);
6490 : :
6491 : : /* Compute result_dst_reg using RESULT_LVALUE *after* popping
6492 : : the frame, but before poisoning pointers into the old frame. */
6493 : 4277 : const region *result_dst_reg = get_lvalue (result_lvalue, ctxt);
6494 : :
6495 : : /* Assign retval to result_dst_reg, using caller_context
6496 : : to set the call_stmt and the popped_frame for any diagnostics
6497 : : due to the assignment. */
6498 : 4277 : gcc_assert (m_current_frame);
6499 : 4277 : caller_context caller_ctxt (ctxt, call_stmt, *m_current_frame);
6500 : 4277 : set_value (result_dst_reg, retval, call_stmt ? &caller_ctxt : ctxt);
6501 : : }
6502 : :
6503 : 26142 : unbind_region_and_descendents (frame_reg,poison_kind::popped_stack);
6504 : 26142 : notify_on_pop_frame (this, &pre_popped_model, retval, ctxt);
6505 : 26142 : }
6506 : :
6507 : : /* Get the number of frames in this region_model's stack. */
6508 : :
6509 : : int
6510 : 5110859 : region_model::get_stack_depth () const
6511 : : {
6512 : 5110859 : const frame_region *frame = get_current_frame ();
6513 : 5110859 : if (frame)
6514 : 5094186 : return frame->get_stack_depth ();
6515 : : else
6516 : : return 0;
6517 : : }
6518 : :
6519 : : /* Get the frame_region with the given index within the stack.
6520 : : The frame_region must exist. */
6521 : :
6522 : : const frame_region *
6523 : 1522348 : region_model::get_frame_at_index (int index) const
6524 : : {
6525 : 1522348 : const frame_region *frame = get_current_frame ();
6526 : 1522348 : gcc_assert (frame);
6527 : 1522348 : gcc_assert (index >= 0);
6528 : 1522348 : gcc_assert (index <= frame->get_index ());
6529 : 1810156 : while (index != frame->get_index ())
6530 : : {
6531 : 287808 : frame = frame->get_calling_frame ();
6532 : 287808 : gcc_assert (frame);
6533 : : }
6534 : 1522348 : return frame;
6535 : : }
6536 : :
6537 : : /* Unbind svalues for any regions in REG and below.
6538 : : Find any pointers to such regions; convert them to
6539 : : poisoned values of kind PKIND.
6540 : : Also purge any dynamic extents. */
6541 : :
6542 : : void
6543 : 36680 : region_model::unbind_region_and_descendents (const region *reg,
6544 : : enum poison_kind pkind)
6545 : : {
6546 : : /* Gather a set of base regions to be unbound. */
6547 : 36680 : hash_set<const region *> base_regs;
6548 : 211781 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6549 : 386882 : iter != m_store.end (); ++iter)
6550 : : {
6551 : 175101 : const region *iter_base_reg = (*iter).first;
6552 : 175101 : if (iter_base_reg->descendent_of_p (reg))
6553 : 41468 : base_regs.add (iter_base_reg);
6554 : : }
6555 : 78148 : for (hash_set<const region *>::iterator iter = base_regs.begin ();
6556 : 119616 : iter != base_regs.end (); ++iter)
6557 : 41468 : m_store.purge_cluster (*iter);
6558 : :
6559 : : /* Find any pointers to REG or its descendents; convert to poisoned. */
6560 : 36680 : poison_any_pointers_to_descendents (reg, pkind);
6561 : :
6562 : : /* Purge dynamic extents of any base regions in REG and below
6563 : : (e.g. VLAs and alloca stack regions). */
6564 : 111640 : for (auto iter : m_dynamic_extents)
6565 : : {
6566 : 19140 : const region *iter_reg = iter.first;
6567 : 19140 : if (iter_reg->descendent_of_p (reg))
6568 : 5958 : unset_dynamic_extents (iter_reg);
6569 : : }
6570 : 36680 : }
6571 : :
6572 : : /* Find any pointers to REG or its descendents; convert them to
6573 : : poisoned values of kind PKIND. */
6574 : :
6575 : : void
6576 : 36680 : region_model::poison_any_pointers_to_descendents (const region *reg,
6577 : : enum poison_kind pkind)
6578 : : {
6579 : 303946 : for (const auto &cluster_iter : m_store)
6580 : : {
6581 : 133633 : binding_cluster *cluster = cluster_iter.second;
6582 : 133633 : for (auto iter = cluster->begin ();
6583 : 270119 : iter != cluster->end ();
6584 : 136486 : ++iter)
6585 : : {
6586 : 136486 : auto bp = *iter;
6587 : 136486 : const svalue *sval = bp.m_sval;
6588 : 136486 : if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ())
6589 : : {
6590 : 31911 : const region *ptr_dst = ptr_sval->get_pointee ();
6591 : : /* Poison ptrs to descendents of REG, but not to REG itself,
6592 : : otherwise double-free detection doesn't work (since sm-state
6593 : : for "free" is stored on the original ptr svalue). */
6594 : 31911 : if (ptr_dst->descendent_of_p (reg)
6595 : 31911 : && ptr_dst != reg)
6596 : : {
6597 : 133 : const svalue *new_sval
6598 : 133 : = m_mgr->get_or_create_poisoned_svalue (pkind,
6599 : : sval->get_type ());
6600 : 133 : cluster->get_map ().overwrite (iter, new_sval);
6601 : : }
6602 : : }
6603 : : }
6604 : : }
6605 : 36680 : }
6606 : :
6607 : : /* Attempt to merge THIS with OTHER_MODEL, writing the result
6608 : : to OUT_MODEL. Use POINT to distinguish values created as a
6609 : : result of merging. */
6610 : :
6611 : : bool
6612 : 117908 : region_model::can_merge_with_p (const region_model &other_model,
6613 : : const program_point &point,
6614 : : region_model *out_model,
6615 : : const extrinsic_state *ext_state,
6616 : : const program_state *state_a,
6617 : : const program_state *state_b) const
6618 : : {
6619 : 117908 : gcc_assert (out_model);
6620 : 117908 : gcc_assert (m_mgr == other_model.m_mgr);
6621 : 117908 : gcc_assert (m_mgr == out_model->m_mgr);
6622 : :
6623 : 117908 : if (m_current_frame != other_model.m_current_frame)
6624 : : return false;
6625 : 117908 : out_model->m_current_frame = m_current_frame;
6626 : :
6627 : 117908 : model_merger m (this, &other_model, point, out_model,
6628 : 117908 : ext_state, state_a, state_b);
6629 : :
6630 : 117908 : if (!store::can_merge_p (&m_store, &other_model.m_store,
6631 : 117908 : &out_model->m_store, m_mgr->get_store_manager (),
6632 : : &m))
6633 : : return false;
6634 : :
6635 : 42132 : if (!m_dynamic_extents.can_merge_with_p (other_model.m_dynamic_extents,
6636 : : &out_model->m_dynamic_extents))
6637 : : return false;
6638 : :
6639 : : /* Merge constraints. */
6640 : 39291 : constraint_manager::merge (*m_constraints,
6641 : 39291 : *other_model.m_constraints,
6642 : : out_model->m_constraints);
6643 : :
6644 : 40027 : for (auto iter : m.m_svals_changing_meaning)
6645 : 736 : out_model->m_constraints->purge_state_involving (iter);
6646 : :
6647 : 39291 : if (m_thrown_exceptions_stack != other_model.m_thrown_exceptions_stack)
6648 : : return false;
6649 : 39262 : out_model->m_thrown_exceptions_stack = m_thrown_exceptions_stack;
6650 : :
6651 : 39262 : if (m_caught_exceptions_stack != other_model.m_caught_exceptions_stack)
6652 : : return false;
6653 : 39262 : out_model->m_caught_exceptions_stack = m_caught_exceptions_stack;
6654 : :
6655 : 39262 : return true;
6656 : 117908 : }
6657 : :
6658 : : /* Attempt to get the fndecl used at CALL, if known, or NULL_TREE
6659 : : otherwise. */
6660 : :
6661 : : tree
6662 : 923572 : region_model::get_fndecl_for_call (const gcall &call,
6663 : : region_model_context *ctxt)
6664 : : {
6665 : 923572 : tree fn_ptr = gimple_call_fn (&call);
6666 : 923572 : if (fn_ptr == NULL_TREE)
6667 : : return NULL_TREE;
6668 : 881752 : const svalue *fn_ptr_sval = get_rvalue (fn_ptr, ctxt);
6669 : 1763504 : if (const region_svalue *fn_ptr_ptr
6670 : 881752 : = fn_ptr_sval->dyn_cast_region_svalue ())
6671 : : {
6672 : 875075 : const region *reg = fn_ptr_ptr->get_pointee ();
6673 : 875075 : if (const function_region *fn_reg = reg->dyn_cast_function_region ())
6674 : : {
6675 : 875019 : tree fn_decl = fn_reg->get_fndecl ();
6676 : 875019 : cgraph_node *node = cgraph_node::get (fn_decl);
6677 : 875019 : if (!node)
6678 : : return NULL_TREE;
6679 : 875019 : const cgraph_node *ultimate_node = node->ultimate_alias_target ();
6680 : 875019 : if (ultimate_node)
6681 : 875019 : return ultimate_node->decl;
6682 : : }
6683 : : }
6684 : :
6685 : : return NULL_TREE;
6686 : : }
6687 : :
6688 : : /* Would be much simpler to use a lambda here, if it were supported. */
6689 : :
6690 : : struct append_regions_cb_data
6691 : : {
6692 : : const region_model *model;
6693 : : auto_vec<const decl_region *> *out;
6694 : : };
6695 : :
6696 : : /* Populate *OUT with all decl_regions in the current
6697 : : frame that have clusters within the store. */
6698 : :
6699 : : void
6700 : 381570 : region_model::
6701 : : get_regions_for_current_frame (auto_vec<const decl_region *> *out) const
6702 : : {
6703 : 381570 : append_regions_cb_data data;
6704 : 381570 : data.model = this;
6705 : 381570 : data.out = out;
6706 : 381570 : m_store.for_each_cluster (append_regions_cb, &data);
6707 : 381570 : }
6708 : :
6709 : : /* Implementation detail of get_regions_for_current_frame. */
6710 : :
6711 : : void
6712 : 2821188 : region_model::append_regions_cb (const region *base_reg,
6713 : : append_regions_cb_data *cb_data)
6714 : : {
6715 : 2821188 : if (base_reg->get_parent_region () != cb_data->model->m_current_frame)
6716 : : return;
6717 : 1461068 : if (const decl_region *decl_reg = base_reg->dyn_cast_decl_region ())
6718 : 1442750 : cb_data->out->safe_push (decl_reg);
6719 : : }
6720 : :
6721 : :
6722 : : /* Abstract class for diagnostics related to the use of
6723 : : floating-point arithmetic where precision is needed. */
6724 : :
6725 : 25 : class imprecise_floating_point_arithmetic : public pending_diagnostic
6726 : : {
6727 : : public:
6728 : 50 : int get_controlling_option () const final override
6729 : : {
6730 : 50 : return OPT_Wanalyzer_imprecise_fp_arithmetic;
6731 : : }
6732 : : };
6733 : :
6734 : : /* Concrete diagnostic to complain about uses of floating-point arithmetic
6735 : : in the size argument of malloc etc. */
6736 : :
6737 : : class float_as_size_arg : public imprecise_floating_point_arithmetic
6738 : : {
6739 : : public:
6740 : 25 : float_as_size_arg (tree arg) : m_arg (arg)
6741 : : {}
6742 : :
6743 : 305 : const char *get_kind () const final override
6744 : : {
6745 : 305 : return "float_as_size_arg_diagnostic";
6746 : : }
6747 : :
6748 : 25 : bool subclass_equal_p (const pending_diagnostic &other) const final override
6749 : : {
6750 : 25 : return same_tree_p (m_arg, ((const float_as_size_arg &) other).m_arg);
6751 : : }
6752 : :
6753 : 25 : bool emit (diagnostic_emission_context &ctxt) final override
6754 : : {
6755 : 25 : bool warned = ctxt.warn ("use of floating-point arithmetic here might"
6756 : : " yield unexpected results");
6757 : 25 : if (warned)
6758 : 25 : inform (ctxt.get_location (),
6759 : : "only use operands of an integer type"
6760 : : " inside the size argument");
6761 : 25 : return warned;
6762 : : }
6763 : :
6764 : : bool
6765 : 50 : describe_final_event (pretty_printer &pp,
6766 : : const evdesc::final_event &) final override
6767 : : {
6768 : 50 : if (m_arg)
6769 : 50 : pp_printf (&pp,
6770 : : "operand %qE is of type %qT",
6771 : 50 : m_arg, TREE_TYPE (m_arg));
6772 : : else
6773 : 0 : pp_printf (&pp,
6774 : : "at least one operand of the size argument is"
6775 : : " of a floating-point type");
6776 : 50 : return true;
6777 : : }
6778 : :
6779 : : private:
6780 : : tree m_arg;
6781 : : };
6782 : :
6783 : : /* Visitor to find uses of floating-point variables/constants in an svalue. */
6784 : :
6785 : : class contains_floating_point_visitor : public visitor
6786 : : {
6787 : : public:
6788 : 7818 : contains_floating_point_visitor (const svalue *root_sval) : m_result (nullptr)
6789 : : {
6790 : 7818 : root_sval->accept (this);
6791 : : }
6792 : :
6793 : 7818 : const svalue *get_svalue_to_report ()
6794 : : {
6795 : 7818 : return m_result;
6796 : : }
6797 : :
6798 : 7525 : void visit_constant_svalue (const constant_svalue *sval) final override
6799 : : {
6800 : : /* At the point the analyzer runs, constant integer operands in a floating
6801 : : point expression are already implictly converted to floating-points.
6802 : : Thus, we do prefer to report non-constants such that the diagnostic
6803 : : always reports a floating-point operand. */
6804 : 7525 : tree type = sval->get_type ();
6805 : 7525 : if (type && FLOAT_TYPE_P (type) && !m_result)
6806 : 9 : m_result = sval;
6807 : 7525 : }
6808 : :
6809 : 445 : void visit_conjured_svalue (const conjured_svalue *sval) final override
6810 : : {
6811 : 445 : tree type = sval->get_type ();
6812 : 445 : if (type && FLOAT_TYPE_P (type))
6813 : 0 : m_result = sval;
6814 : 445 : }
6815 : :
6816 : 974 : void visit_initial_svalue (const initial_svalue *sval) final override
6817 : : {
6818 : 974 : tree type = sval->get_type ();
6819 : 974 : if (type && FLOAT_TYPE_P (type))
6820 : 16 : m_result = sval;
6821 : 974 : }
6822 : :
6823 : : private:
6824 : : /* Non-null if at least one floating-point operand was found. */
6825 : : const svalue *m_result;
6826 : : };
6827 : :
6828 : : /* May complain about uses of floating-point operands in SIZE_IN_BYTES. */
6829 : :
6830 : : void
6831 : 7818 : region_model::check_dynamic_size_for_floats (const svalue *size_in_bytes,
6832 : : region_model_context *ctxt) const
6833 : : {
6834 : 7818 : gcc_assert (ctxt);
6835 : :
6836 : 7818 : contains_floating_point_visitor v (size_in_bytes);
6837 : 7818 : if (const svalue *float_sval = v.get_svalue_to_report ())
6838 : : {
6839 : 25 : tree diag_arg = get_representative_tree (float_sval);
6840 : 25 : ctxt->warn (std::make_unique<float_as_size_arg> (diag_arg));
6841 : : }
6842 : 7818 : }
6843 : :
6844 : : /* Return a region describing a heap-allocated block of memory.
6845 : : Use CTXT to complain about tainted sizes.
6846 : :
6847 : : Reuse an existing heap_allocated_region if it's not being referenced by
6848 : : this region_model; otherwise create a new one.
6849 : :
6850 : : Optionally (update_state_machine) transitions the pointer pointing to the
6851 : : heap_allocated_region from start to assumed non-null. */
6852 : :
6853 : : const region *
6854 : 17707 : region_model::get_or_create_region_for_heap_alloc (const svalue *size_in_bytes,
6855 : : region_model_context *ctxt,
6856 : : bool update_state_machine,
6857 : : const call_details *cd)
6858 : : {
6859 : : /* Determine which regions are referenced in this region_model, so that
6860 : : we can reuse an existing heap_allocated_region if it's not in use on
6861 : : this path. */
6862 : 17707 : auto_bitmap base_regs_in_use;
6863 : 17707 : get_referenced_base_regions (base_regs_in_use);
6864 : :
6865 : : /* Don't reuse regions that are marked as TOUCHED. */
6866 : 111119 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6867 : 204531 : iter != m_store.end (); ++iter)
6868 : 93412 : if ((*iter).second->touched_p ())
6869 : : {
6870 : 14167 : const region *base_reg = (*iter).first;
6871 : 14167 : bitmap_set_bit (base_regs_in_use, base_reg->get_id ());
6872 : : }
6873 : :
6874 : 17707 : const region *reg
6875 : 17707 : = m_mgr->get_or_create_region_for_heap_alloc (base_regs_in_use);
6876 : 17707 : if (size_in_bytes)
6877 : 12186 : if (compat_types_p (size_in_bytes->get_type (), size_type_node))
6878 : 12186 : set_dynamic_extents (reg, size_in_bytes, ctxt);
6879 : :
6880 : 17707 : if (update_state_machine && cd)
6881 : : {
6882 : 0 : const svalue *ptr_sval
6883 : 0 : = m_mgr->get_ptr_svalue (cd->get_lhs_type (), reg);
6884 : 0 : transition_ptr_sval_non_null (ctxt, ptr_sval);
6885 : : }
6886 : :
6887 : 17707 : return reg;
6888 : 17707 : }
6889 : :
6890 : : /* Populate OUT_IDS with the set of IDs of those base regions which are
6891 : : reachable in this region_model. */
6892 : :
6893 : : void
6894 : 19883 : region_model::get_referenced_base_regions (auto_bitmap &out_ids) const
6895 : : {
6896 : 19883 : reachable_regions reachable_regs (const_cast<region_model *> (this));
6897 : 19883 : m_store.for_each_cluster (reachable_regions::init_cluster_cb,
6898 : : &reachable_regs);
6899 : : /* Get regions for locals that have explicitly bound values. */
6900 : 150657 : for (store::cluster_map_t::iterator iter = m_store.begin ();
6901 : 281431 : iter != m_store.end (); ++iter)
6902 : : {
6903 : 130774 : const region *base_reg = (*iter).first;
6904 : 130774 : if (const region *parent = base_reg->get_parent_region ())
6905 : 130774 : if (parent->get_kind () == RK_FRAME)
6906 : 77970 : reachable_regs.add (base_reg, false);
6907 : : }
6908 : :
6909 : 19887 : for (auto &eh_node : m_thrown_exceptions_stack)
6910 : 4 : eh_node.add_to_reachable_regions (reachable_regs);
6911 : 19967 : for (auto &eh_node : m_caught_exceptions_stack)
6912 : 84 : eh_node.add_to_reachable_regions (reachable_regs);
6913 : :
6914 : :
6915 : 19883 : bitmap_clear (out_ids);
6916 : 154120 : for (auto iter_reg : reachable_regs)
6917 : 134237 : bitmap_set_bit (out_ids, iter_reg->get_id ());
6918 : 19883 : }
6919 : :
6920 : : /* Return a new region describing a block of memory allocated within the
6921 : : current frame.
6922 : : Use CTXT to complain about tainted sizes. */
6923 : :
6924 : : const region *
6925 : 425 : region_model::create_region_for_alloca (const svalue *size_in_bytes,
6926 : : region_model_context *ctxt)
6927 : : {
6928 : 425 : const region *reg = m_mgr->create_region_for_alloca (m_current_frame);
6929 : 425 : if (compat_types_p (size_in_bytes->get_type (), size_type_node))
6930 : 424 : set_dynamic_extents (reg, size_in_bytes, ctxt);
6931 : 425 : return reg;
6932 : : }
6933 : :
6934 : : /* Record that the size of REG is SIZE_IN_BYTES.
6935 : : Use CTXT to complain about tainted sizes. */
6936 : :
6937 : : void
6938 : 13070 : region_model::set_dynamic_extents (const region *reg,
6939 : : const svalue *size_in_bytes,
6940 : : region_model_context *ctxt)
6941 : : {
6942 : 13070 : assert_compat_types (size_in_bytes->get_type (), size_type_node);
6943 : 13070 : if (ctxt)
6944 : : {
6945 : 7818 : check_dynamic_size_for_taint (reg->get_memory_space (), size_in_bytes,
6946 : : ctxt);
6947 : 7818 : check_dynamic_size_for_floats (size_in_bytes, ctxt);
6948 : : }
6949 : 13070 : m_dynamic_extents.put (reg, size_in_bytes);
6950 : 13070 : }
6951 : :
6952 : : /* Get the recording of REG in bytes, or nullptr if no dynamic size was
6953 : : recorded. */
6954 : :
6955 : : const svalue *
6956 : 63978 : region_model::get_dynamic_extents (const region *reg) const
6957 : : {
6958 : 63978 : if (const svalue * const *slot = m_dynamic_extents.get (reg))
6959 : 12411 : return *slot;
6960 : : return nullptr;
6961 : : }
6962 : :
6963 : : /* Unset any recorded dynamic size of REG. */
6964 : :
6965 : : void
6966 : 50333 : region_model::unset_dynamic_extents (const region *reg)
6967 : : {
6968 : 50333 : m_dynamic_extents.remove (reg);
6969 : 50333 : }
6970 : :
6971 : : /* A subclass of pending_diagnostic for complaining about uninitialized data
6972 : : being copied across a trust boundary to an untrusted output
6973 : : (e.g. copy_to_user infoleaks in the Linux kernel). */
6974 : :
6975 : : class exposure_through_uninit_copy
6976 : : : public pending_diagnostic_subclass<exposure_through_uninit_copy>
6977 : : {
6978 : : public:
6979 : 25 : exposure_through_uninit_copy (const region *src_region,
6980 : : const region *dest_region,
6981 : : const svalue *copied_sval)
6982 : 25 : : m_src_region (src_region),
6983 : 25 : m_dest_region (dest_region),
6984 : 25 : m_copied_sval (copied_sval)
6985 : : {
6986 : 25 : gcc_assert (m_copied_sval->get_kind () == SK_POISONED
6987 : : || m_copied_sval->get_kind () == SK_COMPOUND);
6988 : 25 : }
6989 : :
6990 : 294 : const char *get_kind () const final override
6991 : : {
6992 : 294 : return "exposure_through_uninit_copy";
6993 : : }
6994 : :
6995 : 25 : bool operator== (const exposure_through_uninit_copy &other) const
6996 : : {
6997 : 25 : return (m_src_region == other.m_src_region
6998 : 25 : && m_dest_region == other.m_dest_region
6999 : 50 : && m_copied_sval == other.m_copied_sval);
7000 : : }
7001 : :
7002 : 50 : int get_controlling_option () const final override
7003 : : {
7004 : 50 : return OPT_Wanalyzer_exposure_through_uninit_copy;
7005 : : }
7006 : :
7007 : 25 : bool emit (diagnostic_emission_context &ctxt) final override
7008 : : {
7009 : : /* CWE-200: Exposure of Sensitive Information to an Unauthorized Actor. */
7010 : 25 : ctxt.add_cwe (200);
7011 : 50 : enum memory_space mem_space = get_src_memory_space ();
7012 : 25 : bool warned;
7013 : 25 : switch (mem_space)
7014 : : {
7015 : 0 : default:
7016 : 0 : warned = ctxt.warn ("potential exposure of sensitive information"
7017 : : " by copying uninitialized data"
7018 : : " across trust boundary");
7019 : 0 : break;
7020 : 25 : case MEMSPACE_STACK:
7021 : 25 : warned = ctxt.warn ("potential exposure of sensitive information"
7022 : : " by copying uninitialized data from stack"
7023 : : " across trust boundary");
7024 : 25 : break;
7025 : 0 : case MEMSPACE_HEAP:
7026 : 0 : warned = ctxt.warn ("potential exposure of sensitive information"
7027 : : " by copying uninitialized data from heap"
7028 : : " across trust boundary");
7029 : 0 : break;
7030 : : }
7031 : 25 : if (warned)
7032 : : {
7033 : 25 : const location_t loc = ctxt.get_location ();
7034 : 25 : inform_number_of_uninit_bits (loc);
7035 : 25 : complain_about_uninit_ranges (loc);
7036 : :
7037 : 25 : if (mem_space == MEMSPACE_STACK)
7038 : 25 : maybe_emit_fixit_hint ();
7039 : : }
7040 : 25 : return warned;
7041 : : }
7042 : :
7043 : : bool
7044 : 50 : describe_final_event (pretty_printer &pp,
7045 : : const evdesc::final_event &) final override
7046 : : {
7047 : 100 : enum memory_space mem_space = get_src_memory_space ();
7048 : 50 : switch (mem_space)
7049 : : {
7050 : 0 : default:
7051 : 0 : pp_string (&pp, "uninitialized data copied here");
7052 : 0 : return true;
7053 : :
7054 : 50 : case MEMSPACE_STACK:
7055 : 50 : pp_string (&pp, "uninitialized data copied from stack here");
7056 : 50 : return true;
7057 : :
7058 : 0 : case MEMSPACE_HEAP:
7059 : 0 : pp_string (&pp, "uninitialized data copied from heap here");
7060 : 0 : return true;
7061 : : }
7062 : : }
7063 : :
7064 : 25 : void mark_interesting_stuff (interesting_t *interest) final override
7065 : : {
7066 : 25 : if (m_src_region)
7067 : 25 : interest->add_region_creation (m_src_region);
7068 : 25 : }
7069 : :
7070 : : void
7071 : 0 : maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
7072 : : const final override
7073 : : {
7074 : 0 : auto &props = result_obj.get_or_create_properties ();
7075 : : #define PROPERTY_PREFIX "gcc/-Wanalyzer-exposure-through-uninit-copy/"
7076 : 0 : props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ());
7077 : 0 : props.set (PROPERTY_PREFIX "dest_region", m_dest_region->to_json ());
7078 : 0 : props.set (PROPERTY_PREFIX "copied_sval", m_copied_sval->to_json ());
7079 : : #undef PROPERTY_PREFIX
7080 : 0 : }
7081 : :
7082 : : private:
7083 : 75 : enum memory_space get_src_memory_space () const
7084 : : {
7085 : 75 : return m_src_region ? m_src_region->get_memory_space () : MEMSPACE_UNKNOWN;
7086 : : }
7087 : :
7088 : 25 : bit_size_t calc_num_uninit_bits () const
7089 : : {
7090 : 25 : switch (m_copied_sval->get_kind ())
7091 : : {
7092 : 0 : default:
7093 : 0 : gcc_unreachable ();
7094 : 4 : break;
7095 : 4 : case SK_POISONED:
7096 : 4 : {
7097 : 4 : const poisoned_svalue *poisoned_sval
7098 : 4 : = as_a <const poisoned_svalue *> (m_copied_sval);
7099 : 4 : gcc_assert (poisoned_sval->get_poison_kind () == poison_kind::uninit);
7100 : :
7101 : : /* Give up if don't have type information. */
7102 : 4 : if (m_copied_sval->get_type () == NULL_TREE)
7103 : 0 : return 0;
7104 : :
7105 : 4 : bit_size_t size_in_bits;
7106 : 4 : if (int_size_in_bits (m_copied_sval->get_type (), &size_in_bits))
7107 : 4 : return size_in_bits;
7108 : :
7109 : : /* Give up if we can't get the size of the type. */
7110 : 0 : return 0;
7111 : : }
7112 : 21 : break;
7113 : 21 : case SK_COMPOUND:
7114 : 21 : {
7115 : 21 : const compound_svalue *compound_sval
7116 : 21 : = as_a <const compound_svalue *> (m_copied_sval);
7117 : 21 : bit_size_t result = 0;
7118 : : /* Find keys for uninit svals. */
7119 : 82 : for (auto iter : *compound_sval)
7120 : : {
7121 : 61 : const svalue *sval = iter.m_sval;
7122 : 122 : if (const poisoned_svalue *psval
7123 : 61 : = sval->dyn_cast_poisoned_svalue ())
7124 : 24 : if (psval->get_poison_kind () == poison_kind::uninit)
7125 : : {
7126 : 24 : const binding_key *key = iter.m_key;
7127 : 24 : const concrete_binding *ckey
7128 : 24 : = key->dyn_cast_concrete_binding ();
7129 : 24 : gcc_assert (ckey);
7130 : 24 : result += ckey->get_size_in_bits ();
7131 : : }
7132 : : }
7133 : 21 : return result;
7134 : : }
7135 : : }
7136 : : }
7137 : :
7138 : 25 : void inform_number_of_uninit_bits (location_t loc) const
7139 : : {
7140 : 25 : bit_size_t num_uninit_bits = calc_num_uninit_bits ();
7141 : 25 : if (num_uninit_bits <= 0)
7142 : 0 : return;
7143 : 25 : if (num_uninit_bits % BITS_PER_UNIT == 0)
7144 : : {
7145 : : /* Express in bytes. */
7146 : 25 : byte_size_t num_uninit_bytes = num_uninit_bits / BITS_PER_UNIT;
7147 : 25 : if (num_uninit_bytes == 1)
7148 : 3 : inform (loc, "1 byte is uninitialized");
7149 : : else
7150 : 22 : inform (loc,
7151 : : "%wu bytes are uninitialized", num_uninit_bytes.to_uhwi ());
7152 : : }
7153 : : else
7154 : : {
7155 : : /* Express in bits. */
7156 : 0 : if (num_uninit_bits == 1)
7157 : 0 : inform (loc, "1 bit is uninitialized");
7158 : : else
7159 : 0 : inform (loc,
7160 : : "%wu bits are uninitialized", num_uninit_bits.to_uhwi ());
7161 : : }
7162 : : }
7163 : :
7164 : 25 : void complain_about_uninit_ranges (location_t loc) const
7165 : : {
7166 : 50 : if (const compound_svalue *compound_sval
7167 : 25 : = m_copied_sval->dyn_cast_compound_svalue ())
7168 : : {
7169 : : /* Find keys for uninit svals. */
7170 : 21 : auto_vec<const concrete_binding *> uninit_keys;
7171 : 82 : for (auto iter : *compound_sval)
7172 : : {
7173 : 61 : const svalue *sval = iter.m_sval;
7174 : 122 : if (const poisoned_svalue *psval
7175 : 61 : = sval->dyn_cast_poisoned_svalue ())
7176 : 24 : if (psval->get_poison_kind () == poison_kind::uninit)
7177 : : {
7178 : 24 : const binding_key *key = iter.m_key;
7179 : 24 : const concrete_binding *ckey
7180 : 24 : = key->dyn_cast_concrete_binding ();
7181 : 24 : gcc_assert (ckey);
7182 : 24 : uninit_keys.safe_push (ckey);
7183 : : }
7184 : : }
7185 : : /* Complain about them in sorted order. */
7186 : 21 : uninit_keys.qsort (concrete_binding::cmp_ptr_ptr);
7187 : :
7188 : 21 : std::unique_ptr<record_layout> layout;
7189 : :
7190 : 21 : tree type = m_copied_sval->get_type ();
7191 : 21 : if (type && TREE_CODE (type) == RECORD_TYPE)
7192 : : {
7193 : 17 : layout = std::make_unique<record_layout> (type);
7194 : :
7195 : 17 : if (0)
7196 : : layout->dump ();
7197 : : }
7198 : :
7199 : : unsigned i;
7200 : : const concrete_binding *ckey;
7201 : 45 : FOR_EACH_VEC_ELT (uninit_keys, i, ckey)
7202 : : {
7203 : 24 : bit_offset_t start_bit = ckey->get_start_bit_offset ();
7204 : 24 : bit_offset_t next_bit = ckey->get_next_bit_offset ();
7205 : 24 : complain_about_uninit_range (loc, start_bit, next_bit,
7206 : 24 : layout.get ());
7207 : : }
7208 : 21 : }
7209 : 25 : }
7210 : :
7211 : 24 : void complain_about_uninit_range (location_t loc,
7212 : : bit_offset_t start_bit,
7213 : : bit_offset_t next_bit,
7214 : : const record_layout *layout) const
7215 : : {
7216 : 24 : if (layout)
7217 : : {
7218 : 75 : while (start_bit < next_bit)
7219 : : {
7220 : 165 : if (const record_layout::item *item
7221 : 55 : = layout->get_item_at (start_bit))
7222 : : {
7223 : 55 : gcc_assert (start_bit >= item->get_start_bit_offset ());
7224 : 55 : gcc_assert (start_bit < item->get_next_bit_offset ());
7225 : 55 : if (item->get_start_bit_offset () == start_bit
7226 : 108 : && item->get_next_bit_offset () <= next_bit)
7227 : 53 : complain_about_fully_uninit_item (*item);
7228 : : else
7229 : 2 : complain_about_partially_uninit_item (*item);
7230 : 55 : start_bit = item->get_next_bit_offset ();
7231 : 55 : continue;
7232 : : }
7233 : : else
7234 : : break;
7235 : : }
7236 : : }
7237 : :
7238 : 24 : if (start_bit >= next_bit)
7239 : : return;
7240 : :
7241 : 4 : if (start_bit % 8 == 0 && next_bit % 8 == 0)
7242 : : {
7243 : : /* Express in bytes. */
7244 : 4 : byte_offset_t start_byte = start_bit / 8;
7245 : 4 : byte_offset_t last_byte = (next_bit / 8) - 1;
7246 : 4 : if (last_byte == start_byte)
7247 : 0 : inform (loc,
7248 : : "byte %wu is uninitialized",
7249 : : start_byte.to_uhwi ());
7250 : : else
7251 : 4 : inform (loc,
7252 : : "bytes %wu - %wu are uninitialized",
7253 : : start_byte.to_uhwi (),
7254 : : last_byte.to_uhwi ());
7255 : : }
7256 : : else
7257 : : {
7258 : : /* Express in bits. */
7259 : 0 : bit_offset_t last_bit = next_bit - 1;
7260 : 0 : if (last_bit == start_bit)
7261 : 0 : inform (loc,
7262 : : "bit %wu is uninitialized",
7263 : : start_bit.to_uhwi ());
7264 : : else
7265 : 0 : inform (loc,
7266 : : "bits %wu - %wu are uninitialized",
7267 : : start_bit.to_uhwi (),
7268 : : last_bit.to_uhwi ());
7269 : : }
7270 : : }
7271 : :
7272 : : static void
7273 : 53 : complain_about_fully_uninit_item (const record_layout::item &item)
7274 : : {
7275 : 53 : const_tree field = item.m_field;
7276 : 53 : bit_size_t num_bits = item.m_bit_range.m_size_in_bits;
7277 : 53 : if (item.m_is_padding)
7278 : : {
7279 : 11 : if (num_bits % 8 == 0)
7280 : : {
7281 : : /* Express in bytes. */
7282 : 9 : byte_size_t num_bytes = num_bits / BITS_PER_UNIT;
7283 : 9 : if (num_bytes == 1)
7284 : 2 : inform (DECL_SOURCE_LOCATION (field),
7285 : : "padding after field %qD is uninitialized (1 byte)",
7286 : : field);
7287 : : else
7288 : 7 : inform (DECL_SOURCE_LOCATION (field),
7289 : : "padding after field %qD is uninitialized (%wu bytes)",
7290 : : field, num_bytes.to_uhwi ());
7291 : : }
7292 : : else
7293 : : {
7294 : : /* Express in bits. */
7295 : 2 : if (num_bits == 1)
7296 : 0 : inform (DECL_SOURCE_LOCATION (field),
7297 : : "padding after field %qD is uninitialized (1 bit)",
7298 : : field);
7299 : : else
7300 : 2 : inform (DECL_SOURCE_LOCATION (field),
7301 : : "padding after field %qD is uninitialized (%wu bits)",
7302 : : field, num_bits.to_uhwi ());
7303 : : }
7304 : : }
7305 : : else
7306 : : {
7307 : 42 : if (num_bits % 8 == 0)
7308 : : {
7309 : : /* Express in bytes. */
7310 : 32 : byte_size_t num_bytes = num_bits / BITS_PER_UNIT;
7311 : 32 : if (num_bytes == 1)
7312 : 1 : inform (DECL_SOURCE_LOCATION (field),
7313 : : "field %qD is uninitialized (1 byte)", field);
7314 : : else
7315 : 31 : inform (DECL_SOURCE_LOCATION (field),
7316 : : "field %qD is uninitialized (%wu bytes)",
7317 : : field, num_bytes.to_uhwi ());
7318 : : }
7319 : : else
7320 : : {
7321 : : /* Express in bits. */
7322 : 10 : if (num_bits == 1)
7323 : 9 : inform (DECL_SOURCE_LOCATION (field),
7324 : : "field %qD is uninitialized (1 bit)", field);
7325 : : else
7326 : 1 : inform (DECL_SOURCE_LOCATION (field),
7327 : : "field %qD is uninitialized (%wu bits)",
7328 : : field, num_bits.to_uhwi ());
7329 : : }
7330 : : }
7331 : 53 : }
7332 : :
7333 : : static void
7334 : 2 : complain_about_partially_uninit_item (const record_layout::item &item)
7335 : : {
7336 : 2 : const_tree field = item.m_field;
7337 : 2 : if (item.m_is_padding)
7338 : 0 : inform (DECL_SOURCE_LOCATION (field),
7339 : : "padding after field %qD is partially uninitialized",
7340 : : field);
7341 : : else
7342 : 2 : inform (DECL_SOURCE_LOCATION (field),
7343 : : "field %qD is partially uninitialized",
7344 : : field);
7345 : : /* TODO: ideally we'd describe what parts are uninitialized. */
7346 : 2 : }
7347 : :
7348 : 25 : void maybe_emit_fixit_hint () const
7349 : : {
7350 : 25 : if (tree decl = m_src_region->maybe_get_decl ())
7351 : : {
7352 : 25 : gcc_rich_location hint_richloc (DECL_SOURCE_LOCATION (decl));
7353 : 25 : hint_richloc.add_fixit_insert_after (" = {0}");
7354 : 25 : inform (&hint_richloc,
7355 : : "suggest forcing zero-initialization by"
7356 : : " providing a %<{0}%> initializer");
7357 : 25 : }
7358 : 25 : }
7359 : :
7360 : : private:
7361 : : const region *m_src_region;
7362 : : const region *m_dest_region;
7363 : : const svalue *m_copied_sval;
7364 : : };
7365 : :
7366 : : /* Return true if any part of SVAL is uninitialized. */
7367 : :
7368 : : static bool
7369 : 80 : contains_uninit_p (const svalue *sval)
7370 : : {
7371 : 80 : switch (sval->get_kind ())
7372 : : {
7373 : : default:
7374 : : return false;
7375 : 4 : case SK_POISONED:
7376 : 4 : {
7377 : 4 : const poisoned_svalue *psval
7378 : 4 : = as_a <const poisoned_svalue *> (sval);
7379 : 4 : return psval->get_poison_kind () == poison_kind::uninit;
7380 : : }
7381 : 43 : case SK_COMPOUND:
7382 : 43 : {
7383 : 43 : const compound_svalue *compound_sval
7384 : 43 : = as_a <const compound_svalue *> (sval);
7385 : :
7386 : 141 : for (auto iter : *compound_sval)
7387 : : {
7388 : 119 : const svalue *sval = iter.m_sval;
7389 : 238 : if (const poisoned_svalue *psval
7390 : 119 : = sval->dyn_cast_poisoned_svalue ())
7391 : 21 : if (psval->get_poison_kind () == poison_kind::uninit)
7392 : 21 : return true;
7393 : : }
7394 : :
7395 : 22 : return false;
7396 : : }
7397 : : }
7398 : : }
7399 : :
7400 : : /* Function for use by plugins when simulating writing data through a
7401 : : pointer to an "untrusted" region DST_REG (and thus crossing a security
7402 : : boundary), such as copying data to user space in an OS kernel.
7403 : :
7404 : : Check that COPIED_SVAL is fully initialized. If not, complain about
7405 : : an infoleak to CTXT.
7406 : :
7407 : : SRC_REG can be nullptr; if non-NULL it is used as a hint in the diagnostic
7408 : : as to where COPIED_SVAL came from. */
7409 : :
7410 : : void
7411 : 80 : region_model::maybe_complain_about_infoleak (const region *dst_reg,
7412 : : const svalue *copied_sval,
7413 : : const region *src_reg,
7414 : : region_model_context *ctxt)
7415 : : {
7416 : : /* Check for exposure. */
7417 : 80 : if (contains_uninit_p (copied_sval))
7418 : 25 : ctxt->warn
7419 : 25 : (std::make_unique<exposure_through_uninit_copy> (src_reg,
7420 : : dst_reg,
7421 : : copied_sval));
7422 : 80 : }
7423 : :
7424 : : /* Set errno to a positive symbolic int, as if some error has occurred. */
7425 : :
7426 : : void
7427 : 453 : region_model::set_errno (const call_details &cd)
7428 : : {
7429 : 453 : const region *errno_reg = m_mgr->get_errno_region ();
7430 : 453 : conjured_purge p (this, cd.get_ctxt ());
7431 : 453 : const svalue *new_errno_sval
7432 : 453 : = m_mgr->get_or_create_conjured_svalue (integer_type_node,
7433 : 453 : &cd.get_call_stmt (),
7434 : : errno_reg, p);
7435 : 453 : const svalue *zero
7436 : 453 : = m_mgr->get_or_create_int_cst (integer_type_node, 0);
7437 : 453 : add_constraint (new_errno_sval, GT_EXPR, zero, cd.get_ctxt ());
7438 : 453 : set_value (errno_reg, new_errno_sval, cd.get_ctxt ());
7439 : 453 : }
7440 : :
7441 : : // class region_model_context
7442 : :
7443 : : bool
7444 : 3912 : region_model_context::
7445 : : warn (std::unique_ptr<pending_diagnostic> d,
7446 : : std::unique_ptr<pending_location::fixer_for_epath> ploc_fixer)
7447 : : {
7448 : 3912 : pending_location ploc (get_pending_location_for_diag ());
7449 : 3912 : ploc.m_fixer_for_epath = std::move (ploc_fixer);
7450 : 3912 : return warn_at (std::move (d), std::move (ploc));
7451 : 3912 : }
7452 : :
7453 : : /* class noop_region_model_context : public region_model_context. */
7454 : :
7455 : : void
7456 : 0 : noop_region_model_context::add_note (std::unique_ptr<pending_note>)
7457 : : {
7458 : 0 : }
7459 : :
7460 : : void
7461 : 0 : noop_region_model_context::add_event (std::unique_ptr<checker_event>)
7462 : : {
7463 : 0 : }
7464 : :
7465 : : void
7466 : 78 : noop_region_model_context::bifurcate (std::unique_ptr<custom_edge_info>)
7467 : : {
7468 : 78 : }
7469 : :
7470 : : void
7471 : 0 : noop_region_model_context::terminate_path ()
7472 : : {
7473 : 0 : }
7474 : :
7475 : : /* class region_model_context_decorator : public region_model_context. */
7476 : :
7477 : : void
7478 : 166 : region_model_context_decorator::add_event (std::unique_ptr<checker_event> event)
7479 : : {
7480 : 166 : if (m_inner)
7481 : 166 : m_inner->add_event (std::move (event));
7482 : 166 : }
7483 : :
7484 : : /* struct model_merger. */
7485 : :
7486 : : /* Dump a multiline representation of this merger to PP. */
7487 : :
7488 : : void
7489 : 0 : model_merger::dump_to_pp (pretty_printer *pp, bool simple) const
7490 : : {
7491 : 0 : pp_string (pp, "model A:");
7492 : 0 : pp_newline (pp);
7493 : 0 : m_model_a->dump_to_pp (pp, simple, true);
7494 : 0 : pp_newline (pp);
7495 : :
7496 : 0 : pp_string (pp, "model B:");
7497 : 0 : pp_newline (pp);
7498 : 0 : m_model_b->dump_to_pp (pp, simple, true);
7499 : 0 : pp_newline (pp);
7500 : :
7501 : 0 : pp_string (pp, "merged model:");
7502 : 0 : pp_newline (pp);
7503 : 0 : m_merged_model->dump_to_pp (pp, simple, true);
7504 : 0 : pp_newline (pp);
7505 : 0 : }
7506 : :
7507 : : /* Dump a multiline representation of this merger to FILE. */
7508 : :
7509 : : void
7510 : 0 : model_merger::dump (FILE *fp, bool simple) const
7511 : : {
7512 : 0 : tree_dump_pretty_printer pp (fp);
7513 : 0 : dump_to_pp (&pp, simple);
7514 : 0 : }
7515 : :
7516 : : /* Dump a multiline representation of this merger to stderr. */
7517 : :
7518 : : DEBUG_FUNCTION void
7519 : 0 : model_merger::dump (bool simple) const
7520 : : {
7521 : 0 : dump (stderr, simple);
7522 : 0 : }
7523 : :
7524 : : /* Return true if it's OK to merge SVAL with other svalues. */
7525 : :
7526 : : bool
7527 : 276817 : model_merger::mergeable_svalue_p (const svalue *sval) const
7528 : : {
7529 : 276817 : if (m_ext_state)
7530 : : {
7531 : : /* Reject merging svalues that have non-purgable sm-state,
7532 : : to avoid falsely reporting memory leaks by merging them
7533 : : with something else. For example, given a local var "p",
7534 : : reject the merger of a:
7535 : : store_a mapping "p" to a malloc-ed ptr
7536 : : with:
7537 : : store_b mapping "p" to a NULL ptr. */
7538 : 276769 : if (m_state_a)
7539 : 276769 : if (!m_state_a->can_purge_p (*m_ext_state, sval))
7540 : : return false;
7541 : 274892 : if (m_state_b)
7542 : 274892 : if (!m_state_b->can_purge_p (*m_ext_state, sval))
7543 : : return false;
7544 : : }
7545 : : return true;
7546 : : }
7547 : :
7548 : : /* Mark WIDENING_SVAL as changing meaning during the merge. */
7549 : :
7550 : : void
7551 : 894 : model_merger::on_widening_reuse (const widening_svalue *widening_sval)
7552 : : {
7553 : 894 : m_svals_changing_meaning.add (widening_sval);
7554 : 894 : }
7555 : :
7556 : : } // namespace ana
7557 : :
7558 : : /* Dump RMODEL fully to stderr (i.e. without summarization). */
7559 : :
7560 : : DEBUG_FUNCTION void
7561 : 0 : debug (const region_model &rmodel)
7562 : : {
7563 : 0 : rmodel.dump (false);
7564 : 0 : }
7565 : :
7566 : : /* class rejected_op_constraint : public rejected_constraint. */
7567 : :
7568 : : void
7569 : 4 : rejected_op_constraint::dump_to_pp (pretty_printer *pp) const
7570 : : {
7571 : 4 : region_model m (m_model);
7572 : 4 : const svalue *lhs_sval = m.get_rvalue (m_lhs, nullptr);
7573 : 4 : const svalue *rhs_sval = m.get_rvalue (m_rhs, nullptr);
7574 : 4 : lhs_sval->dump_to_pp (pp, true);
7575 : 4 : pp_printf (pp, " %s ", op_symbol_code (m_op));
7576 : 4 : rhs_sval->dump_to_pp (pp, true);
7577 : 4 : }
7578 : :
7579 : : /* class rejected_default_case : public rejected_constraint. */
7580 : :
7581 : : void
7582 : 0 : rejected_default_case::dump_to_pp (pretty_printer *pp) const
7583 : : {
7584 : 0 : pp_string (pp, "implicit default for enum");
7585 : 0 : }
7586 : :
7587 : : /* class rejected_ranges_constraint : public rejected_constraint. */
7588 : :
7589 : : void
7590 : 0 : rejected_ranges_constraint::dump_to_pp (pretty_printer *pp) const
7591 : : {
7592 : 0 : region_model m (m_model);
7593 : 0 : const svalue *sval = m.get_rvalue (m_expr, nullptr);
7594 : 0 : sval->dump_to_pp (pp, true);
7595 : 0 : pp_string (pp, " in ");
7596 : 0 : m_ranges->dump_to_pp (pp, true);
7597 : 0 : }
7598 : :
7599 : : /* class engine. */
7600 : :
7601 : : /* engine's ctor. */
7602 : :
7603 : 3317 : engine::engine (region_model_manager &mgr,
7604 : 3317 : const supergraph *sg)
7605 : 3317 : : m_mgr (mgr),
7606 : 3317 : m_sg (sg)
7607 : : {
7608 : 3317 : }
7609 : :
7610 : : /* Dump the managed objects by class to LOGGER, and the per-class totals. */
7611 : :
7612 : : void
7613 : 2 : engine::log_stats (logger *logger) const
7614 : : {
7615 : 2 : m_mgr.log_stats (logger, true);
7616 : 2 : }
7617 : :
7618 : : namespace ana {
7619 : :
7620 : : #if CHECKING_P
7621 : :
7622 : : namespace selftest {
7623 : :
7624 : : /* Build a constant tree of the given type from STR. */
7625 : :
7626 : : static tree
7627 : 64 : build_real_cst_from_string (tree type, const char *str)
7628 : : {
7629 : 64 : REAL_VALUE_TYPE real;
7630 : 64 : real_from_string (&real, str);
7631 : 64 : return build_real (type, real);
7632 : : }
7633 : :
7634 : : /* Append various "interesting" constants to OUT (e.g. NaN). */
7635 : :
7636 : : static void
7637 : 8 : append_interesting_constants (auto_vec<tree> *out)
7638 : : {
7639 : 8 : out->safe_push (integer_zero_node);
7640 : 8 : out->safe_push (build_int_cst (integer_type_node, 42));
7641 : 8 : out->safe_push (build_int_cst (unsigned_type_node, 0));
7642 : 8 : out->safe_push (build_int_cst (unsigned_type_node, 42));
7643 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "QNaN"));
7644 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-QNaN"));
7645 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "SNaN"));
7646 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-SNaN"));
7647 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "0.0"));
7648 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-0.0"));
7649 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "Inf"));
7650 : 8 : out->safe_push (build_real_cst_from_string (float_type_node, "-Inf"));
7651 : 8 : }
7652 : :
7653 : : /* Verify that tree_cmp is a well-behaved comparator for qsort, even
7654 : : if the underlying constants aren't comparable. */
7655 : :
7656 : : static void
7657 : 4 : test_tree_cmp_on_constants ()
7658 : : {
7659 : 4 : auto_vec<tree> csts;
7660 : 4 : append_interesting_constants (&csts);
7661 : :
7662 : : /* Try sorting every triple. */
7663 : 4 : const unsigned num = csts.length ();
7664 : 52 : for (unsigned i = 0; i < num; i++)
7665 : 624 : for (unsigned j = 0; j < num; j++)
7666 : 7488 : for (unsigned k = 0; k < num; k++)
7667 : : {
7668 : 6912 : auto_vec<tree> v (3);
7669 : 6912 : v.quick_push (csts[i]);
7670 : 6912 : v.quick_push (csts[j]);
7671 : 6912 : v.quick_push (csts[k]);
7672 : 6912 : v.qsort (tree_cmp);
7673 : 6912 : }
7674 : 4 : }
7675 : :
7676 : : /* Implementation detail of the ASSERT_CONDITION_* macros. */
7677 : :
7678 : : void
7679 : 8 : assert_condition (const location &loc,
7680 : : region_model &model,
7681 : : const svalue *lhs, tree_code op, const svalue *rhs,
7682 : : tristate expected)
7683 : : {
7684 : 8 : tristate actual = model.eval_condition (lhs, op, rhs);
7685 : 8 : ASSERT_EQ_AT (loc, actual, expected);
7686 : 8 : }
7687 : :
7688 : : /* Implementation detail of the ASSERT_CONDITION_* macros. */
7689 : :
7690 : : void
7691 : 3084 : assert_condition (const location &loc,
7692 : : region_model &model,
7693 : : tree lhs, tree_code op, tree rhs,
7694 : : tristate expected)
7695 : : {
7696 : 3084 : tristate actual = model.eval_condition (lhs, op, rhs, nullptr);
7697 : 3084 : ASSERT_EQ_AT (loc, actual, expected);
7698 : 3084 : }
7699 : :
7700 : : /* Implementation detail of ASSERT_DUMP_TREE_EQ. */
7701 : :
7702 : : static void
7703 : 20 : assert_dump_tree_eq (const location &loc, tree t, const char *expected)
7704 : : {
7705 : 20 : auto_fix_quotes sentinel;
7706 : 20 : pretty_printer pp;
7707 : 20 : pp_format_decoder (&pp) = default_tree_printer;
7708 : 20 : dump_tree (&pp, t);
7709 : 20 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
7710 : 20 : }
7711 : :
7712 : : /* Assert that dump_tree (T) is EXPECTED. */
7713 : :
7714 : : #define ASSERT_DUMP_TREE_EQ(T, EXPECTED) \
7715 : : SELFTEST_BEGIN_STMT \
7716 : : assert_dump_tree_eq ((SELFTEST_LOCATION), (T), (EXPECTED)); \
7717 : : SELFTEST_END_STMT
7718 : :
7719 : : /* Implementation detail of ASSERT_DUMP_EQ. */
7720 : :
7721 : : static void
7722 : 8 : assert_dump_eq (const location &loc,
7723 : : const region_model &model,
7724 : : bool summarize,
7725 : : const char *expected)
7726 : : {
7727 : 8 : auto_fix_quotes sentinel;
7728 : 8 : pretty_printer pp;
7729 : 8 : pp_format_decoder (&pp) = default_tree_printer;
7730 : :
7731 : 8 : model.dump_to_pp (&pp, summarize, true);
7732 : 8 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
7733 : 8 : }
7734 : :
7735 : : /* Assert that MODEL.dump_to_pp (SUMMARIZE) is EXPECTED. */
7736 : :
7737 : : #define ASSERT_DUMP_EQ(MODEL, SUMMARIZE, EXPECTED) \
7738 : : SELFTEST_BEGIN_STMT \
7739 : : assert_dump_eq ((SELFTEST_LOCATION), (MODEL), (SUMMARIZE), (EXPECTED)); \
7740 : : SELFTEST_END_STMT
7741 : :
7742 : : /* Smoketest for region_model::dump_to_pp. */
7743 : :
7744 : : static void
7745 : 4 : test_dump ()
7746 : : {
7747 : 4 : region_model_manager mgr;
7748 : 4 : region_model model (&mgr);
7749 : :
7750 : 4 : ASSERT_DUMP_EQ (model, false,
7751 : : "stack depth: 0\n"
7752 : : "m_called_unknown_fn: FALSE\n"
7753 : : "constraint_manager:\n"
7754 : : " equiv classes:\n"
7755 : : " constraints:\n");
7756 : 4 : ASSERT_DUMP_EQ (model, true,
7757 : : "stack depth: 0\n"
7758 : : "m_called_unknown_fn: FALSE\n"
7759 : : "constraint_manager:\n"
7760 : : " equiv classes:\n"
7761 : : " constraints:\n");
7762 : :
7763 : 4 : text_art::ascii_theme theme;
7764 : 4 : pretty_printer pp;
7765 : 4 : dump_to_pp (model, &theme, &pp);
7766 : 4 : ASSERT_STREQ ("Region Model\n"
7767 : : "`- Store\n"
7768 : : " `- m_called_unknown_fn: false\n",
7769 : : pp_formatted_text (&pp));
7770 : 4 : }
7771 : :
7772 : : /* Helper function for selftests. Create a struct or union type named NAME,
7773 : : with the fields given by the FIELD_DECLS in FIELDS.
7774 : : If IS_STRUCT is true create a RECORD_TYPE (aka a struct), otherwise
7775 : : create a UNION_TYPE. */
7776 : :
7777 : : static tree
7778 : 16 : make_test_compound_type (const char *name, bool is_struct,
7779 : : const auto_vec<tree> *fields)
7780 : : {
7781 : 16 : tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE);
7782 : 16 : TYPE_NAME (t) = get_identifier (name);
7783 : 16 : TYPE_SIZE (t) = 0;
7784 : :
7785 : 16 : tree fieldlist = NULL_TREE;
7786 : 16 : int i;
7787 : 16 : tree field;
7788 : 48 : FOR_EACH_VEC_ELT (*fields, i, field)
7789 : : {
7790 : 32 : gcc_assert (TREE_CODE (field) == FIELD_DECL);
7791 : 32 : DECL_CONTEXT (field) = t;
7792 : 32 : fieldlist = chainon (field, fieldlist);
7793 : : }
7794 : 16 : fieldlist = nreverse (fieldlist);
7795 : 16 : TYPE_FIELDS (t) = fieldlist;
7796 : :
7797 : 16 : layout_type (t);
7798 : 16 : return t;
7799 : : }
7800 : :
7801 : : /* Selftest fixture for creating the type "struct coord {int x; int y; };". */
7802 : :
7803 : : struct coord_test
7804 : : {
7805 : 16 : coord_test ()
7806 : 16 : {
7807 : 16 : auto_vec<tree> fields;
7808 : 16 : m_x_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
7809 : : get_identifier ("x"), integer_type_node);
7810 : 16 : fields.safe_push (m_x_field);
7811 : 16 : m_y_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
7812 : : get_identifier ("y"), integer_type_node);
7813 : 16 : fields.safe_push (m_y_field);
7814 : 16 : m_coord_type = make_test_compound_type ("coord", true, &fields);
7815 : 16 : }
7816 : :
7817 : : tree m_x_field;
7818 : : tree m_y_field;
7819 : : tree m_coord_type;
7820 : : };
7821 : :
7822 : : /* Verify usage of a struct. */
7823 : :
7824 : : static void
7825 : 4 : test_struct ()
7826 : : {
7827 : 4 : coord_test ct;
7828 : :
7829 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
7830 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
7831 : : c, ct.m_x_field, NULL_TREE);
7832 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
7833 : : c, ct.m_y_field, NULL_TREE);
7834 : :
7835 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
7836 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
7837 : :
7838 : 4 : region_model_manager mgr;
7839 : 4 : region_model model (&mgr);
7840 : : /* Set fields in order y, then x. */
7841 : 4 : model.set_value (c_y, int_m3, nullptr);
7842 : 4 : model.set_value (c_x, int_17, nullptr);
7843 : :
7844 : : /* Verify get_offset for "c.x". */
7845 : 4 : {
7846 : 4 : const region *c_x_reg = model.get_lvalue (c_x, nullptr);
7847 : 4 : region_offset offset = c_x_reg->get_offset (&mgr);
7848 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, nullptr));
7849 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
7850 : : }
7851 : :
7852 : : /* Verify get_offset for "c.y". */
7853 : 4 : {
7854 : 4 : const region *c_y_reg = model.get_lvalue (c_y, nullptr);
7855 : 4 : region_offset offset = c_y_reg->get_offset (&mgr);
7856 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, nullptr));
7857 : 4 : ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE);
7858 : : }
7859 : :
7860 : : /* Check iteration order of binding_cluster (and thus of binding_map). */
7861 : 4 : {
7862 : 4 : std::vector<binding_map::binding_pair> vec;
7863 : 4 : auto cluster
7864 : 4 : = model.get_store ()->get_cluster (model.get_lvalue (c, nullptr));
7865 : 12 : for (auto iter : *cluster)
7866 : 8 : vec.push_back (iter);
7867 : 4 : ASSERT_EQ (vec.size (), 2);
7868 : : /* we should get them back in ascending order in memory (x then y). */
7869 : : /* x */
7870 : 4 : ASSERT_EQ (vec[0].m_key->dyn_cast_concrete_binding ()->get_bit_range (),
7871 : : bit_range (0, INT_TYPE_SIZE));
7872 : 4 : ASSERT_TRUE (tree_int_cst_equal(vec[0].m_sval->maybe_get_constant (),
7873 : : int_17));
7874 : : /* y */
7875 : 4 : ASSERT_EQ (vec[1].m_key->dyn_cast_concrete_binding ()->get_bit_range (),
7876 : : bit_range (INT_TYPE_SIZE, INT_TYPE_SIZE));
7877 : 4 : ASSERT_TRUE (tree_int_cst_equal(vec[1].m_sval->maybe_get_constant (),
7878 : : int_m3));
7879 : 4 : }
7880 : 4 : }
7881 : :
7882 : : /* Verify usage of an array element. */
7883 : :
7884 : : static void
7885 : 4 : test_array_1 ()
7886 : : {
7887 : 4 : tree tlen = size_int (10);
7888 : 4 : tree arr_type = build_array_type (char_type_node, build_index_type (tlen));
7889 : :
7890 : 4 : tree a = build_global_decl ("a", arr_type);
7891 : :
7892 : 4 : region_model_manager mgr;
7893 : 4 : region_model model (&mgr);
7894 : 4 : tree int_0 = integer_zero_node;
7895 : 4 : tree a_0 = build4 (ARRAY_REF, char_type_node,
7896 : : a, int_0, NULL_TREE, NULL_TREE);
7897 : 4 : tree char_A = build_int_cst (char_type_node, 'A');
7898 : 4 : model.set_value (a_0, char_A, nullptr);
7899 : 4 : }
7900 : :
7901 : : /* Verify that region_model::get_representative_tree works as expected. */
7902 : :
7903 : : static void
7904 : 4 : test_get_representative_tree ()
7905 : : {
7906 : 4 : region_model_manager mgr;
7907 : :
7908 : : /* STRING_CST. */
7909 : 4 : {
7910 : 4 : tree string_cst = build_string (4, "foo");
7911 : 4 : region_model m (&mgr);
7912 : 4 : const svalue *str_sval = m.get_rvalue (string_cst, nullptr);
7913 : 4 : tree rep = m.get_representative_tree (str_sval);
7914 : 4 : ASSERT_EQ (rep, string_cst);
7915 : 4 : }
7916 : :
7917 : : /* String literal. */
7918 : 4 : {
7919 : 4 : tree string_cst_ptr = build_string_literal (4, "foo");
7920 : 4 : region_model m (&mgr);
7921 : 4 : const svalue *str_sval = m.get_rvalue (string_cst_ptr, nullptr);
7922 : 4 : tree rep = m.get_representative_tree (str_sval);
7923 : 4 : ASSERT_DUMP_TREE_EQ (rep, "&\"foo\"[0]");
7924 : 4 : }
7925 : :
7926 : : /* Value of an element within an array. */
7927 : 4 : {
7928 : 4 : tree tlen = size_int (10);
7929 : 4 : tree arr_type = build_array_type (char_type_node, build_index_type (tlen));
7930 : 4 : tree a = build_global_decl ("a", arr_type);
7931 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
7932 : 4 : char_type_node, "test value");
7933 : :
7934 : : /* Value of a[3]. */
7935 : 4 : {
7936 : 4 : test_region_model_context ctxt;
7937 : 4 : region_model model (&mgr);
7938 : 4 : tree int_3 = build_int_cst (integer_type_node, 3);
7939 : 4 : tree a_3 = build4 (ARRAY_REF, char_type_node,
7940 : : a, int_3, NULL_TREE, NULL_TREE);
7941 : 4 : const region *a_3_reg = model.get_lvalue (a_3, &ctxt);
7942 : 4 : model.set_value (a_3_reg, &test_sval, &ctxt);
7943 : 4 : tree rep = model.get_representative_tree (&test_sval);
7944 : 4 : ASSERT_DUMP_TREE_EQ (rep, "a[3]");
7945 : 4 : }
7946 : :
7947 : : /* Value of a[0]. */
7948 : 4 : {
7949 : 4 : test_region_model_context ctxt;
7950 : 4 : region_model model (&mgr);
7951 : 4 : tree idx = integer_zero_node;
7952 : 4 : tree a_0 = build4 (ARRAY_REF, char_type_node,
7953 : : a, idx, NULL_TREE, NULL_TREE);
7954 : 4 : const region *a_0_reg = model.get_lvalue (a_0, &ctxt);
7955 : 4 : model.set_value (a_0_reg, &test_sval, &ctxt);
7956 : 4 : tree rep = model.get_representative_tree (&test_sval);
7957 : 4 : ASSERT_DUMP_TREE_EQ (rep, "a[0]");
7958 : 4 : }
7959 : 4 : }
7960 : :
7961 : : /* Value of a field within a struct. */
7962 : 4 : {
7963 : 4 : coord_test ct;
7964 : :
7965 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
7966 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
7967 : : c, ct.m_x_field, NULL_TREE);
7968 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
7969 : : c, ct.m_y_field, NULL_TREE);
7970 : :
7971 : 4 : test_region_model_context ctxt;
7972 : :
7973 : : /* Value of initial field. */
7974 : 4 : {
7975 : 4 : region_model m (&mgr);
7976 : 4 : const region *c_x_reg = m.get_lvalue (c_x, &ctxt);
7977 : 4 : placeholder_svalue test_sval_x (mgr.alloc_symbol_id (),
7978 : 4 : integer_type_node, "test x val");
7979 : 4 : m.set_value (c_x_reg, &test_sval_x, &ctxt);
7980 : 4 : tree rep = m.get_representative_tree (&test_sval_x);
7981 : 4 : ASSERT_DUMP_TREE_EQ (rep, "c.x");
7982 : 4 : }
7983 : :
7984 : : /* Value of non-initial field. */
7985 : 4 : {
7986 : 4 : region_model m (&mgr);
7987 : 4 : const region *c_y_reg = m.get_lvalue (c_y, &ctxt);
7988 : 4 : placeholder_svalue test_sval_y (mgr.alloc_symbol_id (),
7989 : 4 : integer_type_node, "test y val");
7990 : 4 : m.set_value (c_y_reg, &test_sval_y, &ctxt);
7991 : 4 : tree rep = m.get_representative_tree (&test_sval_y);
7992 : 4 : ASSERT_DUMP_TREE_EQ (rep, "c.y");
7993 : 4 : }
7994 : 4 : }
7995 : 4 : }
7996 : :
7997 : : /* Verify that calling region_model::get_rvalue repeatedly on the same
7998 : : tree constant retrieves the same svalue *. */
7999 : :
8000 : : static void
8001 : 4 : test_unique_constants ()
8002 : : {
8003 : 4 : tree int_0 = integer_zero_node;
8004 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8005 : :
8006 : 4 : test_region_model_context ctxt;
8007 : 4 : region_model_manager mgr;
8008 : 4 : region_model model (&mgr);
8009 : 4 : ASSERT_EQ (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_0, &ctxt));
8010 : 4 : ASSERT_EQ (model.get_rvalue (int_42, &ctxt),
8011 : : model.get_rvalue (int_42, &ctxt));
8012 : 4 : ASSERT_NE (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_42, &ctxt));
8013 : 4 : ASSERT_EQ (ctxt.get_num_diagnostics (), 0);
8014 : :
8015 : : /* A "(const int)42" will be a different tree from "(int)42)"... */
8016 : 4 : tree const_int_type_node
8017 : 4 : = build_qualified_type (integer_type_node, TYPE_QUAL_CONST);
8018 : 4 : tree const_int_42 = build_int_cst (const_int_type_node, 42);
8019 : 4 : ASSERT_NE (int_42, const_int_42);
8020 : : /* It should have a different const_svalue. */
8021 : 4 : const svalue *int_42_sval = model.get_rvalue (int_42, &ctxt);
8022 : 4 : const svalue *const_int_42_sval = model.get_rvalue (const_int_42, &ctxt);
8023 : 4 : ASSERT_NE (int_42_sval, const_int_42_sval);
8024 : : /* But they should compare as equal. */
8025 : 4 : ASSERT_CONDITION_TRUE (model, int_42_sval, EQ_EXPR, const_int_42_sval);
8026 : 4 : ASSERT_CONDITION_FALSE (model, int_42_sval, NE_EXPR, const_int_42_sval);
8027 : 4 : }
8028 : :
8029 : : /* Verify that each type gets its own singleton unknown_svalue within a
8030 : : region_model_manager, and that NULL_TREE gets its own singleton. */
8031 : :
8032 : : static void
8033 : 4 : test_unique_unknowns ()
8034 : : {
8035 : 4 : region_model_manager mgr;
8036 : 4 : const svalue *unknown_int
8037 : 4 : = mgr.get_or_create_unknown_svalue (integer_type_node);
8038 : : /* Repeated calls with the same type should get the same "unknown"
8039 : : svalue. */
8040 : 4 : const svalue *unknown_int_2
8041 : 4 : = mgr.get_or_create_unknown_svalue (integer_type_node);
8042 : 4 : ASSERT_EQ (unknown_int, unknown_int_2);
8043 : :
8044 : : /* Different types (or the NULL type) should have different
8045 : : unknown_svalues. */
8046 : 4 : const svalue *unknown_NULL_type = mgr.get_or_create_unknown_svalue (nullptr);
8047 : 4 : ASSERT_NE (unknown_NULL_type, unknown_int);
8048 : :
8049 : : /* Repeated calls with NULL for the type should get the same "unknown"
8050 : : svalue. */
8051 : 4 : const svalue *unknown_NULL_type_2 = mgr.get_or_create_unknown_svalue (nullptr);
8052 : 4 : ASSERT_EQ (unknown_NULL_type, unknown_NULL_type_2);
8053 : 4 : }
8054 : :
8055 : : /* Verify that initial_svalue are handled as expected. */
8056 : :
8057 : : static void
8058 : 4 : test_initial_svalue_folding ()
8059 : : {
8060 : 4 : region_model_manager mgr;
8061 : 4 : tree x = build_global_decl ("x", integer_type_node);
8062 : 4 : tree y = build_global_decl ("y", integer_type_node);
8063 : :
8064 : 4 : test_region_model_context ctxt;
8065 : 4 : region_model model (&mgr);
8066 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8067 : 4 : const svalue *y_init = model.get_rvalue (y, &ctxt);
8068 : 4 : ASSERT_NE (x_init, y_init);
8069 : 4 : const region *x_reg = model.get_lvalue (x, &ctxt);
8070 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg));
8071 : :
8072 : 4 : }
8073 : :
8074 : : /* Verify that unary ops are folded as expected. */
8075 : :
8076 : : static void
8077 : 4 : test_unaryop_svalue_folding ()
8078 : : {
8079 : 4 : region_model_manager mgr;
8080 : 4 : tree x = build_global_decl ("x", integer_type_node);
8081 : 4 : tree y = build_global_decl ("y", integer_type_node);
8082 : :
8083 : 4 : test_region_model_context ctxt;
8084 : 4 : region_model model (&mgr);
8085 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8086 : 4 : const svalue *y_init = model.get_rvalue (y, &ctxt);
8087 : 4 : const region *x_reg = model.get_lvalue (x, &ctxt);
8088 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg));
8089 : :
8090 : : /* "(int)x" -> "x". */
8091 : 4 : ASSERT_EQ (x_init, mgr.get_or_create_cast (integer_type_node, x_init));
8092 : :
8093 : : /* "(void *)x" -> something other than "x". */
8094 : 4 : ASSERT_NE (x_init, mgr.get_or_create_cast (ptr_type_node, x_init));
8095 : :
8096 : : /* "!(x == y)" -> "x != y". */
8097 : 4 : ASSERT_EQ (mgr.get_or_create_unaryop
8098 : : (boolean_type_node, TRUTH_NOT_EXPR,
8099 : : mgr.get_or_create_binop (boolean_type_node, EQ_EXPR,
8100 : : x_init, y_init)),
8101 : : mgr.get_or_create_binop (boolean_type_node, NE_EXPR,
8102 : : x_init, y_init));
8103 : : /* "!(x > y)" -> "x <= y". */
8104 : 4 : ASSERT_EQ (mgr.get_or_create_unaryop
8105 : : (boolean_type_node, TRUTH_NOT_EXPR,
8106 : : mgr.get_or_create_binop (boolean_type_node, GT_EXPR,
8107 : : x_init, y_init)),
8108 : : mgr.get_or_create_binop (boolean_type_node, LE_EXPR,
8109 : : x_init, y_init));
8110 : 4 : }
8111 : :
8112 : : /* Verify that binops on constant svalues are folded. */
8113 : :
8114 : : static void
8115 : 4 : test_binop_svalue_folding ()
8116 : : {
8117 : : #define NUM_CSTS 10
8118 : 4 : tree cst_int[NUM_CSTS];
8119 : 4 : region_model_manager mgr;
8120 : 4 : const svalue *cst_sval[NUM_CSTS];
8121 : 44 : for (int i = 0; i < NUM_CSTS; i++)
8122 : : {
8123 : 40 : cst_int[i] = build_int_cst (integer_type_node, i);
8124 : 40 : cst_sval[i] = mgr.get_or_create_constant_svalue (cst_int[i]);
8125 : 40 : ASSERT_EQ (cst_sval[i]->get_kind (), SK_CONSTANT);
8126 : 40 : ASSERT_EQ (cst_sval[i]->maybe_get_constant (), cst_int[i]);
8127 : : }
8128 : :
8129 : 44 : for (int i = 0; i < NUM_CSTS; i++)
8130 : 440 : for (int j = 0; j < NUM_CSTS; j++)
8131 : : {
8132 : 400 : if (i != j)
8133 : 360 : ASSERT_NE (cst_sval[i], cst_sval[j]);
8134 : 400 : if (i + j < NUM_CSTS)
8135 : : {
8136 : 220 : const svalue *sum
8137 : 220 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8138 : : cst_sval[i], cst_sval[j]);
8139 : 220 : ASSERT_EQ (sum, cst_sval[i + j]);
8140 : : }
8141 : 400 : if (i - j >= 0)
8142 : : {
8143 : 220 : const svalue *difference
8144 : 220 : = mgr.get_or_create_binop (integer_type_node, MINUS_EXPR,
8145 : : cst_sval[i], cst_sval[j]);
8146 : 220 : ASSERT_EQ (difference, cst_sval[i - j]);
8147 : : }
8148 : 400 : if (i * j < NUM_CSTS)
8149 : : {
8150 : 168 : const svalue *product
8151 : 168 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8152 : : cst_sval[i], cst_sval[j]);
8153 : 168 : ASSERT_EQ (product, cst_sval[i * j]);
8154 : : }
8155 : 400 : const svalue *eq = mgr.get_or_create_binop (integer_type_node, EQ_EXPR,
8156 : : cst_sval[i], cst_sval[j]);
8157 : 400 : ASSERT_EQ (eq, i == j ? cst_sval[1] : cst_sval [0]);
8158 : 400 : const svalue *neq = mgr.get_or_create_binop (integer_type_node, NE_EXPR,
8159 : : cst_sval[i], cst_sval[j]);
8160 : 400 : ASSERT_EQ (neq, i != j ? cst_sval[1] : cst_sval [0]);
8161 : : // etc
8162 : : }
8163 : :
8164 : 4 : tree x = build_global_decl ("x", integer_type_node);
8165 : :
8166 : 4 : test_region_model_context ctxt;
8167 : 4 : region_model model (&mgr);
8168 : 4 : const svalue *x_init = model.get_rvalue (x, &ctxt);
8169 : :
8170 : : /* PLUS_EXPR folding. */
8171 : 4 : const svalue *x_init_plus_zero
8172 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8173 : : x_init, cst_sval[0]);
8174 : 4 : ASSERT_EQ (x_init_plus_zero, x_init);
8175 : 4 : const svalue *zero_plus_x_init
8176 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8177 : : cst_sval[0], x_init);
8178 : 4 : ASSERT_EQ (zero_plus_x_init, x_init);
8179 : :
8180 : : /* MULT_EXPR folding. */
8181 : 4 : const svalue *x_init_times_zero
8182 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8183 : : x_init, cst_sval[0]);
8184 : 4 : ASSERT_EQ (x_init_times_zero, cst_sval[0]);
8185 : 4 : const svalue *zero_times_x_init
8186 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8187 : : cst_sval[0], x_init);
8188 : 4 : ASSERT_EQ (zero_times_x_init, cst_sval[0]);
8189 : :
8190 : 4 : const svalue *x_init_times_one
8191 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8192 : : x_init, cst_sval[1]);
8193 : 4 : ASSERT_EQ (x_init_times_one, x_init);
8194 : 4 : const svalue *one_times_x_init
8195 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8196 : : cst_sval[1], x_init);
8197 : 4 : ASSERT_EQ (one_times_x_init, x_init);
8198 : :
8199 : : // etc
8200 : : // TODO: do we want to use the match-and-simplify DSL for this?
8201 : :
8202 : : /* Verify that binops put any constants on the RHS. */
8203 : 4 : const svalue *four_times_x_init
8204 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8205 : : cst_sval[4], x_init);
8206 : 4 : const svalue *x_init_times_four
8207 : 4 : = mgr.get_or_create_binop (integer_type_node, MULT_EXPR,
8208 : : x_init, cst_sval[4]);
8209 : 4 : ASSERT_EQ (four_times_x_init, x_init_times_four);
8210 : 4 : const binop_svalue *binop = four_times_x_init->dyn_cast_binop_svalue ();
8211 : 4 : ASSERT_EQ (binop->get_op (), MULT_EXPR);
8212 : 4 : ASSERT_EQ (binop->get_arg0 (), x_init);
8213 : 4 : ASSERT_EQ (binop->get_arg1 (), cst_sval[4]);
8214 : :
8215 : : /* Verify that ((x + 1) + 1) == (x + 2). */
8216 : 4 : const svalue *x_init_plus_one
8217 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8218 : : x_init, cst_sval[1]);
8219 : 4 : const svalue *x_init_plus_two
8220 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8221 : : x_init, cst_sval[2]);
8222 : 4 : const svalue *x_init_plus_one_plus_one
8223 : 4 : = mgr.get_or_create_binop (integer_type_node, PLUS_EXPR,
8224 : : x_init_plus_one, cst_sval[1]);
8225 : 4 : ASSERT_EQ (x_init_plus_one_plus_one, x_init_plus_two);
8226 : :
8227 : : /* Verify various binops on booleans. */
8228 : 4 : {
8229 : 4 : const svalue *sval_true = mgr.get_or_create_int_cst (boolean_type_node, 1);
8230 : 4 : const svalue *sval_false = mgr.get_or_create_int_cst (boolean_type_node, 0);
8231 : 4 : const svalue *sval_unknown
8232 : 4 : = mgr.get_or_create_unknown_svalue (boolean_type_node);
8233 : 4 : const placeholder_svalue sval_placeholder (mgr.alloc_symbol_id (),
8234 : 4 : boolean_type_node, "v");
8235 : 12 : for (auto op : {BIT_IOR_EXPR, TRUTH_OR_EXPR})
8236 : : {
8237 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8238 : : sval_true, sval_unknown),
8239 : : sval_true);
8240 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8241 : : sval_false, sval_unknown),
8242 : : sval_unknown);
8243 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8244 : : sval_false, &sval_placeholder),
8245 : : &sval_placeholder);
8246 : : }
8247 : 12 : for (auto op : {BIT_AND_EXPR, TRUTH_AND_EXPR})
8248 : : {
8249 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8250 : : sval_false, sval_unknown),
8251 : : sval_false);
8252 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8253 : : sval_true, sval_unknown),
8254 : : sval_unknown);
8255 : 8 : ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op,
8256 : : sval_true, &sval_placeholder),
8257 : : &sval_placeholder);
8258 : : }
8259 : 4 : }
8260 : 4 : }
8261 : :
8262 : : /* Verify that sub_svalues are folded as expected. */
8263 : :
8264 : : static void
8265 : 4 : test_sub_svalue_folding ()
8266 : : {
8267 : 4 : coord_test ct;
8268 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
8269 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8270 : : c, ct.m_x_field, NULL_TREE);
8271 : :
8272 : 4 : region_model_manager mgr;
8273 : 4 : region_model model (&mgr);
8274 : 4 : test_region_model_context ctxt;
8275 : 4 : const region *c_x_reg = model.get_lvalue (c_x, &ctxt);
8276 : :
8277 : : /* Verify that sub_svalue of "unknown" simply
8278 : : yields an unknown. */
8279 : :
8280 : 4 : const svalue *unknown = mgr.get_or_create_unknown_svalue (ct.m_coord_type);
8281 : 4 : const svalue *sub = mgr.get_or_create_sub_svalue (TREE_TYPE (ct.m_x_field),
8282 : : unknown, c_x_reg);
8283 : 4 : ASSERT_EQ (sub->get_kind (), SK_UNKNOWN);
8284 : 4 : ASSERT_EQ (sub->get_type (), TREE_TYPE (ct.m_x_field));
8285 : 4 : }
8286 : :
8287 : : /* Get BIT within VAL as a symbolic value within MGR. */
8288 : :
8289 : : static const svalue *
8290 : 256 : get_bit (region_model_manager *mgr,
8291 : : bit_offset_t bit,
8292 : : unsigned HOST_WIDE_INT val)
8293 : : {
8294 : 256 : const svalue *inner_svalue
8295 : 256 : = mgr->get_or_create_int_cst (unsigned_type_node, val);
8296 : 256 : return mgr->get_or_create_bits_within (boolean_type_node,
8297 : 256 : bit_range (bit, 1),
8298 : 256 : inner_svalue);
8299 : : }
8300 : :
8301 : : /* Verify that bits_within_svalues are folded as expected. */
8302 : :
8303 : : static void
8304 : 4 : test_bits_within_svalue_folding ()
8305 : : {
8306 : 4 : region_model_manager mgr;
8307 : :
8308 : 4 : const svalue *zero = mgr.get_or_create_int_cst (boolean_type_node, 0);
8309 : 4 : const svalue *one = mgr.get_or_create_int_cst (boolean_type_node, 1);
8310 : :
8311 : 4 : {
8312 : 4 : const unsigned val = 0x0000;
8313 : 68 : for (unsigned bit = 0; bit < 16; bit++)
8314 : 64 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8315 : : }
8316 : :
8317 : 4 : {
8318 : 4 : const unsigned val = 0x0001;
8319 : 4 : ASSERT_EQ (get_bit (&mgr, 0, val), one);
8320 : 64 : for (unsigned bit = 1; bit < 16; bit++)
8321 : 60 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8322 : : }
8323 : :
8324 : 4 : {
8325 : 4 : const unsigned val = 0x8000;
8326 : 64 : for (unsigned bit = 0; bit < 15; bit++)
8327 : 60 : ASSERT_EQ (get_bit (&mgr, bit, val), zero);
8328 : 4 : ASSERT_EQ (get_bit (&mgr, 15, val), one);
8329 : : }
8330 : :
8331 : 4 : {
8332 : 4 : const unsigned val = 0xFFFF;
8333 : 68 : for (unsigned bit = 0; bit < 16; bit++)
8334 : 64 : ASSERT_EQ (get_bit (&mgr, bit, val), one);
8335 : : }
8336 : 4 : }
8337 : :
8338 : : /* Test that region::descendent_of_p works as expected. */
8339 : :
8340 : : static void
8341 : 4 : test_descendent_of_p ()
8342 : : {
8343 : 4 : region_model_manager mgr;
8344 : 4 : const region *stack = mgr.get_stack_region ();
8345 : 4 : const region *heap = mgr.get_heap_region ();
8346 : 4 : const region *code = mgr.get_code_region ();
8347 : 4 : const region *globals = mgr.get_globals_region ();
8348 : :
8349 : : /* descendent_of_p should return true when used on the region itself. */
8350 : 4 : ASSERT_TRUE (stack->descendent_of_p (stack));
8351 : 4 : ASSERT_FALSE (stack->descendent_of_p (heap));
8352 : 4 : ASSERT_FALSE (stack->descendent_of_p (code));
8353 : 4 : ASSERT_FALSE (stack->descendent_of_p (globals));
8354 : :
8355 : 4 : tree x = build_global_decl ("x", integer_type_node);
8356 : 4 : const region *x_reg = mgr.get_region_for_global (x);
8357 : 4 : ASSERT_TRUE (x_reg->descendent_of_p (globals));
8358 : :
8359 : : /* A cast_region should be a descendent of the original region. */
8360 : 4 : const region *cast_reg = mgr.get_cast_region (x_reg, ptr_type_node);
8361 : 4 : ASSERT_TRUE (cast_reg->descendent_of_p (x_reg));
8362 : 4 : }
8363 : :
8364 : : /* Verify that bit_range_region works as expected. */
8365 : :
8366 : : static void
8367 : 4 : test_bit_range_regions ()
8368 : : {
8369 : 4 : tree x = build_global_decl ("x", integer_type_node);
8370 : 4 : region_model_manager mgr;
8371 : 4 : const region *x_reg = mgr.get_region_for_global (x);
8372 : 4 : const region *byte0
8373 : 4 : = mgr.get_bit_range (x_reg, char_type_node, bit_range (0, 8));
8374 : 4 : const region *byte1
8375 : 4 : = mgr.get_bit_range (x_reg, char_type_node, bit_range (8, 8));
8376 : 4 : ASSERT_TRUE (byte0->descendent_of_p (x_reg));
8377 : 4 : ASSERT_TRUE (byte1->descendent_of_p (x_reg));
8378 : 4 : ASSERT_NE (byte0, byte1);
8379 : 4 : }
8380 : :
8381 : : /* Verify that simple assignments work as expected. */
8382 : :
8383 : : static void
8384 : 4 : test_assignment ()
8385 : : {
8386 : 4 : tree int_0 = integer_zero_node;
8387 : 4 : tree x = build_global_decl ("x", integer_type_node);
8388 : 4 : tree y = build_global_decl ("y", integer_type_node);
8389 : :
8390 : : /* "x == 0", then use of y, then "y = 0;". */
8391 : 4 : region_model_manager mgr;
8392 : 4 : region_model model (&mgr);
8393 : 4 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
8394 : 4 : ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, int_0);
8395 : 4 : model.set_value (model.get_lvalue (y, nullptr),
8396 : : model.get_rvalue (int_0, nullptr),
8397 : : nullptr);
8398 : 4 : ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, int_0);
8399 : 4 : ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, x);
8400 : 4 : }
8401 : :
8402 : : /* Verify that compound assignments work as expected. */
8403 : :
8404 : : static void
8405 : 4 : test_compound_assignment ()
8406 : : {
8407 : 4 : coord_test ct;
8408 : :
8409 : 4 : tree c = build_global_decl ("c", ct.m_coord_type);
8410 : 4 : tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8411 : : c, ct.m_x_field, NULL_TREE);
8412 : 4 : tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
8413 : : c, ct.m_y_field, NULL_TREE);
8414 : 4 : tree d = build_global_decl ("d", ct.m_coord_type);
8415 : 4 : tree d_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field),
8416 : : d, ct.m_x_field, NULL_TREE);
8417 : 4 : tree d_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field),
8418 : : d, ct.m_y_field, NULL_TREE);
8419 : :
8420 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
8421 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
8422 : :
8423 : 4 : region_model_manager mgr;
8424 : 4 : region_model model (&mgr);
8425 : 4 : model.set_value (c_x, int_17, nullptr);
8426 : 4 : model.set_value (c_y, int_m3, nullptr);
8427 : :
8428 : : /* Copy c to d. */
8429 : 4 : const svalue *sval = model.get_rvalue (c, nullptr);
8430 : 4 : model.set_value (model.get_lvalue (d, nullptr), sval, nullptr);
8431 : :
8432 : : /* Check that the fields have the same svalues. */
8433 : 4 : ASSERT_EQ (model.get_rvalue (c_x, nullptr), model.get_rvalue (d_x, nullptr));
8434 : 4 : ASSERT_EQ (model.get_rvalue (c_y, nullptr), model.get_rvalue (d_y, nullptr));
8435 : 4 : }
8436 : :
8437 : : /* Verify the details of pushing and popping stack frames. */
8438 : :
8439 : : static void
8440 : 4 : test_stack_frames ()
8441 : : {
8442 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8443 : 4 : tree int_10 = build_int_cst (integer_type_node, 10);
8444 : 4 : tree int_5 = build_int_cst (integer_type_node, 5);
8445 : 4 : tree int_0 = integer_zero_node;
8446 : :
8447 : 4 : auto_vec <tree> param_types;
8448 : 4 : tree parent_fndecl = make_fndecl (integer_type_node,
8449 : : "parent_fn",
8450 : : param_types);
8451 : 4 : allocate_struct_function (parent_fndecl, true);
8452 : :
8453 : 4 : tree child_fndecl = make_fndecl (integer_type_node,
8454 : : "child_fn",
8455 : : param_types);
8456 : 4 : allocate_struct_function (child_fndecl, true);
8457 : :
8458 : : /* "a" and "b" in the parent frame. */
8459 : 4 : tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8460 : : get_identifier ("a"),
8461 : : integer_type_node);
8462 : 4 : DECL_CONTEXT (a) = parent_fndecl;
8463 : 4 : tree b = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8464 : : get_identifier ("b"),
8465 : : integer_type_node);
8466 : 4 : DECL_CONTEXT (b) = parent_fndecl;
8467 : : /* "x" and "y" in a child frame. */
8468 : 4 : tree x = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8469 : : get_identifier ("x"),
8470 : : integer_type_node);
8471 : 4 : DECL_CONTEXT (x) = child_fndecl;
8472 : 4 : tree y = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8473 : : get_identifier ("y"),
8474 : : integer_type_node);
8475 : 4 : DECL_CONTEXT (y) = child_fndecl;
8476 : :
8477 : : /* "p" global. */
8478 : 4 : tree p = build_global_decl ("p", ptr_type_node);
8479 : :
8480 : : /* "q" global. */
8481 : 4 : tree q = build_global_decl ("q", ptr_type_node);
8482 : :
8483 : 4 : region_model_manager mgr;
8484 : 4 : test_region_model_context ctxt;
8485 : 4 : region_model model (&mgr);
8486 : :
8487 : : /* Push stack frame for "parent_fn". */
8488 : 4 : const region *parent_frame_reg
8489 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (parent_fndecl),
8490 : : nullptr, nullptr, &ctxt);
8491 : 4 : ASSERT_EQ (model.get_current_frame (), parent_frame_reg);
8492 : 4 : ASSERT_TRUE (model.region_exists_p (parent_frame_reg));
8493 : 4 : const region *a_in_parent_reg = model.get_lvalue (a, &ctxt);
8494 : 4 : model.set_value (a_in_parent_reg,
8495 : : model.get_rvalue (int_42, &ctxt),
8496 : : &ctxt);
8497 : 4 : ASSERT_EQ (a_in_parent_reg->maybe_get_frame_region (), parent_frame_reg);
8498 : :
8499 : 4 : model.add_constraint (b, LT_EXPR, int_10, &ctxt);
8500 : 4 : ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
8501 : : tristate (tristate::TS_TRUE));
8502 : :
8503 : : /* Push stack frame for "child_fn". */
8504 : 4 : const region *child_frame_reg
8505 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (child_fndecl),
8506 : : nullptr, nullptr, &ctxt);
8507 : 4 : ASSERT_EQ (model.get_current_frame (), child_frame_reg);
8508 : 4 : ASSERT_TRUE (model.region_exists_p (child_frame_reg));
8509 : 4 : const region *x_in_child_reg = model.get_lvalue (x, &ctxt);
8510 : 4 : model.set_value (x_in_child_reg,
8511 : : model.get_rvalue (int_0, &ctxt),
8512 : : &ctxt);
8513 : 4 : ASSERT_EQ (x_in_child_reg->maybe_get_frame_region (), child_frame_reg);
8514 : :
8515 : 4 : model.add_constraint (y, NE_EXPR, int_5, &ctxt);
8516 : 4 : ASSERT_EQ (model.eval_condition (y, NE_EXPR, int_5, &ctxt),
8517 : : tristate (tristate::TS_TRUE));
8518 : :
8519 : : /* Point a global pointer at a local in the child frame: p = &x. */
8520 : 4 : const region *p_in_globals_reg = model.get_lvalue (p, &ctxt);
8521 : 4 : model.set_value (p_in_globals_reg,
8522 : : mgr.get_ptr_svalue (ptr_type_node, x_in_child_reg),
8523 : : &ctxt);
8524 : 4 : ASSERT_EQ (p_in_globals_reg->maybe_get_frame_region (), nullptr);
8525 : :
8526 : : /* Point another global pointer at p: q = &p. */
8527 : 4 : const region *q_in_globals_reg = model.get_lvalue (q, &ctxt);
8528 : 4 : model.set_value (q_in_globals_reg,
8529 : : mgr.get_ptr_svalue (ptr_type_node, p_in_globals_reg),
8530 : : &ctxt);
8531 : :
8532 : : /* Test region::descendent_of_p. */
8533 : 4 : ASSERT_TRUE (child_frame_reg->descendent_of_p (child_frame_reg));
8534 : 4 : ASSERT_TRUE (x_in_child_reg->descendent_of_p (child_frame_reg));
8535 : 4 : ASSERT_FALSE (a_in_parent_reg->descendent_of_p (child_frame_reg));
8536 : :
8537 : : /* Pop the "child_fn" frame from the stack. */
8538 : 4 : model.pop_frame (nullptr, nullptr, &ctxt, nullptr);
8539 : 4 : ASSERT_FALSE (model.region_exists_p (child_frame_reg));
8540 : 4 : ASSERT_TRUE (model.region_exists_p (parent_frame_reg));
8541 : :
8542 : : /* Verify that p (which was pointing at the local "x" in the popped
8543 : : frame) has been poisoned. */
8544 : 4 : const svalue *new_p_sval = model.get_rvalue (p, nullptr);
8545 : 4 : ASSERT_EQ (new_p_sval->get_kind (), SK_POISONED);
8546 : 4 : ASSERT_EQ (new_p_sval->dyn_cast_poisoned_svalue ()->get_poison_kind (),
8547 : : poison_kind::popped_stack);
8548 : :
8549 : : /* Verify that q still points to p, in spite of the region
8550 : : renumbering. */
8551 : 4 : const svalue *new_q_sval = model.get_rvalue (q, &ctxt);
8552 : 4 : ASSERT_EQ (new_q_sval->get_kind (), SK_REGION);
8553 : 4 : ASSERT_EQ (new_q_sval->maybe_get_region (),
8554 : : model.get_lvalue (p, &ctxt));
8555 : :
8556 : : /* Verify that top of stack has been updated. */
8557 : 4 : ASSERT_EQ (model.get_current_frame (), parent_frame_reg);
8558 : :
8559 : : /* Verify locals in parent frame. */
8560 : : /* Verify "a" still has its value. */
8561 : 4 : const svalue *new_a_sval = model.get_rvalue (a, &ctxt);
8562 : 4 : ASSERT_EQ (new_a_sval->get_kind (), SK_CONSTANT);
8563 : 4 : ASSERT_EQ (new_a_sval->dyn_cast_constant_svalue ()->get_constant (),
8564 : : int_42);
8565 : : /* Verify "b" still has its constraint. */
8566 : 4 : ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt),
8567 : : tristate (tristate::TS_TRUE));
8568 : 4 : }
8569 : :
8570 : : /* Verify that get_representative_path_var works as expected, that
8571 : : we can map from regions to parms and back within a recursive call
8572 : : stack. */
8573 : :
8574 : : static void
8575 : 4 : test_get_representative_path_var ()
8576 : : {
8577 : 4 : auto_vec <tree> param_types;
8578 : 4 : tree fndecl = make_fndecl (integer_type_node,
8579 : : "factorial",
8580 : : param_types);
8581 : 4 : allocate_struct_function (fndecl, true);
8582 : :
8583 : : /* Parm "n". */
8584 : 4 : tree n = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8585 : : get_identifier ("n"),
8586 : : integer_type_node);
8587 : 4 : DECL_CONTEXT (n) = fndecl;
8588 : :
8589 : 4 : region_model_manager mgr;
8590 : 4 : test_region_model_context ctxt;
8591 : 4 : region_model model (&mgr);
8592 : :
8593 : : /* Push 5 stack frames for "factorial", each with a param */
8594 : 4 : auto_vec<const region *> parm_regs;
8595 : 4 : auto_vec<const svalue *> parm_svals;
8596 : 24 : for (int depth = 0; depth < 5; depth++)
8597 : : {
8598 : 20 : const region *frame_n_reg
8599 : 20 : = model.push_frame (*DECL_STRUCT_FUNCTION (fndecl),
8600 : : nullptr, nullptr, &ctxt);
8601 : 20 : const region *parm_n_reg = model.get_lvalue (path_var (n, depth), &ctxt);
8602 : 20 : parm_regs.safe_push (parm_n_reg);
8603 : :
8604 : 20 : ASSERT_EQ (parm_n_reg->get_parent_region (), frame_n_reg);
8605 : 20 : const svalue *sval_n = mgr.get_or_create_initial_value (parm_n_reg);
8606 : 20 : parm_svals.safe_push (sval_n);
8607 : : }
8608 : :
8609 : : /* Verify that we can recognize that the regions are the parms,
8610 : : at every depth. */
8611 : 24 : for (int depth = 0; depth < 5; depth++)
8612 : : {
8613 : 20 : {
8614 : 20 : svalue_set visited;
8615 : 40 : ASSERT_EQ (model.get_representative_path_var (parm_regs[depth],
8616 : : &visited,
8617 : : nullptr),
8618 : : path_var (n, depth + 1));
8619 : 20 : }
8620 : : /* ...and that we can lookup lvalues for locals for all frames,
8621 : : not just the top. */
8622 : 20 : ASSERT_EQ (model.get_lvalue (path_var (n, depth), nullptr),
8623 : : parm_regs[depth]);
8624 : : /* ...and that we can locate the svalues. */
8625 : 20 : {
8626 : 20 : svalue_set visited;
8627 : 40 : ASSERT_EQ (model.get_representative_path_var (parm_svals[depth],
8628 : : &visited,
8629 : : nullptr),
8630 : : path_var (n, depth + 1));
8631 : 20 : }
8632 : : }
8633 : 4 : }
8634 : :
8635 : : /* Ensure that region_model::operator== works as expected. */
8636 : :
8637 : : static void
8638 : 4 : test_equality_1 ()
8639 : : {
8640 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8641 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
8642 : :
8643 : : /* Verify that "empty" region_model instances are equal to each other. */
8644 : 4 : region_model_manager mgr;
8645 : 4 : region_model model0 (&mgr);
8646 : 4 : region_model model1 (&mgr);
8647 : 4 : ASSERT_EQ (model0, model1);
8648 : :
8649 : : /* Verify that setting state in model1 makes the models non-equal. */
8650 : 4 : tree x = build_global_decl ("x", integer_type_node);
8651 : 4 : model0.set_value (x, int_42, nullptr);
8652 : 4 : ASSERT_EQ (model0.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8653 : 4 : ASSERT_NE (model0, model1);
8654 : :
8655 : : /* Verify the copy-ctor. */
8656 : 4 : region_model model2 (model0);
8657 : 4 : ASSERT_EQ (model0, model2);
8658 : 4 : ASSERT_EQ (model2.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8659 : 4 : ASSERT_NE (model1, model2);
8660 : :
8661 : : /* Verify that models obtained from copy-ctor are independently editable
8662 : : w/o affecting the original model. */
8663 : 4 : model2.set_value (x, int_17, nullptr);
8664 : 4 : ASSERT_NE (model0, model2);
8665 : 4 : ASSERT_EQ (model2.get_rvalue (x, nullptr)->maybe_get_constant (), int_17);
8666 : 4 : ASSERT_EQ (model0.get_rvalue (x, nullptr)->maybe_get_constant (), int_42);
8667 : 4 : }
8668 : :
8669 : : /* Verify that region models for
8670 : : x = 42; y = 113;
8671 : : and
8672 : : y = 113; x = 42;
8673 : : are equal. */
8674 : :
8675 : : static void
8676 : 4 : test_canonicalization_2 ()
8677 : : {
8678 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8679 : 4 : tree int_113 = build_int_cst (integer_type_node, 113);
8680 : 4 : tree x = build_global_decl ("x", integer_type_node);
8681 : 4 : tree y = build_global_decl ("y", integer_type_node);
8682 : :
8683 : 4 : region_model_manager mgr;
8684 : 4 : region_model model0 (&mgr);
8685 : 4 : model0.set_value (model0.get_lvalue (x, nullptr),
8686 : : model0.get_rvalue (int_42, nullptr),
8687 : : nullptr);
8688 : 4 : model0.set_value (model0.get_lvalue (y, nullptr),
8689 : : model0.get_rvalue (int_113, nullptr),
8690 : : nullptr);
8691 : :
8692 : 4 : region_model model1 (&mgr);
8693 : 4 : model1.set_value (model1.get_lvalue (y, nullptr),
8694 : : model1.get_rvalue (int_113, nullptr),
8695 : : nullptr);
8696 : 4 : model1.set_value (model1.get_lvalue (x, nullptr),
8697 : : model1.get_rvalue (int_42, nullptr),
8698 : : nullptr);
8699 : :
8700 : 4 : ASSERT_EQ (model0, model1);
8701 : 4 : }
8702 : :
8703 : : /* Verify that constraints for
8704 : : x > 3 && y > 42
8705 : : and
8706 : : y > 42 && x > 3
8707 : : are equal after canonicalization. */
8708 : :
8709 : : static void
8710 : 4 : test_canonicalization_3 ()
8711 : : {
8712 : 4 : tree int_3 = build_int_cst (integer_type_node, 3);
8713 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8714 : 4 : tree x = build_global_decl ("x", integer_type_node);
8715 : 4 : tree y = build_global_decl ("y", integer_type_node);
8716 : :
8717 : 4 : region_model_manager mgr;
8718 : 4 : region_model model0 (&mgr);
8719 : 4 : model0.add_constraint (x, GT_EXPR, int_3, nullptr);
8720 : 4 : model0.add_constraint (y, GT_EXPR, int_42, nullptr);
8721 : :
8722 : 4 : region_model model1 (&mgr);
8723 : 4 : model1.add_constraint (y, GT_EXPR, int_42, nullptr);
8724 : 4 : model1.add_constraint (x, GT_EXPR, int_3, nullptr);
8725 : :
8726 : 4 : model0.canonicalize ();
8727 : 4 : model1.canonicalize ();
8728 : 4 : ASSERT_EQ (model0, model1);
8729 : 4 : }
8730 : :
8731 : : /* Verify that we can canonicalize a model containing NaN and other real
8732 : : constants. */
8733 : :
8734 : : static void
8735 : 4 : test_canonicalization_4 ()
8736 : : {
8737 : 4 : auto_vec<tree> csts;
8738 : 4 : append_interesting_constants (&csts);
8739 : :
8740 : 4 : region_model_manager mgr;
8741 : 4 : region_model model (&mgr);
8742 : :
8743 : 60 : for (tree cst : csts)
8744 : 48 : model.get_rvalue (cst, nullptr);
8745 : :
8746 : 4 : model.canonicalize ();
8747 : 4 : }
8748 : :
8749 : : /* Assert that if we have two region_model instances
8750 : : with values VAL_A and VAL_B for EXPR that they are
8751 : : mergable. Write the merged model to *OUT_MERGED_MODEL,
8752 : : and the merged svalue ptr to *OUT_MERGED_SVALUE.
8753 : : If VAL_A or VAL_B are nullptr_TREE, don't populate EXPR
8754 : : for that region_model. */
8755 : :
8756 : : static void
8757 : 20 : assert_region_models_merge (tree expr, tree val_a, tree val_b,
8758 : : region_model *out_merged_model,
8759 : : const svalue **out_merged_svalue)
8760 : : {
8761 : 20 : region_model_manager *mgr = out_merged_model->get_manager ();
8762 : 20 : program_point point (program_point::origin (*mgr));
8763 : 20 : test_region_model_context ctxt;
8764 : 20 : region_model model0 (mgr);
8765 : 20 : region_model model1 (mgr);
8766 : 20 : if (val_a)
8767 : 16 : model0.set_value (model0.get_lvalue (expr, &ctxt),
8768 : : model0.get_rvalue (val_a, &ctxt),
8769 : : &ctxt);
8770 : 20 : if (val_b)
8771 : 16 : model1.set_value (model1.get_lvalue (expr, &ctxt),
8772 : : model1.get_rvalue (val_b, &ctxt),
8773 : : &ctxt);
8774 : :
8775 : : /* They should be mergeable. */
8776 : 20 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, out_merged_model));
8777 : 20 : *out_merged_svalue = out_merged_model->get_rvalue (expr, &ctxt);
8778 : 20 : }
8779 : :
8780 : : /* Verify that we can merge region_model instances. */
8781 : :
8782 : : static void
8783 : 4 : test_state_merging ()
8784 : : {
8785 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
8786 : 4 : tree int_113 = build_int_cst (integer_type_node, 113);
8787 : 4 : tree x = build_global_decl ("x", integer_type_node);
8788 : 4 : tree y = build_global_decl ("y", integer_type_node);
8789 : 4 : tree z = build_global_decl ("z", integer_type_node);
8790 : 4 : tree p = build_global_decl ("p", ptr_type_node);
8791 : :
8792 : 4 : tree addr_of_y = build1 (ADDR_EXPR, ptr_type_node, y);
8793 : 4 : tree addr_of_z = build1 (ADDR_EXPR, ptr_type_node, z);
8794 : :
8795 : 4 : auto_vec <tree> param_types;
8796 : 4 : tree test_fndecl = make_fndecl (integer_type_node, "test_fn", param_types);
8797 : 4 : allocate_struct_function (test_fndecl, true);
8798 : :
8799 : : /* Param "a". */
8800 : 4 : tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8801 : : get_identifier ("a"),
8802 : : integer_type_node);
8803 : 4 : DECL_CONTEXT (a) = test_fndecl;
8804 : 4 : tree addr_of_a = build1 (ADDR_EXPR, ptr_type_node, a);
8805 : :
8806 : : /* Param "q", a pointer. */
8807 : 4 : tree q = build_decl (UNKNOWN_LOCATION, PARM_DECL,
8808 : : get_identifier ("q"),
8809 : : ptr_type_node);
8810 : 4 : DECL_CONTEXT (q) = test_fndecl;
8811 : :
8812 : 4 : region_model_manager mgr;
8813 : 4 : program_point point (program_point::origin (mgr));
8814 : :
8815 : 4 : {
8816 : 4 : region_model model0 (&mgr);
8817 : 4 : region_model model1 (&mgr);
8818 : 4 : region_model merged (&mgr);
8819 : : /* Verify empty models can be merged. */
8820 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8821 : 4 : ASSERT_EQ (model0, merged);
8822 : 4 : }
8823 : :
8824 : : /* Verify that we can merge two contradictory constraints on the
8825 : : value for a global. */
8826 : : /* TODO: verify that the merged model doesn't have a value for
8827 : : the global */
8828 : 4 : {
8829 : 4 : region_model model0 (&mgr);
8830 : 4 : region_model model1 (&mgr);
8831 : 4 : region_model merged (&mgr);
8832 : 4 : test_region_model_context ctxt;
8833 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
8834 : 4 : model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
8835 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8836 : 4 : ASSERT_NE (model0, merged);
8837 : 4 : ASSERT_NE (model1, merged);
8838 : 4 : }
8839 : :
8840 : : /* Verify handling of a PARM_DECL. */
8841 : 4 : {
8842 : 4 : test_region_model_context ctxt;
8843 : 4 : region_model model0 (&mgr);
8844 : 4 : region_model model1 (&mgr);
8845 : 4 : ASSERT_EQ (model0.get_stack_depth (), 0);
8846 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8847 : : nullptr, nullptr, &ctxt);
8848 : 4 : ASSERT_EQ (model0.get_stack_depth (), 1);
8849 : 4 : model1.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8850 : : nullptr, nullptr, &ctxt);
8851 : :
8852 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
8853 : 4 : integer_type_node, "test sval");
8854 : 4 : model0.set_value (model0.get_lvalue (a, &ctxt), &test_sval, &ctxt);
8855 : 4 : model1.set_value (model1.get_lvalue (a, &ctxt), &test_sval, &ctxt);
8856 : 4 : ASSERT_EQ (model0, model1);
8857 : :
8858 : : /* They should be mergeable, and the result should be the same. */
8859 : 4 : region_model merged (&mgr);
8860 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8861 : 4 : ASSERT_EQ (model0, merged);
8862 : : /* In particular, "a" should have the placeholder value. */
8863 : 4 : ASSERT_EQ (merged.get_rvalue (a, &ctxt), &test_sval);
8864 : 4 : }
8865 : :
8866 : : /* Verify handling of a global. */
8867 : 4 : {
8868 : 4 : test_region_model_context ctxt;
8869 : 4 : region_model model0 (&mgr);
8870 : 4 : region_model model1 (&mgr);
8871 : :
8872 : 4 : placeholder_svalue test_sval (mgr.alloc_symbol_id (),
8873 : 4 : integer_type_node, "test sval");
8874 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt), &test_sval, &ctxt);
8875 : 4 : model1.set_value (model1.get_lvalue (x, &ctxt), &test_sval, &ctxt);
8876 : 4 : ASSERT_EQ (model0, model1);
8877 : :
8878 : : /* They should be mergeable, and the result should be the same. */
8879 : 4 : region_model merged (&mgr);
8880 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8881 : 4 : ASSERT_EQ (model0, merged);
8882 : : /* In particular, "x" should have the placeholder value. */
8883 : 4 : ASSERT_EQ (merged.get_rvalue (x, &ctxt), &test_sval);
8884 : 4 : }
8885 : :
8886 : : /* Use global-handling to verify various combinations of values. */
8887 : :
8888 : : /* Two equal constant values. */
8889 : 4 : {
8890 : 4 : region_model merged (&mgr);
8891 : 4 : const svalue *merged_x_sval;
8892 : 4 : assert_region_models_merge (x, int_42, int_42, &merged, &merged_x_sval);
8893 : :
8894 : : /* In particular, there should be a constant value for "x". */
8895 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_CONSTANT);
8896 : 4 : ASSERT_EQ (merged_x_sval->dyn_cast_constant_svalue ()->get_constant (),
8897 : : int_42);
8898 : 4 : }
8899 : :
8900 : : /* Two non-equal constant values. */
8901 : 4 : {
8902 : 4 : region_model merged (&mgr);
8903 : 4 : const svalue *merged_x_sval;
8904 : 4 : assert_region_models_merge (x, int_42, int_113, &merged, &merged_x_sval);
8905 : :
8906 : : /* In particular, there should be a "widening" value for "x". */
8907 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_WIDENING);
8908 : 4 : }
8909 : :
8910 : : /* Initial and constant. */
8911 : 4 : {
8912 : 4 : region_model merged (&mgr);
8913 : 4 : const svalue *merged_x_sval;
8914 : 4 : assert_region_models_merge (x, NULL_TREE, int_113, &merged, &merged_x_sval);
8915 : :
8916 : : /* In particular, there should be an unknown value for "x". */
8917 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
8918 : 4 : }
8919 : :
8920 : : /* Constant and initial. */
8921 : 4 : {
8922 : 4 : region_model merged (&mgr);
8923 : 4 : const svalue *merged_x_sval;
8924 : 4 : assert_region_models_merge (x, int_42, NULL_TREE, &merged, &merged_x_sval);
8925 : :
8926 : : /* In particular, there should be an unknown value for "x". */
8927 : 4 : ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN);
8928 : 4 : }
8929 : :
8930 : : /* Unknown and constant. */
8931 : : // TODO
8932 : :
8933 : : /* Pointers: NULL and NULL. */
8934 : : // TODO
8935 : :
8936 : : /* Pointers: NULL and non-NULL. */
8937 : : // TODO
8938 : :
8939 : : /* Pointers: non-NULL and non-NULL: ptr to a local. */
8940 : 4 : {
8941 : 4 : region_model model0 (&mgr);
8942 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
8943 : : nullptr, nullptr, nullptr);
8944 : 4 : model0.set_value (model0.get_lvalue (p, nullptr),
8945 : : model0.get_rvalue (addr_of_a, nullptr), nullptr);
8946 : :
8947 : 4 : region_model model1 (model0);
8948 : 4 : ASSERT_EQ (model0, model1);
8949 : :
8950 : : /* They should be mergeable, and the result should be the same. */
8951 : 4 : region_model merged (&mgr);
8952 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
8953 : 4 : ASSERT_EQ (model0, merged);
8954 : 4 : }
8955 : :
8956 : : /* Pointers: non-NULL and non-NULL: ptr to a global. */
8957 : 4 : {
8958 : 4 : region_model merged (&mgr);
8959 : : /* p == &y in both input models. */
8960 : 4 : const svalue *merged_p_sval;
8961 : 4 : assert_region_models_merge (p, addr_of_y, addr_of_y, &merged,
8962 : : &merged_p_sval);
8963 : :
8964 : : /* We should get p == &y in the merged model. */
8965 : 4 : ASSERT_EQ (merged_p_sval->get_kind (), SK_REGION);
8966 : 4 : const region_svalue *merged_p_ptr
8967 : 4 : = merged_p_sval->dyn_cast_region_svalue ();
8968 : 4 : const region *merged_p_star_reg = merged_p_ptr->get_pointee ();
8969 : 4 : ASSERT_EQ (merged_p_star_reg, merged.get_lvalue (y, nullptr));
8970 : 4 : }
8971 : :
8972 : : /* Pointers: non-NULL ptrs to different globals should not merge;
8973 : : see e.g. gcc.dg/analyzer/torture/uninit-pr108725.c */
8974 : 4 : {
8975 : 4 : region_model merged_model (&mgr);
8976 : 4 : program_point point (program_point::origin (mgr));
8977 : 4 : test_region_model_context ctxt;
8978 : : /* x == &y vs x == &z in the input models; these are actually casts
8979 : : of the ptrs to "int". */
8980 : 4 : region_model model0 (&mgr);
8981 : 4 : region_model model1 (&mgr);
8982 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt),
8983 : : model0.get_rvalue (addr_of_y, &ctxt),
8984 : : &ctxt);
8985 : 4 : model1.set_value (model1.get_lvalue (x, &ctxt),
8986 : : model1.get_rvalue (addr_of_z, &ctxt),
8987 : : &ctxt);
8988 : : /* They should not be mergeable. */
8989 : 4 : ASSERT_FALSE (model0.can_merge_with_p (model1, point, &merged_model));
8990 : 4 : }
8991 : :
8992 : : /* Pointers: non-NULL and non-NULL: ptr to a heap region. */
8993 : 4 : {
8994 : 4 : test_region_model_context ctxt;
8995 : 4 : region_model model0 (&mgr);
8996 : 4 : tree size = build_int_cst (size_type_node, 1024);
8997 : 4 : const svalue *size_sval = mgr.get_or_create_constant_svalue (size);
8998 : 4 : const region *new_reg
8999 : 4 : = model0.get_or_create_region_for_heap_alloc (size_sval, &ctxt);
9000 : 4 : const svalue *ptr_sval = mgr.get_ptr_svalue (ptr_type_node, new_reg);
9001 : 4 : model0.set_value (model0.get_lvalue (p, &ctxt),
9002 : : ptr_sval, &ctxt);
9003 : :
9004 : 4 : region_model model1 (model0);
9005 : :
9006 : 4 : ASSERT_EQ (model0, model1);
9007 : :
9008 : 4 : region_model merged (&mgr);
9009 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9010 : :
9011 : : /* The merged model ought to be identical. */
9012 : 4 : ASSERT_EQ (model0, merged);
9013 : 4 : }
9014 : :
9015 : : /* Two regions sharing the same placeholder svalue should continue sharing
9016 : : it after self-merger. */
9017 : 4 : {
9018 : 4 : test_region_model_context ctxt;
9019 : 4 : region_model model0 (&mgr);
9020 : 4 : placeholder_svalue placeholder_sval (mgr.alloc_symbol_id (),
9021 : 4 : integer_type_node, "test");
9022 : 4 : model0.set_value (model0.get_lvalue (x, &ctxt),
9023 : : &placeholder_sval, &ctxt);
9024 : 4 : model0.set_value (model0.get_lvalue (y, &ctxt), &placeholder_sval, &ctxt);
9025 : 4 : region_model model1 (model0);
9026 : :
9027 : : /* They should be mergeable, and the result should be the same. */
9028 : 4 : region_model merged (&mgr);
9029 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9030 : 4 : ASSERT_EQ (model0, merged);
9031 : :
9032 : : /* In particular, we should have x == y. */
9033 : 4 : ASSERT_EQ (merged.eval_condition (x, EQ_EXPR, y, &ctxt),
9034 : : tristate (tristate::TS_TRUE));
9035 : 4 : }
9036 : :
9037 : 4 : {
9038 : 4 : region_model model0 (&mgr);
9039 : 4 : region_model model1 (&mgr);
9040 : 4 : test_region_model_context ctxt;
9041 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
9042 : 4 : model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
9043 : 4 : region_model merged (&mgr);
9044 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9045 : 4 : }
9046 : :
9047 : 4 : {
9048 : 4 : region_model model0 (&mgr);
9049 : 4 : region_model model1 (&mgr);
9050 : 4 : test_region_model_context ctxt;
9051 : 4 : model0.add_constraint (x, EQ_EXPR, int_42, &ctxt);
9052 : 4 : model1.add_constraint (x, NE_EXPR, int_42, &ctxt);
9053 : 4 : model1.add_constraint (x, EQ_EXPR, int_113, &ctxt);
9054 : 4 : region_model merged (&mgr);
9055 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9056 : 4 : }
9057 : :
9058 : : // TODO: what can't we merge? need at least one such test
9059 : :
9060 : : /* TODO: various things
9061 : : - heap regions
9062 : : - value merging:
9063 : : - every combination, but in particular
9064 : : - pairs of regions
9065 : : */
9066 : :
9067 : : /* Views. */
9068 : 4 : {
9069 : 4 : test_region_model_context ctxt;
9070 : 4 : region_model model0 (&mgr);
9071 : :
9072 : 4 : const region *x_reg = model0.get_lvalue (x, &ctxt);
9073 : 4 : const region *x_as_ptr = mgr.get_cast_region (x_reg, ptr_type_node);
9074 : 4 : model0.set_value (x_as_ptr, model0.get_rvalue (addr_of_y, &ctxt), &ctxt);
9075 : :
9076 : 4 : region_model model1 (model0);
9077 : 4 : ASSERT_EQ (model1, model0);
9078 : :
9079 : : /* They should be mergeable, and the result should be the same. */
9080 : 4 : region_model merged (&mgr);
9081 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9082 : 4 : }
9083 : :
9084 : : /* Verify that we can merge a model in which a local in an older stack
9085 : : frame points to a local in a more recent stack frame. */
9086 : 4 : {
9087 : 4 : region_model model0 (&mgr);
9088 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9089 : : nullptr, nullptr, nullptr);
9090 : 4 : const region *q_in_first_frame = model0.get_lvalue (q, nullptr);
9091 : :
9092 : : /* Push a second frame. */
9093 : 4 : const region *reg_2nd_frame
9094 : 4 : = model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9095 : : nullptr, nullptr, nullptr);
9096 : :
9097 : : /* Have a pointer in the older frame point to a local in the
9098 : : more recent frame. */
9099 : 4 : const svalue *sval_ptr = model0.get_rvalue (addr_of_a, nullptr);
9100 : 4 : model0.set_value (q_in_first_frame, sval_ptr, nullptr);
9101 : :
9102 : : /* Verify that it's pointing at the newer frame. */
9103 : 4 : const region *reg_pointee = sval_ptr->maybe_get_region ();
9104 : 4 : ASSERT_EQ (reg_pointee->get_parent_region (), reg_2nd_frame);
9105 : :
9106 : 4 : model0.canonicalize ();
9107 : :
9108 : 4 : region_model model1 (model0);
9109 : 4 : ASSERT_EQ (model0, model1);
9110 : :
9111 : : /* They should be mergeable, and the result should be the same
9112 : : (after canonicalization, at least). */
9113 : 4 : region_model merged (&mgr);
9114 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9115 : 4 : merged.canonicalize ();
9116 : 4 : ASSERT_EQ (model0, merged);
9117 : 4 : }
9118 : :
9119 : : /* Verify that we can merge a model in which a local points to a global. */
9120 : 4 : {
9121 : 4 : region_model model0 (&mgr);
9122 : 4 : model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl),
9123 : : nullptr, nullptr, nullptr);
9124 : 4 : model0.set_value (model0.get_lvalue (q, nullptr),
9125 : : model0.get_rvalue (addr_of_y, nullptr), nullptr);
9126 : :
9127 : 4 : region_model model1 (model0);
9128 : 4 : ASSERT_EQ (model0, model1);
9129 : :
9130 : : /* They should be mergeable, and the result should be the same
9131 : : (after canonicalization, at least). */
9132 : 4 : region_model merged (&mgr);
9133 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9134 : 4 : ASSERT_EQ (model0, merged);
9135 : 4 : }
9136 : 4 : }
9137 : :
9138 : : /* Verify that constraints are correctly merged when merging region_model
9139 : : instances. */
9140 : :
9141 : : static void
9142 : 4 : test_constraint_merging ()
9143 : : {
9144 : 4 : tree int_0 = integer_zero_node;
9145 : 4 : tree int_5 = build_int_cst (integer_type_node, 5);
9146 : 4 : tree x = build_global_decl ("x", integer_type_node);
9147 : 4 : tree y = build_global_decl ("y", integer_type_node);
9148 : 4 : tree z = build_global_decl ("z", integer_type_node);
9149 : 4 : tree n = build_global_decl ("n", integer_type_node);
9150 : :
9151 : 4 : region_model_manager mgr;
9152 : 4 : test_region_model_context ctxt;
9153 : :
9154 : : /* model0: 0 <= (x == y) < n. */
9155 : 4 : region_model model0 (&mgr);
9156 : 4 : model0.add_constraint (x, EQ_EXPR, y, &ctxt);
9157 : 4 : model0.add_constraint (x, GE_EXPR, int_0, nullptr);
9158 : 4 : model0.add_constraint (x, LT_EXPR, n, nullptr);
9159 : :
9160 : : /* model1: z != 5 && (0 <= x < n). */
9161 : 4 : region_model model1 (&mgr);
9162 : 4 : model1.add_constraint (z, NE_EXPR, int_5, nullptr);
9163 : 4 : model1.add_constraint (x, GE_EXPR, int_0, nullptr);
9164 : 4 : model1.add_constraint (x, LT_EXPR, n, nullptr);
9165 : :
9166 : : /* They should be mergeable; the merged constraints should
9167 : : be: (0 <= x < n). */
9168 : 4 : program_point point (program_point::origin (mgr));
9169 : 4 : region_model merged (&mgr);
9170 : 4 : ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged));
9171 : :
9172 : 4 : ASSERT_EQ (merged.eval_condition (x, GE_EXPR, int_0, &ctxt),
9173 : : tristate (tristate::TS_TRUE));
9174 : 4 : ASSERT_EQ (merged.eval_condition (x, LT_EXPR, n, &ctxt),
9175 : : tristate (tristate::TS_TRUE));
9176 : :
9177 : 4 : ASSERT_EQ (merged.eval_condition (z, NE_EXPR, int_5, &ctxt),
9178 : : tristate (tristate::TS_UNKNOWN));
9179 : 4 : ASSERT_EQ (merged.eval_condition (x, LT_EXPR, y, &ctxt),
9180 : : tristate (tristate::TS_UNKNOWN));
9181 : 4 : }
9182 : :
9183 : : /* Verify that widening_svalue::eval_condition_without_cm works as
9184 : : expected. */
9185 : :
9186 : : static void
9187 : 4 : test_widening_constraints ()
9188 : : {
9189 : 4 : region_model_manager mgr;
9190 : 4 : const supernode *snode = nullptr;
9191 : 4 : tree int_0 = integer_zero_node;
9192 : 4 : tree int_m1 = build_int_cst (integer_type_node, -1);
9193 : 4 : tree int_1 = integer_one_node;
9194 : 4 : tree int_256 = build_int_cst (integer_type_node, 256);
9195 : 4 : test_region_model_context ctxt;
9196 : 4 : const svalue *int_0_sval = mgr.get_or_create_constant_svalue (int_0);
9197 : 4 : const svalue *int_1_sval = mgr.get_or_create_constant_svalue (int_1);
9198 : 4 : const svalue *w_zero_then_one_sval
9199 : 4 : = mgr.get_or_create_widening_svalue (integer_type_node, snode,
9200 : : int_0_sval, int_1_sval);
9201 : 4 : const widening_svalue *w_zero_then_one
9202 : 4 : = w_zero_then_one_sval->dyn_cast_widening_svalue ();
9203 : 4 : ASSERT_EQ (w_zero_then_one->get_direction (),
9204 : : widening_svalue::DIR_ASCENDING);
9205 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_m1),
9206 : : tristate::TS_FALSE);
9207 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_0),
9208 : : tristate::TS_FALSE);
9209 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_1),
9210 : : tristate::TS_UNKNOWN);
9211 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_256),
9212 : : tristate::TS_UNKNOWN);
9213 : :
9214 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_m1),
9215 : : tristate::TS_FALSE);
9216 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_0),
9217 : : tristate::TS_UNKNOWN);
9218 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_1),
9219 : : tristate::TS_UNKNOWN);
9220 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_256),
9221 : : tristate::TS_UNKNOWN);
9222 : :
9223 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_m1),
9224 : : tristate::TS_TRUE);
9225 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_0),
9226 : : tristate::TS_UNKNOWN);
9227 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_1),
9228 : : tristate::TS_UNKNOWN);
9229 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_256),
9230 : : tristate::TS_UNKNOWN);
9231 : :
9232 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_m1),
9233 : : tristate::TS_TRUE);
9234 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_0),
9235 : : tristate::TS_TRUE);
9236 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_1),
9237 : : tristate::TS_UNKNOWN);
9238 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_256),
9239 : : tristate::TS_UNKNOWN);
9240 : :
9241 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_m1),
9242 : : tristate::TS_FALSE);
9243 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_0),
9244 : : tristate::TS_UNKNOWN);
9245 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_1),
9246 : : tristate::TS_UNKNOWN);
9247 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_256),
9248 : : tristate::TS_UNKNOWN);
9249 : :
9250 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_m1),
9251 : : tristate::TS_TRUE);
9252 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_0),
9253 : : tristate::TS_UNKNOWN);
9254 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_1),
9255 : : tristate::TS_UNKNOWN);
9256 : 4 : ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_256),
9257 : : tristate::TS_UNKNOWN);
9258 : 4 : }
9259 : :
9260 : : /* Verify merging constraints for states simulating successive iterations
9261 : : of a loop.
9262 : : Simulate:
9263 : : for (i = 0; i < 256; i++)
9264 : : [...body...]
9265 : : i.e. this gimple:.
9266 : : i_15 = 0;
9267 : : goto <bb 4>;
9268 : :
9269 : : <bb 4> :
9270 : : i_11 = PHI <i_15(2), i_23(3)>
9271 : : if (i_11 <= 255)
9272 : : goto <bb 3>;
9273 : : else
9274 : : goto [AFTER LOOP]
9275 : :
9276 : : <bb 3> :
9277 : : [LOOP BODY]
9278 : : i_23 = i_11 + 1;
9279 : :
9280 : : and thus these ops (and resultant states):
9281 : : i_11 = PHI()
9282 : : {i_11: 0}
9283 : : add_constraint (i_11 <= 255) [for the true edge]
9284 : : {i_11: 0} [constraint was a no-op]
9285 : : i_23 = i_11 + 1;
9286 : : {i_22: 1}
9287 : : i_11 = PHI()
9288 : : {i_11: WIDENED (at phi, 0, 1)}
9289 : : add_constraint (i_11 <= 255) [for the true edge]
9290 : : {i_11: WIDENED (at phi, 0, 1); WIDENED <= 255}
9291 : : i_23 = i_11 + 1;
9292 : : {i_23: (WIDENED (at phi, 0, 1) + 1); WIDENED <= 255}
9293 : : i_11 = PHI(); merge with state at phi above
9294 : : {i_11: WIDENED (at phi, 0, 1); WIDENED <= 256}
9295 : : [changing meaning of "WIDENED" here]
9296 : : if (i_11 <= 255)
9297 : : T: {i_11: WIDENED (at phi, 0, 1); WIDENED <= 255}; cache hit
9298 : : F: {i_11: 256}
9299 : : */
9300 : :
9301 : : static void
9302 : 4 : test_iteration_1 ()
9303 : : {
9304 : 4 : region_model_manager mgr;
9305 : 4 : program_point point (program_point::origin (mgr));
9306 : :
9307 : 4 : tree int_0 = integer_zero_node;
9308 : 4 : tree int_1 = integer_one_node;
9309 : 4 : tree int_256 = build_int_cst (integer_type_node, 256);
9310 : 4 : tree i = build_global_decl ("i", integer_type_node);
9311 : :
9312 : 4 : test_region_model_context ctxt;
9313 : :
9314 : : /* model0: i: 0. */
9315 : 4 : region_model model0 (&mgr);
9316 : 4 : model0.set_value (i, int_0, &ctxt);
9317 : :
9318 : : /* model1: i: 1. */
9319 : 4 : region_model model1 (&mgr);
9320 : 4 : model1.set_value (i, int_1, &ctxt);
9321 : :
9322 : : /* Should merge "i" to a widened value. */
9323 : 4 : region_model model2 (&mgr);
9324 : 4 : ASSERT_TRUE (model1.can_merge_with_p (model0, point, &model2));
9325 : 4 : const svalue *merged_i = model2.get_rvalue (i, &ctxt);
9326 : 4 : ASSERT_EQ (merged_i->get_kind (), SK_WIDENING);
9327 : 4 : const widening_svalue *w = merged_i->dyn_cast_widening_svalue ();
9328 : 4 : ASSERT_EQ (w->get_direction (), widening_svalue::DIR_ASCENDING);
9329 : :
9330 : : /* Add constraint: i < 256 */
9331 : 4 : model2.add_constraint (i, LT_EXPR, int_256, &ctxt);
9332 : 4 : ASSERT_EQ (model2.eval_condition (i, LT_EXPR, int_256, &ctxt),
9333 : : tristate (tristate::TS_TRUE));
9334 : 4 : ASSERT_EQ (model2.eval_condition (i, GE_EXPR, int_0, &ctxt),
9335 : : tristate (tristate::TS_TRUE));
9336 : :
9337 : : /* Try merging with the initial state. */
9338 : 4 : region_model model3 (&mgr);
9339 : 4 : ASSERT_TRUE (model2.can_merge_with_p (model0, point, &model3));
9340 : : /* Merging the merged value with the initial value should be idempotent,
9341 : : so that the analysis converges. */
9342 : 4 : ASSERT_EQ (model3.get_rvalue (i, &ctxt), merged_i);
9343 : : /* Merger of 0 and a widening value with constraint < CST
9344 : : should retain the constraint, even though it was implicit
9345 : : for the 0 case. */
9346 : 4 : ASSERT_EQ (model3.eval_condition (i, LT_EXPR, int_256, &ctxt),
9347 : : tristate (tristate::TS_TRUE));
9348 : : /* ...and we should have equality: the analysis should have converged. */
9349 : 4 : ASSERT_EQ (model3, model2);
9350 : :
9351 : : /* "i_23 = i_11 + 1;" */
9352 : 4 : region_model model4 (model3);
9353 : 4 : ASSERT_EQ (model4, model2);
9354 : 4 : model4.set_value (i, build2 (PLUS_EXPR, integer_type_node, i, int_1), &ctxt);
9355 : 4 : const svalue *plus_one = model4.get_rvalue (i, &ctxt);
9356 : 4 : ASSERT_EQ (plus_one->get_kind (), SK_BINOP);
9357 : :
9358 : : /* Try merging with the "i: 1" state. */
9359 : 4 : region_model model5 (&mgr);
9360 : 4 : ASSERT_TRUE (model4.can_merge_with_p (model1, point, &model5));
9361 : 4 : ASSERT_EQ (model5.get_rvalue (i, &ctxt), plus_one);
9362 : 4 : ASSERT_EQ (model5, model4);
9363 : :
9364 : : /* "i_11 = PHI();" merge with state at phi above.
9365 : : For i, we should have a merger of WIDENING with WIDENING + 1,
9366 : : and this should be WIDENING again. */
9367 : 4 : region_model model6 (&mgr);
9368 : 4 : ASSERT_TRUE (model5.can_merge_with_p (model2, point, &model6));
9369 : 4 : const svalue *merged_widening = model6.get_rvalue (i, &ctxt);
9370 : 4 : ASSERT_EQ (merged_widening->get_kind (), SK_WIDENING);
9371 : 4 : }
9372 : :
9373 : : /* Verify that if we mark a pointer to a malloc-ed region as non-NULL,
9374 : : all cast pointers to that region are also known to be non-NULL. */
9375 : :
9376 : : static void
9377 : 4 : test_malloc_constraints ()
9378 : : {
9379 : 4 : region_model_manager mgr;
9380 : 4 : region_model model (&mgr);
9381 : 4 : tree p = build_global_decl ("p", ptr_type_node);
9382 : 4 : tree char_star = build_pointer_type (char_type_node);
9383 : 4 : tree q = build_global_decl ("q", char_star);
9384 : 4 : tree null_ptr = build_int_cst (ptr_type_node, 0);
9385 : :
9386 : 4 : const svalue *size_in_bytes
9387 : 4 : = mgr.get_or_create_unknown_svalue (size_type_node);
9388 : 4 : const region *reg
9389 : 4 : = model.get_or_create_region_for_heap_alloc (size_in_bytes, nullptr);
9390 : 4 : const svalue *sval = mgr.get_ptr_svalue (ptr_type_node, reg);
9391 : 4 : model.set_value (model.get_lvalue (p, nullptr), sval, nullptr);
9392 : 4 : model.set_value (q, p, nullptr);
9393 : :
9394 : 4 : ASSERT_CONDITION_UNKNOWN (model, p, NE_EXPR, null_ptr);
9395 : 4 : ASSERT_CONDITION_UNKNOWN (model, p, EQ_EXPR, null_ptr);
9396 : 4 : ASSERT_CONDITION_UNKNOWN (model, q, NE_EXPR, null_ptr);
9397 : 4 : ASSERT_CONDITION_UNKNOWN (model, q, EQ_EXPR, null_ptr);
9398 : :
9399 : 4 : model.add_constraint (p, NE_EXPR, null_ptr, nullptr);
9400 : :
9401 : 4 : ASSERT_CONDITION_TRUE (model, p, NE_EXPR, null_ptr);
9402 : 4 : ASSERT_CONDITION_FALSE (model, p, EQ_EXPR, null_ptr);
9403 : 4 : ASSERT_CONDITION_TRUE (model, q, NE_EXPR, null_ptr);
9404 : 4 : ASSERT_CONDITION_FALSE (model, q, EQ_EXPR, null_ptr);
9405 : 4 : }
9406 : :
9407 : : /* Smoketest of getting and setting the value of a variable. */
9408 : :
9409 : : static void
9410 : 4 : test_var ()
9411 : : {
9412 : : /* "int i;" */
9413 : 4 : tree i = build_global_decl ("i", integer_type_node);
9414 : :
9415 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9416 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
9417 : :
9418 : 4 : region_model_manager mgr;
9419 : 4 : region_model model (&mgr);
9420 : :
9421 : 4 : const region *i_reg = model.get_lvalue (i, nullptr);
9422 : 4 : ASSERT_EQ (i_reg->get_kind (), RK_DECL);
9423 : :
9424 : : /* Reading "i" should give a symbolic "initial value". */
9425 : 4 : const svalue *sval_init = model.get_rvalue (i, nullptr);
9426 : 4 : ASSERT_EQ (sval_init->get_kind (), SK_INITIAL);
9427 : 4 : ASSERT_EQ (sval_init->dyn_cast_initial_svalue ()->get_region (), i_reg);
9428 : : /* ..and doing it again should give the same "initial value". */
9429 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr), sval_init);
9430 : :
9431 : : /* "i = 17;". */
9432 : 4 : model.set_value (i, int_17, nullptr);
9433 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr),
9434 : : model.get_rvalue (int_17, nullptr));
9435 : :
9436 : : /* "i = -3;". */
9437 : 4 : model.set_value (i, int_m3, nullptr);
9438 : 4 : ASSERT_EQ (model.get_rvalue (i, nullptr),
9439 : : model.get_rvalue (int_m3, nullptr));
9440 : :
9441 : : /* Verify get_offset for "i". */
9442 : 4 : {
9443 : 4 : region_offset offset = i_reg->get_offset (&mgr);
9444 : 4 : ASSERT_EQ (offset.get_base_region (), i_reg);
9445 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
9446 : : }
9447 : 4 : }
9448 : :
9449 : : static void
9450 : 4 : test_array_2 ()
9451 : : {
9452 : : /* "int arr[10];" */
9453 : 4 : tree tlen = size_int (10);
9454 : 4 : tree arr_type
9455 : 4 : = build_array_type (integer_type_node, build_index_type (tlen));
9456 : 4 : tree arr = build_global_decl ("arr", arr_type);
9457 : :
9458 : : /* "int i;" */
9459 : 4 : tree i = build_global_decl ("i", integer_type_node);
9460 : :
9461 : 4 : tree int_0 = integer_zero_node;
9462 : 4 : tree int_1 = integer_one_node;
9463 : :
9464 : 4 : tree arr_0 = build4 (ARRAY_REF, integer_type_node,
9465 : : arr, int_0, NULL_TREE, NULL_TREE);
9466 : 4 : tree arr_1 = build4 (ARRAY_REF, integer_type_node,
9467 : : arr, int_1, NULL_TREE, NULL_TREE);
9468 : 4 : tree arr_i = build4 (ARRAY_REF, integer_type_node,
9469 : : arr, i, NULL_TREE, NULL_TREE);
9470 : :
9471 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9472 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
9473 : 4 : tree int_m3 = build_int_cst (integer_type_node, -3);
9474 : :
9475 : 4 : region_model_manager mgr;
9476 : 4 : region_model model (&mgr);
9477 : : /* "arr[0] = 17;". */
9478 : 4 : model.set_value (arr_0, int_17, nullptr);
9479 : : /* "arr[1] = -3;". */
9480 : 4 : model.set_value (arr_1, int_m3, nullptr);
9481 : :
9482 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr),
9483 : : model.get_rvalue (int_17, nullptr));
9484 : 4 : ASSERT_EQ (model.get_rvalue (arr_1, nullptr),
9485 : : model.get_rvalue (int_m3, nullptr));
9486 : :
9487 : : /* Overwrite a pre-existing binding: "arr[1] = 42;". */
9488 : 4 : model.set_value (arr_1, int_42, nullptr);
9489 : 4 : ASSERT_EQ (model.get_rvalue (arr_1, nullptr),
9490 : : model.get_rvalue (int_42, nullptr));
9491 : :
9492 : : /* Verify get_offset for "arr[0]". */
9493 : 4 : {
9494 : 4 : const region *arr_0_reg = model.get_lvalue (arr_0, nullptr);
9495 : 4 : region_offset offset = arr_0_reg->get_offset (&mgr);
9496 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9497 : 4 : ASSERT_EQ (offset.get_bit_offset (), 0);
9498 : : }
9499 : :
9500 : : /* Verify get_offset for "arr[1]". */
9501 : 4 : {
9502 : 4 : const region *arr_1_reg = model.get_lvalue (arr_1, nullptr);
9503 : 4 : region_offset offset = arr_1_reg->get_offset (&mgr);
9504 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9505 : 4 : ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE);
9506 : : }
9507 : :
9508 : : /* Verify get_offset for "arr[i]". */
9509 : 4 : {
9510 : 4 : const region *arr_i_reg = model.get_lvalue (arr_i, nullptr);
9511 : 4 : region_offset offset = arr_i_reg->get_offset (&mgr);
9512 : 4 : ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, nullptr));
9513 : 4 : const svalue *offset_sval = offset.get_symbolic_byte_offset ();
9514 : 4 : if (const svalue *cast = offset_sval->maybe_undo_cast ())
9515 : 4 : offset_sval = cast;
9516 : 4 : ASSERT_EQ (offset_sval->get_kind (), SK_BINOP);
9517 : : }
9518 : :
9519 : : /* "arr[i] = i;" - this should remove the earlier bindings. */
9520 : 4 : model.set_value (arr_i, i, nullptr);
9521 : 4 : ASSERT_EQ (model.get_rvalue (arr_i, nullptr), model.get_rvalue (i, nullptr));
9522 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr)->get_kind (), SK_UNKNOWN);
9523 : :
9524 : : /* "arr[0] = 17;" - this should remove the arr[i] binding. */
9525 : 4 : model.set_value (arr_0, int_17, nullptr);
9526 : 4 : ASSERT_EQ (model.get_rvalue (arr_0, nullptr),
9527 : : model.get_rvalue (int_17, nullptr));
9528 : 4 : ASSERT_EQ (model.get_rvalue (arr_i, nullptr)->get_kind (), SK_UNKNOWN);
9529 : 4 : }
9530 : :
9531 : : /* Smoketest of dereferencing a pointer via MEM_REF. */
9532 : :
9533 : : static void
9534 : 4 : test_mem_ref ()
9535 : : {
9536 : : /*
9537 : : x = 17;
9538 : : p = &x;
9539 : : *p;
9540 : : */
9541 : 4 : tree x = build_global_decl ("x", integer_type_node);
9542 : 4 : tree int_star = build_pointer_type (integer_type_node);
9543 : 4 : tree p = build_global_decl ("p", int_star);
9544 : :
9545 : 4 : tree int_17 = build_int_cst (integer_type_node, 17);
9546 : 4 : tree addr_of_x = build1 (ADDR_EXPR, int_star, x);
9547 : 4 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
9548 : 4 : tree offset_0 = build_int_cst (ptype, 0);
9549 : 4 : tree star_p = build2 (MEM_REF, integer_type_node, p, offset_0);
9550 : :
9551 : 4 : region_model_manager mgr;
9552 : 4 : region_model model (&mgr);
9553 : :
9554 : : /* "x = 17;". */
9555 : 4 : model.set_value (x, int_17, nullptr);
9556 : :
9557 : : /* "p = &x;". */
9558 : 4 : model.set_value (p, addr_of_x, nullptr);
9559 : :
9560 : 4 : const svalue *sval = model.get_rvalue (star_p, nullptr);
9561 : 4 : ASSERT_EQ (sval->maybe_get_constant (), int_17);
9562 : 4 : }
9563 : :
9564 : : /* Test for a POINTER_PLUS_EXPR followed by a MEM_REF.
9565 : : Analogous to this code:
9566 : : void test_6 (int a[10])
9567 : : {
9568 : : __analyzer_eval (a[3] == 42); [should be UNKNOWN]
9569 : : a[3] = 42;
9570 : : __analyzer_eval (a[3] == 42); [should be TRUE]
9571 : : }
9572 : : from data-model-1.c, which looks like this at the gimple level:
9573 : : # __analyzer_eval (a[3] == 42); [should be UNKNOWN]
9574 : : int *_1 = a_10(D) + 12; # POINTER_PLUS_EXPR
9575 : : int _2 = *_1; # MEM_REF
9576 : : _Bool _3 = _2 == 42;
9577 : : int _4 = (int) _3;
9578 : : __analyzer_eval (_4);
9579 : :
9580 : : # a[3] = 42;
9581 : : int *_5 = a_10(D) + 12; # POINTER_PLUS_EXPR
9582 : : *_5 = 42; # MEM_REF
9583 : :
9584 : : # __analyzer_eval (a[3] == 42); [should be TRUE]
9585 : : int *_6 = a_10(D) + 12; # POINTER_PLUS_EXPR
9586 : : int _7 = *_6; # MEM_REF
9587 : : _Bool _8 = _7 == 42;
9588 : : int _9 = (int) _8;
9589 : : __analyzer_eval (_9); */
9590 : :
9591 : : static void
9592 : 4 : test_POINTER_PLUS_EXPR_then_MEM_REF ()
9593 : : {
9594 : 4 : tree int_star = build_pointer_type (integer_type_node);
9595 : 4 : tree a = build_global_decl ("a", int_star);
9596 : 4 : tree offset_12 = build_int_cst (size_type_node, 12);
9597 : 4 : tree pointer_plus_expr = build2 (POINTER_PLUS_EXPR, int_star, a, offset_12);
9598 : 4 : tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true);
9599 : 4 : tree offset_0 = build_int_cst (ptype, 0);
9600 : 4 : tree mem_ref = build2 (MEM_REF, integer_type_node,
9601 : : pointer_plus_expr, offset_0);
9602 : 4 : region_model_manager mgr;
9603 : 4 : region_model m (&mgr);
9604 : :
9605 : 4 : tree int_42 = build_int_cst (integer_type_node, 42);
9606 : 4 : m.set_value (mem_ref, int_42, nullptr);
9607 : 4 : ASSERT_EQ (m.get_rvalue (mem_ref, nullptr)->maybe_get_constant (), int_42);
9608 : 4 : }
9609 : :
9610 : : /* Verify that malloc works. */
9611 : :
9612 : : static void
9613 : 4 : test_malloc ()
9614 : : {
9615 : 4 : tree int_star = build_pointer_type (integer_type_node);
9616 : 4 : tree p = build_global_decl ("p", int_star);
9617 : 4 : tree n = build_global_decl ("n", integer_type_node);
9618 : 4 : tree n_times_4 = build2 (MULT_EXPR, size_type_node,
9619 : : n, build_int_cst (size_type_node, 4));
9620 : :
9621 : 4 : region_model_manager mgr;
9622 : 4 : test_region_model_context ctxt;
9623 : 4 : region_model model (&mgr);
9624 : :
9625 : : /* "p = malloc (n * 4);". */
9626 : 4 : const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt);
9627 : 4 : const region *reg
9628 : 4 : = model.get_or_create_region_for_heap_alloc (size_sval, &ctxt);
9629 : 4 : const svalue *ptr = mgr.get_ptr_svalue (int_star, reg);
9630 : 4 : model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt);
9631 : 4 : ASSERT_EQ (model.get_capacity (reg), size_sval);
9632 : 4 : }
9633 : :
9634 : : /* Verify that alloca works. */
9635 : :
9636 : : static void
9637 : 4 : test_alloca ()
9638 : : {
9639 : 4 : auto_vec <tree> param_types;
9640 : 4 : tree fndecl = make_fndecl (integer_type_node,
9641 : : "test_fn",
9642 : : param_types);
9643 : 4 : allocate_struct_function (fndecl, true);
9644 : :
9645 : :
9646 : 4 : tree int_star = build_pointer_type (integer_type_node);
9647 : 4 : tree p = build_global_decl ("p", int_star);
9648 : 4 : tree n = build_global_decl ("n", integer_type_node);
9649 : 4 : tree n_times_4 = build2 (MULT_EXPR, size_type_node,
9650 : : n, build_int_cst (size_type_node, 4));
9651 : :
9652 : 4 : region_model_manager mgr;
9653 : 4 : test_region_model_context ctxt;
9654 : 4 : region_model model (&mgr);
9655 : :
9656 : : /* Push stack frame. */
9657 : 4 : const region *frame_reg
9658 : 4 : = model.push_frame (*DECL_STRUCT_FUNCTION (fndecl),
9659 : : nullptr, nullptr, &ctxt);
9660 : : /* "p = alloca (n * 4);". */
9661 : 4 : const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt);
9662 : 4 : const region *reg = model.create_region_for_alloca (size_sval, &ctxt);
9663 : 4 : ASSERT_EQ (reg->get_parent_region (), frame_reg);
9664 : 4 : const svalue *ptr = mgr.get_ptr_svalue (int_star, reg);
9665 : 4 : model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt);
9666 : 4 : ASSERT_EQ (model.get_capacity (reg), size_sval);
9667 : :
9668 : : /* Verify that the pointers to the alloca region are replaced by
9669 : : poisoned values when the frame is popped. */
9670 : 4 : model.pop_frame (nullptr, nullptr, &ctxt, nullptr);
9671 : 4 : ASSERT_EQ (model.get_rvalue (p, nullptr)->get_kind (), SK_POISONED);
9672 : 4 : }
9673 : :
9674 : : /* Verify that svalue::involves_p works. */
9675 : :
9676 : : static void
9677 : 4 : test_involves_p ()
9678 : : {
9679 : 4 : region_model_manager mgr;
9680 : 4 : tree int_star = build_pointer_type (integer_type_node);
9681 : 4 : tree p = build_global_decl ("p", int_star);
9682 : 4 : tree q = build_global_decl ("q", int_star);
9683 : :
9684 : 4 : test_region_model_context ctxt;
9685 : 4 : region_model model (&mgr);
9686 : 4 : const svalue *p_init = model.get_rvalue (p, &ctxt);
9687 : 4 : const svalue *q_init = model.get_rvalue (q, &ctxt);
9688 : :
9689 : 4 : ASSERT_TRUE (p_init->involves_p (p_init));
9690 : 4 : ASSERT_FALSE (p_init->involves_p (q_init));
9691 : :
9692 : 4 : const region *star_p_reg = mgr.get_symbolic_region (p_init);
9693 : 4 : const region *star_q_reg = mgr.get_symbolic_region (q_init);
9694 : :
9695 : 4 : const svalue *init_star_p = mgr.get_or_create_initial_value (star_p_reg);
9696 : 4 : const svalue *init_star_q = mgr.get_or_create_initial_value (star_q_reg);
9697 : :
9698 : 4 : ASSERT_TRUE (init_star_p->involves_p (p_init));
9699 : 4 : ASSERT_FALSE (p_init->involves_p (init_star_p));
9700 : 4 : ASSERT_FALSE (init_star_p->involves_p (q_init));
9701 : 4 : ASSERT_TRUE (init_star_q->involves_p (q_init));
9702 : 4 : ASSERT_FALSE (init_star_q->involves_p (p_init));
9703 : 4 : }
9704 : :
9705 : : /* Run all of the selftests within this file. */
9706 : :
9707 : : void
9708 : 4 : analyzer_region_model_cc_tests ()
9709 : : {
9710 : 4 : test_tree_cmp_on_constants ();
9711 : 4 : test_dump ();
9712 : 4 : test_struct ();
9713 : 4 : test_array_1 ();
9714 : 4 : test_get_representative_tree ();
9715 : 4 : test_unique_constants ();
9716 : 4 : test_unique_unknowns ();
9717 : 4 : test_initial_svalue_folding ();
9718 : 4 : test_unaryop_svalue_folding ();
9719 : 4 : test_binop_svalue_folding ();
9720 : 4 : test_sub_svalue_folding ();
9721 : 4 : test_bits_within_svalue_folding ();
9722 : 4 : test_descendent_of_p ();
9723 : 4 : test_bit_range_regions ();
9724 : 4 : test_assignment ();
9725 : 4 : test_compound_assignment ();
9726 : 4 : test_stack_frames ();
9727 : 4 : test_get_representative_path_var ();
9728 : 4 : test_equality_1 ();
9729 : 4 : test_canonicalization_2 ();
9730 : 4 : test_canonicalization_3 ();
9731 : 4 : test_canonicalization_4 ();
9732 : 4 : test_state_merging ();
9733 : 4 : test_constraint_merging ();
9734 : 4 : test_widening_constraints ();
9735 : 4 : test_iteration_1 ();
9736 : 4 : test_malloc_constraints ();
9737 : 4 : test_var ();
9738 : 4 : test_array_2 ();
9739 : 4 : test_mem_ref ();
9740 : 4 : test_POINTER_PLUS_EXPR_then_MEM_REF ();
9741 : 4 : test_malloc ();
9742 : 4 : test_alloca ();
9743 : 4 : test_involves_p ();
9744 : 4 : }
9745 : :
9746 : : } // namespace selftest
9747 : :
9748 : : #endif /* CHECKING_P */
9749 : :
9750 : : } // namespace ana
9751 : :
9752 : : #endif /* #if ENABLE_ANALYZER */
|