1 /* Classes for modeling the state of memory.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
21 #ifndef GCC_ANALYZER_REGION_MODEL_H
22 #define GCC_ANALYZER_REGION_MODEL_H
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 */
29 #include "analyzer/svalue.h"
30 #include "analyzer/region.h"
36 extern void add_path_var (path_var pv
, hash
&hstate
);
37 } // namespace inchash
45 one_way_id_map (int num_ids
);
46 void put (T src
, T dst
);
47 T
get_dst_for_src (T src
) const;
48 void dump_to_pp (pretty_printer
*pp
) const;
50 void update (T
*) const;
53 auto_vec
<T
> m_src_to_dst
;
56 /* class one_way_id_map. */
58 /* one_way_id_map's ctor, which populates the map with dummy null values. */
61 inline one_way_id_map
<T
>::one_way_id_map (int num_svalues
)
62 : m_src_to_dst (num_svalues
)
64 for (int i
= 0; i
< num_svalues
; i
++)
65 m_src_to_dst
.quick_push (T::null ());
68 /* Record that SRC is to be mapped to DST. */
72 one_way_id_map
<T
>::put (T src
, T dst
)
74 m_src_to_dst
[src
.as_int ()] = dst
;
77 /* Get the new value for SRC within the map. */
81 one_way_id_map
<T
>::get_dst_for_src (T src
) const
85 return m_src_to_dst
[src
.as_int ()];
88 /* Dump this map to PP. */
92 one_way_id_map
<T
>::dump_to_pp (pretty_printer
*pp
) const
94 pp_string (pp
, "src to dst: {");
97 FOR_EACH_VEC_ELT (m_src_to_dst
, i
, dst
)
100 pp_string (pp
, ", ");
101 T
src (T::from_int (i
));
103 pp_string (pp
, " -> ");
110 /* Dump this map to stderr. */
112 template <typename T
>
113 DEBUG_FUNCTION
inline void
114 one_way_id_map
<T
>::dump () const
117 pp
.buffer
->stream
= stderr
;
122 /* Update *ID from the old value to its new value in this map. */
124 template <typename T
>
126 one_way_id_map
<T
>::update (T
*id
) const
128 *id
= get_dst_for_src (*id
);
131 /* A mapping from region to svalue for use when tracking state. */
133 class region_to_value_map
136 typedef hash_map
<const region
*, const svalue
*> hash_map_t
;
137 typedef hash_map_t::iterator iterator
;
139 region_to_value_map () : m_hash_map () {}
140 region_to_value_map (const region_to_value_map
&other
)
141 : m_hash_map (other
.m_hash_map
) {}
142 region_to_value_map
&operator= (const region_to_value_map
&other
);
144 bool operator== (const region_to_value_map
&other
) const;
145 bool operator!= (const region_to_value_map
&other
) const
147 return !(*this == other
);
150 iterator
begin () const { return m_hash_map
.begin (); }
151 iterator
end () const { return m_hash_map
.end (); }
153 const svalue
* const *get (const region
*reg
) const
155 return const_cast <hash_map_t
&> (m_hash_map
).get (reg
);
157 void put (const region
*reg
, const svalue
*sval
)
159 m_hash_map
.put (reg
, sval
);
161 void remove (const region
*reg
)
163 m_hash_map
.remove (reg
);
166 bool is_empty () const { return m_hash_map
.is_empty (); }
168 void dump_to_pp (pretty_printer
*pp
, bool simple
, bool multiline
) const;
169 void dump (bool simple
) const;
171 bool can_merge_with_p (const region_to_value_map
&other
,
172 region_to_value_map
*out
) const;
174 void purge_state_involving (const svalue
*sval
);
177 hash_map_t m_hash_map
;
180 /* Various operations delete information from a region_model.
182 This struct tracks how many of each kind of entity were purged (e.g.
183 for selftests, and for debugging). */
190 m_num_equiv_classes (0),
191 m_num_constraints (0),
192 m_num_client_items (0)
197 int m_num_equiv_classes
;
198 int m_num_constraints
;
199 int m_num_client_items
;
202 /* A base class for visiting regions and svalues, with do-nothing
203 base implementations of the per-subclass vfuncs. */
208 virtual void visit_region_svalue (const region_svalue
*) {}
209 virtual void visit_constant_svalue (const constant_svalue
*) {}
210 virtual void visit_unknown_svalue (const unknown_svalue
*) {}
211 virtual void visit_poisoned_svalue (const poisoned_svalue
*) {}
212 virtual void visit_setjmp_svalue (const setjmp_svalue
*) {}
213 virtual void visit_initial_svalue (const initial_svalue
*) {}
214 virtual void visit_unaryop_svalue (const unaryop_svalue
*) {}
215 virtual void visit_binop_svalue (const binop_svalue
*) {}
216 virtual void visit_sub_svalue (const sub_svalue
*) {}
217 virtual void visit_repeated_svalue (const repeated_svalue
*) {}
218 virtual void visit_bits_within_svalue (const bits_within_svalue
*) {}
219 virtual void visit_unmergeable_svalue (const unmergeable_svalue
*) {}
220 virtual void visit_placeholder_svalue (const placeholder_svalue
*) {}
221 virtual void visit_widening_svalue (const widening_svalue
*) {}
222 virtual void visit_compound_svalue (const compound_svalue
*) {}
223 virtual void visit_conjured_svalue (const conjured_svalue
*) {}
225 virtual void visit_region (const region
*) {}
232 /* A class responsible for owning and consolidating region and svalue
234 region and svalue instances are immutable as far as clients are
235 concerned, so they are provided as "const" ptrs. */
237 class region_model_manager
240 region_model_manager ();
241 ~region_model_manager ();
243 /* svalue consolidation. */
244 const svalue
*get_or_create_constant_svalue (tree cst_expr
);
245 const svalue
*get_or_create_int_cst (tree type
, poly_int64
);
246 const svalue
*get_or_create_unknown_svalue (tree type
);
247 const svalue
*get_or_create_setjmp_svalue (const setjmp_record
&r
,
249 const svalue
*get_or_create_poisoned_svalue (enum poison_kind kind
,
251 const svalue
*get_or_create_initial_value (const region
*reg
);
252 const svalue
*get_ptr_svalue (tree ptr_type
, const region
*pointee
);
253 const svalue
*get_or_create_unaryop (tree type
, enum tree_code op
,
255 const svalue
*get_or_create_cast (tree type
, const svalue
*arg
);
256 const svalue
*get_or_create_binop (tree type
,
258 const svalue
*arg0
, const svalue
*arg1
);
259 const svalue
*get_or_create_sub_svalue (tree type
,
260 const svalue
*parent_svalue
,
261 const region
*subregion
);
262 const svalue
*get_or_create_repeated_svalue (tree type
,
263 const svalue
*outer_size
,
264 const svalue
*inner_svalue
);
265 const svalue
*get_or_create_bits_within (tree type
,
266 const bit_range
&bits
,
267 const svalue
*inner_svalue
);
268 const svalue
*get_or_create_unmergeable (const svalue
*arg
);
269 const svalue
*get_or_create_widening_svalue (tree type
,
270 const program_point
&point
,
271 const svalue
*base_svalue
,
272 const svalue
*iter_svalue
);
273 const svalue
*get_or_create_compound_svalue (tree type
,
274 const binding_map
&map
);
275 const svalue
*get_or_create_conjured_svalue (tree type
, const gimple
*stmt
,
276 const region
*id_reg
);
278 const svalue
*maybe_get_char_from_string_cst (tree string_cst
,
279 tree byte_offset_cst
);
281 /* region consolidation. */
282 const stack_region
* get_stack_region () const { return &m_stack_region
; }
283 const heap_region
*get_heap_region () const { return &m_heap_region
; }
284 const code_region
*get_code_region () const { return &m_code_region
; }
285 const globals_region
*get_globals_region () const
287 return &m_globals_region
;
289 const function_region
*get_region_for_fndecl (tree fndecl
);
290 const label_region
*get_region_for_label (tree label
);
291 const decl_region
*get_region_for_global (tree expr
);
292 const region
*get_field_region (const region
*parent
, tree field
);
293 const region
*get_element_region (const region
*parent
,
295 const svalue
*index
);
296 const region
*get_offset_region (const region
*parent
,
298 const svalue
*byte_offset
);
299 const region
*get_sized_region (const region
*parent
,
301 const svalue
*byte_size_sval
);
302 const region
*get_cast_region (const region
*original_region
,
304 const frame_region
*get_frame_region (const frame_region
*calling_frame
,
306 const region
*get_symbolic_region (const svalue
*sval
);
307 const string_region
*get_region_for_string (tree string_cst
);
310 get_region_for_unexpected_tree_code (region_model_context
*ctxt
,
312 const dump_location_t
&loc
);
314 unsigned alloc_region_id () { return m_next_region_id
++; }
316 store_manager
*get_store_manager () { return &m_store_mgr
; }
318 /* Dynamically-allocated region instances.
319 The number of these within the analysis can grow arbitrarily.
320 They are still owned by the manager. */
321 const region
*create_region_for_heap_alloc ();
322 const region
*create_region_for_alloca (const frame_region
*frame
);
324 void log_stats (logger
*logger
, bool show_objs
) const;
327 bool too_complex_p (const complexity
&c
) const;
328 bool reject_if_too_complex (svalue
*sval
);
330 const svalue
*maybe_fold_unaryop (tree type
, enum tree_code op
,
332 const svalue
*maybe_fold_binop (tree type
, enum tree_code op
,
333 const svalue
*arg0
, const svalue
*arg1
);
334 const svalue
*maybe_fold_sub_svalue (tree type
,
335 const svalue
*parent_svalue
,
336 const region
*subregion
);
337 const svalue
*maybe_fold_repeated_svalue (tree type
,
338 const svalue
*outer_size
,
339 const svalue
*inner_svalue
);
340 const svalue
*maybe_fold_bits_within_svalue (tree type
,
341 const bit_range
&bits
,
342 const svalue
*inner_svalue
);
343 const svalue
*maybe_undo_optimize_bit_field_compare (tree type
,
344 const compound_svalue
*compound_sval
,
345 tree cst
, const svalue
*arg1
);
347 unsigned m_next_region_id
;
348 root_region m_root_region
;
349 stack_region m_stack_region
;
350 heap_region m_heap_region
;
352 /* svalue consolidation. */
353 typedef hash_map
<tree
, constant_svalue
*> constants_map_t
;
354 constants_map_t m_constants_map
;
356 typedef hash_map
<tree
, unknown_svalue
*> unknowns_map_t
;
357 unknowns_map_t m_unknowns_map
;
358 const unknown_svalue
*m_unknown_NULL
;
360 typedef hash_map
<poisoned_svalue::key_t
,
361 poisoned_svalue
*> poisoned_values_map_t
;
362 poisoned_values_map_t m_poisoned_values_map
;
364 typedef hash_map
<setjmp_svalue::key_t
,
365 setjmp_svalue
*> setjmp_values_map_t
;
366 setjmp_values_map_t m_setjmp_values_map
;
368 typedef hash_map
<const region
*, initial_svalue
*> initial_values_map_t
;
369 initial_values_map_t m_initial_values_map
;
371 typedef hash_map
<region_svalue::key_t
, region_svalue
*> pointer_values_map_t
;
372 pointer_values_map_t m_pointer_values_map
;
374 typedef hash_map
<unaryop_svalue::key_t
,
375 unaryop_svalue
*> unaryop_values_map_t
;
376 unaryop_values_map_t m_unaryop_values_map
;
378 typedef hash_map
<binop_svalue::key_t
, binop_svalue
*> binop_values_map_t
;
379 binop_values_map_t m_binop_values_map
;
381 typedef hash_map
<sub_svalue::key_t
, sub_svalue
*> sub_values_map_t
;
382 sub_values_map_t m_sub_values_map
;
384 typedef hash_map
<repeated_svalue::key_t
,
385 repeated_svalue
*> repeated_values_map_t
;
386 repeated_values_map_t m_repeated_values_map
;
388 typedef hash_map
<bits_within_svalue::key_t
,
389 bits_within_svalue
*> bits_within_values_map_t
;
390 bits_within_values_map_t m_bits_within_values_map
;
392 typedef hash_map
<const svalue
*,
393 unmergeable_svalue
*> unmergeable_values_map_t
;
394 unmergeable_values_map_t m_unmergeable_values_map
;
396 typedef hash_map
<widening_svalue::key_t
,
398 widening_svalue::key_t::hash_map_traits*/
>
399 widening_values_map_t
;
400 widening_values_map_t m_widening_values_map
;
402 typedef hash_map
<compound_svalue::key_t
,
403 compound_svalue
*> compound_values_map_t
;
404 compound_values_map_t m_compound_values_map
;
406 typedef hash_map
<conjured_svalue::key_t
,
407 conjured_svalue
*> conjured_values_map_t
;
408 conjured_values_map_t m_conjured_values_map
;
410 /* Maximum complexity of svalues that weren't rejected. */
411 complexity m_max_complexity
;
413 /* region consolidation. */
415 code_region m_code_region
;
416 typedef hash_map
<tree
, function_region
*> fndecls_map_t
;
417 typedef fndecls_map_t::iterator fndecls_iterator_t
;
418 fndecls_map_t m_fndecls_map
;
420 typedef hash_map
<tree
, label_region
*> labels_map_t
;
421 typedef labels_map_t::iterator labels_iterator_t
;
422 labels_map_t m_labels_map
;
424 globals_region m_globals_region
;
425 typedef hash_map
<tree
, decl_region
*> globals_map_t
;
426 typedef globals_map_t::iterator globals_iterator_t
;
427 globals_map_t m_globals_map
;
429 consolidation_map
<field_region
> m_field_regions
;
430 consolidation_map
<element_region
> m_element_regions
;
431 consolidation_map
<offset_region
> m_offset_regions
;
432 consolidation_map
<sized_region
> m_sized_regions
;
433 consolidation_map
<cast_region
> m_cast_regions
;
434 consolidation_map
<frame_region
> m_frame_regions
;
435 consolidation_map
<symbolic_region
> m_symbolic_regions
;
437 typedef hash_map
<tree
, string_region
*> string_map_t
;
438 string_map_t m_string_map
;
440 store_manager m_store_mgr
;
442 /* "Dynamically-allocated" region instances.
443 The number of these within the analysis can grow arbitrarily.
444 They are still owned by the manager. */
445 auto_delete_vec
<region
> m_managed_dynamic_regions
;
448 struct append_ssa_names_cb_data
;
450 /* Helper class for handling calls to functions with known behavior.
451 Implemented in region-model-impl-calls.c. */
456 call_details (const gcall
*call
, region_model
*model
,
457 region_model_context
*ctxt
);
459 region_model_context
*get_ctxt () const { return m_ctxt
; }
460 uncertainty_t
*get_uncertainty () const;
461 tree
get_lhs_type () const { return m_lhs_type
; }
462 const region
*get_lhs_region () const { return m_lhs_region
; }
464 bool maybe_set_lhs (const svalue
*result
) const;
466 unsigned num_args () const;
468 tree
get_arg_tree (unsigned idx
) const;
469 tree
get_arg_type (unsigned idx
) const;
470 const svalue
*get_arg_svalue (unsigned idx
) const;
471 const char *get_arg_string_literal (unsigned idx
) const;
473 void dump_to_pp (pretty_printer
*pp
, bool simple
) const;
474 void dump (bool simple
) const;
476 const svalue
*get_or_create_conjured_svalue (const region
*) const;
480 region_model
*m_model
;
481 region_model_context
*m_ctxt
;
483 const region
*m_lhs_region
;
486 /* A region_model encapsulates a representation of the state of memory, with
487 a tree of regions, along with their associated values.
488 The representation is graph-like because values can be pointers to
491 - a constraint_manager, capturing relationships between the values, and
492 - dynamic extents, mapping dynamically-allocated regions to svalues (their
498 typedef region_to_value_map dynamic_extents_t
;
500 region_model (region_model_manager
*mgr
);
501 region_model (const region_model
&other
);
503 region_model
&operator= (const region_model
&other
);
505 bool operator== (const region_model
&other
) const;
506 bool operator!= (const region_model
&other
) const
508 return !(*this == other
);
511 hashval_t
hash () const;
513 void print (pretty_printer
*pp
) const;
515 void dump_to_pp (pretty_printer
*pp
, bool simple
, bool multiline
) const;
516 void dump (FILE *fp
, bool simple
, bool multiline
) const;
517 void dump (bool simple
) const;
521 void validate () const;
523 void canonicalize ();
524 bool canonicalized_p () const;
527 on_stmt_pre (const gimple
*stmt
,
528 bool *out_terminate_path
,
529 bool *out_unknown_side_effects
,
530 region_model_context
*ctxt
);
532 void on_assignment (const gassign
*stmt
, region_model_context
*ctxt
);
533 const svalue
*get_gassign_result (const gassign
*assign
,
534 region_model_context
*ctxt
);
535 bool on_call_pre (const gcall
*stmt
, region_model_context
*ctxt
,
536 bool *out_terminate_path
);
537 void on_call_post (const gcall
*stmt
,
538 bool unknown_side_effects
,
539 region_model_context
*ctxt
);
541 void purge_state_involving (const svalue
*sval
, region_model_context
*ctxt
);
543 /* Specific handling for on_call_pre. */
544 bool impl_call_alloca (const call_details
&cd
);
545 void impl_call_analyzer_describe (const gcall
*call
,
546 region_model_context
*ctxt
);
547 void impl_call_analyzer_dump_capacity (const gcall
*call
,
548 region_model_context
*ctxt
);
549 void impl_call_analyzer_eval (const gcall
*call
,
550 region_model_context
*ctxt
);
551 bool impl_call_builtin_expect (const call_details
&cd
);
552 bool impl_call_calloc (const call_details
&cd
);
553 bool impl_call_error (const call_details
&cd
, unsigned min_args
,
554 bool *out_terminate_path
);
555 void impl_call_fgets (const call_details
&cd
);
556 void impl_call_fread (const call_details
&cd
);
557 void impl_call_free (const call_details
&cd
);
558 bool impl_call_malloc (const call_details
&cd
);
559 void impl_call_memcpy (const call_details
&cd
);
560 bool impl_call_memset (const call_details
&cd
);
561 void impl_call_realloc (const call_details
&cd
);
562 void impl_call_strcpy (const call_details
&cd
);
563 bool impl_call_strlen (const call_details
&cd
);
564 bool impl_call_operator_new (const call_details
&cd
);
565 bool impl_call_operator_delete (const call_details
&cd
);
566 void impl_deallocation_call (const call_details
&cd
);
568 void handle_unrecognized_call (const gcall
*call
,
569 region_model_context
*ctxt
);
570 void get_reachable_svalues (svalue_set
*out
,
571 const svalue
*extra_sval
,
572 const uncertainty_t
*uncertainty
);
574 void on_return (const greturn
*stmt
, region_model_context
*ctxt
);
575 void on_setjmp (const gcall
*stmt
, const exploded_node
*enode
,
576 region_model_context
*ctxt
);
577 void on_longjmp (const gcall
*longjmp_call
, const gcall
*setjmp_call
,
578 int setjmp_stack_depth
, region_model_context
*ctxt
);
580 void update_for_phis (const supernode
*snode
,
581 const cfg_superedge
*last_cfg_superedge
,
582 region_model_context
*ctxt
);
584 void handle_phi (const gphi
*phi
, tree lhs
, tree rhs
,
585 region_model_context
*ctxt
);
587 bool maybe_update_for_edge (const superedge
&edge
,
588 const gimple
*last_stmt
,
589 region_model_context
*ctxt
,
590 rejected_constraint
**out
);
592 const region
*push_frame (function
*fun
, const vec
<const svalue
*> *arg_sids
,
593 region_model_context
*ctxt
);
594 const frame_region
*get_current_frame () const { return m_current_frame
; }
595 function
* get_current_function () const;
596 void pop_frame (const region
*result_dst
,
597 const svalue
**out_result
,
598 region_model_context
*ctxt
);
599 int get_stack_depth () const;
600 const frame_region
*get_frame_at_index (int index
) const;
602 const region
*get_lvalue (path_var pv
, region_model_context
*ctxt
) const;
603 const region
*get_lvalue (tree expr
, region_model_context
*ctxt
) const;
604 const svalue
*get_rvalue (path_var pv
, region_model_context
*ctxt
) const;
605 const svalue
*get_rvalue (tree expr
, region_model_context
*ctxt
) const;
607 const region
*deref_rvalue (const svalue
*ptr_sval
, tree ptr_tree
,
608 region_model_context
*ctxt
) const;
610 const svalue
*get_rvalue_for_bits (tree type
,
612 const bit_range
&bits
) const;
614 void set_value (const region
*lhs_reg
, const svalue
*rhs_sval
,
615 region_model_context
*ctxt
);
616 void set_value (tree lhs
, tree rhs
, region_model_context
*ctxt
);
617 void clobber_region (const region
*reg
);
618 void purge_region (const region
*reg
);
619 void fill_region (const region
*reg
, const svalue
*sval
);
620 void zero_fill_region (const region
*reg
);
621 void mark_region_as_unknown (const region
*reg
, uncertainty_t
*uncertainty
);
623 void copy_region (const region
*dst_reg
, const region
*src_reg
,
624 region_model_context
*ctxt
);
625 tristate
eval_condition (const svalue
*lhs
,
627 const svalue
*rhs
) const;
628 tristate
eval_condition_without_cm (const svalue
*lhs
,
630 const svalue
*rhs
) const;
631 tristate
compare_initial_and_pointer (const initial_svalue
*init
,
632 const region_svalue
*ptr
) const;
633 tristate
eval_condition (tree lhs
,
636 region_model_context
*ctxt
);
637 bool add_constraint (tree lhs
, enum tree_code op
, tree rhs
,
638 region_model_context
*ctxt
);
639 bool add_constraint (tree lhs
, enum tree_code op
, tree rhs
,
640 region_model_context
*ctxt
,
641 rejected_constraint
**out
);
643 const region
*create_region_for_heap_alloc (const svalue
*size_in_bytes
);
644 const region
*create_region_for_alloca (const svalue
*size_in_bytes
);
646 tree
get_representative_tree (const svalue
*sval
) const;
648 get_representative_path_var (const svalue
*sval
,
649 svalue_set
*visited
) const;
651 get_representative_path_var (const region
*reg
,
652 svalue_set
*visited
) const;
655 constraint_manager
*get_constraints ()
657 return m_constraints
;
660 store
*get_store () { return &m_store
; }
661 const store
*get_store () const { return &m_store
; }
663 const dynamic_extents_t
&
664 get_dynamic_extents () const
666 return m_dynamic_extents
;
668 const svalue
*get_dynamic_extents (const region
*reg
) const;
669 void set_dynamic_extents (const region
*reg
,
670 const svalue
*size_in_bytes
);
671 void unset_dynamic_extents (const region
*reg
);
673 region_model_manager
*get_manager () const { return m_mgr
; }
675 void unbind_region_and_descendents (const region
*reg
,
676 enum poison_kind pkind
);
678 bool can_merge_with_p (const region_model
&other_model
,
679 const program_point
&point
,
680 region_model
*out_model
) const;
682 tree
get_fndecl_for_call (const gcall
*call
,
683 region_model_context
*ctxt
);
685 void get_ssa_name_regions_for_current_frame
686 (auto_vec
<const decl_region
*> *out
) const;
687 static void append_ssa_names_cb (const region
*base_reg
,
688 struct append_ssa_names_cb_data
*data
);
690 const svalue
*get_store_value (const region
*reg
) const;
692 bool region_exists_p (const region
*reg
) const;
694 void loop_replay_fixup (const region_model
*dst_state
);
696 const svalue
*get_capacity (const region
*reg
) const;
699 const region
*get_lvalue_1 (path_var pv
, region_model_context
*ctxt
) const;
700 const svalue
*get_rvalue_1 (path_var pv
, region_model_context
*ctxt
) const;
703 get_representative_path_var_1 (const svalue
*sval
,
704 svalue_set
*visited
) const;
706 get_representative_path_var_1 (const region
*reg
,
707 svalue_set
*visited
) const;
709 bool add_constraint (const svalue
*lhs
,
712 region_model_context
*ctxt
);
713 bool add_constraints_from_binop (const svalue
*outer_lhs
,
714 enum tree_code outer_op
,
715 const svalue
*outer_rhs
,
717 region_model_context
*ctxt
);
719 void update_for_call_superedge (const call_superedge
&call_edge
,
720 region_model_context
*ctxt
);
721 void update_for_return_superedge (const return_superedge
&return_edge
,
722 region_model_context
*ctxt
);
723 void update_for_call_summary (const callgraph_superedge
&cg_sedge
,
724 region_model_context
*ctxt
);
725 bool apply_constraints_for_gcond (const cfg_superedge
&edge
,
726 const gcond
*cond_stmt
,
727 region_model_context
*ctxt
,
728 rejected_constraint
**out
);
729 bool apply_constraints_for_gswitch (const switch_cfg_superedge
&edge
,
730 const gswitch
*switch_stmt
,
731 region_model_context
*ctxt
,
732 rejected_constraint
**out
);
733 bool apply_constraints_for_exception (const gimple
*last_stmt
,
734 region_model_context
*ctxt
,
735 rejected_constraint
**out
);
737 int poison_any_pointers_to_descendents (const region
*reg
,
738 enum poison_kind pkind
);
740 void on_top_level_param (tree param
, region_model_context
*ctxt
);
742 bool called_from_main_p () const;
743 const svalue
*get_initial_value_for_global (const region
*reg
) const;
745 const svalue
*check_for_poison (const svalue
*sval
,
747 region_model_context
*ctxt
) const;
749 void check_for_writable_region (const region
* dest_reg
,
750 region_model_context
*ctxt
) const;
752 /* Storing this here to avoid passing it around everywhere. */
753 region_model_manager
*const m_mgr
;
757 constraint_manager
*m_constraints
; // TODO: embed, rather than dynalloc?
759 const frame_region
*m_current_frame
;
761 /* Map from base region to size in bytes, for tracking the sizes of
762 dynamically-allocated regions.
763 This is part of the region_model rather than the region to allow for
764 memory regions to be resized (e.g. by realloc). */
765 dynamic_extents_t m_dynamic_extents
;
768 /* Some region_model activity could lead to warnings (e.g. attempts to use an
769 uninitialized value). This abstract base class encapsulates an interface
770 for the region model to use when emitting such warnings.
772 Having this as an abstract base class allows us to support the various
773 operations needed by program_state in the analyzer within region_model,
774 whilst keeping them somewhat modularized. */
776 class region_model_context
779 /* Hook for clients to store pending diagnostics.
780 Return true if the diagnostic was stored, or false if it was deleted. */
781 virtual bool warn (pending_diagnostic
*d
) = 0;
783 /* Hook for clients to be notified when an SVAL that was reachable
784 in a previous state is no longer live, so that clients can emit warnings
786 virtual void on_svalue_leak (const svalue
*sval
) = 0;
788 /* Hook for clients to be notified when the set of explicitly live
789 svalues changes, so that they can purge state relating to dead
791 virtual void on_liveness_change (const svalue_set
&live_svalues
,
792 const region_model
*model
) = 0;
794 virtual logger
*get_logger () = 0;
796 /* Hook for clients to be notified when the condition
797 "LHS OP RHS" is added to the region model.
798 This exists so that state machines can detect tests on edges,
799 and use them to trigger sm-state transitions (e.g. transitions due
800 to ptrs becoming known to be NULL or non-NULL, rather than just
802 virtual void on_condition (const svalue
*lhs
,
804 const svalue
*rhs
) = 0;
806 /* Hooks for clients to be notified when an unknown change happens
807 to SVAL (in response to a call to an unknown function). */
808 virtual void on_unknown_change (const svalue
*sval
, bool is_mutable
) = 0;
810 /* Hooks for clients to be notified when a phi node is handled,
811 where RHS is the pertinent argument. */
812 virtual void on_phi (const gphi
*phi
, tree rhs
) = 0;
814 /* Hooks for clients to be notified when the region model doesn't
815 know how to handle the tree code of T at LOC. */
816 virtual void on_unexpected_tree_code (tree t
,
817 const dump_location_t
&loc
) = 0;
819 /* Hook for clients to be notified when a function_decl escapes. */
820 virtual void on_escaped_function (tree fndecl
) = 0;
822 virtual uncertainty_t
*get_uncertainty () = 0;
824 /* Hook for clients to purge state involving SVAL. */
825 virtual void purge_state_involving (const svalue
*sval
) = 0;
828 /* A "do nothing" subclass of region_model_context. */
830 class noop_region_model_context
: public region_model_context
833 bool warn (pending_diagnostic
*) OVERRIDE
{ return false; }
834 void on_svalue_leak (const svalue
*) OVERRIDE
{}
835 void on_liveness_change (const svalue_set
&,
836 const region_model
*) OVERRIDE
{}
837 logger
*get_logger () OVERRIDE
{ return NULL
; }
838 void on_condition (const svalue
*lhs ATTRIBUTE_UNUSED
,
839 enum tree_code op ATTRIBUTE_UNUSED
,
840 const svalue
*rhs ATTRIBUTE_UNUSED
) OVERRIDE
843 void on_unknown_change (const svalue
*sval ATTRIBUTE_UNUSED
,
844 bool is_mutable ATTRIBUTE_UNUSED
) OVERRIDE
847 void on_phi (const gphi
*phi ATTRIBUTE_UNUSED
,
848 tree rhs ATTRIBUTE_UNUSED
) OVERRIDE
851 void on_unexpected_tree_code (tree
, const dump_location_t
&) OVERRIDE
{}
853 void on_escaped_function (tree
) OVERRIDE
{}
855 uncertainty_t
*get_uncertainty () OVERRIDE
{ return NULL
; }
857 void purge_state_involving (const svalue
*sval ATTRIBUTE_UNUSED
) OVERRIDE
{}
860 /* A subclass of region_model_context for determining if operations fail
861 e.g. "can we generate a region for the lvalue of EXPR?". */
863 class tentative_region_model_context
: public noop_region_model_context
866 tentative_region_model_context () : m_num_unexpected_codes (0) {}
868 void on_unexpected_tree_code (tree
, const dump_location_t
&)
871 m_num_unexpected_codes
++;
874 bool had_errors_p () const { return m_num_unexpected_codes
> 0; }
877 int m_num_unexpected_codes
;
880 /* A bundle of data for use when attempting to merge two region_model
881 instances to make a third. */
885 model_merger (const region_model
*model_a
,
886 const region_model
*model_b
,
887 const program_point
&point
,
888 region_model
*merged_model
)
889 : m_model_a (model_a
), m_model_b (model_b
),
891 m_merged_model (merged_model
)
895 void dump_to_pp (pretty_printer
*pp
, bool simple
) const;
896 void dump (FILE *fp
, bool simple
) const;
897 void dump (bool simple
) const;
899 region_model_manager
*get_manager () const
901 return m_model_a
->get_manager ();
904 const region_model
*m_model_a
;
905 const region_model
*m_model_b
;
906 const program_point
&m_point
;
907 region_model
*m_merged_model
;
910 /* A record that can (optionally) be written out when
911 region_model::add_constraint fails. */
913 struct rejected_constraint
915 rejected_constraint (const region_model
&model
,
916 tree lhs
, enum tree_code op
, tree rhs
)
917 : m_model (model
), m_lhs (lhs
), m_op (op
), m_rhs (rhs
)
920 void dump_to_pp (pretty_printer
*pp
) const;
922 region_model m_model
;
928 /* A bundle of state. */
933 region_model_manager
*get_model_manager () { return &m_mgr
; }
935 void log_stats (logger
*logger
) const;
938 region_model_manager m_mgr
;
944 extern void debug (const region_model
&rmodel
);
952 using namespace ::selftest
;
954 /* An implementation of region_model_context for use in selftests, which
955 stores any pending_diagnostic instances passed to it. */
957 class test_region_model_context
: public noop_region_model_context
960 bool warn (pending_diagnostic
*d
) FINAL OVERRIDE
962 m_diagnostics
.safe_push (d
);
966 unsigned get_num_diagnostics () const { return m_diagnostics
.length (); }
968 void on_unexpected_tree_code (tree t
, const dump_location_t
&)
971 internal_error ("unhandled tree code: %qs",
972 get_tree_code_name (TREE_CODE (t
)));
976 /* Implicitly delete any diagnostics in the dtor. */
977 auto_delete_vec
<pending_diagnostic
> m_diagnostics
;
980 /* Attempt to add the constraint (LHS OP RHS) to MODEL.
981 Verify that MODEL remains satisfiable. */
983 #define ADD_SAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
984 SELFTEST_BEGIN_STMT \
985 bool sat = (MODEL).add_constraint (LHS, OP, RHS, NULL); \
989 /* Attempt to add the constraint (LHS OP RHS) to MODEL.
990 Verify that the result is not satisfiable. */
992 #define ADD_UNSAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
993 SELFTEST_BEGIN_STMT \
994 bool sat = (MODEL).add_constraint (LHS, OP, RHS, NULL); \
995 ASSERT_FALSE (sat); \
998 /* Implementation detail of the ASSERT_CONDITION_* macros. */
1000 void assert_condition (const location
&loc
,
1001 region_model
&model
,
1002 const svalue
*lhs
, tree_code op
, const svalue
*rhs
,
1005 void assert_condition (const location
&loc
,
1006 region_model
&model
,
1007 tree lhs
, tree_code op
, tree rhs
,
1010 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1013 #define ASSERT_CONDITION_TRUE(REGION_MODEL, LHS, OP, RHS) \
1014 SELFTEST_BEGIN_STMT \
1015 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1016 tristate (tristate::TS_TRUE)); \
1019 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1022 #define ASSERT_CONDITION_FALSE(REGION_MODEL, LHS, OP, RHS) \
1023 SELFTEST_BEGIN_STMT \
1024 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1025 tristate (tristate::TS_FALSE)); \
1028 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1031 #define ASSERT_CONDITION_UNKNOWN(REGION_MODEL, LHS, OP, RHS) \
1032 SELFTEST_BEGIN_STMT \
1033 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1034 tristate (tristate::TS_UNKNOWN)); \
1037 } /* end of namespace selftest. */
1039 #endif /* #if CHECKING_P */
1043 #endif /* GCC_ANALYZER_REGION_MODEL_H */