0

I'm currently completing the Ethernaut challenges, one of them is to deploy a small contract (runtime bytecode is 10 bytes long).

I'm running the tests on my local machine, and using Solidity to deploy the contract.

I'm using the CREATE opcode to deploy the bytecode, which I have explicitly defined in the contract. The results are the same for CREATE2.

When I use EXTCODESIZE to get back the contract code size for my deployed contract, it is returning 32, when the runtime bytecode size is for sure 10 (with initialization bytecode the size is 22).

My code:

    function Deploy() public returns(uint) {
        bytes memory code = "\x60\x0a\x60\x0c\x60\x00\x39\x60\x20\x60\x00\xf3\x60\x42\x60\x80\x52\x60\x20\x60\x80\xf3";

        address addr;
        uint size;
        assembly {
          addr := create(0, add(code, 0x20), mload(code))
          size := extcodesize(addr)
          if iszero(extcodesize(addr)) {
            revert(0, 0)
          }
        }
        return size;
    }

Why is it returning 32? I know this is the min word size but I am not padding the code input with zeros. However, the returned result (via EXTCODECOPY) is padded with zeros.

1 Answer 1

4

It is returning 32 because your deployement bytecode explicitely return 32 bytes.

Breaking it down :

\x60\x0a\x60\x0c\x60\x00\x39\x60\x20\x60\x00\xf3

  • PUSH 0x0a : length to copy (runtime bytecode is 10 bytes long)
  • PUSH 0x0c : source offset (runtime bytecode starts at the 12th byte)
  • PUSH 0x00 : destination offset (copy at memory address 0x00 and up)
  • CODECOPY : perform the actual copy
  • PUSH 0x20 : length to return <-- Here is your mistake
  • PUSH 0x00 : source address of return
  • RETURN : return 0x20 bytes starting at address 0x00

You just copied 0x0a bytes, but you are returning 0x20. Simply change the faulty PUSH 0x20 (0x60 0x20) of the deployement bytecode to a PUSH 0x0a (0x60 0x0a)and you will see the expected contract size of 10.

I hope that answers your question.

1
  • Yes that solved it, my bad, thank you !
    – riordant
    Feb 16, 2022 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.