Skip to content

Commit

Permalink
Handle defined shared memories in dwarf processing
Browse files Browse the repository at this point in the history
This commit resolves an assert in the dwarf generating of core wasm
modules when the module has a defined linear memory which is flagged
`shared`. This is represented slightly differently in the `VMContext`
than owned memories that aren't `shared`, and looks more like an
imported memory. With support in bytecodealliance#8740 it's now much easier to support
this.

Closes bytecodealliance#8652
  • Loading branch information
alexcrichton committed Jun 5, 2024
1 parent dd8c48b commit 2e65c73
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
28 changes: 12 additions & 16 deletions crates/cranelift/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cranelift_codegen::isa::TargetIsa;
use object::write::SymbolId;
use std::collections::HashMap;
use wasmtime_environ::{
DefinedFuncIndex, EntityRef, MemoryIndex, ModuleTranslation, OwnedMemoryIndex, PrimaryMap,
PtrSize, StaticModuleIndex, Tunables, VMOffsets,
DefinedFuncIndex, DefinedMemoryIndex, EntityRef, MemoryIndex, ModuleTranslation,
OwnedMemoryIndex, PrimaryMap, PtrSize, StaticModuleIndex, Tunables, VMOffsets,
};

/// Memory definition offset in the VMContext structure.
Expand Down Expand Up @@ -88,25 +88,21 @@ impl<'a> Compilation<'a> {
);

let memory_offset = if ofs.num_imported_memories > 0 {
let index = MemoryIndex::new(0);
ModuleMemoryOffset::Imported {
offset_to_vm_memory_definition: ofs.vmctx_vmmemory_import(MemoryIndex::new(0))
offset_to_vm_memory_definition: ofs.vmctx_vmmemory_import(index)
+ u32::from(ofs.vmmemory_import_from()),
offset_to_memory_base: ofs.ptr.vmmemory_definition_base().into(),
}
} else if ofs.num_owned_memories > 0 {
let index = OwnedMemoryIndex::new(0);
ModuleMemoryOffset::Defined(ofs.vmctx_vmmemory_definition_base(index))
} else if ofs.num_defined_memories > 0 {
// The addition of shared memory makes the following assumption,
// "owned memory index = 0", possibly false. If the first memory
// is a shared memory, the base pointer will not be stored in
// the `owned_memories` array. The following code should
// eventually be fixed to not only handle shared memories but
// also multiple memories.
assert_eq!(
ofs.num_defined_memories, ofs.num_owned_memories,
"the memory base pointer may be incorrect due to sharing memory"
);
ModuleMemoryOffset::Defined(
ofs.vmctx_vmmemory_definition_base(OwnedMemoryIndex::new(0)),
)
let index = DefinedMemoryIndex::new(0);
ModuleMemoryOffset::Imported {
offset_to_vm_memory_definition: ofs.vmctx_vmmemory_pointer(index),
offset_to_memory_base: ofs.ptr.vmmemory_definition_base().into(),
}
} else {
ModuleMemoryOffset::None
};
Expand Down
2 changes: 1 addition & 1 deletion crates/test-programs/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn build_and_generate_tests() {
}

// Generate a component from each test.
if kind == "nn" || target == "dwarf_imported_memory" {
if kind == "nn" || target == "dwarf_imported_memory" || target == "dwarf_shared_memory" {
continue;
}
let adapter = match target.as_str() {
Expand Down
2 changes: 2 additions & 0 deletions crates/test-programs/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
fn main() {
println!("cargo:rustc-link-arg-bin=dwarf_imported_memory=--import-memory");
println!("cargo:rustc-link-arg-bin=dwarf_imported_memory=--export-memory");
println!("cargo:rustc-link-arg-bin=dwarf_shared_memory=--no-check-features");
println!("cargo:rustc-link-arg-bin=dwarf_shared_memory=--shared-memory");
}
1 change: 1 addition & 0 deletions crates/test-programs/src/bin/dwarf_shared_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!("./dwarf_simple.rs");
6 changes: 6 additions & 0 deletions tests/all/debug/lldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,10 @@ check: exited with status = 0
&["--preload=env=./tests/all/debug/satisfy_memory_import.wat"],
)
}

#[test]
#[ignore]
fn dwarf_shared_memory() -> Result<()> {
test_dwarf_simple(DWARF_SHARED_MEMORY, &[])
}
}

0 comments on commit 2e65c73

Please sign in to comment.