trap: use SIGILL instead of SIGSEGV
authorBernhard Urban <lewurm@gmail.com>
Wed, 18 Apr 2012 10:40:14 +0000 (12:40 +0200)
committerBernhard Urban <lewurm@gmail.com>
Wed, 18 Apr 2012 10:57:00 +0000 (12:57 +0200)
therefore we can use SIGSEGV for other things (e.g. exception handling)

Mate/X86CodeGen.hs
ffi/trap.c

index 270368ffb78cbe3c9511ee0dbfba4afb1bb8832f..dea2a4b0eb8e2c6c5575e80da79b5be40b28014f 100644 (file)
@@ -176,8 +176,9 @@ emitFromBB cls hmap =  do
         calladdr <- getCodeOffset
         let w32_calladdr = w32_ep + (fromIntegral calladdr) :: Word32
         newNamedLabel (toString l) >>= defineLabel
-        -- TODO(bernhard): better try SIGILL instead of SIGSEGV?
-        mov (Addr 0) eax
+        -- causes SIGILL. in the signal handler we patch it to the acutal call.
+        -- place a nop at the end, therefore the disasm doesn't screw up
+        emit32 (0xffffffff :: Word32) >> emit8 (0x90 :: Word8)
         -- discard arguments (TODO(bernhard): don't hardcode it)
         add esp (4 :: Word32)
         -- push result on stack (TODO(bernhard): if any)
index 32e5217c1ae4e68f178db462db4b004c95ad83e5..2073dae2ec285afe4c1a89343654d1a189882a00 100644 (file)
@@ -58,21 +58,17 @@ void callertrap(int nSignal, siginfo_t *info, void *ctx)
        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
+       unsigned int *to_patch = (unsigned int *) (from + 1);
+       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);
-       if (*to_patch != 0x00000000) {
+       if (*to_patch != 0x90ffffff) {
                printf("something is wrong here. abort\n");
                exit(0);
        }
-       *to_patch = (unsigned int) patchme - ((unsigned int) insn + 5);
+       *to_patch = patchme - (from + 5);
        printf("*to_patch: 0x%08x\n", *to_patch);
        uctx->uc_mcontext.eip = (unsigned long) insn;
        // while (1) ;
@@ -84,7 +80,7 @@ void register_signal(void)
        segvaction.sa_sigaction = callertrap;
        sigemptyset(&segvaction.sa_mask);
        segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
-       sigaction(SIGSEGV, &segvaction, NULL);
+       sigaction(SIGILL, &segvaction, NULL);
 }
 
 unsigned int getaddr(void)