Skip to content

Commit 26bf93a

Browse files
committed
fix: Protect mem consumption from null deref
- add debug assertions to wasm_get_module_inst_mem_consumption - add debug assertions to aot_get_module_inst_mem_consumption - nullify memories pointer after free in destruction path - nullify memories pointer after free in AOT destruction - docs: add precondition documentation for memory consumption functions
1 parent f0aa4e8 commit 26bf93a

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ samples/workload/include/**
4444

4545
tests/unit/runtime-common/wasm-apps/main.aot
4646
tests/unit/aot-stack-frame/wasm-apps/test_aot.h
47+
.worktrees/

core/iwasm/aot/aot_runtime.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@ memories_deinstantiate(AOTModuleInstance *module_inst)
966966
}
967967
}
968968
wasm_runtime_free(module_inst->memories);
969+
module_inst->memories = NULL;
969970
}
970971

971972
static AOTMemoryInstance *
@@ -3795,13 +3796,32 @@ aot_get_module_mem_consumption(const AOTModule *module,
37953796
mem_conspn->total_size += mem_conspn->aot_code_size;
37963797
}
37973798

3799+
/**
3800+
* Calculate memory consumption of an AOT module instance.
3801+
*
3802+
* @param module_inst pointer to a fully initialized AOT module instance
3803+
* @param mem_conspn output structure to store memory consumption details
3804+
*
3805+
* @pre module_inst != NULL
3806+
* @pre module_inst->module != NULL
3807+
* @pre module_inst->e != NULL
3808+
* @pre (module_inst->memory_count == 0) || (module_inst->memories != NULL)
3809+
*
3810+
* In debug builds, these preconditions are validated with bh_assert.
3811+
* In release builds, violating preconditions results in undefined behavior.
3812+
*/
37983813
void
37993814
aot_get_module_inst_mem_consumption(const AOTModuleInstance *module_inst,
38003815
WASMModuleInstMemConsumption *mem_conspn)
38013816
{
38023817
AOTTableInstance *tbl_inst;
38033818
uint32 i;
38043819

3820+
bh_assert(module_inst);
3821+
bh_assert(module_inst->module);
3822+
bh_assert(module_inst->e);
3823+
bh_assert(!module_inst->memory_count || module_inst->memories);
3824+
38053825
memset(mem_conspn, 0, sizeof(*mem_conspn));
38063826

38073827
mem_conspn->module_inst_struct_size = sizeof(AOTModuleInstance);

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3445,9 +3445,11 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
34453445
(WASMModuleInstanceCommon *)module_inst);
34463446
#endif
34473447

3448-
if (module_inst->memory_count > 0)
3448+
if (module_inst->memory_count > 0) {
34493449
memories_deinstantiate(module_inst, module_inst->memories,
34503450
module_inst->memory_count);
3451+
module_inst->memories = NULL;
3452+
}
34513453

34523454
if (module_inst->import_func_ptrs) {
34533455
wasm_runtime_free(module_inst->import_func_ptrs);
@@ -4227,13 +4229,32 @@ wasm_get_module_mem_consumption(const WASMModule *module,
42274229
mem_conspn->total_size += mem_conspn->const_strs_size;
42284230
}
42294231

4232+
/**
4233+
* Calculate memory consumption of a WASM module instance.
4234+
*
4235+
* @param module_inst pointer to a fully initialized WASM module instance
4236+
* @param mem_conspn output structure to store memory consumption details
4237+
*
4238+
* @pre module_inst != NULL
4239+
* @pre module_inst->module != NULL
4240+
* @pre module_inst->e != NULL
4241+
* @pre (module_inst->memory_count == 0) || (module_inst->memories != NULL)
4242+
*
4243+
* In debug builds, these preconditions are validated with bh_assert.
4244+
* In release builds, violating preconditions results in undefined behavior.
4245+
*/
42304246
void
42314247
wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
42324248
WASMModuleInstMemConsumption *mem_conspn)
42334249
{
42344250
uint32 i;
42354251
uint64 size;
42364252

4253+
bh_assert(module_inst);
4254+
bh_assert(module_inst->module);
4255+
bh_assert(module_inst->e);
4256+
bh_assert(!module_inst->memory_count || module_inst->memories);
4257+
42374258
memset(mem_conspn, 0, sizeof(*mem_conspn));
42384259

42394260
mem_conspn->module_inst_struct_size = (uint8 *)module_inst->e

0 commit comments

Comments
 (0)