Skip to content
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 @@ -9,6 +9,7 @@

### Patch

- Enable `unreachable_pub` lint
- Disable `unused-features` lint
- Use heterogeneous try blocks when needed
- Update dependencies
Expand Down
1 change: 1 addition & 0 deletions crates/scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,5 @@ internal-hash-context = []
clippy.mod_module_files = "warn"
clippy.uninlined_format_args = "allow"
clippy.unit_arg = "allow"
rust.unreachable_pub = "warn"
rust.unused_crate_dependencies = "warn"
60 changes: 30 additions & 30 deletions crates/scheduler/src/applet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ use crate::Trap;
use crate::event::InstId;
use crate::event::{Handler, Key};

pub mod store;
pub(crate) mod store;

#[allow(clippy::large_enum_variant)]
pub enum Slot<B: Board> {
pub(crate) enum Slot<B: Board> {
#[cfg(any(feature = "pulley", feature = "wasm"))]
Empty,
Running(Applet<B>),
Exited(wasefire_protocol::applet::ExitStatus),
}

impl<B: Board> Slot<B> {
pub fn get(&mut self) -> Option<&mut Applet<B>> {
pub(crate) fn get(&mut self) -> Option<&mut Applet<B>> {
match self {
Slot::Running(x) => Some(x),
_ => None,
Expand All @@ -50,7 +50,7 @@ impl<B: Board> Slot<B> {
}

#[cfg_attr(not(feature = "pulley"), derive_where(Default))]
pub struct Applet<B: Board> {
pub(crate) struct Applet<B: Board> {
pub store: self::store::Store,
pub events: Events<B>,

Expand All @@ -67,7 +67,7 @@ pub struct Applet<B: Board> {
}

#[derive_where(Default)]
pub struct Events<B: Board> {
pub(crate) struct Events<B: Board> {
/// Pending events.
pending: VecDeque<Event<B>>,

Expand All @@ -76,7 +76,7 @@ pub struct Events<B: Board> {
}

#[cfg(feature = "board-api-vendor")]
pub struct Handlers<'a, B: Board> {
pub(crate) struct Handlers<'a, B: Board> {
inst: Option<InstId>,
events: &'a mut Events<B>,
}
Expand All @@ -93,7 +93,7 @@ enum Protocol {

/// Currently alive hash contexts.
#[cfg(feature = "internal-hash-context")]
pub struct AppletHashes<B: Board>([Option<HashContext<B>>; 4]);
pub(crate) struct AppletHashes<B: Board>([Option<HashContext<B>>; 4]);

// We have to implement manually because derive is not able to find the correct bounds.
#[cfg(feature = "internal-hash-context")]
Expand All @@ -104,7 +104,7 @@ impl<B: Board> Default for AppletHashes<B> {
}

#[cfg(feature = "internal-hash-context")]
pub enum HashContext<B: Board> {
pub(crate) enum HashContext<B: Board> {
#[cfg(feature = "board-api-crypto-sha256")]
Sha256(board::crypto::HashApi<board::crypto::Sha256<B>>),
#[cfg(feature = "board-api-crypto-sha384")]
Expand All @@ -118,17 +118,17 @@ pub enum HashContext<B: Board> {

#[cfg(feature = "internal-hash-context")]
impl<B: Board> AppletHashes<B> {
pub fn insert(&mut self, hash: HashContext<B>) -> Result<usize, Trap> {
pub(crate) fn insert(&mut self, hash: HashContext<B>) -> Result<usize, Trap> {
let id = self.0.iter().position(|x| x.is_none()).ok_or(Trap)?;
self.0[id] = Some(hash);
Ok(id)
}

pub fn get_mut(&mut self, id: usize) -> Result<&mut HashContext<B>, Trap> {
pub(crate) fn get_mut(&mut self, id: usize) -> Result<&mut HashContext<B>, Trap> {
self.0.get_mut(id).ok_or(Trap)?.as_mut().ok_or(Trap)
}

pub fn take(&mut self, id: usize) -> Result<HashContext<B>, Trap> {
pub(crate) fn take(&mut self, id: usize) -> Result<HashContext<B>, Trap> {
self.0.get_mut(id).ok_or(Trap)?.take().ok_or(Trap)
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<B: Board> Events<B> {
}

#[cfg(feature = "board-api-vendor")]
pub fn handlers(&mut self, inst: Option<InstId>) -> Handlers<'_, B> {
pub(crate) fn handlers(&mut self, inst: Option<InstId>) -> Handlers<'_, B> {
Handlers { inst, events: self }
}
}
Expand All @@ -178,7 +178,7 @@ impl<'a, B: Board> board::applet::Handlers<board::vendor::Key<B>> for Handlers<'

impl<B: Board> Applet<B> {
#[cfg(feature = "pulley")]
pub fn new(store: self::store::Store) -> Self {
pub(crate) fn new(store: self::store::Store) -> Self {
Applet {
store,
events: Events::default(),
Expand All @@ -190,16 +190,16 @@ impl<B: Board> Applet<B> {
}
}

pub fn store_mut(&mut self) -> &mut Store {
pub(crate) fn store_mut(&mut self) -> &mut Store {
&mut self.store
}

#[allow(dead_code)] // in case no API uses memory
pub fn memory(&mut self) -> Memory<'_> {
pub(crate) fn memory(&mut self) -> Memory<'_> {
self.store.memory()
}

pub fn push(&mut self, event: Event<B>) {
pub(crate) fn push(&mut self, event: Event<B>) {
const MAX_EVENTS: usize = 5;
#[allow(clippy::if_same_then_else)]
if !self.events.handlers.contains(&Key::from(&event)) {
Expand All @@ -217,7 +217,7 @@ impl<B: Board> Applet<B> {
}

/// Returns the next event action.
pub fn pop(&mut self) -> EventAction<B> {
pub(crate) fn pop(&mut self) -> EventAction<B> {
#[cfg(any(feature = "pulley", feature = "wasm"))]
if core::mem::replace(&mut self.done, false) {
return EventAction::Reply;
Expand All @@ -229,27 +229,27 @@ impl<B: Board> Applet<B> {
}

#[cfg(any(feature = "pulley", feature = "wasm"))]
pub fn done(&mut self) {
pub(crate) fn done(&mut self) {
self.done = true;
}

#[allow(dead_code)] // in case there are no events
pub fn enable(&mut self, handler: Handler<B>) -> Result<(), Trap> {
pub(crate) fn enable(&mut self, handler: Handler<B>) -> Result<(), Trap> {
self.events.enable(handler)
}

pub fn disable(&mut self, key: Key<B>) -> Result<(), Trap> {
pub(crate) fn disable(&mut self, key: Key<B>) -> Result<(), Trap> {
self.events.disable(key)
}

#[cfg_attr(not(feature = "board-api-fingerprint-matcher"), allow(dead_code))]
pub fn disable_noerror(&mut self, key: Key<B>) {
pub(crate) fn disable_noerror(&mut self, key: Key<B>) {
if self.disable(key).is_err() {
log::warn!("Failed disabling {:?}", key);
}
}

pub fn free(&mut self) {
pub(crate) fn free(&mut self) {
self.events.pending.clear();
for &Handler { key, .. } in &self.events.handlers {
if let Err(error) = key.disable() {
Expand All @@ -258,21 +258,21 @@ impl<B: Board> Applet<B> {
}
}

pub fn get(&self, key: Key<B>) -> Option<&Handler<B>> {
pub(crate) fn get(&self, key: Key<B>) -> Option<&Handler<B>> {
self.events.handlers.get(&key)
}

#[cfg(any(feature = "pulley", feature = "wasm"))]
pub fn has_handlers(&self) -> bool {
pub(crate) fn has_handlers(&self) -> bool {
!self.events.handlers.is_empty()
}

pub fn len(&self) -> usize {
pub(crate) fn len(&self) -> usize {
self.events.pending.len()
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn put_request(&mut self, event: Event<B>, request: &[u8]) -> Result<(), Error> {
pub(crate) 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.
if !matches!(self.protocol, Protocol::Processing) {
Expand All @@ -284,7 +284,7 @@ impl<B: Board> Applet<B> {
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn get_request(&mut self) -> Result<Option<Box<[u8]>>, Error> {
pub(crate) 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)),
Protocol::Request(x) => (Protocol::Processing, Ok(Some(x))),
Expand All @@ -295,7 +295,7 @@ impl<B: Board> Applet<B> {
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn put_response(&mut self, response: Box<[u8]>) -> Result<(), Error> {
pub(crate) fn put_response(&mut self, response: Box<[u8]>) -> Result<(), Error> {
match &self.protocol {
Protocol::Processing => self.protocol = Protocol::Response(response),
// We use World:InvalidState to know that there is a new request.
Expand All @@ -306,7 +306,7 @@ impl<B: Board> Applet<B> {
}

#[cfg(feature = "applet-api-platform-protocol")]
pub fn get_response(&mut self) -> Result<Option<Box<[u8]>>, Error> {
pub(crate) 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)),
Protocol::Response(x) => (Protocol::Empty, Ok(Some(x))),
Expand All @@ -319,7 +319,7 @@ impl<B: Board> Applet<B> {

/// Action when waiting for callbacks.
#[derive(Debug)]
pub enum EventAction<B: Board> {
pub(crate) enum EventAction<B: Board> {
/// Should handle the event.
Handle(Event<B>),

Expand Down
8 changes: 4 additions & 4 deletions crates/scheduler/src/applet/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

use wasefire_board_api::applet::Memory as AppletMemory;

pub use self::impl_::Store;
pub(crate) use self::impl_::Store;
#[cfg(feature = "pulley")]
pub use self::impl_::{PreStore, RunResult};
pub(crate) use self::impl_::{PreStore, RunResult};

#[cfg_attr(feature = "native", path = "store/native.rs")]
#[cfg_attr(feature = "pulley", path = "store/pulley.rs")]
#[cfg_attr(feature = "wasm", path = "store/wasm.rs")]
mod impl_;

pub type Memory<'a> = <Store as StoreApi>::Memory<'a>;
pub(crate) type Memory<'a> = <Store as StoreApi>::Memory<'a>;

pub trait StoreApi {
pub(crate) trait StoreApi {
type Memory<'a>: AppletMemory
where Self: 'a;

Expand Down
4 changes: 2 additions & 2 deletions crates/scheduler/src/applet/store/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use super::StoreApi;
use crate::Trap;

#[derive(Debug, Default)]
pub struct Store(());
pub(crate) struct Store(());

pub struct Memory;
pub(crate) struct Memory;

impl StoreApi for Store {
type Memory<'a>
Expand Down
24 changes: 14 additions & 10 deletions crates/scheduler/src/applet/store/pulley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use wasmtime::{
use super::StoreApi;
use crate::Trap;

pub struct PreStore {
pub(crate) struct PreStore {
engine: Engine,
store: WtStore<()>,
linker: Linker<()>,
Expand All @@ -57,7 +57,7 @@ impl Default for PreStore {
}

impl PreStore {
pub fn link_func(&mut self, id: usize, name: &str, params: usize) -> Result<(), Error> {
pub(crate) fn link_func(&mut self, id: usize, name: &str, params: usize) -> Result<(), Error> {
let item = match params {
0 => Func::wrap_async(&mut self.store, move |caller, ()| call(caller, id, vec![])),
1 => Func::wrap_async(&mut self.store, move |caller, args: (u32,)| {
Expand Down Expand Up @@ -109,7 +109,9 @@ impl PreStore {
}

// Safety: the slice must outlive the store.
pub unsafe fn instantiate(mut self, pulley: &'static [u8], id: usize) -> Result<Store, Error> {
pub(crate) unsafe fn instantiate(
mut self, pulley: &'static [u8], id: usize,
) -> Result<Store, Error> {
let Ok(module) = (unsafe { Module::deserialize_raw(&self.engine, pulley.into()) }) else {
log::warn!("Failed to deserialize pulley module.");
return Err(Error::user(Code::InvalidArgument));
Expand Down Expand Up @@ -154,7 +156,7 @@ impl PreStore {
}
}

pub struct Store {
pub(crate) struct Store {
instance: Instance,
// Owned Box if threads is empty, otherwise the first thread exclusively owns it.
store: *mut WtStore<()>,
Expand Down Expand Up @@ -188,14 +190,16 @@ impl Drop for Store {
}
}

pub enum RunResult {
pub(crate) enum RunResult {
Done(Vec<Val>),
Host,
Trap,
}

impl Store {
pub fn invoke(&mut self, name: &str, args: &[u32], nres: usize) -> Result<RunResult, Error> {
pub(crate) fn invoke(
&mut self, name: &str, args: &[u32], nres: usize,
) -> Result<RunResult, Error> {
let mut context = self.context();
let Some(func) = self.instance.get_func(&mut context, name) else {
return Err(Error::internal(Code::NotFound));
Expand All @@ -211,14 +215,14 @@ impl Store {
Ok(self.execute())
}

pub fn resume(&mut self, result: u32) -> Result<RunResult, Error> {
pub(crate) fn resume(&mut self, result: u32) -> Result<RunResult, Error> {
assert_eq!(self.calls.len(), self.threads.len());
self.calls.pop();
STATE.put(State::Reply(result));
Ok(self.execute())
}

pub fn last_call(&self) -> Option<&Call> {
pub(crate) fn last_call(&self) -> Option<&Call> {
self.calls.last()
}

Expand Down Expand Up @@ -255,7 +259,7 @@ impl Store {
}
}

pub struct Call {
pub(crate) struct Call {
// This is an owned box. The lifetime is bound to the thread of this call (the one at the same
// index in the store).
caller: ExclusivePtr<Caller<'static, ()>>,
Expand Down Expand Up @@ -304,7 +308,7 @@ impl StoreApi for Store {
}
}

pub struct Memory<'a> {
pub(crate) struct Memory<'a> {
store: *mut Store,
memory: SliceCell<'a, u8>,
}
Expand Down
Loading
Loading