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