Skip to content

Commit c493b87

Browse files
authored
wasmparser(CM+GC): Assert that we never define new core types inside SubtypeArenas (#2168)
1 parent b6fdcc8 commit c493b87

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

crates/wasmparser/src/validator/component_types.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ pub enum ComponentValType {
534534

535535
impl TypeData for ComponentValType {
536536
type Id = ComponentValueTypeId;
537-
537+
const IS_CORE_SUB_TYPE: bool = false;
538538
fn type_info(&self, types: &TypeList) -> TypeInfo {
539539
match self {
540540
ComponentValType::Primitive(_) => TypeInfo::new(),
@@ -641,7 +641,7 @@ pub struct ModuleType {
641641

642642
impl TypeData for ModuleType {
643643
type Id = ComponentCoreModuleTypeId;
644-
644+
const IS_CORE_SUB_TYPE: bool = false;
645645
fn type_info(&self, _types: &TypeList) -> TypeInfo {
646646
self.info
647647
}
@@ -677,7 +677,7 @@ pub struct InstanceType {
677677

678678
impl TypeData for InstanceType {
679679
type Id = ComponentCoreInstanceTypeId;
680-
680+
const IS_CORE_SUB_TYPE: bool = false;
681681
fn type_info(&self, _types: &TypeList) -> TypeInfo {
682682
self.info
683683
}
@@ -817,7 +817,7 @@ pub struct ComponentType {
817817

818818
impl TypeData for ComponentType {
819819
type Id = ComponentTypeId;
820-
820+
const IS_CORE_SUB_TYPE: bool = false;
821821
fn type_info(&self, _types: &TypeList) -> TypeInfo {
822822
self.info
823823
}
@@ -875,7 +875,7 @@ pub struct ComponentInstanceType {
875875

876876
impl TypeData for ComponentInstanceType {
877877
type Id = ComponentInstanceTypeId;
878-
878+
const IS_CORE_SUB_TYPE: bool = false;
879879
fn type_info(&self, _types: &TypeList) -> TypeInfo {
880880
self.info
881881
}
@@ -894,7 +894,7 @@ pub struct ComponentFuncType {
894894

895895
impl TypeData for ComponentFuncType {
896896
type Id = ComponentFuncTypeId;
897-
897+
const IS_CORE_SUB_TYPE: bool = false;
898898
fn type_info(&self, _types: &TypeList) -> TypeInfo {
899899
self.info
900900
}
@@ -1080,7 +1080,7 @@ pub enum ComponentDefinedType {
10801080

10811081
impl TypeData for ComponentDefinedType {
10821082
type Id = ComponentDefinedTypeId;
1083-
1083+
const IS_CORE_SUB_TYPE: bool = false;
10841084
fn type_info(&self, types: &TypeList) -> TypeInfo {
10851085
match self {
10861086
Self::Primitive(_)
@@ -3364,6 +3364,10 @@ impl Remap for SubtypeArena<'_> {
33643364
where
33653365
T: TypeData,
33663366
{
3367+
assert!(
3368+
!T::IS_CORE_SUB_TYPE,
3369+
"cannot push core sub types into `SubtypeArena`s, that would break type canonicalization"
3370+
);
33673371
let index = T::Id::list(&self.list).len() + T::Id::list(self.types).len();
33683372
let index = u32::try_from(index).unwrap();
33693373
self.list.push(ty);

crates/wasmparser/src/validator/types.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub trait TypeData: core::fmt::Debug {
5252
/// The identifier for this type data.
5353
type Id: TypeIdentifier<Data = Self>;
5454

55+
/// Is this type a core sub type (or rec group of sub types)?
56+
const IS_CORE_SUB_TYPE: bool;
57+
5558
/// Get the info for this type.
5659
#[doc(hidden)]
5760
fn type_info(&self, types: &TypeList) -> TypeInfo;
@@ -134,7 +137,7 @@ impl TypeIdentifier for CoreTypeId {
134137

135138
impl TypeData for SubType {
136139
type Id = CoreTypeId;
137-
140+
const IS_CORE_SUB_TYPE: bool = true;
138141
fn type_info(&self, _types: &TypeList) -> TypeInfo {
139142
// TODO(#1036): calculate actual size for func, array, struct.
140143
let size = 1 + match &self.composite_type.inner {
@@ -156,7 +159,7 @@ define_type_id!(
156159

157160
impl TypeData for Range<CoreTypeId> {
158161
type Id = RecGroupId;
159-
162+
const IS_CORE_SUB_TYPE: bool = true;
160163
fn type_info(&self, _types: &TypeList) -> TypeInfo {
161164
let size = self.end.index() - self.start.index();
162165
TypeInfo::core(u32::try_from(size).unwrap())

0 commit comments

Comments
 (0)