Allow overriding max memory on module instantiation (#3198)

This PR adds a max_memory_pages parameter to module instantiation APIs,
to allow overriding the max memory defined in the WASM module.

Sticking to the max memory defined in the module is quite limiting when
using shared memory in production. If targeted devices have different
memory constraints, many wasm files have to be generated with different
max memory values. And device constraints may not be known in advance.

Being able to set the max memory value during module instantiation allows
to reuse the same wasm module, e.g. by retrying instantiation with different
max memory value.
This commit is contained in:
Enrico Loparco
2024-03-05 10:53:26 +01:00
committed by GitHub
parent 0e8d949440
commit 7692f32a94
15 changed files with 284 additions and 163 deletions

View File

@ -186,6 +186,16 @@ struct wasm_config_t {
/*TODO: wasi args*/
};
#ifndef INSTANTIATION_ARGS_OPTION_DEFINED
#define INSTANTIATION_ARGS_OPTION_DEFINED
/* WASM module instantiation arguments */
typedef struct InstantiationArgs {
uint32_t default_stack_size;
uint32_t host_managed_heap_size;
uint32_t max_memory_pages;
} InstantiationArgs;
#endif /* INSTANTIATION_ARGS_OPTION_DEFINED */
/*
* by default:
* - mem_alloc_type is Alloc_With_System_Allocator
@ -644,6 +654,12 @@ WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
own wasm_trap_t** trap, const uint32_t stack_size, const uint32_t heap_size
);
// please refer to wasm_runtime_instantiate_ex(...) in core/iwasm/include/wasm_export.h
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args_ex(
wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
own wasm_trap_t** trap, const InstantiationArgs *inst_args
);
WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out);