invokevirtual: implemented. not very well tested though
[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, void *, void *);
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
42
43 void mainresult(unsigned int a)
44 {
45         printf("mainresult: 0x%08x\n", a);
46 }
47
48 void callertrap(int nSignal, siginfo_t *info, void *ctx)
49 {
50         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
51         unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
52         unsigned int *to_patch = (unsigned int *) (from + 1);
53         printf("callertrap(mctx)  by 0x%08x\n", from);
54         if (*to_patch != 0x90ffff90) {
55                 printf("callertrap: something is wrong here. abort\n");
56                 exit(0);
57         }
58         unsigned int patchme = getMethodEntry(from, method_map, trap_map);
59
60         unsigned char *insn = (unsigned char *) from;
61         *insn = 0xe8; // call opcode
62         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
63         printf("*to_patch: 0x%08x\n", *to_patch);
64         *to_patch = patchme - (from + 5);
65         printf("*to_patch: 0x%08x\n", *to_patch);
66         mctx->gregs[REG_EIP] = (unsigned long) insn;
67 }
68
69 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
70 {
71         /* TODO(bernhard): more generic and cleaner please... */
72         mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
73         unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
74         if (from == 0) { // invokevirtual
75                 unsigned int eax = (unsigned int) mctx->gregs[REG_EAX];
76                 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
77                 /* get actual eip from stack storage */
78                 unsigned int from = (*esp) - 3;
79                 unsigned char offset = *((unsigned char *) (*esp) - 1);
80                 /* method entry to patch */
81                 unsigned int *to_patch = (unsigned int*) (eax + offset);
82                 printf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
83                 printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
84                 printf("*to_patch: 0x%08x\n", *to_patch);
85                 *to_patch = getMethodEntry(from, method_map, trap_map);
86                 mctx->gregs[REG_EIP] = *to_patch;
87                 printf("*to_patch: 0x%08x\n", *to_patch);
88         } else {
89         unsigned int *to_patch = (unsigned int *) (from + 2);
90         printf("staticfieldtrap by 0x%08x\n", from);
91         if (*to_patch != 0x00000000) {
92                 printf("staticfieldtrap: something is wrong here. abort\n");
93                 exit(0);
94         }
95         unsigned int patchme = getStaticFieldAddr(from, trap_map);
96
97         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
98         printf("*to_patch: 0x%08x\n", *to_patch);
99         *to_patch = patchme;
100         printf("*to_patch: 0x%08x\n", *to_patch);
101         }
102 }
103
104 void register_signal(void)
105 {
106         struct sigaction illaction;
107         illaction.sa_sigaction = callertrap;
108         sigemptyset(&illaction.sa_mask);
109         illaction.sa_flags = SA_SIGINFO | SA_RESTART;
110         sigaction(SIGILL, &illaction, NULL);
111
112         struct sigaction segvaction;
113         segvaction.sa_sigaction = staticfieldtrap;
114         sigemptyset(&segvaction.sa_mask);
115         segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
116         sigaction(SIGSEGV, &segvaction, NULL);
117 }
118
119 unsigned int getaddr(void)
120 {
121         return (unsigned int) mainresult;
122 }
123
124 unsigned int getMallocAddr(void)
125 {
126         return (unsigned int) malloc;
127 }