{-# Language TupleSections #-}
{-# Language DataKinds #-}
module EVM.SymExec where
import Prelude hiding (Word)
import Data.Tuple (swap)
import Control.Lens hiding (pre)
import EVM hiding (Query, Revert, push, bytecode, cache)
import qualified EVM
import EVM.Exec
import qualified EVM.Fetch as Fetch
import EVM.ABI
import EVM.SMT (SMTCex(..), SMT2(..), assertProps, formatSMT2)
import qualified EVM.SMT as SMT
import EVM.Solvers
import EVM.Traversals
import qualified EVM.Expr as Expr
import EVM.Stepper (Stepper)
import qualified EVM.Stepper as Stepper
import qualified Control.Monad.Operational as Operational
import Control.Monad.State.Strict hiding (state)
import EVM.Types
import EVM.Concrete (createAddress)
import qualified EVM.FeeSchedule as FeeSchedule
import Data.DoubleWord (Word256)
import Control.Concurrent.Async
import Data.Maybe
import Data.List (foldl', sortBy)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Control.Monad.State.Class as State
import Data.Bifunctor (first, second)
import Data.Text (Text)
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import EVM.Format (formatExpr)
import Data.Set (Set, isSubsetOf, size)
import qualified Data.Set as Set
import Control.Concurrent.STM (atomically, TVar, readTVarIO, readTVar, newTVarIO, writeTVar)
import Control.Concurrent.Spawn
import GHC.Conc (getNumProcessors)
import EVM.Format (indent, formatBinary)
data Sig = Sig Text [AbiType]
data ProofResult a b c = Qed a | Cex b | Timeout c
deriving (Int -> ProofResult a b c -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b c.
(Show a, Show b, Show c) =>
Int -> ProofResult a b c -> ShowS
forall a b c.
(Show a, Show b, Show c) =>
[ProofResult a b c] -> ShowS
forall a b c.
(Show a, Show b, Show c) =>
ProofResult a b c -> String
showList :: [ProofResult a b c] -> ShowS
$cshowList :: forall a b c.
(Show a, Show b, Show c) =>
[ProofResult a b c] -> ShowS
show :: ProofResult a b c -> String
$cshow :: forall a b c.
(Show a, Show b, Show c) =>
ProofResult a b c -> String
showsPrec :: Int -> ProofResult a b c -> ShowS
$cshowsPrec :: forall a b c.
(Show a, Show b, Show c) =>
Int -> ProofResult a b c -> ShowS
Show, ProofResult a b c -> ProofResult a b c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b c.
(Eq a, Eq b, Eq c) =>
ProofResult a b c -> ProofResult a b c -> Bool
/= :: ProofResult a b c -> ProofResult a b c -> Bool
$c/= :: forall a b c.
(Eq a, Eq b, Eq c) =>
ProofResult a b c -> ProofResult a b c -> Bool
== :: ProofResult a b c -> ProofResult a b c -> Bool
$c== :: forall a b c.
(Eq a, Eq b, Eq c) =>
ProofResult a b c -> ProofResult a b c -> Bool
Eq)
type VerifyResult = ProofResult () (Expr End, SMTCex) (Expr End)
type EquivResult = ProofResult () (SMTCex) ()
isTimeout :: ProofResult a b c -> Bool
isTimeout :: forall a b c. ProofResult a b c -> Bool
isTimeout (Timeout c
_) = Bool
True
isTimeout ProofResult a b c
_ = Bool
False
isCex :: ProofResult a b c -> Bool
isCex :: forall a b c. ProofResult a b c -> Bool
isCex (Cex b
_) = Bool
True
isCex ProofResult a b c
_ = Bool
False
isQed :: ProofResult a b c -> Bool
isQed :: forall a b c. ProofResult a b c -> Bool
isQed (Qed a
_) = Bool
True
isQed ProofResult a b c
_ = Bool
False
data VeriOpts = VeriOpts
{ VeriOpts -> Bool
simp :: Bool
, VeriOpts -> Bool
debug :: Bool
, VeriOpts -> Maybe Integer
maxIter :: Maybe Integer
, VeriOpts -> Maybe Integer
askSmtIters :: Maybe Integer
, VeriOpts -> RpcInfo
rpcInfo :: Fetch.RpcInfo
}
deriving (VeriOpts -> VeriOpts -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VeriOpts -> VeriOpts -> Bool
$c/= :: VeriOpts -> VeriOpts -> Bool
== :: VeriOpts -> VeriOpts -> Bool
$c== :: VeriOpts -> VeriOpts -> Bool
Eq, Int -> VeriOpts -> ShowS
[VeriOpts] -> ShowS
VeriOpts -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VeriOpts] -> ShowS
$cshowList :: [VeriOpts] -> ShowS
show :: VeriOpts -> String
$cshow :: VeriOpts -> String
showsPrec :: Int -> VeriOpts -> ShowS
$cshowsPrec :: Int -> VeriOpts -> ShowS
Show)
defaultVeriOpts :: VeriOpts
defaultVeriOpts :: VeriOpts
defaultVeriOpts = VeriOpts
{ $sel:simp:VeriOpts :: Bool
simp = Bool
True
, $sel:debug:VeriOpts :: Bool
debug = Bool
False
, $sel:maxIter:VeriOpts :: Maybe Integer
maxIter = forall a. Maybe a
Nothing
, $sel:askSmtIters:VeriOpts :: Maybe Integer
askSmtIters = forall a. Maybe a
Nothing
, $sel:rpcInfo:VeriOpts :: RpcInfo
rpcInfo = forall a. Maybe a
Nothing
}
rpcVeriOpts :: (Fetch.BlockNumber, Text) -> VeriOpts
rpcVeriOpts :: (BlockNumber, Text) -> VeriOpts
rpcVeriOpts (BlockNumber, Text)
info = VeriOpts
defaultVeriOpts { $sel:rpcInfo:VeriOpts :: RpcInfo
rpcInfo = forall a. a -> Maybe a
Just (BlockNumber, Text)
info }
debugVeriOpts :: VeriOpts
debugVeriOpts :: VeriOpts
debugVeriOpts = VeriOpts
defaultVeriOpts { $sel:debug:VeriOpts :: Bool
debug = Bool
True }
extractCex :: VerifyResult -> Maybe (Expr End, SMTCex)
(Cex (Expr 'End, SMTCex)
c) = forall a. a -> Maybe a
Just (Expr 'End, SMTCex)
c
extractCex VerifyResult
_ = forall a. Maybe a
Nothing
inRange :: Int -> Expr EWord -> Prop
inRange :: Int -> Expr 'EWord -> Prop
inRange Int
sz Expr 'EWord
e = Prop -> Prop -> Prop
PAnd (Expr 'EWord -> Expr 'EWord -> Prop
PGEq Expr 'EWord
e (W256 -> Expr 'EWord
Lit W256
0)) (Expr 'EWord -> Expr 'EWord -> Prop
PLEq Expr 'EWord
e (W256 -> Expr 'EWord
Lit forall a b. (a -> b) -> a -> b
$ W256
2 forall a b. (Num a, Integral b) => a -> b -> a
^ Int
sz forall a. Num a => a -> a -> a
- W256
1))
bool :: Expr EWord -> Prop
bool :: Expr 'EWord -> Prop
bool Expr 'EWord
e = Prop -> Prop -> Prop
POr (forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq Expr 'EWord
e (W256 -> Expr 'EWord
Lit W256
1)) (forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq Expr 'EWord
e (W256 -> Expr 'EWord
Lit W256
0))
symAbiArg :: Text -> AbiType -> CalldataFragment
symAbiArg :: Text -> AbiType -> CalldataFragment
symAbiArg Text
name = \case
AbiUIntType Int
n ->
if Int
n forall a. Integral a => a -> a -> a
`mod` Int
8 forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
256
then let v :: Expr 'EWord
v = Text -> Expr 'EWord
Var Text
name in [Prop] -> Expr 'EWord -> CalldataFragment
St [Int -> Expr 'EWord -> Prop
inRange Int
n Expr 'EWord
v] Expr 'EWord
v
else forall a. HasCallStack => String -> a
error String
"bad type"
AbiIntType Int
n ->
if Int
n forall a. Integral a => a -> a -> a
`mod` Int
8 forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
256
then let v :: Expr 'EWord
v = Text -> Expr 'EWord
Var Text
name in [Prop] -> Expr 'EWord -> CalldataFragment
St [Int -> Expr 'EWord -> Prop
inRange Int
n Expr 'EWord
v] Expr 'EWord
v
else forall a. HasCallStack => String -> a
error String
"bad type"
AbiType
AbiBoolType -> let v :: Expr 'EWord
v = Text -> Expr 'EWord
Var Text
name in [Prop] -> Expr 'EWord -> CalldataFragment
St [Expr 'EWord -> Prop
bool Expr 'EWord
v] Expr 'EWord
v
AbiType
AbiAddressType -> let v :: Expr 'EWord
v = Text -> Expr 'EWord
Var Text
name in [Prop] -> Expr 'EWord -> CalldataFragment
St [Int -> Expr 'EWord -> Prop
inRange Int
160 Expr 'EWord
v] Expr 'EWord
v
AbiBytesType Int
n
-> if Int
n forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
n forall a. Ord a => a -> a -> Bool
<= Int
32
then let v :: Expr 'EWord
v = Text -> Expr 'EWord
Var Text
name in [Prop] -> Expr 'EWord -> CalldataFragment
St [Int -> Expr 'EWord -> Prop
inRange (Int
n forall a. Num a => a -> a -> a
* Int
8) Expr 'EWord
v] Expr 'EWord
v
else forall a. HasCallStack => String -> a
error String
"bad type"
AbiArrayType Int
sz AbiType
tp -> [CalldataFragment] -> CalldataFragment
Comp forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Text
n -> Text -> AbiType -> CalldataFragment
symAbiArg (Text
name forall a. Semigroup a => a -> a -> a
<> Text
n) AbiType
tp) [String -> Text
T.pack (forall a. Show a => a -> String
show Int
n) | Int
n <- [Int
0..Int
szforall a. Num a => a -> a -> a
-Int
1]]
AbiType
t -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"TODO: symbolic abi encoding for " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show AbiType
t
data CalldataFragment
= St [Prop] (Expr EWord)
| Dy [Prop] (Expr EWord) (Expr Buf)
| Comp [CalldataFragment]
deriving (Int -> CalldataFragment -> ShowS
[CalldataFragment] -> ShowS
CalldataFragment -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CalldataFragment] -> ShowS
$cshowList :: [CalldataFragment] -> ShowS
show :: CalldataFragment -> String
$cshow :: CalldataFragment -> String
showsPrec :: Int -> CalldataFragment -> ShowS
$cshowsPrec :: Int -> CalldataFragment -> ShowS
Show, CalldataFragment -> CalldataFragment -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CalldataFragment -> CalldataFragment -> Bool
$c/= :: CalldataFragment -> CalldataFragment -> Bool
== :: CalldataFragment -> CalldataFragment -> Bool
$c== :: CalldataFragment -> CalldataFragment -> Bool
Eq)
symCalldata :: Text -> [AbiType] -> [String] -> Expr Buf -> (Expr Buf, [Prop])
symCalldata :: Text -> [AbiType] -> [String] -> Expr 'Buf -> (Expr 'Buf, [Prop])
symCalldata Text
sig [AbiType]
typesignature [String]
concreteArgs Expr 'Buf
base =
let args :: [String]
args = [String]
concreteArgs forall a. Semigroup a => a -> a -> a
<> forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length [AbiType]
typesignature forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
concreteArgs) String
"<symbolic>"
mkArg :: AbiType -> String -> Int -> CalldataFragment
mkArg :: AbiType -> String -> Int -> CalldataFragment
mkArg AbiType
typ String
"<symbolic>" Int
n = Text -> AbiType -> CalldataFragment
symAbiArg (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ String
"arg" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
n) AbiType
typ
mkArg AbiType
typ String
arg Int
_ = let v :: AbiValue
v = AbiType -> String -> AbiValue
makeAbiValue AbiType
typ String
arg
in case AbiValue
v of
AbiUInt Int
_ Word256
w -> [Prop] -> Expr 'EWord -> CalldataFragment
St [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. W256 -> Expr 'EWord
Lit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
num forall a b. (a -> b) -> a -> b
$ Word256
w
AbiInt Int
_ Int256
w -> [Prop] -> Expr 'EWord -> CalldataFragment
St [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. W256 -> Expr 'EWord
Lit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
num forall a b. (a -> b) -> a -> b
$ Int256
w
AbiAddress Addr
w -> [Prop] -> Expr 'EWord -> CalldataFragment
St [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. W256 -> Expr 'EWord
Lit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
num forall a b. (a -> b) -> a -> b
$ Addr
w
AbiBool Bool
w -> [Prop] -> Expr 'EWord -> CalldataFragment
St [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. W256 -> Expr 'EWord
Lit forall a b. (a -> b) -> a -> b
$ if Bool
w then W256
1 else W256
0
AbiValue
_ -> forall a. HasCallStack => String -> a
error String
"TODO"
calldatas :: [CalldataFragment]
calldatas = forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 AbiType -> String -> Int -> CalldataFragment
mkArg [AbiType]
typesignature [String]
args [Int
1..]
(Expr 'Buf
cdBuf, [Prop]
props) = [CalldataFragment] -> Expr 'Buf -> (Expr 'Buf, [Prop])
combineFragments [CalldataFragment]
calldatas Expr 'Buf
base
withSelector :: Expr 'Buf
withSelector = Expr 'Buf -> Text -> Expr 'Buf
writeSelector Expr 'Buf
cdBuf Text
sig
sizeConstraints :: Prop
sizeConstraints
= (Expr 'Buf -> Expr 'EWord
Expr.bufLength Expr 'Buf
withSelector Expr 'EWord -> Expr 'EWord -> Prop
.>= [CalldataFragment] -> Expr 'EWord
cdLen [CalldataFragment]
calldatas)
Prop -> Prop -> Prop
.&& (Expr 'Buf -> Expr 'EWord
Expr.bufLength Expr 'Buf
withSelector Expr 'EWord -> Expr 'EWord -> Prop
.< (W256 -> Expr 'EWord
Lit (W256
2 forall a b. (Num a, Integral b) => a -> b -> a
^ (Integer
64 :: Integer))))
in (Expr 'Buf
withSelector, Prop
sizeConstraints forall a. a -> [a] -> [a]
: [Prop]
props)
cdLen :: [CalldataFragment] -> Expr EWord
cdLen :: [CalldataFragment] -> Expr 'EWord
cdLen = Expr 'EWord -> [CalldataFragment] -> Expr 'EWord
go (W256 -> Expr 'EWord
Lit W256
4)
where
go :: Expr 'EWord -> [CalldataFragment] -> Expr 'EWord
go Expr 'EWord
acc = \case
[] -> Expr 'EWord
acc
(CalldataFragment
hd:[CalldataFragment]
tl) -> case CalldataFragment
hd of
St [Prop]
_ Expr 'EWord
_ -> Expr 'EWord -> [CalldataFragment] -> Expr 'EWord
go (Expr 'EWord -> Expr 'EWord -> Expr 'EWord
Expr.add Expr 'EWord
acc (W256 -> Expr 'EWord
Lit W256
32)) [CalldataFragment]
tl
CalldataFragment
_ -> forall a. HasCallStack => String -> a
error String
"unsupported"
writeSelector :: Expr Buf -> Text -> Expr Buf
writeSelector :: Expr 'Buf -> Text -> Expr 'Buf
writeSelector Expr 'Buf
buf Text
sig = Expr 'EWord -> Expr 'Buf -> Expr 'Buf
writeSel (W256 -> Expr 'EWord
Lit W256
0) forall a b. (a -> b) -> a -> b
$ Expr 'EWord -> Expr 'Buf -> Expr 'Buf
writeSel (W256 -> Expr 'EWord
Lit W256
1) forall a b. (a -> b) -> a -> b
$ Expr 'EWord -> Expr 'Buf -> Expr 'Buf
writeSel (W256 -> Expr 'EWord
Lit W256
2) forall a b. (a -> b) -> a -> b
$ Expr 'EWord -> Expr 'Buf -> Expr 'Buf
writeSel (W256 -> Expr 'EWord
Lit W256
3) Expr 'Buf
buf
where
sel :: Expr 'Buf
sel = ByteString -> Expr 'Buf
ConcreteBuf forall a b. (a -> b) -> a -> b
$ Text -> ByteString
selector Text
sig
writeSel :: Expr 'EWord -> Expr 'Buf -> Expr 'Buf
writeSel Expr 'EWord
idx = Expr 'EWord -> Expr 'Byte -> Expr 'Buf -> Expr 'Buf
Expr.writeByte Expr 'EWord
idx (Expr 'EWord -> Expr 'Buf -> Expr 'Byte
Expr.readByte Expr 'EWord
idx Expr 'Buf
sel)
combineFragments :: [CalldataFragment] -> Expr Buf -> (Expr Buf, [Prop])
combineFragments :: [CalldataFragment] -> Expr 'Buf -> (Expr 'Buf, [Prop])
combineFragments [CalldataFragment]
fragments Expr 'Buf
base = Expr 'EWord
-> [CalldataFragment] -> (Expr 'Buf, [Prop]) -> (Expr 'Buf, [Prop])
go (W256 -> Expr 'EWord
Lit W256
4) [CalldataFragment]
fragments (Expr 'Buf
base, [])
where
go :: Expr EWord -> [CalldataFragment] -> (Expr Buf, [Prop]) -> (Expr Buf, [Prop])
go :: Expr 'EWord
-> [CalldataFragment] -> (Expr 'Buf, [Prop]) -> (Expr 'Buf, [Prop])
go Expr 'EWord
_ [] (Expr 'Buf, [Prop])
acc = (Expr 'Buf, [Prop])
acc
go Expr 'EWord
idx (CalldataFragment
f:[CalldataFragment]
rest) (Expr 'Buf
buf, [Prop]
ps) = case CalldataFragment
f of
St [Prop]
p Expr 'EWord
w -> Expr 'EWord
-> [CalldataFragment] -> (Expr 'Buf, [Prop]) -> (Expr 'Buf, [Prop])
go (Expr 'EWord -> Expr 'EWord -> Expr 'EWord
Expr.add Expr 'EWord
idx (W256 -> Expr 'EWord
Lit W256
32)) [CalldataFragment]
rest (Expr 'EWord -> Expr 'EWord -> Expr 'Buf -> Expr 'Buf
Expr.writeWord Expr 'EWord
idx Expr 'EWord
w Expr 'Buf
buf, [Prop]
p forall a. Semigroup a => a -> a -> a
<> [Prop]
ps)
CalldataFragment
s -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"unsupported cd fragment: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show CalldataFragment
s
abstractVM :: (Expr Buf, [Prop]) -> ByteString -> Maybe Precondition -> StorageModel -> VM
abstractVM :: (Expr 'Buf, [Prop])
-> ByteString -> Maybe Precondition -> StorageModel -> VM
abstractVM (Expr 'Buf
cd, [Prop]
calldataProps) ByteString
contractCode Maybe Precondition
maybepre StorageModel
storagemodel = VM
finalVm
where
store :: Expr 'Storage
store = case StorageModel
storagemodel of
StorageModel
SymbolicS -> Expr 'Storage
AbstractStore
StorageModel
InitialS -> Expr 'Storage
EmptyStore
StorageModel
ConcreteS -> Map W256 (Map W256 W256) -> Expr 'Storage
ConcreteStore forall a. Monoid a => a
mempty
caller' :: Expr 'EWord
caller' = Int -> Expr 'EWord
Caller Int
0
value' :: Expr 'EWord
value' = Int -> Expr 'EWord
CallValue Int
0
code' :: ContractCode
code' = RuntimeCode -> ContractCode
RuntimeCode (ByteString -> RuntimeCode
ConcreteRuntimeCode ByteString
contractCode)
vm' :: VM
vm' = ContractCode
-> Expr 'Storage
-> Expr 'EWord
-> Expr 'EWord
-> Expr 'Buf
-> [Prop]
-> VM
loadSymVM ContractCode
code' Expr 'Storage
store Expr 'EWord
caller' Expr 'EWord
value' Expr 'Buf
cd [Prop]
calldataProps
precond :: [Prop]
precond = case Maybe Precondition
maybepre of
Maybe Precondition
Nothing -> []
Just Precondition
p -> [Precondition
p VM
vm']
finalVm :: VM
finalVm = VM
vm' forall a b. a -> (a -> b) -> b
& forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over Lens' VM [Prop]
constraints (forall a. Semigroup a => a -> a -> a
<> [Prop]
precond)
loadSymVM :: ContractCode -> Expr Storage -> Expr EWord -> Expr EWord -> Expr Buf -> [Prop] -> VM
loadSymVM :: ContractCode
-> Expr 'Storage
-> Expr 'EWord
-> Expr 'EWord
-> Expr 'Buf
-> [Prop]
-> VM
loadSymVM ContractCode
x Expr 'Storage
initStore Expr 'EWord
addr Expr 'EWord
callvalue' Expr 'Buf
calldata' [Prop]
calldataProps =
(VMOpts -> VM
makeVm forall a b. (a -> b) -> a -> b
$ VMOpts
{ $sel:vmoptContract:VMOpts :: Contract
vmoptContract = ContractCode -> Contract
initialContract ContractCode
x
, $sel:vmoptCalldata:VMOpts :: (Expr 'Buf, [Prop])
vmoptCalldata = (Expr 'Buf
calldata', [Prop]
calldataProps)
, $sel:vmoptValue:VMOpts :: Expr 'EWord
vmoptValue = Expr 'EWord
callvalue'
, $sel:vmoptStorageBase:VMOpts :: StorageBase
vmoptStorageBase = StorageBase
Symbolic
, $sel:vmoptAddress:VMOpts :: Addr
vmoptAddress = Addr -> W256 -> Addr
createAddress Addr
ethrunAddress W256
1
, $sel:vmoptCaller:VMOpts :: Expr 'EWord
vmoptCaller = Expr 'EWord
addr
, $sel:vmoptOrigin:VMOpts :: Addr
vmoptOrigin = Addr
ethrunAddress
, $sel:vmoptCoinbase:VMOpts :: Addr
vmoptCoinbase = Addr
0
, $sel:vmoptNumber:VMOpts :: W256
vmoptNumber = W256
0
, $sel:vmoptTimestamp:VMOpts :: Expr 'EWord
vmoptTimestamp = (W256 -> Expr 'EWord
Lit W256
0)
, $sel:vmoptBlockGaslimit:VMOpts :: Word64
vmoptBlockGaslimit = Word64
0
, $sel:vmoptGasprice:VMOpts :: W256
vmoptGasprice = W256
0
, $sel:vmoptPrevRandao:VMOpts :: W256
vmoptPrevRandao = W256
42069
, $sel:vmoptGas:VMOpts :: Word64
vmoptGas = Word64
0xffffffffffffffff
, $sel:vmoptGaslimit:VMOpts :: Word64
vmoptGaslimit = Word64
0xffffffffffffffff
, $sel:vmoptBaseFee:VMOpts :: W256
vmoptBaseFee = W256
0
, $sel:vmoptPriorityFee:VMOpts :: W256
vmoptPriorityFee = W256
0
, $sel:vmoptMaxCodeSize:VMOpts :: W256
vmoptMaxCodeSize = W256
0xffffffff
, $sel:vmoptSchedule:VMOpts :: FeeSchedule Word64
vmoptSchedule = forall n. Num n => FeeSchedule n
FeeSchedule.berlin
, $sel:vmoptChainId:VMOpts :: W256
vmoptChainId = W256
1
, $sel:vmoptCreate:VMOpts :: Bool
vmoptCreate = Bool
False
, $sel:vmoptTxAccessList:VMOpts :: Map Addr [W256]
vmoptTxAccessList = forall a. Monoid a => a
mempty
, $sel:vmoptAllowFFI:VMOpts :: Bool
vmoptAllowFFI = Bool
False
}) forall a b. a -> (a -> b) -> b
& forall s t a b. ASetter s t a b -> b -> s -> t
set (Lens' VM Env
env forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' Env (Map Addr Contract)
contracts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at (Addr -> W256 -> Addr
createAddress Addr
ethrunAddress W256
1))
(forall a. a -> Maybe a
Just (ContractCode -> Contract
initialContract ContractCode
x))
forall a b. a -> (a -> b) -> b
& forall s t a b. ASetter s t a b -> b -> s -> t
set (Lens' VM Env
env forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lens' Env (Expr 'Storage)
EVM.storage) Expr 'Storage
initStore
interpret
:: Fetch.Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr End)
-> StateT VM IO (Expr End)
interpret :: Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters =
ProgramView Action (Expr 'End) -> StateT VM IO (Expr 'End)
eval forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (instr :: * -> *) a. Program instr a -> ProgramView instr a
Operational.view
where
eval
:: Operational.ProgramView Stepper.Action (Expr End)
-> StateT VM IO (Expr End)
eval :: ProgramView Action (Expr 'End) -> StateT VM IO (Expr 'End)
eval (Operational.Return Expr 'End
x) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr 'End
x
eval (Action b
action Operational.:>>= b -> Stepper (Expr 'End)
k) =
case Action b
action of
Action b
Stepper.Exec ->
(forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
State.state forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. State s a -> s -> (a, s)
runState) State VM VMResult
exec forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stepper (Expr 'End)
k
Action b
Stepper.Run ->
(forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
State.state forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. State s a -> s -> (a, s)
runState) State VM VM
run forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stepper (Expr 'End)
k
Stepper.IOAct StateT VM IO b
q ->
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
mapStateT forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO StateT VM IO b
q forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stepper (Expr 'End)
k
Stepper.Ask (EVM.PleaseChoosePath Expr 'EWord
cond Bool -> EVM ()
continue) -> do
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
assign Lens' VM (Maybe VMResult)
result forall a. Maybe a
Nothing
VM
vm <- forall s (m :: * -> *). MonadState s m => m s
get
case VM -> Maybe Integer -> Maybe Bool
maxIterationsReached VM
vm Maybe Integer
maxIter of
Maybe Bool
Nothing -> do
Expr 'End
a <- Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters (forall a. EVM a -> Stepper a
Stepper.evm (Bool -> EVM ()
continue Bool
True) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Stepper (Expr 'End)
k)
forall s (m :: * -> *). MonadState s m => s -> m ()
put VM
vm
Expr 'End
b <- Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters (forall a. EVM a -> Stepper a
Stepper.evm (Bool -> EVM ()
continue Bool
False) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Stepper (Expr 'End)
k)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Expr 'EWord -> Expr 'End -> Expr 'End -> Expr 'End
ITE Expr 'EWord
cond Expr 'End
a Expr 'End
b
Just Bool
n ->
Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters (forall a. EVM a -> Stepper a
Stepper.evm (Bool -> EVM ()
continue (Bool -> Bool
not Bool
n)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Stepper (Expr 'End)
k)
Stepper.Wait Query
q -> do
let performQuery :: StateT VM IO (Expr 'End)
performQuery = do
EVM ()
m <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Fetcher
fetcher Query
q)
Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters (forall a. EVM a -> Stepper a
Stepper.evm EVM ()
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Stepper (Expr 'End)
k)
case Query
q of
PleaseAskSMT Expr 'EWord
_ [Prop]
_ BranchCondition -> EVM ()
continue -> do
CodeLocation
codelocation <- VM -> CodeLocation
getCodeLocation forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *). MonadState s m => m s
get
Integer
iteration <- forall a b. (Integral a, Num b) => a -> b
num forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Lens' VM (Map CodeLocation Int)
iterations forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at CodeLocation
codelocation)
if Integer
iteration forall a. Ord a => a -> a -> Bool
< (forall a. a -> Maybe a -> a
fromMaybe Integer
5 Maybe Integer
askSmtIters)
then Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters (forall a. EVM a -> Stepper a
Stepper.evm (BranchCondition -> EVM ()
continue BranchCondition
EVM.Unknown) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> Stepper (Expr 'End)
k)
else StateT VM IO (Expr 'End)
performQuery
Query
_ -> StateT VM IO (Expr 'End)
performQuery
Stepper.EVM EVM b
m ->
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
State.state (forall s a. State s a -> s -> (a, s)
runState EVM b
m) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret Fetcher
fetcher Maybe Integer
maxIter Maybe Integer
askSmtIters forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stepper (Expr 'End)
k
maxIterationsReached :: VM -> Maybe Integer -> Maybe Bool
maxIterationsReached :: VM -> Maybe Integer -> Maybe Bool
maxIterationsReached VM
_ Maybe Integer
Nothing = forall a. Maybe a
Nothing
maxIterationsReached VM
vm (Just Integer
maxIter) =
let codelocation :: CodeLocation
codelocation = VM -> CodeLocation
getCodeLocation VM
vm
iters :: IxValue (Map CodeLocation Int)
iters = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at CodeLocation
codelocation forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> Iso' (Maybe a) a
non IxValue (Map CodeLocation Int)
0) VM
vm._iterations
in if forall a b. (Integral a, Num b) => a -> b
num Integer
maxIter forall a. Ord a => a -> a -> Bool
<= IxValue (Map CodeLocation Int)
iters
then forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (CodeLocation
codelocation, IxValue (Map CodeLocation Int)
iters forall a. Num a => a -> a -> a
- IxValue (Map CodeLocation Int)
1) VM
vm._cache._path
else forall a. Maybe a
Nothing
type Precondition = VM -> Prop
type Postcondition = VM -> Expr End -> Prop
checkAssert :: SolverGroup -> [Word256] -> ByteString -> Maybe Sig -> [String] -> VeriOpts -> IO (Expr End, [VerifyResult])
checkAssert :: SolverGroup
-> [Word256]
-> ByteString
-> Maybe Sig
-> [String]
-> VeriOpts
-> IO (Expr 'End, [VerifyResult])
checkAssert SolverGroup
solvers [Word256]
errs ByteString
c Maybe Sig
signature' [String]
concreteArgs VeriOpts
opts = SolverGroup
-> ByteString
-> Maybe Sig
-> [String]
-> VeriOpts
-> StorageModel
-> Maybe Precondition
-> Maybe Postcondition
-> IO (Expr 'End, [VerifyResult])
verifyContract SolverGroup
solvers ByteString
c Maybe Sig
signature' [String]
concreteArgs VeriOpts
opts StorageModel
SymbolicS forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ [Word256] -> Postcondition
checkAssertions [Word256]
errs)
checkAssertions :: [Word256] -> Postcondition
checkAssertions :: [Word256] -> Postcondition
checkAssertions [Word256]
errs VM
_ = \case
Revert [Prop]
_ (ConcreteBuf ByteString
msg) -> Bool -> Prop
PBool forall a b. (a -> b) -> a -> b
$ ByteString
msg forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word256 -> ByteString
panicMsg [Word256]
errs)
Revert [Prop]
_ Expr 'Buf
b -> forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Prop -> Prop -> Prop
PAnd (Bool -> Prop
PBool Bool
True) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Prop -> Prop
PNeg forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq Expr 'Buf
b forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Expr 'Buf
ConcreteBuf forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word256 -> ByteString
panicMsg) [Word256]
errs)
Expr 'End
_ -> Bool -> Prop
PBool Bool
True
defaultPanicCodes :: [Word256]
defaultPanicCodes :: [Word256]
defaultPanicCodes = [ Word256
0x00, Word256
0x01, Word256
0x12, Word256
0x21, Word256
0x22, Word256
0x31, Word256
0x32, Word256
0x41, Word256
0x51 ]
allPanicCodes :: [Word256]
allPanicCodes :: [Word256]
allPanicCodes = [ Word256
0x00, Word256
0x01, Word256
0x11, Word256
0x12, Word256
0x21, Word256
0x22, Word256
0x31, Word256
0x32, Word256
0x41, Word256
0x51 ]
panicMsg :: Word256 -> ByteString
panicMsg :: Word256 -> ByteString
panicMsg Word256
err = (Text -> ByteString
selector Text
"Panic(uint256)") forall a. Semigroup a => a -> a -> a
<> (AbiValue -> ByteString
encodeAbiValue forall a b. (a -> b) -> a -> b
$ Int -> Word256 -> AbiValue
AbiUInt Int
256 Word256
err)
mkCalldata :: Maybe Sig -> [String] -> (Expr Buf, [Prop])
mkCalldata :: Maybe Sig -> [String] -> (Expr 'Buf, [Prop])
mkCalldata Maybe Sig
Nothing [String]
_ =
( Text -> Expr 'Buf
AbstractBuf Text
"txdata"
, [Expr 'Buf -> Expr 'EWord
Expr.bufLength (Text -> Expr 'Buf
AbstractBuf Text
"txdata") Expr 'EWord -> Expr 'EWord -> Prop
.< (W256 -> Expr 'EWord
Lit (W256
2 forall a b. (Num a, Integral b) => a -> b -> a
^ (Integer
64 :: Integer)))]
)
mkCalldata (Just (Sig Text
name [AbiType]
types)) [String]
args = Text -> [AbiType] -> [String] -> Expr 'Buf -> (Expr 'Buf, [Prop])
symCalldata Text
name [AbiType]
types [String]
args (Text -> Expr 'Buf
AbstractBuf Text
"txdata")
verifyContract :: SolverGroup -> ByteString -> Maybe Sig -> [String] -> VeriOpts -> StorageModel -> Maybe Precondition -> Maybe Postcondition -> IO (Expr End, [VerifyResult])
verifyContract :: SolverGroup
-> ByteString
-> Maybe Sig
-> [String]
-> VeriOpts
-> StorageModel
-> Maybe Precondition
-> Maybe Postcondition
-> IO (Expr 'End, [VerifyResult])
verifyContract SolverGroup
solvers ByteString
theCode Maybe Sig
signature [String]
concreteArgs VeriOpts
opts StorageModel
storagemodel Maybe Precondition
maybepre Maybe Postcondition
maybepost = do
let preState :: VM
preState = (Expr 'Buf, [Prop])
-> ByteString -> Maybe Precondition -> StorageModel -> VM
abstractVM (Maybe Sig -> [String] -> (Expr 'Buf, [Prop])
mkCalldata Maybe Sig
signature [String]
concreteArgs) ByteString
theCode Maybe Precondition
maybepre StorageModel
storagemodel
SolverGroup
-> VeriOpts
-> VM
-> Maybe Postcondition
-> IO (Expr 'End, [VerifyResult])
verify SolverGroup
solvers VeriOpts
opts VM
preState Maybe Postcondition
maybepost
pruneDeadPaths :: [VM] -> [VM]
pruneDeadPaths :: [VM] -> [VM]
pruneDeadPaths =
forall a. (a -> Bool) -> [a] -> [a]
filter forall a b. (a -> b) -> a -> b
$ \VM
vm -> case VM
vm._result of
Just (VMFailure Error
DeadPath) -> Bool
False
Maybe VMResult
_ -> Bool
True
runExpr :: Stepper.Stepper (Expr End)
runExpr :: Stepper (Expr 'End)
runExpr = do
VM
vm <- Stepper VM
Stepper.runFully
let asserts :: [Prop]
asserts = VM
vm._keccakEqs
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case VM
vm._result of
Maybe VMResult
Nothing -> forall a. HasCallStack => String -> a
error String
"Internal Error: vm in intermediate state after call to runFully"
Just (VMSuccess Expr 'Buf
buf) -> [Prop] -> Expr 'Buf -> Expr 'Storage -> Expr 'End
Return [Prop]
asserts Expr 'Buf
buf VM
vm._env._storage
Just (VMFailure Error
e) -> case Error
e of
UnrecognizedOpcode Word8
_ -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
Invalid
Error
SelfDestruction -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
SelfDestruct
Error
EVM.StackLimitExceeded -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
EVM.Types.StackLimitExceeded
Error
EVM.IllegalOverflow -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
EVM.Types.IllegalOverflow
EVM.Revert Expr 'Buf
buf -> [Prop] -> Expr 'Buf -> Expr 'End
EVM.Types.Revert [Prop]
asserts Expr 'Buf
buf
Error
EVM.InvalidMemoryAccess -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
EVM.Types.InvalidMemoryAccess
Error
EVM.BadJumpDestination -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
EVM.Types.BadJumpDestination
Error
EVM.StackUnderrun -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts Error
EVM.Types.StackUnderrun
Error
e' -> [Prop] -> Error -> Expr 'End
Failure [Prop]
asserts forall a b. (a -> b) -> a -> b
$ String -> Error
EVM.Types.TmpErr (forall a. Show a => a -> String
show Error
e')
flattenExpr :: Expr End -> [([Prop], Expr End)]
flattenExpr :: Expr 'End -> [([Prop], Expr 'End)]
flattenExpr = [Prop] -> Expr 'End -> [([Prop], Expr 'End)]
go []
where
go :: [Prop] -> Expr End -> [([Prop], Expr End)]
go :: [Prop] -> Expr 'End -> [([Prop], Expr 'End)]
go [Prop]
pcs = \case
ITE Expr 'EWord
c Expr 'End
t Expr 'End
f -> [Prop] -> Expr 'End -> [([Prop], Expr 'End)]
go (Prop -> Prop
PNeg ((forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq Expr 'EWord
c (W256 -> Expr 'EWord
Lit W256
0))) forall a. a -> [a] -> [a]
: [Prop]
pcs) Expr 'End
t forall a. Semigroup a => a -> a -> a
<> [Prop] -> Expr 'End -> [([Prop], Expr 'End)]
go (forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq Expr 'EWord
c (W256 -> Expr 'EWord
Lit W256
0) forall a. a -> [a] -> [a]
: [Prop]
pcs) Expr 'End
f
e :: Expr 'End
e@(Revert [Prop]
_ Expr 'Buf
_) -> [([Prop]
pcs, Expr 'End
e)]
e :: Expr 'End
e@(Return [Prop]
_ Expr 'Buf
_ Expr 'Storage
_) -> [([Prop]
pcs, Expr 'End
e)]
Failure [Prop]
_ (TmpErr String
s) -> forall a. HasCallStack => String -> a
error String
s
e :: Expr 'End
e@(Failure [Prop]
_ Error
_) -> [([Prop]
pcs, Expr 'End
e)]
GVar GVar 'End
_ -> forall a. HasCallStack => String -> a
error String
"cannot flatten an Expr containing a GVar"
reachable :: SolverGroup -> Expr End -> IO ([SMT2], Expr End)
reachable :: SolverGroup -> Expr 'End -> IO ([SMT2], Expr 'End)
reachable SolverGroup
solvers Expr 'End
e = do
([SMT2], Maybe (Expr 'End))
res <- [Prop] -> Expr 'End -> IO ([SMT2], Maybe (Expr 'End))
go [] Expr 'End
e
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error String
"Internal Error: no reachable paths found")) ([SMT2], Maybe (Expr 'End))
res
where
go :: [Prop] -> Expr End -> IO ([SMT2], Maybe (Expr End))
go :: [Prop] -> Expr 'End -> IO ([SMT2], Maybe (Expr 'End))
go [Prop]
pcs = \case
ITE Expr 'EWord
c Expr 'End
t Expr 'End
f -> do
(([SMT2], Maybe (Expr 'End))
tres, ([SMT2], Maybe (Expr 'End))
fres) <- forall a b. IO a -> IO b -> IO (a, b)
concurrently
([Prop] -> Expr 'End -> IO ([SMT2], Maybe (Expr 'End))
go (forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq (W256 -> Expr 'EWord
Lit W256
1) Expr 'EWord
c forall a. a -> [a] -> [a]
: [Prop]
pcs) Expr 'End
t)
([Prop] -> Expr 'End -> IO ([SMT2], Maybe (Expr 'End))
go (forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
PEq (W256 -> Expr 'EWord
Lit W256
0) Expr 'EWord
c forall a. a -> [a] -> [a]
: [Prop]
pcs) Expr 'End
f)
let subexpr :: Maybe (Expr 'End)
subexpr = case (forall a b. (a, b) -> b
snd ([SMT2], Maybe (Expr 'End))
tres, forall a b. (a, b) -> b
snd ([SMT2], Maybe (Expr 'End))
fres) of
(Just Expr 'End
t', Just Expr 'End
f') -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Expr 'EWord -> Expr 'End -> Expr 'End -> Expr 'End
ITE Expr 'EWord
c Expr 'End
t' Expr 'End
f'
(Just Expr 'End
t', Maybe (Expr 'End)
Nothing) -> forall a. a -> Maybe a
Just Expr 'End
t'
(Maybe (Expr 'End)
Nothing, Just Expr 'End
f') -> forall a. a -> Maybe a
Just Expr 'End
f'
(Maybe (Expr 'End)
Nothing, Maybe (Expr 'End)
Nothing) -> forall a. Maybe a
Nothing
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. (a, b) -> a
fst ([SMT2], Maybe (Expr 'End))
tres forall a. Semigroup a => a -> a -> a
<> forall a b. (a, b) -> a
fst ([SMT2], Maybe (Expr 'End))
fres, Maybe (Expr 'End)
subexpr)
Expr 'End
leaf -> do
let query :: SMT2
query = [Prop] -> SMT2
assertProps [Prop]
pcs
CheckSatResult
res <- SolverGroup -> SMT2 -> IO CheckSatResult
checkSat SolverGroup
solvers SMT2
query
case CheckSatResult
res of
Sat SMTCex
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([SMT2
query], forall a. a -> Maybe a
Just Expr 'End
leaf)
CheckSatResult
Unsat -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ([SMT2
query], forall a. Maybe a
Nothing)
CheckSatResult
r -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Invalid solver result: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show CheckSatResult
r
evalProp :: Prop -> Prop
evalProp :: Prop -> Prop
evalProp = \case
o :: Prop
o@(PBool Bool
_) -> Prop
o
o :: Prop
o@(PNeg Prop
p) -> case Prop
p of
(PBool Bool
b) -> Bool -> Prop
PBool (Bool -> Bool
not Bool
b)
Prop
_ -> Prop
o
o :: Prop
o@(PEq Expr a
l Expr a
r) -> if Expr a
l forall a. Eq a => a -> a -> Bool
== Expr a
r
then Bool -> Prop
PBool Bool
True
else Prop
o
o :: Prop
o@(PLT (Lit W256
l) (Lit W256
r)) -> if W256
l forall a. Ord a => a -> a -> Bool
< W256
r
then Bool -> Prop
PBool Bool
True
else Prop
o
o :: Prop
o@(PGT (Lit W256
l) (Lit W256
r)) -> if W256
l forall a. Ord a => a -> a -> Bool
> W256
r
then Bool -> Prop
PBool Bool
True
else Prop
o
o :: Prop
o@(PGEq (Lit W256
l) (Lit W256
r)) -> if W256
l forall a. Ord a => a -> a -> Bool
>= W256
r
then Bool -> Prop
PBool Bool
True
else Prop
o
o :: Prop
o@(PLEq (Lit W256
l) (Lit W256
r)) -> if W256
l forall a. Ord a => a -> a -> Bool
<= W256
r
then Bool -> Prop
PBool Bool
True
else Prop
o
o :: Prop
o@(PAnd Prop
l Prop
r) -> case (Prop -> Prop
evalProp Prop
l, Prop -> Prop
evalProp Prop
r) of
(PBool Bool
True, PBool Bool
True) -> Bool -> Prop
PBool Bool
True
(PBool Bool
_, PBool Bool
_) -> Bool -> Prop
PBool Bool
False
(Prop, Prop)
_ -> Prop
o
o :: Prop
o@(POr Prop
l Prop
r) -> case (Prop -> Prop
evalProp Prop
l, Prop -> Prop
evalProp Prop
r) of
(PBool Bool
False, PBool Bool
False) -> Bool -> Prop
PBool Bool
False
(PBool Bool
_, PBool Bool
_) -> Bool -> Prop
PBool Bool
True
(Prop, Prop)
_ -> Prop
o
Prop
o -> Prop
o
extractProps :: Expr End -> [Prop]
= \case
ITE Expr 'EWord
_ Expr 'End
_ Expr 'End
_ -> []
Revert [Prop]
asserts Expr 'Buf
_ -> [Prop]
asserts
Return [Prop]
asserts Expr 'Buf
_ Expr 'Storage
_ -> [Prop]
asserts
Failure [Prop]
asserts Error
_ -> [Prop]
asserts
GVar GVar 'End
_ -> forall a. HasCallStack => String -> a
error String
"cannot extract props from a GVar"
verify :: SolverGroup -> VeriOpts -> VM -> Maybe Postcondition -> IO (Expr End, [VerifyResult])
verify :: SolverGroup
-> VeriOpts
-> VM
-> Maybe Postcondition
-> IO (Expr 'End, [VerifyResult])
verify SolverGroup
solvers VeriOpts
opts VM
preState Maybe Postcondition
maybepost = do
String -> IO ()
putStrLn String
"Exploring contract"
Expr 'End
exprInter <- forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret (SolverGroup -> RpcInfo -> Fetcher
Fetch.oracle SolverGroup
solvers VeriOpts
opts.rpcInfo) VeriOpts
opts.maxIter VeriOpts
opts.askSmtIters Stepper (Expr 'End)
runExpr) VM
preState
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when VeriOpts
opts.debug forall a b. (a -> b) -> a -> b
$ String -> Text -> IO ()
T.writeFile String
"unsimplified.expr" (forall (a :: EType). Expr a -> Text
formatExpr Expr 'End
exprInter)
String -> IO ()
putStrLn String
"Simplifying expression"
Expr 'End
expr <- if VeriOpts
opts.simp then (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (a :: EType). Expr a -> Expr a
Expr.simplify Expr 'End
exprInter) else forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr 'End
exprInter
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when VeriOpts
opts.debug forall a b. (a -> b) -> a -> b
$ String -> Text -> IO ()
T.writeFile String
"simplified.expr" (forall (a :: EType). Expr a -> Text
formatExpr Expr 'End
expr)
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Explored contract (" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (Expr 'End -> Int
Expr.numBranches Expr 'End
expr) forall a. Semigroup a => a -> a -> a
<> String
" branches)"
case Maybe Postcondition
maybepost of
Maybe Postcondition
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Expr 'End
expr, [forall a b c. a -> ProofResult a b c
Qed ()])
Just Postcondition
post -> do
let
canViolate :: [([Prop], Expr 'End)]
canViolate = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. (a -> Bool) -> [a] -> [a]
filter (Expr 'End -> [([Prop], Expr 'End)]
flattenExpr Expr 'End
expr) forall a b. (a -> b) -> a -> b
$
\([Prop]
_, Expr 'End
leaf) -> case Prop -> Prop
evalProp (Postcondition
post VM
preState Expr 'End
leaf) of
PBool Bool
True -> Bool
False
Prop
_ -> Bool
True
assumes :: [Prop]
assumes = VM
preState._constraints
withQueries :: [(SMT2, Expr 'End)]
withQueries = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\([Prop]
pcs, Expr 'End
leaf) -> ([Prop] -> SMT2
assertProps (Prop -> Prop
PNeg (Postcondition
post VM
preState Expr 'End
leaf) forall a. a -> [a] -> [a]
: [Prop]
assumes forall a. Semigroup a => a -> a -> a
<> Expr 'End -> [Prop]
extractProps Expr 'End
leaf forall a. Semigroup a => a -> a -> a
<> [Prop]
pcs), Expr 'End
leaf)) [([Prop], Expr 'End)]
canViolate
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Checking for reachability of " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length [(SMT2, Expr 'End)]
withQueries) forall a. Semigroup a => a -> a -> a
<> String
" potential property violation(s)"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when VeriOpts
opts.debug forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a b. [a] -> [b] -> [(a, b)]
zip [(Int
1 :: Int)..] [(SMT2, Expr 'End)]
withQueries) forall a b. (a -> b) -> a -> b
$ \(Int
idx, (SMT2
q, Expr 'End
leaf)) -> do
String -> Text -> IO ()
TL.writeFile
(String
"query-" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
idx forall a. Semigroup a => a -> a -> a
<> String
".smt2")
(Text
"; " forall a. Semigroup a => a -> a -> a
<> (String -> Text
TL.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Expr 'End
leaf) forall a. Semigroup a => a -> a -> a
<> Text
"\n\n" forall a. Semigroup a => a -> a -> a
<> SMT2 -> Text
formatSMT2 SMT2
q forall a. Semigroup a => a -> a -> a
<> Text
"\n\n(check-sat)")
[(CheckSatResult, Expr 'End)]
results <- forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) a b.
Traversable t =>
(a -> IO b) -> t a -> IO (t b)
mapConcurrently [(SMT2, Expr 'End)]
withQueries forall a b. (a -> b) -> a -> b
$ \(SMT2
query, Expr 'End
leaf) -> do
CheckSatResult
res <- SolverGroup -> SMT2 -> IO CheckSatResult
checkSat SolverGroup
solvers SMT2
query
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CheckSatResult
res, Expr 'End
leaf)
let cexs :: [(CheckSatResult, Expr 'End)]
cexs = forall a. (a -> Bool) -> [a] -> [a]
filter (\(CheckSatResult
res, Expr 'End
_) -> Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. CheckSatResult -> Bool
isUnsat forall a b. (a -> b) -> a -> b
$ CheckSatResult
res) [(CheckSatResult, Expr 'End)]
results
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ if forall (t :: * -> *) a. Foldable t => t a -> Bool
Prelude.null [(CheckSatResult, Expr 'End)]
cexs then (Expr 'End
expr, [forall a b c. a -> ProofResult a b c
Qed ()]) else (Expr 'End
expr, forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CheckSatResult, Expr 'End) -> VerifyResult
toVRes [(CheckSatResult, Expr 'End)]
cexs)
where
toVRes :: (CheckSatResult, Expr End) -> VerifyResult
toVRes :: (CheckSatResult, Expr 'End) -> VerifyResult
toVRes (CheckSatResult
res, Expr 'End
leaf) = case CheckSatResult
res of
Sat SMTCex
model -> forall a b c. b -> ProofResult a b c
Cex (Expr 'End
leaf, SMTCex
model)
CheckSatResult
EVM.Solvers.Unknown -> forall a b c. c -> ProofResult a b c
Timeout Expr 'End
leaf
CheckSatResult
Unsat -> forall a b c. a -> ProofResult a b c
Qed ()
Error Text
e -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Internal Error: solver responded with error: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Text
e
type UnsatCache = TVar [Set Prop]
equivalenceCheck :: SolverGroup -> ByteString -> ByteString -> VeriOpts -> (Expr Buf, [Prop]) -> IO [EquivResult]
equivalenceCheck :: SolverGroup
-> ByteString
-> ByteString
-> VeriOpts
-> (Expr 'Buf, [Prop])
-> IO [EquivResult]
equivalenceCheck SolverGroup
solvers ByteString
bytecodeA ByteString
bytecodeB VeriOpts
opts (Expr 'Buf, [Prop])
calldata' = do
case ByteString
bytecodeA forall a. Eq a => a -> a -> Bool
== ByteString
bytecodeB of
Bool
True -> do
String -> IO ()
putStrLn String
"bytecodeA and bytecodeB are identical"
forall (f :: * -> *) a. Applicative f => a -> f a
pure [forall a b c. a -> ProofResult a b c
Qed ()]
Bool
False -> do
[([Prop], Expr 'End)]
branchesA <- ByteString -> IO [([Prop], Expr 'End)]
getBranches ByteString
bytecodeA
[([Prop], Expr 'End)]
branchesB <- ByteString -> IO [([Prop], Expr 'End)]
getBranches ByteString
bytecodeB
let allPairs :: [(([Prop], Expr 'End), ([Prop], Expr 'End))]
allPairs = [(([Prop], Expr 'End)
a,([Prop], Expr 'End)
b) | ([Prop], Expr 'End)
a <- [([Prop], Expr 'End)]
branchesA, ([Prop], Expr 'End)
b <- [([Prop], Expr 'End)]
branchesB]
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Found " forall a. Semigroup a => a -> a -> a
<> (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [(([Prop], Expr 'End), ([Prop], Expr 'End))]
allPairs) forall a. Semigroup a => a -> a -> a
<> String
" total pairs of endstates"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when VeriOpts
opts.debug forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn
forall a b. (a -> b) -> a -> b
$ String
"endstates in bytecodeA: " forall a. Semigroup a => a -> a -> a
<> (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [([Prop], Expr 'End)]
branchesA)
forall a. Semigroup a => a -> a -> a
<> String
"\nendstates in bytecodeB: " forall a. Semigroup a => a -> a -> a
<> (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [([Prop], Expr 'End)]
branchesB)
let differingEndStates :: [Set Prop]
differingEndStates = forall a. [Set a] -> [Set a]
sortBySize (forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ([Prop], Expr 'End) -> ([Prop], Expr 'End) -> Maybe (Set Prop)
distinct) [(([Prop], Expr 'End), ([Prop], Expr 'End))]
allPairs)
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Asking the SMT solver for " forall a. Semigroup a => a -> a -> a
<> (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [Set Prop]
differingEndStates) forall a. Semigroup a => a -> a -> a
<> String
" pairs"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when VeriOpts
opts.debug forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a b. [a] -> [b] -> [(a, b)]
zip [Set Prop]
differingEndStates [(Integer
1::Integer)..]) (\(Set Prop
x, Integer
i) ->
String -> Text -> IO ()
T.writeFile (String
"prop-checked-" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Integer
i) (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Set Prop
x))
TVar [Set Prop]
knownUnsat <- forall a. a -> IO (TVar a)
newTVarIO []
Int
procs <- IO Int
getNumProcessors
[(EquivResult, Bool)]
results <- [Set Prop] -> TVar [Set Prop] -> Int -> IO [(EquivResult, Bool)]
checkAll [Set Prop]
differingEndStates TVar [Set Prop]
knownUnsat Int
procs
let useful :: Integer
useful = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(EquivResult
_, Bool
b) Integer
n -> if Bool
b then Integer
nforall a. Num a => a -> a -> a
+Integer
1 else Integer
n) (Integer
0::Integer) [(EquivResult, Bool)]
results
String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Reuse of previous queries was Useful in " forall a. Semigroup a => a -> a -> a
<> (forall a. Show a => a -> String
show Integer
useful) forall a. Semigroup a => a -> a -> a
<> String
" cases"
case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall a b c. ProofResult a b c -> Bool
isQed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ [(EquivResult, Bool)]
results of
Bool
True -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [forall a b c. a -> ProofResult a b c
Qed ()]
Bool
False -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/= forall a b c. a -> ProofResult a b c
Qed ()) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ [(EquivResult, Bool)]
results
where
sortBySize :: [Set a] -> [Set a]
sortBySize :: forall a. [Set a] -> [Set a]
sortBySize = forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (\Set a
a Set a
b -> if forall a. Set a -> Int
size Set a
a forall a. Ord a => a -> a -> Bool
> forall a. Set a -> Int
size Set a
b then Ordering
Prelude.LT else Ordering
Prelude.GT)
subsetAny :: Set Prop -> [Set Prop] -> Bool
subsetAny :: Set Prop -> [Set Prop] -> Bool
subsetAny Set Prop
a [Set Prop]
b = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Set Prop
bp Bool
acc -> Bool
acc Bool -> Bool -> Bool
|| forall a. Ord a => Set a -> Set a -> Bool
isSubsetOf Set Prop
a Set Prop
bp) Bool
False [Set Prop]
b
getBranches :: ByteString -> IO [([Prop], Expr End)]
getBranches :: ByteString -> IO [([Prop], Expr 'End)]
getBranches ByteString
bs = do
let
bytecode :: ByteString
bytecode = if ByteString -> Bool
BS.null ByteString
bs then [Word8] -> ByteString
BS.pack [Word8
0] else ByteString
bs
prestate :: VM
prestate = (Expr 'Buf, [Prop])
-> ByteString -> Maybe Precondition -> StorageModel -> VM
abstractVM (Expr 'Buf, [Prop])
calldata' ByteString
bytecode forall a. Maybe a
Nothing StorageModel
SymbolicS
Expr 'End
expr <- forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (Fetcher
-> Maybe Integer
-> Maybe Integer
-> Stepper (Expr 'End)
-> StateT VM IO (Expr 'End)
interpret (SolverGroup -> RpcInfo -> Fetcher
Fetch.oracle SolverGroup
solvers forall a. Maybe a
Nothing) VeriOpts
opts.maxIter VeriOpts
opts.askSmtIters Stepper (Expr 'End)
runExpr) VM
prestate
let simpl :: Expr 'End
simpl = if VeriOpts
opts.simp then (forall (a :: EType). Expr a -> Expr a
Expr.simplify Expr 'End
expr) else Expr 'End
expr
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Expr 'End -> [([Prop], Expr 'End)]
flattenExpr Expr 'End
simpl
check :: UnsatCache -> Set Prop -> IO (EquivResult, Bool)
check :: TVar [Set Prop] -> Set Prop -> IO (EquivResult, Bool)
check TVar [Set Prop]
knownUnsat Set Prop
props = do
let smt :: SMT2
smt = [Prop] -> SMT2
assertProps forall a b. (a -> b) -> a -> b
$ forall a. Set a -> [a]
Set.toList Set Prop
props
[Set Prop]
ku <- forall a. TVar a -> IO a
readTVarIO TVar [Set Prop]
knownUnsat
(Bool, CheckSatResult)
res <- if Set Prop -> [Set Prop] -> Bool
subsetAny Set Prop
props [Set Prop]
ku
then forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
True, CheckSatResult
Unsat)
else (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Bool
False),) (SolverGroup -> SMT2 -> IO CheckSatResult
checkSat SolverGroup
solvers SMT2
smt))
case (Bool, CheckSatResult)
res of
(Bool
_, Sat SMTCex
x) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b c. b -> ProofResult a b c
Cex SMTCex
x, Bool
False)
(Bool
quick, CheckSatResult
Unsat) -> case Bool
quick of
Bool
True -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b c. a -> ProofResult a b c
Qed (), Bool
quick)
Bool
False -> do
forall a. STM a -> IO a
atomically forall a b. (a -> b) -> a -> b
$ forall a. TVar a -> STM a
readTVar TVar [Set Prop]
knownUnsat forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. TVar a -> a -> STM ()
writeTVar TVar [Set Prop]
knownUnsat forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set Prop
props :)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b c. a -> ProofResult a b c
Qed (), Bool
False)
(Bool
_, CheckSatResult
EVM.Solvers.Unknown) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b c. c -> ProofResult a b c
Timeout (), Bool
False)
(Bool
_, Error Text
txt) -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Error while running solver: `" forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
txt
checkAll :: [(Set Prop)] -> UnsatCache -> Int -> IO [(EquivResult, Bool)]
checkAll :: [Set Prop] -> TVar [Set Prop] -> Int -> IO [(EquivResult, Bool)]
checkAll [Set Prop]
input TVar [Set Prop]
cache Int
numproc = do
IO (EquivResult, Bool) -> IO (EquivResult, Bool)
wrap <- forall a. Int -> IO (IO a -> IO a)
pool Int
numproc
forall a b. (a -> IO b) -> [a] -> IO [b]
parMapIO (IO (EquivResult, Bool) -> IO (EquivResult, Bool)
wrap forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TVar [Set Prop] -> Set Prop -> IO (EquivResult, Bool)
check TVar [Set Prop]
cache)) [Set Prop]
input
distinct :: ([Prop], Expr End) -> ([Prop], Expr End) -> Maybe (Set Prop)
distinct :: ([Prop], Expr 'End) -> ([Prop], Expr 'End) -> Maybe (Set Prop)
distinct ([Prop]
aProps, Expr 'End
aEnd) ([Prop]
bProps, Expr 'End
bEnd) =
let
differingResults :: Prop
differingResults = case (Expr 'End
aEnd, Expr 'End
bEnd) of
(Return [Prop]
_ Expr 'Buf
aOut Expr 'Storage
aStore, Return [Prop]
_ Expr 'Buf
bOut Expr 'Storage
bStore) ->
if Expr 'Buf
aOut forall a. Eq a => a -> a -> Bool
== Expr 'Buf
bOut Bool -> Bool -> Bool
&& Expr 'Storage
aStore forall a. Eq a => a -> a -> Bool
== Expr 'Storage
bStore
then Bool -> Prop
PBool Bool
False
else Expr 'Storage
aStore forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
./= Expr 'Storage
bStore Prop -> Prop -> Prop
.|| Expr 'Buf
aOut forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
./= Expr 'Buf
bOut
(Return {}, Expr 'End
_) -> Bool -> Prop
PBool Bool
True
(Expr 'End
_, Return {}) -> Bool -> Prop
PBool Bool
True
(Revert [Prop]
_ Expr 'Buf
a, Revert [Prop]
_ Expr 'Buf
b) -> if Expr 'Buf
a forall a. Eq a => a -> a -> Bool
== Expr 'Buf
b then Bool -> Prop
PBool Bool
False else Expr 'Buf
a forall (a :: EType). Typeable a => Expr a -> Expr a -> Prop
./= Expr 'Buf
b
(Revert [Prop]
_ Expr 'Buf
_, Expr 'End
_) -> Bool -> Prop
PBool Bool
True
(Expr 'End
_, Revert [Prop]
_ Expr 'Buf
_) -> Bool -> Prop
PBool Bool
True
(Failure [Prop]
_ (TmpErr String
s), Expr 'End
_) -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Unhandled error: " forall a. Semigroup a => a -> a -> a
<> String
s
(Expr 'End
_, Failure [Prop]
_ (TmpErr String
s)) -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Unhandled error: " forall a. Semigroup a => a -> a -> a
<> String
s
(Failure [Prop]
_ Error
erra, Failure [Prop]
_ Error
errb) -> if Error
erraforall a. Eq a => a -> a -> Bool
==Error
errb then Bool -> Prop
PBool Bool
False else Bool -> Prop
PBool Bool
True
(ITE Expr 'EWord
_ Expr 'End
_ Expr 'End
_, Expr 'End
_) -> forall a. HasCallStack => String -> a
error String
"Expressions must be flattened"
(Expr 'End
_, ITE Expr 'EWord
_ Expr 'End
_ Expr 'End
_) -> forall a. HasCallStack => String -> a
error String
"Expressions must be flattened"
(Expr 'End
a, Expr 'End
b) -> if Expr 'End
a forall a. Eq a => a -> a -> Bool
== Expr 'End
b
then Bool -> Prop
PBool Bool
False
else forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Internal Error: Unimplemented. Left: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Expr 'End
a forall a. Semigroup a => a -> a -> a
<> String
" Right: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Expr 'End
b
in case Prop
differingResults of
PBool Bool
False -> forall a. Maybe a
Nothing
PBool Bool
True -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> Set a
Set.fromList forall a b. (a -> b) -> a -> b
$ [Prop]
aProps forall a. Semigroup a => a -> a -> a
<> [Prop]
bProps
Prop
_ -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> Set a
Set.fromList forall a b. (a -> b) -> a -> b
$ Prop
differingResults forall a. a -> [a] -> [a]
: [Prop]
aProps forall a. Semigroup a => a -> a -> a
<> [Prop]
bProps
both' :: (a -> b) -> (a, a) -> (b, b)
both' :: forall a b. (a -> b) -> (a, a) -> (b, b)
both' a -> b
f (a
x, a
y) = (a -> b
f a
x, a -> b
f a
y)
produceModels :: SolverGroup -> Expr End -> IO [(Expr End, CheckSatResult)]
produceModels :: SolverGroup -> Expr 'End -> IO [(Expr 'End, CheckSatResult)]
produceModels SolverGroup
solvers Expr 'End
expr = do
let flattened :: [([Prop], Expr 'End)]
flattened = Expr 'End -> [([Prop], Expr 'End)]
flattenExpr Expr 'End
expr
withQueries :: [(SMT2, Expr 'End)]
withQueries = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first [Prop] -> SMT2
assertProps) [([Prop], Expr 'End)]
flattened
[(CheckSatResult, Expr 'End)]
results <- forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) a b.
Traversable t =>
(a -> IO b) -> t a -> IO (t b)
mapConcurrently [(SMT2, Expr 'End)]
withQueries forall a b. (a -> b) -> a -> b
$ \(SMT2
query, Expr 'End
leaf) -> do
CheckSatResult
res <- SolverGroup -> SMT2 -> IO CheckSatResult
checkSat SolverGroup
solvers SMT2
query
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CheckSatResult
res, Expr 'End
leaf)
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> (b, a)
swap forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (\(CheckSatResult
res, Expr 'End
_) -> Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. CheckSatResult -> Bool
isUnsat forall a b. (a -> b) -> a -> b
$ CheckSatResult
res) [(CheckSatResult, Expr 'End)]
results
showModel :: Expr Buf -> (Expr End, CheckSatResult) -> IO ()
showModel :: Expr 'Buf -> (Expr 'End, CheckSatResult) -> IO ()
showModel Expr 'Buf
cd (Expr 'End
expr, CheckSatResult
res) = do
case CheckSatResult
res of
CheckSatResult
Unsat -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Error Text
e -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Internal error: smt solver returned an error: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Text
e
CheckSatResult
EVM.Solvers.Unknown -> do
String -> IO ()
putStrLn String
"--- Branch ---"
String -> IO ()
putStrLn String
""
String -> IO ()
putStrLn String
"Unable to produce a model for the following end state:"
String -> IO ()
putStrLn String
""
Text -> IO ()
T.putStrLn forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ forall (a :: EType). Expr a -> Text
formatExpr Expr 'End
expr
String -> IO ()
putStrLn String
""
Sat SMTCex
cex -> do
String -> IO ()
putStrLn String
"--- Branch ---"
String -> IO ()
putStrLn String
""
String -> IO ()
putStrLn String
"Inputs:"
String -> IO ()
putStrLn String
""
Text -> IO ()
T.putStrLn forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ Expr 'Buf -> SMTCex -> Text
formatCex Expr 'Buf
cd SMTCex
cex
String -> IO ()
putStrLn String
""
String -> IO ()
putStrLn String
"End State:"
String -> IO ()
putStrLn String
""
Text -> IO ()
T.putStrLn forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ forall (a :: EType). Expr a -> Text
formatExpr Expr 'End
expr
String -> IO ()
putStrLn String
""
formatCex :: Expr Buf -> SMTCex -> Text
formatCex :: Expr 'Buf -> SMTCex -> Text
formatCex Expr 'Buf
cd m :: SMTCex
m@(SMTCex Map (Expr 'EWord) W256
_ Map (Expr 'Buf) BufModel
_ Map W256 (Map W256 W256)
store Map (Expr 'EWord) W256
blockContext Map (Expr 'EWord) W256
txContext) = [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$
[ Text
"Calldata:"
, Int -> Text -> Text
indent Int
2 Text
cd'
, Text
""
]
forall a. Semigroup a => a -> a -> a
<> [Text]
storeCex
forall a. Semigroup a => a -> a -> a
<> [Text]
txCtx
forall a. Semigroup a => a -> a -> a
<> [Text]
blockCtx
where
cd' :: Text
cd' = Expr 'Buf -> Text
prettyBuf forall a b. (a -> b) -> a -> b
$ forall (a :: EType). Expr a -> Expr a
Expr.simplify forall a b. (a -> b) -> a -> b
$ forall (a :: EType). SMTCex -> Expr a -> Expr a
subModel SMTCex
m Expr 'Buf
cd
storeCex :: [Text]
storeCex :: [Text]
storeCex
| forall k a. Map k a -> Bool
Map.null Map W256 (Map W256 W256)
store = []
| Bool
otherwise =
[ Text
"Storage:"
, Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey (\W256
key Map W256 W256
val [Text]
acc -> (Text
"Addr " forall a. Semigroup a => a -> a -> a
<> (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show W256
key) forall a. Semigroup a => a -> a -> a
<> Text
": " forall a. Semigroup a => a -> a -> a
<> (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show (forall k a. Map k a -> [(k, a)]
Map.toList Map W256 W256
val))) forall a. a -> [a] -> [a]
: [Text]
acc) forall a. Monoid a => a
mempty Map W256 (Map W256 W256)
store
, Text
""
]
txCtx :: [Text]
txCtx :: [Text]
txCtx
| forall k a. Map k a -> Bool
Map.null Map (Expr 'EWord) W256
txContext = []
| Bool
otherwise =
[ Text
"Transaction Context:"
, Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey (\Expr 'EWord
key W256
val [Text]
acc -> (Expr 'EWord -> Text
showTxCtx Expr 'EWord
key forall a. Semigroup a => a -> a -> a
<> Text
": " forall a. Semigroup a => a -> a -> a
<> (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show W256
val)) forall a. a -> [a] -> [a]
: [Text]
acc) forall a. Monoid a => a
mempty (Map (Expr 'EWord) W256 -> Map (Expr 'EWord) W256
filterSubCtx Map (Expr 'EWord) W256
txContext)
, Text
""
]
showTxCtx :: Expr EWord -> Text
showTxCtx :: Expr 'EWord -> Text
showTxCtx (CallValue Int
_) = Text
"CallValue"
showTxCtx (Caller Int
_) = Text
"Caller"
showTxCtx (Address Int
_) = Text
"Address"
showTxCtx Expr 'EWord
x = String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Expr 'EWord
x
filterSubCtx :: Map (Expr EWord) W256 -> Map (Expr EWord) W256
filterSubCtx :: Map (Expr 'EWord) W256 -> Map (Expr 'EWord) W256
filterSubCtx = forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey Expr 'EWord -> W256 -> Bool
go
where
go :: Expr EWord -> W256 -> Bool
go :: Expr 'EWord -> W256 -> Bool
go (CallValue Int
x) W256
_ = Int
x forall a. Eq a => a -> a -> Bool
== Int
0
go (Caller Int
x) W256
_ = Int
x forall a. Eq a => a -> a -> Bool
== Int
0
go (Address Int
x) W256
_ = Int
x forall a. Eq a => a -> a -> Bool
== Int
0
go (Balance {}) W256
_ = forall a. HasCallStack => String -> a
error String
"TODO: BALANCE"
go (SelfBalance {}) W256
_ = forall a. HasCallStack => String -> a
error String
"TODO: SELFBALANCE"
go (Gas {}) W256
_ = forall a. HasCallStack => String -> a
error String
"TODO: Gas"
go Expr 'EWord
_ W256
_ = Bool
False
blockCtx :: [Text]
blockCtx :: [Text]
blockCtx
| forall k a. Map k a -> Bool
Map.null Map (Expr 'EWord) W256
blockContext = []
| Bool
otherwise =
[ Text
"Block Context:"
, Int -> Text -> Text
indent Int
2 forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey (\Expr 'EWord
key W256
val [Text]
acc -> (String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Expr 'EWord
key forall a. Semigroup a => a -> a -> a
<> String
": " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show W256
val) forall a. a -> [a] -> [a]
: [Text]
acc) forall a. Monoid a => a
mempty Map (Expr 'EWord) W256
txContext
, Text
""
]
prettyBuf :: Expr Buf -> Text
prettyBuf :: Expr 'Buf -> Text
prettyBuf (ConcreteBuf ByteString
"") = Text
"Empty"
prettyBuf (ConcreteBuf ByteString
bs) = ByteString -> Text
formatBinary ByteString
bs
prettyBuf Expr 'Buf
_ = Text
"Any"
subModel :: SMTCex -> Expr a -> Expr a
subModel :: forall (a :: EType). SMTCex -> Expr a -> Expr a
subModel SMTCex
c Expr a
expr = forall {a :: EType}. Map (Expr 'Buf) ByteString -> Expr a -> Expr a
subBufs (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BufModel -> ByteString
forceFlattened SMTCex
c.buffers) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a :: EType}. Map (Expr 'EWord) W256 -> Expr a -> Expr a
subVars SMTCex
c.vars forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: EType). Map W256 (Map W256 W256) -> Expr a -> Expr a
subStore SMTCex
c.store forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a :: EType}. Map (Expr 'EWord) W256 -> Expr a -> Expr a
subVars SMTCex
c.blockContext forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a :: EType}. Map (Expr 'EWord) W256 -> Expr a -> Expr a
subVars SMTCex
c.txContext forall a b. (a -> b) -> a -> b
$ Expr a
expr
where
forceFlattened :: BufModel -> ByteString
forceFlattened (SMT.Flat ByteString
bs) = ByteString
bs
forceFlattened b :: BufModel
b@(SMT.Comp CompressedBuf
_) = BufModel -> ByteString
forceFlattened forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Internal Error: cannot flatten buffer: " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show BufModel
b) (BufModel -> Maybe BufModel
SMT.collapse BufModel
b)
subVars :: Map (Expr 'EWord) W256 -> Expr a -> Expr a
subVars Map (Expr 'EWord) W256
model Expr a
b = forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey forall (a :: EType). Expr a -> Expr 'EWord -> W256 -> Expr a
subVar Expr a
b Map (Expr 'EWord) W256
model
subVar :: Expr a -> Expr EWord -> W256 -> Expr a
subVar :: forall (a :: EType). Expr a -> Expr 'EWord -> W256 -> Expr a
subVar Expr a
b Expr 'EWord
var W256
val = forall (b :: EType).
(forall (a :: EType). Expr a -> Expr a) -> Expr b -> Expr b
mapExpr forall (a :: EType). Expr a -> Expr a
go Expr a
b
where
go :: Expr a -> Expr a
go :: forall (a :: EType). Expr a -> Expr a
go = \case
v :: Expr a
v@(Var Text
_) -> if Expr a
v forall a. Eq a => a -> a -> Bool
== Expr 'EWord
var
then W256 -> Expr 'EWord
Lit W256
val
else Expr a
v
Expr a
e -> Expr a
e
subBufs :: Map (Expr 'Buf) ByteString -> Expr a -> Expr a
subBufs Map (Expr 'Buf) ByteString
model Expr a
b = forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey forall (a :: EType). Expr a -> Expr 'Buf -> ByteString -> Expr a
subBuf Expr a
b Map (Expr 'Buf) ByteString
model
subBuf :: Expr a -> Expr Buf -> ByteString -> Expr a
subBuf :: forall (a :: EType). Expr a -> Expr 'Buf -> ByteString -> Expr a
subBuf Expr a
b Expr 'Buf
var ByteString
val = forall (b :: EType).
(forall (a :: EType). Expr a -> Expr a) -> Expr b -> Expr b
mapExpr forall (a :: EType). Expr a -> Expr a
go Expr a
b
where
go :: Expr a -> Expr a
go :: forall (a :: EType). Expr a -> Expr a
go = \case
a :: Expr a
a@(AbstractBuf Text
_) -> if Expr a
a forall a. Eq a => a -> a -> Bool
== Expr 'Buf
var
then ByteString -> Expr 'Buf
ConcreteBuf ByteString
val
else Expr a
a
Expr a
e -> Expr a
e
subStore :: Map W256 (Map W256 W256) -> Expr a -> Expr a
subStore :: forall (a :: EType). Map W256 (Map W256 W256) -> Expr a -> Expr a
subStore Map W256 (Map W256 W256)
m Expr a
b = forall (b :: EType).
(forall (a :: EType). Expr a -> Expr a) -> Expr b -> Expr b
mapExpr forall (a :: EType). Expr a -> Expr a
go Expr a
b
where
go :: Expr a -> Expr a
go :: forall (a :: EType). Expr a -> Expr a
go = \case
Expr a
AbstractStore -> Map W256 (Map W256 W256) -> Expr 'Storage
ConcreteStore Map W256 (Map W256 W256)
m
Expr a
e -> Expr a
e