Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions crates/wasmtime/src/runtime/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ impl Global {
store.id() == self.store
}

/// Returns a stable identifier for this global within its store.
///
/// This allows distinguishing globals when introspecting them
/// e.g. via debug APIs.
#[cfg(feature = "debug")]
pub fn debug_index_in_store(&self) -> u64 {
match self.kind {
VMGlobalKind::Instance(idx) => u64::from(self.instance) << 32 | u64::from(idx.as_u32()),
VMGlobalKind::Host(idx) => u64::from(u32::MAX) << 32 | u64::from(idx.as_u32()),
#[cfg(feature = "component-model")]
VMGlobalKind::ComponentFlags(idx) => {
u64::from(self.instance) << 32 | u64::from(idx.as_u32())
}
#[cfg(feature = "component-model")]
VMGlobalKind::TaskMayBlock => u64::from(self.instance) << 32 | u64::from(u32::MAX),
}
}

/// Get a stable hash key for this global.
///
/// Even if the same underlying global definition is added to the
Expand Down
9 changes: 9 additions & 0 deletions crates/wasmtime/src/runtime/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,15 @@ impl Table {
store.id() == self.instance.store_id()
}

/// Returns a stable identifier for this table within its store.
///
/// This allows distinguishing tables when introspecting them
/// e.g. via debug APIs.
#[cfg(feature = "debug")]
pub fn debug_index_in_store(&self) -> u64 {
u64::from(self.instance.instance().as_u32()) << 32 | u64::from(self.index.as_u32())
}

/// Get a stable hash key for this table.
///
/// Even if the same underlying table definition is added to the
Expand Down
9 changes: 9 additions & 0 deletions crates/wasmtime/src/runtime/externals/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ impl Tag {
store.id() == self.instance.store_id()
}

/// Returns a stable identifier for this tag within its store.
///
/// This allows distinguishing tags when introspecting them
/// e.g. via debug APIs.
#[cfg(feature = "debug")]
pub fn debug_index_in_store(&self) -> u64 {
u64::from(self.instance.instance().as_u32()) << 32 | u64::from(self.index.as_u32())
}

/// Determines whether this tag is reference equal to the other
/// given tag in the given store.
///
Expand Down
12 changes: 12 additions & 0 deletions crates/wasmtime/src/runtime/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@ impl Instance {
self.id.instance()
}

/// Return a unique-within-Store index for this `Instance`.
///
/// Allows distinguishing instance identities when introspecting
/// the `Store`, e.g. via debug APIs.
///
/// This index will match the instance's position in the sequence
/// returned by `Store::debug_all_instances()`.
#[cfg(feature = "debug")]
pub fn debug_index_in_store(&self) -> u32 {
self.id.instance().as_u32()
}

/// Get all globals within this instance.
///
/// Returns both import and defined globals.
Expand Down
9 changes: 9 additions & 0 deletions crates/wasmtime/src/runtime/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ impl Memory {
store.id() == self.instance.store_id()
}

/// Returns a stable identifier for this memory within its store.
///
/// This allows distinguishing memories when introspecting them
/// e.g. via debug APIs.
#[cfg(feature = "debug")]
pub fn debug_index_in_store(&self) -> u64 {
u64::from(self.instance.instance().as_u32()) << 32 | u64::from(self.index.as_u32())
}

/// Get a stable hash key for this memory.
///
/// Even if the same underlying memory definition is added to the
Expand Down
9 changes: 9 additions & 0 deletions crates/wasmtime/src/runtime/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,15 @@ impl Module {
&self.inner.offsets
}

/// Return the unique-within-Engine ID for this module.
///
/// Allows distinguishing module identities when introspecting
/// modules, e.g. via debug APIs.
#[cfg(feature = "debug")]
pub fn debug_index_in_engine(&self) -> u64 {
self.id().as_u64()
}

/// Return the address, in memory, of the trampoline that allows Wasm to
/// call a array function of the given signature.
///
Expand Down
5 changes: 5 additions & 0 deletions crates/wasmtime/src/runtime/vm/module_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ impl CompiledModuleId {
// uniqueness.
CompiledModuleId(crate::store::StoreId::allocate().as_raw())
}

/// Returns the inner unique integer contained in this ID.
pub fn as_u64(self) -> u64 {
self.0.get()
}
}
118 changes: 118 additions & 0 deletions tests/all/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,3 +1200,121 @@ fn component_bytecode() -> wasmtime::Result<()> {

Ok(())
}

#[test]
#[cfg_attr(miri, ignore)]
fn debug_ids() -> wasmtime::Result<()> {
let mut config = Config::default();
config.guest_debug(true);
config.wasm_exceptions(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
let module1 = Module::new(
&engine,
r#"
(module
(memory 1 1)
(memory 1 1)
(global (mut i32) (i32.const 0))
(global (mut i32) (i32.const 1))
(table 1 1 funcref)
(table 1 1 funcref)
(tag (param i32))
(tag (param i64)))
"#,
)?;

let module2 = Module::new(
&engine,
r#"
(module
(memory (export "m") 1 1))
"#,
)?;

let instance1 = Instance::new(&mut store, &module1, &[])?;
let instance2 = Instance::new(&mut store, &module2, &[])?;
let instance3 = Instance::new(&mut store, &module1, &[])?;

assert_ne!(
module1.debug_index_in_engine(),
module2.debug_index_in_engine()
);
assert_ne!(
instance1.debug_index_in_store(),
instance2.debug_index_in_store()
);
assert_ne!(
instance1
.debug_memory(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance1
.debug_memory(&mut store, 1)
.unwrap()
.debug_index_in_store()
);
assert_ne!(
instance1
.debug_memory(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance2
.debug_memory(&mut store, 0)
.unwrap()
.debug_index_in_store()
);
assert_ne!(
instance1
.debug_memory(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance3
.debug_memory(&mut store, 0)
.unwrap()
.debug_index_in_store()
);
assert_ne!(
instance1
.debug_global(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance3
.debug_global(&mut store, 0)
.unwrap()
.debug_index_in_store()
);
assert_ne!(
instance1
.debug_table(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance3
.debug_table(&mut store, 0)
.unwrap()
.debug_index_in_store()
);
assert_ne!(
instance1
.debug_tag(&mut store, 0)
.unwrap()
.debug_index_in_store(),
instance3
.debug_tag(&mut store, 0)
.unwrap()
.debug_index_in_store()
);

let m_via_export = instance2
.get_export(&mut store, "m")
.unwrap()
.into_memory()
.unwrap();
let m_via_introspection = instance2.debug_memory(&mut store, 0).unwrap();
assert_eq!(
m_via_export.debug_index_in_store(),
m_via_introspection.debug_index_in_store()
);

Ok(())
}