traps: delete traps... TODO
[mate.git] / Mate / X86TrapHandling.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.X86TrapHandling (
4   mateHandler,
5   register_signal
6   ) where
7
8 import Numeric
9 import qualified Data.Map as M
10 import qualified Data.ByteString.Lazy as B
11
12 import Foreign
13 import Foreign.C.Types
14
15 import Harpy
16
17 import Mate.Types
18 import Mate.NativeSizes
19 import {-# SOURCE #-} Mate.MethodPool
20 import Mate.ClassPool
21 import Mate.X86CodeGen
22
23 import Mate.Debug
24 import Harpy.X86Disassembler
25
26 foreign import ccall "register_signal"
27   register_signal :: IO ()
28
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
32   tmap <- getTrapMap
33   let reipw32 = fromIntegral reip
34   (deleteMe, ret_nreip) <- case M.lookup reipw32 tmap of
35     (Just (StaticMethod _)) ->
36         patchWithHarpy patchStaticCall reip >>= delTrue
37     (Just (StaticField _))  ->
38         staticFieldHandler reip >>= delTrue
39     (Just (InstanceOf cn))  ->
40         patchWithHarpy (`patchInstanceOf` cn) reip >>= delFalse
41     (Just (NewObject cn))   ->
42         patchWithHarpy (`patchNewObject` cn) reip >>= delTrue
43     (Just (VirtualCall False _ io_offset)) ->
44         patchWithHarpy (patchInvoke reax reax io_offset) reip
45         >>= delTrue
46     (Just (VirtualCall True  _ io_offset)) ->
47         patchWithHarpy (patchInvoke rebx reax io_offset) reip
48         >>= delTrue
49     Nothing -> case resi of
50         0x13371234 -> return (-1) >>= delFalse
51         _ -> error $ "getTrapType: abort :-( " ++ (showHex reip ". ")
52              ++ (concatMap (`showHex` ", ") (M.keys tmap))
53   if deleteMe
54     then setTrapMap $ M.delete reipw32 tmap
55     else return ()
56   return ret_nreip
57   where
58     delTrue = (\nreip -> return (False, nreip))
59     delFalse = (\nreip -> return (False, nreip))
60
61
62 patchWithHarpy :: (CPtrdiff -> CodeGen () () CPtrdiff) -> CPtrdiff -> IO CPtrdiff
63 patchWithHarpy patcher reip = do
64   -- this is just an upperbound. if the value is to low, patching fails. find
65   -- something better?
66   let fixme = 1024
67   let entry = Just (intPtrToPtr (fromIntegral reip), fixme)
68   let cgconfig = defaultCodeGenConfig { customCodeBuffer = entry }
69   (_, Right right) <- runCodeGenWithConfig (withDisasm $ patcher reip) () () cgconfig
70   if mateDEBUG
71     then mapM_ (printfJit . printf "patched: %s\n" . showAtt) $ snd right
72     else return ()
73   return reip
74
75 withDisasm :: CodeGen e s CPtrdiff -> CodeGen e s (CPtrdiff, [Instruction])
76 withDisasm patcher = do
77   reip <- patcher
78   d <- disassemble
79   return (reip, d)
80
81 patchStaticCall :: CPtrdiff -> CodeGen e s CPtrdiff
82 patchStaticCall reip = do
83   entryAddr <- liftIO $ getMethodEntry reip 0
84   call (fromIntegral (entryAddr - (reip + 5)) :: NativeWord)
85   return reip
86
87
88 staticFieldHandler :: CPtrdiff -> IO CPtrdiff
89 staticFieldHandler reip = do
90   -- patch the offset here, first two bytes are part of the insn (opcode + reg)
91   let imm_ptr = intPtrToPtr (fromIntegral (reip + 2)) :: Ptr CPtrdiff
92   checkMe <- peek imm_ptr
93   if checkMe == 0x00000000 then
94     do
95       getStaticFieldAddr reip >>= poke imm_ptr
96       return reip
97     else error "staticFieldHandler: something is wrong here. abort.\n"
98
99 patchInstanceOf :: CPtrdiff -> B.ByteString -> CodeGen e s CPtrdiff
100 patchInstanceOf reip classname = do
101   mtable <- liftIO $ getMethodTable classname
102   mov edx mtable
103   return reip
104
105 patchNewObject :: CPtrdiff -> B.ByteString -> CodeGen e s CPtrdiff
106 patchNewObject reip classname = do
107   objsize <- liftIO $ getObjectSize classname
108   push32 objsize
109   callMalloc
110   mtable <- liftIO $ getMethodTable classname
111   mov (Disp 0, eax) mtable
112   return reip
113
114 patchInvoke :: CPtrdiff -> CPtrdiff -> IO NativeWord -> CPtrdiff -> CodeGen e s CPtrdiff
115 patchInvoke method_table table2patch io_offset reip = do
116   offset <- liftIO io_offset
117   entryAddr <- liftIO $ getMethodEntry reip method_table
118   call32_eax (Disp offset)
119   -- patch entry in table
120   let call_insn = intPtrToPtr . fromIntegral $ table2patch + fromIntegral offset
121   liftIO $ poke call_insn entryAddr
122   return reip