bbe14a4a74e7000a19fa911b048e7436e17b4739
[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 callertrap(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 staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
81 {
82         /* TODO(bernhard): more generic and cleaner please... */
83         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
84         unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
85         unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
86         /* if from is not *the* eip: get actual eip from stack storage */
87         unsigned int from_stack = (*esp) - 3;
88         unsigned int type = getTrapType(from, from_stack);
89         if (type == 0) {
90                 dprintf("something is wrong here: abort\n");
91                 exit(1);
92         } else if (type == 1) {
93                 if (from > 0) {
94                         dprintf("from: 0x%08x but should be 0 :-(\n", from);
95                 }
96                 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
97                 unsigned char offset = *((unsigned char *) (*esp) - 1);
98                 /* method entry to patch */
99                 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
100                 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
101                 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
102                 dprintf("*to_patch: 0x%08x\n", *to_patch);
103                 *to_patch = getMethodEntry(from_stack, method_table_ptr);
104                 mctx->gregs[REG_EIP] = *to_patch;
105                 dprintf("*to_patch: 0x%08x\n", *to_patch);
106         } else if (type == 4) {
107                 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
108                 unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
109                 unsigned char offset = *((unsigned char *) (*esp) - 1);
110                 /* interface entry to patch */
111                 unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
112                 dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
113                 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
114                 dprintf("*to_patch: 0x%08x\n", *to_patch);
115                 *to_patch = getMethodEntry(from_stack, method_table_ptr);
116                 mctx->gregs[REG_EIP] = *to_patch;
117                 dprintf("*to_patch: 0x%08x\n", *to_patch);
118         } else if (type == 2) {
119         unsigned int *to_patch = (unsigned int *) (from + 2);
120         dprintf("staticfieldtrap by 0x%08x\n", from);
121         if (*to_patch != 0x00000000) {
122                 dprintf("staticfieldtrap: something is wrong here. abort\n");
123                 exit(0);
124         }
125         unsigned int patchme = getStaticFieldAddr(from, trap_map);
126
127         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
128         dprintf("*to_patch: 0x%08x\n", *to_patch);
129         *to_patch = patchme;
130         dprintf("*to_patch: 0x%08x\n", *to_patch);
131         }
132 }
133
134 void register_signal(void)
135 {
136         struct sigaction illaction;
137         illaction.sa_sigaction = callertrap;
138         sigemptyset(&illaction.sa_mask);
139         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
140         sigaction(SIGILL, &illaction, NULL);
141
142         struct sigaction segvaction;
143         segvaction.sa_sigaction = staticfieldtrap;
144         sigemptyset(&segvaction.sa_mask);
145         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
146         sigaction(SIGSEGV, &segvaction, NULL);
147 }
148
149 unsigned int getaddr(void)
150 {
151         return (unsigned int) mainresult;
152 }
153
154 unsigned int getMallocAddr(void)
155 {
156         return (unsigned int) malloc;
157 }