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