vtable-class-hierarchy.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /* Copyright (C) 2012-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
  4. under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License 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 Pass - Detect corruption of vtable pointers
  15. before using them for virtual method dispatches. */
  16. /* This file is part of the vtable security feature implementation.
  17. The vtable security feature is designed to detect when a virtual
  18. call is about to be made through an invalid vtable pointer
  19. (possibly due to data corruption or malicious attacks). The
  20. compiler finds every virtual call, and inserts a verification call
  21. before the virtual call. The verification call takes the actual
  22. vtable pointer value in the object through which the virtual call
  23. is being made, and compares the vtable pointer against a set of all
  24. valid vtable pointers that the object could contain (this set is
  25. based on the declared type of the object). If the pointer is in
  26. the valid set, execution is allowed to continue; otherwise the
  27. program is halted.
  28. There are several pieces needed in order to make this work: 1. For
  29. every virtual class in the program (i.e. a class that contains
  30. virtual methods), we need to build the set of all possible valid
  31. vtables that an object of that class could point to. This includes
  32. vtables for any class(es) that inherit from the class under
  33. consideration. 2. For every such data set we build up, we need a
  34. way to find and reference the data set. This is complicated by the
  35. fact that the real vtable addresses are not known until runtime,
  36. when the program is loaded into memory, but we need to reference the
  37. sets at compile time when we are inserting verification calls into
  38. the program. 3. We need to find every virtual call in the program,
  39. and insert the verification call (with the appropriate arguments)
  40. before the virtual call. 4. We need some runtime library pieces:
  41. the code to build up the data sets at runtime; the code to actually
  42. perform the verification using the data sets; and some code to set
  43. protections on the data sets, so they themselves do not become
  44. hacker targets.
  45. To find and reference the set of valid vtable pointers for any given
  46. virtual class, we create a special global varible for each virtual
  47. class. We refer to this as the "vtable map variable" for that
  48. class. The vtable map variable has the type "void *", and is
  49. initialized by the compiler to NULL. At runtime when the set of
  50. valid vtable pointers for a virtual class, e.g. class Foo, is built,
  51. the vtable map variable for class Foo is made to point to the set.
  52. During compile time, when the compiler is inserting verification
  53. calls into the program, it passes the vtable map variable for the
  54. appropriate class to the verification call, so that at runtime the
  55. verification call can find the appropriate data set.
  56. The actual set of valid vtable pointers for a virtual class,
  57. e.g. class Foo, cannot be built until runtime, when the vtables get
  58. loaded into memory and their addresses are known. But the knowledge
  59. about which vtables belong in which class' hierarchy is only known
  60. at compile time. Therefore at compile time we collect class
  61. hierarchy and vtable information about every virtual class, and we
  62. generate calls to build up the data sets at runtime. To build the
  63. data sets, we call one of the functions we add to the runtime
  64. library, __VLTRegisterPair. __VLTRegisterPair takes two arguments,
  65. a vtable map variable and the address of a vtable. If the vtable
  66. map variable is currently NULL, it creates a new data set (hash
  67. table), makes the vtable map variable point to the new data set, and
  68. inserts the vtable address into the data set. If the vtable map
  69. variable is not NULL, it just inserts the vtable address into the
  70. data set. In order to make sure that our data sets are built before
  71. any verification calls happen, we create a special constructor
  72. initialization function for each compilation unit, give it a very
  73. high initialization priority, and insert all of our calls to
  74. __VLTRegisterPair into our special constructor initialization
  75. function.
  76. The vtable verification feature is controlled by the flag
  77. '-fvtable-verify='. There are three flavors of this:
  78. '-fvtable-verify=std', '-fvtable-verify=preinit', and
  79. '-fvtable-verify=none'. If the option '-fvtable-verfy=preinit' is
  80. used, then our constructor initialization function gets put into the
  81. preinit array. This is necessary if there are data sets that need
  82. to be built very early in execution. If the constructor
  83. initialization function gets put into the preinit array, the we also
  84. add calls to __VLTChangePermission at the beginning and end of the
  85. function. The call at the beginning sets the permissions on the
  86. data sets and vtable map variables to read/write, and the one at the
  87. end makes them read-only. If the '-fvtable-verify=std' option is
  88. used, the constructor initialization functions are executed at their
  89. normal time, and the __VLTChangePermission calls are handled
  90. differently (see the comments in libstdc++-v3/libsupc++/vtv_rts.cc).
  91. The option '-fvtable-verify=none' turns off vtable verification.
  92. This file contains code to find and record the class hierarchies for
  93. the virtual classes in a program, and all the vtables associated
  94. with each such class; to generate the vtable map variables; and to
  95. generate the constructor initialization function (with the calls to
  96. __VLTRegisterPair, and __VLTChangePermission). The main data
  97. structures used for collecting the class hierarchy data and
  98. building/maintaining the vtable map variable data are defined in
  99. gcc/vtable-verify.h, because they are used both here and in
  100. gcc/vtable-verify.c. */
  101. #include "config.h"
  102. #include "system.h"
  103. #include "coretypes.h"
  104. #include "cp-tree.h"
  105. #include "output.h"
  106. #include "hash-map.h"
  107. #include "is-a.h"
  108. #include "plugin-api.h"
  109. #include "vec.h"
  110. #include "hashtab.h"
  111. #include "hash-set.h"
  112. #include "machmode.h"
  113. #include "tm.h"
  114. #include "hard-reg-set.h"
  115. #include "input.h"
  116. #include "function.h"
  117. #include "ipa-ref.h"
  118. #include "cgraph.h"
  119. #include "tree-iterator.h"
  120. #include "vtable-verify.h"
  121. #include "gimplify.h"
  122. #include "stringpool.h"
  123. #include "stor-layout.h"
  124. static int num_calls_to_regset = 0;
  125. static int num_calls_to_regpair = 0;
  126. static int current_set_size;
  127. /* Mark these specially since they need to be stored in precompiled
  128. header IR. */
  129. static GTY (()) vec<tree, va_gc> *vlt_saved_class_info;
  130. static GTY (()) tree vlt_register_pairs_fndecl = NULL_TREE;
  131. static GTY (()) tree vlt_register_set_fndecl = NULL_TREE;
  132. struct work_node {
  133. struct vtv_graph_node *node;
  134. struct work_node *next;
  135. };
  136. struct vtbl_map_node *vtable_find_or_create_map_decl (tree);
  137. /* As part of vtable verification the compiler generates and inserts
  138. calls to __VLTVerifyVtablePointer, which is in libstdc++. This
  139. function builds and initializes the function decl that is used
  140. in generating those function calls.
  141. In addition to __VLTVerifyVtablePointer there is also
  142. __VLTVerifyVtablePointerDebug which can be used in place of
  143. __VLTVerifyVtablePointer, and which takes extra parameters and
  144. outputs extra information, to help debug problems. The debug
  145. version of this function is generated and used if flag_vtv_debug is
  146. true.
  147. The signatures for these functions are:
  148. void * __VLTVerifyVtablePointer (void **, void*);
  149. void * __VLTVerifyVtablePointerDebug (void**, void *, char *, char *);
  150. */
  151. void
  152. vtv_build_vtable_verify_fndecl (void)
  153. {
  154. tree func_type = NULL_TREE;
  155. if (verify_vtbl_ptr_fndecl != NULL_TREE
  156. && TREE_CODE (verify_vtbl_ptr_fndecl) != ERROR_MARK)
  157. return;
  158. if (flag_vtv_debug)
  159. {
  160. func_type = build_function_type_list (const_ptr_type_node,
  161. build_pointer_type (ptr_type_node),
  162. const_ptr_type_node,
  163. const_string_type_node,
  164. const_string_type_node,
  165. NULL_TREE);
  166. verify_vtbl_ptr_fndecl =
  167. build_lang_decl (FUNCTION_DECL,
  168. get_identifier ("__VLTVerifyVtablePointerDebug"),
  169. func_type);
  170. }
  171. else
  172. {
  173. func_type = build_function_type_list (const_ptr_type_node,
  174. build_pointer_type (ptr_type_node),
  175. const_ptr_type_node,
  176. NULL_TREE);
  177. verify_vtbl_ptr_fndecl =
  178. build_lang_decl (FUNCTION_DECL,
  179. get_identifier ("__VLTVerifyVtablePointer"),
  180. func_type);
  181. }
  182. TREE_NOTHROW (verify_vtbl_ptr_fndecl) = 1;
  183. DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl)
  184. = tree_cons (get_identifier ("leaf"), NULL,
  185. DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl));
  186. DECL_PURE_P (verify_vtbl_ptr_fndecl) = 1;
  187. TREE_PUBLIC (verify_vtbl_ptr_fndecl) = 1;
  188. DECL_PRESERVE_P (verify_vtbl_ptr_fndecl) = 1;
  189. }
  190. /* As part of vtable verification the compiler generates and inserts
  191. calls to __VLTRegisterSet and __VLTRegisterPair, which are in
  192. libsupc++. This function builds and initializes the function decls
  193. that are used in generating those function calls.
  194. The signatures for these functions are:
  195. void __VLTRegisterSetDebug (void **, const void *, std::size_t,
  196. size_t, void **);
  197. void __VLTRegisterSet (void **, const void *, std::size_t,
  198. size_t, void **);
  199. void __VLTRegisterPairDebug (void **, const void *, size_t,
  200. const void *, const char *, const char *);
  201. void __VLTRegisterPair (void **, const void *, size_t, const void *);
  202. */
  203. static void
  204. init_functions (void)
  205. {
  206. tree register_set_type;
  207. tree register_pairs_type;
  208. if (vlt_register_set_fndecl != NULL_TREE)
  209. return;
  210. gcc_assert (vlt_register_pairs_fndecl == NULL_TREE);
  211. gcc_assert (vlt_register_set_fndecl == NULL_TREE);
  212. /* Build function decl for __VLTRegisterSet*. */
  213. register_set_type = build_function_type_list
  214. (void_type_node,
  215. build_pointer_type (ptr_type_node),
  216. const_ptr_type_node,
  217. size_type_node,
  218. size_type_node,
  219. build_pointer_type (ptr_type_node),
  220. NULL_TREE);
  221. if (flag_vtv_debug)
  222. vlt_register_set_fndecl = build_lang_decl
  223. (FUNCTION_DECL,
  224. get_identifier ("__VLTRegisterSetDebug"),
  225. register_set_type);
  226. else
  227. vlt_register_set_fndecl = build_lang_decl
  228. (FUNCTION_DECL,
  229. get_identifier ("__VLTRegisterSet"),
  230. register_set_type);
  231. TREE_NOTHROW (vlt_register_set_fndecl) = 1;
  232. DECL_ATTRIBUTES (vlt_register_set_fndecl) =
  233. tree_cons (get_identifier ("leaf"), NULL,
  234. DECL_ATTRIBUTES (vlt_register_set_fndecl));
  235. DECL_EXTERNAL(vlt_register_set_fndecl) = 1;
  236. TREE_PUBLIC (vlt_register_set_fndecl) = 1;
  237. DECL_PRESERVE_P (vlt_register_set_fndecl) = 1;
  238. SET_DECL_LANGUAGE (vlt_register_set_fndecl, lang_cplusplus);
  239. /* Build function decl for __VLTRegisterPair*. */
  240. if (flag_vtv_debug)
  241. {
  242. register_pairs_type = build_function_type_list (void_type_node,
  243. build_pointer_type
  244. (ptr_type_node),
  245. const_ptr_type_node,
  246. size_type_node,
  247. const_ptr_type_node,
  248. const_string_type_node,
  249. const_string_type_node,
  250. NULL_TREE);
  251. vlt_register_pairs_fndecl = build_lang_decl
  252. (FUNCTION_DECL,
  253. get_identifier ("__VLTRegisterPairDebug"),
  254. register_pairs_type);
  255. }
  256. else
  257. {
  258. register_pairs_type = build_function_type_list (void_type_node,
  259. build_pointer_type
  260. (ptr_type_node),
  261. const_ptr_type_node,
  262. size_type_node,
  263. const_ptr_type_node,
  264. NULL_TREE);
  265. vlt_register_pairs_fndecl = build_lang_decl
  266. (FUNCTION_DECL,
  267. get_identifier ("__VLTRegisterPair"),
  268. register_pairs_type);
  269. }
  270. TREE_NOTHROW (vlt_register_pairs_fndecl) = 1;
  271. DECL_ATTRIBUTES (vlt_register_pairs_fndecl) =
  272. tree_cons (get_identifier ("leaf"), NULL,
  273. DECL_ATTRIBUTES (vlt_register_pairs_fndecl));
  274. DECL_EXTERNAL(vlt_register_pairs_fndecl) = 1;
  275. TREE_PUBLIC (vlt_register_pairs_fndecl) = 1;
  276. DECL_PRESERVE_P (vlt_register_pairs_fndecl) = 1;
  277. SET_DECL_LANGUAGE (vlt_register_pairs_fndecl, lang_cplusplus);
  278. }
  279. /* This is a helper function for
  280. vtv_compute_class_hierarchy_transitive_closure. It adds a
  281. vtv_graph_node to the WORKLIST, which is a linked list of
  282. seen-but-not-yet-processed nodes. INSERTED is a bitmap, one bit
  283. per node, to help make sure that we don't insert a node into the
  284. worklist more than once. Each node represents a class somewhere in
  285. our class hierarchy information. Every node in the graph gets added
  286. to the worklist exactly once and removed from the worklist exactly
  287. once (when all of its children have been processed). */
  288. static void
  289. add_to_worklist (struct work_node **worklist, struct vtv_graph_node *node,
  290. sbitmap inserted)
  291. {
  292. struct work_node *new_work_node;
  293. if (bitmap_bit_p (inserted, node->class_uid))
  294. return;
  295. new_work_node = XNEW (struct work_node);
  296. new_work_node->next = *worklist;
  297. new_work_node->node = node;
  298. *worklist = new_work_node;
  299. bitmap_set_bit (inserted, node->class_uid);
  300. }
  301. /* This is a helper function for
  302. vtv_compute_class_hierarchy_transitive_closure. It goes through
  303. the WORKLIST of class hierarchy nodes looking for a "leaf" node,
  304. i.e. a node whose children in the hierarchy have all been
  305. processed. When it finds the next leaf node, it removes it from
  306. the linked list (WORKLIST) and returns the node. */
  307. static struct vtv_graph_node *
  308. find_and_remove_next_leaf_node (struct work_node **worklist)
  309. {
  310. struct work_node *prev, *cur;
  311. struct vtv_graph_node *ret_val = NULL;
  312. for (prev = NULL, cur = *worklist; cur; prev = cur, cur = cur->next)
  313. {
  314. if ((cur->node->children).length() == cur->node->num_processed_children)
  315. {
  316. if (prev == NULL)
  317. (*worklist) = cur->next;
  318. else
  319. prev->next = cur->next;
  320. cur->next = NULL;
  321. ret_val = cur->node;
  322. free (cur);
  323. return ret_val;
  324. }
  325. }
  326. return NULL;
  327. }
  328. /* In our class hierarchy graph, each class node contains a bitmap,
  329. with one bit for each class in the hierarchy. The bits are set for
  330. classes that are descendants in the graph of the current node.
  331. Initially the descendants bitmap is only set for immediate
  332. descendants. This function traverses the class hierarchy graph,
  333. bottom up, filling in the transitive closures for the descendants
  334. as we rise up the graph. */
  335. void
  336. vtv_compute_class_hierarchy_transitive_closure (void)
  337. {
  338. struct work_node *worklist = NULL;
  339. sbitmap inserted = sbitmap_alloc (num_vtable_map_nodes);
  340. unsigned i;
  341. unsigned j;
  342. /* Note: Every node in the graph gets added to the worklist exactly
  343. once and removed from the worklist exactly once (when all of its
  344. children have been processed). Each node's children edges are
  345. followed exactly once, and each node's parent edges are followed
  346. exactly once. So this algorithm is roughly O(V + 2E), i.e.
  347. O(E + V). */
  348. /* Set-up: */
  349. /* Find all the "leaf" nodes in the graph, and add them to the worklist. */
  350. bitmap_clear (inserted);
  351. for (j = 0; j < num_vtable_map_nodes; ++j)
  352. {
  353. struct vtbl_map_node *cur = vtbl_map_nodes_vec[j];
  354. if (cur->class_info
  355. && ((cur->class_info->children).length() == 0)
  356. && ! (bitmap_bit_p (inserted, cur->class_info->class_uid)))
  357. add_to_worklist (&worklist, cur->class_info, inserted);
  358. }
  359. /* Main work: pull next leaf node off work list, process it, add its
  360. parents to the worklist, where a 'leaf' node is one that has no
  361. children, or all of its children have been processed. */
  362. while (worklist)
  363. {
  364. struct vtv_graph_node *temp_node =
  365. find_and_remove_next_leaf_node (&worklist);
  366. gcc_assert (temp_node != NULL);
  367. temp_node->descendants = sbitmap_alloc (num_vtable_map_nodes);
  368. bitmap_clear (temp_node->descendants);
  369. bitmap_set_bit (temp_node->descendants, temp_node->class_uid);
  370. for (i = 0; i < (temp_node->children).length(); ++i)
  371. bitmap_ior (temp_node->descendants, temp_node->descendants,
  372. temp_node->children[i]->descendants);
  373. for (i = 0; i < (temp_node->parents).length(); ++i)
  374. {
  375. temp_node->parents[i]->num_processed_children =
  376. temp_node->parents[i]->num_processed_children + 1;
  377. if (!bitmap_bit_p (inserted, temp_node->parents[i]->class_uid))
  378. add_to_worklist (&worklist, temp_node->parents[i], inserted);
  379. }
  380. }
  381. }
  382. /* Keep track of which pairs we have already created __VLTRegisterPair
  383. calls for, to prevent creating duplicate calls within the same
  384. compilation unit. VTABLE_DECL is the var decl for the vtable of
  385. the (descendant) class that we are adding to our class hierarchy
  386. data. VPTR_ADDRESS is an expression for calculating the correct
  387. offset into the vtable (VTABLE_DECL). It is the actual vtable
  388. pointer address that will be stored in our list of valid vtable
  389. pointers for BASE_CLASS. BASE_CLASS is the record_type node for
  390. the base class to whose hiearchy we want to add
  391. VPTR_ADDRESS. (VTABLE_DECL should be the vtable for BASE_CLASS or
  392. one of BASE_CLASS' descendents. */
  393. static bool
  394. check_and_record_registered_pairs (tree vtable_decl, tree vptr_address,
  395. tree base_class)
  396. {
  397. unsigned offset;
  398. struct vtbl_map_node *base_vtable_map_node;
  399. bool inserted_something = false;
  400. if (TREE_CODE (vptr_address) == ADDR_EXPR
  401. && TREE_CODE (TREE_OPERAND (vptr_address, 0)) == MEM_REF)
  402. vptr_address = TREE_OPERAND (vptr_address, 0);
  403. if (TREE_OPERAND_LENGTH (vptr_address) > 1)
  404. offset = TREE_INT_CST_LOW (TREE_OPERAND (vptr_address, 1));
  405. else
  406. offset = 0;
  407. base_vtable_map_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (base_class));
  408. inserted_something = vtbl_map_node_registration_insert
  409. (base_vtable_map_node,
  410. vtable_decl,
  411. offset);
  412. return !inserted_something;
  413. }
  414. /* Given an IDENTIFIER_NODE, build and return a string literal based on it. */
  415. static tree
  416. build_string_from_id (tree identifier)
  417. {
  418. int len;
  419. gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
  420. len = IDENTIFIER_LENGTH (identifier);
  421. return build_string_literal (len + 1, IDENTIFIER_POINTER (identifier));
  422. }
  423. /* A class may contain secondary vtables in it, for various reasons.
  424. This function goes through the decl chain of a class record looking
  425. for any fields that point to secondary vtables, and adding calls to
  426. __VLTRegisterPair for the secondary vtable pointers.
  427. BASE_CLASS_DECL_ARG is an expression for the address of the vtable
  428. map variable for the BASE_CLASS (whose hierarchy we are currently
  429. updating). BASE_CLASS is the record_type node for the base class.
  430. RECORD_TYPE is the record_type node for the descendant class that
  431. we are possibly adding to BASE_CLASS's hierarchy. BODY is the
  432. function body for the constructor init function to which we are
  433. adding our calls to __VLTRegisterPair. */
  434. static void
  435. register_construction_vtables (tree base_class, tree record_type,
  436. vec<tree> *vtable_ptr_array)
  437. {
  438. tree vtbl_var_decl;
  439. if (TREE_CODE (record_type) != RECORD_TYPE)
  440. return;
  441. vtbl_var_decl = CLASSTYPE_VTABLES (record_type);
  442. if (CLASSTYPE_VBASECLASSES (record_type))
  443. {
  444. tree vtt_decl;
  445. bool already_registered = false;
  446. tree val_vtbl_decl = NULL_TREE;
  447. vtt_decl = DECL_CHAIN (vtbl_var_decl);
  448. /* Check to see if we have found a VTT. Add its data if appropriate. */
  449. if (vtt_decl)
  450. {
  451. tree values = DECL_INITIAL (vtt_decl);
  452. if (TREE_ASM_WRITTEN (vtt_decl)
  453. && values != NULL_TREE
  454. && TREE_CODE (values) == CONSTRUCTOR
  455. && TREE_CODE (TREE_TYPE (values)) == ARRAY_TYPE)
  456. {
  457. unsigned HOST_WIDE_INT cnt;
  458. constructor_elt *ce;
  459. /* Loop through the initialization values for this
  460. vtable to get all the correct vtable pointer
  461. addresses that we need to add to our set of valid
  462. vtable pointers for the current base class. This may
  463. result in adding more than just the element assigned
  464. to the primary vptr of the class, so we may end up
  465. with more vtable pointers than are strictly
  466. necessary. */
  467. for (cnt = 0;
  468. vec_safe_iterate (CONSTRUCTOR_ELTS (values),
  469. cnt, &ce);
  470. cnt++)
  471. {
  472. tree value = ce->value;
  473. /* Search for the ADDR_EXPR operand within the value. */
  474. while (value
  475. && TREE_OPERAND (value, 0)
  476. && TREE_CODE (TREE_OPERAND (value, 0)) == ADDR_EXPR)
  477. value = TREE_OPERAND (value, 0);
  478. /* The VAR_DECL for the vtable should be the first
  479. argument of the ADDR_EXPR, which is the first
  480. argument of value.*/
  481. if (TREE_OPERAND (value, 0))
  482. val_vtbl_decl = TREE_OPERAND (value, 0);
  483. while (TREE_CODE (val_vtbl_decl) != VAR_DECL
  484. && TREE_OPERAND (val_vtbl_decl, 0))
  485. val_vtbl_decl = TREE_OPERAND (val_vtbl_decl, 0);
  486. gcc_assert (TREE_CODE (val_vtbl_decl) == VAR_DECL);
  487. /* Check to see if we already have this vtable pointer in
  488. our valid set for this base class. */
  489. already_registered = check_and_record_registered_pairs
  490. (val_vtbl_decl,
  491. value,
  492. base_class);
  493. if (already_registered)
  494. continue;
  495. /* Add this vtable pointer to our set of valid
  496. pointers for the base class. */
  497. vtable_ptr_array->safe_push (value);
  498. current_set_size++;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. /* This function iterates through all the vtables it can find from the
  505. BINFO of a class, to make sure we have found ALL of the vtables
  506. that an object of that class could point to. Generate calls to
  507. __VLTRegisterPair for those vtable pointers that we find.
  508. BINFO is the tree_binfo node for the BASE_CLASS. BODY is the
  509. function body for the constructor init function to which we are
  510. adding calls to __VLTRegisterPair. ARG1 is an expression for the
  511. address of the vtable map variable (for the BASE_CLASS), that will
  512. point to the updated data set. BASE_CLASS is the record_type node
  513. for the base class whose set of valid vtable pointers we are
  514. updating. STR1 and STR2 are all debugging information, to be passed
  515. as parameters to __VLTRegisterPairDebug. STR1 represents the name
  516. of the vtable map variable to be updated by the call. Similarly,
  517. STR2 represents the name of the class whose vtable pointer is being
  518. added to the hierarchy. */
  519. static void
  520. register_other_binfo_vtables (tree binfo, tree base_class,
  521. vec<tree> *vtable_ptr_array)
  522. {
  523. unsigned ix;
  524. tree base_binfo;
  525. tree vtable_decl;
  526. bool already_registered;
  527. if (binfo == NULL_TREE)
  528. return;
  529. for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
  530. {
  531. if ((!BINFO_PRIMARY_P (base_binfo)
  532. || BINFO_VIRTUAL_P (base_binfo))
  533. && (vtable_decl = get_vtbl_decl_for_binfo (base_binfo)))
  534. {
  535. tree vtable_address = build_vtbl_address (base_binfo);
  536. already_registered = check_and_record_registered_pairs
  537. (vtable_decl,
  538. vtable_address,
  539. base_class);
  540. if (!already_registered)
  541. {
  542. vtable_ptr_array->safe_push (vtable_address);
  543. current_set_size++;
  544. }
  545. }
  546. register_other_binfo_vtables (base_binfo, base_class, vtable_ptr_array);
  547. }
  548. }
  549. /* The set of valid vtable pointers for any given class are stored in
  550. a hash table. For reasons of efficiency, that hash table size is
  551. always a power of two. In order to try to prevent re-sizing the
  552. hash tables very often, we pass __VLTRegisterPair an initial guess
  553. as to the number of entries the hashtable will eventually need
  554. (rounded up to the nearest power of two). This function takes the
  555. class information we have collected for a particular class,
  556. CLASS_NODE, and calculates the hash table size guess. */
  557. static int
  558. guess_num_vtable_pointers (struct vtv_graph_node *class_node)
  559. {
  560. tree vtbl;
  561. int total_num_vtbls = 0;
  562. int num_vtbls_power_of_two = 1;
  563. unsigned i;
  564. for (i = 0; i < num_vtable_map_nodes; ++i)
  565. if (bitmap_bit_p (class_node->descendants, i))
  566. {
  567. tree class_type = vtbl_map_nodes_vec[i]->class_info->class_type;
  568. for (vtbl = CLASSTYPE_VTABLES (class_type); vtbl;
  569. vtbl = DECL_CHAIN (vtbl))
  570. {
  571. total_num_vtbls++;
  572. if (total_num_vtbls > num_vtbls_power_of_two)
  573. num_vtbls_power_of_two <<= 1;
  574. }
  575. }
  576. return num_vtbls_power_of_two;
  577. }
  578. /* A simple hash function on strings */
  579. /* Be careful about changing this routine. The values generated will
  580. be stored in the calls to InitSet. So, changing this routine may
  581. cause a binary incompatibility. */
  582. static uint32_t
  583. vtv_string_hash (const char *in)
  584. {
  585. const char *s = in;
  586. uint32_t h = 0;
  587. gcc_assert (in != NULL);
  588. for ( ; *s; ++s)
  589. h = 5 * h + *s;
  590. return h;
  591. }
  592. static char *
  593. get_log_file_name (const char *fname)
  594. {
  595. const char *tmp_dir = concat (dump_dir_name, NULL);
  596. char *full_name;
  597. int dir_len;
  598. int fname_len;
  599. dir_len = strlen (tmp_dir);
  600. fname_len = strlen (fname);
  601. full_name = XNEWVEC (char, dir_len + fname_len + 1);
  602. strcpy (full_name, tmp_dir);
  603. strcpy (full_name + dir_len, fname);
  604. return full_name;
  605. }
  606. static void
  607. write_out_current_set_data (tree base_class, int set_size)
  608. {
  609. static int class_data_log_fd = -1;
  610. char buffer[1024];
  611. int bytes_written __attribute__ ((unused));
  612. char *file_name = get_log_file_name ("vtv_class_set_sizes.log");
  613. if (class_data_log_fd == -1)
  614. class_data_log_fd = open (file_name,
  615. O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
  616. if (class_data_log_fd == -1)
  617. {
  618. warning_at (UNKNOWN_LOCATION, 0,
  619. "unable to open log file %<vtv_class_set_sizes.log%>: %m");
  620. return;
  621. }
  622. snprintf (buffer, sizeof (buffer), "%s %d\n",
  623. IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (base_class))),
  624. set_size);
  625. bytes_written = write (class_data_log_fd, buffer, strlen (buffer));
  626. }
  627. static tree
  628. build_key_buffer_arg (tree base_ptr_var_decl)
  629. {
  630. const int key_type_fixed_size = 8;
  631. uint32_t len1 = IDENTIFIER_LENGTH (DECL_NAME (base_ptr_var_decl));
  632. uint32_t hash_value = vtv_string_hash (IDENTIFIER_POINTER
  633. (DECL_NAME (base_ptr_var_decl)));
  634. void *key_buffer = xmalloc (len1 + key_type_fixed_size);
  635. uint32_t *value_ptr = (uint32_t *) key_buffer;
  636. tree ret_value;
  637. /* Set the len and hash for the string. */
  638. *value_ptr = len1;
  639. value_ptr++;
  640. *value_ptr = hash_value;
  641. /* Now copy the string representation of the vtbl map name... */
  642. memcpy ((char *) key_buffer + key_type_fixed_size,
  643. IDENTIFIER_POINTER (DECL_NAME (base_ptr_var_decl)),
  644. len1);
  645. /* ... and build a string literal from it. This will make a copy
  646. so the key_bufffer is not needed anymore after this. */
  647. ret_value = build_string_literal (len1 + key_type_fixed_size,
  648. (char *) key_buffer);
  649. free (key_buffer);
  650. return ret_value;
  651. }
  652. static void
  653. insert_call_to_register_set (tree class_name,
  654. vec<tree> *vtbl_ptr_array, tree body, tree arg1,
  655. tree arg2, tree size_hint_arg)
  656. {
  657. tree call_expr;
  658. int num_args = vtbl_ptr_array->length();
  659. char *array_arg_name = ACONCAT (("__vptr_array_",
  660. IDENTIFIER_POINTER (class_name), NULL));
  661. tree array_arg_type = build_array_type_nelts (build_pointer_type
  662. (build_pointer_type
  663. (void_type_node)),
  664. num_args);
  665. tree array_arg = build_decl (UNKNOWN_LOCATION, VAR_DECL,
  666. get_identifier (array_arg_name),
  667. array_arg_type);
  668. int k;
  669. vec<constructor_elt, va_gc> *array_elements;
  670. vec_alloc (array_elements, num_args);
  671. tree initial = NULL_TREE;
  672. tree arg3 = NULL_TREE;
  673. TREE_PUBLIC (array_arg) = 0;
  674. DECL_EXTERNAL (array_arg) = 0;
  675. TREE_STATIC (array_arg) = 1;
  676. DECL_ARTIFICIAL (array_arg) = 0;
  677. TREE_READONLY (array_arg) = 1;
  678. DECL_IGNORED_P (array_arg) = 0;
  679. DECL_PRESERVE_P (array_arg) = 0;
  680. DECL_VISIBILITY (array_arg) = VISIBILITY_HIDDEN;
  681. for (k = 0; k < num_args; ++k)
  682. {
  683. CONSTRUCTOR_APPEND_ELT (array_elements, NULL_TREE, (*vtbl_ptr_array)[k]);
  684. }
  685. initial = build_constructor (TREE_TYPE (array_arg), array_elements);
  686. TREE_CONSTANT (initial) = 1;
  687. TREE_STATIC (initial) = 1;
  688. DECL_INITIAL (array_arg) = initial;
  689. relayout_decl (array_arg);
  690. varpool_node::finalize_decl (array_arg);
  691. arg3 = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (array_arg)), array_arg);
  692. TREE_TYPE (arg3) = build_pointer_type (TREE_TYPE (array_arg));
  693. call_expr = build_call_expr (vlt_register_set_fndecl, 5, arg1,
  694. arg2, /* set_symbol_key */
  695. size_hint_arg, build_int_cst (size_type_node,
  696. num_args),
  697. arg3);
  698. append_to_statement_list (call_expr, &body);
  699. num_calls_to_regset++;
  700. }
  701. static void
  702. insert_call_to_register_pair (vec<tree> *vtbl_ptr_array, tree arg1,
  703. tree arg2, tree size_hint_arg, tree str1,
  704. tree str2, tree body)
  705. {
  706. tree call_expr;
  707. int num_args = vtbl_ptr_array->length();
  708. tree vtable_address = NULL_TREE;
  709. if (num_args == 0)
  710. vtable_address = build_int_cst (build_pointer_type (void_type_node), 0);
  711. else
  712. vtable_address = (*vtbl_ptr_array)[0];
  713. if (flag_vtv_debug)
  714. call_expr = build_call_expr (vlt_register_pairs_fndecl, 6, arg1, arg2,
  715. size_hint_arg, vtable_address, str1, str2);
  716. else
  717. call_expr = build_call_expr (vlt_register_pairs_fndecl, 4, arg1, arg2,
  718. size_hint_arg, vtable_address);
  719. append_to_statement_list (call_expr, &body);
  720. num_calls_to_regpair++;
  721. }
  722. static void
  723. output_set_info (tree record_type, vec<tree> vtbl_ptr_array)
  724. {
  725. static int vtv_debug_log_fd = -1;
  726. char buffer[1024];
  727. int bytes_written __attribute__ ((unused));
  728. int array_len = vtbl_ptr_array.length();
  729. const char *class_name =
  730. IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (record_type)));
  731. char *file_name = get_log_file_name ("vtv_set_ptr_data.log");
  732. if (vtv_debug_log_fd == -1)
  733. vtv_debug_log_fd = open (file_name,
  734. O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
  735. if (vtv_debug_log_fd == -1)
  736. {
  737. warning_at (UNKNOWN_LOCATION, 0,
  738. "unable to open log file %<vtv_set_ptr_data.log%>: %m");
  739. return;
  740. }
  741. for (int i = 0; i < array_len; ++i)
  742. {
  743. const char *vptr_name = "unknown";
  744. int vptr_offset = 0;
  745. if (TREE_CODE (vtbl_ptr_array[i]) == POINTER_PLUS_EXPR)
  746. {
  747. tree arg0 = TREE_OPERAND (vtbl_ptr_array[i], 0);
  748. tree arg1 = TREE_OPERAND (vtbl_ptr_array[i], 1);
  749. if (TREE_CODE (arg0) == ADDR_EXPR)
  750. arg0 = TREE_OPERAND (arg0, 0);
  751. if (TREE_CODE (arg0) == VAR_DECL)
  752. vptr_name = IDENTIFIER_POINTER (DECL_NAME (arg0));
  753. if (TREE_CODE (arg1) == INTEGER_CST)
  754. vptr_offset = TREE_INT_CST_LOW (arg1);
  755. }
  756. snprintf (buffer, sizeof (buffer), "%s %s %s + %d\n",
  757. main_input_filename, class_name, vptr_name, vptr_offset);
  758. bytes_written = write (vtv_debug_log_fd, buffer, strlen(buffer));
  759. }
  760. }
  761. /* This function goes through our internal class hierarchy & vtable
  762. pointer data structure and outputs calls to __VLTRegisterPair for
  763. every class-vptr pair (for those classes whose vtable would be
  764. output in the current compilation unit). These calls get put into
  765. our constructor initialization function. BODY is the function
  766. body, so far, of our constructor initialization function, to which we
  767. add the calls. */
  768. static bool
  769. register_all_pairs (tree body)
  770. {
  771. bool registered_at_least_one = false;
  772. vec<tree> *vtbl_ptr_array = NULL;
  773. unsigned j;
  774. for (j = 0; j < num_vtable_map_nodes; ++j)
  775. {
  776. struct vtbl_map_node *current = vtbl_map_nodes_vec[j];
  777. unsigned i = 0;
  778. tree base_class = current->class_info->class_type;
  779. tree base_ptr_var_decl = current->vtbl_map_decl;
  780. tree arg1;
  781. tree arg2;
  782. tree new_type;
  783. tree str1 = NULL_TREE;
  784. tree str2 = NULL_TREE;
  785. size_t size_hint;
  786. tree size_hint_arg;
  787. gcc_assert (current->class_info != NULL);
  788. if (flag_vtv_debug)
  789. str1 = build_string_from_id (DECL_NAME (base_ptr_var_decl));
  790. new_type = build_pointer_type (TREE_TYPE (base_ptr_var_decl));
  791. arg1 = build1 (ADDR_EXPR, new_type, base_ptr_var_decl);
  792. /* We need a fresh vector for each iteration. */
  793. if (vtbl_ptr_array)
  794. vec_free (vtbl_ptr_array);
  795. vec_alloc (vtbl_ptr_array, 10);
  796. for (i = 0; i < num_vtable_map_nodes; ++i)
  797. if (bitmap_bit_p (current->class_info->descendants, i))
  798. {
  799. struct vtbl_map_node *vtbl_class_node = vtbl_map_nodes_vec[i];
  800. tree class_type = vtbl_class_node->class_info->class_type;
  801. if (class_type
  802. && (TREE_CODE (class_type) == RECORD_TYPE))
  803. {
  804. bool already_registered;
  805. tree binfo = TYPE_BINFO (class_type);
  806. tree vtable_decl;
  807. bool vtable_should_be_output = false;
  808. vtable_decl = CLASSTYPE_VTABLES (class_type);
  809. /* Handle main vtable for this class. */
  810. if (vtable_decl)
  811. {
  812. vtable_should_be_output = TREE_ASM_WRITTEN (vtable_decl);
  813. str2 = build_string_from_id (DECL_NAME (vtable_decl));
  814. }
  815. if (vtable_decl && vtable_should_be_output)
  816. {
  817. tree vtable_address = build_vtbl_address (binfo);
  818. already_registered = check_and_record_registered_pairs
  819. (vtable_decl,
  820. vtable_address,
  821. base_class);
  822. if (!already_registered)
  823. {
  824. vtbl_ptr_array->safe_push (vtable_address);
  825. /* Find and handle any 'extra' vtables associated
  826. with this class, via virtual inheritance. */
  827. register_construction_vtables (base_class, class_type,
  828. vtbl_ptr_array);
  829. /* Find and handle any 'extra' vtables associated
  830. with this class, via multiple inheritance. */
  831. register_other_binfo_vtables (binfo, base_class,
  832. vtbl_ptr_array);
  833. }
  834. }
  835. }
  836. }
  837. current_set_size = vtbl_ptr_array->length();
  838. /* Sometimes we need to initialize the set symbol even if we are
  839. not adding any vtable pointers to the set in the current
  840. compilation unit. In that case, we need to initialize the
  841. set to our best guess as to what the eventual size of the set
  842. hash table will be (to prevent having to re-size the hash
  843. table later). */
  844. size_hint = guess_num_vtable_pointers (current->class_info);
  845. /* If we have added vtable pointers to the set in this
  846. compilation unit, adjust the size hint for the set's hash
  847. table appropriately. */
  848. if (vtbl_ptr_array->length() > 0)
  849. {
  850. unsigned len = vtbl_ptr_array->length();
  851. while ((size_t) len > size_hint)
  852. size_hint <<= 1;
  853. }
  854. size_hint_arg = build_int_cst (size_type_node, size_hint);
  855. /* Get the key-buffer argument. */
  856. arg2 = build_key_buffer_arg (base_ptr_var_decl);
  857. if (str2 == NULL_TREE)
  858. str2 = build_string_literal (strlen ("unknown") + 1,
  859. "unknown");
  860. if (flag_vtv_debug)
  861. output_set_info (current->class_info->class_type,
  862. *vtbl_ptr_array);
  863. if (vtbl_ptr_array->length() > 1)
  864. {
  865. insert_call_to_register_set (current->class_name,
  866. vtbl_ptr_array, body, arg1, arg2,
  867. size_hint_arg);
  868. registered_at_least_one = true;
  869. }
  870. else
  871. {
  872. if (vtbl_ptr_array->length() > 0
  873. || (current->is_used
  874. || (current->registered->size() > 0)))
  875. {
  876. insert_call_to_register_pair (vtbl_ptr_array,
  877. arg1, arg2, size_hint_arg, str1,
  878. str2, body);
  879. registered_at_least_one = true;
  880. }
  881. }
  882. if (flag_vtv_counts && current_set_size > 0)
  883. write_out_current_set_data (base_class, current_set_size);
  884. }
  885. return registered_at_least_one;
  886. }
  887. /* Given a tree containing a class type (CLASS_TYPE), this function
  888. finds and returns the class hierarchy node for that class in our
  889. data structure. */
  890. static struct vtv_graph_node *
  891. find_graph_node (tree class_type)
  892. {
  893. struct vtbl_map_node *vtbl_node;
  894. vtbl_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (class_type));
  895. if (vtbl_node)
  896. return vtbl_node->class_info;
  897. return NULL;
  898. }
  899. /* Add base class/derived class pair to our internal class hierarchy
  900. data structure. BASE_NODE is our vtv_graph_node that corresponds
  901. to a base class. DERIVED_NODE is our vtv_graph_node that
  902. corresponds to a class that is a descendant of the base class
  903. (possibly the base class itself). */
  904. static void
  905. add_hierarchy_pair (struct vtv_graph_node *base_node,
  906. struct vtv_graph_node *derived_node)
  907. {
  908. (base_node->children).safe_push (derived_node);
  909. (derived_node->parents).safe_push (base_node);
  910. }
  911. /* This functions adds a new base class/derived class relationship to
  912. our class hierarchy data structure. Both parameters are trees
  913. representing the class types, i.e. RECORD_TYPE trees.
  914. DERIVED_CLASS can be the same as BASE_CLASS. */
  915. static void
  916. update_class_hierarchy_information (tree base_class,
  917. tree derived_class)
  918. {
  919. struct vtv_graph_node *base_node = find_graph_node (base_class);
  920. struct vtv_graph_node *derived_node = find_graph_node (derived_class);
  921. add_hierarchy_pair (base_node, derived_node);
  922. }
  923. static void
  924. write_out_vtv_count_data (void)
  925. {
  926. static int vtv_count_log_fd = -1;
  927. char buffer[1024];
  928. int unused_vtbl_map_vars = 0;
  929. int bytes_written __attribute__ ((unused));
  930. char *file_name = get_log_file_name ("vtv_count_data.log");
  931. if (vtv_count_log_fd == -1)
  932. vtv_count_log_fd = open (file_name,
  933. O_WRONLY | O_APPEND | O_CREAT, S_IRWXU);
  934. if (vtv_count_log_fd == -1)
  935. {
  936. warning_at (UNKNOWN_LOCATION, 0,
  937. "unable to open log file %<vtv_count_data.log%>: %m");
  938. return;
  939. }
  940. for (unsigned i = 0; i < num_vtable_map_nodes; ++i)
  941. {
  942. struct vtbl_map_node *current = vtbl_map_nodes_vec[i];
  943. if (!current->is_used
  944. && current->registered->size() == 0)
  945. unused_vtbl_map_vars++;
  946. }
  947. snprintf (buffer, sizeof (buffer), "%s %d %d %d %d %d\n",
  948. main_input_filename, total_num_virtual_calls,
  949. total_num_verified_vcalls, num_calls_to_regset,
  950. num_calls_to_regpair, unused_vtbl_map_vars);
  951. bytes_written = write (vtv_count_log_fd, buffer, strlen (buffer));
  952. }
  953. /* This function calls register_all_pairs, which actually generates
  954. all the calls to __VLTRegisterPair (in the verification constructor
  955. init function). It also generates the calls to
  956. __VLTChangePermission, if the verification constructor init
  957. function is going into the preinit array. INIT_ROUTINE_BODY is
  958. the body of our constructior initialization function, to which we
  959. add our function calls.*/
  960. bool
  961. vtv_register_class_hierarchy_information (tree init_routine_body)
  962. {
  963. bool registered_something = false;
  964. init_functions ();
  965. if (num_vtable_map_nodes == 0)
  966. return false;
  967. /* Add class hierarchy pairs to the vtable map data structure. */
  968. registered_something = register_all_pairs (init_routine_body);
  969. if (flag_vtv_counts)
  970. write_out_vtv_count_data ();
  971. return registered_something;
  972. }
  973. /* Generate the special constructor function that calls
  974. __VLTChangePermission and __VLTRegisterPairs, and give it a very
  975. high initialization priority. */
  976. void
  977. vtv_generate_init_routine (void)
  978. {
  979. tree init_routine_body;
  980. bool vtable_classes_found = false;
  981. push_lang_context (lang_name_c);
  982. /* The priority for this init function (constructor) is carefully
  983. chosen so that it will happen after the calls to unprotect the
  984. memory used for vtable verification and before the memory is
  985. protected again. */
  986. init_routine_body = vtv_start_verification_constructor_init_function ();
  987. vtable_classes_found =
  988. vtv_register_class_hierarchy_information (init_routine_body);
  989. if (vtable_classes_found)
  990. {
  991. tree vtv_fndecl =
  992. vtv_finish_verification_constructor_init_function (init_routine_body);
  993. TREE_STATIC (vtv_fndecl) = 1;
  994. TREE_USED (vtv_fndecl) = 1;
  995. DECL_PRESERVE_P (vtv_fndecl) = 1;
  996. #if defined (TARGET_PECOFF)
  997. if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
  998. #else
  999. if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
  1000. #endif
  1001. DECL_STATIC_CONSTRUCTOR (vtv_fndecl) = 0;
  1002. gimplify_function_tree (vtv_fndecl);
  1003. cgraph_node::add_new_function (vtv_fndecl, false);
  1004. symtab->process_new_functions ();
  1005. #if defined (TARGET_PECOFF)
  1006. if (flag_vtable_verify == VTV_PREINIT_PRIORITY && !TARGET_PECOFF)
  1007. #else
  1008. if (flag_vtable_verify == VTV_PREINIT_PRIORITY)
  1009. #endif
  1010. assemble_vtv_preinit_initializer (vtv_fndecl);
  1011. }
  1012. pop_lang_context ();
  1013. }
  1014. /* This funtion takes a tree containing a class type (BASE_TYPE), and
  1015. it either finds the existing vtbl_map_node for that class in our
  1016. data structure, or it creates a new node and adds it to the data
  1017. structure if there is not one for the class already. As part of
  1018. this process it also creates the global vtable map variable for the
  1019. class. */
  1020. struct vtbl_map_node *
  1021. vtable_find_or_create_map_decl (tree base_type)
  1022. {
  1023. char *var_name = NULL;
  1024. struct vtbl_map_node *vtable_map_node = NULL;
  1025. /* Verify the type has an associated vtable. */
  1026. if (!TYPE_BINFO (base_type) || !BINFO_VTABLE (TYPE_BINFO (base_type)))
  1027. return NULL;
  1028. /* Create map lookup symbol for base class */
  1029. var_name = get_mangled_vtable_map_var_name (base_type);
  1030. /* We've already created the variable; just look it. */
  1031. vtable_map_node = vtbl_map_get_node (TYPE_MAIN_VARIANT (base_type));
  1032. if (!vtable_map_node || (vtable_map_node->vtbl_map_decl == NULL_TREE))
  1033. {
  1034. /* If we haven't already created the *__vtable_map global
  1035. variable for this class, do so now, and add it to the
  1036. varpool, to make sure it gets saved and written out. */
  1037. tree var_decl = NULL;
  1038. tree var_type = build_pointer_type (void_type_node);
  1039. tree initial_value = integer_zero_node;
  1040. var_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
  1041. get_identifier (var_name), var_type);
  1042. DECL_EXTERNAL (var_decl) = 0;
  1043. TREE_STATIC (var_decl) = 1;
  1044. DECL_VISIBILITY (var_decl) = VISIBILITY_HIDDEN;
  1045. SET_DECL_ASSEMBLER_NAME (var_decl, get_identifier (var_name));
  1046. DECL_ARTIFICIAL (var_decl) = 1;
  1047. /* We cannot mark this variable as read-only because we want to be
  1048. able to write to it at runtime. */
  1049. TREE_READONLY (var_decl) = 0;
  1050. DECL_IGNORED_P (var_decl) = 1;
  1051. DECL_PRESERVE_P (var_decl) = 1;
  1052. /* Put these mmap variables in thr .vtable_map_vars section, so
  1053. we can find and protect them. */
  1054. set_decl_section_name (var_decl, ".vtable_map_vars");
  1055. symtab_node::get (var_decl)->implicit_section = true;
  1056. DECL_INITIAL (var_decl) = initial_value;
  1057. comdat_linkage (var_decl);
  1058. varpool_node::finalize_decl (var_decl);
  1059. if (!vtable_map_node)
  1060. vtable_map_node =
  1061. find_or_create_vtbl_map_node (TYPE_MAIN_VARIANT (base_type));
  1062. if (vtable_map_node->vtbl_map_decl == NULL_TREE)
  1063. vtable_map_node->vtbl_map_decl = var_decl;
  1064. }
  1065. gcc_assert (vtable_map_node);
  1066. return vtable_map_node;
  1067. }
  1068. /* This function is used to build up our class hierarchy data for a
  1069. particular class. TYPE is the record_type tree node for the
  1070. class. */
  1071. static void
  1072. vtv_insert_single_class_info (tree type)
  1073. {
  1074. if (flag_vtable_verify)
  1075. {
  1076. tree binfo = TYPE_BINFO (type);
  1077. tree base_binfo;
  1078. struct vtbl_map_node *own_map;
  1079. int i;
  1080. /* First make sure to create the map for this record type. */
  1081. own_map = vtable_find_or_create_map_decl (type);
  1082. if (own_map == NULL)
  1083. return;
  1084. /* Go through the list of all base classes for the current
  1085. (derived) type, make sure the *__vtable_map global variable
  1086. for the base class exists, and add the base class/derived
  1087. class pair to the class hierarchy information we are
  1088. accumulating (for vtable pointer verification). */
  1089. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  1090. {
  1091. tree tree_val = BINFO_TYPE (base_binfo);
  1092. struct vtbl_map_node *vtable_map_node = NULL;
  1093. vtable_map_node = vtable_find_or_create_map_decl (tree_val);
  1094. if (vtable_map_node != NULL)
  1095. update_class_hierarchy_information (tree_val, type);
  1096. }
  1097. }
  1098. }
  1099. /* This function adds classes we are interested in to a list of
  1100. classes. RECORD is the record_type node for the class we are
  1101. adding to the list. */
  1102. void
  1103. vtv_save_class_info (tree record)
  1104. {
  1105. if (!flag_vtable_verify || TREE_CODE (record) == UNION_TYPE)
  1106. return;
  1107. if (!vlt_saved_class_info)
  1108. vec_alloc (vlt_saved_class_info, 10);
  1109. gcc_assert (TREE_CODE (record) == RECORD_TYPE);
  1110. vec_safe_push (vlt_saved_class_info, record);
  1111. }
  1112. /* This function goes through the list of classes we saved and calls
  1113. vtv_insert_single_class_info on each one, to build up our class
  1114. hierarchy data structure. */
  1115. void
  1116. vtv_recover_class_info (void)
  1117. {
  1118. tree current_class;
  1119. unsigned i;
  1120. if (vlt_saved_class_info)
  1121. {
  1122. for (i = 0; i < vlt_saved_class_info->length(); ++i)
  1123. {
  1124. current_class = (*vlt_saved_class_info)[i];
  1125. gcc_assert (TREE_CODE (current_class) == RECORD_TYPE);
  1126. vtv_insert_single_class_info (current_class);
  1127. }
  1128. }
  1129. }
  1130. #include "gt-cp-vtable-class-hierarchy.h"