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 void *mate_ctx = NULL;
37 void *get_mate_context()
42 void *set_mate_context(void *ctx)
47 void mainresult(unsigned int a)
49 dprintf("mainresult: 0x%08x\n", a);
52 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
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");
62 unsigned int patchme = getMethodEntry(from, 0);
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;
73 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
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];
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)) {
83 dprintf("something is wrong here: abort\n");
86 case 1: { // invokevirtual
88 dprintf("from: 0x%08x but should be 0 :-(\n", from);
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);
101 case 4: { // invokeinterface
103 dprintf("from: 0x%08x but should be 0 :-(\n", from);
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);
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");
124 unsigned int patchme = getStaticFieldAddr(from);
126 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
127 dprintf("*to_patch: 0x%08x\n", *to_patch);
129 dprintf("*to_patch: 0x%08x\n", *to_patch);
134 void register_signal(void)
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);
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);
149 unsigned int getaddr(void)
151 return (unsigned int) mainresult;
154 unsigned int getMallocObjectAddr(void)
156 return (unsigned int) mallocObject;