Code Monkey home page Code Monkey logo

Comments (2)

d-xo avatar d-xo commented on September 15, 2024

Thanks for the bug report!

So the counterexamples produced by your input contain a specific storage prestate, if you just try to execute them without first setting up the correct storage prestate, you won't trigger a counterexample.

Setting up a storage prestate is currently pretty clunky (should be a lot easier once #227 is merged), but I could do it as follows using your testing harness:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

import {Test} from "forge-std/Test.sol";

interface IToken {
    function mint(address, uint256) external;
    function transfer(address, uint256) external;
    function balanceOf(address) external view returns (uint256);
}

/// @author philogy <https://github.com/philogy>
contract METH_WETHVerification is Test {
    function testMain(address _addr1, address _addr2, uint256 _amount1, uint256 _amount2) public {
        require(_amount1 >= _amount2);
        IToken token;
        bytes memory tokenCode =
            hex"606b8060093d393df3583560e01c8063a9059cbb1461002a57806370a0823114610047576340c10f19146100515760006000fd5b335460243581811161005e57600435805482909303335591019055005b600435545952593df35b6004358054602435019055005b63f4d678b83d526004601cfd";
        assembly {
            token := create(0, add(tokenCode, 0x20), mload(tokenCode))
        }
        emit log_named_address("this: ", address(this));

        // -- prepare storage prestate --
        vm.store(address(token), bytes32(uint(0x0)), 0xc41f9400030ace5d0babe3b93bd6041fbbd4060900ffe7ff1b78d6f4fdd74149);
        vm.store(address(token), bytes32(uint(0x4000000000)), 0xb20293c000018103400000000000000000000000000000000000000000000004);

        token.mint(_addr1, _amount1);
        vm.prank(_addr1);
        token.transfer(_addr2, _amount2);
        uint256 bal1 = token.balanceOf(_addr1);
        uint256 bal2 = token.balanceOf(_addr2);
        _assertNoOverflow(bal1, bal2);
        assert(bal1 + bal2 == _amount1);
    }

    function _assertNoOverflow(uint256 x, uint256 y) internal {
        assert(type(uint256).max - x >= y);
    }

    // -- verify that the hevm generated cex is valid
    function testCex() public {
        (bool res, bytes memory data) = address(this).call(hex"7e6f0d300000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000098984142001000b000042886cd1cfbe04523f9ffff02000200072c1f4308caec1aa0dbbe90200000000000000000000000000000000000000000000000000000");
        assertFalse(res);
        assertEq0(abi.encodeWithSignature("Panic(uint256)", 0x1), data);
    }
}

Note the calls to vm.prank that I inserted into testMain. I had to modify the address given in the hevm counterexample, I believe that this is due to differences in the address that METH_WETHVerification is instantiated at between foundry and hevm. I couldn't find the right set of cli flags to either to get this to match.

If you want to get a counterexample that doesn't require setting up a specific storage prestate, you can run hevm with the flag --storage-model InitialS, and it will assume that storage is empty at the start of execution (as opposed to the default where storage is held to be completely symbolic at the start of execution). This gives me back valid counterexamples that depend on calldata only.

None of the generated counterexamples trigger the second assert (regarding the sum of the two balances), if you have an example of a valid input that does trigger this assertion that is not discovered by hevm I would be very happy to know about it.

from hevm.

Philogy avatar Philogy commented on September 15, 2024

Thanks for the explanation, didn't know about / understand storage presets. --storage-model InitialS works like a charm, surprised it's not the default, although it makes sense that the storage would be symbolic as that's required to find certain vulnerabilities.

Not sure what you mean concerning the issue/difference between the forge and hevm addresses? Do you mean the address that's shown in the counterexample?

from hevm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.