Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to provide a directory with additional json files from slither printer #1125

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/Echidna.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ prepareContract
-> IO (VM, World, GenDict)
prepareContract env contracts solFiles specifiedContract seed = do
let solConf = env.cfg.solConf
let campaignConf = env.cfg.campaignConf

-- compile and load contracts
(vm, funs, testNames, signatureMap) <- loadSpecified env specifiedContract contracts

-- run processors
slitherInfo <- runSlither (NE.head solFiles) solConf
mainSlitherInfo <- runSlither (NE.head solFiles) solConf
complementarySlitherInfo <- loadSlitherInfos campaignConf
let slitherInfo = appendSlitherInfo mainSlitherInfo complementarySlitherInfo
case find (< minSupportedSolcVersion) slitherInfo.solcVersions of
Just version | detectVyperVersion version -> pure ()
Just version -> throwM $ OutdatedSolcVersion version
Expand Down
1 change: 1 addition & 0 deletions lib/Echidna/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ instance FromJSON EConfigWithUsage where
<*> v ..:? "seed"
<*> v ..:? "dictFreq" ..!= 0.40
<*> v ..:? "corpusDir" ..!= Nothing
<*> v ..:? "slitherInfoDir" ..!= Nothing
<*> v ..:? "mutConsts" ..!= defaultMutationConsts
<*> v ..:? "coverageFormats" ..!= [Txt,Html,Lcov]
<*> v ..:? "workers"
Expand Down
42 changes: 35 additions & 7 deletions lib/Echidna/Processor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.ByteString.Base16 qualified as BS16 (decode)
import Data.ByteString.Lazy.Char8 qualified as BSL
import Data.ByteString.UTF8 qualified as BSU
import Data.Either (fromRight)
import Data.List (isPrefixOf)
import Data.List (isPrefixOf, isSuffixOf)
import Data.List.NonEmpty qualified as NE
import Data.Map (Map)
import Data.Map qualified as Map
Expand All @@ -19,7 +19,7 @@ import Data.SemVer (Version, fromText)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (pack)
import System.Directory (findExecutable)
import System.Directory (findExecutable, listDirectory)
import System.Exit (ExitCode(..))
import System.Process (StdStream(..), readCreateProcessWithExitCode, proc, std_err)
import Text.Read (readMaybe)
Expand All @@ -30,6 +30,7 @@ import EVM.Types (Addr(..), FunctionSelector)
import Echidna.ABI (hashSig, makeNumAbiValues, makeArrayAbiValues)
import Echidna.Types.Signature (ContractName, FunctionName)
import Echidna.Types.Solidity (SolConf(..))
import Echidna.Types.Campaign (CampaignConf(..))
import Echidna.Utility (measureIO)

-- | Things that can go wrong trying to run a processor. Read the 'Show'
Expand Down Expand Up @@ -135,12 +136,39 @@ runSlither fp solConf = do
(ec, out, err) <- measureIO solConf.quiet ("Running slither on " <> fp) $
readCreateProcessWithExitCode (proc path args) {std_err = Inherit} ""
case ec of
ExitSuccess ->
case eitherDecode (BSL.pack out) of
Right si -> pure si
Left msg -> throwM $
ProcessorFailure "slither" ("decoding slither output failed:\n" ++ msg)
ExitSuccess -> decodeSlitherPrinterJSON (BSL.pack out)
ExitFailure _ -> throwM $ ProcessorFailure "slither" err

decodeSlitherPrinterJSON :: (MonadThrow f) => BSL.ByteString -> f SlitherInfo
decodeSlitherPrinterJSON buffer =
case eitherDecode buffer of
Right si -> pure si
Left msg -> throwM $ ProcessorFailure "slither" ("decoding slither output failed:\n" ++ msg)

loadSlitherInfos :: CampaignConf -> IO SlitherInfo
loadSlitherInfos solConf = case solConf.slitherInfoDir of
Nothing -> return noInfo
Just d -> decodeSlitherPrinterJSONBatch d

decodeSlitherPrinterJSONBatch :: FilePath -> IO SlitherInfo
decodeSlitherPrinterJSONBatch d = do
fs <- filter (".json" `Data.List.isSuffixOf`) <$> listDirectory d
bbs <- mapM BSL.readFile fs
sis <- mapM decodeSlitherPrinterJSON bbs
return $ foldr appendSlitherInfo noInfo sis

appendSlitherInfo :: SlitherInfo -> SlitherInfo -> SlitherInfo
appendSlitherInfo si1 si2 =
SlitherInfo {
payableFunctions = Map.union si1.payableFunctions si2.payableFunctions,
constantFunctions = Map.union si1.constantFunctions si2.constantFunctions,
asserts = Map.union si1.asserts si2.asserts,
constantValues = Map.union si1.constantValues si2.constantValues,
generationGraph = Map.union si1.generationGraph si2.generationGraph,
solcVersions = si1.solcVersions ++ si2.solcVersions,
fallbackDefined = si1.fallbackDefined ++ si2.fallbackDefined,
receiveDefined = si1.receiveDefined ++ si2.receiveDefined
}

noInfo :: SlitherInfo
noInfo = SlitherInfo mempty mempty mempty mempty mempty [] [] []
2 changes: 2 additions & 0 deletions lib/Echidna/Types/Campaign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ data CampaignConf = CampaignConf
-- ^ Frequency for the use of dictionary values in the random transactions
, corpusDir :: Maybe FilePath
-- ^ Directory to load and save lists of transactions
, slitherInfoDir :: Maybe FilePath
-- ^ Directory to load additional slither info files
, mutConsts :: MutationConsts Integer
-- ^ Directory to load and save lists of transactions
, coverageFormats :: [CoverageFileType]
Expand Down