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