1 /* Utility functions for the analyzer.
2 Copyright (C) 2019-2022 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_ANALYZER_H
22 #define GCC_ANALYZER_ANALYZER_H
28 /* Forward decls of common types, with indentation to show inheritance. */
34 class switch_cfg_superedge
;
35 class callgraph_superedge
;
37 class return_superedge
;
41 class constant_svalue
;
43 class poisoned_svalue
;
49 class repeated_svalue
;
50 class bits_within_svalue
;
51 class unmergeable_svalue
;
52 class placeholder_svalue
;
53 class widening_svalue
;
54 class compound_svalue
;
55 class conjured_svalue
;
56 class asm_output_svalue
;
57 class const_fn_result_svalue
;
58 typedef hash_set
<const svalue
*> svalue_set
;
61 class function_region
;
64 class symbolic_region
;
71 class bit_range_region
;
72 class region_model_manager
;
77 class region_model_context
;
78 class impl_region_model_context
;
80 class rejected_constraint
;
81 class constraint_manager
;
83 class reachable_regions
;
85 class bounded_ranges_manager
;
87 class pending_diagnostic
;
88 class state_change_event
;
90 class extrinsic_state
;
99 class feasibility_problem
;
100 class exploded_cluster
;
103 class state_purge_map
;
104 class state_purge_per_ssa_name
;
113 /* Forward decls of functions. */
115 extern void dump_tree (pretty_printer
*pp
, tree t
);
116 extern void dump_quoted_tree (pretty_printer
*pp
, tree t
);
117 extern void print_quoted_type (pretty_printer
*pp
, tree t
);
118 extern int readability_comparator (const void *p1
, const void *p2
);
119 extern int tree_cmp (const void *p1
, const void *p2
);
120 extern tree
fixup_tree_for_diagnostic (tree
);
121 extern tree
get_diagnostic_tree_for_gassign (const gassign
*);
123 /* A tree, extended with stack frame information for locals, so that
124 we can distinguish between different values of locals within a potentially
125 recursive callstack. */
130 path_var (tree t
, int stack_depth
)
131 : m_tree (t
), m_stack_depth (stack_depth
)
133 // TODO: ignore stack depth for globals and constants
136 bool operator== (const path_var
&other
) const
138 return (m_tree
== other
.m_tree
139 && m_stack_depth
== other
.m_stack_depth
);
142 operator bool () const
144 return m_tree
!= NULL_TREE
;
147 void dump (pretty_printer
*pp
) const;
150 int m_stack_depth
; // or -1 for globals?
153 typedef offset_int bit_offset_t
;
154 typedef offset_int bit_size_t
;
155 typedef offset_int byte_offset_t
;
156 typedef offset_int byte_size_t
;
158 extern bool int_size_in_bits (const_tree type
, bit_size_t
*out
);
160 extern tree
get_field_at_bit_offset (tree record_type
, bit_offset_t bit_offset
);
162 /* The location of a region expressesd as an offset relative to a
168 static region_offset
make_concrete (const region
*base_region
,
171 return region_offset (base_region
, offset
, false);
173 static region_offset
make_symbolic (const region
*base_region
)
175 return region_offset (base_region
, 0, true);
178 const region
*get_base_region () const { return m_base_region
; }
180 bool symbolic_p () const { return m_is_symbolic
; }
182 bit_offset_t
get_bit_offset () const
184 gcc_assert (!symbolic_p ());
188 bool operator== (const region_offset
&other
) const
190 return (m_base_region
== other
.m_base_region
191 && m_offset
== other
.m_offset
192 && m_is_symbolic
== other
.m_is_symbolic
);
196 region_offset (const region
*base_region
, bit_offset_t offset
,
198 : m_base_region (base_region
), m_offset (offset
), m_is_symbolic (is_symbolic
)
201 const region
*m_base_region
;
202 bit_offset_t m_offset
;
206 extern location_t
get_stmt_location (const gimple
*stmt
, function
*fun
);
208 extern bool compat_types_p (tree src_type
, tree dst_type
);
210 /* Passed by pointer to PLUGIN_ANALYZER_INIT callbacks. */
212 class plugin_analyzer_init_iface
215 virtual void register_state_machine (state_machine
*) = 0;
216 virtual logger
*get_logger () const = 0;
219 /* An enum for describing the direction of an access to memory. */
221 enum access_direction
227 /* Abstract base class for associating custom data with an
228 exploded_edge, for handling non-standard edges such as
229 rewinding from a longjmp, signal handlers, etc.
230 Also used when "bifurcating" state: splitting the execution
231 path in non-standard ways (e.g. for simulating the various
232 outcomes of "realloc"). */
234 class custom_edge_info
237 virtual ~custom_edge_info () {}
239 /* Hook for making .dot label more readable. */
240 virtual void print (pretty_printer
*pp
) const = 0;
242 /* Hook for updating MODEL within exploded_path::feasible_p
243 and when handling bifurcation. */
244 virtual bool update_model (region_model
*model
,
245 const exploded_edge
*eedge
,
246 region_model_context
*ctxt
) const = 0;
248 virtual void add_events_to_path (checker_path
*emission_path
,
249 const exploded_edge
&eedge
) const = 0;
252 /* Abstract base class for splitting state.
254 Most of the state-management code in the analyzer involves
255 modifying state objects in-place, which assumes a single outcome.
257 This class provides an escape hatch to allow for multiple outcomes
258 for such updates e.g. for modelling multiple outcomes from function
259 calls, such as the various outcomes of "realloc". */
264 virtual ~path_context () {}
266 /* Hook for clients to split state with a non-standard path.
267 Take ownership of INFO. */
268 virtual void bifurcate (custom_edge_info
*info
) = 0;
270 /* Hook for clients to terminate the standard path. */
271 virtual void terminate_path () = 0;
273 /* Hook for clients to determine if the standard path has been
275 virtual bool terminate_path_p () const = 0;
280 extern bool is_special_named_call_p (const gcall
*call
, const char *funcname
,
281 unsigned int num_args
);
282 extern bool is_named_call_p (const_tree fndecl
, const char *funcname
);
283 extern bool is_named_call_p (const_tree fndecl
, const char *funcname
,
284 const gcall
*call
, unsigned int num_args
);
285 extern bool is_std_named_call_p (const_tree fndecl
, const char *funcname
);
286 extern bool is_std_named_call_p (const_tree fndecl
, const char *funcname
,
287 const gcall
*call
, unsigned int num_args
);
288 extern bool is_setjmp_call_p (const gcall
*call
);
289 extern bool is_longjmp_call_p (const gcall
*call
);
291 extern const char *get_user_facing_name (const gcall
*call
);
293 extern void register_analyzer_pass ();
295 extern label_text
make_label_text (bool can_colorize
, const char *fmt
, ...);
297 extern bool fndecl_has_gimple_body_p (tree fndecl
);
299 /* An RAII-style class for pushing/popping cfun within a scope.
300 Doing so ensures we get "In function " announcements
301 from the diagnostics subsystem. */
306 auto_cfun (function
*fun
) { push_cfun (fun
); }
307 ~auto_cfun () { pop_cfun (); }
310 /* A template for creating hash traits for a POD type. */
312 template <typename Type
>
313 struct pod_hash_traits
: typed_noop_remove
<Type
>
315 typedef Type value_type
;
316 typedef Type compare_type
;
317 static inline hashval_t
hash (value_type
);
318 static inline bool equal (const value_type
&existing
,
319 const value_type
&candidate
);
320 static inline void mark_deleted (Type
&);
321 static inline void mark_empty (Type
&);
322 static inline bool is_deleted (Type
);
323 static inline bool is_empty (Type
);
326 /* A hash traits class that uses member functions to implement
327 the various required ops. */
329 template <typename Type
>
330 struct member_function_hash_traits
: public typed_noop_remove
<Type
>
332 typedef Type value_type
;
333 typedef Type compare_type
;
334 static inline hashval_t
hash (value_type v
) { return v
.hash (); }
335 static inline bool equal (const value_type
&existing
,
336 const value_type
&candidate
)
338 return existing
== candidate
;
340 static inline void mark_deleted (Type
&t
) { t
.mark_deleted (); }
341 static inline void mark_empty (Type
&t
) { t
.mark_empty (); }
342 static inline bool is_deleted (Type t
) { return t
.is_deleted (); }
343 static inline bool is_empty (Type t
) { return t
.is_empty (); }
346 /* A map from T::key_t to T* for use in consolidating instances of T.
347 Owns all instances of T.
348 T::key_t should have operator== and be hashable. */
350 template <typename T
>
351 class consolidation_map
354 typedef typename
T::key_t key_t
;
355 typedef T instance_t
;
356 typedef hash_map
<key_t
, instance_t
*> inner_map_t
;
357 typedef typename
inner_map_t::iterator iterator
;
359 /* Delete all instances of T. */
361 ~consolidation_map ()
363 for (typename
inner_map_t::iterator iter
= m_inner_map
.begin ();
364 iter
!= m_inner_map
.end (); ++iter
)
365 delete (*iter
).second
;
368 /* Get the instance of T for K if one exists, or NULL. */
370 T
*get (const key_t
&k
) const
372 if (instance_t
**slot
= const_cast<inner_map_t
&> (m_inner_map
).get (k
))
377 /* Take ownership of INSTANCE. */
379 void put (const key_t
&k
, T
*instance
)
381 m_inner_map
.put (k
, instance
);
384 size_t elements () const { return m_inner_map
.elements (); }
386 iterator
begin () const { return m_inner_map
.begin (); }
387 iterator
end () const { return m_inner_map
.end (); }
390 inner_map_t m_inner_map
;
393 /* Disable -Wformat-diag; we want to be able to use pp_printf
394 for logging/dumping without complying with the rules for diagnostics. */
396 #pragma GCC diagnostic ignored "-Wformat-diag"
400 extern void sorry_no_analyzer ();
401 #endif /* #if !ENABLE_ANALYZER */
403 #endif /* GCC_ANALYZER_ANALYZER_H */