refactor: use `unsafePerformIO hack' for global var
[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 void mainresult(unsigned int a)
36 {
37         dprintf("mainresult: 0x%08x\n", a);
38 }
39
40 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
41 {
42         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
43         unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
44         unsigned int *to_patch = (unsigned int *) (from + 1);
45         dprintf("callertrap(mctx)  by 0x%08x\n", from);
46         if (*to_patch != 0x90ffff90) {
47                 dprintf("callertrap: something is wrong here. abort\n");
48                 exit(0);
49         }
50         unsigned int patchme = getMethodEntry(from, 0);
51
52         unsigned char *insn = (unsigned char *) from;
53         *insn = 0xe8; // call opcode
54         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
55         dprintf("*to_patch: 0x%08x\n", *to_patch);
56         *to_patch = patchme - (from + 5);
57         dprintf("*to_patch: 0x%08x\n", *to_patch);
58         mctx->gregs[REG_EIP] = (unsigned long) insn;
59 }
60
61 void sigsegvtrap(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];
65         unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
66
67         /* if from is not *the* eip: get actual eip from stack storage */
68         unsigned int from_stack = (*esp) - 3;
69         switch(getTrapType(from, from_stack)) {
70                 default: case 0: {
71                         dprintf("something is wrong here: abort\n");
72                         exit(1);
73                 } break;
74                 case 1: { // invokevirtual
75                         if (from > 0) {
76                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
77                         }
78                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
79                         unsigned char offset = *((unsigned char *) (*esp) - 1);
80                         /* method entry to patch */
81                         unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
82                         dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
83                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
84                         dprintf("*to_patch: 0x%08x\n", *to_patch);
85                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
86                         mctx->gregs[REG_EIP] = *to_patch;
87                         dprintf("*to_patch: 0x%08x\n", *to_patch);
88                 } break;
89                 case 4: { // invokeinterface
90                         if (from > 0) {
91                                 dprintf("from: 0x%08x but should be 0 :-(\n", from);
92                         }
93                         unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
94                         unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
95                         unsigned char offset = *((unsigned char *) (*esp) - 1);
96                         /* interface entry to patch */
97                         unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
98                         dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
99                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
100                         dprintf("*to_patch: 0x%08x\n", *to_patch);
101                         *to_patch = getMethodEntry(from_stack, method_table_ptr);
102                         mctx->gregs[REG_EIP] = *to_patch;
103                         dprintf("*to_patch: 0x%08x\n", *to_patch);
104                 } break;
105                 case 2: { // static field patch
106                         unsigned int *to_patch = (unsigned int *) (from + 2);
107                         dprintf("staticfieldtrap by 0x%08x\n", from);
108                         if (*to_patch != 0x00000000) {
109                                 dprintf("staticfieldtrap: something is wrong here. abort\n");
110                                 exit(0);
111                         }
112                         unsigned int patchme = getStaticFieldAddr(from);
113
114                         dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
115                         dprintf("*to_patch: 0x%08x\n", *to_patch);
116                         *to_patch = patchme;
117                         dprintf("*to_patch: 0x%08x\n", *to_patch);
118                 } break;
119         }
120 }
121
122 void register_signal(void)
123 {
124         struct sigaction illaction;
125         illaction.sa_sigaction = staticcalltrap;
126         sigemptyset(&illaction.sa_mask);
127         illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
128         sigaction(SIGILL, &illaction, NULL);
129
130         struct sigaction segvaction;
131         segvaction.sa_sigaction = sigsegvtrap;
132         sigemptyset(&segvaction.sa_mask);
133         segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
134         sigaction(SIGSEGV, &segvaction, NULL);
135 }
136
137 unsigned int getaddr(void)
138 {
139         return (unsigned int) mainresult;
140 }
141
142 unsigned int getMallocObjectAddr(void)
143 {
144         return (unsigned int) mallocObject;
145 }