From 1a0a63003dd2b38a2d5d533b0b6f50366c258803 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 20 Apr 2023 17:28:33 +0200 Subject: [PATCH] EIP170 (#1000) --- evm/src/cpu/kernel/asm/core/create.asm | 5 ++++- evm/src/cpu/kernel/asm/core/process_txn.asm | 10 ++++++++++ evm/src/cpu/kernel/constants/mod.rs | 10 ++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/core/create.asm b/evm/src/cpu/kernel/asm/core/create.asm index 573d1348..6045454b 100644 --- a/evm/src/cpu/kernel/asm/core/create.asm +++ b/evm/src/cpu/kernel/asm/core/create.asm @@ -130,7 +130,6 @@ after_constructor: // stack: new_ctx, leftover_gas, success, address, kexit_info POP - // TODO: EIP-170: Contract code size limit. // TODO: EIP-3541: Reject new contract code starting with the 0xEF byte // TODO: Skip blocks below if success is false. @@ -138,6 +137,10 @@ after_constructor: SWAP3 // stack: kexit_info, success, address, leftover_gas %returndatasize // Size of the code. + // stack: code_size, kexit_info, success, address, leftover_gas + DUP1 %gt_const(@MAX_CODE_SIZE) + %jumpi(fault_exception) + // stack: code_size, kexit_info, success, address, leftover_gas %mul_const(@GAS_CODEDEPOSIT) %charge_gas SWAP3 diff --git a/evm/src/cpu/kernel/asm/core/process_txn.asm b/evm/src/cpu/kernel/asm/core/process_txn.asm index 4f72fe94..6c466749 100644 --- a/evm/src/cpu/kernel/asm/core/process_txn.asm +++ b/evm/src/cpu/kernel/asm/core/process_txn.asm @@ -145,6 +145,16 @@ global process_contract_creation_txn_after_constructor: // stack: success, leftover_gas, new_ctx, address, retdest POP // TODO: Success will go into the receipt when we support that. // stack: leftover_gas, new_ctx, address, retdest + %returndatasize // Size of the code. + // stack: code_size, leftover_gas, new_ctx, address, retdest + DUP1 %gt_const(@MAX_CODE_SIZE) %jumpi(panic) // TODO: need to revert changes here. + // stack: code_size, leftover_gas, new_ctx, address, retdest + %mul_const(@GAS_CODEDEPOSIT) SWAP1 + // stack: leftover_gas, codedeposit_cost, new_ctx, address, retdest + DUP2 DUP2 LT %jumpi(panic) // TODO: need to revert changes here. + // stack: leftover_gas, codedeposit_cost, new_ctx, address, retdest + SUB + // stack: leftover_gas, new_ctx, address, retdest %pay_coinbase_and_refund_sender // TODO: Delete accounts in self-destruct list and empty touched addresses. // stack: new_ctx, address, retdest diff --git a/evm/src/cpu/kernel/constants/mod.rs b/evm/src/cpu/kernel/constants/mod.rs index 75a96403..443e06d7 100644 --- a/evm/src/cpu/kernel/constants/mod.rs +++ b/evm/src/cpu/kernel/constants/mod.rs @@ -40,6 +40,10 @@ pub fn evm_constants() -> HashMap { c.insert(name.into(), U256::from(value)); } + for (name, value) in CODE_SIZE_LIMIT { + c.insert(name.into(), U256::from(value)); + } + for segment in Segment::all() { c.insert(segment.var_name().into(), (segment as u32).into()); } @@ -227,3 +231,9 @@ const PRECOMPILES_GAS: [(&str, u16); 13] = [ ("SNARKV_DYNAMIC_GAS", 34_000), ("BLAKE2_F_DYNAMIC_GAS", 1), ]; + +const CODE_SIZE_LIMIT: [(&str, u64); 3] = [ + ("MAX_CODE_SIZE", 0x6000), + ("MAX_INITCODE_SIZE", 0xc000), + ("INITCODE_WORD_COST", 2), +];