From 13cf9f65321881050edb99776f29eea8580ec457 Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Tue, 31 Jul 2012 22:22:28 +0200 Subject: [PATCH] nativeMaschine: s/unsigned int/ptrdiff_t/g more portable --- Mate/ClassPool.hs | 4 ++-- Mate/GarbageAlloc.hs | 4 ++-- Mate/MethodPool.hs | 2 +- Mate/MethodPool.hs-boot | 2 +- Mate/Strings.hs | 12 ++++++------ Mate/X86CodeGen.hs | 2 +- Mate/X86TrapHandling.hs | 26 +++++++++++++------------- doc/TODO | 3 --- ffi/trap.c | 13 +++++++------ 9 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Mate/ClassPool.hs b/Mate/ClassPool.hs index ad29054..58ba6c7 100644 --- a/Mate/ClassPool.hs +++ b/Mate/ClassPool.hs @@ -62,7 +62,7 @@ getClassFile path = do ci <- getClassInfo path return $ ciFile ci -getStaticFieldOffset :: B.ByteString -> B.ByteString -> IO CUInt +getStaticFieldOffset :: B.ByteString -> B.ByteString -> IO CPtrdiff getStaticFieldOffset path field = do ci <- getClassInfo path return $ fromIntegral $ ciStaticMap ci M.! field @@ -92,7 +92,7 @@ getObjectSize path = do -- one slot for "method-table-ptr" return $ (1 + fsize) * 4 -getStaticFieldAddr :: CUInt -> IO CUInt +getStaticFieldAddr :: CPtrdiff -> IO CPtrdiff getStaticFieldAddr from = do trapmap <- getTrapMap let w32_from = fromIntegral from diff --git a/Mate/GarbageAlloc.hs b/Mate/GarbageAlloc.hs index 1a272fb..56bb8f1 100644 --- a/Mate/GarbageAlloc.hs +++ b/Mate/GarbageAlloc.hs @@ -24,8 +24,8 @@ mallocString size = do printfStr "mallocString: %d\n" size mallocBytes size -foreign export ccall mallocObject :: Int -> IO CUInt -mallocObject :: Int -> IO CUInt +foreign export ccall mallocObject :: Int -> IO CPtrdiff +mallocObject :: Int -> IO CPtrdiff mallocObject size = do ptr <- mallocBytes size printfStr "mallocObject: %d\n" size diff --git a/Mate/MethodPool.hs b/Mate/MethodPool.hs index 134428a..23ff49b 100644 --- a/Mate/MethodPool.hs +++ b/Mate/MethodPool.hs @@ -35,7 +35,7 @@ foreign import ccall "dynamic" code_void :: FunPtr (IO ()) -> IO () -getMethodEntry :: CUInt -> CUInt -> IO CUInt +getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff getMethodEntry signal_from methodtable = do mmap <- getMethodMap tmap <- getTrapMap diff --git a/Mate/MethodPool.hs-boot b/Mate/MethodPool.hs-boot index 477e6cc..7efca9c 100644 --- a/Mate/MethodPool.hs-boot +++ b/Mate/MethodPool.hs-boot @@ -12,4 +12,4 @@ import Foreign.C.Types addMethodRef :: Word32 -> MethodInfo -> [B.ByteString] -> IO () compileBB :: RawMethod -> MethodInfo -> IO Word32 executeFuncPtr :: Word32 -> IO () -getMethodEntry :: CUInt -> CUInt -> IO CUInt +getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff diff --git a/Mate/Strings.hs b/Mate/Strings.hs index 3ac7f99..f41f022 100644 --- a/Mate/Strings.hs +++ b/Mate/Strings.hs @@ -57,7 +57,7 @@ allocateJavaString str = do fsize <- getObjectSize "java/lang/String" printfStr "string: fsize: %d (should be 4 * 5)\n" fsize tblptr <- mallocObject $ fromIntegral fsize - let ptr = intPtrToPtr (fromIntegral tblptr) :: Ptr CUInt + let ptr = intPtrToPtr (fromIntegral tblptr) :: Ptr CPtrdiff mtbl <- getMethodTable "java/lang/String" poke ptr $ fromIntegral mtbl @@ -70,16 +70,16 @@ allocateJavaString str = do copyBytes (plusPtr newstr 4) arr strlen printfStr "new str ptr: (%s)@%d\n" (toString str) strlen - let newstr_length = castPtr newstr :: Ptr CUInt + let newstr_length = castPtr newstr :: Ptr CPtrdiff poke newstr_length $ fromIntegral strlen -- set value pointer - poke (plusPtr ptr 4) (fromIntegral (ptrToIntPtr newstr) :: CUInt) + poke (plusPtr ptr 4) (fromIntegral (ptrToIntPtr newstr) :: CPtrdiff) -- set count field - poke (plusPtr ptr 8) (fromIntegral strlen :: CUInt) + poke (plusPtr ptr 8) (fromIntegral strlen :: CPtrdiff) -- set hash code (TODO) - poke (plusPtr ptr 12) (0 :: CUInt) + poke (plusPtr ptr 12) (0 :: CPtrdiff) -- set offset - poke (plusPtr ptr 16) (0 :: CUInt) + poke (plusPtr ptr 16) (0 :: CPtrdiff) return $ fromIntegral tblptr diff --git a/Mate/X86CodeGen.hs b/Mate/X86CodeGen.hs index df39e5f..b2de573 100644 --- a/Mate/X86CodeGen.hs +++ b/Mate/X86CodeGen.hs @@ -34,7 +34,7 @@ import Text.Printf foreign import ccall "&mallocObject" - mallocObjectAddr :: FunPtr (Int -> IO CUInt) + mallocObjectAddr :: FunPtr (Int -> IO CPtrdiff) type EntryPoint = Ptr Word8 type EntryPointOffset = Int diff --git a/Mate/X86TrapHandling.hs b/Mate/X86TrapHandling.hs index 0e07cd5..6ec0124 100644 --- a/Mate/X86TrapHandling.hs +++ b/Mate/X86TrapHandling.hs @@ -25,7 +25,7 @@ data TrapType = | VirtualMethodCall Bool | InterfaceMethodCall Bool -getTrapType :: TrapMap -> CUInt -> CUInt -> TrapType +getTrapType :: TrapMap -> CPtrdiff -> CPtrdiff -> TrapType getTrapType tmap signal_from from2 = case M.lookup (fromIntegral signal_from) tmap of (Just (StaticMethod _)) -> StaticMethodCall @@ -38,8 +38,8 @@ getTrapType tmap signal_from from2 = (Just _) -> error "getTrapType: abort #1 :-(" Nothing -> error $ "getTrapType: abort #2 :-(" ++ show signal_from ++ ", " ++ show from2 ++ ", " ++ show tmap -foreign export ccall mateHandler :: CUInt -> CUInt -> CUInt -> CUInt -> IO CUInt -mateHandler :: CUInt -> CUInt -> CUInt -> CUInt -> IO CUInt +foreign export ccall mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff +mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff mateHandler eip eax ebx esp = do callerAddr <- callerAddrFromStack esp tmap <- getTrapMap @@ -49,12 +49,12 @@ mateHandler eip eax ebx esp = do VirtualMethodCall imm8 -> invokeHandler eax eax esp imm8 InterfaceMethodCall imm8 -> invokeHandler eax ebx esp imm8 -staticCallHandler :: CUInt -> IO CUInt +staticCallHandler :: CPtrdiff -> IO CPtrdiff staticCallHandler eip = do -- the actual insn to patch is displaced by two bytes let insn_ptr = intPtrToPtr (fromIntegral (eip - 2)) :: Ptr CUChar -- call offset is displaced by one byte - let imm_ptr = intPtrToPtr (fromIntegral (eip - 1)) :: Ptr CUInt + let imm_ptr = intPtrToPtr (fromIntegral (eip - 1)) :: Ptr CPtrdiff -- in codegen we set the immediate to some magic value -- in order to produce a SIGILL signal. we also do a safety -- check here, if we're really the "owner" of this signal. @@ -71,10 +71,10 @@ staticCallHandler eip = do return (eip - 2) else error "staticCallHandler: something is wrong here. abort\n" -staticFieldHandler :: CUInt -> IO CUInt +staticFieldHandler :: CPtrdiff -> IO CPtrdiff staticFieldHandler eip = do -- patch the offset here, first two bytes are part of the insn (opcode + reg) - let imm_ptr = intPtrToPtr (fromIntegral (eip + 2)) :: Ptr CUInt + let imm_ptr = intPtrToPtr (fromIntegral (eip + 2)) :: Ptr CPtrdiff checkMe <- peek imm_ptr if checkMe == 0x00000000 then do @@ -82,7 +82,7 @@ staticFieldHandler eip = do return eip else error "staticFieldHandler: something is wrong here. abort.\n" -invokeHandler :: CUInt -> CUInt -> CUInt -> Bool -> IO CUInt +invokeHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> Bool -> IO CPtrdiff invokeHandler method_table table2patch esp imm8 = do -- table2patch: note, that can be a method-table or a interface-table callerAddr <- callerAddrFromStack esp @@ -93,18 +93,18 @@ invokeHandler method_table table2patch esp imm8 = do return entryAddr -callerAddrFromStack :: CUInt -> IO CUInt +callerAddrFromStack :: CPtrdiff -> IO CPtrdiff callerAddrFromStack = peek . intPtrToPtr . fromIntegral -offsetOfCallInsn8 :: CUInt -> IO CUInt +offsetOfCallInsn8 :: CPtrdiff -> IO CPtrdiff offsetOfCallInsn8 esp = do - let ret_ptr = intPtrToPtr (fromIntegral esp) :: Ptr CUInt + let ret_ptr = intPtrToPtr (fromIntegral esp) :: Ptr CPtrdiff ret <- peek ret_ptr retval <- peek (intPtrToPtr (fromIntegral (ret - 1)) :: Ptr CUChar) return $ fromIntegral retval -offsetOfCallInsn32 :: CUInt -> IO CUInt +offsetOfCallInsn32 :: CPtrdiff -> IO CPtrdiff offsetOfCallInsn32 esp = do - let ret_ptr = intPtrToPtr (fromIntegral esp) :: Ptr CUInt + let ret_ptr = intPtrToPtr (fromIntegral esp) :: Ptr CPtrdiff ret <- peek ret_ptr peek (intPtrToPtr $ fromIntegral (ret - 4)) diff --git a/doc/TODO b/doc/TODO index 52988bb..2e441d7 100644 --- a/doc/TODO +++ b/doc/TODO @@ -57,9 +57,6 @@ (l) check different types (byte, long, ...) -(l) get rid of CUInt where appropriate - -> CPtrdiff and CSize (ptrdiff_t and size_t) is more portable - (l) floating point support (h) better code generation diff --git a/ffi/trap.c b/ffi/trap.c index 1058004..301f206 100644 --- a/ffi/trap.c +++ b/ffi/trap.c @@ -1,5 +1,6 @@ #include #include +#include #include "../debug.h" @@ -21,7 +22,7 @@ #include -unsigned int mateHandler(unsigned int, unsigned int, unsigned int, unsigned int); +ptrdiff_t mateHandler(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t); #ifdef DBG_TRAP #define dprintf(args...) do { printf (args); } while (0); @@ -33,13 +34,13 @@ void chandler(int nSignal, siginfo_t *info, void *ctx) { mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext; - unsigned int eip = (unsigned int) mctx->gregs[REG_EIP]; - unsigned int eax = (unsigned int) mctx->gregs[REG_EAX]; - unsigned int ebx = (unsigned int) mctx->gregs[REG_EBX]; - unsigned int esp = (unsigned int) mctx->gregs[REG_ESP]; + 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, *(unsigned int*) esp); + eax, ebx, esp, *(ptrdiff_t*) esp); mctx->gregs[REG_EIP] = mateHandler(eip, eax, ebx, esp); } -- 2.25.1