Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
add OOGCall overflow test
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamWuGit committed Aug 15, 2023
1 parent 9fa7d9e commit 1db3850
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
common_gadget::{CommonCallGadget, CommonErrorGadget},
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::{IsZeroGadget, LtGadget},
memory_gadget::MemoryAddressGadget,
memory_gadget::MemoryExpandedAddressGadget,
CachedRegion, Cell,
},
},
Expand All @@ -31,7 +31,7 @@ pub(crate) struct ErrorOOGCallGadget<F> {
is_staticcall: IsZeroGadget<F>,
tx_id: Cell<F>,
is_static: Cell<F>,
call: CommonCallGadget<F, MemoryAddressGadget<F>, false>,
call: CommonCallGadget<F, MemoryExpandedAddressGadget<F>, false>,
is_warm: Cell<F>,
insufficient_gas: LtGadget<F, N_BYTES_GAS>,
common_error_gadget: CommonErrorGadget<F>,
Expand All @@ -55,7 +55,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGCallGadget<F> {
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
let is_static = cb.call_context(None, CallContextFieldTag::IsStatic);

let call_gadget = CommonCallGadget::construct(
let call_gadget: CommonCallGadget<F, MemoryExpandedAddressGadget<F>, false> =
CommonCallGadget::construct(
cb,
is_call.expr(),
is_callcode.expr(),
Expand Down Expand Up @@ -270,11 +271,25 @@ mod test {

fn caller(opcode: OpcodeId, stack: Stack) -> Account {
let bytecode = call_bytecode(opcode, Address::repeat_byte(0xff), stack);
Account::mock_100_ether(bytecode)

Account {
address: Address::repeat_byte(0xfe),
balance: Word::from(10).pow(20.into()),
code: bytecode.code().into(),
..Default::default()
}
}

fn callee(code: Bytecode) -> Account {
Account::mock_code_balance(code)
let code = code.code();
let is_empty = code.is_empty();
Account {
address: Address::repeat_byte(0xff),
code: code.into(),
nonce: if is_empty { 0 } else { 1 }.into(),
balance: if is_empty { 0 } else { 0xdeadbeefu64 }.into(),
..Default::default()
}
}

fn test_oog(caller: &Account, callee: &Account, is_root: bool) {
Expand All @@ -285,8 +300,16 @@ mod test {
accs[0]
.address(address!("0x000000000000000000000000000000000000cafe"))
.balance(Word::from(10u64.pow(19)));
accs[1].account(caller);
accs[2].account(callee);
accs[1]
.address(caller.address)
.code(caller.code.clone())
.nonce(caller.nonce.as_u64())
.balance(caller.balance);
accs[2]
.address(callee.address)
.code(callee.code.clone())
.nonce(callee.nonce.as_u64())
.balance(callee.balance);
},
|mut txs, accs| {
txs[0]
Expand Down Expand Up @@ -368,4 +391,46 @@ mod test {
});
test_oog(&caller(OpcodeId::CALL, stack), &callee, true);
}

#[test]
fn test_oog_call_max_expanded_address() {
// 0xffffffff1 + 0xffffffff0 = 0x1fffffffe1
// > MAX_EXPANDED_MEMORY_ADDRESS (0x1fffffffe0)
let stack = Stack {
gas: Word::MAX,
cd_offset: 0xffffffff1,
cd_length: 0xffffffff0,
rd_offset: 0xffffffff1,
rd_length: 0xffffffff0,
..Default::default()
};
let callee = callee(bytecode! {
PUSH32(Word::from(0))
PUSH32(Word::from(0))
STOP
});
for opcode in TEST_CALL_OPCODES {
test_oog(&caller(*opcode, stack), &callee, true);
}
}

#[test]
fn test_oog_call_max_u64_address() {
let stack = Stack {
gas: Word::MAX,
cd_offset: u64::MAX,
cd_length: u64::MAX,
rd_offset: u64::MAX,
rd_length: u64::MAX,
..Default::default()
};
let callee = callee(bytecode! {
PUSH32(Word::from(0))
PUSH32(Word::from(0))
STOP
});
for opcode in TEST_CALL_OPCODES {
test_oog(&caller(*opcode, stack), &callee, true);
}
}
}

0 comments on commit 1db3850

Please sign in to comment.