dapphub / dapptools

Dapp, Seth, Hevm, and more

Home Page:https://dapp.tools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect logs emitted when downcasting to `bytes2` or `bytes1`

ChmielewskiKamil opened this issue · comments

I initially submitted this issue to the Trail of Bits Echidna repo.

What is the problem?

Logs emitted by dapp test -vvv are incorrect when downcasting from bytes32 to bytes2 or bytes1.

Here is an example test that showcases this issue (you can find the repository on my GitHub):

contract WeirdLogsTest is DSTest {
    // This is the default Echidna contractAddr
    // see: https://github.com/crytic/echidna/wiki/Config
    address weirdLogs = address(0x00a329c0648769A73afAc7F9381E08FB43dBEA72);

    bytes32 originalHash = keccak256(abi.encodePacked(weirdLogs));

    event OriginalHash(bytes32 originalHash);
    event HashCastedToBytes16(bytes16 originalHash);
    event HashCastedToBytes8(bytes8 originalHash);
    event HashCastedToBytes4(bytes4 originalHash);
    event HashCastedToBytes3(bytes3 originalHash);
    event HashCastedToBytes2(bytes2 originalHash);
    event HashCastedToBytes1(bytes1 orignalHash);

    function test_always_false() public {
        emit OriginalHash(originalHash);
        emit HashCastedToBytes16(bytes16(originalHash));
        emit HashCastedToBytes8(bytes8(originalHash));
        emit HashCastedToBytes4(bytes4(originalHash));
        emit HashCastedToBytes3(bytes3(originalHash));
        emit HashCastedToBytes2(bytes2(originalHash));
        emit HashCastedToBytes1(bytes1(originalHash));
        assert(false);
    }
}

Running this test with dapp test -vvv results in the following output:

Running 1 tests for src/WeirdLogs.t.sol:WeirdLogsTest
[BAIL] test_always_false()

Failure: test_always_false
  src/WeirdLogs.t.sol:WeirdLogsTest
   ├╴constructor
   └╴test_always_false()
      ├╴OriginalHash(0x6662c13a7c344b8f8c0c3d766a8b6ca60a12a25c96208453b9bb163d6c8ecb56) (src/WeirdLogs.t.sol:24)
      ├╴HashCastedToBytes16(0x6662c13a7c344b8f8c0c3d766a8b6ca6) (src/WeirdLogs.t.sol:25)
      ├╴HashCastedToBytes8(0x6662c13a7c344b8f) (src/WeirdLogs.t.sol:26)
      ├╴HashCastedToBytes4(0x6662c13a) (src/WeirdLogs.t.sol:27)
      ├╴HashCastedToBytes3(0x6662c1) (src/WeirdLogs.t.sol:28)
      ├╴HashCastedToBytes2(«fb») (src/WeirdLogs.t.sol:29)
      ├╴HashCastedToBytes1(«f») (src/WeirdLogs.t.sol:30)
      └╴error Revert 0x4e487b710000000000000000000000000000000000000000000000000000000000000001 <source not found>

The last two logs (HashCastedToBytes2 and HashCastedToBytes1) print values «fb» and «f». This is not the correct result.

I've created the same example in Foundry and this is the result of running forge test -vvv:

Running 1 test for test/WeirdLogs.t.sol:WeirdLogsTest
[FAIL. Reason: Assertion violated] test_always_false() (gas: 10386)
Traces:
  [10386] WeirdLogsTest::test_always_false() 
    ├─ emit OriginalHash(originalHash: 0x6662c13a7c344b8f8c0c3d766a8b6ca60a12a25c96208453b9bb163d6c8ecb56)
    ├─ emit HashCastedToBytes16(originalHash: 0x6662c13a7c344b8f8c0c3d766a8b6ca6)
    ├─ emit HashCastedToBytes8(originalHash: 0x6662c13a7c344b8f)
    ├─ emit HashCastedToBytes4(originalHash: 0x6662c13a)
    ├─ emit HashCastedToBytes3(originalHash: 0x6662c1)
    ├─ emit HashCastedToBytes2(originalHash: 0x6662)
    ├─ emit HashCastedToBytes1(orignalHash: 0x66)
    └─ ← "Assertion violated"

Test result: FAILED. 0 passed; 1 failed; finished in 242.42µs

Failing tests:
Encountered 1 failing test in src/WeirdLogs.sol:WeirdLogs
[FAIL. Reason: Assertion violated] test_always_false() (gas: 10386)

As you can see, the result for the last two logs is correct: 0x6662 and 0x66, respectively.

As mentioned in #crytic/echidna/issues/860 this might already be fixed in the EF HEVM repository.

Thanks!

Since #860 in the Echidna repository has been closed due to the migration to the ethereum/hevm repo, this issue can be closed.