diff --git a/crates/scheduler/CHANGELOG.md b/crates/scheduler/CHANGELOG.md index c47f0521..0843fe7c 100644 --- a/crates/scheduler/CHANGELOG.md +++ b/crates/scheduler/CHANGELOG.md @@ -8,6 +8,7 @@ ### Minor +- Gate dead-code when `applet-api-platform-protocol` is disabled - Exit applets when main exits with no registered callbacks - Call the `applet::notify_{start,exit}()` hooks - Trap applets calling into host during init (except for debug printing) diff --git a/crates/scheduler/src/applet.rs b/crates/scheduler/src/applet.rs index 15ccc015..10f74098 100644 --- a/crates/scheduler/src/applet.rs +++ b/crates/scheduler/src/applet.rs @@ -12,12 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(feature = "applet-api-platform-protocol")] use alloc::boxed::Box; use alloc::collections::{BTreeSet, VecDeque}; #[cfg(feature = "internal-hash-context")] use wasefire_board_api as board; use wasefire_board_api::{Api as Board, Event}; +#[cfg(feature = "applet-api-platform-protocol")] use wasefire_error::{Code, Error}; use wasefire_logger as log; @@ -50,6 +52,7 @@ pub struct Applet { events: VecDeque>, /// Protocol request or response, if any. + #[cfg(feature = "applet-api-platform-protocol")] protocol: Protocol, /// Whether we returned from a callback. @@ -68,6 +71,7 @@ impl Default for Applet { Self { store: Default::default(), events: Default::default(), + #[cfg(feature = "applet-api-platform-protocol")] protocol: Default::default(), #[cfg(feature = "wasm")] done: Default::default(), @@ -79,6 +83,7 @@ impl Default for Applet { } #[derive(Debug, Default)] +#[cfg(feature = "applet-api-platform-protocol")] enum Protocol { #[default] Empty, @@ -216,6 +221,7 @@ impl Applet { self.events.len() } + #[cfg(feature = "applet-api-platform-protocol")] pub fn put_request(&mut self, event: Event, request: &[u8]) -> Result<(), Error> { self.get(Key::from(&event)).ok_or(Error::world(Code::InvalidState))?; // If the applet is processing a request, we'll send the event when they respond. @@ -227,6 +233,7 @@ impl Applet { Ok(()) } + #[cfg(feature = "applet-api-platform-protocol")] pub fn get_request(&mut self) -> Result>, Error> { let (update, result) = match core::mem::take(&mut self.protocol) { x @ (Protocol::Empty | Protocol::Response(_)) => (x, Ok(None)), @@ -237,6 +244,7 @@ impl Applet { result } + #[cfg(feature = "applet-api-platform-protocol")] pub fn put_response(&mut self, response: Box<[u8]>) -> Result<(), Error> { match &self.protocol { Protocol::Processing => self.protocol = Protocol::Response(response), @@ -247,6 +255,7 @@ impl Applet { Ok(()) } + #[cfg(feature = "applet-api-platform-protocol")] pub fn get_response(&mut self) -> Result>, Error> { let (update, result) = match core::mem::take(&mut self.protocol) { x @ (Protocol::Processing | Protocol::Request(_)) => (x, Ok(None)), diff --git a/crates/scheduler/src/protocol.rs b/crates/scheduler/src/protocol.rs index 5fd2b379..a0ceec3f 100644 --- a/crates/scheduler/src/protocol.rs +++ b/crates/scheduler/src/protocol.rs @@ -20,12 +20,13 @@ use wasefire_board_api::platform::protocol::Api as _; use wasefire_board_api::{self as board, Api as Board}; use wasefire_error::{Code, Error}; use wasefire_logger as log; +#[cfg(feature = "applet-api-platform-protocol")] use wasefire_protocol::applet::AppletId; #[cfg(feature = "wasm")] use wasefire_protocol::applet::ExitStatus; use wasefire_protocol::{self as service, Api, ApiResult, Request, Service, VERSION}; -use crate::{Applet, Scheduler}; +use crate::Scheduler; #[derive(Debug, Default)] pub struct State(StateImpl); @@ -55,9 +56,12 @@ pub fn process_event(scheduler: &mut Scheduler, event: board::Event fn process_event_( scheduler: &mut Scheduler, event: board::Event, request: Box<[u8]>, ) -> Result<(), Error> { + #[cfg(not(feature = "applet-api-platform-protocol"))] + let _ = event; match &scheduler.protocol.0 { Normal { .. } => (), Locked => return Err(Error::user(Code::InvalidState)), + #[cfg(feature = "applet-api-platform-protocol")] Tunnel { applet_id, delimiter } => { if request == *delimiter { scheduler.protocol = State::default(); @@ -69,10 +73,16 @@ fn process_event_( let request = Api::::decode(&request)?; match request { Api::ApiVersion(()) => reply::(VERSION), + #[cfg(not(feature = "applet-api-platform-protocol"))] + Api::AppletRequest(_) => return Err(Error::world(Code::NotImplemented)), + #[cfg(feature = "applet-api-platform-protocol")] Api::AppletRequest(service::applet::Request { applet_id, request }) => { applet::(scheduler, applet_id)?.put_request(event, request)?; reply::(()); } + #[cfg(not(feature = "applet-api-platform-protocol"))] + Api::AppletResponse(_) => return Err(Error::world(Code::NotImplemented)), + #[cfg(feature = "applet-api-platform-protocol")] Api::AppletResponse(applet_id) => { let response = applet::(scheduler, applet_id)?.get_response()?; reply::(response.as_deref()); @@ -81,6 +91,9 @@ fn process_event_( use wasefire_board_api::platform::Api as _; board::Platform::::reboot()?; } + #[cfg(not(feature = "applet-api-platform-protocol"))] + Api::AppletTunnel(_) => return Err(Error::world(Code::NotImplemented)), + #[cfg(feature = "applet-api-platform-protocol")] Api::AppletTunnel(service::applet::Tunnel { applet_id, delimiter }) => { let delimiter = delimiter.to_vec().into_boxed_slice(); scheduler.protocol.0 = Tunnel { applet_id, delimiter }; @@ -244,6 +257,7 @@ enum StateImpl { install: TransferState, }, Locked, + #[cfg(feature = "applet-api-platform-protocol")] Tunnel { applet_id: service::applet::AppletId, delimiter: Box<[u8]>, @@ -278,9 +292,10 @@ impl StateImpl { } } +#[cfg(feature = "applet-api-platform-protocol")] fn applet( scheduler: &mut Scheduler, applet_id: AppletId, -) -> Result<&mut Applet, Error> { +) -> Result<&mut crate::Applet, Error> { let AppletId = applet_id; scheduler.applet.get().ok_or_else(|| { log::warn!("Failed to find applet");