From fdfa082c31c3e0b6da8ffc0ef32d7b7ea48b35cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 10 Feb 2023 14:47:40 +0100 Subject: [PATCH 1/8] Remove unused consts --- src/config.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 174293c2183..94eeed934ab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,18 +1,12 @@ #![allow(non_camel_case_types)] #![allow(clippy::upper_case_acronyms)] -use littlefs2::consts; - // TODO: this needs to be overridable. // Should we use the "config crate that can have a replacement patched in" idea? -pub type MAX_APPLICATION_NAME_LENGTH = consts::U256; pub const MAX_LONG_DATA_LENGTH: usize = 1024; pub const MAX_MESSAGE_LENGTH: usize = 1024; -pub type MAX_OBJECT_HANDLES = consts::U16; -pub type MAX_LABEL_LENGTH = consts::U256; pub const MAX_MEDIUM_DATA_LENGTH: usize = 256; -pub type MAX_PATH_LENGTH = consts::U256; cfg_if::cfg_if! { if #[cfg(test)] { pub const MAX_SERVICE_CLIENTS: usize = 6; From c8befcb97b3b0dd6bee70e5c9dbc8341930a0992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 10 Feb 2023 16:13:35 +0100 Subject: [PATCH 2/8] Remove unused patch section --- Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 83f8935910a..bf201dc4b40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,8 +117,6 @@ clients-11 = [] clients-12 = [] test-attestation-cert-ids = [] -# [patch.crates-io] -# interchange = { git = "https://github.com/trussed-dev/interchange", branch = "main" } [package.metadata.docs.rs] features = ["serde-extensions", "virt"] From 1e266065738fd68d78b34810b19fe10bac11f164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 10 Feb 2023 17:41:25 +0100 Subject: [PATCH 3/8] Let the runner define the interchange This allows removing the client-N feature flags --- Cargo.toml | 15 +----------- src/client.rs | 52 +++++++++++++++++++--------------------- src/client/mechanisms.rs | 24 +++++++++---------- src/config.rs | 30 +---------------------- src/pipe.rs | 19 ++++++--------- src/service.rs | 46 ++++++++++++++++++++++------------- 6 files changed, 76 insertions(+), 110 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bf201dc4b40..d629aa73416 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ once_cell = "1.13.0" # rand_core = { version = "0.5", features = ["getrandom"] } [features] -default = ["default-mechanisms", "clients-5"] +default = ["default-mechanisms"] serde-extensions = [] std = [] verbose-tests = ["littlefs2/ll-assertions"] @@ -103,19 +103,6 @@ tdes = ["des"] totp = ["sha-1"] trng = ["sha-1"] -clients-1 = [] -clients-2 = [] -clients-3 = [] -clients-4 = [] -clients-5 = [] -clients-6 = [] -clients-7 = [] -clients-8 = [] -clients-9 = [] -clients-10 = [] -clients-11 = [] -clients-12 = [] - test-attestation-cert-ids = [] [package.metadata.docs.rs] diff --git a/src/client.rs b/src/client.rs index c2f5d58b728..3b812737a4a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -80,7 +80,7 @@ use core::{marker::PhantomData, task::Poll}; use crate::api::*; use crate::backend::{BackendId, CoreOnly, Dispatch}; use crate::error::*; -use crate::pipe::{TrussedRequester, TRUSSED_INTERCHANGE}; +use crate::pipe::TrussedRequester; use crate::service::Service; use crate::types::*; @@ -107,7 +107,7 @@ pub trait Client: { } -impl Client for ClientImplementation {} +impl<'pipe, S: Syscall, E> Client for ClientImplementation<'pipe, S, E> {} /// Lowest level interface, use one of the higher level ones. pub trait PollClient { @@ -142,12 +142,12 @@ where } /// The client implementation client applications actually receive. -pub struct ClientImplementation { +pub struct ClientImplementation<'pipe, S, D = CoreOnly> { // raw: RawClient>, syscall: S, // RawClient: - pub(crate) interchange: TrussedRequester, + pub(crate) interchange: TrussedRequester<'pipe>, // pending: Option>, pending: Option, _marker: PhantomData, @@ -161,11 +161,11 @@ pub struct ClientImplementation { // } // } -impl ClientImplementation +impl<'pipe, S, E> ClientImplementation<'pipe, S, E> where S: Syscall, { - pub fn new(interchange: TrussedRequester, syscall: S) -> Self { + pub fn new(interchange: TrussedRequester<'pipe>, syscall: S) -> Self { Self { interchange, pending: None, @@ -175,7 +175,7 @@ where } } -impl PollClient for ClientImplementation +impl<'pipe, S, E> PollClient for ClientImplementation<'pipe, S, E> where S: Syscall, { @@ -229,12 +229,12 @@ where } } -impl CertificateClient for ClientImplementation {} -impl CryptoClient for ClientImplementation {} -impl CounterClient for ClientImplementation {} -impl FilesystemClient for ClientImplementation {} -impl ManagementClient for ClientImplementation {} -impl UiClient for ClientImplementation {} +impl<'pipe, S: Syscall, E> CertificateClient for ClientImplementation<'pipe, S, E> {} +impl<'pipe, S: Syscall, E> CryptoClient for ClientImplementation<'pipe, S, E> {} +impl<'pipe, S: Syscall, E> CounterClient for ClientImplementation<'pipe, S, E> {} +impl<'pipe, S: Syscall, E> FilesystemClient for ClientImplementation<'pipe, S, E> {} +impl<'pipe, S: Syscall, E> ManagementClient for ClientImplementation<'pipe, S, E> {} +impl<'pipe, S: Syscall, E> UiClient for ClientImplementation<'pipe, S, E> {} /// Read/Write + Delete certificates pub trait CertificateClient: PollClient { @@ -730,13 +730,11 @@ impl ClientBuilder { } } - fn create_endpoint( + fn create_endpoint<'pipe, P: Platform, const MAX_CLIENTS: usize>( self, - service: &mut Service, - ) -> Result { - let (requester, responder) = TRUSSED_INTERCHANGE - .claim() - .ok_or(Error::ClientCountExceeded)?; + service: &mut Service<'pipe, P, MAX_CLIENTS, D>, + ) -> Result, Error> { + let (requester, responder) = service.pipe().claim().ok_or(Error::ClientCountExceeded)?; service.add_endpoint(responder, self.id, self.backends)?; Ok(requester) } @@ -745,10 +743,10 @@ impl ClientBuilder { /// /// This allocates a [`TrussedInterchange`][`crate::pipe::TrussedInterchange`] and a /// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`]. - pub fn prepare( + pub fn prepare<'pipe, P: Platform, const MAX_CLIENTS: usize>( self, - service: &mut Service, - ) -> Result, Error> { + service: &mut Service<'pipe, P, MAX_CLIENTS, D>, + ) -> Result, Error> { self.create_endpoint(service) .map(|requester| PreparedClient::new(requester)) } @@ -759,13 +757,13 @@ impl ClientBuilder { /// This struct already has an allocated [`TrussedInterchange`][`crate::pipe::TrussedInterchange`] and /// [`ServiceEndpoint`][`crate::service::ServiceEndpoint`] but still needs a [`Syscall`][] /// implementation. -pub struct PreparedClient { - requester: TrussedRequester, +pub struct PreparedClient<'pipe, D> { + requester: TrussedRequester<'pipe>, _marker: PhantomData, } -impl PreparedClient { - fn new(requester: TrussedRequester) -> Self { +impl<'pipe, D> PreparedClient<'pipe, D> { + fn new(requester: TrussedRequester<'pipe>) -> Self { Self { requester, _marker: Default::default(), @@ -773,7 +771,7 @@ impl PreparedClient { } /// Builds the client using the given syscall implementation. - pub fn build(self, syscall: S) -> ClientImplementation { + pub fn build(self, syscall: S) -> ClientImplementation<'pipe, S, D> { ClientImplementation::new(self.requester, syscall) } } diff --git a/src/client/mechanisms.rs b/src/client/mechanisms.rs index 0d0dab65e44..533e861b689 100644 --- a/src/client/mechanisms.rs +++ b/src/client/mechanisms.rs @@ -1,7 +1,7 @@ use super::*; #[cfg(feature = "aes256-cbc")] -impl Aes256Cbc for ClientImplementation {} +impl<'pipe, S: Syscall, E> Aes256Cbc for ClientImplementation<'pipe, S, E> {} pub trait Aes256Cbc: CryptoClient { fn decrypt_aes256cbc<'c>( @@ -22,7 +22,7 @@ pub trait Aes256Cbc: CryptoClient { } #[cfg(feature = "chacha8-poly1305")] -impl Chacha8Poly1305 for ClientImplementation {} +impl<'pipe, S: Syscall, E> Chacha8Poly1305 for ClientImplementation<'pipe, S, E> {} pub trait Chacha8Poly1305: CryptoClient { fn decrypt_chacha8poly1305<'c>( @@ -101,7 +101,7 @@ pub trait Chacha8Poly1305: CryptoClient { } #[cfg(feature = "hmac-blake2s")] -impl HmacBlake2s for ClientImplementation {} +impl<'pipe, S: Syscall, E> HmacBlake2s for ClientImplementation<'pipe, S, E> {} pub trait HmacBlake2s: CryptoClient { fn hmacblake2s_derive_key( @@ -133,7 +133,7 @@ pub trait HmacBlake2s: CryptoClient { } #[cfg(feature = "hmac-sha1")] -impl HmacSha1 for ClientImplementation {} +impl<'pipe, S: Syscall, E> HmacSha1 for ClientImplementation<'pipe, S, E> {} pub trait HmacSha1: CryptoClient { fn hmacsha1_derive_key( @@ -165,7 +165,7 @@ pub trait HmacSha1: CryptoClient { } #[cfg(feature = "hmac-sha256")] -impl HmacSha256 for ClientImplementation {} +impl<'pipe, S: Syscall, E> HmacSha256 for ClientImplementation<'pipe, S, E> {} pub trait HmacSha256: CryptoClient { fn hmacsha256_derive_key( @@ -197,7 +197,7 @@ pub trait HmacSha256: CryptoClient { } #[cfg(feature = "hmac-sha512")] -impl HmacSha512 for ClientImplementation {} +impl<'pipe, S: Syscall, E> HmacSha512 for ClientImplementation<'pipe, S, E> {} pub trait HmacSha512: CryptoClient { fn hmacsha512_derive_key( @@ -229,7 +229,7 @@ pub trait HmacSha512: CryptoClient { } #[cfg(feature = "ed255")] -impl Ed255 for ClientImplementation {} +impl<'pipe, S: Syscall, E> Ed255 for ClientImplementation<'pipe, S, E> {} pub trait Ed255: CryptoClient { fn generate_ed255_private_key( @@ -297,7 +297,7 @@ pub trait Ed255: CryptoClient { } #[cfg(feature = "p256")] -impl P256 for ClientImplementation {} +impl<'pipe, S: Syscall, E> P256 for ClientImplementation<'pipe, S, E> {} pub trait P256: CryptoClient { fn generate_p256_private_key( @@ -386,7 +386,7 @@ pub trait P256: CryptoClient { } #[cfg(feature = "sha256")] -impl Sha256 for ClientImplementation {} +impl<'pipe, S: Syscall, E> Sha256 for ClientImplementation<'pipe, S, E> {} pub trait Sha256: CryptoClient { fn sha256_derive_key( @@ -411,7 +411,7 @@ pub trait Sha256: CryptoClient { } #[cfg(feature = "tdes")] -impl Tdes for ClientImplementation {} +impl<'pipe, S: Syscall, E> Tdes for ClientImplementation<'pipe, S, E> {} pub trait Tdes: CryptoClient { fn decrypt_tdes<'c>( @@ -432,7 +432,7 @@ pub trait Tdes: CryptoClient { } #[cfg(feature = "totp")] -impl Totp for ClientImplementation {} +impl<'pipe, S: Syscall, E> Totp for ClientImplementation<'pipe, S, E> {} pub trait Totp: CryptoClient { fn sign_totp(&mut self, key: KeyId, timestamp: u64) -> ClientResult<'_, reply::Sign, Self> { @@ -446,7 +446,7 @@ pub trait Totp: CryptoClient { } #[cfg(feature = "x255")] -impl X255 for ClientImplementation {} +impl<'pipe, S: Syscall, E> X255 for ClientImplementation<'pipe, S, E> {} pub trait X255: CryptoClient { fn generate_x255_secret_key( diff --git a/src/config.rs b/src/config.rs index 94eeed934ab..efe12e8e511 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,35 +7,7 @@ pub const MAX_LONG_DATA_LENGTH: usize = 1024; pub const MAX_MESSAGE_LENGTH: usize = 1024; pub const MAX_MEDIUM_DATA_LENGTH: usize = 256; -cfg_if::cfg_if! { - if #[cfg(test)] { - pub const MAX_SERVICE_CLIENTS: usize = 6; - } else if #[cfg(feature = "clients-12")] { - pub const MAX_SERVICE_CLIENTS: usize = 12; - } else if #[cfg(feature = "clients-11")] { - pub const MAX_SERVICE_CLIENTS: usize = 11; - } else if #[cfg(feature = "clients-10")] { - pub const MAX_SERVICE_CLIENTS: usize = 10; - } else if #[cfg(feature = "clients-9")] { - pub const MAX_SERVICE_CLIENTS: usize = 9; - } else if #[cfg(feature = "clients-8")] { - pub const MAX_SERVICE_CLIENTS: usize = 8; - } else if #[cfg(feature = "clients-7")] { - pub const MAX_SERVICE_CLIENTS: usize = 7; - } else if #[cfg(feature = "clients-6")] { - pub const MAX_SERVICE_CLIENTS: usize = 6; - } else if #[cfg(feature = "clients-5")] { - pub const MAX_SERVICE_CLIENTS: usize = 5; - } else if #[cfg(feature = "clients-4")] { - pub const MAX_SERVICE_CLIENTS: usize = 4; - } else if #[cfg(feature = "clients-3")] { - pub const MAX_SERVICE_CLIENTS: usize = 3; - } else if #[cfg(feature = "clients-2")] { - pub const MAX_SERVICE_CLIENTS: usize = 2; - } else if #[cfg(feature = "clients-1")] { - pub const MAX_SERVICE_CLIENTS: usize = 1; - } -} + pub const MAX_SHORT_DATA_LENGTH: usize = 128; pub const MAX_SIGNATURE_LENGTH: usize = 512 * 2; diff --git a/src/pipe.rs b/src/pipe.rs index 9dfe9875897..9c5f8b62757 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -2,23 +2,18 @@ // Ignore lint caused by interchange! macro #![allow(clippy::derive_partial_eq_without_eq)] -use interchange::{Interchange, InterchangeRef, Requester, Responder}; +use interchange::{Interchange, Requester, Responder}; use crate::api::{Reply, Request}; use crate::backend::BackendId; -use crate::config; use crate::error::Error; use crate::types::Context; -type TrussedInterchangeInner = - Interchange, { config::MAX_SERVICE_CLIENTS }>; -static TRUSSED_INTERCHANGE_INNER: TrussedInterchangeInner = Interchange::new(); +pub type TrussedInterchange = + Interchange, { MAX_CLIENTS }>; -pub type TrussedInterchange = InterchangeRef<'static, Request, Result>; -pub static TRUSSED_INTERCHANGE: TrussedInterchange = TRUSSED_INTERCHANGE_INNER.as_interchange_ref(); - -pub type TrussedResponder = Responder<'static, Request, Result>; -pub type TrussedRequester = Requester<'static, Request, Result>; +pub type TrussedResponder<'pipe> = Responder<'pipe, Request, Result>; +pub type TrussedRequester<'pipe> = Requester<'pipe, Request, Result>; // pub use interchange::TrussedInterchange; @@ -30,8 +25,8 @@ pub type TrussedRequester = Requester<'static, Request, Result>; // https://xenomai.org/documentation/xenomai-2.4/html/api/group__native__queue.html // https://doc.micrium.com/display/osiiidoc/Using+Message+Queues -pub struct ServiceEndpoint { - pub interchange: TrussedResponder, +pub struct ServiceEndpoint<'pipe, I: 'static, C> { + pub interchange: TrussedResponder<'pipe>, // service (trusted) has this, not client (untrusted) // used among other things to namespace cryptographic material pub ctx: Context, diff --git a/src/service.rs b/src/service.rs index 6da1cd908d2..c19a6fe54e9 100644 --- a/src/service.rs +++ b/src/service.rs @@ -3,7 +3,6 @@ use chacha20::ChaCha8Rng; use littlefs2::path::PathBuf; pub use rand_core::{RngCore, SeedableRng}; -use crate::api::*; use crate::backend::{BackendId, CoreOnly, Dispatch}; use crate::client::{ClientBuilder, ClientImplementation}; use crate::config::*; @@ -11,7 +10,7 @@ use crate::error::{Error, Result}; pub use crate::key; use crate::mechanisms; pub use crate::pipe::ServiceEndpoint; -use crate::pipe::TrussedResponder; +use crate::pipe::{TrussedInterchange, TrussedResponder}; use crate::platform::*; pub use crate::store::{ certstore::{Certstore as _, ClientCertstore}, @@ -21,6 +20,7 @@ pub use crate::store::{ }; use crate::types::*; use crate::Bytes; +use crate::{api::*, pipe}; pub mod attest; @@ -78,18 +78,22 @@ impl ServiceResources

{ } } -pub struct Service +pub struct Service<'pipe, P, const MAX_CLIENTS: usize, D = CoreOnly> where P: Platform, D: Dispatch, { - eps: Vec, { MAX_SERVICE_CLIENTS }>, + eps: Vec, { MAX_CLIENTS }>, + pipe: &'pipe pipe::TrussedInterchange, resources: ServiceResources

, dispatch: D, } // need to be able to send crypto service to an interrupt handler -unsafe impl Send for Service {} +unsafe impl<'pipe, P: Platform, D: Dispatch, const MAX_CLIENTS: usize> Send + for Service<'pipe, P, MAX_CLIENTS, D> +{ +} impl ServiceResources

{ pub fn certstore(&mut self, ctx: &CoreContext) -> Result> { @@ -683,24 +687,32 @@ impl ServiceResources

{ } } -impl Service

{ - pub fn new(platform: P) -> Self { - Self::with_dispatch(platform, Default::default()) +impl<'pipe, P: Platform, const MAX_CLIENTS: usize> Service<'pipe, P, MAX_CLIENTS> { + pub fn new(platform: P, pipe: &'pipe TrussedInterchange) -> Self { + Self::with_dispatch(platform, pipe, Default::default()) } } -impl Service { - pub fn with_dispatch(platform: P, dispatch: D) -> Self { +impl<'pipe, P: Platform, D: Dispatch, const MAX_CLIENTS: usize> Service<'pipe, P, MAX_CLIENTS, D> { + pub fn with_dispatch( + platform: P, + pipe: &'pipe TrussedInterchange, + dispatch: D, + ) -> Self { let resources = ServiceResources::new(platform); Self { eps: Vec::new(), resources, dispatch, + pipe, } } + pub fn pipe(&self) -> &'pipe TrussedInterchange { + self.pipe + } } -impl Service

{ +impl<'pipe, P: Platform, const MAX_CLIENTS: usize> Service<'pipe, P, MAX_CLIENTS> { /// Add a new client, claiming one of the statically configured /// interchange pairs. pub fn try_new_client( @@ -730,17 +742,17 @@ impl Service

{ pub fn try_into_new_client( mut self, client_id: &str, - ) -> Result, Error> { + ) -> Result, Error> { ClientBuilder::new(client_id) .prepare(&mut self) .map(|p| p.build(self)) } } -impl Service { +impl<'pipe, P: Platform, D: Dispatch, const MAX_CLIENTS: usize> Service<'pipe, P, MAX_CLIENTS, D> { pub fn add_endpoint( &mut self, - interchange: TrussedResponder, + interchange: TrussedResponder<'pipe>, core_ctx: impl Into, backends: &'static [BackendId], ) -> Result<(), Error> { @@ -838,7 +850,8 @@ impl Service { } } -impl crate::client::Syscall for &mut Service +impl<'pipe, P, D, const MAX_CLIENTS: usize> crate::client::Syscall + for &mut Service<'pipe, P, MAX_CLIENTS, D> where P: Platform, D: Dispatch, @@ -848,7 +861,8 @@ where } } -impl crate::client::Syscall for Service +impl<'pipe, P, D, const MAX_CLIENTS: usize> crate::client::Syscall + for Service<'pipe, P, MAX_CLIENTS, D> where P: Platform, D: Dispatch, From 46c4f6a176f4e7d685a76bb7a309b5396285d375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 10 Feb 2023 18:01:26 +0100 Subject: [PATCH 4/8] Adapt virt to previous changes --- src/virt.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/virt.rs b/src/virt.rs index 0f748400a2e..4e51e442bcd 100644 --- a/src/virt.rs +++ b/src/virt.rs @@ -9,6 +9,7 @@ mod ui; use std::{path::PathBuf, sync::Mutex}; use chacha20::ChaCha8Rng; +use interchange::Interchange; use rand_core::SeedableRng as _; use crate::{ @@ -22,10 +23,10 @@ use crate::{ pub use store::{Filesystem, Ram, StoreProvider}; pub use ui::UserInterface; -pub type Client = ClientImplementation, D>, D>; +pub type Client<'pipe, S, const MAX_CLIENTS: usize = 1, D = CoreOnly> = + ClientImplementation<'pipe, Service<'pipe, Platform, MAX_CLIENTS, D>, D>; // We need this mutex to make sure that: -// - TrussedInterchange is not used concurrently (panics if violated) // - the Store is not used concurrently static MUTEX: Mutex<()> = Mutex::new(()); @@ -51,7 +52,7 @@ where pub fn with_client(store: S, client_id: &str, f: F) -> R where S: StoreProvider, - F: FnOnce(Client) -> R, + F: for<'pipe> FnOnce(Client<'pipe, S>) -> R, { with_platform(store, |platform| platform.run_client(client_id, f)) } @@ -59,14 +60,14 @@ where pub fn with_fs_client(internal: P, client_id: &str, f: F) -> R where P: Into, - F: FnOnce(Client) -> R, + F: for<'pipe> FnOnce(Client<'pipe, Filesystem>) -> R, { with_client(Filesystem::new(internal), client_id, f) } pub fn with_ram_client(client_id: &str, f: F) -> R where - F: FnOnce(Client) -> R, + F: for<'pipe> FnOnce(Client<'pipe, Ram>) -> R, { with_client(Ram::default(), client_id, f) } @@ -78,24 +79,28 @@ pub struct Platform { } impl Platform { - pub fn run_client( - self, - client_id: &str, - test: impl FnOnce(ClientImplementation>) -> R, - ) -> R { - let service = Service::new(self); + pub fn run_client(self, client_id: &str, test: F) -> R + where + F: for<'pipe> FnOnce(ClientImplementation<'pipe, Service<'pipe, Self, 1>>) -> R, + { + let interchange = Interchange::new(); + let service = Service::new(self, &interchange); let client = service.try_into_new_client(client_id).unwrap(); test(client) } - pub fn run_client_with_backends( + pub fn run_client_with_backends( self, client_id: &str, dispatch: D, backends: &'static [BackendId], - test: impl FnOnce(ClientImplementation, D>) -> R, - ) -> R { - let mut service = Service::with_dispatch(self, dispatch); + test: F, + ) -> R + where + F: for<'pipe> FnOnce(ClientImplementation<'pipe, Service<'pipe, Self, 1, D>, D>) -> R, + { + let interchange = Interchange::new(); + let mut service = Service::with_dispatch(self, &interchange, dispatch); let client = ClientBuilder::new(client_id) .backends(backends) .prepare(&mut service) From a3fdf6efe57e8d034b6e25d1e33dd357453a7a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 13 Feb 2023 09:40:46 +0100 Subject: [PATCH 5/8] Fix tests --- src/pipe.rs | 5 +++-- src/tests.rs | 20 +++++++------------- tests/backends.rs | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/pipe.rs b/src/pipe.rs index 9c5f8b62757..3098fd78bc2 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -37,7 +37,7 @@ pub struct ServiceEndpoint<'pipe, I: 'static, C> { #[cfg(test)] mod tests { - use super::TrussedInterchange; + use super::{TrussedRequester, TrussedResponder}; use crate::api::{Reply, Request}; use core::mem; @@ -64,6 +64,7 @@ mod tests { #[test] fn test_interchange_size() { - assert_size::(); + use interchange::Channel; + assert_size::>(); } } diff --git a/src/tests.rs b/src/tests.rs index 55be5e7abb4..084441886f2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -175,23 +175,17 @@ macro_rules! setup { let pc_interface: UserInterface = Default::default(); let platform = $platform::new(rng, store, pc_interface); - let mut trussed: crate::Service<$platform> = crate::service::Service::new(platform); - let (test_trussed_requester, test_trussed_responder) = crate::pipe::TRUSSED_INTERCHANGE - .claim() - .expect("could not setup TEST TrussedInterchange"); + let pipe = interchange::Interchange::new(); let test_client_id = "TEST"; - - assert!(trussed - .add_endpoint(test_trussed_responder, test_client_id, &[]) - .is_ok()); + let mut trussed: crate::Service<$platform, 1> = + crate::service::Service::new(platform, &pipe); trussed.set_seed_if_uninitialized(&$seed); - let mut $client = { - pub type TestClient<'a> = - crate::ClientImplementation<&'a mut crate::Service<$platform>>; - TestClient::new(test_trussed_requester, &mut trussed) - }; + let mut $client = crate::client::ClientBuilder::new(test_client_id) + .prepare(&mut trussed) + .expect("Preparing the client should not fail") + .build(&mut trussed); }; } diff --git a/tests/backends.rs b/tests/backends.rs index 1b7c423a9cc..7df3f344eda 100644 --- a/tests/backends.rs +++ b/tests/backends.rs @@ -13,7 +13,7 @@ use trussed::{ }; type Platform = virt::Platform; -type Client = ClientImplementation, Dispatch>; +type Client<'pipe> = ClientImplementation<'pipe, Service<'pipe, Platform, 1, Dispatch>, Dispatch>; const BACKENDS_TEST: &[BackendId] = &[BackendId::Custom(Backend::Test), BackendId::Core]; From dc930cc08625cb710d198f93605a0c564c92e3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 13 Feb 2023 09:46:05 +0100 Subject: [PATCH 6/8] Fix serde extensions feature --- src/serde_extensions.rs | 2 +- tests/serde_extensions.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serde_extensions.rs b/src/serde_extensions.rs index 8c372d1a3e9..5caebfcc186 100644 --- a/src/serde_extensions.rs +++ b/src/serde_extensions.rs @@ -166,7 +166,7 @@ pub trait ExtensionClient: PollClient { } } -impl ExtensionClient for ClientImplementation +impl<'pipe, E, S, I> ExtensionClient for ClientImplementation<'pipe, S, I> where E: Extension, S: Syscall, diff --git a/tests/serde_extensions.rs b/tests/serde_extensions.rs index 611a13f5533..9e38540c355 100644 --- a/tests/serde_extensions.rs +++ b/tests/serde_extensions.rs @@ -37,7 +37,7 @@ use trussed::{ use runner::Backends; type Platform = virt::Platform; -type Client = ClientImplementation, Backends>; +type Client<'pipe> = ClientImplementation<'pipe, Service<'pipe, Platform, 1, Backends>, Backends>; mod extensions { use serde::{Deserialize, Serialize}; From 494490432ab1531114c4feebbf4f78fd7bc7bcbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 13 Feb 2023 09:53:15 +0100 Subject: [PATCH 7/8] Simplifiy tests --- src/tests.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 084441886f2..889b7091000 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,7 @@ #![cfg(test)] use chacha20::ChaCha20; +use crate::pipe::TrussedInterchange; use crate::types::*; use crate::*; use entropy::shannon_entropy; @@ -147,6 +148,21 @@ macro_rules! create_memory { }}; } +fn test_client( + platform: P, + pipe: &TrussedInterchange<1>, + seed: [u8; 32], +) -> ClientImplementation<'_, Service<'_, P, 1>> { + let test_client_id = "TEST"; + let mut trussed: crate::Service = crate::service::Service::new(platform, pipe); + + trussed.set_seed_if_uninitialized(&seed); + crate::client::ClientBuilder::new(test_client_id) + .prepare(&mut trussed) + .expect("Preparing the client should not fail") + .build(trussed) +} + // TODO: what's going on here? Duplicates code in `tests/client/mod.rs`. // Might make sense as a trussed::fixture submodule activated via feature flag. macro_rules! setup { @@ -177,15 +193,7 @@ macro_rules! setup { let platform = $platform::new(rng, store, pc_interface); let pipe = interchange::Interchange::new(); - let test_client_id = "TEST"; - let mut trussed: crate::Service<$platform, 1> = - crate::service::Service::new(platform, &pipe); - - trussed.set_seed_if_uninitialized(&$seed); - let mut $client = crate::client::ClientBuilder::new(test_client_id) - .prepare(&mut trussed) - .expect("Preparing the client should not fail") - .build(&mut trussed); + let mut $client = test_client(platform, &pipe, $seed); }; } From 8fdeefdee1ca6085597812fb6d83d85eaa0bddb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 22 Feb 2023 17:43:21 +0100 Subject: [PATCH 8/8] Fix pipe tests I don't really understand why the size is that high. The size of the `Message` struct is 2416. The channel is a message in an unsafecell + 1 atomicU8 and 2 atomic bools. It should only 3 more bytes but for some reason it's 16 --- src/pipe.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pipe.rs b/src/pipe.rs index 3098fd78bc2..231217a2407 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -37,7 +37,6 @@ pub struct ServiceEndpoint<'pipe, I: 'static, C> { #[cfg(test)] mod tests { - use super::{TrussedRequester, TrussedResponder}; use crate::api::{Reply, Request}; use core::mem; @@ -45,7 +44,7 @@ mod tests { // size. Bumping the size is not a breaking change but should only be done if really // necessary. - const MAX_SIZE: usize = 2416; + const MAX_SIZE: usize = 2432; fn assert_size() { let size = mem::size_of::(); @@ -65,6 +64,7 @@ mod tests { #[test] fn test_interchange_size() { use interchange::Channel; + // The real cost per-client assert_size::>(); } }