]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/analyzer/region-model.h
analyzer: move bounds checking to a new bounds-checking.cc
[thirdparty/gcc.git] / gcc / analyzer / region-model.h
1 /* Classes for modeling the state of memory.
2 Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #ifndef GCC_ANALYZER_REGION_MODEL_H
22 #define GCC_ANALYZER_REGION_MODEL_H
23
24 /* Implementation of the region-based ternary model described in:
25 "A Memory Model for Static Analysis of C Programs"
26 (Zhongxing Xu, Ted Kremenek, and Jian Zhang)
27 http://lcs.ios.ac.cn/~xuzb/canalyze/memmodel.pdf */
28
29 #include "sbitmap.h"
30 #include "selftest.h"
31 #include "analyzer/svalue.h"
32 #include "analyzer/region.h"
33 #include "analyzer/known-function-manager.h"
34 #include "analyzer/region-model-manager.h"
35 #include "analyzer/pending-diagnostic.h"
36
37 using namespace ana;
38
39 namespace inchash
40 {
41 extern void add_path_var (path_var pv, hash &hstate);
42 } // namespace inchash
43
44 namespace ana {
45
46 template <typename T>
47 class one_way_id_map
48 {
49 public:
50 one_way_id_map (int num_ids);
51 void put (T src, T dst);
52 T get_dst_for_src (T src) const;
53 void dump_to_pp (pretty_printer *pp) const;
54 void dump () const;
55 void update (T *) const;
56
57 private:
58 auto_vec<T> m_src_to_dst;
59 };
60
61 /* class one_way_id_map. */
62
63 /* one_way_id_map's ctor, which populates the map with dummy null values. */
64
65 template <typename T>
66 inline one_way_id_map<T>::one_way_id_map (int num_svalues)
67 : m_src_to_dst (num_svalues)
68 {
69 for (int i = 0; i < num_svalues; i++)
70 m_src_to_dst.quick_push (T::null ());
71 }
72
73 /* Record that SRC is to be mapped to DST. */
74
75 template <typename T>
76 inline void
77 one_way_id_map<T>::put (T src, T dst)
78 {
79 m_src_to_dst[src.as_int ()] = dst;
80 }
81
82 /* Get the new value for SRC within the map. */
83
84 template <typename T>
85 inline T
86 one_way_id_map<T>::get_dst_for_src (T src) const
87 {
88 if (src.null_p ())
89 return src;
90 return m_src_to_dst[src.as_int ()];
91 }
92
93 /* Dump this map to PP. */
94
95 template <typename T>
96 inline void
97 one_way_id_map<T>::dump_to_pp (pretty_printer *pp) const
98 {
99 pp_string (pp, "src to dst: {");
100 unsigned i;
101 T *dst;
102 FOR_EACH_VEC_ELT (m_src_to_dst, i, dst)
103 {
104 if (i > 0)
105 pp_string (pp, ", ");
106 T src (T::from_int (i));
107 src.print (pp);
108 pp_string (pp, " -> ");
109 dst->print (pp);
110 }
111 pp_string (pp, "}");
112 pp_newline (pp);
113 }
114
115 /* Dump this map to stderr. */
116
117 template <typename T>
118 DEBUG_FUNCTION inline void
119 one_way_id_map<T>::dump () const
120 {
121 pretty_printer pp;
122 pp.buffer->stream = stderr;
123 dump_to_pp (&pp);
124 pp_flush (&pp);
125 }
126
127 /* Update *ID from the old value to its new value in this map. */
128
129 template <typename T>
130 inline void
131 one_way_id_map<T>::update (T *id) const
132 {
133 *id = get_dst_for_src (*id);
134 }
135
136 /* A mapping from region to svalue for use when tracking state. */
137
138 class region_to_value_map
139 {
140 public:
141 typedef hash_map<const region *, const svalue *> hash_map_t;
142 typedef hash_map_t::iterator iterator;
143
144 region_to_value_map () : m_hash_map () {}
145 region_to_value_map (const region_to_value_map &other)
146 : m_hash_map (other.m_hash_map) {}
147 region_to_value_map &operator= (const region_to_value_map &other);
148
149 bool operator== (const region_to_value_map &other) const;
150 bool operator!= (const region_to_value_map &other) const
151 {
152 return !(*this == other);
153 }
154
155 iterator begin () const { return m_hash_map.begin (); }
156 iterator end () const { return m_hash_map.end (); }
157
158 const svalue * const *get (const region *reg) const
159 {
160 return const_cast <hash_map_t &> (m_hash_map).get (reg);
161 }
162 void put (const region *reg, const svalue *sval)
163 {
164 m_hash_map.put (reg, sval);
165 }
166 void remove (const region *reg)
167 {
168 m_hash_map.remove (reg);
169 }
170
171 bool is_empty () const { return m_hash_map.is_empty (); }
172
173 void dump_to_pp (pretty_printer *pp, bool simple, bool multiline) const;
174 void dump (bool simple) const;
175
176 bool can_merge_with_p (const region_to_value_map &other,
177 region_to_value_map *out) const;
178
179 void purge_state_involving (const svalue *sval);
180
181 private:
182 hash_map_t m_hash_map;
183 };
184
185 /* Various operations delete information from a region_model.
186
187 This struct tracks how many of each kind of entity were purged (e.g.
188 for selftests, and for debugging). */
189
190 struct purge_stats
191 {
192 purge_stats ()
193 : m_num_svalues (0),
194 m_num_regions (0),
195 m_num_equiv_classes (0),
196 m_num_constraints (0),
197 m_num_bounded_ranges_constraints (0),
198 m_num_client_items (0)
199 {}
200
201 int m_num_svalues;
202 int m_num_regions;
203 int m_num_equiv_classes;
204 int m_num_constraints;
205 int m_num_bounded_ranges_constraints;
206 int m_num_client_items;
207 };
208
209 /* A base class for visiting regions and svalues, with do-nothing
210 base implementations of the per-subclass vfuncs. */
211
212 class visitor
213 {
214 public:
215 virtual void visit_region_svalue (const region_svalue *) {}
216 virtual void visit_constant_svalue (const constant_svalue *) {}
217 virtual void visit_unknown_svalue (const unknown_svalue *) {}
218 virtual void visit_poisoned_svalue (const poisoned_svalue *) {}
219 virtual void visit_setjmp_svalue (const setjmp_svalue *) {}
220 virtual void visit_initial_svalue (const initial_svalue *) {}
221 virtual void visit_unaryop_svalue (const unaryop_svalue *) {}
222 virtual void visit_binop_svalue (const binop_svalue *) {}
223 virtual void visit_sub_svalue (const sub_svalue *) {}
224 virtual void visit_repeated_svalue (const repeated_svalue *) {}
225 virtual void visit_bits_within_svalue (const bits_within_svalue *) {}
226 virtual void visit_unmergeable_svalue (const unmergeable_svalue *) {}
227 virtual void visit_placeholder_svalue (const placeholder_svalue *) {}
228 virtual void visit_widening_svalue (const widening_svalue *) {}
229 virtual void visit_compound_svalue (const compound_svalue *) {}
230 virtual void visit_conjured_svalue (const conjured_svalue *) {}
231 virtual void visit_asm_output_svalue (const asm_output_svalue *) {}
232 virtual void visit_const_fn_result_svalue (const const_fn_result_svalue *) {}
233
234 virtual void visit_region (const region *) {}
235 };
236
237 struct append_regions_cb_data;
238
239 /* Helper class for handling calls to functions with known behavior.
240 Implemented in region-model-impl-calls.c. */
241
242 class call_details
243 {
244 public:
245 call_details (const gcall *call, region_model *model,
246 region_model_context *ctxt);
247
248 region_model *get_model () const { return m_model; }
249 region_model_manager *get_manager () const;
250 region_model_context *get_ctxt () const { return m_ctxt; }
251 logger *get_logger () const;
252
253 uncertainty_t *get_uncertainty () const;
254 tree get_lhs_type () const { return m_lhs_type; }
255 const region *get_lhs_region () const { return m_lhs_region; }
256
257 bool maybe_set_lhs (const svalue *result) const;
258
259 unsigned num_args () const;
260 bool arg_is_pointer_p (unsigned idx) const
261 {
262 return POINTER_TYPE_P (get_arg_type (idx));
263 }
264 bool arg_is_size_p (unsigned idx) const;
265
266 const gcall *get_call_stmt () const { return m_call; }
267 location_t get_location () const;
268
269 tree get_arg_tree (unsigned idx) const;
270 tree get_arg_type (unsigned idx) const;
271 const svalue *get_arg_svalue (unsigned idx) const;
272 const char *get_arg_string_literal (unsigned idx) const;
273
274 tree get_fndecl_for_call () const;
275
276 void dump_to_pp (pretty_printer *pp, bool simple) const;
277 void dump (bool simple) const;
278
279 const svalue *get_or_create_conjured_svalue (const region *) const;
280
281 private:
282 const gcall *m_call;
283 region_model *m_model;
284 region_model_context *m_ctxt;
285 tree m_lhs_type;
286 const region *m_lhs_region;
287 };
288
289 /* A region_model encapsulates a representation of the state of memory, with
290 a tree of regions, along with their associated values.
291 The representation is graph-like because values can be pointers to
292 regions.
293 It also stores:
294 - a constraint_manager, capturing relationships between the values, and
295 - dynamic extents, mapping dynamically-allocated regions to svalues (their
296 capacities). */
297
298 class region_model
299 {
300 public:
301 typedef region_to_value_map dynamic_extents_t;
302
303 region_model (region_model_manager *mgr);
304 region_model (const region_model &other);
305 ~region_model ();
306 region_model &operator= (const region_model &other);
307
308 bool operator== (const region_model &other) const;
309 bool operator!= (const region_model &other) const
310 {
311 return !(*this == other);
312 }
313
314 hashval_t hash () const;
315
316 void print (pretty_printer *pp) const;
317
318 void dump_to_pp (pretty_printer *pp, bool simple, bool multiline) const;
319 void dump (FILE *fp, bool simple, bool multiline) const;
320 void dump (bool simple) const;
321
322 void debug () const;
323
324 void validate () const;
325
326 void canonicalize ();
327 bool canonicalized_p () const;
328
329 void
330 on_stmt_pre (const gimple *stmt,
331 bool *out_unknown_side_effects,
332 region_model_context *ctxt);
333
334 void on_assignment (const gassign *stmt, region_model_context *ctxt);
335 const svalue *get_gassign_result (const gassign *assign,
336 region_model_context *ctxt);
337 void on_asm_stmt (const gasm *asm_stmt, region_model_context *ctxt);
338 bool on_call_pre (const gcall *stmt, region_model_context *ctxt);
339 void on_call_post (const gcall *stmt,
340 bool unknown_side_effects,
341 region_model_context *ctxt);
342
343 void purge_state_involving (const svalue *sval, region_model_context *ctxt);
344
345 void impl_deallocation_call (const call_details &cd);
346
347 const svalue *maybe_get_copy_bounds (const region *src_reg,
348 const svalue *num_bytes_sval);
349 void update_for_int_cst_return (const call_details &cd,
350 int retval,
351 bool unmergeable);
352 void update_for_zero_return (const call_details &cd,
353 bool unmergeable);
354 void update_for_nonzero_return (const call_details &cd);
355
356 void handle_unrecognized_call (const gcall *call,
357 region_model_context *ctxt);
358 void get_reachable_svalues (svalue_set *out,
359 const svalue *extra_sval,
360 const uncertainty_t *uncertainty);
361
362 void on_return (const greturn *stmt, region_model_context *ctxt);
363 void on_setjmp (const gcall *stmt, const exploded_node *enode,
364 region_model_context *ctxt);
365 void on_longjmp (const gcall *longjmp_call, const gcall *setjmp_call,
366 int setjmp_stack_depth, region_model_context *ctxt);
367
368 void update_for_phis (const supernode *snode,
369 const cfg_superedge *last_cfg_superedge,
370 region_model_context *ctxt);
371
372 void handle_phi (const gphi *phi, tree lhs, tree rhs,
373 const region_model &old_state,
374 region_model_context *ctxt);
375
376 bool maybe_update_for_edge (const superedge &edge,
377 const gimple *last_stmt,
378 region_model_context *ctxt,
379 rejected_constraint **out);
380
381 void update_for_gcall (const gcall *call_stmt,
382 region_model_context *ctxt,
383 function *callee = NULL);
384
385 void update_for_return_gcall (const gcall *call_stmt,
386 region_model_context *ctxt);
387
388 const region *push_frame (function *fun, const vec<const svalue *> *arg_sids,
389 region_model_context *ctxt);
390 const frame_region *get_current_frame () const { return m_current_frame; }
391 function * get_current_function () const;
392 void pop_frame (tree result_lvalue,
393 const svalue **out_result,
394 region_model_context *ctxt);
395 int get_stack_depth () const;
396 const frame_region *get_frame_at_index (int index) const;
397
398 const region *get_lvalue (path_var pv, region_model_context *ctxt) const;
399 const region *get_lvalue (tree expr, region_model_context *ctxt) const;
400 const svalue *get_rvalue (path_var pv, region_model_context *ctxt) const;
401 const svalue *get_rvalue (tree expr, region_model_context *ctxt) const;
402
403 const region *deref_rvalue (const svalue *ptr_sval, tree ptr_tree,
404 region_model_context *ctxt) const;
405
406 const svalue *get_rvalue_for_bits (tree type,
407 const region *reg,
408 const bit_range &bits,
409 region_model_context *ctxt) const;
410
411 void set_value (const region *lhs_reg, const svalue *rhs_sval,
412 region_model_context *ctxt);
413 void set_value (tree lhs, tree rhs, region_model_context *ctxt);
414 void clobber_region (const region *reg);
415 void purge_region (const region *reg);
416 void fill_region (const region *reg, const svalue *sval);
417 void zero_fill_region (const region *reg);
418 void mark_region_as_unknown (const region *reg, uncertainty_t *uncertainty);
419
420 tristate eval_condition (const svalue *lhs,
421 enum tree_code op,
422 const svalue *rhs) const;
423 tristate compare_initial_and_pointer (const initial_svalue *init,
424 const region_svalue *ptr) const;
425 tristate symbolic_greater_than (const binop_svalue *a,
426 const svalue *b) const;
427 tristate structural_equality (const svalue *a, const svalue *b) const;
428 tristate eval_condition (tree lhs,
429 enum tree_code op,
430 tree rhs,
431 region_model_context *ctxt) const;
432 bool add_constraint (tree lhs, enum tree_code op, tree rhs,
433 region_model_context *ctxt);
434 bool add_constraint (tree lhs, enum tree_code op, tree rhs,
435 region_model_context *ctxt,
436 rejected_constraint **out);
437
438 const region *
439 get_or_create_region_for_heap_alloc (const svalue *size_in_bytes,
440 region_model_context *ctxt);
441 const region *create_region_for_alloca (const svalue *size_in_bytes,
442 region_model_context *ctxt);
443 void get_referenced_base_regions (auto_sbitmap &out_ids) const;
444
445 tree get_representative_tree (const svalue *sval) const;
446 tree get_representative_tree (const region *reg) const;
447 path_var
448 get_representative_path_var (const svalue *sval,
449 svalue_set *visited) const;
450 path_var
451 get_representative_path_var (const region *reg,
452 svalue_set *visited) const;
453
454 /* For selftests. */
455 constraint_manager *get_constraints ()
456 {
457 return m_constraints;
458 }
459
460 store *get_store () { return &m_store; }
461 const store *get_store () const { return &m_store; }
462
463 const dynamic_extents_t &
464 get_dynamic_extents () const
465 {
466 return m_dynamic_extents;
467 }
468 const svalue *get_dynamic_extents (const region *reg) const;
469 void set_dynamic_extents (const region *reg,
470 const svalue *size_in_bytes,
471 region_model_context *ctxt);
472 void unset_dynamic_extents (const region *reg);
473
474 region_model_manager *get_manager () const { return m_mgr; }
475 bounded_ranges_manager *get_range_manager () const
476 {
477 return m_mgr->get_range_manager ();
478 }
479
480 void unbind_region_and_descendents (const region *reg,
481 enum poison_kind pkind);
482
483 bool can_merge_with_p (const region_model &other_model,
484 const program_point &point,
485 region_model *out_model,
486 const extrinsic_state *ext_state = NULL,
487 const program_state *state_a = NULL,
488 const program_state *state_b = NULL) const;
489
490 tree get_fndecl_for_call (const gcall *call,
491 region_model_context *ctxt);
492
493 void get_regions_for_current_frame (auto_vec<const decl_region *> *out) const;
494 static void append_regions_cb (const region *base_reg,
495 struct append_regions_cb_data *data);
496
497 const svalue *get_store_value (const region *reg,
498 region_model_context *ctxt) const;
499
500 bool region_exists_p (const region *reg) const;
501
502 void loop_replay_fixup (const region_model *dst_state);
503
504 const svalue *get_capacity (const region *reg) const;
505
506 const svalue *get_string_size (const svalue *sval) const;
507 const svalue *get_string_size (const region *reg) const;
508
509 bool replay_call_summary (call_summary_replay &r,
510 const region_model &summary);
511
512 void maybe_complain_about_infoleak (const region *dst_reg,
513 const svalue *copied_sval,
514 const region *src_reg,
515 region_model_context *ctxt);
516
517 void set_errno (const call_details &cd);
518
519 /* Implemented in sm-fd.cc */
520 void mark_as_valid_fd (const svalue *sval, region_model_context *ctxt);
521
522 /* Implemented in sm-malloc.cc */
523 void on_realloc_with_move (const call_details &cd,
524 const svalue *old_ptr_sval,
525 const svalue *new_ptr_sval);
526
527 /* Implemented in sm-taint.cc. */
528 void mark_as_tainted (const svalue *sval,
529 region_model_context *ctxt);
530
531 bool add_constraint (const svalue *lhs,
532 enum tree_code op,
533 const svalue *rhs,
534 region_model_context *ctxt);
535
536 const svalue *check_for_poison (const svalue *sval,
537 tree expr,
538 region_model_context *ctxt) const;
539
540 void check_region_for_write (const region *dest_reg,
541 region_model_context *ctxt) const;
542
543 private:
544 const region *get_lvalue_1 (path_var pv, region_model_context *ctxt) const;
545 const svalue *get_rvalue_1 (path_var pv, region_model_context *ctxt) const;
546
547 path_var
548 get_representative_path_var_1 (const svalue *sval,
549 svalue_set *visited) const;
550 path_var
551 get_representative_path_var_1 (const region *reg,
552 svalue_set *visited) const;
553
554 const known_function *get_known_function (tree fndecl,
555 const call_details &cd) const;
556 const known_function *get_known_function (enum internal_fn) const;
557
558 bool add_constraints_from_binop (const svalue *outer_lhs,
559 enum tree_code outer_op,
560 const svalue *outer_rhs,
561 bool *out,
562 region_model_context *ctxt);
563
564 void update_for_call_superedge (const call_superedge &call_edge,
565 region_model_context *ctxt);
566 void update_for_return_superedge (const return_superedge &return_edge,
567 region_model_context *ctxt);
568 bool apply_constraints_for_gcond (const cfg_superedge &edge,
569 const gcond *cond_stmt,
570 region_model_context *ctxt,
571 rejected_constraint **out);
572 bool apply_constraints_for_gswitch (const switch_cfg_superedge &edge,
573 const gswitch *switch_stmt,
574 region_model_context *ctxt,
575 rejected_constraint **out);
576 bool apply_constraints_for_exception (const gimple *last_stmt,
577 region_model_context *ctxt,
578 rejected_constraint **out);
579
580 int poison_any_pointers_to_descendents (const region *reg,
581 enum poison_kind pkind);
582
583 void on_top_level_param (tree param, region_model_context *ctxt);
584
585 bool called_from_main_p () const;
586 const svalue *get_initial_value_for_global (const region *reg) const;
587
588 const region * get_region_for_poisoned_expr (tree expr) const;
589
590 void check_dynamic_size_for_taint (enum memory_space mem_space,
591 const svalue *size_in_bytes,
592 region_model_context *ctxt) const;
593 void check_dynamic_size_for_floats (const svalue *size_in_bytes,
594 region_model_context *ctxt) const;
595
596 void check_region_for_taint (const region *reg,
597 enum access_direction dir,
598 region_model_context *ctxt) const;
599
600 void check_for_writable_region (const region* dest_reg,
601 region_model_context *ctxt) const;
602 void check_region_access (const region *reg,
603 enum access_direction dir,
604 region_model_context *ctxt) const;
605 void check_region_for_read (const region *src_reg,
606 region_model_context *ctxt) const;
607 void check_region_size (const region *lhs_reg, const svalue *rhs_sval,
608 region_model_context *ctxt) const;
609
610 /* Implemented in bounds-checking.cc */
611 void check_symbolic_bounds (const region *base_reg,
612 const svalue *sym_byte_offset,
613 const svalue *num_bytes_sval,
614 const svalue *capacity,
615 enum access_direction dir,
616 region_model_context *ctxt) const;
617 void check_region_bounds (const region *reg, enum access_direction dir,
618 region_model_context *ctxt) const;
619
620 void check_call_args (const call_details &cd) const;
621 void check_external_function_for_access_attr (const gcall *call,
622 tree callee_fndecl,
623 region_model_context *ctxt) const;
624
625 /* Storing this here to avoid passing it around everywhere. */
626 region_model_manager *const m_mgr;
627
628 store m_store;
629
630 constraint_manager *m_constraints; // TODO: embed, rather than dynalloc?
631
632 const frame_region *m_current_frame;
633
634 /* Map from base region to size in bytes, for tracking the sizes of
635 dynamically-allocated regions.
636 This is part of the region_model rather than the region to allow for
637 memory regions to be resized (e.g. by realloc). */
638 dynamic_extents_t m_dynamic_extents;
639 };
640
641 /* Some region_model activity could lead to warnings (e.g. attempts to use an
642 uninitialized value). This abstract base class encapsulates an interface
643 for the region model to use when emitting such warnings.
644
645 Having this as an abstract base class allows us to support the various
646 operations needed by program_state in the analyzer within region_model,
647 whilst keeping them somewhat modularized. */
648
649 class region_model_context
650 {
651 public:
652 /* Hook for clients to store pending diagnostics.
653 Return true if the diagnostic was stored, or false if it was deleted. */
654 virtual bool warn (std::unique_ptr<pending_diagnostic> d) = 0;
655
656 /* Hook for clients to add a note to the last previously stored
657 pending diagnostic. */
658 virtual void add_note (std::unique_ptr<pending_note> pn) = 0;
659
660 /* Hook for clients to be notified when an SVAL that was reachable
661 in a previous state is no longer live, so that clients can emit warnings
662 about leaks. */
663 virtual void on_svalue_leak (const svalue *sval) = 0;
664
665 /* Hook for clients to be notified when the set of explicitly live
666 svalues changes, so that they can purge state relating to dead
667 svalues. */
668 virtual void on_liveness_change (const svalue_set &live_svalues,
669 const region_model *model) = 0;
670
671 virtual logger *get_logger () = 0;
672
673 /* Hook for clients to be notified when the condition
674 "LHS OP RHS" is added to the region model.
675 This exists so that state machines can detect tests on edges,
676 and use them to trigger sm-state transitions (e.g. transitions due
677 to ptrs becoming known to be NULL or non-NULL, rather than just
678 "unchecked") */
679 virtual void on_condition (const svalue *lhs,
680 enum tree_code op,
681 const svalue *rhs) = 0;
682
683 /* Hook for clients to be notified when the condition that
684 SVAL is within RANGES is added to the region model.
685 Similar to on_condition, but for use when handling switch statements.
686 RANGES is non-empty. */
687 virtual void on_bounded_ranges (const svalue &sval,
688 const bounded_ranges &ranges) = 0;
689
690 /* Hook for clients to be notified when a frame is popped from the stack. */
691 virtual void on_pop_frame (const frame_region *) = 0;
692
693 /* Hooks for clients to be notified when an unknown change happens
694 to SVAL (in response to a call to an unknown function). */
695 virtual void on_unknown_change (const svalue *sval, bool is_mutable) = 0;
696
697 /* Hooks for clients to be notified when a phi node is handled,
698 where RHS is the pertinent argument. */
699 virtual void on_phi (const gphi *phi, tree rhs) = 0;
700
701 /* Hooks for clients to be notified when the region model doesn't
702 know how to handle the tree code of T at LOC. */
703 virtual void on_unexpected_tree_code (tree t,
704 const dump_location_t &loc) = 0;
705
706 /* Hook for clients to be notified when a function_decl escapes. */
707 virtual void on_escaped_function (tree fndecl) = 0;
708
709 virtual uncertainty_t *get_uncertainty () = 0;
710
711 /* Hook for clients to purge state involving SVAL. */
712 virtual void purge_state_involving (const svalue *sval) = 0;
713
714 /* Hook for clients to split state with a non-standard path. */
715 virtual void bifurcate (std::unique_ptr<custom_edge_info> info) = 0;
716
717 /* Hook for clients to terminate the standard path. */
718 virtual void terminate_path () = 0;
719
720 virtual const extrinsic_state *get_ext_state () const = 0;
721
722 /* Hook for clients to access the a specific state machine in
723 any underlying program_state. */
724 virtual bool
725 get_state_map_by_name (const char *name,
726 sm_state_map **out_smap,
727 const state_machine **out_sm,
728 unsigned *out_sm_idx,
729 std::unique_ptr<sm_context> *out_sm_context) = 0;
730
731 /* Precanned ways for clients to access specific state machines. */
732 bool get_fd_map (sm_state_map **out_smap,
733 const state_machine **out_sm,
734 unsigned *out_sm_idx,
735 std::unique_ptr<sm_context> *out_sm_context)
736 {
737 return get_state_map_by_name ("file-descriptor", out_smap, out_sm,
738 out_sm_idx, out_sm_context);
739 }
740 bool get_malloc_map (sm_state_map **out_smap,
741 const state_machine **out_sm,
742 unsigned *out_sm_idx)
743 {
744 return get_state_map_by_name ("malloc", out_smap, out_sm, out_sm_idx, NULL);
745 }
746 bool get_taint_map (sm_state_map **out_smap,
747 const state_machine **out_sm,
748 unsigned *out_sm_idx)
749 {
750 return get_state_map_by_name ("taint", out_smap, out_sm, out_sm_idx, NULL);
751 }
752
753 /* Get the current statement, if any. */
754 virtual const gimple *get_stmt () const = 0;
755 };
756
757 /* A "do nothing" subclass of region_model_context. */
758
759 class noop_region_model_context : public region_model_context
760 {
761 public:
762 bool warn (std::unique_ptr<pending_diagnostic>) override { return false; }
763 void add_note (std::unique_ptr<pending_note>) override;
764 void on_svalue_leak (const svalue *) override {}
765 void on_liveness_change (const svalue_set &,
766 const region_model *) override {}
767 logger *get_logger () override { return NULL; }
768 void on_condition (const svalue *lhs ATTRIBUTE_UNUSED,
769 enum tree_code op ATTRIBUTE_UNUSED,
770 const svalue *rhs ATTRIBUTE_UNUSED) override
771 {
772 }
773 void on_bounded_ranges (const svalue &,
774 const bounded_ranges &) override
775 {
776 }
777 void on_pop_frame (const frame_region *) override {}
778 void on_unknown_change (const svalue *sval ATTRIBUTE_UNUSED,
779 bool is_mutable ATTRIBUTE_UNUSED) override
780 {
781 }
782 void on_phi (const gphi *phi ATTRIBUTE_UNUSED,
783 tree rhs ATTRIBUTE_UNUSED) override
784 {
785 }
786 void on_unexpected_tree_code (tree, const dump_location_t &) override {}
787
788 void on_escaped_function (tree) override {}
789
790 uncertainty_t *get_uncertainty () override { return NULL; }
791
792 void purge_state_involving (const svalue *sval ATTRIBUTE_UNUSED) override {}
793
794 void bifurcate (std::unique_ptr<custom_edge_info> info) override;
795 void terminate_path () override;
796
797 const extrinsic_state *get_ext_state () const override { return NULL; }
798
799 bool get_state_map_by_name (const char *,
800 sm_state_map **,
801 const state_machine **,
802 unsigned *,
803 std::unique_ptr<sm_context> *) override
804 {
805 return false;
806 }
807
808 const gimple *get_stmt () const override { return NULL; }
809 };
810
811 /* A subclass of region_model_context for determining if operations fail
812 e.g. "can we generate a region for the lvalue of EXPR?". */
813
814 class tentative_region_model_context : public noop_region_model_context
815 {
816 public:
817 tentative_region_model_context () : m_num_unexpected_codes (0) {}
818
819 void on_unexpected_tree_code (tree, const dump_location_t &)
820 final override
821 {
822 m_num_unexpected_codes++;
823 }
824
825 bool had_errors_p () const { return m_num_unexpected_codes > 0; }
826
827 private:
828 int m_num_unexpected_codes;
829 };
830
831 /* Subclass of region_model_context that wraps another context, allowing
832 for extra code to be added to the various hooks. */
833
834 class region_model_context_decorator : public region_model_context
835 {
836 public:
837 bool warn (std::unique_ptr<pending_diagnostic> d) override
838 {
839 return m_inner->warn (std::move (d));
840 }
841
842 void add_note (std::unique_ptr<pending_note> pn) override
843 {
844 m_inner->add_note (std::move (pn));
845 }
846
847 void on_svalue_leak (const svalue *sval) override
848 {
849 m_inner->on_svalue_leak (sval);
850 }
851
852 void on_liveness_change (const svalue_set &live_svalues,
853 const region_model *model) override
854 {
855 m_inner->on_liveness_change (live_svalues, model);
856 }
857
858 logger *get_logger () override
859 {
860 return m_inner->get_logger ();
861 }
862
863 void on_condition (const svalue *lhs,
864 enum tree_code op,
865 const svalue *rhs) override
866 {
867 m_inner->on_condition (lhs, op, rhs);
868 }
869
870 void on_bounded_ranges (const svalue &sval,
871 const bounded_ranges &ranges) override
872 {
873 m_inner->on_bounded_ranges (sval, ranges);
874 }
875
876 void on_pop_frame (const frame_region *frame_reg) override
877 {
878 m_inner->on_pop_frame (frame_reg);
879 }
880
881 void on_unknown_change (const svalue *sval, bool is_mutable) override
882 {
883 m_inner->on_unknown_change (sval, is_mutable);
884 }
885
886 void on_phi (const gphi *phi, tree rhs) override
887 {
888 m_inner->on_phi (phi, rhs);
889 }
890
891 void on_unexpected_tree_code (tree t,
892 const dump_location_t &loc) override
893 {
894 m_inner->on_unexpected_tree_code (t, loc);
895 }
896
897 void on_escaped_function (tree fndecl) override
898 {
899 m_inner->on_escaped_function (fndecl);
900 }
901
902 uncertainty_t *get_uncertainty () override
903 {
904 return m_inner->get_uncertainty ();
905 }
906
907 void purge_state_involving (const svalue *sval) override
908 {
909 m_inner->purge_state_involving (sval);
910 }
911
912 void bifurcate (std::unique_ptr<custom_edge_info> info) override
913 {
914 m_inner->bifurcate (std::move (info));
915 }
916
917 void terminate_path () override
918 {
919 m_inner->terminate_path ();
920 }
921
922 const extrinsic_state *get_ext_state () const override
923 {
924 return m_inner->get_ext_state ();
925 }
926
927 bool get_state_map_by_name (const char *name,
928 sm_state_map **out_smap,
929 const state_machine **out_sm,
930 unsigned *out_sm_idx,
931 std::unique_ptr<sm_context> *out_sm_context)
932 override
933 {
934 return m_inner->get_state_map_by_name (name, out_smap, out_sm, out_sm_idx,
935 out_sm_context);
936 }
937
938 const gimple *get_stmt () const override
939 {
940 return m_inner->get_stmt ();
941 }
942
943 protected:
944 region_model_context_decorator (region_model_context *inner)
945 : m_inner (inner)
946 {
947 gcc_assert (m_inner);
948 }
949
950 region_model_context *m_inner;
951 };
952
953 /* Subclass of region_model_context_decorator that adds a note
954 when saving diagnostics. */
955
956 class note_adding_context : public region_model_context_decorator
957 {
958 public:
959 bool warn (std::unique_ptr<pending_diagnostic> d) override
960 {
961 if (m_inner->warn (std::move (d)))
962 {
963 add_note (make_note ());
964 return true;
965 }
966 else
967 return false;
968 }
969
970 /* Hook to make the new note. */
971 virtual std::unique_ptr<pending_note> make_note () = 0;
972
973 protected:
974 note_adding_context (region_model_context *inner)
975 : region_model_context_decorator (inner)
976 {
977 }
978 };
979
980 /* A bundle of data for use when attempting to merge two region_model
981 instances to make a third. */
982
983 struct model_merger
984 {
985 model_merger (const region_model *model_a,
986 const region_model *model_b,
987 const program_point &point,
988 region_model *merged_model,
989 const extrinsic_state *ext_state,
990 const program_state *state_a,
991 const program_state *state_b)
992 : m_model_a (model_a), m_model_b (model_b),
993 m_point (point),
994 m_merged_model (merged_model),
995 m_ext_state (ext_state),
996 m_state_a (state_a), m_state_b (state_b)
997 {
998 }
999
1000 void dump_to_pp (pretty_printer *pp, bool simple) const;
1001 void dump (FILE *fp, bool simple) const;
1002 void dump (bool simple) const;
1003
1004 region_model_manager *get_manager () const
1005 {
1006 return m_model_a->get_manager ();
1007 }
1008
1009 bool mergeable_svalue_p (const svalue *) const;
1010 const function_point &get_function_point () const
1011 {
1012 return m_point.get_function_point ();
1013 }
1014
1015 const region_model *m_model_a;
1016 const region_model *m_model_b;
1017 const program_point &m_point;
1018 region_model *m_merged_model;
1019
1020 const extrinsic_state *m_ext_state;
1021 const program_state *m_state_a;
1022 const program_state *m_state_b;
1023 };
1024
1025 /* A record that can (optionally) be written out when
1026 region_model::add_constraint fails. */
1027
1028 class rejected_constraint
1029 {
1030 public:
1031 virtual ~rejected_constraint () {}
1032 virtual void dump_to_pp (pretty_printer *pp) const = 0;
1033
1034 const region_model &get_model () const { return m_model; }
1035
1036 protected:
1037 rejected_constraint (const region_model &model)
1038 : m_model (model)
1039 {}
1040
1041 region_model m_model;
1042 };
1043
1044 class rejected_op_constraint : public rejected_constraint
1045 {
1046 public:
1047 rejected_op_constraint (const region_model &model,
1048 tree lhs, enum tree_code op, tree rhs)
1049 : rejected_constraint (model),
1050 m_lhs (lhs), m_op (op), m_rhs (rhs)
1051 {}
1052
1053 void dump_to_pp (pretty_printer *pp) const final override;
1054
1055 tree m_lhs;
1056 enum tree_code m_op;
1057 tree m_rhs;
1058 };
1059
1060 class rejected_ranges_constraint : public rejected_constraint
1061 {
1062 public:
1063 rejected_ranges_constraint (const region_model &model,
1064 tree expr, const bounded_ranges *ranges)
1065 : rejected_constraint (model),
1066 m_expr (expr), m_ranges (ranges)
1067 {}
1068
1069 void dump_to_pp (pretty_printer *pp) const final override;
1070
1071 private:
1072 tree m_expr;
1073 const bounded_ranges *m_ranges;
1074 };
1075
1076 /* A bundle of state. */
1077
1078 class engine
1079 {
1080 public:
1081 engine (const supergraph *sg = NULL, logger *logger = NULL);
1082 const supergraph *get_supergraph () { return m_sg; }
1083 region_model_manager *get_model_manager () { return &m_mgr; }
1084 known_function_manager *get_known_function_manager ()
1085 {
1086 return m_mgr.get_known_function_manager ();
1087 }
1088
1089 void log_stats (logger *logger) const;
1090
1091 private:
1092 const supergraph *m_sg;
1093 region_model_manager m_mgr;
1094 };
1095
1096 } // namespace ana
1097
1098 extern void debug (const region_model &rmodel);
1099
1100 namespace ana {
1101
1102 #if CHECKING_P
1103
1104 namespace selftest {
1105
1106 using namespace ::selftest;
1107
1108 /* An implementation of region_model_context for use in selftests, which
1109 stores any pending_diagnostic instances passed to it. */
1110
1111 class test_region_model_context : public noop_region_model_context
1112 {
1113 public:
1114 bool warn (std::unique_ptr<pending_diagnostic> d) final override
1115 {
1116 m_diagnostics.safe_push (d.release ());
1117 return true;
1118 }
1119
1120 unsigned get_num_diagnostics () const { return m_diagnostics.length (); }
1121
1122 void on_unexpected_tree_code (tree t, const dump_location_t &)
1123 final override
1124 {
1125 internal_error ("unhandled tree code: %qs",
1126 get_tree_code_name (TREE_CODE (t)));
1127 }
1128
1129 private:
1130 /* Implicitly delete any diagnostics in the dtor. */
1131 auto_delete_vec<pending_diagnostic> m_diagnostics;
1132 };
1133
1134 /* Attempt to add the constraint (LHS OP RHS) to MODEL.
1135 Verify that MODEL remains satisfiable. */
1136
1137 #define ADD_SAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
1138 SELFTEST_BEGIN_STMT \
1139 bool sat = (MODEL).add_constraint (LHS, OP, RHS, NULL); \
1140 ASSERT_TRUE (sat); \
1141 SELFTEST_END_STMT
1142
1143 /* Attempt to add the constraint (LHS OP RHS) to MODEL.
1144 Verify that the result is not satisfiable. */
1145
1146 #define ADD_UNSAT_CONSTRAINT(MODEL, LHS, OP, RHS) \
1147 SELFTEST_BEGIN_STMT \
1148 bool sat = (MODEL).add_constraint (LHS, OP, RHS, NULL); \
1149 ASSERT_FALSE (sat); \
1150 SELFTEST_END_STMT
1151
1152 /* Implementation detail of the ASSERT_CONDITION_* macros. */
1153
1154 void assert_condition (const location &loc,
1155 region_model &model,
1156 const svalue *lhs, tree_code op, const svalue *rhs,
1157 tristate expected);
1158
1159 void assert_condition (const location &loc,
1160 region_model &model,
1161 tree lhs, tree_code op, tree rhs,
1162 tristate expected);
1163
1164 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1165 as "true". */
1166
1167 #define ASSERT_CONDITION_TRUE(REGION_MODEL, LHS, OP, RHS) \
1168 SELFTEST_BEGIN_STMT \
1169 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1170 tristate (tristate::TS_TRUE)); \
1171 SELFTEST_END_STMT
1172
1173 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1174 as "false". */
1175
1176 #define ASSERT_CONDITION_FALSE(REGION_MODEL, LHS, OP, RHS) \
1177 SELFTEST_BEGIN_STMT \
1178 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1179 tristate (tristate::TS_FALSE)); \
1180 SELFTEST_END_STMT
1181
1182 /* Assert that REGION_MODEL evaluates the condition "LHS OP RHS"
1183 as "unknown". */
1184
1185 #define ASSERT_CONDITION_UNKNOWN(REGION_MODEL, LHS, OP, RHS) \
1186 SELFTEST_BEGIN_STMT \
1187 assert_condition (SELFTEST_LOCATION, REGION_MODEL, LHS, OP, RHS, \
1188 tristate (tristate::TS_UNKNOWN)); \
1189 SELFTEST_END_STMT
1190
1191 } /* end of namespace selftest. */
1192
1193 #endif /* #if CHECKING_P */
1194
1195 } // namespace ana
1196
1197 #endif /* GCC_ANALYZER_REGION_MODEL_H */