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