1/* Copyright (C) 2013-2015 Free Software Foundation, Inc.
2
3This file is part of GCC.
4
5GCC is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free
7Software Foundation; either version 3, or (at your option) any later
8version.
9
10GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or
12FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13for more details.
14
15You should have received a copy of the GNU General Public License
16along with GCC; see the file COPYING3.  If not see
17<http://www.gnu.org/licenses/>.  */
18
19/* Virtual Table Pointer Security.  */
20
21#ifndef VTABLE_VERIFY_H
22#define VTABLE_VERIFY_H
23
24#include "sbitmap.h"
25#include "hash-table.h"
26
27/* The function decl used to create calls to __VLTVtableVerify.  It must
28   be global because it needs to be initialized in the C++ front end, but
29   used in the middle end (in the vtable verification pass).  */
30
31extern tree verify_vtbl_ptr_fndecl;
32
33/* Global variable keeping track of how many vtable map variables we
34   have created. */
35extern unsigned num_vtable_map_nodes;
36
37/* Keep track of how many virtual calls we are actually verifying.  */
38extern int total_num_virtual_calls;
39extern int total_num_verified_vcalls;
40
41/* Each vtable map variable corresponds to a virtual class.  Each
42   vtable map variable has a hash table associated with it, that keeps
43   track of the vtable pointers for which we have generated a call to
44   __VLTRegisterPair (with the current vtable map variable).  This is
45   the hash table node that is used for each entry in this hash table
46   of vtable pointers.
47
48   Sometimes there are multiple valid vtable pointer entries that use
49   the same vtable pointer decl with different offsets.  Therefore,
50   for each vtable pointer in the hash table, there is also an array
51   of offsets used with that vtable. */
52
53struct vtable_registration
54{
55  tree vtable_decl;            /* The var decl of the vtable.               */
56  vec<unsigned> offsets;       /* The offsets array.                        */
57};
58
59struct registration_hasher : typed_noop_remove <struct vtable_registration>
60{
61  typedef struct vtable_registration value_type;
62  typedef struct vtable_registration compare_type;
63  static inline hashval_t hash (const value_type *);
64  static inline bool equal (const value_type *, const compare_type *);
65};
66
67typedef hash_table<registration_hasher> register_table_type;
68typedef register_table_type::iterator registration_iterator_type;
69
70/*  This struct is used to represent the class hierarchy information
71    that we need.  Each vtable map variable has an associated class
72    hierarchy node (struct vtv_graph_node).  Note: In this struct,
73    'children' means immediate descendants in the class hierarchy;
74    'descendant' means any descendant however many levels deep. */
75
76struct vtv_graph_node {
77  tree class_type;                  /* The record_type of the class.        */
78  unsigned class_uid;               /* A unique, monotonically
79                                       ascending id for class node.
80                                       Each vtable map node also has
81                                       an id.  The class uid is the
82                                       same as the vtable map node id
83                                       for nodes corresponding to the
84                                       same class.                          */
85  unsigned num_processed_children;  /* # of children for whom we have
86                                       computed the class hierarchy
87                                       transitive closure.                  */
88  vec<struct vtv_graph_node *> parents;  /* Vector of parents in the graph. */
89  vec<struct vtv_graph_node *> children; /* Vector of children in the graph.*/
90  sbitmap descendants;              /* Bitmap representing all this node's
91                                       descendants in the graph.            */
92};
93
94/* This is the node used for our hashtable of vtable map variable
95   information.  When we create a vtable map variable (var decl) we
96   put it into one of these nodes; create a corresponding
97   vtv_graph_node for our class hierarchy info and store that in this
98   node; generate a unique (monotonically ascending) id for both the
99   vtbl_map_node and the vtv_graph_node; and insert the node into two
100   data structures (to make it easy to find in several different
101   ways): 1). A hash table ("vtbl_map_hash" in vtable-verify.c).
102   This gives us an easy way to check to see if we already have a node
103   for the vtable map variable or not; and 2). An array (vector) of
104   vtbl_map_nodes, where the array index corresponds to the unique id
105   of the vtbl_map_node, which gives us an easy way to use bitmaps to
106   represent and find the vtable map nodes.  */
107
108struct vtbl_map_node {
109  tree vtbl_map_decl;                 /* The var decl for the vtable map
110                                         variable.                          */
111  tree class_name;                    /* The DECL_ASSEMBLER_NAME of the
112                                         class.                             */
113  struct vtv_graph_node *class_info;  /* Our class hierarchy info for the
114                                         class.                             */
115  unsigned uid;                       /* The unique id for the vtable map
116                                         variable.                          */
117  struct vtbl_map_node *next, *prev;  /* Pointers for the linked list
118                                         structure.                         */
119  register_table_type *registered;     /* Hashtable of vtable pointers for which
120                                         we have generated a _VLTRegisterPair
121                                         call with this vtable map variable. */
122  bool is_used;          /* Boolean indicating if we used this vtable map
123                            variable in a call to __VLTVerifyVtablePointer. */
124};
125
126/* Controls debugging for vtable verification.  */
127extern bool vtv_debug;
128
129/* The global vector of vtbl_map_nodes.  */
130extern vec<struct vtbl_map_node *> vtbl_map_nodes_vec;
131
132extern struct vtbl_map_node *vtbl_map_get_node (tree);
133extern struct vtbl_map_node *find_or_create_vtbl_map_node (tree);
134extern void vtbl_map_node_class_insert (struct vtbl_map_node *, unsigned);
135extern bool vtbl_map_node_registration_find (struct vtbl_map_node *,
136                                             tree, unsigned);
137extern bool vtbl_map_node_registration_insert (struct vtbl_map_node *,
138                                               tree, unsigned);
139
140#endif /* VTABLE_VERIFY_H */
141