Skip to content

Commit 938c177

Browse files
authored
Wasmtime: fix PartialEq implementation for RegisteredType (bytecodealliance#10091)
We were incorrectly checking whether they were in the same rec group (i.e. whether the two `RegisteredType`s had the same `RecGroupEntry`) rather than whether they were actually the same type or not, even if they were in the same rec group. This fixes one of two issues reported in bytecodealliance#9714
1 parent 1e6d533 commit 938c177

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

crates/wasmtime/src/engine.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ struct EngineInner {
7070
compatible_with_native_host: OnceLock<Result<(), String>>,
7171
}
7272

73+
impl core::fmt::Debug for Engine {
74+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75+
f.debug_tuple("Engine")
76+
.field(&Arc::as_ptr(&self.inner))
77+
.finish()
78+
}
79+
}
80+
7381
impl Default for Engine {
7482
fn default() -> Engine {
7583
Engine::new(&Config::default()).unwrap()

crates/wasmtime/src/runtime/type_registry.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ impl core::ops::Deref for RegisteredType {
263263

264264
impl PartialEq for RegisteredType {
265265
fn eq(&self, other: &Self) -> bool {
266-
let eq = Arc::ptr_eq(&self.entry.0, &other.entry.0);
266+
let eq = self.index == other.index && Engine::same(&self.engine, &other.engine);
267267

268268
if cfg!(debug_assertions) {
269269
if eq {
270-
assert!(Engine::same(&self.engine, &other.engine));
270+
assert!(Arc::ptr_eq(&self.entry.0, &other.entry.0));
271271
assert_eq!(self.ty, other.ty);
272272
} else {
273273
assert!(self.ty != other.ty || !Engine::same(&self.engine, &other.engine));
@@ -365,6 +365,9 @@ impl RegisteredType {
365365
ty: Arc<WasmSubType>,
366366
layout: Option<GcLayout>,
367367
) -> Self {
368+
log::trace!(
369+
"RegisteredType::from_parts({engine:?}, {entry:?}, {index:?}, {ty:?}, {layout:?})"
370+
);
368371
debug_assert!(entry.0.registrations.load(Acquire) != 0);
369372
RegisteredType {
370373
engine,

tests/all/arrays.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1+
use super::gc_store;
12
use wasmtime::*;
23

3-
fn gc_store() -> Result<Store<()>> {
4-
let _ = env_logger::try_init();
5-
6-
let mut config = Config::new();
7-
config.wasm_function_references(true);
8-
config.wasm_gc(true);
9-
10-
let engine = Engine::new(&config)?;
11-
Ok(Store::new(&engine, ()))
12-
}
13-
144
#[test]
155
fn array_new_empty() -> Result<()> {
166
let mut store = gc_store()?;

tests/all/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,14 @@ pub(crate) fn small_pool_config() -> wasmtime::PoolingAllocationConfig {
106106

107107
config
108108
}
109+
110+
pub(crate) fn gc_store() -> wasmtime::Result<wasmtime::Store<()>> {
111+
let _ = env_logger::try_init();
112+
113+
let mut config = wasmtime::Config::new();
114+
config.wasm_function_references(true);
115+
config.wasm_gc(true);
116+
117+
let engine = wasmtime::Engine::new(&config)?;
118+
Ok(wasmtime::Store::new(&engine, ()))
119+
}

tests/all/structs.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1+
use super::gc_store;
12
use wasmtime::*;
23

3-
fn gc_store() -> Result<Store<()>> {
4-
let _ = env_logger::try_init();
5-
6-
let mut config = Config::new();
7-
config.wasm_function_references(true);
8-
config.wasm_gc(true);
9-
10-
let engine = Engine::new(&config)?;
11-
Ok(Store::new(&engine, ()))
12-
}
13-
144
#[test]
155
fn struct_new_empty() -> Result<()> {
166
let mut store = gc_store()?;
@@ -737,3 +727,26 @@ fn can_put_funcrefs_in_structs() -> Result<()> {
737727

738728
Ok(())
739729
}
730+
731+
#[test]
732+
#[cfg_attr(miri, ignore)]
733+
fn struct_ref_struct_in_same_rec_group_in_global() -> Result<()> {
734+
let mut store = gc_store()?;
735+
let module = Module::new(
736+
store.engine(),
737+
r#"
738+
(module
739+
(rec
740+
(type $b (struct))
741+
(type $a (struct (field (ref $b))))
742+
)
743+
(global (ref $a)
744+
struct.new_default $b
745+
struct.new $a
746+
)
747+
)
748+
"#,
749+
)?;
750+
let _instance = Instance::new(&mut store, &module, &[])?;
751+
Ok(())
752+
}

0 commit comments

Comments
 (0)