From 09ef683303ec7be5510aafc185029610a47cc0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Tue, 20 Aug 2024 15:50:49 +0100 Subject: [PATCH] RISC-V: Make in-memory backend comparable --- src/riscv/lib/src/machine_state.rs | 5 ++-- src/riscv/lib/src/ocaml_api.rs | 2 +- src/riscv/lib/src/pvm/node_pvm.rs | 2 +- src/riscv/lib/src/state_backend.rs | 3 +- .../lib/src/state_backend/memory_backend.rs | 30 +++++++++++++++++++ src/riscv/lib/tests/test_pvm_storage.rs | 2 +- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/riscv/lib/src/machine_state.rs b/src/riscv/lib/src/machine_state.rs index 8ee16d6a4f03..56a9eed292a3 100644 --- a/src/riscv/lib/src/machine_state.rs +++ b/src/riscv/lib/src/machine_state.rs @@ -867,7 +867,6 @@ mod tests { instruction::{CIBTypeArgs, ITypeArgs, Instr, SBTypeArgs}, parse_block, }, - state_backend::tests::read_backend, traps::{EnvironException, Exception, Interrupt, TrapContext}, }; use crate::{bits::u64, machine_state::bus::main_memory::M1K}; @@ -1252,7 +1251,7 @@ mod tests { // The two backends should have the same state. assert_eq!(result, alt_result); - assert_eq!(read_backend(&backend), read_backend(&alt_backend)); + assert_eq!(backend, alt_backend); }); // Test that the machine state does not behave differently when potential ephermeral state is @@ -1460,6 +1459,6 @@ mod tests { // Both backends should have transitioned to the same state. assert_eq!(result, 1); assert_eq!(result, alt_result); - assert_eq!(read_backend(&backend), read_backend(&alt_backend)); + assert_eq!(backend, alt_backend); }); } diff --git a/src/riscv/lib/src/ocaml_api.rs b/src/riscv/lib/src/ocaml_api.rs index e9b99e943223..643530e2a1cb 100644 --- a/src/riscv/lib/src/ocaml_api.rs +++ b/src/riscv/lib/src/ocaml_api.rs @@ -72,7 +72,7 @@ pub fn octez_riscv_storage_id_equal(id1: Pointer, id2: Pointer) -> bool #[ocaml::func] #[ocaml::sig("state -> state -> bool")] pub fn octez_riscv_storage_state_equal(state1: Pointer, state2: Pointer) -> bool { - state1.as_ref().0.to_bytes() == state2.as_ref().0.to_bytes() + state1.as_ref().0 == state2.as_ref().0 } #[ocaml::func] diff --git a/src/riscv/lib/src/pvm/node_pvm.rs b/src/riscv/lib/src/pvm/node_pvm.rs index 1c973b683b6b..172077557ed6 100644 --- a/src/riscv/lib/src/pvm/node_pvm.rs +++ b/src/riscv/lib/src/pvm/node_pvm.rs @@ -59,7 +59,7 @@ pub enum PvmError { SerializationError(String), } -#[derive(Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct NodePvm { backend: InMemoryBackend, } diff --git a/src/riscv/lib/src/state_backend.rs b/src/riscv/lib/src/state_backend.rs index 0e3cbb2a4c35..330dba9b8447 100644 --- a/src/riscv/lib/src/state_backend.rs +++ b/src/riscv/lib/src/state_backend.rs @@ -211,6 +211,7 @@ pub trait Backend: BackendManagement + Sized { pub mod test_helpers { use super::{Backend, Layout}; + use std::fmt; /// Generate a test against all test backends. #[macro_export] @@ -230,7 +231,7 @@ pub mod test_helpers { /// This lets you construct backends for any layout. pub trait TestBackendFactory { - type Backend: Backend + Clone; + type Backend: Backend + Clone + fmt::Debug + Eq; /// Construct a backend for the given layout `L`. fn new() -> Self::Backend; diff --git a/src/riscv/lib/src/state_backend/memory_backend.rs b/src/riscv/lib/src/state_backend/memory_backend.rs index 5e3e4f6ac0ce..e8169e589b71 100644 --- a/src/riscv/lib/src/state_backend/memory_backend.rs +++ b/src/riscv/lib/src/state_backend/memory_backend.rs @@ -70,6 +70,36 @@ impl Clone for InMemoryBackend { } } +impl std::fmt::Debug for InMemoryBackend { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("InMemoryBackend") + .field("backing_storage", &self.backing_storage) + .field("layout", &self.layout) + .field("_pd", &self._pd) + .finish() + } +} + +impl PartialEq for InMemoryBackend { + fn eq(&self, other: &Self) -> bool { + if self.backing_storage == other.backing_storage { + return true; + } + + if self.layout != other.layout { + return false; + } + + let slice = unsafe { std::slice::from_raw_parts(self.backing_storage, self.layout.size()) }; + let other_slice = + unsafe { std::slice::from_raw_parts(other.backing_storage, other.layout.size()) }; + + slice.eq(other_slice) + } +} + +impl Eq for InMemoryBackend {} + impl backend::BackendManagement for InMemoryBackend { type Manager<'backend> = SliceManager<'backend>; diff --git a/src/riscv/lib/tests/test_pvm_storage.rs b/src/riscv/lib/tests/test_pvm_storage.rs index db5a0c262b3c..b57e8a9566ad 100644 --- a/src/riscv/lib/tests/test_pvm_storage.rs +++ b/src/riscv/lib/tests/test_pvm_storage.rs @@ -82,7 +82,7 @@ fn test_pvm_storage() { let mut repo = PvmStorage::load(tmp_dir.path()).unwrap(); let id = repo.commit(&empty).unwrap(); let checked_out_empty = repo.checkout(&id).unwrap(); - assert_eq!(empty.to_bytes(), checked_out_empty.to_bytes()); + assert_eq!(empty, checked_out_empty); let id2 = repo.commit(&empty).unwrap(); assert_eq!(id, id2); repo.close() -- GitLab