ethereum-analyzer 1.0.3 → 1.2.0
raw patch · 9 files changed
+80/−66 lines, 9 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Ethereum.Analyzer: decompile :: Code -> [(Word256, Operation)]
- Ethereum.Analyzer: decompileHexString :: ByteString -> [(Word256, Operation)]
Files
- ethereum-analyzer.cabal +2/−2
- src/Ethereum/Analyzer.hs +1/−3
- src/Ethereum/Analyzer/CfgAugWithTopNPass.hs +68/−50
- src/Ethereum/Analyzer/CfgAugmentPass.hs +2/−0
- src/Ethereum/Analyzer/Util.hs +3/−0
- test/Ethereum/Analyzer/CfgAugWithTopNPassSpec.hs +2/−6
- test/Ethereum/Analyzer/CfgAugmentPassSpec.hs +1/−1
- test/Ethereum/Analyzer/UtilSpec.hs +1/−1
- test/Ethereum/AnalyzerSpec.hs +0/−3
ethereum-analyzer.cabal view
@@ -1,5 +1,5 @@ name: ethereum-analyzer-version: 1.0.3+version: 1.2.0 synopsis: A Ethereum contract analyzer. homepage: https://github.com/ethereumK/ethereum-analyzer license: Apache-2.0@@ -21,7 +21,7 @@ source-repository this type: git location: https://github.com/zchn/ethereum-analyzer- tag: v1.0.3+ tag: v1.2.0 subdir: ethereum-analyzer library
src/Ethereum/Analyzer.hs view
@@ -1,9 +1,7 @@ {-# LANGUAGE OverloadedStrings, FlexibleContexts #-} module Ethereum.Analyzer- ( decompile- , decompileHexString- , module Ethereum.Analyzer.Decompile+ ( module Ethereum.Analyzer.Decompile , module Ethereum.Analyzer.IR ) where
src/Ethereum/Analyzer/CfgAugWithTopNPass.hs view
@@ -11,14 +11,12 @@ import Blockchain.ExtWord import Blockchain.VM.Opcodes as BVO import Compiler.Hoopl-import Control.Monad import Data.Bits as Db import Data.ByteString as DB import Data.List as DL import Data.List.Extra import Data.Maybe as DM import Data.Set as DS-import Data.Word import Legacy.Haskoin.V0102.Network.Haskoin.Crypto.BigWord type StackElemFact = WithTop (Set Word256)@@ -70,6 +68,9 @@ _sizeBound :: Int _sizeBound = 10 +mkTopList+ :: forall b b1 a.+ [b] -> [Pointed C b1 a] mkTopList = DL.map (const Top) pairCompute :: (Word256 -> Word256 -> Word256) -> StackNFact -> StackNFact@@ -87,7 +88,7 @@ popStack :: Int -> StackNFact -> StackNFact popStack 0 f = f-popStack n (h:t) = popStack (n - 1) (t ++ [Top])+popStack n (_:t) = popStack (n - 1) (t ++ [Top]) pushStack' :: StackElemFact -> StackNFact -> StackNFact pushStack' e flist = e : (dropEnd 1 flist)@@ -102,9 +103,8 @@ b2w256 True = 1 b2w256 False = 0 -w256Not :: Word256 -> Word256-w256Not wd = bytesToWord256 $ DL.map complement $ word256ToBytes wd-+-- w256Not :: Word256 -> Word256+-- w256Not wd = bytesToWord256 $ DL.map complement $ word256ToBytes wd w256And :: Word256 -> Word256 -> Word256 w256And wd1 wd2 = bytesToWord256 $ DL.zipWith (.&.) (word256ToBytes wd1) (word256ToBytes wd2)@@ -119,8 +119,8 @@ peekStack :: Int -> StackNFact -> StackElemFact peekStack _ [] = Top-peekStack 1 (h:t) = h-peekStack n (h:t) = peekStack (n - 1) t+peekStack 1 (h:_) = h+peekStack n (_:t) = peekStack (n - 1) t swapStack :: Int -> StackNFact -> StackNFact swapStack n stk =@@ -269,14 +269,14 @@ opGUnit :: HplOp e x -> Graph HplOp e x opGUnit co@CoOp {} = gUnitCO $ BlockCO co BNil opGUnit oo@OoOp {} = gUnitOO $ BMiddle oo+opGUnit oo@HpCodeCopy {} = gUnitOO $ BMiddle oo opGUnit oc@OcOp {} = gUnitOC $ BlockOC BNil oc -catPElems :: [Pointed e x t] -> [t]-catPElems = mapMaybe maybePElem- where- maybePElem (PElem v) = Just v- maybePElem _ = Nothing-+-- catPElems :: [Pointed e x t] -> [t]+-- catPElems = mapMaybe maybePElem+-- where+-- maybePElem (PElem v) = Just v+-- maybePElem _ = Nothing cfgAugWithTopNRewrite :: FwdRewrite WordLabelMapFuelM HplOp StackNFact cfgAugWithTopNRewrite = mkFRewrite3 coR ooR ocR where@@ -332,39 +332,57 @@ let entry_ = entryOf $ ctorOf contract body = bodyOf $ ctorOf contract case entry_ of- Nothing -> return contract- Just entry -> do- newBody <-- runWithFuel- 10000000000- (fst <$>- analyzeAndRewriteFwdBody- cfgAugWithTopNPass- entry- body- (mapSingleton entry $ fact_bot $ fp_lattice cfgAugWithTopNPass))- let blocks = DL.map snd $ bodyList newBody- ooOps = DL.concatMap ((\(_, b, _) -> blockToList b) . blockSplit) blocks- newHexstrings = mapMaybe (- \op -> case op of- HpCodeCopy offset ->- let newhs = DB.drop (fromInteger (getBigWordInteger offset) * 2) hexstring- in if DB.null newhs then Nothing else Just newhs- _ -> Nothing) ooOps- case newHexstrings of- [] -> return contract { ctorOf = HplCode (Just entry) newBody }- [newhs] -> do- HplCode (Just disEntry) disBody <-- evmOps2HplCode $ decompileHexString newhs- newDisBody <- runWithFuel- 10000000000- (fst <$>- analyzeAndRewriteFwdBody- cfgAugWithTopNPass- disEntry- disBody- (mapSingleton disEntry $ fact_bot $ fp_lattice cfgAugWithTopNPass))- return HplContract { ctorOf = HplCode (Just entry) newBody- , dispatcherOf = HplCode (Just disEntry) newDisBody }- _ -> error $ "doCfgAugWithTopNPass: unexpected newHexstrings length: "- ++ (show $ DL.length newHexstrings)+ Nothing -> return contract+ Just entry -> do+ newBody <-+ runWithFuel+ 10000000000+ (fst <$>+ analyzeAndRewriteFwdBody+ cfgAugWithTopNPass+ entry+ body+ (mapSingleton entry $ fact_bot $ fp_lattice cfgAugWithTopNPass))+ let blocks = DL.map snd $ bodyList newBody+ ooOps = DL.concatMap ((\(_, b, _) -> blockToList b) . blockSplit) blocks+ newHexstrings =+ mapMaybe+ (\op ->+ case op of+ HpCodeCopy offset ->+ let newhs =+ DB.drop+ (fromInteger (getBigWordInteger offset) * 2)+ hexstring+ in if DB.null newhs+ then Nothing+ else Just newhs+ _ -> Nothing)+ ooOps+ case newHexstrings of+ [] ->+ return+ contract+ { ctorOf = HplCode (Just entry) newBody+ }+ [newhs] -> do+ HplCode (Just disEntry) disBody <-+ evmOps2HplCode $ decompileHexString newhs+ newDisBody <-+ runWithFuel+ 10000000000+ (fst <$>+ analyzeAndRewriteFwdBody+ cfgAugWithTopNPass+ disEntry+ disBody+ (mapSingleton disEntry $ fact_bot $ fp_lattice cfgAugWithTopNPass))+ return+ HplContract+ { ctorOf = HplCode (Just entry) newBody+ , dispatcherOf = HplCode (Just disEntry) newDisBody+ }+ _ ->+ error $+ "doCfgAugWithTopNPass: unexpected newHexstrings length: " +++ (show $ DL.length newHexstrings)
src/Ethereum/Analyzer/CfgAugmentPass.hs view
@@ -49,6 +49,7 @@ coT _ f = f ooT :: HplOp O O -> StackTopFact -> StackTopFact ooT (OoOp (_, op)) f = opT op f+ ooT (HpCodeCopy _) f = f ocT :: HplOp O C -> StackTopFact -> FactBase StackTopFact ocT hplop@(OcOp (_, op) _) f = distributeFact hplop (opT op f) opT :: Operation -> StackTopFact -> StackTopFact@@ -78,6 +79,7 @@ opGUnit :: HplOp e x -> Graph HplOp e x opGUnit co@CoOp {} = gUnitCO $ BlockCO co BNil opGUnit oo@OoOp {} = gUnitOO $ BMiddle oo+opGUnit oo@HpCodeCopy {} = gUnitOO $ BMiddle oo opGUnit oc@OcOp {} = gUnitOC $ BlockOC BNil oc cfgAugmentRewrite :: FwdRewrite WordLabelMapFuelM HplOp StackTopFact
src/Ethereum/Analyzer/Util.hs view
@@ -62,6 +62,9 @@ bd in mkGraph nList eList +visParams+ :: forall n el.+ GraphvizParams n (Block HplOp C C) el () (Block HplOp C C) visParams = nonClusteredParams { fmtNode = \(_, nl) -> [toLabel $ show nl, shape BoxShape]
test/Ethereum/Analyzer/CfgAugWithTopNPassSpec.hs view
@@ -13,12 +13,8 @@ spec = do describe "doCfgAugWithTopNPass" $ do it "works for hexcode1" $- do let result =- unWordLabelMapM $- show <$> doCfgAugWithTopNPass hexcode1+ do let result = unWordLabelMapM $ show <$> doCfgAugWithTopNPass hexcode1 length result `shouldBe` 4815 it "works for hexcode2" $- do let result =- unWordLabelMapM $- show <$> doCfgAugWithTopNPass hexcode2+ do let result = unWordLabelMapM $ show <$> doCfgAugWithTopNPass hexcode2 result `shouldContain` "OC: 9: JUMPI -> [L2,L4]"
test/Ethereum/Analyzer/CfgAugmentPassSpec.hs view
@@ -20,7 +20,7 @@ show <$> doCfgAugmentPass contract length result `shouldBe` 4769 it "works for hexcode2" $- do let decompiled@((loc, _):_) = decompileHexString hexcode2+ do let decompiled@((_, _):_) = decompileHexString hexcode2 result = unWordLabelMapM $ do contract <- evmOps2HplContract decompiled
test/Ethereum/Analyzer/UtilSpec.hs view
@@ -198,7 +198,7 @@ " 5 -> 6;\n" ++ " 7 -> 8;\n" ++ " 9 -> 10;\n" ++ " 10 -> 11;\n" ++ "}" it "shows HplBody after CfgAugmentPass" $- do let decompiled@((loc, _):_) = decompileHexString hexcode2+ do let decompiled@((_, _):_) = decompileHexString hexcode2 result = unWordLabelMapM $ do contract <- evmOps2HplContract decompiled
test/Ethereum/AnalyzerSpec.hs view
@@ -5,11 +5,8 @@ ) where import Ethereum.Analyzer-import Blockchain.ExtWord-import Blockchain.VM.Opcodes import Data.List as DL import Data.List.Extra as DLE-import Legacy.Haskoin.V0102.Network.Haskoin.Crypto.BigWord import SpecCommon import Test.Hspec