Skip to content

Commit

Permalink
Update filterFallbacks to be selective rather than all-or-nothing (#1200
Browse files Browse the repository at this point in the history
)

* Update filterFallbacks

* hlint suggested eta reducing

* sometimes contractNames are filepath:name, we have to account for that

* calculate eventMap in mkWorld rather than giving it as an argument
  • Loading branch information
samalws-tob authored Apr 25, 2024
1 parent 5b66fd2 commit 27ad095
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/Echidna.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import EVM (cheatCode)
import EVM.ABI (AbiValue(AbiAddress))
import EVM.Dapp (DappInfo(..), dappInfo)
import EVM.Fetch qualified
import EVM.Solidity (SolcContract(..), BuildOutput)
import EVM.Solidity (BuildOutput)
import EVM.Types hiding (Env)

import Echidna.ABI
Expand Down Expand Up @@ -72,8 +72,7 @@ prepareContract env solFiles specifiedContract seed = do
(forceAddr vm.state.contract)
funs

eventMap = Map.unions $ map (.eventMap) contracts
world = mkWorld solConf eventMap signatureMap specifiedContract slitherInfo
world = mkWorld solConf signatureMap specifiedContract slitherInfo contracts

deployedAddresses = Set.fromList $ AbiAddress . forceAddr <$> Map.keys vm.env.contracts
constants = enhanceConstants slitherInfo
Expand Down
22 changes: 13 additions & 9 deletions lib/Echidna/Solidity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Echidna.ABI
, commonTypeSizes, mkValidAbiInt, mkValidAbiUInt )
import Echidna.Deploy (deployContracts, deployBytecodes)
import Echidna.Etheno (loadEthenoBatch)
import Echidna.Events (EventMap, extractEvents)
import Echidna.Events (extractEvents)
import Echidna.Exec (execTx, initialVM)
import Echidna.SourceAnalysis.Slither
import Echidna.Symbolic (forceAddr)
Expand Down Expand Up @@ -296,18 +296,19 @@ loadSpecified env name cs = do
-- for running a 'Campaign' against the tests found.
mkWorld
:: SolConf
-> EventMap
-> SignatureMap
-> Maybe ContractName
-> SlitherInfo
-> [SolcContract]
-> World
mkWorld SolConf{sender, testMode} eventMap sigMap maybeContract slitherInfo =
mkWorld SolConf{sender, testMode} sigMap maybeContract slitherInfo contracts =
let
eventMap = Map.unions $ map (.eventMap) contracts
payableSigs = filterResults maybeContract slitherInfo.payableFunctions
as = if isAssertionMode testMode then filterResults maybeContract slitherInfo.asserts else []
cs = if isDapptestMode testMode then [] else filterResults maybeContract slitherInfo.constantFunctions \\ as
(highSignatureMap, lowSignatureMap) = prepareHashMaps cs as $
filterFallbacks maybeContract slitherInfo.fallbackDefined slitherInfo.receiveDefined sigMap
filterFallbacks slitherInfo.fallbackDefined slitherInfo.receiveDefined contracts sigMap
in World { senders = sender
, highSignatureMap
, lowSignatureMap
Expand All @@ -325,16 +326,19 @@ filterResults (Just contractName) rs =
filterResults Nothing rs = hashSig <$> (concat . Map.elems) rs

filterFallbacks
:: Maybe ContractName
-> [ContractName]
:: [ContractName]
-> [ContractName]
-> [SolcContract]
-> SignatureMap
-> SignatureMap
filterFallbacks _ [] [] sm = Map.map f sm
where f ss = NE.fromList $ case NE.filter (/= fallback) ss of
filterFallbacks la lb contracts = Map.mapWithKey f
where
f k ss | k `elem` keysToIgnore = ss
f _ ss = NE.fromList $ case NE.filter (/= fallback) ss of
[] -> [fallback] -- No other alternative
ss' -> ss'
filterFallbacks _ _ _ sm = sm
keysToIgnore = concatMap contractNameToCodehashes (la ++ lb)
contractNameToCodehashes name = map (.runtimeCodehash) $ filter (\c -> last (T.splitOn ":" c.contractName) == name) contracts

prepareHashMaps
:: [FunctionSelector]
Expand Down

0 comments on commit 27ad095

Please sign in to comment.