Skip to content

Commit ac106d6

Browse files
Simplify virtual store
This patch uses the simplified Store trait to also simplify the store implementation used by the virt module: Instead of using references to static storages and filesystems protected by a mutex, we can now just use separate non-static instances for every test. The only downside is that we can no longer provide raw access to the IFS via the StoreProvider::ifs function, so we cannot run the provisioner app with this platform implementation. But I think this is an acceptable tradeoff.
1 parent 5003249 commit ac106d6

File tree

9 files changed

+132
-231
lines changed

9 files changed

+132
-231
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ delog = "0.1.0"
6262
cbor-smol = { version = "0.5", features = ["heapless-bytes-v0-3"] }
6363
heapless-bytes.workspace = true
6464
interchange = "0.3.0"
65-
littlefs2 = "0.6.0"
65+
littlefs2 = "0.6.1"
6666
littlefs2-core = { workspace = true, features = ["heapless-bytes03"] }
6767
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
6868
salty = { version = "0.3.0", features = ["cose"] }
@@ -86,7 +86,7 @@ serde-extensions = ["trussed-core/serde-extensions"]
8686
std = []
8787
verbose-tests = ["littlefs2/ll-assertions"]
8888
verbose-lfs = ["littlefs2/ll-assertions", "littlefs2/ll-trace"]
89-
virt = ["std"]
89+
virt = ["std", "littlefs2/alloc"]
9090

9191
log-all = []
9292
log-none = []

derive/examples/dispatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ fn main() {
2424
backend::BackendId,
2525
client::CryptoClient,
2626
try_syscall,
27-
virt::{self, Ram},
27+
virt::{self, StoreConfig},
2828
Error,
2929
};
3030

3131
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) {
32-
virt::with_platform(Ram::default(), |platform| {
32+
virt::with_platform(StoreConfig::ram(), |platform| {
3333
platform.run_client_with_backends(
3434
"test",
3535
Dispatch::default(),

derive/examples/extension-dispatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ fn main() {
139139
use trussed::{
140140
backend::BackendId,
141141
try_syscall,
142-
virt::{self, Ram},
142+
virt::{self, StoreConfig},
143143
};
144144

145145
fn run(backends: &'static [BackendId<Backend>], expected: Option<Error>) {
146-
virt::with_platform(Ram::default(), |platform| {
146+
virt::with_platform(StoreConfig::ram(), |platform| {
147147
platform.run_client_with_backends(
148148
"test",
149149
Dispatch::default(),

src/virt.rs

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ mod ui;
88

99
use std::{
1010
iter,
11-
path::PathBuf,
12-
sync::{
13-
mpsc::{self, Receiver, Sender},
14-
Mutex,
15-
},
11+
sync::mpsc::{self, Receiver, Sender},
1612
thread,
1713
};
1814

@@ -28,95 +24,54 @@ use crate::{
2824
ClientImplementation,
2925
};
3026

31-
pub use store::{Filesystem, Ram, StoreProvider};
27+
pub use store::{StorageConfig, StoreConfig};
3228
pub use ui::UserInterface;
3329

3430
pub type Client<'a, D = CoreOnly> = ClientImplementation<'a, Syscall, D>;
3531

36-
// We need this mutex to make sure that:
37-
// - TrussedInterchange is not used concurrently (panics if violated)
38-
// - the Store is not used concurrently
39-
static MUTEX: Mutex<()> = Mutex::new(());
40-
41-
pub fn with_platform<S, R, F>(store: S, f: F) -> R
32+
pub fn with_platform<R, F>(mut store: StoreConfig, f: F) -> R
4233
where
43-
S: StoreProvider,
44-
F: FnOnce(Platform<S>) -> R,
34+
F: FnOnce(Platform<'_>) -> R,
4535
{
46-
let _guard = MUTEX.lock().unwrap_or_else(|err| err.into_inner());
47-
unsafe {
48-
store.reset();
49-
}
50-
// causing a regression again
51-
// let rng = chacha20::ChaCha8Rng::from_rng(rand_core::OsRng).unwrap();
52-
let platform = Platform {
53-
rng: ChaCha8Rng::from_seed([42u8; 32]),
54-
_store: store,
55-
ui: UserInterface::new(),
56-
};
57-
f(platform)
36+
store.with_store(|store| {
37+
// causing a regression again
38+
// let rng = chacha20::ChaCha8Rng::from_rng(rand_core::OsRng).unwrap();
39+
let platform = Platform {
40+
rng: ChaCha8Rng::from_seed([42u8; 32]),
41+
store,
42+
ui: UserInterface::new(),
43+
};
44+
f(platform)
45+
})
5846
}
5947

60-
pub fn with_client<S, R, F>(store: S, client_id: &str, f: F) -> R
48+
pub fn with_client<R, F>(store: StoreConfig, client_id: &str, f: F) -> R
6149
where
62-
S: StoreProvider,
6350
F: FnOnce(Client) -> R,
6451
{
6552
with_platform(store, |platform| platform.run_client(client_id, f))
6653
}
6754

68-
pub fn with_fs_client<P, R, F>(internal: P, client_id: &str, f: F) -> R
69-
where
70-
P: Into<PathBuf>,
71-
F: FnOnce(Client) -> R,
72-
{
73-
with_client(Filesystem::new(internal), client_id, f)
74-
}
75-
76-
pub fn with_ram_client<R, F>(client_id: &str, f: F) -> R
77-
where
78-
F: FnOnce(Client) -> R,
79-
{
80-
with_client(Ram::default(), client_id, f)
81-
}
82-
83-
pub fn with_clients<S, R, F, const N: usize>(store: S, client_ids: [&str; N], f: F) -> R
84-
where
85-
S: StoreProvider,
86-
F: FnOnce([Client; N]) -> R,
87-
{
88-
with_platform(store, |platform| platform.run_clients(client_ids, f))
89-
}
90-
91-
pub fn with_fs_clients<P, R, F, const N: usize>(internal: P, client_ids: [&str; N], f: F) -> R
92-
where
93-
P: Into<PathBuf>,
94-
F: FnOnce([Client; N]) -> R,
95-
{
96-
with_clients(Filesystem::new(internal), client_ids, f)
97-
}
98-
99-
/// Run a function with multiple clients using the RAM for the filesystem.
100-
///
55+
/// Run a function with multiple clients.
10156
///
10257
/// Const generics are used to allow easy deconstruction in the callback arguments
10358
///
10459
/// ```rust
10560
///# use trussed::client::{Ed255, CryptoClient};
10661
///# use trussed::types::{Location, Mechanism};
10762
///# use trussed::syscall;
108-
///# use trussed::virt::with_ram_clients;
109-
/// with_ram_clients(["client1", "client2"], |[mut client1, mut client2]| {
63+
///# use trussed::virt::{with_clients, StoreConfig};
64+
/// with_clients(StoreConfig::ram(), ["client1", "client2"], |[mut client1, mut client2]| {
11065
/// let key = syscall!(client1.generate_ed255_private_key(Location::Internal)).key;
11166
/// // The clients are distinct
11267
/// assert!(!syscall!(client2.exists(Mechanism::Ed255, key)).exists);
11368
/// })
11469
/// ```
115-
pub fn with_ram_clients<R, F, const N: usize>(client_ids: [&str; N], f: F) -> R
70+
pub fn with_clients<R, F, const N: usize>(store: StoreConfig, client_ids: [&str; N], f: F) -> R
11671
where
11772
F: FnOnce([Client; N]) -> R,
11873
{
119-
with_clients(Ram::default(), client_ids, f)
74+
with_platform(store, |platform| platform.run_clients(client_ids, f))
12075
}
12176

12277
pub struct Syscall(Sender<()>);
@@ -184,13 +139,13 @@ impl<'a, I: 'static, C: Default> Runner<'a, I, C> {
184139
}
185140
}
186141

187-
pub struct Platform<S: StoreProvider> {
142+
pub struct Platform<'a> {
188143
rng: ChaCha8Rng,
189-
_store: S,
144+
store: store::Store<'a>,
190145
ui: UserInterface,
191146
}
192147

193-
impl<S: StoreProvider> Platform<S> {
148+
impl Platform<'_> {
194149
pub fn run_client<R>(self, client_id: &str, test: impl FnOnce(Client) -> R) -> R {
195150
self.run_client_with_backends(client_id, CoreOnly, &[], test)
196151
}
@@ -255,9 +210,9 @@ impl<S: StoreProvider> Platform<S> {
255210
}
256211
}
257212

258-
unsafe impl<S: StoreProvider> platform::Platform for Platform<S> {
213+
unsafe impl<'a> platform::Platform for Platform<'a> {
259214
type R = ChaCha8Rng;
260-
type S = S::Store;
215+
type S = store::Store<'a>;
261216
type UI = UserInterface;
262217

263218
fn user_interface(&mut self) -> &mut Self::UI {
@@ -269,6 +224,6 @@ unsafe impl<S: StoreProvider> platform::Platform for Platform<S> {
269224
}
270225

271226
fn store(&self) -> Self::S {
272-
unsafe { S::store() }
227+
self.store
273228
}
274229
}

0 commit comments

Comments
 (0)