Skip to content

Commit

Permalink
emit saved reproducer log message as event rather than putstrln
Browse files Browse the repository at this point in the history
  • Loading branch information
samalws-tob committed Jun 14, 2024
1 parent 8347ad9 commit 8d25e64
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/Echidna/Output/Corpus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import Echidna.Types.Test (EchidnaTest(..))
import Echidna.Types.Tx (Tx)
import Echidna.Utility (listDirectory, withCurrentDirectory)

saveTxs :: FilePath -> [[Tx]] -> IO ()
saveTxs dir = mapM_ saveTxSeq where
saveTxs :: Env -> FilePath -> [[Tx]] -> IO ()
saveTxs env dir = mapM_ saveTxSeq where
saveTxSeq txSeq = do
createDirectoryIfMissing True dir
let file = dir </> (show . abs . hash . show) txSeq <.> "txt"
unlessM (doesFileExist file) $ encodeFile file (toJSON txSeq)
pushCampaignEvent env (ReproducerSaved file)

loadTxs :: FilePath -> IO [(FilePath, [Tx])]
loadTxs dir = do
Expand Down Expand Up @@ -58,7 +59,7 @@ saveCorpusEvent env (_time, campaignEvent) = do

saveFile dir (subdir, txs) =
unless (null txs) $
handle exceptionHandler $ saveTxs (dir </> subdir) [txs]
handle exceptionHandler $ saveTxs env (dir </> subdir) [txs]

exceptionHandler (e :: IOException) =
pushCampaignEvent env (Failure $ "Problem while writing to file: " ++ show e)
5 changes: 5 additions & 0 deletions lib/Echidna/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ instance ToJSON SSE where
object [ "timestamp" .= time
, "data" .= reason
]
toJSON (SSE (time, ReproducerSaved filename)) =
object [ "timestamp" .= time
, "filename" .= filename
]

runSSEServer :: MVar () -> Env -> Word16 -> Int -> IO ()
runSSEServer serverStopVar env port nworkers = do
Expand All @@ -53,6 +57,7 @@ runSSEServer serverStopVar env port nworkers = do
TxSequenceReplayFailed {} -> "tx_sequence_replay_failed"
WorkerStopped _ -> "worker_stopped"
Failure _err -> "failure"
ReproducerSaved _ -> "saved_reproducer"
case campaignEvent of
WorkerEvent _ _ (WorkerStopped _) -> do
aliveAfter <- atomicModifyIORef' aliveRef (\n -> (n-1, n-1))
Expand Down
2 changes: 2 additions & 0 deletions lib/Echidna/Types/Campaign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type WorkerId = Int
data CampaignEvent
= WorkerEvent WorkerId WorkerType WorkerEvent
| Failure String
| ReproducerSaved String -- filename

data WorkerEvent
= TestFalsified !EchidnaTest
Expand Down Expand Up @@ -111,6 +112,7 @@ ppCampaignEvent :: CampaignEvent -> String
ppCampaignEvent = \case
WorkerEvent _ _ e -> ppWorkerEvent e
Failure err -> err
ReproducerSaved f -> "Saved reproducer to " <> f

ppWorkerEvent :: WorkerEvent -> String
ppWorkerEvent = \case
Expand Down
4 changes: 2 additions & 2 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ main = withUtf8 $ withCP65001 $ do
Nothing -> pure ()
Just dir -> do
measureIO cfg.solConf.quiet "Saving test reproducers" $
saveTxs (dir </> "reproducers") (filter (not . null) $ (.reproducer) <$> tests)
saveTxs env (dir </> "reproducers") (filter (not . null) $ (.reproducer) <$> tests)

saveTracesEnabled <- lookupEnv "ECHIDNA_SAVE_TRACES"
when (isJust saveTracesEnabled) $ do
Expand All @@ -98,7 +98,7 @@ main = withUtf8 $ withCP65001 $ do

measureIO cfg.solConf.quiet "Saving corpus" $ do
corpus <- readIORef env.corpusRef
saveTxs (dir </> "coverage") (snd <$> Set.toList corpus)
saveTxs env (dir </> "coverage") (snd <$> Set.toList corpus)

-- TODO: We use the corpus dir to save coverage reports which is confusing.
-- Add config option to pass dir for saving coverage report and decouple it
Expand Down

0 comments on commit 8d25e64

Please sign in to comment.