globalvars: get rid of `trap_map'
[mate.git] / ffi / trap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "../debug.h"
5
6 /* TODO(bernhard): use {u,}int* types */
7
8 #define __USE_GNU
9 // Note by hs: my signal.h includes sys/uconctext which conflicts with
10 // asm/ucontext - this hack kinda solves the problem for me ;-) 
11 // so feel free to blame me for that s**t
12 #if defined __USE_XOPEN2K8
13 #undef __USE_XOPEN2K8
14 #define RESTORE
15 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
16 #endif
17 #include <signal.h>
18 #ifdef RESTORE
19 #define __USE_XOPEN2K8
20 #endif
21
22 #include <sys/ucontext.h>
23
24 unsigned int getMethodEntry(unsigned int, unsigned int);
25 unsigned int getStaticFieldAddr(unsigned int);
26 unsigned int getTrapType(unsigned int, unsigned int);
27 unsigned int mallocObject(int);
28
29 #ifdef DBG_TRAP
30 #define dprintf(args...) do { printf (args); } while (0);
31 #else
32 #define dprintf(args...)
33 #endif
34
35 #define NEW_MAP(prefix) \
36         void* prefix ## _map = NULL; \
37         void set_ ## prefix ## map(void *map) \
38         { \
39                 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
40                 prefix ## _map = map; \
41         } \
42         void *get_ ## prefix ## map() \
43         { \
44                 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
45                 return prefix ## _map; \
46         }
47
48 NEW_MAP(method)
49 NEW_MAP(trap)
50 NEW_MAP(class)
51 NEW_MAP(virtual)
52 NEW_MAP(strings)
53 NEW_MAP(interfaces)
54 NEW_MAP(interfacemethod)
55
56
57 void mainresult(unsigned int a)
58 {
59         dprintf("mainresult: 0x%08x\n", a);
60 }
61
62 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
63 {
64         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
65         unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
66         unsigned int *to_patch = (unsigned int *) (from + 1);
67         dprintf("callertrap(mctx)  by 0x%08x\n", from);
68         if (*to_patch != 0x90ffff90) {
69                 dprintf("callertrap: something is wrong here. abort\n");
70                 exit(0);
71         }
72         unsigned int patchme = getMethodEntry(from, 0);
73
74         unsigned char *insn = (unsigned char *) from;
75         *insn = 0xe8; // call opcode
76         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
77         dprintf("*to_patch: 0x%08x\n", *to_patch);
78         *to_patch = patchme - (from + 5);
79         dprintf("*to_patch: 0x%08x\n", *to_patch);
80         mctx->gregs[REG_EIP] = (unsigned long) insn;
81 }
82
83 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
84 {
85         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
86         unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
87         unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
88
89         /* if from is not *the* eip: get actual eip from stack storage */
90         unsigned int from_stack = (*esp) - 3;
91         switch(getTrapType(from, from_stack)) {
92                 default: case 0: {
93                         dprintf("something is wrong here: abort\n");
94                         exit(1);
95                 } break;
96                 case 1: { // invokevirtual
97                         if (from > 0) {
98                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
99                         }
100                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
101                         unsigned char offset = *((unsigned char *) (*esp) - 1);
102                         /* method entry to patch */
103                         unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
104                         dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
105                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
106                         dprintf("*to_patch: 0x%08x\n", *to_patch);
107                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
108                         mctx->gregs[REG_EIP] = *to_patch;
109                         dprintf("*to_patch: 0x%08x\n", *to_patch);
110                 } break;
111                 case 4: { // invokeinterface
112                         if (from > 0) {
113                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
114                         }
115                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
116                         unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
117                         unsigned char offset = *((unsigned char *) (*esp) - 1);
118                         /* interface entry to patch */
119                         unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
120                         dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
121                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
122                         dprintf("*to_patch: 0x%08x\n", *to_patch);
123                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
124                         mctx->gregs[REG_EIP] = *to_patch;
125                         dprintf("*to_patch: 0x%08x\n", *to_patch);
126                 } break;
127                 case 2: { // static field patch
128                         unsigned int *to_patch = (unsigned int *) (from + 2);
129                         dprintf("staticfieldtrap by 0x%08x\n", from);
130                         if (*to_patch != 0x00000000) {
131                                 dprintf("staticfieldtrap: something is wrong here. abort\n");
132                                 exit(0);
133                         }
134                         unsigned int patchme = getStaticFieldAddr(from);
135
136                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
137                         dprintf("*to_patch: 0x%08x\n", *to_patch);
138                         *to_patch = patchme;
139                         dprintf("*to_patch: 0x%08x\n", *to_patch);
140                 } break;
141         }
142 }
143
144 void register_signal(void)
145 {
146         struct sigaction illaction;
147         illaction.sa_sigaction = staticcalltrap;
148         sigemptyset(&illaction.sa_mask);
149         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
150         sigaction(SIGILL, &illaction, NULL);
151
152         struct sigaction segvaction;
153         segvaction.sa_sigaction = sigsegvtrap;
154         sigemptyset(&segvaction.sa_mask);
155         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
156         sigaction(SIGSEGV, &segvaction, NULL);
157 }
158
159 unsigned int getaddr(void)
160 {
161         return (unsigned int) mainresult;
162 }
163
164 unsigned int getMallocObjectAddr(void)
165 {
166         return (unsigned int) mallocObject;
167 }