Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Support `repr(C, _)` enums in `for_each_vm_type!` (#15048)

Nick Fitzgerald2b41856d66ae
diff --git a/crates/cranelift/src/alias_region.rs b/crates/cranelift/src/alias_region.rs
index 9f469aeab8..3bca3dd4da 100644
--- a/crates/cranelift/src/alias_region.rs
+++ b/crates/cranelift/src/alias_region.rs
@@ -10,7 +10,8 @@
//!
//! 1. Fields of the `VM*` types and of the vmctx types get one region per
//! field, and an accessor per field is generated from our
-//! `for_each_vm[ctx]_type!` macros.
+//! `for_each_vm[ctx]_type!` macros. An `enum` gets only two: one for its
+//! tag, one shared by all variants' fields, which overlap in memory.
//!
//! 2. There are a handful of hand-written methods for memory regions that are
//! not part of the vmctx or a `VM*` type at all (linear memories, the GC
@@ -98,7 +99,8 @@ enum AliasRegionKey {
/// The type of VM data structure being accessed.
ty: VmType,
/// The offset of the accessed field *within* the `ty` structure (or
- /// the base offset of the array, for array fields).
+ /// the base offset of the array, for array fields, or the payload
+ /// offset, for an enum's variants' fields).
offset: u32,
},
@@ -442,12 +444,26 @@ impl<'a, Offsets> Field<'a, Offsets> {
offset: u32,
flags: ir::MemFlagsData,
ty: ir::Type,
+ ) -> Self {
+ Self::new_in_region(regions, vm_type, offset, offset, flags, ty)
+ }
+
+ /// As `new`, but keyed on `region_offset` rather than the field's own
+ /// `offset`, so that fields overlapping in memory (an enum's variants'
+ /// fields) share one region instead of claiming they cannot alias.
+ fn new_in_region(
+ regions: &'a mut AliasRegions<Offsets>,
+ vm_type: VmType,
+ region_offset: u32,
+ offset: u32,
+ flags: ir::MemFlagsData,
+ ty: ir::Type,
) -> Self {
Field {
regions,
key: AliasRegionKey::Vm {
ty: vm_type,
- offset,
+ offset: region_offset,
},
offset,
flags,
@@ -658,6 +674,8 @@ macro_rules! define_vm_type_alias_region_helpers {
(@field_ty $pt:expr, i64) => { ir::types::I64 };
(@field_ty $pt:expr, u64) => { ir::types::I64 };
(@field_ty $pt:expr, u32) => { ir::types::I32 };
+ (@field_ty $pt:expr, u16) => { ir::types::I16 };
+ (@field_ty $pt:expr, u8) => { ir::types::I8 };
(@field_ty $pt:expr, NonZeroU32) => { ir::types::I32 };
(@field_ty $pt:expr, Option < VMGcRef >) => { ir::types::I32 };
// `VMLazyThread` is a pointer-sized bitpacked integer; see its definition in
@@ -835,19 +853,237 @@ macro_rules! define_vm_type_alias_region_helpers {
define_vm_type_alias_region_helpers!(@munch_ty $Name $snake { $($groups)* } $fkind $fname [ $($fattr)* ] [ $($fty)* $tok ] $($rest)*);
};
- // Top-level entry: the list of `VM*` type definitions.
+ // Emit the accessor `struct` and per-field methods for one enum variant,
+ // so that `alias_regions.vm_foo().variant_a().field()` mirrors the
+ // generated offset accessors.
+ (@emit_variant $Name:ident $snake:ident $VariantAccess:ident $vsnake:ident {
+ $( { $fkind:ident $fname:ident [ $($fattr:tt)* ] [ $($fty:tt)* ] } )*
+ }) => {
+ #[doc = concat!(
+ "An [`AliasRegions`] accessor for the fields of the `",
+ stringify!($Name), "::", stringify!($VariantAccess), "` variant."
+ )]
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ pub struct $VariantAccess<'a, Offsets> {
+ regions: &'a mut AliasRegions<Offsets>,
+ }
+
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ impl<'a, Offsets> $Name<'a, Offsets> {
+ #[doc = concat!(
+ "Get an accessor for the fields of the `", stringify!($Name),
+ "::", stringify!($VariantAccess), "` variant."
+ )]
+ pub fn $vsnake(self) -> $VariantAccess<'a, Offsets> {
+ $VariantAccess { regions: self.regions }
+ }
+ }
+
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ impl<'a, Offsets> $VariantAccess<'a, Offsets>
+ where
+ Offsets: GetPtrSize,
+ {
+ $(
+ define_vm_type_alias_region_helpers!(
+ @variant_method $Name $snake $vsnake $fkind $fname
+ [ $($fattr)* ] [ $($fty)* ]
+ );
+ )*
+ }
+ };
+
+ // As `@method` above, but the offset comes from the variant's own offsets
+ // and the region is the enum's shared payload one.
+ (@variant_method $Name:ident $snake:ident $vsnake:ident plain $fname:ident
+ [ $($fattr:tt)* ] [ $($fty:tt)* ]
+ ) => {
+ #[doc = concat!(
+ "Get the [`Field`] for the `", stringify!($fname),
+ "` field of this variant of `", stringify!($Name), "`.\n\n\
+ All variants' fields share one alias region."
+ )]
+ pub fn $fname(self) -> Field<'a, Offsets> {
+ let ptr_size = self.regions.offsets.get_ptr_size();
+ let payload = u32::from(ptr_size.$snake().payload());
+ let offset = u32::from(ptr_size.$snake().$vsnake().$fname());
+ let flags = ir::MemFlagsData::trusted();
+ $(
+ let flags = define_vm_type_alias_region_helpers!(
+ @apply_attr flags, $fattr
+ );
+ )*
+ let ty = define_vm_type_alias_region_helpers!(
+ @field_ty self.regions.pointer_type, $($fty)*
+ );
+ Field::new_in_region(self.regions, VmType::$Name, payload, offset, flags, ty)
+ }
+ };
+
+ // Emit the accessor `struct` for a `#[repr(C, u32)]` enum: its `u32` tag,
+ // plus one nested accessor type per variant.
+ //
+ // An enum gets two regions, not one per field: variants' fields occupy the
+ // same payload bytes, so they can alias both across and within a variant.
+ (@emit_enum $Name:ident $snake:ident $tag:ident {
+ $( { $VariantAccess:ident $vsnake:ident { $($vgroups:tt)* } } )*
+ }) => {
+ #[doc = concat!(
+ "An [`AliasRegions`] accessor for the tag and variants of the `",
+ stringify!($Name), "` enum."
+ )]
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ pub struct $Name<'a, Offsets> {
+ regions: &'a mut AliasRegions<Offsets>,
+ }
+
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ impl<Offsets> AliasRegions<Offsets> {
+ #[doc = concat!(
+ "Get an accessor for the tag and variants of a `",
+ stringify!($Name), "`."
+ )]
+ pub fn $snake(&mut self) -> $Name<'_, Offsets> {
+ $Name { regions: self }
+ }
+ }
+
+ #[allow(
+ dead_code,
+ reason = "generated uniformly for all VM types; not all accessors are used yet"
+ )]
+ impl<'a, Offsets> $Name<'a, Offsets>
+ where
+ Offsets: GetPtrSize,
+ {
+ #[doc = concat!(
+ "Get the [`Field`] for the `", stringify!($tag), "` tag of `",
+ stringify!($Name), "`.\n\n\
+ The tag has its own alias region, distinct from the variants'."
+ )]
+ pub fn tag(self) -> Field<'a, Offsets> {
+ let offset = u32::from(
+ self.regions.offsets.get_ptr_size().$snake().tag()
+ );
+ let flags = ir::MemFlagsData::trusted();
+ let ty = define_vm_type_alias_region_helpers!(
+ @field_ty self.regions.pointer_type, $tag
+ );
+ Field::new(self.regions, VmType::$Name, offset, flags, ty)
+ }
+ }
+
+ $(
+ define_vm_type_alias_region_helpers!(
+ @emit_variant $Name $snake $VariantAccess $vsnake { $($vgroups)* }
+ );
+ )*
+ };
+
+ // Peel variants off an enum's body one at a time; `@munch_variant` handles
+ // each variant's own body and hands the remainder back.
+ (@munch_enum $Name:ident $snake:ident $tag:ident { $($vs:tt)* }) => {
+ define_vm_type_alias_region_helpers!(@emit_enum $Name $snake $tag { $($vs)* });
+ };
+ (@munch_enum $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $(#[doc = $vdoc:literal])*
+ #[snake_name = $vsnake:ident]
+ $Variant:ident { $($vbody:tt)* } = $disc:literal ,
+ $($rest:tt)*
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_variant $Name $snake $tag { $($vs)* } $Variant $vsnake {}
+ [ $($vbody)* ] [ $($rest)* ]
+ );
+ };
+
+ // As `@munch` for a struct body, resuming `@munch_enum` on the remainder.
+ (@munch_variant $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $Variant:ident $vsnake:ident { $($g:tt)* } [] [ $($rest:tt)* ]
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_enum $Name $snake $tag
+ { $($vs)* { $Variant $vsnake { $($g)* } } }
+ $($rest)*
+ );
+ };
+ // As in `@munch`, an `#[aggregate]` field gets no accessor.
+ (@munch_variant $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $Variant:ident $vsnake:ident { $($g:tt)* }
+ [ $(#[doc = $fdoc:literal])* #[aggregate] $fname:ident : $fty:ty , $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_variant $Name $snake $tag { $($vs)* } $Variant $vsnake { $($g)* }
+ [ $($vrest)* ] $rest
+ );
+ };
+ (@munch_variant $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $Variant:ident $vsnake:ident { $($g:tt)* }
+ [ $(# $fattr:tt)* $fname:ident : $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_variant_ty $Name $snake $tag { $($vs)* } $Variant $vsnake { $($g)* }
+ $fname [ $($fattr)* ] [] [ $($vrest)* ] $rest
+ );
+ };
+ (@munch_variant_ty $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $Variant:ident $vsnake:ident { $($g:tt)* }
+ $fname:ident [ $($fattr:tt)* ] [ $($fty:tt)* ] [ , $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_variant $Name $snake $tag { $($vs)* } $Variant $vsnake
+ { $($g)* { plain $fname [ $($fattr)* ] [ $($fty)* ] } }
+ [ $($vrest)* ] $rest
+ );
+ };
+ (@munch_variant_ty $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $Variant:ident $vsnake:ident { $($g:tt)* }
+ $fname:ident [ $($fattr:tt)* ] [ $($fty:tt)* ] [ $tok:tt $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_alias_region_helpers!(
+ @munch_variant_ty $Name $snake $tag { $($vs)* } $Variant $vsnake { $($g)* }
+ $fname [ $($fattr)* ] [ $($fty)* $tok ] [ $($vrest)* ] $rest
+ );
+ };
+
+ // Dispatch on `struct` vs `enum`.
+ (@def struct $Name:ident $snake:ident $repr:tt { $($body:tt)* }) => {
+ define_vm_type_alias_region_helpers!(@munch $Name $snake {} $($body)*);
+ };
+ // See the matching arm in `define_vm_type_offsets!`.
+ (@def enum $Name:ident $snake:ident [ C, $tag:ident ] { $($body:tt)* }) => {
+ define_vm_type_alias_region_helpers!(@munch_enum $Name $snake $tag {} $($body)*);
+ };
+
+ // Top-level entry: the list of `VM*` type definitions. Each is either a
+ // `struct` or a `#[repr(C, u32)]` `enum`; `@def` dispatches on which.
( $(
$(#[doc = $sdoc:literal])*
$(#[cfg($($scfg:tt)*)])?
$(#[derive($($d:ident),*)])?
#[repr($($repr:tt)*)]
#[snake_name = $snake:ident]
- $svis:vis struct $Name:ident {
+ $svis:vis $kind:ident $Name:ident {
$($body:tt)*
}
)* ) => {
$(
- define_vm_type_alias_region_helpers!(@munch $Name $snake {} $($body)*);
+ define_vm_type_alias_region_helpers!(@def $kind $Name $snake [$($repr)*] { $($body)* });
)*
};
}
diff --git a/crates/environ/src/vmoffsets.rs b/crates/environ/src/vmoffsets.rs
index 67c0065d32..7820cd796d 100644
--- a/crates/environ/src/vmoffsets.rs
+++ b/crates/environ/src/vmoffsets.rs
@@ -34,6 +34,16 @@ fn align(offset: u32, width: u32) -> u32 {
/// Generate the [`offsets`] module: a `struct VMFoo<P: PtrSize>(P)` for each
/// `VM*` type, with a method per field returning that field's offset, plus
/// `size` and `align` methods.
+///
+/// A `#[repr(C, $tag)]` enum additionally gets `tag`/`tag_size`/`payload` and
+/// a nested `offsets::<snake_name>::Variant<P>` type per variant, holding that
+/// variant's fields' offsets within the enum as a whole.
+#[allow(
+ unused_macro_rules,
+ reason = "the enum arms go unused whenever no `VM*` type is an enum, and \
+ the type classifiers are written for all field types rather than \
+ only those some type happens to use"
+)]
macro_rules! define_vm_type_offsets {
// `UnsafeCell<T>` is `repr(transparent)`, so it has exactly `T`'s layout;
// delegate to the inner type.
@@ -51,6 +61,8 @@ macro_rules! define_vm_type_offsets {
(@size ($p:expr) i64) => { 8u32 };
(@size ($p:expr) u64) => { 8u32 };
(@size ($p:expr) u32) => { 4u32 };
+ (@size ($p:expr) u16) => { 2u32 };
+ (@size ($p:expr) u8) => { 1u32 };
// `VMGcRef` is a `NonZeroU32` newtype, so `Option<VMGcRef>` is niche-packed
// back down into four bytes.
(@size ($p:expr) NonZeroU32) => { 4u32 };
@@ -104,6 +116,8 @@ macro_rules! define_vm_type_offsets {
(@align ($p:expr) i64) => { 8u32 };
(@align ($p:expr) u64) => { 8u32 };
(@align ($p:expr) u32) => { 4u32 };
+ (@align ($p:expr) u16) => { 2u32 };
+ (@align ($p:expr) u8) => { 1u32 };
(@align ($p:expr) NonZeroU32) => { 4u32 };
(@align ($p:expr) Option < VMGcRef >) => { 4u32 };
(@align ($p:expr) [u32; $n:expr]) => { 4u32 };
@@ -135,6 +149,13 @@ macro_rules! define_vm_type_offsets {
(@repr_align transparent) => { 1u32 };
(@repr_align C, align($n:literal)) => {{ let align: u32 = $n; align }};
+ // Classify a `#[repr(C, $tag)]` enum's tag type to its size in bytes, which
+ // for these types is also its alignment.
+ (@tag_size u8) => { 1u32 };
+ (@tag_size u16) => { 2u32 };
+ (@tag_size u32) => { 4u32 };
+ (@tag_size u64) => { 8u32 };
+
// Emit a `pub fn` per field returning that field's offset, computed by
// accumulating the aligned size of each preceding field. `$p`/`$o` are
// caller-minted identifiers (for the pointer size and running offset) that
@@ -235,14 +256,237 @@ macro_rules! define_vm_type_offsets {
define_vm_type_offsets!(@impl_ty $Name $repr { $($groups)* } $fname [ $($fty)* $tok ] $($rest)*);
};
- // Top-level entry: the list of `VM*` type definitions.
+ // Dispatch on `struct` vs `enum`.
+ (@def struct $Name:ident $snake:ident $repr:tt { $($body:tt)* }) => {
+ define_vm_type_offsets!(@impl $Name $repr {} $($body)*);
+ };
+ // `C` guarantees the tag-plus-union layout.
+ (@def enum $Name:ident $snake:ident [ C, $tag:ident ] { $($body:tt)* }) => {
+ define_vm_type_offsets!(@enum_variants $Name $snake $tag {} $($body)*);
+ };
+
+ // Peel variants off an enum's body one at a time. Each variant's own body
+ // is split by `@enum_fields`, so the enum body's unconsumed remainder rides
+ // along in brackets until that recursion hands it back.
+ (@enum_variants $Name:ident $snake:ident $tag:ident { $($vs:tt)* }) => {
+ define_vm_type_offsets!(@enum_emit $Name $snake $tag { $($vs)* });
+ };
+ (@enum_variants $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $(#[doc = $vdoc:literal])*
+ #[snake_name = $vsnake:ident]
+ $Variant:ident { $($vbody:tt)* } = $disc:literal ,
+ $($rest:tt)*
+ ) => {
+ define_vm_type_offsets!(
+ @enum_fields $Name $snake $tag { $($vs)* }
+ $vsnake $Variant $disc {}
+ [ $($vbody)* ] [ $($rest)* ]
+ );
+ };
+
+ // As `@impl`/`@impl_ty` for a struct body, resuming `@enum_variants` on the
+ // enum body's remainder once the variant is exhausted.
+ (@enum_fields $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $vsnake:ident $Variant:ident $disc:literal { $($g:tt)* }
+ [] [ $($rest:tt)* ]
+ ) => {
+ define_vm_type_offsets!(
+ @enum_variants $Name $snake $tag
+ { $($vs)* { $vsnake $Variant $disc { $($g)* } } }
+ $($rest)*
+ );
+ };
+ (@enum_fields $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $vsnake:ident $Variant:ident $disc:literal { $($g:tt)* }
+ [ $(#[$($fattr:tt)*])* $fname:ident : $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_offsets!(
+ @enum_field_ty $Name $snake $tag { $($vs)* }
+ $vsnake $Variant $disc { $($g)* }
+ $fname [] [ $($vrest)* ] $rest
+ );
+ };
+ (@enum_field_ty $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $vsnake:ident $Variant:ident $disc:literal { $($g:tt)* }
+ $fname:ident [ $($fty:tt)* ] [ , $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_offsets!(
+ @enum_fields $Name $snake $tag { $($vs)* }
+ $vsnake $Variant $disc { $($g)* [ $fname : $($fty)* ] }
+ [ $($vrest)* ] $rest
+ );
+ };
+ (@enum_field_ty $Name:ident $snake:ident $tag:ident { $($vs:tt)* }
+ $vsnake:ident $Variant:ident $disc:literal { $($g:tt)* }
+ $fname:ident [ $($fty:tt)* ] [ $tok:tt $($vrest:tt)* ] $rest:tt
+ ) => {
+ define_vm_type_offsets!(
+ @enum_field_ty $Name $snake $tag { $($vs)* }
+ $vsnake $Variant $disc { $($g)* }
+ $fname [ $($fty)* $tok ] [ $($vrest)* ] $rest
+ );
+ };
+
+ // Emit an enum's `offsets::VMFoo` inherent `impl`, plus a `mod $snake`
+ // holding one `$Variant<P>` offsets type per variant.
+ //
+ // The tag is at offset zero, every variant's fields start at the same
+ // payload offset (the tag's size rounded up to the enum's alignment), and
+ // within a variant the fields are laid out as a struct's are.
+ (@enum_emit $Name:ident $snake:ident $tag:ident {
+ $(
+ { $vsnake:ident $Variant:ident $disc:literal {
+ $( [ $fname:ident : $($fty:tt)* ] )*
+ } }
+ )*
+ }) => {
+ impl<P: PtrSize> $Name<P> {
+ #[doc = concat!(
+ "The offset of the `", stringify!($tag), "` tag of `",
+ stringify!($Name), "`."
+ )]
+ #[inline]
+ pub fn tag(&self) -> u8 {
+ 0
+ }
+
+ #[doc = concat!(
+ "The size, in bytes, of the `", stringify!($tag), "` tag of `",
+ stringify!($Name), "`."
+ )]
+ #[inline]
+ pub fn tag_size(&self) -> u8 {
+ u8::try_from(define_vm_type_offsets!(@tag_size $tag)).unwrap()
+ }
+
+ #[doc = concat!(
+ "The offset at which every variant of `", stringify!($Name),
+ "` lays out its fields."
+ )]
+ #[inline]
+ pub fn payload(&self) -> u8 {
+ let tag_size = u32::from(self.tag_size());
+ u8::try_from(align(tag_size, u32::from(self.align()))).unwrap()
+ }
+
+ #[doc = concat!("The alignment of the `", stringify!($Name), "` type.")]
+ #[inline]
+ pub fn align(&self) -> u8 {
+ let p = self.0.size();
+ // The tag sets the floor on the enum's alignment.
+ let a: u32 = define_vm_type_offsets!(@tag_size $tag);
+ $($(
+ let a = core::cmp::max(
+ a,
+ define_vm_type_offsets!(@align (p) $($fty)*),
+ );
+ )*)*
+ let _ = p;
+ u8::try_from(a).unwrap()
+ }
+
+ #[doc = concat!("The size of the `", stringify!($Name), "` type.")]
+ #[inline]
+ pub fn size(&self) -> u8 {
+ let p = self.0.size();
+ let payload = u32::from(self.payload());
+ let s: u32 = payload;
+ $(
+ let o: u32 = payload;
+ $(
+ let o = align(o, define_vm_type_offsets!(@align (p) $($fty)*));
+ let o = o + define_vm_type_offsets!(@size (p) $($fty)*);
+ )*
+ let s = core::cmp::max(s, o);
+ )*
+ let s = align(s, u32::from(self.align()));
+ let _ = p;
+ u8::try_from(s).unwrap()
+ }
+
+ $(
+ #[doc = concat!(
+ "Get the offsets of the `", stringify!($Variant),
+ "` variant's fields."
+ )]
+ #[inline]
+ pub fn $vsnake(&self) -> $snake::$Variant<&P> {
+ $snake::$Variant($Name(&self.0))
+ }
+ )*
+ }
+
+ #[doc = concat!(
+ "Offsets of the fields of each variant of the `", stringify!($Name),
+ "` enum."
+ )]
+ pub mod $snake {
+ use super::{$Name, PtrSize, align};
+
+ $(
+ #[doc = concat!(
+ "Offsets of fields within the `", stringify!($Name), "::",
+ stringify!($Variant), "` variant."
+ )]
+ pub struct $Variant<P: PtrSize>(pub $Name<P>);
+
+ impl<P: PtrSize> $Variant<P> {
+ #[doc = concat!(
+ "The value of the `u32` tag that identifies the `",
+ stringify!($Variant), "` variant."
+ )]
+ #[inline]
+ pub fn tag_value(&self) -> u32 {
+ $disc
+ }
+
+ define_vm_type_offsets!(@vfields $Name $Variant (p, o) prefix()
+ $( [ $fname : $($fty)* ] )*
+ );
+ }
+ )*
+ }
+ };
+
+ // As `@fields` above, except that the running offset starts at the enum's
+ // payload offset.
+ (@vfields $Name:ident $Variant:ident ($p:ident, $o:ident) prefix( $($prefix:tt)* )) => {};
+ (@vfields $Name:ident $Variant:ident ($p:ident, $o:ident) prefix( $($prefix:tt)* )
+ [ $fname:ident : $($fty:tt)* ]
+ $($rest:tt)*
+ ) => {
+ #[doc = concat!(
+ "The offset of the `", stringify!($fname),
+ "` field of `", stringify!($Name), "::", stringify!($Variant), "`."
+ )]
+ #[inline]
+ pub fn $fname(&self) -> u8 {
+ let $p = (self.0).0.size();
+ let $o: u32 = u32::from(self.0.payload());
+ $($prefix)*
+ let $o = align($o, define_vm_type_offsets!(@align ($p) $($fty)*));
+ let _ = $p;
+ u8::try_from($o).unwrap()
+ }
+ define_vm_type_offsets!(@vfields $Name $Variant ($p, $o)
+ prefix(
+ $($prefix)*
+ let $o = align($o, define_vm_type_offsets!(@align ($p) $($fty)*));
+ let $o = $o + define_vm_type_offsets!(@size ($p) $($fty)*);
+ )
+ $($rest)*
+ );
+ };
+
+ // Top-level entry: the list of `VM*` type definitions. Each is either a
+ // `struct` or a `#[repr(C, u32)]` `enum`; `@def` dispatches on which.
( $(
$(#[doc = $sdoc:literal])*
$(#[cfg($($scfg:tt)*)])?
$(#[derive($($d:ident),*)])?
#[repr($($repr:tt)*)]
#[snake_name = $snake:ident]
- $svis:vis struct $Name:ident {
+ $svis:vis $kind:ident $Name:ident {
$($body:tt)*
}
)* ) => {
@@ -250,7 +494,7 @@ macro_rules! define_vm_type_offsets {
#[doc = concat!("Offsets of fields within the `", stringify!($Name), "` type.")]
pub struct $Name<P: PtrSize>(pub P);
- define_vm_type_offsets!(@impl $Name [$($repr)*] {} $($body)*);
+ define_vm_type_offsets!(@def $kind $Name $snake [$($repr)*] { $($body)* });
)*
};
}
@@ -420,7 +664,7 @@ macro_rules! define_ptr_size_vm_type_accessors {
$(#[derive($($d:ident),*)])?
#[repr($($repr:tt)*)]
#[snake_name = $snake:ident]
- $svis:vis struct $Name:ident {
+ $svis:vis $kind:ident $Name:ident {
$($body:tt)*
}
)* ) => {
diff --git a/crates/environ/src/vmtypes.rs b/crates/environ/src/vmtypes.rs
index 834517f198..5885775922 100644
--- a/crates/environ/src/vmtypes.rs
+++ b/crates/environ/src/vmtypes.rs
@@ -16,7 +16,7 @@
///
/// # Grammar
///
-/// Each type is emitted as a struct definition preceded by:
+/// Each type is either a `struct` or a `#[repr(C, _)]` `enum`, preceded by:
///
/// * Doc-comment attributes (`#[doc = "..."]`).
///
@@ -54,6 +54,40 @@
///
/// * `#[readonly]` and/or `#[can_move]`: describe how Cranelift may treat loads
/// and stores of that field.
+///
+/// ## Enums
+///
+/// A type may also be an `enum`, which must be `#[repr(C, $tag)]` for an
+/// integer `$tag`:
+///
+/// ```ignore
+/// #[derive(Debug)]
+/// #[repr(C, u32)]
+/// #[snake_name = vm_example]
+/// pub enum VMExample {
+/// #[snake_name = variant_a]
+/// VariantA {
+/// field: u32,
+/// } = 0,
+///
+/// #[snake_name = variant_b]
+/// VariantB {
+/// other: u64,
+/// } = 1,
+/// }
+/// ```
+///
+/// Rust lays such an enum out as a `#[repr(C)]` struct of a `$tag` tag followed
+/// by a union of the variants, which is what lets the same offset machinery
+/// describe it. The `C` is what guarantees that, so it is required.
+///
+/// Beyond a struct's rules, each variant needs an explicit discriminant (the
+/// tag values compiled code compares against), braces even when empty, and its
+/// own `#[snake_name]` for its generated accessors. Variant fields take a
+/// struct field's marker attributes but no visibility.
+///
+/// An enum gets two alias regions rather than a struct's one per field: one for
+/// the tag, one shared by all variants' fields, because they overlap.
#[macro_export]
macro_rules! for_each_vm_type {
($mac:ident) => {
diff --git a/crates/wasmtime/src/runtime/vm/vmcontext.rs b/crates/wasmtime/src/runtime/vm/vmcontext.rs
index 00f80fe232..d29d86a419 100644
--- a/crates/wasmtime/src/runtime/vm/vmcontext.rs
+++ b/crates/wasmtime/src/runtime/vm/vmcontext.rs
@@ -195,15 +195,15 @@ unsafe impl VmSafe for VMGlobalKind {}
unsafe impl VmSafe for VMTagImport {}
/// Define the runtime definitions of the shared `VM*` types.
-#[cfg_attr(
- not(test),
- allow(
- unused_macro_rules,
- reason = "the `@test` arms are only expanded inside the `#[cfg(test)]` \
- layout-test module"
- )
+#[allow(
+ unused_macro_rules,
+ reason = "the `@test` arms are only expanded inside the `#[cfg(test)]` \
+ layout-test module, and the enum arms go unused whenever no \
+ `VM*` type is an enum"
)]
macro_rules! define_vm_types {
+ // Emit the layout test for one `struct` VM type: its size and alignment,
+ // plus each field's offset, checked against the real Rust type.
(@test [ $($cfg:tt)* ] $Name:ident $snake:ident [ $($fname:ident)* ]) => {
$($cfg)*
#[test]
@@ -245,7 +245,180 @@ macro_rules! define_vm_types {
}
};
- ( $(
+ // Emit the layout test for one `#[repr(C, $tag)]` enum VM type.
+ //
+ // `offset_of!` does not work on enum variants, so instead of measuring a
+ // variant field's offset this checks the generated offsets against the
+ // layout rule `#[repr(C, $tag)]` guarantees, evaluated with each field
+ // type's real `size_of`/`align_of`.
+ (@test_enum [ $($cfg:tt)* ] $Name:ident $snake:ident $tag:ty [
+ $( { $vsnake:ident $Variant:ident [ $( $fname:ident : $fty:ty , )* ] } )*
+ ]) => {
+ $($cfg)*
+ #[test]
+ fn $snake() {
+ use super::$Name;
+
+ let host = HostPtr;
+ let offsets = host.$snake();
+
+ let expected = usize::from(offsets.size());
+ let actual = size_of::<$Name>();
+ assert_eq!(
+ expected,
+ actual,
+ "size of {} failed: {expected} (expected) != {actual} (actual)",
+ stringify!($Name),
+ );
+
+ let expected = usize::from(offsets.align());
+ let actual = align_of::<$Name>();
+ assert_eq!(
+ expected,
+ actual,
+ "alignment of {} failed: {expected} (expected) != {actual} (actual)",
+ stringify!($Name),
+ );
+
+ let expected = usize::from(offsets.tag_size());
+ let actual = size_of::<$tag>();
+ assert_eq!(
+ expected,
+ actual,
+ "tag size of {} failed: {expected} (expected) != {actual} (actual)",
+ stringify!($Name),
+ );
+
+ // The payload of a `#[repr(C, $tag)]` enum starts just past the
+ // tag, rounded up to the enum's alignment.
+ let expected = usize::from(offsets.payload());
+ let actual = align_up(size_of::<$tag>(), align_of::<$Name>());
+ assert_eq!(
+ expected,
+ actual,
+ "payload offset of {} failed: {expected} (expected) != {actual} (actual)",
+ stringify!($Name),
+ );
+
+ $(
+ // A variant with no fields leaves both untouched.
+ #[allow(
+ unused_assignments,
+ unused_mut,
+ unused_variables,
+ reason = "a variant may have no fields, and the last \
+ field's size is accumulated but never read"
+ )]
+ {
+ let variant = offsets.$vsnake();
+ let mut o = usize::from(offsets.payload());
+ $(
+ o = align_up(o, align_of::<$fty>());
+ let expected = usize::from(variant.$fname());
+ assert_eq!(
+ expected,
+ o,
+ "offset of {}::{}::{} failed: \
+ {expected} (expected) != {o} (actual)",
+ stringify!($Name),
+ stringify!($Variant),
+ stringify!($fname),
+ );
+ o += size_of::<$fty>();
+ )*
+ }
+ )*
+ }
+ };
+
+ // Munched one at a time rather than matched as a repetition because a
+ // `struct` and an `enum` do not share a body shape.
+ (@munch { $($defs:tt)* } { $($tests:tt)* }) => {
+ $($defs)*
+
+ #[cfg(test)]
+ #[allow(
+ unused_imports,
+ dead_code,
+ reason = "the glob import and `align_up` are only used by the enum \
+ tests, and not every `VM*` type is an enum"
+ )]
+ mod test_vm_type_layouts {
+ // The enum tests name variant fields' types, so the module needs
+ // everything the definitions above were written against.
+ use super::*;
+ use core::mem::{align_of, offset_of, size_of};
+ use wasmtime_environ::{HostPtr, PtrSize};
+
+ /// Round `offset` up to a multiple of `align`.
+ fn align_up(offset: usize, align: usize) -> usize {
+ offset.next_multiple_of(align)
+ }
+
+ $($tests)*
+ }
+ };
+
+ (@munch { $($defs:tt)* } { $($tests:tt)* }
+ $(#[doc = $sdoc:literal])*
+ $(#[cfg($($scfg:tt)*)])?
+ $(#[derive($($d:ident),*)])?
+ #[repr(C, $tag:ident)]
+ #[snake_name = $snake:ident]
+ $svis:vis enum $Name:ident {
+ $(
+ $(#[doc = $vdoc:literal])*
+ #[snake_name = $vsnake:ident]
+ $Variant:ident {
+ $(
+ $(#[doc = $fdoc:literal])*
+ $(#[aggregate])?
+ $(#[indexed])?
+ $(#[readonly])?
+ $(#[can_move])?
+ $fname:ident : $fty:ty ,
+ )*
+ } = $disc:literal ,
+ )*
+ }
+ $($rest:tt)*
+ ) => {
+ define_vm_types!(
+ @munch
+ {
+ $($defs)*
+
+ $(#[doc = $sdoc])*
+ $(#[cfg($($scfg)*)])?
+ $(#[derive($($d),*)])?
+ #[repr(C, $tag)]
+ $svis enum $Name {
+ $(
+ $(#[doc = $vdoc])*
+ $Variant {
+ $(
+ $(#[doc = $fdoc])*
+ $fname: $fty,
+ )*
+ } = $disc,
+ )*
+ }
+ }
+ {
+ $($tests)*
+
+ define_vm_types!(
+ @test_enum
+ [ $(#[cfg($($scfg)*)])? ]
+ $Name $snake $tag
+ [ $( { $vsnake $Variant [ $( $fname : $fty , )* ] } )* ]
+ );
+ }
+ $($rest)*
+ );
+ };
+
+ (@munch { $($defs:tt)* } { $($tests:tt)* }
$(#[doc = $sdoc:literal])*
$(#[cfg($($scfg:tt)*)])?
$(#[derive($($d:ident),*)])?
@@ -264,34 +437,40 @@ macro_rules! define_vm_types {
$fvis:vis $fname:ident : $fty:ty ,
)*
}
- )* ) => {
- $(
- $(#[doc = $sdoc])*
- $(#[cfg($($scfg)*)])?
- $(#[derive($($d),*)])?
- #[repr($($repr)*)]
- $svis struct $Name {
- $(
- $(#[doc = $fdoc])*
- $fvis $fname: $fty,
- )*
+ $($rest:tt)*
+ ) => {
+ define_vm_types!(
+ @munch
+ {
+ $($defs)*
+
+ $(#[doc = $sdoc])*
+ $(#[cfg($($scfg)*)])?
+ $(#[derive($($d),*)])?
+ #[repr($($repr)*)]
+ $svis struct $Name {
+ $(
+ $(#[doc = $fdoc])*
+ $fvis $fname: $fty,
+ )*
+ }
}
- )*
-
- #[cfg(test)]
- mod test_vm_type_layouts {
- use core::mem::{align_of, offset_of, size_of};
- use wasmtime_environ::{HostPtr, PtrSize};
+ {
+ $($tests)*
- $(
define_vm_types!(
@test
[ $(#[cfg($($scfg)*)])? ]
$Name $snake
[ $($fname)* ]
);
- )*
- }
+ }
+ $($rest)*
+ );
+ };
+
+ ( $($defs:tt)* ) => {
+ define_vm_types!(@munch {} {} $($defs)*);
};
}
wasmtime_environ::for_each_vm_type!(define_vm_types);