Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix `Module::image_range` to include non-text part of module. (#12302)

Chris Fallin5566d52077e2
diff --git a/crates/wasmtime/src/runtime/component/component.rs b/crates/wasmtime/src/runtime/component/component.rs
index d53889a3d0..495f859b4b 100644
--- a/crates/wasmtime/src/runtime/component/component.rs
+++ b/crates/wasmtime/src/runtime/component/component.rs
@@ -682,10 +682,7 @@ impl Component {
/// For more information see
/// [`Module::image_range`](crate::Module::image_range).
pub fn image_range(&self) -> Range<*const u8> {
- let range = self.inner.code.text_range();
- let start = range.start.raw() as *const u8;
- let end = range.end.raw() as *const u8;
- start..end
+ self.inner.code.image().as_ptr_range()
}
/// Force initialization of copy-on-write images to happen here-and-now
@@ -911,7 +908,7 @@ impl InstanceExportLookup for ComponentExportIndex {
#[cfg(test)]
mod tests {
use crate::component::Component;
- use crate::{Config, Engine};
+ use crate::{CodeBuilder, Config, Engine};
use wasmtime_environ::MemoryInitialization;
#[test]
@@ -937,4 +934,27 @@ mod tests {
assert!(matches!(init, MemoryInitialization::Static { .. }));
}
}
+
+ #[test]
+ #[cfg_attr(miri, ignore)]
+ fn image_range_is_whole_image() {
+ let wat = r#"
+ (component
+ (core module
+ (memory 1)
+ (data (i32.const 0) "1234")
+ (func (export "f") (param i32) (result i32)
+ local.get 0)))
+ "#;
+ let engine = Engine::default();
+ let mut builder = CodeBuilder::new(&engine);
+ builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap();
+ let bytes = builder.compile_component_serialized().unwrap();
+
+ let comp = unsafe { Component::deserialize(&engine, &bytes).unwrap() };
+ let image_range = comp.image_range();
+ let len = image_range.end.addr() - image_range.start.addr();
+ // Length may be strictly greater if it becomes page-aligned.
+ assert!(len >= bytes.len());
+ }
}
diff --git a/crates/wasmtime/src/runtime/module.rs b/crates/wasmtime/src/runtime/module.rs
index 28231d82e5..dbca042d1f 100644
--- a/crates/wasmtime/src/runtime/module.rs
+++ b/crates/wasmtime/src/runtime/module.rs
@@ -972,10 +972,7 @@ impl Module {
/// Note that depending on the engine configuration, this image
/// range may not actually be the code that is directly executed.
pub fn image_range(&self) -> Range<*const u8> {
- let range = self.engine_code().text_range();
- let start = range.start.raw() as *const u8;
- let end = range.end.raw() as *const u8;
- start..end
+ self.engine_code().image().as_ptr_range()
}
/// Force initialization of copy-on-write images to happen here-and-now
@@ -1238,7 +1235,7 @@ impl crate::vm::ModuleMemoryImageSource for CodeMemory {
#[cfg(test)]
mod tests {
- use crate::{Engine, Module};
+ use crate::{CodeBuilder, Engine, Module};
use wasmtime_environ::MemoryInitialization;
#[test]
@@ -1258,4 +1255,26 @@ mod tests {
let init = &module.env_module().memory_initialization;
assert!(matches!(init, MemoryInitialization::Static { .. }));
}
+
+ #[test]
+ #[cfg_attr(miri, ignore)]
+ fn image_range_is_whole_image() {
+ let wat = r#"
+ (module
+ (memory 1)
+ (data (i32.const 0) "1234")
+ (func (export "f") (param i32) (result i32)
+ local.get 0))
+ "#;
+ let engine = Engine::default();
+ let mut builder = CodeBuilder::new(&engine);
+ builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap();
+ let bytes = builder.compile_module_serialized().unwrap();
+
+ let module = unsafe { Module::deserialize(&engine, &bytes).unwrap() };
+ let image_range = module.image_range();
+ let len = image_range.end.addr() - image_range.start.addr();
+ // Length may be strictly greater if it becomes page-aligned.
+ assert!(len >= bytes.len());
+ }
}