Skip to content

Commit 2a59507

Browse files
committed
fix(runtime): populate globals and memories during instantiation
- Add populate_globals_from_module() to copy globals from module to instance - Add populate_memories_from_module() to copy memories from module to instance - Use Vec instead of BoundedVec for instance collections in std mode to avoid serialization overhead and the MemoryWrapper::from_bytes panic - Update memory/global/table accessor methods for Vec vs BoundedVec API - Call population methods during instantiation in CapabilityAwareEngine - Add tracing debug output for instantiation and memory operations The global "Failed to get global from instance" error is now fixed. Memory writes now work correctly until an invalid address calculation (0xFFFFFFD0) occurs in the WASM code itself.
1 parent b86de62 commit 2a59507

File tree

3 files changed

+243
-66
lines changed

3 files changed

+243
-66
lines changed

wrt-runtime/src/engine/capability_engine.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//! to enforce safety constraints based on the selected preset (QM, ASIL-A,
55
//! ASIL-B).
66
7+
// Import tracing utilities
8+
#[cfg(feature = "tracing")]
9+
use wrt_foundation::tracing::debug;
10+
711
#[cfg(all(feature = "alloc", not(feature = "std")))]
812
use alloc::sync::Arc;
913
use core::sync::atomic::{
@@ -514,17 +518,27 @@ impl CapabilityEngine for CapabilityAwareEngine {
514518
self.context.verify_operation(CrateId::Runtime, &operation)?;
515519

516520
// Create module instance (clone the Arc, not the Module)
517-
let mut instance = ModuleInstance::new(module_arc.clone(), self.next_instance_idx)?;
521+
let instance = ModuleInstance::new(module_arc.clone(), self.next_instance_idx)?;
522+
#[cfg(feature = "tracing")]
523+
debug!("Created ModuleInstance for idx {}", self.next_instance_idx);
518524

519-
// TODO: Copy globals from module to instance (critical for stack pointer initialization!)
520-
// instance.populate_globals_from_module()?;
525+
// Copy globals from module to instance (critical for stack pointer initialization!)
526+
#[cfg(feature = "tracing")]
527+
debug!("Populating globals from module...");
528+
instance.populate_globals_from_module()?;
521529

522-
// TODO: Copy memories from module to instance (critical for memory access!)
523-
// instance.populate_memories_from_module()?;
530+
// Copy memories from module to instance (critical for memory access!)
531+
#[cfg(feature = "tracing")]
532+
debug!("Populating memories from module...");
533+
instance.populate_memories_from_module()?;
524534

525-
// TODO: Initialize data segments into instance memory (critical for static data!)
526-
// #[cfg(feature = "std")]
527-
// instance.initialize_data_segments()?;
535+
// Initialize data segments into instance memory (critical for static data!)
536+
#[cfg(feature = "std")]
537+
{
538+
#[cfg(feature = "tracing")]
539+
debug!("Initializing data segments...");
540+
instance.initialize_data_segments()?;
541+
}
528542

529543
// Apply import links if any exist for this module
530544
// We need the instance_idx before we can register links, so we'll do it after registration

wrt-runtime/src/memory.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,20 @@ impl Memory {
10741074
.checked_add(size)
10751075
.ok_or_else(|| Error::memory_out_of_bounds("Memory write would overflow"))?;
10761076

1077-
if end > self.size_in_bytes() {
1077+
let mem_size = self.size_in_bytes();
1078+
#[cfg(feature = "tracing")]
1079+
{
1080+
use wrt_foundation::tracing::debug;
1081+
debug!("write_shared: offset={}, size={}, end={}, mem_size={}, pages={}",
1082+
offset_usize, size, end, mem_size, self.current_pages.load(Ordering::Relaxed));
1083+
}
1084+
1085+
if end > mem_size {
1086+
#[cfg(feature = "tracing")]
1087+
{
1088+
use wrt_foundation::tracing::error;
1089+
error!("write_shared bounds check FAILED: end {} > mem_size {}", end, mem_size);
1090+
}
10781091
return Err(Error::memory_out_of_bounds("Memory write out of bounds"));
10791092
}
10801093

0 commit comments

Comments
 (0)