X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=ffi%2Ftrap.c;h=301f2060ce47f5252bb4d2dfd73112efa069d9a7;hb=13cf9f65321881050edb99776f29eea8580ec457;hp=a2af9708299ce884c30d1fb70d8220842c9c6453;hpb=b6e379114b45fca215e766816e9db94199bb4f00;p=mate.git diff --git a/ffi/trap.c b/ffi/trap.c index a2af970..301f206 100644 --- a/ffi/trap.c +++ b/ffi/trap.c @@ -1,6 +1,12 @@ #include #include +#include +#include "../debug.h" + +/* TODO(bernhard): use {u,}int* types */ + +#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 @@ -14,95 +20,42 @@ #define __USE_XOPEN2K8 #endif -#include - -unsigned int getMethodEntry(unsigned int, void *, void *); -unsigned int getStaticFieldAddr(unsigned int, void*); +#include -#define NEW_MAP(prefix) \ - void* prefix ## _map = NULL; \ - void set_ ## prefix ## map(void *map) \ - { \ - printf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \ - prefix ## _map = map; \ - } \ - void *get_ ## prefix ## map() \ - { \ - printf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \ - return prefix ## _map; \ - } - -NEW_MAP(method) -NEW_MAP(trap) -NEW_MAP(class) +ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t); +#ifdef DBG_TRAP +#define dprintf(args...) do { printf (args); } while (0); +#else +#define dprintf(args...) +#endif -void mainresult(unsigned int a) +void chandler(int nSignal, siginfo_t *info, void *ctx) { - printf("mainresult: 0x%08x\n", a); -} + mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext; -void callertrap(int nSignal, siginfo_t *info, void *ctx) -{ - struct ucontext *uctx = (struct ucontext *) ctx; - unsigned int from = (unsigned int) uctx->uc_mcontext.eip - 2; - unsigned int *to_patch = (unsigned int *) (from + 1); - printf("callertrap(mctx) by 0x%08x\n", from); - if (*to_patch != 0x90ffff90) { - printf("callertrap: something is wrong here. abort\n"); - exit(0); - } - unsigned int patchme = getMethodEntry(from, method_map, trap_map); + ptrdiff_t eip = (ptrdiff_t) mctx->gregs[REG_EIP]; + ptrdiff_t eax = (ptrdiff_t) mctx->gregs[REG_EAX]; + ptrdiff_t ebx = (ptrdiff_t) mctx->gregs[REG_EBX]; + ptrdiff_t esp = (ptrdiff_t) mctx->gregs[REG_ESP]; + dprintf("trap: type %d, eip 0x%08x, eax 0x%08x, ebx 0x%08x, " + "esp 0x%08x, *esp 0x%08x\n", nSignal, eip, + eax, ebx, esp, *(ptrdiff_t*) esp); - unsigned char *insn = (unsigned char *) from; - *insn = 0xe8; // call opcode - printf(" to_patch: 0x%08x\n", (unsigned int) to_patch); - printf("*to_patch: 0x%08x\n", *to_patch); - *to_patch = patchme - (from + 5); - printf("*to_patch: 0x%08x\n", *to_patch); - uctx->uc_mcontext.eip = (unsigned long) insn; - // while (1) ; -} - -void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx) -{ - struct ucontext *uctx = (struct ucontext *) ctx; - unsigned int from = (unsigned int) uctx->uc_mcontext.eip; - unsigned int *to_patch = (unsigned int *) (from + 2); - printf("staticfieldtrap by 0x%08x\n", from); - if (*to_patch != 0x00000000) { - printf("staticfieldtrap: something is wrong here. abort\n"); - exit(0); - } - unsigned int patchme = getStaticFieldAddr(from, trap_map); - - printf(" to_patch: 0x%08x\n", (unsigned int) to_patch); - printf("*to_patch: 0x%08x\n", *to_patch); - *to_patch = patchme; - printf("*to_patch: 0x%08x\n", *to_patch); + mctx->gregs[REG_EIP] = mateHandler(eip, eax, ebx, esp); } void register_signal(void) { struct sigaction illaction; - illaction.sa_sigaction = callertrap; + illaction.sa_sigaction = chandler; sigemptyset(&illaction.sa_mask); - illaction.sa_flags = SA_SIGINFO | SA_RESTART; + illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER; sigaction(SIGILL, &illaction, NULL); struct sigaction segvaction; - segvaction.sa_sigaction = staticfieldtrap; + 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; -} - -unsigned int getMallocAddr(void) -{ - return (unsigned int) malloc; -}