Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ edition = "2021"
[dependencies]
counters = { git = "https://github.com/oxidecomputer/hubris" }
userlib = {git = "https://github.com/oxidecomputer/hubris"}
zerocopy = "0.6.1"
zerocopy = { version = "0.8.25", features = ["derive"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

this is not how the zerocopy folks currently recommend getting the derive macros. From the crate docs:

However, you may experience better compile times if you instead directly depend on both zerocopy and zerocopy-derive in your Cargo.toml, since doing so will allow Rust to compile these crates in parallel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I had overlooked that in their docs, good catch. I'll change it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out idol-runtime doesn't actually use the derives, just the traits. So we can just drop the feature here. However, the codegen will need to be updated if we want the rest of Hubris to derive on the two of them separately.

32 changes: 24 additions & 8 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use userlib::{
sys_reply_fault, FromPrimitive, LeaseAttributes, RecvMessage,
ReplyFaultReason, TaskId,
};
use zerocopy::{AsBytes, FromBytes};
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes};

#[derive(Copy, Clone, Debug, Eq, PartialEq, Count)]
#[repr(u32)]
Expand Down Expand Up @@ -539,7 +539,11 @@ impl<T> Leased<W, [T]> {
/// These functions are available on any readable lease (that is, read-only or
/// read-write) where the content type `T` is `Sized` and can be moved around by
/// naive mem-copy.
impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, T> {
impl<A, T> Leased<A, T>
where
A: AttributeRead,
T: Sized + Copy + FromZeros + FromBytes + IntoBytes,
{
/// Reads the leased value by copy.
///
/// If the lending task has been restarted between the time we checked lease
Expand All @@ -549,7 +553,7 @@ impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, T> {
pub fn read(&self) -> Option<T> {
let mut temp = T::new_zeroed();
let (rc, len) =
sys_borrow_read(self.lender, self.index, 0, temp.as_bytes_mut());
sys_borrow_read(self.lender, self.index, 0, temp.as_mut_bytes());
if rc != 0 || len != core::mem::size_of::<T>() {
None
} else {
Expand All @@ -561,7 +565,11 @@ impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, T> {
/// These functions are available on any readable leased slice (that is,
/// read-only or read-write) where the element type `T` is `Sized` and can be
/// moved around by naive mem-copy.
impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, [T]> {
impl<A, T> Leased<A, [T]>
where
A: AttributeRead,
T: Sized + Copy + FromZeros + FromBytes + IntoBytes,
{
/// Reads a single element of the leased slice by copy.
///
/// Like indexing a native slice, `index` must be less than `self.len()`, or
Expand All @@ -580,7 +588,7 @@ impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, [T]> {
self.lender,
self.index,
offset,
temp.as_bytes_mut(),
temp.as_mut_bytes(),
);
if rc != 0 || len != core::mem::size_of::<T>() {
None
Expand Down Expand Up @@ -615,7 +623,7 @@ impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, [T]> {
self.lender,
self.index,
offset,
dest.as_bytes_mut(),
dest.as_mut_bytes(),
);

if rc != 0 || len != expected_len {
Expand All @@ -629,7 +637,11 @@ impl<A: AttributeRead, T: Sized + Copy + FromBytes + AsBytes> Leased<A, [T]> {
/// These functions are available on any writable lease (that is, write-only or
/// read-write) where the content type `T` is `Sized` and can be moved around by
/// naive mem-copy.
impl<A: AttributeWrite, T: Sized + Copy + AsBytes> Leased<A, T> {
impl<A, T> Leased<A, T>
where
A: AttributeWrite,
T: Sized + Copy + IntoBytes + Immutable,
{
/// Writes the leased value by copy.
///
/// If the lending task has been restarted between the time we checked lease
Expand All @@ -650,7 +662,11 @@ impl<A: AttributeWrite, T: Sized + Copy + AsBytes> Leased<A, T> {
/// These functions are available on any writable leased slice (that is,
/// write-only or read-write) where the element type `T` is `Sized` and can be
/// moved around by naive mem-copy.
impl<A: AttributeWrite, T: Sized + Copy + AsBytes> Leased<A, [T]> {
impl<A, T> Leased<A, [T]>
where
A: AttributeWrite,
T: Sized + Copy + IntoBytes + Immutable,
{
/// Writes a single element of the leased slice by copy.
///
/// Like indexing a native slice, `index` must be less than `self.len()`, or
Expand Down
14 changes: 6 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Generator {
let attrs = match op.encoding {
syntax::Encoding::Zerocopy => {
quote! {
#[derive(zerocopy::AsBytes)]
#[derive(zerocopy::IntoBytes, zerocopy::Immutable)]
#[repr(C, packed)]
}
}
Expand Down Expand Up @@ -323,16 +323,16 @@ impl Generator {
let op_enum_name = iface_name.as_op_enum();
let buf = match op.encoding {
syntax::Encoding::Zerocopy => {
quote! { zerocopy::AsBytes::as_bytes(&args) }
quote! { zerocopy::IntoBytes::as_bytes(&args) }
}
syntax::Encoding::Ssmarshal | syntax::Encoding::Hubpack => {
quote! { &argsbuf[..arglen] }
}
};
let leases = op.leases.iter().map(|(leasename, lease)| {
let (ctor, asbytes) = match (lease.read, lease.write) {
(true, true) => ("read_write", "as_bytes_mut"),
(false, true) => ("write_only", "as_bytes_mut"),
(true, true) => ("read_write", "as_mut_bytes"),
(false, true) => ("write_only", "as_mut_bytes"),
(true, false) => ("read_only", "as_bytes"),
(false, false) => panic!("should have been caught above"),
};
Expand All @@ -342,7 +342,7 @@ impl Generator {
syn::Ident::new(asbytes, proc_macro2::Span::call_site());
let argname = leasename.arg_prefixed();
quote! {
userlib::Lease::#ctor(zerocopy::AsBytes::#asbytes(#argname))
userlib::Lease::#ctor(zerocopy::IntoBytes::#asbytes(#argname))
}
});
quote! {
Expand Down Expand Up @@ -372,9 +372,7 @@ impl Generator {
struct #reply_ty {
value: #repr_ty,
}
let lv = zerocopy::LayoutVerified::<_, #reply_ty>::new_unaligned(&reply[..])
.unwrap_lite();
let v: #repr_ty = lv.value;
let v: #repr_ty = zerocopy::FromBytes::read_from_bytes(&reply[..]).unwrap_lite();
}
}
syntax::Encoding::Ssmarshal => quote! {
Expand Down
14 changes: 10 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ impl Generator {
let attrs = match op.encoding {
syntax::Encoding::Zerocopy => quote! {
#[repr(C, packed)]
#[derive(Copy, Clone, zerocopy::FromBytes, zerocopy::Unaligned)]
#[derive(
Copy,
Clone,
zerocopy::FromBytes,
zerocopy::KnownLayout,
zerocopy::Immutable,
zerocopy::Unaligned,
)]
},
syntax::Encoding::Ssmarshal => quote! {
#[derive(Copy, Clone, serde::Deserialize)]
Expand Down Expand Up @@ -307,8 +314,7 @@ impl Generator {
match op.encoding {
syntax::Encoding::Zerocopy => quote! {
pub fn #read_fn(bytes: &[u8]) -> Option<&#struct_name> {
Some(zerocopy::LayoutVerified::<_, #struct_name>::new_unaligned(bytes)?
.into_ref())
zerocopy::FromBytes::ref_from_bytes(bytes).ok()
}
},
syntax::Encoding::Ssmarshal => quote! {
Expand Down Expand Up @@ -551,7 +557,7 @@ impl Generator {
let reply = {
let encode = match op.encoding {
syntax::Encoding::Zerocopy => quote! {
userlib::sys_reply(rm.sender, 0, zerocopy::AsBytes::as_bytes(&val));
userlib::sys_reply(rm.sender, 0, zerocopy::IntoBytes::as_bytes(&val));
},
syntax::Encoding::Hubpack | syntax::Encoding::Ssmarshal => {
let reply_size = opname.as_reply_size();
Expand Down