Line data Source code
1 : /* Regions 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_H
22 : #define GCC_ANALYZER_REGION_H
23 :
24 : #include "analyzer/symbolic-nodes.h"
25 : #include "analyzer/store.h"
26 : #include "analyzer/concrete-ranges.h"
27 : #include "analyzer/symbolic-memory-offsets.h"
28 : #include "analyzer/symbolic-memory-ranges.h"
29 : #include "analyzer/symbolic-pointers.h"
30 :
31 : namespace ana {
32 :
33 : /* An enum for identifying different spaces within memory. */
34 :
35 : enum class memory_space
36 : {
37 : unknown,
38 : code,
39 : globals,
40 : stack,
41 : heap,
42 : read_only_data,
43 : thread_local_,
44 : private_
45 : };
46 :
47 : extern const char *
48 : get_dump_name (memory_space);
49 :
50 : /* An enum for discriminating between the different concrete subclasses
51 : of region. */
52 :
53 : enum region_kind
54 : {
55 : RK_FRAME,
56 : RK_MEMORY_SPACE,
57 : RK_FUNCTION,
58 : RK_LABEL,
59 : RK_ROOT,
60 : RK_SYMBOLIC,
61 : RK_DECL,
62 : RK_FIELD,
63 : RK_ELEMENT,
64 : RK_OFFSET,
65 : RK_SIZED,
66 : RK_CAST,
67 : RK_HEAP_ALLOCATED,
68 : RK_ALLOCA,
69 : RK_STRING,
70 : RK_BIT_RANGE,
71 : RK_VAR_ARG,
72 : RK_ERRNO,
73 : RK_PRIVATE,
74 : RK_UNKNOWN,
75 : };
76 :
77 : /* Region and its subclasses.
78 :
79 : The class hierarchy looks like this (using indentation to show
80 : inheritance, and with region_kinds shown for the concrete subclasses):
81 :
82 : region
83 : frame_region (RK_FRAME): a function frame on the stack
84 : memory_space_region (RK_MEMORY_SPACE): a region for a particular
85 : memory_space
86 : root_region (RK_ROOT): the top-level region
87 : cluster_region
88 : alloca_region (RK_ALLOCA): an untyped region dynamically allocated on the
89 : stack via "alloca"
90 : decl_region (RK_DECL): the memory occupied by a particular global, local,
91 : or SSA name
92 : errno_region (RK_ERRNO): a region for holding "errno"
93 : function_region (RK_FUNCTION): the code for a particular function
94 : heap_allocated_region (RK_HEAP_ALLOCATED): an untyped region dynamically
95 : allocated on the heap via
96 : "malloc" or similar
97 : label_region (RK_LABEL): a particular label within a function
98 : private_region (RK_PRIVATE): a region for internal state of an API
99 : string_region (RK_STRING): a region for a STRING_CST
100 : symbolic_region (RK_SYMBOLIC): dereferencing a symbolic pointer
101 : var_arg_region (RK_VAR_ARG): a region for the N-th vararg within a
102 : frame_region for a variadic call
103 : unknown_region (RK_UNKNOWN): for handling unimplemented tree codes.
104 : field_region (RK_FIELD): the memory occupied by a field within a struct
105 : or union
106 : element_region (RK_ELEMENT): an element within an array
107 : offset_region (RK_OFFSET): a byte-offset within another region, for
108 : handling pointer arithmetic as a region
109 : sized_region (RK_SIZED): a subregion of symbolic size (in bytes)
110 : within its parent
111 : cast_region (RK_CAST): a region that views another region using a
112 : different type
113 : bit_range_region (RK_BIT_RANGE): a region for a specific range of bits
114 : within another region.
115 :
116 : For this example code:
117 :
118 : . extern int arr[10];
119 : . void foo ()
120 : . {
121 : . int *p = (int *)malloc (sizeof (int));
122 : . [...etc...].
123 :
124 : the *object* hierarchy, in terms of the ancestry expressed by the
125 : get_parent () pointers of region *instances*, might look something like
126 : this:
127 :
128 : . root (RK_ROOT)
129 : . +- memory_space_region for memory_space::globals (RK_MEMORY_SPACE)
130 : . | | (all descendents have m_mem_space == memory_space::globals)
131 : . | `- [C] decl_region for 'arr[10]' (RK_DECL)
132 : . | `- element_region for 'arr[3]' (RK_ELEMENT)
133 : . +- memory_space_region for memory_space::heap (RK_MEMORY_SPACE)
134 : . | | (all descendents have m_mem_space == memory_space::heap)
135 : . | `- [C] heap_allocated_region for '*p'
136 : . +- memory_space_region for memory_space::stack (RK_MEMORY_SPACE)
137 : . | | (all descendents have m_mem_space == memory_space::stack)
138 : . | +- frame_region (RK_FRAME)
139 : . | +- frame_region (RK_FRAME)
140 : . | `- frame_region (RK_FRAME)
141 : . | `- [C] decl_region for 'p' (RK_DECL)
142 :
143 : where [C] denotes a cluster region. */
144 :
145 : /* Abstract base class for representing ways of accessing chunks of memory.
146 :
147 : Regions form a tree-like hierarchy, with a root region at the base,
148 : with memory space regions within it, representing the stack and
149 : globals, with frames within the stack, and regions for variables
150 : within the frames and the "globals" region. Regions for structs
151 : can have subregions for fields. */
152 :
153 : class region : public symbolic::node
154 : {
155 : public:
156 : using symbolic_bit_count = symbolic::scalar<integer_with_unit<units::bit>>;
157 : using symbolic_byte_count = symbolic::scalar<integer_with_unit<units::byte>>;
158 :
159 : virtual ~region ();
160 :
161 : virtual enum region_kind get_kind () const = 0;
162 : virtual const frame_region *
163 1355485 : dyn_cast_frame_region () const { return nullptr; }
164 : virtual const function_region *
165 22987 : dyn_cast_function_region () const { return nullptr; }
166 : virtual const symbolic_region *
167 15377285 : dyn_cast_symbolic_region () const { return nullptr; }
168 : virtual const decl_region *
169 566322 : dyn_cast_decl_region () const { return nullptr; }
170 : virtual const field_region *
171 0 : dyn_cast_field_region () const { return nullptr; }
172 : virtual const element_region *
173 34920 : dyn_cast_element_region () const { return nullptr; }
174 : virtual const offset_region *
175 32121 : dyn_cast_offset_region () const { return nullptr; }
176 : virtual const sized_region *
177 0 : dyn_cast_sized_region () const { return nullptr; }
178 : virtual const cast_region *
179 47163 : dyn_cast_cast_region () const { return nullptr; }
180 : virtual const string_region *
181 12290 : dyn_cast_string_region () const { return nullptr; }
182 : virtual const bit_range_region *
183 0 : dyn_cast_bit_range_region () const { return nullptr; }
184 : virtual const var_arg_region *
185 577 : dyn_cast_var_arg_region () const { return nullptr; }
186 :
187 : void print_dump_widget_label (pretty_printer *pp) const final override;
188 : virtual void print_region_for_dump_widget_label (pretty_printer *pp) const = 0;
189 : void
190 : add_dump_widget_children (text_art::tree_widget &,
191 : symbolic::node_dump_state &nds) const final override;
192 : const char *
193 28620 : get_any_dump_color () const override { return "region"; }
194 :
195 : virtual void accept (visitor *v) const;
196 :
197 83652210 : const region *get_parent_region () const { return m_parent; }
198 17329927 : const cluster_region *get_base_region () const { return m_cluster_reg; }
199 1327024 : const cluster_region *get_cluster_region () const { return m_cluster_reg; }
200 : bool base_region_p () const;
201 : bool descendent_of_p (const region *elder) const;
202 : const frame_region *maybe_get_frame_region () const;
203 6627401 : memory_space get_memory_space () const { return m_mem_space; }
204 : bool can_have_initial_svalue_p () const;
205 : const svalue *get_initial_value_at_main (region_model_manager *mgr) const;
206 :
207 : tree maybe_get_decl () const;
208 :
209 8484319 : tree get_type () const { return m_type; }
210 :
211 : void print (const region_model &model,
212 : pretty_printer *pp) const;
213 : label_text get_desc (bool simple=true) const;
214 :
215 : virtual void dump_to_pp (pretty_printer *pp, bool simple) const = 0;
216 : void dump (bool simple) const;
217 : void dump () const;
218 :
219 : bool maybe_print_for_user (pretty_printer *pp,
220 : const region_model &model) const;
221 :
222 : bool non_null_p () const;
223 :
224 : static int cmp_ptr_ptr (const void *, const void *);
225 :
226 : symbolic::memory_offset
227 : get_memory_offset (region_model_manager &mgr) const;
228 :
229 : symbolic::memory_range
230 : get_memory_range (region_model_manager &mgr) const;
231 :
232 : symbolic::pointer
233 : get_pointer (region_model_manager &mgr) const;
234 :
235 : /* Attempt to get the size of this region in bytes.
236 : This is the accessed size, not necessarily the size that's valid to
237 : access. */
238 : virtual const symbolic_byte_size *
239 : maybe_get_byte_size (symbolic::scalar_manager<concrete_byte_size> &byte_mgr) const;
240 :
241 : /* Attempt to get the size of this region in bits.
242 : This is the accessed size, not necessarily the size that's valid to
243 : access. */
244 : virtual const symbolic_bit_size *
245 : maybe_get_bit_size (symbolic::scalar_manager<concrete_bit_size> &bit_mgr) const;
246 :
247 : /* Get a symbolic value describing the size of this region in bytes
248 : (which could be "unknown").
249 : This is the accessed size, not necessarily the size that's valid to
250 : access. */
251 : const svalue *get_byte_size_sval (region_model_manager *mgr) const;
252 :
253 : virtual const symbolic_bit_count *
254 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr) const;
255 :
256 : virtual const symbolic_byte_count *
257 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr) const;
258 :
259 : void
260 : get_subregions_for_bit_range (region_model_manager *mgr,
261 : concrete_bit_range relative_bit_range,
262 : tree type,
263 : auto_vec <const region *> *out) const;
264 :
265 : bool symbolic_for_unknown_ptr_p () const;
266 :
267 : bool symbolic_p () const;
268 :
269 : /* For most base regions it makes sense to track the bindings of the region
270 : within the store. As an optimization, some are not tracked (to avoid
271 : bloating the store object with redundant binding clusters). */
272 474973 : virtual bool tracked_p () const { return true; }
273 :
274 : bool is_named_decl_p (const char *decl_name) const;
275 :
276 : bool empty_p (region_model_manager &mgr) const;
277 :
278 : protected:
279 : region (complexity c,
280 : symbolic::node::id_t id,
281 : const region *parent,
282 : const cluster_region *cluster_reg,
283 : memory_space mem_space,
284 : tree type);
285 :
286 : std::unique_ptr<json::object>
287 : make_json_obj (const char *type_name) const;
288 :
289 : private:
290 : symbolic::memory_offset
291 : calc_memory_offset (symbolic::node_manager &mgr) const;
292 :
293 : symbolic::memory_range
294 : calc_memory_range (symbolic::node_manager &mgr) const;
295 :
296 : symbolic::pointer
297 : calc_pointer (symbolic::node_manager &mgr) const;
298 :
299 : const svalue *calc_initial_value_at_main (region_model_manager *mgr) const;
300 :
301 : virtual void
302 : add_dump_widget_children_for_region (text_art::tree_widget &,
303 : symbolic::node_dump_state &nds) const;
304 :
305 : /* The immediate parent (if any) of this node. */
306 : const region *m_parent;
307 :
308 : /* The region within the ancestry of this region that identifies which
309 : cluster (if any) they should be partitioned into within the store.
310 :
311 : (This is sometimes called the "base region" in existing code, but that
312 : term is confusing so I'm deprecating it)
313 :
314 : Regions within the same struct/union/array are in the same cluster.
315 : Different decls are in different clusters.
316 : Instances of cluster_region have "this" as their m_cluster_reg.
317 : Nodes above the level of clusters (e.g. memory_space_region) have
318 : nullptr for their m_cluster_reg. */
319 : const cluster_region *m_cluster_reg;
320 :
321 : memory_space m_mem_space;
322 :
323 : tree m_type;
324 :
325 : mutable std::unique_ptr<symbolic::memory_offset> m_cached_memory_offset;
326 : mutable std::unique_ptr<symbolic::memory_range> m_cached_memory_range;
327 : mutable std::unique_ptr<symbolic::pointer> m_cached_pointer;
328 :
329 : /* For regions within a global decl, a cache of the svalue for the initial
330 : value of this region when the program starts. */
331 : mutable const svalue *m_cached_init_sval_at_main;
332 : };
333 :
334 : } // namespace ana
335 :
336 : template <>
337 : template <>
338 : inline bool
339 : is_a_helper <const region *>::test (const region *)
340 : {
341 : return true;
342 : }
343 :
344 : namespace ana {
345 :
346 : /* Concrete subclass of region, for regions that represent an untyped
347 : space within memory, such as the stack or the heap.
348 : Child regions "inherit" its memory_space value. */
349 :
350 13485 : class memory_space_region : public region
351 : {
352 : public:
353 31465 : memory_space_region (symbolic::node::id_t id, const region *parent,
354 : memory_space mem_space)
355 31465 : : region (complexity (parent), id, parent,
356 : nullptr, // no cluster
357 : mem_space,
358 31465 : NULL_TREE) // no type
359 31465 : {}
360 :
361 : /* region vfuncs. */
362 : enum region_kind
363 1342276 : get_kind () const final override { return RK_MEMORY_SPACE; }
364 :
365 : void
366 : dump_to_pp (pretty_printer *pp, bool simple) const final override;
367 :
368 : void
369 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
370 :
371 : void
372 : accept_node_visitor (symbolic::node_visitor &v) const final override;
373 :
374 : std::unique_ptr<json::object>
375 : to_json () const final override;
376 :
377 : std::unique_ptr<diagnostics::markup::node>
378 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
379 : };
380 :
381 : } // namespace ana
382 :
383 : template <>
384 : template <>
385 : inline bool
386 : is_a_helper <const memory_space_region *>::test (const region *reg)
387 : {
388 : return reg->get_kind () == RK_MEMORY_SPACE;
389 : }
390 :
391 : namespace ana {
392 :
393 : /* Concrete region subclass, representing a function frame on the stack,
394 : to contain the locals.
395 : The parent is the stack region; there's also a hierarchy of call-stack
396 : prefixes expressed via m_calling_frame.
397 : For example, given "oldest" calling "middle" called "newest" we would have
398 : - a stack depth of 3
399 : - frame (A) for "oldest" with index 0 for depth 1, calling_frame == NULL
400 : - frame (B) for "middle" with index 1 for depth 2, calling_frame == (A)
401 : - frame (C) for "newest" with index 2 for depth 3, calling_frame == (B)
402 : where the parent region for each of the frames is the "stack" region.
403 : The index is the count of frames earlier than this in the stack. */
404 :
405 : class frame_region : public region
406 : {
407 : public:
408 : /* A support class for uniquifying instances of frame_region. */
409 : struct key_t
410 : {
411 42017 : key_t (const frame_region *calling_frame, const function &fun)
412 42017 : : m_calling_frame (calling_frame), m_fun (&fun)
413 : {
414 : /* calling_frame can be nullptr. */
415 : }
416 :
417 286244 : hashval_t hash () const
418 : {
419 286244 : inchash::hash hstate;
420 286244 : hstate.add_ptr (m_calling_frame);
421 286244 : hstate.add_ptr (m_fun);
422 286244 : return hstate.end ();
423 : }
424 :
425 257310 : bool operator== (const key_t &other) const
426 : {
427 257310 : return (m_calling_frame == other.m_calling_frame
428 257310 : && m_fun == other.m_fun);
429 : }
430 :
431 : void mark_deleted () { m_fun = reinterpret_cast<function *> (1); }
432 0 : void mark_empty () { m_fun = nullptr; }
433 320514 : bool is_deleted () const
434 : {
435 320514 : return m_fun == reinterpret_cast<function *> (1);
436 : }
437 1294616 : bool is_empty () const { return m_fun == nullptr; }
438 :
439 : const frame_region *m_calling_frame;
440 : const function *m_fun;
441 : };
442 :
443 18966 : frame_region (symbolic::node::id_t id,
444 : const region &parent,
445 : const frame_region *calling_frame,
446 : const function &fun,
447 : int index)
448 18966 : : region (complexity (&parent),
449 : id,
450 : &parent,
451 : nullptr, // no cluster
452 : parent.get_memory_space (),
453 : NULL_TREE),
454 18966 : m_calling_frame (calling_frame),
455 18966 : m_fun (fun), m_index (index)
456 18966 : {}
457 : ~frame_region ();
458 :
459 : /* region vfuncs. */
460 3786732 : enum region_kind get_kind () const final override { return RK_FRAME; }
461 1052928 : const frame_region * dyn_cast_frame_region () const final override
462 : {
463 1052928 : return this;
464 : }
465 : void accept (visitor *v) const final override;
466 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
467 :
468 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
469 : void
470 : accept_node_visitor (symbolic::node_visitor &v) const final override;
471 :
472 : std::unique_ptr<json::object>
473 : to_json () const final override;
474 :
475 : std::unique_ptr<diagnostics::markup::node>
476 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
477 :
478 : /* Accessors. */
479 4428193 : const frame_region *get_calling_frame () const { return m_calling_frame; }
480 4711050 : const function &get_function () const { return m_fun; }
481 1042 : tree get_fndecl () const { return get_function ().decl; }
482 13252909 : int get_index () const { return m_index; }
483 4983419 : int get_stack_depth () const { return m_index + 1; }
484 :
485 : const decl_region *
486 : get_region_for_local (region_model_manager *mgr,
487 : tree expr,
488 : const region_model_context *ctxt) const;
489 :
490 6 : unsigned get_num_locals () const { return m_locals.elements (); }
491 :
492 : /* Implemented in region-model-manager.cc. */
493 : void dump_untracked_regions () const;
494 :
495 : private:
496 : const frame_region *m_calling_frame;
497 : const function &m_fun;
498 : int m_index;
499 :
500 : /* The regions for the decls within this frame are managed by this
501 : object, rather than the region_model_manager, to make it a simple
502 : lookup by tree. */
503 : typedef hash_map<tree, decl_region *> map_t;
504 : map_t m_locals;
505 : };
506 :
507 : } // namespace ana
508 :
509 : template <>
510 : template <>
511 : inline bool
512 625 : is_a_helper <const frame_region *>::test (const region *reg)
513 : {
514 625 : return reg->get_kind () == RK_FRAME;
515 : }
516 :
517 : template <> struct default_hash_traits<frame_region::key_t>
518 : : public member_function_hash_traits<frame_region::key_t>
519 : {
520 : static const bool empty_zero_p = true;
521 : };
522 :
523 : namespace ana {
524 :
525 : /* Abstract subclass of region for those regions that are the "top-level"
526 : within the store and thus get their own binding_cluster.
527 : All regions within the same struct/union/array are in the same cluster,
528 : whereas different decls are in different clusters.
529 :
530 : (This is sometimes called the "base region" in existing code, but that
531 : term is confusing so I'm deprecating it)
532 :
533 : A cluster_region and its descendents will all have "this" as the result
534 : of their get_cluster_region (). */
535 :
536 4495 : class cluster_region : public region
537 : {
538 : protected:
539 158741 : cluster_region (complexity c, symbolic::node::id_t id,
540 : const region &parent, tree type)
541 : : region (c, id, &parent,
542 : this, // this is its own cluster_region
543 : parent.get_memory_space (),
544 158741 : type)
545 : {}
546 :
547 : /* An overload for specifying a memory space, rather than
548 : reusing the parent's. */
549 9901 : cluster_region (complexity c, symbolic::node::id_t id,
550 : const region &parent,
551 : memory_space mem_space,
552 : tree type)
553 : : region (c, id, &parent,
554 : this, // this is its own cluster_region
555 : mem_space,
556 9901 : type)
557 : {}
558 : };
559 :
560 : inline bool
561 214560 : region::base_region_p () const
562 : {
563 214560 : return m_cluster_reg == this;
564 : }
565 :
566 : /* Concrete cluster_region subclass. A region representing the code for
567 : a particular function. */
568 :
569 : class function_region : public cluster_region
570 : {
571 : public:
572 10711 : function_region (symbolic::node::id_t id,
573 : const memory_space_region &parent,
574 : tree fndecl)
575 10711 : : cluster_region (complexity (&parent), id, parent,
576 10711 : TREE_TYPE (fndecl)),
577 10711 : m_fndecl (fndecl)
578 : {
579 10711 : gcc_assert (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (fndecl)));
580 10711 : }
581 :
582 : /* region vfuncs. */
583 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
584 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
585 : void
586 : accept_node_visitor (symbolic::node_visitor &v) const final override;
587 : std::unique_ptr<json::object>
588 : to_json () const final override;
589 :
590 : std::unique_ptr<diagnostics::markup::node>
591 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
592 :
593 34272 : enum region_kind get_kind () const final override { return RK_FUNCTION; }
594 : const function_region *
595 892105 : dyn_cast_function_region () const final override{ return this; }
596 :
597 891812 : tree get_fndecl () const { return m_fndecl; }
598 :
599 : private:
600 : tree m_fndecl;
601 : };
602 :
603 : } // namespace ana
604 :
605 : template <>
606 : template <>
607 : inline bool
608 13 : is_a_helper <const function_region *>::test (const region *reg)
609 : {
610 13 : return reg->get_kind () == RK_FUNCTION;
611 : }
612 :
613 : namespace ana {
614 :
615 : /* Concrete cluster_region subclass. A region representing a particular label
616 : within a function. */
617 :
618 : class label_region : public cluster_region
619 : {
620 : public:
621 51 : label_region (symbolic::node::id_t id,
622 : const function_region &parent,
623 : tree label)
624 51 : : cluster_region (complexity (&parent), id, parent, NULL_TREE),
625 51 : m_label (label)
626 : {
627 51 : gcc_assert (TREE_CODE (label) == LABEL_DECL);
628 51 : }
629 :
630 : /* region vfuncs. */
631 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
632 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
633 : void
634 : accept_node_visitor (symbolic::node_visitor &v) const final override;
635 : std::unique_ptr<json::object>
636 : to_json () const final override;
637 : std::unique_ptr<diagnostics::markup::node>
638 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
639 :
640 264 : enum region_kind get_kind () const final override { return RK_LABEL; }
641 :
642 8 : tree get_label () const { return m_label; }
643 :
644 : private:
645 : tree m_label;
646 : };
647 :
648 : } // namespace ana
649 :
650 : template <>
651 : template <>
652 : inline bool
653 8 : is_a_helper <const label_region *>::test (const region *reg)
654 : {
655 8 : return reg->get_kind () == RK_LABEL;
656 : }
657 :
658 : namespace ana {
659 :
660 : /* Concrete region subclass. The root region, containing all regions
661 : (either directly, or as descendents).
662 : Unique within a region_model_manager. */
663 :
664 4495 : class root_region : public region
665 : {
666 : public:
667 : root_region (symbolic::node::id_t id);
668 :
669 1704076 : enum region_kind get_kind () const final override { return RK_ROOT; }
670 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
671 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
672 :
673 : void
674 : accept_node_visitor (symbolic::node_visitor &v) const final override;
675 :
676 : std::unique_ptr<json::object>
677 : to_json () const final override;
678 :
679 : std::unique_ptr<diagnostics::markup::node>
680 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
681 : };
682 :
683 : } // namespace ana
684 :
685 : template <>
686 : template <>
687 : inline bool
688 : is_a_helper <const root_region *>::test (const region *reg)
689 : {
690 : return reg->get_kind () == RK_ROOT;
691 : }
692 :
693 : namespace ana {
694 :
695 : /* Concrete region subclass: a region to use when dereferencing an unknown
696 : pointer. */
697 :
698 : class symbolic_region : public cluster_region
699 : {
700 : public:
701 : /* A support class for uniquifying instances of symbolic_region. */
702 : struct key_t
703 : {
704 74263 : key_t (const region *parent, const svalue *sval_ptr)
705 74263 : : m_parent (parent), m_sval_ptr (sval_ptr)
706 : {
707 74263 : gcc_assert (sval_ptr);
708 : }
709 :
710 447303 : hashval_t hash () const
711 : {
712 447303 : inchash::hash hstate;
713 447303 : hstate.add_ptr (m_parent);
714 447303 : hstate.add_ptr (m_sval_ptr);
715 447303 : return hstate.end ();
716 : }
717 :
718 427046 : bool operator== (const key_t &other) const
719 : {
720 427046 : return (m_parent == other.m_parent && m_sval_ptr == other.m_sval_ptr);
721 : }
722 :
723 : void mark_deleted () { m_sval_ptr = reinterpret_cast<const svalue *> (1); }
724 0 : void mark_empty () { m_sval_ptr = nullptr; }
725 483329 : bool is_deleted () const
726 : {
727 483329 : return m_sval_ptr == reinterpret_cast<const svalue *> (1);
728 : }
729 1687664 : bool is_empty () const { return m_sval_ptr == nullptr; }
730 :
731 : const region *m_parent;
732 : const svalue *m_sval_ptr;
733 : };
734 :
735 : symbolic_region (symbolic::node::id_t id,
736 : const region &parent,
737 : const svalue *sval_ptr);
738 :
739 : const symbolic_region *
740 3236173 : dyn_cast_symbolic_region () const final override { return this; }
741 :
742 2803426 : enum region_kind get_kind () const final override { return RK_SYMBOLIC; }
743 : void accept (visitor *v) const final override;
744 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
745 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
746 : void
747 : add_dump_widget_children_for_region (text_art::tree_widget &w,
748 : symbolic::node_dump_state &nds)
749 : const final override;
750 : void
751 : accept_node_visitor (symbolic::node_visitor &v) const final override;
752 :
753 : std::unique_ptr<json::object>
754 : to_json () const final override;
755 :
756 : std::unique_ptr<diagnostics::markup::node>
757 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
758 :
759 3255208 : const svalue *get_pointer () const { return m_sval_ptr; }
760 :
761 : private:
762 : static memory_space
763 : calc_memory_space (const region &parent,
764 : const svalue &sval_ptr);
765 :
766 : const svalue *m_sval_ptr;
767 : };
768 :
769 : } // namespace ana
770 :
771 : template <>
772 : template <>
773 : inline bool
774 18573 : is_a_helper <const symbolic_region *>::test (const region *reg)
775 : {
776 18573 : return reg->get_kind () == RK_SYMBOLIC;
777 : }
778 :
779 : template <> struct default_hash_traits<symbolic_region::key_t>
780 : : public member_function_hash_traits<symbolic_region::key_t>
781 : {
782 : static const bool empty_zero_p = true;
783 : };
784 :
785 : namespace ana {
786 :
787 : /* Concrete region subclass representing the memory occupied by a
788 : variable (whether for a global or a local).
789 : Also used for representing SSA names, as if they were locals. */
790 :
791 : class decl_region : public cluster_region
792 : {
793 : public:
794 128641 : decl_region (symbolic::node::id_t id, const region &parent, tree decl)
795 128641 : : cluster_region (complexity (&parent), id, parent, TREE_TYPE (decl)),
796 128641 : m_decl (decl),
797 257282 : m_tracked (calc_tracked_p (decl)),
798 257282 : m_ctor_bit_contents (nullptr)
799 128641 : {}
800 :
801 14357767 : enum region_kind get_kind () const final override { return RK_DECL; }
802 : const decl_region *
803 5374095 : dyn_cast_decl_region () const final override { return this; }
804 :
805 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
806 :
807 : void
808 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
809 :
810 : void
811 : accept_node_visitor (symbolic::node_visitor &v) const final override;
812 :
813 : std::unique_ptr<json::object>
814 : to_json () const final override;
815 :
816 : std::unique_ptr<diagnostics::markup::node>
817 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
818 :
819 1381398 : bool tracked_p () const final override { return m_tracked; }
820 :
821 4137767 : tree get_decl () const { return m_decl; }
822 : int get_stack_depth () const;
823 :
824 : const symbolic::bit_contents *
825 : maybe_get_bit_contents_for_initializer (region_model_manager *mgr) const;
826 :
827 : const svalue *maybe_get_constant_value (region_model_manager *mgr) const;
828 : const svalue *get_svalue_for_constructor (tree ctor,
829 : region_model_manager *mgr) const;
830 : const symbolic::bit_contents &
831 : get_bit_contents_for_constructor (tree ctor,
832 : region_model_manager *mgr) const;
833 : const svalue *get_svalue_for_initializer (region_model_manager *mgr) const;
834 :
835 : const symbolic::bit_vector *
836 : maybe_get_bit_vector_for_initializer (symbolic::node_manager &node_mgr) const;
837 :
838 : private:
839 : const symbolic::bit_contents &
840 : calc_bit_contents_for_constructor (tree ctor,
841 : region_model_manager *mgr) const;
842 : static bool calc_tracked_p (tree decl);
843 :
844 : tree m_decl;
845 :
846 : /* Cached result of calc_tracked_p, so that we can quickly determine when
847 : we don't to track a binding_cluster for this decl (to avoid bloating
848 : store objects).
849 : This can be debugged using -fdump-analyzer-untracked. */
850 : bool m_tracked;
851 :
852 : /* Cached result of get_bit_contents_for_constructor. */
853 : mutable const symbolic::bit_contents *m_ctor_bit_contents;
854 : };
855 :
856 : } // namespace ana
857 :
858 : template <>
859 : template <>
860 : inline bool
861 821991 : is_a_helper <const decl_region *>::test (const region *reg)
862 : {
863 821991 : return reg->get_kind () == RK_DECL;
864 : }
865 :
866 : namespace ana {
867 :
868 : /* Concrete region subclass representing the memory occupied by a
869 : field within a struct or union. */
870 :
871 : class field_region : public region
872 : {
873 : public:
874 : /* A support class for uniquifying instances of field_region. */
875 : struct key_t
876 : {
877 88123 : key_t (const region *parent, tree field)
878 88123 : : m_parent (parent), m_field (field)
879 : {
880 88123 : gcc_assert (field);
881 : }
882 :
883 565333 : hashval_t hash () const
884 : {
885 565333 : inchash::hash hstate;
886 565333 : hstate.add_ptr (m_parent);
887 565333 : hstate.add_ptr (m_field);
888 565333 : return hstate.end ();
889 : }
890 :
891 559956 : bool operator== (const key_t &other) const
892 : {
893 559956 : return (m_parent == other.m_parent && m_field == other.m_field);
894 : }
895 :
896 : void mark_deleted () { m_field = reinterpret_cast<tree> (1); }
897 0 : void mark_empty () { m_field = NULL_TREE; }
898 629849 : bool is_deleted () const { return m_field == reinterpret_cast<tree> (1); }
899 2115305 : bool is_empty () const { return m_field == NULL_TREE; }
900 :
901 : const region *m_parent;
902 : tree m_field;
903 : };
904 :
905 13412 : field_region (symbolic::node::id_t id, const region &parent, tree field)
906 13412 : : region (complexity (&parent), id, &parent,
907 : parent.get_cluster_region (),
908 : parent.get_memory_space (),
909 13412 : TREE_TYPE (field)),
910 13412 : m_field (field)
911 13412 : {}
912 :
913 42573 : enum region_kind get_kind () const final override { return RK_FIELD; }
914 :
915 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
916 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
917 :
918 : void
919 : accept_node_visitor (symbolic::node_visitor &v) const final override;
920 :
921 : std::unique_ptr<json::object>
922 : to_json () const final override;
923 :
924 : std::unique_ptr<diagnostics::markup::node>
925 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
926 :
927 : const field_region *
928 0 : dyn_cast_field_region () const final override { return this; }
929 :
930 2029 : tree get_field () const { return m_field; }
931 :
932 : const symbolic_bit_count *
933 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr)
934 : const final override;
935 :
936 : const symbolic_byte_count *
937 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr)
938 : const final override;
939 :
940 : private:
941 : tree m_field;
942 : };
943 :
944 : } // namespace ana
945 :
946 : template <>
947 : template <>
948 : inline bool
949 2045 : is_a_helper <const field_region *>::test (const region *reg)
950 : {
951 2045 : return reg->get_kind () == RK_FIELD;
952 : }
953 :
954 : template <> struct default_hash_traits<field_region::key_t>
955 : : public member_function_hash_traits<field_region::key_t>
956 : {
957 : static const bool empty_zero_p = true;
958 : };
959 :
960 : namespace ana {
961 :
962 : /* An element within an array. */
963 :
964 : class element_region : public region
965 : {
966 : public:
967 : /* A support class for uniquifying instances of element_region. */
968 : struct key_t
969 : {
970 29833 : key_t (const region *parent, tree element_type, const svalue *index)
971 29833 : : m_parent (parent), m_element_type (element_type), m_index (index)
972 : {
973 29833 : gcc_assert (index);
974 : }
975 :
976 189121 : hashval_t hash () const
977 : {
978 189121 : inchash::hash hstate;
979 189121 : hstate.add_ptr (m_parent);
980 189121 : hstate.add_ptr (m_element_type);
981 189121 : hstate.add_ptr (m_index);
982 189121 : return hstate.end ();
983 : }
984 :
985 180338 : bool operator== (const key_t &other) const
986 : {
987 180338 : return (m_parent == other.m_parent
988 76778 : && m_element_type == other.m_element_type
989 257116 : && m_index == other.m_index);
990 : }
991 :
992 : void mark_deleted () { m_index = reinterpret_cast<const svalue *> (1); }
993 0 : void mark_empty () { m_index = nullptr; }
994 208785 : bool is_deleted () const
995 : {
996 208785 : return m_index == reinterpret_cast<const svalue *> (1);
997 : }
998 811605 : bool is_empty () const { return m_index == nullptr; }
999 :
1000 : const region *m_parent;
1001 : tree m_element_type;
1002 : const svalue *m_index;
1003 : };
1004 :
1005 6411 : element_region (symbolic::node::id_t id,
1006 : const region &parent,
1007 : tree element_type,
1008 : const svalue *index)
1009 6411 : : region (complexity::from_pair (&parent, index), id, &parent,
1010 : parent.get_cluster_region (),
1011 : parent.get_memory_space (),
1012 : element_type),
1013 12822 : m_index (index)
1014 6411 : {}
1015 :
1016 17919 : enum region_kind get_kind () const final override { return RK_ELEMENT; }
1017 : const element_region *
1018 0 : dyn_cast_element_region () const final override { return this; }
1019 :
1020 : void accept (visitor *v) const final override;
1021 :
1022 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1023 :
1024 : void
1025 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1026 : void
1027 : add_dump_widget_children_for_region (text_art::tree_widget &,
1028 : symbolic::node_dump_state &nds)
1029 : const final override;
1030 :
1031 : void
1032 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1033 :
1034 : std::unique_ptr<json::object>
1035 : to_json () const final override;
1036 :
1037 : std::unique_ptr<diagnostics::markup::node>
1038 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1039 :
1040 10561 : const svalue *get_index () const { return m_index; }
1041 :
1042 : const symbolic_bit_count *
1043 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr)
1044 : const final override;
1045 :
1046 : const symbolic_byte_count *
1047 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr)
1048 : const final override;
1049 :
1050 : private:
1051 : const svalue *m_index;
1052 : };
1053 :
1054 : } // namespace ana
1055 :
1056 : template <>
1057 : template <>
1058 : inline bool
1059 235 : is_a_helper <const element_region *>::test (const region *reg)
1060 : {
1061 235 : return reg->get_kind () == RK_ELEMENT;
1062 : }
1063 :
1064 : template <> struct default_hash_traits<element_region::key_t>
1065 : : public member_function_hash_traits<element_region::key_t>
1066 : {
1067 : static const bool empty_zero_p = true;
1068 : };
1069 :
1070 : namespace ana {
1071 :
1072 : /* A byte-offset within another region, for handling pointer arithmetic
1073 : as a region. */
1074 :
1075 : class offset_region : public region
1076 : {
1077 : public:
1078 : /* A support class for uniquifying instances of offset_region. */
1079 : struct key_t
1080 : {
1081 32121 : key_t (const region *parent, tree element_type, const svalue *byte_offset)
1082 32121 : : m_parent (parent), m_element_type (element_type), m_byte_offset (byte_offset)
1083 : {
1084 32121 : gcc_assert (byte_offset);
1085 : }
1086 :
1087 202663 : hashval_t hash () const
1088 : {
1089 202663 : inchash::hash hstate;
1090 202663 : hstate.add_ptr (m_parent);
1091 202663 : hstate.add_ptr (m_element_type);
1092 202663 : hstate.add_ptr (m_byte_offset);
1093 202663 : return hstate.end ();
1094 : }
1095 :
1096 196464 : bool operator== (const key_t &other) const
1097 : {
1098 196464 : return (m_parent == other.m_parent
1099 71511 : && m_element_type == other.m_element_type
1100 256988 : && m_byte_offset == other.m_byte_offset);
1101 : }
1102 :
1103 : void mark_deleted () { m_byte_offset = reinterpret_cast<const svalue *> (1); }
1104 0 : void mark_empty () { m_byte_offset = nullptr; }
1105 224646 : bool is_deleted () const
1106 : {
1107 224646 : return m_byte_offset == reinterpret_cast<const svalue *> (1);
1108 : }
1109 863679 : bool is_empty () const { return m_byte_offset == nullptr; }
1110 :
1111 : const region *m_parent;
1112 : tree m_element_type;
1113 : const svalue *m_byte_offset;
1114 : };
1115 :
1116 6583 : offset_region (symbolic::node::id_t id, const region &parent, tree type,
1117 : const svalue *byte_offset)
1118 6583 : : region (complexity::from_pair (&parent, byte_offset), id, &parent,
1119 : parent.get_cluster_region (),
1120 : parent.get_memory_space (),
1121 : type),
1122 13166 : m_byte_offset (byte_offset)
1123 6583 : {}
1124 :
1125 17866 : enum region_kind get_kind () const final override { return RK_OFFSET; }
1126 : const offset_region *
1127 215 : dyn_cast_offset_region () const final override { return this; }
1128 :
1129 : void accept (visitor *v) const final override;
1130 :
1131 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1132 :
1133 : void
1134 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1135 : void
1136 : add_dump_widget_children_for_region (text_art::tree_widget &,
1137 : symbolic::node_dump_state &nds)
1138 : const final override;
1139 :
1140 : void
1141 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1142 :
1143 : std::unique_ptr<json::object>
1144 : to_json () const final override;
1145 :
1146 : std::unique_ptr<diagnostics::markup::node>
1147 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1148 :
1149 11094 : const svalue *get_byte_offset () const { return m_byte_offset; }
1150 : const svalue *get_bit_offset (region_model_manager *mgr) const;
1151 :
1152 : const symbolic_bit_count *
1153 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr)
1154 : const final override;
1155 :
1156 : const symbolic_byte_count *
1157 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr)
1158 : const final override;
1159 :
1160 : private:
1161 : const svalue *m_byte_offset;
1162 : };
1163 :
1164 : } // namespace ana
1165 :
1166 : template <>
1167 : template <>
1168 : inline bool
1169 0 : is_a_helper <const offset_region *>::test (const region *reg)
1170 : {
1171 0 : return reg->get_kind () == RK_OFFSET;
1172 : }
1173 :
1174 : template <> struct default_hash_traits<offset_region::key_t>
1175 : : public member_function_hash_traits<offset_region::key_t>
1176 : {
1177 : static const bool empty_zero_p = true;
1178 : };
1179 :
1180 : namespace ana {
1181 :
1182 : /* A region that is size BYTES_SIZE_SVAL in size within its parent
1183 : region (or possibly larger, which would lead to an overflow. */
1184 :
1185 : class sized_region : public region
1186 : {
1187 : public:
1188 : /* A support class for uniquifying instances of sized_region. */
1189 : struct key_t
1190 : {
1191 36172 : key_t (const region *parent, tree element_type,
1192 : const svalue *byte_size_sval)
1193 36172 : : m_parent (parent), m_element_type (element_type),
1194 36172 : m_byte_size_sval (byte_size_sval)
1195 : {
1196 36172 : gcc_assert (byte_size_sval);
1197 : }
1198 :
1199 254630 : hashval_t hash () const
1200 : {
1201 254630 : inchash::hash hstate;
1202 254630 : hstate.add_ptr (m_parent);
1203 254630 : hstate.add_ptr (m_element_type);
1204 254630 : hstate.add_ptr (m_byte_size_sval);
1205 254630 : return hstate.end ();
1206 : }
1207 :
1208 247460 : bool operator== (const key_t &other) const
1209 : {
1210 247460 : return (m_parent == other.m_parent
1211 32266 : && m_element_type == other.m_element_type
1212 276454 : && m_byte_size_sval == other.m_byte_size_sval);
1213 : }
1214 :
1215 : void mark_deleted () { m_byte_size_sval = reinterpret_cast<const svalue *> (1); }
1216 0 : void mark_empty () { m_byte_size_sval = nullptr; }
1217 287003 : bool is_deleted () const
1218 : {
1219 287003 : return m_byte_size_sval == reinterpret_cast<const svalue *> (1);
1220 : }
1221 1054821 : bool is_empty () const { return m_byte_size_sval == nullptr; }
1222 :
1223 : const region *m_parent;
1224 : tree m_element_type;
1225 : const svalue *m_byte_size_sval;
1226 : const svalue *m_end_offset;
1227 : };
1228 :
1229 10463 : sized_region (symbolic::node::id_t id, const region &parent, tree type,
1230 : const svalue *byte_size_sval)
1231 10463 : : region (complexity::from_pair (&parent, byte_size_sval),
1232 : id, &parent,
1233 : parent.get_cluster_region (),
1234 : parent.get_memory_space (),
1235 : type),
1236 20926 : m_byte_size_sval (byte_size_sval)
1237 10463 : {}
1238 :
1239 25984 : enum region_kind get_kind () const final override { return RK_SIZED; }
1240 : const sized_region *
1241 0 : dyn_cast_sized_region () const final override { return this; }
1242 :
1243 : void accept (visitor *v) const final override;
1244 :
1245 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1246 : void
1247 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1248 : void
1249 : add_dump_widget_children_for_region (text_art::tree_widget &,
1250 : symbolic::node_dump_state &nds)
1251 : const final override;
1252 :
1253 : void
1254 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1255 :
1256 : std::unique_ptr<json::object>
1257 : to_json () const final override;
1258 :
1259 : std::unique_ptr<diagnostics::markup::node>
1260 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1261 :
1262 : const symbolic_byte_size *
1263 : maybe_get_byte_size (symbolic::scalar_manager<concrete_byte_size> &byte_mgr) const final override;
1264 : const symbolic_bit_size *
1265 : maybe_get_bit_size (symbolic::scalar_manager<concrete_bit_size> &bit_mgr) const final override;
1266 :
1267 : private:
1268 : const svalue *m_byte_size_sval;
1269 : };
1270 :
1271 : } // namespace ana
1272 :
1273 : template <>
1274 : template <>
1275 : inline bool
1276 0 : is_a_helper <const sized_region *>::test (const region *reg)
1277 : {
1278 0 : return reg->get_kind () == RK_SIZED;
1279 : }
1280 :
1281 : template <> struct default_hash_traits<sized_region::key_t>
1282 : : public member_function_hash_traits<sized_region::key_t>
1283 : {
1284 : static const bool empty_zero_p = true;
1285 : };
1286 :
1287 : namespace ana {
1288 :
1289 : /* A region that views another region using a different type. */
1290 :
1291 : class cast_region : public region
1292 : {
1293 : public:
1294 : /* A support class for uniquifying instances of cast_region. */
1295 : struct key_t
1296 : {
1297 29507 : key_t (const region *parent, tree type)
1298 29507 : : m_parent (parent), m_type (type)
1299 : {
1300 29507 : gcc_assert (parent);
1301 : }
1302 :
1303 162991 : hashval_t hash () const
1304 : {
1305 162991 : inchash::hash hstate;
1306 162991 : hstate.add_ptr (m_parent);
1307 162991 : hstate.add_ptr (m_type);
1308 162991 : return hstate.end ();
1309 : }
1310 :
1311 150011 : bool operator== (const key_t &other) const
1312 : {
1313 150011 : return (m_parent == other.m_parent
1314 150011 : && m_type == other.m_type);
1315 : }
1316 :
1317 : void mark_deleted ()
1318 : {
1319 : m_parent = reinterpret_cast<const region *> (1);
1320 : }
1321 0 : void mark_empty () { m_parent = nullptr; }
1322 180984 : bool is_deleted () const
1323 : {
1324 180984 : return m_parent == reinterpret_cast<const region *> (1);
1325 : }
1326 774729 : bool is_empty () const { return m_parent == nullptr; }
1327 :
1328 : const region *m_parent;
1329 : tree m_type;
1330 : };
1331 :
1332 6034 : cast_region (symbolic::node::id_t id, const region &parent, tree type)
1333 6034 : : region (complexity (&parent), id, &parent,
1334 : parent.get_cluster_region (),
1335 : parent.get_memory_space (),
1336 6034 : type)
1337 6034 : {}
1338 :
1339 9071 : enum region_kind get_kind () const final override { return RK_CAST; }
1340 : const cast_region *
1341 36 : dyn_cast_cast_region () const final override { return this; }
1342 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1343 : void
1344 : print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1345 :
1346 : void
1347 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1348 :
1349 : std::unique_ptr<json::object>
1350 : to_json () const final override;
1351 :
1352 : std::unique_ptr<diagnostics::markup::node>
1353 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1354 :
1355 : const symbolic_bit_count *
1356 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr)
1357 : const final override;
1358 :
1359 : const symbolic_byte_count *
1360 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr)
1361 : const final override;
1362 : };
1363 :
1364 : } // namespace ana
1365 :
1366 : template <>
1367 : template <>
1368 : inline bool
1369 : is_a_helper <const cast_region *>::test (const region *reg)
1370 : {
1371 : return reg->get_kind () == RK_CAST;
1372 : }
1373 :
1374 : template <> struct default_hash_traits<cast_region::key_t>
1375 : : public member_function_hash_traits<cast_region::key_t>
1376 : {
1377 : static const bool empty_zero_p = true;
1378 : };
1379 :
1380 : namespace ana {
1381 :
1382 : /* An untyped region dynamically allocated on the heap via "malloc"
1383 : or similar. */
1384 :
1385 : class heap_allocated_region : public cluster_region
1386 : {
1387 : public:
1388 1826 : heap_allocated_region (symbolic::node::id_t id, const region &parent)
1389 1826 : : cluster_region (complexity (parent), id, parent, NULL_TREE)
1390 1826 : {}
1391 :
1392 : enum region_kind
1393 1018997 : get_kind () const final override { return RK_HEAP_ALLOCATED; }
1394 :
1395 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1396 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1397 :
1398 : void
1399 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1400 :
1401 : std::unique_ptr<json::object>
1402 : to_json () const final override;
1403 :
1404 : std::unique_ptr<diagnostics::markup::node>
1405 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1406 : };
1407 :
1408 : /* An untyped region dynamically allocated on the stack via "alloca". */
1409 :
1410 : class alloca_region : public cluster_region
1411 : {
1412 : public:
1413 1151 : alloca_region (symbolic::node::id_t id, const frame_region &parent)
1414 1151 : : cluster_region (complexity (parent), id, parent, NULL_TREE)
1415 1151 : {}
1416 :
1417 86079 : enum region_kind get_kind () const final override { return RK_ALLOCA; }
1418 :
1419 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1420 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1421 :
1422 : void
1423 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1424 :
1425 : std::unique_ptr<json::object>
1426 : to_json () const final override;
1427 :
1428 : std::unique_ptr<diagnostics::markup::node>
1429 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1430 : };
1431 :
1432 : /* A region for a STRING_CST. */
1433 :
1434 : class string_region : public cluster_region
1435 : {
1436 : public:
1437 3930 : string_region (symbolic::node::id_t id, const region &parent, tree string_cst)
1438 3930 : : cluster_region (complexity (&parent), id, parent, TREE_TYPE (string_cst)),
1439 3930 : m_string_cst (string_cst)
1440 3930 : {}
1441 :
1442 : const string_region *
1443 1712 : dyn_cast_string_region () const final override { return this; }
1444 :
1445 25687 : enum region_kind get_kind () const final override { return RK_STRING; }
1446 :
1447 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1448 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1449 : void
1450 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1451 :
1452 : std::unique_ptr<json::object>
1453 : to_json () const final override;
1454 :
1455 : std::unique_ptr<diagnostics::markup::node>
1456 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1457 :
1458 : /* We assume string literals are immutable, so we don't track them in
1459 : the store. */
1460 596 : bool tracked_p () const final override { return false; }
1461 :
1462 8414 : tree get_string_cst () const { return m_string_cst; }
1463 :
1464 : private:
1465 : tree m_string_cst;
1466 : };
1467 :
1468 : } // namespace ana
1469 :
1470 : template <>
1471 : template <>
1472 : inline bool
1473 6343 : is_a_helper <const string_region *>::test (const region *reg)
1474 : {
1475 6343 : return reg->get_kind () == RK_STRING;
1476 : }
1477 :
1478 : namespace ana {
1479 :
1480 : /* A region for a specific range of bits within another region. */
1481 :
1482 : class bit_range_region : public region
1483 : {
1484 : public:
1485 : /* A support class for uniquifying instances of bit_range_region. */
1486 168777 : struct key_t
1487 : {
1488 920 : key_t (const region *parent, tree type, const concrete_bit_range &bits)
1489 920 : : m_parent (parent), m_type (type), m_bits (bits)
1490 : {
1491 920 : gcc_assert (parent);
1492 920 : }
1493 :
1494 6697 : hashval_t hash () const
1495 : {
1496 6697 : inchash::hash hstate;
1497 6697 : hstate.add_ptr (m_parent);
1498 6697 : hstate.add_ptr (m_type);
1499 6697 : hstate.add_int (m_bits.get_start_offset ().as_long ());
1500 6697 : hstate.add_int (m_bits.get_size ().as_long ());
1501 6697 : return hstate.end ();
1502 : }
1503 :
1504 6094 : bool operator== (const key_t &other) const
1505 : {
1506 6094 : return (m_parent == other.m_parent
1507 1435 : && m_type == other.m_type
1508 7529 : && m_bits == other.m_bits);
1509 : }
1510 :
1511 : void mark_deleted () { m_parent = reinterpret_cast<const region *> (1); }
1512 0 : void mark_empty () { m_parent = nullptr; }
1513 7975 : bool is_deleted () const
1514 : {
1515 7975 : return m_parent == reinterpret_cast<const region *> (1);
1516 : }
1517 144565 : bool is_empty () const { return m_parent == nullptr; }
1518 :
1519 : const region *m_parent;
1520 : tree m_type;
1521 : concrete_bit_range m_bits;
1522 : };
1523 :
1524 451 : bit_range_region (symbolic::node::id_t id, const region &parent, tree type,
1525 : const concrete_bit_range &bits)
1526 451 : : region (complexity (&parent), id, &parent,
1527 : parent.get_cluster_region (),
1528 : parent.get_memory_space (),
1529 : type),
1530 451 : m_bits (bits)
1531 451 : {}
1532 :
1533 : const bit_range_region *
1534 0 : dyn_cast_bit_range_region () const final override { return this; }
1535 :
1536 473 : enum region_kind get_kind () const final override { return RK_BIT_RANGE; }
1537 :
1538 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1539 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1540 : void
1541 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1542 :
1543 : std::unique_ptr<json::object>
1544 : to_json () const final override;
1545 :
1546 : std::unique_ptr<diagnostics::markup::node>
1547 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1548 :
1549 0 : const concrete_bit_range &get_bits () const { return m_bits; }
1550 :
1551 : const symbolic_byte_size *
1552 : maybe_get_byte_size (symbolic::scalar_manager<concrete_byte_size> &byte_mgr)
1553 : const final override;
1554 :
1555 : const symbolic_bit_size *
1556 : maybe_get_bit_size (symbolic::scalar_manager<concrete_bit_size> &bit_mgr)
1557 : const final override;
1558 :
1559 : const symbolic_bit_count *
1560 : maybe_get_bit_offset_relative_to_parent (region_model_manager &mgr)
1561 : const final override;
1562 :
1563 : const symbolic_byte_count *
1564 : maybe_get_byte_offset_relative_to_parent (region_model_manager &mgr)
1565 : const final override;
1566 :
1567 : private:
1568 : concrete_bit_range m_bits;
1569 : };
1570 :
1571 : } // namespace ana
1572 :
1573 : template <>
1574 : template <>
1575 : inline bool
1576 0 : is_a_helper <const bit_range_region *>::test (const region *reg)
1577 : {
1578 0 : return reg->get_kind () == RK_BIT_RANGE;
1579 : }
1580 :
1581 : template <> struct default_hash_traits<bit_range_region::key_t>
1582 : : public member_function_hash_traits<bit_range_region::key_t>
1583 : {
1584 : static const bool empty_zero_p = true;
1585 : };
1586 :
1587 : namespace ana {
1588 :
1589 : /* A region for the N-th vararg within a frame_region for a variadic call. */
1590 :
1591 : class var_arg_region : public cluster_region
1592 : {
1593 : public:
1594 : /* A support class for uniquifying instances of var_arg_region. */
1595 : struct key_t
1596 : {
1597 1310 : key_t (const frame_region *parent, unsigned idx)
1598 1310 : : m_parent (parent), m_idx (idx)
1599 : {
1600 1310 : gcc_assert (parent);
1601 : }
1602 :
1603 8807 : hashval_t hash () const
1604 : {
1605 8807 : inchash::hash hstate;
1606 8807 : hstate.add_ptr (m_parent);
1607 8807 : hstate.add_int (m_idx);
1608 8807 : return hstate.end ();
1609 : }
1610 :
1611 8184 : bool operator== (const key_t &other) const
1612 : {
1613 8184 : return (m_parent == other.m_parent
1614 8184 : && m_idx == other.m_idx);
1615 : }
1616 :
1617 : void mark_deleted ()
1618 : {
1619 : m_parent = reinterpret_cast<const frame_region *> (1);
1620 : }
1621 0 : void mark_empty () { m_parent = nullptr; }
1622 9944 : bool is_deleted () const
1623 : {
1624 9944 : return m_parent == reinterpret_cast<const frame_region *> (1);
1625 : }
1626 152261 : bool is_empty () const { return m_parent == nullptr; }
1627 :
1628 : const frame_region *m_parent;
1629 : unsigned m_idx;
1630 : };
1631 :
1632 496 : var_arg_region (symbolic::node::id_t id, const frame_region &parent,
1633 : unsigned idx)
1634 496 : : cluster_region (complexity (&parent), id, parent, NULL_TREE),
1635 496 : m_idx (idx)
1636 496 : {}
1637 :
1638 : const var_arg_region *
1639 583 : dyn_cast_var_arg_region () const final override { return this; }
1640 :
1641 52677 : enum region_kind get_kind () const final override { return RK_VAR_ARG; }
1642 :
1643 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1644 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1645 : void
1646 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1647 :
1648 : std::unique_ptr<json::object>
1649 : to_json () const final override;
1650 :
1651 : std::unique_ptr<diagnostics::markup::node>
1652 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1653 :
1654 : const frame_region *get_frame_region () const;
1655 703 : unsigned get_index () const { return m_idx; }
1656 :
1657 : private:
1658 : unsigned m_idx;
1659 : };
1660 :
1661 : } // namespace ana
1662 :
1663 : template <>
1664 : template <>
1665 : inline bool
1666 : is_a_helper <const var_arg_region *>::test (const region *reg)
1667 : {
1668 : return reg->get_kind () == RK_VAR_ARG;
1669 : }
1670 :
1671 : template <> struct default_hash_traits<var_arg_region::key_t>
1672 : : public member_function_hash_traits<var_arg_region::key_t>
1673 : {
1674 : static const bool empty_zero_p = true;
1675 : };
1676 :
1677 : namespace ana {
1678 :
1679 : /* A region for errno for the current thread. */
1680 :
1681 4495 : class errno_region : public cluster_region
1682 : {
1683 : public:
1684 4495 : errno_region (symbolic::node::id_t id, const region &parent)
1685 4495 : : cluster_region (complexity (&parent), id, parent, integer_type_node)
1686 4495 : {}
1687 :
1688 65521 : enum region_kind get_kind () const final override { return RK_ERRNO; }
1689 :
1690 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1691 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1692 : void
1693 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1694 :
1695 : std::unique_ptr<json::object>
1696 : to_json () const final override;
1697 :
1698 : std::unique_ptr<diagnostics::markup::node>
1699 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1700 : };
1701 :
1702 : } // namespace ana
1703 :
1704 : template <>
1705 : template <>
1706 : inline bool
1707 : is_a_helper <const errno_region *>::test (const region *reg)
1708 : {
1709 : return reg->get_kind () == RK_ERRNO;
1710 : }
1711 :
1712 : namespace ana {
1713 :
1714 : /* Similar to a decl region, but we don't have the decl.
1715 : For implementing e.g. static buffers of known_functions,
1716 : or other internal state of an API.
1717 :
1718 : These are owned by known_function instances, rather than the
1719 : region_model_manager. */
1720 :
1721 : class private_region : public cluster_region
1722 : {
1723 : public:
1724 7318 : private_region (unsigned id, const region &parent, tree type,
1725 : const char *desc)
1726 7318 : : cluster_region (complexity (&parent), id, parent, type),
1727 7318 : m_desc (desc)
1728 7318 : {}
1729 :
1730 6223 : enum region_kind get_kind () const final override { return RK_PRIVATE; }
1731 :
1732 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1733 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1734 : void
1735 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1736 :
1737 : std::unique_ptr<json::object>
1738 : to_json () const final override;
1739 :
1740 : std::unique_ptr<diagnostics::markup::node>
1741 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1742 :
1743 : private:
1744 : const char *m_desc;
1745 : };
1746 :
1747 : } // namespace ana
1748 :
1749 : template <>
1750 : template <>
1751 : inline bool
1752 : is_a_helper <const private_region *>::test (const region *reg)
1753 : {
1754 : return reg->get_kind () == RK_PRIVATE;
1755 : }
1756 :
1757 : namespace ana {
1758 :
1759 : /* An unknown region, for handling unimplemented tree codes. */
1760 :
1761 : class unknown_region : public cluster_region
1762 : {
1763 : public:
1764 122 : unknown_region (symbolic::node::id_t id, const region &parent, tree type)
1765 122 : : cluster_region (complexity (&parent), id, parent, type)
1766 122 : {}
1767 :
1768 500 : enum region_kind get_kind () const final override { return RK_UNKNOWN; }
1769 :
1770 : void dump_to_pp (pretty_printer *pp, bool simple) const final override;
1771 : void print_region_for_dump_widget_label (pretty_printer *pp) const final override;
1772 : void
1773 : accept_node_visitor (symbolic::node_visitor &v) const final override;
1774 :
1775 : std::unique_ptr<json::object>
1776 : to_json () const final override;
1777 :
1778 : std::unique_ptr<diagnostics::markup::node>
1779 : make_markup_node (symbolic::markup_context &markup_ctxt) const final override;
1780 : };
1781 :
1782 : } // namespace ana
1783 :
1784 : #endif /* GCC_ANALYZER_REGION_H */
|