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