Fix version check for ATTRIBUTE_GCC_DUMP_PRINTF
[official-gcc.git] / libvtv / testsuite / other-tests / so.cc
blob3f0a346f1e8c71b57165a44ba8b6bce9c754fabb
1 #include <dlfcn.h>
2 #include <assert.h>
3 #include <unistd.h>
4 #include <vtv_fail.h>
6 extern "C" int printf(const char *, ...);
7 extern "C" int sprintf(char *, const char*, ...);
9 static int counter = 0;
10 extern int failures;
12 template <int i> struct base
14 virtual char * whoami() {
15 static char sl[100];
16 sprintf(sl, "I am base %d", i);
17 return sl;
19 virtual void inc() { counter += i; }
22 template <int i> struct derived: base<i>
24 virtual char * whoami() {
25 static char sl[100];
26 sprintf(sl, "I am derived %d", i);
27 return sl;
29 virtual void inc() { counter += (10*i); }
32 // We don't use this class. It is just here so that the
33 // compiler does not devirtualize calls to derived::inc()
34 template <int i> struct derived2: derived<i>
36 virtual void inc() { counter += (20*i); }
39 static base<TPID> * bp = new base<TPID>();
40 static derived<TPID> * dp = new derived<TPID>();
41 static base<TPID> * dbp = new derived<TPID>();
44 // Given 2 pointers to C++ objects (non PODs), exchange the pointers to vtable
45 static void exchange_vtptr(void * object1_ptr, void * object2_ptr)
47 void ** object1_vtptr_ptr = (void **)object1_ptr;
48 void ** object2_vtptr_ptr = (void **)object2_ptr;
49 void * object1_vtptr = *object1_vtptr_ptr;
50 void * object2_vtptr = *object2_vtptr_ptr;
51 *object1_vtptr_ptr = object2_vtptr;
52 *object2_vtptr_ptr = object1_vtptr;
55 #define BUILD_NAME(NAME,ID) NAME##ID
56 #define EXPAND(NAME,X) BUILD_NAME(NAME,X)
57 extern "C" void EXPAND(so_entry_,TPID)(void)
59 int prev_counter;
60 int prev_failures;
62 counter = 0;
63 bp->inc();
64 dp->inc();
65 dbp->inc();
66 assert(counter == (TPID + 10*TPID + 10*TPID));
68 prev_counter = counter;
69 exchange_vtptr(bp, dp);
70 bp->inc(); // This one should succeed but it is calling the wrong member
71 if (counter != (prev_counter + 10*TPID))
73 printf("TPID=%d whoami=%s wrong counter value prev_counter=%d counter=%d\n", TPID, bp->whoami(), prev_counter, counter);
74 sleep(2);
76 assert(counter == (prev_counter + 10*TPID));
77 // printf("Pass first attack!\n");
79 // This one should fail verification!. So it should jump to __vtv_verify_fail above.
80 prev_failures = failures;
81 dp->inc();
82 // this code may be executed by multiple threads at the same time. So, just verify the number of failures has
83 // increased as opposed to check for increase by 1.
84 assert(failures > prev_failures);
85 assert(counter == (prev_counter + 10*TPID + TPID));
86 // printf("TPDI=%d counter %d\n", TPID, counter);
87 // printf("Pass second attack!\n");
89 // restore the vtable pointers to the original state.
90 // This is very important. For some reason the dlclose is not "really" closing the library so when we reopen it we are
91 // getting the old memory state.
92 exchange_vtptr(bp, dp);