From 5ba59fff1d4658bbe050565598acc051363a9f56 Mon Sep 17 00:00:00 2001 From: "Chris T." Date: Tue, 13 Aug 2024 14:12:45 -0700 Subject: [PATCH] fix: memory checkpoints with syscalls (#1284) --- core/src/runtime/mod.rs | 11 ++++++---- core/src/runtime/syscall.rs | 8 ++++---- core/src/runtime/utils.rs | 40 ++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/core/src/runtime/mod.rs b/core/src/runtime/mod.rs index 17d6846607..08f47d475a 100644 --- a/core/src/runtime/mod.rs +++ b/core/src/runtime/mod.rs @@ -227,7 +227,7 @@ impl<'a> Runtime<'a> { } /// Get the current values of the registers. - pub fn registers(&self) -> [u32; 32] { + pub fn registers(&mut self) -> [u32; 32] { let mut registers = [0; 32]; for i in 0..32 { let addr = Register::from_u32(i as u32) as u32; @@ -235,13 +235,15 @@ impl<'a> Runtime<'a> { Some(record) => record.value, None => 0, }; + self.touched_memory.insert(addr); } registers } /// Get the current value of a register. - pub fn register(&self, register: Register) -> u32 { + pub fn register(&mut self, register: Register) -> u32 { let addr = register as u32; + self.touched_memory.insert(addr); match self.state.memory.get(&addr) { Some(record) => record.value, None => 0, @@ -249,7 +251,8 @@ impl<'a> Runtime<'a> { } /// Get the current value of a word. - pub fn word(&self, addr: u32) -> u32 { + pub fn word(&mut self, addr: u32) -> u32 { + self.touched_memory.insert(addr); match self.state.memory.get(&addr) { Some(record) => record.value, None => 0, @@ -257,7 +260,7 @@ impl<'a> Runtime<'a> { } /// Get the current value of a byte. - pub fn byte(&self, addr: u32) -> u8 { + pub fn byte(&mut self, addr: u32) -> u8 { let word = self.word(addr - addr % 4); (word >> ((addr % 4) * 8)) as u8 } diff --git a/core/src/runtime/syscall.rs b/core/src/runtime/syscall.rs index 7a6465ee30..35cff1bc3a 100644 --- a/core/src/runtime/syscall.rs +++ b/core/src/runtime/syscall.rs @@ -238,19 +238,19 @@ impl<'a, 'b> SyscallContext<'a, 'b> { /// Get the current value of a register, but doesn't use a memory record. /// This is generally unconstrained, so you must be careful using it. - pub fn register_unsafe(&self, register: Register) -> u32 { + pub fn register_unsafe(&mut self, register: Register) -> u32 { self.rt.register(register) } - pub fn byte_unsafe(&self, addr: u32) -> u8 { + pub fn byte_unsafe(&mut self, addr: u32) -> u8 { self.rt.byte(addr) } - pub fn word_unsafe(&self, addr: u32) -> u32 { + pub fn word_unsafe(&mut self, addr: u32) -> u32 { self.rt.word(addr) } - pub fn slice_unsafe(&self, addr: u32, len: usize) -> Vec { + pub fn slice_unsafe(&mut self, addr: u32, len: usize) -> Vec { let mut values = Vec::new(); for i in 0..len { values.push(self.rt.word(addr + i as u32 * 4)); diff --git a/core/src/runtime/utils.rs b/core/src/runtime/utils.rs index 41e132bccc..7c0ad541e7 100644 --- a/core/src/runtime/utils.rs +++ b/core/src/runtime/utils.rs @@ -1,7 +1,6 @@ use std::io::Write; use super::{Instruction, Runtime}; -use crate::runtime::Register; pub const fn align(addr: u32) -> u32 { addr - addr % 4 @@ -42,30 +41,31 @@ impl<'a> Runtime<'a> { // If RUST_LOG is set to "trace", then log the current state of the runtime every cycle. let width = 12; + let registers = self.registers(); log::trace!( "clk={} [pc=0x{:x?}] {: