Line data Source code
1 : /* Classes for modeling the state of memory.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_ANALYZER_REGION_MODEL_H
22 : #define GCC_ANALYZER_REGION_MODEL_H
23 :
24 : /* Implementation of the region-based ternary model described in:
25 : "A Memory Model for Static Analysis of C Programs"
26 : (Zhongxing Xu, Ted Kremenek, and Jian Zhang)
27 : http://lcs.ios.ac.cn/~xuzb/canalyze/memmodel.pdf */
28 :
29 : #include "bitmap.h"
30 : #include "stringpool.h"
31 : #include "attribs.h" // for rdwr_map
32 : #include "selftest.h"
33 : #include "analyzer/svalue.h"
34 : #include "analyzer/region.h"
35 : #include "analyzer/known-function-manager.h"
36 : #include "analyzer/region-model-manager.h"
37 : #include "analyzer/pending-diagnostic.h"
38 : #include "analyzer/diagnostic-manager.h"
39 : #include "text-art/widget.h"
40 : #include "text-art/dump.h"
41 :
42 : using namespace ana;
43 :
44 : namespace inchash
45 : {
46 : extern void add_path_var (path_var pv, hash &hstate);
47 : } // namespace inchash
48 :
49 : namespace ana {
50 :
51 : template <typename T>
52 785401 : class one_way_id_map
53 : {
54 : public:
55 : one_way_id_map (int num_ids);
56 : void put (T src, T dst);
57 : T get_dst_for_src (T src) const;
58 : void dump_to_pp (pretty_printer *pp) const;
59 : void dump () const;
60 : void update (T *) const;
61 :
62 : private:
63 : auto_vec<T> m_src_to_dst;
64 : };
65 :
66 : /* class one_way_id_map. */
67 :
68 : /* one_way_id_map's ctor, which populates the map with dummy null values. */
69 :
70 : template <typename T>
71 785401 : inline one_way_id_map<T>::one_way_id_map (int num_svalues)
72 785401 : : m_src_to_dst (num_svalues)
73 : {
74 3175516 : for (int i = 0; i < num_svalues; i++)
75 2390115 : m_src_to_dst.quick_push (T::null ());
76 785401 : }
77 :
78 : /* Record that SRC is to be mapped to DST. */
79 :
80 : template <typename T>
81 : inline void
82 2362687 : one_way_id_map<T>::put (T src, T dst)
83 : {
84 2362687 : m_src_to_dst[src.as_int ()] = dst;
85 : }
86 :
87 : /* Get the new value for SRC within the map. */
88 :
89 : template <typename T>
90 : inline T
91 3187994 : one_way_id_map<T>::get_dst_for_src (T src) const
92 : {
93 3187994 : if (src.null_p ())
94 0 : return src;
95 3187994 : return m_src_to_dst[src.as_int ()];
96 : }
97 :
98 : /* Dump this map to PP. */
99 :
100 : template <typename T>
101 : inline void
102 0 : one_way_id_map<T>::dump_to_pp (pretty_printer *pp) const
103 : {
104 0 : pp_string (pp, "src to dst: {");
105 : unsigned i;
106 : T *dst;
107 0 : FOR_EACH_VEC_ELT (m_src_to_dst, i, dst)
108 : {
109 0 : if (i > 0)
110 0 : pp_string (pp, ", ");
111 0 : T src (T::from_int (i));
112 0 : src.print (pp);
113 0 : pp_string (pp, " -> ");
114 0 : dst->print (pp);
115 : }
116 0 : pp_string (pp, "}");
117 0 : pp_newline (pp);
118 0 : }
119 :
120 : /* Dump this map to stderr. */
121 :
122 : template <typename T>
123 : DEBUG_FUNCTION inline void
124 0 : one_way_id_map<T>::dump () const
125 : {
126 0 : pretty_printer pp;
127 0 : pp.set_output_stream (stderr);
128 0 : dump_to_pp (&pp);
129 0 : pp_flush (&pp);
130 0 : }
131 :
132 : /* Update *ID from the old value to its new value in this map. */
133 :
134 : template <typename T>
135 : inline void
136 3187994 : one_way_id_map<T>::update (T *id) const
137 : {
138 1598949 : *id = get_dst_for_src (*id);
139 : }
140 :
141 : /* A mapping from region to svalue for use when tracking state. */
142 :
143 2932952 : class region_to_value_map
144 : {
145 : public:
146 : typedef hash_map<const region *, const svalue *> hash_map_t;
147 : typedef hash_map_t::iterator iterator;
148 :
149 197982 : region_to_value_map () : m_hash_map () {}
150 2734970 : region_to_value_map (const region_to_value_map &other)
151 2734970 : : m_hash_map (other.m_hash_map) {}
152 : region_to_value_map &operator= (const region_to_value_map &other);
153 :
154 : bool operator== (const region_to_value_map &other) const;
155 427938 : bool operator!= (const region_to_value_map &other) const
156 : {
157 427938 : return !(*this == other);
158 : }
159 :
160 538956 : iterator begin () const { return m_hash_map.begin (); }
161 538956 : iterator end () const { return m_hash_map.end (); }
162 :
163 241402 : const svalue * const *get (const region *reg) const
164 : {
165 241402 : return const_cast <hash_map_t &> (m_hash_map).get (reg);
166 : }
167 22174 : void put (const region *reg, const svalue *sval)
168 : {
169 22174 : m_hash_map.put (reg, sval);
170 : }
171 49761 : void remove (const region *reg)
172 : {
173 49761 : m_hash_map.remove (reg);
174 : }
175 :
176 720 : bool is_empty () const { return m_hash_map.is_empty (); }
177 :
178 : void dump_to_logger (logger &) const;
179 :
180 : std::unique_ptr<json::object> to_json () const;
181 :
182 : std::unique_ptr<text_art::tree_widget>
183 : make_dump_widget (symbolic::node_dump_state &nds) const;
184 :
185 : bool can_merge_with_p (const region_to_value_map &other,
186 : region_to_value_map *out) const;
187 :
188 : void purge_state_involving (const symbolic::node &node);
189 :
190 : private:
191 : hash_map_t m_hash_map;
192 : };
193 :
194 : /* Various operations delete information from a region_model.
195 :
196 : This struct tracks how many of each kind of entity were purged (e.g.
197 : for selftests, and for debugging). */
198 :
199 : struct purge_stats
200 : {
201 : purge_stats ()
202 : : m_num_svalues (0),
203 : m_num_regions (0),
204 : m_num_equiv_classes (0),
205 : m_num_constraints (0),
206 : m_num_bounded_ranges_constraints (0),
207 : m_num_client_items (0)
208 : {}
209 :
210 : int m_num_svalues;
211 : int m_num_regions;
212 : int m_num_equiv_classes;
213 : int m_num_constraints;
214 : int m_num_bounded_ranges_constraints;
215 : int m_num_client_items;
216 : };
217 :
218 : /* A base class for visiting regions and svalues, with do-nothing
219 : base implementations of the per-subclass vfuncs. */
220 :
221 11571 : class visitor
222 : {
223 : public:
224 0 : virtual void visit_pointer_svalue (const pointer_svalue *) {}
225 0 : virtual void visit_constant_svalue (const constant_svalue *) {}
226 3399 : virtual void visit_unknown_svalue (const unknown_svalue *) {}
227 0 : virtual void visit_poisoned_svalue (const poisoned_svalue *) {}
228 0 : virtual void visit_setjmp_svalue (const setjmp_svalue *) {}
229 0 : virtual void visit_initial_svalue (const initial_svalue *) {}
230 488 : virtual void visit_unaryop_svalue (const unaryop_svalue *) {}
231 943 : virtual void visit_binop_svalue (const binop_svalue *) {}
232 397 : virtual void visit_svalue_from_bit_vector (const svalue_from_bit_vector *) {}
233 0 : virtual void visit_unmergeable_svalue (const unmergeable_svalue *) {}
234 11 : virtual void visit_placeholder_svalue (const placeholder_svalue *) {}
235 24 : virtual void visit_widening_svalue (const widening_svalue *) {}
236 0 : virtual void visit_asm_output_svalue (const asm_output_svalue *) {}
237 0 : virtual void visit_const_fn_result_svalue (const const_fn_result_svalue *) {}
238 :
239 5133 : virtual void visit_region (const region *) {}
240 : };
241 :
242 : struct append_regions_cb_data;
243 :
244 : // TODO: should this be a new kind of symbolic::node ?
245 :
246 : /* Roughly equivalent to a struct __cxa_exception, except we store a std::vector
247 : rather than a linked list. */
248 :
249 : struct exception_node
250 : {
251 5883 : exception_node (const svalue *exception_sval,
252 : const svalue *typeinfo_sval,
253 : const svalue *destructor_sval)
254 5883 : : m_exception_sval (exception_sval),
255 5883 : m_typeinfo_sval (typeinfo_sval),
256 5883 : m_destructor_sval (destructor_sval)
257 : {
258 : }
259 :
260 : bool operator== (const exception_node &other) const;
261 :
262 : void dump_to_logger (logger &) const;
263 : void dump () const;
264 :
265 : std::unique_ptr<json::object> to_json () const;
266 :
267 : std::unique_ptr<text_art::tree_widget>
268 : make_dump_widget (symbolic::node_dump_state &nds) const;
269 :
270 : tree maybe_get_type () const;
271 :
272 : void add_to_reachable_regions (reachable_regions &) const;
273 :
274 : const svalue *m_exception_sval;
275 : const svalue *m_typeinfo_sval;
276 : const svalue *m_destructor_sval;
277 : };
278 :
279 : /* A region_model encapsulates a representation of the state of memory, with
280 : a tree of regions, along with their associated values.
281 : The representation is graph-like because values can be pointers to
282 : regions.
283 : It also stores:
284 : - a constraint_manager, capturing relationships between the values, and
285 : - dynamic extents, mapping dynamically-allocated regions to svalues (their
286 : capacities). */
287 :
288 : class region_model
289 : {
290 : public:
291 : typedef region_to_value_map dynamic_extents_t;
292 :
293 : region_model (symbolic::node_manager &node_mgr);
294 : region_model (const region_model &other);
295 : ~region_model ();
296 : region_model &operator= (const region_model &other);
297 :
298 : bool operator== (const region_model &other) const;
299 28 : bool operator!= (const region_model &other) const
300 : {
301 28 : return !(*this == other);
302 : }
303 :
304 : hashval_t hash () const;
305 :
306 : void print (pretty_printer *pp) const;
307 :
308 : void dump_to_logger (logger &) const;
309 : void dump_to_pp (pretty_printer *pp) const;
310 : void dump () const;
311 :
312 : std::unique_ptr<json::object> to_json () const;
313 :
314 : std::unique_ptr<text_art::tree_widget>
315 : make_dump_widget (symbolic::node_dump_state &nds) const;
316 :
317 : void validate () const;
318 :
319 : void canonicalize ();
320 : bool canonicalized_p () const;
321 :
322 : void
323 : on_stmt_pre (const gimple *stmt,
324 : bool *out_unknown_side_effects,
325 : region_model_context *ctxt);
326 :
327 : void on_assignment (const gassign *stmt, region_model_context *ctxt);
328 : const svalue *get_gassign_result (const gassign *assign,
329 : region_model_context *ctxt);
330 : void on_asm_stmt (const gasm *asm_stmt, region_model_context *ctxt);
331 : bool on_call_pre (const gcall &stmt, region_model_context *ctxt);
332 : void on_call_post (const gcall &stmt,
333 : bool unknown_side_effects,
334 : region_model_context *ctxt);
335 :
336 : void purge_state_involving (const symbolic::node &node,
337 : region_model_context *ctxt);
338 :
339 : void impl_deallocation_call (const call_details &cd);
340 :
341 : const svalue *maybe_get_copy_bounds (const region *src_reg,
342 : const svalue *num_bytes_sval);
343 : void update_for_int_cst_return (const call_details &cd,
344 : int retval,
345 : bool unmergeable);
346 : void update_for_zero_return (const call_details &cd,
347 : bool unmergeable);
348 : void update_for_null_return (const call_details &cd,
349 : bool unmergeable);
350 : void update_for_nonzero_return (const call_details &cd);
351 :
352 : void handle_unrecognized_call (const gcall &call,
353 : region_model_context *ctxt);
354 : void get_reachable_svalues (value_set *out,
355 : const svalue *extra_sval,
356 : const uncertainty_t *uncertainty);
357 :
358 : void on_return (const greturn *stmt, region_model_context *ctxt);
359 : void on_setjmp (const gcall &stmt,
360 : const exploded_node &enode,
361 : const superedge &sedge,
362 : region_model_context *ctxt);
363 : void on_longjmp (const gcall &longjmp_call, const gcall &setjmp_call,
364 : int setjmp_stack_depth, region_model_context *ctxt);
365 :
366 : void update_for_gcall (const gcall &call_stmt,
367 : region_model_context *ctxt,
368 : function *callee = nullptr);
369 :
370 : void update_for_return_gcall (const gcall &call_stmt,
371 : region_model_context *ctxt);
372 :
373 : const region *push_frame (const function &fun,
374 : const gcall *call_stmt,
375 : const vec<const svalue *> *arg_sids,
376 : region_model_context *ctxt);
377 11467512 : const frame_region *get_current_frame () const { return m_current_frame; }
378 : const function *get_current_function () const;
379 : void pop_frame (tree result_lvalue,
380 : const svalue **out_result,
381 : region_model_context *ctxt,
382 : const gcall *call_stmt,
383 : bool eval_return_svalue = true);
384 : int get_stack_depth () const;
385 : const frame_region *get_frame_at_index (int index) const;
386 :
387 : const region *get_lvalue (path_var pv, region_model_context *ctxt) const;
388 : const region *get_lvalue (tree expr, region_model_context *ctxt) const;
389 : const svalue *get_rvalue (path_var pv, region_model_context *ctxt) const;
390 : const svalue *get_rvalue (tree expr, region_model_context *ctxt) const;
391 :
392 : const region *deref_rvalue (const svalue *ptr_sval, tree ptr_tree,
393 : region_model_context *ctxt,
394 : bool add_nonnull_constraint = true) const;
395 :
396 : const svalue *get_rvalue_for_bits (tree type,
397 : const region *reg,
398 : const bit_range &bits,
399 : region_model_context *ctxt) const;
400 :
401 : void set_value (const region *lhs_reg, const svalue *rhs_sval,
402 : region_model_context *ctxt);
403 : void set_value (tree lhs, tree rhs, region_model_context *ctxt);
404 : void clobber_region (const region *reg);
405 : void purge_region (const region *reg);
406 : void zero_fill_region (const region *reg,
407 : region_model_context *ctxt);
408 :
409 : void
410 : write_bytes (symbolic::pointer dest_sym_ptr,
411 : const symbolic::bit_vector &bytes,
412 : region_model_context *ctxt);
413 :
414 : const symbolic::bit_vector &
415 : read_bytes (symbolic::pointer src_sym_ptr,
416 : tree src_ptr_expr,
417 : const symbolic_byte_size &num_bytes,
418 : region_model_context *ctxt) const;
419 :
420 : void
421 : copy_bytes (symbolic::pointer dest_sym_ptr,
422 : symbolic::pointer src_sym_ptr,
423 : tree src_ptr_expr,
424 : const symbolic_byte_size &num_bytes,
425 : region_model_context *ctxt);
426 :
427 : void
428 : mark_region_as_unknown (const region *reg,
429 : uncertainty_t *uncertainty);
430 : void
431 : mark_region_as_conjured (const region &base_reg,
432 : const gimple &stmt,
433 : region_model_context *ctxt);
434 :
435 : tristate eval_condition (const svalue *lhs,
436 : enum tree_code op,
437 : const svalue *rhs) const;
438 : tristate compare_initial_and_pointer (const initial_svalue *init,
439 : const symbolic::pointer &ptr) const;
440 : tristate symbolic_greater_than (const binop_svalue *a,
441 : const svalue *b) const;
442 : tristate structural_equality (const svalue *a, const svalue *b) const;
443 : tristate eval_condition (tree lhs,
444 : enum tree_code op,
445 : tree rhs,
446 : region_model_context *ctxt) const;
447 : bool add_constraint (tree lhs, enum tree_code op, tree rhs,
448 : region_model_context *ctxt);
449 : bool add_constraint (tree lhs, enum tree_code op, tree rhs,
450 : region_model_context *ctxt,
451 : std::unique_ptr<rejected_constraint> *out);
452 :
453 : const region *
454 : get_or_create_region_for_heap_alloc (const svalue *size_in_bytes,
455 : region_model_context *ctxt,
456 : bool update_state_machine = false,
457 : const call_details *cd = nullptr);
458 :
459 : const region *create_region_for_alloca (const svalue *size_in_bytes,
460 : region_model_context *ctxt);
461 : void get_referenced_base_regions (auto_bitmap &out_ids) const;
462 :
463 : tree get_representative_tree (const svalue *sval,
464 : logger *logger = nullptr) const;
465 : tree get_representative_tree (const region *reg,
466 : logger *logger = nullptr) const;
467 : path_var
468 : get_representative_path_var (const svalue *sval,
469 : value_set *visited,
470 : logger *logger) const;
471 : path_var
472 : get_representative_path_var (const region *reg,
473 : value_set *visited,
474 : logger *logger) const;
475 :
476 : const region *
477 : maybe_get_region_for_mem_range (const symbolic::memory_range &mem_range,
478 : tree type,
479 : logger *logger = nullptr) const;
480 :
481 : /* For selftests. */
482 476855 : constraint_manager *get_constraints ()
483 : {
484 476855 : return m_constraints;
485 : }
486 :
487 916940 : store *get_store () { return &m_store; }
488 230276 : const store *get_store () const { return &m_store; }
489 :
490 : const dynamic_extents_t &
491 46327 : get_dynamic_extents () const
492 : {
493 46327 : return m_dynamic_extents;
494 : }
495 : const svalue *get_dynamic_extents (const region *reg) const;
496 : void set_dynamic_extents (const region *reg,
497 : const svalue *size_in_bytes,
498 : region_model_context *ctxt);
499 : void unset_dynamic_extents (const region *reg);
500 :
501 32 : symbolic::node_manager &get_node_manager () const { return m_node_mgr; }
502 707497 : region_model_manager *get_manager () const { return m_mgr; }
503 : bounded_ranges_manager *get_range_manager () const
504 : {
505 : return get_manager ()->get_range_manager ();
506 : }
507 :
508 : void unbind_region_and_descendents (const region *reg,
509 : enum poison_kind pkind);
510 :
511 : bool can_merge_with_p (const region_model &other_model,
512 : const program_point &point,
513 : region_model *out_model,
514 : const extrinsic_state *ext_state = nullptr,
515 : const program_state *state_a = nullptr,
516 : const program_state *state_b = nullptr) const;
517 :
518 : tree get_fndecl_for_call (const gcall &call,
519 : region_model_context *ctxt);
520 : tree get_fndecl_for_virtual_call (const_tree fn_ptr,
521 : region_model_context *ctxt);
522 :
523 : tree get_vtable_from_obj (tree obj, tree obj_type, region_model_manager *mgr,
524 : region_model_context *ctxt,
525 : unsigned HOST_WIDE_INT *out = nullptr) const;
526 :
527 : void get_regions_for_current_frame (auto_vec<const decl_region *> *out) const;
528 : static void append_regions_cb (const cluster_region &cluster_reg,
529 : struct append_regions_cb_data *data);
530 :
531 : const svalue *get_store_value (const region *reg,
532 : region_model_context *ctxt) const;
533 :
534 : const symbolic::bit_vector &
535 : get_store_bits (const symbolic::memory_range &mem_range,
536 : region_model_context *ctxt) const;
537 :
538 : const symbolic::bit_contents &
539 : get_bit_contents (const cluster_region &cluster_reg) const;
540 :
541 : void
542 : set_bit_contents_raw (const region &base_reg,
543 : const symbolic::bit_contents &new_bc);
544 :
545 : const svalue *scan_for_null_terminator (const region *reg,
546 : tree expr,
547 : const symbolic::bit_vector **out_bv,
548 : region_model_context *ctxt) const;
549 : const svalue *scan_for_null_terminator_1 (const region *reg,
550 : tree expr,
551 : const symbolic::bit_vector **out_bv,
552 : region_model_context *ctxt) const;
553 :
554 : void
555 : write_to_memory_offset (const symbolic::memory_offset &mem_offset,
556 : const symbolic::bit_vector &bv,
557 : region_model_context *ctxt);
558 :
559 : bool region_exists_p (const region *reg) const;
560 :
561 : void loop_replay_fixup (const region_model *dst_state);
562 :
563 : const svalue *get_capacity (const region *reg) const;
564 :
565 : const symbolic::scalar<integer_with_unit<units::byte>> &
566 : get_capacity_as_scalar (const region *reg) const;
567 :
568 : bool replay_call_summary (call_summary_replay &r,
569 : const region_model &summary);
570 :
571 : void maybe_complain_about_infoleak (const region *dst_reg,
572 : const svalue *copied_sval,
573 : const region *src_reg,
574 : region_model_context *ctxt);
575 :
576 : void set_errno (const call_details &cd);
577 :
578 : /* Implemented in sm-fd.cc */
579 : void mark_as_valid_fd (const svalue *sval, region_model_context *ctxt);
580 :
581 : /* Implemented in sm-malloc.cc */
582 : void on_realloc_with_move (const call_details &cd,
583 : const svalue *old_ptr_sval,
584 : const svalue *new_ptr_sval);
585 :
586 : /* Implemented in sm-malloc.cc. */
587 : void
588 : transition_ptr_sval_non_null (region_model_context *ctxt,
589 : const svalue *new_ptr_sval);
590 :
591 : /* Implemented in sm-taint.cc. */
592 : void mark_as_tainted (const svalue *sval,
593 : region_model_context *ctxt);
594 :
595 : bool add_constraint (const svalue *lhs,
596 : enum tree_code op,
597 : const svalue *rhs,
598 : region_model_context *ctxt);
599 :
600 : const svalue *check_for_poison (const svalue *sval,
601 : tree expr,
602 : const region *src_region,
603 : region_model_context *ctxt) const;
604 :
605 : void
606 : check_bytes_for_poison (const symbolic::bit_vector &bv,
607 : symbolic::pointer sym_ptr,
608 : tree sym_ptr_expr,
609 : region_model_context *ctxt) const;
610 :
611 : void check_region_for_write (const region *dest_reg,
612 : const svalue *sval_hint,
613 : region_model_context *ctxt) const;
614 :
615 : const svalue *
616 : check_for_null_terminated_string_arg (const call_details &cd,
617 : unsigned idx) const;
618 : const svalue *
619 : check_for_null_terminated_string_arg (const call_details &cd,
620 : unsigned idx,
621 : bool include_terminator,
622 : const symbolic::bit_vector **out_bv) const;
623 :
624 : const builtin_known_function *
625 : get_builtin_kf (const gcall &call,
626 : region_model_context *ctxt = nullptr) const;
627 :
628 : bool called_from_main_p () const;
629 :
630 : baseline_state
631 : get_baseline_state () const;
632 :
633 5967 : void push_thrown_exception (const exception_node &node)
634 : {
635 5967 : m_thrown_exceptions_stack.push_back (node);
636 : }
637 649 : const exception_node *get_current_thrown_exception () const
638 : {
639 649 : if (m_thrown_exceptions_stack.empty ())
640 : return nullptr;
641 646 : return &m_thrown_exceptions_stack.back ();
642 : }
643 349 : exception_node pop_thrown_exception ()
644 : {
645 349 : gcc_assert (!m_thrown_exceptions_stack.empty ());
646 349 : const exception_node retval = m_thrown_exceptions_stack.back ();
647 349 : m_thrown_exceptions_stack.pop_back ();
648 349 : return retval;
649 : }
650 :
651 349 : void push_caught_exception (const exception_node &node)
652 : {
653 349 : m_caught_exceptions_stack.push_back (node);
654 : }
655 198 : const exception_node *get_current_caught_exception () const
656 : {
657 198 : if (m_caught_exceptions_stack.empty ())
658 : return nullptr;
659 150 : return &m_caught_exceptions_stack.back ();
660 : }
661 233 : exception_node pop_caught_exception ()
662 : {
663 233 : gcc_assert (!m_caught_exceptions_stack.empty ());
664 233 : const exception_node retval = m_caught_exceptions_stack.back ();
665 233 : m_caught_exceptions_stack.pop_back ();
666 233 : return retval;
667 : }
668 :
669 : bool
670 300705 : bits_live_p (const symbolic::bit_vector_from_ubc &bv_from_ubc) const
671 : {
672 300705 : return m_store.bits_live_p (bv_from_ubc, *this);
673 : }
674 :
675 : private:
676 : const region *get_lvalue_1 (path_var pv, region_model_context *ctxt) const;
677 : const svalue *get_rvalue_1 (path_var pv, region_model_context *ctxt) const;
678 :
679 : path_var
680 : get_representative_path_var_1 (const svalue *sval,
681 : value_set *visited,
682 : logger *logger) const;
683 : path_var
684 : get_representative_path_var_1 (const region *reg,
685 : value_set *visited,
686 : logger *logger) const;
687 :
688 : const known_function *get_known_function (tree fndecl,
689 : const call_details &cd) const;
690 : const known_function *get_known_function (enum internal_fn) const;
691 :
692 : const region *
693 : maybe_get_subregion_for_symbolic_bit_range (const region ®,
694 : tree type,
695 : symbolic_bit_range bit_range) const;
696 : const region *
697 : maybe_get_subregion_for_concrete_bit_range (const region ®,
698 : tree desired_type,
699 : concrete_bit_range bit_range) const;
700 : const region *
701 : maybe_get_subregion_for_symbolic_bit_range_in_array (const region &array_reg,
702 : tree desired_type,
703 : symbolic_bit_range bit_range) const;
704 : const region *
705 : maybe_get_subregion_for_symbolic_bit_range_in_record (const region &record_reg,
706 : tree desired_type,
707 : symbolic_bit_range bit_range) const;
708 :
709 : bool add_constraints_from_binop (const svalue *outer_lhs,
710 : enum tree_code outer_op,
711 : const svalue *outer_rhs,
712 : bool *out,
713 : region_model_context *ctxt);
714 :
715 : void poison_any_pointers_to_descendents (const region *reg,
716 : enum poison_kind pkind);
717 :
718 : void on_top_level_param (tree param,
719 : bool nonnull,
720 : region_model_context *ctxt);
721 :
722 : const svalue *get_initial_value_for_global (const region *reg) const;
723 :
724 : const region * get_region_for_poisoned_expr (tree expr) const;
725 :
726 : const svalue *
727 : complain_about_poison (const svalue *sval,
728 : tree expr,
729 : const region *src_region,
730 : region_model_context *ctxt,
731 : enum poison_kind pkind) const;
732 :
733 : void check_dynamic_size_for_taint (memory_space mem_space,
734 : const svalue *size_in_bytes,
735 : region_model_context *ctxt) const;
736 : void check_dynamic_size_for_floats (const svalue *size_in_bytes,
737 : region_model_context *ctxt) const;
738 :
739 : void check_memory_range_for_taint (const symbolic::memory_range &mem_range,
740 : region_model_context *ctxt) const;
741 : void check_region_for_taint (const region *reg,
742 : region_model_context *ctxt) const;
743 :
744 : void check_for_writable_region (const region* dest_reg,
745 : region_model_context *ctxt) const;
746 :
747 : bool
748 : check_memory_range_access (const symbolic::memory_range &mem_range,
749 : enum access_direction dir,
750 : const svalue *sval_hint,
751 : region_model_context *ctxt) const;
752 :
753 : bool check_region_access (const region *reg,
754 : enum access_direction dir,
755 : const svalue *sval_hint,
756 : region_model_context *ctxt) const;
757 :
758 : void check_region_size (const region *lhs_reg, const svalue *rhs_sval,
759 : region_model_context *ctxt) const;
760 :
761 : /* Implemented in bounds-checking.cc */
762 : bool
763 : check_memory_range_bounds (const symbolic::memory_range &mem_range,
764 : enum access_direction dir,
765 : const svalue *sval_hint,
766 : region_model_context *ctxt) const;
767 :
768 : bool check_symbolic_bounds (const symbolic::memory_range &attempted_range,
769 : const svalue *capacity,
770 : enum access_direction dir,
771 : const svalue *sval_hint,
772 : region_model_context *ctxt) const;
773 : bool check_region_bounds (const region *reg, enum access_direction dir,
774 : const svalue *sval_hint,
775 : region_model_context *ctxt) const;
776 :
777 : void check_call_args (const call_details &cd) const;
778 : void check_call_format_attr (const call_details &cd,
779 : tree format_attr) const;
780 : void check_function_attr_access (const gcall &call,
781 : tree callee_fndecl,
782 : region_model_context *ctxt,
783 : rdwr_map &rdwr_idx) const;
784 : void check_function_attr_null_terminated_string_arg (const gcall &call,
785 : tree callee_fndecl,
786 : region_model_context *ctxt,
787 : rdwr_map &rdwr_idx);
788 : void check_one_function_attr_null_terminated_string_arg (const gcall &call,
789 : tree callee_fndecl,
790 : region_model_context *ctxt,
791 : rdwr_map &rdwr_idx,
792 : tree attr);
793 : void check_function_attrs (const gcall &call,
794 : tree callee_fndecl,
795 : region_model_context *ctxt);
796 :
797 : void check_for_throw_inside_call (const gcall &call,
798 : tree fndecl,
799 : region_model_context *ctxt);
800 :
801 : const svalue *
802 : canonicalize_svalue_for_constraint (const svalue *sval) const;
803 :
804 : bool
805 : can_recurse_eval_condition_p (const unaryop_svalue *unary_op) const;
806 :
807 : /* Storing this here to avoid passing it around everywhere. */
808 : symbolic::node_manager &m_node_mgr;
809 :
810 : // TODO: eliminate this copy (kept here to minimize churn)
811 : region_model_manager *const m_mgr;
812 :
813 : store m_store;
814 :
815 : constraint_manager *m_constraints; // TODO: embed, rather than dynalloc?
816 :
817 : const frame_region *m_current_frame;
818 :
819 : std::vector<exception_node> m_thrown_exceptions_stack;
820 : std::vector<exception_node> m_caught_exceptions_stack;
821 :
822 : /* Map from base region to size in bytes, for tracking the sizes of
823 : dynamically-allocated regions.
824 : This is part of the region_model rather than the region to allow for
825 : memory regions to be resized (e.g. by realloc). */
826 : dynamic_extents_t m_dynamic_extents;
827 : };
828 :
829 : /* Some region_model activity could lead to warnings (e.g. attempts to use an
830 : uninitialized value). This abstract base class encapsulates an interface
831 : for the region model to use when emitting such warnings.
832 :
833 : Having this as an abstract base class allows us to support the various
834 : operations needed by program_state in the analyzer within region_model,
835 : whilst keeping them somewhat modularized. */
836 :
837 1281074 : class region_model_context
838 : {
839 : public:
840 : bool
841 : warn (std::unique_ptr<pending_diagnostic> d,
842 : std::unique_ptr<pending_location::fixer_for_epath> ploc_fixer = nullptr);
843 :
844 : /* Hook for determining where diagnostics are to currently be emitted. */
845 : virtual pending_location
846 : get_pending_location_for_diag () const = 0;
847 :
848 : /* Hook for clients to store pending diagnostics.
849 : Return true if the diagnostic was stored, or false if it was deleted. */
850 : virtual bool
851 : warn_at (std::unique_ptr<pending_diagnostic> d,
852 : pending_location &&ploc) = 0;
853 :
854 : /* Hook for clients to add a note to the last previously stored
855 : pending diagnostic. */
856 : virtual void add_note (std::unique_ptr<pending_note> pn) = 0;
857 :
858 : /* Hook for clients to add an event to the last previously stored
859 : pending diagnostic. */
860 : virtual void add_event (std::unique_ptr<checker_event> event) = 0;
861 :
862 : /* Hook for clients to be notified when an SVAL that was reachable
863 : in a previous state is no longer live, so that clients can emit warnings
864 : about leaks. */
865 : virtual void on_svalue_leak (const svalue *sval) = 0;
866 :
867 : /* Hook for clients to be notified when the set of explicitly live
868 : svalues changes, so that they can purge state relating to dead
869 : svalues and report leaks. */
870 : virtual void on_liveness_change (symbolic::liveness_context &liveness_ctxt) = 0;
871 :
872 : virtual logger *get_logger () = 0;
873 :
874 : /* Hook for clients to be notified when the condition
875 : "LHS OP RHS" is added to the region model.
876 : This exists so that state machines can detect tests on edges,
877 : and use them to trigger sm-state transitions (e.g. transitions due
878 : to ptrs becoming known to be NULL or non-NULL, rather than just
879 : "unchecked") */
880 : virtual void on_condition (const svalue *lhs,
881 : enum tree_code op,
882 : const svalue *rhs) = 0;
883 :
884 : /* Hook for clients to be notified when the condition that
885 : SVAL is within RANGES is added to the region model.
886 : Similar to on_condition, but for use when handling switch statements.
887 : RANGES is non-empty. */
888 : virtual void on_bounded_ranges (const svalue &sval,
889 : const bounded_ranges &ranges) = 0;
890 :
891 : /* Hook for clients to be notified when a frame is popped from the stack. */
892 : virtual void on_pop_frame (const frame_region *) = 0;
893 :
894 : /* Hooks for clients to be notified when an unknown change happens
895 : to VAL (in response to a call to an unknown function). */
896 : virtual void on_unknown_change (const symbolic::value_node &val,
897 : bool is_mutable) = 0;
898 :
899 : /* Hooks for clients to be notified when a phi node is handled,
900 : where RHS is the pertinent argument. */
901 : virtual void on_phi (const gphi *phi, tree rhs) = 0;
902 :
903 : /* Hooks for clients to be notified when the region model doesn't
904 : know how to handle the tree code of T at LOC. */
905 : virtual void on_unexpected_tree_code (tree t,
906 : const dump_location_t &loc) = 0;
907 :
908 : /* Hook for clients to be notified when a function_decl escapes. */
909 : virtual void on_escaped_function (tree fndecl) = 0;
910 :
911 : virtual uncertainty_t *get_uncertainty () = 0;
912 :
913 : /* Hook for clients to purge state involving NODE. */
914 : virtual void purge_state_involving (const symbolic::node &node) = 0;
915 :
916 : /* Hook for clients to split state with a non-standard path. */
917 : virtual void bifurcate (std::unique_ptr<custom_edge_info> info) = 0;
918 :
919 : /* Hook for clients to terminate the standard path. */
920 : virtual void terminate_path () = 0;
921 :
922 : virtual const extrinsic_state *get_ext_state () const = 0;
923 :
924 : /* Hook for clients to access the a specific state machine in
925 : any underlying program_state. */
926 : virtual bool
927 : get_state_map_by_name (const char *name,
928 : sm_state_map **out_smap,
929 : const state_machine **out_sm,
930 : unsigned *out_sm_idx,
931 : std::unique_ptr<sm_context> *out_sm_context) = 0;
932 :
933 : /* Precanned ways for clients to access specific state machines. */
934 708 : bool get_fd_map (sm_state_map **out_smap,
935 : const state_machine **out_sm,
936 : unsigned *out_sm_idx,
937 : std::unique_ptr<sm_context> *out_sm_context)
938 : {
939 708 : return get_state_map_by_name ("file-descriptor", out_smap, out_sm,
940 : out_sm_idx, out_sm_context);
941 : }
942 280 : bool get_malloc_map (sm_state_map **out_smap,
943 : const state_machine **out_sm,
944 : unsigned *out_sm_idx)
945 : {
946 280 : return get_state_map_by_name ("malloc", out_smap, out_sm, out_sm_idx,
947 : nullptr);
948 : }
949 671892 : bool get_taint_map (sm_state_map **out_smap,
950 : const state_machine **out_sm,
951 : unsigned *out_sm_idx)
952 : {
953 671892 : return get_state_map_by_name ("taint", out_smap, out_sm, out_sm_idx,
954 : nullptr);
955 : }
956 :
957 : bool possibly_tainted_p (const svalue *sval);
958 :
959 : /* Get the current statement, if any. */
960 : virtual const gimple *get_stmt () const = 0;
961 :
962 : virtual const exploded_graph *get_eg () const = 0;
963 :
964 : virtual const program_state *get_state () const = 0;
965 :
966 : /* Hooks for detecting infinite loops. */
967 : virtual void maybe_did_work () = 0;
968 : virtual bool checking_for_infinite_loop_p () const = 0;
969 : virtual void on_unusable_in_infinite_loop () = 0;
970 : };
971 :
972 : /* A "do nothing" subclass of region_model_context. */
973 :
974 24554 : class noop_region_model_context : public region_model_context
975 : {
976 : public:
977 : pending_location
978 753 : get_pending_location_for_diag () const override
979 : {
980 753 : return pending_location ();
981 : }
982 : bool
983 749 : warn_at (std::unique_ptr<pending_diagnostic>,
984 : pending_location &&) override
985 : {
986 749 : return false;
987 : }
988 : void add_note (std::unique_ptr<pending_note>) override;
989 : void add_event (std::unique_ptr<checker_event>) override;
990 0 : void on_svalue_leak (const svalue *) override {}
991 0 : void on_liveness_change (symbolic::liveness_context &) override {}
992 73681 : logger *get_logger () override { return nullptr; }
993 44 : void on_condition (const svalue *lhs ATTRIBUTE_UNUSED,
994 : enum tree_code op ATTRIBUTE_UNUSED,
995 : const svalue *rhs ATTRIBUTE_UNUSED) override
996 : {
997 44 : }
998 63 : void on_bounded_ranges (const svalue &,
999 : const bounded_ranges &) override
1000 : {
1001 63 : }
1002 297 : void on_pop_frame (const frame_region *) override {}
1003 1296 : void on_unknown_change (const symbolic::value_node &val ATTRIBUTE_UNUSED,
1004 : bool is_mutable ATTRIBUTE_UNUSED) override
1005 : {
1006 1296 : }
1007 0 : void on_phi (const gphi *phi ATTRIBUTE_UNUSED,
1008 : tree rhs ATTRIBUTE_UNUSED) override
1009 : {
1010 0 : }
1011 0 : void on_unexpected_tree_code (tree, const dump_location_t &) override {}
1012 :
1013 0 : void on_escaped_function (tree) override {}
1014 :
1015 12280 : uncertainty_t *get_uncertainty () override { return nullptr; }
1016 :
1017 1862 : void purge_state_involving (const symbolic::node &) override {}
1018 :
1019 : void bifurcate (std::unique_ptr<custom_edge_info> info) override;
1020 : void terminate_path () override;
1021 :
1022 14428 : const extrinsic_state *get_ext_state () const override { return nullptr; }
1023 :
1024 36947 : bool get_state_map_by_name (const char *,
1025 : sm_state_map **,
1026 : const state_machine **,
1027 : unsigned *,
1028 : std::unique_ptr<sm_context> *) override
1029 : {
1030 36947 : return false;
1031 : }
1032 :
1033 12360 : const gimple *get_stmt () const override { return nullptr; }
1034 0 : const exploded_graph *get_eg () const override { return nullptr; }
1035 0 : const program_state *get_state () const override { return nullptr; }
1036 :
1037 746 : void maybe_did_work () override {}
1038 44 : bool checking_for_infinite_loop_p () const override { return false; }
1039 0 : void on_unusable_in_infinite_loop () override {}
1040 : };
1041 :
1042 : /* A subclass of region_model_context for determining if operations fail
1043 : e.g. "can we generate a region for the lvalue of EXPR?". */
1044 :
1045 : class tentative_region_model_context : public noop_region_model_context
1046 : {
1047 : public:
1048 : tentative_region_model_context () : m_num_unexpected_codes (0) {}
1049 :
1050 0 : void on_unexpected_tree_code (tree, const dump_location_t &)
1051 : final override
1052 : {
1053 0 : m_num_unexpected_codes++;
1054 0 : }
1055 :
1056 : bool had_errors_p () const { return m_num_unexpected_codes > 0; }
1057 :
1058 : private:
1059 : int m_num_unexpected_codes;
1060 : };
1061 :
1062 : /* Subclass of region_model_context that wraps another context, allowing
1063 : for extra code to be added to the various hooks. */
1064 :
1065 : class region_model_context_decorator : public region_model_context
1066 : {
1067 : public:
1068 : pending_location
1069 241 : get_pending_location_for_diag () const override
1070 : {
1071 241 : if (m_inner)
1072 235 : return m_inner->get_pending_location_for_diag ();
1073 : else
1074 6 : return pending_location ();
1075 : }
1076 :
1077 : bool
1078 9 : warn_at (std::unique_ptr<pending_diagnostic> d,
1079 : pending_location &&ploc) override
1080 : {
1081 9 : if (m_inner)
1082 9 : return m_inner->warn_at (std::move (d), std::move (ploc));
1083 : else
1084 : return false;
1085 : }
1086 :
1087 230 : void add_note (std::unique_ptr<pending_note> pn) override
1088 : {
1089 230 : if (m_inner)
1090 230 : m_inner->add_note (std::move (pn));
1091 230 : }
1092 : void add_event (std::unique_ptr<checker_event> event) override;
1093 :
1094 0 : void on_svalue_leak (const svalue *sval) override
1095 : {
1096 0 : if (m_inner)
1097 0 : m_inner->on_svalue_leak (sval);
1098 0 : }
1099 :
1100 0 : void on_liveness_change (symbolic::liveness_context &liveness_ctxt) override
1101 : {
1102 0 : if (m_inner)
1103 0 : m_inner->on_liveness_change (liveness_ctxt);
1104 0 : }
1105 :
1106 34863 : logger *get_logger () override
1107 : {
1108 34863 : if (m_inner)
1109 25331 : return m_inner->get_logger ();
1110 : else
1111 : return nullptr;
1112 : }
1113 :
1114 0 : void on_condition (const svalue *lhs,
1115 : enum tree_code op,
1116 : const svalue *rhs) override
1117 : {
1118 0 : if (m_inner)
1119 0 : m_inner->on_condition (lhs, op, rhs);
1120 0 : }
1121 :
1122 0 : void on_bounded_ranges (const svalue &sval,
1123 : const bounded_ranges &ranges) override
1124 : {
1125 0 : if (m_inner)
1126 0 : m_inner->on_bounded_ranges (sval, ranges);
1127 0 : }
1128 :
1129 0 : void on_pop_frame (const frame_region *frame_reg) override
1130 : {
1131 0 : if (m_inner)
1132 0 : m_inner->on_pop_frame (frame_reg);
1133 0 : }
1134 :
1135 0 : void on_unknown_change (const symbolic::value_node &val,
1136 : bool is_mutable) override
1137 : {
1138 0 : if (m_inner)
1139 0 : m_inner->on_unknown_change (val, is_mutable);
1140 0 : }
1141 :
1142 0 : void on_phi (const gphi *phi, tree rhs) override
1143 : {
1144 0 : if (m_inner)
1145 0 : m_inner->on_phi (phi, rhs);
1146 0 : }
1147 :
1148 0 : void on_unexpected_tree_code (tree t,
1149 : const dump_location_t &loc) override
1150 : {
1151 0 : if (m_inner)
1152 0 : m_inner->on_unexpected_tree_code (t, loc);
1153 0 : }
1154 :
1155 0 : void on_escaped_function (tree fndecl) override
1156 : {
1157 0 : if (m_inner)
1158 0 : m_inner->on_escaped_function (fndecl);
1159 0 : }
1160 :
1161 5782 : uncertainty_t *get_uncertainty () override
1162 : {
1163 5782 : if (m_inner)
1164 4484 : return m_inner->get_uncertainty ();
1165 : else
1166 : return nullptr;
1167 : }
1168 :
1169 0 : void purge_state_involving (const symbolic::node &node) override
1170 : {
1171 0 : if (m_inner)
1172 0 : m_inner->purge_state_involving (node);
1173 0 : }
1174 :
1175 0 : void bifurcate (std::unique_ptr<custom_edge_info> info) override
1176 : {
1177 0 : if (m_inner)
1178 0 : m_inner->bifurcate (std::move (info));
1179 0 : }
1180 :
1181 0 : void terminate_path () override
1182 : {
1183 0 : if (m_inner)
1184 0 : m_inner->terminate_path ();
1185 0 : }
1186 :
1187 7799 : const extrinsic_state *get_ext_state () const override
1188 : {
1189 7799 : if (m_inner)
1190 7799 : return m_inner->get_ext_state ();
1191 : else
1192 : return nullptr;
1193 : }
1194 :
1195 10373 : bool get_state_map_by_name (const char *name,
1196 : sm_state_map **out_smap,
1197 : const state_machine **out_sm,
1198 : unsigned *out_sm_idx,
1199 : std::unique_ptr<sm_context> *out_sm_context)
1200 : override
1201 : {
1202 10373 : if (m_inner)
1203 8033 : return m_inner->get_state_map_by_name (name, out_smap, out_sm, out_sm_idx,
1204 8033 : out_sm_context);
1205 : else
1206 : return false;
1207 : }
1208 :
1209 101 : const gimple *get_stmt () const override
1210 : {
1211 101 : if (m_inner)
1212 101 : return m_inner->get_stmt ();
1213 : else
1214 : return nullptr;
1215 : }
1216 :
1217 0 : const exploded_graph *get_eg () const override
1218 : {
1219 0 : if (m_inner)
1220 0 : return m_inner->get_eg ();
1221 : else
1222 : return nullptr;
1223 : }
1224 :
1225 0 : const program_state *get_state () const override
1226 : {
1227 0 : if (m_inner)
1228 0 : return m_inner->get_state ();
1229 : else
1230 : return nullptr;
1231 : }
1232 :
1233 0 : void maybe_did_work () override
1234 : {
1235 0 : if (m_inner)
1236 0 : m_inner->maybe_did_work ();
1237 0 : }
1238 :
1239 0 : bool checking_for_infinite_loop_p () const override
1240 : {
1241 0 : if (m_inner)
1242 0 : return m_inner->checking_for_infinite_loop_p ();
1243 : return false;
1244 : }
1245 0 : void on_unusable_in_infinite_loop () override
1246 : {
1247 0 : if (m_inner)
1248 0 : m_inner->on_unusable_in_infinite_loop ();
1249 0 : }
1250 :
1251 : protected:
1252 12886 : region_model_context_decorator (region_model_context *inner)
1253 5782 : : m_inner (inner)
1254 : {
1255 : }
1256 :
1257 : region_model_context *m_inner;
1258 : };
1259 :
1260 : /* Subclass of region_model_context_decorator with a hook for adding
1261 : notes/events when saving diagnostics. */
1262 :
1263 : class annotating_context : public region_model_context_decorator
1264 : {
1265 : public:
1266 : bool
1267 232 : warn_at (std::unique_ptr<pending_diagnostic> d,
1268 : pending_location &&ploc) override
1269 : {
1270 232 : if (m_inner)
1271 226 : if (m_inner->warn_at (std::move (d), std::move (ploc)))
1272 : {
1273 222 : add_annotations ();
1274 222 : return true;
1275 : }
1276 : return false;
1277 : }
1278 :
1279 : /* Hook to add new event(s)/note(s) */
1280 : virtual void add_annotations () = 0;
1281 :
1282 : protected:
1283 7104 : annotating_context (region_model_context *inner)
1284 7104 : : region_model_context_decorator (inner)
1285 : {
1286 : }
1287 : };
1288 :
1289 : /* A bundle of data for use when attempting to merge two region_model
1290 : instances to make a third. */
1291 :
1292 65300 : struct model_merger
1293 : {
1294 65300 : model_merger (const region_model *model_a,
1295 : const region_model *model_b,
1296 : const program_point &point,
1297 : region_model *merged_model,
1298 : const extrinsic_state *ext_state,
1299 : const program_state *state_a,
1300 : const program_state *state_b)
1301 65300 : : m_model_a (model_a), m_model_b (model_b),
1302 65300 : m_point (point),
1303 65300 : m_merged_model (merged_model),
1304 65300 : m_ext_state (ext_state),
1305 65300 : m_state_a (state_a), m_state_b (state_b)
1306 : {
1307 : }
1308 :
1309 : void dump_to_logger (logger &) const;
1310 : void dump () const;
1311 :
1312 : region_model_manager *get_manager () const
1313 : {
1314 : return m_model_a->get_manager ();
1315 : }
1316 :
1317 : bool mergeable_svalue_p (const svalue *) const;
1318 : bool mergeable_bit_vector_p (const symbolic::bit_vector &) const;
1319 :
1320 2747 : const supernode *get_supernode () const
1321 : {
1322 2747 : return m_point.get_supernode ();
1323 : }
1324 :
1325 : void on_widening_reuse (const widening_svalue *widening_sval);
1326 :
1327 : const region_model *m_model_a;
1328 : const region_model *m_model_b;
1329 : const program_point &m_point;
1330 : region_model *m_merged_model;
1331 :
1332 : const extrinsic_state *m_ext_state;
1333 : const program_state *m_state_a;
1334 : const program_state *m_state_b;
1335 :
1336 : hash_set<const svalue *> m_svals_changing_meaning;
1337 : };
1338 :
1339 : /* A record that can (optionally) be written out when
1340 : region_model::add_constraint fails. */
1341 :
1342 : class rejected_constraint
1343 : {
1344 : public:
1345 : virtual ~rejected_constraint () {}
1346 : virtual void dump_to_pp (pretty_printer *pp) const = 0;
1347 : virtual std::unique_ptr<json::object> to_json () const = 0;
1348 :
1349 0 : const region_model &get_model () const { return m_model; }
1350 :
1351 : protected:
1352 1624 : rejected_constraint (const region_model &model)
1353 1611 : : m_model (model)
1354 : {}
1355 :
1356 : std::unique_ptr<json::object>
1357 : make_base_json (const char *kind) const;
1358 :
1359 : region_model m_model;
1360 : };
1361 :
1362 : class rejected_op_constraint : public rejected_constraint
1363 : {
1364 : public:
1365 1612 : rejected_op_constraint (const region_model &model,
1366 : const svalue *lhs, enum tree_code op, const svalue *rhs)
1367 : : rejected_constraint (model),
1368 1612 : m_lhs (lhs), m_op (op), m_rhs (rhs)
1369 : {}
1370 :
1371 : void dump_to_pp (pretty_printer *pp) const final override;
1372 : std::unique_ptr<json::object> to_json () const final override;
1373 :
1374 : const svalue *m_lhs;
1375 : enum tree_code m_op;
1376 : const svalue *m_rhs;
1377 : };
1378 :
1379 : class rejected_default_case : public rejected_constraint
1380 : {
1381 : public:
1382 0 : rejected_default_case (const region_model &model)
1383 0 : : rejected_constraint (model)
1384 : {}
1385 :
1386 : void dump_to_pp (pretty_printer *pp) const final override;
1387 : std::unique_ptr<json::object> to_json () const final override;
1388 : };
1389 :
1390 : class rejected_ranges_constraint : public rejected_constraint
1391 : {
1392 : public:
1393 12 : rejected_ranges_constraint (const region_model &model,
1394 : tree expr, const bounded_ranges *ranges)
1395 : : rejected_constraint (model),
1396 12 : m_expr (expr), m_ranges (ranges)
1397 : {}
1398 :
1399 : void dump_to_pp (pretty_printer *pp) const final override;
1400 : std::unique_ptr<json::object> to_json () const final override;
1401 :
1402 : private:
1403 : tree m_expr;
1404 : const bounded_ranges *m_ranges;
1405 : };
1406 :
1407 : /* A bundle of state. */
1408 :
1409 : class engine
1410 : {
1411 : public:
1412 : engine (symbolic::node_manager &node_mgr,
1413 : const supergraph *sg = nullptr);
1414 290280 : const supergraph *get_supergraph () { return m_sg; }
1415 :
1416 : symbolic::node_manager &
1417 187392 : get_node_manager ()
1418 : {
1419 141068 : return m_node_mgr;
1420 : }
1421 :
1422 : region_model_manager *
1423 8164835 : get_model_manager ()
1424 : {
1425 8164797 : return &m_node_mgr.get_region_model_manager ();
1426 : }
1427 :
1428 3697 : known_function_manager *get_known_function_manager ()
1429 : {
1430 3697 : return get_model_manager ()->get_known_function_manager ();
1431 : }
1432 :
1433 : void log_stats (logger *logger) const;
1434 :
1435 : private:
1436 : symbolic::node_manager &m_node_mgr;
1437 : const supergraph *m_sg;
1438 : };
1439 :
1440 : /* Factory functions for various diagnostics. */
1441 :
1442 : extern std::unique_ptr<pending_diagnostic>
1443 : make_poisoned_value_diagnostic (tree expr, enum poison_kind pkind,
1444 : const region *src_region,
1445 : tree check_expr);
1446 :
1447 : extern std::unique_ptr<pending_diagnostic>
1448 : make_shift_count_negative_diagnostic (const gassign *assign,
1449 : tree count_cst,
1450 : const region *src_region);
1451 :
1452 : extern std::unique_ptr<pending_diagnostic>
1453 : make_shift_count_overflow_diagnostic (const gassign *assign,
1454 : int operand_precision,
1455 : tree count_cst,
1456 : const region *src_region);
1457 :
1458 : extern std::unique_ptr<pending_diagnostic>
1459 : make_write_to_const_diagnostic (const region *dest_reg, tree decl);
1460 :
1461 : extern std::unique_ptr<pending_diagnostic>
1462 : make_write_to_string_literal_diagnostic (const region *reg);
1463 :
1464 5464 : struct poison_finder : public symbolic::node_visitor
1465 : {
1466 5542 : poison_finder ()
1467 5542 : : m_found_poison (false)
1468 : {
1469 : }
1470 :
1471 : bool
1472 : visit_svalue (const svalue &sval) final override;
1473 :
1474 : bool
1475 : visit_bit_vector (const symbolic::bit_vector &bv) final override;
1476 :
1477 : bool m_found_poison;
1478 : enum poison_kind m_pkind;
1479 : };
1480 :
1481 : } // namespace ana
1482 :
1483 : extern void debug (const region_model &rmodel);
1484 :
1485 : namespace ana {
1486 :
1487 : #if CHECKING_P
1488 :
1489 : namespace selftest {
1490 :
1491 : using namespace ::selftest;
1492 :
1493 : /* An implementation of region_model_context for use in selftests, which
1494 : stores any pending_diagnostic instances passed to it. */
1495 :
1496 224 : class test_region_model_context : public noop_region_model_context
1497 : {
1498 : public:
1499 : bool
1500 4 : warn_at (std::unique_ptr<pending_diagnostic> d,
1501 : pending_location &&) final override
1502 : {
1503 4 : m_diagnostics.safe_push (d.release ());
1504 4 : return true;
1505 : }
1506 :
1507 4 : unsigned get_num_diagnostics () const { return m_diagnostics.length (); }
1508 :
1509 0 : void on_unexpected_tree_code (tree t, const dump_location_t &)
1510 : final override
1511 : {
1512 0 : internal_error ("unhandled tree code: %qs",
1513 0 : get_tree_code_name (TREE_CODE (t)));
1514 : }
1515 :
1516 : private:
1517 : /* Implicitly delete any diagnostics in the dtor. */
1518 : auto_delete_vec<pending_diagnostic> m_diagnostics;
1519 : };
1520 :
1521 : /* Attempt to add the constraint (LHS OP RHS) to MODEL.
1522 : Verify that MODEL remains satisfiable. */
1523 :
1524 : #define ADD_SAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
1525 : SELFTEST_BEGIN_STMT \
1526 : bool sat = (MODEL).add_constraint (LHS, OP, RHS, nullptr); \
1527 : ASSERT_TRUE (sat); \
1528 : SELFTEST_END_STMT
1529 :
1530 : /* Attempt to add the constraint (LHS OP RHS) to MODEL.
1531 : Verify that the result is not satisfiable. */
1532 :
1533 : #define ADD_UNSAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
1534 : SELFTEST_BEGIN_STMT \
1535 : bool sat = (MODEL).add_constraint (LHS, OP, RHS, nullptr); \
1536 : ASSERT_FALSE (sat); \
1537 : SELFTEST_END_STMT
1538 :
1539 : /* Implementation detail of the ASSERT_CONDITION_* macros. */
1540 :
1541 : void assert_condition (const location &loc,
1542 : region_model &model,
1543 : const svalue *lhs, tree_code op, const svalue *rhs,
1544 : tristate expected);
1545 :
1546 : void assert_condition (const location &loc,
1547 : region_model &model,
1548 : tree lhs, tree_code op, tree rhs,
1549 : tristate expected);
1550 :
1551 : /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1552 : as "true". */
1553 :
1554 : #define ASSERT_CONDITION_TRUE(REGION_MODEL, LHS, OP, RHS) \
1555 : SELFTEST_BEGIN_STMT \
1556 : assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1557 : tristate (tristate::TS_TRUE)); \
1558 : SELFTEST_END_STMT
1559 :
1560 : /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1561 : as "false". */
1562 :
1563 : #define ASSERT_CONDITION_FALSE(REGION_MODEL, LHS, OP, RHS) \
1564 : SELFTEST_BEGIN_STMT \
1565 : assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1566 : tristate (tristate::TS_FALSE)); \
1567 : SELFTEST_END_STMT
1568 :
1569 : /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1570 : as "unknown". */
1571 :
1572 : #define ASSERT_CONDITION_UNKNOWN(REGION_MODEL, LHS, OP, RHS) \
1573 : SELFTEST_BEGIN_STMT \
1574 : assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1575 : tristate (tristate::TS_UNKNOWN)); \
1576 : SELFTEST_END_STMT
1577 :
1578 : } /* end of namespace selftest. */
1579 :
1580 : #endif /* #if CHECKING_P */
1581 :
1582 : } // namespace ana
1583 :
1584 : #endif /* GCC_ANALYZER_REGION_MODEL_H */
|