Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better gate applet-platform-protocol code #662

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions crates/scheduler/src/applet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,6 +52,7 @@ pub struct Applet<B: Board> {
events: VecDeque<Event<B>>,

/// Protocol request or response, if any.
#[cfg(feature = "applet-api-platform-protocol")]
protocol: Protocol,

/// Whether we returned from a callback.
Expand All @@ -68,6 +71,7 @@ impl<B: Board> Default for Applet<B> {
Self {
store: Default::default(),
events: Default::default(),
#[cfg(feature = "applet-api-platform-protocol")]
protocol: Default::default(),
#[cfg(feature = "wasm")]
done: Default::default(),
Expand All @@ -79,6 +83,7 @@ impl<B: Board> Default for Applet<B> {
}

#[derive(Debug, Default)]
#[cfg(feature = "applet-api-platform-protocol")]
enum Protocol {
#[default]
Empty,
Expand Down Expand Up @@ -216,6 +221,7 @@ impl<B: Board> Applet<B> {
self.events.len()
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn put_request(&mut self, event: Event<B>, 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.
Expand All @@ -227,6 +233,7 @@ impl<B: Board> Applet<B> {
Ok(())
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn get_request(&mut self) -> Result<Option<Box<[u8]>>, Error> {
let (update, result) = match core::mem::take(&mut self.protocol) {
x @ (Protocol::Empty | Protocol::Response(_)) => (x, Ok(None)),
Expand All @@ -237,6 +244,7 @@ impl<B: Board> Applet<B> {
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),
Expand All @@ -247,6 +255,7 @@ impl<B: Board> Applet<B> {
Ok(())
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn get_response(&mut self) -> Result<Option<Box<[u8]>>, Error> {
let (update, result) = match core::mem::take(&mut self.protocol) {
x @ (Protocol::Processing | Protocol::Request(_)) => (x, Ok(None)),
Expand Down
19 changes: 17 additions & 2 deletions crates/scheduler/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -55,9 +56,12 @@ pub fn process_event<B: Board>(scheduler: &mut Scheduler<B>, event: board::Event
fn process_event_<B: Board>(
scheduler: &mut Scheduler<B>, event: board::Event<B>, 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();
Expand All @@ -69,10 +73,16 @@ fn process_event_<B: Board>(
let request = Api::<Request>::decode(&request)?;
match request {
Api::ApiVersion(()) => reply::<B, service::ApiVersion>(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::<B>(scheduler, applet_id)?.put_request(event, request)?;
reply::<B, service::AppletRequest>(());
}
#[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::<B>(scheduler, applet_id)?.get_response()?;
reply::<B, service::AppletResponse>(response.as_deref());
Expand All @@ -81,6 +91,9 @@ fn process_event_<B: Board>(
use wasefire_board_api::platform::Api as _;
board::Platform::<B>::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 };
Expand Down Expand Up @@ -244,6 +257,7 @@ enum StateImpl {
install: TransferState,
},
Locked,
#[cfg(feature = "applet-api-platform-protocol")]
Tunnel {
applet_id: service::applet::AppletId,
delimiter: Box<[u8]>,
Expand Down Expand Up @@ -278,9 +292,10 @@ impl StateImpl {
}
}

#[cfg(feature = "applet-api-platform-protocol")]
fn applet<B: Board>(
scheduler: &mut Scheduler<B>, applet_id: AppletId,
) -> Result<&mut Applet<B>, Error> {
) -> Result<&mut crate::Applet<B>, Error> {
let AppletId = applet_id;
scheduler.applet.get().ok_or_else(|| {
log::warn!("Failed to find applet");
Expand Down