annotate libvtv/testsuite/other-tests/temp_deriv2.cc @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Compile with /home/llozano/local2/proj/vtable/gcc-root/usr/local/bin/g++ -m32 -fvtable-verify=std -fpic -rdynamic -Wl,-R,/home/llozano/local2/proj/vtable/gcc-root/usr/local/lib32:./lib32 -I/home/llozano/local2/proj/vtable/vt2/gcc-4_6-mobile-vtable-security//libstdc++-v3/libsupc++ temp_deriv.cc -O0 -ldl -lpthread -Wl,--whole-archive,-lvtv_init,--no-whole-archive,-z,relro -DTPID=0 -g
kono
parents:
diff changeset
2 // Look at assembly with: objdump -drl a.out
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 #include <dlfcn.h>
kono
parents:
diff changeset
5 #include <assert.h>
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 extern "C" int printf(const char *, ...);
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 static int counter = 0;
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 int i = TPID;
kono
parents:
diff changeset
12 struct base
kono
parents:
diff changeset
13 {
kono
parents:
diff changeset
14 virtual void inc() { counter += i; }
kono
parents:
diff changeset
15 };
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 struct derived: public base
kono
parents:
diff changeset
18 {
kono
parents:
diff changeset
19 virtual void inc() { counter += (10*i); }
kono
parents:
diff changeset
20 };
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 // We don't use this class. It is just here so that the
kono
parents:
diff changeset
23 // compiler does not devirtualize calls to derived::inc()
kono
parents:
diff changeset
24 struct derived2: public derived
kono
parents:
diff changeset
25 {
kono
parents:
diff changeset
26 virtual void inc() { counter += (20*i); }
kono
parents:
diff changeset
27 };
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 static base * bp = new base();
kono
parents:
diff changeset
30 static derived * dp = new derived();
kono
parents:
diff changeset
31 static base * dbp = new derived();
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 // Given 2 pointers to C++ objects (non PODs), exchange the pointers to vtable
kono
parents:
diff changeset
34 void exchange_vtptr(void * object1_ptr, void * object2_ptr)
kono
parents:
diff changeset
35 {
kono
parents:
diff changeset
36 typedef void * vtptr;
kono
parents:
diff changeset
37 vtptr * object1_vtptr_ptr = (vtptr *)object1_ptr;
kono
parents:
diff changeset
38 vtptr * object2_vtptr_ptr = (vtptr *)object2_ptr;
kono
parents:
diff changeset
39 vtptr object1_vtptr = *object1_vtptr_ptr;
kono
parents:
diff changeset
40 vtptr object2_vtptr = *object2_vtptr_ptr;
kono
parents:
diff changeset
41 *object1_vtptr_ptr = object2_vtptr;
kono
parents:
diff changeset
42 *object2_vtptr_ptr = object1_vtptr;
kono
parents:
diff changeset
43 }
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 main()
kono
parents:
diff changeset
46 {
kono
parents:
diff changeset
47 int prev_counter;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 exchange_vtptr(bp, dp);
kono
parents:
diff changeset
50 exchange_vtptr(bp, dp);
kono
parents:
diff changeset
51 exchange_vtptr(bp, dbp);
kono
parents:
diff changeset
52 exchange_vtptr(bp, dbp);
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 counter = 0;
kono
parents:
diff changeset
55 bp->inc();
kono
parents:
diff changeset
56 dp->inc();
kono
parents:
diff changeset
57 dbp->inc();
kono
parents:
diff changeset
58 assert(counter == (TPID + 10*TPID + 10*TPID));
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 prev_counter = counter;
kono
parents:
diff changeset
61 exchange_vtptr(bp, dp);
kono
parents:
diff changeset
62 bp->inc(); // This one should succeed but it is calling the wrong member
kono
parents:
diff changeset
63 assert(counter == (prev_counter + 10*TPID));
kono
parents:
diff changeset
64 printf("Pass first attack!\n");
kono
parents:
diff changeset
65 dp->inc();
kono
parents:
diff changeset
66 printf("TPDI=%d counter %d\n", TPID, counter);
kono
parents:
diff changeset
67 printf("Pass second attack!\n");
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 }