invokevirtual: implement lazy class loading right
[mate.git] / ffi / trap.c
index 477897c93c96562a38fcf06c3eaa8e9df2dd2933..4e526f23275d8967c9d05ed03899a7c29181aee1 100644 (file)
@@ -1,80 +1,74 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <signal.h>
-#include <asm/ucontext.h>
+#include <stddef.h>
 
-unsigned int getMethodEntry(unsigned int, void *, void *);
+#include "../debug.h"
 
-void *method_map = NULL;
-void *caller_map = NULL;
+/* TODO(bernhard): use {u,}int* types */
 
-void set_mmap(void *mmap)
-{
-       printf("set_mmap: 0x%08x\n", (unsigned int) mmap);
-       method_map = mmap;
-}
-
-void *get_mmap()
-{
-       printf("get_mmap: 0x%08x\n", (unsigned int) method_map);
-       return method_map;
-}
-
-void set_cmap(void *cmap)
-{
-       printf("set_cmap: 0x%08x\n", (unsigned int) cmap);
-       caller_map = cmap;
-}
+#define __USE_GNU
+// Note by hs: my signal.h includes sys/uconctext which conflicts with
+// asm/ucontext - this hack kinda solves the problem for me ;-) 
+// so feel free to blame me for that s**t
+#if defined __USE_XOPEN2K8
+#undef __USE_XOPEN2K8
+#define RESTORE
+#warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
+#endif
+#include <signal.h>
+#ifdef RESTORE
+#define __USE_XOPEN2K8
+#endif
 
-void *get_cmap()
-{
-       printf("get_cmap: 0x%08x\n", (unsigned int) caller_map);
-       return caller_map;
-}
+#include <sys/ucontext.h>
 
+ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t);
 
-void mainresult(unsigned int a)
-{
-       printf("mainresult: 0x%08x\n", a);
-}
+#ifdef DBG_TRAP
+#define dprintf(args...) do { printf (args); } while (0);
+#else
+#define dprintf(args...)
+#endif
 
-void callertrap(int nSignal, siginfo_t *info, void *ctx)
+void chandler(int nSignal, siginfo_t *info, void *ctx)
 {
-       struct ucontext *uctx = (struct ucontext *) ctx;
-       unsigned int from = (unsigned int) uctx->uc_mcontext.eip;
-       unsigned int patchme = getMethodEntry(from, method_map, caller_map);
+       mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
+       greg_t *regs = mctx->gregs;
 
-       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);
+       ptrdiff_t eip = (ptrdiff_t) regs[REG_EIP];
+       ptrdiff_t eax = (ptrdiff_t) regs[REG_EAX];
+       ptrdiff_t ebx = (ptrdiff_t) regs[REG_EBX];
+       ptrdiff_t esp = (ptrdiff_t) regs[REG_ESP];
+       ptrdiff_t esi = (ptrdiff_t) regs[REG_ESI];
+       ptrdiff_t ebp = (ptrdiff_t) regs[REG_EBP];
+       dprintf("trap: type %d, eip 0x%08x, eax 0x%08x, ebx 0x%08x, \n"
+                       "esp 0x%08x, *esp 0x%08x, *(ebp+8) 0x%08x\n", nSignal, eip,
+                       eax, ebx, esp, *(ptrdiff_t*) esp, *(ptrdiff_t *) (ebp + 8));
 
-       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);
-       if (*to_patch != 0x00000000) {
-               printf("something is wrong here. abort\n");
-               exit(0);
+       ptrdiff_t ret = mateHandler(eip, eax, ebx, esi);
+       if (ret == -1) {
+               dprintf("regdump @ EIP: 0x%08x\n", regs[REG_EIP]);
+               dprintf("\tEAX: 0x%08lx EBX: 0x%08lx ECX: 0x%08lx EDX: 0x%08lx\n",
+                       regs[REG_EAX], regs[REG_EBX], regs[REG_ECX], regs[REG_EDX]);
+               dprintf("\tESI: 0x%08lx EDI: 0x%08lx EBP: 0x%08lx ESP: 0x%08lx\n",
+                       regs[REG_ESI], regs[REG_EDI], regs[REG_EBP], regs[REG_ESP]);
+               mctx->gregs[REG_EIP] = eip + 6;
+       } else {
+               mctx->gregs[REG_EIP] = ret;
        }
-       *to_patch = (unsigned int) patchme - ((unsigned int) insn + 5);
-       printf("*to_patch: 0x%08x\n", *to_patch);
-       uctx->uc_mcontext.eip = (unsigned long) insn;
-       // while (1) ;
 }
 
 void register_signal(void)
 {
+       struct sigaction illaction;
+       illaction.sa_sigaction = chandler;
+       sigemptyset(&illaction.sa_mask);
+       illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
+       sigaction(SIGILL, &illaction, NULL);
+
        struct sigaction segvaction;
-       segvaction.sa_sigaction = callertrap;
+       segvaction.sa_sigaction = chandler;
        sigemptyset(&segvaction.sa_mask);
-       segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
+       segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
        sigaction(SIGSEGV, &segvaction, NULL);
 }
-
-unsigned int getaddr(void)
-{
-       return (unsigned int) mainresult;
-}