vtable-verify.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright (C) 2013-2019 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. /* The function decl used to create calls to __VLTVtableVerify. It must
  19. be global because it needs to be initialized in the C++ front end, but
  20. used in the middle end (in the vtable verification pass). */
  21. extern tree verify_vtbl_ptr_fndecl;
  22. /* Global variable keeping track of how many vtable map variables we
  23. have created. */
  24. extern unsigned num_vtable_map_nodes;
  25. /* Keep track of how many virtual calls we are actually verifying. */
  26. extern int total_num_virtual_calls;
  27. extern int total_num_verified_vcalls;
  28. /* Each vtable map variable corresponds to a virtual class. Each
  29. vtable map variable has a hash table associated with it, that keeps
  30. track of the vtable pointers for which we have generated a call to
  31. __VLTRegisterPair (with the current vtable map variable). This is
  32. the hash table node that is used for each entry in this hash table
  33. of vtable pointers.
  34. Sometimes there are multiple valid vtable pointer entries that use
  35. the same vtable pointer decl with different offsets. Therefore,
  36. for each vtable pointer in the hash table, there is also an array
  37. of offsets used with that vtable. */
  38. struct vtable_registration
  39. {
  40. tree vtable_decl; /* The var decl of the vtable. */
  41. vec<unsigned> offsets; /* The offsets array. */
  42. };
  43. struct registration_hasher : nofree_ptr_hash <struct vtable_registration>
  44. {
  45. static inline hashval_t hash (const vtable_registration *);
  46. static inline bool equal (const vtable_registration *,
  47. const vtable_registration *);
  48. };
  49. typedef hash_table<registration_hasher> register_table_type;
  50. typedef register_table_type::iterator registration_iterator_type;
  51. /* This struct is used to represent the class hierarchy information
  52. that we need. Each vtable map variable has an associated class
  53. hierarchy node (struct vtv_graph_node). Note: In this struct,
  54. 'children' means immediate descendants in the class hierarchy;
  55. 'descendant' means any descendant however many levels deep. */
  56. struct vtv_graph_node {
  57. tree class_type; /* The record_type of the class. */
  58. unsigned class_uid; /* A unique, monotonically
  59. ascending id for class node.
  60. Each vtable map node also has
  61. an id. The class uid is the
  62. same as the vtable map node id
  63. for nodes corresponding to the
  64. same class. */
  65. unsigned num_processed_children; /* # of children for whom we have
  66. computed the class hierarchy
  67. transitive closure. */
  68. vec<struct vtv_graph_node *> parents; /* Vector of parents in the graph. */
  69. vec<struct vtv_graph_node *> children; /* Vector of children in the graph.*/
  70. sbitmap descendants; /* Bitmap representing all this node's
  71. descendants in the graph. */
  72. };
  73. /* This is the node used for our hashtable of vtable map variable
  74. information. When we create a vtable map variable (var decl) we
  75. put it into one of these nodes; create a corresponding
  76. vtv_graph_node for our class hierarchy info and store that in this
  77. node; generate a unique (monotonically ascending) id for both the
  78. vtbl_map_node and the vtv_graph_node; and insert the node into two
  79. data structures (to make it easy to find in several different
  80. ways): 1). A hash table ("vtbl_map_hash" in vtable-verify.c).
  81. This gives us an easy way to check to see if we already have a node
  82. for the vtable map variable or not; and 2). An array (vector) of
  83. vtbl_map_nodes, where the array index corresponds to the unique id
  84. of the vtbl_map_node, which gives us an easy way to use bitmaps to
  85. represent and find the vtable map nodes. */
  86. struct vtbl_map_node {
  87. tree vtbl_map_decl; /* The var decl for the vtable map
  88. variable. */
  89. tree class_name; /* The DECL_ASSEMBLER_NAME of the
  90. class. */
  91. struct vtv_graph_node *class_info; /* Our class hierarchy info for the
  92. class. */
  93. unsigned uid; /* The unique id for the vtable map
  94. variable. */
  95. struct vtbl_map_node *next, *prev; /* Pointers for the linked list
  96. structure. */
  97. register_table_type *registered; /* Hashtable of vtable pointers for which
  98. we have generated a _VLTRegisterPair
  99. call with this vtable map variable. */
  100. bool is_used; /* Boolean indicating if we used this vtable map
  101. variable in a call to __VLTVerifyVtablePointer. */
  102. };
  103. /* Controls debugging for vtable verification. */
  104. extern bool vtv_debug;
  105. /* The global vector of vtbl_map_nodes. */
  106. extern vec<struct vtbl_map_node *> vtbl_map_nodes_vec;
  107. /* The global vectors for mangled class names for anonymous classes. */
  108. extern GTY(()) vec<tree, va_gc> *vtbl_mangled_name_types;
  109. extern GTY(()) vec<tree, va_gc> *vtbl_mangled_name_ids;
  110. extern void vtbl_register_mangled_name (tree, tree);
  111. extern struct vtbl_map_node *vtbl_map_get_node (tree);
  112. extern struct vtbl_map_node *find_or_create_vtbl_map_node (tree);
  113. extern void vtbl_map_node_class_insert (struct vtbl_map_node *, unsigned);
  114. extern bool vtbl_map_node_registration_find (struct vtbl_map_node *,
  115. tree, unsigned);
  116. extern bool vtbl_map_node_registration_insert (struct vtbl_map_node *,
  117. tree, unsigned);
  118. #endif /* VTABLE_VERIFY_H */