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 mainresult(unsigned int a)
37 dprintf("mainresult: 0x%08x\n", a);
40 void staticcalltrap(int nSignal, siginfo_t *info, void *ctx)
42 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
43 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
44 unsigned int *to_patch = (unsigned int *) (from + 1);
45 dprintf("callertrap(mctx) by 0x%08x\n", from);
46 if (*to_patch != 0x90ffff90) {
47 dprintf("callertrap: something is wrong here. abort\n");
50 unsigned int patchme = getMethodEntry(from, 0);
52 unsigned char *insn = (unsigned char *) from;
53 *insn = 0xe8; // call opcode
54 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
55 dprintf("*to_patch: 0x%08x\n", *to_patch);
56 *to_patch = patchme - (from + 5);
57 dprintf("*to_patch: 0x%08x\n", *to_patch);
58 mctx->gregs[REG_EIP] = (unsigned long) insn;
61 void sigsegvtrap(int nSignal, siginfo_t *info, void *ctx)
63 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
64 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
65 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
67 /* if from is not *the* eip: get actual eip from stack storage */
68 unsigned int from_stack = (*esp) - 3;
69 switch(getTrapType(from, from_stack)) {
71 dprintf("something is wrong here: abort\n");
74 case 1: { // invokevirtual
76 dprintf("from: 0x%08x but should be 0 :-(\n", from);
78 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
79 unsigned char offset = *((unsigned char *) (*esp) - 1);
80 /* method entry to patch */
81 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
82 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from_stack, offset);
83 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
84 dprintf("*to_patch: 0x%08x\n", *to_patch);
85 *to_patch = getMethodEntry(from_stack, method_table_ptr);
86 mctx->gregs[REG_EIP] = *to_patch;
87 dprintf("*to_patch: 0x%08x\n", *to_patch);
89 case 4: { // invokeinterface
91 dprintf("from: 0x%08x but should be 0 :-(\n", from);
93 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
94 unsigned int interface_table_ptr = (unsigned int) mctx->gregs[REG_EBX];
95 unsigned char offset = *((unsigned char *) (*esp) - 1);
96 /* interface entry to patch */
97 unsigned int *to_patch = (unsigned int*) (interface_table_ptr + offset);
98 dprintf("invokeinterface by 0x%08x with offset 0x%08x\n", from_stack, offset);
99 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
100 dprintf("*to_patch: 0x%08x\n", *to_patch);
101 *to_patch = getMethodEntry(from_stack, method_table_ptr);
102 mctx->gregs[REG_EIP] = *to_patch;
103 dprintf("*to_patch: 0x%08x\n", *to_patch);
105 case 2: { // static field patch
106 unsigned int *to_patch = (unsigned int *) (from + 2);
107 dprintf("staticfieldtrap by 0x%08x\n", from);
108 if (*to_patch != 0x00000000) {
109 dprintf("staticfieldtrap: something is wrong here. abort\n");
112 unsigned int patchme = getStaticFieldAddr(from);
114 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
115 dprintf("*to_patch: 0x%08x\n", *to_patch);
117 dprintf("*to_patch: 0x%08x\n", *to_patch);
122 void register_signal(void)
124 struct sigaction illaction;
125 illaction.sa_sigaction = staticcalltrap;
126 sigemptyset(&illaction.sa_mask);
127 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
128 sigaction(SIGILL, &illaction, NULL);
130 struct sigaction segvaction;
131 segvaction.sa_sigaction = sigsegvtrap;
132 sigemptyset(&segvaction.sa_mask);
133 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
134 sigaction(SIGSEGV, &segvaction, NULL);
137 unsigned int getaddr(void)
139 return (unsigned int) mainresult;
142 unsigned int getMallocObjectAddr(void)
144 return (unsigned int) mallocObject;