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