Browse Source

Add Component::image_range (#7939)

* Add Component::image_range

This is the same as `Module::image_range` but for components. While I'm
here additionally return a pointer instead of a `usize` to further
emphasize that it's in the host's address space.

* Remove unused import

* Fix compilation of the C API
pull/7941/head
Alex Crichton 9 months ago
committed by GitHub
parent
commit
120e6b2395
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      crates/c-api/include/wasmtime/module.h
  2. 4
      crates/c-api/src/module.rs
  3. 8
      crates/runtime/src/mmap_vec.rs
  4. 10
      crates/wasmtime/src/runtime/component/component.rs
  5. 9
      crates/wasmtime/src/runtime/instantiate.rs
  6. 4
      crates/wasmtime/src/runtime/module.rs

4
crates/c-api/include/wasmtime/module.h

@ -145,8 +145,8 @@ wasmtime_module_deserialize_file(wasm_engine_t *engine, const char *path,
* https://docs.wasmtime.dev/api/wasmtime/struct.Module.html#method.image_range * https://docs.wasmtime.dev/api/wasmtime/struct.Module.html#method.image_range
*/ */
WASM_API_EXTERN void WASM_API_EXTERN void
wasmtime_module_image_range(const wasmtime_module_t *module, size_t *start, wasmtime_module_image_range(const wasmtime_module_t *module, void **start,
size_t *end); void **end);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

4
crates/c-api/src/module.rs

@ -186,8 +186,8 @@ pub extern "C" fn wasmtime_module_serialize(
#[no_mangle] #[no_mangle]
pub extern "C" fn wasmtime_module_image_range( pub extern "C" fn wasmtime_module_image_range(
module: &wasmtime_module_t, module: &wasmtime_module_t,
start: &mut usize, start: &mut *const u8,
end: &mut usize, end: &mut *const u8,
) { ) {
let range = module.module.image_range(); let range = module.module.image_range();
*start = range.start; *start = range.start;

8
crates/runtime/src/mmap_vec.rs

@ -122,6 +122,14 @@ impl MmapVec {
pub fn original_offset(&self) -> usize { pub fn original_offset(&self) -> usize {
self.range.start self.range.start
} }
/// Returns the bounds, in host memory, of where this mmap
/// image resides.
pub fn image_range(&self) -> Range<*const u8> {
let base = self.as_ptr();
let len = self.len();
base..base.wrapping_add(len)
}
} }
impl Deref for MmapVec { impl Deref for MmapVec {

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

@ -5,6 +5,7 @@ use crate::{
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use std::fs; use std::fs;
use std::mem; use std::mem;
use std::ops::Range;
use std::path::Path; use std::path::Path;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::sync::Arc; use std::sync::Arc;
@ -510,6 +511,15 @@ impl Component {
} }
Some(resources) Some(resources)
} }
/// Returns the range, in the host's address space, that this module's
/// compiled code resides at.
///
/// For more information see
/// [`Module;:image_range`](crate::Module::image_range).
pub fn image_range(&self) -> Range<*const u8> {
self.inner.code.code_memory().mmap().image_range()
}
} }
impl ComponentRuntimeInfo for ComponentInner { impl ComponentRuntimeInfo for ComponentInner {

9
crates/wasmtime/src/runtime/instantiate.rs

@ -7,7 +7,6 @@ use crate::{code_memory::CodeMemory, profiling_agent::ProfilingAgent};
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use object::write::WritableBuffer; use object::write::WritableBuffer;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::ops::Range;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
use wasmtime_environ::{ use wasmtime_environ::{
@ -319,14 +318,6 @@ impl CompiledModule {
pub fn has_address_map(&self) -> bool { pub fn has_address_map(&self) -> bool {
!self.code_memory.address_map_data().is_empty() !self.code_memory.address_map_data().is_empty()
} }
/// Returns the bounds, in host memory, of where this module's compiled
/// image resides.
pub fn image_range(&self) -> Range<usize> {
let base = self.mmap().as_ptr() as usize;
let len = self.mmap().len();
base..base + len
}
} }
#[cfg(feature = "addr2line")] #[cfg(feature = "addr2line")]

4
crates/wasmtime/src/runtime/module.rs

@ -981,8 +981,8 @@ impl Module {
/// ///
/// It is not safe to modify the memory in this range, nor is it safe to /// It is not safe to modify the memory in this range, nor is it safe to
/// modify the protections of memory in this range. /// modify the protections of memory in this range.
pub fn image_range(&self) -> Range<usize> { pub fn image_range(&self) -> Range<*const u8> {
self.compiled_module().image_range() self.compiled_module().mmap().image_range()
} }
/// Force initialization of copy-on-write images to happen here-and-now /// Force initialization of copy-on-write images to happen here-and-now

Loading…
Cancel
Save