vtable-verify.h 6.6 KB

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