Skip to content

Commit

Permalink
Fix EOF initcode handling before EOF is enabled
Browse files Browse the repository at this point in the history
The special rule of handling EOF initcode in CREATE/CREATE2 instructions
should not be activated before EOF is activated.
  • Loading branch information
chfast committed May 20, 2024
1 parent 7ad882c commit b449afc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/evmone/instructions_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,12 @@ Result create_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noex
msg.input_data = &state.memory[init_code_offset];
msg.input_size = init_code_size;

// EOF initcode is not allowed for legacy creation
if (is_eof_container({msg.input_data, msg.input_size}))
return {EVMC_SUCCESS, gas_left}; // "Light" failure.
if (state.rev >= EVMC_PRAGUE)
{
// EOF initcode is not allowed for legacy creation
if (is_eof_container({msg.input_data, msg.input_size}))
return {EVMC_SUCCESS, gas_left}; // "Light" failure.
}
}
msg.sender = state.msg->recipient;
msg.depth = state.msg->depth + 1;
Expand Down
49 changes: 47 additions & 2 deletions test/unittests/state_transition_eof_create_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST_F(state_transition, create_with_eof_initcode)
tx.gas_limit = block.gas_limit;
pre.get(tx.sender).balance = tx.gas_limit * tx.max_gas_price + tx.value + 1;

const bytecode init_container = eof_bytecode(ret(0, 1));
const bytecode init_container = eof_bytecode(OP_INVALID);
const auto factory_code =
mstore(0, push(init_container)) +
// init_container will be left-padded in memory to 32 bytes
Expand All @@ -46,14 +46,36 @@ TEST_F(state_transition, create_with_eof_initcode)
expect.post[*tx.to].storage[0x01_bytes32] = 0x01_bytes32;
}

TEST_F(state_transition, create_with_eof_initcode_cancun)
{
rev = EVMC_CANCUN;
block.gas_limit = 10'000'000;
tx.gas_limit = block.gas_limit;
pre.get(tx.sender).balance = tx.gas_limit * tx.max_gas_price + tx.value + 1;

const bytecode init_container = eof_bytecode(OP_INVALID);
const auto factory_code =
mstore(0, push(init_container)) +
// init_container will be left-padded in memory to 32 bytes
sstore(0, create().input(32 - init_container.size(), init_container.size())) + sstore(1, 1);

tx.to = To;

pre.insert(*tx.to, {.nonce = 1, .code = factory_code});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce + 1; // fails by EF execution, nonce bumped.
expect.post[*tx.to].storage[0x00_bytes32] = 0x00_bytes32;
expect.post[*tx.to].storage[0x01_bytes32] = 0x01_bytes32;
}

TEST_F(state_transition, create2_with_eof_initcode)
{
rev = EVMC_PRAGUE;
block.gas_limit = 10'000'000;
tx.gas_limit = block.gas_limit;
pre.get(tx.sender).balance = tx.gas_limit * tx.max_gas_price + tx.value + 1;

const bytecode init_container = eof_bytecode(ret(0, 1));
const bytecode init_container = eof_bytecode(OP_INVALID);
const auto factory_code =
mstore(0, push(init_container)) +
// init_container will be left-padded in memory to 32 bytes
Expand All @@ -69,6 +91,29 @@ TEST_F(state_transition, create2_with_eof_initcode)
expect.post[*tx.to].storage[0x01_bytes32] = 0x01_bytes32;
}

TEST_F(state_transition, create2_with_eof_initcode_cancun)
{
rev = EVMC_CANCUN;
block.gas_limit = 10'000'000;
tx.gas_limit = block.gas_limit;
pre.get(tx.sender).balance = tx.gas_limit * tx.max_gas_price + tx.value + 1;

const bytecode init_container = eof_bytecode(OP_INVALID);
const auto factory_code =
mstore(0, push(init_container)) +
// init_container will be left-padded in memory to 32 bytes
sstore(0, create2().input(32 - init_container.size(), init_container.size()).salt(Salt)) +
sstore(1, 1);

tx.to = To;

pre.insert(*tx.to, {.nonce = 1, .code = factory_code});

expect.post[*tx.to].nonce = pre.get(*tx.to).nonce + 1; // fails by EF execution, nonce bumped.
expect.post[*tx.to].storage[0x00_bytes32] = 0x00_bytes32;
expect.post[*tx.to].storage[0x01_bytes32] = 0x01_bytes32;
}

TEST_F(state_transition, create_tx_deploying_eof)
{
rev = EVMC_PRAGUE;
Expand Down

0 comments on commit b449afc

Please sign in to comment.