foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Executing code from bytes array makes the debugger crash

Open peyha opened this issue 9 months ago • 0 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (d431f74 2024-04-27T00:16:52.929844000Z)

What command(s) is the bug in?

forge test --debug "_()" --match-contract PublicTest9

Operating System

macOS (Apple Silicon)

Describe the bug

Tried to use the debugger with the following code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract Challenge {
    bytes internal constant bytecode = (
        hex"5b608060806080608060806080608060806080"
        hex"60806080608060806080608060806080608060"
        hex"60806080608060806080608060806080608060"
    );
    uint256 internal constant bytecode_start = 0x145;

    /**
     * @dev Remove duplicates from an array of uint8
     */
    function dispelDuplicates(
        uint8[] calldata input
    ) public pure returns (uint8[] memory output) {
        function() internal pure bytecode_func;
        assembly {
            bytecode_func := bytecode_start
        }
        bytecode_func();

        bytes memory r = bytecode;
        assembly {
            output := r
        }
    }
}

turns out the debugger crashes once it gets to the line bytecode_func(), here is the corresponding log

The application panicked (crashed).
Message:  swap_remove index (is 241) should be < len (is 240)
Location: /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04/library/alloc/src/vec/mod.rs:1463

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                                ⋮ 9 frames hidden ⋮                               
  10: alloc::vec::Vec<T,A>::swap_remove::assert_failed::h5c3445a08183cfb1
      at <unknown source file>:<unknown line>
  11: foundry_debugger::tui::draw::<impl foundry_debugger::tui::context::DebuggerContext>::draw_src::h513d58bd179bc875
      at <unknown source file>:<unknown line>
  12: foundry_debugger::tui::Debugger::try_run_real::{{closure}}::h3a79640c3b313821
      at <unknown source file>:<unknown line>
  13: foundry_debugger::tui::Debugger::try_run::hd03d67fab6d66e5d
      at <unknown source file>:<unknown line>
  14: forge::cmd::test::TestArgs::execute_tests::{{closure}}::h517417722672d6c0
      at <unknown source file>:<unknown line>
  15: forge::cmd::test::TestArgs::run::{{closure}}::h8c22520d60eb572f
      at <unknown source file>:<unknown line>
  16: forge::main::hf3c5a2ed3e2f95d7
      at <unknown source file>:<unknown line>
  17: std::sys_common::backtrace::__rust_begin_short_backtrace::hbc8ec14722bb3757
      at <unknown source file>:<unknown line>
  18: _main<unknown>
      at <unknown source file>:<unknown line>

peyha avatar Apr 27 '24 12:04 peyha