aboutsummaryrefslogtreecommitdiff
path: root/vtable-security/gcc/tree-vtable-verify.c
blob: ba7206892550a7f0bae0bf47815a860a8053078d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* Interprocedural constant propagation
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
   Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* Virtual Table Pointer Security Pass.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "cp/cp-tree.h"
#include "tm_p.h"
#include "basic-block.h"
#include "output.h"
#include "tree-flow.h"
#include "tree-dump.h"
#include "tree-pass.h"
#include "timevar.h"
#include "cfgloop.h"
#include "flags.h"
#include "tree-inline.h"
#include "tree-scalar-evolution.h"
#include "diagnostic-core.h"
#include "gimple-pretty-print.h"
#include "toplev.h"
#include "langhooks.h"

#include "tree-vtable-verify.h"

unsigned num_vtable_map_nodes = 0;
bool any_verification_calls_generated = false;

static GTY(()) tree verify_vtbl_ptr_fndecl = NULL_TREE;

unsigned int vtable_verify_main (void);
static bool gate_tree_vtable_verify (void);
static void build_vtable_verify_fndecl (void);
static tree my_build1 (enum tree_code, tree, tree);

bool
vtbl_map_node_registration_find (struct vtbl_map_node *node,
                                 tree vtable_decl,
                                 unsigned offset)
{
  struct vtable_registration key;
  struct vtable_registration **slot;

  gcc_assert (node);
  gcc_assert (node->registered);

  key.vtable_decl = vtable_decl;
  slot = (struct vtable_registration **) htab_find_slot (node->registered,
                                                         &key, NO_INSERT);

  if (slot && (*slot) && (*slot)->offsets)
    {
      unsigned i;
      for (i = 0; i < (*slot)->cur_offset; ++i)
        if ((*slot)->offsets[i] == offset)
          return true;
    }

  return false;
}

void
vtbl_map_node_registration_insert (struct vtbl_map_node *node,
                                   tree vtable_decl,
                                   unsigned offset)
{
  struct vtable_registration key;
  struct vtable_registration **slot;

  if (!node || !(node->registered))
    return;

  key.vtable_decl = vtable_decl;
  slot = (struct vtable_registration **) htab_find_slot (node->registered,
                                                         &key, INSERT);

  if (!(*slot))
    {
      unsigned i;
      struct vtable_registration *node;
      node = (struct vtable_registration *)
                                 xmalloc (sizeof (struct vtable_registration));
      node->vtable_decl = vtable_decl;
      node->offsets = (unsigned *) xmalloc (10 * sizeof (unsigned));
      for (i= 0; i < 10; ++i)
        node->offsets[i] = 0;
      node->offsets[0] = offset;
      node->cur_offset = 1;
      node->max_offsets = 10;
      *slot = node;
    }
  else
    {
      /* We found the vtable_decl slot; we need to see if it already
         contains the offset.  If not, we need to add the offset.  */
      unsigned i;
      bool found = false;
      for (i = 0; (i < (*slot)->cur_offset) && !found; ++i)
        if ((*slot)->offsets[i] == offset)
          found = true;

      if (!found)
        {
          if ((*slot)->cur_offset == (*slot)->max_offsets)
            {
              unsigned new_max = 2 * (*slot)->max_offsets;
              (*slot)->offsets = (unsigned *)
                  xrealloc ((*slot)->offsets, new_max * sizeof (unsigned));

              for (i = (*slot)->max_offsets; i < new_max; ++i)
                (*slot)->offsets[i] = 0;
              (*slot)->max_offsets = new_max;
            }
          (*slot)->offsets[(*slot)->cur_offset] = offset;
          (*slot)->cur_offset = (*slot)->cur_offset + 1;
        }
    }
}

/* Hashtable functions for vtable_registration hashtables.  */

static hashval_t
hash_vtable_registration (const void * p)
{
  const struct vtable_registration *n = (const struct vtable_registration *) p;
  return (hashval_t) (DECL_UID (n->vtable_decl));
}

static int
eq_vtable_registration (const void *p1, const void *p2)
{
  const struct vtable_registration *n1 =
                                    (const struct vtable_registration *) p1;
  const struct vtable_registration *n2 =
                                    (const struct vtable_registration *) p2;
  return (DECL_UID (n1->vtable_decl) == DECL_UID (n2->vtable_decl));
}

/* End of hashtable functions for "registered" hashtables*/

/* Hashtable functions for vtable_map variables hashtable.  */

static htab_t vtbl_map_hash = NULL;
struct vtbl_map_node *vtbl_map_nodes = NULL;
struct vtbl_map_node **vtbl_map_nodes_array = NULL;

static void
vtable_map_array_insert (struct vtbl_map_node *node)
{
  static unsigned array_size = 0;
  unsigned i;

  if (vtbl_map_nodes_array == NULL
      || array_size == 0)
    {
      array_size = 16;
      vtbl_map_nodes_array = (struct vtbl_map_node **)
                       xmalloc (array_size * sizeof (struct vtbl_map_node *));
      memset (vtbl_map_nodes_array, 0,
              array_size * sizeof (struct vtbl_map_node *));
    }
  else if (node->uid >= array_size)
    {
      unsigned new_size = 2 * array_size;
      vtbl_map_nodes_array = (struct vtbl_map_node **)
          xrealloc (vtbl_map_nodes_array,
                    new_size * sizeof (struct vtbl_map_node *));

      for (i = array_size; i < new_size; ++i)
        vtbl_map_nodes_array[i] = NULL;

      array_size = new_size;
    }

  gcc_assert (node->uid < array_size);
  gcc_assert (vtbl_map_nodes_array[node->uid] == NULL);

  vtbl_map_nodes_array[node->uid] = node;
}

/* Returns a hash code for P.  */
static hashval_t
hash_vtbl_map_node (const void *p)
{
  const struct vtbl_map_node *n = (const struct vtbl_map_node *) p;
  return (hashval_t) IDENTIFIER_HASH_VALUE (n->class_name);
}

/* Returns nonzero if P1 and P2 are equal.  */
static int
eq_vtbl_map_node (const void *p1, const void *p2)
{
  const struct vtbl_map_node *n1 = (const struct vtbl_map_node *) p1;
  const struct vtbl_map_node *n2 = (const struct vtbl_map_node *) p2;
  return (IDENTIFIER_HASH_VALUE (n1->class_name) ==
          IDENTIFIER_HASH_VALUE (n2->class_name));
}

/* Return vtbl_map node assigned to DECL without creating a new one.  */
struct vtbl_map_node *
vtbl_map_get_node (const_tree class_name)
{
  struct vtbl_map_node key;
  struct vtbl_map_node **slot;

  if (!vtbl_map_hash)
    return NULL;

  key.class_name = CONST_CAST2 (tree, const_tree, class_name);
  slot = (struct vtbl_map_node **) htab_find_slot (vtbl_map_hash, &key,
                                                   NO_INSERT);
  if (!slot)
    return NULL;
  return *slot;
}

/* Return vtbl_map node assigned to BASE_CLASS_TYPE.  Create new one
 * when needed.  */
struct vtbl_map_node *
vtbl_map_node (tree base_class_type)
{
  struct vtbl_map_node key;
  struct vtbl_map_node *node;
  struct vtbl_map_node **slot;
  unsigned i;

  if (!vtbl_map_hash)
    vtbl_map_hash = htab_create (10, hash_vtbl_map_node,
                                 eq_vtbl_map_node, NULL);

  if (TREE_CHAIN (base_class_type))
    key.class_name = DECL_ASSEMBLER_NAME (TREE_CHAIN (base_class_type));
  else
    key.class_name = DECL_ASSEMBLER_NAME (TYPE_NAME (base_class_type));
  slot = (struct vtbl_map_node **) htab_find_slot (vtbl_map_hash, &key,
                                                   INSERT);
  if (*slot)
    return *slot;

  node = (struct vtbl_map_node *) xmalloc (sizeof (struct vtbl_map_node));
  node->vtbl_map_decl = NULL_TREE;
  node->class_name = key.class_name;
  node->uid = num_vtable_map_nodes++;

  node->class_info = (struct vtv_graph_node *)
                                      xmalloc (sizeof (struct vtv_graph_node));
  node->class_info->class_type = base_class_type;
  node->class_info->class_uid = node->uid;
  node->class_info->max_parents = 4;
  node->class_info->max_children = 4;
  node->class_info->num_parents = 0;
  node->class_info->num_children = 0;
  node->class_info->num_processed_children = 0;
  node->class_info->parents = (struct vtv_graph_node **)
                                xmalloc (4 * sizeof (struct vtv_graph_node *));
  node->class_info->children = (struct vtv_graph_node **)
                                xmalloc (4 * sizeof (struct vtv_graph_node *));
  for (i = 0; i < 4; ++i)
    {
      node->class_info->parents[i] = NULL;
      node->class_info->children[i] = NULL;
    }

  node->registered = htab_create (16, hash_vtable_registration,
                                  eq_vtable_registration, NULL);
  node->is_used = false;
  node->next = vtbl_map_nodes;
  if (vtbl_map_nodes)
    vtbl_map_nodes->prev = node;

  vtable_map_array_insert (node);

  vtbl_map_nodes = node;
  *slot = node;
  return node;
}

/* End of hashtable functions for vtable_map variables hash table.   */

static tree
my_build1 (enum tree_code code, tree type, tree node MEM_STAT_DECL)
{
  int length = sizeof (struct tree_exp);
#ifdef GATHER_STATISTICS
  tree_node_kind kind;
#endif
  tree t;

#ifdef GATHER_STATISTICS
  switch (TREE_CODE_CLASS (code))
    {
    case tcc_statement:  /* an expression with side effects */
      kind = s_kind;
      break;
    case tcc_reference:  /* a reference */
      kind = r_kind;
      break;
    default:
      kind = e_kind;
      break;
    }

  tree_node_counts[(int) kind]++;
  tree_node_sizes[(int) kind] += length;
#endif

  gcc_assert (TREE_CODE_LENGTH (code) == 1);

  t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);

  memset (t, 0, sizeof (struct tree_common));

  TREE_SET_CODE (t, code);

  TREE_TYPE (t) = type;
  SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
  TREE_OPERAND (t, 0) = node;
  TREE_BLOCK (t) = NULL_TREE;
  if (node && !TYPE_P (node))
    {
      TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
      TREE_READONLY (t) = TREE_READONLY (node);
    }

  if (TREE_CODE_CLASS (code) == tcc_statement)
    TREE_SIDE_EFFECTS (t) = 1;
  else switch (code)
    {
    case VA_ARG_EXPR:
      /* All of these have side-effects, no matter what their
         operands are.  */
      TREE_SIDE_EFFECTS (t) = 1;
      TREE_READONLY (t) = 0;
      break;

    case INDIRECT_REF:
      /* Whether a dereference is readonly has nothing to do with whether
         its operand is readonly.  */
      TREE_READONLY (t) = 0;
      break;

    case ADDR_EXPR:
      if (node)
        recompute_tree_invariant_for_addr_expr (t);
      break;

    default:
      if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
          && node && !TYPE_P (node)
          && TREE_CONSTANT (node))
        TREE_CONSTANT (t) = 1;
      if (TREE_CODE_CLASS (code) == tcc_reference
          && node && TREE_THIS_VOLATILE (node))
        TREE_THIS_VOLATILE (t) = 1;
      break;
    }

  return t;
}

static int
type_name_is_vtable_pointer (tree node)
{

  if (TYPE_NAME (node))
  {
    if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
      return (strcmp (IDENTIFIER_POINTER (TYPE_NAME (node)),
                      "__vtbl_ptr_type") == 0);
    else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
             && DECL_NAME (TYPE_NAME (node)))
      return (strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node))),
                      "__vtbl_ptr_type") == 0);
    else
      return 0;
  }

  return 0;
}

static int
is_vtable_assignment_stmt (gimple stmt)
{

  if (gimple_code (stmt) != GIMPLE_ASSIGN)
    return 0;
  else
    {
      tree lhs = gimple_assign_lhs (stmt);
      tree rhs = gimple_assign_rhs1 (stmt);

      if (TREE_CODE (lhs) != SSA_NAME)
        return 0;

      if (TREE_CODE (TREE_TYPE (lhs)) != POINTER_TYPE)
        return 0;

      if (TREE_CODE (TREE_TYPE (TREE_TYPE (lhs))) != POINTER_TYPE)
        return 0;

      if (! type_name_is_vtable_pointer (TREE_TYPE (TREE_TYPE (lhs))))
        return 0;


      if (TREE_CODE (rhs) != COMPONENT_REF)
        return 0;

      if (! (TREE_OPERAND (rhs, 1))
          || (TREE_CODE (TREE_OPERAND (rhs, 1)) != FIELD_DECL))
        return 0;

      if (! (DECL_NAME (TREE_OPERAND (rhs, 1)))
          || (strncmp (IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (rhs, 1))),
                       "_vptr.", 6) != 0))
        return 0;

      while ((TREE_OPERAND (rhs, 0))
             && (TREE_CODE (TREE_OPERAND (rhs, 0)) == COMPONENT_REF))
        rhs = TREE_OPERAND (rhs, 0);

      if (! (TREE_OPERAND (rhs, 0))
          || (TREE_CODE (TREE_OPERAND (rhs, 0)) != MEM_REF))
        return 0;
    }

    return 1;
}

/* This is a copy of the function in cp/class.c.  For some reason that file
   does not get linked with this one during the gcc build process, so we
   need to reproduce the function here in order to be able to call it.  */

static tree
my_get_vtbl_decl_for_binfo (tree binfo)
{
  tree decl;

  decl = BINFO_VTABLE (binfo);
  if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
  {
    gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
    decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
  }
  if (decl)
    gcc_assert (TREE_CODE (decl) == VAR_DECL);

  return decl;
}

static void
verify_bb_vtables (basic_block bb)
{
  gimple_seq stmts;
  gimple stmt = NULL;
  bool bb_contains_virtual_call = false;
  gimple_stmt_iterator gsi_vtbl_assign;
  gimple_stmt_iterator gsi_virtual_call;

  /* If we are in the global 'main' function, the very first thing
     we need to do in the very first BB is to tell the __VerifyVtablePointer
     function to actually start verification (it does not do verification
     before 'main', during library and constructor initialization, because
     the necessary verification data structures may not be complete then).
     The assumption is that no hacker will be able to attack the program before
    'main'... */

  /* Search the basic block to see if it contains a virtual method
     call, i.e. a call with the tree code OBJ_TYPE_REF  */

  stmts = bb_seq (bb);
  gsi_virtual_call = gsi_start (stmts);
  for (; !gsi_end_p (gsi_virtual_call); gsi_next (&gsi_virtual_call))
    {
      stmt = gsi_stmt (gsi_virtual_call);
      if (gimple_code (stmt) == GIMPLE_CALL)
        {
          tree fncall = gimple_call_fn (stmt);
          if (TREE_CODE (fncall) == OBJ_TYPE_REF)
            {
              bb_contains_virtual_call = true;
              break;
            }
        }
    }

  if (bb_contains_virtual_call)
    {
      /* If we found a virtual method call, it must be preceded by assigning
         the vtable pointer in the object to something.  Search backwards
         from the virtual method call to the assignment statement whose
         right hand side is a vtable pointer.  */

      bool found = false;
      gsi_vtbl_assign = gsi_virtual_call;
      for (; !gsi_end_p (gsi_vtbl_assign) && !found;
           gsi_prev (&gsi_vtbl_assign))
        {
          stmt = gsi_stmt (gsi_vtbl_assign);
          if (is_vtable_assignment_stmt (stmt))
            {
              tree rhs = gimple_assign_rhs1 (stmt);
              tree lhs = gimple_assign_lhs (stmt);
              tree vtbl_var_decl = NULL_TREE;
              tree vtbl = NULL_TREE;
              const char *vtable_name = NULL;
              char *vtbl_map_name = NULL;
              tree var_id;
              gimple_seq pre_p = NULL;

              /* Now we have found the virtual method dispatch and the
                 preceding access of the _vptr.* field... Now we need to
                 find the vtable for the base class (statically declared type)
                 of the object.  */

              found = true;
              if (TREE_CODE (rhs) == COMPONENT_REF)
                {
                  bool vtbl_found = false;
                  struct vtbl_map_node *vtable_map_node = NULL;

                  while (!vtbl_found)
                    {
                      if (TREE_CODE (rhs) == COMPONENT_REF
                          && (TREE_CODE (TREE_OPERAND (rhs, 1)) == FIELD_DECL)
                          && (strncmp (IDENTIFIER_POINTER
				           (DECL_NAME (TREE_OPERAND (rhs, 1))),
                                       "_vptr.", 6) == 0))
                        {
                          vtbl_found = true;
                        }
                      rhs = TREE_OPERAND (rhs, 0);
                    }
                  if ((TREE_CODE (TREE_TYPE (rhs)) == RECORD_TYPE)
                      && TYPE_BINFO (TREE_TYPE (rhs)))
                    {
                      vtbl_var_decl = my_get_vtbl_decl_for_binfo
                                                (TYPE_BINFO (TREE_TYPE (rhs)));
                      vtbl = BINFO_VTABLE (TYPE_BINFO (TREE_TYPE (rhs)));

                      if (!vtbl_var_decl || !vtbl)
                        /* Problem:  This does NOT end up checking virtual
                           functions if there is NO inheritance involved.  */
                        continue;

                      if (TREE_CODE (TREE_TYPE (vtbl)) == POINTER_TYPE)
                        force_gimple_operand (vtbl, &pre_p, 1, NULL);

                      vtable_name = IDENTIFIER_POINTER
			                          (DECL_NAME (vtbl_var_decl));
                      vtbl_map_name = (char *) xmalloc
			                           (strlen (vtable_name) + 12);
                      sprintf (vtbl_map_name, "%s.vtable_map", vtable_name);

                      if (TREE_CHAIN (TREE_TYPE (rhs)))
                        var_id = DECL_ASSEMBLER_NAME (TREE_CHAIN
                                                      (TREE_TYPE (rhs)));
                      else
                        var_id = DECL_ASSEMBLER_NAME (TYPE_NAME
                                                      (TREE_TYPE (rhs)));
                      vtable_map_node = vtbl_map_get_node (var_id);
                      if (vtable_map_node)
                        {
                          vtbl_var_decl = vtable_map_node->vtbl_map_decl;
                          vtable_map_node->is_used = true;
                        }
                      else
                        vtbl_var_decl = NULL;
                    }

                  /* Build  verify_vtbl_ptr_fndecl */

                  build_vtable_verify_fndecl ();

                  /* Given the vtable pointer for the base class of the object,
                     build the call to __VerifyVtablePointer to verify that the
                     object's vtable pointer (contained in lhs) is in the set
                     of valid vtable pointers for the base class.  */

                  if (verify_vtbl_ptr_fndecl && vtbl_var_decl)
                    {
                      tree expr_tree = NULL_TREE;
                      struct gimplify_ctx gctx;
                      tree arg4 = TREE_OPERAND (rhs, 0);
                      const char *vtable_name = "<unknown>";

                      int len1 = 0;
                      int len2 = 0;

                      while (TREE_CODE (arg4) == COMPONENT_REF)
                        arg4 = TREE_OPERAND (arg4, 0);

                      if (TREE_CODE (arg4) == MEM_REF)
                        arg4 = TREE_OPERAND (arg4, 0);

                      if ((TREE_CODE (arg4) == SSA_NAME)
                          && (DECL_NAME (SSA_NAME_VAR (arg4))))
                        vtable_name = IDENTIFIER_POINTER
                                             (DECL_NAME (SSA_NAME_VAR (arg4)));

                      push_gimplify_context (&gctx);
                      len1 = strlen (IDENTIFIER_POINTER
				                  (DECL_NAME (vtbl_var_decl)));
                      len2 = strlen (vtable_name);

                      expr_tree = build_call_expr
			             (verify_vtbl_ptr_fndecl, 6,
				      my_build1 (ADDR_EXPR,
						 TYPE_POINTER_TO
						   (TREE_TYPE (vtbl_var_decl)),
						 vtbl_var_decl),
				      SSA_NAME_VAR (lhs),
				      build_string_literal
				                  (len1,
						   IDENTIFIER_POINTER
						       (DECL_NAME
							    (vtbl_var_decl))),
				      build_int_cst (integer_type_node,
						     len1),
				      build_string_literal (len2, vtable_name),
				      build_int_cst (integer_type_node,
						     len2));
		      /*  Eventually we will remove the 4 'debugging'
			  parameters from the call above and will use the
			  call below instead.
                      expr_tree = build_call_expr
		                     (verify_vtbl_ptr_fndecl, 2,
				      my_build1 (ADDR_EXPR,
				                 TYPE_POINTER_TO
                                                   (TREE_TYPE (vtbl_var_decl)),
                                                 vtbl_var_decl),
                                      SSA_NAME_VAR (lhs));
		      */
                      /* Assign the result of the call to the original
                         variable receiving the assignment of the object's
                         vtable pointer; mark that variable to be updated by
                         update_ssa.  */

                      mark_sym_for_renaming (SSA_NAME_VAR (lhs));
                      force_gimple_operand (expr_tree, &pre_p, 1,
                                            SSA_NAME_VAR (lhs));

                      /* Insert the new call just after the original assignment
                         of the object's vtable pointer.  */

                      pop_gimplify_context (NULL);
                      gsi_insert_seq_after (&gsi_vtbl_assign, pre_p,
                                            GSI_NEW_STMT);
                      any_verification_calls_generated = true;
                    }
                }
            }
        }
    }
}

static void
build_vtable_verify_fndecl (void)
{
  tree void_ptr_type = build_pointer_type (void_type_node);
  tree char_ptr_type = build_pointer_type (char_type_node);
  tree arg_types = NULL_TREE;
  tree type = build_pointer_type (void_type_node);
  struct lang_decl *ld;

  ld = ggc_alloc_cleared_lang_decl (sizeof (struct lang_decl_fn));
  ld->u.base.selector = 1;

  arg_types = build_tree_list (NULL_TREE, build_pointer_type (void_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, void_ptr_type));
  /* Start: Arg types to be removed when we remove debugging parameters from
     the library function. */
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, char_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE,
                                                   integer_type_node));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, char_ptr_type));
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE,
                                                   integer_type_node));
  /* End: Arg types to be removed...*/
  arg_types = chainon (arg_types, build_tree_list (NULL_TREE, void_type_node));

  type = build_function_type (type, arg_types);

  verify_vtbl_ptr_fndecl = build_fn_decl ("__VerifyVtablePointer", type);
  TREE_NOTHROW (verify_vtbl_ptr_fndecl) = 1;
  DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl)
      = tree_cons (get_identifier ("leaf"), NULL,
                   DECL_ATTRIBUTES (verify_vtbl_ptr_fndecl));
  TREE_PUBLIC (verify_vtbl_ptr_fndecl) = 1;
  DECL_PRESERVE_P (verify_vtbl_ptr_fndecl) = 1;
  DECL_LANG_SPECIFIC (verify_vtbl_ptr_fndecl) = ld;
  SET_DECL_LANGUAGE (verify_vtbl_ptr_fndecl, lang_cplusplus);
}

unsigned int
vtable_verify_main (void)
{
  unsigned int ret = 1;
  basic_block bb;

  FOR_ALL_BB (bb)
      verify_bb_vtables (bb);

  return ret;
}

static bool
gate_tree_vtable_verify (void)
{
  return (flag_vtable_verify
          && (strcmp (lang_hooks.name, "GNU C++") == 0));
}

struct gimple_opt_pass pass_vtable_verify =
{
 {
  GIMPLE_PASS,
  "vtable-verify",                      /* name */
  gate_tree_vtable_verify,              /* gate */
  vtable_verify_main,                   /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_VTABLE_VERIFICATION,               /* tv_id */
  PROP_cfg | PROP_ssa,                  /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
  TODO_dump_func | TODO_update_ssa
    | TODO_ggc_collect                  /* todo_flags_finish */
 }
};