From ab70bc536d3a849b1f331d84d34579afe9235bd2 Mon Sep 17 00:00:00 2001 From: Linda Guiga <101227802+LindaGuiga@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:34:41 -0500 Subject: [PATCH] Fix run_syscall in interpreter. (#1351) * Fix syscall and change sload test to catch the error * Update comment * Cleanup --- evm/src/cpu/kernel/interpreter.rs | 3 +-- evm/src/cpu/kernel/tests/account_code.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 6c5c1a87..37de2314 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -1047,7 +1047,7 @@ impl<'a> Interpreter<'a> { let new_program_counter = u256_to_usize(handler_addr).map_err(|_| anyhow!("The program counter is too large"))?; - let syscall_info = U256::from(self.generation_state.registers.program_counter + 1) + let syscall_info = U256::from(self.generation_state.registers.program_counter) + U256::from((self.generation_state.registers.is_kernel as usize) << 32) + (U256::from(self.generation_state.registers.gas_used) << 192); self.generation_state.registers.program_counter = new_program_counter; @@ -1057,7 +1057,6 @@ impl<'a> Interpreter<'a> { self.generation_state.registers.gas_used = 0; self.push(syscall_info); - self.run().map_err(|_| anyhow!("Syscall failed"))?; Ok(()) } diff --git a/evm/src/cpu/kernel/tests/account_code.rs b/evm/src/cpu/kernel/tests/account_code.rs index b7b6debb..919b9f2f 100644 --- a/evm/src/cpu/kernel/tests/account_code.rs +++ b/evm/src/cpu/kernel/tests/account_code.rs @@ -322,8 +322,10 @@ fn sload() -> Result<()> { let addr_nibbles = Nibbles::from_bytes_be(addr_hashed.as_bytes()).unwrap(); // This code is similar to the one in add11_yml's contract, but we pop the added value - // and carry out an SLOAD instead of an SSTORE. - let code = [0x60, 0x01, 0x60, 0x01, 0x01, 0x50, 0x60, 0x00, 0x54, 0x00]; + // and carry out an SLOAD instead of an SSTORE. We also add a PUSH at the end. + let code = [ + 0x60, 0x01, 0x60, 0x01, 0x01, 0x50, 0x60, 0x00, 0x54, 0x60, 0x03, 0x00, + ]; let code_hash = keccak(code); let account_before = AccountRlp { @@ -350,9 +352,13 @@ fn sload() -> Result<()> { prepare_interpreter_all_accounts(&mut interpreter, trie_inputs, addr, &code)?; interpreter.run()?; - // We check that no value was found. - let value = interpreter.pop(); - assert_eq!(value, 0.into()); + + // The SLOAD in the provided code should return 0, since + // the storage trie is empty. The last step in the code + // pushes the value 3. + assert_eq!(interpreter.stack(), vec![0x0.into(), 0x3.into()]); + interpreter.pop(); + interpreter.pop(); // Now, execute mpt_hash_state_trie. We check that the state trie has not changed. let mpt_hash_state_trie = KERNEL.global_labels["mpt_hash_state_trie"]; interpreter.generation_state.registers.program_counter = mpt_hash_state_trie;