-
Notifications
You must be signed in to change notification settings - Fork 133
Add component bindgen macros #376
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c01407
Add trait to abstract over sandboxes in host function registration
syntactically dd55d85
Add trait to abstract over sandboxes in guest function calling
syntactically e2ff7ed
[hyperlight_common] Add resource table abstraction
syntactically ff92209
[component_util] Add Component Model elaboration/bindgen utilities
syntactically e23332d
[component_macro] Add host_bindgen!() and guest_bindgen!()
syntactically 34dd941
Add simple WIT guest and roundtrip tests
syntactically d8d1f3f
Publish the new hyperlight-component-* crates
syntactically 3143032
Update Cargo.lock
syntactically File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,6 @@ pub mod mem; | |
|
||
/// cbindgen:ignore | ||
pub mod outb; | ||
|
||
/// cbindgen:ignore | ||
pub mod resource; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
Copyright 2025 The Hyperlight Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
//! Shared operations around resources | ||
|
||
// "Needless" lifetimes are useful for clarity | ||
#![allow(clippy::needless_lifetimes)] | ||
|
||
use alloc::sync::Arc; | ||
|
||
#[cfg(feature = "std")] | ||
extern crate std; | ||
use core::marker::{PhantomData, Send}; | ||
use core::ops::Deref; | ||
#[cfg(feature = "std")] | ||
use std::sync::{RwLock, RwLockReadGuard}; | ||
|
||
#[cfg(not(feature = "std"))] | ||
use spin::{RwLock, RwLockReadGuard}; | ||
|
||
/// The semantics of component model resources are, pleasingly, | ||
/// roughly compatible with those of Rust references, so we would like | ||
/// to use the more-or-less directly in interfaces generated by | ||
/// hyperlight_component_macro. Less pleasingly, it's not terribly | ||
/// easy to show the semantic agreement statically. | ||
/// | ||
/// In particular, if the host calls into the guest and gives it a | ||
/// borrow of a resource, reentrant host function calls that use that | ||
/// borrow need to be able to resolve the original reference and use | ||
/// it in an appropriately scoped manner, but it is not simple to do | ||
/// this, because the core Hyperlight machinery doesn't offer an easy | ||
/// way to augment the host's context for the span of time of a guest | ||
/// function call. This may be worth revisiting at some time, but in | ||
/// the meantime, it's easier to just do it dynamically. | ||
/// | ||
/// # Safety | ||
/// Informally: this only creates SharedRead references, so having a | ||
/// bunch of them going at once is fine. Safe Rust in the host can't | ||
/// use any earlier borrows (potentially invalidating these) until | ||
/// borrow passed into [`ResourceEntry::lend`] has expired. Because | ||
/// that borrow outlives the [`LentResourceGuard`], it will not expire | ||
/// until that destructor is called. That destructor ensures that (a) | ||
/// there are no outstanding [`BorrowedResourceGuard`]s alive (since | ||
/// they would be holding the read side of the [`RwLock`] if they | ||
/// were), and that (b) the shared flag has been set to false, so | ||
/// [`ResourceEntry::borrow`] will never create another borrow | ||
pub enum ResourceEntry<T> { | ||
syntactically marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Empty, | ||
Owned(T), | ||
Borrowed(Arc<RwLock<bool>>, *const T), | ||
} | ||
unsafe impl<T: Send> Send for ResourceEntry<T> {} | ||
|
||
pub struct LentResourceGuard<'a> { | ||
flag: Arc<RwLock<bool>>, | ||
already_revoked: bool, | ||
_phantom: core::marker::PhantomData<&'a mut ()>, | ||
} | ||
impl<'a> LentResourceGuard<'a> { | ||
pub fn revoke_nonblocking(&mut self) -> bool { | ||
syntactically marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg(feature = "std")] | ||
let Ok(mut flag) = self.flag.try_write() else { | ||
return false; | ||
}; | ||
#[cfg(not(feature = "std"))] | ||
let Some(mut flag) = self.flag.try_write() else { | ||
return false; | ||
}; | ||
*flag = false; | ||
self.already_revoked = true; | ||
true | ||
} | ||
} | ||
impl<'a> Drop for LentResourceGuard<'a> { | ||
fn drop(&mut self) { | ||
if !self.already_revoked { | ||
#[allow(unused_mut)] // it isn't actually unused | ||
let mut guard = self.flag.write(); | ||
#[cfg(feature = "std")] | ||
// If a mutex that is just protecting us from our own | ||
// mistakes is poisoned, something is so seriously | ||
// wrong that dying is a sensible response. | ||
danbugs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[allow(clippy::unwrap_used)] | ||
{ | ||
*guard.unwrap() = false; | ||
} | ||
#[cfg(not(feature = "std"))] | ||
{ | ||
*guard = false; | ||
} | ||
} | ||
} | ||
} | ||
pub struct BorrowedResourceGuard<'a, T> { | ||
_flag: Option<RwLockReadGuard<'a, bool>>, | ||
reference: &'a T, | ||
} | ||
impl<'a, T> Deref for BorrowedResourceGuard<'a, T> { | ||
type Target = T; | ||
fn deref(&self) -> &T { | ||
self.reference | ||
} | ||
} | ||
impl<T> ResourceEntry<T> { | ||
pub fn give(x: T) -> ResourceEntry<T> { | ||
ResourceEntry::Owned(x) | ||
} | ||
pub fn lend<'a>(x: &'a T) -> (LentResourceGuard<'a>, ResourceEntry<T>) { | ||
let flag = Arc::new(RwLock::new(true)); | ||
( | ||
LentResourceGuard { | ||
flag: flag.clone(), | ||
already_revoked: false, | ||
_phantom: PhantomData {}, | ||
}, | ||
ResourceEntry::Borrowed(flag, x as *const T), | ||
) | ||
} | ||
pub fn borrow<'a>(&'a self) -> Option<BorrowedResourceGuard<'a, T>> { | ||
match self { | ||
ResourceEntry::Empty => None, | ||
ResourceEntry::Owned(t) => Some(BorrowedResourceGuard { | ||
_flag: None, | ||
reference: t, | ||
}), | ||
ResourceEntry::Borrowed(flag, t) => { | ||
let guard = flag.read(); | ||
// If a mutex that is just protecting us from our own | ||
// mistakes is poisoned, something is so seriously | ||
// wrong that dying is a sensible response. | ||
#[allow(clippy::unwrap_used)] | ||
danbugs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let flag = { | ||
#[cfg(feature = "std")] | ||
{ | ||
guard.unwrap() | ||
} | ||
#[cfg(not(feature = "std"))] | ||
{ | ||
guard | ||
} | ||
}; | ||
if *flag { | ||
Some(BorrowedResourceGuard { | ||
_flag: Some(flag), | ||
reference: unsafe { &**t }, | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
} | ||
pub fn take(&mut self) -> Option<T> { | ||
match core::mem::replace(self, ResourceEntry::Empty) { | ||
ResourceEntry::Owned(t) => Some(t), | ||
_ => None, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.