3b5e78309dbf301656fa33001bd962fb6be2f144
[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 unsigned int getTrapType(unsigned int, unsigned int);
25
26 #ifdef DEBUG
27 #define dprintf(args...) do { printf (args); } while (0);
28 #else
29 #define dprintf(args...)
30 #endif
31
32 #define NEW_MAP(prefix) \
33         void* prefix ## _map = NULL; \
34         void set_ ## prefix ## map(void *map) \
35         { \
36                 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
37                 prefix ## _map = map; \
38         } \
39         void *get_ ## prefix ## map() \
40         { \
41                 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
42                 return prefix ## _map; \
43         }
44
45 NEW_MAP(method)
46 NEW_MAP(trap)
47 NEW_MAP(class)
48 NEW_MAP(virtual)
49 NEW_MAP(strings)
50 NEW_MAP(interfaces)
51 NEW_MAP(interfacemethod)
52
53
54 void mainresult(unsigned int a)
55 {
56         dprintf("mainresult: 0x%08x\n", a);
57 }
58
59 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
60 {
61         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
62         unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
63         unsigned int *to_patch = (unsigned int *) (from + 1);
64         dprintf("callertrap(mctx)  by 0x%08x\n", from);
65         if (*to_patch != 0x90ffff90) {
66                 dprintf("callertrap: something is wrong here. abort\n");
67                 exit(0);
68         }
69         unsigned int patchme = getMethodEntry(from, 0);
70
71         unsigned char *insn = (unsigned char *) from;
72         *insn = 0xe8; // call opcode
73         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
74         dprintf("*to_patch: 0x%08x\n", *to_patch);
75         *to_patch = patchme - (from + 5);
76         dprintf("*to_patch: 0x%08x\n", *to_patch);
77         mctx->gregs[REG_EIP] = (unsigned long) insn;
78 }
79
80 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
81 {
82         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
83         unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
84         unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
85
86         /* if from is not *the* eip: get actual eip from stack storage */
87         unsigned int from_stack = (*esp) - 3;
88         switch(getTrapType(from, from_stack)) {
89                 default: case 0: {
90                         dprintf("something is wrong here: abort\n");
91                         exit(1);
92                 } break;
93                 case 1: { // invokevirtual
94                         if (from > 0) {
95                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
96                         }
97                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
98                         unsigned char offset = *((unsigned char *) (*esp) - 1);
99                         /* method entry to patch */
100                         unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
101                         dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
102                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
103                         dprintf("*to_patch: 0x%08x\n", *to_patch);
104                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
105                         mctx->gregs[REG_EIP] = *to_patch;
106                         dprintf("*to_patch: 0x%08x\n", *to_patch);
107                 } break;
108                 case 4: { // invokeinterface
109                         if (from > 0) {
110                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
111                         }
112                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
113                         unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
114                         unsigned char offset = *((unsigned char *) (*esp) - 1);
115                         /* interface entry to patch */
116                         unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
117                         dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
118                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
119                         dprintf("*to_patch: 0x%08x\n", *to_patch);
120                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
121                         mctx->gregs[REG_EIP] = *to_patch;
122                         dprintf("*to_patch: 0x%08x\n", *to_patch);
123                 } break;
124                 case 2: { // static field patch
125                         unsigned int *to_patch = (unsigned int *) (from + 2);
126                         dprintf("staticfieldtrap by 0x%08x\n", from);
127                         if (*to_patch != 0x00000000) {
128                                 dprintf("staticfieldtrap: something is wrong here. abort\n");
129                                 exit(0);
130                         }
131                         unsigned int patchme = getStaticFieldAddr(from, trap_map);
132
133                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
134                         dprintf("*to_patch: 0x%08x\n", *to_patch);
135                         *to_patch = patchme;
136                         dprintf("*to_patch: 0x%08x\n", *to_patch);
137                 } break;
138         }
139 }
140
141 void register_signal(void)
142 {
143         struct sigaction illaction;
144         illaction.sa_sigaction = staticcalltrap;
145         sigemptyset(&illaction.sa_mask);
146         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
147         sigaction(SIGILL, &illaction, NULL);
148
149         struct sigaction segvaction;
150         segvaction.sa_sigaction = sigsegvtrap;
151         sigemptyset(&segvaction.sa_mask);
152         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
153         sigaction(SIGSEGV, &segvaction, NULL);
154 }
155
156 unsigned int getaddr(void)
157 {
158         return (unsigned int) mainresult;
159 }
160
161 unsigned int getMallocAddr(void)
162 {
163         return (unsigned int) malloc;
164 }