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

Emit "saved reproducer" log message as event rather than putStrLn #1274

Merged
merged 1 commit into from
Jun 21, 2024
Merged
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
8 changes: 4 additions & 4 deletions lib/Echidna/Output/Corpus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +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"
putStrLn ("Saving reproducer to " ++ file)
unlessM (doesFileExist file) $ encodeFile file (toJSON txSeq)
pushCampaignEvent env (ReproducerSaved file)

loadTxs :: FilePath -> IO [(FilePath, [Tx])]
loadTxs dir = do
Expand Down Expand Up @@ -59,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