c++: further optimize tsubst_template_decl
[official-gcc.git] / libvtv / testsuite / libvtv.cc / thunk_vtable_map_attack.cc
blob51f974ee4e7c3bdd2801b1766b5104520c39d856
1 // { dg-do run }
3 #include <assert.h>
4 #include <signal.h>
5 #include <setjmp.h>
6 #include <stdio.h>
8 #include <iostream>
9 #include <fstream>
11 using std::ofstream;
12 using std::ifstream;
13 using std::ios;
15 struct A {
16 A():value(123) {}
17 int value;
18 virtual int access() { return this->value; }
20 struct B {
21 B():value(456) {}
22 int value;
23 virtual int access() { return this->value; }
25 struct C : public A, public B {
26 C():better_value(789) {}
27 int better_value;
28 virtual int access() { return this->better_value; }
30 struct D: public C {
31 D():other_value(987) {}
32 int other_value;
33 virtual int access() { return this->other_value; }
36 volatile static int signal_count = 0;
38 sigjmp_buf before_segv;
40 static void
41 handler(int sig, siginfo_t *si, void *unused)
44 printf("Got SIGSEGV at address: 0x%lx\n",
45 (long) si->si_addr);
48 signal_count++;
49 /* You are not supposed to longjmp out of a signal handler but it seems
50 to work for this test case and it simplifies it */
51 siglongjmp(before_segv, 1);
52 /* exit(1); */
55 /* Access one of the vtable_map variables generated by this .o */
56 extern void * _ZN4_VTVI1BE12__vtable_mapE;
58 /* Access one of the vtable_map variables generated by libstdc++ */
59 extern void * _ZN4_VTVISt8ios_baseE12__vtable_mapE;
61 int use(B *b)
63 int ret;
65 ret = sigsetjmp(before_segv, 1);
66 if (ret == 0)
68 /* This should generate a segmentation violation. ie: at this point it should
69 be protected */
70 _ZN4_VTVI1BE12__vtable_mapE = 0;
72 assert(ret == 1 && signal_count == 1);
74 ret = sigsetjmp(before_segv, 1);
75 if (ret == 0)
77 /* Try to modify one of the vtable_map variables in the stdc++ library.
78 This should generate a segmentation violation. ie: at this point it
79 should be protected */
80 _ZN4_VTVISt8ios_baseE12__vtable_mapE = 0;
82 assert(ret == 1 && signal_count == 2);
84 return b->access();
87 void myread(std::istream * in)
89 char input_str[50] = "\0";
90 if (in->good())
91 (*in) >> input_str;
92 std::cout << input_str << std::endl;
93 delete in;
96 int main()
98 ifstream * infile = new ifstream("./thunk_vtable_map_attack.cpp");
99 myread(infile);
101 /* Set up handler for SIGSEGV. */
102 struct sigaction sa;
103 sa.sa_flags = SA_SIGINFO;
104 sigemptyset(&sa.sa_mask);
105 sa.sa_sigaction = handler;
106 if (sigaction(SIGSEGV, &sa, NULL) == -1)
107 assert(0);
109 C c;
110 assert(use(&c) == 789);
112 return 0;