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