Skip to content
Open
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
28 changes: 27 additions & 1 deletion src/EVM/Dapp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,35 @@ showTraceLocation dapp trace =
Nothing -> Left "<no source map>"
Just sm ->
case srcMapCodePos dapp.sources sm of
Nothing -> Left "<source not found>"
Just (fileName, lineIx) ->
Right (pack fileName <> ":" <> pack (show lineIx))
-- srcmap points to a file not in the source cache (e.g. compiler-
-- generated panic handler). Walk backwards through the contract's
-- srcmap to find the nearest entry with a valid source location —
-- this typically resolves to the assert/require that triggered the
-- error.
Nothing ->
case fallbackSrcPos dapp trace of
Just (fileName, lineIx) ->
Right (pack fileName <> ":" <> pack (show lineIx))
Nothing -> Left "<source not found>"

-- | Walk backwards from the current opIx to find the nearest srcmap entry
-- whose file index exists in the source cache.
fallbackSrcPos :: DappInfo -> Trace -> Maybe (FilePath, Int)
fallbackSrcPos dapp trace = do
sol <- findSrc trace.contract dapp
let srcmaps = case trace.contract.code of
InitCode _ _ -> sol.creationSrcmap
RuntimeCode _ -> sol.runtimeSrcmap
UnknownCode _ -> mempty
go (trace.opIx - 1) srcmaps
where
go i srcmaps
| i < 0 = Nothing
| otherwise = case Seq.lookup i srcmaps >>= srcMapCodePos dapp.sources of
Just pos -> Just pos
Nothing -> go (i - 1) srcmaps

srcMapCodePos :: SourceCache -> SrcMap -> Maybe (FilePath, Int)
srcMapCodePos cache sm =
Expand Down
4 changes: 4 additions & 0 deletions test/EVM/Test/FoundryTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ tests = testGroup "Foundry tests"
, test "Keccak" $ do
let testFile = "test/contracts/pass/keccak.sol"
executeSingleMethod testFile "prove_access" >>= assertEqualM "test result" (True, True)
, test "Panic-Source-Location" $ do
-- Regression test for #895: panic in a called contract should not show "<source not found>"
let testFile = "test/contracts/fail/assertPanic.sol"
executeSingleMethod testFile "prove_panic" >>= assertEqualM "prove_panic" (False, False)
]

executeSingleMethod :: (App m, MonadMask m) => FilePath -> Text -> m (Bool, Bool)
Expand Down
15 changes: 15 additions & 0 deletions test/contracts/fail/assertPanic.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import "forge-std/Test.sol";

contract PanicTest is Test {
function prove_panic() public {
Panicker p = new Panicker();
p.doPanic();
assert(true);
}
}

contract Panicker {
function doPanic() public pure {
assert(false);
}
}
Loading