Skip to content

Commit 35e3e49

Browse files
authored
wasmparser(CM+GC): Assert that SubtypeCxs are always using the same typing context (#2170)
A `SubtypeCx`, used for subtype-checking in the component model, is constructed with two `TypesRef`s. These `TypesRef`s must be associated with the same validator, so that we know that core types are canonicalized the same across the two of them and we can compare `CoreTypeId`s for equality. I had originally hoped to move the `SubtypeCx` to wrapping a single `TypesRef`, but this cannot work because (unlike core types) new component model types can be pushed into each different `TypesRef` for different components.
1 parent c493b87 commit 35e3e49

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

crates/wasmparser/src/validator/component_types.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,13 @@ pub enum ComponentEntityType {
730730

731731
impl ComponentEntityType {
732732
/// Determines if component entity type `a` is a subtype of `b`.
733-
pub fn is_subtype_of(a: &Self, at: TypesRef, b: &Self, bt: TypesRef) -> bool {
733+
///
734+
/// # Panics
735+
///
736+
/// Panics if the two given `TypesRef`s are not associated with the same
737+
/// `Validator`.
738+
pub fn is_subtype_of(a: &Self, at: TypesRef<'_>, b: &Self, bt: TypesRef<'_>) -> bool {
739+
assert_eq!(at.id(), bt.id());
734740
SubtypeCx::new(at.list, bt.list)
735741
.component_entity_type(a, b, 0)
736742
.is_ok()
@@ -2150,6 +2156,9 @@ where
21502156
{
21512157
/// Pushes a new anonymous type within this object, returning an identifier
21522158
/// which can be used to refer to it.
2159+
///
2160+
/// For internal use only!
2161+
#[doc(hidden)]
21532162
fn push_ty<T>(&mut self, ty: T) -> T::Id
21542163
where
21552164
T: TypeData;
@@ -2496,7 +2505,13 @@ macro_rules! limits_match {
24962505

24972506
impl<'a> SubtypeCx<'a> {
24982507
/// Create a new instance with the specified type lists
2508+
///
2509+
/// # Panics
2510+
///
2511+
/// Panics if the two given `TypesRef`s are not associated with the same
2512+
/// `Validator`.
24992513
pub fn new_with_refs(a: TypesRef<'a>, b: TypesRef<'a>) -> SubtypeCx<'a> {
2514+
assert_eq!(a.id(), b.id());
25002515
Self::new(a.list, b.list)
25012516
}
25022517

crates/wasmparser/src/validator/types.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,13 @@ impl<T> Index<usize> for SnapshotList<T> {
783783

784784
#[inline]
785785
fn index(&self, index: usize) -> &T {
786-
self.get(index).unwrap()
786+
match self.get(index) {
787+
Some(x) => x,
788+
None => panic!(
789+
"out-of-bounds indexing into `SnapshotList`: index is {index}, but length is {}",
790+
self.len()
791+
),
792+
}
787793
}
788794
}
789795

0 commit comments

Comments
 (0)