From 5d4483674f25fddf87c0842ac37afcd0fd0b64cb Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Tue, 19 Sep 2023 15:57:13 -0500 Subject: [PATCH] c-api: Expose image_range for modules We're using this to monitor the amount of executable memory each module needs. Signed-off-by: Tyler Rockwood --- crates/c-api/include/wasmtime/module.h | 15 +++++++++++++++ crates/c-api/src/module.rs | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/crates/c-api/include/wasmtime/module.h b/crates/c-api/include/wasmtime/module.h index deb6bcec1efa..8eca72a4899e 100644 --- a/crates/c-api/include/wasmtime/module.h +++ b/crates/c-api/include/wasmtime/module.h @@ -148,6 +148,21 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_module_deserialize_file( wasmtime_module_t **ret ); + +/** + * \brief Returns the range of bytes in memory where this module’s compilation image resides. + * + * The compilation image for a module contains executable code, data, debug information, etc. + * This is roughly the same as the wasmtime_module_serialize but not the exact same. + * + * For more details see: https://docs.wasmtime.dev/api/wasmtime/struct.Module.html#method.image_range + */ +WASM_API_EXTERN void wasmtime_module_image_range( + const wasm_module_t *module, + size_t *start, + size_t *end +); + #ifdef __cplusplus } // extern "C" #endif diff --git a/crates/c-api/src/module.rs b/crates/c-api/src/module.rs index a1dce264931d..3a1a6e6664cc 100644 --- a/crates/c-api/src/module.rs +++ b/crates/c-api/src/module.rs @@ -183,6 +183,17 @@ pub extern "C" fn wasmtime_module_serialize( handle_result(module.module.serialize(), |buf| ret.set_buffer(buf)) } +#[no_mangle] +pub extern "C" fn wasmtime_module_image_range( + module: &wasmtime_module_t, + start: &mut usize, + end: &mut usize, +) { + let range = module.module.image_range(); + *start = range.start; + *end = range.end; +} + #[no_mangle] pub unsafe extern "C" fn wasmtime_module_deserialize( engine: &wasm_engine_t,