1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.X86TrapHandling (
9 import qualified Data.Map as M
10 import qualified Data.ByteString.Lazy as B
13 import Foreign.C.Types
15 import Harpy hiding (fst)
18 import Mate.NativeSizes
19 import {-# SOURCE #-} Mate.MethodPool
21 import Mate.X86CodeGen
24 import Harpy.X86Disassembler
26 foreign import ccall "register_signal"
27 register_signal :: IO ()
29 foreign export ccall mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
30 mateHandler :: CPtrdiff -> CPtrdiff -> CPtrdiff -> CPtrdiff -> IO CPtrdiff
31 mateHandler reip reax rebx resi = do
33 let reipw32 = fromIntegral reip
34 (deleteMe, ret_nreip) <- case M.lookup reipw32 tmap of
35 (Just (StaticMethod patcher)) ->
36 patchWithHarpy patcher reip >>= delFalse
37 (Just (StaticField _)) ->
38 staticFieldHandler reip >>= delTrue
39 (Just (ObjectField patcher)) ->
40 patchWithHarpy patcher reip >>= delTrue
41 (Just (InstanceOf patcher)) ->
42 patchWithHarpy (patcher reax) reip >>= delFalse
43 (Just (NewObject patcher)) ->
44 patchWithHarpy patcher reip >>= delTrue
45 (Just (VirtualCall False mi io_offset)) ->
46 patchWithHarpy (patchInvoke mi reax reax io_offset) reip
48 (Just (VirtualCall True mi io_offset)) ->
49 patchWithHarpy (patchInvoke mi rebx reax io_offset) reip
51 Nothing -> case resi of
52 0x13371234 -> return (-1) >>= delFalse
53 _ -> error $ "getTrapType: abort :-( " ++ (showHex reip ". ")
54 ++ (concatMap (`showHex` ", ") (M.keys tmap))
56 then setTrapMap $ M.delete reipw32 tmap
60 delTrue = (\nreip -> return (True, nreip))
61 delFalse = (\nreip -> return (False, nreip))
64 patchWithHarpy :: (CPtrdiff -> CodeGen () () CPtrdiff) -> CPtrdiff -> IO CPtrdiff
65 patchWithHarpy patcher reip = do
66 -- this is just an upperbound. if the value is to low, patching fails. find
69 let entry = Just (intPtrToPtr (fromIntegral reip), fixme)
70 let cgconfig = defaultCodeGenConfig { customCodeBuffer = entry }
71 (_, Right right) <- runCodeGenWithConfig (withDisasm $ patcher reip) () () cgconfig
73 then mapM_ (printfJit . printf "patched: %s\n" . showAtt) $ snd right
77 withDisasm :: CodeGen e s CPtrdiff -> CodeGen e s (CPtrdiff, [Instruction])
78 withDisasm patcher = do
83 staticFieldHandler :: CPtrdiff -> IO CPtrdiff
84 staticFieldHandler reip = do
85 -- patch the offset here, first two bytes are part of the insn (opcode + reg)
86 let imm_ptr = intPtrToPtr (fromIntegral (reip + 2)) :: Ptr CPtrdiff
87 checkMe <- peek imm_ptr
88 if checkMe == 0x00000000 then
90 getStaticFieldAddr reip >>= poke imm_ptr
92 else error "staticFieldHandler: something is wrong here. abort.\n"
94 patchInvoke :: MethodInfo -> CPtrdiff -> CPtrdiff -> IO NativeWord -> CPtrdiff -> CodeGen e s CPtrdiff
95 patchInvoke (MethodInfo methname _ msig) method_table table2patch io_offset reip = do
96 vmap <- liftIO $ getVirtualMap
97 let newmi = MethodInfo methname (vmap M.! fromIntegral method_table) msig
98 offset <- liftIO io_offset
99 entryAddr <- liftIO $ getMethodEntry newmi
100 call32_eax (Disp offset)
101 -- patch entry in table
102 let call_insn = intPtrToPtr . fromIntegral $ table2patch + fromIntegral offset
103 liftIO $ poke call_insn entryAddr