invokevirtual: get the actual class at run-time
[mate.git] / ffi / trap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 /* TODO(bernhard): use {u,}int* types */
5
6 #define __USE_GNU
7 // Note by hs: my signal.h includes sys/uconctext which conflicts with
8 // asm/ucontext - this hack kinda solves the problem for me ;-) 
9 // so feel free to blame me for that s**t
10 #if defined __USE_XOPEN2K8
11 #undef __USE_XOPEN2K8
12 #define RESTORE
13 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
14 #endif
15 #include <signal.h>
16 #ifdef RESTORE
17 #define __USE_XOPEN2K8
18 #endif
19
20 #include <sys/ucontext.h>
21
22 unsigned int getMethodEntry(unsigned int, unsigned int);
23 unsigned int getStaticFieldAddr(unsigned int, void*);
24
25 #define NEW_MAP(prefix) \
26         void* prefix ## _map = NULL; \
27         void set_ ## prefix ## map(void *map) \
28         { \
29                 printf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
30                 prefix ## _map = map; \
31         } \
32         void *get_ ## prefix ## map() \
33         { \
34                 printf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
35                 return prefix ## _map; \
36         }
37
38 NEW_MAP(method)
39 NEW_MAP(trap)
40 NEW_MAP(class)
41 NEW_MAP(virtual)
42
43
44 void mainresult(unsigned int a)
45 {
46         printf("mainresult: 0x%08x\n", a);
47 }
48
49 void callertrap(int nSignal, siginfo_t *info, void *ctx)
50 {
51         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
52         unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
53         unsigned int *to_patch = (unsigned int *) (from + 1);
54         printf("callertrap(mctx)  by 0x%08x\n", from);
55         if (*to_patch != 0x90ffff90) {
56                 printf("callertrap: something is wrong here. abort\n");
57                 exit(0);
58         }
59         unsigned int patchme = getMethodEntry(from, 0);
60
61         unsigned char *insn = (unsigned char *) from;
62         *insn = 0xe8; // call opcode
63         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
64         printf("*to_patch: 0x%08x\n", *to_patch);
65         *to_patch = patchme - (from + 5);
66         printf("*to_patch: 0x%08x\n", *to_patch);
67         mctx->gregs[REG_EIP] = (unsigned long) insn;
68 }
69
70 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
71 {
72         /* TODO(bernhard): more generic and cleaner please... */
73         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
74         unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
75         if (from < 0x10000) { // invokevirtual
76                 if (from > 0) {
77                         printf("from: 0x%08x but should be 0 :-(\n", from);
78                 }
79                 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
80                 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
81                 /* get actual eip from stack storage */
82                 unsigned int from = (*esp) - 3;
83                 unsigned char offset = *((unsigned char *) (*esp) - 1);
84                 /* method entry to patch */
85                 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
86                 printf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
87                 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
88                 printf("*to_patch: 0x%08x\n", *to_patch);
89                 *to_patch = getMethodEntry(from, method_table_ptr);
90                 mctx->gregs[REG_EIP] = *to_patch;
91                 printf("*to_patch: 0x%08x\n", *to_patch);
92         } else {
93         unsigned int *to_patch = (unsigned int *) (from + 2);
94         printf("staticfieldtrap by 0x%08x\n", from);
95         if (*to_patch != 0x00000000) {
96                 printf("staticfieldtrap: something is wrong here. abort\n");
97                 exit(0);
98         }
99         unsigned int patchme = getStaticFieldAddr(from, trap_map);
100
101         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
102         printf("*to_patch: 0x%08x\n", *to_patch);
103         *to_patch = patchme;
104         printf("*to_patch: 0x%08x\n", *to_patch);
105         }
106 }
107
108 void register_signal(void)
109 {
110         struct sigaction illaction;
111         illaction.sa_sigaction = callertrap;
112         sigemptyset(&illaction.sa_mask);
113         illaction.sa_flags = SA_SIGINFO | SA_RESTART;
114         sigaction(SIGILL, &illaction, NULL);
115
116         struct sigaction segvaction;
117         segvaction.sa_sigaction = staticfieldtrap;
118         sigemptyset(&segvaction.sa_mask);
119         segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
120         sigaction(SIGSEGV, &segvaction, NULL);
121 }
122
123 unsigned int getaddr(void)
124 {
125         return (unsigned int) mainresult;
126 }
127
128 unsigned int getMallocAddr(void)
129 {
130         return (unsigned int) malloc;
131 }