- struct ucontext *uctx = (struct ucontext *) ctx;
- unsigned int from = (unsigned int) uctx->uc_mcontext.eip;
- unsigned int patchme = getMethodEntry(from, method_map, caller_map);
-
- printf("callertrap(mctx) by 0x%08x\n", from);
- // printf("callertrap(addr) by 0x%08x\n", info->si_addr);
- // printf("callertrap(*esp) by 0x%08x\n", * (unsigned int *) uctx->uc_mcontext.esp);
-
- unsigned int *to_patch = (unsigned int *) (uctx->uc_mcontext.eip + 2);
- unsigned char *insn = (unsigned char *) (uctx->uc_mcontext.eip);
- *insn = 0x90; // nop
- insn++;
- *insn = 0xe8; // call
- printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
- printf("*to_patch: 0x%08x\n", *to_patch);
+ /* TODO(bernhard): more generic and cleaner please... */
+ mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
+ unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
+ if (from < 0x10000) { // invokevirtual
+ if (from > 0) {
+ dprintf("from: 0x%08x but should be 0 :-(\n", from);
+ }
+ unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
+ unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
+ /* get actual eip from stack storage */
+ unsigned int from = (*esp) - 3;
+ unsigned char offset = *((unsigned char *) (*esp) - 1);
+ /* method entry to patch */
+ unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
+ dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
+ dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
+ dprintf("*to_patch: 0x%08x\n", *to_patch);
+ *to_patch = getMethodEntry(from, method_table_ptr);
+ mctx->gregs[REG_EIP] = *to_patch;
+ dprintf("*to_patch: 0x%08x\n", *to_patch);
+ } else {
+ unsigned int *to_patch = (unsigned int *) (from + 2);
+ dprintf("staticfieldtrap by 0x%08x\n", from);