Skip to content

Commit 5566d52

Browse files
authored
Fix Module::image_range to include non-text part of module. (#12302)
* Fix `Module::image_range` to include non-text part of module. This method is informational for embedders to be able to, for example, ensure an image in memory is `mlock`'d (not swapped out). In the refactors around the StoreCode/EngineCode split, I mistakenly redefined this to only the text section. This is not a Wasm execution correctness issue but may lead to performance issues if an embedder relies on this behavior. This PR fixes the definition. * Components as well as core modules. * Ignore new tests in miri.
1 parent ec76a6b commit 5566d52

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

crates/wasmtime/src/runtime/component/component.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,7 @@ impl Component {
682682
/// For more information see
683683
/// [`Module::image_range`](crate::Module::image_range).
684684
pub fn image_range(&self) -> Range<*const u8> {
685-
let range = self.inner.code.text_range();
686-
let start = range.start.raw() as *const u8;
687-
let end = range.end.raw() as *const u8;
688-
start..end
685+
self.inner.code.image().as_ptr_range()
689686
}
690687

691688
/// Force initialization of copy-on-write images to happen here-and-now
@@ -911,7 +908,7 @@ impl InstanceExportLookup for ComponentExportIndex {
911908
#[cfg(test)]
912909
mod tests {
913910
use crate::component::Component;
914-
use crate::{Config, Engine};
911+
use crate::{CodeBuilder, Config, Engine};
915912
use wasmtime_environ::MemoryInitialization;
916913

917914
#[test]
@@ -937,4 +934,27 @@ mod tests {
937934
assert!(matches!(init, MemoryInitialization::Static { .. }));
938935
}
939936
}
937+
938+
#[test]
939+
#[cfg_attr(miri, ignore)]
940+
fn image_range_is_whole_image() {
941+
let wat = r#"
942+
(component
943+
(core module
944+
(memory 1)
945+
(data (i32.const 0) "1234")
946+
(func (export "f") (param i32) (result i32)
947+
local.get 0)))
948+
"#;
949+
let engine = Engine::default();
950+
let mut builder = CodeBuilder::new(&engine);
951+
builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap();
952+
let bytes = builder.compile_component_serialized().unwrap();
953+
954+
let comp = unsafe { Component::deserialize(&engine, &bytes).unwrap() };
955+
let image_range = comp.image_range();
956+
let len = image_range.end.addr() - image_range.start.addr();
957+
// Length may be strictly greater if it becomes page-aligned.
958+
assert!(len >= bytes.len());
959+
}
940960
}

crates/wasmtime/src/runtime/module.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,7 @@ impl Module {
972972
/// Note that depending on the engine configuration, this image
973973
/// range may not actually be the code that is directly executed.
974974
pub fn image_range(&self) -> Range<*const u8> {
975-
let range = self.engine_code().text_range();
976-
let start = range.start.raw() as *const u8;
977-
let end = range.end.raw() as *const u8;
978-
start..end
975+
self.engine_code().image().as_ptr_range()
979976
}
980977

981978
/// Force initialization of copy-on-write images to happen here-and-now
@@ -1238,7 +1235,7 @@ impl crate::vm::ModuleMemoryImageSource for CodeMemory {
12381235

12391236
#[cfg(test)]
12401237
mod tests {
1241-
use crate::{Engine, Module};
1238+
use crate::{CodeBuilder, Engine, Module};
12421239
use wasmtime_environ::MemoryInitialization;
12431240

12441241
#[test]
@@ -1258,4 +1255,26 @@ mod tests {
12581255
let init = &module.env_module().memory_initialization;
12591256
assert!(matches!(init, MemoryInitialization::Static { .. }));
12601257
}
1258+
1259+
#[test]
1260+
#[cfg_attr(miri, ignore)]
1261+
fn image_range_is_whole_image() {
1262+
let wat = r#"
1263+
(module
1264+
(memory 1)
1265+
(data (i32.const 0) "1234")
1266+
(func (export "f") (param i32) (result i32)
1267+
local.get 0))
1268+
"#;
1269+
let engine = Engine::default();
1270+
let mut builder = CodeBuilder::new(&engine);
1271+
builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap();
1272+
let bytes = builder.compile_module_serialized().unwrap();
1273+
1274+
let module = unsafe { Module::deserialize(&engine, &bytes).unwrap() };
1275+
let image_range = module.image_range();
1276+
let len = image_range.end.addr() - image_range.start.addr();
1277+
// Length may be strictly greater if it becomes page-aligned.
1278+
assert!(len >= bytes.len());
1279+
}
12611280
}

0 commit comments

Comments
 (0)