From 2c93636919370d51b6f607677a58fcdfb8b02a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Mon, 24 Feb 2025 18:14:33 +0000 Subject: [PATCH] RISC-V: Group imports using "StdExternalCrate" --- src/riscv/.rustfmt.toml | 1 + src/riscv/jstz/bench/src/generate.rs | 5 +-- src/riscv/jstz/bench/src/main.rs | 5 +-- src/riscv/jstz/bench/src/results.rs | 8 +++-- src/riscv/jstz/kernel/src/main.rs | 1 + src/riscv/lib/src/cache_utils.rs | 3 +- src/riscv/lib/src/devicetree.rs | 3 +- src/riscv/lib/src/interpreter/atomics.rs | 3 +- src/riscv/lib/src/interpreter/float.rs | 12 ++++--- src/riscv/lib/src/interpreter/rv32a.rs | 9 +++-- src/riscv/lib/src/interpreter/rv32c.rs | 3 +- src/riscv/lib/src/interpreter/rv32i.rs | 9 ++--- src/riscv/lib/src/interpreter/rv32m.rs | 3 +- src/riscv/lib/src/interpreter/rv64a.rs | 9 +++-- src/riscv/lib/src/interpreter/rv64c.rs | 6 ++-- src/riscv/lib/src/interpreter/rv64d.rs | 14 ++++---- src/riscv/lib/src/interpreter/rv64dc.rs | 5 +-- src/riscv/lib/src/interpreter/rv64f.rs | 12 +++---- src/riscv/lib/src/interpreter/rv64i.rs | 3 +- src/riscv/lib/src/interpreter/rv64m.rs | 3 +- src/riscv/lib/src/interpreter/rv64priv.rs | 3 +- src/riscv/lib/src/jit.rs | 28 ++++++++------- src/riscv/lib/src/jit/builder.rs | 9 ++--- src/riscv/lib/src/jit/state_access.rs | 20 ++++++----- src/riscv/lib/src/kernel_loader.rs | 3 +- src/riscv/lib/src/machine_state.rs | 34 +++++++++++-------- .../src/machine_state/address_translation.rs | 3 +- .../address_translation/physical_address.rs | 3 +- .../machine_state/address_translation/pte.rs | 3 +- .../address_translation/translation_cache.rs | 3 +- .../address_translation/virtual_address.rs | 3 +- .../lib/src/machine_state/block_cache.rs | 8 +++-- .../lib/src/machine_state/csregisters.rs | 12 ++++--- .../src/machine_state/csregisters/values.rs | 11 +++--- .../lib/src/machine_state/instruction.rs | 8 +++-- .../instruction/tagged_instruction.rs | 8 +++-- .../lib/src/machine_state/main_memory.rs | 10 +++--- src/riscv/lib/src/machine_state/registers.rs | 11 +++--- src/riscv/lib/src/ocaml_api.rs | 16 +++++---- src/riscv/lib/src/parser.rs | 10 +++--- src/riscv/lib/src/parser/instruction.rs | 8 +++-- src/riscv/lib/src/program.rs | 6 ++-- src/riscv/lib/src/pvm/common.rs | 32 +++++++++-------- src/riscv/lib/src/pvm/linux.rs | 12 ++++--- src/riscv/lib/src/pvm/node_pvm.rs | 6 ++-- src/riscv/lib/src/pvm/reveals.rs | 3 +- src/riscv/lib/src/pvm/sbi.rs | 26 +++++++------- src/riscv/lib/src/state_backend/effects.rs | 3 +- src/riscv/lib/src/state_backend/hash.rs | 1 + src/riscv/lib/src/state_backend/layout.rs | 3 +- .../lib/src/state_backend/owned_backend.rs | 14 ++++---- .../lib/src/state_backend/proof_backend.rs | 22 +++++++----- .../src/state_backend/proof_backend/merkle.rs | 9 +++-- .../src/state_backend/proof_backend/proof.rs | 8 +++-- src/riscv/lib/src/state_backend/region.rs | 6 ++-- .../lib/src/state_backend/verify_backend.rs | 12 ++++--- src/riscv/lib/src/stepper.rs | 3 +- src/riscv/lib/src/stepper/pvm.rs | 10 +++--- src/riscv/lib/src/stepper/pvm/reveals.rs | 1 + src/riscv/lib/src/stepper/test.rs | 8 +++-- src/riscv/lib/src/storage.rs | 4 ++- src/riscv/lib/src/storage/binary.rs | 3 +- src/riscv/lib/src/storage/chunked_io.rs | 3 +- src/riscv/lib/src/traps.rs | 4 ++- src/riscv/lib/tests/common/mod.rs | 3 +- src/riscv/lib/tests/test_determinism.rs | 3 +- src/riscv/lib/tests/test_proofs.rs | 3 +- src/riscv/lib/tests/test_regression.rs | 3 +- src/riscv/lib/tests/test_suite_interpreter.rs | 3 +- src/riscv/lib/tests/test_suite_parser.rs | 3 +- src/riscv/sandbox/src/cli.rs | 3 +- src/riscv/sandbox/src/commands/bench.rs | 9 ++--- .../src/commands/bench/commands/compare.rs | 6 ++-- .../src/commands/bench/commands/parser.rs | 6 ++-- .../src/commands/bench/commands/run.rs | 30 ++++++++-------- src/riscv/sandbox/src/commands/bench/data.rs | 5 +-- src/riscv/sandbox/src/commands/bench/stats.rs | 8 +++-- src/riscv/sandbox/src/commands/debug.rs | 6 ++-- .../src/commands/debug/debugger_app.rs | 22 +++++++----- .../src/commands/debug/debugger_app/render.rs | 9 ++--- .../commands/debug/debugger_app/updates.rs | 6 ++-- .../sandbox/src/commands/debug/errors.rs | 6 ++-- src/riscv/sandbox/src/commands/gdb.rs | 16 +++++---- src/riscv/sandbox/src/commands/run.rs | 12 ++++--- src/riscv/sandbox/src/main.rs | 3 +- src/riscv/sandbox/src/table.rs | 11 +++--- src/riscv/sandbox/src/table/utils.rs | 10 +++--- src/riscv/tests/inline_asm/src/main.rs | 1 + 88 files changed, 432 insertions(+), 285 deletions(-) diff --git a/src/riscv/.rustfmt.toml b/src/riscv/.rustfmt.toml index 06656bc1df08..97cb6b98a41f 100644 --- a/src/riscv/.rustfmt.toml +++ b/src/riscv/.rustfmt.toml @@ -1,2 +1,3 @@ style_edition = "2024" required_version = "1.8.0" +group_imports = "StdExternalCrate" diff --git a/src/riscv/jstz/bench/src/generate.rs b/src/riscv/jstz/bench/src/generate.rs index 9f7f7cee5ac4..397376f9d736 100644 --- a/src/riscv/jstz/bench/src/generate.rs +++ b/src/riscv/jstz/bench/src/generate.rs @@ -2,14 +2,15 @@ // // SPDX-License-Identifier: MIT +use std::error::Error; +use std::path::Path; + use base64::{Engine, engine::general_purpose::URL_SAFE}; use http::{HeaderMap, Method, Uri}; use jstz_crypto::{keypair_from_passphrase, public_key::PublicKey, secret_key::SecretKey}; use jstz_proto::context::account::{Address, Nonce, ParsedCode}; use jstz_proto::operation::{Content, DeployFunction, Operation, RunFunction, SignedOperation}; use serde::{Serialize, Serializer}; -use std::error::Error; -use std::path::Path; use tezos_data_encoding::enc::BinWriter; use tezos_smart_rollup::inbox::ExternalMessageFrame; use tezos_smart_rollup::types::SmartRollupAddress; diff --git a/src/riscv/jstz/bench/src/main.rs b/src/riscv/jstz/bench/src/main.rs index 57d3ed7776c7..7bf1a5fb786c 100644 --- a/src/riscv/jstz/bench/src/main.rs +++ b/src/riscv/jstz/bench/src/main.rs @@ -2,11 +2,12 @@ // // SPDX-License-Identifier: MIT +use std::error::Error; +use std::path::Path; + use clap::{Parser, Subcommand}; use generate::{handle_generate, handle_generate_script}; use results::handle_results; -use std::error::Error; -use std::path::Path; mod generate; mod results; diff --git a/src/riscv/jstz/bench/src/results.rs b/src/riscv/jstz/bench/src/results.rs index 07ce12e579b3..0214b83f3b79 100644 --- a/src/riscv/jstz/bench/src/results.rs +++ b/src/riscv/jstz/bench/src/results.rs @@ -2,15 +2,17 @@ // // SPDX-License-Identifier: MIT -use crate::Result; -use regex::Regex; -use serde::Deserialize; use std::fs::read_to_string; use std::path::Path; use std::time::Duration; use std::{collections::HashSet, fmt}; + +use regex::Regex; +use serde::Deserialize; use tezos_smart_rollup::utils::inbox::file::{InboxFile, Message}; +use crate::Result; + // Three sets of messages: // 1. Deployment // 2. Minting & Transfers diff --git a/src/riscv/jstz/kernel/src/main.rs b/src/riscv/jstz/kernel/src/main.rs index fe7bb54b02e6..50e62d775b41 100644 --- a/src/riscv/jstz/kernel/src/main.rs +++ b/src/riscv/jstz/kernel/src/main.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT use std::sync::Once; + use tezos_crypto_rs::hash::ContractKt1Hash; use tezos_smart_rollup::prelude::Runtime; use tezos_smart_rollup::{entrypoint, storage::path::RefPath}; diff --git a/src/riscv/lib/src/cache_utils.rs b/src/riscv/lib/src/cache_utils.rs index 9d0731c678c1..a15c8aba0a48 100644 --- a/src/riscv/lib/src/cache_utils.rs +++ b/src/riscv/lib/src/cache_utils.rs @@ -3,6 +3,8 @@ // // SPDX-License-Identifier: MIT +use std::{convert::Infallible, marker::PhantomData}; + use crate::{ default::ConstDefault, machine_state::main_memory::Address, @@ -12,7 +14,6 @@ use crate::{ }, storage::{Hash, HashError}, }; -use std::{convert::Infallible, marker::PhantomData}; /// Integer to keep track of the fence counter #[derive( diff --git a/src/riscv/lib/src/devicetree.rs b/src/riscv/lib/src/devicetree.rs index b14e6343c107..c5f02656d980 100644 --- a/src/riscv/lib/src/devicetree.rs +++ b/src/riscv/lib/src/devicetree.rs @@ -6,9 +6,10 @@ //! - //! - -use crate::machine_state::main_memory; use vm_fdt::FdtWriter; +use crate::machine_state::main_memory; + /// Information about the initial ramdisk. pub struct InitialRamDisk { /// Start address of the initrd diff --git a/src/riscv/lib/src/interpreter/atomics.rs b/src/riscv/lib/src/interpreter/atomics.rs index e7cfece4eb48..5f82629a9196 100644 --- a/src/riscv/lib/src/interpreter/atomics.rs +++ b/src/riscv/lib/src/interpreter/atomics.rs @@ -4,12 +4,13 @@ //! Core logic for atomic instructions +use std::mem; + use crate::{ machine_state::{MachineCoreState, main_memory::MainMemoryLayout, registers::XRegister}, state_backend as backend, traps::Exception, }; -use std::mem; pub const SC_SUCCESS: u64 = 0; pub const SC_FAILURE: u64 = 1; diff --git a/src/riscv/lib/src/interpreter/float.rs b/src/riscv/lib/src/interpreter/float.rs index 25c68189a938..ad6014e97ee9 100644 --- a/src/riscv/lib/src/interpreter/float.rs +++ b/src/riscv/lib/src/interpreter/float.rs @@ -4,6 +4,13 @@ //! Core-logic implementation of F/D instructions. +use std::{ + fmt::{self, Display}, + ops::Neg, +}; + +use rustc_apfloat::{Float, FloatConvert, Round, Status, StatusAnd}; + use crate::{ machine_state::{ csregisters::{CSRRepr, CSRegister, CSRegisters}, @@ -14,11 +21,6 @@ use crate::{ state_backend as backend, traps::Exception, }; -use rustc_apfloat::{Float, FloatConvert, Round, Status, StatusAnd}; -use std::{ - fmt::{self, Display}, - ops::Neg, -}; pub trait FloatExt: Float + Into + Copy + Neg + From { /// The canonical NaN has a positive sign and all diff --git a/src/riscv/lib/src/interpreter/rv32a.rs b/src/riscv/lib/src/interpreter/rv32a.rs index e167e1b84dc2..410765059287 100644 --- a/src/riscv/lib/src/interpreter/rv32a.rs +++ b/src/riscv/lib/src/interpreter/rv32a.rs @@ -7,12 +7,13 @@ //! //! Chapter 8 - Unprivileged spec +use std::ops::{BitAnd, BitOr, BitXor}; + use crate::{ machine_state::{MachineCoreState, main_memory::MainMemoryLayout, registers::XRegister}, state_backend as backend, traps::Exception, }; -use std::ops::{BitAnd, BitOr, BitXor}; impl MachineCoreState where @@ -218,6 +219,10 @@ where #[cfg(test)] mod test { + use std::ops::{BitAnd, BitOr, BitXor}; + + use proptest::prelude::*; + use crate::{ backend_test, create_state, interpreter::atomics::{SC_FAILURE, SC_SUCCESS}, @@ -227,8 +232,6 @@ mod test { registers::{a0, a1, a2}, }, }; - use proptest::prelude::*; - use std::ops::{BitAnd, BitOr, BitXor}; #[macro_export] macro_rules! test_lrsc { diff --git a/src/riscv/lib/src/interpreter/rv32c.rs b/src/riscv/lib/src/interpreter/rv32c.rs index badf554c41f2..eb4fc7ddc041 100644 --- a/src/riscv/lib/src/interpreter/rv32c.rs +++ b/src/riscv/lib/src/interpreter/rv32c.rs @@ -210,6 +210,8 @@ where #[cfg(test)] mod tests { + use proptest::{prelude::*, prop_assert_eq, proptest}; + use crate::{ backend_test, create_state, machine_state::{ @@ -224,7 +226,6 @@ mod tests { }, parser::instruction::InstrWidth, }; - use proptest::{prelude::*, prop_assert_eq, proptest}; backend_test!(run_cj, F, { let test_case = [ diff --git a/src/riscv/lib/src/interpreter/rv32i.rs b/src/riscv/lib/src/interpreter/rv32i.rs index 4d04ef053fb1..d124d1f41989 100644 --- a/src/riscv/lib/src/interpreter/rv32i.rs +++ b/src/riscv/lib/src/interpreter/rv32i.rs @@ -375,6 +375,11 @@ where #[cfg(test)] mod tests { + use proptest::{ + prelude::{any, prop}, + prop_assert_eq, prop_assume, proptest, + }; + use crate::{ backend_test, create_state, interpreter::i::run_add, @@ -394,10 +399,6 @@ mod tests { parser::instruction::{FenceSet, InstrWidth}, traps::Exception, }; - use proptest::{ - prelude::{any, prop}, - prop_assert_eq, prop_assume, proptest, - }; backend_test!(test_add_sub, F, { let imm_rs1_rd_res = [ diff --git a/src/riscv/lib/src/interpreter/rv32m.rs b/src/riscv/lib/src/interpreter/rv32m.rs index 07766ea80786..fa6039b5da4b 100644 --- a/src/riscv/lib/src/interpreter/rv32m.rs +++ b/src/riscv/lib/src/interpreter/rv32m.rs @@ -141,11 +141,12 @@ where #[cfg(test)] mod test { + use proptest::{prelude::any, prop_assert_eq, proptest}; + use crate::{ backend_test, create_state, machine_state::registers::{XRegisters, XRegistersLayout, a0, a1, a2, a3}, }; - use proptest::{prelude::any, prop_assert_eq, proptest}; backend_test!(test_div_rem_invariant, F, { proptest!(|( diff --git a/src/riscv/lib/src/interpreter/rv64a.rs b/src/riscv/lib/src/interpreter/rv64a.rs index b2bc77fa0362..2e282c6f81eb 100644 --- a/src/riscv/lib/src/interpreter/rv64a.rs +++ b/src/riscv/lib/src/interpreter/rv64a.rs @@ -7,12 +7,13 @@ //! //! Chapter 8 - Unprivileged spec +use std::ops::{BitAnd, BitOr, BitXor}; + use crate::{ machine_state::{MachineCoreState, main_memory::MainMemoryLayout, registers::XRegister}, state_backend as backend, traps::Exception, }; -use std::ops::{BitAnd, BitOr, BitXor}; impl MachineCoreState where @@ -218,6 +219,10 @@ where #[cfg(test)] mod test { + use std::ops::{BitAnd, BitOr, BitXor}; + + use proptest::prelude::*; + use crate::{ backend_test, create_state, interpreter::atomics::{SC_FAILURE, SC_SUCCESS}, @@ -228,8 +233,6 @@ mod test { }, test_amo, test_lrsc, }; - use proptest::prelude::*; - use std::ops::{BitAnd, BitOr, BitXor}; test_lrsc!(test_lrd_scd, run_lrd, run_scd, 8, u64); diff --git a/src/riscv/lib/src/interpreter/rv64c.rs b/src/riscv/lib/src/interpreter/rv64c.rs index 009c828e930e..e36ca988e6e0 100644 --- a/src/riscv/lib/src/interpreter/rv64c.rs +++ b/src/riscv/lib/src/interpreter/rv64c.rs @@ -122,6 +122,10 @@ where } #[cfg(test)] mod tests { + use std::panic::{AssertUnwindSafe, catch_unwind}; + + use proptest::{arbitrary::any, prop_assert, prop_assert_eq, proptest}; + use crate::{ backend_test, create_state, machine_state::{ @@ -132,8 +136,6 @@ mod tests { }, traps::Exception, }; - use proptest::{arbitrary::any, prop_assert, prop_assert_eq, proptest}; - use std::panic::{AssertUnwindSafe, catch_unwind}; backend_test!(test_caddiw, F, { proptest!(|( diff --git a/src/riscv/lib/src/interpreter/rv64d.rs b/src/riscv/lib/src/interpreter/rv64d.rs index e2038597169d..947929a81f13 100644 --- a/src/riscv/lib/src/interpreter/rv64d.rs +++ b/src/riscv/lib/src/interpreter/rv64d.rs @@ -6,6 +6,11 @@ //! //! Chapter 12 - "D" Standard Extension for Double-Precision Floating-Point +use rustc_apfloat::{ + Float, Status, StatusAnd, + ieee::{Double, Single}, +}; + use super::float::FloatExt; use crate::{ machine_state::{ @@ -18,10 +23,6 @@ use crate::{ state_backend as backend, traps::Exception, }; -use rustc_apfloat::{ - Float, Status, StatusAnd, - ieee::{Double, Single}, -}; impl From for FValue { fn from(f: Double) -> Self { @@ -546,6 +547,9 @@ where #[cfg(test)] mod tests { + use arbitrary_int::u5; + use proptest::prelude::*; + use crate::{ backend_test, bits::Bits64, @@ -562,8 +566,6 @@ mod tests { }, traps::Exception, }; - use arbitrary_int::u5; - use proptest::prelude::*; backend_test!(test_fmv_d, F, { let state = create_state!(HartState, HartStateLayout, F); diff --git a/src/riscv/lib/src/interpreter/rv64dc.rs b/src/riscv/lib/src/interpreter/rv64dc.rs index 386425e07490..2e003b83d578 100644 --- a/src/riscv/lib/src/interpreter/rv64dc.rs +++ b/src/riscv/lib/src/interpreter/rv64dc.rs @@ -73,6 +73,9 @@ where #[cfg(test)] mod test { + use arbitrary_int::u5; + use proptest::prelude::*; + use crate::{ backend_test, bits::Bits64, @@ -88,8 +91,6 @@ mod test { }, traps::Exception, }; - use arbitrary_int::u5; - use proptest::prelude::*; const ZERO_OFFSET: i64 = 0; const OUT_OF_BOUNDS_OFFSET: i64 = 1024; diff --git a/src/riscv/lib/src/interpreter/rv64f.rs b/src/riscv/lib/src/interpreter/rv64f.rs index afd6d75d83c6..81d846ac9898 100644 --- a/src/riscv/lib/src/interpreter/rv64f.rs +++ b/src/riscv/lib/src/interpreter/rv64f.rs @@ -6,6 +6,8 @@ //! //! Chapter 11 - "F" Standard Extension for Single-Precision Floating-Point +use rustc_apfloat::{Float, Status, StatusAnd, ieee::Single}; + use super::float::FloatExt; use crate::{ machine_state::{ @@ -18,7 +20,6 @@ use crate::{ state_backend as backend, traps::Exception, }; -use rustc_apfloat::{Float, Status, StatusAnd, ieee::Single}; impl From for FValue { fn from(f: Single) -> Self { @@ -551,6 +552,10 @@ fn fvalue_to_f32_bits(f: FValue) -> u32 { #[cfg(test)] mod tests { + use arbitrary_int::u5; + use proptest::prelude::*; + use rustc_apfloat::{Float, ieee::Double, ieee::Single}; + use super::f32_to_fvalue; use crate::{ backend_test, @@ -569,11 +574,6 @@ mod tests { traps::Exception, }; - use arbitrary_int::u5; - use proptest::prelude::*; - - use rustc_apfloat::{Float, ieee::Double, ieee::Single}; - backend_test!(test_fmv_f, F, { proptest!(|( f in any::().prop_map(f32::to_bits), diff --git a/src/riscv/lib/src/interpreter/rv64i.rs b/src/riscv/lib/src/interpreter/rv64i.rs index 0c175371ffb3..7bcaa7a38702 100644 --- a/src/riscv/lib/src/interpreter/rv64i.rs +++ b/src/riscv/lib/src/interpreter/rv64i.rs @@ -357,6 +357,8 @@ where #[cfg(test)] mod tests { + use proptest::{arbitrary::any, prop_assert, prop_assert_eq, proptest}; + use crate::{ backend_test, create_state, machine_state::{ @@ -367,7 +369,6 @@ mod tests { }, traps::Exception, }; - use proptest::{arbitrary::any, prop_assert, prop_assert_eq, proptest}; backend_test!(test_add_w, F, { proptest!(|( diff --git a/src/riscv/lib/src/interpreter/rv64m.rs b/src/riscv/lib/src/interpreter/rv64m.rs index 41a947097981..d3ec6a900a25 100644 --- a/src/riscv/lib/src/interpreter/rv64m.rs +++ b/src/riscv/lib/src/interpreter/rv64m.rs @@ -101,11 +101,12 @@ where #[cfg(test)] mod test { + use proptest::{prelude::any, prop_assert_eq, proptest}; + use crate::{ backend_test, create_state, machine_state::registers::{XRegisters, XRegistersLayout, a0, a1, a2, a3}, }; - use proptest::{prelude::any, prop_assert_eq, proptest}; backend_test!(test_div_rem_invariant, F, { proptest!(|( diff --git a/src/riscv/lib/src/interpreter/rv64priv.rs b/src/riscv/lib/src/interpreter/rv64priv.rs index 40f70b732f3f..6d75b1de54f8 100644 --- a/src/riscv/lib/src/interpreter/rv64priv.rs +++ b/src/riscv/lib/src/interpreter/rv64priv.rs @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: MIT +use strum::IntoEnumIterator; + use crate::{ machine_state::{ AccessType, MachineCoreState, @@ -18,7 +20,6 @@ use crate::{ state_backend::{self as backend}, traps::Exception, }; -use strum::IntoEnumIterator; impl HartState where diff --git a/src/riscv/lib/src/jit.rs b/src/riscv/lib/src/jit.rs index ab7f7d31b74f..297bfc049931 100644 --- a/src/riscv/lib/src/jit.rs +++ b/src/riscv/lib/src/jit.rs @@ -8,15 +8,6 @@ mod builder; pub mod state_access; -use self::builder::Builder; -use self::state_access::JsaCalls; -use self::state_access::JsaImports; -use self::state_access::register_jsa_symbols; -use crate::machine_state::MachineCoreState; -use crate::machine_state::ProgramCounterUpdate; -use crate::machine_state::instruction::Instruction; -use crate::machine_state::main_memory::MainMemoryLayout; -use crate::traps::EnvironException; use cranelift::codegen::CodegenError; use cranelift::codegen::ir::types::I64; use cranelift::codegen::settings::SetError; @@ -29,6 +20,16 @@ use cranelift_module::ModuleError; use state_access::JitStateAccess; use thiserror::Error; +use self::builder::Builder; +use self::state_access::JsaCalls; +use self::state_access::JsaImports; +use self::state_access::register_jsa_symbols; +use crate::machine_state::MachineCoreState; +use crate::machine_state::ProgramCounterUpdate; +use crate::machine_state::instruction::Instruction; +use crate::machine_state::main_memory::MainMemoryLayout; +use crate::traps::EnvironException; + /// Alias for the function signature produced by the JIT compilation. type JitFn = unsafe extern "C" fn(&mut MachineCoreState, u64, &mut usize); @@ -330,9 +331,10 @@ mod tests { }); backend_test!(test_cmv, F, { - use crate::machine_state::registers::NonZeroXRegister::*; use Instruction as I; + use crate::machine_state::registers::NonZeroXRegister::*; + // Arrange let scenarios: &[&[I]] = &[ &[I::new_mv(x2, x1, Compressed)], @@ -403,9 +405,10 @@ mod tests { }); backend_test!(test_add, F, { - use crate::machine_state::registers::NonZeroXRegister::*; use Instruction as I; + use crate::machine_state::registers::NonZeroXRegister::*; + // Arrange // calculate fibonacci(4) == 5 @@ -473,9 +476,10 @@ mod tests { }); backend_test!(test_jit_recovers_from_compilation_failure, F, { - use crate::machine_state::registers::NonZeroXRegister::*; use Instruction as I; + use crate::machine_state::registers::NonZeroXRegister::*; + // Arrange let failure: &[I] = &[ // does not currently lowering diff --git a/src/riscv/lib/src/jit/builder.rs b/src/riscv/lib/src/jit/builder.rs index 895d3ed35d98..a8ef52ed05e3 100644 --- a/src/riscv/lib/src/jit/builder.rs +++ b/src/riscv/lib/src/jit/builder.rs @@ -6,6 +6,11 @@ //! //! [instructions]: crate::machine_state::instruction::Instruction +use cranelift::{ + codegen::ir::{InstBuilder, MemFlags, Type, Value}, + frontend::FunctionBuilder, +}; + use super::state_access::{JitStateAccess, JsaCalls}; use crate::{ instruction_context::ICB, @@ -14,10 +19,6 @@ use crate::{ registers::NonZeroXRegister as XRegister, }, }; -use cranelift::{ - codegen::ir::{InstBuilder, MemFlags, Type, Value}, - frontend::FunctionBuilder, -}; /// Builder context used when lowering individual instructions within a block. pub(super) struct Builder<'a, ML: MainMemoryLayout, JSA: JitStateAccess> { diff --git a/src/riscv/lib/src/jit/state_access.rs b/src/riscv/lib/src/jit/state_access.rs index 6185f9bfd785..6ec871d3dfa3 100644 --- a/src/riscv/lib/src/jit/state_access.rs +++ b/src/riscv/lib/src/jit/state_access.rs @@ -20,14 +20,8 @@ //! [function builder]: cranelift::frontend::FunctionBuilderContext //! [direct function call]: cranelift::codegen::ir::InstBuilder::call -use crate::{ - machine_state::{ - MachineCoreState, - main_memory::MainMemoryLayout, - registers::{NonZeroXRegister, XValue}, - }, - state_backend::{ManagerReadWrite, owned_backend::Owned}, -}; +use std::marker::PhantomData; + use cranelift::{ codegen::ir::{ AbiParam, FuncRef, InstBuilder, Signature, Value, @@ -37,7 +31,15 @@ use cranelift::{ }; use cranelift_jit::{JITBuilder, JITModule}; use cranelift_module::{FuncId, Linkage, Module, ModuleResult}; -use std::marker::PhantomData; + +use crate::{ + machine_state::{ + MachineCoreState, + main_memory::MainMemoryLayout, + registers::{NonZeroXRegister, XValue}, + }, + state_backend::{ManagerReadWrite, owned_backend::Owned}, +}; const PC_WRITE_SYMBOL: &str = "JSA::pc_write"; diff --git a/src/riscv/lib/src/kernel_loader.rs b/src/riscv/lib/src/kernel_loader.rs index 413d98b6880f..acb1683bfae1 100644 --- a/src/riscv/lib/src/kernel_loader.rs +++ b/src/riscv/lib/src/kernel_loader.rs @@ -1,3 +1,5 @@ +use std::io::{Cursor, Seek, SeekFrom, Write}; + use derive_more::{Error, From}; use goblin::{ elf::Elf, @@ -7,7 +9,6 @@ use goblin::{ program_header::{PT_LOAD, ProgramHeader}, }, }; -use std::io::{Cursor, Seek, SeekFrom, Write}; #[derive(Debug, From, Error, derive_more::Display)] pub enum Error { diff --git a/src/riscv/lib/src/machine_state.rs b/src/riscv/lib/src/machine_state.rs index c4a66745f794..0bc697d39596 100644 --- a/src/riscv/lib/src/machine_state.rs +++ b/src/riscv/lib/src/machine_state.rs @@ -17,18 +17,8 @@ pub mod reservation_set; #[cfg(test)] extern crate proptest; -use crate::{ - bits::u64, - devicetree, - parser::{ - instruction::{Instr, InstrCacheable, InstrUncacheable, InstrWidth}, - is_compressed, parse_compressed_instruction, parse_uncompressed_instruction, - }, - program::Program, - range_utils::{bound_saturating_sub, less_than_bound, unwrap_bound}, - state_backend::{self as backend, ManagerReadWrite}, - traps::{EnvironException, Exception}, -}; +use std::ops::Bound; + pub use address_translation::AccessType; use address_translation::{ PAGE_SIZE, @@ -45,7 +35,19 @@ use hart_state::{HartState, HartStateLayout}; use instruction::Instruction; use main_memory::{Address, MainMemory, OutOfBounds}; use mode::Mode; -use std::ops::Bound; + +use crate::{ + bits::u64, + devicetree, + parser::{ + instruction::{Instr, InstrCacheable, InstrUncacheable, InstrWidth}, + is_compressed, parse_compressed_instruction, parse_uncompressed_instruction, + }, + program::Program, + range_utils::{bound_saturating_sub, less_than_bound, unwrap_bound}, + state_backend::{self as backend, ManagerReadWrite}, + traps::{EnvironException, Exception}, +}; /// Layout for the machine 'run state' - which contains everything required for the running of /// instructions. @@ -664,6 +666,10 @@ pub enum MachineError { #[cfg(test)] mod tests { + use std::ops::Bound; + + use proptest::{prop_assert_eq, proptest}; + use super::{ MachineState, MachineStateLayout, block_cache::bcall::{Interpreted, InterpretedBlockBuilder}, @@ -708,8 +714,6 @@ mod tests { traps::{EnvironException, Exception, TrapContext}, }; use crate::{bits::u64, machine_state::main_memory::M1K}; - use proptest::{prop_assert_eq, proptest}; - use std::ops::Bound; backend_test!(test_step, F, { let state = create_state!( diff --git a/src/riscv/lib/src/machine_state/address_translation.rs b/src/riscv/lib/src/machine_state/address_translation.rs index f4c428ec796f..2e6960633f27 100644 --- a/src/riscv/lib/src/machine_state/address_translation.rs +++ b/src/riscv/lib/src/machine_state/address_translation.rs @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: MIT +use strum::{EnumCount, EnumIter}; + use super::{ MachineCoreState, csregisters::{ @@ -17,7 +19,6 @@ use crate::{ state_backend::{self as backend}, traps::Exception, }; -use strum::{EnumCount, EnumIter}; mod physical_address; pub mod pte; diff --git a/src/riscv/lib/src/machine_state/address_translation/physical_address.rs b/src/riscv/lib/src/machine_state/address_translation/physical_address.rs index 2616580af5b1..6b00d2d45620 100644 --- a/src/riscv/lib/src/machine_state/address_translation/physical_address.rs +++ b/src/riscv/lib/src/machine_state/address_translation/physical_address.rs @@ -91,9 +91,10 @@ pub fn set_ppn_idx( #[cfg(test)] mod tests { - use crate::machine_state::csregisters::{CSRRepr, satp::SvLength}; use proptest::proptest; + use crate::machine_state::csregisters::{CSRRepr, satp::SvLength}; + #[test] pub fn test_physical_address() { proptest!(|( diff --git a/src/riscv/lib/src/machine_state/address_translation/pte.rs b/src/riscv/lib/src/machine_state/address_translation/pte.rs index 9903b62a37d7..0c63ce673211 100644 --- a/src/riscv/lib/src/machine_state/address_translation/pte.rs +++ b/src/riscv/lib/src/machine_state/address_translation/pte.rs @@ -68,11 +68,12 @@ csr! { #[cfg(test)] mod tests { + use proptest::{prop_assert_eq, proptest}; + use crate::{ bits::Bits64, machine_state::{address_translation::pte::PageTableEntry, csregisters::satp::SvLength}, }; - use proptest::{prop_assert_eq, proptest}; #[test] pub fn test_pte_fields() { diff --git a/src/riscv/lib/src/machine_state/address_translation/translation_cache.rs b/src/riscv/lib/src/machine_state/address_translation/translation_cache.rs index 9c97cd46fb6a..a1412725bff1 100644 --- a/src/riscv/lib/src/machine_state/address_translation/translation_cache.rs +++ b/src/riscv/lib/src/machine_state/address_translation/translation_cache.rs @@ -33,6 +33,8 @@ //! invalidate the cache entries all at once because that would result in a very large proof for //! a single invalidation step. Therefore, we do it gradually. +use strum::EnumCount; + use super::{AccessType, PAGE_OFFSET_WIDTH}; use crate::{ bits::ones, @@ -43,7 +45,6 @@ use crate::{ ManagerReadWrite, ManagerWrite, Many, Ref, }, }; -use strum::EnumCount; /// Bit mask to obtain the offset into a page const OFFSET_MASK: u64 = ones(PAGE_OFFSET_WIDTH as u64); diff --git a/src/riscv/lib/src/machine_state/address_translation/virtual_address.rs b/src/riscv/lib/src/machine_state/address_translation/virtual_address.rs index 412eddf608eb..124cfe52a487 100644 --- a/src/riscv/lib/src/machine_state/address_translation/virtual_address.rs +++ b/src/riscv/lib/src/machine_state/address_translation/virtual_address.rs @@ -53,9 +53,10 @@ pub fn get_vpn_idx(v_addr: Address, sv_length: &SvLength, index: usize) -> Optio #[cfg(test)] mod tests { - use crate::machine_state::{address_translation::virtual_address, csregisters::satp::SvLength}; use proptest::proptest; + use crate::machine_state::{address_translation::virtual_address, csregisters::satp::SvLength}; + #[test] pub fn test_virtual_address() { proptest!(|( diff --git a/src/riscv/lib/src/machine_state/block_cache.rs b/src/riscv/lib/src/machine_state/block_cache.rs index da0655f7abb5..f95a64766c43 100644 --- a/src/riscv/lib/src/machine_state/block_cache.rs +++ b/src/riscv/lib/src/machine_state/block_cache.rs @@ -81,6 +81,11 @@ pub mod bcall; +use std::marker::PhantomData; +use std::u64; + +use bcall::{BCall, Block}; + use super::MachineCoreState; use super::address_translation::PAGE_OFFSET_WIDTH; use super::instruction::Instruction; @@ -100,9 +105,6 @@ use crate::{ }; use crate::{default::ConstDefault, state_backend::verify_backend}; use crate::{machine_state::instruction::Args, storage::binary}; -use bcall::{BCall, Block}; -use std::marker::PhantomData; -use std::u64; /// Mask for getting the offset within a page const PAGE_OFFSET_MASK: usize = (1 << PAGE_OFFSET_WIDTH) - 1; diff --git a/src/riscv/lib/src/machine_state/csregisters.rs b/src/riscv/lib/src/machine_state/csregisters.rs index 1b2da0608cd1..43f869242940 100644 --- a/src/riscv/lib/src/machine_state/csregisters.rs +++ b/src/riscv/lib/src/machine_state/csregisters.rs @@ -11,6 +11,11 @@ pub mod satp; pub mod values; pub mod xstatus; +use num_enum::TryFromPrimitive; +use root::RootCSRegister; +use strum::IntoEnumIterator; +use values::{CSRValues, CSRValuesLayout, MStatusValue}; + use self::{ bits::NormaliseFields, satp::Satp, @@ -24,10 +29,6 @@ use crate::{ state_backend::{self as backend, ManagerRead}, traps::{Exception, Interrupt, TrapContext, TrapKind}, }; -use num_enum::TryFromPrimitive; -use root::RootCSRegister; -use strum::IntoEnumIterator; -use values::{CSRValues, CSRValuesLayout, MStatusValue}; /// Privilege required to access a CSR #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -1672,6 +1673,8 @@ impl Clone for CSRegisters { #[cfg(test)] #[allow(clippy::identity_op)] mod tests { + use strum::IntoEnumIterator; + use crate::{ backend_test, bits::Bits64, @@ -1685,7 +1688,6 @@ mod tests { }, traps::{Interrupt, TrapContext}, }; - use strum::IntoEnumIterator; #[test] fn test_privilege_access() { diff --git a/src/riscv/lib/src/machine_state/csregisters/values.rs b/src/riscv/lib/src/machine_state/csregisters/values.rs index 4d205bb965ce..fddd183eb875 100644 --- a/src/riscv/lib/src/machine_state/csregisters/values.rs +++ b/src/riscv/lib/src/machine_state/csregisters/values.rs @@ -6,6 +6,10 @@ mod mstatus; mod xip; +use mstatus::MStatusLayout; +pub(super) use mstatus::MStatusValue; +use xip::{XipCell, XipCellLayout}; + use super::{ CSRegisters, effects::{NoEffect, handle_csr_effect}, @@ -26,9 +30,6 @@ use crate::{ }, storage::binary, }; -use mstatus::MStatusLayout; -pub(super) use mstatus::MStatusValue; -use xip::{XipCell, XipCellLayout}; /// Representation of a value in a CSR pub type CSRRepr = u64; @@ -1717,10 +1718,12 @@ where #[cfg(test)] mod tests { - use super::*; use std::sync::atomic::{AtomicUsize, Ordering}; + use strum::IntoEnumIterator; + use super::*; + /// Ensure that [`CSRValues::fold_ref`] and [`CSRValuesF::fold_mut`] refer to the same CSR /// value field provided the same [`RootCSRegister`]. #[test] diff --git a/src/riscv/lib/src/machine_state/instruction.rs b/src/riscv/lib/src/machine_state/instruction.rs index 9418a68d7b2b..ec47cf2f9182 100644 --- a/src/riscv/lib/src/machine_state/instruction.rs +++ b/src/riscv/lib/src/machine_state/instruction.rs @@ -15,6 +15,11 @@ mod constructors; pub mod tagged_instruction; +use std::fmt::{self, Debug, Formatter}; + +use serde::{Deserialize, Serialize}; +use tagged_instruction::{ArgsShape, TaggedInstruction, opcode_to_argsshape}; + use super::{ MachineCoreState, ProgramCounterUpdate, csregisters::CSRegister, @@ -37,9 +42,6 @@ use crate::{ state_backend::{ManagerBase, ManagerReadWrite}, traps::Exception, }; -use serde::{Deserialize, Serialize}; -use std::fmt::{self, Debug, Formatter}; -use tagged_instruction::{ArgsShape, TaggedInstruction, opcode_to_argsshape}; /// An instruction formed of an opcode and flat arguments. /// diff --git a/src/riscv/lib/src/machine_state/instruction/tagged_instruction.rs b/src/riscv/lib/src/machine_state/instruction/tagged_instruction.rs index 373771bbcddd..5d639fa23b85 100644 --- a/src/riscv/lib/src/machine_state/instruction/tagged_instruction.rs +++ b/src/riscv/lib/src/machine_state/instruction/tagged_instruction.rs @@ -2,14 +2,16 @@ // // SPDX-License-Identifier: MIT +use std::fmt::Debug; + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + use super::{ Args, CSRegister, FRegister, InstrRoundingMode, Instruction, NonZeroXRegister, OpCode, XRegister, }; use crate::{default::ConstDefault, parser::instruction::InstrWidth}; -use serde::{Deserialize, Serialize}; -use std::fmt::Debug; -use thiserror::Error; #[derive(Error, Debug)] /// Errors that may be returned when converting between tagged and untagged registers. diff --git a/src/riscv/lib/src/machine_state/main_memory.rs b/src/riscv/lib/src/machine_state/main_memory.rs index 907d97ef6d66..8c7498495d35 100644 --- a/src/riscv/lib/src/machine_state/main_memory.rs +++ b/src/riscv/lib/src/machine_state/main_memory.rs @@ -3,14 +3,16 @@ // // SPDX-License-Identifier: MIT +use std::mem; + +use serde::{Deserialize, Serialize}; +use tezos_smart_rollup_constants::riscv::SbiError; +use thiserror::Error; + use crate::{ machine_state::registers::XValue, state_backend::{self as backend, ManagerDeserialise, ManagerSerialise}, }; -use serde::{Deserialize, Serialize}; -use std::mem; -use tezos_smart_rollup_constants::riscv::SbiError; -use thiserror::Error; /// Bus address pub type Address = XValue; diff --git a/src/riscv/lib/src/machine_state/registers.rs b/src/riscv/lib/src/machine_state/registers.rs index 26fa52f9bc81..111bc16f2e22 100644 --- a/src/riscv/lib/src/machine_state/registers.rs +++ b/src/riscv/lib/src/machine_state/registers.rs @@ -6,10 +6,12 @@ // specification. #![allow(non_upper_case_globals)] -use crate::{default::ConstDefault, machine_state::backend}; -use arbitrary_int::u5; use std::fmt; +use arbitrary_int::u5; + +use crate::{default::ConstDefault, machine_state::backend}; + /// Integer register index #[allow(non_camel_case_types)] // To make names consistent with specification #[repr(u8)] @@ -608,11 +610,12 @@ impl Clone for FRegisters { #[cfg(test)] mod tests { - use super::*; - use crate::{backend_test, create_state}; use arbitrary_int::Number; use strum::IntoEnumIterator; + use super::*; + use crate::{backend_test, create_state}; + backend_test!(test_zero, F, { let mut registers = create_state!(XRegisters, XRegistersLayout, F); diff --git a/src/riscv/lib/src/ocaml_api.rs b/src/riscv/lib/src/ocaml_api.rs index 755613818583..bf3322ba93fd 100644 --- a/src/riscv/lib/src/ocaml_api.rs +++ b/src/riscv/lib/src/ocaml_api.rs @@ -6,6 +6,15 @@ mod move_semantics; mod pointer_apply; +use std::{fs, str}; + +use arbitrary_int::u31; +use move_semantics::{ImmutableState, MutableState}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use ocaml::{Pointer, ToValue}; +use pointer_apply::{ApplyReadOnly, apply_imm, apply_mut}; +use sha2::{Digest, Sha256}; + use crate::{ pvm::{ PvmHooks, PvmStatus, @@ -19,13 +28,6 @@ use crate::{ state_backend::proof_backend::proof, storage::{self, StorageError}, }; -use arbitrary_int::u31; -use move_semantics::{ImmutableState, MutableState}; -use num_enum::{IntoPrimitive, TryFromPrimitive}; -use ocaml::{Pointer, ToValue}; -use pointer_apply::{ApplyReadOnly, apply_imm, apply_mut}; -use sha2::{Digest, Sha256}; -use std::{fs, str}; const HERMIT_LOADER: &[u8] = include_bytes!("../../assets/hermit-loader"); diff --git a/src/riscv/lib/src/parser.rs b/src/riscv/lib/src/parser.rs index b7a7c26e9520..ad4f058f4a5b 100644 --- a/src/riscv/lib/src/parser.rs +++ b/src/riscv/lib/src/parser.rs @@ -7,16 +7,18 @@ pub mod instruction; +use core::ops::Range; +use std::fmt::{self, Display, Formatter}; + +use arbitrary_int::{u3, u5}; +use instruction::*; + use crate::bits::u16; use crate::machine_state::registers::{NonZeroXRegister, x0}; use crate::machine_state::{ csregisters::CSRegister, registers::{FRegister, XRegister, parse_fregister, parse_xregister}, }; -use arbitrary_int::{u3, u5}; -use core::ops::Range; -use instruction::*; -use std::fmt::{self, Display, Formatter}; /// Given an instruction encoded as a little-endian `u32`, extract `n` bits /// starting at `pos`. diff --git a/src/riscv/lib/src/parser/instruction.rs b/src/riscv/lib/src/parser/instruction.rs index 35d4296bf24d..9fc1cc02c73d 100644 --- a/src/riscv/lib/src/parser/instruction.rs +++ b/src/riscv/lib/src/parser/instruction.rs @@ -3,6 +3,11 @@ // // SPDX-License-Identifier: MIT +use std::fmt; + +use enum_tag::EnumTag; +use serde::{Deserialize, Serialize}; + use super::XRegisterParsed; use crate::{ default::ConstDefault, @@ -12,9 +17,6 @@ use crate::{ registers::{FRegister, NonZeroXRegister, XRegister}, }, }; -use enum_tag::EnumTag; -use serde::{Deserialize, Serialize}; -use std::fmt; #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, serde::Serialize, serde::Deserialize)] pub struct RTypeArgs { diff --git a/src/riscv/lib/src/program.rs b/src/riscv/lib/src/program.rs index 90a7d06e846c..7ce639616a44 100644 --- a/src/riscv/lib/src/program.rs +++ b/src/riscv/lib/src/program.rs @@ -2,9 +2,10 @@ // // SPDX-License-Identifier: MIT -use crate::{kernel_loader, machine_state::main_memory, parser::parse_block}; use std::{borrow::Cow, collections::BTreeMap, marker::PhantomData}; +use crate::{kernel_loader, machine_state::main_memory, parser::parse_block}; + /// RISC-V program pub struct Program<'a, ML> { _pd: PhantomData, @@ -130,12 +131,13 @@ impl<'a, ML: main_memory::MainMemoryLayout> Program<'a, ML> { #[cfg(test)] mod tests { + use std::{cell::RefCell, collections::BTreeMap, fs, io::Cursor, marker::PhantomData}; + use crate::{ kernel_loader::{self, Memory}, machine_state::main_memory::{self, M1M}, program::Program, }; - use std::{cell::RefCell, collections::BTreeMap, fs, io::Cursor, marker::PhantomData}; #[test] fn test_impl_memory_program() { diff --git a/src/riscv/lib/src/pvm/common.rs b/src/riscv/lib/src/pvm/common.rs index 8db7e337cc84..83f82ed849d5 100644 --- a/src/riscv/lib/src/pvm/common.rs +++ b/src/riscv/lib/src/pvm/common.rs @@ -2,6 +2,15 @@ // // SPDX-License-Identifier: MIT +use std::{ + convert::Infallible, + fmt, + io::{Write, stdout}, + ops::Bound, +}; + +#[cfg(feature = "supervisor")] +use super::linux; use super::reveals::{RevealRequest, RevealRequestLayout}; use crate::{ default::ConstDefault, @@ -17,15 +26,6 @@ use crate::{ }, traps::EnvironException, }; -use std::{ - convert::Infallible, - fmt, - io::{Write, stdout}, - ops::Bound, -}; - -#[cfg(feature = "supervisor")] -use super::linux; /// PVM configuration pub struct PvmHooks<'a> { @@ -385,6 +385,14 @@ impl< #[cfg(test)] mod tests { + use std::mem; + + use rand::{Fill, thread_rng}; + use tezos_smart_rollup_constants::riscv::{ + REVEAL_REQUEST_MAX_SIZE, SBI_CONSOLE_PUTCHAR, SBI_FIRMWARE_TEZOS, SBI_TEZOS_INBOX_NEXT, + SBI_TEZOS_REVEAL, + }; + use super::*; use crate::state_backend::test_helpers::TestBackendFactory; use crate::{ @@ -397,12 +405,6 @@ mod tests { }, state_backend::owned_backend::Owned, }; - use rand::{Fill, thread_rng}; - use std::mem; - use tezos_smart_rollup_constants::riscv::{ - REVEAL_REQUEST_MAX_SIZE, SBI_CONSOLE_PUTCHAR, SBI_FIRMWARE_TEZOS, SBI_TEZOS_INBOX_NEXT, - SBI_TEZOS_REVEAL, - }; #[test] fn test_read_input() { diff --git a/src/riscv/lib/src/pvm/linux.rs b/src/riscv/lib/src/pvm/linux.rs index ad94a5655ae8..ee4ece7ab5ae 100644 --- a/src/riscv/lib/src/pvm/linux.rs +++ b/src/riscv/lib/src/pvm/linux.rs @@ -6,6 +6,10 @@ mod error; mod fds; mod fs; +use std::{ffi::CStr, num::NonZeroU64}; + +use error::Error; + use super::{Pvm, PvmHooks}; use crate::{ machine_state::{ @@ -21,8 +25,6 @@ use crate::{ ManagerReadWrite, ManagerWrite, Ref, }, }; -use error::Error; -use std::{ffi::CStr, num::NonZeroU64}; /// Size of a memory page in bytes pub const PAGE_SIZE: u64 = 4096; @@ -508,12 +510,14 @@ impl Clone for SupervisorState { #[cfg(test)] mod tests { + use std::array; + + use rand::Rng; + use super::*; use crate::{ backend_test, create_state, machine_state::MachineCoreStateLayout, state_backend::DynArray, }; - use rand::Rng; - use std::array; // Check that the `set_tid_address` system call is working correctly. backend_test!(set_tid_address, F, { diff --git a/src/riscv/lib/src/pvm/node_pvm.rs b/src/riscv/lib/src/pvm/node_pvm.rs index 7f32a244c040..62b8e44ef129 100644 --- a/src/riscv/lib/src/pvm/node_pvm.rs +++ b/src/riscv/lib/src/pvm/node_pvm.rs @@ -3,6 +3,10 @@ // // SPDX-License-Identifier: MIT +use std::{fmt, ops::Bound, path::Path}; + +use thiserror::Error; + use crate::{ machine_state::{ TestCacheLayouts, @@ -22,8 +26,6 @@ use crate::{ }, storage::{self, Hash, Repo}, }; -use std::{fmt, ops::Bound, path::Path}; -use thiserror::Error; pub type StateLayout = ( PvmLayout, diff --git a/src/riscv/lib/src/pvm/reveals.rs b/src/riscv/lib/src/pvm/reveals.rs index 8d737ea3044a..8cf861f79143 100644 --- a/src/riscv/lib/src/pvm/reveals.rs +++ b/src/riscv/lib/src/pvm/reveals.rs @@ -2,11 +2,12 @@ // // SPDX-License-Identifier: MIT +use tezos_smart_rollup_constants::riscv::REVEAL_REQUEST_MAX_SIZE; + use crate::state_backend::{ AllocatedOf, Atom, Cell, DynArray, DynCells, FnManager, ManagerBase, ManagerClone, ManagerRead, Ref, }; -use tezos_smart_rollup_constants::riscv::REVEAL_REQUEST_MAX_SIZE; /// Reveal request layout pub type RevealRequestLayout = (DynArray, Atom); diff --git a/src/riscv/lib/src/pvm/sbi.rs b/src/riscv/lib/src/pvm/sbi.rs index 8128ed806d7c..721519974e23 100644 --- a/src/riscv/lib/src/pvm/sbi.rs +++ b/src/riscv/lib/src/pvm/sbi.rs @@ -5,6 +5,20 @@ // When the Supervisor is enabled, most of this module is not used. #![cfg_attr(feature = "supervisor", allow(dead_code))] +use std::cmp::min; + +use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; +use tezos_smart_rollup_constants::{ + core::MAX_INPUT_MESSAGE_SIZE, + riscv::{ + REVEAL_DATA_MAX_SIZE, REVEAL_REQUEST_MAX_SIZE, SBI_CONSOLE_PUTCHAR, SBI_DBCN, + SBI_DBCN_CONSOLE_WRITE_BYTE, SBI_FIRMWARE_TEZOS, SBI_SHUTDOWN, SBI_SRST, + SBI_SRST_SYSTEM_RESET, SBI_TEZOS_BLAKE2B_HASH256, SBI_TEZOS_ED25519_SIGN, + SBI_TEZOS_ED25519_VERIFY, SBI_TEZOS_INBOX_NEXT, SBI_TEZOS_METADATA_REVEAL, + SBI_TEZOS_REVEAL, SbiError, + }, +}; + use super::{PvmHooks, PvmStatus, reveals::RevealRequest}; use crate::{ machine_state::{ @@ -17,18 +31,6 @@ use crate::{ state_backend::{CellRead, CellReadWrite, CellWrite, ManagerReadWrite}, traps::EnvironException, }; -use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; -use std::cmp::min; -use tezos_smart_rollup_constants::{ - core::MAX_INPUT_MESSAGE_SIZE, - riscv::{ - REVEAL_DATA_MAX_SIZE, REVEAL_REQUEST_MAX_SIZE, SBI_CONSOLE_PUTCHAR, SBI_DBCN, - SBI_DBCN_CONSOLE_WRITE_BYTE, SBI_FIRMWARE_TEZOS, SBI_SHUTDOWN, SBI_SRST, - SBI_SRST_SYSTEM_RESET, SBI_TEZOS_BLAKE2B_HASH256, SBI_TEZOS_ED25519_SIGN, - SBI_TEZOS_ED25519_VERIFY, SBI_TEZOS_INBOX_NEXT, SBI_TEZOS_METADATA_REVEAL, - SBI_TEZOS_REVEAL, SbiError, - }, -}; /// Write the SBI error code as the return value. #[inline(always)] diff --git a/src/riscv/lib/src/state_backend/effects.rs b/src/riscv/lib/src/state_backend/effects.rs index 7626551ae815..34d23d512e73 100644 --- a/src/riscv/lib/src/state_backend/effects.rs +++ b/src/riscv/lib/src/state_backend/effects.rs @@ -2,12 +2,13 @@ // // SPDX-License-Identifier: MIT +use std::marker::PhantomData; + use super::{ AllocatedOf, Atom, Cell, FnManager, ManagerBase, ManagerClone, ManagerRead, ManagerReadWrite, ManagerWrite, Ref, StaticCopy, }; use crate::default::ConstDefault; -use std::marker::PhantomData; /// XXX: Workaround trait for not having enum variants as const-generics pub trait EffectGetter { diff --git a/src/riscv/lib/src/state_backend/hash.rs b/src/riscv/lib/src/state_backend/hash.rs index 90196736527c..0bb8b8494353 100644 --- a/src/riscv/lib/src/state_backend/hash.rs +++ b/src/riscv/lib/src/state_backend/hash.rs @@ -6,6 +6,7 @@ //! Common type for hashes use std::num::NonZeroUsize; + use thiserror::Error; #[derive(Error, Debug)] diff --git a/src/riscv/lib/src/state_backend/layout.rs b/src/riscv/lib/src/state_backend/layout.rs index 5c1da8539c9f..10a9201e6db2 100644 --- a/src/riscv/lib/src/state_backend/layout.rs +++ b/src/riscv/lib/src/state_backend/layout.rs @@ -3,9 +3,10 @@ // // SPDX-License-Identifier: MIT -use crate::default::ConstDefault; use std::marker::PhantomData; +use crate::default::ConstDefault; + /// Structural description of a state type pub trait Layout { /// Representation of the allocated regions in the state backend diff --git a/src/riscv/lib/src/state_backend/owned_backend.rs b/src/riscv/lib/src/state_backend/owned_backend.rs index c1dc4c5e97cc..3c50e0c4ac6a 100644 --- a/src/riscv/lib/src/state_backend/owned_backend.rs +++ b/src/riscv/lib/src/state_backend/owned_backend.rs @@ -3,18 +3,20 @@ // // SPDX-License-Identifier: MIT -use super::{ - AllocatedOf, Elem, EnrichedValue, EnrichedValueLinked, Layout, ManagerAlloc, ManagerBase, - ManagerClone, ManagerDeserialise, ManagerRead, ManagerReadWrite, ManagerSerialise, - ManagerWrite, StaticCopy, -}; -use serde::ser::SerializeTuple; use std::{ fmt, marker::PhantomData, mem::{self, MaybeUninit}, }; +use serde::ser::SerializeTuple; + +use super::{ + AllocatedOf, Elem, EnrichedValue, EnrichedValueLinked, Layout, ManagerAlloc, ManagerBase, + ManagerClone, ManagerDeserialise, ManagerRead, ManagerReadWrite, ManagerSerialise, + ManagerWrite, StaticCopy, +}; + /// Manager that allows state binders to own the state storage #[derive(Clone, Copy, Debug)] pub struct Owned; diff --git a/src/riscv/lib/src/state_backend/proof_backend.rs b/src/riscv/lib/src/state_backend/proof_backend.rs index 149c76f7269a..16e3b90b1416 100644 --- a/src/riscv/lib/src/state_backend/proof_backend.rs +++ b/src/riscv/lib/src/state_backend/proof_backend.rs @@ -14,18 +14,20 @@ //! [`MerkleTree`]: merkle::MerkleTree //! [`ProofLayout`]: super::ProofLayout -use super::{ - EnrichedValue, EnrichedValueLinked, ManagerBase, ManagerRead, ManagerReadWrite, - ManagerSerialise, ManagerWrite, -}; -use merkle::AccessInfo; -use serde::{Serialize, ser::SerializeTuple}; use std::{ cell::{Cell, RefCell}, collections::{BTreeMap, BTreeSet}, mem, slice, }; +use merkle::AccessInfo; +use serde::{Serialize, ser::SerializeTuple}; + +use super::{ + EnrichedValue, EnrichedValueLinked, ManagerBase, ManagerRead, ManagerReadWrite, + ManagerSerialise, ManagerWrite, +}; + pub mod merkle; pub mod proof; pub mod tree; @@ -455,15 +457,17 @@ impl DynAccess { #[cfg(test)] mod tests { + use std::collections::VecDeque; + + use proptest::{array, prop_assert_eq, proptest}; + use tests::merkle::MerkleTree; + use super::merkle::MERKLE_LEAF_SIZE; use super::*; use crate::state_backend::{ Cells, CommitmentLayout, DynArray, DynCells, ProofLayout, Ref, layout::Array, owned_backend::Owned, }; - use proptest::{array, prop_assert_eq, proptest}; - use std::collections::VecDeque; - use tests::merkle::MerkleTree; const CELLS_SIZE: usize = 32; diff --git a/src/riscv/lib/src/state_backend/proof_backend/merkle.rs b/src/riscv/lib/src/state_backend/proof_backend/merkle.rs index 73a8887c01c1..af4106ca21d5 100644 --- a/src/riscv/lib/src/state_backend/proof_backend/merkle.rs +++ b/src/riscv/lib/src/state_backend/proof_backend/merkle.rs @@ -4,6 +4,8 @@ //! Merkle trees used for proof generation by the PVM +use std::{convert::Infallible, num::NonZeroUsize}; + use super::{ DynAccess, proof::{MerkleProof, MerkleProofLeaf}, @@ -12,7 +14,6 @@ use crate::state_backend::{ hash::{Hash, HashError}, proof_backend::tree::{ModifyResult, impl_modify_map_collect}, }; -use std::{convert::Infallible, num::NonZeroUsize}; // TODO RV-322: Choose optimal Merkleisation parameters for main memory. /// Size of the Merkle leaf used for Merkleising [`DynArrays`]. @@ -443,6 +444,10 @@ impl MerkleTree { #[cfg(test)] mod tests { + use std::io::Cursor; + + use proptest::prelude::*; + use super::{ AccessInfo, CompressedAccessInfo, CompressedMerkleTree, MERKLE_LEAF_SIZE, MerkleTree, chunks_to_writer, @@ -451,8 +456,6 @@ mod tests { hash::{Hash, HashError}, proof_backend::proof::{MerkleProof, MerkleProofLeaf}, }; - use proptest::prelude::*; - use std::io::Cursor; impl CompressedMerkleTree { /// Get the root hash of a compressed Merkle tree diff --git a/src/riscv/lib/src/state_backend/proof_backend/proof.rs b/src/riscv/lib/src/state_backend/proof_backend/proof.rs index 8542c0b9ec38..a882fe2ca342 100644 --- a/src/riscv/lib/src/state_backend/proof_backend/proof.rs +++ b/src/riscv/lib/src/state_backend/proof_backend/proof.rs @@ -14,12 +14,13 @@ //! //! - Convert [`super::merkle::MerkleTree`] to [`MerkleProof`] +use itertools::Itertools; + use super::tree::{ModifyResult, Tree, impl_modify_map_collect}; use crate::{ state_backend::hash::Hash, storage::{DIGEST_SIZE, HashError}, }; -use itertools::Itertools; /// Structure of a proof transitioning from state A to state B. /// @@ -357,6 +358,9 @@ pub fn deserialise_proof( #[cfg(test)] mod tests { + use proptest::proptest; + use rand::{Fill, thread_rng}; + use super::{ DeserialiseError, MerkleProof, MerkleProofLeaf, Shape, TAG_BLIND, TAG_NODE, TAG_READ, serialise_proof, @@ -365,8 +369,6 @@ mod tests { state_backend::proof_backend::proof::{Proof, deserialise_proof}, storage::{DIGEST_SIZE, Hash}, }; - use proptest::proptest; - use rand::{Fill, thread_rng}; /// Utility struct that computes the bounds of a [`MerkleProof`] serialisation /// based on total number of nodes in the tree and total size of raw data in the leafs. diff --git a/src/riscv/lib/src/state_backend/region.rs b/src/riscv/lib/src/state_backend/region.rs index 66f8da9693ae..94121ffc0f0f 100644 --- a/src/riscv/lib/src/state_backend/region.rs +++ b/src/riscv/lib/src/state_backend/region.rs @@ -3,6 +3,8 @@ // // SPDX-License-Identifier: MIT +use std::borrow::Borrow; + use super::{ Elem, EnrichedValue, EnrichedValueLinked, FnManager, ManagerBase, ManagerClone, ManagerDeserialise, ManagerRead, ManagerReadWrite, ManagerSerialise, ManagerWrite, Ref, @@ -11,7 +13,6 @@ use super::{ merkle::{AccessInfo, AccessInfoAggregatable}, }, }; -use std::borrow::Borrow; /// Link a stored value directly with a derived value - /// that would either be expensive to compute each time, or cannot @@ -569,6 +570,8 @@ impl Clone for DynCells { #[cfg(test)] pub(crate) mod tests { + use serde::ser::SerializeTuple; + use crate::{ backend_test, default::ConstDefault, @@ -577,7 +580,6 @@ pub(crate) mod tests { layout::{Atom, Layout}, }, }; - use serde::ser::SerializeTuple; /// Dummy type that helps us implement custom normalisation via [Elem] #[repr(packed)] diff --git a/src/riscv/lib/src/state_backend/verify_backend.rs b/src/riscv/lib/src/state_backend/verify_backend.rs index 0eba9b71fd96..41204f331423 100644 --- a/src/riscv/lib/src/state_backend/verify_backend.rs +++ b/src/riscv/lib/src/state_backend/verify_backend.rs @@ -2,11 +2,6 @@ // // SPDX-License-Identifier: MIT -use super::{ - Cell, EnrichedValue, ManagerBase, ManagerClone, ManagerRead, ManagerReadWrite, ManagerWrite, -}; -use crate::state_backend::{owned_backend::Owned, proof_backend::merkle::MERKLE_LEAF_SIZE}; -use range_collections::RangeSet2; use std::{ array, collections::BTreeMap, @@ -16,6 +11,13 @@ use std::{ slice, }; +use range_collections::RangeSet2; + +use super::{ + Cell, EnrichedValue, ManagerBase, ManagerClone, ManagerRead, ManagerReadWrite, ManagerWrite, +}; +use crate::state_backend::{owned_backend::Owned, proof_backend::merkle::MERKLE_LEAF_SIZE}; + /// Panic payload that is raised when a value isn't present in a part of the Verifier backend. #[derive(Copy, Clone, Debug, Eq, PartialEq)] struct NotFound; diff --git a/src/riscv/lib/src/stepper.rs b/src/riscv/lib/src/stepper.rs index 1f667a8bec11..5f04237369ee 100644 --- a/src/riscv/lib/src/stepper.rs +++ b/src/riscv/lib/src/stepper.rs @@ -2,11 +2,12 @@ // // SPDX-License-Identifier: MIT +use std::ops::{AddAssign, Bound}; + use crate::{ machine_state::{CacheLayouts, MachineCoreState, main_memory::MainMemoryLayout}, state_backend::{ManagerBase, ManagerRead}, }; -use std::ops::{AddAssign, Bound}; pub mod pvm; pub mod test; diff --git a/src/riscv/lib/src/stepper/pvm.rs b/src/riscv/lib/src/stepper/pvm.rs index 8d6e6985c415..d973dcd6a35c 100644 --- a/src/riscv/lib/src/stepper/pvm.rs +++ b/src/riscv/lib/src/stepper/pvm.rs @@ -5,6 +5,12 @@ mod reveals; +use std::ops::Bound; + +use reveals::RevealRequestResponseMap; +use serde::{Serialize, de::DeserializeOwned}; +use tezos_smart_rollup_utils::inbox::Inbox; + use super::{Stepper, StepperStatus}; use crate::machine_state::block_cache::bcall::{Block, Interpreted, InterpretedBlockBuilder}; use crate::state_backend::{ManagerBase, ManagerReadWrite}; @@ -26,10 +32,6 @@ use crate::{ }, storage::binary, }; -use reveals::RevealRequestResponseMap; -use serde::{Serialize, de::DeserializeOwned}; -use std::ops::Bound; -use tezos_smart_rollup_utils::inbox::Inbox; /// Error during PVM stepping #[derive(Debug, derive_more::From, thiserror::Error, derive_more::Display)] diff --git a/src/riscv/lib/src/stepper/pvm/reveals.rs b/src/riscv/lib/src/stepper/pvm/reveals.rs index b2baa0c3ec36..15d3b8bd8e5f 100644 --- a/src/riscv/lib/src/stepper/pvm/reveals.rs +++ b/src/riscv/lib/src/stepper/pvm/reveals.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT use std::{collections::HashMap, sync::Arc}; + use tezos_smart_rollup_constants::core::{METADATA_LENGTH, ROLLUP_ADDRESS_LENGTH}; type ResponseFn = Arc Result, std::io::Error>>; diff --git a/src/riscv/lib/src/stepper/test.rs b/src/riscv/lib/src/stepper/test.rs index 6b26b0e4fe19..0004414a29a6 100644 --- a/src/riscv/lib/src/stepper/test.rs +++ b/src/riscv/lib/src/stepper/test.rs @@ -4,6 +4,11 @@ mod posix; +use std::{collections::BTreeMap, ops::Bound}; + +use derive_more::{Error, From}; +use posix::{PosixState, PosixStateLayout}; + use super::{StepResult, Stepper, StepperStatus}; use crate::{ kernel_loader, @@ -18,9 +23,6 @@ use crate::{ state_backend::owned_backend::Owned, traps::EnvironException, }; -use derive_more::{Error, From}; -use posix::{PosixState, PosixStateLayout}; -use std::{collections::BTreeMap, ops::Bound}; #[derive(Clone, Debug)] pub enum TestStepperResult { diff --git a/src/riscv/lib/src/storage.rs b/src/riscv/lib/src/storage.rs index 132e3f163dc7..9ed0ac2c88b8 100644 --- a/src/riscv/lib/src/storage.rs +++ b/src/riscv/lib/src/storage.rs @@ -6,13 +6,15 @@ pub mod binary; mod chunked_io; -pub use crate::state_backend::hash::{DIGEST_SIZE, Hash, HashError}; use std::{ io::{self, Write}, path::{Path, PathBuf}, }; + use thiserror::Error; +pub use crate::state_backend::hash::{DIGEST_SIZE, Hash, HashError}; + const CHUNK_SIZE: usize = 4096; #[derive(Error, Debug)] diff --git a/src/riscv/lib/src/storage/binary.rs b/src/riscv/lib/src/storage/binary.rs index 06ddb0923172..564e6acd3657 100644 --- a/src/riscv/lib/src/storage/binary.rs +++ b/src/riscv/lib/src/storage/binary.rs @@ -2,9 +2,10 @@ // // SPDX-License-Identifier: MIT -use bincode::{DefaultOptions, Options}; use std::io::{Read, Write}; +use bincode::{DefaultOptions, Options}; + /// Constructs the default options for bincode serialisation and deserialisation. #[inline(always)] pub(crate) fn bincode_default() -> impl Options { diff --git a/src/riscv/lib/src/storage/chunked_io.rs b/src/riscv/lib/src/storage/chunked_io.rs index ba5a297d5e06..4637f3c25d54 100644 --- a/src/riscv/lib/src/storage/chunked_io.rs +++ b/src/riscv/lib/src/storage/chunked_io.rs @@ -2,13 +2,14 @@ // // SPDX-License-Identifier: MIT -use super::{CHUNK_SIZE, Hash, StorageError, Store, binary}; use std::{ cmp, collections::VecDeque, io::{self, Cursor}, }; +use super::{CHUNK_SIZE, Hash, StorageError, Store, binary}; + /// Simple writer that stores data in chunks of size [`CHUNK_SIZE`] pub struct ChunkWriter<'a> { store: &'a mut Store, diff --git a/src/riscv/lib/src/traps.rs b/src/riscv/lib/src/traps.rs index 03f14ed2cc5f..46d47185c8fb 100644 --- a/src/riscv/lib/src/traps.rs +++ b/src/riscv/lib/src/traps.rs @@ -21,10 +21,12 @@ //! A trap which causes the execution environment to halt the machine. //! -use crate::machine_state::{csregisters::CSRRepr, main_memory::Address}; use std::fmt::Formatter; + use tezos_smart_rollup_constants::riscv::SbiError; +use crate::machine_state::{csregisters::CSRRepr, main_memory::Address}; + /// RISC-V Exceptions (also known as synchronous exceptions) #[derive(PartialEq, Eq, thiserror::Error, strum::Display, Debug, Clone, Copy)] pub enum EnvironException { diff --git a/src/riscv/lib/tests/common/mod.rs b/src/riscv/lib/tests/common/mod.rs index 646911fd01be..923ddc2b5715 100644 --- a/src/riscv/lib/tests/common/mod.rs +++ b/src/riscv/lib/tests/common/mod.rs @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: MIT +use std::fs; + use octez_riscv::{ machine_state::{ DefaultCacheLayouts, block_cache::bcall::InterpretedBlockBuilder, main_memory::M64M, @@ -10,7 +12,6 @@ use octez_riscv::{ stepper::pvm::PvmStepper, }; use rand::{Rng, seq::SliceRandom}; -use std::fs; use tezos_smart_rollup_utils::inbox::InboxBuilder; pub fn make_stepper_factory() -> impl Fn() -> PvmStepper<'static, M64M, DefaultCacheLayouts> { diff --git a/src/riscv/lib/tests/test_determinism.rs b/src/riscv/lib/tests/test_determinism.rs index da10c8c5b44f..1f95ea1a9914 100644 --- a/src/riscv/lib/tests/test_determinism.rs +++ b/src/riscv/lib/tests/test_determinism.rs @@ -4,6 +4,8 @@ mod common; +use std::ops::Bound; + use common::*; use octez_riscv::{ machine_state::{ @@ -13,7 +15,6 @@ use octez_riscv::{ state_backend::{RefOwnedAlloc, hash}, stepper::{Stepper, StepperStatus, pvm::PvmStepper}, }; -use std::ops::Bound; #[test] #[ignore] diff --git a/src/riscv/lib/tests/test_proofs.rs b/src/riscv/lib/tests/test_proofs.rs index acd629cb9c24..6665e0efdf5a 100644 --- a/src/riscv/lib/tests/test_proofs.rs +++ b/src/riscv/lib/tests/test_proofs.rs @@ -5,6 +5,8 @@ mod common; +use std::ops::Bound; + use common::*; use octez_riscv::{ machine_state::{DefaultCacheLayouts, main_memory::M64M}, @@ -12,7 +14,6 @@ use octez_riscv::{ stepper::{Stepper, StepperStatus, pvm::PvmStepper}, }; use rand::Rng; -use std::ops::Bound; #[test] #[ignore] diff --git a/src/riscv/lib/tests/test_regression.rs b/src/riscv/lib/tests/test_regression.rs index 50fde3192237..f965e9cafeeb 100644 --- a/src/riscv/lib/tests/test_regression.rs +++ b/src/riscv/lib/tests/test_regression.rs @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: MIT +use std::{fs, io::Write, ops::Bound}; + use octez_riscv::{ machine_state::{ DefaultCacheLayouts, block_cache::bcall::InterpretedBlockBuilder, main_memory::M64M, @@ -10,7 +12,6 @@ use octez_riscv::{ state_backend::owned_backend::Owned, stepper::{Stepper, StepperStatus, pvm::PvmStepper}, }; -use std::{fs, io::Write, ops::Bound}; use tezos_smart_rollup_utils::inbox::InboxBuilder; const GOLDEN_DIR: &str = "tests/expected/jstz"; diff --git a/src/riscv/lib/tests/test_suite_interpreter.rs b/src/riscv/lib/tests/test_suite_interpreter.rs index f0eecce37bba..6781887fcf87 100644 --- a/src/riscv/lib/tests/test_suite_interpreter.rs +++ b/src/riscv/lib/tests/test_suite_interpreter.rs @@ -3,6 +3,8 @@ // // SPDX-License-Identifier: MIT +use std::{fs, io::Write, ops::Bound}; + use goldenfile::Mint; use octez_riscv::{ machine_state::{ @@ -17,7 +19,6 @@ use octez_riscv::{ test::{TestStepper, TestStepperResult::*}, }, }; -use std::{fs, io::Write, ops::Bound}; const TESTS_DIR: &str = "../../../tezt/tests/riscv-tests/generated"; const GOLDEN_DIR: &str = "tests/expected"; diff --git a/src/riscv/lib/tests/test_suite_parser.rs b/src/riscv/lib/tests/test_suite_parser.rs index bec5fd9677a0..0ab8e3227465 100644 --- a/src/riscv/lib/tests/test_suite_parser.rs +++ b/src/riscv/lib/tests/test_suite_parser.rs @@ -3,9 +3,10 @@ // SPDX-License-Identifier: MIT use core::panic; +use std::{fs::DirEntry, process::Command}; + use lazy_static::lazy_static; use octez_riscv::parser::{instruction::Instr, parse_block}; -use std::{fs::DirEntry, process::Command}; fn compute_offset(src: &str, dest: &str) -> i64 { i64::from_str_radix(dest, 16).unwrap() - i64::from_str_radix(src, 16).unwrap() diff --git a/src/riscv/sandbox/src/cli.rs b/src/riscv/sandbox/src/cli.rs index 84ba06e01df8..d4ebf180a445 100644 --- a/src/riscv/sandbox/src/cli.rs +++ b/src/riscv/sandbox/src/cli.rs @@ -3,9 +3,10 @@ // // SPDX-License-Identifier: MIT -use clap::{Parser, Subcommand, ValueEnum}; use std::{error::Error, path::Path}; +use clap::{Parser, Subcommand, ValueEnum}; + #[derive(Debug, Clone, Subcommand)] pub enum Mode { /// Run a program using the RISC-V interpreter diff --git a/src/riscv/sandbox/src/commands/bench.rs b/src/riscv/sandbox/src/commands/bench.rs index 1ee2a437e54f..f9ecd3d2b8ac 100644 --- a/src/riscv/sandbox/src/commands/bench.rs +++ b/src/riscv/sandbox/src/commands/bench.rs @@ -3,15 +3,16 @@ // // SPDX-License-Identifier: MIT +use std::{ + error::Error, + fs::{self, File}, +}; + pub(crate) use crate::commands::bench::stats::{BenchStats, NamedStats}; use crate::{ cli::{self, BenchOptions, BenchRunOptions}, table, }; -use std::{ - error::Error, - fs::{self, File}, -}; pub mod commands; mod data; diff --git a/src/riscv/sandbox/src/commands/bench/commands/compare.rs b/src/riscv/sandbox/src/commands/bench/commands/compare.rs index b9be3a8bc3ee..989e5a7b3180 100644 --- a/src/riscv/sandbox/src/commands/bench/commands/compare.rs +++ b/src/riscv/sandbox/src/commands/bench/commands/compare.rs @@ -1,7 +1,9 @@ -use crate::{cli::BenchCompareOptions, commands::bench::load_from_file, table}; -use itertools::Itertools; use std::error::Error; +use itertools::Itertools; + +use crate::{cli::BenchCompareOptions, commands::bench::load_from_file, table}; + pub fn compare(opts: BenchCompareOptions) -> Result<(), Box> { let files = opts.comparison_files()?; diff --git a/src/riscv/sandbox/src/commands/bench/commands/parser.rs b/src/riscv/sandbox/src/commands/bench/commands/parser.rs index 1d2850f67144..622686eeb706 100644 --- a/src/riscv/sandbox/src/commands/bench/commands/parser.rs +++ b/src/riscv/sandbox/src/commands/bench/commands/parser.rs @@ -2,11 +2,13 @@ // // SPDX-License-Identifier: MIT -use crate::cli::BenchParserRunOptions; -use octez_riscv::parser::parse_block; use std::error::Error; use std::ops::Div; +use octez_riscv::parser::parse_block; + +use crate::cli::BenchParserRunOptions; + pub fn bench(opts: BenchParserRunOptions) -> Result<(), Box> { let runs = opts.repeat; let files = opts.inputs; diff --git a/src/riscv/sandbox/src/commands/bench/commands/run.rs b/src/riscv/sandbox/src/commands/bench/commands/run.rs index 3a29b7c341e5..bcba63b4f569 100644 --- a/src/riscv/sandbox/src/commands/bench/commands/run.rs +++ b/src/riscv/sandbox/src/commands/bench/commands/run.rs @@ -2,6 +2,22 @@ // // SPDX-License-Identifier: MIT +use std::{ + collections::HashSet, + error::Error, + fs, + ops::Bound, + path::{Path, PathBuf}, +}; + +use enum_tag::EnumTag; +use octez_riscv::{ + machine_state::{AccessType, main_memory::Address}, + parser::{instruction::Instr, parse}, + state_backend::ManagerRead, + stepper::{StepResult, Stepper, StepperStatus}, +}; + use crate::{ cli::{BenchMode, BenchRunOptions}, commands::{ @@ -14,20 +30,6 @@ use crate::{ }, format_status, }; -use enum_tag::EnumTag; -use octez_riscv::{ - machine_state::{AccessType, main_memory::Address}, - parser::{instruction::Instr, parse}, - state_backend::ManagerRead, - stepper::{StepResult, Stepper, StepperStatus}, -}; -use std::{ - collections::HashSet, - error::Error, - fs, - ops::Bound, - path::{Path, PathBuf}, -}; /// Helper function to look in the [`Stepper`] to peek for the current [`Instr`] /// Assumes the program counter will be a multiple of 2. diff --git a/src/riscv/sandbox/src/commands/bench/data.rs b/src/riscv/sandbox/src/commands/bench/data.rs index 538d3accf4ac..fcd67fbdb2c4 100644 --- a/src/riscv/sandbox/src/commands/bench/data.rs +++ b/src/riscv/sandbox/src/commands/bench/data.rs @@ -3,14 +3,15 @@ // SPDX-License-Identifier: MIT use core::fmt; -use enum_tag::EnumTag; -use octez_riscv::{parser::instruction::Instr, stepper::StepperStatus}; use std::{ collections::HashMap, fmt::{Debug, Display}, time::Duration, }; +use enum_tag::EnumTag; +use octez_riscv::{parser::instruction::Instr, stepper::StepperStatus}; + pub(super) type InstructionData = HashMap>; /// Underlying data for a benchmark run. diff --git a/src/riscv/sandbox/src/commands/bench/stats.rs b/src/riscv/sandbox/src/commands/bench/stats.rs index 482b2f5a52a7..706af7b4066c 100644 --- a/src/riscv/sandbox/src/commands/bench/stats.rs +++ b/src/riscv/sandbox/src/commands/bench/stats.rs @@ -2,13 +2,15 @@ // // SPDX-License-Identifier: MIT -use super::data::{BenchData, InstructionData}; -use crate::{format_status, table::utils::thousand_format}; use core::fmt; +use std::time::Duration; + use itertools::Itertools; use meansd::MeanSD; use serde::{Deserialize, Serialize}; -use std::time::Duration; + +use super::data::{BenchData, InstructionData}; +use crate::{format_status, table::utils::thousand_format}; /// Serializable data for instruction-level statistics #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/riscv/sandbox/src/commands/debug.rs b/src/riscv/sandbox/src/commands/debug.rs index 4943db6cd7d1..aaf5028f798c 100644 --- a/src/riscv/sandbox/src/commands/debug.rs +++ b/src/riscv/sandbox/src/commands/debug.rs @@ -3,15 +3,17 @@ // // SPDX-License-Identifier: MIT -use crate::{cli::DebugOptions, posix_exit_mode}; +use std::{error::Error, fs}; + use octez_riscv::{ machine_state::main_memory::M1G, stepper::{pvm::PvmStepper, test::TestStepper}, }; -use std::{error::Error, fs}; use tezos_smart_rollup::utils::inbox::InboxBuilder; use tezos_smart_rollup_encoding::smart_rollup::SmartRollupAddress; +use crate::{cli::DebugOptions, posix_exit_mode}; + mod debugger_app; mod errors; mod tui; diff --git a/src/riscv/sandbox/src/commands/debug/debugger_app.rs b/src/riscv/sandbox/src/commands/debug/debugger_app.rs index 0e5ac6ae516f..935deb21219f 100644 --- a/src/riscv/sandbox/src/commands/debug/debugger_app.rs +++ b/src/riscv/sandbox/src/commands/debug/debugger_app.rs @@ -3,8 +3,12 @@ // // SPDX-License-Identifier: MIT -use super::{errors, tui}; -use crate::commands::debug::DebugOptions; +use std::{ + borrow::Cow, + collections::{BTreeMap, HashMap, HashSet}, + ops::Bound, +}; + use color_eyre::Result; use crossterm::event::{self, Event, KeyCode, KeyEventKind}; use goblin::{elf, elf::Elf, elf::header::ET_DYN}; @@ -25,12 +29,10 @@ use octez_riscv::{ }; use ratatui::{prelude::*, style::palette::tailwind, widgets::*}; use rustc_demangle::demangle; -use std::{ - borrow::Cow, - collections::{BTreeMap, HashMap, HashSet}, - ops::Bound, -}; use tezos_smart_rollup::utils::inbox::Inbox; + +use super::{errors, tui}; +use crate::commands::debug::DebugOptions; mod render; mod updates; @@ -491,10 +493,12 @@ impl<'a> ProgramView<'a> { #[cfg(test)] mod test { + use std::fs; + + use octez_riscv::machine_state::main_memory::M1G; + use super::*; use crate::{ExitMode, posix_exit_mode}; - use octez_riscv::machine_state::main_memory::M1G; - use std::fs; #[test] fn test_max_steps_respected() { diff --git a/src/riscv/sandbox/src/commands/debug/debugger_app/render.rs b/src/riscv/sandbox/src/commands/debug/debugger_app/render.rs index 27ef1fe899e6..0d5977692a4e 100644 --- a/src/riscv/sandbox/src/commands/debug/debugger_app/render.rs +++ b/src/riscv/sandbox/src/commands/debug/debugger_app/render.rs @@ -3,10 +3,6 @@ // // SPDX-License-Identifier: MIT -use super::{ - BLUE, DebuggerApp, GRAY, GREEN, Instruction, NEXT_STYLE_FG, ORANGE, RED, SATPModeState, - SELECTED_STYLE_FG, TranslationState, YELLOW, -}; use octez_riscv::{ machine_state::{ csregisters::{ @@ -26,6 +22,11 @@ use ratatui::{ widgets::{block::*, *}, }; +use super::{ + BLUE, DebuggerApp, GRAY, GREEN, Instruction, NEXT_STYLE_FG, ORANGE, RED, SATPModeState, + SELECTED_STYLE_FG, TranslationState, YELLOW, +}; + macro_rules! xregister_line { ($self: ident, $reg: ident) => { Line::from(vec![ diff --git a/src/riscv/sandbox/src/commands/debug/debugger_app/updates.rs b/src/riscv/sandbox/src/commands/debug/debugger_app/updates.rs index 0a79bc3d961f..4a6e6032dc79 100644 --- a/src/riscv/sandbox/src/commands/debug/debugger_app/updates.rs +++ b/src/riscv/sandbox/src/commands/debug/debugger_app/updates.rs @@ -3,7 +3,8 @@ // // SPDX-License-Identifier: MIT -use super::{DebuggerApp, Instruction, PC_CONTEXT}; +use std::{borrow::Cow, collections::HashMap, ops::Range}; + use octez_riscv::{ bits::Bits64, machine_state::{ @@ -15,7 +16,8 @@ use octez_riscv::{ state_backend::{ManagerRead, ManagerReadWrite}, stepper::{Stepper, StepperStatus}, }; -use std::{borrow::Cow, collections::HashMap, ops::Range}; + +use super::{DebuggerApp, Instruction, PC_CONTEXT}; impl<'a, S> DebuggerApp<'a, S> where diff --git a/src/riscv/sandbox/src/commands/debug/errors.rs b/src/riscv/sandbox/src/commands/debug/errors.rs index f632dad11ecc..7d2f4fd9369e 100644 --- a/src/riscv/sandbox/src/commands/debug/errors.rs +++ b/src/riscv/sandbox/src/commands/debug/errors.rs @@ -6,10 +6,12 @@ // Ratatui boilerplate // https://github.com/ratatui-org/ratatui-website/blob/main/code/counter-app-error-handling/src/errors.rs -use super::tui; -use color_eyre::{config::HookBuilder, eyre}; use std::panic; +use color_eyre::{config::HookBuilder, eyre}; + +use super::tui; + /// This replaces the standard color_eyre panic and error hooks with hooks that /// restore the terminal before printing the panic or error. pub fn install_hooks() -> color_eyre::Result<()> { diff --git a/src/riscv/sandbox/src/commands/gdb.rs b/src/riscv/sandbox/src/commands/gdb.rs index ed5e9cf952ee..572107b80894 100644 --- a/src/riscv/sandbox/src/commands/gdb.rs +++ b/src/riscv/sandbox/src/commands/gdb.rs @@ -6,7 +6,13 @@ //! //! This is achieved by implementing 'just enough' of the gdb server protocol to be useful. -use crate::cli::GdbServerOptions; +use std::collections::HashSet; +use std::error::Error; +use std::marker::PhantomData; +use std::net::{TcpListener, TcpStream}; +use std::ops::Bound; +use std::{fs, io}; + use gdbstub::arch::Arch; use gdbstub::common::Signal; use gdbstub::conn::{Connection, ConnectionExt}; @@ -30,14 +36,10 @@ use octez_riscv::{ machine_state::main_memory::{M1G, OutOfBounds}, state_backend::FnManagerIdent, }; -use std::collections::HashSet; -use std::error::Error; -use std::marker::PhantomData; -use std::net::{TcpListener, TcpStream}; -use std::ops::Bound; -use std::{fs, io}; use tezos_smart_rollup::utils::inbox::InboxBuilder; +use crate::cli::GdbServerOptions; + /// Run a gdb server that can be used for debugging RISC-V programs. pub fn gdb_server(opts: GdbServerOptions) -> Result<(), Box> { let fname = fs::canonicalize(opts.input).map_err(|_| "Invalid program path")?; diff --git a/src/riscv/sandbox/src/commands/run.rs b/src/riscv/sandbox/src/commands/run.rs index a00fefae0dc2..3ae05c81ccb9 100644 --- a/src/riscv/sandbox/src/commands/run.rs +++ b/src/riscv/sandbox/src/commands/run.rs @@ -3,10 +3,8 @@ // // SPDX-License-Identifier: MIT -use crate::{ - cli::{CommonOptions, RunOptions}, - posix_exit_mode, -}; +use std::{error::Error, fs, io::Write, ops::Bound}; + use octez_riscv::{ machine_state::{DefaultCacheLayouts, main_memory::M1G}, machine_state::{ @@ -17,10 +15,14 @@ use octez_riscv::{ state_backend::owned_backend::Owned, stepper::{StepResult, Stepper, StepperStatus, pvm::PvmStepper, test::TestStepper}, }; -use std::{error::Error, fs, io::Write, ops::Bound}; use tezos_smart_rollup::utils::{console::Console, inbox::InboxBuilder}; use tezos_smart_rollup_encoding::smart_rollup::SmartRollupAddress; +use crate::{ + cli::{CommonOptions, RunOptions}, + posix_exit_mode, +}; + pub fn run(opts: RunOptions) -> Result<(), Box> { let program = fs::read(&opts.input)?; let initrd = opts.initrd.as_ref().map(fs::read).transpose()?; diff --git a/src/riscv/sandbox/src/main.rs b/src/riscv/sandbox/src/main.rs index 9ccc97c777dc..8d4c74655092 100644 --- a/src/riscv/sandbox/src/main.rs +++ b/src/riscv/sandbox/src/main.rs @@ -3,9 +3,10 @@ // // SPDX-License-Identifier: MIT +use std::error::Error; + use cli::ExitMode; use octez_riscv::{machine_state::mode::Mode, stepper::StepperStatus}; -use std::error::Error; mod cli; mod commands; diff --git a/src/riscv/sandbox/src/table.rs b/src/riscv/sandbox/src/table.rs index 8bd4dc1f4fab..e9d908dbbbee 100644 --- a/src/riscv/sandbox/src/table.rs +++ b/src/riscv/sandbox/src/table.rs @@ -2,6 +2,12 @@ // // SPDX-License-Identifier: MIT +use comfy_table::{ + Attribute, Cell, CellAlignment, Color, ContentArrangement, Table, + modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, +}; +use itertools::Itertools; + use self::utils::{ NamedBenchInstrStats, prepend_cell, tableify_bench_stats, thousand_format, vec_cell_factory_ref, }; @@ -10,11 +16,6 @@ use crate::{ commands::bench::{BenchStats, NamedStats}, table::utils::format_opt_duration, }; -use comfy_table::{ - Attribute, Cell, CellAlignment, Color, ContentArrangement, Table, - modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, -}; -use itertools::Itertools; pub mod utils; diff --git a/src/riscv/sandbox/src/table/utils.rs b/src/riscv/sandbox/src/table/utils.rs index f267e47929e7..8d83b552128d 100644 --- a/src/riscv/sandbox/src/table/utils.rs +++ b/src/riscv/sandbox/src/table/utils.rs @@ -2,15 +2,17 @@ // // SPDX-License-Identifier: MIT -use crate::commands::bench::{BenchStats, NamedStats}; -use comfy_table::Cell; -use itertools::Itertools; -use numfmt::{Formatter, Numeric}; use std::{ collections::{BTreeSet, HashMap}, time::Duration, }; +use comfy_table::Cell; +use itertools::Itertools; +use numfmt::{Formatter, Numeric}; + +use crate::commands::bench::{BenchStats, NamedStats}; + /// Produce a string by formatting the number with the given `separator` and `precision` pub fn thousand_format(content: N, num_decimals: u8) -> String { let mut fmt = Formatter::new() diff --git a/src/riscv/tests/inline_asm/src/main.rs b/src/riscv/tests/inline_asm/src/main.rs index d4dc2e7f4937..54128983e398 100644 --- a/src/riscv/tests/inline_asm/src/main.rs +++ b/src/riscv/tests/inline_asm/src/main.rs @@ -9,6 +9,7 @@ mod syscalls; use core::arch::asm; use core::panic::PanicInfo; + use syscalls::exit; #[no_mangle] -- GitLab