6 /* TODO(bernhard): use {u,}int* types */
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
15 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
19 #define __USE_XOPEN2K8
22 #include <sys/ucontext.h>
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);
30 #define dprintf(args...) do { printf (args); } while (0);
32 #define dprintf(args...)
35 #define NEW_MAP(prefix) \
36 void* prefix ## _map = NULL; \
37 void set_ ## prefix ## map(void *map) \
39 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
40 prefix ## _map = map; \
42 void *get_ ## prefix ## map() \
44 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
45 return prefix ## _map; \
54 NEW_MAP(interfacemethod)
57 void mainresult(unsigned int a)
59 dprintf("mainresult: 0x%08x\n", a);
62 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
64 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
65 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
66 unsigned int *to_patch = (unsigned int *) (from + 1);
67 dprintf("callertrap(mctx) by 0x%08x\n", from);
68 if (*to_patch != 0x90ffff90) {
69 dprintf("callertrap: something is wrong here. abort\n");
72 unsigned int patchme = getMethodEntry(from, 0);
74 unsigned char *insn = (unsigned char *) from;
75 *insn = 0xe8; // call opcode
76 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
77 dprintf("*to_patch: 0x%08x\n", *to_patch);
78 *to_patch = patchme - (from + 5);
79 dprintf("*to_patch: 0x%08x\n", *to_patch);
80 mctx->gregs[REG_EIP] = (unsigned long) insn;
83 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
85 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
86 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
87 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
89 /* if from is not *the* eip: get actual eip from stack storage */
90 unsigned int from_stack = (*esp) - 3;
91 switch(getTrapType(from, from_stack)) {
93 dprintf("something is wrong here: abort\n");
96 case 1: { // invokevirtual
98 dprintf("from: 0x%08x but should be 0 :-(\n", from);
100 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
101 unsigned char offset = *((unsigned char *) (*esp) - 1);
102 /* method entry to patch */
103 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
104 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
105 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
106 dprintf("*to_patch: 0x%08x\n", *to_patch);
107 *to_patch = getMethodEntry(from_stack, method_table_ptr);
108 mctx->gregs[REG_EIP] = *to_patch;
109 dprintf("*to_patch: 0x%08x\n", *to_patch);
111 case 4: { // invokeinterface
113 dprintf("from: 0x%08x but should be 0 :-(\n", from);
115 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
116 unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
117 unsigned char offset = *((unsigned char *) (*esp) - 1);
118 /* interface entry to patch */
119 unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
120 dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
121 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
122 dprintf("*to_patch: 0x%08x\n", *to_patch);
123 *to_patch = getMethodEntry(from_stack, method_table_ptr);
124 mctx->gregs[REG_EIP] = *to_patch;
125 dprintf("*to_patch: 0x%08x\n", *to_patch);
127 case 2: { // static field patch
128 unsigned int *to_patch = (unsigned int *) (from + 2);
129 dprintf("staticfieldtrap by 0x%08x\n", from);
130 if (*to_patch != 0x00000000) {
131 dprintf("staticfieldtrap: something is wrong here. abort\n");
134 unsigned int patchme = getStaticFieldAddr(from);
136 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
137 dprintf("*to_patch: 0x%08x\n", *to_patch);
139 dprintf("*to_patch: 0x%08x\n", *to_patch);
144 void register_signal(void)
146 struct sigaction illaction;
147 illaction.sa_sigaction = staticcalltrap;
148 sigemptyset(&illaction.sa_mask);
149 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
150 sigaction(SIGILL, &illaction, NULL);
152 struct sigaction segvaction;
153 segvaction.sa_sigaction = sigsegvtrap;
154 sigemptyset(&segvaction.sa_mask);
155 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
156 sigaction(SIGSEGV, &segvaction, NULL);
159 unsigned int getaddr(void)
161 return (unsigned int) mainresult;
164 unsigned int getMallocObjectAddr(void)
166 return (unsigned int) mallocObject;