]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/vtable-verify.c
arm.md (generic_sched): Add cortexa12.
[thirdparty/gcc.git] / gcc / vtable-verify.c
CommitLineData
2077db1b
CT
1/* Copyright (C) 2013
2 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20/* Virtual Table Pointer Security Pass - Detect corruption of vtable pointers
21 before using them for virtual method dispatches. */
22
23/* This file is part of the vtable security feature implementation.
24 The vtable security feature is designed to detect when a virtual
25 call is about to be made through an invalid vtable pointer
26 (possibly due to data corruption or malicious attacks). The
27 compiler finds every virtual call, and inserts a verification call
28 before the virtual call. The verification call takes the actual
29 vtable pointer value in the object through which the virtual call
30 is being made, and compares the vtable pointer against a set of all
31 valid vtable pointers that the object could contain (this set is
32 based on the declared type of the object). If the pointer is in
33 the valid set, execution is allowed to continue; otherwise the
34 program is halted.
35
36 There are several pieces needed in order to make this work: 1. For
37 every virtual class in the program (i.e. a class that contains
38 virtual methods), we need to build the set of all possible valid
39 vtables that an object of that class could point to. This includes
40 vtables for any class(es) that inherit from the class under
41 consideration. 2. For every such data set we build up, we need a
42 way to find and reference the data set. This is complicated by the
43 fact that the real vtable addresses are not known until runtime,
44 when the program is loaded into memory, but we need to reference the
45 sets at compile time when we are inserting verification calls into
46 the program. 3. We need to find every virtual call in the program,
47 and insert the verification call (with the appropriate arguments)
48 before the virtual call. 4. We need some runtime library pieces:
49 the code to build up the data sets at runtime; the code to actually
50 perform the verification using the data sets; and some code to set
51 protections on the data sets, so they themselves do not become
52 hacker targets.
53
54 To find and reference the set of valid vtable pointers for any given
55 virtual class, we create a special global variable for each virtual
56 class. We refer to this as the "vtable map variable" for that
57 class. The vtable map variable has the type "void *", and is
58 initialized by the compiler to NULL. At runtime when the set of
59 valid vtable pointers for a virtual class, e.g. class Foo, is built,
60 the vtable map variable for class Foo is made to point to the set.
61 During compile time, when the compiler is inserting verification
62 calls into the program, it passes the vtable map variable for the
63 appropriate class to the verification call, so that at runtime the
64 verification call can find the appropriate data set.
65
66 The actual set of valid vtable pointers for a virtual class,
67 e.g. class Foo, cannot be built until runtime, when the vtables get
68 loaded into memory and their addresses are known. But the knowledge
69 about which vtables belong in which class' hierarchy is only known
70 at compile time. Therefore at compile time we collect class
71 hierarchy and vtable information about every virtual class, and we
72 generate calls to build up the data sets at runtime. To build the
73 data sets, we call one of the functions we add to the runtime
74 library, __VLTRegisterPair. __VLTRegisterPair takes two arguments,
75 a vtable map variable and the address of a vtable. If the vtable
76 map variable is currently NULL, it creates a new data set (hash
77 table), makes the vtable map variable point to the new data set, and
78 inserts the vtable address into the data set. If the vtable map
79 variable is not NULL, it just inserts the vtable address into the
80 data set. In order to make sure that our data sets are built before
81 any verification calls happen, we create a special constructor
82 initialization function for each compilation unit, give it a very
83 high initialization priority, and insert all of our calls to
84 __VLTRegisterPair into our special constructor initialization
85 function.
86
87 The vtable verification feature is controlled by the flag
88 '-fvtable-verify='. There are three flavors of this:
89 '-fvtable-verify=std', '-fvtable-verify=preinit', and
90 '-fvtable-verify=none'. If the option '-fvtable-verfy=preinit' is
91 used, then our constructor initialization function gets put into the
92 preinit array. This is necessary if there are data sets that need
93 to be built very early in execution. If the constructor
94 initialization function gets put into the preinit array, the we also
95 add calls to __VLTChangePermission at the beginning and end of the
96 function. The call at the beginning sets the permissions on the
97 data sets and vtable map variables to read/write, and the one at the
98 end makes them read-only. If the '-fvtable-verify=std' option is
99 used, the constructor initialization functions are executed at their
100 normal time, and the __VLTChangePermission calls are handled
101 differently (see the comments in libstdc++-v3/libsupc++/vtv_rts.cc).
102 The option '-fvtable-verify=none' turns off vtable verification.
103
104 This file contains code for the tree pass that goes through all the
105 statements in each basic block, looking for virtual calls, and
106 inserting a call to __VLTVerifyVtablePointer (with appropriate
107 arguments) before each one. It also contains the hash table
108 functions for the data structures used for collecting the class
109 hierarchy data and building/maintaining the vtable map variable data
110 are defined in gcc/vtable-verify.h. These data structures are
111 shared with the code in the C++ front end that collects the class
112 hierarchy & vtable information and generates the vtable map
113 variables (see cp/vtable-class-hierarchy.c). This tree pass should
114 run just before the gimple is converted to RTL.
115
116 Some implementation details for this pass:
117
118 To find all of the virtual calls, we iterate through all the
119 gimple statements in each basic block, looking for any call
120 statement with the code "OBJ_TYPE_REF". Once we have found the
121 virtual call, we need to find the vtable pointer through which the
122 call is being made, and the type of the object containing the
123 pointer (to find the appropriate vtable map variable). We then use
124 these to build a call to __VLTVerifyVtablePointer, passing the
125 vtable map variable, and the vtable pointer. We insert the
126 verification call just after the gimple statement that gets the
127 vtable pointer out of the object, and we update the next
128 statement to depend on the result returned from
129 __VLTVerifyVtablePointer (the vtable pointer value), to ensure
130 subsequent compiler phases don't remove or reorder the call (it's no
131 good to have the verification occur after the virtual call, for
132 example). To find the vtable pointer being used (and the type of
133 the object) we search backwards through the def_stmts chain from the
134 virtual call (see verify_bb_vtables for more details). */
135
136#include "config.h"
137#include "system.h"
138#include "coretypes.h"
139#include "tree.h"
140#include "basic-block.h"
2fb9a547
AM
141#include "tree-ssa-alias.h"
142#include "internal-fn.h"
143#include "gimple-expr.h"
144#include "is-a.h"
442b4905 145#include "gimple.h"
5be5c238 146#include "gimple-iterator.h"
442b4905
AM
147#include "gimple-ssa.h"
148#include "tree-phinodes.h"
149#include "ssa-iterators.h"
d8a2d370 150#include "stringpool.h"
442b4905 151#include "tree-ssanames.h"
2077db1b
CT
152#include "tree-pass.h"
153#include "cfgloop.h"
154
155#include "vtable-verify.h"
156
157unsigned num_vtable_map_nodes = 0;
158int total_num_virtual_calls = 0;
159int total_num_verified_vcalls = 0;
160
161extern GTY(()) tree verify_vtbl_ptr_fndecl;
162tree verify_vtbl_ptr_fndecl = NULL_TREE;
163
164/* Keep track of whether or not any virtual call were verified. */
165static bool any_verification_calls_generated = false;
166
167unsigned int vtable_verify_main (void);
168
169
170/* The following few functions are for the vtbl pointer hash table
171 in the 'registered' field of the struct vtable_map_node. The hash
172 table keeps track of which vtable pointers have been used in
173 calls to __VLTRegisterPair with that particular vtable map variable. */
174
175/* This function checks to see if a particular VTABLE_DECL and OFFSET are
176 already in the 'registered' hash table for NODE. */
177
178bool
179vtbl_map_node_registration_find (struct vtbl_map_node *node,
180 tree vtable_decl,
181 unsigned offset)
182{
183 struct vtable_registration key;
184 struct vtable_registration **slot;
185
c3284718 186 gcc_assert (node && node->registered.is_created ());
2077db1b
CT
187
188 key.vtable_decl = vtable_decl;
189 slot = (struct vtable_registration **) node->registered.find_slot (&key,
190 NO_INSERT);
191
192 if (slot && (*slot))
193 {
194 unsigned i;
c3284718 195 for (i = 0; i < ((*slot)->offsets).length (); ++i)
2077db1b
CT
196 if ((*slot)->offsets[i] == offset)
197 return true;
198 }
199
200 return false;
201}
202
203/* This function inserts VTABLE_DECL and OFFSET into the 'registered'
204 hash table for NODE. It returns a boolean indicating whether or not
205 it actually inserted anything. */
206
207bool
208vtbl_map_node_registration_insert (struct vtbl_map_node *node,
209 tree vtable_decl,
210 unsigned offset)
211{
212 struct vtable_registration key;
213 struct vtable_registration **slot;
214 bool inserted_something = false;
215
c3284718 216 if (!node || !node->registered.is_created ())
2077db1b
CT
217 return false;
218
219 key.vtable_decl = vtable_decl;
220 slot = (struct vtable_registration **) node->registered.find_slot (&key,
221 INSERT);
222
223 if (! *slot)
224 {
225 struct vtable_registration *node;
226 node = XNEW (struct vtable_registration);
227 node->vtable_decl = vtable_decl;
228
229 (node->offsets).create (10);
230 (node->offsets).safe_push (offset);
231 *slot = node;
232 inserted_something = true;
233 }
234 else
235 {
236 /* We found the vtable_decl slot; we need to see if it already
237 contains the offset. If not, we need to add the offset. */
238 unsigned i;
239 bool found = false;
c3284718 240 for (i = 0; i < ((*slot)->offsets).length () && !found; ++i)
2077db1b
CT
241 if ((*slot)->offsets[i] == offset)
242 found = true;
243
244 if (!found)
245 {
246 ((*slot)->offsets).safe_push (offset);
247 inserted_something = true;
248 }
249 }
250 return inserted_something;
251}
252
253/* Hashtable functions for vtable_registration hashtables. */
254
255inline hashval_t
256registration_hasher::hash (const value_type *p)
257{
258 const struct vtable_registration *n = (const struct vtable_registration *) p;
259 return (hashval_t) (DECL_UID (n->vtable_decl));
260}
261
262inline bool
263registration_hasher::equal (const value_type *p1, const compare_type *p2)
264{
265 const struct vtable_registration *n1 =
266 (const struct vtable_registration *) p1;
267 const struct vtable_registration *n2 =
268 (const struct vtable_registration *) p2;
269 return (DECL_UID (n1->vtable_decl) == DECL_UID (n2->vtable_decl));
270}
271
272/* End of hashtable functions for "registered" hashtables. */
273
274
275
276/* Hashtable definition and functions for vtbl_map_hash. */
277
278struct vtbl_map_hasher : typed_noop_remove <struct vtbl_map_node>
279{
280 typedef struct vtbl_map_node value_type;
281 typedef struct vtbl_map_node compare_type;
282 static inline hashval_t hash (const value_type *);
283 static inline bool equal (const value_type *, const compare_type *);
284};
285
286/* Returns a hash code for P. */
287
288inline hashval_t
289vtbl_map_hasher::hash (const value_type *p)
290{
291 const struct vtbl_map_node n = *((const struct vtbl_map_node *) p);
292 return (hashval_t) IDENTIFIER_HASH_VALUE (n.class_name);
293}
294
295/* Returns nonzero if P1 and P2 are equal. */
296
297inline bool
298vtbl_map_hasher::equal (const value_type *p1, const compare_type *p2)
299{
300 const struct vtbl_map_node n1 = *((const struct vtbl_map_node *) p1);
301 const struct vtbl_map_node n2 = *((const struct vtbl_map_node *) p2);
302 return (IDENTIFIER_HASH_VALUE (n1.class_name) ==
303 IDENTIFIER_HASH_VALUE (n2.class_name));
304}
305
306/* Here are the two structures into which we insert vtable map nodes.
307 We use two data structures because of the vastly different ways we need
308 to find the nodes for various tasks (see comments in vtable-verify.h
309 for more details. */
310
311typedef hash_table <vtbl_map_hasher> vtbl_map_table_type;
312typedef vtbl_map_table_type::iterator vtbl_map_iterator_type;
313
314/* Vtable map variable nodes stored in a hash table. */
315static vtbl_map_table_type vtbl_map_hash;
316
317/* Vtable map variable nodes stored in a vector. */
318vec<struct vtbl_map_node *> vtbl_map_nodes_vec;
319
320/* Return vtbl_map node for CLASS_NAME without creating a new one. */
321
322struct vtbl_map_node *
323vtbl_map_get_node (tree class_type)
324{
325 struct vtbl_map_node key;
326 struct vtbl_map_node **slot;
327
328 tree class_type_decl;
329 tree class_name;
330 unsigned int type_quals;
331
c3284718 332 if (!vtbl_map_hash.is_created ())
2077db1b
CT
333 return NULL;
334
335 gcc_assert (TREE_CODE (class_type) == RECORD_TYPE);
336
337
338 /* Find the TYPE_DECL for the class. */
339 class_type_decl = TYPE_NAME (class_type);
340
341 /* Verify that there aren't any qualifiers on the type. */
342 type_quals = TYPE_QUALS (TREE_TYPE (class_type_decl));
343 gcc_assert (type_quals == TYPE_UNQUALIFIED);
344
345 /* Get the mangled name for the unqualified type. */
346 gcc_assert (HAS_DECL_ASSEMBLER_NAME_P (class_type_decl));
347 class_name = DECL_ASSEMBLER_NAME (class_type_decl);
348
349 key.class_name = class_name;
350 slot = (struct vtbl_map_node **) vtbl_map_hash.find_slot (&key,
351 NO_INSERT);
352 if (!slot)
353 return NULL;
354 return *slot;
355}
356
357/* Return vtbl_map node assigned to BASE_CLASS_TYPE. Create new one
358 when needed. */
359
360struct vtbl_map_node *
361find_or_create_vtbl_map_node (tree base_class_type)
362{
363 struct vtbl_map_node key;
364 struct vtbl_map_node *node;
365 struct vtbl_map_node **slot;
366 tree class_type_decl;
367 unsigned int type_quals;
368
c3284718 369 if (!vtbl_map_hash.is_created ())
2077db1b
CT
370 vtbl_map_hash.create (10);
371
372 /* Find the TYPE_DECL for the class. */
373 class_type_decl = TYPE_NAME (base_class_type);
374
375 /* Verify that there aren't any type qualifiers on type. */
376 type_quals = TYPE_QUALS (TREE_TYPE (class_type_decl));
377 gcc_assert (type_quals == TYPE_UNQUALIFIED);
378
379 gcc_assert (HAS_DECL_ASSEMBLER_NAME_P (class_type_decl));
380 key.class_name = DECL_ASSEMBLER_NAME (class_type_decl);
381 slot = (struct vtbl_map_node **) vtbl_map_hash.find_slot (&key,
382 INSERT);
383
384 if (*slot)
385 return *slot;
386
387 node = XNEW (struct vtbl_map_node);
388 node->vtbl_map_decl = NULL_TREE;
389 node->class_name = key.class_name;
390 node->uid = num_vtable_map_nodes++;
391
392 node->class_info = XNEW (struct vtv_graph_node);
393 node->class_info->class_type = base_class_type;
394 node->class_info->class_uid = node->uid;
395 node->class_info->num_processed_children = 0;
396
397 (node->class_info->parents).create (4);
398 (node->class_info->children).create (4);
399
400 node->registered.create (16);
401
402 node->is_used = false;
403
404 vtbl_map_nodes_vec.safe_push (node);
405 gcc_assert (vtbl_map_nodes_vec[node->uid] == node);
406
407 *slot = node;
408 return node;
409}
410
411/* End of hashtable functions for vtable_map variables hash table. */
412
413/* Given a gimple STMT, this function checks to see if the statement
414 is an assignment, the rhs of which is getting the vtable pointer
415 value out of an object. (i.e. it's the value we need to verify
416 because its the vtable pointer that will be used for a virtual
417 call). */
418
419static bool
420is_vtable_assignment_stmt (gimple stmt)
421{
422
423 if (gimple_code (stmt) != GIMPLE_ASSIGN)
424 return false;
425 else
426 {
427 tree lhs = gimple_assign_lhs (stmt);
428 tree rhs = gimple_assign_rhs1 (stmt);
429
430 if (TREE_CODE (lhs) != SSA_NAME)
431 return false;
432
433 if (TREE_CODE (rhs) != COMPONENT_REF)
434 return false;
435
436 if (! (TREE_OPERAND (rhs, 1))
437 || (TREE_CODE (TREE_OPERAND (rhs, 1)) != FIELD_DECL))
438 return false;
439
440 if (! DECL_VIRTUAL_P (TREE_OPERAND (rhs, 1)))
441 return false;
442 }
443
444 return true;
445}
446
447/* This function attempts to recover the declared class of an object
448 that is used in making a virtual call. We try to get the type from
449 the type cast in the gimple assignment statement that extracts the
450 vtable pointer from the object (DEF_STMT). The gimple statement
451 usually looks something like this:
452
453 D.2201_4 = MEM[(struct Event *)this_1(D)]._vptr.Event */
454
455static tree
456extract_object_class_type (tree rhs)
457{
458 tree result = NULL_TREE;
459
460 /* Try to find and extract the type cast from that stmt. */
461 if (TREE_CODE (rhs) == COMPONENT_REF)
462 {
463 tree op0 = TREE_OPERAND (rhs, 0);
464 tree op1 = TREE_OPERAND (rhs, 1);
465
466 if (TREE_CODE (op1) == FIELD_DECL
467 && DECL_VIRTUAL_P (op1))
468 {
469 if (TREE_CODE (op0) == COMPONENT_REF
470 && TREE_CODE (TREE_OPERAND (op0, 0)) == MEM_REF
471 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0, 0)))== RECORD_TYPE)
472 result = TREE_TYPE (TREE_OPERAND (op0, 0));
473 else
474 result = TREE_TYPE (op0);
475 }
476 else if (TREE_CODE (op0) == COMPONENT_REF)
477 {
478 result = extract_object_class_type (op0);
479 if (result == NULL_TREE
480 && TREE_CODE (op1) == COMPONENT_REF)
481 result = extract_object_class_type (op1);
482 }
483 }
484
485 return result;
486}
487
488/* This function traces forward through the def-use chain of an SSA
489 variable to see if it ever gets used in a virtual function call. It
490 returns a boolean indicating whether or not it found a virtual call in
491 the use chain. */
492
493static bool
494var_is_used_for_virtual_call_p (tree lhs, int *mem_ref_depth)
495{
496 imm_use_iterator imm_iter;
497 bool found_vcall = false;
498 use_operand_p use_p;
499
500 if (TREE_CODE (lhs) != SSA_NAME)
501 return false;
502
503 if (*mem_ref_depth > 2)
504 return false;
505
506 /* Iterate through the immediate uses of the current variable. If
507 it's a virtual function call, we're done. Otherwise, if there's
508 an LHS for the use stmt, add the ssa var to the work list
509 (assuming it's not already in the list and is not a variable
510 we've already examined. */
511
512 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
513 {
514 gimple stmt2 = USE_STMT (use_p);
515
516 if (gimple_code (stmt2) == GIMPLE_CALL)
517 {
518 tree fncall = gimple_call_fn (stmt2);
519 if (TREE_CODE (fncall) == OBJ_TYPE_REF)
520 found_vcall = true;
521 else
522 return false;
523 }
524 else if (gimple_code (stmt2) == GIMPLE_PHI)
525 {
526 found_vcall = var_is_used_for_virtual_call_p
527 (gimple_phi_result (stmt2),
528 mem_ref_depth);
529 }
530 else if (gimple_code (stmt2) == GIMPLE_ASSIGN)
531 {
532 tree rhs = gimple_assign_rhs1 (stmt2);
533 if (TREE_CODE (rhs) == ADDR_EXPR
534 || TREE_CODE (rhs) == MEM_REF)
535 *mem_ref_depth = *mem_ref_depth + 1;
536
537 if (TREE_CODE (rhs) == COMPONENT_REF)
538 {
539 while (TREE_CODE (TREE_OPERAND (rhs, 0)) == COMPONENT_REF)
540 rhs = TREE_OPERAND (rhs, 0);
541
542 if (TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR
543 || TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF)
544 *mem_ref_depth = *mem_ref_depth + 1;
545 }
546
547 if (*mem_ref_depth < 3)
548 found_vcall = var_is_used_for_virtual_call_p
549 (gimple_assign_lhs (stmt2),
550 mem_ref_depth);
551 }
552
553 else
554 break;
555
556 if (found_vcall)
557 return true;
558 }
559
560 return false;
561}
562
563/* Search through all the statements in a basic block (BB), searching
564 for virtual method calls. For each virtual method dispatch, find
565 the vptr value used, and the statically declared type of the
566 object; retrieve the vtable map variable for the type of the
567 object; generate a call to __VLTVerifyVtablePointer; and insert the
568 generated call into the basic block, after the point where the vptr
569 value is gotten out of the object and before the virtual method
570 dispatch. Make the virtual method dispatch depend on the return
571 value from the verification call, so that subsequent optimizations
572 cannot reorder the two calls. */
573
574static void
575verify_bb_vtables (basic_block bb)
576{
577 gimple_seq stmts;
578 gimple stmt = NULL;
579 gimple_stmt_iterator gsi_vtbl_assign;
580 gimple_stmt_iterator gsi_virtual_call;
581
582 stmts = bb_seq (bb);
583 gsi_virtual_call = gsi_start (stmts);
584 for (; !gsi_end_p (gsi_virtual_call); gsi_next (&gsi_virtual_call))
585 {
586 stmt = gsi_stmt (gsi_virtual_call);
587
588 /* Count virtual calls. */
589 if (gimple_code (stmt) == GIMPLE_CALL)
590 {
591 tree fncall = gimple_call_fn (stmt);
592 if (TREE_CODE (fncall) == OBJ_TYPE_REF)
593 total_num_virtual_calls++;
594 }
595
596 if (is_vtable_assignment_stmt (stmt))
597 {
598 tree lhs = gimple_assign_lhs (stmt);
599 tree vtbl_var_decl = NULL_TREE;
600 struct vtbl_map_node *vtable_map_node;
601 tree vtbl_decl = NULL_TREE;
602 gimple call_stmt;
603 const char *vtable_name = "<unknown>";
604 tree tmp0;
605 bool found;
606 int mem_ref_depth = 0;
607
608 /* Make sure this vptr field access is for a virtual call. */
609 if (!var_is_used_for_virtual_call_p (lhs, &mem_ref_depth))
610 continue;
611
612 /* Now we have found the virtual method dispatch and
613 the preceding access of the _vptr.* field... Next
614 we need to find the statically declared type of
615 the object, so we can find and use the right
616 vtable map variable in the verification call. */
617 tree class_type = extract_object_class_type
618 (gimple_assign_rhs1 (stmt));
619
620 gsi_vtbl_assign = gsi_for_stmt (stmt);
621
622 if (class_type
623 && (TREE_CODE (class_type) == RECORD_TYPE)
624 && TYPE_BINFO (class_type))
625 {
626 /* Get the vtable VAR_DECL for the type. */
627 vtbl_var_decl = BINFO_VTABLE (TYPE_BINFO (class_type));
628
629 if (TREE_CODE (vtbl_var_decl) == POINTER_PLUS_EXPR)
630 vtbl_var_decl = TREE_OPERAND (TREE_OPERAND (vtbl_var_decl, 0),
631 0);
632
633 gcc_assert (vtbl_var_decl);
634
635 vtbl_decl = vtbl_var_decl;
636 vtable_map_node = vtbl_map_get_node
637 (TYPE_MAIN_VARIANT (class_type));
638
639 gcc_assert (verify_vtbl_ptr_fndecl);
640
641 /* Given the vtable pointer for the base class of the
642 object, build the call to __VLTVerifyVtablePointer to
643 verify that the object's vtable pointer (contained in
644 lhs) is in the set of valid vtable pointers for the
645 base class. */
646
647 if (vtable_map_node && vtable_map_node->vtbl_map_decl)
648 {
2077db1b
CT
649 vtable_map_node->is_used = true;
650 vtbl_var_decl = vtable_map_node->vtbl_map_decl;
651
652 if (TREE_CODE (vtbl_decl) == VAR_DECL)
653 vtable_name = IDENTIFIER_POINTER (DECL_NAME (vtbl_decl));
654
655 /* Call different routines if we are interested in
656 trace information to debug problems. */
657 if (flag_vtv_debug)
658 {
659 int len1 = IDENTIFIER_LENGTH
660 (DECL_NAME (vtbl_var_decl));
661 int len2 = strlen (vtable_name);
662
663 call_stmt = gimple_build_call
664 (verify_vtbl_ptr_fndecl, 4,
665 build1 (ADDR_EXPR,
666 TYPE_POINTER_TO
667 (TREE_TYPE (vtbl_var_decl)),
668 vtbl_var_decl),
669 lhs,
670 build_string_literal
671 (len1 + 1,
672 IDENTIFIER_POINTER
673 (DECL_NAME
674 (vtbl_var_decl))),
675 build_string_literal (len2 + 1,
676 vtable_name));
677 }
678 else
679 call_stmt = gimple_build_call
680 (verify_vtbl_ptr_fndecl, 2,
681 build1 (ADDR_EXPR,
682 TYPE_POINTER_TO
683 (TREE_TYPE (vtbl_var_decl)),
684 vtbl_var_decl),
685 lhs);
686
687
688 /* Create a new SSA_NAME var to hold the call's
689 return value, and make the call_stmt use the
690 variable for that purpose. */
691 tmp0 = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "VTV");
692 gimple_call_set_lhs (call_stmt, tmp0);
693 update_stmt (call_stmt);
694
31226750 695 /* Replace all uses of lhs with tmp0. */
2077db1b 696 found = false;
31226750
CT
697 imm_use_iterator iterator;
698 gimple use_stmt;
699 FOR_EACH_IMM_USE_STMT (use_stmt, iterator, lhs)
2077db1b 700 {
31226750
CT
701 use_operand_p use_p;
702 if (use_stmt == call_stmt)
703 continue;
704 FOR_EACH_IMM_USE_ON_STMT (use_p, iterator)
705 SET_USE (use_p, tmp0);
706 update_stmt (use_stmt);
707 found = true;
2077db1b 708 }
31226750 709
2077db1b
CT
710 gcc_assert (found);
711
712 /* Insert the new verification call just after the
713 statement that gets the vtable pointer out of the
714 object. */
31226750 715 gcc_assert (gsi_stmt (gsi_vtbl_assign) == stmt);
2077db1b
CT
716 gsi_insert_after (&gsi_vtbl_assign, call_stmt,
717 GSI_NEW_STMT);
718
719 any_verification_calls_generated = true;
720 total_num_verified_vcalls++;
721 }
722 }
723 }
724 }
725}
726
727/* Main function, called from pass->excute(). Loop through all the
728 basic blocks in the current function, passing them to
729 verify_bb_vtables, which searches for virtual calls, and inserts
730 calls to __VLTVerifyVtablePointer. */
731
732unsigned int
733vtable_verify_main (void)
734{
735 unsigned int ret = 1;
736 basic_block bb;
737
738 FOR_ALL_BB (bb)
739 verify_bb_vtables (bb);
740
741 return ret;
742}
743
744/* Gate function for the pass. */
745
746static bool
747gate_tree_vtable_verify (void)
748{
749 return (flag_vtable_verify);
750}
751
752/* Definition of this optimization pass. */
753
754namespace {
755
756const pass_data pass_data_vtable_verify =
757{
758 GIMPLE_PASS, /* type */
759 "vtable-verify", /* name */
760 OPTGROUP_NONE, /* optinfo_flags */
761 true, /* has_gate */
762 true, /* has_execute */
763 TV_VTABLE_VERIFICATION, /* tv_id */
764 ( PROP_cfg | PROP_ssa ), /* properties_required */
765 0, /* properties_provided */
766 0, /* properties_destroyed */
767 0, /* todo_flags_start */
768 TODO_update_ssa, /* todo_flags_finish */
769};
770
771class pass_vtable_verify : public gimple_opt_pass
772{
773public:
c3284718
RS
774 pass_vtable_verify (gcc::context *ctxt)
775 : gimple_opt_pass (pass_data_vtable_verify, ctxt)
2077db1b
CT
776 {}
777
778 /* opt_pass methods: */
779 bool gate () { return gate_tree_vtable_verify (); }
780 unsigned int execute () { return vtable_verify_main (); }
781
782}; // class pass_vtable_verify
783
784} // anon namespace
785
786gimple_opt_pass *
787make_pass_vtable_verify (gcc::context *ctxt)
788{
789 return new pass_vtable_verify (ctxt);
790}
791
792#include "gt-vtable-verify.h"