Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a debugger tool to check Kernel (user code) execution against StructLogs #697

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::generation::{state::State, GenerationInputs};
use crate::keccak_sponge::columns::KECCAK_WIDTH_BYTES;
use crate::keccak_sponge::keccak_sponge_stark::KeccakSpongeOp;
use crate::memory::segments::Segment;
use crate::structlog::zerostructlog::ZeroStructLog;
use crate::structlog::OptionalZeroStructLogs;
use crate::util::h2u;
use crate::witness::errors::ProgramError;
use crate::witness::memory::{
Expand Down Expand Up @@ -67,7 +67,7 @@ pub(crate) struct Interpreter<F: RichField> {
/// Log of the maximal number of CPU cycles in one segment execution.
max_cpu_len_log: Option<usize>,
/// Optional logs for transactions code.
pub(crate) struct_logs: Option<Vec<Option<Vec<ZeroStructLog>>>>,
pub(crate) struct_logs: Option<Vec<OptionalZeroStructLogs>>,
/// Counter within a transaction.
pub(crate) struct_log_debugger_info: StructLogDebuggerInfo,
}
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<F: RichField> Interpreter<F> {
initial_stack: Vec<U256>,
inputs: &GenerationInputs<F>,
max_cpu_len_log: Option<usize>,
struct_logs: &Option<&[Option<Vec<ZeroStructLog>>]>,
struct_logs: Option<Vec<OptionalZeroStructLogs>>,
) -> Self {
debug_inputs(inputs);

Expand All @@ -190,7 +190,7 @@ impl<F: RichField> Interpreter<F> {
initial_offset: usize,
initial_stack: Vec<U256>,
max_cpu_len_log: Option<usize>,
struct_logs: &Option<&[Option<Vec<ZeroStructLog>>]>,
struct_logs: Option<Vec<OptionalZeroStructLogs>>,
) -> Self {
let mut interpreter = Self {
generation_state: GenerationState::new(&GenerationInputs::default(), &KERNEL.code)
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<F: RichField> Interpreter<F> {
halt_offset: usize,
halt_context: usize,
max_cpu_len_log: Option<usize>,
struct_logs: Option<Vec<Option<Vec<ZeroStructLog>>>>,
struct_logs: Option<Vec<OptionalZeroStructLogs>>,
) -> Self {
Self {
generation_state: state.soft_clone(),
Expand Down Expand Up @@ -591,14 +591,14 @@ impl<F: RichField> State<F> for Interpreter<F> {
}

// Check opcode.
let cur_txn_struct_logs = txn_struct_logs[counter].clone();
let struct_op = cur_txn_struct_logs.op;
let cur_txn_struct_logs = &txn_struct_logs[counter];
let struct_op = cur_txn_struct_logs.op.as_str();
let op_string_res = get_mnemonic(opcode);
match op_string_res {
Ok(cur_op_str) => {
let cur_op = cur_op_str.to_string();
if struct_op != cur_op {
log::warn!("Wrong opcode: expected {}, got {}.", struct_op, cur_op);
log::warn!("Wrong opcode: expected {struct_op}, got {cur_op}.");
return Err(ProgramError::StructLogDebuggerError);
}
}
Expand All @@ -619,14 +619,13 @@ impl<F: RichField> State<F> for Interpreter<F> {
}

// Check stack.
if let Some(txn_stack) = cur_txn_struct_logs.stack {
if let Some(txn_stack) = &cur_txn_struct_logs.stack {
let cur_stack = self.get_full_stack();
let txn_stack = txn_stack
.into_iter()
.map(|s| U256(*s.as_limbs()))
.collect::<Vec<_>>();

if txn_stack != cur_stack {
if !cur_stack
.iter()
.copied()
.eq(txn_stack.iter().map(|s| U256(*s.as_limbs())))
{
log::warn!(
"Wrong stack: expected {:?} but got {:?}.",
txn_stack,
Expand Down Expand Up @@ -657,8 +656,7 @@ impl<F: RichField> State<F> for Interpreter<F> {
if let Some(txn_struct_logs) = &struct_logs[txn_idx - 1] {
// If the transaction errors, we simply log a warning, since struct logs do not
// actually return an error in that case.
let cur_txn_struct_logs =
txn_struct_logs[self.struct_log_debugger_info.counter].clone();
let cur_txn_struct_logs = &txn_struct_logs[self.struct_log_debugger_info.counter];

if res.is_err() {
log::warn!("Kernel execution errored with: {:?}.", res);
Expand Down Expand Up @@ -1177,7 +1175,7 @@ mod tests {
0x60, 0xff, 0x60, 0x0, 0x52, 0x60, 0, 0x51, 0x60, 0x1, 0x51, 0x60, 0x42, 0x60, 0x27,
0x53,
];
let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, None);

interpreter.set_code(1, code.to_vec());

Expand Down
8 changes: 4 additions & 4 deletions evm_arithmetization/src/cpu/kernel/tests/account_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn test_extcodesize() -> Result<()> {
let code = random_code();
let account = test_account(&code);

let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, None);
let address: Address = thread_rng().gen();
// Prepare the interpreter by inserting the account in the state trie.
prepare_interpreter(&mut interpreter, address, &account)?;
Expand Down Expand Up @@ -305,7 +305,7 @@ fn test_extcodecopy() -> Result<()> {
let code = random_code();
let account = test_account(&code);

let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, None);
let address: Address = thread_rng().gen();
// Prepare the interpreter by inserting the account in the state trie.
prepare_interpreter(&mut interpreter, address, &account)?;
Expand Down Expand Up @@ -489,7 +489,7 @@ fn sstore() -> Result<()> {
};

let initial_stack = vec![];
let mut interpreter: Interpreter<F> = Interpreter::new(0, initial_stack, None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, initial_stack, None, None);

// Pre-initialize the accessed addresses list.
let init_accessed_addresses = KERNEL.global_labels["init_access_lists"];
Expand Down Expand Up @@ -583,7 +583,7 @@ fn sload() -> Result<()> {
};

let initial_stack = vec![];
let mut interpreter: Interpreter<F> = Interpreter::new(0, initial_stack, None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, initial_stack, None, None);

// Pre-initialize the accessed addresses list.
let init_accessed_addresses = KERNEL.global_labels["init_access_lists"];
Expand Down
18 changes: 4 additions & 14 deletions evm_arithmetization/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,8 @@ fn test_add11_yml() {

let initial_stack = vec![];
let initial_offset = KERNEL.global_labels["init"];
let mut interpreter: Interpreter<F> = Interpreter::new_with_generation_inputs(
initial_offset,
initial_stack,
&inputs,
None,
&None,
);
let mut interpreter: Interpreter<F> =
Interpreter::new_with_generation_inputs(initial_offset, initial_stack, &inputs, None, None);

interpreter.set_is_kernel(true);
interpreter.run().expect("Proving add11 failed.");
Expand Down Expand Up @@ -379,13 +374,8 @@ fn test_add11_yml_with_exception() {

let initial_stack = vec![];
let initial_offset = KERNEL.global_labels["init"];
let mut interpreter: Interpreter<F> = Interpreter::new_with_generation_inputs(
initial_offset,
initial_stack,
&inputs,
None,
&None,
);
let mut interpreter: Interpreter<F> =
Interpreter::new_with_generation_inputs(initial_offset, initial_stack, &inputs, None, None);

interpreter.set_is_kernel(true);
interpreter
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/tests/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_balance() -> Result<()> {
let balance = U256(rng.gen());
let account = test_account(balance);

let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(0, vec![], None, None);
let address: Address = rng.gen();
// Prepare the interpreter by inserting the account in the state trie.
prepare_interpreter(&mut interpreter, address, &account)?;
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/tests/bignum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn run_test(fn_label: &str, memory: Vec<U256>, stack: Vec<U256>) -> Result<(Vec<
initial_stack.push(retdest);
initial_stack.reverse();

let mut interpreter: Interpreter<F> = Interpreter::new(fn_label, initial_stack, None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(fn_label, initial_stack, None, None);
interpreter.set_current_general_memory(memory);
interpreter.run()?;

Expand Down
4 changes: 2 additions & 2 deletions evm_arithmetization/src/cpu/kernel/tests/blobhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_valid_blobhash() -> Result<()> {
let index = 3;
let target_hash = versioned_hashes[index];

let mut interpreter: Interpreter<F> = Interpreter::new(blobhash_label, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(blobhash_label, vec![], None, None);
interpreter
.generation_state
.memory
Expand Down Expand Up @@ -60,7 +60,7 @@ fn test_invalid_blobhash() -> Result<()> {
let index = 7;
let target_hash = U256::zero(); // out of bound indexing yields 0.

let mut interpreter: Interpreter<F> = Interpreter::new(blobhash_label, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(blobhash_label, vec![], None, None);
interpreter
.generation_state
.memory
Expand Down
8 changes: 4 additions & 4 deletions evm_arithmetization/src/cpu/kernel/tests/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_correct_block_hash() -> Result<()> {
let hashes: Vec<U256> = vec![U256::from_big_endian(&thread_rng().gen::<H256>().0); 257];

let mut interpreter: Interpreter<F> =
Interpreter::new(blockhash_label, initial_stack, None, &None);
Interpreter::new(blockhash_label, initial_stack, None, None);
interpreter.set_memory_segment(Segment::BlockHashes, hashes[0..256].to_vec());
interpreter.set_global_metadata_field(GlobalMetadata::BlockCurrentHash, hashes[256]);
interpreter.set_global_metadata_field(GlobalMetadata::BlockNumber, 256.into());
Expand Down Expand Up @@ -51,7 +51,7 @@ fn test_big_index_block_hash() -> Result<()> {
let hashes: Vec<U256> = vec![U256::from_big_endian(&thread_rng().gen::<H256>().0); 257];

let mut interpreter: Interpreter<F> =
Interpreter::new(blockhash_label, initial_stack, None, &None);
Interpreter::new(blockhash_label, initial_stack, None, None);
interpreter.set_memory_segment(Segment::BlockHashes, hashes[0..256].to_vec());
interpreter.set_global_metadata_field(GlobalMetadata::BlockCurrentHash, hashes[256]);
interpreter.set_global_metadata_field(GlobalMetadata::BlockNumber, cur_block_number.into());
Expand Down Expand Up @@ -82,7 +82,7 @@ fn test_small_index_block_hash() -> Result<()> {
let hashes: Vec<U256> = vec![U256::from_big_endian(&thread_rng().gen::<H256>().0); 257];

let mut interpreter: Interpreter<F> =
Interpreter::new(blockhash_label, initial_stack, None, &None);
Interpreter::new(blockhash_label, initial_stack, None, None);
interpreter.set_memory_segment(Segment::BlockHashes, hashes[0..256].to_vec());
interpreter.set_global_metadata_field(GlobalMetadata::BlockCurrentHash, hashes[256]);
interpreter.set_global_metadata_field(GlobalMetadata::BlockNumber, cur_block_number.into());
Expand Down Expand Up @@ -111,7 +111,7 @@ fn test_block_hash_with_overflow() -> Result<()> {
let hashes: Vec<U256> = vec![U256::from_big_endian(&thread_rng().gen::<H256>().0); 257];

let mut interpreter: Interpreter<F> =
Interpreter::new(blockhash_label, initial_stack, None, &None);
Interpreter::new(blockhash_label, initial_stack, None, None);
interpreter.set_memory_segment(Segment::BlockHashes, hashes[0..256].to_vec());
interpreter.set_global_metadata_field(GlobalMetadata::BlockCurrentHash, hashes[256]);
interpreter.set_global_metadata_field(GlobalMetadata::BlockNumber, cur_block_number.into());
Expand Down
3 changes: 1 addition & 2 deletions evm_arithmetization/src/cpu/kernel/tests/bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ fn test_kzg_peval_precompile() -> Result<()> {
stack.reverse();

let verify_kzg_proof = KERNEL.global_labels["verify_kzg_proof"];
let mut interpreter: Interpreter<F> =
Interpreter::new(verify_kzg_proof, stack, None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(verify_kzg_proof, stack, None, None);
interpreter.halt_offsets = vec![
KERNEL.global_labels["store_kzg_verification"],
KERNEL.global_labels["fault_exception"],
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/tests/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn test_ecpairing_precompile_invalid_input() -> Result<()> {

for bytes in ECPAIRING_PRECOMPILE_INVALID_INPUTS.iter() {
let mut interpreter: Interpreter<F> =
Interpreter::new(pairing_label, stack.clone(), None, &None);
Interpreter::new(pairing_label, stack.clone(), None, None);
let preloaded_memory = vec![
U256::from_big_endian(&bytes[0..32]), // Px
U256::from_big_endian(&bytes[32..64]), // Py
Expand Down
10 changes: 5 additions & 5 deletions evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_init_access_lists() -> Result<()> {

// Check the initial state of the access list in the kernel.
let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, &None);
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, None);
interpreter.run()?;

assert!(interpreter.stack().is_empty());
Expand Down Expand Up @@ -64,7 +64,7 @@ fn test_list_iterator() -> Result<()> {
let init_label = KERNEL.global_labels["init_access_lists"];

let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, &None);
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, None);
interpreter.run()?;

// test the list iterator
Expand All @@ -90,7 +90,7 @@ fn test_insert_address() -> Result<()> {

// Test for address already in list.
let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, &None);
let mut interpreter = Interpreter::<F>::new(init_label, initial_stack, None, None);
interpreter.run()?;

let insert_accessed_addresses = KERNEL.global_labels["insert_accessed_addresses"];
Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_insert_accessed_addresses() -> Result<()> {

// Test for address already in list.
let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter = Interpreter::<F>::new(init_access_lists, initial_stack, None, &None);
let mut interpreter = Interpreter::<F>::new(init_access_lists, initial_stack, None, None);
interpreter.run()?;

let insert_accessed_addresses = KERNEL.global_labels["insert_accessed_addresses"];
Expand Down Expand Up @@ -214,7 +214,7 @@ fn test_insert_accessed_storage_keys() -> Result<()> {

// Test for address already in list.
let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter = Interpreter::<F>::new(init_access_lists, initial_stack, None, &None);
let mut interpreter = Interpreter::<F>::new(init_access_lists, initial_stack, None, None);
interpreter.run()?;

let insert_accessed_storage_keys = KERNEL.global_labels["insert_accessed_storage_keys"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_get_create_address() -> Result<()> {

let initial_stack = vec![retaddr, nonce, sender];
let mut interpreter: Interpreter<F> =
Interpreter::new(get_create_address, initial_stack, None, &None);
Interpreter::new(get_create_address, initial_stack, None, None);
interpreter.run()?;

assert_eq!(interpreter.stack(), &[expected_addr]);
Expand Down Expand Up @@ -108,7 +108,7 @@ fn test_get_create2_address() -> Result<()> {
{
let initial_stack = vec![retaddr, salt, U256::from(code_hash.0), sender];
let mut interpreter: Interpreter<F> =
Interpreter::new(get_create2_address, initial_stack, None, &None);
Interpreter::new(get_create2_address, initial_stack, None, None);
interpreter.run()?;

assert_eq!(interpreter.stack(), &[expected_addr]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ fn test_intrinsic_gas() -> Result<()> {
// Contract creation transaction.
let initial_stack = vec![0xdeadbeefu32.into()];
let mut interpreter: Interpreter<F> =
Interpreter::new(intrinsic_gas, initial_stack.clone(), None, &None);
Interpreter::new(intrinsic_gas, initial_stack.clone(), None, None);
interpreter.set_global_metadata_field(GlobalMetadata::ContractCreation, U256::one());
interpreter.run()?;
assert_eq!(interpreter.stack(), vec![(GAS_TX + GAS_TXCREATE).into()]);

// Message transaction.
let mut interpreter: Interpreter<F> =
Interpreter::new(intrinsic_gas, initial_stack, None, &None);
Interpreter::new(intrinsic_gas, initial_stack, None, None);
interpreter.set_txn_field(NormalizedTxnField::To, 123.into());
interpreter.run()?;
assert_eq!(interpreter.stack(), vec![GAS_TX.into()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn test_jumpdest_analysis() -> Result<()> {
.chain(std::iter::once(true)),
);

let mut interpreter: Interpreter<F> = Interpreter::new(jumpdest_analysis, vec![], None, &None);
let mut interpreter: Interpreter<F> = Interpreter::new(jumpdest_analysis, vec![], None, None);
let code_len = code.len();

interpreter.set_code(CONTEXT, code);
Expand Down Expand Up @@ -173,7 +173,7 @@ fn test_packed_verification() -> Result<()> {
U256::one(),
];
let mut interpreter: Interpreter<F> =
Interpreter::new(write_table_if_jumpdest, initial_stack.clone(), None, &None);
Interpreter::new(write_table_if_jumpdest, initial_stack.clone(), None, None);
interpreter.set_code(CONTEXT, code.clone());
interpreter.generation_state.jumpdest_table = Some(HashMap::from([(3, vec![1, 33])]));

Expand All @@ -186,7 +186,7 @@ fn test_packed_verification() -> Result<()> {
for i in 1..=32 {
code[i] += 1;
let mut interpreter: Interpreter<F> =
Interpreter::new(write_table_if_jumpdest, initial_stack.clone(), None, &None);
Interpreter::new(write_table_if_jumpdest, initial_stack.clone(), None, None);
interpreter.set_code(CONTEXT, code.clone());
interpreter.generation_state.jumpdest_table = Some(HashMap::from([(3, vec![1, 33])]));

Expand Down Expand Up @@ -236,7 +236,7 @@ fn test_verify_non_jumpdest() -> Result<()> {
for i in 8..code_len - 1 {
code[i] += 1;
let mut interpreter: Interpreter<F> =
Interpreter::new(verify_non_jumpdest, vec![], None, &None);
Interpreter::new(verify_non_jumpdest, vec![], None, None);
interpreter.generation_state.registers.context = CONTEXT;

interpreter.set_code(CONTEXT, code.clone());
Expand Down
Loading
Loading